psin_data: break cyclic array/map recursion, bound align_log2, and validate block_table/collect bounds (PSIN-DATA-RECURSION-AND-BOUNDS) #1463

Merged
leonida/mandy
quietfold merged 1 commit from fix/psin-data-recursion-and-bounds into main 2026-11-28 10:17:31 +00:00

Pull request successfully merged and closed

quietfold commented 2026-11-25 00:43:05 +00:00

Claimed in the coordination issue as: PSIN-DATA-RECURSION-AND-BOUNDS

Summary

Hardens tools/psin_data.py against cyclic/damaged PSIN container graphs and extreme schema values:

  1. Cyclic array/map recursion (Walker.value, Walker.array, Walker.map):
    • Previously, only Walker.struct() checked depth > MAX_DEPTH and self.seen. Cyclic 0x5C/0x52/0x50/0x53/0x54 arrays or 0x71/0x73/0x75 maps recursed forever until crashing with an uncaught RecursionError (neither measure() nor --dump catches RecursionError), and branching self-referential arrays (count >= 2) would hang exponentially even with a depth cap alone.
    • Now enforces depth > MAX_DEPTH in Walker.value() and tracks active container recursion frames in self._visiting (added on entry, removed in finally), terminating cycles in O(1) (stats["container cycle"]) while preserving normal decoding for non-cyclic DAG sharing (two fields pointing to the same read-only array/map).
  2. Unbounded align_log2 in Container.align_of() / Container.slot():
    • align_log2 is a u8 in LSCH (0..255), so a damaged value like 250 computed 1 << 250 (~1.8e75) inside slot(). Caps valid align_log2 to 0..MAX_ALIGN_LOG2 (16, 64 KiB) and treats invalid alignments as undecoded (None).
  3. block_table() & collect() bounds:
    • block_table(blob, first) now validates 8 <= first <= len(blob) and refuses 16-byte end padding end > first that would read past first into the first section.
    • collect(src, limit=...) now rejects limit <= 0 and boolean limit values instead of silently treating limit=0 as unlimited.

Testing

  • AI-assisted: yes
  • Host: macOS Apple M5 (arm64), Python 3.14 (CPU-only, synthetic PSIN containers only, no game data)
  • python3 -m compileall -q make_game.py tools tests -> clean
  • python3 -I scripts/check_no_game_data.py tools/psin_data.py tests/test_psin_data.py -> clean
  • python3 -I tests/test_psin_data.py -v -> 34/34 OK (3 new tests)
  • python3 -I tests/test_psin_index.py -> 33/33 OK
  • 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_psin_data.py on unpatched upstream/main b4b7756e8): FAILED (failures=2, errors=1) (reproducing uncaught RecursionError: maximum recursion depth exceeded on cyclic array/map, 1809251394333065553493296640760748560207343510400633813116524750123642650624 alignment on align_log2=250, and unvalidated first in block_table()).
Claimed in the coordination issue as: PSIN-DATA-RECURSION-AND-BOUNDS ## Summary Hardens `tools/psin_data.py` against cyclic/damaged PSIN container graphs and extreme schema values: 1. **Cyclic array/map recursion (`Walker.value`, `Walker.array`, `Walker.map`)**: - Previously, only `Walker.struct()` checked `depth > MAX_DEPTH` and `self.seen`. Cyclic `0x5C`/`0x52`/`0x50`/`0x53`/`0x54` arrays or `0x71`/`0x73`/`0x75` maps recursed forever until crashing with an uncaught `RecursionError` (neither `measure()` nor `--dump` catches `RecursionError`), and branching self-referential arrays (`count >= 2`) would hang exponentially even with a depth cap alone. - Now enforces `depth > MAX_DEPTH` in `Walker.value()` and tracks active container recursion frames in `self._visiting` (added on entry, removed in `finally`), terminating cycles in O(1) (`stats["container cycle"]`) while preserving normal decoding for non-cyclic DAG sharing (two fields pointing to the same read-only array/map). 2. **Unbounded `align_log2` in `Container.align_of()` / `Container.slot()`**: - `align_log2` is a `u8` in LSCH (`0..255`), so a damaged value like `250` computed `1 << 250` (~`1.8e75`) inside `slot()`. Caps valid `align_log2` to `0..MAX_ALIGN_LOG2` (`16`, 64 KiB) and treats invalid alignments as undecoded (`None`). 3. **`block_table()` & `collect()` bounds**: - `block_table(blob, first)` now validates `8 <= first <= len(blob)` and refuses 16-byte end padding `end > first` that would read past `first` into the first section. - `collect(src, limit=...)` now rejects `limit <= 0` and boolean `limit` values instead of silently treating `limit=0` as unlimited. ## Testing - **AI-assisted**: yes - **Host**: macOS Apple M5 (`arm64`), Python 3.14 (CPU-only, synthetic PSIN containers only, no game data) - `python3 -m compileall -q make_game.py tools tests` -> clean - `python3 -I scripts/check_no_game_data.py tools/psin_data.py tests/test_psin_data.py` -> clean - `python3 -I tests/test_psin_data.py -v` -> **34/34 OK** (3 new tests) - `python3 -I tests/test_psin_index.py` -> **33/33 OK** - `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_psin_data.py` on unpatched `upstream/main` `b4b7756e8`): `FAILED (failures=2, errors=1)` (reproducing uncaught `RecursionError: maximum recursion depth exceeded` on cyclic array/map, `1809251394333065553493296640760748560207343510400633813116524750123642650624` alignment on `align_log2=250`, and unvalidated `first` in `block_table()`).
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 00:43:21 +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:17:24 +00:00

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