Skip to content

cpu: lower CMPXCHG8B and CMPXCHG16B without LOCK - #628

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
danielradosa:cpu/cmpxchg8b-16b
Nov 23, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
danielradosa:cpu/cmpxchg8b-16b

Conversation

@danielradosa

@danielradosa danielradosa commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Change

The shared lowering in tools/ffx_version_query.py now accepts cmpxchg8b and cmpxchg16b with a memory operand. LOCK stays rejected by the prefix guard, as for cmpxchg.

  • Each compares EDX:EAX (RDX:RAX) with the operand. When they are equal it sets ZF and stores ECX:EBX (RCX:RBX), leaving RAX and RDX unchanged. Otherwise it clears ZF and loads the operand into EDX:EAX (RDX:RAX), zero-extending the 32-bit halves for cmpxchg8b. The other flags keep their values.
  • A cmpxchg16b operand that is not 16-byte aligned raises #GP(0), which Windows reports as an access violation reading address -1. INTEGER_HEADER gains raise_cmpxchg16b_alignment, which raises the same exception with the same two parameters.

They add 2 x64 forms to the coverage chart.

Verification

cpu_aot_cmpxchg_wide (tests/cpu/cmpxchg_wide.asm, 2 exports) loads RAX, RDX, RBX and RCX from a block, runs the instruction on an operand in the same block, and stores RAX, RDX, ZF and CF back. It compares the results with the native DLL and a reference model over 120 cases: equal operands, either half differing, register upper halves that cmpxchg8b must ignore on success and clear on failure, and both CF values. It also checks that cmpxchg16b operands at offsets 4 and 8 raise the same exception in both DLLs. The fixture saves RBX in the home area with .savereg unwind data, so the faulting cases still restore the caller's RBX. lock cmpxchg8b and lock cmpxchg16b stay rejected.

Windows 11 build 26300.9974, Ryzen 5 7600, MSVC 19.51.36256, Python 3.14.4, on main at 9b0df70:

ctest --preset dev --no-tests=error -R '^cpu_aot_cmpxchg_wide$' -V
Build Result
dev and release (/O2) 120 cases matched; offsets 4 and 8 raised exception: access violation reading 0xFFFFFFFFFFFFFFFF in both DLLs; both lock forms rejected
all cpu_ tests, dev 113 of 115 passed; the 2 failures are cpu_native_archive_reader and cpu_native_archive_blast, which need the Zig-built archive_reader.exe (ziglang is not installed on this machine). In one of three parallel (-j 8) runs cpu_native_startup also failed; it passed in the other two and in three runs on its own, and it does not use the shared lowering

Evidence and follow-up

The upper-half behaviour of cmpxchg8b follows the SDM's 32-bit register writes. On success RAX and RDX keep bits 32 to 63; on failure both are zero-extended. The native runs confirm this. A non-locked cmpxchg16b still writes the operand back when the comparison fails, but that write cannot be observed in a single thread, so it is not modelled.

enter and leave would need a stack model that the shared lowering does not have (it has no push or pop), so this PR leaves them out.

Companion PRs, independent of this one: fences and cache hints (#626), and the x87 status-word forms (#627). x64 coverage goes from 551 (the published chart for 9b0df70) to 573 of 948 with all three.

AI-assisted.

Each compares EDX:EAX (RDX:RAX) with its memory operand. When they are
equal it sets ZF and stores ECX:EBX (RCX:RBX); otherwise it clears ZF and
loads the operand into EDX:EAX (RDX:RAX), zero-extending the 32-bit
halves of CMPXCHG8B. The other flags keep their values, and LOCK stays
rejected by the prefix guard. A CMPXCHG16B operand that is not 16-byte
aligned raises #GP(0), which Windows reports as an access violation
reading address -1; the translation raises the same exception.

cpu_aot_cmpxchg_wide compares memory, RAX, RDX, ZF and CF with the native
DLL and a reference model over 120 cases, and checks that operands at
offsets 4 and 8 raise the same exception in both DLLs. The fixture saves
RBX in the home area with unwind data, so the fault restores it.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T16:24:25.309883Z 79c93c8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 79c93c8d71

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/ffx_version_query.py Outdated
"const uint64_t lo = load<uint64_t>(addr), hi = load<uint64_t>(addr + 8); "
"zero = lo == rax && hi == rdx; "
"if (zero) { store<uint64_t>(addr, rbx); store<uint64_t>(addr + 8, rcx); } "
"else { rax = lo; rdx = hi; } }")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve writeback on a compare mismatch

When a CMPXCHG16B comparison fails, the instruction still writes the original operand back, but this arm only reloads RAX/RDX. On a read-only page, native code therefore raises an access violation while the lifted code returns normally; the CMPXCHG8B mismatch arm has the same gap. Write the original value back on mismatch so protected operands preserve this behavior.

Useful? React with 👍 / 👎.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Verification of 79c93c8 on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029, dev preset. It merges cleanly with main.

ctest --preset dev --no-tests=error -R "^cpu_" -j 4
100% tests passed, 0 tests failed out of 115
cpu_aot_cmpxchg_wide: 120 CMPXCHG8B and CMPXCHG16B cases matched the reference in native and translated DLLs

I measured the open Codex finding (writeback on a compare mismatch) on hardware, and it reproduces.

Setup:

  • I used the built cpu_cmpxchg_wide.dll and cpu_cmpxchg_wide_lifted.dll unchanged, one child process per call.
  • The block sits on a read-write page. RDX (the operand offset) is 0x1000, so the 16-byte operand is the first bytes of the next page, set to PAGE_READONLY.
  • "Mismatch" means RDX:RAX differs from the operand. "Equal" means it matches.
Export Case Native Translated
cmpxchg8b_op mismatch access violation writing the operand returns normally, ZF=0, RDX:RAX loaded
cmpxchg16b_op mismatch access violation writing the operand returns normally, ZF=0, RDX:RAX loaded
cmpxchg8b_op equal access violation writing access violation writing
cmpxchg16b_op equal access violation writing access violation writing

The mismatch rows are a real native/translated divergence: the hardware performs the write cycle even when the comparison fails, and the translation does not. Writing the original operand back in the mismatch arm, as the review suggests, would close it. The same read-only-page setup would work as a regression case in tests/cpu/cmpxchg_wide.py, because RDX already selects the operand offset.

AI-assisted: measured and summarized with Claude Code; outputs copied from the local runs.

R0Y00 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

I pushed 40ec7cd so both wide forms preserve the destination write on a compare mismatch, including under /O2. The Windows Release differential passed all 120 value cases, both original alignment and LOCK controls, and the added read-only cases. On each form, both the equal and unequal compare recorded a write access violation at the operand and left the result slots untouched. Waiting for the refreshed CI on this commit.

@R0Y00
R0Y00 merged commit d998665 into R0Y00:main Nov 23, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants