Repository navigation
[release/11.0] ARM64: extend contained cast index in genLeaInstruction - #138226
AndyAyersMS wants to merge 1 commit into
Conversation
Backport of the ARM64 codegen part of dotnet#134466. genLeaInstruction dropped the sign/zero extension of a contained GT_CAST/GT_BFIZ index and computed the address with the full 64-bit source register. When the upper 32 bits held unrelated data (e.g. the second field of a register-passed ValueTuple<int, int>), the bounds check (32-bit) passed but the store address was far out of bounds. Emit "add xd, xn, wm, UXTW/SXTW #scale" according to the cast's signedness instead. Fixes dotnet#138205. Co-authored-by: Copilot <[email protected]> Copilot-Session: 7817a9ca-235e-4dbc-996b-a0f628817492
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
|
@EgorBo ptal |
There was a problem hiding this comment.
🔵 Needs a closer look
Because this change affects ARM64 address generation and managed memory safety without independent JIT/NativeAOT execution validation, a maintainer should confirm release validation coverage.
No open issues 🎉
What changed in this PR
Backports the ARM64 address-generation fix from #134466 to release/11.0, addressing #138205. The change prevents unrelated upper-register bits from corrupting array-store addresses after a successful bounds check.
Changes:
- Emits
UXTWorSXTWfor containedCAST/BFIZindices, preserving the cast’s signedness during address calculation. - Leaves other address-generation paths unchanged and excludes the upstream range-check changes.
Validation: No builds or tests were run during this review.
| File | Description |
|---|---|
src/coreclr/jit/codegenarmarch.cpp |
Preserves 32-bit index extension in ARM64 genLeaInstruction. |
Review effort: Balanced
EgorBo
left a comment
There was a problem hiding this comment.
I would probably backport the entire PR, but this works too.
Fixes Issue #138205
main PR #134466 (ARM64
genLeaInstructionpart only)Description
On ARM64,
CodeGen::genLeaInstructiondropped the sign/zero extension of a containedGT_CAST/GT_BFIZindex and computed the address with the full 64-bit source register (add xd, xn, xm, LSL #scale). If the upper 32 bits of that register hold unrelated data, the 32-bit bounds check passes but the computed address is far out of range.In the issue, a
(int Id, int Tag)value tuple is passed packed in one register. The array storevalues[key.Id] = valuebounds-checksw1(Id), but the write-barrier address is computed fromx1, which also containsTag. WithTag = -1, the store writes outside the array, causing anAccessViolationExceptionor silent memory corruption.The fix emits
add xd, xn, wm, UXTW/SXTW #scaleaccording to the cast's signedness, so the index is extended as part of the add. This is the ARM64 codegen change from #134466, applied verbatim. The range-check changes from that PR are not included.Customer Impact
Silent bad codegen on ARM64 (JIT and NativeAOT) causes out-of-bounds writes through a managed array store that passed its bounds check. Consequences range from an
AccessViolationExceptioncrash to silent heap corruption. The affected codegen is not OS-specific, but the failure has only been reproduced here on osx-arm64.It is triggered by ordinary safe C# code: an
intfield of a register-passed struct, such asValueTuple<int, int>, used as an array index. #136335 is another customer report of the same codegen bug, under NativeAOT. Workarounds require first identifying the affected code and then restructuring it, for example by passing the fields as separate parameters.Regression
The codegen pattern dates back to fd11209 (2022) and is present in .NET 8, 9, 10, and 11. Locally, the issue repro crashes on official .NET 10.0.15 and 11.0.0 and passes on a current .NET 12 daily build, which contains JIT: Fix several range check correctness bugs #134466.
Testing
The issue repro was run on an Apple M4 Max (osx-arm64). The control case uses
Tag = 0.AccessViolationException; each crashed withDOTNET_TieredCompilation=0,DOTNET_TC_OnStackReplacement=0, orDOTNET_TieredPGO=0. The control passed 3/3.release/11.0(f465948): one Releaseclr+libs+hostbuild and Core_Root, then onlyclr.jitrebuilt with this change.libclrjit.dylibis the only file that differs between the two layouts.Tag = 0(×3)TieredCompilation=0TC_OnStackReplacement=0TieredPGO=0add x14, x14, x1, LSL #3add x14, x14, w1, UXTW #3The change is identical to the code merged in main via #134466, where SuperPMI reported size-neutral ARM64 diffs (
LSL->UXTW). The fix has been in main since then.Risk
Low. The change is limited to ARM64
genLeaInstructionfor a containedCAST/BFIZindex. It replaces oneaddwith the extended-register form of the same instruction, with identical encoding size. Other targets and code paths are unaffected. The code has shipped in main/.NET 12 daily builds since #134466 merged.Note
This PR description was drafted with GitHub Copilot.