Skip to content

[release/11.0] JIT x64: fix register allocation for Interlocked.Or/And with used result - #138550

Open
EgorBo wants to merge 2 commits into
dotnet:release/11.0from
EgorBo:backport-138275-release-11.0
Open

EgorBo wants to merge 2 commits into
dotnet:release/11.0from
EgorBo:backport-138275-release-11.0

Conversation

@EgorBo

@EgorBo EgorBo commented Nov 30, 2026

Copy link
Copy Markdown
Member

Fixes Issue #138258

main PR #138275

Description

Backport of #138275 to release/11.0. When the result of Interlocked.Or/Interlocked.And is used, LSRA could give the cmpxchg loop the same register for the address and the new value. LSRA now builds the internal register uses before the def, like other nodes do.

Customer Impact

  • Customer reported
  • Found internally

Tier0 code that uses the result of Interlocked.Or/And (e.g. passes it to a call) writes through current | value as if it were the address. This causes NullReferenceException, fatal AVs, or silent memory corruption. Every method with this pattern starts in Tier0, so default configurations hit it.

Regression

  • Yes
  • No

Works on .NET 10, broken in 11.0.0.

Testing

Test is backported.

Risk

Low. A one-line change in the LSRA build order for these nodes, matching what arm64 already does.

Note

PR description generated with GitHub Copilot.

EgorBo and others added 2 commits November 30, 2026 23:35
…ult (dotnet#138275)

Fixes dotnet#138258

When the result of `Interlocked.Or`/`Interlocked.And` is used, xarch
emits a `cmpxchg` loop that needs an internal register. LSRA built the
internal register use *after* the fixed-RAX def of the node. When that
def has a def-use conflict (e.g. consumed by `PUTARG_REG` in `rdi` on
SysV), LSRA defines it in the use's fixed register, evicting the
still-live internal register, which then got "reloaded" into the address
register:

```asm
mov      rcx, rax
or       rcx, rdx
lock
cmpxchg  qword ptr [rcx], rcx   ; rcx is both the address and the new value
```

Checked builds assert `genExactlyOneBit(availableSet)` instead. The fix
builds internal uses before the def, as other nodes do. Arm64 already
uses that order; other targets don't use internal registers here.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 00944461-fda2-4219-ac82-621f01c20bbe
(cherry picked from commit 8895186)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 8c6b9802-8643-479e-bbb4-f4e04e60fc0a
@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 30, 2026
@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.

@EgorBo
EgorBo requested a review from jakobbotsch November 30, 2026 22:46

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants