OLD-7883: audit_shader4_regs: harden stage map loading, bound PSV0 reads, and write JSON atomically #888

Merged
narekshaghoyandev merged 1 commit from upstream-pr/888 into main 2026-11-28 13:07:11 +00:00
narekshaghoyandev commented 2026-11-24 14:57:25 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by narekshaghoyandev on 2026-11-24T14:57:25Z.


Reopening my original upstream PR #7883 (previously verified on Windows 11 by @TieruYT; zero overlap with #713 --limit), cleanly rebased onto main:

  1. Stage map loading (load_stage_map): Reads index.json with utf-8-sig, guards malformed list entries and non-string/unhashable stage values, and catches RecursionError.
  2. PSV0 bounds checks (read_psv0): Rejects truncated DXBC PSV0 chunks (off + 8 + plen > len(d)) and inner records whose declared rsize exceeds chunk payload bytes (4 + rsize > len(p)).
  3. Atomic JSON output & report path guard (write_json, main): Passes a.json_out directly to protect_input, and publishes --json atomically via tempfile.mkstemp + file_mode.published_mode + replace_retry.replace with allow_nan=False and read-only-safe temp cleanup.

Verification

  • python3 -I tests/test_audit_shader4_regs.py -v: 23/23 pass (all 3 new tests fail/error against main).
  • python3 -I scripts/check_no_game_data.py scripts/audit_shader4_regs.py tests/test_audit_shader4_regs.py: clean.
  • python3 -m compileall -q scripts/audit_shader4_regs.py tests/test_audit_shader4_regs.py: clean.

Claimed in the coordination issue as: OLD-7883 (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7135201067)
AI-assisted: yes (Gemini)

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5474600356 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/888). Originally posted by [narekshaghoyandev](https://github.com/narekshaghoyandev) on 2026-11-24T14:57:25Z. --- Reopening my original upstream PR #7883 (previously verified on Windows 11 by `@TieruYT`; zero overlap with #713 `--limit`), cleanly rebased onto `main`: 1. **Stage map loading (`load_stage_map`)**: Reads `index.json` with `utf-8-sig`, guards malformed list entries and non-string/unhashable `stage` values, and catches `RecursionError`. 2. **PSV0 bounds checks (`read_psv0`)**: Rejects truncated DXBC `PSV0` chunks (`off + 8 + plen > len(d)`) and inner records whose declared `rsize` exceeds chunk payload bytes (`4 + rsize > len(p)`). 3. **Atomic JSON output & report path guard (`write_json`, `main`)**: Passes `a.json_out` directly to `protect_input`, and publishes `--json` atomically via `tempfile.mkstemp` + `file_mode.published_mode` + `replace_retry.replace` with `allow_nan=False` and read-only-safe temp cleanup. ### Verification - `python3 -I tests/test_audit_shader4_regs.py -v`: 23/23 pass (all 3 new tests fail/error against `main`). - `python3 -I scripts/check_no_game_data.py scripts/audit_shader4_regs.py tests/test_audit_shader4_regs.py`: clean. - `python3 -m compileall -q scripts/audit_shader4_regs.py tests/test_audit_shader4_regs.py`: clean. Claimed in the coordination issue as: OLD-7883 (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7135201067) AI-assisted: yes (Gemini)
KernelSmoker commented 2026-11-24 15:04:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KernelSmoker on 2026-11-24T15:04:00Z.

Review: APPROVED


Reviewed the exact submitted head 6c6d66fbb8b3b93740f3f7a9a9caca652cadb4db. The stage-map loader now tolerates malformed entries, BOM-prefixed JSON and recursion errors; PSV0 chunk and record lengths are bounded before slicing/unpacking; report-path collision checks protect inputs; and report publication is atomic with permission preservation and cleanup. Validation on Bazzite/Linux: python3 -I tests/test_audit_shader4_regs.py -v (20/20 passed), python3 -I -m py_compile scripts/audit_shader4_regs.py tests/test_audit_shader4_regs.py, and git diff --check all passed. Synthetic inputs only; no game data or GPU used.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6041747358 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/888#pullrequestreview-6041747358). Originally posted by [KernelSmoker](https://github.com/KernelSmoker) on 2026-11-24T15:04:00Z. **Review:** APPROVED --- Reviewed the exact submitted head `6c6d66fbb8b3b93740f3f7a9a9caca652cadb4db`. The stage-map loader now tolerates malformed entries, BOM-prefixed JSON and recursion errors; PSV0 chunk and record lengths are bounded before slicing/unpacking; report-path collision checks protect inputs; and report publication is atomic with permission preservation and cleanup. Validation on Bazzite/Linux: `python3 -I tests/test_audit_shader4_regs.py -v` (20/20 passed), `python3 -I -m py_compile scripts/audit_shader4_regs.py tests/test_audit_shader4_regs.py`, and `git diff --check` all passed. Synthetic inputs only; no game data or GPU used.
MoonEyes commented 2026-11-28 13:07:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T13:07:06Z.


Red/green: tests/test_audit_shader4_regs.py 5 failed on main, 23 passed with the PR.
Targeted batch of 30 PRs (their tests, the tests importing the changed modules, the docs tests, the translator suite) on main a997a3c: no new failure, Windows 11 / Python.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239703821 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/888#issuecomment-7239703821). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T13:07:06Z. --- Red/green: tests/test_audit_shader4_regs.py 5 failed on main, 23 passed with the PR. Targeted batch of 30 PRs (their tests, the tests importing the changed modules, the docs tests, the translator suite) on main a997a3c: no new failure, Windows 11 / Python. 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!888
No description provided.