Repository navigation
Description
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 afterSystemInfoTeststarted. - A fork-only loop running the
oshi-coretests repeatedly on OpenIndiana crashed 4 times in about 80 runs. Two crashes were a SIGSEGV instrlen, inside JNA'sNative.open, called fromCups.<clinit><-CupsPrinterJNA.<clinit><-SolarisHardwareAbstractionLayerJNA.getPrinters(). The other two were damagedmallocfree lists inlibumem, holding text where a pointer should be. - With
UMEM_DEBUG=defaultthe 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
dlopenordlsymlike 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 globallasterrpointer into a single static, wrapping message buffer (usr/src/cmd/sgs/rtld/common/dlfcns.candutil.c).dlerror()returns that pointer and then clears it. Any faileddlopenordlsymon any thread, including the JVM's own lookups, rewrites the buffer. - So when two failures overlap:
dlerror()can return NULL, andstrlen(NULL)crashes.- The message can change between
strlenandstrcpy, sostrcpywrites past the end of themalloc'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
- Move the file check into
oshi-commonas one shared helper. It takes the library name prefixes, searches/lib/64,/usr/lib/64,/lib,/usr/libandLD_LIBRARY_PATH_64/LD_LIBRARY_PATH, and reports whether the library is present. Bothlibkstat2Present()copies are replaced by it. - 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. - Route
libcups(JNA and FFM) andlibkstat2(JNA and FFM) through the wrappers. - Keep
PrinterTestand 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.
Activity
Check that optional native libraries exist before loading them on ill…