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

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

@⁠Ammmae0x on GitHub · 2026-11-27 15:45 UTC · original
Ammmae0x:tooling/cpu-leaf-aot-cli → main · +763 −4 · 3 files · 1 commits · merged 2026-11-27 20:41 UTC

Change

Harden tools/cpu_leaf_aot.py CLI argument handling, path validation, and atomic output 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, and output does not alias image via resolve() or samefile()).
  • Replace direct write_text with sibling-temp atomic writes in _write_atomic (creating missing parent directories and unlinking temp files on any failure).
  • Guard translate(start, code) against boolean/negative start RVAs and non-bytes-like code while preserving existing export_rva, bounded_code, function_bytes, and translate behavior.
  • Add tests/cpu_leaf_aot_cli_check.py (unit, CLI, path-guard, symlink, atomic-write failure, malformed synthetic PE32+, and fast self-mutation tests) and register tooling_cpu_leaf_aot_cli_check (TIMEOUT 60) in tests/cmake/tooling_cpu_leaf_aot_cli.cmake.

Verification

Verified on commit 4db40e97 in the primary workspace:

  • out\pe-env\Scripts\python.exe -B tests/cpu_leaf_aot_cli_check.py -v -> Ran 8 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 tests/cpu_ranges_check.py -v -> Ran 5 tests ... OK (exit 0).
  • out\pe-env\Scripts\python.exe -B -c "import sys; sys.path.insert(0, 'tests/cpu'); import branches; branches.check_rejections()" -> 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 87 Markdown files and 586 links. (exit 0).
  • out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py -> Self-test passed. Checked 2293 tracked source, script and build/CI files: no bidirectional controls. (exit 0).
  • Spot mutation audit on tools/cpu_leaf_aot.py: 3/3 mutations killed (_validate_paths bypassed -> exit 1, 15 failures; _write_atomic replaced -> exit 1, 4 failures; start < 0 guard bypassed -> exit 1, 2 failures).

Evidence and follow-up

All synthetic PE32+ fixtures are generated directly in tests/cpu_leaf_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 15:45 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3111) `Ammmae0x:tooling/cpu-leaf-aot-cli` → `main` · +763 −4 · 3 files · 1 commits · merged 2026-11-27 20:41 UTC ## Change Harden `tools/cpu_leaf_aot.py` CLI argument handling, path validation, and atomic output 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, and output does not alias image via `resolve()` or `samefile()`). - Replace direct `write_text` with sibling-temp atomic writes in `_write_atomic` (creating missing parent directories and unlinking temp files on any failure). - Guard `translate(start, code)` against boolean/negative `start` RVAs and non-bytes-like `code` while preserving existing `export_rva`, `bounded_code`, `function_bytes`, and `translate` behavior. - Add `tests/cpu_leaf_aot_cli_check.py` (unit, CLI, path-guard, symlink, atomic-write failure, malformed synthetic PE32+, and fast self-mutation tests) and register `tooling_cpu_leaf_aot_cli_check` (`TIMEOUT 60`) in `tests/cmake/tooling_cpu_leaf_aot_cli.cmake`. ## Verification Verified on commit `4db40e97` in the primary workspace: - `out\pe-env\Scripts\python.exe -B tests/cpu_leaf_aot_cli_check.py -v` -> `Ran 8 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 tests/cpu_ranges_check.py -v` -> `Ran 5 tests ... OK` (exit 0). - `out\pe-env\Scripts\python.exe -B -c "import sys; sys.path.insert(0, 'tests/cpu'); import branches; branches.check_rejections()"` -> 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 87 Markdown files and 586 links.` (exit 0). - `out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py` -> `Self-test passed. Checked 2293 tracked source, script and build/CI files: no bidirectional controls.` (exit 0). - Spot mutation audit on `tools/cpu_leaf_aot.py`: 3/3 mutations killed (`_validate_paths` bypassed -> exit 1, 15 failures; `_write_atomic` replaced -> exit 1, 4 failures; `start < 0` guard bypassed -> exit 1, 2 failures). ## Evidence and follow-up All synthetic PE32+ fixtures are generated directly in `tests/cpu_leaf_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 15:54:32 +00:00
Author
Owner

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

**@⁠KelboLive** on GitHub · 2026-11-27 18:54 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3111#pullrequestreview-6092296628) · **approved these changes**
github-bot changed target branch from main to gh-pr/3111/base 2026-11-27 20:46:58 +00:00
github manually merged commit 4db40e978a into gh-pr/3111/base 2026-11-27 20:47:00 +00:00
Author
Owner

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

I reviewed 4db40e97 merged over current main 5fc9f6ff. The merge is clean (tree 7b1f56a5) and git diff --cached --check is clean.

What I traced.

  • Callers: the only command-line caller is cpu_translation(cpu_leaf_${variant} cpu_leaf_aot branch_value) in CMakeLists.txt. It writes <image>_lifted.cpp, so the new .cpp suffix rule and the path guards don't refuse anything the build does.
    • The ~190 importers use only export_rva and bounded_code, which are unchanged.
    • translate has one outside caller, tests/cpu/branches.py, and it passes an int and bytes.
  • Errors: _validate_paths raises ValueError, IsADirectoryError or NotADirectoryError. All three are caught by the existing except (ValueError, OSError, pefile.PEFormatError), so a refusal still exits 1 with one line on stderr, the same as main's parser.exit(1, ...).
  • Same output: on the built cpu_leaf_original.dll, main's tool and the PR's tool produce the same C++ apart from line endings (next point).

What I ran. I built cpu_leaf_original, cpu_leaf_inverted (and their _lifted DLLs) and cpu_flag_conditions in dev and release, then ran

ctest --test-dir out/build/<preset> --no-tests=error -R '^(tooling_cpu_leaf_aot_cli_check|cpu_leaf_aot_branches|cpu_aot_flag_conditions|cpu_ranges_check|form_coverage)$'

5/5 pass in both presets. out\pe-env\Scripts\python.exe -B tests/cpu_leaf_aot_cli_check.py -v runs 8 tests, OK, with no skips: this account can create symlinks, so test_output_symlink_rejected_upfront really ran.

Mutants in tools/cpu_leaf_aot.py, each run against tests/cpu_leaf_aot_cli_check.py:

Mutant Result
.suffix.lower() → .suffix killed (1 failure)
start < 0 → start < -1 killed (1)
os.replace → copy then unlink killed (3)
alias check keeps only resolve() killed (2)
alias check keeps only samefile() killed (1)
output is_dir() guard removed killed (1)
error path returns 0 killed (26)
newline="\n" removed survives
except BaseException → except Exception survives

Non-blocking notes.

  1. Line endings change on Windows. newline="\n" changes the generated file's bytes: main's write_text writes CRLF (55 line ends for branch_value), and the PR writes LF. The content is otherwise identical and MSVC doesn't care, but the body says existing behavior is preserved. A sentence in the body, or a test that pins LF output, would make it deliberate. No test pins it today (mutant above).
  2. BaseException cleanup isn't tested. The temp-file cleanup is only tested with Exception subclasses. A KeyboardInterrupt raised from the patched os.replace would cover the BaseException branch.
  3. Linux CI doesn't run the check. It is pure Python, but it is only registered with CTest. A tests/checks/python-linux/cpu_leaf_aot_cli.txt line would also run it on Linux, where the symlink case never skips.

Verdict: approve on 4db40e97.

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 4db40e97.

This reply was AI-assisted.

**@⁠pigalot** on GitHub · 2026-11-27 20:41 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3111#pullrequestreview-6093162329) · **approved these changes** I reviewed `4db40e97` merged over current `main` `5fc9f6ff`. The merge is clean (tree `7b1f56a5`) and `git diff --cached --check` is clean. **What I traced.** - **Callers:** the only command-line caller is `cpu_translation(cpu_leaf_${variant} cpu_leaf_aot branch_value)` in `CMakeLists.txt`. It writes `<image>_lifted.cpp`, so the new `.cpp` suffix rule and the path guards don't refuse anything the build does. - The ~190 importers use only `export_rva` and `bounded_code`, which are unchanged. - `translate` has one outside caller, `tests/cpu/branches.py`, and it passes an `int` and `bytes`. - **Errors:** `_validate_paths` raises `ValueError`, `IsADirectoryError` or `NotADirectoryError`. All three are caught by the existing `except (ValueError, OSError, pefile.PEFormatError)`, so a refusal still exits 1 with one line on stderr, the same as main's `parser.exit(1, ...)`. - **Same output:** on the built `cpu_leaf_original.dll`, main's tool and the PR's tool produce the same C++ apart from line endings (next point). **What I ran.** I built `cpu_leaf_original`, `cpu_leaf_inverted` (and their `_lifted` DLLs) and `cpu_flag_conditions` in `dev` and `release`, then ran ```powershell ctest --test-dir out/build/<preset> --no-tests=error -R '^(tooling_cpu_leaf_aot_cli_check|cpu_leaf_aot_branches|cpu_aot_flag_conditions|cpu_ranges_check|form_coverage)$' ``` 5/5 pass in both presets. `out\pe-env\Scripts\python.exe -B tests/cpu_leaf_aot_cli_check.py -v` runs 8 tests, OK, with no skips: this account can create symlinks, so `test_output_symlink_rejected_upfront` really ran. **Mutants** in `tools/cpu_leaf_aot.py`, each run against `tests/cpu_leaf_aot_cli_check.py`: | Mutant | Result | |---|---| | `.suffix.lower()` → `.suffix` | killed (1 failure) | | `start < 0` → `start < -1` | killed (1) | | `os.replace` → copy then unlink | killed (3) | | alias check keeps only `resolve()` | killed (2) | | alias check keeps only `samefile()` | killed (1) | | output `is_dir()` guard removed | killed (1) | | error path returns 0 | killed (26) | | `newline="\n"` removed | **survives** | | `except BaseException` → `except Exception` | **survives** | **Non-blocking notes.** 1. **Line endings change on Windows.** `newline="\n"` changes the generated file's bytes: main's `write_text` writes CRLF (55 line ends for `branch_value`), and the PR writes LF. The content is otherwise identical and MSVC doesn't care, but the body says existing behavior is preserved. A sentence in the body, or a test that pins LF output, would make it deliberate. No test pins it today (mutant above). 2. **`BaseException` cleanup isn't tested.** The temp-file cleanup is only tested with `Exception` subclasses. A `KeyboardInterrupt` raised from the patched `os.replace` would cover the `BaseException` branch. 3. **Linux CI doesn't run the check.** It is pure Python, but it is only registered with CTest. A `tests/checks/python-linux/cpu_leaf_aot_cli.txt` line would also run it on Linux, where the symlink case never skips. **Verdict:** approve on `4db40e97`. 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 `4db40e97`. This reply was AI-assisted.
This discussion has been locked. Commenting is limited to contributors.
No description provided.