Skip to content

FFM free string failed on windows #3422

Closed

Description

@JaynLau
09:49:54.547 [main] DEBUG oshi.ffm.platform.windows.com.BStrFFM -- BStrFFM.free failed
java.lang.invoke.WrongMethodTypeException: handle's method type (MemorySegment)void but found (MemorySegment)Object
	at java.base/java.lang.invoke.Invokers.newWrongMethodTypeException(Invokers.java:524)
	at java.base/java.lang.invoke.Invokers.checkExactType(Invokers.java:533)
	at oshi.ffm.platform.windows.com.BStrFFM.lambda$free$0(BStrFFM.java:72)
	at oshi.util.ExceptionUtil.runOrLog(ExceptionUtil.java:497)
	at oshi.util.ExceptionUtil.runOrLog(ExceptionUtil.java:484)
	at oshi.ffm.platform.windows.com.BStrFFM.free(BStrFFM.java:72)
	at oshi.ffm.platform.windows.com.IWbemLocatorFFM.connectServer(IWbemLocatorFFM.java:113)
	at oshi.ffm.util.platform.windows.WmiQueryHandlerFFM.queryWMI(WmiQueryHandlerFFM.java:149)
	at oshi.ffm.util.platform.windows.WmiQueryExecutorFFM.queryWMI(WmiQueryExecutorFFM.java:57)
	at oshi.ffm.util.platform.windows.WmiQueryExecutorFFM.queryWMI(WmiQueryExecutorFFM.java:50)
	at oshi.driver.common.windows.wmi.Win32BaseBoard.queryBaseboardInfo(Win32BaseBoard.java:50)

version: 7.3.2

Activity

  1. changed the title [-]FFM free string faild on windows[/-] [+]FFM free string failed on windows[/+] on Jul 1, 2026
  2. added
    confirmed bug
    on Jul 1, 2026
  3. dbwiddis commented on Jul 1, 2026

    @dbwiddis
    Member

    Thanks for the report — confirmed, and there's a fix in #3424.

    Root cause: SysFreeString is a void native call, so its method handle type is (MemorySegment)void. MethodHandle.invokeExact is signature-polymorphic and infers its return type from the call site. In the expression-bodied lambda passed to the void-returning runOrLog(...), javac infers Object instead of void, so the call site ((MemorySegment)Object) doesn't match the handle and throws WrongMethodTypeException. It's caught and logged at DEBUG, so it doesn't crash — but the BSTR isn't freed, so there's a small memory leak on each affected WMI query.

    This is a regression of #3302 (which fixed #3301). That fix converted this exact line in BStrFFM.free to a block lambda, but it was lost when the FFM code moved from oshi.ffm.windows.com to oshi.ffm.platform.windows.com — the file came back in the old expression-lambda form.

    The fix reinstates the block-lambda form (so invokeExact is in a statement context and infers void) and adds a comment so a future move doesn't silently revert it again. I also scanned the rest of oshi-core-ffm and this was the only remaining reintroduction of the pattern.

  4. dbwiddis commented on Jul 1, 2026

    @dbwiddis
    Member

    As a preventative, #3424 also adds a source-policy test (VoidInvokeExactSourceTest) that scans the oshi-core-ffm sources for the expression-lambda invokeExact form and fails the build with the offending file:line.

    Because the actual WrongMethodTypeException only fires on the native platform and is swallowed by runOrLog, no ordinary unit test can observe it — and this has now regressed twice on package moves. The source scan runs on every build (including non-Windows CI), so the next reintroduction is caught immediately. Verified it fails on the pre-fix expression-lambda form and passes on the block-lambda fix.

  5. dbwiddis commented on Jul 1, 2026

    @dbwiddis
    Member

    Correction to my note above: the regression was not caused by the FFM package move (#3356 preserved the #3302 fix). It was re-broken by 1d0cb9cf97 "Collapse single-statement block lambdas to expression form (S1602)" — a Sonar mechanical sweep that collapsed the block lambda back to expression form. Worth being aware that Sonar rule S1602 (and its autofix) will reintroduce this footgun on any such lambda; the new source-policy test in #3424 guards against it.

  6. dbwiddis commented on Jul 1, 2026

    @dbwiddis
    Member

    @JaynLau Since you also reported the original (#3301), here's the full story of how it came back and how we're stopping it from happening a third time.

    What broke

    SysFreeString is a void native call, so its method handle type is (MemorySegment)void. MethodHandle.invokeExact is signature-polymorphic: its return type is inferred from the call site. In an expression-bodied lambda passed to a void-returning helper (runOrLog(...)), javac infers Object instead of void, so the call site ((MemorySegment)Object) no longer matches the handle and throws WrongMethodTypeException. It's caught and logged at DEBUG, so nothing crashes — but the BSTR is never freed, leaking a little memory on each WMI query.

    Empirically, for a void handle:

    Form Result
    handle::invokeExact (method reference) OK
    () -> handle.invokeExact() (expression lambda) WrongMethodTypeException
    () -> { handle.invokeExact(); } (block lambda) OK

    Why it regressed (it was a Sonar rule, not the package move)

    #3302 originally fixed this in two places by using block lambdas: BStrFFM.free and Ole32FFM.CoUninitialize. The later FFM package move (#3356) preserved both. What re-broke it was a Sonar mechanical cleanup (#3394, rules S1602 / S1612):

    • S1602 ("single-statement lambdas shouldn't use a block") collapsed () -> { … } back to the expression form — exactly the shape that miscompiles a void invokeExact.

    Why only one of the two regressed

    • Ole32FFM.CoUninitialize takes no arguments, so after S1602 collapsed it, S1612 ("use method references") further rewrote it to CoUninitialize::invokeExact — which is the safe form (see table). It came out fine.
    • BStrFFM.free captures an argument (bstr), so it can't become a no-arg method reference. S1602 collapsed it to () -> SysFreeString.invokeExact(bstr) and left it there — the broken expression form you hit.

    The fix and how we prevent recurrence (#3424)

    1. Restore the block lambda in BStrFFM.free.
    2. Add // NOSONAR (java:S1602) on that line so a future S1602 sweep can't collapse it again.
    3. Add a source-policy test (VoidInvokeExactSourceTest) that scans the oshi-core-ffm sources for the runOrLog/runSilently(() -> … invokeExact) expression-lambda form (single- or multi-line) and fails the build with the offending file:line. Because the runtime error only fires on Windows and is swallowed, no ordinary unit test can see it — the source scan runs on every build (including non-Windows CI) and guards both call sites against reintroduction.

    Thanks again for the report — the stack trace made this quick to pin down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions