cpu_emulation: CLWB rewrite in place without the race between threads (follow-up of #1472) #2559
No reviewers
Labels
No labels
accessibility
area: ci
area: d3d12
area: docs
area: launcher
area: runtime (legacy)
area: shader translator
area: tools
area: viewer
bug
dependencies
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs in-game run
needs rebase
needs-triage
question
ready-for-agent
submodules
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!2559
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2559"
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?
@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 UTCItem: 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 /0without 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()givesOp::none, and the handler returnedEXCEPTION_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, or66 0F 1F /0and the halfway/6, memory operand only) returnsEXCEPTION_CONTINUE_EXECUTION; RIP is unchanged, so the CPU runs the rewritten instruction.patch_clwb(): re-check,VirtualProtect, the write, the restore andFlushInstructionCacherun 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 sees66 0F 1F /6, also a NOP.Test (
runtime/test/cpu_emulation_test.cpp)test_clwb_rewrite()hands the handler a made-up #UD onclwb [rcx]; retsites inPAGE_EXECUTE_READmemory, 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 withLEONIDA_CLWB_PATCH=0. No game files.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
@DadaCap on GitHub · 2026-11-28 04:43 UTC · original
Merged into
main(merge commitb1619e0a3), approved by DadaCap. Thanks!Tested on head
5d677b7baon two machines: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_SEARCHand crashed the title.AI-assisted (Claude Code), posted for DadaCap.