Skip to content

[release/11.0] ARM64: extend contained cast index in genLeaInstruction - #138226

Open
AndyAyersMS wants to merge 1 commit into
dotnet:release/11.0from
AndyAyersMS:backport-138205-release-11.0
Open

AndyAyersMS wants to merge 1 commit into
dotnet:release/11.0from
AndyAyersMS:backport-138205-release-11.0

Conversation

@AndyAyersMS

@AndyAyersMS AndyAyersMS commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Fixes Issue #138205

main PR #134466 (ARM64 genLeaInstruction part only)

Description

On ARM64, CodeGen::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 (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 store values[key.Id] = value bounds-checks w1 (Id), but the write-barrier address is computed from x1, which also contains Tag. With Tag = -1, the store writes outside the array, causing an AccessViolationException or silent memory corruption.

The fix emits add xd, xn, wm, UXTW/SXTW #scale according 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

  • Customer reported
  • Found internally
    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 AccessViolationException crash 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 int field of a register-passed struct, such as ValueTuple<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

Testing

The issue repro was run on an Apple M4 Max (osx-arm64). The control case uses Tag = 0.

  • Official .NET 11.0.0: 3/3 default runs crashed with AccessViolationException; each crashed with DOTNET_TieredCompilation=0, DOTNET_TC_OnStackReplacement=0, or DOTNET_TieredPGO=0. The control passed 3/3.
  • Same-source A/B on release/11.0 (f465948): one Release clr+libs+host build and Core_Root, then only clr.jit rebuilt with this change. libclrjit.dylib is the only file that differs between the two layouts.
Case Without fix With fix
Default (×3) 3/3 AV crash 3/3 pass
Control, Tag = 0 (×3) 3/3 pass 3/3 pass
TieredCompilation=0 crash pass
TC_OnStackReplacement=0 crash pass
TieredPGO=0 crash pass
Codegen for the store address add x14, x14, x1, LSL #3 add x14, x14, w1, UXTW #3

The 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 genLeaInstruction for a contained CAST/BFIZ index. It replaces one add with 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.

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
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 24, 2026
@azure-pipelines

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@AndyAyersMS
AndyAyersMS requested review from EgorBo and a balanced review from Copilot November 24, 2026 19:41
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@EgorBo ptal
fyi @dotnet/jit-contrib

@AndyAyersMS AndyAyersMS added the Servicing-consider Issue for next servicing release review label Nov 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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 UXTW or SXTW for contained CAST/BFIZ indices, 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 EgorBo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably backport the entire PR, but this works too.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI Servicing-consider Issue for next servicing release review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ARM64 JIT includes both ValueTuple<Int32,Int32> fields in a bounds-checked array index

4 participants