Console CPU instructions: leave CLWB intact while single stepping #571

Open
github-bot wants to merge 1 commit from github-bot/gh-571 into main AGit
Owner

@⁠Prime1997 on GitHub · 2026-11-24 01:56 UTC · original
Prime1997:contrib/clwb-trap-flag → main · +62 −1 · 1 files · 1 commits · open

When a debugger single-steps a title on a CPU without CLWB, the exception handler currently replaces the original instruction with several one-byte NOPs. A later visit to the same address sees different instruction boundaries. Keep the instruction bytes while the x86 trap flag is set.

What is in it

  • Check the saved trap flag before patching a CLWB site. The handler still advances the instruction pointer.
  • Exercise 4-, 5- and 6-byte encodings on an executable, read-only page. The test revisits each site while stepping, then clears the trap flag and checks the ordinary patching path.
  • This touches the same handler arm as #321. That PR addresses concurrent code patching. This change covers stepping and can be retained if the patch mechanism changes.

Tested

  • ff37edccb1dabb9e39985b51ecf33a5e352c699a, Windows 11 Pro build 26200, Intel Core i5-10400, NVIDIA GeForce RTX 3060 Ti, driver 32.0.16.1714, Rust 1.100.0 MSVC.
  • The new test failed on main 508a0c7 with the original four-byte CLWB replaced by four NOPs. The release exception-handler tests passed after the change (7 passed, 0 failed, 0 ignored).
  • The full cargo test --workspace run with locked dependencies, four test threads and --no-fail-fast completed with 681 passed, 0 failed, 41 ignored. Two GPU tests reported a wave64 hardware skip. Both the debug workspace and release CLI builds passed.

Regression run

Both runs used the same retail title, RTX 3060 Ti, 60-second limit, offline mode, first-use compute setting and two DXC workers.

Main

xcl-smoke v1
commit: 508a0c74d7c0625d6d2de8d24494e69dc2e1926f
gpu: NVIDIA GeForce RTX 3060 Ti
title: 419239bf
seconds: 60
exit: 0x0
frames shown: 556 (PresentX 556)
presents per 2 s (last 3): 4 5 4
device removals: 0
crashes: 0
peak commit: 14899 MB

PR head

xcl-smoke v1
commit: ff37edccb1dabb9e39985b51ecf33a5e352c699a
gpu: NVIDIA GeForce RTX 3060 Ti
title: 419239bf
seconds: 60
exit: 0x0
frames shown: 1229 (PresentX 1229)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14469 MB

Not tested

The focused test exercises the real exception handler with a saved Windows context. An interactive debugger session and CPUs that execute CLWB natively were outside this test. Optional retail sample tests had no external input.

AI use

OpenAI Codex (GPT-6) assisted with reproducing the behavior, implementing the guard and tests, checking overlap and preparing this report.

I use a temporary Discord to coordinate my work. Anyone helping test XCL or compare results is welcome. Test coordination on Discord

**@⁠Prime1997** on GitHub · 2026-11-24 01:56 UTC · [original](https://github.com/xcl-dev/xcl/pull/571) `Prime1997:contrib/clwb-trap-flag` → `main` · +62 −1 · 1 files · 1 commits · open When a debugger single-steps a title on a CPU without CLWB, the exception handler currently replaces the original instruction with several one-byte NOPs. A later visit to the same address sees different instruction boundaries. Keep the instruction bytes while the x86 trap flag is set. **What is in it** - Check the saved trap flag before patching a CLWB site. The handler still advances the instruction pointer. - Exercise 4-, 5- and 6-byte encodings on an executable, read-only page. The test revisits each site while stepping, then clears the trap flag and checks the ordinary patching path. - This touches the same handler arm as #321. That PR addresses concurrent code patching. This change covers stepping and can be retained if the patch mechanism changes. **Tested** - `ff37edccb1dabb9e39985b51ecf33a5e352c699a`, Windows 11 Pro build 26200, Intel Core i5-10400, NVIDIA GeForce RTX 3060 Ti, driver 32.0.16.1714, Rust 1.100.0 MSVC. - The new test failed on main `508a0c7` with the original four-byte CLWB replaced by four NOPs. The release exception-handler tests passed after the change (7 passed, 0 failed, 0 ignored). - The full `cargo test --workspace` run with locked dependencies, four test threads and `--no-fail-fast` completed with 681 passed, 0 failed, 41 ignored. Two GPU tests reported a wave64 hardware skip. Both the debug workspace and release CLI builds passed. **Regression run** Both runs used the same retail title, RTX 3060 Ti, 60-second limit, offline mode, first-use compute setting and two DXC workers. Main ``` xcl-smoke v1 commit: 508a0c74d7c0625d6d2de8d24494e69dc2e1926f gpu: NVIDIA GeForce RTX 3060 Ti title: 419239bf seconds: 60 exit: 0x0 frames shown: 556 (PresentX 556) presents per 2 s (last 3): 4 5 4 device removals: 0 crashes: 0 peak commit: 14899 MB ``` PR head ``` xcl-smoke v1 commit: ff37edccb1dabb9e39985b51ecf33a5e352c699a gpu: NVIDIA GeForce RTX 3060 Ti title: 419239bf seconds: 60 exit: 0x0 frames shown: 1229 (PresentX 1229) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14469 MB ``` **Not tested** The focused test exercises the real exception handler with a saved Windows context. An interactive debugger session and CPUs that execute CLWB natively were outside this test. Optional retail sample tests had no external input. **AI use** OpenAI Codex (GPT-6) assisted with reproducing the behavior, implementing the guard and tests, checking overlap and preparing this report. I use a temporary Discord to coordinate my work. Anyone helping test XCL or compare results is welcome. [Test coordination on Discord](https://discord.gg/gK3y7WRRY)
With EFLAGS.TF set, the generic handler still replaces one multibyte CLWB with several NOP instructions. A later visit then has different instruction boundaries. Skip the patch only for instruction stepping and advance by the decoded instruction length, preserving the saved registers and flags.

Add a direct-handler regression on registered PAGE_EXECUTE_READ code for three CLWB encodings, two TF-set visits each, protection and register checks, and TF-clear controls that still require the existing NOP patch. The test fails on unmodified official508a0c7; all7 release EH tests pass with the guard.

Related:217 introduced hot-patching; active321 dfa332a5 addresses patch atomicity and late faults, without a TF guard. This patch imports no local batching or321 implementation. No GPU/game run or attached-debugger event trace was performed.

Assisted-by: OpenAI Codex (exact model version not exposed)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:26:05 +00:00
Author
Owner

@⁠MesshM on GitHub · 2026-11-24 03:12 UTC · original

Test run of ff37edccb1dabb9e39985b51ecf33a5e352c699a on AMD RDNA4. I merged it onto current main dd2ae9a locally (clean merge) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace + cargo test --workspace -- --test-threads=4 on the merge: 691 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 651 passed, 0 failed, 21 ignored
  • 0 SKIPPED; clwb_single_step_keeps_code_and_normal_faults_still_patch passes.

Review: With the trap flag (EFlags & 0x100) set the handler leaves the bytes alone and only advances RIP; without it it patches as before, and the test covers the 4-, 5- and 6-byte encodings both ways. I found nothing to change.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the PR's head on its old base was not run, only its merge onto main.

Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

**@⁠MesshM** on GitHub · 2026-11-24 03:12 UTC · [original](https://github.com/xcl-dev/xcl/pull/571#issuecomment-7120866319) Test run of `ff37edccb1dabb9e39985b51ecf33a5e352c699a` on AMD RDNA4. I merged it onto current `main` `dd2ae9a` locally (clean merge) and tested that tree. **Tested** - AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 `stable-x86_64-pc-windows-msvc`, debug - `cargo build --workspace` + `cargo test --workspace -- --test-threads=4` on the merge: **691 passed, 0 failed, 41 ignored**, exit code 0; `xcl` lib tests 651 passed, 0 failed, 21 ignored - **0 `SKIPPED`**; `clwb_single_step_keeps_code_and_normal_faults_still_patch` passes. **Review:** With the trap flag (`EFlags & 0x100`) set the handler leaves the bytes alone and only advances RIP; without it it patches as before, and the test covers the 4-, 5- and 6-byte encodings both ways. I found nothing to change. **Not tested:** no retail title or regression run (I have no unpacked game), no `XCL_SAMPLE_*` tests, release build; the PR's head on its old base was not run, only its merge onto `main`. Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 04:05 UTC · original

On the overlap with #321 that the description mentions: I checked it at heads ff37edccb1 (this PR) and dfa332a5a4 (#321). The two conflict in crates/xcl/src/eh.rs in either order against main f4ce27ceb6, and #321 changes the premise of this one.

  • This PR guards the code.fill(0x90) path. Several one-byte NOPs move the instruction boundaries a stepping debugger sees on its next visit.
  • #321 removes that path. hot_patch_clwb writes a single 2-byte jmp short over the whole CLWB, atomically under the CLWB_PATCHES lock. After that patch, a later visit sees one instruction at rip that ends at rip + len, so the boundary problem this PR fixes no longer occurs.

So if #321 lands first, this PR's change mostly becomes unnecessary. The TF check would only matter if the code should stay untouched while a debugger steps at all, in which case it would go at #321's call site (if hot_patch_clwb(rip, ...) == ClwbPatch::Patched, line 428 at its head) and return the Stepped behaviour. Your test cases (4-, 5- and 6-byte encodings, TF on and off) would still be useful against #321's jump patch either way.

Read-only, not built. AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-24 04:05 UTC · [original](https://github.com/xcl-dev/xcl/pull/571#issuecomment-7121694805) On the overlap with #321 that the description mentions: I checked it at heads `ff37edccb1` (this PR) and `dfa332a5a4` (#321). The two conflict in `crates/xcl/src/eh.rs` in either order against `main` `f4ce27ceb6`, and #321 changes the premise of this one. - This PR guards the `code.fill(0x90)` path. Several one-byte NOPs move the instruction boundaries a stepping debugger sees on its next visit. - #321 removes that path. `hot_patch_clwb` writes a single 2-byte `jmp short` over the whole CLWB, atomically under the `CLWB_PATCHES` lock. After that patch, a later visit sees one instruction at `rip` that ends at `rip + len`, so the boundary problem this PR fixes no longer occurs. So if #321 lands first, this PR's change mostly becomes unnecessary. The TF check would only matter if the code should stay untouched while a debugger steps at all, in which case it would go at #321's call site (`if hot_patch_clwb(rip, ...) == ClwbPatch::Patched`, line 428 at its head) and return the `Stepped` behaviour. Your test cases (4-, 5- and 6-byte encodings, TF on and off) would still be useful against #321's jump patch either way. Read-only, not built. AI-assisted (Claude Code).
Author
Owner

@⁠Prime1997 on GitHub · 2026-11-24 04:08 UTC · original

On the overlap with #321 that the description mentions: I checked it at heads ff37edccb1 (this PR) and dfa332a5a4 (#321). The two conflict in crates/xcl/src/eh.rs in either order against main f4ce27ceb6, and #321 changes the premise of this one.

  • This PR guards the code.fill(0x90) path. Several one-byte NOPs move the instruction boundaries a stepping debugger sees on its next visit.
  • Console CPU instructions: serialize CLWB hot-patching and flush icache (#278) #321 removes that path. hot_patch_clwb writes a single 2-byte jmp short over the whole CLWB, atomically under the CLWB_PATCHES lock. After that patch, a later visit sees one instruction at rip that ends at rip + len, so the boundary problem this PR fixes no longer occurs.

So if #321 lands first, this PR's change mostly becomes unnecessary. The TF check would only matter if the code should stay untouched while a debugger steps at all, in which case it would go at #321's call site (if hot_patch_clwb(rip, ...) == ClwbPatch::Patched, line 428 at its head) and return the Stepped behaviour. Your test cases (4-, 5- and 6-byte encodings, TF on and off) would still be useful against #321's jump patch either way.

Read-only, not built. AI-assisted (Claude Code).

Interesting. I was using 321 but I didn't think it would be an issue, but I will try and see if this gets pushed first. If anything, we can use a debugger for the lower end systems.

Good catch!

**@⁠Prime1997** on GitHub · 2026-11-24 04:08 UTC · [original](https://github.com/xcl-dev/xcl/pull/571#issuecomment-7121746048) > On the overlap with #321 that the description mentions: I checked it at heads `ff37edccb1` (this PR) and `dfa332a5a4` (#321). The two conflict in `crates/xcl/src/eh.rs` in either order against `main` `f4ce27ceb6`, and #321 changes the premise of this one. > > * This PR guards the `code.fill(0x90)` path. Several one-byte NOPs move the instruction boundaries a stepping debugger sees on its next visit. > * [Console CPU instructions: serialize CLWB hot-patching and flush icache (#278) #321](https://github.com/ivall/xcl/pull/321) removes that path. `hot_patch_clwb` writes a single 2-byte `jmp short` over the whole CLWB, atomically under the `CLWB_PATCHES` lock. After that patch, a later visit sees one instruction at `rip` that ends at `rip + len`, so the boundary problem this PR fixes no longer occurs. > > So if #321 lands first, this PR's change mostly becomes unnecessary. The TF check would only matter if the code should stay untouched while a debugger steps at all, in which case it would go at #321's call site (`if hot_patch_clwb(rip, ...) == ClwbPatch::Patched`, line 428 at its head) and return the `Stepped` behaviour. Your test cases (4-, 5- and 6-byte encodings, TF on and off) would still be useful against #321's jump patch either way. > > Read-only, not built. AI-assisted (Claude Code). Interesting. I was using 321 but I didn't think it would be an issue, but I will try and see if this gets pushed first. If anything, we can use a debugger for the lower end systems. Good catch!
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 13:18 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-25 13:18 UTC · [original](https://github.com/xcl-dev/xcl/pull/571#issuecomment-7163589058) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
Author
Owner

@⁠upipibi on GitHub · 2026-11-26 04:30 UTC · original

NVIDIA test report for ff37edc: RTX 3070 8 GB (Ampere, driver 616.56), Windows 11 26100, Rust 1.100.0, debug.

  • cargo test -p xcl --lib -- --skip hle::xaudio2 --test-threads=4 (the hle::xaudio2 tests hang on this machine, on main too):
  • this PR: 632 passed, 0 failed, 21 ignored; 2 SKIPPED lines (both no wave64). The new clwb_single_step_keeps_code_and_normal_faults_still_patch passes.

Lib tests only; no title run.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠upipibi** on GitHub · 2026-11-26 04:30 UTC · [original](https://github.com/xcl-dev/xcl/pull/571#issuecomment-7182649611) NVIDIA test report for `ff37edc`: RTX 3070 8 GB (Ampere, driver 616.56), Windows 11 26100, Rust 1.100.0, debug. - `cargo test -p xcl --lib -- --skip hle::xaudio2 --test-threads=4` (the `hle::xaudio2` tests hang on this machine, on `main` too): - this PR: 632 passed, 0 failed, 21 ignored; 2 SKIPPED lines (both `no wave64`). The new `clwb_single_step_keeps_code_and_normal_faults_still_patch` passes. Lib tests only; no title run. AI-assisted (Claude Code, Claude Opus 5.5).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/571/head:github-bot/gh-571'
git switch 'github-bot/gh-571'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-571'
git switch 'github-bot/gh-571'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-571'
git switch 'github-bot/gh-571'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-571'
git switch 'main'
git merge --squash 'github-bot/gh-571'
git switch 'main'
git merge --ff-only 'github-bot/gh-571'
git switch 'main'
git merge 'github-bot/gh-571'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.