Skip to content

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

Open
Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/clwb-trap-flag
Open

Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/clwb-trap-flag

Conversation

@Prime1997

@Prime1997 Prime1997 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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 Console CPU instructions: serialize CLWB hot-patching and flush icache (#278) #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

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 dfa332a 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)
@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

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.

@upipibi upipibi mentioned this pull request Nov 24, 2026
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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).

@Prime1997

Copy link
Copy Markdown
Contributor Author

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!

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants