pe: harden pe_inventory changes and CLI validation with unit suite #1963
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1970"
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-24T22:19:49Z.
Change
Harden
changes()schema/structure validation and CLI error handling intools/pe_inventory.py, and add a synthetic AMD64 PE32+ unit and CLI validation suite (pe_inventory_cli_check) intests/pe_inventory_cli_check.py, registered inCMakeLists.txt.old/newdict types,"schema"presence/equality, and"images"(dict) /"imports"(list) structure inchanges()so malformed inventory payloads raiseValueErrorinstead ofKeyErrororTypeError.main(argv=None) -> intintools/pe_inventory.pyto accept an optionalargv, verifyroot.is_dir(), unlink temporary JSON files if atomic replacement fails, and exit cleanly with code1(no traceback) on(OSError, ValueError, json.JSONDecodeError, pefile.PEFormatError), while keeping exit code2viaparser.error(...)when--outputis inside the input tree or aliases--baseline.tests/pe_inventory_cli_check.pycoveringchanges()diff categories, atomic JSON output +--baselinediffing, and negative controls for schema mismatches, missing/empty/malformed input directories, missing/invalid/mismatched--baselinefiles, and invalid--outputpaths.Verification
.\out\pe-env\Scripts\python.exe -B tests/pe_inventory_cli_check.py -v->Ran 6 tests in 1.009s(OK);tests/pe_package_preflight_check.py(14/14 OK);tests/intake/import_forms_check.py,tests/intake/paths_check.py, andtests/intake/routing_check.pyall passed.tools/pe_inventory.pytoupstream/main):Ran 6 tests->FAILED (failures=1, errors=3)(KeyError: 'schema', missingargvparameter inmain, and traceback on missing input directory).devpreset (rebased ontoupstream/main3682b34f;Total Tests: 1254):ctest --preset dev --no-tests=error -R "^(pe_inventory_cli_check|pe_package_import_closure|pe_package_ordinal_plan|static_exe_intake|conditional_import_paths|bundled_export_forwarders)$" -V->6/6passed (pe_package_ordinal_plan3/3,conditional_import_paths,static_exe_intake,bundled_export_forwarders,pe_package_import_closure14/14,pe_inventory_cli_check6/6).releasepreset (rebased ontoupstream/main3682b34f;Total Tests: 1254):ctest --preset release --no-tests=error -R "^(pe_inventory_cli_check|pe_package_import_closure|pe_package_ordinal_plan|static_exe_intake|conditional_import_paths|bundled_export_forwarders)$" -V->6/6passed.tests/form_coverage_check.py,tests/docs_links_check.py, andtests/bidi_controls_check.pyall exited0.Evidence and follow-up
tests/pe_inventory_cli_check.pyconstructs minimal synthetic AMD64 PE32+ binaries in memory with normal and delay import tables using synthetic module/symbol names and zero external binaries or game files.Checklist
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T22:26:35Z.
Ran it, broke five things: two caught, and two of the three survivors are real
Windows 11 build 26300.9974:
tools/pe_inventory.pyexcept (OSError, ValueError, json.JSONDecodeError, pefile.PEFormatError)→except (KeyboardInterrupt,)parser.exit(1, …)→parser.exit(0, …)raise ValueError("Invalid inventory structure")→passif not root.is_dir():→if False:The structure check is unpinned, and its failure modes are the ones this PR exists to remove
I drove the CLI end-to-end with a real synthetic PE in the root and a baseline whose structure is wrong:
Two separate problems, both invisible to the suite:
AttributeErroris not in the caught tuple, so a baseline with"images": []prints a Python traceback and exits 1 — the exact class of output this PR was written to stop.changes()raisesValueErrorfor that shape only because of the guard being mutated here."imports": {}is silently accepted and the run exits 0, producing a diff as if the baseline had no imports. A wrong diff that looks like a clean run is worse than a refusal.The fixture asserts the CLI boundary and the diff categories, but nothing feeds
changes()a structurally wrong payload, so both survive.Suggested cases — the first is the one I would insist on:
The two survivors I checked and am not reporting
if not root.is_dir():removed: a file as--directorystill exits 1 in both runs, becauseresolve(strict=True)/scan()raise anOSErrorthat the handler catches. Different message, same outcome.isinstancehalf of the schema guard removed:"schema" not in oldalready refuses a list or a string, sochanges()raisesValueError: Inventory schema mismatcheither way. Redundant, not load-bearing.I am stating both because a reader of the mutation table would otherwise think three behaviours are untested when only one is.
What the rest does well
Exit code
2for--outputinside the input tree or aliasing the baseline (viaparser.error) versus1for runtime failures is the right split — a caller scripting this can tell "I passed a bad path" from "the run failed". Unlinking the temporary JSON when the atomic replace fails is the kind of cleanup that is usually missing.Verdict
Reviewed. The CLI boundary is pinned; the new structure check is not, and its two failure modes are a leaked traceback and a silently wrong diff.
Notes
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T22:32:44Z.
Updated PR #1970 (commit
cc171feb) to address the unpinned structure-validation finding:test_malformed_baseline_structure_is_refused_without_a_tracebackintests/pe_inventory_cli_check.pydriving the CLI end-to-end with synthetic PE baselines containing"images": []and"imports": {}(asserting exit code1, no traceback,Invalid inventory structurein stderr, and no output file created).test_changes_schema_and_structure_validation_raises_value_errorandtest_missing_empty_and_malformed_input_dirs_exit_1_without_tracebackto pin directchanges()structure checks (oldandnew) and file-as-directory refusal (Not a directory:).AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T22:40:24Z.
Three source-built CLI inputs still raised tracebacks on the preceding head: missing image digest, missing import fields and a list-valued symbol. Added nested validation and pinned all three refusals. The inventory CLI suite passed 7/7 and the adjacent PE preflight suite passed 14/14 on Windows.