tooling: harden bink_frame_aot anchor validation and add unit suite #1961

Merged
Ammmae0x merged 1 commit from upstream-pr/1968 into main 2026-11-24 22:34:48 +00:00
Ammmae0x commented 2026-11-24 22:16:36 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T22:16:36Z.


Change

Harden anchor lookup and CLI error handling in tools/bink_frame_aot.py and add an offline synthetic AMD64 PE32+ validation suite (bink_frame_aot_check) in tests/bink_frame_aot_check.py, registered in CMakeLists.txt.

  • Replace bare next(...) generator lookups for the mov rdi, rdx and xor eax, eax slice anchors in generate() with explicit ValueError diagnostics when either anchor is missing or xor eax, eax precedes mov rdi, rdx, instead of leaking StopIteration.
  • Extract main(argv=None) -> int in tools/bink_frame_aot.py so (OSError, ValueError, pefile.PEFormatError) failures exit cleanly with code 1 and an error message on stderr instead of printing a Python traceback.
  • Add tests/bink_frame_aot_check.py covering positive C++ lowering output and CLI invocation, plus negative controls for missing input files, SHA-256 profile mismatches, missing BinkAllocateFrameBuffers export, missing/inverted anchors, call-count and indirect-call rejections, mismatched helper RVAs, malformed PE inputs, and invalid .cpp/same-path CLI arguments.

Verification

  • Direct Python suite: .\out\pe-env\Scripts\python.exe -B tests/bink_frame_aot_check.py -v -> Ran 7 tests in 3.073s (OK).
  • Mutation sanity check (temporarily reverting tools/bink_frame_aot.py to upstream/main): Ran 7 tests -> FAILED (failures=2, errors=2) (StopIteration on missing anchor, missing main, tracebacks on missing/non-PE CLI inputs).
  • dev preset (cmake --preset dev, ctest --preset dev -N -> Total Tests: 1251): ctest --preset dev --no-tests=error -R "^(bink_frame_aot_check|bink_heap_aot_check|cpu_aot_bink_heap|cpu_aot_bink_group)$" -V -> 4/4 passed (cpu_aot_bink_heap, cpu_aot_bink_group, bink_heap_aot_check 12/12, bink_frame_aot_check 7/7).
  • release preset (cmake --preset release, ctest --preset release -N -> Total Tests: 1251): ctest --preset release --no-tests=error -R "^(bink_frame_aot_check|bink_heap_aot_check|cpu_aot_bink_heap|cpu_aot_bink_group)$" -V -> 4/4 passed.
  • Repo checks: tests/form_coverage_check.py, tests/docs_links_check.py, and tests/bidi_controls_check.py all exited 0.

Evidence and follow-up

  • tests/bink_frame_aot_check.py constructs a minimal synthetic AMD64 PE32+ DLL in memory (two leaf helpers, BinkAllocateFrameBuffers with the expected control-flow/call shape, export directory, .pdata, and UNWIND_INFO) with no external binaries or game files; BINK_SHA256 is patched in-memory only for synthetic-image tests.
  • This change is AI-assisted.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6592505279 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1968). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T22:16:36Z. --- ## Change Harden anchor lookup and CLI error handling in `tools/bink_frame_aot.py` and add an offline synthetic AMD64 PE32+ validation suite (`bink_frame_aot_check`) in `tests/bink_frame_aot_check.py`, registered in `CMakeLists.txt`. - Replace bare `next(...)` generator lookups for the `mov rdi, rdx` and `xor eax, eax` slice anchors in `generate()` with explicit `ValueError` diagnostics when either anchor is missing or `xor eax, eax` precedes `mov rdi, rdx`, instead of leaking `StopIteration`. - Extract `main(argv=None) -> int` in `tools/bink_frame_aot.py` so `(OSError, ValueError, pefile.PEFormatError)` failures exit cleanly with code `1` and an error message on stderr instead of printing a Python traceback. - Add `tests/bink_frame_aot_check.py` covering positive C++ lowering output and CLI invocation, plus negative controls for missing input files, SHA-256 profile mismatches, missing `BinkAllocateFrameBuffers` export, missing/inverted anchors, call-count and indirect-call rejections, mismatched helper RVAs, malformed PE inputs, and invalid `.cpp`/same-path CLI arguments. ## Verification - Direct Python suite: `.\out\pe-env\Scripts\python.exe -B tests/bink_frame_aot_check.py -v` -> `Ran 7 tests in 3.073s` (`OK`). - Mutation sanity check (temporarily reverting `tools/bink_frame_aot.py` to `upstream/main`): `Ran 7 tests` -> `FAILED (failures=2, errors=2)` (`StopIteration` on missing anchor, missing `main`, tracebacks on missing/non-PE CLI inputs). - `dev` preset (`cmake --preset dev`, `ctest --preset dev -N` -> `Total Tests: 1251`): `ctest --preset dev --no-tests=error -R "^(bink_frame_aot_check|bink_heap_aot_check|cpu_aot_bink_heap|cpu_aot_bink_group)$" -V` -> `4/4` passed (`cpu_aot_bink_heap`, `cpu_aot_bink_group`, `bink_heap_aot_check` 12/12, `bink_frame_aot_check` 7/7). - `release` preset (`cmake --preset release`, `ctest --preset release -N` -> `Total Tests: 1251`): `ctest --preset release --no-tests=error -R "^(bink_frame_aot_check|bink_heap_aot_check|cpu_aot_bink_heap|cpu_aot_bink_group)$" -V` -> `4/4` passed. - Repo checks: `tests/form_coverage_check.py`, `tests/docs_links_check.py`, and `tests/bidi_controls_check.py` all exited `0`. ## Evidence and follow-up - `tests/bink_frame_aot_check.py` constructs a minimal synthetic AMD64 PE32+ DLL in memory (two leaf helpers, `BinkAllocateFrameBuffers` with the expected control-flow/call shape, export directory, `.pdata`, and `UNWIND_INFO`) with no external binaries or game files; `BINK_SHA256` is patched in-memory only for synthetic-image tests. - This change is AI-assisted. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [ ] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets.
upstream-sync closed this pull request 2026-11-24 23:03:19 +00:00
ChazY11 commented 2026-11-24 23:03:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T22:27:48Z.


Ran it and broke all five new behaviours

Windows 11 build 26300.9974:

out/pe-env/Scripts/python.exe -B tests/bink_frame_aot_check.py
Ran 7 tests in 4.349s
OK      exit 0

Mutations to tools/bink_frame_aot.py, each reverted:

mutation result
if first is None: → if False: (missing mov rdi, rdx → StopIteration) caught
if failure is None: → if False: (missing xor eax, eax → StopIteration) caught
if failure <= first: → if False: (inverted anchor order accepted) caught
except (OSError, ValueError, pefile.PEFormatError) → except (KeyboardInterrupt,) caught
parser.exit(1, …) → parser.exit(0, …) caught

The inverted-order case is the one I expected to slip: both anchors still exist, so a generator-based lookup would pass and the
slice would simply be taken over the wrong range, producing plausible C++ from the wrong bytes. The fixture refuses it.

Why the three anchor guards matter here specifically

next(...) without a default raises StopIteration, and inside a generator that is not an error a caller sees — it terminates the
enclosing iteration silently. A generate() that returned a truncated or empty translation instead of failing is exactly the
failure mode a report would not notice, so replacing it with ValueError plus a message is the right shape, and the three cases
are pinned independently.

What this does not establish

Nothing about the real package image: the fixture is a synthetic PE32+ with the anchors placed by the test. The SHA-256 profile
comparison is exercised only against the synthetic image, and the translated function is not linked or run. That matches the
description's scope.

One note

import pefile added at module level means the tool now fails at import time on a machine without pefile, rather than at the
first generate(). Since the check is registered as a CTest that runs the CLI, a missing dependency surfaces as a failed test
instead of a skip — if that is not wanted, the import could move inside generate(). Not a defect, just the usual question about
where a third-party import belongs in a tool that CI runs unconditionally.

Verdict

Reviewed. Three anchor refusals and both CLI behaviours are pinned, and the inverted-order case is the interesting one.

Notes

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7146259144 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1968#issuecomment-7146259144). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T22:27:48Z. --- ## Ran it and broke all five new behaviours Windows 11 build 26300.9974: ```powershell out/pe-env/Scripts/python.exe -B tests/bink_frame_aot_check.py Ran 7 tests in 4.349s OK exit 0 ``` Mutations to `tools/bink_frame_aot.py`, each reverted: | mutation | result | |---|---| | `if first is None:` → `if False:` (missing `mov rdi, rdx` → `StopIteration`) | **caught** | | `if failure is None:` → `if False:` (missing `xor eax, eax` → `StopIteration`) | **caught** | | `if failure <= first:` → `if False:` (inverted anchor order accepted) | **caught** | | `except (OSError, ValueError, pefile.PEFormatError)` → `except (KeyboardInterrupt,)` | **caught** | | `parser.exit(1, …)` → `parser.exit(0, …)` | **caught** | The inverted-order case is the one I expected to slip: both anchors still exist, so a generator-based lookup would pass and the slice would simply be taken over the wrong range, producing plausible C++ from the wrong bytes. The fixture refuses it. ## Why the three anchor guards matter here specifically `next(...)` without a default raises `StopIteration`, and inside a generator that is not an error a caller sees — it terminates the enclosing iteration silently. A `generate()` that returned a truncated or empty translation instead of failing is exactly the failure mode a report would not notice, so replacing it with `ValueError` plus a message is the right shape, and the three cases are pinned independently. ## What this does not establish Nothing about the real package image: the fixture is a synthetic PE32+ with the anchors placed by the test. The SHA-256 profile comparison is exercised only against the synthetic image, and the translated function is not linked or run. That matches the description's scope. ## One note `import pefile` added at module level means the tool now fails at import time on a machine without `pefile`, rather than at the first `generate()`. Since the check is registered as a CTest that runs the CLI, a missing dependency surfaces as a failed test instead of a skip — if that is not wanted, the import could move inside `generate()`. Not a defect, just the usual question about where a third-party import belongs in a tool that CI runs unconditionally. ## Verdict Reviewed. Three anchor refusals and both CLI behaviours are pinned, and the inverted-order case is the interesting one. ## Notes - AI-assisted review.
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
R0Y00/Xbox2Windows!1961
No description provided.