Skip to content

startup: refuse a storage request with a non-writable destination - #1672

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-xcrd-writable
Nov 24, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-xcrd-writable

Conversation

@tedin7

@tedin7 tedin7 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Change

A DirectStorage request into a plain destination pointer is copied with one memcpy in xcrdapi.cpp. The only check before it compares sizes. A run on #870 ended with a write fault inside that copy: the destination was not writable to its declared capacity.

Now destination_writable walks the destination with VirtualQuery, region by region, over the whole declared capacity, before the first byte is written. A page that is uncommitted, read-only, PAGE_GUARD or PAGE_NOACCESS refuses the request:

  • nothing is written, including to the writable pages before the bad one;
  • the request is counted in requests_failed, logged once per the existing limit as ... -> destination not writable, and the next status entry reports E_FAIL, like the other refusals in the file;
  • later requests on the same queue run normally.

Choices:

  • The whole declared capacity is judged, not only the bytes this request would write. A destination shorter in memory than its declared size is what the fault came from. If a title legitimately declares more than it maps, this refuses it; the log line shows the destination and size.
  • Page-token destinations (option bit 21) are unchanged; they write through the xmem helper.
  • Cost: one VirtualQuery per region the destination spans, per request that reaches the copy. On a multi-gigabyte region one query can take about a millisecond.

Refs #870, #1198.

Verification

Linux:

  • git diff --check: clean.
  • python tests/docs_links_check.py: self-test passed, 16 Markdown files and 351 links.
  • python tests/bidi_controls_check.py: self-test passed, 1277 tracked files, no bidirectional controls.
  • x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only on tests/startup_dstorage_destination_check.cpp: no diagnostics. On xcrdapi.cpp: only the MSVC-only __try/__except errors in local_log.h.
  • destination_writable built outside the project and run under Wine: all rows passed (all writable, zero bytes, null, length overflow, past the allocation, read-only, guard, no-access and decommitted middle or last page, mixed read-write and execute-read-write). Two local mutations were each caught and reverted: removing the guard/no-access test failed the guard row; stopping after the first region failed five rows.
  • git merge-tree against startup: decode BC3, BC4, and BC5 BCPack DirectStorage streams #1390, startup: support interleaved chunked streams in DirectStorage #1429 and runtime: experiment with metadata-backed compressed chunk windows #1616: the same files conflict with this branch as with current main, so this change adds no new conflicting file.

Windows Server 2025 (Build 26100, MSVC 14.44.35207), commit 79899b7d, via x2w_pipe.py win2025 1672 --targets d3d12_xs_shim,startup_dstorage_destination_check,startup_storage_alloc_failure,startup_stub_acphal,startup_stub_chad,startup_stub_consumer,startup_stub_gameinput,startup_stub_xapu,startup_stub_xcrdapi,startup_stub_xg_xs,xmem_shim --regex '^(startup_dstorage_destination|startup_heartbeat_commit|startup_storage_alloc_failure|startup_stubs)$':

  • Debug (--preset dev): build passed; ctest --preset dev: 4/4 passed, including startup_dstorage_destination.
  • Release /O2 (--preset release): build passed; ctest --preset release: 4/4 passed, including startup_dstorage_destination.

Evidence

tests/startup_dstorage_destination_check.cpp (ctest startup_dstorage_destination) loads the stand-in DLL and sends memory-source requests into three pages that start as 0xAA. Each case makes one part unusable: a read-only, a guard and a no-access middle page, and a capacity one page past the committed pages. Each must fail with the process alive and the first page (and, where readable, the last) still 0xAA. Afterwards a request into a fully writable buffer on the same queue must write every byte. docs/startup-runtime.md and docs/experiments.md describe it.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted; commands listed were executed locally.

@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

The guard works; one allowed protection is never exercised, and one check is redundant

Reviewed 79899b7d. Built at that head, ran the fixture, then removed four pieces of destination_writable one at a
time and rebuilt the DLL that the fixture actually loads (XCRDAPI.dll, target startup_stub_xcrdapi) — the first
attempt rebuilt the wrong target and every mutant appeared to survive, so the numbers below are from the run where
XCRDAPI.dll's timestamp changed each time.

Environment: Windows 11 build 26300.9974, MSVC 14.44.35207, SDK 10.0.26100.

cmake --preset dev
ctest --preset dev --no-tests=error -R '^startup_dstorage_' -V
startup_dstorage_file_read ............... Passed 0.13 s
startup_dstorage_file_read_alias_control . Passed 0.02 s
startup_dstorage_destination ............. Passed
  ... -> destination not writable      (repeated, requests_failed counting up, and the 0x1000 size case)

Mutations

change result
drop PAGE_GUARD from the reject mask caught
allow only PAGE_READWRITE, drop PAGE_WRITECOPY not caught
walk cursor = region_end -> cursor += 1 not caught (behaviourally equal, only slower)
drop info.State != MEM_COMMIT not caught, and I think it is equivalent

The one that matters

PAGE_WRITECOPY and PAGE_EXECUTE_WRITECOPY are in the allowed set, and no case exercises them: deleting
protect != PAGE_WRITECOPY leaves the fixture green. A copy-on-write destination is a legitimate target for a
DirectStorage copy, so that allowance is load-bearing, and today nothing would notice if it were dropped — or, worse, if
it were moved to the reject side, the fixture would stay green while a valid title request started failing.

Adding one case is cheap: VirtualAlloc(MEM_COMMIT, PAGE_READWRITE) then VirtualProtect(..., PAGE_WRITECOPY), enqueue
a small copy, expect success and the bytes present. If you want it in this PR I can push it as a second commit on a
branch of yours, or open it separately with Refs #1672 — your call, since it is your fixture.

The two that do not need a test

  • cursor += 1 is the same accept/reject decision, only one VirtualQuery per byte instead of per region. On a
    multi-megabyte destination that is a real cost, but asserting call counts would be brittle; I would leave it.
  • info.State != MEM_COMMIT is redundant with the PAGE_NOACCESS check that follows: free and reserved ranges report
    PAGE_NOACCESS. Keeping it is fine as defence in depth; I mention it so nobody reads its survival as a hole.

Verdict

Reviewed 79899b7d. The refusal is correct, the counters behave as described, and the "nothing written before the bad
page" property holds in the run. One gap worth closing: the copy-on-write destination is allowed but untested.

@tedin7
tedin7 marked this pull request as ready for review November 24, 2026 16:57
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T16:59:53.228328Z 79899b7 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Here is the copy-on-write case I mentioned, verified on your head 79899b7d. I did not push to your branch — apply it or tell me to open it separately with Refs #1672.

The first thing I tried does not work and is worth knowing before anyone else tries it: VirtualProtect(..., PAGE_WRITECOPY) fails on private committed memory, so a copy-on-write destination cannot be made that way. It needs a mapped view — a pagefile-backed section mapped FILE_MAP_COPY. That is why the case below creates a section instead of protecting the existing region.

@@ -139,6 +139,25 @@ int main(int argc, char** argv)
     else std::printf("ok: writable control after the refusals wrote every byte\n");
     VirtualFree(region, 0, MEM_RELEASE);
 
+    // A copy-on-write page fault is not a write to the section, so the whitelist has to keep
+    // PAGE_WRITECOPY/PAGE_EXECUTE_WRITECOPY. Windows refuses PAGE_WRITECOPY on private memory,
+    // so the case needs a mapped view: a pagefile-backed section mapped FILE_MAP_COPY.
+    {
+        HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_WRITECOPY, 0,
+                                           static_cast<DWORD>(bytes), nullptr);
+        auto* cow = section ? static_cast<unsigned char*>(MapViewOfFile(section, FILE_MAP_COPY, 0, 0, bytes)) : nullptr;
+        if (!cow) fail("copy-on-write destination", "could not map a copy view");
+        else {
+            const long cow_hr = run_request(cow, bytes, bytes);
+            if (cow_hr == -1) fail("copy-on-write destination", "no completion");
+            else if (cow_hr != 0) fail("copy-on-write destination", "request failed");
+            else if (std::memcmp(cow, source, bytes) != 0) fail("copy-on-write destination", "bytes differ");
+            else std::printf("ok: copy-on-write destination accepted and written\n");
+            UnmapViewOfFile(cow);
+        }
+        if (section) CloseHandle(section);
+    }
+
     if (failures) return 1;
     std::printf("dstorage destination: refused without writes, queue kept working\n");
     return 0;

Verified both ways, rebuilding startup_dstorage_destination_check and XCRDAPI.dll each time:

with the case, unmodified xcrdapi.cpp        ok: copy-on-write destination accepted and written
                                             startup_dstorage_destination PASSED 0.21 s

with the case, PAGE_WRITECOPY removed from
the whitelist in xcrdapi.cpp                 startup_dstorage_destination FAILED

reverted, both rebuilt                       PASSED

The first line is the positive case; the second is the negative control that was missing before — without the new case, deleting protect != PAGE_WRITECOPY left the fixture green (that was the mutation in my earlier comment).

Two details in case the mapping matters to you: the section is pages * 0x1000 bytes, which matches the bytes the other cases use, and the copy view is unwritten before the request, so memcmp against source proves the copy wrote every byte rather than leaving zeros. MapViewOfFile needs FILE_MAP_COPY rather than FILE_MAP_WRITE for the pages to come back as PAGE_WRITECOPY at all.

If you would rather keep the case out of this PR, I will open it as its own with Refs #1672 — it is a test-only change either way.

@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks @ChazY11 for the thorough review and mutation check.

Addressed in 1a4557cef4bc82d6f7bac4a63244d613ad56a576:

  • Added a copy-on-write (PAGE_WRITECOPY) destination case to tests/startup_dstorage_destination_check.cpp (and updated docs/startup-runtime.md). It creates a pagefile-backed CreateFileMappingA section mapped with FILE_MAP_COPY, verifies via VirtualQuery that the view starts as PAGE_WRITECOPY, enqueues a copy across the full mapping, and checks that it succeeds and every byte matches. (A mapped view is used because Windows rejects VirtualProtect(..., PAGE_WRITECOPY) on private VirtualAlloc pages.)
  • Kept info.State != MEM_COMMIT as defense in depth (since MEMORY_BASIC_INFORMATION::Protect is only defined for committed pages), as you noted.

Verification (1a4557cef4bc82d6f7bac4a63244d613ad56a576)

Linux:

  • git diff --check: clean
  • python3 tests/docs_links_check.py: self-test passed, 16 Markdown files and 351 links
  • python3 tests/bidi_controls_check.py: self-test passed, 1278 tracked files, no bidirectional controls
  • x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only tests/startup_dstorage_destination_check.cpp: clean

Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP):

  • Debug (--preset dev):
    • cmake --build --preset dev --target d3d12_xs_shim startup_dstorage_destination_check startup_storage_alloc_failure startup_stub_acphal startup_stub_chad startup_stub_consumer startup_stub_gameinput startup_stub_xapu startup_stub_xcrdapi startup_stub_xg_xs xmem_shim -- -j 6: passed (3.52s)
    • ctest --preset dev -R '^(startup_dstorage_destination|startup_heartbeat_commit|startup_storage_alloc_failure|startup_stubs)$' --parallel 4 --no-tests=error --output-on-failure: 4/4 passed (30.27s: startup_storage_alloc_failure 0.07s, startup_dstorage_destination 0.09s, startup_stubs 0.55s, startup_heartbeat_commit 30.20s)
  • Release /O2 (--preset release):
    • cmake --build --preset release --target d3d12_xs_shim startup_dstorage_destination_check startup_storage_alloc_failure startup_stub_acphal startup_stub_chad startup_stub_consumer startup_stub_gameinput startup_stub_xapu startup_stub_xcrdapi startup_stub_xg_xs xmem_shim -- -j 6: passed (1.08s)
    • ctest --preset release -R '^(startup_dstorage_destination|startup_heartbeat_commit|startup_storage_alloc_failure|startup_stubs)$' --parallel 4 --no-tests=error --output-on-failure: 4/4 passed (30.26s: startup_dstorage_destination 0.09s, startup_storage_alloc_failure 0.09s, startup_stubs 0.46s, startup_heartbeat_commit 30.19s)

AI-assisted; commands listed were executed locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants