Repository navigation
Run NativeFreeComparisonTest on NetBSD, and report an unknown current PID or TID as 0 everywhere - #3754
Conversation
The native-free provider supports Linux and NetBSD, but the comparison test was gated to Linux, so its NetBSD code was never compared against the native path. It now runs on both; the comparison is meaningful on NetBSD only where pkgsrc java-jna supplies libjnidispatch, which the JDK 25 NetBSD CI job installs. - The two Linux-only methods (getHz, getPageSize via a cast to LinuxOperatingSystem) keep their own Linux gate. - currentProcess re-reads a snapshot taken mid-posix_spawn, the same degraded-row check NativeComparisonTest uses, and compares start times within the seconds-resolution ps tolerance on NetBSD. - The thread-ID assertion moves to the end of operatingSystemInfo so a mismatch cannot hide the assertions before it.
Without JNA or FFM, NetBsdOperatingSystem.getThreadId() returned Java's Thread.getId(), a different numbering from the kernel LWP ID the native paths return. OperatingSystem.getThreadId() promises the OS thread ID or 0 when unknown, and getCurrentThread() matches ps rows against this value, so a Java ID that happened to equal another LWP's ID picked the wrong thread. Return 0 instead; getCurrentThread() then falls back to the same stub thread the other BSDs use when the ID does not match. Found by running NativeFreeComparisonTest on NetBSD (expected 4156 from _lwp_self, got 26 from Thread.getId).
OperatingSystem.getThreadId() documents 0 when the ID is unknown, and most implementations of getThreadId() and getProcessId() return 0 when their native call fails. Five did not: - NetBsdOperatingSystem.getProcessId() (native-free) returned -1 when the JVM name could not be parsed. - NetBsdOperatingSystemFFM returned -1 from both getpid and _lwp_self. - WindowsOperatingSystemFFM returned -1 from both GetCurrentProcessId and GetCurrentThreadId. getProcessId() now documents the same 0-when-unknown contract, and these return 0. A -1 was worse than a wrong number: the BSD, Solaris and AIX process queries treat a negative PID as "all processes", so getCurrentProcess() would have returned whichever process came first. getCurrentProcess() now returns its stand-in without a lookup when the PID is unknown, since PID 0 is a real kernel or idle process on most platforms. SolarisOperatingSystem and AixOperatingSystem.getProcess(int) also reject a negative PID, as BsdOperatingSystem already did; a caller passing one got an arbitrary process back.
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (11)
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. (6)
🧰 Additional context used📓 Path-based instructions (4)`oshi-core-ffm` may not reference `com.sun.jna.**`.📄 CodeRabbit inference engine (AGENTS.md) Files:
`oshi-common` may not reference `com.sun.jna.**`, `java.lang.foreign.**`, or any `System.load`/`loadLibrary`.📄 CodeRabbit inference engine (AGENTS.md) Files:
Test files are named `*Test.java`.📄 CodeRabbit inference engine (AGENTS.md) Files:
Add a `CHANGELOG.md` entry under *Next Release* **only for user-visible changes**.📄 CodeRabbit inference engine (AGENTS.md) Files:
🧠 Learnings (1)📚 Learning: 2026-08-15T15:43:29.296ZApplied to files:
📝 WalkthroughWalkthroughUnknown process and thread IDs now use zero in the changed implementations. Current-process lookup avoids querying nonpositive PIDs, and AIX and Solaris return null for negative process lookups. Native-free comparisons now include NetBSD with platform-specific checks. ChangesProcess identity and lookup
Priority: ⬇️ Low Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Bug fix Merge Risk: ⚪ Minimal · up to No merge-blocking issue was identified; the NetBSD comparison fails with an assertion if a process snapshot remains unavailable. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The changed lookup path avoids treating an unknown process ID as a real process. No introduced security exposure is established, but the behavior of the NetBSD current-thread fallback remains uncertain. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 24.32% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 37 functions across 10 files. (1 skipped: 1 unsupported.)
✨ 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 process IDs at dawn Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3754 +/- ##
============================================
+ Coverage 80.30% 80.32% +0.01%
- Complexity 83 218 +135
============================================
Files 717 717
Lines 25662 25666 +4
Branches 4335 4336 +1
============================================
+ Hits 20608 20616 +8
Misses 2833 2833
+ Partials 2221 2217 -4
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:
|
NativeFreeComparisonTest on NetBSD
The native-free provider supports Linux and NetBSD, but its comparison test ran only on Linux. It now runs on both. On NetBSD it is meaningful only where pkgsrc
java-jnasupplieslibjnidispatch, which the JDK 25 NetBSD CI job installs; otherwise JNA falls back to the same command-line code.NetBSD has three providers, and the two comparison tests share JNA as their baseline:
NativeComparisonTestchecks FFM against JNA, andNativeFreeComparisonTestchecks native-free against JNA. #3745 added NetBSD toNativeComparisonTestalong with the FFM port, so FFM was already covered; native-free was the provider left uncompared. Before #3433 gave NetBSD's JNA provider a native path throughjava-jna, the NetBSD JNA classes were the command-line code the native-free provider uses, so the comparison would have checked a class against itself. With both tests running, all three providers are compared on Linux and NetBSD.getHz/getPageSize(a cast toLinuxOperatingSystem) keep a Linux-only gate.currentProcessre-reads a snapshot taken mid-posix_spawn, the same degraded-row checkNativeComparisonTestuses, and allows the seconds-resolutionpsstart-time tolerance on NetBSD.The first NetBSD run passed every comparison except one: the current thread ID (JNA
4156from_lwp_self, native-free26fromThread.getId()).Unknown current process and thread IDs
OperatingSystem.getThreadId()documents 0 when the ID is unknown, and most implementations of it and ofgetProcessId()already return 0 on failure. Five did not:NetBsdOperatingSystem.getThreadId()(native-free / no JNA)Thread.getId()NetBsdOperatingSystem.getProcessId()NetBsdOperatingSystemFFMgetProcessId()/getThreadId()WindowsOperatingSystemFFMgetProcessId()/getThreadId()getProcessId()now documents the same 0-when-unknown contract.Following those values into their callers turned up two more bugs of the same kind:
getCurrentProcess()returned whichever process came first. It now returns its stand-in without a lookup when the PID is unknown; it cannot look up 0 instead, since PID 0 is a real kernel or idle process on most platforms.SolarisOperatingSystemandAixOperatingSystem.getProcess(int)returned an arbitrary process for any negative PID. They now return null, asBsdOperatingSystemalready did.A Java thread ID was also unsafe in
NetBsdOperatingSystem.getCurrentThread(), which matchespsLWP rows against it: a collision picked another thread. With 0 it falls back to the same stub thread the other BSDs use.Tests
OperatingSystemTest(oshi-common):getCurrentProcess()never looks up an unknown PID.getProcess(-1) == nullon every platform.Verification
-Perror-prone,aggregate-coverage.Summary by CodeRabbit
0across platforms.