PE loader: patch a CLWB with one atomic jump under the rewrite lock #3588
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3588"
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?
Imported from ivall/xcl. Originally posted by BBareth on 2026-12-01T17:18:08Z.
On a CPU without CLWB (older Intel), XCL patches the instruction out at its first fault. The patch made the bytes writable, filled them with NOPs one at a time and restored the protection, with nothing shared between threads. This is item 4 of #278, the part #321 addressed before it was closed with conflicts; #571 fixed only the single-step case. Part of #278.
What could go wrong on
main:CODE_REWRITE. The page could stay writable, or turn read-only while the other thread still wrote.90 0F AE 30is a nop followed byxsaveopt [rax]).The patch now goes through
rewrite_code, like the waits:EB, len - 2), in one atomic word write underCODE_REWRITE.A site whose first two bytes cross an aligned 8-byte word cannot take the jump in one write. Only its second opcode byte changes,
AEto1F:0F 1F /6is a reserved NOP of the same length. A one-byte write cannot be seen half done by another core. Changing the ModRM byte as well, as the previous head did, could let a core fetch the old opcode with the new ModRM (0F AE /0, FXSAVE). The opcode is found past the prefixes of one copy of the bytes, so the scan cannot match0F AEinside the displacement of a site another thread patched in the meantime.What is in it
crates/xcl/src/eh.rs, theConsoleOnly::Clwbarm ofemulate_console_only:rewrite_code(rip, first two bytes, [EB, len - 2])instead of its own protect / fill / restore;rewrite_code_within, which records the site with its unchanged prefix so a stale fault is retried there too;clwb_opcode_atfinds the opcode past the prefix bytes;clwb_and_a_wait_patched_at_once_on_one_page_keep_its_protection: 32 threads fault at once on a CLWB and on amonitorxin one page. Every fault is handled, both sites are patched whole, and the page is read-execute afterwards.clwb_across_an_atomic_word_becomes_a_nop_with_its_operand: a CLWB at word offset 7, with and without a REX prefix, becomes66 0F 1F 30/66 41 0F 1F 74 24 08. The bytes around it stay, and a second fault at the site is retried there.clwb_opcode_is_found_past_the_prefixes_only: a displacement that holds0F AEis not taken for the opcode.clwb_single_step_keeps_code_and_normal_faults_still_patch(changed): expects the jump and the untouched tail, and checks that a second fault at the patched address is retried there.Tested
cargo build --workspace, thencargo test --workspace --no-fail-fast -- --test-threads=4onf2179fc0(on main078358a0): 3009 passed, 0 failed, 52 ignored.SKIPPEDon this adapter, 28 in all:no wave64no heap windowsno d3d12SDKLayers.dllno D3D12 debug layerillegal instruction ... code bytes 90 90 90 90 90 90(the late faulter), and one run ended inSTATUS_ACCESS_VIOLATION(the protection race).66 0F 1F /6with a null operand register was run on this CPU: it is a NOP and touches no memory.Regression run
Each run follows one warm-up run of the same build; the A button is pressed once at 70 s. This CPU executes CLWB, so the changed code did not run in them.
Not tested
CONTEXT. The smoke runs are a regression check only.clwb_patches_instruction_bytes_to_nop_and_advances_rip, which expects NOP bytes at an ordinary site; whichever of the two lands second has to adjust that test.rewrite_code; whichever lands second needs a rebase. #3548 no longer widens the write to 16 bytes, so the word rule here stays the same.AI use
🤖 Generated with Claude Code
Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-01T17:59:15Z.
merge-check
base:
078358a058head:
f2179fc05dmerge: clean (one commit, directly on that base)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3009 passed, 0 failed, 52 ignored (base: 3006 passed, 0 failed, 52 ignored); SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps), the same set as base; failing: none
review: rerun on the new head; read the whole diff (
eh.rs, +144 -10). No correctness problem found. My report on81ad460dpassed the two-byte form that bmjask then objected to; this head no longer has it.clwb_opcode_atskips the same prefix bytes asinsn::decode, so it finds the opcode of every CLWBdecodeaccepts.eh::tests::at--test-threads=8: 30 of 30.0F AEanywhere in the instruction failsclwb_opcode_is_found_past_the_prefixes_only; writing the ModRM byte as well failsclwb_across_an_atomic_word_becomes_a_nop_with_its_operand.emulate_console_only. 3 runs, each 12,000 sites at word offset 7 and 12,000 that take the jump: no crash, and the operand's memory was not written. The previous two-byte form passed the same test 3 of 3, so this shows that the new form works, not that the old one failed.clwb_opcode_atwas put betweenrewrite_code_withinand its doc comment, so the two lines starting "rewrite_codefor bytesskipinto the instruction" now document the wrong function.performance: none on CPUs with CLWB. Without it, each site is patched once, as on base (reasoned, not measured).
verdict: safe to merge
hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
Earlier reports (
81ad460d,9ac69984)Rerun on the new head (
81ad460d, the cross-word fix after Foniass's review). The earlier report, on9ac69984, is below.merge-check
base:
f07e426dadhead:
81ad460d7fmerge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2906 passed, 0 failed, 51 ignored (base: 2904 passed, 0 failed, 51 ignored); SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps), the same set as base; failing: none
review: read the change since
9ac69984(eh.rs). No correctness problem found.rip & 7 == 7) now gets0F 1Fwith the same ModRM (modrm & 0xC7clears the/6reg field), SIB and displacement, throughrewrite_code_within. That is a NOP of the same length, and the prefixes before and the operand after stay.AE, ModRM) are always inside the next aligned word, so one CAS covers them, and the first word is untouched.skipbytes, so a thread that faulted on the old bytes finds0F 1F(not a console instruction), matchesrewritten_wait, and is retried. The test checks that.eh::tests::clwb_across_an_atomic_word_becomes_a_nop_with_its_operand,clwb_and_a_wait_patched_at_once_on_one_page_keep_its_protection,clwb_single_step_keeps_code_and_normal_faults_still_patch.eh::tests::at--test-threads=8: 30 of 30 ok.clwb_across_an_atomic_word_becomes_a_nop_with_its_operand. Good.performance: none on CPUs with CLWB. Without it, every site, including those crossing a word, is patched once.
verdict: safe to merge
hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
Earlier report on
9ac69984(base6bfb5c51)Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T20:27:56Z.
Review (head
9ac69984, diff read, not a merge-check):rewrite_code(eh.rs~454) returns false when the instruction's first two bytes start at offset 7 of an aligned 8-byte word, and the CLWB arm (~585) ignores that and only steps RIP, so the site is never patched and every execution goes through the vectored handler again. Onmainsuch a site was patched to NOPs once, whichb25582a39added precisely to avoid repeated exceptions in tight loops. On a CPU without CLWB this is a default-path cost the PR does not measure; these sites need a fallback that still patches them once.clwb_patches_instruction_bytes_to_nop_and_advances_rip, which expects NOP bytes at the site. Whichever of the two lands second breaks that test.Fine: the patch now takes
CODE_REWRITE, so it cannot restore page protection over another rewrite on the same page; the two-byteEB len-2jump goes in with one CAS and leaves no half-written instruction; a thread that faulted on the old bytes is retried throughrewritten_wait(EBdoes not decode, so it reaches that path); the tests check the jump bytes, the untouched tail, the stale-fault retry and the page protection.AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by BBareth on 2026-12-01T21:49:29Z.
On the review of
9ac69984: both points checked against the source; the first is fixed in81ad460d.One CLWB site in eight faulted at every visit: right.
rewrite_coderefuses a rewrite that crosses an aligned word, and the arm only stepped RIP then, so a site at word offset 7 was never patched, wheremainpatched it once. Such a site now gets a second form instead of the jump: its opcode and ModRM bytes, which are both in the second word, become those of the NOP with the same operand (0F AE /6to0F 1F /0, e.g.66 0F AE 30to66 0F 1F 00). Prefixes and operand bytes stay, the length is the same, it is one atomic write underCODE_REWRITE, and the site is recorded with its unchanged prefix (rewrite_code_within), so a thread that faulted on the old bytes is retried there as for the jump. Every site is patched once again. The test is nowclwb_across_an_atomic_word_becomes_a_nop_with_its_operand(offset 7, with and without a REX prefix: the patched bytes, the bytes around them, the retry of a stale fault).Clash with #1829: yes, its
clwb_patches_instruction_bytes_to_nop_and_advances_ripexpects NOP bytes at an ordinary site and this PR writesEB, len - 2there. Whichever lands second has to adjust that test; I added it to Not tested and will rebase if #1829 goes first.Suite on
81ad460d: 2906 passed, 0 failed, 51 ignored, 28 SKIPPED lines. The smoke block in the description is rerun on this head (3082 frames, no device removal, no crash). Not done: the NOP form was not executed on a CPU, the test checks bytes.AI use: Claude Code, Claude Fable 5.2 checked the points against the source, wrote the change and this comment and ran the tests.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T21:53:24Z.
Follow-up review (head
81ad460d, since9ac69984, diff read, not a merge-check):eh.rs~597-601) now writes a NOP with the same operand (0F AE /6to0F 1F /0, prefixes and operand bytes kept, same length) with one CAS throughrewrite_code_within(~454), which records the site with its prefix bytes so a stale fault is retried throughrewritten_wait.clwb_across_an_atomic_word_becomes_a_nop_with_its_operand(~1718) checks both encodings, the surrounding bytes and the retry.clwb_patches_instruction_bytes_to_nop_and_advances_ripstill expects NOP bytes where this PR writesEB, len - 2; the description now lists it, so whichever lands second adjusts that test.New:
rewrite_code. #3548 rewrites the body ofrewrite_code(16-bytecmpxchg16bpath,remember_rewritten_wait(at, new)), which this PR moves intorewrite_code_within(~454) and changes (~476), so whichever lands second needs a rebase. With #3548, an offset-7 CLWB could take the jump inside its 16-byte block and the NOP fallback would only be needed at offset 15; neither description mentions the other.Fine: only the opcode and ModRM bytes change, both in one aligned word;
0Fis never a prefix byte so the first0F AEfound is the opcode;modrm & 0xC7keepsmod/rm, so SIB, displacement and length stay valid; the jump path, lock, protection restore and TF case are unchanged.AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:18:58Z.
Code review location:
crates/xcl/src/eh.rs; line 601Critical: Not certain about this one, because it depends on how the CPU handles code modified on another core without serialization: the NOP form rewrites the ModRM reg field (/6 to /0) as well as the opcode, in the middle of a live instruction, with no other core serialized first. A core that fetches a mix of old and new bytes can decode
66 0F AE /0, which is FXSAVE, not a NOP. For site66 0F AE 30at word offset 7, thread B writes[1F 00]over[AE 30]while core A is fetching it. Intel only guarantees stale-or-new for the first byte, so if core A combines the old opcodeAEwith the new ModRM00, it runsfxsave [rax]and writes 512 bytes at rax, corrupting title memory. Changing only the 0xAE byte (to0F 1F /6, also a reserved NOP of the same length) is a single-byte write, so it can never be seen half-done and works at every word offset.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:18:58Z.
Code review location:
crates/xcl/src/eh.rs; line 599Medium: The search for
0F AEreads live code memory afterdecodehas run, so a concurrent patch can make it skip the real opcode (now0F 1F) and match0F AEbytes inside the displacement. It then rewrites displacement bytes and replaces the registry entry with a different byte string. For66 0F AE 35 0F AE xx xx(rip-relative, disp32 starting0F AE) at rip&7 == 7 with two threads faulting at once, B patches the opcode, then A finds i=4 and CASes the displacement[AE, xx]to[1F, xx&C7], so the site's bytes change a second time andremember_rewritten_waitstores a longer prefix. Taking the opcode offset from the decoded prefix length, not a scan of mutable memory, would avoid it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:18:58Z.
Review: COMMENTED
Review of
81ad460d: 2 finding(s) (1 critical, 1 medium), most severe first.crates/xcl/src/eh.rs:601: Not certain about this one, because it depends on how the CPU handles code modified on another core without serialization: the NOP form rewrites the ModRM reg field (/6 to /0) as well as the opcode, in the middle of a live instruction, w.... (inline)crates/xcl/src/eh.rs:599: The search for0F AEreads live code memory afterdecodehas run, so a concurrent patch can make it skip the real opcode (now0F 1F) and match0F AEbytes inside the displacement. (inline)These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:19:04Z.
@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.
Imported from ivall/xcl. Originally posted by BBareth on 2026-12-03T01:37:11Z.
Code review location:
crates/xcl/src/eh.rs; line 601Agreed, fixed in
f2179fc0. The cross-word form now changes only the second opcode byte,AEto1F, and leaves the ModRM byte as it is:0F 1F /6is a reserved NOP of the same length. A one-byte write can only be seen as the old or the new byte, so a core can no longer combine the old opcode with a new ModRM (0F AE /0, FXSAVE). The test now expects66 0F 1F 30and66 41 0F 1F 74 24 08.Imported from ivall/xcl. Originally posted by BBareth on 2026-12-03T01:37:12Z.
Code review location:
crates/xcl/src/eh.rs; line 599Fixed in
f2179fc0. The scan now takes one copy of the bytes and looks for0F AEonly right after the prefix bytes (clwb_opcode_at: legacy prefixes and REX, then the opcode must follow). A site another thread patched to0F 1Fin the meantime no longer matches, and nothing inside the displacement is ever taken for the opcode. Test:clwb_opcode_is_found_past_the_prefixes_only.Imported from ivall/xcl. Originally posted by BBareth on 2026-12-03T01:37:15Z.
Updated to
f2179fc0(rebased on main078358a0) after @bmjask's review of81ad460d. Both points are fixed; replies are in the threads:AEto1F;0F 1F /6is a NOP of the same length), so no core can fetch a half-changed instruction.@Foniass, on #3548: I've reworked that PR so it no longer has the 16-byte
cmpxchg16bpath. It changes only bytes inside one aligned word, like this one, so the CLWB fallback is still needed at offset 7. The two PRs still both touchrewrite_code, and whichever lands second gets rebased.Suite on
f2179fc0: 3009 passed, 0 failed, 52 ignored, 28 SKIPPED. Smoke blocks in the description were rerun on this head. Still not done: a real CLWB fault (this CPU has CLWB).AI use: Claude Code (Claude Opus 5.5) made the fixes, ran the suite and the smoke runs, and wrote these replies.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-03T01:39:50Z.
Follow-up review of
f2179fc0, since81ad460d(code reading only, not a merge-check) · no new findingsIn short: This push changes how a CLWB whose first two bytes cross an aligned word is patched. Only its second opcode byte is now rewritten,
AE→1F.66 0F 1F /6is a reserved NOP of the same length, and a one-byte write can never be seen half done by another core, which before could fetch the old opcode with a new ModRM byte (66 0F AE /0, FXSAVE). The opcode is now found past the prefixes of one copy of the bytes, so a site another thread already patched is not matched inside its displacement. Both have tests. The two overlaps are now in the description, and nothing blocks it.Checked and fine
0F 1Fwith any ModRMregis in the hint-NOP space and runs as a NOP. Keeping/6and the operand keeps the length and the SIB / displacement bytes valid, as the test expectations show.clwb_opcode_at(eh.rs) skips the legacy and REX prefixes (66 67 F2 F3 2E 3E 26 36 64 65 40-4F) and requires0F AEright after them. A copy patched sincedecoderead it (0F 1Fwith0F AEin its displacement) returnsNone.clwb_opcode_is_found_past_the_prefixes_onlycovers both encodings and that case.rewrite_code_within(rip, i + 1, &[0xAE], &[0x1F])writes one byte through the same CAS, records the site, and keeps the retry throughrewritten_wait.rewrite_codeare both stated in the description now. #3548 no longer widens the write to 16 bytes.Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.