Skip to content

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

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
Cattn:dstorage-enqueue-reads
Nov 24, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
Cattn:dstorage-enqueue-reads

Conversation

@Cattn

@Cattn Cattn commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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

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.

…thout sizing regions

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.
@mastrr9

mastrr9 commented Nov 23, 2026

Copy link
Copy Markdown

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).

@throny

throny commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

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.

@ivall
ivall merged commit 1efacca into xcl-dev:main Nov 24, 2026
2 checks passed
@ivall

ivall commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

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.

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.

4 participants