Skip to content

JIT: Do not fold a relocatable constant into a block store address mode on Arm, LoongArch64 and RISC-V - #137602

Merged
jkotas merged 1 commit into
dotnet:mainfrom
SHAZAM-Analytics:fix/jit-blockstore-reloc
Nov 16, 2026
Merged

jkotas merged 1 commit into
dotnet:mainfrom
SHAZAM-Analytics:fix/jit-blockstore-reloc

Conversation

@SHAZAM-Analytics

Copy link
Copy Markdown
Contributor

This replaces #137128, which GitHub closed when the fork it came from was deleted by mistake. The commit is unchanged (d5ff0dc).

Fixes #136687

Problem

Lowering::ContainBlockStoreAddress for arm/arm64 (and its loongarch64 and riscv64 copies) turns ADD(base, CNS_INT) into a contained LEA(base, offset) whenever the constant fits the offset range. It does not check whether the constant needs a relocation. When the constant is a handle, such as a frozen object's address under NativeAOT, the handle becomes a plain address-mode offset. Codegen then materializes it with movz/movk and no relocation, so at run time the value is ILC's placeholder rather than an address and the block copy faults.

The xarch version builds its address mode through TryCreateAddrMode, which only folds constants for which ImmedValCanBeFolded is true, so x86 and x64 are not affected.

Minimal case: a loop over a preinitialized static readonly array of structs that copies each element to a local (an unrolled block copy):

struct Element { public int A, B, C, D, E, F, G, H, I, J, K, L, M; /* 52 bytes */ }

static readonly Element[] s_elements = new Element[] { new Element(0), /* ... */ };

static int SumElements()
{
    int sum = 0, index = 0;
    foreach (Element e in s_elements)
        sum += Checksum(e, index++);   // Checksum is NoInlining and takes Element by value
    return sum;
}

SumElements compiled for win-arm64 by the 10.0.14 ILCompiler:

movz    x0, #0x20B0
movk    x0, #66 LSL #16        ; the frozen array's handle, with no relocation
add     x0, x21, x0
ldp     q16, q17, [x0, #0x20]

With this change:

adrp    x0, [HIGH RELOC #0x421B58]
add     x0, x0, [LOW RELOC #0x421B58]
add     x0, x21, x0

Fix

ContainBlockStoreAddress returns early when the offset constant ImmedValNeedsReloc, so the ADD stays uncontained and the constant is emitted through the normal relocatable path. The same check is added in the loongarch64 and riscv64 implementations, which have the same shape.

Testing

Adds TestFrozenStructArrayElementCopy to the NativeAOT Preinitialization smoke test, which runs on the osx-arm64, linux-arm64 and windows-arm64 NativeAOT legs. It asserts the class is preinitialized and checks a checksum over every field of every element. Without the fix it faults at the first element copy on arm64; on x64 it passes either way.

I confirmed the codegen difference above by cross-compiling the test (in the release/10.0 copy of Preinitialization.cs) for win-arm64 with the 10.0.14 ILCompiler, once with its shipped arm64 JIT and once with the release/10.0 JIT rebuilt with this change. I have not run it on main's JIT, whose JIT-EE interface does not match a shipped ILCompiler.

ContainBlockStoreAddress on arm/arm64, loongarch64 and riscv64 turned
ADD(base, CNS_INT) into a contained LEA whenever the constant fit the
offset range, without checking whether the constant needs a relocation.
A frozen object handle under NativeAOT became a plain offset that codegen
materialized with movz/movk and no relocation, so the copy faulted.

Leave the ADD uncontained when the constant needs a relocation, matching
what TryCreateAddrMode does on xarch.

Fixes dotnet#136687
@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 15, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 15, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 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.

@SHAZAM-Analytics

Copy link
Copy Markdown
Contributor Author

For servicing: as agreed on #137129, a .NET 11 only backport is enough for us. This is one of the three related fixes to take together.

@jkotas

jkotas commented Nov 16, 2026

Copy link
Copy Markdown
Member

@AndyAyersMS Could you please review?

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

Validation used the release/10.0 JIT, leaving main-branch NativeAOT ARM64 codegen, runtime behavior, and regression coverage of the faulty containment path unconfirmed.

No open issues 🎉

What changed in this PR

Prevents JIT block-store address containment from discarding required relocations, addressing the NativeAOT frozen-array crash reported in #136687.

Changes:

  • Rejects relocation-bearing constants when forming block-store address modes on ARM/ARM64, LoongArch64, and RISC-V64.
  • Adds a NativeAOT regression test that verifies preinitialization and checks copied struct-array elements using a checksum.
File Description
src/​tests/​nativeaot/​SmokeTests/​Preinitialization/​Preinitialization.cs Adds and invokes the frozen struct-array copy regression test.
src/​coreclr/​jit/​lowerriscv64.cpp Prevents folding relocatable constants into RISC-V64 block-store offsets.
src/​coreclr/​jit/​lowerloongarch64.cpp Prevents folding relocatable constants into LoongArch64 block-store offsets.
src/​coreclr/​jit/​lowerarmarch.cpp Prevents folding relocatable constants into ARM/ARM64 block-store offsets.

Review effort: Balanced

@jkotas

jkotas commented Nov 16, 2026

Copy link
Copy Markdown
Member

/ba-g dead letter

@jkotas
jkotas merged commit a5d7014 into dotnet:main Nov 16, 2026
155 of 159 checks passed
jkotas pushed a commit to jkotas/runtime that referenced this pull request Nov 19, 2026
…de on Arm, LoongArch64 and RISC-V (dotnet#137602)

Fixes dotnet#136687

Return early in Lowering::ContainBlockStoreAddress when the offset constant
`ImmedValNeedsReloc`, so the `ADD` stays uncontained and the constant is
emitted through the normal relocatable path. The same check is added in
the loongarch64 and riscv64 implementations, which have the same shape.
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
jkotas added a commit that referenced this pull request Nov 20, 2026
…37652)

Fixes Issues #103073 and #136687

Main PRs:
- [#137129](#137129) - ARM64
out-of-range branch relocation
- [#137603](#137603) - Split large
ARM64 code sections so linkers can place range-extension thunks
- [#137602](#137602) - Preserve
relocations when folding block-store addresses

## Customer Impact

- [x] Customer reported
- [ ] Found internally

# Description

Large NativeAOT ARM64 binaries can exceed the range of direct branches.
This change splits managed code into sections so linkers can place
range-extension thunks, preserves out-of-range branch relocations, and
handles those thunks during runtime method lookup, thread suspension,
and unwinding. It also avoids folding relocatable constants into
block-store addresses on Arm, LoongArch64, and RISC-V.

# Customer Impact

Without the fix, linking large NativeAOT ARM64 applications can fail
with branch-out-of-range errors, or execution can fail when runtime code
encounters linker-inserted thunks. The included relocation fix also
prevents incorrect addresses and potential faults when copying
preinitialized struct data on affected architectures.

# Regression

No.

# Testing

- Customer validated the fix
- Added smoke tests cover the thunk and relocation scenarios.

# Risk

Moderate. Relatively large change for servicing, it may not fix the
scenario completely.

---------

Co-authored-by: Services <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Copilot App <[email protected]>
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 community-contribution Indicates that the PR has been added by a community member

Projects

None yet

5 participants