Repository navigation
[release/11.0] Fix STJ unsafe accessor detection with polyfilled attributes - #136473
Merged
Merged
Conversation
<!-- --> The System.Text.Json source generator treats polyfilled `UnsafeAccessorAttribute` definitions as evidence of runtime support. Referencing an assembly such as BenchmarkDotNet can therefore cause a .NET Framework project to emit unsupported unsafe accessors, even without using any APIs from that assembly. A polyfilled `OverloadResolutionPriorityAttribute` can similarly cause generic unsafe accessors to be emitted on .NET 8. Require both capability markers to come from the target compilation's core library, identified by the assembly defining `System.Object`. Other type lookups are unchanged. Adds one generator unit-test theory covering source and referenced polyfills with public and internal visibility. Validation: ` .\dotnet.cmd build src\libraries\System.Text.Json\tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn4.4.Unit.Tests.csproj -t:Test -f net481 -p:RuntimeConfiguration=Release '-p:XunitMethodName=*UnsafeAccessorPolyfill*'` passed all four cases. Standalone public API checks also passed for net472 and .NET 8 with source/referenced and public/internal marker polyfills. Resolves #136457 > [!NOTE] > This pull request was generated with GitHub Copilot. Co-authored-by: Copilot App <[email protected]>
|
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. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
Member
|
Approved for December servicing. |
Member
|
/ba-g test failure is unrelated. |
AArnott
approved these changes
Oct 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #136463 to release/11.0
/cc @eiriktsarpalis
Customer Impact
In .NET 11, the STJ source generator added support for inaccessible members (supported historically in the reflection-based serializer via the
JsonIncludeAttributeopt in) by way of unsafe accessors. The generator checks for the availability of unsafe accessors in a target framework by looking up the unsafe accessor attribute itself. It appears however that the popular polyfill library PolySharp (used among others by BenchmarkDotNet) is creating polyfills for targets that do not natively support unsafe accessors, resulting in code that that fails at runtime.This PR strengthens the unsafe accessor availability heuristic against such polyfills.
Regression
Testing
Added testing validating the scenario.
Risk
Low. Makes a straightforward update to the platform detection heuristic.