startup: support DirectStorage zlib requests exceeding destination size #1160

Closed
Holastor wants to merge 5 commits from upstream-pr/1167 into main
Holastor commented 2026-11-24 02:17:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-24T02:17:30Z.


Refs #1144.

Problem

In src/startup_stubs/xcrdapi.cpp, zlib decompression requests failed when an archive stream uncompressed to more than 64 KiB. The standalone decompressor x2w_inflate::inflate_zlib operates in a single pass into a fixed linear buffer. Because the runtime sized the output buffer strictly to destination_size read from request + 0x18 (which is 0x10000 = 64 KiB for a single tile/page target), any stream exceeding 64 KiB ran out of buffer space, returning false and causing outcome = "zlib failed" with E_FAIL.

Furthermore, for requests targeting physical page tokens (options & (1ull << 21)), queue_enqueue_request computed page token count using destination_size (64 KiB), capturing only 1 token and dropping subsequent page tokens for multi-page requests.

Change

  1. In src/startup_stubs/xcrdapi.cpp:
    • Read uncompressed_size from request + 0x38 (matching Microsoft DirectStorage specification, where UncompressedSize can exceed Destination.Size for non-memory destinations).
    • In queue_enqueue_request: compute size = (uncompressed_size > destination_size) ? uncompressed_size : destination_size so all physical page tokens are retained in entry.tokens.
    • In execute_request: size decompression buffer to std::max(destination_size, uncompressed_size). If decompression requires more space (e.g. if uncompressed_size is unset), expand dynamically up to 512 KiB.
    • Bound destination writes to destination_size, ensuring buffer safety.
  2. In tests/dstorage_rfc1950_check.cpp:
    • Added test scenarios large_128k (128 KiB) and large_256k (256 KiB) testing full RFC 1950 stream decompression through the DirectStorage file queue with exact byte verification.
    • Added negative control overflow_64k: a 128 KiB stream submitted to a 64 KiB destination buffer. Verifies that decompression rejects the oversized payload with 0x8007000d (ERROR_INVALID_DATA), preserves all sentinel bytes without buffer overrun, and reports completion status 0x8007000d.

Verification

  • Windows 11 Pro x64 (build 26100), MSVC 19.51 (VS 2026 Developer PowerShell), DirectStorage SDK 1.3.0, zlib 1.3.1.
  • release preset:
    ctest --preset release --no-tests=error -R '^dstorage_rfc1950_' -V (9/9 pass, 0.17s)
    ctest --preset release --no-tests=error -R '^startup_stubs$' -V (pass, 0.09s)
  • dev preset:
    ctest --preset dev --no-tests=error -R '^dstorage_rfc1950_' -V (9/9 pass, 0.18s)
    ctest --preset dev --no-tests=error -R '^startup_stubs$' -V (pass, 0.10s)
  • Linters:
    tests/docs_links_check.py (16 files, 254 links) and tests/bidi_controls_check.py (973 files) pass with 0 errors.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6573106794 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1167). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-24T02:17:30Z. --- Refs #1144. ## Problem In `src/startup_stubs/xcrdapi.cpp`, zlib decompression requests failed when an archive stream uncompressed to more than 64 KiB. The standalone decompressor `x2w_inflate::inflate_zlib` operates in a single pass into a fixed linear buffer. Because the runtime sized the output buffer strictly to `destination_size` read from `request + 0x18` (which is `0x10000` = 64 KiB for a single tile/page target), any stream exceeding 64 KiB ran out of buffer space, returning `false` and causing `outcome = "zlib failed"` with `E_FAIL`. Furthermore, for requests targeting physical page tokens (`options & (1ull << 21)`), `queue_enqueue_request` computed page token count using `destination_size` (64 KiB), capturing only 1 token and dropping subsequent page tokens for multi-page requests. ## Change 1. In `src/startup_stubs/xcrdapi.cpp`: - Read `uncompressed_size` from `request + 0x38` (matching Microsoft DirectStorage specification, where `UncompressedSize` can exceed `Destination.Size` for non-memory destinations). - In `queue_enqueue_request`: compute `size = (uncompressed_size > destination_size) ? uncompressed_size : destination_size` so all physical page tokens are retained in `entry.tokens`. - In `execute_request`: size decompression buffer to `std::max(destination_size, uncompressed_size)`. If decompression requires more space (e.g. if `uncompressed_size` is unset), expand dynamically up to 512 KiB. - Bound destination writes to `destination_size`, ensuring buffer safety. 2. In `tests/dstorage_rfc1950_check.cpp`: - Added test scenarios `large_128k` (128 KiB) and `large_256k` (256 KiB) testing full RFC 1950 stream decompression through the DirectStorage file queue with exact byte verification. - Added negative control `overflow_64k`: a 128 KiB stream submitted to a 64 KiB destination buffer. Verifies that decompression rejects the oversized payload with `0x8007000d` (`ERROR_INVALID_DATA`), preserves all sentinel bytes without buffer overrun, and reports completion status `0x8007000d`. ## Verification - Windows 11 Pro x64 (build 26100), MSVC 19.51 (VS 2026 Developer PowerShell), DirectStorage SDK 1.3.0, zlib 1.3.1. - `release` preset: `ctest --preset release --no-tests=error -R '^dstorage_rfc1950_' -V` (9/9 pass, 0.17s) `ctest --preset release --no-tests=error -R '^startup_stubs$' -V` (pass, 0.09s) - `dev` preset: `ctest --preset dev --no-tests=error -R '^dstorage_rfc1950_' -V` (9/9 pass, 0.18s) `ctest --preset dev --no-tests=error -R '^startup_stubs$' -V` (pass, 0.10s) - Linters: `tests/docs_links_check.py` (16 files, 254 links) and `tests/bidi_controls_check.py` (973 files) pass with 0 errors.
upstream-sync closed this pull request 2026-11-24 06:46:38 +00:00
tedin7 commented 2026-11-24 06:46:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T02:27:04Z.

Review: COMMENTED


Reviewed commit 1089f77cd49d45f458dc6de4801189e7a04b3de1 on Linux.

Code notes:

  • src/startup_stubs/xcrdapi.cpp: execute_request sizes the inflate buffer from max(uncompressed_size, destination_size) (with a 512 KiB fallback) and still clamps destination writes to destination_size. One minor question in queue_enqueue_request: pages for page_destination is also computed from max(uncompressed_size, destination_size); if a title's token array only covers destination_size, x2w_local::readable(title, pages * sizeof(ULONG_PTR)) could reject a request whose uncompressed_size crosses into an extra 64 KiB page.
  • tests/dstorage_rfc1950_check.cpp and CMakeLists.txt: cleanly add large_128k, large_256k, and overflow_64k scenarios.

Linux checks passed: python3 tests/docs_links_check.py, python3 tests/bidi_controls_check.py, python3 tests/gpu_report_format_check.py, python3 tests/xgameruntime_table_check.py, python3 tests/hardware_reports_check.py.
Windows MSVC build, DirectStorage SDK tests, and runtime behavior rely on CI (currently pending).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6032752880 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1167#pullrequestreview-6032752880). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T02:27:04Z. **Review:** COMMENTED --- Reviewed commit `1089f77cd49d45f458dc6de4801189e7a04b3de1` on Linux. Code notes: - `src/startup_stubs/xcrdapi.cpp`: `execute_request` sizes the inflate buffer from `max(uncompressed_size, destination_size)` (with a 512 KiB fallback) and still clamps destination writes to `destination_size`. One minor question in `queue_enqueue_request`: `pages` for `page_destination` is also computed from `max(uncompressed_size, destination_size)`; if a title's token array only covers `destination_size`, `x2w_local::readable(title, pages * sizeof(ULONG_PTR))` could reject a request whose `uncompressed_size` crosses into an extra 64 KiB page. - `tests/dstorage_rfc1950_check.cpp` and `CMakeLists.txt`: cleanly add `large_128k`, `large_256k`, and `overflow_64k` scenarios. Linux checks passed: `python3 tests/docs_links_check.py`, `python3 tests/bidi_controls_check.py`, `python3 tests/gpu_report_format_check.py`, `python3 tests/xgameruntime_table_check.py`, `python3 tests/hardware_reports_check.py`. Windows MSVC build, DirectStorage SDK tests, and runtime behavior rely on CI (currently pending).
Holastor commented 2026-11-24 06:50:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-24T02:26:52Z.


Closing for now: Issue #1144 fixes code in PR #1135 (src/startup_stubs/), which is still open and pending merge into main. Opening against main pulled in the 9,600+ lines of PR #1135. Our actual change is strictly 46 additions / 14 deletions across 4 files (detailed in #1144). I will reopen a clean PR once #1135 lands on main.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7120151534 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1167#issuecomment-7120151534). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-24T02:26:52Z. --- Closing for now: Issue #1144 fixes code in PR #1135 (src/startup_stubs/), which is still open and pending merge into main. Opening against main pulled in the 9,600+ lines of PR #1135. Our actual change is strictly 46 additions / 14 deletions across 4 files (detailed in #1144). I will reopen a clean PR once #1135 lands on main.
Holastor commented 2026-11-24 06:50:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-24T02:58:45Z.


Thank you @tedin7 for the review!

Good catch on queue_enqueue_request: we updated pages to be bounded strictly by destination_size ((page_offset + destination_size + 0xFFFF) / 0x10000), ensuring we never read past the caller's token array. Furthermore, per maintainer @R0Y00's guidance, destination_capacity and decode_length are now strictly tracked as separate values in execute_request, and multi-page grants as well as negative controls (verifying destination memory remains untouched on exceeded capacity or missing grant) have been integrated and verified in ests/startup_stub_consumer.cpp and ests/startup_stub_check.py.

Full details and test logs have been posted on Issue #1144 (https://github.com/R0Y00/Xbox2Windows/issues/1144#issuecomment-7120645046). We will reopen a clean, standalone PR once PR #1135 merges.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7120656871 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1167#issuecomment-7120656871). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-24T02:58:45Z. --- Thank you @tedin7 for the review! Good catch on queue_enqueue_request: we updated pages to be bounded strictly by destination_size ((page_offset + destination_size + 0xFFFF) / 0x10000), ensuring we never read past the caller's token array. Furthermore, per maintainer @R0Y00's guidance, destination_capacity and decode_length are now strictly tracked as separate values in execute_request, and multi-page grants as well as negative controls (verifying destination memory remains untouched on exceeded capacity or missing grant) have been integrated and verified in ests/startup_stub_consumer.cpp and ests/startup_stub_check.py. Full details and test logs have been posted on Issue #1144 (https://github.com/R0Y00/Xbox2Windows/issues/1144#issuecomment-7120645046). We will reopen a clean, standalone PR once PR #1135 merges.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1160
No description provided.