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

Manually merged
github merged 2 commits from gh-pr/1075/head into gh-pr/1075/base 2026-11-26 22:39:41 +00:00
Owner

@⁠mumba17 on GitHub · 2026-11-24 00:23 UTC · original
mumba17:pe/trap-missing → main · +345 −6 · 6 files · 2 commits · merged 2026-11-24 01:12 UTC

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

**@⁠mumba17** on GitHub · 2026-11-24 00:23 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1075) `mumba17:pe/trap-missing` → `main` · +345 −6 · 6 files · 2 commits · merged 2026-11-24 01:12 UTC ## 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)
Startup checkpoints on #870 bind the missing externals of a package to
stubs that end the process when called, so the last reached stage can be
named. Those harnesses were private. Add the same diagnostic to the
repository runner.

With --trap-missing, every unresolved external is bound to a numbered
ctypes trap instead of refusing the run. The traps are listed on stderr in
sorted dll!symbol order. A trap never returns: it writes its number,
importers and name with an unbuffered write and terminates the process
with 0xE7A00000 | number. If the entry returns without reaching a trap,
the JSON result adds unhit_traps. No DLL is written or loaded under the
missing name. The default run and --preflight-only are unchanged, and the
two flags are mutually exclusive.

trap_missing_exe is a CRT-free source-built EXE that imports two
functions from trap_fixture.dll, which nothing builds, and picks a call
from TRAP_FIXTURE_MODE. pe_run_package_trap_missing checks the default
refusal, each trap's code and stderr line, the unhit report and the flag
conflict.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review of #1075 found two gaps.

A trap wrote its report with os.write before terminating. If that write
raised, ctypes swallowed the exception and returned to the guest, and
the run went on. The report is now best effort: termination runs in a
finally block, and os._exit backs up TerminateProcess.

An import table entry names a symbol but does not say whether the caller
calls it or reads it as data, so binding every unresolved name to a code
pointer could let a data read go on without a trap. Replace the
--trap-missing flag with a repeatable --trap-callable DLL!SYMBOL (or
DLL!#ORDINAL). Only listed names get a trap. Any unlisted unresolved
external is still refused before mapping, and so are a listed name that
is not an unresolved external and a malformed name.

trap_missing_data_exe builds the same fixture with a data import.
pe_run_package_trap_missing adds refusals for a partial list, a stale
name, a malformed name and the unlisted data import, and a child-process
regression that closes fd 2 and replaces sys.stderr before calling a
trap thunk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:39:40 +00:00
github manually merged commit 80d113005a into gh-pr/1075/base 2026-11-26 22:39:41 +00:00
Author
Owner

@⁠mumba17 on GitHub · 2026-11-24 00:38 UTC · original

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).

**@⁠mumba17** on GitHub · 2026-11-24 00:38 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1075#issuecomment-7118275140) 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).
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 00:28 UTC · original · requested changes

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** on GitHub · 2026-11-24 00:28 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1075#pullrequestreview-6031749245) · **requested changes** 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.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-24 00:50 UTC · original · approved these changes

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.

**@⁠R0Y00** on GitHub · 2026-11-24 00:50 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1075#pullrequestreview-6031934705) · **approved these changes** 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.