Repository navigation
Treat the kstat control structure as opaque to stop a heap overrun - #3777
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (3)
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)
🧰 Additional context used📚 Code guidelines (1)📓 Path-based instructions (1)Source excerpt: The Changelog lets users know what's changed.📄 CodeRabbit inference engine (CONTRIBUTING.md) Files:
🔇 Additional comments (3)
📝 WalkthroughWalkthroughThe 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. ChangesSolaris kstat control-handle mapping
Priority: ⬆️ High Estimated code review effort: 2 (Simple) | ~12 minutes Change: Bug fix · Severity of issue fixed: High Merge Risk: ⚪ Minimal · up to No actionable issue is established that would prevent merging. Oracle Solaris has not been tested. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Linked Issues checkExplanation Issue Resolution Implement the shared presence helper and the required JNA/FFM optional-library wrappers from
✨ Finishing Touches🧪 Generate unit tests (beta)
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. A rabbit checks the kstat trail, Comment |
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The bug
com.sun.jna.platform.unix.solaris.LibKstat.KstatCtldeclares the kstat chain as an inlineStructurewhere C declares a pointer to one:JNA lays an inline
Structurefield out in place, so it computessizeof(kstat_ctl_t)as 200bytes where the real struct is 24:
A
Structurepassed by reference isautoWrite()n before every call andautoRead()after, sokstat_open()read 176 bytes past the end of libkstat's 24-byte allocation, and everykstat_chain_update,kstat_lookup,kstat_readandkstat_closewrote 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:
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
dlopenracing anotherthread, because illumos keeps
dlerror()state per process rather than per thread). The crashescontinued afterward. This is the cause that remained.
The fix
Declare the four
kstat_ctl_t *entry points locally with that argument as an opaquePointer,which is exactly what the FFM twin's
LibKstatFunctionsalready does withADDRESS— so the FFMimplementation was never affected.
Kstatitself maps correctly at its real 184 bytes and isunchanged.
oshi.jna.platform.unix.SolarisLibKstatis marked non-API and can be deleted if the mapping isfixed upstream.
Verification
Reported upstream as java-native-access/jna#1740.
Measured on a fork-only OpenIndiana harness that loops the
oshi-coretests underLD_PRELOAD_64=libumem.so.1with noUMEM_DEBUG(the debug allocator hides the bug by changingbuffer sizes and moving the 176-byte window onto different neighbours):
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