cpu: lower CMPXCHG8B and CMPXCHG16B without LOCK #628
No reviewers
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows!628
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/628/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@danielradosa on GitHub · 2026-11-22 16:21 UTC · original
danielradosa:cpu/cmpxchg8b-16b→main· +276 −0 · 6 files · 3 commits · merged 2026-11-23 05:49 UTCChange
The shared lowering in
tools/ffx_version_query.pynow acceptscmpxchg8bandcmpxchg16bwith a memory operand.LOCKstays rejected by the prefix guard, as forcmpxchg.cmpxchg8b. The other flags keep their values.cmpxchg16boperand that is not 16-byte aligned raises #GP(0), which Windows reports as an access violation reading address -1.INTEGER_HEADERgainsraise_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 thatcmpxchg8bmust ignore on success and clear on failure, and both CF values. It also checks thatcmpxchg16boperands at offsets 4 and 8 raise the same exception in both DLLs. The fixture saves RBX in the home area with.saveregunwind data, so the faulting cases still restore the caller's RBX.lock cmpxchg8bandlock cmpxchg16bstay rejected.Windows 11 build 26300.9974, Ryzen 5 7600, MSVC 19.51.36256, Python 3.14.4, on
mainat9b0df70:devandrelease(/O2)exception: access violation reading 0xFFFFFFFFFFFFFFFFin both DLLs; bothlockforms rejectedcpu_tests,devcpu_native_archive_readerandcpu_native_archive_blast, which need the Zig-builtarchive_reader.exe(ziglangis not installed on this machine). In one of three parallel (-j 8) runscpu_native_startupalso failed; it passed in the other two and in three runs on its own, and it does not use the shared loweringEvidence and follow-up
The upper-half behaviour of
cmpxchg8bfollows 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-lockedcmpxchg16bstill writes the operand back when the comparison fails, but that write cannot be observed in a single thread, so it is not modelled.enterandleavewould need a stack model that the shared lowering does not have (it has nopushorpop), 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.
@chatgpt-codex-connector[bot] on GitHub · 2026-11-22 16:21 UTC · original · edited 2026-11-22 16:24 UTC
Codex Review Summary: ✅ Completed
This comment shows the latest Codex review activity on this pull request.
79c93c8ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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[bot] on GitHub · 2026-11-22 16:24 UTC · original
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 on GitHub · 2026-11-22 18:19 UTC · original
Verification of
79c93c8on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029,devpreset. It merges cleanly withmain.I measured the open Codex finding (writeback on a compare mismatch) on hardware, and it reproduces.
Setup:
cpu_cmpxchg_wide.dllandcpu_cmpxchg_wide_lifted.dllunchanged, one child process per call.0x1000, so the 16-byte operand is the first bytes of the next page, set toPAGE_READONLY.cmpxchg8b_opcmpxchg16b_opcmpxchg8b_opcmpxchg16b_opThe 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 on GitHub · 2026-11-23 05:34 UTC · original
I pushed
40ec7cdso 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.@chatgpt-codex-connector[bot] on GitHub · 2026-11-22 16:24 UTC · original · reviewed
💡 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
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".