startup-runtime: soft assert in a chunked streaming read #1129

Closed
opened 2026-11-24 01:45:23 +00:00 by denn0x · 3 comments
denn0x commented 2026-11-24 01:45:23 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-24T01:45:23Z.


Problem

Under tools/startup/run.ps1, one soft assert (an int3 followed by a continuation) fires about ten times a
second once texture streaming starts. The surrounding code reads a compressed stream in chunks, and the assert
checks that a read length ends on a chunk boundary; the requested length ends inside a compressed chunk.

What is known

  • Size and offset come from the title's own data, not from the XG layout stand-in (changing XG answers does not move it).
  • It is not caused by DirectStorage page-array entries that are 0 at enqueue (they are re-read at run time now).
  • With -ContinueAsserts N the run continues; without a debugger the title steps over it.

Done when

The cause is identified (which stand-in answer produces the length) and the assert no longer fires, or the
finding is documented with a reproduction that does not need game files.

Context: docs/startup-runtime.md (#1135). Do not attach game files, dumps or logs that contain addresses or names from an executable; describe results in terms of the component tested.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6572711543 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1136). Originally posted by [denn0x](https://github.com/denn0x) on 2026-11-24T01:45:23Z. --- ## Problem Under `tools/startup/run.ps1`, one soft assert (an `int3` followed by a continuation) fires about ten times a second once texture streaming starts. The surrounding code reads a compressed stream in chunks, and the assert checks that a read length ends on a chunk boundary; the requested length ends inside a compressed chunk. ## What is known - Size and offset come from the title's own data, not from the XG layout stand-in (changing XG answers does not move it). - It is not caused by DirectStorage page-array entries that are 0 at enqueue (they are re-read at run time now). - With `-ContinueAsserts N` the run continues; without a debugger the title steps over it. ## Done when The cause is identified (which stand-in answer produces the length) and the assert no longer fires, or the finding is documented with a reproduction that does not need game files. Context: `docs/startup-runtime.md` (#1135). Do not attach game files, dumps or logs that contain addresses or names from an executable; describe results in terms of the component tested. AI-assisted (Claude Code).
Holastor commented 2026-11-24 06:50:14 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-24T03:23:59Z.


Claiming #1136.

I am taking this issue to investigate and resolve the chunked streaming read failure and assert in the DirectStorage stand-in (src/startup_stubs/xcrdapi.cpp).

  • Scope:
    • Support piece-wise streaming decompression for DirectStorage zlib requests where an n \times D stream is enqueued as n destination-sized requests (piece 0 with zlib header at base offset B, pieces k \ge 1 offset by B + k \times D without headers).
    • Cache inflated stream chunks by (source, base_offset, compressed_size) and serve subsequent slices [k \times D, (k+1) \times D) with bounded LRU eviction.
    • Implement a clean-room, source-built regression fixture generating an n \times D RFC 1950 stream enqueued as n slice requests, verifying exact byte readback across all pieces.
    • Add negative controls: unaligned offset, mismatched compressed size, and short destination, verifying clean E_FAIL and that destination memory remains untouched.
  • Environment: Windows 11 Pro x64 (build 26100), MSVC 19.51, CMake / Ninja, dev and release presets.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7121051643 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1136#issuecomment-7121051643). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-24T03:23:59Z. --- Claiming #1136. I am taking this issue to investigate and resolve the chunked streaming read failure and assert in the DirectStorage stand-in (`src/startup_stubs/xcrdapi.cpp`). - **Scope:** - Support piece-wise streaming decompression for DirectStorage zlib requests where an $n \times D$ stream is enqueued as $n$ destination-sized requests (piece 0 with zlib header at base offset $B$, pieces $k \ge 1$ offset by $B + k \times D$ without headers). - Cache inflated stream chunks by `(source, base_offset, compressed_size)` and serve subsequent slices $[k \times D, (k+1) \times D)$ with bounded LRU eviction. - Implement a clean-room, source-built regression fixture generating an $n \times D$ RFC 1950 stream enqueued as $n$ slice requests, verifying exact byte readback across all pieces. - Add negative controls: unaligned offset, mismatched compressed size, and short destination, verifying clean `E_FAIL` and that destination memory remains untouched. - **Environment:** Windows 11 Pro x64 (build 26100), MSVC 19.51, CMake / Ninja, `dev` and `release` presets.
Holastor commented 2026-11-24 06:50:14 +00:00 (Migrated from github.com)
Author
Contributor

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


Cause Analysis & Source-Built Solution

1. Root Cause

In DirectStorage streams under the startup stand-in, texture streaming assets are enqueued as n piece requests targeting 64 KiB destination pages/buffers:

  • Piece 0: base file offset B, compressed size S, destination size D, starting with a standard zlib header (78 da / 78 01).
  • Piece $k \ge 1$: file offset B + k \times D, compressed size S, destination size D. The offset in the request is stepped by the uncompressed destination slice size (k \times D), not the compressed stream offset, so ReadFile at B + k \times D reads mid-stream bytes that do not start with a zlib header.

Without chunk caching:

  1. Piece 0 fails if n \times D > D and bounds are strictly enforced.
  2. Pieces k \ge 1 fail immediately on header validation ("zlib failed: header ...").
  3. The title's streaming loop encounters an assert or attempts to read incomplete tables, triggering the soft assert / access violation.

2. Implementation (src/startup_stubs/xcrdapi.cpp)

  • StreamCache: thread-safe LRU cache storing up to 16 uncompressed streams indexed by (source, base_offset, compressed_size).
  • has_following_chunk(self, source, offset, source_size, destination_size): checks whether the queue's submitted or waiting deques contain an upcoming request matching (source, source_size) with offset B + D.
  • Piece 0 Handling:
    • Inflates the stream.
    • If produced > destination_size AND has_following_chunk(...) is true, the full decompressed buffer is stored in stream_cache, and slice 0 ([0, D)) is served to Piece 0's destination.
    • If no following chunk exists, it is treated as a standalone request with an exceeded destination capacity and rejected ("destination capacity exceeded"), leaving destination memory completely untouched.
  • Piece k \ge 1 Handling:
    • stream_cache.get_slice(source, offset, source_size, destination_size, output) matches the cached entry by (source, compressed_size) where offset >= base_offset.
    • Calculates delta = offset - base_offset.
    • If delta + destination_size <= cached_size, slices [k \times D, (k+1) \times D) are extracted directly from the cache and written to the destination ("inflated (slice from stream cache)").
  • Negative Controls:
    • Out-of-bounds offset (delta + D > \text{cached\_size}) or mismatched compressed size S misses cache and fails with E_FAIL (0x80004005), leaving destination memory untouched.

3. Source-Built Regression Fixtures (tests/startup_stub_consumer.cpp)

Extended startup_stub_consumer under scenario dstorage:

  1. 4-Chunk Streaming Read (256 KiB across 4 requests):
    • 256 KiB generated stream (4 \times 64\text{ KiB}), compressed with stored RFC 1950 zlib.
    • Enqueued as 4 separate requests with offsets 0, 64\text{ KiB}, 128\text{ KiB}, 192\text{ KiB}.
    • All 4 complete with S_OK and every 64 KiB destination buffer matches the exact plain data slice.
  2. Out-of-Bounds Chunk Negative Control:
    • Enqueues Piece 0, then an invalid offset request (500\text{ KiB} > 128\text{ KiB}) into a buffer prefilled with 0xEE.
    • The invalid chunk fails with E_FAIL (0x80004005), and its buffer remains 100% untouched (0xEE).
  3. Standalone Short Destination Negative Control:
    • Single 128 KiB stream with 64 KiB destination (without following chunks).
    • Fails with E_FAIL (0x80004005), and its buffer remains 100% untouched (0xAA).

4. Verification Results

Environment: Windows 11 Pro x64 (build 26100), MSVC 19.51, Ninja.

  • Release Preset:

    cmake --build --preset release --target startup_stub_consumer startup_stub_xcrdapi
    ctest --preset release -R '^startup_stubs$' -V
    
    chad: exit=0xe2570200 ok
    xg_xs: exit=0xe2570501 ok
    none control: exit=0 ok
    physical control: exit=0 ok
    dstorage control: exit=0 ok
    PASS
    1/1 Test #56: startup_stubs .................... Passed 0.21 sec
    
  • Dev Preset:

    cmake --build --preset dev --target startup_stub_consumer startup_stub_xcrdapi
    ctest --preset dev -R '^startup_stubs$' -V
    
    1/1 Test #56: startup_stubs .................... Passed 0.53 sec
    
  • Linters:
    tests/docs_links_check.py and tests/bidi_controls_check.py pass with 0 errors.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7121168176 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1136#issuecomment-7121168176). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-24T03:30:48Z. --- ### Cause Analysis & Source-Built Solution #### 1. Root Cause In DirectStorage streams under the startup stand-in, texture streaming assets are enqueued as $n$ piece requests targeting 64 KiB destination pages/buffers: - **Piece 0**: base file offset $B$, compressed size $S$, destination size $D$, starting with a standard zlib header (`78 da` / `78 01`). - **Piece $k \ge 1$**: file offset $B + k \times D$, compressed size $S$, destination size $D$. The offset in the request is stepped by the *uncompressed destination slice size* ($k \times D$), not the compressed stream offset, so `ReadFile` at $B + k \times D$ reads mid-stream bytes that do not start with a zlib header. Without chunk caching: 1. Piece 0 fails if $n \times D > D$ and bounds are strictly enforced. 2. Pieces $k \ge 1$ fail immediately on header validation (`"zlib failed: header ..."`). 3. The title's streaming loop encounters an assert or attempts to read incomplete tables, triggering the soft assert / access violation. #### 2. Implementation (`src/startup_stubs/xcrdapi.cpp`) - **`StreamCache`**: thread-safe LRU cache storing up to 16 uncompressed streams indexed by `(source, base_offset, compressed_size)`. - **`has_following_chunk(self, source, offset, source_size, destination_size)`**: checks whether the queue's `submitted` or `waiting` deques contain an upcoming request matching `(source, source_size)` with offset $B + D$. - **Piece 0 Handling**: - Inflates the stream. - If `produced > destination_size` AND `has_following_chunk(...)` is true, the full decompressed buffer is stored in `stream_cache`, and slice 0 ($[0, D)$) is served to Piece 0's destination. - If no following chunk exists, it is treated as a standalone request with an exceeded destination capacity and rejected (`"destination capacity exceeded"`), leaving destination memory completely untouched. - **Piece $k \ge 1$ Handling**: - `stream_cache.get_slice(source, offset, source_size, destination_size, output)` matches the cached entry by `(source, compressed_size)` where `offset >= base_offset`. - Calculates `delta = offset - base_offset`. - If `delta + destination_size <= cached_size`, slices $[k \times D, (k+1) \times D)$ are extracted directly from the cache and written to the destination (`"inflated (slice from stream cache)"`). - **Negative Controls**: - Out-of-bounds offset ($delta + D > \text{cached\_size}$) or mismatched compressed size $S$ misses cache and fails with `E_FAIL` (`0x80004005`), leaving destination memory untouched. #### 3. Source-Built Regression Fixtures (`tests/startup_stub_consumer.cpp`) Extended `startup_stub_consumer` under scenario `dstorage`: 1. **4-Chunk Streaming Read (256 KiB across 4 requests)**: - 256 KiB generated stream ($4 \times 64\text{ KiB}$), compressed with stored RFC 1950 zlib. - Enqueued as 4 separate requests with offsets $0, 64\text{ KiB}, 128\text{ KiB}, 192\text{ KiB}$. - All 4 complete with `S_OK` and every 64 KiB destination buffer matches the exact plain data slice. 2. **Out-of-Bounds Chunk Negative Control**: - Enqueues Piece 0, then an invalid offset request ($500\text{ KiB} > 128\text{ KiB}$) into a buffer prefilled with `0xEE`. - The invalid chunk fails with `E_FAIL` (`0x80004005`), and its buffer remains 100% untouched (`0xEE`). 3. **Standalone Short Destination Negative Control**: - Single 128 KiB stream with 64 KiB destination (without following chunks). - Fails with `E_FAIL` (`0x80004005`), and its buffer remains 100% untouched (`0xAA`). #### 4. Verification Results **Environment:** Windows 11 Pro x64 (build 26100), MSVC 19.51, Ninja. * **Release Preset:** ```powershell cmake --build --preset release --target startup_stub_consumer startup_stub_xcrdapi ctest --preset release -R '^startup_stubs$' -V ``` ```text chad: exit=0xe2570200 ok xg_xs: exit=0xe2570501 ok none control: exit=0 ok physical control: exit=0 ok dstorage control: exit=0 ok PASS 1/1 Test #56: startup_stubs .................... Passed 0.21 sec ``` * **Dev Preset:** ```powershell cmake --build --preset dev --target startup_stub_consumer startup_stub_xcrdapi ctest --preset dev -R '^startup_stubs$' -V ``` ```text 1/1 Test #56: startup_stubs .................... Passed 0.53 sec ``` * **Linters:** `tests/docs_links_check.py` and `tests/bidi_controls_check.py` pass with 0 errors.
Holastor commented 2026-11-24 06:50:15 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-24T03:37:49Z.


PR #1214 is now open for review on top of main (c02af631) following the merge of #1135: https://github.com/R0Y00/Xbox2Windows/pull/1214

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7121275163 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1136#issuecomment-7121275163). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-24T03:37:49Z. --- PR #1214 is now open for review on top of main (c02af631) following the merge of #1135: https://github.com/R0Y00/Xbox2Windows/pull/1214
Sign in to join this conversation.
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#1129
No description provided.