Skip to content

runtime: DirectStorage zlib requests (file and memory sources) - #7180

Closed
KkelebekK wants to merge 8 commits into
ProjectLeonida:mainfrom
KkelebekK:dstorage-zlib-requests
Closed

KkelebekK wants to merge 8 commits into
ProjectLeonida:mainfrom
KkelebekK:dstorage-zlib-requests

Conversation

@KkelebekK

Copy link
Copy Markdown

Claimed in the coordination issue as: #7049 (comment) (item 2)

Depends on: #4348. runtime/shims/dstorage.cpp only exists there, so this branch is based on its head (45c39cf6f). This PR's own diff is the one commit on top, 00551ba00 ("DirectStorage: inflate zlib requests (file and memory sources)"). Until #4348 lands, GitHub shows #4348's commits here too.

What and why

#4348 answers every DirectStorage request with the zlib option with E_NOTIMPL. The retail executable reads its pipeline and state data through DirectStorage zlib requests. With those requests refused, the title's native pipeline descriptions arrive zero-filled. On my own copy, a local inflate in this path made those reads succeed and the data come back non-zero. This PR does that work properly.

  • zlib requests (file and memory sources). A request with ZlibDecompress alone reads its SourceSize bytes into a staging buffer and inflates them on the queue's worker. The inflater is the runtime's existing RFC 1950 one, leonida::zlib_inflate in runtime/src/shader_library.h (the XBCH one); no second inflater is added. The result is copied to Destination only when it is exactly DestinationSize bytes. So a failed stream leaves the destination untouched, and a memory source inside its own destination (in-place loading) inflates correctly. Both buffers are at most 32 MiB (the existing unpaged-buffer limit), so staging is bounded.

  • Checks from the public docs:

    • With zlib, DestinationSize must be larger than SourceSize. This is E_DSTORAGE_INVALID_DESTINATION_SIZE's documented rule, and DSTORAGE_REQUEST says the same about DestinationSize.
    • A zlib FileOffset must be 16-byte aligned (DSTORAGE_REQUEST), else E_DSTORAGE_INVALID_FILE_OFFSET. That constant was already declared but never used.
    • IntermediateSize is only for zlib + BCPack, so it stays 0.
    • The console DSTORAGE_REQUEST has no UncompressedSize field (that is the PC struct's); DestinationSize plays that role.
  • Errors, all from dstorageerr.h:

    Stream HRESULT
    header not deflate, window over 32 KiB, bad check value, preset dictionary E_DSTORAGE_ZLIB_BAD_HEADER 0x89240031
    reserved block type, invalid code, distance before start, truncated E_DSTORAGE_ZLIB_BAD_DATA 0x89240032
    Adler-32 mismatch E_DSTORAGE_ZLIB_PARITY_FAIL 0x89240033
    inflates to more or fewer bytes than DestinationSize E_DSTORAGE_DECOMPRESSION_ERROR 0x89240030

    0x89240030 is on Microsoft's public DirectStorage constants page ("a generic error ... during decompression"); no code names a size mismatch, so this generic one is used. The three zlib codes have no public description. Their names and values come from the windows-bindgen-generated bindings of the direct-storage crate 0.5.1, which agree with its 0.2.0 errors.rs. That they map to header / data / Adler-32 is my reading of their names (see "Not verified").

  • RFC 1950 strictness. The header and the Adler-32 are checked. Bytes after the Adler-32 are not read, so a SourceSize rounded up over padding works. The game's own data supports a strict zlib reading: the texture pools' codec-8 chunks, laid out for this decompression (docs/textures.md), inflate with Python's zlib.decompress in tools/ttpf_mips.py, which checks both the header and the Adler-32.

  • GetStats: TotalDecompressionRequestCount, PendingDecompressionRequestCount and DecompressionErrors are now kept.

  • zlib_inflate gets an optional ZlibError* why (header, data, checksum, limit). Its existing callers are unchanged, and shader_library_test checks the new reasons.

  • Out of scope: BCPack and swizzle (with or without zlib) and physical pages still return E_NOTIMPL, as in runtime: console DirectStorage (DStorageGetFactory) with file-to-memory reads #4348. Nothing is accepted silently.

  • Docs: docs/directstorage.md (request checks, a zlib section with the error table, tests, sources), docs/README.md, runtime/README.md and the comment at the top of dstorage.cpp no longer say that zlib is E_NOTIMPL.

The inflater decodes one bit at a time: correct, not fast. A table-driven decoder can replace it behind zlib_inflate later without touching the request path (noted in the doc).

How tested

Linux, Wine 11.21, clang-cl/lld-link 23.1.4 cross build against the MSVC CRT and Windows SDK from xwin, RelWithDebInfo. No GPU and no game data needed.

dstorage_test gets a new test_zlib. It uses synthetic streams only:

  • a dynamic-Huffman stream, Python zlib.compress(text, 9);
  • a fixed-Huffman stream, zlib.compress(text, 1);
  • stored-block streams built by the test (for one block, byte for byte what zlib.compress(data, 0) gives), one of them a stored block followed by the dynamic stream's blocks.

The test rebuilds the plain text itself, and checks the block types (BTYPE 2, 1 and 0).

It covers:

  • zlib file requests and memory requests, including in place;
  • padding after the trailer;
  • a bad method, a preset dictionary, a reserved block type, a truncated stream, an Adler-32 mismatch;
  • a stream shorter and one longer than DestinationSize;
  • a stored-only stream larger than its result;
  • an unaligned offset, an IntermediateSize, a source past the end of the file;
  • zlib + BCPack and zlib + swizzle (E_NOTIMPL), plus swizzle alone;
  • an unreadable source and an unwritable destination;
  • that failed requests leave the destination (and the bytes past it) untouched;
  • the decompression statistics.

The old case "zlib is not implemented yet" is now "zlib with DestinationSize equal to SourceSize" (E_DSTORAGE_INVALID_DESTINATION_SIZE).

Build dstorage_test under Wine
#4348 as it is 272 checks, 2 failures
#4348's shim with this PR's test 329 checks, 31 failures (the 29 new zlib checks, e.g. a zlib file request: dynamic Huffman (got 0x80004001, want 0x00000000), plus the 2 below)
this PR 329 checks, 2 failures

The 2 failures in every row are the same and come from #4348's test_stats_timing: the copy reached the guard page and a running request is pending. Under Wine the PAGE_GUARD vectored handler that holds the copy never fires. They are unrelated to this change, and I expect them to pass on Windows, but I have not run it there.

  • shader_library_test passes under Wine (inflate stored/fixed/dynamic, plus the new failure reasons).
  • ctest -R "^(dstorage|shader_library)$" gives the same results.
  • gta6rt builds, with no warnings from dstorage.cpp.
  • On real data, the same approach (an inflate in this request path) made the title's zlib DirectStorage reads succeed with non-zero data on my own copy. That was a local build with other boot changes; nothing from the game is in this PR.

Not verified

  • The meaning of E_DSTORAGE_ZLIB_BAD_HEADER / _BAD_DATA / _PARITY_FAIL. The values come from the crate's header-generated bindings; Microsoft's public pages do not describe them. Mapping "parity" to the Adler-32 check is an inference.
  • Whether the console accepts bytes after the Adler-32 (here: yes, ignored), and whether it refuses a stored-only stream that is larger than its result. Here that stream is refused, following the documented DestinationSize rule.
  • No run on Windows.

AI-assisted.

🤖 Generated with Claude Code

mariuslozo-boop and others added 8 commits November 23, 2026 01:48
…ry reads

Serve the Xbox DirectStorage API (IDStorageFactoryX2, IDStorageQueueX2,
IDStorageFileX, IDStorageStatusArrayX) under dstorage_xs.dll, xcrdapi.dll and
dstorage.dll. Declarations are our own (shims/dstorage_xs_abi.h), written from
the public GDK reference; layouts are pinned with static_asserts.

- OpenFile goes through the registry's kernel32!CreateFileW, so GAME: paths use
  the console path mapping and the write overlay. GetHandle returns a ReOpenFile
  handle with its own file position.
- One worker thread per queue runs submitted entries in order: file and memory
  requests, status arrays, ID3D12Fence::Signal, EnqueueSetEvent (on a duplicate
  of the title's handle taken at enqueue).
- Auto-submit at half the capacity (or disabled through DSTORAGE_QUEUE_OPTIONS),
  enqueue into a full queue waits, CancelRequestsWithTag (a cancelled request is
  a success and not a failure), error record and error event, Close and the last
  Release drain the queue, IDStorageFileX::Close waits for the file's submitted
  reads.
- Request checks: reserved bits, source type, BCPack/swizzle ranges, sizes,
  1 GiB limit, null and foreign objects and closed files (also for zero-size
  requests), end of file, inaccessible memory.
- zlib, BCPack, swizzle and physical pages return E_NOTIMPL (later PRs).
- Interface ids of the X interfaces are not public: a creating call accepts and
  logs a new id per object kind, but refuses system COM ids (IMarshal,
  IAgileObject, ...) and the PC DirectStorage ids; QueryInterface accepts only
  IUnknown, the PC IDStorageStatusArray id (same shape) and the ids creating
  calls accepted for that kind. Bad arguments are logged once per method.
- registry.cpp classifies xcrdapi.dll!DStorageGetFactory as HRESULT; the cl.exe
  fallback in build_runtime.ps1 builds the new shim too.

ctest dstorage: 258 checks over synthetic files in %TEMP%, no GPU, no game data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/directstorage.md records which DLL and ABI the console title is expected
to use (dstorage_xs.dll medium, xcrdapi.dll and dstorage.dll low confidence),
what is not public (vtable order, interface ids, some constants) and the choice
made for each, what owning the modules shadows at run time, what the shim does
(including the order of its request checks), and what later PRs add (zlib,
BCPack, swizzle, physical pages, GPU-backed destinations, parallel reads).
Index row in docs/README.md; the owned-module list in runtime_imports.md and
the shim list in runtime/README.md name the new modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mit, /EHa under clang-cl

Review fixes for ItsNotRexed#4348 (Flyander, AirKyzzZ):
- DSTORAGE_REQUEST allows a buffer over 32 MiB only with 64 KiB or 2 MiB
  pages, which are not implemented yet, so such a request now fails with
  E_DSTORAGE_REQUEST_TOO_LARGE before any memory is touched.
- TotalRequestCount and the byte totals grow when a request is submitted,
  not when it is enqueued, and PendingRequestCount counts the request the
  worker is processing (DSTORAGE_PRIORITY_STATS).
- dstorage.cpp is built with /EHa under clang-cl, so guarded_move's __try
  catches an access violation in a title's buffer there too.

Tests: an over-limit and an at-limit 32 MiB copy, and stats checked
before Submit and while a copy is held on a guard page. The three new
failing checks fail with the previous shim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A request with ZlibDecompress alone now reads SourceSize bytes of the file or of memory into a staging buffer,
inflates them with the runtime's RFC 1950 inflater (leonida::zlib_inflate in src/shader_library.h) and copies the
result to Destination only when it is exactly DestinationSize bytes. A failed stream leaves the destination
untouched, and a memory source inside its own destination inflates correctly.

Checks follow DSTORAGE_REQUEST and dstorageerr.h: DestinationSize must be larger than SourceSize with zlib
(E_DSTORAGE_INVALID_DESTINATION_SIZE), a zlib FileOffset must be 16-byte aligned (E_DSTORAGE_INVALID_FILE_OFFSET),
IntermediateSize stays 0 without BCPack. Stream failures map to E_DSTORAGE_ZLIB_BAD_HEADER, _ZLIB_BAD_DATA,
_ZLIB_PARITY_FAIL (Adler-32) and E_DSTORAGE_DECOMPRESSION_ERROR (a result of another size). GetStats counts
the inflates, the pending ones and the decompression errors.

zlib_inflate gains an optional failure reason (header, data, checksum, limit); its existing callers are unchanged.
BCPack, swizzle and physical-page requests still fail with E_NOTIMPL.

dstorage_test: synthetic streams only (Python zlib levels 9 and 1, stored blocks built by the test), from a file
and from memory, with every failure code; shader_library_test checks the new failure reasons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 00551ba on Windows 11, CPU only: pass.

I built the dstorage_test and shader_library_test targets with MSVC in RelWithDebInfo. Both tests pass with ctest -C RelWithDebInfo -R "^(dstorage|shader_library)$" --output-on-failure. DirectStorage passes all 329 checks. This includes the two guard-page timing checks that failed under Wine.

For the control, I built the same DirectStorage test with the implementation from parent 45c39cf. I used the four new error constants from the PR so the test could compile. The control fails 29 of 329 checks. For example, the dynamic Huffman file request returns 0x80004001. The test expects 0x00000000.

AI-assisted.

@simonbronk

Copy link
Copy Markdown
Contributor

Tested on Windows 11, RTX 5070, Vulkan, own 1.0.2.1 copy, merged onto Pipodi's boot-integration stack (one doc conflict in docs/README.md; the code merged cleanly). The title's zlib requests are inflated. The slot 67 buffers that were empty before now hold real FSPX programs (42/42 with the kind-6 prologue). Without this PR the run reached a fatal-error write after about 30 s; with it, boot moves on past slot 67 in about 6 s. AI-assisted (Claude Code).

@Pipodi

Pipodi commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested the same idea on the retail boot: pass. Own 1.0.2.1 copy, Windows 11, RTX 4090 / 617.42, LEONIDA_D3D12=vulkan, on boot-integration plus #7115, #6891 and #7201. I ran a minimal local zlib stage in #4348's execute_request (read the source, inflate it with zlib_inflate, then copy it); I didn't run your branch itself.

  • Every zlib request the title sends during boot carries the RFC 1950 header (78 da), with 64 KiB destinations and IntermediateSize 0. Each one inflates to exactly DestinationSize. So the header-required zlib_inflate is enough for this title.
  • With the stage in place, the title reaches 42 slot 67 calls (28 without it). fspx_unpack.py --disasm and rdna2spirv.py --no-asm translate 38/42, all 38 spirv-val-valid. The other 4 hit the CS thread-group-size SGPR rule. These numbers are the same as @simonbronk's on an RTX 5070.
  • Slot 108 then goes through d3d12_xs: route device slots 86, 101, and 108 to PC Device2/Device5/Device8 #7201. The run reaches 1,675 D3D12 calls, then slot 57 (KkelebekK's claim 1), then a null global the title fills later in init.

AI-assisted (Claude Code).

@umnitus

umnitus commented Nov 23, 2026

Copy link
Copy Markdown

Independent native Windows clang-cl/xwin check at 00551ba. The dstorage and shader_library CTests both passed, with 329 checks and 0 failures in dstorage, including the CPU zlib request path. Synthetic files and temporary output were on E:. AI-assisted testing.

@jlric

jlric commented Nov 23, 2026

Copy link
Copy Markdown

AI-assisted A/B on Wine 11.22, RTX 4070 (NVIDIA 620.73.04), with 00551ba on a local boot stack. The zlib path was reached and the baseline DirectStorage E_NOTIMPL disappeared from the boot log. shader_library_test passed. dstorage_test passed 327/329, with the two previously reported guard-page timing failures under Wine.

@danekbiglike

Copy link
Copy Markdown

Measured on Windows 11, RTX 3090 and the pinned ladder-frame integration. With the same 40 main-archive programs, 10 embedded programs and 52 BVH programs, the earlier runtime refused compressed DirectStorage requests with E_NOTIMPL. With the inflater from this PR, the first 4,096 completed requests delivered 256 MiB in total, with zero failures at that observation. At completion 8,192 the counter showed 525,316,348 bytes and zero failures. The title continued to request additional render resources. The local DirectStorage fixture passed 329 checks and the shader-library inflater fixture passed.

The source patch, counter definition and build base are in the RTX 3090 handoff. AI-assisted measurement with OpenAI Codex.

@sennecools

Copy link
Copy Markdown
Contributor

Tested 00551ba00 on Windows 11, RX 9070 XT (driver 32.0.32029.2010), own 1.0.2.1 copy: pass, it clears the stop. Native Windows with the AMD driver, plus one measurement of how the failure reaches the title.

Local integration tree: the shared rt5-integration branch plus #7516, #7330, #7323, #7201, #6891, #7403, #7286, #7949, #7880, #7514, #7555, #7679, #7290 and #7755, fully translated shader corpus. Only docs/README.md conflicted.

without this PR (2 runs) with it (2 runs)
DirectStorage entries failed yes, zlib E_NOTIMPL 0
End of run title assert at 54,568 D3D12 calls, same place both times no assert; 81,468 and 96,281 calls, then a different stop

What the title does with a refused request, measured with #7555's probe: its read still reports the full size, but the buffer holds the first bytes of the file it read before, so its chunk parser fails on a missing chunk and the creator returns null. That is the assert. All 400 logged requests before it were zlib into 64 KiB destinations.

The stop after it here is AcpHalAllocateShapeContexts leaving its buffer null (#6885's area). Not run: dstorage_test and shader_library_test in this tree. AI-assisted.

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

checked: #7180 | with: clang-cl cross-build (xwin SDK, ninja, ccache) of runtime target dstorage_test, run under Wine | on: Ubuntu, Wine 9.0 (x86_64 prefix), main@867d922f0 + #7180 (merge needed, see below) | result: dstorage_test builds and runs, 329 checks, 327 pass, 2 FAIL ("the copy reached the guard page" and "a running request is pending", both in test_stats_timing); all zlib checks pass; negative control: with runtime/shims/dstorage.cpp put back to the parent commit ac1843d (no zlib path) the same test shows 31 failures, 29 of them zlib checks (E_NOTIMPL 0x80004001 instead of the expected inflate result or E_DSTORAGE_ZLIB_BAD_HEADER/BAD_DATA/PARITY_FAIL/DECOMPRESSION_ERROR/INVALID_FILE_OFFSET/INVALID_DESTINATION_SIZE) plus the same 2 guard-page failures; NOT tested: real Windows, a real GPU, any game data, BCPack/swizzle/pages (still E_NOTIMPL by design), device_proxy/other tests

Details:

  • Merge: runtime: DirectStorage zlib requests (file and memory sources) #7180 does not merge cleanly into current main. 3 conflicts, all additive: docs/README.md (table rows), docs/runtime_imports.md (owns_dll list: main added xg_xs.dll, the PR adds the three DirectStorage modules) and runtime/CMakeLists.txt (main added api_ref_state/context/wait_registration add_test blocks, the PR adds dstorage_test). I resolved them only in my test worktree by keeping both sides; the PR needs the same merge. runtime/src/shader_library.h, shims/registry.cpp, shader_library_test.cpp and the rest merge automatically. I did not fold it on top of runtime: DirectStorage requests with DestinationIsPhysicalPages (written through the xmem shim's pages) #7652/runtime: console DirectStorage (DStorageGetFactory) with file-to-memory reads #4348.
  • The two failing checks are not in the zlib code: they fail identically with the parent commit's dstorage.cpp, so they come from the earlier "count stats at submit" commit. The test makes the queue worker copy from a PAGE_GUARD page and waits in a vectored handler for STATUS_GUARD_PAGE_VIOLATION (runtime/test/dstorage_test.cpp, hold_on_guard / test_stats_timing, lines ~890-963). Under Wine 9 the handler never saw the fault within 10 s, three runs in a row (deterministic here). That may be Wine's guard-page handling and not the shim; I could not check it on Windows. If it passes on Windows, a comment saying the check is Wine-sensitive would help; otherwise the stats-at-submit claim is unproven.
  • Diff vs docs: docs/runtime_imports.md now lists dstorage_xs.dll, xcrdapi.dll, dstorage.dll as owned and registry.cpp line 117 and the table agree. zlib_inflate's new optional ZlibError* leaves existing callers unchanged (shader_library_test is the only other consumer I looked at; I did not run it).

agent: yes

@throny throny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 00551ba merged onto main 867d922 on Windows 11, RTX 4080 610.88: pass; whole diff read, no defect found in the shim, the inflater change or the tests.

  • Build: MSVC 14.44, Ninja, RelWithDebInfo; dstorage.cpp and dstorage_test.cpp compile with 0 warnings under /W4. dstorage_test: 329 checks, 0 failures, so the two test_stats_timing checks that fail under Wine pass on Windows. shader_library_test passes, unimplemented_policy_test 98 checks, 0 failed; gta6rt links.
  • Read: all 13 files. Checked by hand: zlib_inflate returns the same true/false as before for every path (only the reason is new), the request check order against docs/directstorage.md, the file/status-array lifetime under live_mutex, Close with entries in flight, and that a failed inflate cannot write the destination. The embedded zlib streams are the test's own text (it rebuilds and compares them).
  • Needs a rebase before it can merge: #4348 is closed and #8461 took it over. The merge into main conflicts in docs/README.md, docs/runtime_imports.md and runtime/CMakeLists.txt (three unions; the add_test(NAME dstorage ...) block has to go after the endif() of the XTaskQueue block). Depends on: should name #8461.
  • With #7556 (first-touch commit of XMem pages): a plain file request calls ReadFile straight into Destination (read_file_range), and the kernel does not fault an uncommitted page in, so it would fail with E_DSTORAGE_ACCESS_VIOLATION there. The zlib path is fine (guarded_move runs in user mode). Whichever lands second needs rt::xmem::commit_physical_range(r.Destination, r.SourceSize) before the read.
  • Three small notes inline (two with suggestions). My earlier comment review has the retail boot result for this head.

AI-assisted (Claude Code). No game data.

Comment thread docs/directstorage.md
| [DirectStorage overview (GDK)](https://learn.microsoft.com/en-us/gaming/gdk/docs/features/console/storage/directstorage/directstorage-overview) | Queue, auto-submit, cancellation, notification order, file sharing, decode order | Same terms; paraphrased |
| `dstorage.h`, `dstorageerr.h` in the public `Microsoft.Direct3D.DirectStorage` 1.3.0 NuGet package | PC method order, interface ids (to refuse them), capacity and name-length constants, `E_DSTORAGE_*` values | Read for facts only; not vendored. `runtime/shims/dstorage_xs_abi.h` is Leonida's own declaration |
| [DirectStorage constants](https://learn.microsoft.com/en-us/windows/win32/dstorage/dstorage-constants) (Win32 reference) | `E_DSTORAGE_DECOMPRESSION_ERROR` = `0x89240030`; `E_DSTORAGE_INVALID_DESTINATION_SIZE`: equal to the length without decompression, larger with it; `E_DSTORAGE_INVALID_FILE_OFFSET` is the offset invalid for a decompression mode | [Microsoft Learn Terms of Use](https://learn.microsoft.com/en-us/legal/termsofuse); paraphrased |
| [`direct-storage` crate](https://docs.rs/direct-storage/0.5.1/src/direct_storage/bindings.rs.html) 0.5.1 (bindings generated by `windows-bindgen` from the DirectStorage headers; 0.2.0's `errors.rs` has the same values) | `E_DSTORAGE_ZLIB_BAD_HEADER` `0x89240031`, `E_DSTORAGE_ZLIB_BAD_DATA` `0x89240032`, `E_DSTORAGE_ZLIB_PARITY_FAIL` `0x89240033` (names and values only) | MIT/Apache-2.0; two constants' names and values, no code copied |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three codes are listed in this row.

Suggested change
| [`direct-storage` crate](https://docs.rs/direct-storage/0.5.1/src/direct_storage/bindings.rs.html) 0.5.1 (bindings generated by `windows-bindgen` from the DirectStorage headers; 0.2.0's `errors.rs` has the same values) | `E_DSTORAGE_ZLIB_BAD_HEADER` `0x89240031`, `E_DSTORAGE_ZLIB_BAD_DATA` `0x89240032`, `E_DSTORAGE_ZLIB_PARITY_FAIL` `0x89240033` (names and values only) | MIT/Apache-2.0; two constants' names and values, no code copied |
| [`direct-storage` crate](https://docs.rs/direct-storage/0.5.1/src/direct_storage/bindings.rs.html) 0.5.1 (bindings generated by `windows-bindgen` from the DirectStorage headers; 0.2.0's `errors.rs` has the same values) | `E_DSTORAGE_ZLIB_BAD_HEADER` `0x89240031`, `E_DSTORAGE_ZLIB_BAD_DATA` `0x89240032`, `E_DSTORAGE_ZLIB_PARITY_FAIL` `0x89240033` (names and values only) | MIT/Apache-2.0; three constants' names and values, no code copied |

Comment thread docs/directstorage.md
## Which DLL and which ABI: the evidence

The game executable is not in the package available here, so its import table is unmeasured
([runtime_imports.md](runtime_imports.md)). What is known:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale against current main: docs/runtime_imports.md there lists the executable's imports, with XCRDAPI.dll importing exactly one function, DStorageGetFactory. So this paragraph and the xcrdapi.dll row below ("low", "only a name in a library list") can say it is measured, and dstorage_xs.dll / dstorage.dll become the unmeasured aliases. Fine to do in the rebase.

}
std::memset(hold.page, 0x5A, 4096);
DWORD old = 0;
VirtualProtect(hold.page, 4096, PAGE_READWRITE | PAGE_GUARD, &old);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the guard is not set, the test waits 10 s and then reports "the copy reached the guard page" and "a running request is pending", the two checks that fail under Wine. Checking the call would tell the two cases apart.

Suggested change
VirtualProtect(hold.page, 4096, PAGE_READWRITE | PAGE_GUARD, &old);
check(VirtualProtect(hold.page, 4096, PAGE_READWRITE | PAGE_GUARD, &old) != FALSE, "the guard page is armed");

@rideinstead

Copy link
Copy Markdown

✅ Automated security review: CLEAN (commit 00551ba0)

  • No findings.

Scope: static analysis of added diff lines only; no code was executed. Please review findings manually.

dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…eGetFactory) with file-to-memory reads

Squashed from ItsNotRexed#4348 by mariuslozo-boop, head 45c39cf (security scan PASS).

Conflicts: runtime/CMakeLists.txt kept main's XTaskQueue/wait_registration test block with its closing endif() (a plain union drops it) and put the dstorage test in its own if(). docs/README.md keeps main's rows and adds the directstorage.md row (main's gameinput.md row kept). registry.cpp's comment merges main's note that the retail executable imports xcrdapi.dll!DStorageGetFactory (ItsNotRexed#6715) with the PR's note that dstorage.cpp serves it; the HRESULT classification was already on the branch (ItsNotRexed#7175). ItsNotRexed#7180 (zlib) not pulled.
dadnotdaddy pushed a commit to dadnotdaddy/Leonida that referenced this pull request Nov 23, 2026
…ile and memory sources)

Squashed from ItsNotRexed#7180 by its author, head 00551ba (the one
commit on top of ItsNotRexed#4348's 45c39cf, already in main). Security scan PASS:
prescan flags were doc links, a doc mention of zlib.decompress and a test's
guard page; three Haiku reads of the whole 3,334-line diff, PASS each. The
inflater is the in-tree leonida::zlib_inflate; nothing is downloaded.

Conflict resolution: shader_library.h keeps main's constexpr fixed Huffman
tables (no function-local static) with ItsNotRexed#7180's ZlibError return; docs/README.md
keeps main's index.

@LaugeM LaugeM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should merge second, right after #8461. The retail runs in the comments show it clears a real stop on integration stacks that are past device slot 26: the zlib reads of shaders.rpf and the slot 67 programs. On main plus #8461 alone the run stops at slot 26 (#6964) before any zlib request, so this is the next DirectStorage step, not the next stop on main.

The shim base is #4348's 45c39cf6f, not #8461's head. #8461 has one more commit, 077606cb4 (Close and CurrentOpenFileCount). A merge of this head onto #8461 (git merge-tree) conflicts only in docs/README.md (the directstorage.md row); dstorage.cpp and dstorage_test.cpp auto-merge. Onto main it also conflicts in docs/runtime_imports.md and runtime/CMakeLists.txt, so it should rebase onto #8461 and say Depends on: #8461.

Checked against the GDK DSTORAGE_REQUEST page

  • With decompression DestinationSize must be larger than SourceSize, and equal without it. dstorage.cpp:791-792 enforces that.
  • FileOffset must be 16-byte aligned if ZlibDecompress or BcpackMode is set. dstorage.cpp:801 checks it for zlib. BCPack is refused earlier (line 777), so nothing is missed today, but the check has to cover BcpackMode != 0 when BCPack lands.

Findings

  1. Confirmed (code): dstorage.cpp:799 keeps #8461's 32 MiB cap on address destinations, so a zlib read that inflates to more than 32 MiB is refused with E_DSTORAGE_REQUEST_TOO_LARGE, where the console allows it on 64 KiB or 2 MiB pages. The same note is on #8461. Whichever PR drops the cap, the zlib staging buffers stay bounded by the 1 GiB check at line 795.
  2. Order: this changes leonida::zlib_inflate's failure returns (runtime/src/shader_library.h). #8682, #8942 and #9566 edit the same inflater, and #8942 already depends on this PR. Land this one first, then those, since every zlib request now runs through it on the queue worker.

Tested

MSVC (VS 2026, x64, /W4 /permissive-), Windows 11, CPU only: dstorage_test from this head, 329 checks, 0 failures.

AI-assisted review (Opus 5.5). Read the diff against main; built dstorage_test with MSVC and ran it (329 checks, 0 failures), and merged this head onto #8461 with git merge-tree.

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.