tools: skip the step window for ntdll stub hooks and last returns #3463
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3463"
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?
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 0is unchanged):Nt*hooks (file, directory, symbolic link, ALPC and, withX2W_VALLOC_MIN, the allocation hooks), when the first instruction read at arm time ismov 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.X2W_LOG_ATpoint 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 retiredX2W_LOG_ATpoints. A return still pending for another thread keeps the hold and step.hook emulated: N disarmed: Mnext tohook windows:.tests/startup_launch_probe.cpp: new modeidle_opens(96 threads blocked on an event, one thread doing 500 opens, prints ms/open).tests/startup_hook_check.py: the overlap check now requireswindows + emulated >= 64,emulated > 0anddisarmed > 0under the default, andemulated == 0 and disarmed == 0with--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 noidle_opensmode, so all legs run the probe built from this branch; onlydebug_launch.pydiffers.5de0b114, defaultwindows=1004 unprotected=05de0b114,--suspend-others 0windows=1004 unprotected=1004windows=0 unprotected=0,hook emulated: 502 disarmed: 502Every run reported 500 of 500 opens.
Overlap mode (4 threads, 64 opens), same arguments as the test:
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
startup_hook_windowandstartup_hook_overlappass twice (64 of 64 opens traced with protection; control lost 30, thenlost 28).Mutations of
debug_launch.py, each restored afterwards; all makestartup_hook_overlapfail:armattempt 0: no hook entries were emulatedoverlap opens 0 of 64attempt 0: 16 of 64 opens tracedattempt 0: no return breakpoints were disarmedNot covered
mov r10, rcx, and the hit-counter, runtime-call and movie-player hooks, keep the restore, hold and step.idle_opensis a manual probe mode, not a ctest; the timings above are not checked by any test.--suspend-others 0behaves as before.Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-30T09:01:02Z.
Review: APPROVED
Reviewed
bb207dbdby 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.
syscall_hooks = set(hooks)is taken right after theNt*file/ALPC andX2W_VALLOC_MINhooks (debug_launch.py:505), before the count, runtime and bink hooks are added (:973-:1007). So only ntdll stubs can be emulated, andarg not in ("count", "runtime", "bink")is a backstop.argis bound wheneveraddr in hooks(:673), and the condition tests that first.CONTEXT, R10 is at 0xC8 and RIP at 0xF8, andmov r10, rcxleaves 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.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_sourcerewinds only when the byte is no longer0xcc, so a re-armed site goes through normal handling.UNLOAD_DLL:lpBaseOfDllis at offset 16 ofDEBUG_EVENT.forget_moduledrops sites byAllocationBase, so a different module later loaded at that address is not rewound by mistake, which the oldretiredset allowed.Tests:
startup_hook_check.pycoversLateBreakpointsdirectly, plusemulated/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 runstartup_hook_window/startup_hook_overlap: they are Windows ctests outside theprpreset, and the Linux job does not run this file.