Skip to content

Conversation

@21Joakim
Copy link
Contributor

@21Joakim 21Joakim commented Feb 5, 2026

The Linker.nativeLinker() doesn't take into account LD_PRELOAD which means if we change the allocator to something like jemalloc or tcmalloc it will not get the correct free function causing it to segfault when called. This PR instead makes use of dlsym with RTLD_DEFAULT to get the correct function pointer.

MemorySegment.NULL (or 0) is the same as RTLD_DEFAULT which according to https://man7.org/linux/man-pages/man3/dlsym.3.html

Find the first occurrence of the desired symbol using the default shared object search order. The search will include global symbols in the executable and its dependencies, as well as symbols in shared objects that were dynamically loaded with the RTLD_GLOBAL flag.

This is the same way Java gets the pointer (see the trace below), except they call it with the handle to libsyslookup.so instead of RTLD_DEFAULT which means we don't get the actual free function that will be called.

  1. https://github.com/openjdk/jdk25u/blob/5a27cfef3bde7356c2d20057393ed52786815bc8/src/java.base/share/classes/jdk/internal/foreign/SystemLookup.java#L118
  2. https://github.com/openjdk/jdk25u/blob/5a27cfef3bde7356c2d20057393ed52786815bc8/src/java.base/share/native/libjava/NativeLibraries.c#L224
  3. https://github.com/openjdk/jdk25u/blob/5a27cfef3bde7356c2d20057393ed52786815bc8/src/hotspot/share/prims/jvm.cpp#L3233
  4. https://github.com/openjdk/jdk25u/blob/5a27cfef3bde7356c2d20057393ed52786815bc8/src/hotspot/os/posix/os_posix.cpp#L829
    I skipped a few less important steps in the trace just to keep it concise

@MinnDevelopment MinnDevelopment merged commit 671df98 into MinnDevelopment:master Feb 7, 2026
8 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants