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

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

@⁠IlyassRS3 on GitHub · 2026-11-25 00:43 UTC · original
IlyassRS3:fix/psin-data-recursion-and-bounds → main · +130 −5 · 2 files · 1 commits · open

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()).
**@⁠IlyassRS3** on GitHub · 2026-11-25 00:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1463) `IlyassRS3:fix/psin-data-recursion-and-bounds` → `main` · +130 −5 · 2 files · 1 commits · open 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()`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:52:19 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 00:43 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:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1463#pullrequestreview-6049785641) · **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/1463/head:github-bot/gh-1463'
git switch 'github-bot/gh-1463'

Merge

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