Validate shader records and output stems in extract_shaders #191

Merged
alexander-src merged 2 commits from upstream-pr/192 into main 2026-11-21 18:41:43 +00:00
alexander-src commented 2026-11-21 18:12:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by alexander-src on 2026-11-21T18:12:18Z.


Change

Harden record scanning, XBCH validation, and output filename disambiguation in tools/extract_shaders.py:

  • Validate candidate records before advancing cursor or incrementing total_found: enforce 32 <= decoded <= 16 << 20, inflate raw DEFLATE via bounded zlib.decompressobj(-15) (decoded + 1) checking len(blob) == decoded and stream.eof, and require valid parts(blob) plus a 24-byte DXIL header (matching tools/resource_inventory.py::scene_records). Previously, a false-positive ASCII header with matching stored == repeated jumped cursor past up to 8 MiB of valid shader records and inflated total_found.
  • Validate XBCH entry bounds (offset >= table_idx + 12 + count * 12) and decoded FSSM payload length against the XBCH header size field in extract_native_rdna2_code using bounded zlib.decompressobj(15) (matching tools/pixel_aot.py).
  • Guard 0-byte archives before calling mmap.mmap so empty files emit an empty manifest.json instead of raising ValueError.
  • Track used output stems per stage case-insensitively so disambiguated names (PS_Dup, PS_Dup_1) and case variants (ps_dup on Windows) do not overwrite earlier outputs, avoid writing .dxbc twice under --format both when native code is absent, and expose all STAGE_NAMES stages in --stage.
  • Add 4 source-owned synthetic regression tests to tests/rdna2_tools_check.py.

Verification

Run on Linux x86_64 (python3 3.14) at branch HEAD (against main ccfa703):

  • python3 -B tests/rdna2_tools_check.py -> Ran 16 tests in 0.339s : OK (skipped=1) (test_llvm_comparison skipped because LLVM_LIB is unset; all 15 non-LLVM tests passed).
  • Full python-linux suite (script_state_check.py, bcpack_check.py, resource_ranges_check.py, dxcontainer_part_offsets_check.py, layout_chunks_check.py, intake/paths_check.py, shader_probe_check.py, rdna2_tools_check.py) + tests/gpu_report_format_check.py: all passed.
  • Negative control: running the 4 new tests against main:tools/extract_shaders.py produced 3 failures and 1 error (ValueError: cannot mmap an empty file, skipped valid record after false header, colliding PS_Dup_1.bin / ps_dup.bin outputs, and accepted mismatched XBCH size).
  • Not run locally: Windows MSVC/D3D12 build-and-test job and Ubuntu llvm-15 job (missing Windows host and llvm-15 package locally; covered by CI).

Evidence and follow-up

All new fixtures in tests/rdna2_tools_check.py are synthetic byte sequences constructed in-memory by the existing envelope() helper (same MIT license as the repository). No game files or external dumps were used.

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.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: yes.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6532583678 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/192). Originally posted by [alexander-src](https://github.com/alexander-src) on 2026-11-21T18:12:18Z. --- ## Change Harden record scanning, `XBCH` validation, and output filename disambiguation in `tools/extract_shaders.py`: - Validate candidate records before advancing `cursor` or incrementing `total_found`: enforce `32 <= decoded <= 16 << 20`, inflate raw DEFLATE via bounded `zlib.decompressobj(-15)` (`decoded + 1`) checking `len(blob) == decoded and stream.eof`, and require valid `parts(blob)` plus a 24-byte `DXIL` header (matching `tools/resource_inventory.py::scene_records`). Previously, a false-positive ASCII header with matching `stored == repeated` jumped `cursor` past up to 8 MiB of valid shader records and inflated `total_found`. - Validate `XBCH` entry bounds (`offset >= table_idx + 12 + count * 12`) and decoded `FSSM` payload length against the `XBCH` header size field in `extract_native_rdna2_code` using bounded `zlib.decompressobj(15)` (matching `tools/pixel_aot.py`). - Guard 0-byte archives before calling `mmap.mmap` so empty files emit an empty `manifest.json` instead of raising `ValueError`. - Track used output stems per stage case-insensitively so disambiguated names (`PS_Dup`, `PS_Dup_1`) and case variants (`ps_dup` on Windows) do not overwrite earlier outputs, avoid writing `.dxbc` twice under `--format both` when native code is absent, and expose all `STAGE_NAMES` stages in `--stage`. - Add 4 source-owned synthetic regression tests to `tests/rdna2_tools_check.py`. ## Verification Run on Linux x86_64 (`python3 3.14`) at branch HEAD (against `main` `ccfa703`): - `python3 -B tests/rdna2_tools_check.py` -> `Ran 16 tests in 0.339s : OK (skipped=1)` (`test_llvm_comparison` skipped because `LLVM_LIB` is unset; all 15 non-LLVM tests passed). - Full `python-linux` suite (`script_state_check.py`, `bcpack_check.py`, `resource_ranges_check.py`, `dxcontainer_part_offsets_check.py`, `layout_chunks_check.py`, `intake/paths_check.py`, `shader_probe_check.py`, `rdna2_tools_check.py`) + `tests/gpu_report_format_check.py`: all passed. - Negative control: running the 4 new tests against `main:tools/extract_shaders.py` produced 3 failures and 1 error (`ValueError: cannot mmap an empty file`, skipped valid record after false header, colliding `PS_Dup_1.bin` / `ps_dup.bin` outputs, and accepted mismatched `XBCH` size). - Not run locally: Windows MSVC/D3D12 `build-and-test` job and Ubuntu `llvm-15` job (missing Windows host and `llvm-15` package locally; covered by CI). ## Evidence and follow-up All new fixtures in `tests/rdna2_tools_check.py` are synthetic byte sequences constructed in-memory by the existing `envelope()` helper (same MIT license as the repository). No game files or external dumps were used. ## 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. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted: yes.
upstream-sync closed this pull request 2026-11-24 06:41:45 +00:00
R0Y00 commented 2026-11-24 06:47:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T18:14:10Z.


The new check defers cursor advancement until after a valid DXIL record, but stream.eof can also be true with stream.unused_data still inside stored. In that case cursor = start_payload + stored still skips the following valid record. Please cover a valid raw-DEFLATE stream followed by another envelope inside an oversized claimed span, and ensure that second record is found. If the format permits padding, bound cursor advancement to the bytes actually validated.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067103155 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/192#issuecomment-7067103155). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T18:14:10Z. --- The new check defers cursor advancement until after a valid DXIL record, but `stream.eof` can also be true with `stream.unused_data` still inside `stored`. In that case `cursor = start_payload + stored` still skips the following valid record. Please cover a valid raw-DEFLATE stream followed by another envelope inside an oversized claimed span, and ensure that second record is found. If the format permits padding, bound cursor advancement to the bytes actually validated.
alexander-src commented 2026-11-24 06:47:49 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by alexander-src on 2026-11-21T18:32:00Z.


Good catch, fixed in 671a9e5. extract_shaders() now advances cursor by stored - len(stream.unused_data) so it only steps past the validated raw-DEFLATE bytes and still scans any trailing data inside an oversized claimed span. Added test_oversized_claimed_span_with_unused_data_does_not_skip_following_record in tests/rdna2_tools_check.py (confirmed it fails on e9e6118 and passes on 671a9e5, 17 tests ran, 16 passed, 1 skipped without LLVM_LIB).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067324058 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/192#issuecomment-7067324058). Originally posted by [alexander-src](https://github.com/alexander-src) on 2026-11-21T18:32:00Z. --- Good catch, fixed in `671a9e5`. `extract_shaders()` now advances `cursor` by `stored - len(stream.unused_data)` so it only steps past the validated raw-DEFLATE bytes and still scans any trailing data inside an oversized claimed span. Added `test_oversized_claimed_span_with_unused_data_does_not_skip_following_record` in `tests/rdna2_tools_check.py` (confirmed it fails on `e9e6118` and passes on `671a9e5`, 17 tests ran, 16 passed, 1 skipped without `LLVM_LIB`).
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!191
No description provided.