Repository navigation
[release/11.0] ILC/crossgen2: make IsAsyncCall instantiation-invariant - #136115
Merged
Merged
Conversation
Fixes #135922. ## Problem Awaiting a `Task<ValueTask>` (or `Task<ValueTask<int>>`) in a NativeAOT app published with `OptimizationPreference=Size` throws `NullReferenceException`. The same code works with `OptimizationPreference=Speed`, with `runtime-async` disabled, and on CoreCLR. ```csharp var tcs = new TaskCompletionSource<ValueTask>(); tcs.SetResult(ValueTask.CompletedTask); ValueTask inner = await tcs.Task.ConfigureAwait(false); await inner.ConfigureAwait(false); ``` `cdb` on the Size build: all faults are at `AsyncHelpers.TransparentAwait<ValueTask>+0xc` (`test dword ptr [rbx+34h], 1600000h` = `Task.m_stateFlags`) with `rbx = rdx = 0`, while the real `Task<ValueTask>` object sits in `r8`. ## Root cause `MethodExtensions.IsAsyncCall` evaluates the Task/ValueTask check on the **instantiated** signature: ```csharp method.IsAsync && !method.Signature.ReturnsTaskOrValueTask() ``` 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 a `ValueTask`, so it flips to "no". The result is instantiation-dependent, which the runtime never does: `AsyncMethodFlags` are computed once from the definition (`vm/class.cpp`, `ClassifyMethodReturnKind` + `AsyncMethodFlags::AsyncCall`) and read back by `MethodDesc::IsAsyncMethod` for every instantiation. `Compiler::impWrapTopOfStackInAwait`, which inserts the call, relies on that: it asserts `awaitSig.isAsyncCall()` and unconditionally adds a `WellKnownArg::AsyncContinuation` argument. When the callee is compiled without `CORJIT_FLAG_ASYNC`, the hidden continuation parameter is missing. For results that are 16-byte structs the hidden return buffer takes `RCX`, so the caller layout `[retbuf][asyncCont][task]` puts the task in `R8` while the callee reads it from `RDX`: ```asm ; caller movq 0x8(%rsi),%r8 ; Task<ValueTask> leaq -0x48(%rbp),%rcx ; retbuf xorl %edx,%edx ; async continuation call TransparentAwait<ValueTask> ; callee (compiled without the async calling convention) movq %rcx,%rsi ; retbuf movq %rdx,%rbx ; "task" == 0 -> AV at [0+0x34] ``` With the fix the callee reads `%r8`, matching the call site. Results of type `int`/`void` are unaffected because there is no return buffer, so the extra argument lands in the unused `RCX` - which is 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: ```diff - (method.IsAsync && !method.Signature.ReturnsTaskOrValueTask()); + (method.IsAsync && !method.GetTypicalMethodDefinition().Signature.ReturnsTaskOrValueTask()); ``` This also makes `getJitFlags` set `CORJIT_FLAG_ASYNC` and `Get_CORINFO_SIG_INFO` set `CORINFO_CALLCONV_ASYNCCALL` for these instantiations, which is what the JIT already assumes. ## Test `src/tests/async/regression/135922.cs` awaits `Task<ValueTask>` and `Task<ValueTask<int>>` and round-trips the awaited results. Note on coverage: the failure itself needs all three of NativeAOT, `OptimizationPreference=Size`, and the inserted `TransparentAwait` call not being inlined - CoreCLR folds the await and the AOT test pipeline compiles 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`): | configuration | before | after | |---|---|---| | NativeAOT `Size` (`--Os`), `Task<ValueTask>` / `Task<ValueTask<int>>` | `NullReferenceException` | pass | | NativeAOT `Size`, `Task<int>` / `Task` / `Task.Run(() => ValueTask)` | pass | pass | | NativeAOT `Speed` | pass | pass | | CoreCLR | pass | pass | The before/after runs used an `ilc` rebuilt from the same tree with and without the one-line change; the new 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.
|
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. |
MichalStrehovsky
approved these changes
Oct 20, 2026
agocke
approved these changes
Oct 21, 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 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.
Backport of #136002 to release/11.0
/cc @MichalStrehovsky @wpscott
Customer Impact
Bad codegen with runtime async leading to crashes. Customer reported in #135922. Affects both native AOT and ReadyToRun.
Regression
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:
release/X.0-staging, notrelease/X.0.release/X.0(no-stagingsuffix).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.