Microsoft-specific exception handling mechanisms
Structured Exception Handling
Microsoft Structured Exception Handling is the native exception handling mechanism for Windows and a forerunner technology to Vectored Exception Handling (VEH). It features the
finally mechanism not present in standard C++ exceptions (but present in most imperative languages introduced later). SEH is set up and handled separately for each thread of execution.
Microsoft supports SEH as a programming technique at the compiler level only. MS Visual C++ compiler features three non-standard keywords:
__finally — for this purpose. Other exception handling aspects are backed by a number of Win32 API functions, for example,
RaiseException to raise SEH exceptions manually.
Each thread of execution in Windows IA-32 edition or the WoW64 emulation layer for the x86-64 version has a link to an undocumented _EXCEPTION_REGISTRATION_RECORD list at the start of its Thread Information Block. The
__try statement essentially calls a compiler-defined
EH_prolog function. That function allocates an _EXCEPTION_REGISTRATION_RECORD on the stack pointing to the
__except_handler3[lower-alpha 1] function in
msvcrt.dll,[lower-alpha 2] then adds the record to the list's head. At the end of the
__try block a compiler-defined
EH_epilog function is called that does the reverse operation. Either of these compiler-defined routines can be inline. All the programmer-defined
__finally blocks are called from within
__except_handler3. If the programmer-defined blocks are present, the _EXCEPTION_REGISTRATION_RECORD created by
EH_prolog is extended with a few additional fields used by
In the case of an exception in user mode code, the operating system[lower-alpha 3] parses the thread's _EXCEPTION_REGISTRATION_RECORD list and calls each exception handler in sequence until a handler signals it has handled the exception (by return value) or the list is exhausted. The last one in the list is always the
kernel32!UnhandledExceptionFilter which displays the General protection fault error message.[lower-alpha 4] Then the list is traversed once more giving handlers a chance to clean up any resources used. Finally, the execution returns to kernel mode[lower-alpha 5] where the process is either resumed or terminated.
The patent on this mode of SEH, US5628016, expired in 2014.
SEH on 64-bit Windows does not involve a runtime exception handler list; instead, it uses a stack unwinding table (
UNWIND_INFO) interpreted by the system when an exception occurs.
This means that the compiler does not have to generate extra code to manually perform stack unwinding and to call exception handlers appropriately. It merely has to emit information in the form of unwinding tables about the stack frame layout and specified exception handlers.
Vectored Exception Handling
Vectored Exception Handling was introduced in Windows XP. Vectored Exception Handling is made available to Windows programmers using languages such as C++ and Visual Basic. VEH does not replace Structured Exception Handling (SEH); rather, VEH and SEH coexist, with VEH handlers having priority over SEH handlers. Compared with SEH, VEH works more like kernel-delivered Unix signals.
- The name varies in different versions of VC runtime
kernel32.dll, as well as other programs linked statically with VC runtime, have this function compiled-in instead
- More specifically,
ntdll!RtlDispatchExceptionsystem routine called from
ntdll!KiUserExceptionDispatcherwhich is in turn called from the
nt!KiDispatchExceptionkernel function. (See Ken Johnson (November 16, 2007). "A catalog of NTDLL kernel mode to user mode callbacks, part 2: KiUserExceptionDispatcher". http://www.nynaeve.net/?p=201. for details)
- The message can be silenced by altering the process's error mode; the default last handler can be replaced with SetUnhandledExceptionFilter API
- "Vectored Exception Handling in Windows Server 2003 (Through Internet Archive)". http://www.devx.com/microsoftISV/Article/15992.
- Microsoft Corp. (2009-11-12). "Structured Exception Handling Functions". MSDN Library. https://msdn.microsoft.com/en-us/library/ms680659.aspx.
- Peter Kleissner (February 14, 2009). "Windows Exception Handling - Peter Kleissner". http://stoned-vienna.com/html/index.php?page=windows-exception-handling. , Compiler based Structured Exception Handling section
- "Exceptional Behavior - x64 Structured Exception Handling". The NT Insider. https://www.osronline.com/article.cfm%5earticle=469.htm.
- "x64 exception handling" (in en-us). https://docs.microsoft.com/en-us/cpp/build/exception-handling-x64?view=vs-2019.
- "MSVC compatibility". http://clang.llvm.org/docs/MSVCCompatibility.html.
- "Under the Hood: New Vectored Exception Handling in Windows XP". https://msdn.microsoft.com/en-us/magazine/cc301714.aspx.
- "Windows Server 2003 Discover Improved System Info, New Kernel, Debugging, Security, and UI APIs". https://msdn.microsoft.com/en-us/magazine/cc300448.aspx.
- Microsoft Corp. (2009-11-12). "Structured Exception Handling". MSDN Library. https://msdn.microsoft.com/en-us/library/ms680657.aspx.
- Matt Pietrek (Jan 1997). "A Crash Course on the Depths of Win32 Structured Exception Handling". MSJ 12 (1). https://www.microsoft.com/msj/0197/Exception/Exception.aspx. Note that the examples given there do not work as-is on modern Windows systems (post XP SP2) due to the changes Microsoft made to address the security issues present in the early SEH design. The examples still work on later versions of Windows if compiled with
- "win32: Safe Structured Exception Handling". Yasm manual. https://www.tortall.net/projects/yasm/manual/html/objfmt-win32-safeseh.html.
- US patent 7,480,919 - Safe exceptions
- Johannes Passing (May 20, 2008). "Fun with low level SEH". https://jpassing.com/2008/05/20/fun-with-low-level-seh/. Covers the obscure details needed to get low-level SEH (and particularly SafeSEH) code to work on more modern Windows.
- Igor Skochinsky (March 6, 2006). "Reversing Microsoft Visual C++ Part I: Exception Handling". OpenRCE. https://www.openrce.org/articles/full_view/21.
- Matt Miller (2 Feb 2009). "Preventing the Exploitation of Structured Exception Handler (SEH) Overwrites with SEHOP". Technet. https://blogs.technet.com/b/srd/archive/2009/02/02/preventing-the-exploitation-of-seh-overwrites-with-sehop.aspx.
- Stéfan Le Berre, Damien Cauquil (22 Dec 2009). "Bypassing SEHOP". Sysdream. http://www.sysdream.com/sites/default/files/sehop_en.pdf.
- Joshua J. Drake (10 Jan 2012). "Old Meets New: Microsoft Windows SafeSEH Incompatibility". https://www.optiv.com/blog/old-meets-new-microsoft-windows-safeseh-incompatibility/. An article explaining why Windows 7 SP1 ignores SafeSEH for some older binaries, while Windows XP SP3 honors it.
Original source: https://en.wikipedia.org/wiki/Microsoft-specific exception handling mechanisms. Read more