llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Fix ORC runtime ELF atexit implementation.

Open lhames opened this issue 3 years ago • 3 comments

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.

lhames avatar Dec 06 '23 18:12 lhames

@llvm/issue-subscribers-orcjit

Author: None (lhames)

The current Linux 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 avatar Dec 06 '23 18:12 llvmbot

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.

lhames avatar Dec 06 '23 20:12 lhames

request to be assigned

sonar3d avatar Apr 13 '25 07:04 sonar3d