cpu: harden cpu_chained_aot CLI validation and atomic writes #3131

Manually merged
github merged 1 commit from github-bot/gh-3129 into gh-pr/3129/base 2026-11-27 21:17:34 +00:00 AGit
Owner

@⁠Ammmae0x on GitHub · 2026-11-27 16:21 UTC · original
Ammmae0x:cpu/chained-aot-cli → main · +535 −6 · 4 files · 1 commits · merged 2026-11-27 21:15 UTC

Change

Harden tools/cpu_chained_aot.py CLI validation, callback-offset type checking, and atomic output writes, and add a standalone pure-Python synthetic PE32+ verification suite:

  • Extract main(argv=None) -> int and invoke raise SystemExit(main()) under if __name__ == "__main__".
  • Validate input/output paths upfront in _validate_cli_inputs (input image is an existing file, non-empty export name, .cpp output suffix, output is not a symlink or directory, output parent is not a file/non-directory, and output does not collide with the input image via resolve() or samefile()) and exit 1 cleanly on (ValueError, OSError, pefile.PEFormatError).
  • Guard translate() against empty/non-string export names and reject non-int callback_offset values (explicitly rejecting bool False/True via type(callback_offset) is not int) alongside 0 <= callback_offset <= 0x7fffffff and 8-byte alignment.
  • Replace direct write_text with sibling-temp atomic writes in _write_atomic, creating missing parent directories and closing/unlinking temp files on all failure paths.
  • Add tests/cpu_chained_aot_cli_check.py (synthetic chained, --partial, --callback-offset, --ffx-destroy, hostile paths, bool/negative/unaligned/overflow offset refusals, os.replace/os.fdopen failure cleanup, and 5 fast self-mutations) and register cpu_chained_aot_cli_check via tests/cmake/cpu_chained_aot_cli.cmake and tests/checks/python-linux/cpu_chained_aot_cli.txt.

Verification

Verified on commit dcf9f44e in slot-2:

  • out\pe-env\Scripts\python.exe -B tests/cpu_chained_aot_cli_check.py -v -> Ran 5 tests ... OK (exit 0, including 5 fast self-mutations).
  • ctest --preset dev --no-tests=error -R "^(cpu_chained_aot_cli_check|cpu_aot_chained_ranges|cpu_aot_object_callback|cpu_aot_chained_dead_reads)$" --output-on-failure -> 4/4 passed (exit 0).
  • out\pe-env\Scripts\python.exe -B tests/cpu/malformed_pe_check.py -> ok (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_chained_aot.py: 3/3 mutations killed (drop type(callback_offset) is not int -> exit 1; disable _validate_cli_inputs in main() -> exit 1; disable tmp_path.unlink in _write_atomic -> exit 1).

Evidence and follow-up

All synthetic PE32+ fixtures are generated directly in tests/cpu_chained_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:21 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3129) `Ammmae0x:cpu/chained-aot-cli` → `main` · +535 −6 · 4 files · 1 commits · merged 2026-11-27 21:15 UTC ## Change Harden `tools/cpu_chained_aot.py` CLI validation, callback-offset type checking, and atomic output writes, and add a standalone pure-Python synthetic PE32+ verification suite: - Extract `main(argv=None) -> int` and invoke `raise SystemExit(main())` under `if __name__ == "__main__"`. - Validate input/output paths upfront in `_validate_cli_inputs` (input image is an existing file, non-empty export name, `.cpp` output suffix, output is not a symlink or directory, output parent is not a file/non-directory, and output does not collide with the input image via `resolve()` or `samefile()`) and exit `1` cleanly on `(ValueError, OSError, pefile.PEFormatError)`. - Guard `translate()` against empty/non-string export names and reject non-`int` `callback_offset` values (explicitly rejecting `bool` `False`/`True` via `type(callback_offset) is not int`) alongside `0 <= callback_offset <= 0x7fffffff` and 8-byte alignment. - Replace direct `write_text` with sibling-temp atomic writes in `_write_atomic`, creating missing parent directories and closing/unlinking temp files on all failure paths. - Add `tests/cpu_chained_aot_cli_check.py` (synthetic chained, `--partial`, `--callback-offset`, `--ffx-destroy`, hostile paths, `bool`/negative/unaligned/overflow offset refusals, `os.replace`/`os.fdopen` failure cleanup, and 5 fast self-mutations) and register `cpu_chained_aot_cli_check` via `tests/cmake/cpu_chained_aot_cli.cmake` and `tests/checks/python-linux/cpu_chained_aot_cli.txt`. ## Verification Verified on commit `dcf9f44e` in `slot-2`: - `out\pe-env\Scripts\python.exe -B tests/cpu_chained_aot_cli_check.py -v` -> `Ran 5 tests ... OK` (exit 0, including 5 fast self-mutations). - `ctest --preset dev --no-tests=error -R "^(cpu_chained_aot_cli_check|cpu_aot_chained_ranges|cpu_aot_object_callback|cpu_aot_chained_dead_reads)$" --output-on-failure` -> `4/4 passed` (exit 0). - `out\pe-env\Scripts\python.exe -B tests/cpu/malformed_pe_check.py` -> `ok` (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_chained_aot.py`: 3/3 mutations killed (drop `type(callback_offset) is not int` -> exit 1; disable `_validate_cli_inputs` in `main()` -> exit 1; disable `tmp_path.unlink` in `_write_atomic` -> exit 1). ## Evidence and follow-up All synthetic PE32+ fixtures are generated directly in `tests/cpu_chained_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:27:56 +00:00
Author
Owner

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

Consistent hardening of cpu_chained_aot.py following the same pattern as recent CLI validation PRs. The extracted main(argv), path guards, atomic writes with proper cleanup, and explicit bool rejection for callback_offset are all correct. The synthetic PE32+ test suite covers hostile paths, collision detection, and mutation survival effectively. Approved.

**@⁠KelboLive** on GitHub · 2026-11-27 18:50 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3129#pullrequestreview-6092265429) · **approved these changes** Consistent hardening of `cpu_chained_aot.py` following the same pattern as recent CLI validation PRs. The extracted `main(argv)`, path guards, atomic writes with proper cleanup, and explicit bool rejection for callback_offset are all correct. The synthetic PE32+ test suite covers hostile paths, collision detection, and mutation survival effectively. Approved.
github-bot changed target branch from main to gh-pr/3129/base 2026-11-27 21:17:30 +00:00
github manually merged commit dcf9f44ef5 into gh-pr/3129/base 2026-11-27 21:17:34 +00:00
Author
Owner

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

I reviewed dcf9f44e merged over current main cf3df832. The merge is clean (tree cea67e3d) and git diff --cached --check is clean.

What I traced.

  • Command-line callers: the only ones are the cpu_translation(... cpu_chained_aot ...) rules: choose_value ×5, cpu_chained_dead_read, call_context CALLBACK_OFFSET 8, and release_context FFX_DESTROY. Each writes ${BIND_NAME}.cpp, so the new guards refuse nothing the build does.
  • Library callers: tests/cpu/callback_check.py, tests/cpu/chained_check.py and tests/cpu/chained_dead_read_check.py import translate. All three pass a string name and an int (or no) callback_offset, so the new name and type(callback_offset) is not int guards accept them.
  • _write_atomic: if os.fdopen raises, fd_open stays true and the descriptor is closed. Once the with block owns it, only the temp file is unlinked. Both paths are tested (see the close-fd and unlink mutants below).
  • Line endings: as in #3111 and #3128, the output changes from CRLF to LF on Windows, because main's write_text translates newlines.

What I ran. I did a full build in dev and release, then ran

ctest --test-dir out/build/<preset> --no-tests=error -j 8 -R '^(cpu_chained_aot_cli_check|cpu_aot_(destroy_contract|object_callback|chained_ranges|chained_dead_reads|fuzz_chains)|cpu_native_exe_.*chain|cpu_shim_chain_.*)$' -E '_hardware$'

10/10 pass in both presets. tests/cpu_chained_aot_cli_check.py -v runs 5 tests, OK, with no skips.

Mutants in tools/cpu_chained_aot.py, each run against the new check:

Mutant Result
type(...) is not int → isinstance (lets True through) killed
translate name guard removed killed
image is_file() guard removed killed
output symlink guard removed killed
output is_dir() guard removed killed
parent guard removed killed
samefile() alias check removed killed
temp unlink removed killed
os.close(fd) removed killed
offset upper bound 0x7fffffff → 0xffffffff killed
error path returns 0 killed
CLI export guard removed survives (equivalent: translate refuses the same name)
resolve() comparison removed survives (equivalent: is_file() means the image exists, so an alias that resolves to it also exists and samefile() catches it)
.suffix.lower() → .suffix survives
except BaseException → except Exception survives
newline="\n" → None survives

Non-blocking.

  1. Upper-case suffix: an out.CPP case would pin the case-insensitive suffix rule.
  2. Same two survivors as the sibling PRs: a KeyboardInterrupt raised from a patched os.replace would pin the BaseException cleanup, and a byte check would pin LF output.
  3. Parent check: it only looks at the immediate parent. A file further up still fails cleanly: mkdir raises an OSError, and the handler turns that into exit 1. So this is consistent, just less direct than the output.parents loop in #3111.

Verdict: approve on dcf9f44e.

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 dcf9f44e.

This reply was AI-assisted.

**@⁠pigalot** on GitHub · 2026-11-27 21:14 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3129#pullrequestreview-6093408122) · **approved these changes** I reviewed `dcf9f44e` merged over current `main` `cf3df832`. The merge is clean (tree `cea67e3d`) and `git diff --cached --check` is clean. **What I traced.** - **Command-line callers:** the only ones are the `cpu_translation(... cpu_chained_aot ...)` rules: `choose_value` ×5, `cpu_chained_dead_read`, `call_context CALLBACK_OFFSET 8`, and `release_context FFX_DESTROY`. Each writes `${BIND_NAME}.cpp`, so the new guards refuse nothing the build does. - **Library callers:** `tests/cpu/callback_check.py`, `tests/cpu/chained_check.py` and `tests/cpu/chained_dead_read_check.py` import `translate`. All three pass a string name and an `int` (or no) `callback_offset`, so the new `name` and `type(callback_offset) is not int` guards accept them. - **`_write_atomic`:** if `os.fdopen` raises, `fd_open` stays true and the descriptor is closed. Once the `with` block owns it, only the temp file is unlinked. Both paths are tested (see the `close-fd` and `unlink` mutants below). - **Line endings:** as in #3111 and #3128, the output changes from CRLF to LF on Windows, because main's `write_text` translates newlines. **What I ran.** I did a full build in `dev` and `release`, then ran ```powershell ctest --test-dir out/build/<preset> --no-tests=error -j 8 -R '^(cpu_chained_aot_cli_check|cpu_aot_(destroy_contract|object_callback|chained_ranges|chained_dead_reads|fuzz_chains)|cpu_native_exe_.*chain|cpu_shim_chain_.*)$' -E '_hardware$' ``` 10/10 pass in both presets. `tests/cpu_chained_aot_cli_check.py -v` runs 5 tests, OK, with no skips. **Mutants** in `tools/cpu_chained_aot.py`, each run against the new check: | Mutant | Result | |---|---| | `type(...) is not int` → `isinstance` (lets `True` through) | killed | | `translate` name guard removed | killed | | image `is_file()` guard removed | killed | | output symlink guard removed | killed | | output `is_dir()` guard removed | killed | | parent guard removed | killed | | `samefile()` alias check removed | killed | | temp unlink removed | killed | | `os.close(fd)` removed | killed | | offset upper bound `0x7fffffff` → `0xffffffff` | killed | | error path returns 0 | killed | | CLI export guard removed | survives (equivalent: `translate` refuses the same name) | | `resolve()` comparison removed | survives (equivalent: `is_file()` means the image exists, so an alias that resolves to it also exists and `samefile()` catches it) | | `.suffix.lower()` → `.suffix` | **survives** | | `except BaseException` → `except Exception` | **survives** | | `newline="\n"` → `None` | **survives** | **Non-blocking.** 1. **Upper-case suffix:** an `out.CPP` case would pin the case-insensitive suffix rule. 2. **Same two survivors as the sibling PRs:** a `KeyboardInterrupt` raised from a patched `os.replace` would pin the `BaseException` cleanup, and a byte check would pin LF output. 3. **Parent check:** it only looks at the immediate parent. A file further up still fails cleanly: `mkdir` raises an `OSError`, and the handler turns that into exit 1. So this is consistent, just less direct than the `output.parents` loop in #3111. **Verdict:** approve on `dcf9f44e`. 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 `dcf9f44e`. This reply was AI-assisted.
This discussion has been locked. Commenting is limited to contributors.
No description provided.