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

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

@⁠xel1uz on GitHub · 2026-11-25 05:33 UTC · original
xel1uz:fix/amd-swizzle-argument-guards → main · +98 −2 · 2 files · 1 commits · open

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.
**@⁠xel1uz** on GitHub · 2026-11-25 05:33 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1601) `xel1uz:fix/amd-swizzle-argument-guards` → `main` · +98 −2 · 2 files · 1 commits · open 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.
amd_swizzle.py turned bad arguments into tracebacks or into a silent success:

- --width 0 or --height 0 crashed in Pillow with UnidentifiedImageError, and a
  negative size crashed in numpy with "negative dimensions are not allowed",
  because the guards only checked --out and the blob path.
- A negative --offset sliced from the end of the blob, and an offset past the
  end left an empty buffer; both exited 0 and wrote no image.
- A --modes value matching no pattern table produced no line at all, so a
  caller could not tell success from a mistyped mode.
- The default header path is a Windows-shaped placeholder, so on a host
  without the file the tool died in a raw FileNotFoundError.

Each case now prints one ERROR line and exits 2 before any work starts, and a
missing or malformed gfx10SwizzlePattern.h names AMD_SWIZZLE_HEADER instead of
raising. A valid run is unchanged.

Tests: the six cases above exercise the CLI with a synthetic header; five of
them fail on c7e82e26 and pass here, and the valid-run test passes on both.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:57:47 +00:00
Author
Owner

@⁠PierreEbele on GitHub · 2026-11-25 15:41 UTC · original

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

**@⁠PierreEbele** on GitHub · 2026-11-25 15:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1601#issuecomment-7167230485) 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
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/1601/head:github-bot/gh-1601'
git switch 'github-bot/gh-1601'

Merge

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