[mono][wasm] Handle versioned OS platform P/Invoke attributes - #132300
Conversation
|
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. |
d477ae9 to
b47c7f3
Compare
|
@dotnet-policy-service agree |
b47c7f3 to
25f2a3f
Compare
|
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. |
|
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara |
Why is that ? cc @akoeplinger |
|
Browser doesn't have an OS version like Windows 11, macOS 16 etc. It is always 1.0 |
|
@Sieluna could you please add |
There was a problem hiding this comment.
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
TargetOSwith a helper that also accepts a validVersionsuffix. - Applies the new matching logic to both
SupportedOSPlatformAttributeandUnsupportedOSPlatformAttributehandling. - Keeps the existing “platform-only” behavior by ignoring the parsed version value beyond validation.
| 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; |
| 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 _); | ||
| } |
|
I'm going to merge this so it makes RC1 and ask copilot to create the tests. @Sieluna thanks for the fix! |
…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>
Summary
browser1.0P/Invokes in the generated tableRoot cause
The Mono P/Invoke collector compared the complete platform attribute string
with
TargetOS. Standardnet11.0-browserlibraries carrySupportedOSPlatform("browser1.0"), while the build task receivesTargetOS=browser, so valid P/Invokes were silently filtered out.The fix preserves the existing platform-only behavior while accepting a valid
optional
Versionsuffix. It is intentionally limited to the Mono collectorwhere the regression was introduced.
Fixes #132297
Validation
WasmAppBuilder.csprojin Release for bothnet11.0andnet472to CI and the maintainers