Skip to content

psin_psch: reject overlapping PMAP blocks and duplicate sections - #7301

Open
narekshaghoyandev wants to merge 2 commits into
ItsNotRexed:mainfrom
narekshaghoyandev:psin-psch-pmap-order-and-dup-sections
Open

narekshaghoyandev wants to merge 2 commits into
ItsNotRexed:mainfrom
narekshaghoyandev:psin-psch-pmap-order-and-dup-sections

Conversation

@narekshaghoyandev

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #7049 (comment) (separate from #6260, which touches check_report_path / main()).

What and why

In tools/psin_psch.py:

  1. decode_pmap() (lines 72–93) verified that block 1 starts at 0x10, each block fits within 0x10..first_section, and the last block ends at first_section, but never checked that consecutive blocks are non-overlapping (blocks[i]["offset"] >= blocks[i - 1]["offset"] + blocks[i - 1]["length"]). Overlapping or out-of-order blocks silently passed decoding.
  2. parse_psch_container() (line 241) mapped sections with {s["tag"]: s for s in rec["sections"]}, silently overwriting and ignoring duplicate sections with the same tag.

Fix:

  • Reject overlapping or out-of-order PMAP blocks in decode_pmap() with ValueError.
  • Reject duplicate section tags in parse_psch_container() with ValueError.
  • Added synthetic regression test test_rejects_overlapping_pmap_blocks_and_duplicate_sections in tests/test_psin_psch.py.

Tested

Linux x86_64 (Ubuntu 24.04 offline container, Python 3.12.3, CPU-only, no game files):

  • python3 -I tests/test_psin_psch.py -v — all tests pass
  • python3 -I scripts/check_no_game_data.py tools/psin_psch.py tests/test_psin_psch.py — pass
  • python3 -m compileall -q make_game.py tools tests — pass

AI-assisted: yes (Gemini)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

In tools/psin_psch.py, decode_pmap() verified block 0 and the last block bounds but never checked that consecutive blocks are non-overlapping/monotonic, and parse_psch_container() built a dict of sections that silently dropped duplicate tags. Reject overlapping PMAP blocks and duplicate section tags with ValueError, and add synthetic regression tests.
@Kev-Ball

Kev-Ball commented Nov 23, 2026 •

Copy link
Copy Markdown

Checked 2eb93b82 against its parent on the three manifest-matching Xbox 1.0.2.1 archives listed in docs/psin.md. All 57 retail PMAP/PSCH containers produced identical parse results, including validation errors and decoded schema records.

Archive Containers PMAP blocks Valid on both revisions
data.rpf 54 287 54
packs.rpf 2 5 1
anim.rpf 1 2 1

Neither new rejection fired on an intact container. The one existing validation failure in packs.rpf returned the same errors on both revisions. This covers 294 retail PMAP blocks alongside the PR’s synthetic rejection tests. CachyOS, CPU only, AI-assisted.

Retested ec5b887d9 after the overlap and duplicate-tag checks were narrowed. All 57 retail parse results still matched the original parent, including the existing validation error. All four tests in the revised PSCH suite passed on CachyOS.

@SirKingg SirKingg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the measurement across all 57 observed containers. Is there a format reference or additional aggregate evidence that PMAP rows must be non-overlapping and in table order, and that every section tag, including unknown tags, must be unique? Or is this validation intentionally limited to the observed subset?

Synthetic comparisons show a compatibility change: the base accepts disjoint PMAP blocks listed out of order, an empty block whose offset lies inside the preceding block, and repeated unknown TEST sections, while this revision rejects them. These examples do not establish that such inputs are valid.

AI-assisted review.

@devnull34 devnull34 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Windows 11:

  • python -m unittest discover -s tests -p "test_psin*.py" passed all 84 tests (3.478s).
  • Validated tools/psin_psch.py:
    • decode_pmap() enforces monotonic, non-overlapping block bounds (block_offset < prev_end), raising ValueError on corrupted/overlapping layouts.
    • parse_psch_container() rejects duplicate section tags cleanly with ValueError("Duplicate section tag").
  • Whitespace clean (git diff HEAD~1..HEAD --check).

@narekshaghoyandev

Copy link
Copy Markdown
Contributor Author

Thank you @Kev-Ball for checking all 57 retail containers across data.rpf, packs.rpf, and anim.rpf, and thank you @SirKingg for the careful review.

Good catch on both compatibility edge cases — pushed ec5b887d9 to narrow both checks to the actual invariant and silent-overwrite bug without inventing rules for out-of-order/empty rows or unknown tags:

  • decode_pmap: now sorts non-empty blocks (length > 0) by byte span and only rejects true byte-range overlaps between non-empty blocks. Disjoint middle blocks listed out of table order and empty (length == 0) blocks remain accepted.
  • parse_psch_container: now checks for duplicate tags only among the six sections this parser actually consumes (PMAP, PSCH, PSIG, STNS, STDN, STFN), preventing silent dict overwrites while still accepting repeated unknown tags (such as TEST).
  • Updated tests/test_psin_psch.py to cover both the rejected cases and the accepted edge cases (python3 -I tests/test_psin_psch.py -v: 4/4 pass).

AI-assisted (Gemini).

@memorypressure

Copy link
Copy Markdown

Tested 2eb93b8, clean vs main 976b3ec (re-run): all 4 tests in test_psin_psch.py pass, and the new overlap/duplicate-section test fails on clean main (silent first-section drop via dict overwrite). The consecutive-pair overlap scan is sufficient since out-of-order offsets overlap the previous block. Complements #7297's rejection table without case overlap. Checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@airp1ane

Copy link
Copy Markdown
Contributor

Tested ec5b887 on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_psin_psch.py - all exit 0.

AI-assisted (Claude Code).

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Tested ec5b887d on Linux, CPython 3.14, CPU only, synthetic inputs, merged onto main dde82f27: pass

  • python -I tests/test_psin_psch.py: Ran 4 tests, OK. With only the non-test files reverted to main: FAILED (failures=1), so the new tests exercise the change.
  • compileall and scripts/check_no_game_data.py on the changed files: clean.

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting; claimed in #8047.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants