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

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

📋 Pull Request Information

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

Base: main ← Head: fix/psin-data-recursion-and-bounds


📝 Commits (1)

  • ed37d34 psin_data: break cyclic array/map recursion, bound align_log2, and validate block_table/collect bounds

📊 Changes

2 files changed (+130 additions, -5 deletions)

View changed files

📝 tests/test_psin_data.py (+81 -0)
📝 tools/psin_data.py (+49 -5)

📄 Description

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

🔄 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/1463 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/psin-data-recursion-and-bounds` --- ### 📝 Commits (1) - [`ed37d34`](https://github.com/ubiquitarianism/leonida/commit/ed37d34e17b7c9a5452b6f750cdb196cf35d7c99) psin_data: break cyclic array/map recursion, bound align_log2, and validate block_table/collect bounds ### 📊 Changes **2 files changed** (+130 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_psin_data.py` (+81 -0) 📝 `tools/psin_data.py` (+49 -5) </details> ### 📄 Description 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()`). --- <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#1493
No description provided.