tools: harden isa_coverage.py BOM, SyntaxError, encoding and zero-division paths #1799

Merged
leonida/mandy
kirigayakazima merged 1 commit from fix/isa-coverage-safety into main 2026-11-28 15:00:46 +00:00

Pull request successfully merged and closed

kirigayakazima commented 2026-11-25 12:16:48 +00:00

Claimed in the coordination issue as: ISA-COVERAGE-PARSER-BOM-AND-ENCODING-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162112152)

What and why

  • Read --translator with encoding="utf-8-sig" in scan_translator() and accepted_names() so files carrying a UTF-8 BOM do not fail ast.parse().
  • Catch (SyntaxError, ValueError) in main() when parsing --translator and report it via ap.error(...) (exit 2) instead of an unhandled traceback.
  • Guard markdown() against empty ISA instruction tables (0 instructions -> 0.0% instead of ZeroDivisionError).
  • Normalize empty or whitespace-only <EncodingName> elements in read_isa() to "OTHER:?".
  • Pass encoding="utf-8", errors="replace" to subprocess.run() in probe_llvm_mc() so python -I does not fail decoding tool diagnostics on ANSI code pages.
  • Add synthetic regression tests in tests/test_isa_coverage.py.

Tested

  • python -I tests/test_isa_coverage.py -v -> Ran 27 tests in 20.636s, OK (skipped=4) (real AMD ISA XML tests skipped without XML path; verified in a detached upstream/main worktree that all new tests fail before the fix: FAILED (failures=2, errors=4, skipped=4)).
  • python -I tests/test_ci_registration.py -> Ran 16 tests in 4.530s, OK.
  • python -I tests/test_text_write_line_endings.py -> Ran 3 tests in 12.503s, OK.
  • python -I scripts/check_no_game_data.py tools/isa_coverage.py tests/test_isa_coverage.py -> exit 0.
  • python -I -m compileall -q tools/isa_coverage.py tests/test_isa_coverage.py and git diff --check -> clean.

GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64)

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. (n/a)
Claimed in the coordination issue as: ISA-COVERAGE-PARSER-BOM-AND-ENCODING-SAFETY (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162112152) ### What and why - Read `--translator` with `encoding="utf-8-sig"` in `scan_translator()` and `accepted_names()` so files carrying a UTF-8 BOM do not fail `ast.parse()`. - Catch `(SyntaxError, ValueError)` in `main()` when parsing `--translator` and report it via `ap.error(...)` (exit `2`) instead of an unhandled traceback. - Guard `markdown()` against empty ISA instruction tables (`0` instructions -> `0.0%` instead of `ZeroDivisionError`). - Normalize empty or whitespace-only `<EncodingName>` elements in `read_isa()` to `"OTHER:?"`. - Pass `encoding="utf-8", errors="replace"` to `subprocess.run()` in `probe_llvm_mc()` so `python -I` does not fail decoding tool diagnostics on ANSI code pages. - Add synthetic regression tests in `tests/test_isa_coverage.py`. ### Tested - `python -I tests/test_isa_coverage.py -v` -> `Ran 27 tests in 20.636s`, `OK (skipped=4)` (real AMD ISA XML tests skipped without XML path; verified in a detached `upstream/main` worktree that all new tests fail before the fix: `FAILED (failures=2, errors=4, skipped=4)`). - `python -I tests/test_ci_registration.py` -> `Ran 16 tests in 4.530s`, `OK`. - `python -I tests/test_text_write_line_endings.py` -> `Ran 3 tests in 12.503s`, `OK`. - `python -I scripts/check_no_game_data.py tools/isa_coverage.py tests/test_isa_coverage.py` -> exit `0`. - `python -I -m compileall -q tools/isa_coverage.py tests/test_isa_coverage.py` and `git diff --check` -> clean. GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64) 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. (n/a)
madvillain91 reviewed 2026-11-25 13:01:48 +00:00
madvillain91 left a comment
No description provided.
Independent Windows 11 26H2 (26300.9711) check at 23fad6301 against main 98c2f7777 (PR base 0d80bc5df), using CPython 3.12.3 (python -I) and Git for Windows 2.56.0.windows.2. Passed test_isa_coverage.py (23 passed, 4 skipped without LEONIDA_RDNA2_ISA_XML, 20.786s), test_ci_registration.py (16/16, 6.163s), and test_text_write_line_endings.py (3/3, 15.982s), with check_no_game_data.py and compileall clean. Confirmed UTF-8 BOM scanning, SyntaxError/NUL-byte usage errors (preserving UnicodeDecodeError precedence), encoding="utf-8", errors="replace" in probe_llvm_mc() for Windows python -I, empty/whitespace <EncodingName> normalization to OTHER:?, and zero-instruction markdown() handling. LGTM.
MSedgeMC approved these changes 2026-11-25 17:20:46 +00:00
MSedgeMC left a comment

Verified on the PR branch: python3 -m unittest tests.test_isa_coverage -v -> 27 tests OK (4 skips), including the new BOM, SyntaxError/NUL, and zero-instruction markdown cases. The hardenings are appropriate for a tool that parses arbitrary translator source text. Python-only, no game data.

Verified on the PR branch: python3 -m unittest tests.test_isa_coverage -v -> 27 tests OK (4 skips), including the new BOM, SyntaxError/NUL, and zero-instruction markdown cases. The hardenings are appropriate for a tool that parses arbitrary translator source text. Python-only, no game data.
aslan2142 commented 2026-11-25 17:27:35 +00:00

Thanks. The new guards cover inputs that don't occur: --translator is the repository's own rdna2spirv.py, which has no BOM, and the AMD ISA XML always has its tables and encoding names.

Can you name a real input that hits them? Otherwise we'd rather not take the extra code.

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

Thanks. The new guards cover inputs that don't occur: `--translator` is the repository's own `rdna2spirv.py`, which has no BOM, and the AMD ISA XML always has its tables and encoding names. Can you name a real input that hits them? Otherwise we'd rather not take the extra code. AI-assisted (Claude Code, Claude Opus 5.5).
zmasi commented 2026-11-28 12:04:43 +00:00

#1799 | head 23fad6301f55693032e954c7446679fd62cf5f0b (merged cleanly with main d48a957bb) | python -I tests/test_isa_coverage.py | red on main: FAILED (failures=2, errors=4, skipped=4) (ZeroDivisionError on empty ISA, unhandled SyntaxError/NUL/UTF-8 BOM in --translator, OTHER: on empty EncodingName, and missing utf-8 replace decoding in probe_llvm_mc) | green on PR+main: 27 tests OK (skipped=4) | Windows 11, Python 3.12.10 | AI-assisted: yes

Audited read_isa, scan_translator, accepted_names, probe_llvm_mc, markdown, and main in tools/isa_coverage.py (normalizing empty/whitespace EncodingName to OTHER:?, reading translator sources with utf-8-sig, guarding ZeroDivisionError when n == 0, passing encoding="utf-8", errors="replace" to llvm-mc, and converting SyntaxError/ValueError into clean exit-2 usage errors). Also verified tests/test_ci_registration.py (18 OK), tests/test_text_write_line_endings.py (3 OK), and scripts/check_no_game_data.py.

#1799 | head 23fad6301f55693032e954c7446679fd62cf5f0b (merged cleanly with main d48a957bb) | `python -I tests/test_isa_coverage.py` | red on main: `FAILED (failures=2, errors=4, skipped=4)` (`ZeroDivisionError` on empty ISA, unhandled `SyntaxError`/NUL/UTF-8 BOM in `--translator`, `OTHER:` on empty `EncodingName`, and missing `utf-8` replace decoding in `probe_llvm_mc`) | green on PR+main: `27 tests OK (skipped=4)` | Windows 11, Python 3.12.10 | AI-assisted: yes Audited `read_isa`, `scan_translator`, `accepted_names`, `probe_llvm_mc`, `markdown`, and `main` in `tools/isa_coverage.py` (normalizing empty/whitespace `EncodingName` to `OTHER:?`, reading translator sources with `utf-8-sig`, guarding `ZeroDivisionError` when `n == 0`, passing `encoding="utf-8", errors="replace"` to `llvm-mc`, and converting `SyntaxError`/`ValueError` into clean exit-`2` usage errors). Also verified `tests/test_ci_registration.py` (`18 OK`), `tests/test_text_write_line_endings.py` (`3 OK`), and `scripts/check_no_game_data.py`.
MoonEyes commented 2026-11-28 15:00:33 +00:00

Red/green: pytest tests/test_isa_coverage.py fails on main (6 failed, 19 passed, 4 skipped, 4 subtests passed) and passes on head 23fad6301f55 merged with main (23 passed, 4 skipped, 6 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_isa_coverage.py` fails on main (6 failed, 19 passed, 4 skipped, 4 subtests passed) and passes on head 23fad6301f55 merged with main (23 passed, 4 skipped, 6 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.