runtime: speed up stored blocks, match copies and symbol decoding in the zlib decoder #1812

Merged
NiiRoZz merged 14 commits from upstream-pr/1819 into main 2026-11-24 20:54:39 +00:00
NiiRoZz commented 2026-11-24 19:13:45 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-24T19:13:45Z.


Change

Follow-up to #1743, which must land first: this branch is built on its head (fe375f43), so until it merges the diff here also shows its two commits. The new work is fe375f43..ea3b6378.

It continues the same thing #1743 started, decode throughput of src/startup_stubs/inflate.h (the zlib decoder of the DirectStorage stand-in), with no change to results. Three steps, kept as separate commits so they can be reviewed one at a time:

  • c7fa63c4: a stored block is copied with one bounds-checked memcpy instead of byte by byte.
  • c0e5059c: a match is copied 8 bytes at a time when the distance allows it, and with memset for distance 1; the copy never writes past the end of the match.
  • 587688e9: away from the ends of the buffers, symbols are decoded from a 64-bit bit buffer in a loop that runs only while at least 8 input bytes and 258 output bytes remain, so one refill covers a whole length/distance pair (at most 10+5+10+13 = 38 bits) and no per-symbol bounds check is needed. End of block, codes longer than the table width, invalid symbols and distances before the first output byte are left to the existing careful loop, and unused whole bytes are given back on exit so stored blocks and the Adler-32 trailer see the same position as before.

Statuses (ok, insufficient_output, corrupt_stream, bad_checksum), public signatures and xcrdapi.cpp are unchanged. The remaining commits are tests, controls, compile-time checks and the benchmark mode.

No other open PR touches inflate.h or its tests (checked by touched file on 2026-11-24).

Verification

Head ea3b6378, Windows 11, MSVC 19.44 x64. A trial merge with current main is clean.

ctest --no-tests=error -R "^(startup_inflate|startup_inflate_status|startup_storage_|startup_dstorage_|cpu_native_dstorage|startup_heartbeat_commit|startup_stubs)"
  • Release: 15 passed, 0 failed, 0 skipped. The same filter on #1743's head also gives 15/0/0.
  • Debug, startup_inflate only: passed.
  • python -B tests/docs_links_check.py and python -B tests/bidi_controls_check.py: pass.

Not run: a sanitizer build of this head, and any hardware or WARP test (no GPU path).

Throughput, reproducible with the mode this PR adds (not a ctest; interleaved builds, warm-up dropped, best of N, output checksummed):

.\out\pe-env\Scripts\python.exe -B tests/startup_inflate_check.py --bench <probe built from #1743> out/build/release/startup_inflate_probe.exe

Decoded MiB/s on 16 MiB synthetic streams, Ryzen 7 9800X3D:

stream class #1743 this PR CPython zlib 1.3.1
stored 1222 3311 3030
fixed Huffman 716 1177 853
dynamic, word text 273 428 405
dynamic, skewed 225 388 368

The machine was under other load during this run and numbers move by tens of percent between runs; in one earlier run of the same build the stored class came out below zlib. Read the zlib column as "about level", not as a margin.

Evidence and follow-up

All inputs are generated by the test from fixed seeds or written by hand as raw deflate; no external data.

  • The 900-stream random case from #1743 is unchanged: 3597 cases equal to the bit-at-a-time build and to CPython's zlib.
  • New boundary case: 57 streams at every capacity and every truncation length, 63,994 cases.
  • Window case (added in ea3b6378 after review): 5 streams of 96 to 160 KiB, three of them hand-built fixed blocks with matches at distance 32768 (zlib's encoder never emits that distance) and lengths up to 258, at exact capacity, one byte short, at inner match ends and truncated; 52 cases equal to the bit-at-a-time build and to zlib.
  • The probe now ends both the input and the destination at a no-access page, so a read past the input or a write past the destination faults the test.
  • The byte-wise stored copy is kept in the bit-at-a-time reference build, so the stored memcpy has an oracle other than zlib.
  • Negative controls, each a deliberately wrong build that the check must catch: X2W_INFLATE_FAULTY_TABLE (from #1743), X2W_INFLATE_FAULTY_MARGIN (one output byte short), a 7-byte input margin, and a fast loop without its distance check.
  • static_asserts for the table width range and for the byte order the 8-byte loads assume.

One existing difference from zlib is kept as it was before this PR and is now checked more narrowly: a truncated stored block that also does not fit the destination is reported as corrupt_stream here and as insufficient output by zlib (809 cases with this seed; the check requires a stored block to be the one involved).

Local observation, not reproducible from this PR: a separate review harness compared the decoder before #1743, #1743 and this head on about 240,000 generated and hand-built raw-deflate cases (codes up to 15 bits, incomplete and over-subscribed code sets, every distance 1 to 9 at lengths 3 to 258, the maximum distance, stored blocks of 0 and 65535 bytes at any bit position, truncations and bit flips, guard pages on both sides of both buffers) and found no difference in status, size or destination bytes.

Second local observation, also not reproducible from this PR: in one local startup run per build on a quiet machine, with a local timer around the stand-in's inflate calls (not included here), the same streams and the same 762,010,967 output bytes took 2,490 ms of cumulative decode time with #1743 alone and 1,504 ms with this head (about 292 and 483 MiB/s). Byte totals were identical; with one run per build there is no measured run-to-run spread. Wall time to the end of that load did not change at the 10 s resolution of the log, so this is a CPU-time saving there, not a demonstrated loading-time saving.

Things a reviewer may want to weigh: three speed steps in one PR (one subject, separate commits; I can split it if preferred); the test file grows by about 325 lines; the test-only macros in inflate.h must have the same value in every translation unit linked into one binary (noted in a comment).

Left for follow-up: Adler-32 is now the largest remaining cost (most of the stored path); a second-level table for long codes showed about 1 % in the profile and was not done.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed. (docs/startup-runtime.md, zlib decoder check and the benchmark command)
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code): the change, tests and measurements were produced with AI agents, reviewed by a separate agent, and the commands listed were executed locally.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6589450082 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1819). Originally posted by [NiiRoZz](https://github.com/NiiRoZz) on 2026-11-24T19:13:45Z. --- ## Change Follow-up to #1743, which must land first: this branch is built on its head (fe375f43), so until it merges the diff here also shows its two commits. The new work is fe375f43..ea3b6378. It continues the same thing #1743 started, decode throughput of `src/startup_stubs/inflate.h` (the zlib decoder of the DirectStorage stand-in), with no change to results. Three steps, kept as separate commits so they can be reviewed one at a time: - c7fa63c4: a stored block is copied with one bounds-checked `memcpy` instead of byte by byte. - c0e5059c: a match is copied 8 bytes at a time when the distance allows it, and with `memset` for distance 1; the copy never writes past the end of the match. - 587688e9: away from the ends of the buffers, symbols are decoded from a 64-bit bit buffer in a loop that runs only while at least 8 input bytes and 258 output bytes remain, so one refill covers a whole length/distance pair (at most 10+5+10+13 = 38 bits) and no per-symbol bounds check is needed. End of block, codes longer than the table width, invalid symbols and distances before the first output byte are left to the existing careful loop, and unused whole bytes are given back on exit so stored blocks and the Adler-32 trailer see the same position as before. Statuses (`ok`, `insufficient_output`, `corrupt_stream`, `bad_checksum`), public signatures and `xcrdapi.cpp` are unchanged. The remaining commits are tests, controls, compile-time checks and the benchmark mode. No other open PR touches `inflate.h` or its tests (checked by touched file on 2026-11-24). ## Verification Head ea3b6378, Windows 11, MSVC 19.44 x64. A trial merge with current `main` is clean. ``` ctest --no-tests=error -R "^(startup_inflate|startup_inflate_status|startup_storage_|startup_dstorage_|cpu_native_dstorage|startup_heartbeat_commit|startup_stubs)" ``` - Release: 15 passed, 0 failed, 0 skipped. The same filter on #1743's head also gives 15/0/0. - Debug, `startup_inflate` only: passed. - `python -B tests/docs_links_check.py` and `python -B tests/bidi_controls_check.py`: pass. Not run: a sanitizer build of this head, and any hardware or WARP test (no GPU path). Throughput, reproducible with the mode this PR adds (not a ctest; interleaved builds, warm-up dropped, best of N, output checksummed): ``` .\out\pe-env\Scripts\python.exe -B tests/startup_inflate_check.py --bench <probe built from #1743> out/build/release/startup_inflate_probe.exe ``` Decoded MiB/s on 16 MiB synthetic streams, Ryzen 7 9800X3D: | stream class | #1743 | this PR | CPython zlib 1.3.1 | |---|---|---|---| | stored | 1222 | 3311 | 3030 | | fixed Huffman | 716 | 1177 | 853 | | dynamic, word text | 273 | 428 | 405 | | dynamic, skewed | 225 | 388 | 368 | The machine was under other load during this run and numbers move by tens of percent between runs; in one earlier run of the same build the stored class came out below zlib. Read the zlib column as "about level", not as a margin. ## Evidence and follow-up All inputs are generated by the test from fixed seeds or written by hand as raw deflate; no external data. - The 900-stream random case from #1743 is unchanged: 3597 cases equal to the bit-at-a-time build and to CPython's zlib. - New boundary case: 57 streams at every capacity and every truncation length, 63,994 cases. - Window case (added in ea3b6378 after review): 5 streams of 96 to 160 KiB, three of them hand-built fixed blocks with matches at distance 32768 (zlib's encoder never emits that distance) and lengths up to 258, at exact capacity, one byte short, at inner match ends and truncated; 52 cases equal to the bit-at-a-time build and to zlib. - The probe now ends both the input and the destination at a no-access page, so a read past the input or a write past the destination faults the test. - The byte-wise stored copy is kept in the bit-at-a-time reference build, so the stored `memcpy` has an oracle other than zlib. - Negative controls, each a deliberately wrong build that the check must catch: `X2W_INFLATE_FAULTY_TABLE` (from #1743), `X2W_INFLATE_FAULTY_MARGIN` (one output byte short), a 7-byte input margin, and a fast loop without its distance check. - `static_assert`s for the table width range and for the byte order the 8-byte loads assume. One existing difference from zlib is kept as it was before this PR and is now checked more narrowly: a truncated stored block that also does not fit the destination is reported as `corrupt_stream` here and as insufficient output by zlib (809 cases with this seed; the check requires a stored block to be the one involved). Local observation, not reproducible from this PR: a separate review harness compared the decoder before #1743, #1743 and this head on about 240,000 generated and hand-built raw-deflate cases (codes up to 15 bits, incomplete and over-subscribed code sets, every distance 1 to 9 at lengths 3 to 258, the maximum distance, stored blocks of 0 and 65535 bytes at any bit position, truncations and bit flips, guard pages on both sides of both buffers) and found no difference in status, size or destination bytes. Second local observation, also not reproducible from this PR: in one local startup run per build on a quiet machine, with a local timer around the stand-in's inflate calls (not included here), the same streams and the same 762,010,967 output bytes took 2,490 ms of cumulative decode time with #1743 alone and 1,504 ms with this head (about 292 and 483 MiB/s). Byte totals were identical; with one run per build there is no measured run-to-run spread. Wall time to the end of that load did not change at the 10 s resolution of the log, so this is a CPU-time saving there, not a demonstrated loading-time saving. Things a reviewer may want to weigh: three speed steps in one PR (one subject, separate commits; I can split it if preferred); the test file grows by about 325 lines; the test-only macros in `inflate.h` must have the same value in every translation unit linked into one binary (noted in a comment). Left for follow-up: Adler-32 is now the largest remaining cost (most of the stored path); a second-level table for long codes showed about 1 % in the profile and was not done. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. (`docs/startup-runtime.md`, zlib decoder check and the benchmark command) - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted (Claude Code): the change, tests and measurements were produced with AI agents, reviewed by a separate agent, and the commands listed were executed locally. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
tedin7 commented 2026-11-24 20:00:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:22:24Z.

Review: COMMENTED


Review of head a95e7f191e (a merge of #1743 at fe375f43 into the second-pass branch; base origin/main ddeaa5eb). Verdict: no P1, no P2. Three nits below.

What I read The part of the diff that is new on top of #1743: copy_match, the fast loop in codes(), the stored-block memcpy, the probe and the new test cases.

Fast loop (codes, src/startup_stubs/inflate.h)

  • Refill: buffer |= next << count; in_pos += (63 - count) >> 3; count |= 56 keeps count equal to the bits actually loaded for every count from 0 to 63, so count is 56 to 63 after a refill. The loaded bytes are all inside the input: the loop runs only while at least fast_in_margin (8) bytes remain, and the 8-byte read is a memcpy, so there is no unaligned or aliasing access. The little-endian assumption is guarded by the new #error.
  • Worst symbol is 10 + 5 + 10 + 13 = 38 bits, within the 56 available, and static_assert limits fast_bits to 15 (48 bits).
  • Nothing is consumed when the loop breaks (end of block, unassigned table entry, symbol 286/287, distance symbol 30/31, distance > out_pos): used is a local, and on exit in_pos - (count >> 3) hands back the whole bytes and bit_count = count & 7 keeps the invariant bit_count < 8 that decode(), stored() and the Adler-32 trailer rely on. The unconsumed partial byte is the low count & 7 bits, which is what is stored.
  • Output: the loop runs only while 258 bytes of capacity remain, the longest match, so no write can pass out_size. The careful loop still does the capacity check and sets overflow.
  • copy_match: the 8-byte chunk branch needs distance >= 8 and length >= 8. Each chunk's source ends at or before its destination, so memcpy never sees overlapping ranges, and the final chunk ends at the match end and only rewrites bytes whose source is already final. For distance < 8 the byte loop keeps the forward-overlap semantics, and distance == 1 is memset. It never writes past out + length.
  • Stored blocks: memcpy is skipped when length == 0, so a null out is not passed. The capacity check comes first.

Commands run locally on Linux (g++ 14.2, Python 3.14.8, zlib 1.3.1). The probe now calls VirtualAlloc, so I built it against a small local stand-in for the three <windows.h> calls (mmap and mprotect guard pages, kept outside the repository).

  • g++ -std=c++17 -O2 -fsanitize=address,undefined -fno-sanitize-recover=undefined -I src for the default, X2W_INFLATE_FAST_BITS=0 and X2W_INFLATE_FAULTY_TABLE probes; the three fast-loop control probes without sanitizers, so that the guard-page fault is a plain SIGSEGV (the check expects 0xC0000005 on Windows; I ran a scratch copy of tests/startup_inflate_check.py with that one constant set to -11).
  • startup_inflate_check.py: passes. 900 random streams / 3597 cases equal to the bit-at-a-time build and to zlib; 57 boundary streams / 63994 cases equal to the bit-at-a-time build and to zlib in 63185 (809 are truncated stored blocks neither can complete); the four controls are caught (table, input margin, output margin, distance).
  • My own differential run of the default (ASan/UBSan) probe against the bit-at-a-time probe, two seeds, 7612 cases each: periodic data with periods 1, 2, 3, 5, 7, 8, 9, 15, 16, 17, 255, 257, 1000 and 32768, matches with distances up to 32768, stored/fixed/dynamic and sync-flushed streams, capacities at exact, plus or minus 1 and plus or minus 257/258/259, every kind of truncation, and 1 to 3 bit flips. Result: 0 mismatches in status, size and bytes.
  • python3 -B tests/docs_links_check.py and python3 -B tests/bidi_controls_check.py pass; git diff --check origin/main...HEAD is clean.

Local mutations (each reverted, tree clean afterwards)

  1. Chunk-copy guard distance >= 8 changed to >= 4: the check fails.
  2. Exit gives back one byte too many (in_pos - (count >> 3) + 1): fails.
  3. Stored memcpy one byte short: fails.
  4. Refill advances (62 - count) >> 3 bytes: fails.

Not run: a Windows build and the CTest run (and the --bench timings in the docs). I have no Windows machine in this environment, so the Windows probe with real VirtualAlloc guard pages and the claimed speed-up are not confirmed by me.

Nits (not blocking)

  • The 900-stream differential in the test uses at most 12 KiB per stream, so it rarely reaches distances above 8 KiB with long matches; the boundary case covers capacity edges but not the 32 KiB window. The data above did, with no difference found.
  • The existing leniency stays: when bit flips leave an incomplete distance code set, inflate.h can decode where zlib reports "invalid distances set" (one such case in my first seed: 1 of 7612). It is the documented exception, it is identical in the bit-at-a-time build, and this PR does not change it.
  • copy_match and the fast loop are guarded by fast_bits > 0, so the reference build keeps the old byte loops; good for the differential, but the bit-at-a-time probe therefore does not exercise copy_match's byte loop under the new code. The default build does for distance < 8.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6045836327 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1819#pullrequestreview-6045836327). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T19:22:24Z. **Review:** COMMENTED --- Review of head a95e7f191e8c6b74151d0f3b90b5ab6cc7a36279 (a merge of #1743 at fe375f43 into the second-pass branch; base `origin/main` ddeaa5eb). Verdict: no P1, no P2. Three nits below. **What I read** The part of the diff that is new on top of #1743: `copy_match`, the fast loop in `codes()`, the stored-block `memcpy`, the probe and the new test cases. **Fast loop (`codes`, src/startup_stubs/inflate.h)** - Refill: `buffer |= next << count; in_pos += (63 - count) >> 3; count |= 56` keeps `count` equal to the bits actually loaded for every `count` from 0 to 63, so `count` is 56 to 63 after a refill. The loaded bytes are all inside the input: the loop runs only while at least `fast_in_margin` (8) bytes remain, and the 8-byte read is a `memcpy`, so there is no unaligned or aliasing access. The little-endian assumption is guarded by the new `#error`. - Worst symbol is 10 + 5 + 10 + 13 = 38 bits, within the 56 available, and `static_assert` limits `fast_bits` to 15 (48 bits). - Nothing is consumed when the loop breaks (end of block, unassigned table entry, symbol 286/287, distance symbol 30/31, `distance > out_pos`): `used` is a local, and on exit `in_pos - (count >> 3)` hands back the whole bytes and `bit_count = count & 7` keeps the invariant `bit_count < 8` that `decode()`, `stored()` and the Adler-32 trailer rely on. The unconsumed partial byte is the low `count & 7` bits, which is what is stored. - Output: the loop runs only while 258 bytes of capacity remain, the longest match, so no write can pass `out_size`. The careful loop still does the capacity check and sets `overflow`. - `copy_match`: the 8-byte chunk branch needs `distance >= 8` and `length >= 8`. Each chunk's source ends at or before its destination, so `memcpy` never sees overlapping ranges, and the final chunk ends at the match end and only rewrites bytes whose source is already final. For `distance < 8` the byte loop keeps the forward-overlap semantics, and `distance == 1` is `memset`. It never writes past `out + length`. - Stored blocks: `memcpy` is skipped when `length == 0`, so a null `out` is not passed. The capacity check comes first. **Commands run locally on Linux** (g++ 14.2, Python 3.14.8, zlib 1.3.1). The probe now calls `VirtualAlloc`, so I built it against a small local stand-in for the three `<windows.h>` calls (mmap and mprotect guard pages, kept outside the repository). - `g++ -std=c++17 -O2 -fsanitize=address,undefined -fno-sanitize-recover=undefined -I src` for the default, `X2W_INFLATE_FAST_BITS=0` and `X2W_INFLATE_FAULTY_TABLE` probes; the three fast-loop control probes without sanitizers, so that the guard-page fault is a plain SIGSEGV (the check expects 0xC0000005 on Windows; I ran a scratch copy of `tests/startup_inflate_check.py` with that one constant set to -11). - `startup_inflate_check.py`: passes. 900 random streams / 3597 cases equal to the bit-at-a-time build and to zlib; 57 boundary streams / 63994 cases equal to the bit-at-a-time build and to zlib in 63185 (809 are truncated stored blocks neither can complete); the four controls are caught (table, input margin, output margin, distance). - My own differential run of the default (ASan/UBSan) probe against the bit-at-a-time probe, two seeds, 7612 cases each: periodic data with periods 1, 2, 3, 5, 7, 8, 9, 15, 16, 17, 255, 257, 1000 and 32768, matches with distances up to 32768, stored/fixed/dynamic and sync-flushed streams, capacities at exact, plus or minus 1 and plus or minus 257/258/259, every kind of truncation, and 1 to 3 bit flips. Result: 0 mismatches in status, size and bytes. - `python3 -B tests/docs_links_check.py` and `python3 -B tests/bidi_controls_check.py` pass; `git diff --check origin/main...HEAD` is clean. **Local mutations (each reverted, tree clean afterwards)** 1. Chunk-copy guard `distance >= 8` changed to `>= 4`: the check fails. 2. Exit gives back one byte too many (`in_pos - (count >> 3) + 1`): fails. 3. Stored `memcpy` one byte short: fails. 4. Refill advances `(62 - count) >> 3` bytes: fails. **Not run:** a Windows build and the CTest run (and the `--bench` timings in the docs). I have no Windows machine in this environment, so the Windows probe with real `VirtualAlloc` guard pages and the claimed speed-up are not confirmed by me. **Nits (not blocking)** - The 900-stream differential in the test uses at most 12 KiB per stream, so it rarely reaches distances above 8 KiB with long matches; the boundary case covers capacity edges but not the 32 KiB window. The data above did, with no difference found. - The existing leniency stays: when bit flips leave an incomplete distance code set, `inflate.h` can decode where zlib reports "invalid distances set" (one such case in my first seed: 1 of 7612). It is the documented exception, it is identical in the bit-at-a-time build, and this PR does not change it. - `copy_match` and the fast loop are guarded by `fast_bits > 0`, so the reference build keeps the old byte loops; good for the differential, but the bit-at-a-time probe therefore does not exercise `copy_match`'s byte loop under the new code. The default build does for `distance < 8`. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-24 21:00:52 +00:00
tedin7 commented 2026-11-24 21:00:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T20:09:05Z.

Review: COMMENTED


Review of head ea3b637824 (base origin/main ddeaa5eb). Verdict: no P1, no P2, 0 defects found. Retest after my earlier review of a95e7f19.

What changed since a95e7f19 One commit, ea3b6378: no change to src/startup_stubs/inflate.h or CMakeLists.txt. It adds a window case to tests/startup_inflate_check.py (LENGTH_BASE/DISTANCE_BASE tables, fixed_tokens, window_cases, expect_window, called with its own seed) and one paragraph in docs/startup-runtime.md. So my earlier reading of copy_match, the fast loop, the refill and exit arithmetic and the stored-block memcpy still holds, because the decoder bytes are identical.

The new test, read against RFC 1951

  • The length table (3..258 with 0,1,2,3,4,5 extra bits, code 285 = 258 with 0 extra bits) and the distance table (30 codes, 0 extra bits for codes 0-3, then code // 2 - 1, last base 24577) match section 3.2.5. fixed_tokens uses the fixed Huffman code of 3.2.6 (8/9/7/8 bits) and 5-bit distance codes. Length 258 is forced to code 28, as the RFC requires.
  • The hand-built streams do reach distance 32768 with length 258, which zlib's encoder never emits (it keeps 262 bytes of lookahead out of the window). assert reference(stream, "zlib") == data checks each stream against CPython zlib before anything runs, so the test data are valid streams.
  • Cases: exact capacity, one byte short, capacity at the end of an inner 32768-distance match, and 6 truncations per stream. The probe puts a no-access page at the end of input and output, so an over-read or over-write faults. 5 streams, 52 cases, matching the docs.

Commands run locally on Linux (g++ 14.2, Python 3, zlib 1.3.1). The probe calls VirtualAlloc, VirtualProtect and VirtualFree, so I built it against a small local stand-in for those three <windows.h> calls (mmap and mprotect guard pages, kept outside the repository).

  • g++ -std=c++17 -O2 -fsanitize=address,undefined -fno-sanitize-recover=undefined for the default probe, the X2W_INFLATE_FAST_BITS=0 probe and the X2W_INFLATE_FAULTY_TABLE probe; the margin, input-margin and distance controls without sanitizers so the guard page gives a plain SIGSEGV (a scratch copy of tests/startup_inflate_check.py with ACCESS_VIOLATION = -11; the repo expects 0xC0000005 on Windows).
  • startup_inflate_check.py: passes. 900 random streams / 3597 cases; 57 boundary streams / 63994 cases (equal to zlib in 63185, 809 truncated stored blocks neither can complete); window: 5 streams / 52 cases equal to bit-at-a-time decoding and to zlib in all; the four controls are caught.
  • git diff --check origin/main...HEAD clean; python3 -B tests/docs_links_check.py and python3 -B tests/bidi_controls_check.py pass.
  • Local mutation: in the fast loop if (distance > out_pos) break; changed to also break for distance > 32767. The window case does not fail, which is expected: the fast loop then hands the match to the careful loop, which produces identical output. It is an equivalent mutant, not a gap in the test. I did not find a mutation specific to distance 32768 that the existing cases miss (earlier mutations from my first review are still caught by the earlier cases).

Not run: a Windows build, the CTest run and the --bench timings in the docs. I have no Windows machine in this environment, so the Windows probe with real VirtualAlloc guard pages and the claimed speed-up are not confirmed by me. Verification here is Linux plus source analysis, pending a Windows 2025 run.

Nits (not blocking)

  • docs/startup-runtime.md: the new window paragraph has two lines far over the width used in the rest of the file (the line with "zlib's encoder keeps 262 bytes of lookahead out of its window and never reaches distance 32768, so three are fixed blocks written by" and the one ending "In every case the probe ends both the stream and the destination"). A re-wrap would match the surrounding text. The docs link check passes either way.
  • The two zlib-encoded window streams repeat runs from 24 to 32.5 KiB back, so they never use distance 32768 themselves; only the three hand-built ones do. Fine, the docs say so.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046546463 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1819#pullrequestreview-6046546463). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T20:09:05Z. **Review:** COMMENTED --- Review of head ea3b637824d6eae5129d01753a0258e1ec39b2d3 (base `origin/main` ddeaa5eb). Verdict: no P1, no P2, 0 defects found. Retest after my earlier review of a95e7f19. **What changed since a95e7f19** One commit, ea3b6378: no change to `src/startup_stubs/inflate.h` or `CMakeLists.txt`. It adds a window case to `tests/startup_inflate_check.py` (`LENGTH_BASE`/`DISTANCE_BASE` tables, `fixed_tokens`, `window_cases`, `expect_window`, called with its own seed) and one paragraph in `docs/startup-runtime.md`. So my earlier reading of `copy_match`, the fast loop, the refill and exit arithmetic and the stored-block `memcpy` still holds, because the decoder bytes are identical. **The new test, read against RFC 1951** - The length table (3..258 with 0,1,2,3,4,5 extra bits, code 285 = 258 with 0 extra bits) and the distance table (30 codes, 0 extra bits for codes 0-3, then `code // 2 - 1`, last base 24577) match section 3.2.5. `fixed_tokens` uses the fixed Huffman code of 3.2.6 (8/9/7/8 bits) and 5-bit distance codes. Length 258 is forced to code 28, as the RFC requires. - The hand-built streams do reach distance 32768 with length 258, which zlib's encoder never emits (it keeps 262 bytes of lookahead out of the window). `assert reference(stream, "zlib") == data` checks each stream against CPython zlib before anything runs, so the test data are valid streams. - Cases: exact capacity, one byte short, capacity at the end of an inner 32768-distance match, and 6 truncations per stream. The probe puts a no-access page at the end of input and output, so an over-read or over-write faults. 5 streams, 52 cases, matching the docs. **Commands run locally on Linux** (g++ 14.2, Python 3, zlib 1.3.1). The probe calls `VirtualAlloc`, `VirtualProtect` and `VirtualFree`, so I built it against a small local stand-in for those three `<windows.h>` calls (mmap and mprotect guard pages, kept outside the repository). - `g++ -std=c++17 -O2 -fsanitize=address,undefined -fno-sanitize-recover=undefined` for the default probe, the `X2W_INFLATE_FAST_BITS=0` probe and the `X2W_INFLATE_FAULTY_TABLE` probe; the margin, input-margin and distance controls without sanitizers so the guard page gives a plain SIGSEGV (a scratch copy of `tests/startup_inflate_check.py` with `ACCESS_VIOLATION = -11`; the repo expects 0xC0000005 on Windows). - `startup_inflate_check.py`: passes. 900 random streams / 3597 cases; 57 boundary streams / 63994 cases (equal to zlib in 63185, 809 truncated stored blocks neither can complete); **window: 5 streams / 52 cases equal to bit-at-a-time decoding and to zlib in all**; the four controls are caught. - `git diff --check origin/main...HEAD` clean; `python3 -B tests/docs_links_check.py` and `python3 -B tests/bidi_controls_check.py` pass. - Local mutation: in the fast loop `if (distance > out_pos) break;` changed to also break for `distance > 32767`. The window case does **not** fail, which is expected: the fast loop then hands the match to the careful loop, which produces identical output. It is an equivalent mutant, not a gap in the test. I did not find a mutation specific to distance 32768 that the existing cases miss (earlier mutations from my first review are still caught by the earlier cases). **Not run:** a Windows build, the CTest run and the `--bench` timings in the docs. I have no Windows machine in this environment, so the Windows probe with real `VirtualAlloc` guard pages and the claimed speed-up are not confirmed by me. Verification here is Linux plus source analysis, pending a Windows 2025 run. **Nits (not blocking)** - `docs/startup-runtime.md`: the new window paragraph has two lines far over the width used in the rest of the file (the line with "zlib's encoder keeps 262 bytes of lookahead out of its window and never reaches distance 32768, so three are fixed blocks written by" and the one ending "In every case the probe ends both the stream and the destination"). A re-wrap would match the surrounding text. The docs link check passes either way. - The two zlib-encoded window streams repeat runs from 24 to 32.5 KiB back, so they never use distance 32768 themselves; only the three hand-built ones do. Fine, the docs say so. AI-assisted; commands listed were executed locally.
NiiRoZz commented 2026-11-24 21:01:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-24T20:05:06Z.


ea3b6378 adds a window case: 5 streams of 96 to 160 KiB, three of them hand-built fixed blocks with matches at distance 32768 (zlib's encoder never emits it) and lengths up to 258, at exact capacity, one byte short, at inner match ends and truncated, 52 cases equal to the bit-at-a-time build and to zlib. A build that rejects distances above 32767 fails it, and the existing random, boundary and control counts are unchanged.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7143019273 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1819#issuecomment-7143019273). Originally posted by [NiiRoZz](https://github.com/NiiRoZz) on 2026-11-24T20:05:06Z. --- ea3b6378 adds a window case: 5 streams of 96 to 160 KiB, three of them hand-built fixed blocks with matches at distance 32768 (zlib's encoder never emits it) and lengths up to 258, at exact capacity, one byte short, at inner match ends and truncated, 52 cases equal to the bit-at-a-time build and to zlib. A build that rejects distances above 32767 fails it, and the existing random, boundary and control counts are unchanged.
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!1812
No description provided.