pe: add a --trap-callable diagnostic mode to pe_run_package #1068

Merged
mumba17 merged 2 commits from upstream-pr/1075 into main 2026-11-24 01:12:42 +00:00
mumba17 commented 2026-11-24 00:23:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by mumba17 on 2026-11-24T00:23:05Z.


Change

Startup checkpoints on #870 need to name the last stage a package reaches while some externals are still missing. So far each checkpoint has bound the missing externals to process-ending stubs in a private harness. This PR adds that diagnostic to tools/pe_run_package.py as an opt-in --trap-callable DLL!SYMBOL (or DLL!#ORDINAL, repeatable).

  • Default and --preflight-only are unchanged. Unresolved externals are still refused before any image is mapped, and plan(report, allow_external=False) still refuses as before.
  • Only listed callable imports get a trap. An IAT entry names a symbol but does not say whether the caller calls it or reads it as data. Each name listed with --trap-callable is bound to a numbered ctypes trap. Any unlisted unresolved external is still refused before mapping:
    • 1 unresolved external symbol(s) not listed with --trap-callable, first <image>: <dll>!<symbol>
    • a listed name that is not an unresolved external: --trap-callable <dll>!<symbol> is not an unresolved external
    • a malformed name
  • Listing: before mapping, the runner lists the traps on stderr in sorted dll!symbol order, with their exit codes:
    trap-callable: 2 listed unresolved external(s) bound to terminating traps; diagnostic only, not providers
      trap 1 (exit 0xe7a00001): trap_fixture.dll!trap_fixture_alpha
      trap 2 (exit 0xe7a00002): trap_fixture.dll!trap_fixture_beta
    
  • A trap never returns to its caller. It tries to write trap N: <importers>: <dll>!<symbol> with an unbuffered os.write, but that report is best effort. Termination runs in a finally block (TerminateProcess with 0xE7A00000 | N, then os._exit as a backstop), so a failing diagnostic stream cannot return control to the guest. The trap thunks are kept for the life of the process.
  • If the entry returns without reaching a trap, the JSON result adds "unhit_traps".
  • No DLL is written or loaded under a missing name, so nothing can satisfy name resolution as a provider. This follows the #1051 review.
  • --trap-callable and --preflight-only are mutually exclusive.

Fixtures and test

  • tests/trap_missing_exe.cpp, CRT-free and built twice:
    • trap_missing_exe imports trap_fixture_alpha (six arguments) and trap_fixture_beta from trap_fixture.dll, plus kernel32!GetEnvironmentVariableA. TRAP_FIXTURE_MODE selects alpha (also the default), beta or none. A call that returns makes the EXE exit with 0xBAD.
    • trap_missing_data_exe (TRAP_DATA_IMPORT) also imports trap_fixture_data as data and reads it in mode data.
  • tests/trap_missing_import.def: an import-only library, with trap_fixture_data DATA. Nothing builds or copies trap_fixture.dll.
  • tests/trap_missing_check.py, registered as pe_run_package_trap_missing:
Case Observed
default run exit 1, 2 unresolved external symbol(s), first trap_missing_exe.exe: trap_fixture.dll!trap_fixture_alpha, no stdout
alpha and beta listed; mode unset or alpha exit 0xe7a00001, stderr exactly the listing plus trap 1: trap_missing_exe.exe: trap_fixture.dll!trap_fixture_alpha
mode beta exit 0xe7a00002, trap 2's line
mode none exit 0, JSON with exit_code 1952 (0x7a0) and unhit_traps [alpha, beta]
only alpha listed refused before mapping: beta not listed
a listed trap_fixture.dll!absent refused: not an unresolved external
--trap-callable trap_fixture_alpha refused: malformed
data EXE, default refused: 3 unresolved
data EXE with alpha and beta listed, mode unset or data refused before mapping: trap_fixture.dll!trap_fixture_data not listed
with --preflight-only exit 2 from the argument parser
child: bind_traps, then os.close(2), sys.stderr = io.StringIO(), call the thunk exit 0xe7a00001, nothing printed after the call

Mutation controls (local, not committed):

Mutation Result
a trap that returns instead of terminating the check fails
the previous unguarded body (os.write, then terminate) failed-report exit: expected 3886022657, got 0
trapping every unresolved name partial list exit: expected 1, got 3886022657

Verification

Environment: Windows 11 Pro build 26300, MSVC 19.44.35207.1 x64, Python 3.13.1 (out/pe-env), branch on d33845b.

cmake --build --preset dev --target pe_map trap_missing_exe trap_missing_data_exe
ctest --preset dev --no-tests=error -R '^pe_run_package_trap_missing$' -V
  • pe_run_package_trap_missing passes in dev and release.
  • At the first revision, the related set passed 8/8: cpu_native_startup, cpu_package_shim_lifetime, pe_package_ordinal_plan, cpu_native_acphal_hal, static_exe_intake, pe_package_import_closure, xgameruntime_preflight_stub_refused and this test. The review revision changes only the opt-in path.
  • pe_package_preflight_check.py, docs_links_check.py, bidi_controls_check.py and git diff --check are clean.
  • Not run: the full CTest suite and Windows CI for this branch.

Local use (no image or log attached): I ran the main executable from the #870 checkpoints with the two shims, its System32 providers and each of its 22 missing externals listed. No trap was reached, and the process ended with 0x80000003 at the refused reserve+commit XMemVirtualAlloc, the stage I reported under the Windows loader. That run used the first revision, which trapped every unresolved name; the same 22 names are now passed explicitly. The 22 are function imports by their call sites in the earlier checkpoints, which is the caller's responsibility under this option. Reaching the later DirectStorage stage under pe_map also needs #1085.

docs/experiments.md documents the option next to the other pe_run_package.py options.

Refs #870.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6571717557 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1075). Originally posted by [mumba17](https://github.com/mumba17) on 2026-11-24T00:23:05Z. --- ## Change Startup checkpoints on #870 need to name the last stage a package reaches while some externals are still missing. So far each checkpoint has bound the missing externals to process-ending stubs in a private harness. This PR adds that diagnostic to `tools/pe_run_package.py` as an opt-in `--trap-callable DLL!SYMBOL` (or `DLL!#ORDINAL`, repeatable). - **Default and `--preflight-only` are unchanged.** Unresolved externals are still refused before any image is mapped, and `plan(report, allow_external=False)` still refuses as before. - **Only listed callable imports get a trap.** An IAT entry names a symbol but does not say whether the caller calls it or reads it as data. Each name listed with `--trap-callable` is bound to a numbered ctypes trap. Any unlisted unresolved external is still refused before mapping: - `1 unresolved external symbol(s) not listed with --trap-callable, first <image>: <dll>!<symbol>` - a listed name that is not an unresolved external: `--trap-callable <dll>!<symbol> is not an unresolved external` - a malformed name - **Listing:** before mapping, the runner lists the traps on stderr in sorted `dll!symbol` order, with their exit codes: ```text trap-callable: 2 listed unresolved external(s) bound to terminating traps; diagnostic only, not providers trap 1 (exit 0xe7a00001): trap_fixture.dll!trap_fixture_alpha trap 2 (exit 0xe7a00002): trap_fixture.dll!trap_fixture_beta ``` - **A trap never returns to its caller.** It tries to write `trap N: <importers>: <dll>!<symbol>` with an unbuffered `os.write`, but that report is best effort. Termination runs in a `finally` block (`TerminateProcess` with `0xE7A00000 | N`, then `os._exit` as a backstop), so a failing diagnostic stream cannot return control to the guest. The trap thunks are kept for the life of the process. - **If the entry returns without reaching a trap**, the JSON result adds `"unhit_traps"`. - **No DLL is written or loaded under a missing name**, so nothing can satisfy name resolution as a provider. This follows the #1051 review. - `--trap-callable` and `--preflight-only` are mutually exclusive. ## Fixtures and test - `tests/trap_missing_exe.cpp`, CRT-free and built twice: - `trap_missing_exe` imports `trap_fixture_alpha` (six arguments) and `trap_fixture_beta` from `trap_fixture.dll`, plus `kernel32!GetEnvironmentVariableA`. `TRAP_FIXTURE_MODE` selects `alpha` (also the default), `beta` or `none`. A call that returns makes the EXE exit with `0xBAD`. - `trap_missing_data_exe` (`TRAP_DATA_IMPORT`) also imports `trap_fixture_data` as data and reads it in mode `data`. - `tests/trap_missing_import.def`: an import-only library, with `trap_fixture_data DATA`. Nothing builds or copies `trap_fixture.dll`. - `tests/trap_missing_check.py`, registered as `pe_run_package_trap_missing`: | Case | Observed | | --- | --- | | default run | exit 1, `2 unresolved external symbol(s), first trap_missing_exe.exe: trap_fixture.dll!trap_fixture_alpha`, no stdout | | alpha and beta listed; mode unset or `alpha` | exit `0xe7a00001`, stderr exactly the listing plus `trap 1: trap_missing_exe.exe: trap_fixture.dll!trap_fixture_alpha` | | mode `beta` | exit `0xe7a00002`, trap 2's line | | mode `none` | exit 0, JSON with `exit_code` 1952 (`0x7a0`) and `unhit_traps` [alpha, beta] | | only alpha listed | refused before mapping: beta not listed | | a listed `trap_fixture.dll!absent` | refused: not an unresolved external | | `--trap-callable trap_fixture_alpha` | refused: malformed | | data EXE, default | refused: 3 unresolved | | data EXE with alpha and beta listed, mode unset or `data` | refused before mapping: `trap_fixture.dll!trap_fixture_data` not listed | | with `--preflight-only` | exit 2 from the argument parser | | child: `bind_traps`, then `os.close(2)`, `sys.stderr = io.StringIO()`, call the thunk | exit `0xe7a00001`, nothing printed after the call | **Mutation controls (local, not committed):** | Mutation | Result | | --- | --- | | a trap that returns instead of terminating | the check fails | | the previous unguarded body (`os.write`, then terminate) | `failed-report exit: expected 3886022657, got 0` | | trapping every unresolved name | `partial list exit: expected 1, got 3886022657` | ## Verification Environment: Windows 11 Pro build 26300, MSVC 19.44.35207.1 x64, Python 3.13.1 (`out/pe-env`), branch on `d33845b`. ```powershell cmake --build --preset dev --target pe_map trap_missing_exe trap_missing_data_exe ctest --preset dev --no-tests=error -R '^pe_run_package_trap_missing$' -V ``` - `pe_run_package_trap_missing` passes in `dev` and `release`. - At the first revision, the related set passed 8/8: `cpu_native_startup`, `cpu_package_shim_lifetime`, `pe_package_ordinal_plan`, `cpu_native_acphal_hal`, `static_exe_intake`, `pe_package_import_closure`, `xgameruntime_preflight_stub_refused` and this test. The review revision changes only the opt-in path. - `pe_package_preflight_check.py`, `docs_links_check.py`, `bidi_controls_check.py` and `git diff --check` are clean. - Not run: the full CTest suite and Windows CI for this branch. **Local use (no image or log attached):** I ran the main executable from the #870 checkpoints with the two shims, its System32 providers and each of its 22 missing externals listed. No trap was reached, and the process ended with `0x80000003` at the refused reserve+commit `XMemVirtualAlloc`, the stage I reported under the Windows loader. That run used the first revision, which trapped every unresolved name; the same 22 names are now passed explicitly. The 22 are function imports by their call sites in the earlier checkpoints, which is the caller's responsibility under this option. Reaching the later DirectStorage stage under `pe_map` also needs #1085. `docs/experiments.md` documents the option next to the other `pe_run_package.py` options. Refs #870. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:46:10 +00:00
R0Y00 commented 2026-11-24 06:46:10 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


The source-built Windows trap test passes with normal stderr. I reproduced a missing failure-path control on this candidate. In an isolated Python child, bind a synthetic callable trap, close fd 2, direct Python's stderr to a StringIO, and call the thunk. os.write raises inside ctypes; ctypes returns to the caller, which then exits 0. Ensure the trap terminates even if reporting fails, and add a child-process regression with a failing diagnostic stream.

There is also a resolution boundary to make explicit. A missing PE IAT entry names a symbol or ordinal; it does not say whether the caller invokes it or reads it as data. Binding every unresolved name to a code pointer can let ordinary data reads proceed without taking a trap. Restrict diagnostic bindings to explicitly identified callable imports and keep unclassified imports as preflight failures. Please add a synthetic unresolved data import or an equivalent refusal control. Keep the existing default strict path and same-image fixture.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6031749245 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1075#pullrequestreview-6031749245). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T00:28:53Z. **Review:** CHANGES_REQUESTED --- The source-built Windows trap test passes with normal stderr. I reproduced a missing failure-path control on this candidate. In an isolated Python child, bind a synthetic callable trap, close fd 2, direct Python's stderr to a StringIO, and call the thunk. os.write raises inside ctypes; ctypes returns to the caller, which then exits 0. Ensure the trap terminates even if reporting fails, and add a child-process regression with a failing diagnostic stream. There is also a resolution boundary to make explicit. A missing PE IAT entry names a symbol or ordinal; it does not say whether the caller invokes it or reads it as data. Binding every unresolved name to a code pointer can let ordinary data reads proceed without taking a trap. Restrict diagnostic bindings to explicitly identified callable imports and keep unclassified imports as preflight failures. Please add a synthetic unresolved data import or an equivalent refusal control. Keep the existing default strict path and same-image fixture.
R0Y00 commented 2026-11-24 06:46:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T00:50:54Z.

Review: APPROVED


Windows Release passed on 80d11300. With fd 2 closed the child now exits through the trap. The unlisted DATA import is refused before mapping, and partial, stale and malformed callable lists also reject. Default and preflight remain strict. This clears my earlier findings. Recheck against #1085 if the two runner branches conflict.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6031934705 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1075#pullrequestreview-6031934705). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T00:50:54Z. **Review:** APPROVED --- Windows Release passed on 80d11300. With fd 2 closed the child now exits through the trap. The unlisted DATA import is refused before mapping, and partial, stale and malformed callable lists also reject. Default and preflight remain strict. This clears my earlier findings. Recheck against #1085 if the two runner branches conflict.
mumba17 commented 2026-11-24 06:49:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by mumba17 on 2026-11-24T00:38:12Z.


Thanks. Both points are addressed in 80d1130.

Trap termination when the report fails. I reproduced your case: the previous body (os.write, then TerminateProcess) let the child exit 0. The report is now best effort, and termination runs in a finally block: TerminateProcess(..., 0xE7A00000 | N), then os._exit as a backstop. The new child-process regression binds one trap with bind_traps, calls os.close(2), sets sys.stderr = io.StringIO() and calls the thunk. It exits 0xe7a00001 and prints nothing after the call. With the old body restored locally, the check fails with failed-report exit: expected 3886022657, got 0.

Resolution boundary. --trap-missing is replaced by a repeatable --trap-callable DLL!SYMBOL (or DLL!#ORDINAL). Only listed names get a trap. Any unlisted unresolved external stays a preflight refusal (N unresolved external symbol(s) not listed with --trap-callable, first ...). A listed name that is not an unresolved external and a malformed name are refused too. trap_missing_data_exe builds the same fixture with trap_fixture_data declared DATA and read in mode data. With the two callables listed, it is refused before mapping in both modes, naming trap_fixture.dll!trap_fixture_data. With "trap every unresolved name" restored locally, the check fails at the partial-list refusal.

The default strict path, the --preflight-only exclusion and the original same-image fixture cases are unchanged. pe_run_package_trap_missing passes in dev and release. The PR body is updated with the full case table.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7118275140 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1075#issuecomment-7118275140). Originally posted by [mumba17](https://github.com/mumba17) on 2026-11-24T00:38:12Z. --- Thanks. Both points are addressed in `80d1130`. **Trap termination when the report fails.** I reproduced your case: the previous body (`os.write`, then `TerminateProcess`) let the child exit 0. The report is now best effort, and termination runs in a `finally` block: `TerminateProcess(..., 0xE7A00000 | N)`, then `os._exit` as a backstop. The new child-process regression binds one trap with `bind_traps`, calls `os.close(2)`, sets `sys.stderr = io.StringIO()` and calls the thunk. It exits `0xe7a00001` and prints nothing after the call. With the old body restored locally, the check fails with `failed-report exit: expected 3886022657, got 0`. **Resolution boundary.** `--trap-missing` is replaced by a repeatable `--trap-callable DLL!SYMBOL` (or `DLL!#ORDINAL`). Only listed names get a trap. Any unlisted unresolved external stays a preflight refusal (`N unresolved external symbol(s) not listed with --trap-callable, first ...`). A listed name that is not an unresolved external and a malformed name are refused too. `trap_missing_data_exe` builds the same fixture with `trap_fixture_data` declared `DATA` and read in mode `data`. With the two callables listed, it is refused before mapping in both modes, naming `trap_fixture.dll!trap_fixture_data`. With "trap every unresolved name" restored locally, the check fails at the partial-list refusal. The default strict path, the `--preflight-only` exclusion and the original same-image fixture cases are unchanged. `pe_run_package_trap_missing` passes in `dev` and `release`. The PR body is updated with the full case table. AI-assisted (Claude Code).
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!1068
No description provided.