Skip to content

[release/11.0] ILC/crossgen2: make IsAsyncCall instantiation-invariant - #136115

Merged
agocke merged 1 commit into
release/11.0from
backport/pr-136002-to-release/11.0
Oct 21, 2026
Merged

agocke merged 1 commit into
release/11.0from
backport/pr-136002-to-release/11.0

Conversation

@github-actions

@github-actions github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #136002 to release/11.0

/cc @MichalStrehovsky @wpscott

Customer Impact

  • Customer reported
  • Found internally

Bad codegen with runtime async leading to crashes. Customer reported in #135922. Affects both native AOT and ReadyToRun.

Regression

  • Yes
  • 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.

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

Copy link
Copy Markdown
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 MichalStrehovsky changed the title [release/11.0] ILC: make IsAsyncCall instantiation-invariant [release/11.0] ILC/crossgen2: make IsAsyncCall instantiation-invariant Oct 20, 2026
@agocke agocke added the Servicing-approved Approved for servicing release label Oct 21, 2026
@agocke
agocke merged commit a3c8cf3 into release/11.0 Oct 21, 2026
120 of 122 checks passed
@agocke
agocke deleted the backport/pr-136002-to-release/11.0 branch October 21, 2026 17:01
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-ReadyToRun Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants