Skip to content

amd64: Preserve RAX on successful memory CMPXCHG - #107

Open
heartpunk wants to merge 4 commits into
angr:masterfrom
heartpunk:codex/amd64-cmpxchg-preserve-rax
Open

heartpunk wants to merge 4 commits into
angr:masterfrom
heartpunk:codex/amd64-cmpxchg-preserve-rax

Conversation

@heartpunk

@heartpunk heartpunk commented Nov 19, 2026 •

Copy link
Copy Markdown

When a 32-bit CMPXCHG compares against memory successfully, VEX incorrectly clears the upper half of the RAX register. This can cause angr to analyze an instruction using the wrong register value. This change preserves RAX on success while keeping the correct value on failure, with and without the LOCK prefix. We reproduced the error with tests of both outcomes: the two success cases failed before the fix, and all four cases passed afterward.

Details

For example, with RAX = 0x1234567811223344 and memory containing 0x11223344, the old lift incorrectly produces RAX = 0x0000000011223344 on success. The failure path still zero-extends the 32-bit memory value into RAX.

The Intel and AMD instruction entries assign the source to memory on equality and write the old memory value to EAX on inequality. Their register rules specify that a 32-bit GPR write zero-extends to 64 bits. Intel SDM Vol. 2A, CMPXCHG, Operation, p. 3-194 · Intel SDM Vol. 1, §3.4.1.1, p. 3-13 · AMD APM Vol. 3, CMPXCHG, p. 167 · AMD APM Vol. 1, §3.4.5, p. 74.

T.J. Purtell proposed preserving the full accumulator in both memory paths in 2011. The 2024 angr/VEX fix covered the register operand. Valgrind bug 275024 · Purtell’s original patch · angr/vex#63.

The regression lives in the PyVEX suite. Its two cases check the zero-flag condition and trace both inputs to the accumulator write in the lifted IR for locked and unlocked memory operands. Both failed with the old VEX pin (fd41c67), and the complete PyVEX suite passed 92/92 with the fix. Companion regression in angr/pyvex#610.

Separately, local concrete execution through angr 10.0.7 checked RAX, memory, and ZF for both 32-bit memory forms on both outcomes. Only the two successes failed on the parent. All four passed with the fix. A broader check of locked 8-, 16-, 32-, and 64-bit memory operands, unlocked 32-bit memory, and a 32-bit register operand also passed all 12 cases with the fix.

The functional fix was already present in be99aa9. A Codex subagent (gpt-6.2-astra, high reasoning effort) clarified the source comment, implemented the regression, and ran the tests reported above. A separate Codex subagent (gpt-6.2-astra, max reasoning effort) independently reviewed the VEX change.

@heartpunk
heartpunk marked this pull request as ready for review November 22, 2026 02:52
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.

1 participant