spirv_asm: validate --out upfront, accept UTF-8 BOM, and write atomically #2264

Merged
leonida/mandy
kirigayakazima merged 1 commit from fix/spirv-asm-cli-safety into main 2026-11-28 15:06:22 +00:00

Pull request successfully merged and closed

kirigayakazima commented 2026-11-26 11:44:47 +00:00

Claimed in the coordination issue as: SPIRV-ASM-CLI-BOM-DIR-GUARD-AND-ATOMIC-WRITE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191092674)

What and why

tools/spirv_asm.py previously:

  • Read the .spvasm input before validating --out, causing full assembly before rejecting empty paths, directories, or missing parent directories.
  • Opened .spvasm with "utf-8" instead of "utf-8-sig", rejecting UTF-8 BOM inputs with unknown opcode '\ufeff...'.
  • Wrote binary .spv output directly in place with open(args.out, "wb"), risking truncated or corrupted existing outputs if a write failed partway.

This adds _check_out_path upfront in main(), reads source files with utf-8-sig (keeping newline=""), publishes assembled SPIR-V atomically with tempfile + file_mode.published_mode + replace_retry.replace, and adds unit/CLI tests covering all three behaviors.

Tested

  • python -I tests/test_spirv_asm.py -v: 41 tests run (40 passed; 1 skipped: spirv-as not installed)
  • Detached upstream/main baseline negative control: failed on the new CLI tests before the fix
  • python -I tests/test_ci_registration.py (16 passed), python -I tests/test_text_write_line_endings.py (3 passed), python -I scripts/check_no_game_data.py, python -I -m compileall -q, and git diff --cached --check

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • 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.
Claimed in the coordination issue as: SPIRV-ASM-CLI-BOM-DIR-GUARD-AND-ATOMIC-WRITE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7191092674) ### What and why `tools/spirv_asm.py` previously: - Read the `.spvasm` input before validating `--out`, causing full assembly before rejecting empty paths, directories, or missing parent directories. - Opened `.spvasm` with `"utf-8"` instead of `"utf-8-sig"`, rejecting UTF-8 BOM inputs with `unknown opcode '\ufeff...'`. - Wrote binary `.spv` output directly in place with `open(args.out, "wb")`, risking truncated or corrupted existing outputs if a write failed partway. This adds `_check_out_path` upfront in `main()`, reads source files with `utf-8-sig` (keeping `newline=""`), publishes assembled SPIR-V atomically with `tempfile` + `file_mode.published_mode` + `replace_retry.replace`, and adds unit/CLI tests covering all three behaviors. ### Tested - `python -I tests/test_spirv_asm.py -v`: 41 tests run (40 passed; 1 skipped: `spirv-as not installed`) - Detached `upstream/main` baseline negative control: failed on the new CLI tests before the fix - `python -I tests/test_ci_registration.py` (16 passed), `python -I tests/test_text_write_line_endings.py` (3 passed), `python -I scripts/check_no_game_data.py`, `python -I -m compileall -q`, and `git diff --cached --check` GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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.
real-rusher commented 2026-11-26 12:23:34 +00:00

Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. With this PR, python -I tests/test_spirv_asm.py: 41 tests OK. The PR's test file against main's tool: 2 failures and 1 error, so the new tests fail on main and pass with the change. compileall clean.

AI-assisted: yes (Claude Code)

Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. With this PR, python -I tests/test_spirv_asm.py: 41 tests OK. The PR's test file against main's tool: 2 failures and 1 error, so the new tests fail on main and pass with the change. compileall clean. AI-assisted: yes (Claude Code)
tiguan26 approved these changes 2026-11-27 04:37:08 +00:00
tiguan26 left a comment

Approving 9569d1806.

--out is checked (not empty, not a directory, parent exists) before the source is read, the source is read BOM-tolerant, and the module is published through a temp file and replace_retry.

Merged onto main d1dff7949 in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. tests/test_spirv_asm.py: 41 passed (main: 38). AI-assisted (Claude Code, Claude Opus 5.5).

Approving 9569d1806. `--out` is checked (not empty, not a directory, parent exists) before the source is read, the source is read BOM-tolerant, and the module is published through a temp file and `replace_retry`. Merged onto `main` `d1dff7949` in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. `tests/test_spirv_asm.py`: 41 passed (main: 38). AI-assisted (Claude Code, Claude Opus 5.5).
zmasi commented 2026-11-28 10:35:12 +00:00

#2264 | head 9569d180657b0e50a25eaec9ecca17969d086af0 (merged cleanly with main 6c7f79343) | python -I tests/test_spirv_asm.py | red on main: FAILED (failures=2, errors=1, skipped=2) (unknown opcode '\ufeffOpCapability' on UTF-8 BOM input, assemble running before --out validation, and non-atomic direct output write) | green on PR+main: 41 tests OK (skipped=2) | Windows 11, Python 3.12.10 | AI-assisted: yes

Audited _check_out_path (upfront empty/directory/missing-parent checks before reading or assembling inputs), utf-8-sig source decoding, and _write_atomic_bytes (NamedTemporaryFile + published_mode + replace_retry.replace with finally temp cleanup) in tools/spirv_asm.py. Also passed tests/test_ci_registration.py (18 OK), tests/test_text_write_line_endings.py (3 OK), tests/test_runner_calls_all_tests.py (3 OK), and scripts/check_no_game_data.py.

#2264 | head 9569d180657b0e50a25eaec9ecca17969d086af0 (merged cleanly with main 6c7f79343) | `python -I tests/test_spirv_asm.py` | red on main: `FAILED (failures=2, errors=1, skipped=2)` (`unknown opcode '\ufeffOpCapability'` on UTF-8 BOM input, `assemble` running before `--out` validation, and non-atomic direct output write) | green on PR+main: `41 tests OK (skipped=2)` | Windows 11, Python 3.12.10 | AI-assisted: yes Audited `_check_out_path` (upfront empty/directory/missing-parent checks before reading or assembling inputs), `utf-8-sig` source decoding, and `_write_atomic_bytes` (`NamedTemporaryFile` + `published_mode` + `replace_retry.replace` with `finally` temp cleanup) in `tools/spirv_asm.py`. Also passed `tests/test_ci_registration.py` (`18 OK`), `tests/test_text_write_line_endings.py` (`3 OK`), `tests/test_runner_calls_all_tests.py` (`3 OK`), and `scripts/check_no_game_data.py`.
MoonEyes commented 2026-11-28 15:06:18 +00:00

Red/green: pytest tests/test_spirv_asm.py fails on main (3 failed, 38 passed, 1 skipped, 250 subtests passed) and passes on head 9569d180657b merged with main (41 passed, 1 skipped, 250 subtests passed).
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

Red/green: `pytest tests/test_spirv_asm.py` fails on main (3 failed, 38 passed, 1 skipped, 250 subtests passed) and passes on head 9569d180657b merged with main (41 passed, 1 skipped, 250 subtests passed). Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No description provided.