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

Merged
IlyassRS3 merged 1 commit from upstream-pr/1454 into main 2026-11-28 10:15:22 +00:00
IlyassRS3 commented 2026-11-25 00:31:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T00:31:45Z.


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).
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5482771815 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1454). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T00:31:45Z. --- 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] commented 2026-11-25 00:32:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T00:32:00Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6049691087 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1454#pullrequestreview-6049691087). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T00:32:00Z. **Review:** COMMENTED --- 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 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T10:15:17Z.


Red/green: the PR's tests fail on main 4938673 and pass on head 4f56b089d9 (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)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7237775635 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1454#issuecomment-7237775635). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T10:15:17Z. --- 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 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
ProjectLeonida/mandy!1454
No description provided.