Skip to content

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

Open
Cattn wants to merge 1 commit into
ivall:mainfrom
Cattn:dstorage-enqueue-reads
Open

Cattn wants to merge 1 commit into
ivall: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: GTA6.Xbox.v1.0.2
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: GTA6.Xbox.v1.0.2
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.
@ethandepstein-del

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

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.

2 participants