runtime: add main(argv), atomic --output writes, and directory/error guards in xmem_attribute_scan with CLI suite #2421
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2429"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T16:28:19Z.
Change
Add a testable
main(argv=None) -> intentrypoint, upfront directory/collision validation, atomic--outputwrites, PE handle cleanup, and structured exit-code-2 error handling totools/xmem_attribute_scan.py, with a standalone pure-Python CLI test suite registered inCMakeLists.txt(xmem_attribute_scan_cli_check).tools/xmem_attribute_scan.py:argv: list[str] | None = Noneinmain, forwardargvtoparser.parse_args(argv), and invokeraise SystemExit(main())under__main__.--outputpointing to an existing directory or colliding withimage(_same_pathchecking bothPath.resolve()andPath.samefile()), and rejectimagebeing a directory before scanning.--outputJSON atomically viatempfile.TemporaryDirectory(dir=destination.parent)+os.replace()(_atomic_write_text), leaving zero temporary directories or partial files on failure.pefile.PEhandles deterministically inImage.__init__(on exception) andscan()(finally).(OSError, ValueError, pefile.PEFormatError)across both scanning and output writing viaparser.exit(2, f"{type(exc).__name__}: {args.image}: {exc}\n"), keeping exit code1strictly reserved for--fail-on-null/--fail-on-unresolved, exit0for clean scans, and exit2for I/O/PE/output errors with zero raw tracebacks.tests/xmem_attribute_scan_cli_check.py: standalone unittest suite using synthetic x64 PE32+ fixtures covering stdout and nested atomic--outputJSON schema, exit 0/1/2 separation,main(None)/__main__forwarding, and negative/mutation controls (missing/directory/corrupt/x86 PE,--outputdirectory,resolve()& hardlinksamefile()collisions, andos.replaceOSErrorleaving zero temp dirs and preserving prior output).CMakeLists.txt: registerxmem_attribute_scan_cli_checkimmediately afterxmem_attribute_scan(tests/xmem_attribute_scan_check.pyleft untouched).Verification
Verified on rebased commit
9cc402ee(based onupstream/mainb442b57f) usingout/pe-env/Scripts/python.exe -B:out/pe-env/Scripts/python.exe -B tests/xmem_attribute_scan_cli_check.py -v->Ran 6 tests,OK(exit 0).out/pe-env/Scripts/python.exe -B tests/xmem_attribute_scan_check.py->decoding: 16 cases passed+ unreadable image exit-2 checks (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.(exit 0).out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py->Self-test passed.(exit 0).tools/xmem_attribute_scan.py: 8/8 mutations killed (argvforwarding,--outputis_dir(),_same_pathresolve(),_same_pathsamefile(),image.is_dir(), atomic write bypass,PEFormatErrorguard, exit code2vs1).Evidence and follow-up
All PE32+ test fixtures in
tests/xmem_attribute_scan_cli_check.pyare synthetic minimal byte arrays generated in-memory under the repository MIT license; no external binaries or game files are used. Contributors can run the suite directly viaout/pe-env/Scripts/python.exe -B tests/xmem_attribute_scan_cli_check.pyor through CTest (xmem_attribute_scan_cli_check).Checklist
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T18:22:28Z.
Review: COMMENTED
Review of head
9cc402ee(tooling only, 3 files:tools/xmem_attribute_scan.py, newtests/xmem_attribute_scan_cli_check.py,CMakeLists.txt).No author Windows results are posted on this PR, so none were checked. I ran no Windows tests.
What I checked:
main(argv), the directory/collision guards and the atomic--outputwrite read correctly. Exit code 2 for errors and 1 for the--fail-on-*gates is kept.try/finallyaroundscan()and thetry/exceptinImage.__init__close the PE handle on every path. Most of the 275-line diff is re-indentation.git diff -wis the easier way to read it.python3 -B tests/xmem_attribute_scan_cli_check.pywith the pefile venv: 6 tests, OK.tests/xmem_attribute_scan_check.py: decoding cases pass.python3 tests/docs_links_check.py,python3 tests/bidi_controls_check.pyandgit diff --check origin/main...HEADpass.Mutations I tried (each reverted with
git checkout -- .):os.replacewith a directwrite_text: the suite fails (2 failures).self.pe.close()inImage.__init__'s error path: the suite still passes (see P2).Findings:
Image.__init__andscan()has no test. Add a test that mockspefile.PE, forces a failure after construction (for example a wrong machine type) and assertsclose()was called. Without it the fix can be reverted silently.docs/experiments.md(around line 9384, thexmem_attribute_scantable row) lists the existing suite only. Mention the newxmem_attribute_scan_cli_checktest and the new stderr formatValueError: <image>: <message>.No blocking bug found.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T19:54:47Z.
Review: COMMENTED
Review of head
4791df9e(the merge of main into the PR branch; the PR's own change is the one @tedin7 reviewed at9cc402ee). Comment only.Result: sound. One non-blocking note.
What I checked
tools/xmem_attribute_scan.py:main(argv)forwards toparse_args. The directory and collision checks (resolve()andsamefile()) run before scanning.--outputis written through a sibling temp dir plusos.replace. Exit 2 coversOSError,ValueErrorandPEFormatError. Exit 1 stays only for the--fail-on-*gates.scanandImage.__init__is re-indented intotry/finallyblocks that close the PE handle.git diff -wshows no other logic change there.xmem_attribute_scan_check.pydoes not depend on the old wording.xmem_attribute_scan_cli_checkis registered next toxmem_attribute_scan, and the new test is pure Python with synthetic PE files.Commands run locally (Linux, venv with pefile 2024.8.26 and iced-x86 1.21.0, as in requirements-dev.txt)
python -B tests/xmem_attribute_scan_cli_check.py: 6 tests, OKpython3 tests/docs_links_check.py: okpython3 tests/bidi_controls_check.py: okgit diff --check origin/main...HEAD: cleanos.replaceswapped for a direct write; exit 2 changed to 1; the--outputvs image collision check disabled.Non-blocking note
pe.close()(inImage.__init__on error, or in thescanfinally) leaves the suite green. The PE handle cleanup in the description has no test that guards it. Not a blocker for this tool; a small test that checks the handle is closed would close the gap.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T19:57:59Z.
Win2025 WARP receipt for head
4791df9eafter updating with main.Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit
4791df9edb82fb5885b7b8eeed44b9fe7ceee317:--preset dev):cmake --build --preset dev --target cpu_xmem_attributes cpu_xmem_delay -- -j 2):0.15s(configure3.59s)ctest --preset dev -R 'xmem_attribute_scan' --parallel 2 --no-tests=error --output-on-failure): 2/2 passed (10.13s:xmem_attribute_scan8.47s,xmem_attribute_scan_cli_check9.45s)/O2(--preset release):cmake --build --preset release --target cpu_xmem_attributes cpu_xmem_delay -- -j 2):10.14s(configure3.47s)ctest --preset release -R 'xmem_attribute_scan' --parallel 2 --no-tests=error --output-on-failure): 2/2 passed (15.64s:xmem_attribute_scan_cli_check13.25s,xmem_attribute_scan14.81s)tests/xmem_attribute_scan_check.pyok (3.0s)sccache: 264/279 hits; wall57.1sAI-assisted; commands listed were executed locally.