Skip to content

Fix crossgen2 sometimes emitting tokens from the wrong module #114172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 5, 2025

Conversation

kg
Copy link
Member

@kg kg commented Apr 2, 2025

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.)

Fixes #114183

davidwrighton and others added 3 commits April 2, 2025 13:26
…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
Copy link
Member Author

kg commented Apr 2, 2025

/azp run runtime-coreclr crossgen2 outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@kg kg marked this pull request as ready for review April 3, 2025 01:45
@Copilot Copilot AI review requested due to automatic review settings April 3, 2025 01:45
Copy link
Contributor

@Copilot 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 aims to prevent crossgen2 from emitting tokens from the wrong module by adding a safety check during R2R generation and updating token comparers.

  • Updated equality comparisons in RuntimeDeterminedTypeHelper to include module tokens.
  • Added a safety check in SignatureBuilder to throw an error for tokens from modules outside the version bubble.
  • Added a similar safety check in ModuleTokenResolver for validating module tokens.

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 Adds module token comparisons in equality checks for methods and fields.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/SignatureBuilder.cs Introduces a safety check ensuring that tokens are only used from modules within the version bubble.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs Adds a version-bubble check for module tokens and raises an error if the module is not valid.
Comments suppressed due to low confidence (1)

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

  • Consider caching the result of _moduleIndexLookup(module) after the initial call to avoid duplicate lookups later in the method.
return _moduleIndexLookup(module);

@kg
Copy link
Member Author

kg commented Apr 3, 2025

The arm64 outerloop failure looks like it would be fixed by #106099.

@kg kg merged commit f29cccf into dotnet:main Jun 5, 2025
94 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When compiling with --opt-cross-module Crossgen2 can use invalid tokens
2 participants