Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Technique:Recognizing compiler-generated code/x86 SEH frames

From ReverseEngineering.wiki

The 32-bit MSVC structured exception handling frame: the push/call prolog with __SEH_prolog4, the SCOPETABLE of (previous try level, filter, handler) entries, the trylevel variable and _except_handler4_common, with the layouts as Wine implements them.

32-bit Windows programs keep exception handlers on a linked list rooted at fs:[0]. MSVC functions with __try/__except/__finally (and, on x86, C++ functions with unwinding) build a frame on the stack and register it. x64 does not: it uses tables (Format:PE (Portable Executable)/Exception directory).

Recognizing the frame

  • Prologue: push <N> (frame size or 0xC), push offset __sehtable$func (or stru_...), call __SEH_prolog4 (older CRTs: __SEH_prolog, _SEH_prolog, SEH3 uses __except_handler3). The epilogue is call __SEH_epilog4. Unverified: the helper bodies were not read; names are from the MSVC CRT.
  • The helper builds, below the saved EBP: scope-table pointer (xored with __security_cookie in SEH4), the trylevel, the handler _except_handler4, the previous list entry from fs:[0]; then stores the frame as new fs:[0]. In the function body the current try level is stored at [ebp-4] (mov [ebp+ms_exc.registration.TryLevel], N); the first try block is level 0, and the initial value is -2 (SEH4; -1 in SEH3, called TRYLEVEL_END in Wine).
  • Frame layout in Wine's reimplementation (MSVCRT_EXCEPTION_FRAME): prev, handler, scopetable, trylevel, _ebp, xpointers (C GetExceptionInformation).

SCOPETABLE

SEH3 table entries (12 bytes): int previousTryLevel; int (*lpfnFilter)(PEXCEPTION_POINTERS); void *(*lpfnHandler)(void). A __finally block has a null filter; a nonnull filter returning 1 runs the handler, -1 continues execution, 0 continues searching. The try level indexes the entries and previousTryLevel chains out to the enclosing block.

SEH4 puts a 16-byte header before the entries: int gs_cookie_offset; ULONG gs_cookie_xor; int eh_cookie_offset; ULONG eh_cookie_xor; (offsets relative to the frame, values xored with the cookie before comparing), then the same three-field entries. The frame stores scopetable ^ __security_cookie, so a decompiler shows an xor with the global; the real table address is stored ^ cookie. A GS cookie offset of -2 means no GS cookie in that frame. Unverified: the -2 meaning is from memory of the CRT, not from Wine.

Finding handlers

To list a function's __except and __finally bodies, find the push offset operand before __SEH_prolog4, read the header (SEH4) then entries until the chain ends; each filter/handler is a code address. Wine's _except_handler4_common and msvcrt_local_unwind4 walk exactly this structure (loop while trylevel != -2).

C++ functions with try/catch or destructors use a different handler, __CxxFrameHandler3, reached through an __ehhandler$ thunk: see Platform:Microsoft Visual C++ ABI/C++ exception handling.

Evidence & references

  • wine dlls/msvcrt/except_i386.c (SCOPETABLE, SCOPETABLE_V4, MSVCRT_EXCEPTION_FRAME, get_scopetable_v4, _except_handler3, _except_handler4_common, _local_unwind4), commit 7298a818b79446bae153b8d5aa27235ab3703bf9.