tooling: harden atomic writes, path guards, and CLI entry in cpu_call_aot with synthetic PE32+ suite #3128

Manually merged
github merged 1 commit from github-bot/gh-3126 into gh-pr/3126/base 2026-11-27 20:59:09 +00:00 AGit
Owner

@⁠Ammmae0x on GitHub · 2026-11-27 16:18 UTC · original
Ammmae0x:cpu/call-aot-cli → main · +988 −6 · 4 files · 1 commits · merged 2026-11-27 20:55 UTC

Change

Harden tools/cpu_call_aot.py CLI validation, import-binding guards, and atomic file writes, and add a standalone pure-Python synthetic PE32+ verification suite:

  • Extract main(argv=None) -> int and run raise SystemExit(main()) under if __name__ == "__main__".
  • Validate inputs and output paths upfront in _validate_paths (image exists and is not a directory, non-empty export string, .cpp output suffix, output is not a symlink or directory, no output ancestor is a file/non-directory, valid --bind/--link-name combinations, and output does not alias image or provider via resolve() or samefile()).
  • Guard empty/blank link_name and non-file provider paths directly in resolve_import() and translate().
  • Replace direct write_text with sibling-temp atomic writes in _write_atomic (creating missing parent directories and unlinking temp files on any failure).
  • Add tests/cpu_call_aot_cli_check.py (direct & ordinal IAT --link-name synthesis, mixed/overlapping/non-pointer R8/out-of-trace/non-exec refusals, hostile paths, os.replace failure, and fast self-mutation tests) and register cpu_call_aot_cli_check via tests/cmake/cpu_call_aot_cli.cmake and tests/checks/python-linux/cpu_call_aot_cli.txt.

Verification

Verified on commit 48f0572c in the primary workspace:

  • out\pe-env\Scripts\python.exe -B tests/cpu_call_aot_cli_check.py -v -> Ran 7 tests ... OK (exit 0, including 10 fast mutations).
  • out\pe-env\Scripts\python.exe -B tests/cpu/malformed_pe_check.py -> ok (exit 0).
  • out\pe-env\Scripts\python.exe -B -c "import sys; sys.path.insert(0, 'tests/cpu'); import repeat_call_check; repeat_call_check.check_mixed_calls()" -> mixed direct/import calls and two distinct IAT slots are rejected (exit 0).
  • out\pe-env\Scripts\python.exe -B tests/run_checks_fragments_check.py -> run_checks_fragments_check ok (exit 0).
  • out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py -> form coverage checks passed (exit 0).
  • out\pe-env\Scripts\python.exe -B tests/docs_links_check.py -> Self-test passed. Checked 96 Markdown files and 586 links. (exit 0).
  • out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py -> Self-test passed. Checked 2311 tracked source, script and build/CI files: no bidirectional controls. (exit 0).
  • Spot mutation audit on tools/cpu_call_aot.py: 3/3 mutations killed (_validate_paths bypassed -> exit 1, 25 failures; link_name empty guard bypassed in resolve_import() -> exit 1, 2 failures; _write_atomic replaced -> exit 1, 4 failures).

Evidence and follow-up

All synthetic PE32+ fixtures are generated directly in tests/cpu_call_aot_cli_check.py; no external binaries or game files are used.

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.

AI-assisted.

**@⁠Ammmae0x** on GitHub · 2026-11-27 16:18 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3126) `Ammmae0x:cpu/call-aot-cli` → `main` · +988 −6 · 4 files · 1 commits · merged 2026-11-27 20:55 UTC ## Change Harden `tools/cpu_call_aot.py` CLI validation, import-binding guards, and atomic file writes, and add a standalone pure-Python synthetic PE32+ verification suite: - Extract `main(argv=None) -> int` and run `raise SystemExit(main())` under `if __name__ == "__main__"`. - Validate inputs and output paths upfront in `_validate_paths` (image exists and is not a directory, non-empty export string, `.cpp` output suffix, output is not a symlink or directory, no output ancestor is a file/non-directory, valid `--bind`/`--link-name` combinations, and output does not alias image or provider via `resolve()` or `samefile()`). - Guard empty/blank `link_name` and non-file provider paths directly in `resolve_import()` and `translate()`. - Replace direct `write_text` with sibling-temp atomic writes in `_write_atomic` (creating missing parent directories and unlinking temp files on any failure). - Add `tests/cpu_call_aot_cli_check.py` (direct & ordinal IAT `--link-name` synthesis, mixed/overlapping/non-pointer R8/out-of-trace/non-exec refusals, hostile paths, `os.replace` failure, and fast self-mutation tests) and register `cpu_call_aot_cli_check` via `tests/cmake/cpu_call_aot_cli.cmake` and `tests/checks/python-linux/cpu_call_aot_cli.txt`. ## Verification Verified on commit `48f0572c` in the primary workspace: - `out\pe-env\Scripts\python.exe -B tests/cpu_call_aot_cli_check.py -v` -> `Ran 7 tests ... OK` (exit 0, including 10 fast mutations). - `out\pe-env\Scripts\python.exe -B tests/cpu/malformed_pe_check.py` -> `ok` (exit 0). - `out\pe-env\Scripts\python.exe -B -c "import sys; sys.path.insert(0, 'tests/cpu'); import repeat_call_check; repeat_call_check.check_mixed_calls()"` -> `mixed direct/import calls and two distinct IAT slots are rejected` (exit 0). - `out\pe-env\Scripts\python.exe -B tests/run_checks_fragments_check.py` -> `run_checks_fragments_check ok` (exit 0). - `out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py` -> `form coverage checks passed` (exit 0). - `out\pe-env\Scripts\python.exe -B tests/docs_links_check.py` -> `Self-test passed. Checked 96 Markdown files and 586 links.` (exit 0). - `out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py` -> `Self-test passed. Checked 2311 tracked source, script and build/CI files: no bidirectional controls.` (exit 0). - Spot mutation audit on `tools/cpu_call_aot.py`: 3/3 mutations killed (`_validate_paths` bypassed -> exit 1, 25 failures; `link_name` empty guard bypassed in `resolve_import()` -> exit 1, 2 failures; `_write_atomic` replaced -> exit 1, 4 failures). ## Evidence and follow-up All synthetic PE32+ fixtures are generated directly in `tests/cpu_call_aot_cli_check.py`; no external binaries or game files are used. ## 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. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 16:21:00 +00:00
Author
Owner

@⁠KelboLive on GitHub · 2026-11-27 18:50 UTC · original · approved these changes

Consistent hardening of cpu_call_aot.py matching the established pattern: extracted main(argv), comprehensive path validation, atomic writes with proper cleanup, and thorough synthetic PE32+ test suite covering hostile paths, binding guards, and mutation survival. Approved.

**@⁠KelboLive** on GitHub · 2026-11-27 18:50 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3126#pullrequestreview-6092267465) · **approved these changes** Consistent hardening of `cpu_call_aot.py` matching the established pattern: extracted `main(argv)`, comprehensive path validation, atomic writes with proper cleanup, and thorough synthetic PE32+ test suite covering hostile paths, binding guards, and mutation survival. Approved.
github-bot changed target branch from main to gh-pr/3126/base 2026-11-27 20:59:07 +00:00
github manually merged commit 48f0572ce6 into gh-pr/3126/base 2026-11-27 20:59:09 +00:00
Author
Owner

@⁠pigalot on GitHub · 2026-11-27 20:55 UTC · original · approved these changes

I reviewed 48f0572c merged over current main 9e7eab5b. The merge is clean (tree 01677bc0) and git diff --cached --check is clean.

What I traced.

  • Command-line callers: the only ones are the cpu_translation(... cpu_call_aot ...) rules in CMakeLists.txt (call_mix, call_twice, call_pair, and the PROVIDER variants). Each writes ${BIND_NAME}.cpp, and --bind always gets a built provider DLL, so the new upfront guards refuse nothing the build does.

  • resolve_import: it is also imported by cpu_exe_aot.py, cpu_switch_aot.py and tests/cpu/import_check.py. The new Path(provider) and the link_name guard accept every call they make.

  • Changed error messages: translate now checks the provider before parsing the image. Two refusals therefore report missing provider for <dll>!<symbol> when the provider is absent, where main gave a different message:

    • a direct-call image with a binding (main: provider binding supplied for a direct call);
    • an unbound import (main: unbound import ...).

    Both are still refused, and no existing test pins the older message.

  • Line endings: as in #3111, the output changes from CRLF to LF on Windows (main's write_text translates newlines).

What I ran.

  • dev: full build, then

    ctest --test-dir out/build/dev --no-tests=error -j 8 -R '^(cpu_|tooling_cpu)' -E '_hardware$|startup_audio'
    

    All 300 pass, including cpu_call_aot_cli_check. One capability skip, cpu_aot_sysdesc, doesn't use this tool.

  • release: full build, then the call tests: cpu_call_aot_cli_check, cpu_aot_direct_call, cpu_aot_call_pair, cpu_aot_repeated_call, cpu_aot_import_call, cpu_aot_ordinal_import, cpu_aot_import_tail_jump, cpu_aot_exe_call_targets and cpu_aot_exe_nested_calls. 9/9 pass.

Mutants in tools/cpu_call_aot.py, each run against tests/cpu_call_aot_cli_check.py:

Mutant Result
provider dropped from the output-alias inputs killed (3)
translate provider is_file() check removed killed (1)
_validate_paths provider is_file() check removed killed (2)
resolve_import empty link_name guard removed killed (2)
"link name requires a provider binding" removed from _validate_paths killed (1)
"provider binding supplied for a direct call" removed killed (1)
.suffix.lower() → .suffix killed (1)
alias check keeps only resolve() killed (3)
alias check keeps only samefile() killed (1)
error path returns 0 killed (29)
except BaseException → except Exception survives
newline="\n" removed survives

Non-blocking.

  1. Same two survivors as #3111. A KeyboardInterrupt raised from the patched os.replace would pin the BaseException cleanup, and a byte check would pin LF output.
  2. Validation is written twice. The binding and link_name checks appear in both _validate_paths and translate. The copy in translate covers library callers, so keeping both is defensible, but one shared helper would stop the two copies from drifting apart.

Verdict: approve on 48f0572c.

Environment: Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; Python 3.12.10.
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on 48f0572c.

This reply was AI-assisted.

**@⁠pigalot** on GitHub · 2026-11-27 20:55 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3126#pullrequestreview-6093267058) · **approved these changes** I reviewed `48f0572c` merged over current `main` `9e7eab5b`. The merge is clean (tree `01677bc0`) and `git diff --cached --check` is clean. **What I traced.** - **Command-line callers:** the only ones are the `cpu_translation(... cpu_call_aot ...)` rules in `CMakeLists.txt` (`call_mix`, `call_twice`, `call_pair`, and the `PROVIDER` variants). Each writes `${BIND_NAME}.cpp`, and `--bind` always gets a built provider DLL, so the new upfront guards refuse nothing the build does. - **`resolve_import`:** it is also imported by `cpu_exe_aot.py`, `cpu_switch_aot.py` and `tests/cpu/import_check.py`. The new `Path(provider)` and the `link_name` guard accept every call they make. - **Changed error messages:** `translate` now checks the provider before parsing the image. Two refusals therefore report `missing provider for <dll>!<symbol>` when the provider is absent, where main gave a different message: - a direct-call image with a binding (main: `provider binding supplied for a direct call`); - an unbound import (main: `unbound import ...`). Both are still refused, and no existing test pins the older message. - **Line endings:** as in #3111, the output changes from CRLF to LF on Windows (main's `write_text` translates newlines). **What I ran.** - **`dev`:** full build, then ```powershell ctest --test-dir out/build/dev --no-tests=error -j 8 -R '^(cpu_|tooling_cpu)' -E '_hardware$|startup_audio' ``` All 300 pass, including `cpu_call_aot_cli_check`. One capability skip, `cpu_aot_sysdesc`, doesn't use this tool. - **`release`:** full build, then the call tests: `cpu_call_aot_cli_check`, `cpu_aot_direct_call`, `cpu_aot_call_pair`, `cpu_aot_repeated_call`, `cpu_aot_import_call`, `cpu_aot_ordinal_import`, `cpu_aot_import_tail_jump`, `cpu_aot_exe_call_targets` and `cpu_aot_exe_nested_calls`. 9/9 pass. **Mutants** in `tools/cpu_call_aot.py`, each run against `tests/cpu_call_aot_cli_check.py`: | Mutant | Result | |---|---| | provider dropped from the output-alias inputs | killed (3) | | `translate` provider `is_file()` check removed | killed (1) | | `_validate_paths` provider `is_file()` check removed | killed (2) | | `resolve_import` empty `link_name` guard removed | killed (2) | | "link name requires a provider binding" removed from `_validate_paths` | killed (1) | | "provider binding supplied for a direct call" removed | killed (1) | | `.suffix.lower()` → `.suffix` | killed (1) | | alias check keeps only `resolve()` | killed (3) | | alias check keeps only `samefile()` | killed (1) | | error path returns 0 | killed (29) | | `except BaseException` → `except Exception` | **survives** | | `newline="\n"` removed | **survives** | **Non-blocking.** 1. **Same two survivors as #3111.** A `KeyboardInterrupt` raised from the patched `os.replace` would pin the `BaseException` cleanup, and a byte check would pin LF output. 2. **Validation is written twice.** The binding and `link_name` checks appear in both `_validate_paths` and `translate`. The copy in `translate` covers library callers, so keeping both is defensible, but one shared helper would stop the two copies from drifting apart. **Verdict:** approve on `48f0572c`. Environment: Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; Python 3.12.10. CI: `Build and test on Windows`, `Assemble and translate gfx1013 fixtures` and `Run platform-independent Python checks on Linux` pass on `48f0572c`. This reply was AI-assisted.
This discussion has been locked. Commenting is limited to contributors.
No description provided.