llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.69k stars 11.87k forks source link

Fix ORC runtime ELF atexit implementation. #74641

Open lhames opened 10 months ago

lhames commented 10 months ago

The current ELF atexit implementation in the ORC runtime (as of 7b83f69db4a) is:

int __orc_rt_elfnix_atexit(void (*func)(void *)) {
  auto &PlatformRTState = ELFNixPlatformRuntimeState::get();
  return ELFNixPlatformRuntimeState::get().registerAtExit(
      func, NULL, PlatformRTState.getPlatformJDDSOHandle());
}

But there's no reason to assume that func is in the Platform JITDylib, the common case is that it will not be (now that both LLJIT and the llvm-jitlink tool place the ORC runtime code in a separate JITDylib from the "main" code by default).

A better implementation would be:

int __orc_rt_elfnix_atexit(void (*func)(void *)) {
  auto &PlatformRTState = ELFNixPlatformRuntimeState::get();
  Dl_info Info;
  if (!dladdr(func, &Info))
    return -1; // Could not identify address. Error out.
  return ELFNixPlatformRuntimeState::get().registerAtExit(
      func, NULL, Info.dli_fbase);
}

but we'll need a JIT dladdr implementation before we can do that.

llvmbot commented 10 months ago

@llvm/issue-subscribers-orcjit

Author: None (lhames)

The current Linux atexit implementation in the ORC runtime (as of 7b83f69db4a) is: ```c++ int __orc_rt_elfnix_atexit(void (*func)(void *)) { auto &PlatformRTState = ELFNixPlatformRuntimeState::get(); return ELFNixPlatformRuntimeState::get().registerAtExit( func, NULL, PlatformRTState.getPlatformJDDSOHandle()); } ``` But there's no reason to assume that `func` is in the `Platform` JITDylib, the common case is that it will not be (now that both `LLJIT` and the `llvm-jitlink` tool place the ORC runtime code in a separate JITDylib from the "main" code by default). A better implementation would be: ```c++ int __orc_rt_elfnix_atexit(void (*func)(void *)) { auto &PlatformRTState = ELFNixPlatformRuntimeState::get(); Dl_info Info; if (!dladdr(func, &Info)) return -1; // Could not identify address. Error out. return ELFNixPlatformRuntimeState::get().registerAtExit( func, NULL, Info.dli_fbase); } ``` but we'll need a JIT `dladdr` implementation before we can do that.
lhames commented 10 months ago

Commit b4e19155171e disabled the compiler-rt/test/orc/TestCases/Linux/ppc64/trivial-atexit.S and compiler-rt/test/orc/TestCases/Linux/x86-64/trivial-atexit.S tests due to this issue. Those tests should be re-enabled once this issue is fixed.