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>
TG0000
force-pushed
the
fix/clwb-hotpatch-race
branch
from
November 24, 2026 00:22
7d5f5f0 to
dfa332a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: 848 frames against 828 on its base, no device removals or crashes, peak commit 15149 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).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