startup: read the whole destination page array when a storage request runs #1415

Manually merged
github merged 2 commits from gh-pr/1415/head into gh-pr/1415/base 2026-11-26 23:05:15 +00:00
Owner

@⁠denn0x on GitHub · 2026-11-24 11:01 UTC · original
denn0x:startup/storage-token-reread → main · +175 −10 · 4 files · 2 commits · merged 2026-11-24 12:03 UTC

Change

src/startup_stubs/xcrdapi.cpp: a DirectStorage request with page destinations (option bit 21) captured the title's page-array entries at EnqueueRequest, and run_entry refreshed only the entries that were still 0. The executable also replaces entries it filled earlier, between enqueue and execution. Data for those entries was written through the token captured at enqueue, into a page the title had already given to something else.

run_entry now reads every entry of the title's page array again when the request runs. A changed non-zero entry is counted, and the heartbeat reports it:

... page tokens 0 at enqueue=N, still 0 at run=N, replaced after enqueue=N; commit ...

docs/startup-runtime.md (DirectStorage ordering) is updated to say the whole array is re-read.

Measurement

Local runs of the executable on Windows 11 26300, RX 9070 XT, release build, same switches in every run:

replaced after enqueue result
before (3 runs) not counted access violations on corrupted pointers at 187-212 s, during loading
after (5 runs) 48,222 by 200 s in the first counted run no such crash in any run; loading continues past that point (two runs reach the main loop, the others stop later on unrelated stand-in gaps)
after, later run 106,800 by 210 s access violation during loading at 231 s at a different site; cause not known yet

This is a run-time observation on one executable, not a fixture. The heartbeat counter lets another contributor see whether their title replaces entries too.

Tests

Updated for the review commit (12864bdb): see the review reply for the dstorage-pages cases and their negative controls.

ctest --test-dir out/build/check --no-tests=error -j 6 (Release, Ninja, MSVC 19.50, Windows 11 26300, RX 9070 XT, debug layer off), on this branch rebased onto 429a8b92:

  • 884 passed, among them every startup_*, storage and XCRDAPI test (112).
  • 5 failed: dxr_placed_build_hardware, dxr_opacity_hardware, gpu_alias_lookup_writes_hardware, cpu_placed_dxr_hardware and pixel_aot_comparisons_hardware. None of them loads the XCRDAPI stand-in, and the three that exist in an older build (before #1135) fail the same way on this adapter. The pixel comparison differs in the low mantissa bits of denormal results.
  • cpu_exe_aot_convert passes when INCLUDE/LIB are set for its run-time cl.exe call. It fails without them, which is a shell setup issue only.

AI-assisted (Claude Code).

**@⁠denn0x** on GitHub · 2026-11-24 11:01 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1415) `denn0x:startup/storage-token-reread` → `main` · +175 −10 · 4 files · 2 commits · merged 2026-11-24 12:03 UTC ## Change `src/startup_stubs/xcrdapi.cpp`: a DirectStorage request with page destinations (option bit 21) captured the title's page-array entries at `EnqueueRequest`, and `run_entry` refreshed only the entries that were still 0. The executable also *replaces* entries it filled earlier, between enqueue and execution. Data for those entries was written through the token captured at enqueue, into a page the title had already given to something else. `run_entry` now reads every entry of the title's page array again when the request runs. A changed non-zero entry is counted, and the heartbeat reports it: ``` ... page tokens 0 at enqueue=N, still 0 at run=N, replaced after enqueue=N; commit ... ``` `docs/startup-runtime.md` (DirectStorage ordering) is updated to say the whole array is re-read. ## Measurement Local runs of the executable on Windows 11 26300, RX 9070 XT, release build, same switches in every run: | | replaced after enqueue | result | |---|---|---| | before (3 runs) | not counted | access violations on corrupted pointers at 187-212 s, during loading | | after (5 runs) | 48,222 by 200 s in the first counted run | no such crash in any run; loading continues past that point (two runs reach the main loop, the others stop later on unrelated stand-in gaps) | | after, later run | 106,800 by 210 s | access violation during loading at 231 s at a different site; cause not known yet | This is a run-time observation on one executable, not a fixture. The heartbeat counter lets another contributor see whether their title replaces entries too. ## Tests Updated for the review commit (12864bdb): see the review reply for the `dstorage-pages` cases and their negative controls. `ctest --test-dir out/build/check --no-tests=error -j 6` (Release, Ninja, MSVC 19.50, Windows 11 26300, RX 9070 XT, debug layer off), on this branch rebased onto 429a8b92: - 884 passed, among them every `startup_*`, storage and XCRDAPI test (112). - 5 failed: `dxr_placed_build_hardware`, `dxr_opacity_hardware`, `gpu_alias_lookup_writes_hardware`, `cpu_placed_dxr_hardware` and `pixel_aot_comparisons_hardware`. None of them loads the XCRDAPI stand-in, and the three that exist in an older build (before #1135) fail the same way on this adapter. The pixel comparison differs in the low mantissa bits of denormal results. - `cpu_exe_aot_convert` passes when `INCLUDE`/`LIB` are set for its run-time `cl.exe` call. It fails without them, which is a shell setup issue only. AI-assisted (Claude Code).
Requests with page destinations captured the title's page tokens at enqueue
and refreshed only entries that were still zero. The title also replaces
non-zero entries between enqueue and execution, so data went to pages it had
already reassigned. The array is now read in full when the request runs;
replaced entries are counted in the heartbeat.

Measured on a local run: 48,222 tokens replaced after enqueue by 200 s.
Before the change those writes landed in stale pages and loading ended in
crashes on corrupted pointers at 187-212 s; with it, loading continues past
that point.

AI-assisted (Claude Code).
run_entry kept the token captured at EnqueueRequest for any page-array entry it could not read
when the request ran. It now checks the whole array first and fails the request (E_FAIL) before
any destination page is written; otherwise it copies the array once and uses that copy.

startup_stub_consumer dstorage-pages (run by the startup_stubs test) covers a non-zero entry
replaced between EnqueueRequest and Submit (the new page receives the data, the old page keeps its
sentinel) and an array made no-access before Submit (that request fails, its pages keep their
sentinel, and a valid request queued after it completes).

AI-assisted (Claude Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 23:05:15 +00:00
github manually merged commit 12864bdb00 into gh-pr/1415/base 2026-11-26 23:05:15 +00:00
Author
Owner

@⁠denn0x on GitHub · 2026-11-24 11:40 UTC · original

Thanks, both points are addressed in the new commit.

Stale-write path. run_entry now checks that the whole page array is readable before it copies it. If it is not (null pointer, or any part unreadable), the request fails with E_FAIL before execute_request runs, so no destination page is written and no token captured at enqueue is used. Otherwise the array is copied once into the entry, and the replaced and zero counters come from that copy. docs/startup-runtime.md now says the caller has to keep the array readable and unchanged once the request is submitted.

Source-built queue test. startup_stub_consumer dstorage-pages runs from startup_stubs (tests/startup_stub_check.py). Each of three physical pages has its own view and is filled with a sentinel first.

  • replaced: a two-page request is enqueued with {page 0, page 1}, and entry 1 is replaced with page 2 before Submit. It checks that page 0 and page 2 hold the data, page 1 keeps its sentinel, and the status is S_OK.
  • unreadable: a two-page request whose array is then made PAGE_NOACCESS, followed on the same queue by a valid one-page request into page 2. It checks that the first status is a failure, pages 0 and 1 keep their sentinel, the second status is S_OK, and page 2 holds the data.

Results at 12864bdb (Release, Ninja, MSVC 19.50, Windows 11 26300, RX 9070 XT, debug layer off):

  • ctest --test-dir out/build/check --no-tests=error -R '^startup_|storage|xcrdapi|dstorage': 105 tests, 0 failed. Four are capability skips (startup_view_heap_warp/hardware, startup_logical_view_warp/hardware), the same as before this change.
  • startup_stubs output:
    replaced: hr=0x00000000 first page ok, new page ok, old page kept
    unreadable: failed request hr=0x80004005, pages kept; following request hr=0x00000000, page ok
    
  • Negative controls, with only xcrdapi.cpp swapped and the new test kept:
    • Previous head (dc16b29b): unreadable: failed request hr=0x00000000, pages WRITTEN, so the test fails.
    • main: replaced: ... new page WRONG, old page WRITTEN, so the test fails.

AI-assisted (Claude Code).

**@⁠denn0x** on GitHub · 2026-11-24 11:40 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1415#issuecomment-7130408771) Thanks, both points are addressed in the new commit. **Stale-write path.** `run_entry` now checks that the whole page array is readable before it copies it. If it is not (null pointer, or any part unreadable), the request fails with `E_FAIL` before `execute_request` runs, so no destination page is written and no token captured at enqueue is used. Otherwise the array is copied once into the entry, and the replaced and zero counters come from that copy. `docs/startup-runtime.md` now says the caller has to keep the array readable and unchanged once the request is submitted. **Source-built queue test.** `startup_stub_consumer dstorage-pages` runs from `startup_stubs` (`tests/startup_stub_check.py`). Each of three physical pages has its own view and is filled with a sentinel first. - `replaced`: a two-page request is enqueued with `{page 0, page 1}`, and entry 1 is replaced with page 2 before `Submit`. It checks that page 0 and page 2 hold the data, page 1 keeps its sentinel, and the status is `S_OK`. - `unreadable`: a two-page request whose array is then made `PAGE_NOACCESS`, followed on the same queue by a valid one-page request into page 2. It checks that the first status is a failure, pages 0 and 1 keep their sentinel, the second status is `S_OK`, and page 2 holds the data. **Results** at 12864bdb (Release, Ninja, MSVC 19.50, Windows 11 26300, RX 9070 XT, debug layer off): - `ctest --test-dir out/build/check --no-tests=error -R '^startup_|storage|xcrdapi|dstorage'`: 105 tests, 0 failed. Four are capability skips (`startup_view_heap_warp/hardware`, `startup_logical_view_warp/hardware`), the same as before this change. - `startup_stubs` output: ``` replaced: hr=0x00000000 first page ok, new page ok, old page kept unreadable: failed request hr=0x80004005, pages kept; following request hr=0x00000000, page ok ``` - Negative controls, with only `xcrdapi.cpp` swapped and the new test kept: - Previous head (dc16b29b): `unreadable: failed request hr=0x00000000, pages WRITTEN`, so the test fails. - `main`: `replaced: ... new page WRONG, old page WRITTEN`, so the test fails. AI-assisted (Claude Code).
Author
Owner

@⁠triggered02 on GitHub · 2026-11-24 14:10 UTC · original

Heads-up: the page-array control this PR added is failing on current main (a3633d4d), and because the startup stand-in checks are not in CI (#1149 is still open) it is red without anyone seeing it.

Reproduction on a clean tree, no local changes:

$ ctest --preset release --no-tests=error -R '^startup_stubs$'
293 - startup_stubs ....................***Failed
  dstorage page-array control: exit=9 MISMATCH
  store context: exit=2 MISMATCH

The scenario itself prints the mismatch:

replaced: hr=0x00000000 first page ok, new page WRONG, old page WRITTEN

That is the replaced case from dstorage_page_array_checks(): a non-zero entry is reassigned between EnqueueRequest and Submit, and the expectation is that the new page receives the data while the old page keeps its sentinel. What I observe is the opposite — the old page is written — which is the exact failure mode the change in run_entry was meant to prevent: it re-reads the whole array at run time (std::vector<ULONG_PTR> now(title, title + entry.tokens.size())) and counts tokens_replaced_after_enqueue.

Two things I could not settle from the outside, so I am not proposing a fix:

  1. Whether the write path still resolves the destination through a token captured at enqueue. run_entry refreshes entry.tokens, but if the page-to-address resolution happens earlier (or is cached per entry), the refreshed tokens would not reach the write.
  2. Whether the test is simply wrong about the mapped view. It maps each physical page into its own view and checks the sentinel per view; if the replacement makes the request write through the new token but the view for that token is not the one the test inspects, the assertion would also read as "new page WRONG".

startup_stubs fails the same way on origin/main detached, so this is not something local to a branch. store context: exit=2 looks like a separate, older failure in the same test — a consumer that rejects the scenario argument — so it may just need the consumer rebuilt; I rebuilt mine and the page-array failure remained.

If it helps, the reproduction is ctest --preset dev --no-tests=error -R '^startup_stubs$' -V after cmake --build --preset dev --target startup_stub_consumer xg_xs d3d12_xs_shim xmem.

**@⁠triggered02** on GitHub · 2026-11-24 14:10 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1415#issuecomment-7133947694) Heads-up: the page-array control this PR added is failing on current `main` (`a3633d4d`), and because the startup stand-in checks are not in CI (#1149 is still open) it is red without anyone seeing it. Reproduction on a clean tree, no local changes: ``` $ ctest --preset release --no-tests=error -R '^startup_stubs$' 293 - startup_stubs ....................***Failed dstorage page-array control: exit=9 MISMATCH store context: exit=2 MISMATCH ``` The scenario itself prints the mismatch: ``` replaced: hr=0x00000000 first page ok, new page WRONG, old page WRITTEN ``` That is the `replaced` case from `dstorage_page_array_checks()`: a non-zero entry is reassigned between `EnqueueRequest` and `Submit`, and the expectation is that the new page receives the data while the old page keeps its sentinel. What I observe is the opposite — the old page is written — which is the exact failure mode the change in `run_entry` was meant to prevent: it re-reads the whole array at run time (`std::vector<ULONG_PTR> now(title, title + entry.tokens.size())`) and counts `tokens_replaced_after_enqueue`. Two things I could not settle from the outside, so I am not proposing a fix: 1. **Whether the write path still resolves the destination through a token captured at enqueue.** `run_entry` refreshes `entry.tokens`, but if the page-to-address resolution happens earlier (or is cached per entry), the refreshed tokens would not reach the write. 2. **Whether the test is simply wrong about the mapped view.** It maps each physical page into its own view and checks the sentinel per view; if the replacement makes the request write through the new token but the view for that token is not the one the test inspects, the assertion would also read as "new page WRONG". `startup_stubs` fails the same way on `origin/main` detached, so this is not something local to a branch. `store context: exit=2` looks like a separate, older failure in the same test — a consumer that rejects the scenario argument — so it may just need the consumer rebuilt; I rebuilt mine and the page-array failure remained. If it helps, the reproduction is `ctest --preset dev --no-tests=error -R '^startup_stubs$' -V` after `cmake --build --preset dev --target startup_stub_consumer xg_xs d3d12_xs_shim xmem`.
Author
Owner

@⁠triggered02 on GitHub · 2026-11-24 14:19 UTC · original

Correction — this was my workspace, not this change. With the stand-ins rebuilt, the page-array control passes on current main:

293: unreadable: failed request hr=0x80004005, pages kept; following request hr=0x00000000, page ok
293: dstorage page-array control: exit=0 ok
293: store context: exit=0 ok
293: PASS
293 - startup_stubs ... Passed 0.73 sec

Cause of the false alarm: my build directory held a stale XCRDAPI.dll. ctest does not build anything, and I had rebuilt startup_stub_consumer (and some shims) after syncing main but not startup_stub_xcrdapi. The stale DLL still had the pre-run_entry behaviour — pages read from the enqueue-time snapshot — which is exactly the symptom I reported: "new page WRONG, old page WRITTEN". Once startup_stub_xcrdapi was rebuilt, the re-read enqueue=0x20 run=0x30 is visible and the replacement lands on the new page.

With the debug line I added to confirm it, the log shows the intended behaviour directly:

x2w-dstorage   page token[0] enqueue=0x10 run=0x10
x2w-dstorage   page token[1] enqueue=0x20 run=0x30
x2w-dstorage   options=0x600001 page array 0000022AF7340000 (2 entries) unreadable when the request runs -> failed
x2w-dstorage   page token[0] enqueue=0x30 run=0x30

So: the replaced and unreadable cases both behave as this PR intended, and I should not have reported them as failing. Sorry for the noise — the two hypotheses I offered were both wrong because the third possibility (stale artifact) never occurred to me.

One thing that may still be worth acting on: startup_stubs takes DLL paths from the build tree, so a stale shim fakes a failure exactly like a real regression. That is a sharp edge for anyone bisecting locally, but it is not this PR's problem and I am not proposing a change to the test here.

I have reverted my local debug line; nothing in this PR or the tree needs changing on account of my report.

**@⁠triggered02** on GitHub · 2026-11-24 14:19 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1415#issuecomment-7134184709) Correction — this was my workspace, not this change. With the stand-ins rebuilt, the page-array control passes on current `main`: ``` 293: unreadable: failed request hr=0x80004005, pages kept; following request hr=0x00000000, page ok 293: dstorage page-array control: exit=0 ok 293: store context: exit=0 ok 293: PASS 293 - startup_stubs ... Passed 0.73 sec ``` **Cause of the false alarm:** my build directory held a stale `XCRDAPI.dll`. `ctest` does not build anything, and I had rebuilt `startup_stub_consumer` (and some shims) after syncing `main` but not `startup_stub_xcrdapi`. The stale DLL still had the pre-`run_entry` behaviour — pages read from the enqueue-time snapshot — which is exactly the symptom I reported: "new page WRONG, old page WRITTEN". Once `startup_stub_xcrdapi` was rebuilt, the re-read `enqueue=0x20 run=0x30` is visible and the replacement lands on the new page. With the debug line I added to confirm it, the log shows the intended behaviour directly: ``` x2w-dstorage page token[0] enqueue=0x10 run=0x10 x2w-dstorage page token[1] enqueue=0x20 run=0x30 x2w-dstorage options=0x600001 page array 0000022AF7340000 (2 entries) unreadable when the request runs -> failed x2w-dstorage page token[0] enqueue=0x30 run=0x30 ``` So: the `replaced` and `unreadable` cases both behave as this PR intended, and I should not have reported them as failing. Sorry for the noise — the two hypotheses I offered were both wrong because the third possibility (stale artifact) never occurred to me. One thing that may still be worth acting on: `startup_stubs` takes DLL paths from the build tree, so a stale shim fakes a failure exactly like a real regression. That is a sharp edge for anyone bisecting locally, but it is not this PR's problem and I am not proposing a change to the test here. I have reverted my local debug line; nothing in this PR or the tree needs changing on account of my report.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 11:10 UTC · original · requested changes

The replacement counts and loading runs help isolate this. Please add a source-built queue test that changes a nonzero destination entry between EnqueueRequest and Submit, then checks the new page receives the data and the old page retains its sentinel. There is still a stale-write path in run_entry: if the page array or an entry cannot be read at execution, the loop can retain the old captured token. Please fail that request before writing any destination pages, and cover the failure and a following valid request. Document the caller requirement to keep the array readable and stable while the worker snapshots it.

**@⁠R0Y00** on GitHub · 2026-11-24 11:10 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1415#pullrequestreview-6038140190) · **requested changes** The replacement counts and loading runs help isolate this. Please add a source-built queue test that changes a nonzero destination entry between EnqueueRequest and Submit, then checks the new page receives the data and the old page retains its sentinel. There is still a stale-write path in run_entry: if the page array or an entry cannot be read at execution, the loop can retain the old captured token. Please fail that request before writing any destination pages, and cover the failure and a following valid request. Document the caller requirement to keep the array readable and stable while the worker snapshots it.
Author
Owner

@⁠tedin7 on GitHub · 2026-11-24 11:16 UTC · original · reviewed

Reviewed and verified at dc16b29bed3895a675a2b1205c6423820f0ff23a:

  • Diff & bounds audit: git diff --check origin/main...HEAD is clean. pages is computed in std::size_t (xcrdapi.cpp:671) and execute_request checks needed_pages > granted_pages and zero tokens before x2w_xmem_write_physical. However, two gaps remain (aligning with the open change request):
    1. In run_entry (xcrdapi.cpp:574-580), if title is null or x2w_local::readable(title + i, sizeof(ULONG_PTR)) returns false at execution time, entry.tokens[i] retains the stale token captured at EnqueueRequest instead of failing the request before any destination write.
    2. No source-built queue test yet covers replacing a non-zero destination page token between EnqueueRequest and Submit (or the unreadable-array failure + subsequent valid request).
  • Linux checks: docs_links_check.py (16 Markdown files, 322 links) and bidi_controls_check.py (1181 tracked files) passed.
  • Windows Server 2025 (Build 26100, MSVC 14.44.35207):
    • Debug (--preset dev): ctest --preset dev -R '^(startup_stubs|startup_storage_directory_batch|startup_storage_file_range)$' 3/3 passed (0.48s).
    • Release /O2 (--preset release): 3/3 passed (0.53s). (Note: existing tests pass, but do not yet cover mid-flight non-zero token replacement.)
**@⁠tedin7** on GitHub · 2026-11-24 11:16 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1415#pullrequestreview-6038228814) · **reviewed** Reviewed and verified at `dc16b29bed3895a675a2b1205c6423820f0ff23a`: - **Diff & bounds audit**: `git diff --check origin/main...HEAD` is clean. `pages` is computed in `std::size_t` (`xcrdapi.cpp:671`) and `execute_request` checks `needed_pages > granted_pages` and zero tokens before `x2w_xmem_write_physical`. However, two gaps remain (aligning with the open change request): 1. In `run_entry` (`xcrdapi.cpp:574-580`), if `title` is null or `x2w_local::readable(title + i, sizeof(ULONG_PTR))` returns false at execution time, `entry.tokens[i]` retains the stale token captured at `EnqueueRequest` instead of failing the request before any destination write. 2. No source-built queue test yet covers replacing a non-zero destination page token between `EnqueueRequest` and `Submit` (or the unreadable-array failure + subsequent valid request). - **Linux checks**: `docs_links_check.py` (16 Markdown files, 322 links) and `bidi_controls_check.py` (1181 tracked files) passed. - **Windows Server 2025 (Build 26100, MSVC 14.44.35207)**: - Debug (`--preset dev`): `ctest --preset dev -R '^(startup_stubs|startup_storage_directory_batch|startup_storage_file_range)$'` **3/3 passed** (`0.48s`). - Release `/O2` (`--preset release`): **3/3 passed** (`0.53s`). (Note: existing tests pass, but do not yet cover mid-flight non-zero token replacement.)
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 12:03 UTC · original · approved these changes

Windows Release startup_stubs passed on this head. It observed a replaced nonzero token writing to the new page with the old sentinel unchanged. The unreadable-array case returns E_FAIL without changing either page, and the next request succeeds.

**@⁠R0Y00** on GitHub · 2026-11-24 12:03 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1415#pullrequestreview-6038840759) · **approved these changes** Windows Release startup_stubs passed on this head. It observed a replaced nonzero token writing to the new page with the old sentinel unchanged. The unreadable-array case returns E_FAIL without changing either page, and the next request succeeds.
This discussion has been locked. Commenting is limited to contributors.
No description provided.