amd_swizzle: reject impossible sizes, unusable offsets and empty results #1601

Merged
leonida/mandy
xel1uz merged 1 commit from fix/amd-swizzle-argument-guards into main 2026-11-29 18:59:06 +00:00

Pull request successfully merged and closed

xel1uz commented 2026-11-25 05:33:34 +00:00

Item ID: AMD-SWIZZLE-ARG-GUARDS (new item; the coordination issue #14 is at GitHub's 2,500-comment cap, so the claim lives here)

Claimed as: AMD-SWIZZLE-ARG-GUARDS - tools/amd_swizzle.py dies with a raw traceback on --width 0 or a negative size, and silently exits 0 with no output when --offset or --modes cannot produce an image. Reproduced on c7e82e26 with a synthetic addrlib header and a 256-byte blob.

What and why

On c7e82e26 the tool validated only --out and the blob path, so every other bad argument ended in one of four ways:

  • --width 0 / --height 0 reached Pillow: PIL.UnidentifiedImageError: cannot identify image file.
  • --width -4 reached numpy: ValueError: negative dimensions are not allowed.
  • --offset -1 silently sliced the blob from the end; --offset 999 (past the end of a 256-byte blob) left an empty buffer. Both exited 0, printed nothing and wrote no image, so a caller could not tell either from a mistyped value.
  • A --modes value matching no pattern table printed nothing and exited 0.
  • The default header path is a Windows-shaped placeholder (<ref>\mesa-addrlib\gfx10SwizzlePattern.h), so on a host without that file the tool died in a raw FileNotFoundError traceback instead of naming AMD_SWIZZLE_HEADER.

Each case now prints one ERROR: line and exits 2 before any work starts, in the same shape as the existing --out guards. A missing or malformed header is reported as such. Valid runs are untouched.

Tested

  • python -m pytest tests/test_amd_swizzle.py -q -> 20 passed, 8 subtests passed (pytest 9.2.0, numpy 2.5.5, Pillow 13.0.0 on Linux).
  • The six new cases on the original implementation (patched tool stashed, tests kept): 5 failed, 1 passed - the valid-run test passes on both sides, which is what distinguishes the guards from a workaround.
  • git diff --check clean; python -I scripts/check_no_game_data.py tools/amd_swizzle.py tests/test_amd_swizzle.py exits 0.

GPUs tested: n/a - CPU only, no Vulkan device involved.

AI-assisted: Yes.

Checklist

  • Based on current main (c7e82e26) and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption; the tests build a synthetic header and blob.
  • A new translator rule in the contract table of docs/design.md - not applicable, no translator change.
Item ID: `AMD-SWIZZLE-ARG-GUARDS` (new item; the coordination issue #14 is at GitHub's 2,500-comment cap, so the claim lives here) Claimed as: `AMD-SWIZZLE-ARG-GUARDS` - `tools/amd_swizzle.py` dies with a raw traceback on `--width 0` or a negative size, and silently exits 0 with no output when `--offset` or `--modes` cannot produce an image. Reproduced on c7e82e26 with a synthetic addrlib header and a 256-byte blob. ### What and why On c7e82e26 the tool validated only `--out` and the blob path, so every other bad argument ended in one of four ways: - `--width 0` / `--height 0` reached Pillow: `PIL.UnidentifiedImageError: cannot identify image file`. - `--width -4` reached numpy: `ValueError: negative dimensions are not allowed`. - `--offset -1` silently sliced the blob from the end; `--offset 999` (past the end of a 256-byte blob) left an empty buffer. Both exited 0, printed nothing and wrote no image, so a caller could not tell either from a mistyped value. - A `--modes` value matching no pattern table printed nothing and exited 0. - The default header path is a Windows-shaped placeholder (`<ref>\mesa-addrlib\gfx10SwizzlePattern.h`), so on a host without that file the tool died in a raw `FileNotFoundError` traceback instead of naming `AMD_SWIZZLE_HEADER`. Each case now prints one `ERROR:` line and exits 2 before any work starts, in the same shape as the existing `--out` guards. A missing or malformed header is reported as such. Valid runs are untouched. ### Tested - `python -m pytest tests/test_amd_swizzle.py -q` -> 20 passed, 8 subtests passed (pytest 9.2.0, numpy 2.5.5, Pillow 13.0.0 on Linux). - The six new cases on the original implementation (patched tool stashed, tests kept): 5 failed, 1 passed - the valid-run test passes on both sides, which is what distinguishes the guards from a workaround. - `git diff --check` clean; `python -I scripts/check_no_game_data.py tools/amd_swizzle.py tests/test_amd_swizzle.py` exits 0. GPUs tested: n/a - CPU only, no Vulkan device involved. AI-assisted: Yes. ### Checklist - [x] Based on current `main` (`c7e82e26`) and merges cleanly. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption; the tests build a synthetic header and blob. - [ ] A new translator rule in the contract table of `docs/design.md` - not applicable, no translator change.
PierreEbele commented 2026-11-25 15:41:26 +00:00

Reviewed 672dba7: ran tests/test_amd_swizzle.py on this branch and on main (your test file copied in): 20/20 pass here; on main the five new guard tests fail and only the valid-run test passes, so they test the fix; the tex_export, tool_startup and resource_hygiene suites pass on both. A CLI probe reproduced all five pre-fix behaviours (negative-dimension and Pillow-on-4x0 tracebacks; silent rc=0 for negative and past-EOF offsets and unmatched --modes); each is now one ERROR line, exit 2. One reproduced residual, non-blocking: a header that parses but references a non-existent NIBBLE row (one NIBBLE01 row, entry {0, 5, 0, 0, 0}) still raises a raw IndexError traceback, same as on main, because equation() runs outside the new load() guard; a follow-up could validate the indices in load().

AI-assisted review.

Recommendation: Merge

Reviewed 672dba7: ran tests/test_amd_swizzle.py on this branch and on main (your test file copied in): 20/20 pass here; on main the five new guard tests fail and only the valid-run test passes, so they test the fix; the tex_export, tool_startup and resource_hygiene suites pass on both. A CLI probe reproduced all five pre-fix behaviours (negative-dimension and Pillow-on-4x0 tracebacks; silent rc=0 for negative and past-EOF offsets and unmatched --modes); each is now one ERROR line, exit 2. One reproduced residual, non-blocking: a header that parses but references a non-existent NIBBLE row (one NIBBLE01 row, entry `{0, 5, 0, 0, 0}`) still raises a raw IndexError traceback, same as on main, because equation() runs outside the new load() guard; a follow-up could validate the indices in load(). AI-assisted review. **Recommendation:** Merge
MoonEyes commented 2026-11-28 03:02:24 +00:00

Hi @xel1uz, thanks for this pull request.

CONTRIBUTING.md asks contributors to claim their work in the coordination issue ("Coordinating who works on what", currently #1608) before opening a pull request: post claiming: <ID> there, or a one-line description if the item is not listed yet, and put the ID in the pull request. I could not find a claim from you for this one.

The claim matters because "it is claimed in the coordination issue by the pull request's author" is the first item on the maintainers' checklist for a mergeable pull request, and because it lets others see that the work is taken before they write it a second time.

Could you post the claim in #1608 and put its ID (or a link to it) on the pull request's "Claimed in the coordination issue as:" line? If you already claimed it in another form, a link to your comment is enough.

AI-assisted (Claude Code, Claude Opus 5.5).

Hi @xel1uz, thanks for this pull request. CONTRIBUTING.md asks contributors to claim their work in the coordination issue ("Coordinating who works on what", currently #1608) before opening a pull request: post `claiming: <ID>` there, or a one-line description if the item is not listed yet, and put the ID in the pull request. I could not find a claim from you for this one. The claim matters because "it is claimed in the coordination issue by the pull request's author" is the first item on the maintainers' checklist for a mergeable pull request, and because it lets others see that the work is taken before they write it a second time. Could you post the claim in #1608 and put its ID (or a link to it) on the pull request's "Claimed in the coordination issue as:" line? If you already claimed it in another form, a link to your comment is enough. AI-assisted (Claude Code, Claude Opus 5.5).
zmasi commented 2026-11-28 22:21:57 +00:00

#1601 | head 672dba76f99afc0439472116c3e5baf5b00ff49d (merged cleanly with main c7753ed98a) | python -m pytest tests/test_amd_swizzle.py -v (tests/requirements.txt: pytest 9.2.0, numpy 2.5.5, Pillow 13.0.0) | red on main: 5 failed, 15 passed (--width 0/--height 0/-4, --offset -1, --offset 999, and unmatched --modes exited 0 instead of 2, and missing AMD_SWIZZLE_HEADER raised an unhandled FileNotFoundError traceback with rc=1) | green on PR+main: 20 passed, 8 subtests passed | Windows 11 (10.0.26200.9974), Python 3.12.10 | AI-assisted: yes

Audited 672dba76f9 (tools/amd_swizzle.py and tests/test_amd_swizzle.py) on Windows 11, confirming upfront dimension (>= 1), negative/past-EOF --offset, missing/malformed AMD_SWIZZLE_HEADER (gfx10SwizzlePattern.h), and empty-match --modes guards all exit 2 with clean ERROR: diagnostics and no traceback. Minor non-blocking note: test_a_valid_run_still_succeeds is gated only by @needs_numpy while passing --out out.png (which imports PIL.Image), so adding pytest.importorskip("PIL.Image") like test_cli_previews_uncompressed_r8_and_rg8 and test_preview_keeps_partial_bc_blocks would let partial numpy-only setups skip cleanly too (full tests/requirements.txt CI installs Pillow==13.0.0 and passes 20/20). Also verified tests/test_ci_registration.py, tests/test_text_write_line_endings.py, and scripts/check_no_game_data.py.

#1601 | head 672dba76f99afc0439472116c3e5baf5b00ff49d (merged cleanly with main c7753ed98a) | `python -m pytest tests/test_amd_swizzle.py -v` (`tests/requirements.txt`: pytest 9.2.0, numpy 2.5.5, Pillow 13.0.0) | red on main: `5 failed, 15 passed` (`--width 0`/`--height 0`/`-4`, `--offset -1`, `--offset 999`, and unmatched `--modes` exited `0` instead of `2`, and missing `AMD_SWIZZLE_HEADER` raised an unhandled `FileNotFoundError` traceback with `rc=1`) | green on PR+main: `20 passed, 8 subtests passed` | Windows 11 (`10.0.26200.9974`), Python 3.12.10 | AI-assisted: yes Audited `672dba76f9` (`tools/amd_swizzle.py` and `tests/test_amd_swizzle.py`) on Windows 11, confirming upfront dimension (`>= 1`), negative/past-EOF `--offset`, missing/malformed `AMD_SWIZZLE_HEADER` (`gfx10SwizzlePattern.h`), and empty-match `--modes` guards all exit `2` with clean `ERROR:` diagnostics and no traceback. Minor non-blocking note: `test_a_valid_run_still_succeeds` is gated only by `@needs_numpy` while passing `--out out.png` (which imports `PIL.Image`), so adding `pytest.importorskip("PIL.Image")` like `test_cli_previews_uncompressed_r8_and_rg8` and `test_preview_keeps_partial_bc_blocks` would let partial `numpy`-only setups skip cleanly too (full `tests/requirements.txt` CI installs `Pillow==13.0.0` and passes 20/20). Also verified `tests/test_ci_registration.py`, `tests/test_text_write_line_endings.py`, and `scripts/check_no_game_data.py`.
TheGeogeo commented 2026-11-29 00:17:08 +00:00

Tested 672dba76f merged (no commit) with main 829530dbd: Python 3.12.4 with pytest 9.2.0 (from tests/requirements.txt), Windows 11 10.0.26300, CPU only. check_no_game_data.py is clean.

  • Green: python -m pytest tests/test_amd_swizzle.py: 20 passed (8 subtests). The main version gives 14 passed.
  • Red: with tools/amd_swizzle.py back to main, 5 fail: the bad arguments exit 0 instead of 2 (e.g. a negative offset with --width 4 --height 4).

No blocker from my side.

AI-assisted (Claude Code).

Tested `672dba76f` merged (no commit) with `main` `829530dbd`: Python 3.12.4 with pytest 9.2.0 (from `tests/requirements.txt`), Windows 11 10.0.26300, CPU only. `check_no_game_data.py` is clean. - Green: `python -m pytest tests/test_amd_swizzle.py`: 20 passed (8 subtests). The `main` version gives 14 passed. - Red: with `tools/amd_swizzle.py` back to `main`, 5 fail: the bad arguments exit 0 instead of 2 (e.g. a negative offset with `--width 4 --height 4`). No blocker from my side. AI-assisted (Claude Code).
OreoFR commented 2026-11-29 01:47:55 +00:00

Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, main 53b77df14, PR head 672dba76f : red 5 failures / green 20 OK

python -m pytest tests/test_amd_swizzle.py (pytest 9.2.0, venv):

  • Red (main + only this PR's test file): 5 failed, 15 passed, 8 subtests passed. All 5 fail on the exit code (tool exits 0, or 1 on an uncaught FileNotFoundError, instead of 2):
    • test_rejects_non_positive_dimensions
    • test_rejects_negative_offset
    • test_rejects_offset_past_the_end
    • test_reports_when_no_pattern_matches
    • test_reports_a_missing_or_malformed_header
  • Green (main + git merge --no-ff of this PR): 20 passed, 8 subtests passed.
  • (MEASURED n=3) each of the 5 rerun alone 3 times: red 3/3 fail, green 3/3 pass.
  • Skipped: none.
  • Merge: clean (git merge-tree).

Same result as TheGeogeo's Windows verification.

Verdict: READY

AI-assisted: Claude Code

Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, main 53b77df14, PR head 672dba76f : red 5 failures / green 20 OK `python -m pytest tests/test_amd_swizzle.py` (pytest 9.2.0, venv): - **Red** (main + only this PR's test file): 5 failed, 15 passed, 8 subtests passed. All 5 fail on the exit code (tool exits 0, or 1 on an uncaught `FileNotFoundError`, instead of 2): - `test_rejects_non_positive_dimensions` - `test_rejects_negative_offset` - `test_rejects_offset_past_the_end` - `test_reports_when_no_pattern_matches` - `test_reports_a_missing_or_malformed_header` - **Green** (main + `git merge --no-ff` of this PR): 20 passed, 8 subtests passed. - (MEASURED n=3) each of the 5 rerun alone 3 times: red 3/3 fail, green 3/3 pass. - Skipped: none. - Merge: clean (`git merge-tree`). Same result as TheGeogeo's Windows verification. **Verdict: READY** AI-assisted: Claude Code
TheGeogeo commented 2026-11-29 18:50:04 +00:00

Tested 672dba76f merged (no commit) with main 7c330c76d: Python 3.12.4, Windows 11 10.0.26300, CPU only.

  • Green: pytest tests/test_amd_swizzle.py gives 20 passed (8 subtests).
  • Red: with tools/amd_swizzle.py back to main, 5 fail: test_rejects_non_positive_dimensions, test_rejects_negative_offset, test_rejects_offset_past_the_end, test_reports_when_no_pattern_matches and test_reports_a_missing_or_malformed_header.
  • Base: main's own version gives 14 passed. check_no_game_data.py is clean.

AI-assisted (Claude Code).

Tested `672dba76f` merged (no commit) with `main` `7c330c76d`: Python 3.12.4, Windows 11 10.0.26300, CPU only. - Green: `pytest tests/test_amd_swizzle.py` gives 20 passed (8 subtests). - Red: with `tools/amd_swizzle.py` back to `main`, 5 fail: `test_rejects_non_positive_dimensions`, `test_rejects_negative_offset`, `test_rejects_offset_past_the_end`, `test_reports_when_no_pattern_matches` and `test_reports_a_missing_or_malformed_header`. - Base: `main`'s own version gives 14 passed. `check_no_game_data.py` is clean. AI-assisted (Claude Code).
MoonEyes commented 2026-11-29 18:59:03 +00:00

Merging. Head 672dba76f has red/green verdicts from reviewers other than the author (above), merges cleanly with main 7c330c76d, does not force a rebase of #2566, and carries a claim ID. Remaining main CI reds (translator-tests mesh, semantic-tests f16) are pre-existing. Merged by MoonEyes.

AI-assisted (Claude Code).

Merging. Head `672dba76f` has red/green verdicts from reviewers other than the author (above), merges cleanly with `main` `7c330c76d`, does not force a rebase of #2566, and carries a claim ID. Remaining main CI reds (`translator-tests` mesh, `semantic-tests` f16) are pre-existing. Merged by MoonEyes. AI-assisted (Claude Code).
Sign in to join this conversation.
No description provided.