DirectStorage: EnqueueRequest reads its page array and file vtable without sizing regions #322

Manually merged
github merged 1 commit from gh-pr/322/head into gh-pr/322/base 2026-11-26 04:20:00 +00:00
Owner

@⁠Cattn on GitHub · 2026-11-23 23:26 UTC · original
Cattn:dstorage-enqueue-reads → main · +12 −3 · 1 files · 1 commits · merged 2026-11-24 00:03 UTC

IDStorageQueueX::EnqueueRequest checked two small inputs with gpu::shader::readable: the destination page array of a request with DEST_PHYS_PAGES, and the file object's vtable pointer. readable calls VirtualQuery for each region the range touches. VirtualQuery sizes the region around the address by walking it, and a retail title keeps its page arrays inside very large reservations with scattered committed pages. While the title streams its world it enqueues thousands of requests a second from one thread, so the check costs far more than the read.

What is in it

  • dstorage.rs: snapshot_page_array copies the array with ReadProcessMemory on the current process. That copies the bytes or fails on memory that isn't readable, which is what the check was for, without sizing the region. Unreadable memory still refuses the request with E_ACCESS_VIOLATION.
  • The file-object check reads the vtable pointer the same way (8 bytes).
  • readable itself is unchanged; other callers keep it.

Tested

  • RTX 4070 12 GB (driver 617.42), Ryzen 7 7800X3D, 32 GB RAM, Windows 11 26200, rustc 1.93.1, cargo build --workspace then release and debug tests.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 656 passed, 0 failed, 40 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • Cost of one check, synthetic: a 64-byte read inside a 24 GB reservation with every third 64 KiB page committed takes 7.2 µs through readable and 0.4 µs through ReadProcessMemory.
  • Retail title, world streaming after the intro, 400 s runs on a local stack: main 7a033c8 + #203, #187, #208 a7ffb2f, #244, #252, #283, #269, #292, #289.
    • Time to the stop:
      • without this change: 49,469 requests (2.9 GB), 1,646 failed, by 303 s, where the title's watchdog ended it;
      • with it: 62,142 requests (3.5 GB), 324 failed, by 292 s, where a free-RAM guard ended it.
    • The pause in presents about 60 s into streaming is unchanged, so this is not its cause.
    • With the timing logged, every EnqueueRequest call on that stack before the change took 5–13 ms, small requests too. This change removes one part of that cost; I did not identify the rest.

Regression run

main:

xcl-smoke v1
commit: 21b8723038aa05e2541edd9f83928b1b73383974
gpu: Meta Virtual Monitor
title: 2615684f
seconds: 60
exit: 0x0
frames shown: 1421 (PresentX 1421)
presents per 2 s (last 3): 60 58 57
device removals: 0
crashes: 0
peak commit: 14880 MB

PR:

xcl-smoke v1
commit: c8e3ca06c3a43610b4870486febf72513d0758e3
gpu: Meta Virtual Monitor
title: 2615684f
seconds: 60
exit: 0x0
frames shown: 1469 (PresentX 1470)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14794 MB

(gpu: names a virtual monitor's adapter; the title ran on the RTX 4070. #282 changes what smoke.ps1 prints there.)

Not tested

  • AMD GPUs.
  • The 60 s smoke run ends before world streaming starts, so it covers the menus' DirectStorage requests only. The streaming numbers above come from the larger local stack, not from main.

AI use

  • Claude Code (Claude Opus 5.5): found the call in a thread dump, wrote the change and this description. I ran the builds, the tests, the smoke runs and the game runs above on the hardware named.
**@⁠Cattn** on GitHub · 2026-11-23 23:26 UTC · [original](https://github.com/xcl-dev/xcl/pull/322) `Cattn:dstorage-enqueue-reads` → `main` · +12 −3 · 1 files · 1 commits · merged 2026-11-24 00:03 UTC `IDStorageQueueX::EnqueueRequest` checked two small inputs with `gpu::shader::readable`: the destination page array of a request with `DEST_PHYS_PAGES`, and the file object's vtable pointer. `readable` calls `VirtualQuery` for each region the range touches. `VirtualQuery` sizes the region around the address by walking it, and a retail title keeps its page arrays inside very large reservations with scattered committed pages. While the title streams its world it enqueues thousands of requests a second from one thread, so the check costs far more than the read. **What is in it** - `dstorage.rs`: `snapshot_page_array` copies the array with `ReadProcessMemory` on the current process. That copies the bytes or fails on memory that isn't readable, which is what the check was for, without sizing the region. Unreadable memory still refuses the request with `E_ACCESS_VIOLATION`. - The file-object check reads the vtable pointer the same way (8 bytes). - `readable` itself is unchanged; other callers keep it. **Tested** - RTX 4070 12 GB (driver 617.42), Ryzen 7 7800X3D, 32 GB RAM, Windows 11 26200, rustc 1.93.1, `cargo build --workspace` then release and debug tests. - `cargo test --workspace --no-fail-fast -- --test-threads=4`: 656 passed, 0 failed, 40 ignored. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - Cost of one check, synthetic: a 64-byte read inside a 24 GB reservation with every third 64 KiB page committed takes 7.2 µs through `readable` and 0.4 µs through `ReadProcessMemory`. - Retail title, world streaming after the intro, 400 s runs on a local stack: main `7a033c8` + #203, #187, #208 `a7ffb2f`, #244, #252, #283, #269, #292, #289. - Time to the stop: - without this change: 49,469 requests (2.9 GB), 1,646 failed, by 303 s, where the title's watchdog ended it; - with it: 62,142 requests (3.5 GB), 324 failed, by 292 s, where a free-RAM guard ended it. - The pause in presents about 60 s into streaming is unchanged, so this is not its cause. - With the timing logged, every `EnqueueRequest` call on that stack before the change took 5–13 ms, small requests too. This change removes one part of that cost; I did not identify the rest. **Regression run** main: ``` xcl-smoke v1 commit: 21b8723038aa05e2541edd9f83928b1b73383974 gpu: Meta Virtual Monitor title: 2615684f seconds: 60 exit: 0x0 frames shown: 1421 (PresentX 1421) presents per 2 s (last 3): 60 58 57 device removals: 0 crashes: 0 peak commit: 14880 MB ``` PR: ``` xcl-smoke v1 commit: c8e3ca06c3a43610b4870486febf72513d0758e3 gpu: Meta Virtual Monitor title: 2615684f seconds: 60 exit: 0x0 frames shown: 1469 (PresentX 1470) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14794 MB ``` (`gpu:` names a virtual monitor's adapter; the title ran on the RTX 4070. #282 changes what smoke.ps1 prints there.) **Not tested** - AMD GPUs. - The 60 s smoke run ends before world streaming starts, so it covers the menus' DirectStorage requests only. The streaming numbers above come from the larger local stack, not from main. **AI use** - Claude Code (Claude Opus 5.5): found the call in a thread dump, wrote the change and this description. I ran the builds, the tests, the smoke runs and the game runs above on the hardware named.
EnqueueRequest checked the destination page array and the file object with
gpu::shader::readable, which calls VirtualQuery for each region the range
touches. VirtualQuery sizes a region by walking it, and a retail title keeps
those arrays inside very large reservations with scattered committed pages,
so the check costs far more than the read. While the title streams its world
it enqueues thousands of requests a second, on one of its threads.

The two reads are now ReadProcessMemory on the current process: it copies
the bytes or fails on memory that is not readable, which is what the check
was for, without sizing the region around them.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:19:59 +00:00
github manually merged commit c8e3ca06c3 into gh-pr/322/base 2026-11-26 04:20:00 +00:00
Author
Owner

@⁠mastrr9 on GitHub · 2026-11-23 23:38 UTC · original

Static code audit of c8e3ca0 against crates/xcl/src/hle/dstorage.rs on main (not tested on Windows; no game files):

Looks clean and is a strict safety + performance improvement over readable() + raw deref:

  • snapshot_page_array returns early when byte_len == 0 (dstorage.rs:699), so read_own is only called with count >= 1 (bytes >= 8).
  • ReadProcessMemory(GetCurrentProcess(), ...) copies into Vec<usize> / vt: usize without VirtualQuery's region-walk cost, closes the TOCTOU window between readable() and reading the bytes, and avoids potential unaligned pointer UB from the previous slice::from_raw_parts(ptr as *const usize, count) and *(f as *const *const usize).
  • at != 0 plus done == len properly rejects null, partial, unmapped, PAGE_NOACCESS, and PAGE_GUARD reads with E_ACCESS_VIOLATION / E_FILE_HANDLE as before.

AI use: static code review of c8e3ca0 with Antigravity (Gemini 4 Neon).

**@⁠mastrr9** on GitHub · 2026-11-23 23:38 UTC · [original](https://github.com/xcl-dev/xcl/pull/322#issuecomment-7117196658) Static code audit of `c8e3ca0` against `crates/xcl/src/hle/dstorage.rs` on `main` (not tested on Windows; no game files): Looks clean and is a strict safety + performance improvement over `readable()` + raw deref: - `snapshot_page_array` returns early when `byte_len == 0` (`dstorage.rs:699`), so `read_own` is only called with `count >= 1` (`bytes >= 8`). - `ReadProcessMemory(GetCurrentProcess(), ...)` copies into `Vec<usize>` / `vt: usize` without `VirtualQuery`'s region-walk cost, closes the TOCTOU window between `readable()` and reading the bytes, and avoids potential unaligned pointer UB from the previous `slice::from_raw_parts(ptr as *const usize, count)` and `*(f as *const *const usize)`. - `at != 0` plus `done == len` properly rejects null, partial, unmapped, `PAGE_NOACCESS`, and `PAGE_GUARD` reads with `E_ACCESS_VIOLATION` / `E_FILE_HANDLE` as before. *AI use: static code review of `c8e3ca0` with Antigravity (Gemini 4 Neon).*
Author
Owner

@⁠throny on GitHub · 2026-11-23 23:58 UTC · original

Test report for c8e3ca0, merged onto current main 2b4bb43 (clean merge, merge commit d55c8e8): pass.

Tested

  • Windows 11 Pro 10.0.26300, NVIDIA GeForce RTX 4080 (driver 610.88), Intel i9-12900K, 64 GB; Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace && cargo test --workspace -- --test-threads=4: 670 passed, 0 failed, 41 ignored (lib: 630 / 0 / 21)
  • SKIPPED on this adapter: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64)
  • The existing dstorage tests that enqueue DEST_PHYS_PAGES requests through snapshot_page_array pass, including the one where a stack table's entry is filled in after enqueue and an all-zero table is refused with E_ACCESS_VIOLATION.

Reviewed (read the whole diff)

  • byte_len == 0 returns before read_own, and count comes from a u32 size, so the new vec![0usize; count] is at most ~0.5 MB, the same size the old to_vec allocated.
  • The refusal paths the change keeps (a page array in unreadable memory → E_ACCESS_VIOLATION, a source pointer that is not readable → E_FILE_HANDLE) have no test in the suite, before or after this PR. A test with a PAGE_NOACCESS page for each would cover what read_own replaces.

Not tested

  • The performance claim (no timing run here), no regression run (scripts/smoke.ps1) on a title, no XCL_SAMPLE_* game-data tests.
  • AMD and wave64 hardware, release build.

AI use

  • Claude Code (Claude Opus 5.5) ran the build and the tests, read the diff and wrote this report. The runs above are real, on the hardware named.
**@⁠throny** on GitHub · 2026-11-23 23:58 UTC · [original](https://github.com/xcl-dev/xcl/pull/322#issuecomment-7117564627) Test report for `c8e3ca0`, merged onto current `main` `2b4bb43` (clean merge, merge commit `d55c8e8`): **pass**. **Tested** - Windows 11 Pro 10.0.26300, NVIDIA GeForce RTX 4080 (driver 610.88), Intel i9-12900K, 64 GB; Rust 1.100.0 `stable-x86_64-pc-windows-msvc`, debug - `cargo build --workspace && cargo test --workspace -- --test-threads=4`: 670 passed, 0 failed, 41 ignored (lib: 630 / 0 / 21) - `SKIPPED` on this adapter: `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware` (no wave64) - The existing `dstorage` tests that enqueue `DEST_PHYS_PAGES` requests through `snapshot_page_array` pass, including the one where a stack table's entry is filled in after enqueue and an all-zero table is refused with `E_ACCESS_VIOLATION`. **Reviewed** (read the whole diff) - `byte_len == 0` returns before `read_own`, and `count` comes from a `u32` size, so the new `vec![0usize; count]` is at most ~0.5 MB, the same size the old `to_vec` allocated. - The refusal paths the change keeps (a page array in unreadable memory → `E_ACCESS_VIOLATION`, a source pointer that is not readable → `E_FILE_HANDLE`) have no test in the suite, before or after this PR. A test with a `PAGE_NOACCESS` page for each would cover what `read_own` replaces. **Not tested** - The performance claim (no timing run here), no regression run (`scripts/smoke.ps1`) on a title, no `XCL_SAMPLE_*` game-data tests. - AMD and wave64 hardware, release build. **AI use** - Claude Code (Claude Opus 5.5) ran the build and the tests, read the diff and wrote this report. The runs above are real, on the hardware named.
Author
Owner

@⁠ivall on GitHub · 2026-11-24 00:03 UTC · original

Merged (a6f5f7a). Checked on an RX 9070 (RDNA4) on main a6f5f7a, together with #335, #322, #331, #330, #338, #318 and #332: cargo test --workspace -- --test-threads=4 - 636 unit tests passed, 0 failed; the retail game reaches its menu and draws it (frame dump) and goes on past it.

**@⁠ivall** on GitHub · 2026-11-24 00:03 UTC · [original](https://github.com/xcl-dev/xcl/pull/322#issuecomment-7117661107) Merged (a6f5f7a). Checked on an RX 9070 (RDNA4) on main a6f5f7a, together with #335, #322, #331, #330, #338, #318 and #332: `cargo test --workspace -- --test-threads=4` - 636 unit tests passed, 0 failed; the retail game reaches its menu and draws it (frame dump) and goes on past it.
This discussion has been locked. Commenting is limited to contributors.
No description provided.