Raise and catch SEH exceptions inside a mapped image #65

Merged
Leyukaka merged 2 commits from upstream-pr/65 into main 2026-11-21 15:21:08 +00:00
Leyukaka commented 2026-11-21 14:54:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T14:54:16Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6530372749 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/65). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T14:54:16Z. --- ## 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._
upstream-sync closed this pull request 2026-11-24 06:41:11 +00:00
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!65
No description provided.