Raise and catch SEH exceptions inside a mapped image #65

Manually merged
github merged 2 commits from gh-pr/65/head into gh-pr/65/base 2026-11-26 21:25:03 +00:00
Owner

@⁠Leyukaka on GitHub · 2026-11-21 14:54 UTC · original
Leyukaka:mapped-seh → main · +155 −1 · 5 files · 2 commits · merged 2026-11-21 15:21 UTC

Change

Item 4 of #46: a separate fixture that raises and catches an exception inside an image mapped by tools/pe_map.cpp.

  • tests/cpu/seh_route.asm: guarded_load(address, mode, trace) names its own language-specific handler through PROC FRAME:on_fault. The built image has one RUNTIME_FUNCTION, with UNWIND_INFO flags EHANDLER|UHANDLER. The handler resumes only an access violation raised by the load instruction itself: it checks ExceptionAddress, then sets CONTEXT.Rip to a recovery label and CONTEXT.Rax to a fixed value. It declines everything else and every unwind call.
  • tests/cpu/native_seh_check.py (cpu_native_seh): runs the same four cases against the Windows-loaded original and the mapped copy, each target in its own child process. If an unwind is broken, only the child crashes, and the test reports it as a failed check.
    • A valid load returns the value.
    • A load from address zero is caught by the image's handler and returns the fixed value. RBX/RSI are checked after recovery through the register probe.
    • ud2 is declined and unwinds through the mapped frame to the ctypes caller, which reports 0xc000001d. The trace shows that the function ran up to ud2.
    • A later valid call still succeeds.
  • docs/experiments.md now describes this case, and SEH is removed from its list of open items.

Verification

At 5995954 on ec05dab (Windows 11 build 26300, VS Build Tools 17.14, Python 3.14.10):

ctest --preset dev --no-tests=error -R '^cpu_native_(map|seh)$'
100% tests passed, 0 tests failed out of 2

The full suite gives 73 tests and 0 failures; only bvh_wave64_hardware and bvh_wave64_warp are skipped.

As one-off manual checks (not in CI), I disabled .pdata registration in pe_map. The mapped child then crashes with 0xc0000005 and the test fails. I also made on_fault decline everything, which fails both the native and the mapped targets.

Evidence and follow-up

The fixture is original and built from this repository; there is no game data. CI runs Python 3.12, and I only ran 3.14 locally. ctypes reports an unhandled SEH code through OSError.winerror, and the test masks it to 32 bits. TLS callbacks and entry-point calls remain open in pe_map.

Refs #46

AI-assisted.

**@⁠Leyukaka** on GitHub · 2026-11-21 14:54 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/65) `Leyukaka:mapped-seh` → `main` · +155 −1 · 5 files · 2 commits · merged 2026-11-21 15:21 UTC ## Change Item 4 of #46: a separate fixture that raises and catches an exception inside an image mapped by `tools/pe_map.cpp`. - `tests/cpu/seh_route.asm`: `guarded_load(address, mode, trace)` names its own language-specific handler through `PROC FRAME:on_fault`. The built image has one `RUNTIME_FUNCTION`, with `UNWIND_INFO` flags EHANDLER|UHANDLER. The handler resumes only an access violation raised by the load instruction itself: it checks `ExceptionAddress`, then sets `CONTEXT.Rip` to a recovery label and `CONTEXT.Rax` to a fixed value. It declines everything else and every unwind call. - `tests/cpu/native_seh_check.py` (`cpu_native_seh`): runs the same four cases against the Windows-loaded original and the mapped copy, each target in its own child process. If an unwind is broken, only the child crashes, and the test reports it as a failed check. - A valid load returns the value. - A load from address zero is caught by the image's handler and returns the fixed value. RBX/RSI are checked after recovery through the register probe. - `ud2` is declined and unwinds through the mapped frame to the ctypes caller, which reports `0xc000001d`. The trace shows that the function ran up to `ud2`. - A later valid call still succeeds. - `docs/experiments.md` now describes this case, and SEH is removed from its list of open items. ## Verification At `5995954` on `ec05dab` (Windows 11 build 26300, VS Build Tools 17.14, Python 3.14.10): ``` ctest --preset dev --no-tests=error -R '^cpu_native_(map|seh)$' 100% tests passed, 0 tests failed out of 2 ``` The full suite gives 73 tests and 0 failures; only `bvh_wave64_hardware` and `bvh_wave64_warp` are skipped. As one-off manual checks (not in CI), I disabled `.pdata` registration in `pe_map`. The mapped child then crashes with `0xc0000005` and the test fails. I also made `on_fault` decline everything, which fails both the native and the mapped targets. ## Evidence and follow-up The fixture is original and built from this repository; there is no game data. CI runs Python 3.12, and I only ran 3.14 locally. ctypes reports an unhandled SEH code through `OSError.winerror`, and the test masks it to 32 bits. TLS callbacks and entry-point calls remain open in `pe_map`. Refs #46 _AI-assisted._
seh_route's guarded_load names its own handler in its unwind data. The
cpu_native_seh test runs a valid load, an access violation the image's
handler resumes, and an illegal instruction it declines, against both the
Windows-loaded original and the pe_map copy, each in its own child process.

Refs #46
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:25:01 +00:00
github manually merged commit c263f5fd64 into gh-pr/65/base 2026-11-26 21:25:03 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.