Skip to content

[mono][wasm] Handle versioned OS platform P/Invoke attributes - #132300

Merged
akoeplinger merged 1 commit into
dotnet:mainfrom
Sieluna:fix-versioned-os-platform-pinvoke
Aug 14, 2026
Merged

[mono][wasm] Handle versioned OS platform P/Invoke attributes#132300
akoeplinger merged 1 commit into
dotnet:mainfrom
Sieluna:fix-versioned-os-platform-pinvoke

Conversation

@Sieluna

@Sieluna Sieluna commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Summary

  • match versioned OS platform attributes against the Mono P/Invoke target OS
  • keep valid browser1.0 P/Invokes in the generated table

Root cause

The Mono P/Invoke collector compared the complete platform attribute string
with TargetOS. Standard net11.0-browser libraries carry
SupportedOSPlatform("browser1.0"), while the build task receives
TargetOS=browser, so valid P/Invokes were silently filtered out.

The fix preserves the existing platform-only behavior while accepting a valid
optional Version suffix. It is intentionally limited to the Mono collector
where the regression was introduced.

Fixes #132297

Validation

  • built WasmAppBuilder.csproj in Release for both net11.0 and net472
  • no automated regression test is included; full workload validation is left
    to CI and the maintainers

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Aug 14, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@Sieluna
Sieluna force-pushed the fix-versioned-os-platform-pinvoke branch from d477ae9 to b47c7f3 Compare August 14, 2026 01:15
@Sieluna

Sieluna commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@Sieluna
Sieluna force-pushed the fix-versioned-os-platform-pinvoke branch from b47c7f3 to 25f2a3f Compare August 14, 2026 01:21
@Sieluna
Sieluna marked this pull request as ready for review August 14, 2026 01:22
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@pavelsavara pavelsavara added the arch-wasm WebAssembly architecture label Aug 14, 2026
@pavelsavara pavelsavara self-assigned this Aug 14, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

@pavelsavara

Copy link
Copy Markdown
Member

Standard net11.0-browser libraries carry
SupportedOSPlatform("browser1.0")

Why is that ?

cc @akoeplinger

@akoeplinger

Copy link
Copy Markdown
Member

Browser doesn't have an OS version like Windows 11, macOS 16 etc. It is always 1.0

@pavelsavara

Copy link
Copy Markdown
Member

@Sieluna could you please add SupportedOSPlatform("browser1.0") and other combinations to src\mono\wasm\testassets\EntryPoints\PInvoke\AbiRules.cs to test it in WBT ?

Copilot AI 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.

Pull request overview

Updates the Mono WasmAppBuilder P/Invoke platform-attribute filtering to treat version-suffixed OS platform strings (for example, browser1.0) as matching the unversioned TargetOS (for example, browser), preventing valid P/Invokes from being dropped during table generation.

Changes:

  • Replaces exact string comparisons against TargetOS with a helper that also accepts a valid Version suffix.
  • Applies the new matching logic to both SupportedOSPlatformAttribute and UnsupportedOSPlatformAttribute handling.
  • Keeps the existing “platform-only” behavior by ignoring the parsed version value beyond validation.

Comment on lines 291 to 295
if (cattr.AttributeType.FullName == "System.Runtime.Versioning.UnsupportedOSPlatformAttribute" &&
cattr.ConstructorArguments.Count > 0 &&
cattr.ConstructorArguments[0].Value?.ToString() == _targetOS)
MatchesTargetOS(cattr.ConstructorArguments[0].Value?.ToString()))
{
return PlatformSupport.Unsupported;
Comment on lines +317 to +331
private bool MatchesTargetOS(string? platformName)
{
if (string.Equals(platformName, _targetOS, StringComparison.OrdinalIgnoreCase))
return true;

if (platformName?.StartsWith(_targetOS, StringComparison.OrdinalIgnoreCase) != true)
return false;

#if NETFRAMEWORK
string version = platformName.Substring(_targetOS.Length);
#else
ReadOnlySpan<char> version = platformName.AsSpan(_targetOS.Length);
#endif
return Version.TryParse(version, out _);
}
@akoeplinger

Copy link
Copy Markdown
Member

I'm going to merge this so it makes RC1 and ask copilot to create the tests.

@Sieluna thanks for the fix!

akoeplinger added a commit that referenced this pull request Aug 18, 2026
…dd regression test (#132332)

Follow-up to #132300: the CoreCLR generator's platform-attribute filter
still compared `SupportedOSPlatform`/`UnsupportedOSPlatform` strings to
`_targetOS` with exact equality, so versioned attributes like
`[SupportedOSPlatform("browser1.0")]` were treated as non-matching on
that path (mono's collector was already fixed).

## CoreCLR collector fix
- Duplicated the `MatchesTargetOS` helper (with the `NETFRAMEWORK`
conditional for `net472`) into
`src/tasks/WasmAppBuilder/coreclr/PInvokeCollector.cs`, per maintainer
preference to keep the collectors independent rather than share code.
- `EvaluatePlatformAttributes` now calls `MatchesTargetOS` instead of
exact string comparison for both attributes.
- `src/tasks/WasmAppBuilder/mono/PInvokeCollector.cs` is unchanged.

```csharp
if (cattr.AttributeType.FullName == "System.Runtime.Versioning.UnsupportedOSPlatformAttribute" &&
    cattr.ConstructorArguments.Count > 0 &&
    MatchesTargetOS(cattr.ConstructorArguments[0].Value?.ToString()))
{
    return PlatformSupport.Unsupported;
}
```

## Regression test
- Added `VersionedOSPlatformPInvokeIsIncluded` to
`PInvokeTableGeneratorTests` in `src/mono/wasm/Wasm.Build.Tests`.
- New test asset declares a P/Invoke annotated
`[SupportedOSPlatform("browser1.0")]` (must match `TargetOS=browser`)
alongside one annotated `[SupportedOSPlatform("windows1.0")]` (must not
match).
- Asserts the browser-versioned entry is present in the generated
pinvoke table and the windows-versioned one is absent, then runs the app
to confirm the included P/Invoke is callable at runtime.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: akoeplinger <1376924+akoeplinger@users.noreply.github.com>
Co-authored-by: Pavel Savara <pavel.savara@gmail.com>
Co-authored-by: Marek Fišera <mara@neptuo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-Build-mono community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[mono][wasm] Versioned SupportedOSPlatform filters valid browser P/Invokes

6 participants