Control: tags -1 - moreinfo

Hi Aron,

Thanks. I don't think the LLVM 23 transition will fix this: the problem is
that libtorch_cpu exports the symbols of its own, statically linked copy of
LLVM, so any process that also loads a shared libLLVM ends up with two LLVM
instances whose global objects get mixed up. Matching versions doesn't
change that.

To check, I loaded libLLVM.so.23.1 (the same version libtorch uses) with
dlopen(RTLD_NOW|RTLD_LOCAL), as a Vulkan driver's dependencies are loaded,
into a small C program, called LLVMParseCommandLineOptions and
LLVMGetDefaultTargetTriple, and exited:

  without libtorch:                    exit 0
  linked with libtorch_cpu (2.15.0):   free(): double free detected in tcache 2
                                       Aborted (exit 134), at process exit

The same happens with libLLVM.so.22.1. The double free is in libtorch's own
exit-time destructors:

  #7  tcache_double_free_verify
  #8  libtorch_cpu.so.2.15
  #9  __cxa_finalize
  #10 libtorch_cpu.so.2.15
  #12 _dl_call_fini
  #13 _dl_fini

So once mesa moves to LLVM 23 (#1155413), C++ programs using libtorch's
Vulkan backend under RADV or llvmpipe will still crash, at least at exit.
Python is unaffected because it loads libtorch with RTLD_LOCAL.

A fix would be to stop libtorch_cpu from exporting the LLVM symbols, for
example by linking the LLVM static libraries with -Wl,--exclude-libs,ALL,
or with a version script that exports only libtorch's own API.

The reproducer (one C file and a script) is attached.

On Mon, Nov 30, 2026 at 12:51 AM Aron Xu <[email protected]> wrote:
>
> Control: tags -1 + moreinfo
>
> Hi,
>
> Building with llvm 23 is proposed by the LLVM maintainers (Bug
> #1155394), and mesa has an open bug for it (Bug #1155413), too.
>
> Regards,
> Aron

Attachment: run.sh
Description: application/shellscript

/* dup.c: load a shared libLLVM privately (as a Vulkan driver's dependencies are) into a
   process, call into its global state, and exit. Built with and without libtorch. */
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
typedef void (*parse_t)(int, const char * const *, const char *);
typedef char * (*triple_t)(void);
typedef void (*dispose_t)(char *);
int main(int argc, char **argv) {
  const char *lib = argc > 1 ? argv[1] : "libLLVM.so.23.1";
  void *h = dlopen(lib, RTLD_NOW | RTLD_LOCAL);
  if (!h) { printf("dlopen: %s\n", dlerror()); return 1; }
  printf("loaded %s\n", lib); fflush(stdout);
  parse_t parse = (parse_t)dlsym(h, "LLVMParseCommandLineOptions");
  const char *args[] = { "dup", "-time-passes" };
  parse(2, args, "test");
  printf("parsed an option\n"); fflush(stdout);
  char *t = ((triple_t)dlsym(h, "LLVMGetDefaultTargetTriple"))();
  printf("default triple: %s\n", t); fflush(stdout);
  ((dispose_t)dlsym(h, "LLVMDisposeMessage"))(t);
  printf("exiting\n"); fflush(stdout);
  return 0;
}

Reply via email to