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

Open
opened 2026-11-26 20:21:24 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1454
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/psocache-inspect-bounds-and-refs


📝 Commits (1)

  • 4f56b08 psocache_inspect: guard --entry bounds, zero-meta references, and header string slots

📊 Changes

2 files changed (+139 additions, -8 deletions)

View changed files

📝 tests/test_psocache_inspect.py (+109 -0)
📝 tools/psocache_inspect.py (+30 -8)

📄 Description

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).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1454 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/psocache-inspect-bounds-and-refs` --- ### 📝 Commits (1) - [`4f56b08`](https://github.com/ubiquitarianism/leonida/commit/4f56b089d92a5193e5e3a03ad3f024d1ce5076fb) psocache_inspect: guard --entry bounds, zero-meta references, and header string slots ### 📊 Changes **2 files changed** (+139 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_psocache_inspect.py` (+109 -0) 📝 `tools/psocache_inspect.py` (+30 -8) </details> ### 📄 Description 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`). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1483
No description provided.