pe: validate package plan edges and add pe_run_package unit checks #1886

Merged
Ammmae0x merged 2 commits from upstream-pr/1893 into main 2026-11-24 20:57:10 +00:00
Ammmae0x commented 2026-11-24 20:26:14 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T20:26:14Z.


Change

Harden input and graph-edge validation in tools/pe_run_package.py and add pure-Python unit and CLI refusal coverage in tests/pe_run_package_plan_check.py, registered in CMakeLists.txt as pe_run_package_plan_check:

  • Reject empty import resolution chains in plan() with ValueError instead of raising IndexError on chain[-1].
  • Reject an empty reachable package graph in plan() with ValueError instead of raising IndexError on order[-1].
  • Reject ., .., and path separators in trap_key() DLL names and validate #ORDINAL suffixes (1..65535 ASCII digits) so malformed ordinals fail deterministically before run().
  • Reject empty strings, ., .., and path separators in --system provider names in main().
  • Add 11 unittest cases covering multi-DLL topological planning, is_host_create_thread() contracts, trap_key() positive/negative cases, plan() edge rejections (empty graph, empty chain, cycle, self-import, entry-as-dependency, conflicting bindings, strict forwarded_external_unverified), and CLI refusal paths without tracebacks.

Verification

Verified on commit 9a06df39:

  • ctest --preset dev -E "cpu_native_archive_" -R "^(pe_package_ordinal_plan|pe_package_import_closure|pe_run_package_invalid_pe|pe_run_package_plan_check|pe_host_commit_preflight|xgameruntime_preflight_stub_refused|pe_run_package_trap_missing)$" --output-on-failure: 7/7 passed (Total Tests: 1154).
  • ctest --preset release -E "cpu_native_archive_" -R "^(pe_package_ordinal_plan|pe_package_import_closure|pe_run_package_invalid_pe|pe_run_package_plan_check|pe_host_commit_preflight|xgameruntime_preflight_stub_refused|pe_run_package_trap_missing)$" --output-on-failure: 7/7 passed (Total Tests: 1154).
  • python -B tests/pe_run_package_plan_check.py -v: 11/11 passed.
  • python -B tests/form_coverage_check.py: passed.
  • python -B tests/docs_links_check.py: passed (18 Markdown files, 376 links).
  • python -B tests/bidi_controls_check.py: passed (1330 tracked files).

Evidence and follow-up

All new test fixtures in tests/pe_run_package_plan_check.py are synthetic in-memory reports and minimal synthetic PEs generated via compat_report_check.build_image. Running the new test suite against unpatched upstream/main:tools/pe_run_package.py failed with 6 failures and 2 errors on the newly added edge-validation cases.

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:6590737298 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1893). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T20:26:14Z. --- ## Change Harden input and graph-edge validation in `tools/pe_run_package.py` and add pure-Python unit and CLI refusal coverage in `tests/pe_run_package_plan_check.py`, registered in `CMakeLists.txt` as `pe_run_package_plan_check`: - Reject empty import resolution chains in `plan()` with `ValueError` instead of raising `IndexError` on `chain[-1]`. - Reject an empty reachable package graph in `plan()` with `ValueError` instead of raising `IndexError` on `order[-1]`. - Reject `.`, `..`, and path separators in `trap_key()` DLL names and validate `#ORDINAL` suffixes (`1..65535` ASCII digits) so malformed ordinals fail deterministically before `run()`. - Reject empty strings, `.`, `..`, and path separators in `--system` provider names in `main()`. - Add 11 unittest cases covering multi-DLL topological planning, `is_host_create_thread()` contracts, `trap_key()` positive/negative cases, `plan()` edge rejections (empty graph, empty chain, cycle, self-import, entry-as-dependency, conflicting bindings, strict `forwarded_external_unverified`), and CLI refusal paths without tracebacks. ## Verification Verified on commit `9a06df39`: - `ctest --preset dev -E "cpu_native_archive_" -R "^(pe_package_ordinal_plan|pe_package_import_closure|pe_run_package_invalid_pe|pe_run_package_plan_check|pe_host_commit_preflight|xgameruntime_preflight_stub_refused|pe_run_package_trap_missing)$" --output-on-failure`: 7/7 passed (Total Tests: 1154). - `ctest --preset release -E "cpu_native_archive_" -R "^(pe_package_ordinal_plan|pe_package_import_closure|pe_run_package_invalid_pe|pe_run_package_plan_check|pe_host_commit_preflight|xgameruntime_preflight_stub_refused|pe_run_package_trap_missing)$" --output-on-failure`: 7/7 passed (Total Tests: 1154). - `python -B tests/pe_run_package_plan_check.py -v`: 11/11 passed. - `python -B tests/form_coverage_check.py`: passed. - `python -B tests/docs_links_check.py`: passed (18 Markdown files, 376 links). - `python -B tests/bidi_controls_check.py`: passed (1330 tracked files). ## Evidence and follow-up All new test fixtures in `tests/pe_run_package_plan_check.py` are synthetic in-memory reports and minimal synthetic PEs generated via `compat_report_check.build_image`. Running the new test suite against unpatched `upstream/main:tools/pe_run_package.py` failed with 6 failures and 2 errors on the newly added edge-validation cases. ## 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. - [ ] 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.
upstream-sync closed this pull request 2026-11-24 21:01:10 +00:00
R0Y00 commented 2026-11-24 21:01:24 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T20:53:18Z.


I fixed one parser edge on this branch: a zero-padded ordinal was accepted, but its spelling did not match the canonical import-route key. The parser now normalizes it and rejects an oversized decimal before converting it to int. The eleven source-built plan/CLI tests passed with the new padded, zero and long-ordinal cases.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7144190246 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1893#issuecomment-7144190246). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T20:53:18Z. --- I fixed one parser edge on this branch: a zero-padded ordinal was accepted, but its spelling did not match the canonical import-route key. The parser now normalizes it and rejects an oversized decimal before converting it to int. The eleven source-built plan/CLI tests passed with the new padded, zero and long-ordinal cases.
tedin7 commented 2026-11-24 21:27:27 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:06:40Z.

Review: COMMENTED


Review of #1893, exact SHAs: first commit 9a06df3916f2e5c81ebc23917c76f71764fae845 and final head c3b05e453e047c555845fc395e91c6a708183dd2 (the PR was already MERGED when I finished, so this is a post-merge comment).

Verdict: no P1. One P2 (test is not runnable on Linux), merge was reasonable.

Diff read

  • tools/pe_run_package.py: empty-chain and empty-graph refusals in plan(), stricter trap_key() (dot names, slashes, ordinal range 1..0xFFFF, leading zeros normalised to #42 in the last commit), stricter --system basenames. Each refusal replaces an IndexError / silent accept; semantics look right, no off-by-one in the 0xFFFF bound (#65535 accepted, #65536 and #0 refused).
  • CMakeLists.txt: one add_test inside the INTAKE_PYTHON (Windows pe-env) block.
  • Clean-room and title: neutral, no external-source material, only behaviour of the repo's own tool.

P2: test_cli_refusal_paths_without_tracebacks fails on Linux, case pin-shim without shim

tests/pe_run_package_plan_check.py (the cases list, the --pin-shim kernel32.dll --preflight-only row). The check set(pin_shims) - shims.keys() lives in run() at tools/pe_run_package.py:144, after C.WINFUNCTYPE (line 137) and C.WinDLL. On a non-Windows host the tool dies with AttributeError: module 'ctypes' has no attribute 'WINFUNCTYPE' and a Traceback, so the test asserts "no Traceback" and fails. The test header says "pure-Python controls", but this one case is not. It passes on Windows (see below) and CMake only registers it under the Windows pe-env, so CI is not hurt. Fix options: move the --pin-shim/--shim validation into main() before run(), or skip that row when sys.platform != "win32".

P3 (nit)

The 1 <= len(ordinal) <= 5 guard added in the last commit is not independently pinned by a test: Python's int-digit limit already raises ValueError for the 5000-digit case, so removing the guard keeps the suite as it is (it only affects the message). Harmless.

Commands run locally (Linux, venv python /root/.local/share/x2w-rev-venv/bin/python)

  • git diff --check origin/main...HEAD: clean at both SHAs.
  • tests/pe_run_package_plan_check.py: 11 tests, 10 pass, 1 FAIL (the P2 above), same at 9a06df39 and c3b05e45.
  • pe_run_package_invalid_check.py, pe_package_ordinal_plan_check.py, pe_package_preflight_check.py, host_commit_check.py, intake/import_forms_check.py, bidi_controls_check.py: rc=0. docs_links_check.py: "Checked 18 Markdown files and 376 links". trap_missing_check.py and xgameruntime_preflight_check.py need built exe arguments, not run on Linux.
  • Windows Server 2025 via the pipeline at c3b05e45: ctest on ^(pe_run_package_plan_check|pe_run_package_invalid_pe|pe_package_ordinal_plan|pe_host_commit_preflight)$ passed 4/4 in Debug and Release, import_forms_check.py ok.

Mutations (all reverted with git checkout -- .)

Mutation Result
drop the empty-chain check (if not chain -> if False) caught: test_plan_refuses_empty_resolution_chain errors
drop the empty-graph check (if not order -> if False) caught: test_plan_refuses_empty_reachable_graph errors
ordinal max 0xFFFF -> 0xFFFFF (at 9a06df39) caught: #65536 case
drop the "/"/"\\" checks in trap_key caught: dir\\user32.dll!MessageBoxW case
drop .lstrip("0") (at c3b05e45) caught
drop symbol = "#" + ordinal normalisation caught
drop 1 <= len(ordinal) <= 5 NOT caught (see P3)

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6047412989 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1893#pullrequestreview-6047412989). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T21:06:40Z. **Review:** COMMENTED --- Review of #1893, exact SHAs: first commit `9a06df3916f2e5c81ebc23917c76f71764fae845` and final head `c3b05e453e047c555845fc395e91c6a708183dd2` (the PR was already MERGED when I finished, so this is a post-merge comment). **Verdict: no P1. One P2 (test is not runnable on Linux), merge was reasonable.** ### Diff read - `tools/pe_run_package.py`: empty-chain and empty-graph refusals in `plan()`, stricter `trap_key()` (dot names, slashes, ordinal range 1..0xFFFF, leading zeros normalised to `#42` in the last commit), stricter `--system` basenames. Each refusal replaces an `IndexError` / silent accept; semantics look right, no off-by-one in the 0xFFFF bound (`#65535` accepted, `#65536` and `#0` refused). - `CMakeLists.txt`: one `add_test` inside the `INTAKE_PYTHON` (Windows pe-env) block. - Clean-room and title: neutral, no external-source material, only behaviour of the repo's own tool. ### P2: `test_cli_refusal_paths_without_tracebacks` fails on Linux, case `pin-shim without shim` `tests/pe_run_package_plan_check.py` (the `cases` list, the `--pin-shim kernel32.dll --preflight-only` row). The check `set(pin_shims) - shims.keys()` lives in `run()` at `tools/pe_run_package.py:144`, after `C.WINFUNCTYPE` (line 137) and `C.WinDLL`. On a non-Windows host the tool dies with `AttributeError: module 'ctypes' has no attribute 'WINFUNCTYPE'` and a Traceback, so the test asserts "no Traceback" and fails. The test header says "pure-Python controls", but this one case is not. It passes on Windows (see below) and CMake only registers it under the Windows pe-env, so CI is not hurt. Fix options: move the `--pin-shim`/`--shim` validation into `main()` before `run()`, or skip that row when `sys.platform != "win32"`. ### P3 (nit) The `1 <= len(ordinal) <= 5` guard added in the last commit is not independently pinned by a test: Python's int-digit limit already raises `ValueError` for the 5000-digit case, so removing the guard keeps the suite as it is (it only affects the message). Harmless. ### Commands run locally (Linux, venv python `/root/.local/share/x2w-rev-venv/bin/python`) - `git diff --check origin/main...HEAD`: clean at both SHAs. - `tests/pe_run_package_plan_check.py`: 11 tests, 10 pass, 1 FAIL (the P2 above), same at `9a06df39` and `c3b05e45`. - `pe_run_package_invalid_check.py`, `pe_package_ordinal_plan_check.py`, `pe_package_preflight_check.py`, `host_commit_check.py`, `intake/import_forms_check.py`, `bidi_controls_check.py`: rc=0. `docs_links_check.py`: "Checked 18 Markdown files and 376 links". `trap_missing_check.py` and `xgameruntime_preflight_check.py` need built exe arguments, not run on Linux. - Windows Server 2025 via the pipeline at `c3b05e45`: `ctest` on `^(pe_run_package_plan_check|pe_run_package_invalid_pe|pe_package_ordinal_plan|pe_host_commit_preflight)$` passed 4/4 in Debug and Release, `import_forms_check.py` ok. ### Mutations (all reverted with `git checkout -- .`) | Mutation | Result | |---|---| | drop the empty-chain check (`if not chain` -> `if False`) | caught: `test_plan_refuses_empty_resolution_chain` errors | | drop the empty-graph check (`if not order` -> `if False`) | caught: `test_plan_refuses_empty_reachable_graph` errors | | ordinal max `0xFFFF` -> `0xFFFFF` (at `9a06df39`) | caught: `#65536` case | | drop the `"/"`/`"\\"` checks in `trap_key` | caught: `dir\\user32.dll!MessageBoxW` case | | drop `.lstrip("0")` (at `c3b05e45`) | caught | | drop `symbol = "#" + ordinal` normalisation | caught | | drop `1 <= len(ordinal) <= 5` | NOT caught (see P3) | 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!1886
No description provided.