Microsoft-specific exception handling mechanisms
From Wikipedia, the free encyclopedia
Microsoft Windows OS family employs some exception handling mechanisms that are based on the operation system specifics.
Contents |
[edit] Structured Exception Handling
Microsoft Structured Exception Handling is the native exception handling mechanism for Windows and a forerunner technology to VEH.[1] It features the finally mechanism not present is standard С++ exceptions (but present in most imperative languages introduced later).
SEH mechanism is per-thread.
[edit] Usage
Microsot supports SEH as a programming technique at compiler level only. MS Visual C++ compiler features 3 non-standard keywords: __try, __except and __finally — for this purpose. Some other exception handling aspects are backed up by a number of Win32 API functions[2] including RaiseException to raise SEH exceptions manually.
[edit] Implementation
Each thread of execution in Windows 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 __except_handler3 function in msvcrt.dll[3], 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 __except and __finally blocks are called from within __except_handler3. If such blocks are present, _EXCEPTION_REGISTRATION_RECORD being created is extended by a few msvcrt-specific fields to be used by __except_handler3[4].
In a case of an exception in a user mode code, the operating system[5] 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 displayes the GPF error message[6]. Then the list is traversed once more giving handlers a chance to clean up any resources used. Finally, the execution returns to kernel mode[7] where the process is either resumed or terminated.
[edit] Links
- Microsoft Corp. (11/12/2009). "Structured Exception Handling". MSDN Library. http://msdn.microsoft.com/en-us/library/ms680657%28VS.85%29.aspx. Retrieved 2009-11-17.
- Matt Pietrek (Jan 1997). "A Crash Course on the Depths of Win32™ Structured Exception Handling". MSJ 12 (1). http://www.microsoft.com/msj/0197/Exception/Exception.aspx.
- Igor Skochinsky (Monday, March 6 2006 12:02.38 CST). "Reversing Microsoft Visual C++ Part I: Exception Handling". OpenRCE. http://www.openrce.org/articles/full_view/21. Retrieved 2009-11-17.
[edit] Vectored Exception Handling
Vectored Exception Handling was introduced with Windows XP. [8] 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.[8][1] Compared with SEH, VEH works more like a traditional notification callback scheme.[9]
[edit] Use of VEH
Use AddVectoredExceptionHandler API
[edit] References
- ^ a b "Vectored Exception Handling in Windows Server 2003 (Through Internet Archive)". http://web.archive.org/web/20080118162324/http://www.devx.com/microsoftISV/Article/15992.
- ^ Microsoft Corp. (11/12/2009). "Structured Exception Handling Functions". MSDN Library. http://msdn.microsoft.com/en-us/library/ms680659%28VS.85%29.aspx. Retrieved 2009-11-17.
- ^
ntdll.dllandkernel32.dllhave this function compiled-in instead - ^ Peter Kleissner (February 2009). "Windows Exception Handling". http://web17.webbpro.de/index.php?page=windows-exception-handling. Retrieved 2009-11-21., Compiler based Structured Exception Handling section
- ^ More specifically, the
ntdll!RtlDispatchExceptionroutine called fromntdll!KiUserExceptionDispatcherwhich is in turn called from thent!KiDispatchExceptionkernel routine. See Ken Johnson (November 16th, 2007, 7:00 am). "A catalog of NTDLL kernel mode to user mode callbacks, part 2: KiUserExceptionDispatcher". http://www.nynaeve.net/?p=201. - ^ Depending on the process's error mode; the default unhandled exception filter can be overwritten
- ^
ntdll!KiUserExceptionDispatchercalls eithernt!ZwContinueornt!ZwRaiseException - ^ a b "New Vectored Exception Handling in Windows XP". http://msdn.microsoft.com/en-us/magazine/cc301714.aspx.
- ^ "Windows Server 2003 Discover Improved System Info, New Kernel, Debugging, Security, and UI APIs". http://msdn.microsoft.com/en-us/magazine/cc300448.aspx.
| This Microsoft Windows article is a stub. You can help Wikipedia by expanding it. |