Repository navigation
Conversation
|
Static code audit of Overall this is a very clean fix for the CLWB hot-patch race in #278:
One small hardening suggestion
AI use: static code audit of |
|
MSVC run, since Not tested lists the MSVC toolchain. This PR's head Tested
Not tested
AI use
|
ivall#278) A CLWB was hot-patched by making its bytes writable, filling them with 0x90 one byte at a time and restoring the protection, with nothing shared between threads: - two threads patching on one page could restore each other's protection, leaving the page PAGE_EXECUTE_READWRITE or making it read-only while the other still wrote; - another thread could run the half-written bytes (90 0F AE 30 is a nop and then xsaveopt [rax]); - a thread that faulted on the CLWB just before another patched it read NOPs, which do not decode, and its fault went on to the title's handlers; - the instruction cache was not flushed. Now the patching runs under one lock, which also keeps the set of patched addresses. Only the first two bytes change, to a short jump over the instruction (EB, len - 2), written by a single lock cmpxchg so another thread runs either the whole CLWB or the whole jump. The old protection is put back and FlushInstructionCache is called. A fault on an address already patched resumes at the jump. A CLWB whose first two bytes cross a cache line, where no single store is atomic, is stepped over on each fault instead. Tests: the jump bytes; three CLWB lengths (one at an odd address) patched, the page PAGE_EXECUTE_READ afterwards, a late fault resumed, and the code run; a cache-line crossing stepped over; eight threads faulting on 64 CLWBs of one page at once while a watcher checks that each CLWB's first two bytes are only ever the old ones or the jump. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7d5f5f0 to
dfa332a
Compare
|
Tested on an RTX 5080, release build: pass at the current head
Not tested
AI use: Claude Code (Claude Opus 5.5, Claude Fable 5.2) |
|
Test run of Tested
Review: Hot-patching is serialized and the patched range's instruction cache is flushed, so a thread that faults on a CLWB while another patches the same site sees either the old bytes or the whole jump. I did not trace every interaction with the CLZERO loop handling (#278, #295). Not tested: no retail title or regression run (I have no unpacked game), no Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code. |
|
Test report for Tested
Not tested
AI use
The real-fault test module (appended to the end of
|
…CLWB Guard the late-fault resume path with code.first() == Some(&0xEB) so a stale CLWB_PATCHES entry after DLL unload and address reuse cannot spin on a genuine #UD at the same RIP. Add a regression check in clwb_is_patched_into_a_jump_and_the_page_keeps_its_protection and fix a minor offset comment in clwb_patching_from_many_threads_at_once. Co-Authored-By: Gemini 4 Neon <noreply@google.com>
|
Thanks @mastrr9 for the hardening note — pushed $sha\ checking \code.first() == Some(&0xEB) && clwb_patched(rip)\ so a stale \CLWB_PATCHES\ entry after a DLL unload / address reuse cannot spin on a genuine #UD, with a regression assertion in \clwb_is_patched_into_a_jump_and_the_page_keeps_its_protection\ (and fixed the offset comment nit). Verified \cargo test -p xcl --lib eh::tests::clwb -- --test-threads=1: 4 passed, 0 failed. Also huge thanks to @bunbeans2-arch for the real-fault i9-9900K verification (100/100), and @MesshM / @H0UNDZER0 / @nullmarsh for the independent MSVC + AMD/NVIDIA runs! (Note: \scripts/smoke.ps1\ will need a quick refresh on $sha\ to update the \PR description\ check.) (AI-assisted) |
Part of #278 (item 4: the CLWB hot-patch race). Items 1 and 2 (the CLZERO loop) are in #295 and #290; item 3 (
is_writable) is not in this PR.A CLWB was hot-patched like this: make its bytes writable, fill them with
0x90one byte at a time, put the protection back. Nothing was shared between threads, which caused four problems:PAGE_EXECUTE_READWRITE, or become read-only again while the other thread was still writing.90 0F AE 30is a nop followed byxsaveopt [rax].FlushInstructionCachewas never called.What is in it
FlushInstructionCache. The lock also guards the set of addresses already patched. Everything is incrates/xcl/src/eh.rs.EB, len - 2), written by a singlelock cmpxchg, so another thread runs either the whole CLWB or the whole jump. The bytes after the jump are left as they were.Tested
stable-x86_64-pc-windows-gnuwith llvm-mingw, debug build.emulate_console_only) directly on code pages registered as a title module and set toPAGE_EXECUTE_READ. The patched code is then actually executed.eh::tests, all passing:clwb_patch_is_a_short_jump_over_the_instruction: the jump bytes for lengths 4 to 15; no patch when the bytes cross a cache line or a page, or for an invalid length.clwb_is_patched_into_a_jump_and_the_page_keeps_its_protection:clwb [rax],clwb [rsp+8]at an odd address, andclwb [r8]. Only two bytes change, the page isPAGE_EXECUTE_READafterwards, a second (late) fault resumes at the jump, and the code runs.clwb_across_a_cache_line_is_stepped_overclwb_patching_from_many_threads_at_once: 8 threads fault on 64 CLWBs on one page at once, in different orders, some twice. A watcher thread checks that each CLWB's first two bytes are only ever the old bytes or the jump. Every fault is handled, every CLWB is patched, the page endsPAGE_EXECUTE_READ, and all the patched code runs. Passed 5 of 5 runs.cargo test --workspace --exclude testgame-mw --no-fail-fast -- --test-threads=4:xcllib 614 passed, 1 failed, 20 ignored. All other targets passed apart from thee2etarget, which had 1 failure. Neither failure involves this change:pe::tests::parses_own_test_binary_tlsexpects the test binary to have a PE TLS directory. Awindows-gnubinary doesn't have one; the MSVC target does.e2e::test_title_runs_and_game_folder_is_untouchedneedstestgame_mw.dll, andtestgame-mwwas excluded because it doesn't build with this local toolchain.fetch_updatedeprecation inhle/xgameruntime/mod.rs, was already there (see XGameRuntime: try_update instead of the deprecated fetch_update #251).Regression run
Windows 11, Ryzen 9 5900X, AMD Radeon RX 6800, Rust stable
x86_64-pc-windows-msvc(VS 2022 Build Tools, link.exe),cargo build --release -p xcl,scripts/smoke.ps1 -Seconds 45, one run at a time under a RAM watchdog.main (a6f5f7a, current
origin/main, this PR's base):PR (head commit, no local changes):
No regression: 790 frames against 828 on its base, no device removals or crashes, peak commit 15155 MB against 15144 MB. The 5900X has CLWB natively, so this run doesn't reach the hot-patching path; it only shows nothing else changed. Frame counts move by about 10 % between runs of the same commit on this machine (earlier runs of
maingave 843, 709 and 828), and more when other builds are running: a first run of this head, made while other builds were compiling on the machine, gave 524 frames, so I reran it once the machine was idle and give that run above.Rebased onto current
main(a6f5f7a): the one conflict, ineh.rs, is resolved by keepingmain's tests (cmp_eflags_matches_x86_64_sub_flags, the CLZERO loop test,code_bytes_on_the_instructions_page_are_not_asked_about) next to the CLWB ones. On the rebased head, built withstable-x86_64-pc-windows-msvc,cargo test -p xcl --lib ehpasses: 11 passed, 0 failed, including the fourclwb_*tests.Not tested
hook_gs_failure) doesn't take this lock. Those patches run at load time, not from the exception handler.AI use
🤖 Generated with Claude Code