Fix composite R2R token corruption for devirtualized async-variant callees#129053
Merged
Conversation
…llees In composite ReadyToRun, a runtime-async caller that awaits a devirtualized call to a non-runtime-async virtual method reaches token resolution with the callee's compiler-synthesized async-variant MethodDesc. GetTypicalMethodDefinition() on that variant returns the variant itself rather than the underlying EcmaMethod, so _HandleToModuleToken skipped the methoddef fast path and fell through to the faux-IL token path, indexing the thunk's small synthetic token table with the callee's real methoddef token. This corrupts token resolution (out-of-bounds for larger rows, mis-resolution for low rows) and aborts crossgen2. Insert GetPrimaryMethodDesc() before GetTypicalMethodDefinition() so synthetic wrappers unwrap to the underlying EcmaMethod, matching the idiom already used at the sibling resolver sites. The token and module are then sourced consistently from that EcmaMethod. Add regression tests in ILCompiler.ReadyToRun.Tests (composite, two-assembly) and src/tests/async (single-assembly via [RuntimeAsyncMethodGeneration(false)]). Both abort crossgen2 without the fix and pass with it. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a composite ReadyToRun (crossgen2) token-resolution failure when a runtime-async caller awaits a devirtualized call whose resolved callee is a compiler-synthesized async-variant thunk, by unwrapping to the primary (underlying ECMA) method before resolving module/token. It also adds regression coverage in both ILCompiler.ReadyToRun.Tests (composite, multi-assembly) and src/tests/async (single-assembly).
Changes:
- Update R2R JIT interface token handling to unwrap synthetic
MethodDescwrappers viaGetPrimaryMethodDesc()beforeGetTypicalMethodDefinition(). - Add a new composite runtime-async devirtualization regression test case (two-assembly) in ILCompiler.ReadyToRun.Tests.
- Add a new
src/tests/asynctest project covering devirtualization to inherited non-runtime-async virtual methods.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs | Unwrap synthetic method wrappers before resolving methoddef tokens to avoid faux-token-table indexing issues. |
| src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/R2RTestSuites.cs | Adds a new composite runtime-async devirtualization test case and validation assertions. |
| src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/RuntimeAsync/Dependencies/AsyncDevirtNonAsyncCalleeLib.cs | New dependency library source for the composite regression scenario (non-runtime-async callee). |
| src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/TestCases/RuntimeAsync/CompositeAsyncDevirtNonAsyncCalleeMain.cs | New runtime-async “main” source that awaits the devirtualized inherited virtual methods. |
| src/tests/async/devirtualize-inherited-nonasync/devirtualize-inherited-nonasync.csproj | New async test project wrapper (single source compile include). |
| src/tests/async/devirtualize-inherited-nonasync/devirtualize-inherited-nonasync.cs | New async regression test exercising devirtualization to inherited non-runtime-async virtual methods. |
davidwrighton
approved these changes
Jun 5, 2026
DrewScoggins
approved these changes
Jun 5, 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.
In composite ReadyToRun, a runtime-async caller that awaits a devirtualized call to a non-runtime-async virtual method reaches token resolution with the callee's compiler-generated async thunk. GetTypicalMethodDefinition() on that variant returns the AsyncMethodVariant rather than the underlying EcmaMethod, so _HandleToModuleToken skipped the methoddef fast path and fell through to the faux-IL token path, indexing the thunk's small synthetic token table with the callee's real methoddef token. This causes an IndexOutOfRangeException (usually), or a subtly corrupted reference.
Insert GetPrimaryMethodDesc() before GetTypicalMethodDefinition() in the devirtualized target case we can get the underlying EcmaMethod and its Module. The MetadataToken comes from resolveVirtualMethod which should return a token that is valid in the EcmaMethod's Module.
Add regression tests in ILCompiler.ReadyToRun.Tests (composite, two-assembly) and src/tests/async (single-assembly via [RuntimeAsyncMethodGeneration(false)]). Both abort crossgen2 without the fix and pass with it.
Fixes #128413