Skip to content

Conversation

@kg
Copy link
Member

@kg kg commented Mar 28, 2025

Description

Under very specific circumstances, R2R can accidentally allow tokens from other modules into its output. These tokens are valid as long as none of the assemblies in use change, but if an assembly changes (i.e. due to a runtime update) tokens pointing into that assembly are no longer valid, and you'll get crashes at runtime from the tokens no longer being what the R2R module expects them to be.

This PR adds a safety check to detect this happening during R2R generation so that we won't quietly produce broken R2R modules, and it also fixes a couple comparers to detect the case where tokens are from different modules (previously, tokens from different modules would compare as equal if they were otherwise equivalent.)

Customer Impact

Customers rely on R2R and without this fix, users who install updated versions of the .NET runtime will experience breakage due to BCL assemblies no longer matching the ones that the application's R2R modules were compiled against

Regression

No

Testing

Manual testing by building+packaging an affected assembly (from roslyn) from source using 9.0.3 and this PR. File size did not meaningfully change and the bad tokens are no longer emitted.

Risk

If I missed a scenario where bad tokens can be emitted, R2R compilation will now fail with an error instead of silently producing a corrupted assembly. This would stop the customer from using R2R until the issue is fixed.

@kg
Copy link
Member Author

kg commented Mar 28, 2025

cc @davidwrighton

@kg kg added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Mar 28, 2025
@kg
Copy link
Member Author

kg commented Mar 28, 2025

/azp run runtime-coreclr crossgen2 outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@kg
Copy link
Member Author

kg commented Mar 29, 2025

/azp run runtime-coreclr crossgen2 outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

davidwrighton and others added 3 commits March 30, 2025 13:20
…ldWithToken to ensure that the tokens in question are from the same module. Prevents us from erroneously using another module's token in a way that will produce bad R2R modules.
@kg kg force-pushed the r2r-crossmodule-token-fix branch from 7cdf547 to cf5986e Compare March 30, 2025 20:20
@kg
Copy link
Member Author

kg commented Mar 31, 2025

/azp run runtime-coreclr crossgen2 outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@steveisok
Copy link
Member

/azp run runtime-coreclr crossgen2 outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@steveisok
Copy link
Member

/azp run runtime-coreclr crossgen2 outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@kg
Copy link
Member Author

kg commented Apr 1, 2025

Looks like R2RDump broke again, but R2R is working fine on outerloop.

@kg
Copy link
Member Author

kg commented Apr 1, 2025

@kg kg added Servicing-consider Issue for next servicing release review and removed NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) labels Apr 1, 2025
@kg kg marked this pull request as ready for review April 1, 2025 23:54
Copilot AI review requested due to automatic review settings April 1, 2025 23:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses a bug where R2R accidentally emits tokens from the wrong module by adding safety checks and updating comparison logic between tokens.

  • Updated token equality checks in RuntimeDeterminedTypeHelper to include module comparisons.
  • Added validation in SignatureBuilder and ModuleTokenResolver to ensure tokens are used only from modules within the version bubble.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/RuntimeDeterminedTypeHelper.cs Added module equality checks for MethodWithToken and FieldWithToken comparisons.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/SignatureBuilder.cs Introduced a validation check that throws an error if a token from a module outside the version bubble is used.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs Implemented a module version check similar to SignatureBuilder and suggested caching the lookup result.
Comments suppressed due to low confidence (1)

src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs:316

  • Consider returning the previously computed 'moduleIndex' (from line 308) instead of calling _moduleIndexLookup(module) a second time to avoid redundant computations.
return _moduleIndexLookup(module);

@kg
Copy link
Member Author

kg commented Apr 2, 2025

Looking at the recent scheduled outerloop runs, the arm64 jit issue has been around for at least a month, but this r2rdump problem is either brand new or is somehow related to this PR.

@carlossanlop
Copy link
Contributor

Friendly reminder that code complete is on April 14th for the May Release. If you'd like to get this change included in that release, please get a Tactics approval and merge this PR before that date.

@kg kg requested review from a team and davidwrighton April 2, 2025 18:51
@steveisok steveisok removed the Servicing-consider Issue for next servicing release review label Apr 2, 2025
@kg
Copy link
Member Author

kg commented Apr 2, 2025

After some discussion we decided to target main with this first, because on 9.0-staging it breaks CI due to not having #107064 .

@kg kg closed this Apr 2, 2025
@github-actions github-actions bot locked and limited conversation to collaborators May 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants