Repository navigation
Conversation
On FEATURE_64BIT_ALIGNMENT targets, an auto-layout value type that is an Align8 candidate always got an alignment requirement of 8, even when a field required more. On wasm, Int128/UInt128, Vector128 and Decimal128 require 16, so an auto-layout struct containing them was 8-aligned by the runtime but 16-aligned by crossgen2 and by the runtime's own sequential layout. With composite ReadyToRun, crossgen2 bakes the field offsets of such a struct inside a class, so R2R code wrote past the end of the object and corrupted the GC heap. In BenchmarkDotNet runs this surfaced as "Invalid Program: attempted to call a UnmanagedCallersOnly method from managed code". Use max(8, largest field alignment) for Align8 candidates without GC pointers. ARM32 is unaffected since no type there requires more than 8. Re-enable readytorun/fieldlayout on browser CoreCLR and add a case for an auto-layout Int128 struct in a derived class. Fixes #135368 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 05ba961f-8f2d-42b8-ac78-8f138880e420
- Shorten the Align8 comment in HandleAutoLayout to the invariant. - Drop an assertion in the fieldlayout test that did not exercise the bug. - Add Wasm32 rows to TestAlignmentBehavior_AutoAlignmentRulesWithOSDependence so crossgen2's 16-byte alignment for auto-layout structs containing Int128/UInt128/Decimal128 is pinned as the layout the runtime must match. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 05ba961f-8f2d-42b8-ac78-8f138880e420
|
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: @dotnet/crossgen-contrib |
Member
Author
|
cc @dotnet/wasm-contrib |
Contributor
|
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara |
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
The logic appears consistent with crossgen2, but changing low-level wasm type layout, including wide-vector alignment, warrants final maintainer review.
0 open findings
What changed in this PR
Aligns CoreCLR’s wasm auto-layout rules with crossgen2 to prevent ReadyToRun field-offset and size mismatches.
Changes:
- Preserves field alignment above 8 bytes for Align8 auto-layout structs.
- Re-enables browser CoreCLR field-layout testing.
- Adds wasm alignment and Int128 regression coverage.
| File | Description |
|---|---|
methodtablebuilder.cpp |
Corrects auto-layout alignment calculation. |
fieldlayouttests.cs |
Adds an Int128 derived-class regression test. |
ArchitectureSpecificFieldLayoutTests.cs |
Adds Wasm32 alignment cases. |
🧠 Review effort: Balanced
radekdoulik
approved these changes
Oct 9, 2026
radekdoulik
left a comment
Member
There was a problem hiding this comment.
LGTM, it would be nice to also extend src/tests/Interop/StructPacking/StructPacking.cs
adamperlin
approved these changes
Oct 9, 2026
davidwrighton
approved these changes
Oct 9, 2026
This branch has not been deployed
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.
On
FEATURE_64BIT_ALIGNMENTtargets (ARM32 and wasm),MethodTableBuilder::HandleAutoLayoutset the alignment of an auto-layout value type that is an Align8 candidate to exactly 8, even when one of its fields requires more. On wasm,Int128/UInt128,Decimal128andVector128<T>require 16, so the runtime aligned an auto-layout struct containing them to 8 while crossgen2 (and the runtime's own sequential layout) align it to 16.The runtime now uses
max(8, largest field alignment)(still 8 when the struct contains GC pointers), the same rule crossgen2 applies inMetadataFieldLayoutAlgorithm.ComputeAutoFieldLayout. Field offsets inside the struct are unchanged; only the struct's own alignment, and therefore its tail padding and its placement as a field of another type, changes. On ARM32 no field requires more than 8, so behavior there is unchanged.Why this matters: perf pipeline
This fix is required to get the wasm CoreCLR composite ReadyToRun perf pipeline green. BenchmarkDotNet's generated
Runnable_Nclasses store benchmark arguments in an auto-layoutFieldsContainerstruct field. ForPerf_Int128(TryFormat,ToString,CopySign), that struct holdsInt128fields. With composite R2R, crossgen2 bakes its field offsets into the R2R code, so the code wrote the container at offset 16 while the runtime sized the object for offset 8, writing past the end of the object. The run then aborted with:The link between the out-of-bounds write and that particular fatal error is inferred (no stack connects them), but it is removed by this fix: with it, the locally reproduced
Perf_Int128composite R2R run completes for all of the previously failing benchmarks.Tests
readytorun/fieldlayoutis re-enabled on browser CoreCLR. ItsActiveIssuepointed at [wasm] Use ordinary struct alignment for Int128/UInt128 on wasm #131421 and attributed the failure to Int128 alignment, but the failure was this bug. Without the fix it fails--verify-type-and-field-layoutonAuto.Wrapper_int8x16x2(a Vector128 case), so the problem was not Int128-specific.Int128pair as a field of a derived class, read back through reflection so the check does not depend on the verify flag.Wasm32rows are added toTestAlignmentBehavior_AutoAlignmentRulesWithOSDependence, pinning crossgen2's 16/32 for the Int128/UInt128/Decimal128 auto wrappers.Validation:
dotnet test ILCompiler.TypeSystem.Tests --filter ArchitectureSpecificFieldLayoutTests: 44 passed, including the 3 new Wasm32 rows.fieldlayouttests.cssources published as a composite wasm R2R app with--verify-type-and-field-layout, run under node:Verify_FieldOffset 'Derived.Pair' Field offset 16!=8(actual), andVerify_TypeLayout 'Auto.Wrapper_int8x16x2' failed.Perf_Int128with--wasm-ready-to-run-composite: reproduced the fatal error; with the fix, theTryFormat,ToStringandCopySignbenchmarks (benchmarkId2, 5, 6, 9) run to completion.src/testsharness (per-test, non-composite crossgen with the verify flag); the browser R2R CI lane covers it. No ARM32 build.The cDAC needs no change: it reads stored field offsets and sizes, and
GetClassAlignmentRequirementmirrorsgetClassAlignmentRequirementStatic, which does not read the auto-layout alignment changed here.Follow-up (not in this PR): Vector256/Vector512 alignment on wasm (#135483)
With this change, auto-layout structs containing
Vector256<T>/Vector512<T>on wasm get alignment 32/64. That matches crossgen2 (both fall through to the generic branch inCheckForSystemTypes/VectorFieldLayoutAlgorithm), so there is no runtime/crossgen2 disagreement, but 32/64 is questionable for wasm:v128. Checked with clang 22 targeting wasm32:__BIGGEST_ALIGNMENT__is 16, the stack alignment isS128, andstruct { v128 lo, hi; }has alignment 16. Only clang's genericvector_size(32/64)extension types get 32/64, and those are not part of the ABI.Changing this is a layout and R2R compatibility decision. It should be made before wasm CoreCLR ships, as a separate change to both the VM and crossgen2. Tracked by #135483.
Resolves #135368
Note
This pull request description was generated with GitHub Copilot.