runtime: check the DirectStorage stand-in's zlib decoder against zlib #1397

Merged
RenzoWit35 merged 2 commits from upstream-pr/1404 into main 2026-11-24 11:17:14 +00:00
RenzoWit35 commented 2026-11-24 10:41:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T10:41:48Z.


Change

Adds a direct check for the DirectStorage stand-in's zlib decoder, src/startup_stubs/inflate.h, as one more startup-stand-in slice of #1149. The stand-in inflates zlib requests with this decoder. Before this change, the stub-consumer fixtures only reached stored blocks, a fixed block that repeats one byte at distance 1, and single-byte runs.

  • tests/startup_inflate_probe.cpp runs x2w_inflate::inflate_zlib on a zlib stream, or x2w_inflate::inflate_raw on bare deflate data (raw mode), into a destination followed by 16 guard bytes (0xA5). It reports inflated <size>, rejected (in raw mode with the number of bytes decoded before the rejection), or a write past the capacity.
  • tests/startup_inflate_check.py (ctest startup_inflate) has CPython's zlib encode seeded synthetic data, and each decoded result must equal the input:
    • stored blocks (70 000 random bytes, level 0);
    • a fixed Huffman block (Z_FIXED) and a dynamic block over random literals and copies of 3–258 bytes from 1–32 000 bytes back;
    • a dynamic block over a sparse alphabet, whose header needs the code-length repeat codes 16, 17 and 18;
    • a sync flush (an empty stored block) between two parts, where the second part copies from the first;
    • an empty stream and spare destination capacity.
  • Controls that must be rejected without writes past the destination: one byte too little capacity, and a stream cut in half (CPython's zlib rejects it too).
  • The distance bound, checked on bare deflate data so that neither the zlib header nor the Adler-32 check can reject the stream. After the literals ABCD, a length-3 match at distance 4 decodes to ABCDABC. The same block with distance 5 is rejected with only the four literals decoded, before the match copies anything. CPython's zlib also rejects it (wbits=-15).

The decoder is not changed. The test is registered with the other Python-driven startup tests. It is not added to the pr preset, so the full main and manual workflows run it with the rest of the suite. It runs in under a second without a GPU or game files, so it can move into the preset if you prefer. docs/startup-runtime.md has the command.

Refs #1149

Verification

On a953c41a (based on a8946ec0), Windows 11, MSVC 14.51, Python 3.14.5 (zlib-ng 1.3.1), dev preset:

ctest --preset dev --no-tests=error -R '^startup_inflate$' -V
stored                 block 0,  70016 ->  70000 bytes, decoded
fixed                  block 1,  19699 ->  98304 bytes, decoded
dynamic                block 2,  19335 ->  98304 bytes, decoded
dynamic_spare_capacity block 2,  19335 ->  98304 bytes, decoded
dynamic_sparse         block 2,  21307 ->  32768 bytes, decoded
sync_flush             block 0,  24821 ->  35092 bytes, decoded
empty                  block 1,      8 ->      0 bytes, decoded
dynamic_short_capacity  19335 bytes, capacity  98303, rejected
dynamic_truncated        9667 bytes, capacity  98304, rejected
raw_distance_4         block 1,      7 ->      7 bytes, decoded
raw_distance_5              7 bytes, capacity     64, rejected after 4 bytes
inflate.h matches zlib 1.3.1.zlib-ng in 8 cases and rejects 3 controls
1/1 Test #243: startup_inflate ..................   Passed    0.98 sec

Negative control: I broke inflate.h one way at a time, rebuilt the probe and reran the check. Each mutation fails it:

Mutation First failing case
last distance code has 12 extra bits instead of 13 fixed: exit 1: rejected
length code 284 has 4 extra bits instead of 5 fixed: exit 1: rejected
distance base 24577 off by one fixed: exit 1: rejected
distance bound off by one (distance > out_pos + 1) raw_distance_5: exit 0: inflated 7
distance bound too strict (distance >= out_pos) raw_distance_4: exit 1: rejected after 4 bytes
no distance bound raw_distance_5: exit 0: inflated 7
no capacity check for a match dynamic_short_capacity: exit 3: wrote past the destination at +0
repeat code 18 repeats 10 + n zeros dynamic_sparse: exit 1: rejected
repeat code 17 repeats 2 + n zeros dynamic_sparse: exit 1: rejected
repeat code 16 repeats 4 + n dynamic: exit 1: rejected
stored block does not realign to a byte stored: exit 1: rejected

On the first revision, with the last-distance-code and no-distance-bound mutations applied together, startup_stubs and startup_storage_directory_batch still passed and only startup_inflate failed. On a953c41a, with the header unchanged, all three pass. tests/docs_links_check.py, tests/bidi_controls_check.py and git diff --check pass.

Evidence and follow-up

All inputs are generated at test time from a fixed seed with CPython's zlib. The distance-bound blocks are hand-built from RFC 1951's fixed code table. No external data is involved. The check compares only decoded bytes and the first block's type, not compressed bytes, so it should not depend on the zlib build. I ran it locally only with zlib-ng 1.3.1.

Not covered here: header rejection (FDICT, method, FCHECK), reserved block type 3 and stored LEN/NLEN mismatches. The stub consumer already covers a damaged Adler-32.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6579966420 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1404). Originally posted by [RenzoWit35](https://github.com/RenzoWit35) on 2026-11-24T10:41:48Z. --- ## Change Adds a direct check for the DirectStorage stand-in's zlib decoder, `src/startup_stubs/inflate.h`, as one more startup-stand-in slice of #1149. The stand-in inflates zlib requests with this decoder. Before this change, the stub-consumer fixtures only reached stored blocks, a fixed block that repeats one byte at distance 1, and single-byte runs. - `tests/startup_inflate_probe.cpp` runs `x2w_inflate::inflate_zlib` on a zlib stream, or `x2w_inflate::inflate_raw` on bare deflate data (`raw` mode), into a destination followed by 16 guard bytes (0xA5). It reports `inflated <size>`, `rejected` (in raw mode with the number of bytes decoded before the rejection), or a write past the capacity. - `tests/startup_inflate_check.py` (ctest `startup_inflate`) has CPython's `zlib` encode seeded synthetic data, and each decoded result must equal the input: - stored blocks (70 000 random bytes, level 0); - a fixed Huffman block (`Z_FIXED`) and a dynamic block over random literals and copies of 3–258 bytes from 1–32 000 bytes back; - a dynamic block over a sparse alphabet, whose header needs the code-length repeat codes 16, 17 and 18; - a sync flush (an empty stored block) between two parts, where the second part copies from the first; - an empty stream and spare destination capacity. - Controls that must be rejected without writes past the destination: one byte too little capacity, and a stream cut in half (CPython's zlib rejects it too). - The distance bound, checked on bare deflate data so that neither the zlib header nor the Adler-32 check can reject the stream. After the literals `ABCD`, a length-3 match at distance 4 decodes to `ABCDABC`. The same block with distance 5 is rejected with only the four literals decoded, before the match copies anything. CPython's zlib also rejects it (`wbits=-15`). The decoder is not changed. The test is registered with the other Python-driven startup tests. It is not added to the `pr` preset, so the full `main` and manual workflows run it with the rest of the suite. It runs in under a second without a GPU or game files, so it can move into the preset if you prefer. `docs/startup-runtime.md` has the command. Refs #1149 ## Verification On a953c41a (based on a8946ec0), Windows 11, MSVC 14.51, Python 3.14.5 (zlib-ng 1.3.1), `dev` preset: ```text ctest --preset dev --no-tests=error -R '^startup_inflate$' -V stored block 0, 70016 -> 70000 bytes, decoded fixed block 1, 19699 -> 98304 bytes, decoded dynamic block 2, 19335 -> 98304 bytes, decoded dynamic_spare_capacity block 2, 19335 -> 98304 bytes, decoded dynamic_sparse block 2, 21307 -> 32768 bytes, decoded sync_flush block 0, 24821 -> 35092 bytes, decoded empty block 1, 8 -> 0 bytes, decoded dynamic_short_capacity 19335 bytes, capacity 98303, rejected dynamic_truncated 9667 bytes, capacity 98304, rejected raw_distance_4 block 1, 7 -> 7 bytes, decoded raw_distance_5 7 bytes, capacity 64, rejected after 4 bytes inflate.h matches zlib 1.3.1.zlib-ng in 8 cases and rejects 3 controls 1/1 Test #243: startup_inflate .................. Passed 0.98 sec ``` Negative control: I broke `inflate.h` one way at a time, rebuilt the probe and reran the check. Each mutation fails it: | Mutation | First failing case | |---|---| | last distance code has 12 extra bits instead of 13 | `fixed: exit 1: rejected` | | length code 284 has 4 extra bits instead of 5 | `fixed: exit 1: rejected` | | distance base 24577 off by one | `fixed: exit 1: rejected` | | distance bound off by one (`distance > out_pos + 1`) | `raw_distance_5: exit 0: inflated 7` | | distance bound too strict (`distance >= out_pos`) | `raw_distance_4: exit 1: rejected after 4 bytes` | | no distance bound | `raw_distance_5: exit 0: inflated 7` | | no capacity check for a match | `dynamic_short_capacity: exit 3: wrote past the destination at +0` | | repeat code 18 repeats 10 + n zeros | `dynamic_sparse: exit 1: rejected` | | repeat code 17 repeats 2 + n zeros | `dynamic_sparse: exit 1: rejected` | | repeat code 16 repeats 4 + n | `dynamic: exit 1: rejected` | | stored block does not realign to a byte | `stored: exit 1: rejected` | On the first revision, with the last-distance-code and no-distance-bound mutations applied together, `startup_stubs` and `startup_storage_directory_batch` still passed and only `startup_inflate` failed. On a953c41a, with the header unchanged, all three pass. `tests/docs_links_check.py`, `tests/bidi_controls_check.py` and `git diff --check` pass. ## Evidence and follow-up All inputs are generated at test time from a fixed seed with CPython's `zlib`. The distance-bound blocks are hand-built from RFC 1951's fixed code table. No external data is involved. The check compares only decoded bytes and the first block's type, not compressed bytes, so it should not depend on the zlib build. I ran it locally only with zlib-ng 1.3.1. Not covered here: header rejection (`FDICT`, method, `FCHECK`), reserved block type 3 and stored `LEN`/`NLEN` mismatches. The stub consumer already covers a damaged Adler-32. AI-assisted.
R0Y00 commented 2026-11-24 11:02:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T10:51:35Z.

Review: CHANGES_REQUESTED


The zlib comparison adds useful coverage for valid long back-references, dynamic Huffman blocks, truncation and output capacity. Please keep those cases and check invalid-distance rejection at the bounds-validation step. The crafted guard-byte case is designed to make an unchecked read observable if the guard is removed. A focused check of the distance validation can protect that rule without adding an out-of-bounds-read demonstration to the fixture.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6037883913 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1404#pullrequestreview-6037883913). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T10:51:35Z. **Review:** CHANGES_REQUESTED --- The zlib comparison adds useful coverage for valid long back-references, dynamic Huffman blocks, truncation and output capacity. Please keep those cases and check invalid-distance rejection at the bounds-validation step. The crafted guard-byte case is designed to make an unchecked read observable if the guard is removed. A focused check of the distance validation can protect that rule without adding an out-of-bounds-read demonstration to the fixture.
tedin7 commented 2026-11-24 11:02:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T10:52:53Z.

Review: COMMENTED


Reviewed commit c4fb0f7fd5c550263288cb92dbaaba85ef5ee5e5:

  • Diff & scope: Cleanly adds tests/startup_inflate_probe.cpp (16-byte 0xA5 before/after destination guards checked regardless of decode outcome) and tests/startup_inflate_check.py (checks src/startup_stubs/inflate.h against CPython's zlib across 8 cases—stored, fixed, dynamic, spare capacity, sparse alphabet with repeat codes 16/17/18, sync flush across an empty stored block, empty stream, and distance-1 match—plus 3 negative controls: 1-byte short capacity, truncated stream, and distance-2 match before output start with matching guard-byte checksum), wired into CMakeLists.txt (startup_inflate) and docs/startup-runtime.md.
  • Linux checks: git diff --check clean; tests/docs_links_check.py (16 Markdown files, 319 links) and tests/bidi_controls_check.py (1182 files) passed.
  • Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP):
    • Debug (--preset dev, startup_inflate_probe): 1/1 passed (startup_inflate).
    • Release /O2 (--preset release, startup_inflate_probe): 1/1 passed (startup_inflate).
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6037901049 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1404#pullrequestreview-6037901049). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T10:52:53Z. **Review:** COMMENTED --- Reviewed commit `c4fb0f7fd5c550263288cb92dbaaba85ef5ee5e5`: - **Diff & scope**: Cleanly adds `tests/startup_inflate_probe.cpp` (16-byte `0xA5` before/after destination guards checked regardless of decode outcome) and `tests/startup_inflate_check.py` (checks `src/startup_stubs/inflate.h` against CPython's `zlib` across 8 cases—stored, fixed, dynamic, spare capacity, sparse alphabet with repeat codes 16/17/18, sync flush across an empty stored block, empty stream, and distance-1 match—plus 3 negative controls: 1-byte short capacity, truncated stream, and distance-2 match before output start with matching guard-byte checksum), wired into `CMakeLists.txt` (`startup_inflate`) and `docs/startup-runtime.md`. - **Linux checks**: `git diff --check` clean; `tests/docs_links_check.py` (16 Markdown files, 319 links) and `tests/bidi_controls_check.py` (1182 files) passed. - **Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP)**: - Debug (`--preset dev`, `startup_inflate_probe`): **1/1 passed** (`startup_inflate`). - Release `/O2` (`--preset release`, `startup_inflate_probe`): **1/1 passed** (`startup_inflate`).
RenzoWit35 commented 2026-11-24 11:03:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T11:01:47Z.


Thanks. a953c41a removes the guard-byte case and checks the distance bound directly:

  • The probe has a raw mode that runs inflate_raw on bare deflate data, so neither the zlib header nor the Adler-32 check can reject the stream. The guard before the destination is gone. The guard after it remains for the capacity control.
  • After the literals ABCD, a length-3 match at distance 4 decodes to ABCDABC. The same block with distance 5 is rejected with only the four literals decoded (rejected after 4 bytes), so the rejection comes from the bound before the copy.
  • The zlib comparison cases are unchanged: stored, fixed, dynamic and sparse-alphabet blocks, the sync flush, spare and one-byte-short capacity, and the truncated stream.

ctest --preset dev --no-tests=error -R '^startup_inflate$' -V passes on a953c41a (8 cases decoded, 3 controls rejected). Relaxing the bound to distance > out_pos + 1 fails raw_distance_5 (inflated 7). Tightening it to distance >= out_pos fails raw_distance_4 (rejected after 4 bytes). The PR description has the updated output and mutation table.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7129560887 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1404#issuecomment-7129560887). Originally posted by [RenzoWit35](https://github.com/RenzoWit35) on 2026-11-24T11:01:47Z. --- Thanks. a953c41a removes the guard-byte case and checks the distance bound directly: - The probe has a `raw` mode that runs `inflate_raw` on bare deflate data, so neither the zlib header nor the Adler-32 check can reject the stream. The guard before the destination is gone. The guard after it remains for the capacity control. - After the literals `ABCD`, a length-3 match at distance 4 decodes to `ABCDABC`. The same block with distance 5 is rejected with only the four literals decoded (`rejected after 4 bytes`), so the rejection comes from the bound before the copy. - The zlib comparison cases are unchanged: stored, fixed, dynamic and sparse-alphabet blocks, the sync flush, spare and one-byte-short capacity, and the truncated stream. `ctest --preset dev --no-tests=error -R '^startup_inflate$' -V` passes on a953c41a (8 cases decoded, 3 controls rejected). Relaxing the bound to `distance > out_pos + 1` fails `raw_distance_5` (`inflated 7`). Tightening it to `distance >= out_pos` fails `raw_distance_4` (`rejected after 4 bytes`). The PR description has the updated output and mutation table. AI-assisted.
upstream-sync closed this pull request 2026-11-24 12:02:44 +00:00
R0Y00 commented 2026-11-24 12:02:45 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T11:16:41Z.

Review: APPROVED


Re-reviewed a953c41. Windows Release startup_inflate passed with eight comparisons and three rejection controls. The revised test checks invalid input rejection using the intact decoder.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6038234856 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1404#pullrequestreview-6038234856). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T11:16:41Z. **Review:** APPROVED --- Re-reviewed a953c41. Windows Release startup_inflate passed with eight comparisons and three rejection controls. The revised test checks invalid input rejection using the intact decoder.
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!1397
No description provided.