startup: refuse unmapped source ranges before a probe or a copy #2838
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2846"
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 tedin7 on 2026-11-26T13:21:13Z.
Part of #870
Fixes #2845
What this unblocks: the startup runtime stops raising a first-chance access violation for every read of unmapped title memory, so a debugger attached to a run no longer stops on each of them.
Change
src/startup_stubs/local_log.h: newmapped_prefix()asksVirtualQueryfirst (a small per-thread cache of mapped spans) and refuses onlyMEM_FREEranges. Reserved but uncommitted pages stay accessible because the lazy-commit memory relies on first-touch faults.probe_readable()touches only the mapped prefix. Newguarded_memcpy()runs the pre-check, then the existing__trycopy as the last guard (a fault drops the cached spans). Newfaults_avoidedcounter.src/startup_stubs/linear_buffer_backing.h,src/startup_stubs/d3d12_draw.inl:guarded_copyandcopy_title_bytescallguarded_memcpy.src/startup_stubs/d3d12_xs_device.cpp: the render totals line gainsreads refused before a fault N(inserted before the line's end, whichtests/startup_compute_request_check.pypins).docs/startup-runtime.md: describes the pre-check.Tests
startup_local_log(existing C++ check) gainstest_unmapped_no_faults: it releases its own address range, requires it to beMEM_FREEbefore and after the calls, and requires zero first-chance exceptions on the checking thread fromprobe_readable,readableand both copy flavours, four counted refusals per round, and a control touch of a free range that is exactly one first-chance fault. A mapped source still copies and counts nothing. A round that lost its range to another thread is dropped; ten stable rounds are required, so the check does not depend on other threads in the process.Verification
Linux (Stage 1; the check uses MSVC
__try, so it compiles on Windows only):git diff --checkclean, mingw-fsyntax-onlyshows no errors other than the SEH keywords.Win2025 WARP (Windows Server 2025 build 26100, MSVC 14.44), the same change applied as a patch on a build base, five runs at once, dev and release:
Result:
^startup_local_log5/5 passed in dev and 5/5 in release, in all five runs. Control: with the pre-check disabled (mapped_prefixalways reports mapped)startup_local_logfails on "zero first-chance exceptions in every judged round".Gate on this PR's own head
3b259bd78(independent worker, head fetched from the PR, no patch): Win2025 dev 10/10 and release 10/10 for^(startup_local_log|startup_compute_request|startup_shader_cache). Mutant (patch drops the MEM_FREE refusal):startup_local_logfails ("zero first-chance exceptions", "four refusals counted"). Receipt on this PR.Real-hardware testing: @Makto94 - thanks for the help.
Source: Microsoft documentation, VirtualQuery and Structured Exception Handling.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T14:20:14Z.
Review: COMMENTED
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit:
3b259bd786ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T14:31:16Z.
Review: COMMENTED
No description provided.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T14:31:19Z.
Review: COMMENTED
No description provided.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T14:11:09Z.
Independent review of head
3b259bd786: PASS.Verification:
ctest -R '^(startup_local_log|startup_compute_request|startup_shader_cache)': dev 10/10 and release 10/10.MEM_FREErefusal inmapped_prefixremoved (patch applied on the same head),startup_local_logfails in dev with "zero first-chance exceptions in every judged round" and "the four refusals of every judged round are counted".copy_title_bytesstill catches every exception,guarded_copystill catches only access violations and in-page errors; an overflowing range or a failedVirtualQueryfalls back to the old touch/__trypath; reserved pages are not refused, so lazy-commit first touch keeps working.Not blocking: the per-thread cache holds 16 spans with round-robin replacement, so a thread that alternates over more than 16 regions pays one
VirtualQueryper switch (about 1 ms on the title's multi-gigabyte regions, per the code comment). Worth watching the probe cost after the next tester run.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T14:12:34Z.
Codex Review Summary: ✅ Completed
This comment shows the latest Codex review activity on this pull request.
3b259bdℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T14:31:10Z.
Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit
f85b47bfa92b51a566ecc3c4500a7591a5665822:--preset dev):cmake --build --preset dev --target startup_local_log_check -- -j 2):1.50s(configure2.46s)ctest --preset dev -R '^startup_local_log' --parallel 2 --no-tests=error --output-on-failure): 5/5 passed (6.22s:startup_local_log_cache_failure_control3.01s,startup_local_log_guard_drop_control3.04s,startup_local_log_reopen_control2.12s,startup_local_log_wrap_control0.01s,startup_local_log3.05s)/O2(--preset release):cmake --build --preset release --target startup_local_log_check -- -j 2):6.23s(configure2.26s)ctest --preset release -R '^startup_local_log' --parallel 2 --no-tests=error --output-on-failure): 5/5 passed (6.04s:startup_local_log2.93s,startup_local_log_guard_drop_control2.94s,startup_local_log_reopen_control2.25s,startup_local_log_wrap_control0.02s,startup_local_log_cache_failure_control2.95s)sccache: 9/13 hits; wall29.4stested:
f85b47bfvia fetch (pull/2846/head, no patch)AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T14:20:14Z.
Code review location:
src/startup_stubs/local_log.h; line 116When a requested range contains a
PAGE_NOACCESSpage before aMEM_FREEtail,mapped_prefix()returns a short prefix and this incrementsfaults_avoided, but the subsequent touch loop still faults on the earlier page and exits before reaching the free range. The previous implementation raises the same exception in this case, so repeated reads increase the heartbeat's avoided-fault total without avoiding any exceptions. For nonzero mapped prefixes, defer the increment until the probe successfully traverses the prefix; retain the immediate count whenmapped == 0.Useful? React with 👍 / 👎.
Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-26T14:20:14Z.
Code review location:
tests/startup_local_log_check.cpp; line 658If another thread reuses the control allocation and either
range_is_free()check rejects it,controlsremains zero, but the control is never attempted again because subsequent judged rounds incrementstablebeyond one. Consequently, even ten successful probe rounds end in a failedcontrols == 1assertion under the address-space churn that the surrounding retry logic is intended to tolerate. Retry the control until one stable control round has been observed, rather than restricting it to the first stable probe round.Useful? React with 👍 / 👎.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T14:31:16Z.
Code review location:
src/startup_stubs/local_log.h; line 116Fixed in
f85b47bf: the avoided-fault count for a free tail is now added only after the mapped prefix was walked without a fault (immediate when the prefix is 0). New testtest_protected_prefix_not_countedinstartup_local_log_checkcovers a PAGE_NOACCESS page before a free tail. Win2025 dev+release^startup_local_log5/5.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T14:31:19Z.
Code review location:
tests/startup_local_log_check.cpp; line 658Fixed in
f85b47bf: the control now retries until one control round survives (controls == 0instead ofstable == 1). Win2025 dev+release^startup_local_log5/5.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by pigalot on 2026-11-27T19:44:33Z.
Review: COMMENTED
Independently reviewed
f85b47bfintegrated overmain0f3e8248(treeb139609a, clean merge).What I traced.
mapped_prefixrefuses onlyMEM_FREE, and only from a freshVirtualQuery. The per-thread cache holds only regions that are not free. So a stale entry can only send a range back to the old touch /__trypath; it can never refuse a mapped range.copy_title_byteskeeps its catch-all filter (any_exceptiontrue), andguarded_copykeeps its AV / in-page filter. Both Codex threads are resolved, and the changes inf85b47bfdo what the replies say.What I ran. I built
startup_local_log_check,startup_local_log_cache_failure_checkandd3d12_xs_shimindevandrelease, then ran:ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^startup_local_log.*$'5/5 passed in Debug and Release
/O2, including the four controls.Mutant. Dropping the up-front refusal in
guarded_memcpy(if (false)) failsstartup_local_logwith two checks:FAIL free address space: zero first-chance exceptions in every judged roundFAIL free address space: the four refusals of every judged round are countedRestored, it passes.
git diff --checkis clean.Cost measurement. This puts a
VirtualQueryin front of every probe and guarded copy, with no debugger attached as well. The file's own comment aboveprobe_readablesays the probe avoidedVirtualQueryfor cost. So I timedprobe_readablewithmain'slocal_log.hagainst this PR's, in a small standalone program on synthetic memory only (cl /O2, two runs each, consistent):mainSo:
I couldn't measure how often the runtime hits the first two patterns. Mapped-page runs and texture uploads are the likely candidates, but that is a hypothesis.
Benchmark source and commands
mainheader:git show origin/main:src/startup_stubs/local_log.h > main/startup_stubs/local_log.h, thencl /nologo /std:c++20 /O2 /EHsc /I main /I src bench.cpp /Fe:bench_main.exeand the same without/I mainfor the PR.Suggested direction (untested): gate the up-front query on
IsDebuggerPresent(), the case the PR is for. Without a debugger, a first-chance exception doesn't stop the process, and__tryalready handles it at the old cost.faults_avoidedwould then count only debugger runs, which the docs line could say.Verdict: comment on
f85b47bf. The behaviour is correct and tested. I'm not approving until the author decides whether the always-on query cost is acceptable or should be gated.Environment: Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; no debugger attached.
CI:
Build and test on Windowspasses onf85b47bf. Both review threads are resolved.This reply was AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-27T22:08:15Z.
Thanks for the cost measurement. The VirtualQuery pre-check is now debug-only: it runs only when a debugger is attached or
X2W_PROBE_PRECHECKis set (anything but0), read once and cached. Normal runs keep the old probe /__trypath at the old cost. Pusheda870370723565aad456a060f20aad3b3965a34cf(includes a merge of currentmain).Verification:
a8703707:x2w_pipe.py win2025 2846 --targets startup_local_log_check,startup_local_log_cache_failure_check,d3d12_xs_shim --regex '^startup_local_log' --forcectest --preset dev -R '^startup_local_log'6/6 passed. Release /O2: 6/6 passed.startup_local_log_precheck_off(env off): a free range is still refused, nothing is counted as an avoided fault, and the touch takes the real first-chance fault. The existing cases run with the env var on.python3 tests/docs_links_check.py,cmake -S . -B <tmp> -DDXC_EXECUTABLE=/bin/true,public_guard.py.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by pigalot on 2026-11-27T23:24:24Z.
Review: APPROVED
Re-reviewed
88b915dbmerged over currentmain9345c21f. The merge is clean andgit diff --cached --checkis clean.The cost question from my comment on
f85b47bfis resolved.03c2ee01gatesmapped_prefixonprecheck_enabled(). The pre-check runs only when a debugger is attached at the first call orX2W_PROBE_PRECHECKis set to anything but0, and the result is read once into a function-local static.mapped_prefixreturnsbytesbefore anyVirtualQuery. So probes and guarded copies takemain's touch /__trypath atmain's cost, the case my benchmark measured.startup_local_lognow sets the variable itself before its first case. The newstartup_local_log_precheck_offruns the same free-range probe withX2W_PROBE_PRECHECK=0. It requires that the range is still refused, that nothing is counted infaults_avoided, and exactly one first-chance fault per round.What I ran. I did a full build in
devandrelease, then ran12/12 pass in both presets, with no skips. That includes
startup_local_log_precheck_offand the controls.Mutants. For each, I rebuilt
startup_local_log_checkandstartup_local_log_cache_failure_checkindevand ran^startup_local_log.*$:mapped_prefixignores the gate (always pre-checks)startup_local_log_precheck_off)precheck_enabled()always falsestartup_local_log,startup_local_log_cache_failure_control)X2W_PROBE_PRECHECKcounts as on (n == 0 || value[0] != '0')IsDebuggerPresent()dropped from the gateRestored and rebuilt afterwards;
git diff --quietis clean.Non-blocking.
startup_local_log_precheck_offsetsX2W_PROBE_PRECHECK=0, but no test leaves the variable unset, and unset is how every normal run starts. A gate that treats "not set" as on would bring back the always-on cost and still pass. A second phase that callsSetEnvironmentVariableA("X2W_PROBE_PRECHECK", nullptr)before its first probe and then runs the same checks would pin it. (ENVIRONMENT_MODIFICATION ... unset:would need CMake 3.22, above the project's 3.21 minimum.)Verdict: approve on
88b915db.Environment: Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; Python 3.12.10; no debugger attached (no GPU tests in this set).
CI:
Build and test on Windows,Assemble and translate gfx1013 fixturesandRun platform-independent Python checks on Linuxpass on88b915db.This reply was AI-assisted.