startup: resume threads that fault on a host-cpu site another thread rewrote #2197

Manually merged
github merged 1 commit from gh-pr/2197/head into gh-pr/2197/base 2026-11-27 00:04:51 +00:00
Owner

@⁠lukaslw-lab on GitHub · 2026-11-25 04:00 UTC · original
lukaslw-lab:tooling/host-cpu-rewrite-race → main · +62 −3 · 3 files · 1 commits · merged 2026-11-25 04:24 UTC

Change

With X2W_HOST_CPU=1 the startup debugger rewrites a missing instruction on its first fault. A second thread that ran the same instruction before the rewrite reports its fault afterwards. Until now that fault went to the process as a second chance, and the run stopped. Fixes #2196.

tools/startup/debug_launch.py now records the sites it rewrote. A first-chance illegal instruction at such a site, where nothing is left to rewrite, is continued once per thread, so the thread runs the replacement. A fault that stays at the site is still reported. The summary prints how many threads were resumed.

tests/startup_host_cpu_probe.cpp adds 32 RDPID sites. A barrier before each site makes eight threads reach it together. startup_host_cpu_debug now also requires all eight threads to finish.

Related, but a different mechanism: #1281 and its PRs #1408 and #1951 (hooked-function breakpoints).

Verification

Commit 6429104a on a116c379, Windows 10 22H2 (build 19045), i7-4790K (no RDPID, no SHA), Visual Studio 2026 18.12 (MSVC 19.52), Windows SDK 10.0.26100, CMake 4.3.1, Python 3.13.7.

Negative control, the new probe without the debug_launch.py change, 3 runs:

python -B tests/startup_host_cpu_debug_check.py out/build/release/startup_host_cpu_probe.exe
AssertionError: stopped at illegal instruction (0xc000001d) at startup_host_cpu_probe.exe+0x1000 after 0.11 s (second chance)   (3 of 3)

With the change, 5 runs:

startup_host_cpu_debug: PASS (performed by the debugger: rdpid x32, sha (per hit) x56; threads resumed after another thread's rewrite: 205 to 224)   (5 of 5)

CTest:

ctest --preset release -R "^startup_(host_cpu|debug)"
100% tests passed, 0 tests failed out of 3   (startup_host_cpu, startup_host_cpu_debug, startup_debug)
ctest --preset dev -R "^startup_(host_cpu|debug)"
100% tests passed, 0 tests failed out of 3

A full startup run with this change (and #1277 for Windows 10) rewrote the same sites as before (clflushopt-nop x1, clwb x1, clwb-nop x4, clzero x5, insertq x2, monitorx x1, mwaitx x1). The race did not occur in that run, so nothing was resumed.

Evidence and follow-up

  • The probe is source-built (MIT, as the rest of the repository). On a host that has RDPID the new sites run directly, nothing is resumed, and the check reports which case applied.
  • Each (site, thread) pair is continued only once, so a site that keeps faulting is still reported and cannot loop. The CLWB → CLFLUSHOPT → NOP chain is unchanged, because a rewrite is tried before the resume.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed. (No change in what contributors run.)
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: prepared with Claude Code; the commands and results above were run on the author's PC.

🤖 Generated with Claude Code

**@⁠lukaslw-lab** on GitHub · 2026-11-25 04:00 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2197) `lukaslw-lab:tooling/host-cpu-rewrite-race` → `main` · +62 −3 · 3 files · 1 commits · merged 2026-11-25 04:24 UTC ## Change With `X2W_HOST_CPU=1` the startup debugger rewrites a missing instruction on its first fault. A second thread that ran the same instruction before the rewrite reports its fault afterwards. Until now that fault went to the process as a second chance, and the run stopped. Fixes #2196. `tools/startup/debug_launch.py` now records the sites it rewrote. A first-chance illegal instruction at such a site, where nothing is left to rewrite, is continued once per thread, so the thread runs the replacement. A fault that stays at the site is still reported. The summary prints how many threads were resumed. `tests/startup_host_cpu_probe.cpp` adds 32 RDPID sites. A barrier before each site makes eight threads reach it together. `startup_host_cpu_debug` now also requires all eight threads to finish. Related, but a different mechanism: #1281 and its PRs #1408 and #1951 (hooked-function breakpoints). ## Verification Commit `6429104a` on `a116c379`, Windows 10 22H2 (build 19045), i7-4790K (no RDPID, no SHA), Visual Studio 2026 18.12 (MSVC 19.52), Windows SDK 10.0.26100, CMake 4.3.1, Python 3.13.7. Negative control, the new probe without the `debug_launch.py` change, 3 runs: python -B tests/startup_host_cpu_debug_check.py out/build/release/startup_host_cpu_probe.exe AssertionError: stopped at illegal instruction (0xc000001d) at startup_host_cpu_probe.exe+0x1000 after 0.11 s (second chance) (3 of 3) With the change, 5 runs: startup_host_cpu_debug: PASS (performed by the debugger: rdpid x32, sha (per hit) x56; threads resumed after another thread's rewrite: 205 to 224) (5 of 5) CTest: ctest --preset release -R "^startup_(host_cpu|debug)" 100% tests passed, 0 tests failed out of 3 (startup_host_cpu, startup_host_cpu_debug, startup_debug) ctest --preset dev -R "^startup_(host_cpu|debug)" 100% tests passed, 0 tests failed out of 3 A full startup run with this change (and #1277 for Windows 10) rewrote the same sites as before (`clflushopt-nop x1, clwb x1, clwb-nop x4, clzero x5, insertq x2, monitorx x1, mwaitx x1`). The race did not occur in that run, so nothing was resumed. ## Evidence and follow-up - The probe is source-built (MIT, as the rest of the repository). On a host that has RDPID the new sites run directly, nothing is resumed, and the check reports which case applied. - Each (site, thread) pair is continued only once, so a site that keeps faulting is still reported and cannot loop. The CLWB → CLFLUSHOPT → NOP chain is unchanged, because a rewrite is tried before the resume. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. (No change in what contributors run.) - [x] I did not include game files, dumps, distribution links or secrets. _AI-assisted: prepared with Claude Code; the commands and results above were run on the author's PC._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
With X2W_HOST_CPU=1 the debugger rewrites a missing instruction on its first fault. A second thread that
ran the same instruction before the rewrite reports its fault after it: the site then holds the
replacement, nothing is rewritten again, and the fault went to the title as a second chance. Such a
thread is now continued once and runs the replacement; a fault that stays is still reported.

The probe runs 32 RDPID sites that eight threads reach together; without the change the check stops at
an illegal instruction on a host without RDPID.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 00:04:49 +00:00
github manually merged commit 6429104adc into gh-pr/2197/base 2026-11-27 00:04:51 +00:00
Author
Owner

@⁠tedin7 on GitHub · 2026-11-25 04:04 UTC · original · reviewed

Review of head 6429104adc5c9a2c29afa8042b63c2615dd94a4d (comment only).

Summary: the change is small and sound. A thread that faulted on a host-cpu site before another thread rewrote it is continued once, so it runs the replacement. No P1/P2 findings.

Checked:

  • tools/startup/debug_launch.py: rewrite_for_host and sha_for_host are tried first, so a fresh site is always handled the old way. Only an already-rewritten address that nobody can rewrite again reaches the new branch. The (site, tid) set bounds it to one resume per thread, so a fault that persists after the replacement is still reported and not looped on.
  • SHA sites are never added to the rewritten set (they are performed per hit), so they are not affected.
  • The summary line is only printed when something was resumed, so existing output is unchanged otherwise.
  • Probe: 32 distinct RDPID sites (the +N keeps the linker from merging them) and a spin barrier before each site make every thread reach the site together. The check asserts rdpid threads 8 of 8.

Commands executed locally (Linux):

  • git diff --stat origin/main...HEAD: 3 files, +62/-3
  • python3 tests/docs_links_check.py: self-test passed, 18 files, 405 links
  • python3 tests/bidi_controls_check.py: 1480 files, no bidirectional controls
  • git diff --check origin/main...HEAD: clean
  • python3 -I -m py_compile tools/startup/debug_launch.py tests/startup_host_cpu_debug_check.py: ok
  • Mutation (removed the (ex_addr, tid) not in host_cpu_resumed guard): the check needs the Windows probe binary (IndexError on argv on Linux), so it cannot kill or confirm the mutation here. Reverted with git checkout -- ..
  • python3 x2w_pipe.py win2025 2197 --targets startup_host_cpu_probe --regex startup_host_cpu --wait 3300: dev 2/2 passed (startup_host_cpu, startup_host_cpu_debug), release 2/2 passed, exit 0.
  • CI: Linux checks, gfx1013 fixtures and Windows build/test all pass; mergeable; title is neutral.

Nits (non-blocking):

  • N1 tests/startup_host_cpu_debug_check.py: on a host that has RDPID nothing is rewritten, so the resume branch is not exercised and 8 of 8 passes trivially. It only guards the fix on hosts without RDPID. Worth a comment, or asserting the resumed count when summary mentions rdpid.
  • N2 tests/startup_host_cpu_debug_check.py: the resumed count is reported but not asserted. It is timing dependent, so this is a reasonable choice; the 8 of 8 assert is the real guard.

AI-assisted; commands listed were executed locally.

**@⁠tedin7** on GitHub · 2026-11-25 04:04 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2197#pullrequestreview-6051700266) · **reviewed** Review of head `6429104adc5c9a2c29afa8042b63c2615dd94a4d` (comment only). Summary: the change is small and sound. A thread that faulted on a host-cpu site before another thread rewrote it is continued once, so it runs the replacement. No P1/P2 findings. Checked: - `tools/startup/debug_launch.py`: `rewrite_for_host` and `sha_for_host` are tried first, so a fresh site is always handled the old way. Only an already-rewritten address that nobody can rewrite again reaches the new branch. The `(site, tid)` set bounds it to one resume per thread, so a fault that persists after the replacement is still reported and not looped on. - SHA sites are never added to the rewritten set (they are performed per hit), so they are not affected. - The summary line is only printed when something was resumed, so existing output is unchanged otherwise. - Probe: 32 distinct RDPID sites (the `+N` keeps the linker from merging them) and a spin barrier before each site make every thread reach the site together. The check asserts `rdpid threads 8 of 8`. Commands executed locally (Linux): - `git diff --stat origin/main...HEAD`: 3 files, +62/-3 - `python3 tests/docs_links_check.py`: self-test passed, 18 files, 405 links - `python3 tests/bidi_controls_check.py`: 1480 files, no bidirectional controls - `git diff --check origin/main...HEAD`: clean - `python3 -I -m py_compile tools/startup/debug_launch.py tests/startup_host_cpu_debug_check.py`: ok - Mutation (removed the `(ex_addr, tid) not in host_cpu_resumed` guard): the check needs the Windows probe binary (`IndexError` on argv on Linux), so it cannot kill or confirm the mutation here. Reverted with `git checkout -- .`. - `python3 x2w_pipe.py win2025 2197 --targets startup_host_cpu_probe --regex startup_host_cpu --wait 3300`: dev 2/2 passed (`startup_host_cpu`, `startup_host_cpu_debug`), release 2/2 passed, exit 0. - CI: Linux checks, gfx1013 fixtures and Windows build/test all pass; mergeable; title is neutral. Nits (non-blocking): - N1 `tests/startup_host_cpu_debug_check.py`: on a host that has RDPID nothing is rewritten, so the resume branch is not exercised and `8 of 8` passes trivially. It only guards the fix on hosts without RDPID. Worth a comment, or asserting the resumed count when `summary` mentions rdpid. - N2 `tests/startup_host_cpu_debug_check.py`: the resumed count is reported but not asserted. It is timing dependent, so this is a reasonable choice; the `8 of 8` assert is the real guard. AI-assisted; commands listed were executed locally.
This discussion has been locked. Commenting is limited to contributors.
No description provided.