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

Open
github-bot wants to merge 1 commit from github-bot/gh-1454 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 00:31 UTC · original
IlyassRS3:fix/psocache-inspect-bounds-and-refs → main · +139 −8 · 2 files · 1 commits · open

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).
**@⁠IlyassRS3** on GitHub · 2026-11-25 00:31 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1454) `IlyassRS3:fix/psocache-inspect-bounds-and-refs` → `main` · +139 −8 · 2 files · 1 commits · open 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`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:51:58 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 00:32 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 00:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1454#pullrequestreview-6049691087) · **reviewed** 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1454/head:github-bot/gh-1454'
git switch 'github-bot/gh-1454'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1454'
git switch 'github-bot/gh-1454'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1454'
git switch 'github-bot/gh-1454'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1454'
git switch 'main'
git merge --squash 'github-bot/gh-1454'
git switch 'main'
git merge --ff-only 'github-bot/gh-1454'
git switch 'main'
git merge 'github-bot/gh-1454'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.