Repository navigation
ILC: make IsAsyncCall instantiation-invariant - #136002
Merged
MichalStrehovsky merged 2 commits intoOct 19, 2026
Merged
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. |
Contributor
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
Contributor
Author
|
@dotnet-policy-service agree |
MichalStrehovsky
left a comment
Member
There was a problem hiding this comment.
LGTM otherwise, thanks!
MethodExtensions.IsAsyncCall evaluated Signature.ReturnsTaskOrValueTask() on the instantiated signature, so the answer depended on the instantiation for AsyncHelpers.TransparentAwait<T>(Task<T>): true for T=int, but false for T=ValueTask/ValueTask<int>, whose instantiated signature returns a ValueTask. The runtime computes the async method flags once, on the generic method definition (vm/class.cpp ClassifyMethodReturnKind + AsyncMethodFlags::AsyncCall, read back by MethodDesc::IsAsyncMethod), and instantiations share that flag. The JIT's Compiler::impWrapTopOfStackInAwait, which inserts the call in the first place, assumes exactly that: it asserts awaitSig.isAsyncCall() and unconditionally passes a WellKnownArg::AsyncContinuation argument. When the callee is compiled without the async calling convention (CORJIT_FLAG_ASYNC is derived from the same predicate), the hidden continuation parameter is missing. For results that are 16-byte structs the hidden return buffer takes RCX, so the caller's [retbuf][asyncCont][task] layout puts Task<T> in R8 while the callee reads it from RDX. The callee then dereferences null at Task.m_stateFlags. This is observable in NativeAOT with OptimizationPreference=Size: awaiting Task<ValueTask> or Task<ValueTask<int>> throws NullReferenceException at AsyncHelpers.TransparentAwait<ValueTask>+0xc, while the same code works when the inserted call is inlined (OptimizationPreference=Speed), when runtime-async is disabled, and on CoreCLR. Results of type int or void are unaffected because there is no return buffer, so the extra continuation slot lands in the unused RCX. Evaluate the return type check on the generic method definition instead, which makes the answer instantiation-invariant and matches the runtime's own flag semantics. Add src/tests/async/regression/135922, which awaits Task<ValueTask> and Task<ValueTask<int>> and round-trips the awaited results. Fixes dotnet#135922
wpscott
force-pushed
the
ilc-asynccall-instantiation-invariant
branch
from
October 19, 2026 13:42
be3e54e to
994a6d6
Compare
wpscott
marked this pull request as ready for review
October 19, 2026 13:59
Contributor
Author
|
Thanks for the review! I've addressed all three suggestions but force-pushed the updated branch instead of adding a new commit. My apologies for rewriting the commit history. |
Member
|
/ba-g timeout in networking test, tired of filing issues that are only going to be put in Future milestone |
Member
|
/backport to release/11.0 |
Contributor
|
Started backporting to |
2 of 4 tasks
agocke
pushed a commit
that referenced
this pull request
Oct 21, 2026
#136115) Backport of #136002 to release/11.0 /cc @MichalStrehovsky @wpscott ## Customer Impact - [X] Customer reported - [ ] Found internally Bad codegen with runtime async leading to crashes. Customer reported in #135922. Affects both native AOT and ReadyToRun. ## Regression - [ ] Yes - [X] No Runtime-async is a new feature. ## Testing Test that hits the failure was added. Triggering this requires interactions of several things that we didn't hit in any of our testing. ## Risk The risk should be low, the fix is obvious. **IMPORTANT**: If this backport is for a servicing release, please verify that: - For .NET 8 and .NET 9: The PR target branch is `release/X.0-staging`, not `release/X.0`. - For .NET 10+: The PR target branch is `release/X.0` (no `-staging` suffix). ## Package authoring no longer needed in .NET 9 **IMPORTANT**: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version. Keep in mind that we still need package authoring in .NET 8 and older versions. Co-authored-by: wpscott <[email protected]>
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #135922.
Problem
Awaiting a
Task<ValueTask>(orTask<ValueTask<int>>) in a NativeAOT app published withOptimizationPreference=SizethrowsNullReferenceException. The same code works withOptimizationPreference=Speed, withruntime-asyncdisabled, and on CoreCLR.cdbon the Size build: all faults are atAsyncHelpers.TransparentAwait<ValueTask>+0xc(
test dword ptr [rbx+34h], 1600000h=Task.m_stateFlags) withrbx = rdx = 0, while the realTask<ValueTask>object sits inr8.Root cause
MethodExtensions.IsAsyncCallevaluates the Task/ValueTask check on the instantiated signature:For
AsyncHelpers.TransparentAwait<T>(Task<T>)the definition returns!!T, so the answer is "yes",but for
T = ValueTask/T = ValueTask<int>the instantiated signature returns aValueTask, so itflips to "no". The result is instantiation-dependent, which the runtime never does:
AsyncMethodFlagsare computed once from the definition (
vm/class.cpp,ClassifyMethodReturnKind+AsyncMethodFlags::AsyncCall) and read back byMethodDesc::IsAsyncMethodfor every instantiation.Compiler::impWrapTopOfStackInAwait, which inserts the call, relies on that: it assertsawaitSig.isAsyncCall()and unconditionally adds aWellKnownArg::AsyncContinuationargument. Whenthe callee is compiled without
CORJIT_FLAG_ASYNC, the hidden continuation parameter is missing. Forresults that are 16-byte structs the hidden return buffer takes
RCX, so the caller layout[retbuf][asyncCont][task]puts the task inR8while the callee reads it fromRDX:With the fix the callee reads
%r8, matching the call site. Results of typeint/voidareunaffected because there is no return buffer, so the extra argument lands in the unused
RCX- whichis why
Task<int>never failed.Fix
Evaluate the return type on the generic method definition so the predicate is instantiation-invariant,
matching the runtime flag semantics:
This also makes
getJitFlagssetCORJIT_FLAG_ASYNCandGet_CORINFO_SIG_INFOsetCORINFO_CALLCONV_ASYNCCALLfor these instantiations, which is what the JIT already assumes.Test
src/tests/async/regression/135922.csawaitsTask<ValueTask>andTask<ValueTask<int>>andround-trips the awaited results.
Note on coverage: the failure itself needs all three of NativeAOT,
OptimizationPreference=Size, and theinserted
TransparentAwaitcall not being inlined - CoreCLR folds the await and the AOT test pipelinecompiles such xunit tests with default (Speed) settings, so this test is a semantic regression test
rather than a direct reproducer of the AOT crash.
Verification
Manually, against a NativeAOT toolchain built from this branch (
build.cmd clr.aot+libs -rc Release -lc Release):Size(--Os),Task<ValueTask>/Task<ValueTask<int>>NullReferenceExceptionSize,Task<int>/Task/Task.Run(() => ValueTask)SpeedThe before/after runs used an
ilcrebuilt from the same tree with and without the one-line change; thenew test builds in the test tree (
src\tests\build.cmd x64 Release -Test async\regression\135922.csproj).Related: #129896 / #130482 made the JIT-inserted call inlineable, which hides this on the JIT side and
under
OptimizationPreference=Speed; the arity mismatch remains whenever the call stays out of line.