psocache_inspect: guard --entry bounds, zero-meta references, and header string slots (PSOCACHE-INSPECT-BOUNDS-AND-REFS) #1454

Merged
leonida/mandy
quietfold merged 1 commit from fix/psocache-inspect-bounds-and-refs into main 2026-11-28 10:15:22 +00:00

Pull request successfully merged and closed

quietfold commented 2026-11-25 00:31:45 +00:00

Claimed in the coordination issue as: PSOCACHE-INSPECT-BOUNDS-AND-REFS

Summary

Hardens tools/psocache_inspect.py against malformed/corrupt PSO cache inputs and zero-entry meta-index edge cases:

  1. --entry bounds & truncated-meta handling (inspect(), parse_chunks()):
    • Previously, inspect(..., entry=N) passed entries[N] straight to describe_entry() without checking a + b <= meta_size, raising an uncaught struct.error when an index entry pointed past EOF or silently decoding bytes from code_blob when it spilled past meta_blob. Now reports "entry offset 0x... size ...: points past the 0x...-byte meta blob" cleanly.
    • When meta_index or meta_blob does not fit in a truncated file, --entry is now rejected with ValueError instead of being silently ignored (meta_entry: None).
    • Rejects negative entry and boolean entry/limit arguments upfront, and bounds-checks (base, start, size) inside parse_chunks().
  2. PSO record reference validation when meta_index == 0:
    • Previously if ... and counts["meta_index"]: skipped reference validation completely when meta_index == 0, letting non-hole graphics/compute references pass with "verdict": "model holds" (exit 0). Now runs whenever graphics, compute, or meta_index is non-zero.
  3. Header string slot bounds (0x04..0x24, 0x24..0x48):
    • Previously m.find(b"\x00", off, HEADER_SIZE) scanned past each string's fixed slot into string 1 / HASH_OFF (0x48) / counts when unterminated. Now bounds each scan to its slot (STRING_BOUNDS = ((0x04, 0x24), (0x24, HASH_OFF))) and records a problem if unterminated.

Testing

  • AI-assisted: yes
  • Host: macOS Apple M5 (arm64), Python 3.14 (CPU-only, synthetic cache fixtures only, no game data)
  • python3 -m compileall -q make_game.py tools tests -> clean
  • python3 -I scripts/check_no_game_data.py tools/psocache_inspect.py tests/test_psocache_inspect.py -> clean
  • python3 -I tests/test_psocache_inspect.py -v -> 19/19 OK (5 new tests)
  • python3 -I tests/test_text_write_line_endings.py -> 3/3 OK
  • python3 -I tests/test_ci_registration.py -> 16/16 OK
  • Mutation proof (running the new tests/test_psocache_inspect.py on unpatched upstream/main b4b7756e8): FAILED (failures=4, errors=2) (reproducing struct.error on OOB --entry and parse_chunks, silent --entry ignore on truncated meta, skipped references on meta_index == 0, and header string bleed lengths 34 / 36).
Claimed in the coordination issue as: PSOCACHE-INSPECT-BOUNDS-AND-REFS ## Summary Hardens `tools/psocache_inspect.py` against malformed/corrupt PSO cache inputs and zero-entry meta-index edge cases: 1. **`--entry` bounds & truncated-meta handling (`inspect()`, `parse_chunks()`)**: - Previously, `inspect(..., entry=N)` passed `entries[N]` straight to `describe_entry()` without checking `a + b <= meta_size`, raising an uncaught `struct.error` when an index entry pointed past EOF or silently decoding bytes from `code_blob` when it spilled past `meta_blob`. Now reports `"entry offset 0x... size ...: points past the 0x...-byte meta blob"` cleanly. - When `meta_index` or `meta_blob` does not fit in a truncated file, `--entry` is now rejected with `ValueError` instead of being silently ignored (`meta_entry: None`). - Rejects negative `entry` and boolean `entry`/`limit` arguments upfront, and bounds-checks `(base, start, size)` inside `parse_chunks()`. 2. **PSO record reference validation when `meta_index == 0`**: - Previously `if ... and counts["meta_index"]:` skipped reference validation completely when `meta_index == 0`, letting non-hole graphics/compute references pass with `"verdict": "model holds"` (exit 0). Now runs whenever `graphics`, `compute`, or `meta_index` is non-zero. 3. **Header string slot bounds (`0x04..0x24`, `0x24..0x48`)**: - Previously `m.find(b"\x00", off, HEADER_SIZE)` scanned past each string's fixed slot into string 1 / `HASH_OFF` (`0x48`) / counts when unterminated. Now bounds each scan to its slot (`STRING_BOUNDS = ((0x04, 0x24), (0x24, HASH_OFF))`) and records a problem if unterminated. ## Testing - **AI-assisted**: yes - **Host**: macOS Apple M5 (`arm64`), Python 3.14 (CPU-only, synthetic cache fixtures only, no game data) - `python3 -m compileall -q make_game.py tools tests` -> clean - `python3 -I scripts/check_no_game_data.py tools/psocache_inspect.py tests/test_psocache_inspect.py` -> clean - `python3 -I tests/test_psocache_inspect.py -v` -> **19/19 OK** (5 new tests) - `python3 -I tests/test_text_write_line_endings.py` -> **3/3 OK** - `python3 -I tests/test_ci_registration.py` -> **16/16 OK** - **Mutation proof** (running the new `tests/test_psocache_inspect.py` on unpatched `upstream/main` `b4b7756e8`): `FAILED (failures=4, errors=2)` (reproducing `struct.error` on OOB `--entry` and `parse_chunks`, silent `--entry` ignore on truncated meta, skipped `references` on `meta_index == 0`, and header string bleed lengths `34` / `36`).
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 00:32:00 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
MoonEyes commented 2026-11-28 10:15:17 +00:00

Red/green: the PR's tests fail on main 4938673 and pass on head 4f56b089d92a (Linux, Python 3.13.18).
Targeted batch of 11 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13.
AI-assisted (Claude Code, Claude Opus 5.5)

Red/green: the PR's tests fail on main 4938673 and pass on head 4f56b089d92a (Linux, Python 3.13.18). Targeted batch of 11 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No description provided.