shader: extract main(argv), bounds checks, and struct/zlib guards in shader_census with CLI suite #2429

Merged
Ammmae0x merged 3 commits from upstream-pr/2437 into main 2026-11-25 23:57:57 +00:00
Ammmae0x commented 2026-11-25 16:43:17 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T16:43:17Z.


Change

Extract a testable main(argv=None) -> int entrypoint, upfront directory and --code-base bounds validation, clean 0-byte mapping, and struct.error/zlib.error guards in tools/shader_census.py, together with a standalone pure-Python CLI test suite registered in CMakeLists.txt (shader_census_cli_check).

  • tools/shader_census.py:
    • Accept argv: list[str] | None = None in main, forward argv to parser.parse_args(argv), return 0 on success, and invoke raise SystemExit(main()) under __main__.
    • Extract _map_input(stack, path) to reject directory paths cleanly (ValueError) and return b"" on 0-byte files without hitting mmap ValueError.
    • Validate non-negative --code-base via parser.error("--code-base must be non-negative") (exit 2) alongside the paired --cache-blob / --code-base check.
    • Guard (OSError, ValueError, struct.error, zlib.error) via parser.exit(1, f"Shader census: {error}\n"), preserving the exact 'Shader census:' stderr prefix and exit codes expected by tests/shader_census_check.py with zero raw tracebacks.
  • tests/shader_census_cli_check.py: standalone unittest suite using synthetic archive and PS/9 --cache-blob fixtures covering argv forwarding, decimal and hex --code-base, exact JSON schema, 0-byte archive and --cache-blob handling, and negative/mutation controls (directory/missing archive or --cache-blob, mismatched or negative --code-base exit 2, malformed/truncated caches, corrupt deflate payloads, and direct struct.error / zlib.error guards).
  • CMakeLists.txt: register shader_census_cli_check immediately after shader_metadata_census (tests/shader_census_check.py left untouched).

Verification

Verified on commit 3ce3d413 (based on upstream/main b442b57f) using out/pe-env/Scripts/python.exe -B:

  • out/pe-env/Scripts/python.exe -B tests/shader_census_cli_check.py -v -> Ran 5 tests in 0.318s, OK (exit 0).
  • out/pe-env/Scripts/python.exe -B tests/shader_census_check.py -v -> Ran 9 tests in 0.857s, OK (exit 0).
  • out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py -> form coverage checks passed (exit 0).
  • out/pe-env/Scripts/python.exe -B tests/docs_links_check.py -> Self-test passed. Checked 19 Markdown files and 465 links. (exit 0).
  • out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py -> Self-test passed. Checked 1726 tracked source, script and build/CI files: no bidirectional controls. (exit 0).
  • Spot mutation audit on tools/shader_census.py: 8/8 mutations killed (argv forwarding, is_dir(), 0-byte mmap guard, paired --cache-blob/--code-base check, --code-base < 0, struct.error guard, zlib.error guard, integer 0 return).

Evidence and follow-up

All fixtures in tests/shader_census_cli_check.py are synthetic non-executable framing byte arrays generated in-memory under the repository MIT license; no external binaries or game files are used. Contributors can run the suite directly via out/pe-env/Scripts/python.exe -B tests/shader_census_cli_check.py or through CTest (shader_census_cli_check).

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:6609198627 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2437). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T16:43:17Z. --- ## Change Extract a testable `main(argv=None) -> int` entrypoint, upfront directory and `--code-base` bounds validation, clean 0-byte mapping, and `struct.error`/`zlib.error` guards in `tools/shader_census.py`, together with a standalone pure-Python CLI test suite registered in `CMakeLists.txt` (`shader_census_cli_check`). - `tools/shader_census.py`: - Accept `argv: list[str] | None = None` in `main`, forward `argv` to `parser.parse_args(argv)`, return `0` on success, and invoke `raise SystemExit(main())` under `__main__`. - Extract `_map_input(stack, path)` to reject directory paths cleanly (`ValueError`) and return `b""` on 0-byte files without hitting `mmap` `ValueError`. - Validate non-negative `--code-base` via `parser.error("--code-base must be non-negative")` (exit 2) alongside the paired `--cache-blob` / `--code-base` check. - Guard `(OSError, ValueError, struct.error, zlib.error)` via `parser.exit(1, f"Shader census: {error}\n")`, preserving the exact `'Shader census:'` stderr prefix and exit codes expected by `tests/shader_census_check.py` with zero raw tracebacks. - `tests/shader_census_cli_check.py`: standalone unittest suite using synthetic archive and PS/9 `--cache-blob` fixtures covering `argv` forwarding, decimal and hex `--code-base`, exact JSON schema, 0-byte archive and `--cache-blob` handling, and negative/mutation controls (directory/missing archive or `--cache-blob`, mismatched or negative `--code-base` exit 2, malformed/truncated caches, corrupt deflate payloads, and direct `struct.error` / `zlib.error` guards). - `CMakeLists.txt`: register `shader_census_cli_check` immediately after `shader_metadata_census` (`tests/shader_census_check.py` left untouched). ## Verification Verified on commit `3ce3d413` (based on `upstream/main` `b442b57f`) using `out/pe-env/Scripts/python.exe -B`: - `out/pe-env/Scripts/python.exe -B tests/shader_census_cli_check.py -v` -> `Ran 5 tests in 0.318s`, `OK` (exit 0). - `out/pe-env/Scripts/python.exe -B tests/shader_census_check.py -v` -> `Ran 9 tests in 0.857s`, `OK` (exit 0). - `out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py` -> `form coverage checks passed` (exit 0). - `out/pe-env/Scripts/python.exe -B tests/docs_links_check.py` -> `Self-test passed. Checked 19 Markdown files and 465 links.` (exit 0). - `out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` -> `Self-test passed. Checked 1726 tracked source, script and build/CI files: no bidirectional controls.` (exit 0). - Spot mutation audit on `tools/shader_census.py`: 8/8 mutations killed (`argv` forwarding, `is_dir()`, 0-byte mmap guard, paired `--cache-blob`/`--code-base` check, `--code-base < 0`, `struct.error` guard, `zlib.error` guard, integer `0` return). ## Evidence and follow-up All fixtures in `tests/shader_census_cli_check.py` are synthetic non-executable framing byte arrays generated in-memory under the repository MIT license; no external binaries or game files are used. Contributors can run the suite directly via `out/pe-env/Scripts/python.exe -B tests/shader_census_cli_check.py` or through CTest (`shader_census_cli_check`). ## 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-25 19:03:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T18:24:07Z.

Review: COMMENTED


Review of head 3ce3d413 (Linux-only checks; I did not run any Windows tests, and the PR shows no author Windows results).

Findings

  • P2, tests/shader_census_cli_check.py:208-212: the --code-base -0x10 (two separate arguments) case asserts the stderr fragment expected one argument. That text comes from argparse treating -0x10 as an option flag, which depends on the Python version. On 3.11, 3.12 and 3.13 the test passes. On 3.14.8 argparse accepts -0x10 as the value, so the run reaches the new --code-base must be non-negative error. The test then fails with AssertionError: 'expected one argument' not found. The behavior under test is fine (exit 2 either way). Suggested fix: accept either message, or assert only exit code 2 for that case. As written, the new CTest shader_census_cli_check will fail on any runner using Python 3.14.
  • Nit: the title says "bounds checks", but the only new bound is --code-base < 0. Not a blocker.

What I checked

  • The diff is small and additive: tools/shader_census.py (_map_input helper, main(argv) -> int, catching struct.error and zlib.error), one new CTest entry in CMakeLists.txt, and a new CLI test.
  • No other open or merged PR touches shader_census.
  • The existing tests/shader_census_check.py passes on 3.14.8. The new CLI test passes on 3.13, 3.12 and 3.11 and fails on 3.14.8 (see P2).
  • Mutation 1: removing struct.error, zlib.error from the except makes the CLI test fail (1 failure, 1 error). The test guards that guard.
  • Mutation 2: weakening the code_base < 0 check makes 2 CLI tests fail. The test guards that check too.
  • Both mutations were reverted with git checkout -- ..
  • git diff --check origin/main...HEAD, tests/docs_links_check.py and tests/bidi_controls_check.py are clean.

Commands run: python3 tests/shader_census_cli_check.py (on python3 3.14.8, 3.13, 3.12 and 3.11), python3 tests/shader_census_check.py, python3 tests/docs_links_check.py, python3 tests/bidi_controls_check.py, git diff --check origin/main...HEAD, two sed mutations, git checkout -- ..

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6062831416 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2437#pullrequestreview-6062831416). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T18:24:07Z. **Review:** COMMENTED --- Review of head 3ce3d413 (Linux-only checks; I did not run any Windows tests, and the PR shows no author Windows results). Findings - P2, `tests/shader_census_cli_check.py:208-212`: the `--code-base -0x10` (two separate arguments) case asserts the stderr fragment `expected one argument`. That text comes from argparse treating `-0x10` as an option flag, which depends on the Python version. On 3.11, 3.12 and 3.13 the test passes. On 3.14.8 argparse accepts `-0x10` as the value, so the run reaches the new `--code-base must be non-negative` error. The test then fails with `AssertionError: 'expected one argument' not found`. The behavior under test is fine (exit 2 either way). Suggested fix: accept either message, or assert only exit code 2 for that case. As written, the new CTest `shader_census_cli_check` will fail on any runner using Python 3.14. - Nit: the title says "bounds checks", but the only new bound is `--code-base < 0`. Not a blocker. What I checked - The diff is small and additive: `tools/shader_census.py` (`_map_input` helper, `main(argv) -> int`, catching `struct.error` and `zlib.error`), one new CTest entry in `CMakeLists.txt`, and a new CLI test. - No other open or merged PR touches `shader_census`. - The existing `tests/shader_census_check.py` passes on 3.14.8. The new CLI test passes on 3.13, 3.12 and 3.11 and fails on 3.14.8 (see P2). - Mutation 1: removing `struct.error, zlib.error` from the `except` makes the CLI test fail (1 failure, 1 error). The test guards that guard. - Mutation 2: weakening the `code_base < 0` check makes 2 CLI tests fail. The test guards that check too. - Both mutations were reverted with `git checkout -- .`. - `git diff --check origin/main...HEAD`, `tests/docs_links_check.py` and `tests/bidi_controls_check.py` are clean. Commands run: `python3 tests/shader_census_cli_check.py` (on python3 3.14.8, 3.13, 3.12 and 3.11), `python3 tests/shader_census_check.py`, `python3 tests/docs_links_check.py`, `python3 tests/bidi_controls_check.py`, `git diff --check origin/main...HEAD`, two `sed` mutations, `git checkout -- .`. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 21:00:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T20:07:40Z.

Review: COMMENTED


Review of head a5679cee (tooling only: tools/shader_census.py, new CLI suite, CTest registration). One finding to fix before merge; the rest looks sound.

Finding (test is tied to the argparse version):

  • tests/shader_census_cli_check.py:208-212 runs --code-base -0x10 (two separate arguments) and expects exit 2 with the text expected one argument. That message is standard-library behavior, and it differs by Python version. On Python 3.12 (the version CI pins in .github/workflows/windows.yml) it matches. On Python 3.14.8 argparse accepts -0x10 as the value, so the new check --code-base must be non-negative fires instead, and test_mismatched_and_negative_code_base_exit_2 fails on an unmodified head.
  • Fix: delete that one assertion. The --code-base=-0x10 and -1 cases just above already guard the real behavior (negative value -> exit 2 with non-negative). Or accept either fragment.

What else I checked:

  • main(argv) returns an int and is wrapped in SystemExit(main()). struct.error and zlib.error now become exit 1 with Shader census: and no traceback. The directory guard is shared for both inputs in _map_input. Negative --code-base exits 2.
  • Behavior on valid input is unchanged.
  • shader_census_cli_check is registered next to shader_metadata_census in CMakeLists.txt.
  • Mutations: removing struct.error, zlib.error from the except clause makes the suite fail (1 failure, 1 error). Changing the error exit code from 1 to 0 makes it fail (4 failures). So the suite guards the new behavior.
  • No other open or merged PR covers this slice (searched shader_census).
  • The other 4 tests in the suite pass on 3.14.8 as well.

Commands run locally on the exact head:

  • python3 -B tests/shader_census_cli_check.py -> 1 failure on 3.14.8 (the finding above)
  • python3 tests/shader_census_check.py -> OK
  • python3 tests/docs_links_check.py -> OK
  • python3 tests/bidi_controls_check.py -> OK
  • git diff --check origin/main...HEAD -> clean
  • the two mutations above, reverted with git checkout -- .

I did not merge. I will not run Win2025 until the test no longer depends on the Python version.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6064228531 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2437#pullrequestreview-6064228531). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T20:07:40Z. **Review:** COMMENTED --- Review of head a5679cee (tooling only: `tools/shader_census.py`, new CLI suite, CTest registration). One finding to fix before merge; the rest looks sound. Finding (test is tied to the argparse version): - `tests/shader_census_cli_check.py:208-212` runs `--code-base -0x10` (two separate arguments) and expects exit 2 with the text `expected one argument`. That message is standard-library behavior, and it differs by Python version. On Python 3.12 (the version CI pins in `.github/workflows/windows.yml`) it matches. On Python 3.14.8 argparse accepts `-0x10` as the value, so the new check `--code-base must be non-negative` fires instead, and `test_mismatched_and_negative_code_base_exit_2` fails on an unmodified head. - Fix: delete that one assertion. The `--code-base=-0x10` and `-1` cases just above already guard the real behavior (negative value -> exit 2 with `non-negative`). Or accept either fragment. What else I checked: - `main(argv)` returns an int and is wrapped in `SystemExit(main())`. `struct.error` and `zlib.error` now become exit 1 with `Shader census:` and no traceback. The directory guard is shared for both inputs in `_map_input`. Negative `--code-base` exits 2. - Behavior on valid input is unchanged. - `shader_census_cli_check` is registered next to `shader_metadata_census` in `CMakeLists.txt`. - Mutations: removing `struct.error, zlib.error` from the except clause makes the suite fail (1 failure, 1 error). Changing the error exit code from 1 to 0 makes it fail (4 failures). So the suite guards the new behavior. - No other open or merged PR covers this slice (searched `shader_census`). - The other 4 tests in the suite pass on 3.14.8 as well. Commands run locally on the exact head: - `python3 -B tests/shader_census_cli_check.py` -> 1 failure on 3.14.8 (the finding above) - `python3 tests/shader_census_check.py` -> OK - `python3 tests/docs_links_check.py` -> OK - `python3 tests/bidi_controls_check.py` -> OK - `git diff --check origin/main...HEAD` -> clean - the two mutations above, reverted with `git checkout -- .` I did not merge. I will not run Win2025 until the test no longer depends on the Python version. AI-assisted; commands listed were executed locally.
Ammmae0x commented 2026-11-25 21:01:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T20:10:45Z.


Addressed in 671d1981:

  • Updated _assert_main_fails and test_mismatched_and_negative_code_base_exit_2 in tests/shader_census_cli_check.py so --code-base -0x10 (two separate args) still asserts exit 2 with no traceback while accepting either "expected one argument" (Python <= 3.13) or "non-negative" (Python 3.14+ argparse accepting -0x10 as a value).

Verified on 671d1981 (Python 3.12.8):

  • out/pe-env/Scripts/python.exe -B tests/shader_census_cli_check.py: 5/5 passed (plus simulated Python 3.14 --code-base=-0x10 path verified, and code_base < 0 spot-mutation caught).
  • out/pe-env/Scripts/python.exe -B tests/shader_census_check.py: 9/9 passed.
  • ctest --preset dev -R "^shader_(census_cli_check|metadata_census)$" --output-on-failure: 2/2 passed (2.34 s).
  • ctest --preset release -R "^shader_(census_cli_check|metadata_census)$" --output-on-failure: 2/2 passed (2.58 s).
  • tests/docs_links_check.py and tests/bidi_controls_check.py: both passed.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7173296616 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2437#issuecomment-7173296616). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T20:10:45Z. --- Addressed in `671d1981`: - Updated `_assert_main_fails` and `test_mismatched_and_negative_code_base_exit_2` in `tests/shader_census_cli_check.py` so `--code-base -0x10` (two separate args) still asserts exit `2` with no traceback while accepting either `"expected one argument"` (Python <= 3.13) or `"non-negative"` (Python 3.14+ argparse accepting `-0x10` as a value). Verified on `671d1981` (Python 3.12.8): - `out/pe-env/Scripts/python.exe -B tests/shader_census_cli_check.py`: 5/5 passed (plus simulated Python 3.14 `--code-base=-0x10` path verified, and `code_base < 0` spot-mutation caught). - `out/pe-env/Scripts/python.exe -B tests/shader_census_check.py`: 9/9 passed. - `ctest --preset dev -R "^shader_(census_cli_check|metadata_census)$" --output-on-failure`: 2/2 passed (2.34 s). - `ctest --preset release -R "^shader_(census_cli_check|metadata_census)$" --output-on-failure`: 2/2 passed (2.58 s). - `tests/docs_links_check.py` and `tests/bidi_controls_check.py`: both passed. AI-assisted.
upstream-sync closed this pull request 2026-11-26 00:00:44 +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!2429
No description provided.