shader: harden DXIL/PSV0 validation and add unit suite for shader_profile_audit #1843

Merged
Ammmae0x merged 2 commits from upstream-pr/1850 into main 2026-11-24 21:38:05 +00:00
Ammmae0x commented 2026-11-24 19:33:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T19:33:47Z.


Change

Hardens DXIL, PSV0, disassembly, and archive/CLI input validation in tools/shader_profile_audit.py and adds a unit suite in tests/shader_profile_audit_check.py (registered in CMakeLists.txt as shader_profile_audit_check):

  • Validates stage (rejecting booleans, non-integers, and unknown stage IDs), sections mapping, DXIL presence/length (>= 24), PSV0 presence/length (>= 52, plus mesh stage byte 13, PSV size bounds, and container payload length >= 32) in classify(), raising explicit ValueError diagnostics.
  • Validates code payload type/4-byte alignment, compute group_size tuple bounds, and disassembly return shape in pixel_profiles(), compute_profiles(), and cb_diagnostic(), and guards emit=None and missing DXIL during --cb-reads diagnostics in scan().
  • Adds path/bytes validation and clean OSError/ValueError CLI exit handling in scan() and main().
  • Adds tests/shader_profile_audit_check.py covering PS (general, attribute, depth, coverage), CS (store), and MS (point mesh, indexed mesh) classification, --source-dir HLSL output, cb_diagnostic() + --cb-reads aggregation, CLI JSON execution, and negative controls for missing/truncated DXIL/PSV0, unsupported instructions, unknown stages, and empty/missing archives.

Verification

Commit: 759e9063 (rebased onto upstream/main fb8947f0)

  • cmake --preset dev -DINTAKE_PYTHON=<pe-env python>: configured cleanly (Total Tests: 1133).
  • cmake --preset release -DINTAKE_PYTHON=<pe-env python>: configured cleanly (Total Tests: 1133).
  • ctest --preset dev --no-tests=error -R "^(shader_profile_audit_check|shader_metadata_census|shader_metadata_controls)$" -E "cpu_native_archive_" -V: 3/3 passed (shader_metadata_controls, shader_metadata_census, shader_profile_audit_check).
  • ctest --preset release --no-tests=error -R "^(shader_profile_audit_check|shader_metadata_census|shader_metadata_controls)$" -E "cpu_native_archive_" -V: 3/3 passed (shader_metadata_controls, shader_metadata_census, shader_profile_audit_check).
  • <pe-env python> -B tests/shader_profile_audit_check.py -v: Ran 3 tests in 1.566s - OK.
  • <pe-env python> -B tests/form_coverage_check.py: form coverage checks passed.
  • <pe-env python> -B tests/docs_links_check.py: Self-test passed. Checked 18 Markdown files and 370 links.
  • <pe-env python> -B tests/bidi_controls_check.py: Self-test passed. Checked 1325 tracked source, script and build/CI files: no bidirectional controls.

Note: shader_profile_audit_check is a pure-Python CTest target (no compiled binary target). tests/shader_profile_audit_check.py stubs pixel_aot.disassemble and mesh_entry_aot.disassemble with synthetic DXBC/DXIL/PSV0 containers so tests run without LLVM 15.

Evidence and follow-up

All test containers and shader instruction sequences in tests/shader_profile_audit_check.py are synthetic and self-contained; no external archives, dumps, or game files 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6589800152 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1850). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T19:33:47Z. --- ## Change Hardens DXIL, PSV0, disassembly, and archive/CLI input validation in `tools/shader_profile_audit.py` and adds a unit suite in `tests/shader_profile_audit_check.py` (registered in `CMakeLists.txt` as `shader_profile_audit_check`): - Validates `stage` (rejecting booleans, non-integers, and unknown stage IDs), `sections` mapping, `DXIL` presence/length (`>= 24`), `PSV0` presence/length (`>= 52`, plus mesh stage byte `13`, PSV size bounds, and container payload length `>= 32`) in `classify()`, raising explicit `ValueError` diagnostics. - Validates code payload type/4-byte alignment, compute `group_size` tuple bounds, and disassembly return shape in `pixel_profiles()`, `compute_profiles()`, and `cb_diagnostic()`, and guards `emit=None` and missing `DXIL` during `--cb-reads` diagnostics in `scan()`. - Adds path/bytes validation and clean `OSError`/`ValueError` CLI exit handling in `scan()` and `main()`. - Adds `tests/shader_profile_audit_check.py` covering PS (`general`, `attribute`, `depth`, `coverage`), CS (`store`), and MS (`point mesh`, `indexed mesh`) classification, `--source-dir` HLSL output, `cb_diagnostic()` + `--cb-reads` aggregation, CLI JSON execution, and negative controls for missing/truncated DXIL/PSV0, unsupported instructions, unknown stages, and empty/missing archives. ## Verification Commit: `759e9063` (rebased onto `upstream/main` `fb8947f0`) - `cmake --preset dev -DINTAKE_PYTHON=<pe-env python>`: configured cleanly (Total Tests: 1133). - `cmake --preset release -DINTAKE_PYTHON=<pe-env python>`: configured cleanly (Total Tests: 1133). - `ctest --preset dev --no-tests=error -R "^(shader_profile_audit_check|shader_metadata_census|shader_metadata_controls)$" -E "cpu_native_archive_" -V`: `3/3` passed (`shader_metadata_controls`, `shader_metadata_census`, `shader_profile_audit_check`). - `ctest --preset release --no-tests=error -R "^(shader_profile_audit_check|shader_metadata_census|shader_metadata_controls)$" -E "cpu_native_archive_" -V`: `3/3` passed (`shader_metadata_controls`, `shader_metadata_census`, `shader_profile_audit_check`). - `<pe-env python> -B tests/shader_profile_audit_check.py -v`: `Ran 3 tests in 1.566s - OK`. - `<pe-env python> -B tests/form_coverage_check.py`: `form coverage checks passed`. - `<pe-env python> -B tests/docs_links_check.py`: `Self-test passed. Checked 18 Markdown files and 370 links.` - `<pe-env python> -B tests/bidi_controls_check.py`: `Self-test passed. Checked 1325 tracked source, script and build/CI files: no bidirectional controls.` Note: `shader_profile_audit_check` is a pure-Python CTest target (no compiled binary target). `tests/shader_profile_audit_check.py` stubs `pixel_aot.disassemble` and `mesh_entry_aot.disassemble` with synthetic DXBC/DXIL/PSV0 containers so tests run without LLVM 15. ## Evidence and follow-up All test containers and shader instruction sequences in `tests/shader_profile_audit_check.py` are synthetic and self-contained; no external archives, dumps, or game files 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.
tedin7 commented 2026-11-24 20:01:01 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Review of 759e9063 (base fb8947f0; origin/main is now 8e2923ae, and none of the three touched files changed on main in between). Comment only, not an approval.

Verdict

Looks sound, with one behaviour change I would fix or state before merge (P2) and a few guards that no test pins (P3). Clean-room and title-neutral: the diff and the fixtures are synthetic, with no game files, dumps or titles.

P2: unknown stage ids now change the audit report

classify() now raises ValueError("Unknown shader stage") for any stage not in STAGES ({0,1,2,5,13}). Before, every stage outside 0/5/13 returned ([], "no stage profile"). In scan() that ValueError is caught per record and counted in first_stops, so the report changes for real archives with other stage ids (hull and domain are 3 and 4).

Reproduced on this SHA with a synthetic record of stage 3:

  • origin/main (with the PR's test helpers copied in): first_stops = {'3': [('no stage profile', 1)]}
  • PR head: first_stops = {'3': [('Unknown shader stage', 1)]}

The new test only checks -1, 99 and True, so it does not show this. Either keep no stage profile for any non-negative int stage and reject only non-ints/bools, or say in the description that the report label changes and pin it with a stage-3 case.

P3: guards the suite does not pin

I removed each guard one at a time and ran tests/shader_profile_audit_check.py. These mutants survived (3 tests, OK):

  • len(psv) < 52 in classify(): the later group_size and mesh size checks already refuse the same inputs.
  • len(dxil) < 24 in classify(): no case reaches it that container_code would not refuse anyway.
  • the mesh payload len(blob) < 32 check.
  • the mesh upper bound psv_size <= len(psv) - 4.

Either add a case that only that guard rejects, or drop the guard. The mesh PSV0 size check also duplicates compute_store_aot.group_size; sharing one helper would keep them from drifting.

Bounds and pointer arithmetic

I found no out-of-bounds read, integer overflow or pointer arithmetic in the diff. All offsets (unpack_from("<I", psv), psv[28]) are behind the length checks. scan() closes the mmap only after the result is built, as before. The CLI now maps OSError/ValueError to exit 1 with a message on stderr and empty stdout.

Commands run (Linux, Python 3.14.8, pefile 2024.8.26, iced-x86 1.21.0)

  • python -B tests/shader_profile_audit_check.py -v: Ran 3 tests, OK
  • python -B tests/docs_links_check.py: Self-test passed. Checked 18 Markdown files and 370 links.
  • python -B tests/bidi_controls_check.py: Self-test passed. Checked 1325 tracked source, script and build/CI files: no bidirectional controls.
  • python -B tests/form_coverage_check.py: form coverage checks passed; tests/shader_census_check.py: OK
  • git diff --check origin/main...HEAD: clean
  • Mutations (all reverted with git checkout -- .), the 4 survivors are listed under P3. Killed by the suite: dropping the mesh PSV0 stage-byte check, accepting a zero group dimension, accepting a bool stage, dropping the pixel 4-byte alignment check.

Not run

The Windows ctest registration (shader_profile_audit_check in CMakeLists.txt) and the dev/release presets were not run; I had no Windows/MSVC environment. The one-line add_test follows the neighbouring pure-Python entries. The tests stub disassemble, so no LLVM 15 path was exercised.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046070157 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1850#pullrequestreview-6046070157). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T19:38:03Z. **Review:** COMMENTED --- Review of `759e9063` (base `fb8947f0`; `origin/main` is now `8e2923ae`, and none of the three touched files changed on main in between). Comment only, not an approval. ## Verdict Looks sound, with one behaviour change I would fix or state before merge (P2) and a few guards that no test pins (P3). Clean-room and title-neutral: the diff and the fixtures are synthetic, with no game files, dumps or titles. ## P2: unknown stage ids now change the audit report `classify()` now raises `ValueError("Unknown shader stage")` for any stage not in `STAGES` ({0,1,2,5,13}). Before, every stage outside 0/5/13 returned `([], "no stage profile")`. In `scan()` that ValueError is caught per record and counted in `first_stops`, so the report changes for real archives with other stage ids (hull and domain are 3 and 4). Reproduced on this SHA with a synthetic record of stage 3: - `origin/main` (with the PR's test helpers copied in): `first_stops = {'3': [('no stage profile', 1)]}` - PR head: `first_stops = {'3': [('Unknown shader stage', 1)]}` The new test only checks -1, 99 and True, so it does not show this. Either keep `no stage profile` for any non-negative int stage and reject only non-ints/bools, or say in the description that the report label changes and pin it with a stage-3 case. ## P3: guards the suite does not pin I removed each guard one at a time and ran `tests/shader_profile_audit_check.py`. These mutants survived (3 tests, OK): - `len(psv) < 52` in `classify()`: the later `group_size` and mesh size checks already refuse the same inputs. - `len(dxil) < 24` in `classify()`: no case reaches it that `container_code` would not refuse anyway. - the mesh payload `len(blob) < 32` check. - the mesh upper bound `psv_size <= len(psv) - 4`. Either add a case that only that guard rejects, or drop the guard. The mesh PSV0 size check also duplicates `compute_store_aot.group_size`; sharing one helper would keep them from drifting. ## Bounds and pointer arithmetic I found no out-of-bounds read, integer overflow or pointer arithmetic in the diff. All offsets (`unpack_from("<I", psv)`, `psv[28]`) are behind the length checks. `scan()` closes the mmap only after the result is built, as before. The CLI now maps `OSError`/`ValueError` to exit 1 with a message on stderr and empty stdout. ## Commands run (Linux, Python 3.14.8, pefile 2024.8.26, iced-x86 1.21.0) - `python -B tests/shader_profile_audit_check.py -v`: Ran 3 tests, OK - `python -B tests/docs_links_check.py`: Self-test passed. Checked 18 Markdown files and 370 links. - `python -B tests/bidi_controls_check.py`: Self-test passed. Checked 1325 tracked source, script and build/CI files: no bidirectional controls. - `python -B tests/form_coverage_check.py`: form coverage checks passed; `tests/shader_census_check.py`: OK - `git diff --check origin/main...HEAD`: clean - Mutations (all reverted with `git checkout -- .`), the 4 survivors are listed under P3. Killed by the suite: dropping the mesh PSV0 stage-byte check, accepting a zero group dimension, accepting a bool stage, dropping the pixel 4-byte alignment check. ## Not run The Windows ctest registration (`shader_profile_audit_check` in `CMakeLists.txt`) and the dev/release presets were not run; I had no Windows/MSVC environment. The one-line add_test follows the neighbouring pure-Python entries. The tests stub `disassemble`, so no LLVM 15 path was exercised. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-24 20:01:02 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Re-review of dffd7f56acec6c49af5691bba635b45dba6bb229 (previous review at 759e9063; one new commit on top of it).

Verdict: the P2 is closed, the P3 guards are pinned or removed, no new finding above P3.

Previous findings

  1. P2, unknown stage ids changed the audit report: closed. classify() now rejects only a bool, a non-int or a negative stage (stage < 0); any other stage not in 0/5/13 returns ([], "no stage profile") again, as on origin/main. The test now runs stages 1, 3, 4 and 99 through classify and expects no stage profile, adds a stage-3 record to the archive scan (first_stops["3"] == [("no stage profile", 1)], records["3"] == 1 in the report and in the CLI JSON), and keeps -1, True and "PS" as ValueError.
  2. P3, unpinned guards: closed. The two guards no test could reach (len(psv) < 52 for CS and MS, len(dxil) < 24) are removed. That is safe: compute_store_aot.group_size and the mesh branch both treat a PSV0 shorter than 4 bytes as size 0 and raise ValueError, the mesh branch requires 48 <= size <= len(psv) - 4 (so psv[28] is in range), and container_code already refuses a DXIL part under 24 bytes. The mesh payload guard and the PSV0 upper bound now have cases: an oversized PSV0 (struct.pack("<I", 52) plus a short body), a 1-byte PSV0, and None/b"short" as the mesh blob.
  3. P3, mesh PSV0 size check duplicates compute_store_aot.group_size: still open, unchanged (the two checks agree today).

Commands run locally on Linux (Python 3.14.8, iced-x86 installed in a scratch folder outside the repository)

  • python3 -B tests/shader_profile_audit_check.py -v: Ran 3 tests, OK.
  • tests/form_coverage_check.py: form coverage checks passed. tests/shader_census_check.py: OK.
  • python3 -B tests/docs_links_check.py (18 files, 370 links), python3 -B tests/bidi_controls_check.py (1325 files): pass. git diff --check origin/main...HEAD: clean.

Local mutations of tools/shader_profile_audit.py (each restored with git checkout -- ., tree clean afterwards)

Mutation Result
stage guard back to stage not in STAGES fails (the 1/3/4/99 cases and the stage-3 scan)
mesh payload len(blob) < 32 removed fails
mesh PSV0 upper bound <= len(psv) - 4 removed fails
mesh PSV0 stage byte check removed fails
pixel 4-byte alignment check removed fails
zero group dimension accepted fails
first_diagnostic: tokens[0] if tokens else reason reduced to tokens[0] passes

Finding

  • P3: the new empty-token fallback in first_diagnostic (an "Unsupported instruction" message with nothing after the colon) is not pinned; the mutation above survives. One case with that message would pin it.

Not run: the Windows ctest registration (shader_profile_audit_check in CMakeLists.txt) and the dev/release presets; no Windows/MSVC here. The tests stub disassemble, so no LLVM 15 path was exercised.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046271557 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1850#pullrequestreview-6046271557). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T19:51:03Z. **Review:** COMMENTED --- Re-review of `dffd7f56acec6c49af5691bba635b45dba6bb229` (previous review at `759e9063`; one new commit on top of it). **Verdict: the P2 is closed, the P3 guards are pinned or removed, no new finding above P3.** **Previous findings** 1. **P2, unknown stage ids changed the audit report: closed.** `classify()` now rejects only a bool, a non-int or a negative stage (`stage < 0`); any other stage not in 0/5/13 returns `([], "no stage profile")` again, as on `origin/main`. The test now runs stages 1, 3, 4 and 99 through `classify` and expects `no stage profile`, adds a stage-3 record to the archive scan (`first_stops["3"] == [("no stage profile", 1)]`, `records["3"] == 1` in the report and in the CLI JSON), and keeps -1, True and "PS" as `ValueError`. 2. **P3, unpinned guards: closed.** The two guards no test could reach (`len(psv) < 52` for CS and MS, `len(dxil) < 24`) are removed. That is safe: `compute_store_aot.group_size` and the mesh branch both treat a PSV0 shorter than 4 bytes as size 0 and raise `ValueError`, the mesh branch requires `48 <= size <= len(psv) - 4` (so `psv[28]` is in range), and `container_code` already refuses a DXIL part under 24 bytes. The mesh payload guard and the PSV0 upper bound now have cases: an oversized PSV0 (`struct.pack("<I", 52)` plus a short body), a 1-byte PSV0, and `None`/`b"short"` as the mesh blob. 3. **P3, mesh PSV0 size check duplicates `compute_store_aot.group_size`: still open**, unchanged (the two checks agree today). **Commands run locally on Linux** (Python 3.14.8, iced-x86 installed in a scratch folder outside the repository) - `python3 -B tests/shader_profile_audit_check.py -v`: Ran 3 tests, OK. - `tests/form_coverage_check.py`: form coverage checks passed. `tests/shader_census_check.py`: OK. - `python3 -B tests/docs_links_check.py` (18 files, 370 links), `python3 -B tests/bidi_controls_check.py` (1325 files): pass. `git diff --check origin/main...HEAD`: clean. **Local mutations of `tools/shader_profile_audit.py` (each restored with `git checkout -- .`, tree clean afterwards)** | Mutation | Result | |---|---| | stage guard back to `stage not in STAGES` | fails (the 1/3/4/99 cases and the stage-3 scan) | | mesh payload `len(blob) < 32` removed | fails | | mesh PSV0 upper bound `<= len(psv) - 4` removed | fails | | mesh PSV0 stage byte check removed | fails | | pixel 4-byte alignment check removed | fails | | zero group dimension accepted | fails | | `first_diagnostic`: `tokens[0] if tokens else reason` reduced to `tokens[0]` | **passes** | **Finding** - P3: the new empty-token fallback in `first_diagnostic` (an "Unsupported instruction" message with nothing after the colon) is not pinned; the mutation above survives. One case with that message would pin it. **Not run:** the Windows ctest registration (`shader_profile_audit_check` in `CMakeLists.txt`) and the dev/release presets; no Windows/MSVC here. The tests stub `disassemble`, so no LLVM 15 path was exercised. AI-assisted; commands listed were executed locally.
Ammmae0x commented 2026-11-24 20:01:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T19:42:21Z.


Updated tools/shader_profile_audit.py and tests/shader_profile_audit_check.py (commit dffd7f56) to preserve the "no stage profile" classification for non-negative integer shader stages (while rejecting negative, boolean, or non-integer stages), remove redundant length guards already covered by container_code/group_size/PSV0 bounds checks, and pin the PSV0 upper-bound, short-buffer, stage-3 scan(), and truncated mesh container controls. Verified with tests/shader_profile_audit_check.py, tests/shader_census_check.py, tests/form_coverage_check.py, tests/docs_links_check.py, tests/bidi_controls_check.py, and shader_profile_audit_check in dev and release. AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7142473465 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1850#issuecomment-7142473465). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T19:42:21Z. --- Updated `tools/shader_profile_audit.py` and `tests/shader_profile_audit_check.py` (commit `dffd7f56`) to preserve the `"no stage profile"` classification for non-negative integer shader stages (while rejecting negative, boolean, or non-integer stages), remove redundant length guards already covered by `container_code`/`group_size`/`PSV0` bounds checks, and pin the `PSV0` upper-bound, short-buffer, stage-3 `scan()`, and truncated mesh container controls. Verified with `tests/shader_profile_audit_check.py`, `tests/shader_census_check.py`, `tests/form_coverage_check.py`, `tests/docs_links_check.py`, `tests/bidi_controls_check.py`, and `shader_profile_audit_check` in `dev` and `release`. AI-assisted.
upstream-sync closed this pull request 2026-11-24 22:00:47 +00:00
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!1843
No description provided.