Skip to content

Implement x86 structured exception handling - #166

Open
malvarezcastillo wants to merge 1 commit into
decompals:mainfrom
malvarezcastillo:fix/commit-reserved-on-fault
Open

malvarezcastillo wants to merge 1 commit into
decompals:mainfrom
malvarezcastillo:fix/commit-reserved-on-fault

Conversation

@malvarezcastillo

@malvarezcastillo malvarezcastillo commented Nov 3, 2026 •

Copy link
Copy Markdown

Summary

Replaces the commit-on-fault hack with x86 structured exception handling, as suggested in review. Guest faults and RaiseException now reach the guest's fs:[0] handlers, so c1xx commits its reserved pages with its own filter. It works on i686 and x86_64 Linux and on macOS.

guest fault ─ handleSignal (sigaltstack)
                builds EXCEPTION_RECORD + CONTEXT below the faulting esp
                sigreturn into wibo_exception_dispatch
RaiseException ─ captures the caller's context ─┐
wibo_exception_dispatch (guest mode)  <─────────┘
  loop: entry::beginDispatch / continueDispatch pick the next step
    call the next fs:[0] handler, with a protector frame linked
    at the end of the chain: UnhandledExceptionFilter → top-level filter, or print and exit with the code
  wibo_restore_context resumes the (possibly edited) CONTEXT
RtlUnwind (guest mode)
  loop: entry::beginUnwind / continueUnwind call unwind handlers up to the target frame, then return to the caller

The loops run in guest mode and call handlers directly. A handler that never returns (__except, a C++ catch, longjmp) therefore leaves no host frames behind. Host→guest thunks now keep the outer TEB_SP, and on x86_64 r12–r15, so unwinding out of a guest callback leaves the host consistent.

Evidence

  • Before: a guest access violation kills wibo with SIGSEGV. cl.exe 14.00.50727.762 dies with exit 139 on large translation units. test_seh.exe fails at its first check.
  • After: test_seh.exe passes under Wine and under wibo on the debug and release presets for i686 and x86_64 Linux, and on macOS arm64 (Rosetta). It covers commit-on-write, register and FPU state across a fault, context edits, RaiseException, nested exceptions, unwinding, noncontinuable exceptions, stack overflow, unwinding out of a callback and the top-level filter. All 61 fixtures pass on those builds.
  • cl.exe: I applied this patch to a 1.2.0-based branch that adds the imports that toolchain needs, then compiled 58 translation units from a /GL project. Six of them hit the reserved-page fault. All 58 objects are byte-identical to the ones the commit-on-fault build produced, which means c1xx's own filter now does the commit. I can't share the sources.

Merge Danger

Door: two-way

Blast Radius: wide

Every guest fault now runs guest handlers before anything else happens. Every host→guest callback goes through the changed thunks, and guest stacks reserve one more guard page.

Known limits:

  • Segment registers edited in a CONTEXT are not restored.
  • RaiseException reports its caller's address rather than one inside it.
  • Unwinding out of a callback that a host API called abandons that API's host frames: their stack leaks until the outer host call returns, and a lock they hold stays held (for example the loader's, when DllMain raises).
  • General-protection faults, privileged instructions included, are reported as access violations.
  • A thread survives one stack overflow. _resetstkoflw can't re-arm the guard page, because VirtualProtect doesn't handle guest stacks or PAGE_GUARD.
  • Under Rosetta, unmasked x87 and SSE exceptions don't trap, and into reports an illegal instruction.
  • Vectored handlers are still a stub.

@simonlindholm

Copy link
Copy Markdown
Contributor

I don't think we should take this hack upstream. Surely it's not that hard to work out signal handlers and basic SEH. Worst case, I could see accepting it behind a flag, that prints an error on non-32-bit-Linux.

@malvarezcastillo

Copy link
Copy Markdown
Author

I don't think we should take this hack upstream. Surely it's not that hard to work out signal handlers and basic SEH. Worst case, I could see accepting it behind a flag, that prints an error on non-32-bit-Linux.

I'll look for a way to implement this (I need it for my environment) that isn't considered a hack then. Marking PR as draft in the meantiem

@malvarezcastillo
malvarezcastillo marked this pull request as draft November 5, 2026 07:14
Guest faults and RaiseException now go through the guest's fs:[0]
handlers instead of killing the process, on i686 and x86_64 Linux and
on macOS. The dispatch and unwind loops run in guest mode, so a handler
that never returns leaves no host frames behind.

This replaces the commit-on-fault hack: c1xx commits its reserved pages
from its own filter.
@malvarezcastillo
malvarezcastillo force-pushed the fix/commit-reserved-on-fault branch from 30f8414 to 5650084 Compare November 5, 2026 14:11
@malvarezcastillo
malvarezcastillo marked this pull request as ready for review November 5, 2026 14:11
@malvarezcastillo

Copy link
Copy Markdown
Author

Reworked this into actual SEH: guest faults and RaiseException now go through the guest's fs:[0] handlers on i686 and x86_64 Linux and on macOS, no flag needed, and c1xx commits its reserved pages from its own filter. Details and known limits are in the updated description.

@malvarezcastillo malvarezcastillo changed the title Commit reserved pages on access violations on i686 Linux Implement x86 structured exception handling Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants