Skip to content

JVM crash on illumos when an optional native library (libcups) fails to load concurrently #3775

Closed

Description

@dbwiddis

Summary

On illumos (and probably Solaris), a failed attempt to load an optional native library can crash the JVM when another thread is loading or looking up native code at the same time. OSHI triggers this when it tries to load libcups on a system without CUPS. This is the cause of the intermittent OpenIndiana CI crashes (a SIGSEGV in libumem, a SIGSEGV in strlen, or a JVM "Internal Error"), and it can affect users too.

Evidence

  • OpenIndiana CI jobs have crashed about 2.5% of the time since August (2026-08-19, 2026-08-23, 2026-10-03, and twice on Run pvs through the Linux privileged prefix #3774). Every crash with full output came from the oshi-core (JNA) test JVM, right after SystemInfoTest started.
  • A fork-only loop running the oshi-core tests repeatedly on OpenIndiana crashed 4 times in about 80 runs. Two crashes were a SIGSEGV in strlen, inside JNA's Native.open, called from Cups.<clinit> <- CupsPrinterJNA.<clinit> <- SolarisHardwareAbstractionLayerJNA.getPrinters(). The other two were damaged malloc free lists in libumem, holding text where a pointer should be.
  • With UMEM_DEBUG=default the same loop ran 120 times without a crash. The debug allocator changes memory layout and timing enough to hide it.

Root cause

  • JNA builds its error message after a failed dlopen or dlsym like this (native/dispatch.c, LOAD_ERROR(), 5.19.1):
    const char* message = dlerror();
    char* buf = (char*) malloc(strlen(message) + 1);
    strcpy(buf, message);
  • On illumos, dlerror() state is shared by the whole process, not kept per thread. The runtime linker keeps one global lasterr pointer into a single static, wrapping message buffer (usr/src/cmd/sgs/rtld/common/dlfcns.c and util.c). dlerror() returns that pointer and then clears it. Any failed dlopen or dlsym on any thread, including the JVM's own lookups, rewrites the buffer.
  • So when two failures overlap:
    • dlerror() can return NULL, and strlen(NULL) crashes.
    • The message can change between strlen and strcpy, so strcpy writes past the end of the malloc'd buffer. That corrupts the heap, and the crash then lands on whichever thread allocates next.
  • glibc keeps dlerror() state per thread, so Linux is unaffected. I believe macOS and the BSDs are too, but I haven't checked their source. AIX is unknown.

This has been hit before. libkstat2Present() was added to both SolarisOperatingSystemJNA and Kstat2Functions in #3338 because a failed Native.load of libkstat2 "has been seen to SIGSEGV in libc strlen". The fix was to check for the file on disk before loading, and that check is the right approach. It just only covers libkstat2.

Inventory of native library loads

Libraries that are always present on their platform don't need any change:

  • Solaris/illumos: libc, libkstat (JNA and FFM)
  • AIX: libc, libperfstat (FFM loads the 64-bit archive member first, which succeeds on a 64-bit JVM)
  • macOS: system frameworks
  • Windows: system DLLs

Optional loads that can fail:

Library Platforms JNA FFM Guarded today?
libkstat2 Solaris 11.4+ only; absent on illumos and older Solaris SolarisOperatingSystemJNA static init Kstat2Functions static init Yes, by libkstat2Present(), duplicated in both twins
libcups All UNIX, including Solaris, illumos and AIX CupsPrinterJNA static init, via Cups.INSTANCE CupsFunctions static init, via libraryLookup("cups") No. It relies on catching UnsatisfiedLinkError, but the crash happens inside JNA before Java gets an exception.
libsystemd, libudev Linux only Systemd, Udev SystemdFunctions, UdevFunctions Not needed: glibc keeps dlerror() state per thread
libnvidia-ml / nvml, atiadlxx / atiadlxy Linux, Windows NvmlUtilJNA, AdlUtilJNA NvmlFunctions, AdlUtilFFM Not needed, for the same reason
IOReport, dxgi macOS, Windows Not needed

On Solaris/illumos, CUPS is the only unguarded optional load. Display information on UNIX runs the xrandr command rather than loading X11.

Failed symbol lookups go through the same LOAD_ERROR(). FFM's Solaris and AIX symbols are all resolved with findOrThrow on libraries that are always present, and the CUPS symbols are only looked up after libcups loads. JNA binds each interface method the first time it is called, so a Solaris 11-only function called on illumos would also go through LOAD_ERROR(). I haven't checked that statically.

Proposed fix

  1. Move the file check into oshi-common as one shared helper. It takes the library name prefixes, searches /lib/64, /usr/lib/64, /lib, /usr/lib and LD_LIBRARY_PATH_64 / LD_LIBRARY_PATH, and reports whether the library is present. Both libkstat2Present() copies are replaced by it.
  2. Add thin JNA and FFM wrappers for optional libraries. Each one calls the shared check on Solaris/illumos (and AIX, if its dlerror() turns out to be process-wide), skips the load when the file is absent, and otherwise loads and handles failure as today. On other platforms they load directly, so a wrong "absent" can't cost a working feature where it isn't needed.
  3. Route libcups (JNA and FFM) and libkstat2 (JNA and FFM) through the wrappers.
  4. Keep PrinterTest and the parallel test execution on OpenIndiana as they are, since they are what exposed the bug.

To verify: run the fork loop against the fix branch without UMEM_DEBUG. The current rate is 4 crashes in about 80 runs, so zero crashes in around 120 runs is the result to look for.

Upstream

A fix in JNA's LOAD_ERROR() will be tracked separately: a NULL check plus a bounded copy. It would make JNA safe against failed lookups from other code, including the JVM, which OSHI can't prevent. The OSHI change is still worth doing, because it avoids the failed loads entirely and doesn't need a new JNA release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions