Skip to content

Treat the kstat control structure as opaque to stop a heap overrun - #3777

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:fix-kstat-ctl-heap-overrun
Oct 7, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:fix-kstat-ctl-heap-overrun

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

The bug

com.sun.jna.platform.unix.solaris.LibKstat.KstatCtl declares the kstat chain as an inline
Structure where C declares a pointer to one:

public int kc_chain_id;
public Kstat kc_chain;   // real type: kstat_t *
public int kc_kd;

JNA lays an inline Structure field out in place, so it computes sizeof(kstat_ctl_t) as 200
bytes where the real struct is 24
:

sizeof(JNA KstatCtl) = 200   [real kstat_ctl_t = 24]
    kc_chain_id @ 0
    kc_chain    @ 8   (184 bytes)
    kc_kd       @ 192

A Structure passed by reference is autoWrite()n before every call and autoRead() after, so
kstat_open() read 176 bytes past the end of libkstat's 24-byte allocation, and every
kstat_chain_update, kstat_lookup, kstat_read and kstat_close wrote 176 bytes past it.
Because the payload is the previous call's snapshot rather than fresh garbage, the effect is to
revert 176 bytes of neighbouring heap to their earlier contents, on every query, for the life of the
process.

Under libumem a 24-byte buffer's 176-byte tail covers about seven other 24-byte buffers in the
same slab, and a freed buffer keeps its free-list link at offset 0. Overwriting one gives the
assertion we have been chasing:

Assertion failed: UMEM_SLAB_MEMBER(sp, buf), file ../common/umem.c, line 1580

The alternative manifestation is a plain SIGSEGV on a half-overwritten pointer, in whatever code
owned the clobbered buffer — JIT, GC, class loading. The crash site is always innocent and
arbitrarily far from any kstat call, which is what made this hard to attribute. A diagnostic detail
worth recording: the corrupt free-list link was repeatedly a short ASCII string, because the overrun
writes back former heap contents, and a short string is the most common thing a 24-byte buffer
held.

#3776 fixed a genuine, separate cause of these crashes (#3775 — a failed dlopen racing another
thread, because illumos keeps dlerror() state per process rather than per thread). The crashes
continued afterward. This is the cause that remained.

The fix

Declare the four kstat_ctl_t * entry points locally with that argument as an opaque Pointer,
which is exactly what the FFM twin's LibKstatFunctions already does with ADDRESS — so the FFM
implementation was never affected
. Kstat itself maps correctly at its real 184 bytes and is
unchanged.

oshi.jna.platform.unix.SolarisLibKstat is marked non-API and can be deleted if the mapping is
fixed upstream.

Verification

Reported upstream as java-native-access/jna#1740.

Measured on a fork-only OpenIndiana harness that loops the oshi-core tests under
LD_PRELOAD_64=libumem.so.1 with no UMEM_DEBUG (the debug allocator hides the bug by changing
buffer sizes and moving the 176-byte window onto different neighbours):

Shards crashing Iterations per shard
Before (3 runs) 3-4 of 6, every run 20
After 0 of 6 95

The baseline works out to roughly a 4% crash rate per iteration, so 6 x 95 clean iterations is
decisive rather than suggestive.

Not verified on Oracle Solaris, which has the same 24-byte kstat_ctl_t.

Closes #3775.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a memory corruption issue on Solaris and illumos that could affect CPU, memory, disk, and network statistics collection.

JNA's LibKstat.KstatCtl declares kc_chain as an inline Kstat structure
where the real kstat_ctl_t has a kstat_t pointer, so JNA computes the
struct as 200 bytes against its real 24. A Structure passed by reference
is auto-written before every call and auto-read after, so kstat_open()
read 176 bytes past the end of libkstat's 24-byte allocation, and every
kstat_chain_update, kstat_lookup, kstat_read, and kstat_close wrote a
stale 176-byte snapshot past it.

The payload is the previous call's snapshot rather than fresh garbage, so
the effect is to revert 176 bytes of neighbouring heap to their earlier
contents. Under libumem a 24-byte buffer's tail covers about seven other
buffers in the same slab, and a freed buffer keeps its free-list link at
offset 0, which is the UMEM_SLAB_MEMBER assertion failure in
umem_slab_alloc seen on OpenIndiana. The alternative manifestation is a
SIGSEGV on a half-overwritten pointer, arbitrarily far from any kstat
call. #3776 removed a different cause of these crashes; this is the one
that remained.

Declare the four kstat_ctl_t * entry points locally with that argument as
an opaque Pointer, which is what the FFM twin's LibKstatFunctions already
does, so the FFM implementation was never affected. Kstat itself maps
correctly at its real 184 bytes and is unchanged.

Reported upstream as java-native-access/jna#1740; this class can go away
if the mapping is fixed there.
@coderabbitai

coderabbitai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dc804f37-d070-4fdc-9ebb-408d4fa0bc29
📥 Commits

Reviewing files that changed from the base of the PR and between d41ce93 and 33f4010.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • oshi-core/src/main/java/oshi/jna/platform/unix/SolarisLibKstat.java
  • oshi-core/src/main/java/oshi/util/platform/unix/solaris/KstatUtil.java

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📜 Recent review details
⏰ Context from checks skipped due to timeout. (13)
  • GitHub Check: Test JDK 25, FreeBSD
  • GitHub Check: Test JDK 25, OpenIndiana
  • GitHub Check: Test JDK 25, OpenBSD
  • GitHub Check: Test JDK 25, NetBSD
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Test JDK 25, ubuntu-latest
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Lint (Spotless, Checkstyle)
  • GitHub Check: Lint (ForbiddenAPIs, Javadoc, SLF4J)
  • GitHub Check: Error Prone
  • GitHub Check: Analyze (java)
🧰 Additional context used
📚 Code guidelines (1)
CONTRIBUTING.md — auto-discovered
📓 Path-based instructions (1)
Source excerpt: The Changelog lets users know what's changed.

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Files:

  • CHANGELOG.md
🔇 Additional comments (3)
oshi-core/src/main/java/oshi/jna/platform/unix/SolarisLibKstat.java (1)

1-103: LGTM!

oshi-core/src/main/java/oshi/util/platform/unix/solaris/KstatUtil.java (1)

31-31: LGTM!

Also applies to: 47-49, 58-85, 100-130, 141-151, 168-174, 187-199, 225-237

CHANGELOG.md (1)

9-9: LGTM!


📝 Walkthrough

Walkthrough

The change adds a Solaris-specific JNA interface that represents kstat control handles as opaque pointers, then routes KstatUtil operations through it. Existing lookup filtering, retry, validation, and conversion behavior remain unchanged. The changelog records the reported heap overwrite.

Changes

Solaris kstat control-handle mapping

Layer / File(s) Summary
Opaque control handle and kstat operations
oshi-core/src/main/java/oshi/jna/platform/unix/SolarisLibKstat.java, oshi-core/src/main/java/oshi/util/platform/unix/solaris/KstatUtil.java, CHANGELOG.md
A new JNA interface declares kstat operations with opaque control-handle pointers. KstatUtil uses the pointer for chain setup, reads, and updates. The changelog describes the reported overwrite.
Lookup and data access routing
oshi-core/src/main/java/oshi/util/platform/unix/solaris/KstatUtil.java
Lookup and data lookup calls now use SolarisLibKstat. Existing filtering and value conversion remain unchanged.

Priority: ⬆️ High

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: High

Merge Risk: ⚪ Minimal · up to 33f40

No actionable issue is established that would prevent merging. Oracle Solaris has not been tested.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning Issue #3775 directly requires shared library-presence handling and optional-load wrappers for Solaris/illumos, with libcups and libkstat2 routed through those wrappers. The reviewed changes add `S… Implement the shared presence helper and the required JNA/FFM optional-library wrappers from #3775, then route libcups and libkstat2 through them. Alternatively, update the active issue requirements to document the separate kstat-overru…
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: treating the kstat control structure as opaque to prevent the heap overrun.
Out of Scope Changes check ✅ Passed The added non-API SolarisLibKstat mapping and the KstatUtil pointer changes directly support the PR's kstat heap-overrun fix. The changelog entry documents that same fix. No unrelated change is es…
Docstring Coverage ✅ Passed Docstring coverage is 86.67% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 2 files. (1 skipped: 1 …
Full details: Linked Issues check

Explanation

Issue #3775 directly requires shared library-presence handling and optional-load wrappers for Solaris/illumos, with libcups and libkstat2 routed through those wrappers. The reviewed changes add SolarisLibKstat and change KstatUtil to pass an opaque kstat pointer. They do not add the required helper or wrappers, and they do not change the optional libcups or libkstat2 load paths. The kstat mapping fix addresses a separate heap-overrun cause, but it does not implement the active issue's stated coding requirements.

Resolution

Implement the shared presence helper and the required JNA/FFM optional-library wrappers from #3775, then route libcups and libkstat2 through them. Alternatively, update the active issue requirements to document the separate kstat-overrun resolution before treating this PR as complete.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the kstat trail,
With pointers safe and mappings pale.
The chain is read, the lookups flow,
No inline handle gets to grow.
I hop through stats, then rest below.

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 80.28%. Comparing base (d41ce93) to head (33f4010).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ava/oshi/util/platform/unix/solaris/KstatUtil.java 90.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3777      +/-   ##
============================================
- Coverage     80.34%   80.28%   -0.07%     
- Complexity      245      369     +124     
============================================
  Files           722      722              
  Lines         25917    25917              
  Branches       4376     4376              
============================================
- Hits          20824    20807      -17     
- Misses         2857     2867      +10     
- Partials       2236     2243       +7     
Flag Coverage Δ
freebsd 36.42% <0.00%> (+<0.01%) ⬆️
linux 40.20% <0.00%> (+<0.01%) ⬆️
macos 41.04% <0.00%> (-0.04%) ⬇️
netbsd 35.14% <0.00%> (+0.01%) ⬆️
openbsd 35.66% <0.00%> (ø)
solaris 37.02% <90.00%> (ø)
windows 47.28% <0.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbwiddis
dbwiddis merged commit a85c4ad into oshi:master Oct 7, 2026
20 checks passed
@dbwiddis
dbwiddis deleted the fix-kstat-ctl-heap-overrun branch October 7, 2026 20:55
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.

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

1 participant