pe: harden atomic report writes and path/XML/mmap error guards in resource_inventory with CLI suite #2574

Merged
Ammmae0x merged 2 commits from upstream-pr/2582 into main 2026-11-25 22:02:35 +00:00
Ammmae0x commented 2026-11-25 21:45:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T21:45:47Z.


Summary

Hardens tools/resource_inventory.py with atomic report staging, clean main(argv=None) -> int return semantics, and explicit path/XML/mmap/validator error guards without raw tracebacks, backed by a standalone pure-Python CLI test suite registered in CTest.

Changes

  • tools/resource_inventory.py:
    • Update main(argv=None) -> int to return 0 and invoke raise SystemExit(main()) under __main__.
    • Replace the leaky NamedTemporaryFile(delete=False) write with _write_atomic_json() using tempfile.TemporaryDirectory(dir=output.parent) + os.replace(), rejecting existing directory targets, leaving zero temporary artifacts on failure, and preserving any pre-existing report if json.dump() or os.replace() fails.
    • Guard args.directory.resolve(strict=True) and root.is_dir(), wrap read_layout(layout) against (OSError, ET.ParseError, KeyError), guard --shader-control (missing/directory/ValueError), guard probe_archive() (OSError, ValueError on empty RPF9), and guard --scene-records/--scene-validator (missing/directory shaders.rpf, missing/directory validator, and (OSError, ValueError, KeyError, struct.error, subprocess.SubprocessError, json.JSONDecodeError)).
    • Exit cleanly with code 1 via parser.exit(1, f"resource_inventory: {exc}\n") (zero raw tracebacks) while preserving parser.error(...) exit 2 for layout*.xml count != 1 tested by tests/layout_chunks_check.py.
  • tests/resource_inventory_cli_check.py:
    • Add standalone pure-Python CLI, atomic-write, error-guard, and subprocess smoke tests running in isolated temporary workspaces.
  • CMakeLists.txt:
    • Register pe_resource_inventory_cli_check in CTest right after range_manifest_check.

Verification

All commands executed after rebasing onto upstream/main (8f045aee):

  • out/pe-env/Scripts/python.exe -B tests/resource_inventory_cli_check.py -v
    • Observed: Ran 7 tests in 0.895s / OK (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/layout_chunks_check.py
    • Observed: layout chunk summary ok (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/scene_records_check.py out/build/dev/DualUav.cso out/build/dev/triangle_ps.cso out/build/dev/root_probe.exe
    • Observed: exit 0 (no errors)
  • out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py
    • Observed: form coverage checks passed (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/docs_links_check.py
    • Observed: Self-test passed. Checked 19 Markdown files and 493 links. (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py
    • Observed: Self-test passed. Checked 1809 tracked source, script and build/CI files: no bidirectional controls. (exit 0)
  • Minimal temp-copy mutation testing (14 non-equivalent mutations across atomic staging/dir guard, root dir/OSError, layout XML/KeyError/OSError, shader-control ValueError, probe_archive ValueError, scene-records SubprocessError/struct.error/KeyError, output-inside-root guard, and main() return code): 14/14 killed.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6614390716 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2582). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T21:45:47Z. --- ## Summary Hardens `tools/resource_inventory.py` with atomic report staging, clean `main(argv=None) -> int` return semantics, and explicit path/XML/mmap/validator error guards without raw tracebacks, backed by a standalone pure-Python CLI test suite registered in CTest. ### Changes - **`tools/resource_inventory.py`**: - Update `main(argv=None) -> int` to return `0` and invoke `raise SystemExit(main())` under `__main__`. - Replace the leaky `NamedTemporaryFile(delete=False)` write with `_write_atomic_json()` using `tempfile.TemporaryDirectory(dir=output.parent)` + `os.replace()`, rejecting existing directory targets, leaving zero temporary artifacts on failure, and preserving any pre-existing report if `json.dump()` or `os.replace()` fails. - Guard `args.directory.resolve(strict=True)` and `root.is_dir()`, wrap `read_layout(layout)` against `(OSError, ET.ParseError, KeyError)`, guard `--shader-control` (missing/directory/`ValueError`), guard `probe_archive()` (`OSError`, `ValueError` on empty RPF9), and guard `--scene-records`/`--scene-validator` (missing/directory `shaders.rpf`, missing/directory validator, and `(OSError, ValueError, KeyError, struct.error, subprocess.SubprocessError, json.JSONDecodeError)`). - Exit cleanly with code `1` via `parser.exit(1, f"resource_inventory: {exc}\n")` (zero raw tracebacks) while preserving `parser.error(...)` exit `2` for `layout*.xml` count != 1 tested by `tests/layout_chunks_check.py`. - **`tests/resource_inventory_cli_check.py`**: - Add standalone pure-Python CLI, atomic-write, error-guard, and subprocess smoke tests running in isolated temporary workspaces. - **`CMakeLists.txt`**: - Register `pe_resource_inventory_cli_check` in CTest right after `range_manifest_check`. ## Verification All commands executed after rebasing onto `upstream/main` (`8f045aee`): - `out/pe-env/Scripts/python.exe -B tests/resource_inventory_cli_check.py -v` - Observed: `Ran 7 tests in 0.895s` / `OK` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/layout_chunks_check.py` - Observed: `layout chunk summary ok` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/scene_records_check.py out/build/dev/DualUav.cso out/build/dev/triangle_ps.cso out/build/dev/root_probe.exe` - Observed: exit 0 (no errors) - `out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py` - Observed: `form coverage checks passed` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/docs_links_check.py` - Observed: `Self-test passed. Checked 19 Markdown files and 493 links.` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` - Observed: `Self-test passed. Checked 1809 tracked source, script and build/CI files: no bidirectional controls.` (exit 0) - Minimal temp-copy mutation testing (14 non-equivalent mutations across atomic staging/dir guard, root dir/OSError, layout XML/KeyError/OSError, shader-control ValueError, probe_archive ValueError, scene-records SubprocessError/struct.error/KeyError, output-inside-root guard, and `main()` return code): 14/14 killed. AI-assisted.
tedin7 commented 2026-11-25 22:02:14 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T21:51:41Z.

Review: COMMENTED


Review of head 2bd359dc (comment only).

Code: tools/resource_inventory.py looks correct. main() returns 0, and every new guard exits 1 with a one-line message. The layout*.xml count check still uses parser.error (exit 2). _write_atomic_json stages the report in a temp dir next to the output, so a failed write leaves the old report and no temp files. No other open or merged PR covers this slice (#644 only added the single-layout check).

Test: tests/resource_inventory_cli_check.py (7 tests) is registered in CTest as pe_resource_inventory_cli_check. It checks the real CLI result and the exit codes.

Commands run on this head:

  • python3 tests/resource_inventory_cli_check.py -> 7 tests OK
  • python3 tests/layout_chunks_check.py -> ok
  • python3 tests/docs_links_check.py -> ok
  • python3 tests/bidi_controls_check.py -> ok
  • git diff --check origin/main...HEAD -> clean
  • Mutation 1: replace os.replace with a raise -> 2 failures
  • Mutation 2: drop the root.is_dir() guard -> 1 failure
  • Mutation 3: narrow the read_layout except clause -> 4 errors

No blocking findings. This does not claim any startup change. Windows runs follow separately.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6065473696 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2582#pullrequestreview-6065473696). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T21:51:41Z. **Review:** COMMENTED --- Review of head 2bd359dc (comment only). Code: `tools/resource_inventory.py` looks correct. `main()` returns 0, and every new guard exits 1 with a one-line message. The `layout*.xml` count check still uses `parser.error` (exit 2). `_write_atomic_json` stages the report in a temp dir next to the output, so a failed write leaves the old report and no temp files. No other open or merged PR covers this slice (#644 only added the single-layout check). Test: `tests/resource_inventory_cli_check.py` (7 tests) is registered in CTest as `pe_resource_inventory_cli_check`. It checks the real CLI result and the exit codes. Commands run on this head: - `python3 tests/resource_inventory_cli_check.py` -> 7 tests OK - `python3 tests/layout_chunks_check.py` -> ok - `python3 tests/docs_links_check.py` -> ok - `python3 tests/bidi_controls_check.py` -> ok - `git diff --check origin/main...HEAD` -> clean - Mutation 1: replace `os.replace` with a raise -> 2 failures - Mutation 2: drop the `root.is_dir()` guard -> 1 failure - Mutation 3: narrow the `read_layout` except clause -> 4 errors No blocking findings. This does not claim any startup change. Windows runs follow separately. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 23:01:53 +00:00
tedin7 commented 2026-11-25 23:02:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T22:02:25Z.


Win2025 WARP receipt for head cb0e5577 after updating with main.

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

  • Debug (--preset dev):
    • Build (cmake --build --preset dev -- -j 2): 150.91s (configure 2.69s)
    • Test (ctest --preset dev -R 'resource_inventory' --parallel 2 --no-tests=error --output-on-failure): 1/1 passed (1.88s: pe_resource_inventory_cli_check 1.70s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release -- -j 2): 192.80s (configure 3.28s)
    • Test (ctest --preset release -R 'resource_inventory' --parallel 2 --no-tests=error --output-on-failure): 1/1 passed (1.85s: pe_resource_inventory_cli_check 1.64s)
  • Windows Python checks: tests/dxcontainer_part_offsets_check.py ok (0.4s), tests/dxil_retarget_check.py ok (0.7s), tests/layout_chunks_check.py ok (0.8s)
  • sccache: 30/1550 hits; wall 360.9s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7175858233 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2582#issuecomment-7175858233). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T22:02:25Z. --- Win2025 WARP receipt for head cb0e5577 after updating with main. Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `cb0e5577fe037a5f2dd9df4c1d791280f126d514`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev -- -j 2`): `150.91s` (configure `2.69s`) - Test (`ctest --preset dev -R 'resource_inventory' --parallel 2 --no-tests=error --output-on-failure`): **1/1 passed** (`1.88s`: `pe_resource_inventory_cli_check` `1.70s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release -- -j 2`): `192.80s` (configure `3.28s`) - Test (`ctest --preset release -R 'resource_inventory' --parallel 2 --no-tests=error --output-on-failure`): **1/1 passed** (`1.85s`: `pe_resource_inventory_cli_check` `1.64s`) - **Windows Python checks**: `tests/dxcontainer_part_offsets_check.py` ok (`0.4s`), `tests/dxil_retarget_check.py` ok (`0.7s`), `tests/layout_chunks_check.py` ok (`0.8s`) - `sccache`: 30/1550 hits; wall `360.9s` 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!2574
No description provided.