Skip to content

Migrate remaining mac OperatingSystem JNA to FFM - #3104

Merged
dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:mac-os
Mar 29, 2026
Merged

dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:mac-os

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Mar 29, 2026 •

Copy link
Copy Markdown
Member

This PR completes the FFM migration of all classes called from MacOperatingSystemFFM, eliminating all remaining JNA dependencies from the macOS OperatingSystem interface implementation.

Changes

New FFM bindings (oshi.ffm.mac)

  • CoreGraphicsFunctions — downcall handles for CGWindowListCopyWindowInfo and CGRectMakeWithDictionaryRepresentation
  • MacSystem — added structs and constants:
  • UTMPX with correct alignment padding for tv_sec
  • ADDRINFO for getaddrinfo result traversal
  • CG_POINT, CG_SIZE, CG_RECT using JAVA_DOUBLE for CGFloat fields
  • Constants: LOGIN_PROCESS, USER_PROCESS, AI_CANONNAME, HOST_NAME_MAX
  • MacSystemFunctions — added setutxent, getutxent, endutxent, gethostname, getaddrinfo, freeaddrinfo, gai_strerror

New driver classes (oshi.driver.mac)

  • WhoFFM — replaces Who.queryUtxent() using FFM utxent bindings; falls back to unix.Who.queryWho() on invalid sessions or errors
  • WindowInfoFFM — replaces WindowInfo.queryDesktopWindows() using FFM CoreGraphics bindings

New OS classes (oshi.software.os.mac)

  • MacNetworkParamsFFM — replaces MacNetworkParams; implements getDomainName() via getaddrinfo and getHostName() via gethostname; gateway methods retain the existing ExecutingCommand approach as macOS has no suitable syscall alternative
  • MacOSFileStoreFFM — mirrors MacOSFileStore but updateAttributes() delegates to MacFileSystemFFM.getFileStoreMatching() instead of the JNA-based MacFileSystem

Modified

  • MacFileSystemFFM — instantiates MacOSFileStoreFFM instead of MacOSFileStore so the full file store lifecycle is JNA-free
  • MacOperatingSystemFFM — wired up all new classes; removed three // TODO: migrate comments

Summary by CodeRabbit

  • Bug Fixes / Improvements
    • Improved macOS session listing, network parameter detection, and desktop window enumeration for greater reliability and accuracy.
    • Updated filesystem and process attribute handling to provide more consistent names, paths, and storage metrics.
    • Minor changelog and tooling cleanups related to macOS native integration.

@coderabbitai

coderabbitai Bot commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: dc615c71-35d1-40d6-9458-a23cd8867175

📥 Commits

Reviewing files that changed from the base of the PR and between 0671786 and 6b856e9.

📒 Files selected for processing (2)
  • oshi-core-java25/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core-java25/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • oshi-core-java25/src/main/java/oshi/driver/mac/WhoFFM.java
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Analyze (java)
🔇 Additional comments (3)
oshi-core-java25/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java (3)

14-15: LGTM!

Correct imports for the macOS kernel-defined constants used to bound native string extraction.


487-490: LGTM!

Correctly bounds the pbi_comm slice to MAXCOMLEN (16 bytes) before getString(), matching the struct field size defined in MacSystem.PROC_BSD_INFO. This prevents reading beyond the field boundary.


567-568: LGTM!

Correctly bounds the vip_path slice to MAXPATHLEN (1024 bytes) before getString(), matching the struct field size defined in MacSystem.VNODE_INFO_PATH. This prevents reading beyond the field boundary in the nested slice access.


📝 Walkthrough

Walkthrough

This PR continues migrating macOS platform code from JNA to the Java FFM API. It adds FFM bindings and macOS-specific structs, introduces FFM-based drivers for sessions, window enumeration, network params, and file-store representations, and wires these FFM drivers into the macOS OS layer.

Changes

Cohort / File(s) Summary
Changelog & Checkstyle
CHANGELOG.md, config/checkstyle-suppressions.xml
Added changelog entry for migration; updated Checkstyle suppression from TodoComment on old file to ConstantName for WindowInfoFFM.
FFM Constants & Layouts
oshi-core-java25/src/main/java/oshi/ffm/mac/MacSystem.java
Added constants (LOGIN_PROCESS, USER_PROCESS, AI_CANONNAME, HOST_NAME_MAX) and new struct/layout declarations (UTMPX, ADDRINFO, CG_POINT/CG_SIZE/CG_RECT) for macOS FFM interop.
FFM Native Bindings
oshi-core-java25/src/main/java/oshi/ffm/mac/MacSystemFunctions.java, oshi-core-java25/src/main/java/oshi/ffm/mac/CoreGraphicsFunctions.java
Introduced libSystem and CoreGraphics downcalls and public wrappers: utxent/getutxent/endutxent, gethostname, getaddrinfo/freeaddrinfo/gai_strerror, CGWindowListCopyWindowInfo, CGRectMakeWithDictionaryRepresentation.
macOS FFM Drivers
oshi-core-java25/src/main/java/oshi/driver/mac/WhoFFM.java, oshi-core-java25/src/main/java/oshi/driver/mac/WindowInfoFFM.java
Added WhoFFM to iterate getutxent and produce OSSession list (with fallback to Unix Who), and WindowInfoFFM to enumerate desktop windows via CoreGraphics/CoreFoundation (visibility, bounds, owner info).
Network & File-store FFM Implementations
oshi-core-java25/src/main/java/oshi/software/os/mac/MacNetworkParamsFFM.java, oshi-core-java25/src/main/java/oshi/software/os/mac/MacOSFileStoreFFM.java
Added MacNetworkParamsFFM (getDomainName via getaddrinfo, getHostName via gethostname, gateway parsing via commands) and MacOSFileStoreFFM (FFM-backed OSFileStore with attribute update from MacFileSystemFFM).
Integration & Minor updates
oshi-core-java25/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java, oshi-core-java25/src/main/java/oshi/software/os/mac/MacOperatingSystemFFM.java, oshi-core-java25/src/main/java/oshi/software/os/mac/MacOSProcessFFM.java
MacFileSystemFFM now constructs MacOSFileStoreFFM; MacOperatingSystemFFM wired to WhoFFM, MacNetworkParamsFFM, and WindowInfoFFM; bounds for process string extraction now use MAXCOMLEN/MAXPATHLEN.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant Java as WindowInfoFFM (Java)
participant CoreGraphics as CoreGraphics.framework
participant CoreFoundation as CoreFoundation
participant OS as macOS Kernel
Java->>CoreGraphics: CGWindowListCopyWindowInfo(option, relative)
CoreGraphics-->>Java: CFArrayRef (window dictionaries)
Java->>CoreFoundation: Wrap array, iterate entries, read keys (kCGWindowIsOnscreen, kCGWindowNumber, kCGWindowOwnerPID, kCGWindowBounds, kCGWindowName, kCGWindowOwnerName)
CoreFoundation-->>Java: CFType values / dictionaries
Java->>CoreGraphics: CGRectMakeWithDictionaryRepresentation(boundsDict, rectOut)
CoreGraphics-->>Java: boolean success
Java->>Java: build OSDesktopWindow objects, release CF resources
Java-->>OS: (none directly) — uses native frameworks via FFM

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Possibly related PRs

  • #3057 — Related changes to macOS file-store construction and propagation of isLocal, overlapping file-store instantiation concerns.

Poem

🐰 FFM hops where JNA once played,
native calls leap in memory's glade,
windows and users scurry in line,
gateways found and file stores shine,
this rabbit cheers: the migration's made! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Migrate remaining mac OperatingSystem JNA to FFM' accurately summarizes the main objective of the PR, which is to complete the FFM migration by replacing remaining JNA dependencies in the macOS OperatingSystem implementation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ Simplify code
  • Create PR with simplified code

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

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

@codecov

codecov Bot commented Mar 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.28%. Comparing base (c9dd62d) to head (6b856e9).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3104      +/-   ##
==========================================
+ Coverage   68.69%   69.28%   +0.58%     
==========================================
  Files          36       36              
  Lines        1875     1875              
  Branches      305      305              
==========================================
+ Hits         1288     1299      +11     
+ Misses        492      478      -14     
- Partials       95       98       +3     

☔ View full report in Codecov by Sentry.
📢 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@oshi-core-java25/src/main/java/oshi/driver/mac/WhoFFM.java`:
- Around line 56-58: The code currently calls getString(0) on unbounded slices
(in WhoFFM using UTMPX.byteOffset(UT_USER), UT_LINE, UT_HOST), which can
over-read or throw; replace those calls with a bounded C-string reader: add a
helper (e.g., readCStringField(MemorySegment struct, long offset, int maxLen))
that slices the struct with the fixed field length, reads bytes up to the first
NUL, and returns an ASCII String, then call readCStringField(struct,
UTMPX.byteOffset(UT_USER), USER_FIELD_LEN) etc. so each field is read within its
fixed max length rather than to the end of the struct.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: faaa282b-4843-4aeb-ba51-aa3399e8dd59

📥 Commits

Reviewing files that changed from the base of the PR and between c9dd62d and 0671786.

📒 Files selected for processing (11)
  • CHANGELOG.md
  • config/checkstyle-suppressions.xml
  • oshi-core-java25/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core-java25/src/main/java/oshi/driver/mac/WindowInfoFFM.java
  • oshi-core-java25/src/main/java/oshi/ffm/mac/CoreGraphicsFunctions.java
  • oshi-core-java25/src/main/java/oshi/ffm/mac/MacSystem.java
  • oshi-core-java25/src/main/java/oshi/ffm/mac/MacSystemFunctions.java
  • oshi-core-java25/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java
  • oshi-core-java25/src/main/java/oshi/software/os/mac/MacNetworkParamsFFM.java
  • oshi-core-java25/src/main/java/oshi/software/os/mac/MacOSFileStoreFFM.java
  • oshi-core-java25/src/main/java/oshi/software/os/mac/MacOperatingSystemFFM.java
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2026-03-29T20:01:01.877Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3014
File: oshi-core-java11/src/main/java/module-info.java:10-17
Timestamp: 2026-03-29T20:01:01.877Z
Learning: In the oshi project, `oshi-core-java25/src/main/java/module-info.java` intentionally does NOT export `oshi.util.platform.unix.freebsd`, `oshi.util.platform.unix.openbsd`, or `oshi.util.platform.unix.solaris`, because the FFM (Foreign Function & Memory) implementation only supports Windows, macOS, and Linux. This differs from `oshi-core-java11/src/main/java/module-info.java` which exports all Unix platform packages. The asymmetry is by design.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/software/os/mac/MacOperatingSystemFFM.java
  • oshi-core-java25/src/main/java/oshi/ffm/mac/MacSystemFunctions.java
  • oshi-core-java25/src/main/java/oshi/software/os/mac/MacOSFileStoreFFM.java
  • oshi-core-java25/src/main/java/oshi/ffm/mac/MacSystem.java
📚 Learning: 2026-03-29T20:02:58.470Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3014
File: oshi-core-java25/src/main/java/oshi/util/platform/windows/PdhUtilFFM.java:87-89
Timestamp: 2026-03-29T20:02:58.470Z
Learning: In the OSHI FFM codebase (oshi-core-java25), all PDH/Kernel32/COM wrapper methods in classes like PdhFFM, Kernel32FFM, etc. declare `throws Throwable` solely as a compiler artifact of `MethodHandle.invokeExact`. They are native calls and do not throw Java checked exceptions in practice. The enclosing `getOpenFileDescriptors()` method in PdhUtilFFM already has an outer `catch (Throwable t)` that covers the entire method body including any `finally` blocks, so no exception-masking concern exists for cleanup calls like `PdhCloseQuery` in `finally`.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/ffm/mac/MacSystemFunctions.java
🔇 Additional comments (13)
oshi-core-java25/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java (1)

188-190: FFM file-store instantiation is correctly wired.

Line 188 now constructs MacOSFileStoreFFM with the existing argument flow intact, so file-store creation stays on the FFM path without changing matching behavior.

oshi-core-java25/src/main/java/oshi/software/os/mac/MacOSFileStoreFFM.java (3)

30-42: Constructor field mapping looks consistent and complete.

The super(...) call and subsequent assignments correctly capture all file-store attributes required by this FFM implementation.


44-82: Accessor overrides are clean and consistent.

These getters correctly expose the stored filesystem metadata and capacity/inode values without extra side effects.


85-100: updateAttributes() matching logic preserves existing behavior.

The refresh loop uses the expected name + locality query and then disambiguates on volume/mount before copying updated values, which mirrors the prior macOS file-store strategy.

config/checkstyle-suppressions.xml (1)

33-34: Scoped suppression looks appropriate.

The ConstantName suppression is tightly targeted to WindowInfoFFM.java and matches the CoreGraphics kCG... constant style used in that file.

oshi-core-java25/src/main/java/oshi/ffm/mac/CoreGraphicsFunctions.java (1)

29-50: CoreGraphics FFM bindings are clean and correctly scoped.

The symbol lookup, downcall descriptors, and wrapper methods are consistent and correctly minimal for the two CoreGraphics APIs being introduced.

oshi-core-java25/src/main/java/oshi/ffm/mac/MacSystemFunctions.java (1)

158-223: New libSystem downcalls are consistent with existing FFM wrappers.

The added utmpx and addrinfo-related bindings follow the same invocation/error-handling pattern already used across this class.

oshi-core-java25/src/main/java/oshi/ffm/mac/MacSystem.java (1)

87-407: Struct and constant additions align with new macOS FFM callers.

The new utmpx/addrinfo/CoreGraphics layouts and constants are well-factored and fit the new drivers introduced in this PR.

oshi-core-java25/src/main/java/oshi/driver/mac/WhoFFM.java (1)

62-73: Fallback and cleanup flow is solid.

The invalid-session fallback path and guaranteed endutxent() cleanup mirror the previous behavior and keep failure handling robust.

oshi-core-java25/src/main/java/oshi/driver/mac/WindowInfoFFM.java (1)

57-139: FFM window enumeration implementation looks good.

The CoreGraphics traversal, CGRect extraction, and CoreFoundation release handling are well-structured and consistent with the migration goal.

oshi-core-java25/src/main/java/oshi/software/os/mac/MacNetworkParamsFFM.java (1)

45-121: Network params migration is coherent and complete.

Domain/hostname resolution moved to FFM cleanly, while IPv4/IPv6 gateway logic correctly preserves the established command-based approach.

oshi-core-java25/src/main/java/oshi/software/os/mac/MacOperatingSystemFFM.java (1)

100-190: Wiring changes correctly complete the macOS OS-layer migration path.

Line 100, Line 184, and Line 189 now route sessions, network params, and desktop windows through the new FFM implementations as expected.

CHANGELOG.md (1)

16-16: LGTM!

The changelog entry is well-formatted, accurately documents the PR's changes, and follows the established conventions of the file.

@dbwiddis
dbwiddis merged commit a24d8dc into oshi:master Mar 29, 2026
13 of 14 checks passed
@dbwiddis
dbwiddis deleted the mac-os branch March 29, 2026 21:47
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.

1 participant