tooling: harden bink_descriptors_aot CLI path guards and atomic writes #2662

Merged
Ammmae0x merged 2 commits from upstream-pr/2670 into main 2026-11-26 03:07:52 +00:00
Ammmae0x commented 2026-11-26 01:38:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-26T01:38:29Z.


Change

Harden tools/bink_descriptors_aot.py CLI argument validation and atomic output writing, and add a pure-Python synthetic PE32+ CLI test suite (tests/bink_descriptors_aot_cli_check.py) registered in CMakeLists.txt as tooling_bink_descriptors_aot_cli_check (TIMEOUT 60).

  • Add _validate_args(args, parser) validating upfront before code generation that args.image is a regular file (rejecting directories and missing paths), and that args.output has a .cpp suffix, is not a symlink, directory, or non-directory-parent path, and is not resolve()-equal or samefile()-equal to args.image.
  • Add _write_atomic(path, content) creating missing parent directories, writing ASCII text to a sibling .tmp file, replacing path via os.replace, and unlinking the temporary file on any exception so failed writes leave zero .tmp files.
  • Catch (OSError, ValueError, struct.error, pefile.PEFormatError) cleanly in main() with non-zero exit and zero raw tracebacks.

Verification

  • Commit: 890a5841 (based on upstream/main 72879797)
  • out\pe-env\Scripts\python.exe -B tests\bink_descriptors_aot_cli_check.py -> Ran 8 tests in 11.088s / OK (exit 0)
  • out\pe-env\Scripts\python.exe -B tests\bink_descriptors_aot_check.py -> Ran 9 tests in 2.848s / 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 19 Markdown files and 510 links. (exit 0)
  • out\pe-env\Scripts\python.exe -B tests\bidi_controls_check.py -> Self-test passed. Checked 1894 tracked source, script and build/CI files: no bidirectional controls. (exit 0)
  • Spot-mutation gate: 3/3 mutations killed (args.output.is_symlink(), args.image.samefile(args.output), tmp_path.unlink(missing_ok=True)).
  • Negative controls: missing/directory/corrupt PE inputs, non-.cpp/empty/same-path/hardlink-samefile/directory/file-parent/ancestor-file/live-symlink/dangling-symlink outputs (verifying generate() and _write_atomic() are never called), simulated os.replace and write failures preserving existing target bytes with zero .tmp files, and Python 3.14-safe argparse checks.

Evidence and follow-up

All test inputs in tests/bink_descriptors_aot_cli_check.py use the synthetic AMD64 PE32+ builder from tests/bink_descriptors_aot_check.py under the repository MIT license; no external binaries or game files are used. Existing tests/bink_descriptors_aot_check.py and generated C++ output remain unchanged.

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6617541127 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2670). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-26T01:38:29Z. --- ## Change Harden `tools/bink_descriptors_aot.py` CLI argument validation and atomic output writing, and add a pure-Python synthetic PE32+ CLI test suite (`tests/bink_descriptors_aot_cli_check.py`) registered in `CMakeLists.txt` as `tooling_bink_descriptors_aot_cli_check` (`TIMEOUT 60`). - Add `_validate_args(args, parser)` validating upfront before code generation that `args.image` is a regular file (rejecting directories and missing paths), and that `args.output` has a `.cpp` suffix, is not a symlink, directory, or non-directory-parent path, and is not `resolve()`-equal or `samefile()`-equal to `args.image`. - Add `_write_atomic(path, content)` creating missing parent directories, writing ASCII text to a sibling `.tmp` file, replacing `path` via `os.replace`, and unlinking the temporary file on any exception so failed writes leave zero `.tmp` files. - Catch `(OSError, ValueError, struct.error, pefile.PEFormatError)` cleanly in `main()` with non-zero exit and zero raw tracebacks. ## Verification - Commit: `890a5841` (based on `upstream/main` `72879797`) - `out\pe-env\Scripts\python.exe -B tests\bink_descriptors_aot_cli_check.py` -> `Ran 8 tests in 11.088s / OK` (exit 0) - `out\pe-env\Scripts\python.exe -B tests\bink_descriptors_aot_check.py` -> `Ran 9 tests in 2.848s / 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 19 Markdown files and 510 links.` (exit 0) - `out\pe-env\Scripts\python.exe -B tests\bidi_controls_check.py` -> `Self-test passed. Checked 1894 tracked source, script and build/CI files: no bidirectional controls.` (exit 0) - Spot-mutation gate: 3/3 mutations killed (`args.output.is_symlink()`, `args.image.samefile(args.output)`, `tmp_path.unlink(missing_ok=True)`). - Negative controls: missing/directory/corrupt PE inputs, non-`.cpp`/empty/same-path/hardlink-samefile/directory/file-parent/ancestor-file/live-symlink/dangling-symlink outputs (verifying `generate()` and `_write_atomic()` are never called), simulated `os.replace` and write failures preserving existing target bytes with zero `.tmp` files, and Python 3.14-safe argparse checks. ## Evidence and follow-up All test inputs in `tests/bink_descriptors_aot_cli_check.py` use the synthetic AMD64 PE32+ builder from `tests/bink_descriptors_aot_check.py` under the repository MIT license; no external binaries or game files are used. Existing `tests/bink_descriptors_aot_check.py` and generated C++ output remain unchanged. ## 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.
tedin7 commented 2026-11-26 03:03:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T03:00:50Z.

Review: COMMENTED


Review of head 890a5841 (bink_descriptors_aot.py CLI path guards and atomic writes).

Result: no blocking findings.

What I checked

  • _validate_args: image must be a file; output must end in .cpp, must not be a symlink or directory, parents must be directories; output equal to the image (resolve/samefile) is refused. All covered by the new CLI check.
  • _write_atomic: temp file in the output directory, os.replace, temp unlinked on failure. main now reports OSError, ValueError, struct.error and pefile.PEFormatError as a clean error instead of a traceback.
  • Sibling: #1950 (merged) added the earlier CLI hardening and unit suite; this PR extends it and does not duplicate it. No other open PR touches this tool.
  • Registration: CMake test tooling_bink_descriptors_aot_cli_check added.
  • Mutation controls (applied, then reverted): replacing os.replace with a plain write_text makes the new check fail (2 failures); disabling the symlink guard makes it fail (2 failures).

Non-blocking notes

  • The temp file is created with mode 0600, so a newly written output (and a replaced 0644 file) ends up 0600. The old write_text gave the umask default. Harmless for a generated .cpp in a build tree, but a chmod to the umask default would keep the old behavior.
  • Behavior change: symlink outputs are now rejected. Intended per the PR text, just worth knowing for existing scripts.

Commands run (Linux, worktree at the PR head)

  • python3 -B tests/bink_descriptors_aot_cli_check.py (8 OK), tests/bink_descriptors_aot_check.py (9 OK), tests/form_coverage_check.py, tests/docs_links_check.py, tests/bidi_controls_check.py: all pass
  • git diff --check origin/main...HEAD: clean

Win2025 (Build 26100, MSVC 14.44, D3D12 WARP) at 890a5841: x2w_pipe.py win2025 2670 --wait 7200 --keep-release: PASS (debug and release builds).

The PR is 43 commits behind main. I did not update the branch. Merge left to the maintainers.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6067970045 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2670#pullrequestreview-6067970045). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T03:00:50Z. **Review:** COMMENTED --- Review of head 890a5841 (`bink_descriptors_aot.py` CLI path guards and atomic writes). **Result: no blocking findings.** What I checked - `_validate_args`: image must be a file; output must end in `.cpp`, must not be a symlink or directory, parents must be directories; output equal to the image (resolve/samefile) is refused. All covered by the new CLI check. - `_write_atomic`: temp file in the output directory, `os.replace`, temp unlinked on failure. `main` now reports `OSError`, `ValueError`, `struct.error` and `pefile.PEFormatError` as a clean error instead of a traceback. - Sibling: #1950 (merged) added the earlier CLI hardening and unit suite; this PR extends it and does not duplicate it. No other open PR touches this tool. - Registration: CMake test `tooling_bink_descriptors_aot_cli_check` added. - Mutation controls (applied, then reverted): replacing `os.replace` with a plain `write_text` makes the new check fail (2 failures); disabling the symlink guard makes it fail (2 failures). Non-blocking notes - The temp file is created with mode 0600, so a newly written output (and a replaced 0644 file) ends up 0600. The old `write_text` gave the umask default. Harmless for a generated `.cpp` in a build tree, but a `chmod` to the umask default would keep the old behavior. - Behavior change: symlink outputs are now rejected. Intended per the PR text, just worth knowing for existing scripts. Commands run (Linux, worktree at the PR head) - `python3 -B tests/bink_descriptors_aot_cli_check.py` (8 OK), `tests/bink_descriptors_aot_check.py` (9 OK), `tests/form_coverage_check.py`, `tests/docs_links_check.py`, `tests/bidi_controls_check.py`: all pass - `git diff --check origin/main...HEAD`: clean Win2025 (Build 26100, MSVC 14.44, D3D12 WARP) at 890a5841: `x2w_pipe.py win2025 2670 --wait 7200 --keep-release`: PASS (debug and release builds). The PR is 43 commits behind main. I did not update the branch. Merge left to the maintainers. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-26 04:04:13 +00:00
tedin7 commented 2026-11-26 04:04:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T03:05:00Z.

Review: COMMENTED


Review of head f77c41b8 (main merged in), comment only.

Checked:

  • _validate_args in tools/bink_descriptors_aot.py rejects directory or missing image, non-.cpp output, symlink or directory output, non-directory parents, and output equal to the image (by resolve() and samefile()). All run before code generation.
  • _write_atomic writes a sibling temp file and uses os.replace. The temp file is removed on any exception, so failures leave no .tmp.
  • main() now also catches struct.error, so malformed images exit 1 without a traceback.
  • It extends #1950 (the merged CLI/unit suite); no other open PR covers this slice. New test tooling_bink_descriptors_aot_cli_check sits next to the existing bink_descriptors_aot_check.
  • Win2025 receipt for this head is in the next comment: dev and release both 3/3.

Commands run locally: git diff --check origin/main...HEAD. I read the new code paths by hand. The suite needs pefile, which is not installed on the Linux box, so I did not run mutations there; the suite ran on Win2025 only.

Minor, not blocking: NamedTemporaryFile creates the file with mode 0600, so the replaced .cpp keeps that mode on POSIX. Harmless on Windows.

No blocking findings.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6067999406 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2670#pullrequestreview-6067999406). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T03:05:00Z. **Review:** COMMENTED --- Review of head f77c41b8 (main merged in), comment only. Checked: - `_validate_args` in `tools/bink_descriptors_aot.py` rejects directory or missing image, non-`.cpp` output, symlink or directory output, non-directory parents, and output equal to the image (by `resolve()` and `samefile()`). All run before code generation. - `_write_atomic` writes a sibling temp file and uses `os.replace`. The temp file is removed on any exception, so failures leave no `.tmp`. - `main()` now also catches `struct.error`, so malformed images exit 1 without a traceback. - It extends #1950 (the merged CLI/unit suite); no other open PR covers this slice. New test `tooling_bink_descriptors_aot_cli_check` sits next to the existing `bink_descriptors_aot_check`. - Win2025 receipt for this head is in the next comment: dev and release both 3/3. Commands run locally: `git diff --check origin/main...HEAD`. I read the new code paths by hand. The suite needs `pefile`, which is not installed on the Linux box, so I did not run mutations there; the suite ran on Win2025 only. Minor, not blocking: `NamedTemporaryFile` creates the file with mode 0600, so the replaced `.cpp` keeps that mode on POSIX. Harmless on Windows. No blocking findings. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-26 04:04:35 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T03:05:02Z.


Win2025 WARP receipt for head f77c41b8 after updating with main.

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit f77c41b820de3bb1876aee7c92582b096c0b1be4:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target cpu_bink_descriptors_lifted cpu_bink_frame_to_heap_objects -- -j 2): 1.07s (configure 3.65s)
    • Test (ctest --preset dev -R 'bink_descriptors' --parallel 2 --no-tests=error --output-on-failure): 3/3 passed (9.60s: cpu_bink_descriptors 0.15s, bink_descriptors_aot_check 2.68s, tooling_bink_descriptors_aot_cli_check 9.21s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target cpu_bink_descriptors_lifted cpu_bink_frame_to_heap_objects -- -j 2): 9.62s (configure 3.37s)
    • Test (ctest --preset release -R 'bink_descriptors' --parallel 2 --no-tests=error --output-on-failure): 3/3 passed (8.93s: cpu_bink_descriptors 0.12s, bink_descriptors_aot_check 2.74s, tooling_bink_descriptors_aot_cli_check 8.57s)
  • sccache: 57/59 hits; wall 34.3s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7181379739 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2670#issuecomment-7181379739). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T03:05:02Z. --- Win2025 WARP receipt for head f77c41b8 after updating with main. Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `f77c41b820de3bb1876aee7c92582b096c0b1be4`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev --target cpu_bink_descriptors_lifted cpu_bink_frame_to_heap_objects -- -j 2`): `1.07s` (configure `3.65s`) - Test (`ctest --preset dev -R 'bink_descriptors' --parallel 2 --no-tests=error --output-on-failure`): **3/3 passed** (`9.60s`: `cpu_bink_descriptors` `0.15s`, `bink_descriptors_aot_check` `2.68s`, `tooling_bink_descriptors_aot_cli_check` `9.21s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release --target cpu_bink_descriptors_lifted cpu_bink_frame_to_heap_objects -- -j 2`): `9.62s` (configure `3.37s`) - Test (`ctest --preset release -R 'bink_descriptors' --parallel 2 --no-tests=error --output-on-failure`): **3/3 passed** (`8.93s`: `cpu_bink_descriptors` `0.12s`, `bink_descriptors_aot_check` `2.74s`, `tooling_bink_descriptors_aot_cli_check` `8.57s`) - `sccache`: 57/59 hits; wall `34.3s` AI-assisted; commands listed were executed locally.
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!2662
No description provided.