tools: skip the step window for ntdll stub hooks and last returns #3463

Merged
Leyukaka merged 6 commits from upstream-pr/3463 into main 2026-11-30 19:58:18 +00:00
Leyukaka commented 2026-11-29 15:01:08 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T15:01:08Z.


Fixes #3461.

A hook is one INT3 byte that is restored for a single step; a thread entering the function during that step runs untraced (#1281), so #1408 holds every other thread around each step. With many live threads that hold dominates the cost of a traced call. #3332 added --suspend-others 0, which removes the hold but loses calls again. This change removes the step itself on the two common paths of the default mode, so there is nothing to hold for.

What changes

tools/startup/debug_launch.py, default path only (--suspend-others 0 is unchanged):

  • Hook entry. For the ntdll Nt* hooks (file, directory, symbolic link, ALPC and, with X2W_VALLOC_MIN, the allocation hooks), when the first instruction read at arm time is mov r10, rcx (4C 8B D1), the debugger sets R10 = RCX and RIP = entry + 3 and continues. The INT3 byte is never restored, so no other thread can pass the entry untraced and no step window opens.
  • Return breakpoint. When a return breakpoint delivered its result and no other call is waiting on that address, no hook owns it, it is not an X2W_LOG_AT point and the original byte is not itself an INT3, the original byte is written back and the breakpoint is dropped. The next call re-arms it on its entry event. An INT3 already executed by another thread before the byte was restored is rewound to the restored instruction, as for retired X2W_LOG_AT points. A return still pending for another thread keeps the hold and step.
  • The summary gains a line hook emulated: N disarmed: M next to hook windows:.

tests/startup_launch_probe.cpp: new mode idle_opens (96 threads blocked on an event, one thread doing 500 opens, prints ms/open).

tests/startup_hook_check.py: the overlap check now requires windows + emulated >= 64, emulated > 0 and disarmed > 0 under the default, and emulated == 0 and disarmed == 0 with --suspend-others 0.

Measurements

idle_opens, 96 idle threads, 500 opens, 3 runs each, --timeout 300 --console 0 --trace-files 1 --continue-asserts 0. Main has no idle_opens mode, so all legs run the probe built from this branch; only debug_launch.py differs.

debugger ms/open (runs) mean summary
none 0.017
main 5de0b114, default 3.163, 3.074, 3.212 ~3.15 windows=1004 unprotected=0
main 5de0b114, --suspend-others 0 0.483, 0.426, 0.407 ~0.44 windows=1004 unprotected=1004
this branch, default 0.362, 0.235, 0.368 ~0.32 windows=0 unprotected=0, hook emulated: 502 disarmed: 502

Every run reported 500 of 500 opens.

Overlap mode (4 threads, 64 opens), same arguments as the test:

default:
  hook windows: entries=66 windows=48 unprotected=0 deferred=19 timeouts=0 protection=on
  hook emulated: 66 disarmed: 18
--suspend-others 0:
  hook windows: entries=67 windows=104 unprotected=104 deferred=0 timeouts=0 protection=off
  hook emulated: 0 disarmed: 0

Under the default all 66 entries are emulated; the 48 remaining windows are return breakpoints that were stepped rather than disarmed (18 disarmed + 48 stepped = 66).

Tests

ctest --preset dev --no-tests=error -R '^startup_hook_(window|overlap)$' --repeat until-fail:2 -V

startup_hook_window and startup_hook_overlap pass twice (64 of 64 opens traced with protection; control lost 30, then lost 28).

Mutations of debug_launch.py, each restored afterwards; all make startup_hook_overlap fail:

mutation failure
emulation never selected in arm attempt 0: no hook entries were emulated
emulated value written to R11 instead of R10 overlap opens 0 of 64
return disarmed even when another call is pending on it attempt 0: 16 of 64 opens traced
return disarm never selected attempt 0: no return breakpoints were disarmed

Not covered

  • Hooks whose first instruction is not mov r10, rcx, and the hit-counter, runtime-call and movie-player hooks, keep the restore, hold and step.
  • idle_opens is a manual probe mode, not a ctest; the timings above are not checked by any test.
  • The effect on a full run of the application is not measured yet.
  • --suspend-others 0 behaves as before.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5551950376 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3463). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-29T15:01:08Z. --- Fixes #3461. A hook is one INT3 byte that is restored for a single step; a thread entering the function during that step runs untraced (#1281), so #1408 holds every other thread around each step. With many live threads that hold dominates the cost of a traced call. #3332 added `--suspend-others 0`, which removes the hold but loses calls again. This change removes the step itself on the two common paths of the default mode, so there is nothing to hold for. ## What changes `tools/startup/debug_launch.py`, default path only (`--suspend-others 0` is unchanged): - **Hook entry.** For the ntdll `Nt*` hooks (file, directory, symbolic link, ALPC and, with `X2W_VALLOC_MIN`, the allocation hooks), when the first instruction read at arm time is `mov r10, rcx` (`4C 8B D1`), the debugger sets R10 = RCX and RIP = entry + 3 and continues. The INT3 byte is never restored, so no other thread can pass the entry untraced and no step window opens. - **Return breakpoint.** When a return breakpoint delivered its result and no other call is waiting on that address, no hook owns it, it is not an `X2W_LOG_AT` point and the original byte is not itself an INT3, the original byte is written back and the breakpoint is dropped. The next call re-arms it on its entry event. An INT3 already executed by another thread before the byte was restored is rewound to the restored instruction, as for retired `X2W_LOG_AT` points. A return still pending for another thread keeps the hold and step. - The summary gains a line `hook emulated: N disarmed: M` next to `hook windows:`. `tests/startup_launch_probe.cpp`: new mode `idle_opens` (96 threads blocked on an event, one thread doing 500 opens, prints ms/open). `tests/startup_hook_check.py`: the overlap check now requires `windows + emulated >= 64`, `emulated > 0` and `disarmed > 0` under the default, and `emulated == 0 and disarmed == 0` with `--suspend-others 0`. ## Measurements `idle_opens`, 96 idle threads, 500 opens, 3 runs each, `--timeout 300 --console 0 --trace-files 1 --continue-asserts 0`. Main has no `idle_opens` mode, so all legs run the probe built from this branch; only `debug_launch.py` differs. | debugger | ms/open (runs) | mean | summary | |---|---|---|---| | none | 0.017 | | | | main 5de0b114, default | 3.163, 3.074, 3.212 | ~3.15 | `windows=1004 unprotected=0` | | main 5de0b114, `--suspend-others 0` | 0.483, 0.426, 0.407 | ~0.44 | `windows=1004 unprotected=1004` | | this branch, default | 0.362, 0.235, 0.368 | ~0.32 | `windows=0 unprotected=0`, `hook emulated: 502 disarmed: 502` | Every run reported 500 of 500 opens. Overlap mode (4 threads, 64 opens), same arguments as the test: ``` default: hook windows: entries=66 windows=48 unprotected=0 deferred=19 timeouts=0 protection=on hook emulated: 66 disarmed: 18 --suspend-others 0: hook windows: entries=67 windows=104 unprotected=104 deferred=0 timeouts=0 protection=off hook emulated: 0 disarmed: 0 ``` Under the default all 66 entries are emulated; the 48 remaining windows are return breakpoints that were stepped rather than disarmed (18 disarmed + 48 stepped = 66). ## Tests ``` ctest --preset dev --no-tests=error -R '^startup_hook_(window|overlap)$' --repeat until-fail:2 -V ``` `startup_hook_window` and `startup_hook_overlap` pass twice (`64 of 64 opens traced with protection; control lost 30`, then `lost 28`). Mutations of `debug_launch.py`, each restored afterwards; all make `startup_hook_overlap` fail: | mutation | failure | |---|---| | emulation never selected in `arm` | `attempt 0: no hook entries were emulated` | | emulated value written to R11 instead of R10 | `overlap opens 0 of 64` | | return disarmed even when another call is pending on it | `attempt 0: 16 of 64 opens traced` | | return disarm never selected | `attempt 0: no return breakpoints were disarmed` | ## Not covered - Hooks whose first instruction is not `mov r10, rcx`, and the hit-counter, runtime-call and movie-player hooks, keep the restore, hold and step. - `idle_opens` is a manual probe mode, not a ctest; the timings above are not checked by any test. - The effect on a full run of the application is not measured yet. - `--suspend-others 0` behaves as before.
lukaslw-lab commented 2026-11-30 09:01:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-30T09:01:02Z.

Review: APPROVED


Reviewed bb207dbd by reading the diff; not built or run.

Verdict: approve. Both shortcuts are confined to cases where skipping the step cannot lose a call. No MUST.

  • Emulation scope: syscall_hooks = set(hooks) is taken right after the Nt* file/ALPC and X2W_VALLOC_MIN hooks (debug_launch.py:505), before the count, runtime and bink hooks are added (:973-:1007). So only ntdll stubs can be emulated, and arg not in ("count", "runtime", "bink") is a backstop. arg is bound whenever addr in hooks (:673), and the condition tests that first.
  • Register write: in the x64 CONTEXT, R10 is at 0xC8 and RIP at 0xF8, and mov r10, rcx leaves the flags alone, so writing R10 and RIP is the whole effect. The 3-byte check runs before the INT3 is written, so it reads the real instruction, and a stub that is already patched (jmp, EDR) keeps the step.
  • Return disarm: since entries now always trap, a thread's entry event (which calls arm(ret) and records the pending return) is handled before its return hit. not returns.pending(addr) therefore covers every other thread in flight, and the next entry re-arms. rewind_source rewinds only when the byte is no longer 0xcc, so a re-armed site goes through normal handling.
  • UNLOAD_DLL: lpBaseOfDll is at offset 16 of DEBUG_EVENT. forget_module drops sites by AllocationBase, so a different module later loaded at that address is not rewound by mistake, which the old retired set allowed.

Tests: startup_hook_check.py covers LateBreakpoints directly, plus emulated/disarmed > 0 by default and == 0 with --suspend-others 0. The four mutations show the counts and the R10 target are checked. CI did not run startup_hook_window / startup_hook_overlap: they are Windows ctests outside the pr preset, and the Linux job does not run this file.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6109290192 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3463#pullrequestreview-6109290192). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-30T09:01:02Z. **Review:** APPROVED --- Reviewed `bb207dbd` by reading the diff; not built or run. **Verdict: approve.** Both shortcuts are confined to cases where skipping the step cannot lose a call. No MUST. - Emulation scope: `syscall_hooks = set(hooks)` is taken right after the `Nt*` file/ALPC and `X2W_VALLOC_MIN` hooks (`debug_launch.py:505`), before the count, runtime and bink hooks are added (`:973`-`:1007`). So only ntdll stubs can be emulated, and `arg not in ("count", "runtime", "bink")` is a backstop. `arg` is bound whenever `addr in hooks` (`:673`), and the condition tests that first. - Register write: in the x64 `CONTEXT`, R10 is at 0xC8 and RIP at 0xF8, and `mov r10, rcx` leaves the flags alone, so writing R10 and RIP is the whole effect. The 3-byte check runs before the INT3 is written, so it reads the real instruction, and a stub that is already patched (jmp, EDR) keeps the step. - Return disarm: since entries now always trap, a thread's entry event (which calls `arm(ret)` and records the pending return) is handled before its return hit. `not returns.pending(addr)` therefore covers every other thread in flight, and the next entry re-arms. `rewind_source` rewinds only when the byte is no longer `0xcc`, so a re-armed site goes through normal handling. - `UNLOAD_DLL`: `lpBaseOfDll` is at offset 16 of `DEBUG_EVENT`. `forget_module` drops sites by `AllocationBase`, so a different module later loaded at that address is not rewound by mistake, which the old `retired` set allowed. Tests: `startup_hook_check.py` covers `LateBreakpoints` directly, plus `emulated`/`disarmed` > 0 by default and == 0 with `--suspend-others 0`. The four mutations show the counts and the R10 target are checked. CI did not run `startup_hook_window` / `startup_hook_overlap`: they are Windows ctests outside the `pr` preset, and the Linux job does not run this file.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
R0Y00/Xbox2Windows!3463
No description provided.