ffx: add clean CLI error handling in ffx_create_context and unit and refusal suite #1903

Merged
Ammmae0x merged 2 commits from upstream-pr/1910 into main 2026-11-24 21:25:29 +00:00
Ammmae0x commented 2026-11-24 20:53:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T20:53:13Z.


Change

Add clean CLI error handling to tools/ffx_create_context.py and add a synthetic AMD64 PE32+ unit and refusal suite in tests/ffx_create_context_check.py registered as ffx_create_context_check in CMakeLists.txt:

  • Extract main(argv=None) in tools/ffx_create_context.py and wrap output validation and generate(args.loader) so OSError, ValueError, and pefile.PEFormatError exit cleanly via parser.exit(1, ...) instead of emitting raw tracebacks.
  • Preserve create_source() and generate() lowering contracts (null-host/allocator/zero-alloc checks returning 6, host->table->create output slot handling, and allocation-failure host->table->destroy rollback returning 5).
  • Add tests/ffx_create_context_check.py with positive controls (create_source() guards/rollback and synthetic PE32+ generate() / main() emission) and refusal controls (missing loader file, non-PE input with both unpatched and patched digest, SHA-256 profile mismatch, non-.cpp output suffix, and matching loader/output paths).

Verification

  • python -B tests/ffx_create_context_check.py -v: Ran 6 tests in 3.046s — OK.
  • cmake --preset dev + ctest --preset dev --no-tests=error -R "^ffx_create_context_check$" -V: 1/1 passed (Ran 6 tests in 2.940s, 3.63 sec); pre-rebase sibling smoke run ctest --preset dev --no-tests=error -R "^(ffx_create_context_check|ffx_provider_loop_warp|ffx_device_init_warp|ffx_recurrent_warp)$" --output-on-failure passed 4/4.
  • cmake --preset release + ctest --preset release --no-tests=error -R "^ffx_create_context_check$" -V: 1/1 passed (Ran 6 tests in 3.031s, 3.68 sec); pre-rebase sibling smoke run ctest --preset release --no-tests=error -R "^(ffx_create_context_check|ffx_provider_loop_warp|ffx_device_init_warp|ffx_recurrent_warp)$" --output-on-failure passed 4/4.
  • python -B tests/form_coverage_check.py: passed.
  • python -B tests/docs_links_check.py: passed (Checked 18 Markdown files and 377 links.).
  • python -B tests/bidi_controls_check.py: passed (Checked 1339 tracked source, script and build/CI files: no bidirectional controls.).

Evidence and follow-up

  • tests/ffx_create_context_check.py builds minimal in-memory synthetic AMD64 PE32+ binaries with struct and patches ffx_create_context.LOADER in unit tests; no external binaries or proprietary files are used.
  • Mutation sanity check reverting the try/except (OSError, ValueError, pefile.PEFormatError) wrapper in main() confirmed test_non_pe_input_exits_cleanly fails with an unhandled pefile.PEFormatError.

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. (N/A: CLI error handling and unit test suite only)
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6591185668 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1910). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T20:53:13Z. --- ## Change Add clean CLI error handling to `tools/ffx_create_context.py` and add a synthetic AMD64 PE32+ unit and refusal suite in `tests/ffx_create_context_check.py` registered as `ffx_create_context_check` in `CMakeLists.txt`: - Extract `main(argv=None)` in `tools/ffx_create_context.py` and wrap output validation and `generate(args.loader)` so `OSError`, `ValueError`, and `pefile.PEFormatError` exit cleanly via `parser.exit(1, ...)` instead of emitting raw tracebacks. - Preserve `create_source()` and `generate()` lowering contracts (null-host/allocator/zero-alloc checks returning `6`, `host->table->create` output slot handling, and allocation-failure `host->table->destroy` rollback returning `5`). - Add `tests/ffx_create_context_check.py` with positive controls (`create_source()` guards/rollback and synthetic PE32+ `generate()` / `main()` emission) and refusal controls (missing loader file, non-PE input with both unpatched and patched digest, SHA-256 profile mismatch, non-`.cpp` output suffix, and matching loader/output paths). ## Verification - `python -B tests/ffx_create_context_check.py -v`: `Ran 6 tests in 3.046s` — `OK`. - `cmake --preset dev` + `ctest --preset dev --no-tests=error -R "^ffx_create_context_check$" -V`: `1/1` passed (`Ran 6 tests in 2.940s`, `3.63 sec`); pre-rebase sibling smoke run `ctest --preset dev --no-tests=error -R "^(ffx_create_context_check|ffx_provider_loop_warp|ffx_device_init_warp|ffx_recurrent_warp)$" --output-on-failure` passed `4/4`. - `cmake --preset release` + `ctest --preset release --no-tests=error -R "^ffx_create_context_check$" -V`: `1/1` passed (`Ran 6 tests in 3.031s`, `3.68 sec`); pre-rebase sibling smoke run `ctest --preset release --no-tests=error -R "^(ffx_create_context_check|ffx_provider_loop_warp|ffx_device_init_warp|ffx_recurrent_warp)$" --output-on-failure` passed `4/4`. - `python -B tests/form_coverage_check.py`: passed. - `python -B tests/docs_links_check.py`: passed (`Checked 18 Markdown files and 377 links.`). - `python -B tests/bidi_controls_check.py`: passed (`Checked 1339 tracked source, script and build/CI files: no bidirectional controls.`). ## Evidence and follow-up - `tests/ffx_create_context_check.py` builds minimal in-memory synthetic AMD64 PE32+ binaries with `struct` and patches `ffx_create_context.LOADER` in unit tests; no external binaries or proprietary files are used. - Mutation sanity check reverting the `try/except (OSError, ValueError, pefile.PEFormatError)` wrapper in `main()` confirmed `test_non_pe_input_exits_cleanly` fails with an unhandled `pefile.PEFormatError`. ## 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. (N/A: CLI error handling and unit test suite only) - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
tedin7 commented 2026-11-24 21:01:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T20:57:34Z.

Review: COMMENTED


Review of #1910 at exact head 1d1f638bcdf6305ac7e0e1a50f90aede22960062 (checked out from pull/1910/head, HEAD equals current headRefOid; merge-base is behind current main but git merge-tree shows no conflicts).

Verdict: no P1/P2 found. Comment only, not an approval.

What I checked

  • Diff scope: tools/ffx_create_context.py (+14/-5, only the CLI entry is wrapped into main(argv); generate/create_source untouched), CMakeLists.txt (+2, one add_test inside the existing if(EXISTS "${INTAKE_PYTHON}") block), tests/ffx_create_context_check.py (new).
  • Clean-room: the test builds a fully synthetic PE32+ in code (no vendor bytes, no digests of real images); title is neutral.
  • Behavior: parser.error raises SystemExit so it is not swallowed by the new except; generate() runs before write_text, so a failed generate leaves no output file; UnicodeEncodeError is a ValueError, so it is covered too. Existing CMake caller (CMakeLists.txt:1863) and tests/cpu/create_fixture.py (imports create_source only) are not affected.

Commands run locally (venv /root/.local/share/x2w-rev-venv)

  • git diff --check origin/main...HEAD -> exit 0, no output
  • python -B tests/ffx_create_context_check.py -v -> Ran 6 tests ... OK
  • python -B tests/bidi_controls_check.py -> Checked 1339 tracked ... no bidirectional controls
  • python -B tests/docs_links_check.py -> Checked 18 Markdown files and 377 links
  • Windows lane (win2025) not run: the PR changes no generated output or Windows-built code, only the Python CLI wrapper, a Python test and a ctest registration.

Mutations (each applied to tools/ffx_create_context.py, suite re-run, then git checkout -- .)

Mutation Result
drop OSError from the except killed (test_missing_loader_exits_cleanly)
drop pefile.PEFormatError killed (test_non_pe_input_exits_cleanly)
parser.exit(1,...) -> parser.exit(0,...) killed (3 refusal tests)
rollback destroy(&inner, allocator) removed killed (2 tests)
rollback return 5; -> return 0; killed (2 tests)
allocator null/field guard removed killed
rollback bound only to calloc (not allocate) killed
store<void*>(provider_slot, inner) dropped killed
write empty output before generate (stale file on failure) killed (3 refusal tests)

9/9 killed.

Non-blocking notes (P3)

  • tests/ffx_create_context_check.py (positive tests): the checks are substring matches on the emitted C++. They prove the guard, create call and rollback text are emitted, but not that the C++ compiles or that the rollback runs on an allocation failure. The cpu_aot_context_lifecycle Windows test remains the real behavioral guard, so this is fine as a unit layer, just do not read it as proof of OOM behavior.
  • tools/ffx_create_context.py:main, pefile.PEFormatError branch: with the pinned LOADER SHA-256 this branch is only reachable when the test patches LOADER. That is fine for a defensive path; a one-line comment would stop a future reader from deleting it as dead code.
  • CMakeLists.txt: the new add_test has no TIMEOUT property, unlike the neighbouring pe_run_package_trap_missing. The suite runs in about 1.3 s here, so this is cosmetic.
  • The test needs iced_x86 in INTAKE_PYTHON; other tests/cpu/*.py already import it, so no new dependency.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6047281265 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1910#pullrequestreview-6047281265). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T20:57:34Z. **Review:** COMMENTED --- Review of #1910 at exact head `1d1f638bcdf6305ac7e0e1a50f90aede22960062` (checked out from `pull/1910/head`, HEAD equals current `headRefOid`; merge-base is behind current `main` but `git merge-tree` shows no conflicts). **Verdict: no P1/P2 found. Comment only, not an approval.** ### What I checked - Diff scope: `tools/ffx_create_context.py` (+14/-5, only the CLI entry is wrapped into `main(argv)`; `generate`/`create_source` untouched), `CMakeLists.txt` (+2, one `add_test` inside the existing `if(EXISTS "${INTAKE_PYTHON}")` block), `tests/ffx_create_context_check.py` (new). - Clean-room: the test builds a fully synthetic PE32+ in code (no vendor bytes, no digests of real images); title is neutral. - Behavior: `parser.error` raises `SystemExit` so it is not swallowed by the new `except`; `generate()` runs before `write_text`, so a failed generate leaves no output file; `UnicodeEncodeError` is a `ValueError`, so it is covered too. Existing CMake caller (`CMakeLists.txt:1863`) and `tests/cpu/create_fixture.py` (imports `create_source` only) are not affected. ### Commands run locally (venv `/root/.local/share/x2w-rev-venv`) - `git diff --check origin/main...HEAD` -> exit 0, no output - `python -B tests/ffx_create_context_check.py -v` -> `Ran 6 tests ... OK` - `python -B tests/bidi_controls_check.py` -> `Checked 1339 tracked ... no bidirectional controls` - `python -B tests/docs_links_check.py` -> `Checked 18 Markdown files and 377 links` - Windows lane (`win2025`) not run: the PR changes no generated output or Windows-built code, only the Python CLI wrapper, a Python test and a ctest registration. ### Mutations (each applied to `tools/ffx_create_context.py`, suite re-run, then `git checkout -- .`) | Mutation | Result | |---|---| | drop `OSError` from the except | killed (`test_missing_loader_exits_cleanly`) | | drop `pefile.PEFormatError` | killed (`test_non_pe_input_exits_cleanly`) | | `parser.exit(1,...)` -> `parser.exit(0,...)` | killed (3 refusal tests) | | rollback `destroy(&inner, allocator)` removed | killed (2 tests) | | rollback `return 5;` -> `return 0;` | killed (2 tests) | | allocator null/field guard removed | killed | | rollback bound only to `calloc` (not `allocate`) | killed | | `store<void*>(provider_slot, inner)` dropped | killed | | write empty output before `generate` (stale file on failure) | killed (3 refusal tests) | 9/9 killed. ### Non-blocking notes (P3) - `tests/ffx_create_context_check.py` (positive tests): the checks are substring matches on the emitted C++. They prove the guard, create call and rollback text are emitted, but not that the C++ compiles or that the rollback runs on an allocation failure. The `cpu_aot_context_lifecycle` Windows test remains the real behavioral guard, so this is fine as a unit layer, just do not read it as proof of OOM behavior. - `tools/ffx_create_context.py:main`, `pefile.PEFormatError` branch: with the pinned `LOADER` SHA-256 this branch is only reachable when the test patches `LOADER`. That is fine for a defensive path; a one-line comment would stop a future reader from deleting it as dead code. - `CMakeLists.txt`: the new `add_test` has no `TIMEOUT` property, unlike the neighbouring `pe_run_package_trap_missing`. The suite runs in about 1.3 s here, so this is cosmetic. - The test needs `iced_x86` in `INTAKE_PYTHON`; other `tests/cpu/*.py` already import it, so no new dependency. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-24 21:27:32 +00:00
R0Y00 commented 2026-11-24 21:27:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T21:21:31Z.


Resolved the CTest registration conflict with #1908 by retaining both test entries. On the integrated candidate, Release CTest passed ffx_create_context_check, bink_heap_aot_check and ffx_dispatch_profile_check (3/3).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7144823563 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1910#issuecomment-7144823563). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T21:21:31Z. --- Resolved the CTest registration conflict with #1908 by retaining both test entries. On the integrated candidate, Release CTest passed `ffx_create_context_check`, `bink_heap_aot_check` and `ffx_dispatch_profile_check` (3/3).
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!1903
No description provided.