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

Merged
IlyassRS3 merged 1 commit from upstream-pr/1463 into main 2026-11-28 10:17:31 +00:00
IlyassRS3 commented 2026-11-25 00:43:05 +00:00 (Migrated from github.com)

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


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

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

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:6049785641 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1463#pullrequestreview-6049785641). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T00:43:21Z. **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:17:24 +00:00 (Migrated from github.com)

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


Red/green: the PR's tests fail on main 4938673 and pass on head ed37d34e17 (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:7237798575 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1463#issuecomment-7237798575). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T10:17:24Z. --- 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 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!1463
No description provided.