Skip to content

PE loader: rewrite console waits that cross an 8-byte word - #3548

Open
BBareth wants to merge 1 commit into
xcl-dev:mainfrom
BBareth:console-cpu/rewrite-waits-across-words
Open

BBareth wants to merge 1 commit into
xcl-dev:mainfrom
BBareth:console-cpu/rewrite-waits-across-words

Conversation

@BBareth

@BBareth BBareth commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

On a CPU without monitorx / mwaitx (Intel), the first fault rewrites a console wait in place, but only when the instruction fits in one aligned 8-byte word. GTA 6 has a monitorx at an address ending in 7, so its 3 bytes cross two words: rewrite_code refused it and every poll of that spin loop raised #UD and was emulated. This change rewrites such an instruction with one lock cmpxchg16b (the std::arch intrinsic) on its aligned 16-byte block. Only an instruction across a 16-byte boundary stays emulated. Part of #505.

What is in it

  • crates/xcl/src/eh.rs: rewrite_code uses an aligned 8-byte word when the instruction fits and its 16-byte block otherwise (replace_code_block, compare_exchange_16), with the same compare-and-retry and page protection as the 8-byte path.
  • Test a_rewrite_crossing_an_atomic_word_uses_its_16_byte_block (replaces a_rewrite_crossing_an_atomic_word_stays_emulated): sites at block offsets 6 and 7 are rewritten, one at offset 14 stays emulated, and the bytes around them are untouched. It fails without the change (at block offset 6: false).
  • Test compare_exchange_16_stores_on_a_match_and_reports_a_mismatch: a matching exchange stores, a failed comparison stores nothing and reports the value found, and the 16-byte blocks on either side keep their bytes.

Tested

  • RTX 5070 Ti, driver 32.0.16.2124; i7-12700K; Windows 11 Pro 26200; rustc 1.95.0; debug suite, release smoke.
  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4 on 3c29d4b (on main ff0c364): 2890 passed, 0 failed, 51 ignored.
  • SKIPPED on this adapter: 28: 22 no wave64; 2 no heap windows; 2 no d3d12SDKLayers.dll; 1 no D3D12 debug layer; 1 cannot disable the driver cache (no Developer Mode).
  • GTA 6 (Xbox 1.0.2.1), on an earlier local tree with other open PRs (not rerun on this base), 10 s WPR CPU trace at 700 s in the world: before, the render thread's samples were 49 % kernel, about 2,359 of them in the exception path from that monitorx; after, the site is gone from the kernel callers. The frame rate did not change (2–5 presents per 2 s): this saves CPU time, it is not what limits GTA 6 there.

Regression run

xcl-smoke v1
commit: d0832e614fa7d6ae027bb4feb27ccb9121f54a8f
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 120
exit: 0x0
frames shown: 3092 (PresentX 3092)
presents per 2 s (last 3): 43 45 61
device removals: 0
crashes: 0
peak commit: 31416 MB
xcl-smoke v1
commit: 3c29d4bcb7baea22aae59b5893e8aa03b00d276f
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 120
exit: 0x0
frames shown: 3028 (PresentX 3028)
presents per 2 s (last 3): 38 39 11
device removals: 0
crashes: 0
peak commit: 31958 MB

Each run follows one warm-up run of the same build; the A button is pressed once at 70 s.

Not tested

  • AMD CPUs: they run monitorx / mwaitx natively and never take this path.
  • The CLWB patch path does not take CODE_REWRITE (Console CPU emulation: ClzeroLoop decoder/semantics, is_writable guards, and CLWB hot-patch race #278). That is the same on main, also for the 8-byte path, and is not changed here.
  • The GTA 6 trace runs were made with the earlier inline-assembly form of the exchange, and on a tree with other open PRs, not on this branch alone.
  • The 120 s smoke does not show the CPU saving: XCL logs no count of emulated waits, so the smoke blocks only show that nothing regressed.

AI use

  • Claude Code, Claude Opus 5.5: found the cause in a WPR trace, wrote the fix, the test and this description, and ran the GTA 6 trace runs above.
  • Claude Code, Claude Fable 5.2: rebased the branch, replaced the inline assembly with the intrinsic after review, wrote the second test, ran the suite and the regression runs above and updated this description.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH label Nov 30, 2026
@thehollowedone

Copy link
Copy Markdown
Contributor

Source review of current head 29be3d1d: two issues need resolution before the 16-byte rewrite path is safe to use.

  1. The new cmpxchg16b assembly may lose its address. cell = in(reg) can be allocated to rbx. Rust's inline-assembly reference explicitly allows reg operands to use LLVM-reserved base registers. xchg {new_lo}, rbx then replaces the address in rbx with replacement bytes before cmpxchg16b [{cell}], so a cross-word wait can fault in the exception handler. The stable cmpxchg16b intrinsic avoids this register-allocation hazard; it returns the prior 128-bit value needed by the retry loop. Please test both successful exchange and failed comparison, including preservation of the surrounding bytes.

  2. CLWB can change the same page's protection concurrently. The new wait path holds CODE_REWRITE across its RWX/write/restore sequence, but the existing CLWB patch path does not. If CLWB first saves RX, then the wait saves RWX, CLWB can restore RX before the wait's write. That can fault inside the handler; another ordering can leave the page RWX. This underlying CLWB race is tracked in #278. Serialize CLWB's full protection/write/restore sequence with CODE_REWRITE, and add a controlled same-page CLWB plus offset-7 wait regression that checks the final protection is RX. The existing concurrent test covers two waits, so it misses this interleaving.

These are source-level findings. I did not run a build, GPU test, or title on this PR. AI use: Codex (GPT-6.2 Sol) reviewed the integration and existing tracker; Codex (GPT-6.2 Astra) traced the assembly and concurrency paths.

A monitorx / mwaitx on a CPU without them (Intel) is rewritten in place
at its first fault, but only when it fits in one aligned 8-byte word.
GTA 6 has a monitorx at an address ending in 7: its 3 bytes cross two
words, rewrite_code refused it, and every poll of that spin loop raised
#UD and was emulated. A 10 s WPR trace in the world showed about 2,359
kernel samples on the render thread in that exception path.

Such an instruction is now rewritten with one lock cmpxchg16b (the
std::arch intrinsic) on its aligned 16-byte block, with the same
compare-and-retry and page protection as the 8-byte path. Only one
across a 16-byte boundary stays emulated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@BBareth
BBareth force-pushed the console-cpu/rewrite-waits-across-words branch from 29be3d1 to 3c29d4b Compare December 1, 2026 07:28
@BBareth

BBareth commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Head is now 3c29d4b.

  1. Confirmed. cell = in(reg) could be given rbx, and the xchg would then replace the address. compare_exchange_16 now calls std::arch::x86_64::cmpxchg16b instead of asm!. The new test compare_exchange_16_stores_on_a_match_and_reports_a_mismatch covers a matching exchange, a failed comparison (nothing stored, the value found is returned) and the blocks on either side.
  2. Confirmed from the source, but it is not from this PR: on main the CLWB path already patches without CODE_REWRITE while the 8-byte wait rewrite holds it, so the same interleaving exists there. It is tracked in Console CPU emulation: ClzeroLoop decoder/semantics, is_writable guards, and CLWB hot-patch race #278. I left it out of this PR to keep it to one change, and listed it under Not tested.

Suite on 3c29d4b: 2890 passed, 0 failed, 51 ignored, 28 SKIPPED lines (as before). The smoke blocks in the description are rerun on this head.

AI use: Claude Code, Claude Fable 5.2 checked both points against the source, wrote the change and this reply, and ran the tests.

@github-actions github-actions Bot added the needs-pr-check Failing the PR description check (CONTRIBUTING.md) label Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: 7d86c5d
head: 3c29d4b
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2897 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: re-reviewed at 3c29d4b. Since 29be3d1 the hand-written assembly is replaced by the stable std::arch::x86_64::cmpxchg16b intrinsic (eh.rs ~450), with a new single-threaded test of the exchange. Only CPUs without monitorx / mwaitx (Intel) change; this run is on a Ryzen 7 7800X3D, which has monitorx, so the smoke run doesn't reach the new path. Findings: (1) replace_code_block (~457) still reads the block with a plain volatile read and returns early when it already shows the replacement; sound only while rewrites of a site are serialized by CODE_REWRITE, while the CLWB patch (~614) writes code bytes without that lock (formally a data race; #278, the same on main, now noted in the PR). (2) No concurrent test of two rewrites in one 16-byte block, unlike the 8-byte path (~1465). Fine: the intrinsic removes the old rbx juggling, the failure ordering (Acquire) is allowed, the new test checks a stored match, a reported mismatch and that neighbouring blocks are untouched, the 16-byte-aligned block stays within one page and cache line, lengths still come from insn::decode with a same-length replacement, and the earlier test fails without the change.
performance: 60 s smoke of a retail title, main 7d86c5d vs this merge (a21b7f0d): 1663 -> 1680 frames shown (101 %; last presents per 2 s: 60 60 60), peak commit 17153 -> 17475 MB, 0 device removals, 0 crashes.
verdict: safe to merge (on this AMD CPU the changed path isn't reached; an Intel run is what exercises it)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-pr-check Failing the PR description check (CONTRIBUTING.md) labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 3c29d4b

No open or merged PR makes the same change. #2618 (open) adds an older 8-byte-only rewrite_code. Main already has its own form of that, and this PR extends main's form to an aligned 16-byte block with cmpxchg16b.
No problems found in the diff. A 16-byte aligned block never crosses a page, the compare-and-retry reloads atomically after a torn first read, and the test covers offsets 6, 7 and 14 plus the bytes around them.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants