You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 56c46fb
Browse filesBrowse the repository at this point in the historyBrowse files
authored
Fix crossgen2 OverflowException emitting inlining info for runtime-async inliners (dotnet#134697)
With cross-module inlining enabled (`--opt-cross-module`), crossgen2 can
fail while emitting the cross-module inlining info table:
```
System.OverflowException: Arithmetic operation resulted in an overflow.
at ILCompiler.DependencyAnalysis.ReadyToRun.InliningInfoNode.GetData(...)
```
Debug builds assert `_index != InvalidOffset` at the same place.
## Root cause
When the runtime-async variant of a cross-module generic method is
compiled into the image and inlines another method, `CompileMethod`
records a `Check_IL_Body` fixup for the method's typical definition,
which is the `AsyncMethodVariant`. `InliningInfoNode` reduced every
inliner to its primary `EcmaMethod` and looked up that method's
`Check_IL_Body` import instead. Nothing ever marked that import, so it
was never placed, its index stayed at `int.MinValue`, and the checked
`(uint)` cast threw. For an `async` method the `EcmaMethod` is the
compiler-generated thunk, so debug builds instead assert while creating
the signature.
In the reported app, the failing inliner was the async variant of
`Task<Completion<...>>.WaitAsync(CancellationToken)` (a CoreLib generic
over an app type) inlining `TimeProvider.get_System()`.
The cause is the async inlining change in dotnet#125472. dotnet#133146 probably just
changed codegen enough to produce this case in the reported app. The bug
isn't Wasm-specific: the new test reproduces it on osx-arm64. Browser
CoreCLR R2R hits it first because its targets pass
`--opt-cross-module:*` by default.
## Fix
`CompileMethod` and `InliningInfoNode` now share
`ILBodyFixupSignature.GetSignatureMethodForCompiledMethod`, which
returns the typical `EcmaMethod` or `AsyncMethodVariant` that gets a
compiled method's own `Check_IL_Body` fixup, or null if it can't have
one. `InliningInfoNode` keeps one inliner entry per `EcmaMethod`, but
stores that identity for cross-module inliners. Cross-module inliners
with no such identity (async resumption stubs, return-dropping thunks,
and other wrappers) or whose identity is a compiler-generated async
thunk are not reported, since no `Check_IL_Body` import describes them.
Inliners inside the version bubble are still encoded by `EcmaMethod`
RID. When both variants inline the same method, the entry chosen doesn't
depend on enumeration order. The runtime reader (`inlinetracking.cpp`,
`GetILBodyTokenInfo`) uses only the module and token from these imports,
so the output means the same thing. The inlining info format is
unchanged.
## IL body checks now include runtime-async and synchronized flags
The IL body signature used by `Check_IL_Body` and `Verify_IL_Body`
covered the IL bytes, EH clauses, locals and referenced metadata, but
not the method's impl flags. Both variants of a method read the same IL
through the method token, so a method whose `miAsync` flag changed while
its IL stayed the same still matched, and stale cross-module inlined
code was used. `miSynchronized` had the same gap: a stale inline would
skip the lock.
`ReadyToRunStandaloneMethodMetadata` in crossgen2 and the VM now both
record `miAsync` (`0x04`) and `miSynchronized` (`0x08`) in the byte that
describes the locals table. Methods without these flags produce the same
blob as before. The bits are documented in `readytorun-format.md`. The
VM also rejects the fixup when the runtime method has no IL header, for
example the task-returning variant of a method that became
runtime-async, instead of reading a header that doesn't exist.
If crossgen2 and the runtime come from different sides of this change,
they reject each other's IL body checks for async or synchronized
methods. For `Check_IL_Body` that only costs the precompiled code.
`Verify_IL_Body`, used for Debug/Checked CoreLib, fails fast, so a local
build that mixes a new VM with an old CoreLib image needs a CoreLib
rebuild. The R2R version is unchanged, because older runtimes reject the
new blob rather than misread it.
## Validation
- Added `AsyncCrossModuleGenericInliner` to the R2R test suite. It fails
without the fix (on osx-arm64) and passes with it. It also covers a
method whose task-returning and async variants both inline the same
cross-module inlinee. The full `ILCompiler.ReadyToRun.Tests` suite
passes locally: 35 passed, 13 skipped for platform.
- The skip for stubs and thunks has no test. I couldn't get crossgen2 to
produce a return-dropping thunk that inlines something.
- Replayed the crossgen2 command line from the failing `dotnet-inspect`
browser CoreCLR R2R publish (`12.0.100-alpha.1.26472.116`) with a Debug
crossgen2 built from this branch. It now emits
`DotnetInspect.Web.Core.dll` without asserts.
- Added `src/tests/readytorun/ilbody-implflags-versioning`. It compiles
a consumer with `--opt-cross-module` against helper v1 and runs it
against v2, where the helper methods have byte-identical IL and differ
only in impl flags. The cases are a method that becomes runtime-async
(awaited, and called for its `Task`), one that stops being
runtime-async, and one that becomes synchronized. Without the IL body
change, all three async checks fail. Running against v1 fails all four
checks, including the synchronized one. With the change the test passes.
The existing `readytorun/tests` (`mainv1`/`v2`/`v3`) and
`async-inline-thunks` tests still pass. These were run locally on
osx-arm64 Debug.
Resolvesdotnet#134015
> [!NOTE]
> This PR description was generated with GitHub Copilot.
---------
Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copy file name to clipboardExpand all lines: docs/design/coreclr/botr/readytorun-format.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -336,7 +336,7 @@ ECMA 335 does not have a natural encoding for describing an overridden method. T
336
336
337
337
#### IL Body signatures
338
338
339
-
ECMA 335 does not define a format that can represent the exact implementation of a method by itself. This signature holds all of the IL of the method, the EH table, the locals table, and each token (other than type references) in those tables is replaced with an index into a local stream of signatures. Those signatures are simply verbatim copies of the needed metadata to describe MemberRefs, TypeSpecs, MethodSpecs, StandaloneSignatures and strings. All of that is bundled into a large byte array. In addition, a series of TypeSignatures follows which allow the type references to be resolved, as well as a methodreference to the uninstantiated method. Assuming all of this matches with the data that is present at runtime, the fixup is considered to be satisfied. See ReadyToRunStandaloneMetadata.cs for the exact details of the format.
339
+
ECMA 335 does not define a format that can represent the exact implementation of a method by itself. This signature holds all of the IL of the method, the EH table, the locals table, and each token (other than type references) in those tables is replaced with an index into a local stream of signatures. Those signatures are simply verbatim copies of the needed metadata to describe MemberRefs, TypeSpecs, MethodSpecs, StandaloneSignatures and strings. All of that is bundled into a large byte array. The byte that describes the locals table also records the method implementation flags that change how the same IL executes: `0x04` is set for `miAsync` (runtime-async) methods and `0x08` for `miSynchronized` methods. In addition, a series of TypeSignatures follows which allow the type references to be resolved, as well as a methodreference to the uninstantiated method. Assuming all of this matches with the data that is present at runtime, the fixup is considered to be satisfied. See ReadyToRunStandaloneMethodMetadata.cs for the exact details of the format.
Copy file name to clipboardExpand all lines: src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ILBodyFixupSignature.cs
+9Lines changed: 9 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -51,6 +51,15 @@ public ILBodyFixupSignature(ReadyToRunFixupKind fixupKind, MethodDesc signatureM
51
51
_signatureMethod=signatureMethod;
52
52
}
53
53
54
+
/// <summary>
55
+
/// Returns the method a compiled method's own IL body fixup is created for, or null if the compiled method cannot have one.
0 commit comments