Skip to content

Fix ADL GPU concurrency/overflow/OD6 gating and WMI handler thread-safety - #3490

Merged
dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:fix-gpu-wmi-concurrency
Jul 19, 2026
Merged

dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:fix-gpu-wmi-concurrency

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jul 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Concurrency and correctness fixes surfaced by CodeQL/AI review in the AMD ADL GPU (FFM) path and the JNA WMI query handler.

AdlUtilFFM

  • Adapter-enumeration race — ensureAdaptersEnumerated did a check-then-act on the volatile adaptersEnumerated flag, so two threads could both run enumerateAdapters and reset BUS_TO_INDEX. Now guarded with double-checked locking so enumeration happens at most once.
  • Integer-overflow guard — enumerateAdapters passed ADAPTER_INFO_SIZE * num (a plain int) to the native adapter-info call while allocating the buffer with a long. The native argument must stay 32-bit, so an overflow guard now rejects an implausibly large adapter count rather than letting the int multiplication wrap.
  • Overdrive-6 power gating — getPowerDraw calls the Overdrive 6 power API but gated on Overdrive N (version >= 8), wrongly returning -1 on OD6-only cards. It now gates on OD6 (version >= 6); the native return code still guards any card that doesn't implement the call. (AMD-hardware-specific; validated by code inspection, consistent with the rest of the best-effort Windows GPU path.)

WmiQueryHandler (JNA)

The @ThreadSafe handler cached failed class names in a plain HashSet mutated without synchronization (concurrent add/contains can corrupt or spin), and wmiTimeout was a plain int. Brought to parity with the FFM twin: a ConcurrentHashMap-backed set and a volatile wmiTimeout. (comThreading stays plain — guarded by the existing synchronized accessors; securityInitialized was already volatile.)

Testing

./mvnw clean install (full reactor) plus spotless, checkstyle, forbiddenapis, and javadoc all pass.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved AMD GPU adapter enumeration for better correctness and reliability under concurrent queries.
    • Added safeguards against invalid adapter counts that could lead to incorrect native buffer sizing.
    • Corrected AMD power-draw capability detection to align with newer Overdrive 6 support.
    • Improved capability checks for Overdrive versions to enhance compatibility.
    • Strengthened Windows WMI query handling with thread-safe caching of failed lookups and more consistent timeout behavior.

…fety

AdlUtilFFM:
- ensureAdaptersEnumerated had a check-then-act race on adaptersEnumerated:
  two threads could both enumerate and reset BUS_TO_INDEX. Guard it with
  double-checked locking so enumeration happens at most once.
- enumerateAdapters passed ADAPTER_INFO_SIZE * num (a plain int) to the native
  adapter-info call while allocating the buffer with a long. Add an overflow
  guard so the int argument can never wrap (implausible in practice).
- getPowerDraw gated on Overdrive N (version >= 8) but calls the Overdrive 6
  power API, wrongly rejecting OD6-only cards. Gate on OD6 (version >= 6)
  instead; the native return code still guards cards lacking the call.

WmiQueryHandler (JNA): the @threadsafe class cached failed class names in a
plain HashSet mutated without synchronization, and wmiTimeout was a plain int.
Match the FFM twin: a ConcurrentHashMap-backed set and a volatile wmiTimeout.
@coderabbitai

coderabbitai Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 2ff37ac2-2d44-4568-b5c7-5464a9a3cf9b

📥 Commits

Reviewing files that changed from the base of the PR and between 55f546e and 1059cfb.

📒 Files selected for processing (1)
  • oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java
📜 Recent review details
⏰ Context from checks skipped due to timeout. (15)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Analyze (java)

📝 Walkthrough

Walkthrough

ADL runtime binding now synchronizes lazy adapter enumeration, rejects oversized native allocations, and distinguishes Overdrive 6 power support from Overdrive-N. WMI handling uses a concurrent failed-class cache and a volatile timeout field.

Changes

ADL runtime handling

Layer / File(s) Summary
Adapter enumeration synchronization and validation
oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java
Lazy adapter enumeration uses double-checked locking, while oversized adapter counts return an empty map before buffer allocation.
Overdrive version capability gating
oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java
Overdrive checks use a shared minimum-version helper, add Overdrive 6 support detection, and gate power draw through that check.

WMI concurrency state

Layer / File(s) Summary
Concurrent WMI state
oshi-core/src/main/java/oshi/util/platform/windows/WmiQueryHandler.java
The failed WMI class cache uses a concurrent set, and wmiTimeout is now volatile.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Poem

I’m a rabbit hopping through locks in the night,
Counting GPU adapters and keeping them right.
Overdrive six brings power to play,
While WMI shares its cache safely each day.
Carrots compile, and the threads all agree!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ 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 accurately summarizes the main changes: ADL concurrency/overflow fixes, Overdrive 6 gating, and WMI handler thread-safety.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@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.

🧹 Nitpick comments (1)
oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java (1)

273-274: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low value

Explicitly verify that Overdrive is supported.

While FFM safely zero-initializes the allocated memory (so if an unsupported device leaves the buffer untouched, the evaluation safely defaults to 0 >= minVersion which is false), it is safer and more explicit to verify the supported flag returned by the API.

♻️ Proposed refactor
-            if ((int) ADL2_OVERDRIVE_CAPS.invokeExact(context, adapterIndex, supported, enabled, version) == ADL_OK) {
-                return version.get(JAVA_INT, 0) >= minVersion;
-            }
+            if ((int) ADL2_OVERDRIVE_CAPS.invokeExact(context, adapterIndex, supported, enabled, version) == ADL_OK) {
+                return supported.get(JAVA_INT, 0) != 0 && version.get(JAVA_INT, 0) >= minVersion;
+            }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java` around lines
273 - 274, Update the Overdrive capability check in the ADL2_OVERDRIVE_CAPS
invocation to require the returned supported flag before evaluating version
against minVersion. Preserve the existing ADL_OK and version checks, and return
false when Overdrive is unsupported.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java`:
- Around line 273-274: Update the Overdrive capability check in the
ADL2_OVERDRIVE_CAPS invocation to require the returned supported flag before
evaluating version against minVersion. Preserve the existing ADL_OK and version
checks, and return false when Overdrive is unsupported.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f269ea1e-b041-4116-9bb4-2b4179135bd4

📥 Commits

Reviewing files that changed from the base of the PR and between 9bb6307 and 55f546e.

📒 Files selected for processing (2)
  • oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java
  • oshi-core/src/main/java/oshi/util/platform/windows/WmiQueryHandler.java
📜 Review details
⏰ Context from checks skipped due to timeout. (11)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-07-06T19:25:59.225Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3432
File: oshi-common/src/main/java/oshi/util/ExceptionUtil.java:26-56
Timestamp: 2026-07-06T19:25:59.225Z
Learning: In this repo’s SLF4J 1.x-compatible logging code, do not flag cases where a logger call uses SLF4J 1.6+ implicit-cause handling—e.g., `log.error("...{}", value, t)` (or similar with a single `{}` placeholder) where `t` is passed as the final/trailing argument. SLF4J will attach `t` as the log event’s cause with the full stack trace; this is equivalent to SLF4J 2.x’s fluent `atLevel(...).setCause(t).log(...)` and is intentionally used to preserve SLF4J 1.x runtime compatibility.

Applied to files:

  • oshi-core/src/main/java/oshi/util/platform/windows/WmiQueryHandler.java
  • oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java
🔇 Additional comments (4)
oshi-core/src/main/java/oshi/util/platform/windows/WmiQueryHandler.java (1)

8-10: LGTM!

Also applies to: 45-48

oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java (3)

204-214: LGTM!


228-233: LGTM!


432-434: LGTM!

ADL2_Overdrive_Caps returns a supported flag alongside the version; check it
so an adapter reporting a version but supported=0 is not treated as capable.
@codecov

codecov Bot commented Jul 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.87%. Comparing base (9bb6307) to head (1059cfb).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3490      +/-   ##
============================================
- Coverage     74.93%   74.87%   -0.07%     
  Complexity       76       76              
============================================
  Files           591      591              
  Lines         21584    21584              
  Branches       3614     3614              
============================================
- Hits          16175    16161      -14     
- Misses         3436     3444       +8     
- Partials       1973     1979       +6     
Flag Coverage Δ
freebsd 27.74% <ø> (-0.01%) ⬇️
linux 32.27% <ø> (ø)
macos 32.82% <ø> (-0.05%) ⬇️
openbsd 27.02% <ø> (+<0.01%) ⬆️
solaris 29.43% <ø> (-0.01%) ⬇️
windows 40.84% <ø> (-0.03%) ⬇️

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 7e75cf5 into oshi:master Jul 19, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the fix-gpu-wmi-concurrency branch July 19, 2026 19:21
dbwiddis added a commit that referenced this pull request Aug 4, 2026
PR #3490 fixed three defects in AdlUtilFFM that were never applied to
AdlUtilJNA:

* getPowerDraw reads power through ADL2_Overdrive6_CurrentPower_Get but
  gated on Overdrive N (version 8) support, so Overdrive 6-era Radeon
  cards were rejected before the call they actually implement and
  GpuStats#getPowerDraw() returned -1 instead of real wattage. Gate on
  Overdrive 6 instead; the return code still guards cards that do not
  implement the call.
* The Overdrive capability check allocated the ADL2_Overdrive_Caps
  iSupported out-parameter and never read it, treating a card that
  reports a version but no Overdrive support as supported. Check both
  iSupported and the version, via a shared supportsOverdriveVersion
  helper matching the twin.
* ensureAdaptersEnumerated was check-then-act on a @threadsafe class, so
  concurrent callers could both enumerate and both publish. Use
  double-checked locking on a dedicated lock, as the twin does.

Also fix a separate divergence in the opposite direction:
WindowsGraphicsCardFFM passed the full DXGI adapter list to the WMI
fallback where the JNA twin deliberately passes the match-and-consume
copy. Adapters are removed from that copy only after a card is
successfully constructed, so an adapter whose registry reads threw
remains available to the fallback. FFM reaches this path by swallowing
the registry walk failure, which is exactly when it matters.

The #3490 overflow guard has no JNA equivalent; JNA computes the buffer
size from Structure.size() rather than manual int arithmetic.

Compiles clean; these are Windows-only paths, not executable from a
macOS host, and the power-draw fix additionally needs an AMD GPU. CI
covers the rest.
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