From fa754b0f44c0420354de7e4dc4dcb1281dd6fce0 Mon Sep 17 00:00:00 2001 From: lewisalex86 Date: Thu, 26 Nov 2026 19:41:58 +0000 Subject: [PATCH 1/2] tooling: add --assert-sites to the startup debugger --continue-asserts N counts hits, so a soft assert that repeats runs the budget out and long runs need ever larger values. --assert-sites N steps over every hit at up to N distinct main-image int3 sites and stops at the first hit of one more site. run.ps1 -AssertSites and run.sh --assert-sites pass it through with 0 as the default. --- docs/startup-runtime.md | 6 +++++- tests/startup_debug_check.py | 26 ++++++++++++++++++++++++++ tests/startup_run_ps1_check.py | 4 ++-- tests/startup_run_sh_check.py | 4 ++-- tools/startup/debug_launch.py | 16 ++++++++++++++-- tools/startup/run.ps1 | 3 ++- tools/startup/run.sh | 9 +++++---- 7 files changed, 56 insertions(+), 12 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 8a953e4d81..3f394ea322 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -2310,7 +2310,11 @@ These are measured on one executable; treat them as hypotheses for others. [#2618](https://github.com/R0Y00/Xbox2Windows/issues/2618) - **Soft asserts.** Retail code keeps `int3` instructions followed by a continuation. Without a debugger the title's handler steps over them; under the startup debugger use - `-ContinueAsserts N`. One of them fires about ten times a second in the texture-streaming path + `-AssertSites N` (`--assert-sites N` for `run.sh`), which steps over every hit at up to N distinct + main-image `int3` sites, so a site that repeats never runs the budget out however long the run is. + The first hit at one more site still stops the run, as do `X2W_STOP_AT` sites. `-ContinueAsserts N` + counts hits instead: it steps over the first N hits at any site. A hit is stepped over when either + budget allows it. Run `ctest --preset dev --no-tests=error -R "^startup_debug"`. One of them fires about ten times a second in the texture-streaming path (a chunked read whose length ends inside a compressed chunk); its cause is open. - **Struct-returning methods.** When both register arguments of a wrapped method look like objects, the one on the calling thread's stack is the return buffer. diff --git a/tests/startup_debug_check.py b/tests/startup_debug_check.py index f0e78798a1..accb91daae 100644 --- a/tests/startup_debug_check.py +++ b/tests/startup_debug_check.py @@ -7,6 +7,8 @@ ends the run as the timeout does; one left from an earlier run is removed at start. The launcher keeps the debug heap off unless the caller sets _NO_DEBUG_HEAP=0. X2W_NOP_SOFT_ASSERTS=1 replaces a main-image INT3 with a NOP after the first continue, and the probe repeats one site when STARTUP_DEBUG_PROBE_ASSERTS is set. +--assert-sites N steps over every hit at up to N distinct main-image sites, however often each +repeats, and stops at the first hit of one more site. tests/startup_debug_probe.cpp is the main input: it hits two breakpoints, optionally repeats a third, reserves 256 MiB, prints its debug-heap flags, waits 3 s and exits 0. @@ -212,6 +214,30 @@ def stack_read(address, size): assert "process exited with 0x00000000" in out, out assert breakpoint_sites(logdir, image).count(repeated[0]) == repeats, text(logdir / "events.txt") + # --assert-sites 3: every hit at the three sites is stepped over, however often the third + # repeats, with no hit budget at all. + many = 20 + out, logdir = run(probe, work, "assert-sites", {"STARTUP_DEBUG_PROBE_ASSERTS": str(many)}, + options=("--assert-sites", "3")) + assert "process exited with 0x00000000" in out, out + assert f"debug-probe repeated {many}\n" in text(logdir / "stdout.txt"), text(logdir / "stdout.txt") + assert breakpoint_sites(logdir, image).count(repeated[0]) == many, text(logdir / "events.txt") + # Control: a hit budget of three runs out at the repeated site's second hit. + out, _ = run(probe, work, "assert-sites-control", {"STARTUP_DEBUG_PROBE_ASSERTS": str(many)}, + options=("--continue-asserts", "3")) + assert f"stopped at breakpoint (0x80000003) at {image}+{repeated[0]:#x}" in out, out + # Two sites: the first hit at the third site stops the run. + out, logdir = run(probe, work, "assert-sites-two", {"STARTUP_DEBUG_PROBE_ASSERTS": str(many)}, + options=("--assert-sites", "2")) + assert f"stopped at breakpoint (0x80000003) at {image}+{repeated[0]:#x}" in out, out + assert breakpoint_sites(logdir, image).count(repeated[0]) == 1, text(logdir / "events.txt") + # X2W_STOP_AT still stops at a site the site budget covers. + out, _ = run(probe, work, "assert-sites-stop", {"X2W_STOP_AT": f"{sites[1]:x}"}, + options=("--assert-sites", "3")) + assert f"stopped at breakpoint (0x80000003) at {image}+{sites[1]:#x}" in out, out + out, _ = run(probe, work, "assert-sites-negative", {}, options=("--assert-sites", "-1")) + assert "expected an --assert-sites value of 0 or greater" in out, out + # X2W_DUMP_AT: a dump one second in, while the probe is still alive. out, logdir = run(probe, work, "dump", {"X2W_DUMP_AT": "1"}) assert "process exited with 0x00000000" in out, out diff --git a/tests/startup_run_ps1_check.py b/tests/startup_run_ps1_check.py index 6acfb28c6b..5443b11bc6 100644 --- a/tests/startup_run_ps1_check.py +++ b/tests/startup_run_ps1_check.py @@ -188,7 +188,7 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(), command = [shell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(startup / "run.ps1"), "-Exe", str(exe), "-DataDir", str(data), "-LogDir", str(log), "-BuildDir", build_arg, - "-Seconds", "9", "-MemoryGiB", "3", "-ContinueAsserts", "4", *switches] + "-Seconds", "9", "-MemoryGiB", "3", "-ContinueAsserts", "4", "-AssertSites", "2", *switches] sdk = root / "dxc" if native: sdk.mkdir() @@ -227,7 +227,7 @@ def run_case(shell, script, code, work, translate=False, switches=(), passed=(), elif [line.strip() for line in decode(summary.read_bytes()).splitlines() if line.strip()] != STUB_LINES: found.append(f"stub exit {code}: {summary.name} does not hold the debugger's output") want = [str(exe), str(data), str(log), "--timeout", "9", "--memory-gib", "3", - "--trace-files", "1", "--continue-asserts", "4", *passed] + "--trace-files", "1", "--continue-asserts", "4", "--assert-sites", "2", *passed] record = startup / "debugger.json" got = json.loads(record.read_text(encoding="utf-8")) if record.is_file() else None expected = [want, "1" if "-TraceApi" in switches else trace_api_env, diff --git a/tests/startup_run_sh_check.py b/tests/startup_run_sh_check.py index beab30d418..8522b04aa0 100644 --- a/tests/startup_run_sh_check.py +++ b/tests/startup_run_sh_check.py @@ -197,7 +197,7 @@ def run_case(script, code, work, switches=(), passed=(), env_extra=None, drive_d log = data / "logs" command = ["bash", str(startup / "run.sh"), "--exe", str(exe), "--data", "G:\\" if drive_data else str(data), "--log", log_arg, "--build-dir", "bld", "--seconds", "9", "--memory-gib", "3", - "--continue-asserts", "4", *switches] + "--continue-asserts", "4", "--assert-sites", "2", *switches] if translate: command += ["--render", "--translate", "--shader-cache", "cache"] (startup / "translate.flag").write_bytes(b"") @@ -266,7 +266,7 @@ def run_case(script, code, work, switches=(), passed=(), env_extra=None, drive_d if missing: found.append(f"{label}: stand-in DLLs not copied next to the executable: {missing}") want = [win(exe), "G:\\" if drive_data else win(data), log_win, "--timeout", "9", "--memory-gib", "3", - "--trace-files", "1", "--continue-asserts", "4", *passed] + "--trace-files", "1", "--continue-asserts", "4", "--assert-sites", "2", *passed] got = read_json(startup / "argv.json") if got != want: found.append(f"{label}: the debugger received {got}, expected {want}") diff --git a/tools/startup/debug_launch.py b/tools/startup/debug_launch.py index e6bb341ddf..5f823fb4cc 100644 --- a/tools/startup/debug_launch.py +++ b/tools/startup/debug_launch.py @@ -1,11 +1,16 @@ """Run one EXE under a minimal debugger inside a kill-on-close job (startup diagnostics; see docs/startup-runtime.md). usage: debug_launch.py [--timeout S] [--memory-gib N] [--stop-first-chance 1] + [--continue-asserts N] [--assert-sites N] Records module loads, every exception (code, address as module+offset, first/second chance) and, for the stopping exception, the integer registers, the stack words and the code bytes around RIP. Stops at a second-chance exception, an uncontinued main-image breakpoint, or the timeout. A timeout of zero disables the timer. An assertion continuation limit of -1 removes that limit. +--continue-asserts N steps over the first N main-image int3 hits in total. --assert-sites N instead +steps over every hit at up to N distinct main-image int3 sites, so a site that repeats does not use +the budget up however long the run lasts; the first hit at one more site stops the run. A hit is +stepped over when either budget allows it. A file named `stop` created in during the run ends it as the timeout does (checked twice a second; a stop file left from an earlier run is removed at start). """ @@ -124,13 +129,18 @@ def print(*args, **kwargs): timeout = float(opts.get("--timeout", 15)) # zero leaves the session running until exit or a debug stop memory_gib = float(opts.get("--memory-gib", 16)) skip_asserts = int(opts.get("--continue-asserts", "0")) # -1 continues all diagnostic assertions + assert_sites = int(opts.get("--assert-sites", "0")) # distinct sites whose every hit is continued except ValueError: - print("expected numeric --timeout and --memory-gib values and an integer --continue-asserts value") + print("expected numeric --timeout and --memory-gib values and integer --continue-asserts and " + "--assert-sites values") return 2 stop_first = opts.get("--stop-first-chance") == "1" if timeout < 0 or not timeout < float("inf") or skip_asserts < -1: print("expected a finite nonnegative timeout and an assertion limit of -1 or greater") return 2 + if assert_sites < 0: + print("expected an --assert-sites value of 0 or greater") + return 2 # The job always sets JOB_MEMORY (0x200), so a limit of 0 would deny the process any commit. if not 1 <= memory_gib * 2**30 < 2**(8 * ctypes.sizeof(ctypes.c_size_t)): print("expected a finite --memory-gib that is at least one byte and fits the Windows size_t limit") @@ -770,7 +780,9 @@ def poll_watches(): log(f"X2W_LOG_AT ignored {rva:#x}: cannot arm a main-image instruction breakpoint") del log_at[rva] elif (ex_code == 0x80000003 and first and main_base and main_base <= ex_addr < main_base + 0x10000000 - and (skip_asserts == -1 or sum(asserts_seen.values()) < skip_asserts) + and (skip_asserts == -1 or sum(asserts_seen.values()) < skip_asserts + or ex_addr in asserts_seen and len(asserts_seen) <= assert_sites + or ex_addr not in asserts_seen and len(asserts_seen) < assert_sites) and (ex_addr - main_base) not in stop_at): asserts_seen[ex_addr] = asserts_seen.get(ex_addr, 0) + 1 if nop_soft_asserts and ex_addr not in nop_sites and read(ex_addr, 1) == b"\xcc" and write(ex_addr, b"\x90"): diff --git a/tools/startup/run.ps1 b/tools/startup/run.ps1 index a4e89bd86c..acef2fbbcd 100644 --- a/tools/startup/run.ps1 +++ b/tools/startup/run.ps1 @@ -19,6 +19,7 @@ param( [string]$BuildDir = "out/build/release", [int]$MemoryGiB = 14, [int]$ContinueAsserts = 0, # main-image int3s to log and step over (title soft asserts) + [int]$AssertSites = 0, # distinct main-image int3 sites whose every hit is stepped over [string]$InputScript = "", # X2W_INPUT_SCRIPT, e.g. "4@70" = A at 70 s [string]$StoragePath = "", # X2W_STORAGE_PATH: root of the console drive the title sees (it appends x2w-gamesave) [string]$ShaderCache = "out/startup/shader_cache", @@ -82,7 +83,7 @@ $traceArgs = @() if ($TraceCalls) { $traceArgs = @("--trace-calls", "1") } try { & $python -B (Join-Path $PSScriptRoot "debug_launch.py") $Exe $DataDir $LogDir --timeout $Seconds --memory-gib $MemoryGiB ` - --trace-files 1 --continue-asserts $ContinueAsserts @traceArgs *> "$LogDir.out" + --trace-files 1 --continue-asserts $ContinueAsserts --assert-sites $AssertSites @traceArgs *> "$LogDir.out" $debuggerExit = $LASTEXITCODE } finally { # taskkill /T needs a live parent PID to reach the watcher's workers. diff --git a/tools/startup/run.sh b/tools/startup/run.sh index 01a32d4872..a9231cddf4 100755 --- a/tools/startup/run.sh +++ b/tools/startup/run.sh @@ -26,7 +26,7 @@ set -uo pipefail usage() { echo "usage: run.sh --exe EXE --data DIR --log DIR [--seconds 120] [--build-dir out/build/release]" >&2 - echo " [--memory-gib 14] [--continue-asserts 0] [--input-script S] [--storage DIR]" >&2 + echo " [--memory-gib 14] [--continue-asserts 0] [--assert-sites 0] [--input-script S] [--storage DIR]" >&2 echo " [--shader-cache out/startup/shader_cache] [--console 0|1] [--render] [--fake-user]" >&2 echo " [--gpu-mirror] [--host-cpu] [--translate] [--startup-below-40] [--trace-calls]" >&2 echo " [--hide-title-window]" >&2 @@ -35,18 +35,19 @@ usage() { repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) here="$repo/tools/startup" -exe="" data="" log="" seconds=120 build_dir=out/build/release memory_gib=14 continue_asserts=0 +exe="" data="" log="" seconds=120 build_dir=out/build/release memory_gib=14 continue_asserts=0 assert_sites=0 input_script="" storage="" shader_cache=out/startup/shader_cache console=1 render="" fake_user="" gpu_mirror="" host_cpu="" translate="" below_40="" trace_calls="" hide_title="" while [ $# -gt 0 ]; do case $1 in - --exe|--data|--log|--seconds|--build-dir|--memory-gib|--continue-asserts|--input-script|--storage|\ + --exe|--data|--log|--seconds|--build-dir|--memory-gib|--continue-asserts|--assert-sites|--input-script|--storage|\ --shader-cache|--console) [ $# -ge 2 ] || usage case $1 in --exe) exe=$2 ;; --data) data=$2 ;; --log) log=$2 ;; --seconds) seconds=$2 ;; --build-dir) build_dir=$2 ;; --memory-gib) memory_gib=$2 ;; --continue-asserts) continue_asserts=$2 ;; # main-image int3s to log and step over + --assert-sites) assert_sites=$2 ;; # distinct int3 sites whose every hit is stepped over --input-script) input_script=$2 ;; # X2W_INPUT_SCRIPT, e.g. "4@70" = A at 70 s --storage) storage=$2 ;; # X2W_STORAGE_PATH: persistent local storage root --shader-cache) shader_cache=$2 ;; --console) console=$2 ;; @@ -197,7 +198,7 @@ console_args=() [ "$console" = 1 ] || console_args=(--console "$console") "$wine" "$X2W_WIN_PYTHON" -B "$(win_path "$here/debug_launch.py")" "$(win_path "$exe")" "$(win_path "$data")" \ "$(win_path "$log")" --timeout "$seconds" --memory-gib "$memory_gib" --trace-files 1 \ - --continue-asserts "$continue_asserts" "${trace_args[@]}" "${console_args[@]}" > "$log_host.out" 2>&1 + --continue-asserts "$continue_asserts" --assert-sites "$assert_sites" "${trace_args[@]}" "${console_args[@]}" > "$log_host.out" 2>&1 debugger_exit=$? head -n 5 "$log_host.out" exit $debugger_exit From 3d4460f777c04de2755aedb8db66708e5c31c383 Mon Sep 17 00:00:00 2001 From: lewisalex86 Date: Fri, 27 Nov 2026 06:50:57 +0000 Subject: [PATCH 2/2] tooling: keep the two assert budgets apart Both budgets read asserts_seen, so a site the hit budget continued also filled a slot of the site budget. With hits A, B, A and --assert-sites 1 --continue-asserts 2, A and B continued and A's second hit stopped the run. Keep the sites the site budget admits in their own set and count only the hits the hit budget covers. Test: the probe can revisit its first breakpoint (STARTUP_DEBUG_PROBE_REVISIT=1); A, B, A with both budgets now runs to the end, and the control with the hit budget alone stops at A's second hit. --- docs/startup-runtime.md | 3 ++- tests/startup_debug_check.py | 12 ++++++++++++ tests/startup_debug_probe.cpp | 6 +++++- tools/startup/debug_launch.py | 14 +++++++++++--- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 3f394ea322..5b47190bb9 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -2314,7 +2314,8 @@ These are measured on one executable; treat them as hypotheses for others. main-image `int3` sites, so a site that repeats never runs the budget out however long the run is. The first hit at one more site still stops the run, as do `X2W_STOP_AT` sites. `-ContinueAsserts N` counts hits instead: it steps over the first N hits at any site. A hit is stepped over when either - budget allows it. Run `ctest --preset dev --no-tests=error -R "^startup_debug"`. One of them fires about ten times a second in the texture-streaming path + budget allows it, and each budget counts only the hits it covers: the site budget takes the first N + distinct sites, and the hit budget counts the hits at other sites. Run `ctest --preset dev --no-tests=error -R "^startup_debug"`. One of them fires about ten times a second in the texture-streaming path (a chunked read whose length ends inside a compressed chunk); its cause is open. - **Struct-returning methods.** When both register arguments of a wrapped method look like objects, the one on the calling thread's stack is the return buffer. diff --git a/tests/startup_debug_check.py b/tests/startup_debug_check.py index accb91daae..631d3af670 100644 --- a/tests/startup_debug_check.py +++ b/tests/startup_debug_check.py @@ -9,6 +9,8 @@ the first continue, and the probe repeats one site when STARTUP_DEBUG_PROBE_ASSERTS is set. --assert-sites N steps over every hit at up to N distinct main-image sites, however often each repeats, and stops at the first hit of one more site. +With both budgets, each counts only the hits it covers (hits A, B, A with --assert-sites 1 +--continue-asserts 2 all continue). tests/startup_debug_probe.cpp is the main input: it hits two breakpoints, optionally repeats a third, reserves 256 MiB, prints its debug-heap flags, waits 3 s and exits 0. @@ -235,6 +237,16 @@ def stack_read(address, size): out, _ = run(probe, work, "assert-sites-stop", {"X2W_STOP_AT": f"{sites[1]:x}"}, options=("--assert-sites", "3")) assert f"stopped at breakpoint (0x80000003) at {image}+{sites[1]:#x}" in out, out + # Both budgets: the hits A, B, A with --assert-sites 1 --continue-asserts 2. The site budget + # covers A, the hit budget B, and A's second hit is still A, so the probe runs to its end. + out, logdir = run(probe, work, "assert-sites-combined", {"STARTUP_DEBUG_PROBE_REVISIT": "1"}, + options=("--assert-sites", "1", "--continue-asserts", "2")) + assert "process exited with 0x00000000" in out, out + assert breakpoint_sites(logdir, image) == [sites[0], sites[1], sites[0]], text(logdir / "events.txt") + # Control: the hit budget alone runs out at A's second hit, so the site budget is what continues it. + out, _ = run(probe, work, "assert-sites-combined-control", {"STARTUP_DEBUG_PROBE_REVISIT": "1"}, + options=("--continue-asserts", "2")) + assert f"stopped at breakpoint (0x80000003) at {image}+{sites[0]:#x}" in out, out out, _ = run(probe, work, "assert-sites-negative", {}, options=("--assert-sites", "-1")) assert "expected an --assert-sites value of 0 or greater" in out, out diff --git a/tests/startup_debug_probe.cpp b/tests/startup_debug_probe.cpp index 4c1db8e51c..e4f2e4fcf1 100644 --- a/tests/startup_debug_probe.cpp +++ b/tests/startup_debug_probe.cpp @@ -1,6 +1,7 @@ // Input for the startup debugger's diagnostics check (tests/startup_debug_check.py). It reaches two // breakpoints in its own image, the way a retail image keeps soft asserts, and with -// STARTUP_DEBUG_PROBE_ASSERTS= n more at a third site (the repeated-assert case). It also +// STARTUP_DEBUG_PROBE_ASSERTS= n more at a third site (the repeated-assert case). With +// STARTUP_DEBUG_PROBE_REVISIT=1 it reaches the first breakpoint once more after the second. It also // reserves one large range, reports whether the loader gave it the debug heap, stays alive for a // moment and exits 0. // With STARTUP_DEBUG_PROBE_DLL= and STARTUP_DEBUG_PROBE_EXPORT= it also loads that DLL @@ -145,6 +146,9 @@ int main() { first_assert(); second_assert(); + if (const char* const revisit = std::getenv("STARTUP_DEBUG_PROBE_REVISIT"); revisit && *revisit == '1') { + first_assert(); + } const char* const repeated = std::getenv("STARTUP_DEBUG_PROBE_ASSERTS"); const int repeated_count = repeated ? std::atoi(repeated) : 0; repeated_assert(repeated_count); diff --git a/tools/startup/debug_launch.py b/tools/startup/debug_launch.py index 5f823fb4cc..41f5c068e8 100644 --- a/tools/startup/debug_launch.py +++ b/tools/startup/debug_launch.py @@ -147,6 +147,11 @@ def print(*args, **kwargs): return 2 memory = int(memory_gib * 2**30) asserts_seen = {} + # The two assert budgets keep their own books, so a hit one of them covers never uses up the other: + # assert_sites_admitted holds the sites --assert-sites covers (the first assert_sites distinct ones), + # asserts_counted the hits --continue-asserts stepped over. + assert_sites_admitted = set() + asserts_counted = 0 # X2W_NOP_SOFT_ASSERTS=1: after the first hit of a main-image int3 that is continued, replace the # trap byte with NOP. DBG_CONTINUE already bypasses the title's handler, so the visible behavior is # unchanged, but a long mapping that traps once per chunk then runs at native speed. @@ -780,10 +785,13 @@ def poll_watches(): log(f"X2W_LOG_AT ignored {rva:#x}: cannot arm a main-image instruction breakpoint") del log_at[rva] elif (ex_code == 0x80000003 and first and main_base and main_base <= ex_addr < main_base + 0x10000000 - and (skip_asserts == -1 or sum(asserts_seen.values()) < skip_asserts - or ex_addr in asserts_seen and len(asserts_seen) <= assert_sites - or ex_addr not in asserts_seen and len(asserts_seen) < assert_sites) + and (ex_addr in assert_sites_admitted or len(assert_sites_admitted) < assert_sites + or skip_asserts == -1 or asserts_counted < skip_asserts) and (ex_addr - main_base) not in stop_at): + if ex_addr in assert_sites_admitted or len(assert_sites_admitted) < assert_sites: + assert_sites_admitted.add(ex_addr) + else: + asserts_counted += 1 asserts_seen[ex_addr] = asserts_seen.get(ex_addr, 0) + 1 if nop_soft_asserts and ex_addr not in nop_sites and read(ex_addr, 1) == b"\xcc" and write(ex_addr, b"\x90"): nop_sites[ex_addr] = 1