cpu_emulation: CLWB rewrite in place without the race between threads (follow-up of #1472) #2559

Manually merged
github merged 3 commits from github-bot/gh-2559 into main 2026-11-28 04:47:27 +00:00 AGit
Owner

@⁠OreoFR on GitHub · 2026-11-28 04:37 UTC · original
OreoFR:fix/clwb-patch-race → main · +210 −1 · 2 files · 3 commits · merged 2026-11-28 04:43 UTC

Item: RT-CLWB-PATCH-RACE (claimed in #1608: #1608)
Depends on: #1472

DadaCap asked for this pull request: #1472's author is inactive, and a review on their side found a race in it. This branch is #1472 + a merge of main + the fix, so it can replace #1472.

The race in #1472

#1472 rewrites a CLWB site in place at its first #UD (as CLFLUSHOPT, or as 66 0F 1F /0 without CLFLUSHOPT). Threads of a worker reach the same site together. A thread whose #UD is handled after another thread rewrote the site reads the new bytes, decode() gives Op::none, and the handler returned EXCEPTION_CONTINUE_SEARCH: the process crashes on an instruction that now runs fine. Two threads could also unprotect and protect the same page at once, and leave it read-write-execute.

The fix (runtime/src/cpu_emulation.h)

  • clwb_rewritten(): a #UD on bytes that are the form this host's mode writes (66 0F AE /7, or 66 0F 1F /0 and the halfway /6, memory operand only) returns EXCEPTION_CONTINUE_EXECUTION; RIP is unchanged, so the CPU runs the rewritten instruction.
  • patch_clwb(): re-check, VirtualProtect, the write, the restore and FlushInstructionCache run under one SRWLOCK. A site is written once. For the NOP form the opcode byte is written before ModRM, so a thread running the site in between sees 66 0F 1F /6, also a NOP.
  • The site counter only counts the thread that wrote.

Test (runtime/test/cpu_emulation_test.cpp)

test_clwb_rewrite() hands the handler a made-up #UD on clwb [rcx]; ret sites in PAGE_EXECUTE_READ memory, so it runs on hosts with CLWB too: (a) the first #UD, (b) a #UD on an already rewritten site, (c) 16 threads × 2048 sites at once. It checks continuation, rewrite, count and page protection, and is skipped with LEONIDA_CLWB_PATCH=0. No game files.

  • Red (#1472 + main): 4 failures, e.g. 27958 of 32768 concurrent #UDs not continued, 4810 sites counted for 2048.
  • Green: ctest -R ^cpu_emulation$ passes, 13 runs out of 13.

Tested on an Intel Core Ultra 7 155H (has CLWB and CLFLUSHOPT natively, so the real #UD path and the NOP mode are not exercised here), RTX 4070 Laptop, NVIDIA 621.40, Windows 11, MSVC 2022 RelWithDebInfo. An independent review found no blocking issue; known nit: in NOP mode, if the two written bytes straddle a cache line, another core could in theory fetch a mixed state.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

**@⁠OreoFR** on GitHub · 2026-11-28 04:37 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2559) `OreoFR:fix/clwb-patch-race` → `main` · +210 −1 · 2 files · 3 commits · merged 2026-11-28 04:43 UTC Item: RT-CLWB-PATCH-RACE (claimed in #1608: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) Depends on: #1472 DadaCap asked for this pull request: #1472's author is inactive, and a review on their side found a race in it. This branch is #1472 + a merge of main + the fix, so it can replace #1472. ## The race in #1472 #1472 rewrites a CLWB site in place at its first #UD (as CLFLUSHOPT, or as `66 0F 1F /0` without CLFLUSHOPT). Threads of a worker reach the same site together. A thread whose #UD is handled after another thread rewrote the site reads the new bytes, `decode()` gives `Op::none`, and the handler returned `EXCEPTION_CONTINUE_SEARCH`: the process crashes on an instruction that now runs fine. Two threads could also unprotect and protect the same page at once, and leave it read-write-execute. ## The fix (`runtime/src/cpu_emulation.h`) - `clwb_rewritten()`: a #UD on bytes that are the form this host's mode writes (`66 0F AE /7`, or `66 0F 1F /0` and the halfway `/6`, memory operand only) returns `EXCEPTION_CONTINUE_EXECUTION`; RIP is unchanged, so the CPU runs the rewritten instruction. - `patch_clwb()`: re-check, `VirtualProtect`, the write, the restore and `FlushInstructionCache` run under one SRWLOCK. A site is written once. For the NOP form the opcode byte is written before ModRM, so a thread running the site in between sees `66 0F 1F /6`, also a NOP. - The site counter only counts the thread that wrote. ## Test (`runtime/test/cpu_emulation_test.cpp`) `test_clwb_rewrite()` hands the handler a made-up #UD on `clwb [rcx]; ret` sites in `PAGE_EXECUTE_READ` memory, so it runs on hosts with CLWB too: (a) the first #UD, (b) a #UD on an already rewritten site, (c) 16 threads × 2048 sites at once. It checks continuation, rewrite, count and page protection, and is skipped with `LEONIDA_CLWB_PATCH=0`. No game files. - Red (#1472 + main): 4 failures, e.g. 27958 of 32768 concurrent #UDs not continued, 4810 sites counted for 2048. - Green: `ctest -R ^cpu_emulation$` passes, 13 runs out of 13. Tested on an Intel Core Ultra 7 155H (has CLWB and CLFLUSHOPT natively, so the real #UD path and the NOP mode are not exercised here), RTX 4070 Laptop, NVIDIA 621.40, Windows 11, MSVC 2022 RelWithDebInfo. An independent review found no blocking issue; known nit: in NOP mode, if the two written bytes straddle a cache line, another core could in theory fetch a mixed state. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
On a host without CLWB every execution of the instruction raised #UD and the vectored handler emulated it. The
retail title's GPU upload worker flushes whole buffers with CLWB in a 64-byte loop (exe+0x532f1b0 on 1.0.2.1), so a
buffer cost one exception per cache line; under Wine that kept the worker busy for minutes and the main thread
polling its event in session 0xd, so the boot never left the first-run pages.

The handler now rewrites the instruction at its first #UD: 66 0F AE /6 becomes 66 0F AE /7 (CLFLUSHOPT), the same
operand and length, which also writes the line back; a host without CLFLUSHOPT gets 66 0F 1F /0, a NOP of the same
length. LEONIDA_CLWB_PATCH=0 keeps the old per-execution emulation.

Tested: cpu_emulation_test under Wine 11.21 on an i9-9900KF (no CLWB, has CLFLUSHOPT): both sites rewritten, SIB and
displacement kept, the code runs again. Retail boot on the same machine with #984 + #968 (RTX 2070 SUPER, Fedora 44):
the worker that sat in the CLWB loop at 420, 540 and 660 s now moves on, and the title gets past the first-run pages
into the story load.

AI-assisted: yes

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RBZntAmGCDe3LdLjbzZzuc
Threads that fault on the same CLWB site together lost the race: a #UD handled
after another thread rewrote the site decoded CLFLUSHOPT (or the NOP), did not
know it and returned EXCEPTION_CONTINUE_SEARCH, which ends the title.

- The handler continues at a site that already holds the form written on this
  host (or the NOP's halfway state): the CPU runs it.
- One lock around unprotect, re-check, write and protect again, so two threads
  never interleave on a page; a site found rewritten under it is not written.
- The NOP is written opcode first (66 0F 1F /6 in between is a NOP too).
- The sites counter counts each site once, by the thread that wrote it.

cpu_emulation_test calls the handler with a made-up #UD, so it runs on hosts
with CLWB: first #UD, a second one on the rewritten site, and 16 threads on
2048 fresh execute-read sites. Before the fix 27958 of 32768 concurrent #UDs
were passed on, 4810 rewrites were counted for 2048 sites and pages were left
writable; with it all checks pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 04:39:17 +00:00
github manually merged commit b1619e0a30 into main 2026-11-28 04:47:27 +00:00
Author
Owner

@⁠DadaCap on GitHub · 2026-11-28 04:43 UTC · original

Merged into main (merge commit b1619e0a3), approved by DadaCap. Thanks!

Tested on head 5d677b7ba on two machines:

  • OreoFR, Core Ultra 7 155H (no SSE4a, CLZERO or MONITORX; CLWB native): the fake-#UD test fails on #1472 + main (4 failures; 27,958 of 32,768 #UDs not continued, 16 threads × 2,048 sites) and passes with this PR, 13 runs out of 13.
  • DadaCap, Ryzen 9 9900X (CLWB native): ctest -R ^cpu_emulation$ passes 3 runs out of 3. The test fakes the #UD, so it covers the race on CPUs that have CLWB too.

This also brings in #1472 (CLWB rewritten in place at its first #UD), whose concurrency bug the review found: a thread whose #UD landed on a site another thread had already rewritten got EXCEPTION_CONTINUE_SEARCH and crashed the title.

AI-assisted (Claude Code), posted for DadaCap.

**@⁠DadaCap** on GitHub · 2026-11-28 04:43 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2559#issuecomment-7234223044) Merged into `main` (merge commit b1619e0a3), approved by DadaCap. Thanks! Tested on head 5d677b7ba on two machines: - **OreoFR, Core Ultra 7 155H** (no SSE4a, CLZERO or MONITORX; CLWB native): the fake-#UD test fails on #1472 + main (4 failures; 27,958 of 32,768 #UDs not continued, 16 threads × 2,048 sites) and passes with this PR, 13 runs out of 13. - **DadaCap, Ryzen 9 9900X** (CLWB native): `ctest -R ^cpu_emulation$` passes 3 runs out of 3. The test fakes the #UD, so it covers the race on CPUs that have CLWB too. This also brings in #1472 (CLWB rewritten in place at its first #UD), whose concurrency bug the review found: a thread whose #UD landed on a site another thread had already rewritten got `EXCEPTION_CONTINUE_SEARCH` and crashed the title. _AI-assisted (Claude Code), posted for DadaCap._
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/ubiquitarianism--leonida!2559
No description provided.