2 * msvcrt.dll exception handling
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2005 Juan Lang
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * See http://www.microsoft.com/msj/0197/exception/exception.htm,
24 * but don't believe all of it.
26 * FIXME: Incomplete support for nested exceptions/try block cleanup.
30 #include "wine/port.h"
37 #include "wine/exception.h"
41 #include "msvcrt/float.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(seh);
46 /* VC++ extensions to Win32 SEH */
47 typedef struct _SCOPETABLE
50 int (*lpfnFilter)(PEXCEPTION_POINTERS);
51 int (*lpfnHandler)(void);
52 } SCOPETABLE, *PSCOPETABLE;
54 typedef struct _MSVCRT_EXCEPTION_FRAME
56 EXCEPTION_REGISTRATION_RECORD *prev;
57 void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
58 PCONTEXT, PEXCEPTION_RECORD);
59 PSCOPETABLE scopetable;
62 PEXCEPTION_POINTERS xpointers;
63 } MSVCRT_EXCEPTION_FRAME;
65 #define TRYLEVEL_END (-1) /* End of trylevel list */
67 #if defined(__GNUC__) && defined(__i386__)
68 static inline void call_finally_block( void *code_block, void *base_ptr )
70 __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax"
71 : : "a" (code_block), "g" (base_ptr));
74 static inline DWORD call_filter( void *func, void *arg, void *ebp )
77 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
79 : "0" (func), "r" (ebp), "r" (arg)
80 : "ecx", "edx", "memory" );
85 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
86 EXCEPTION_REGISTRATION_RECORD* frame,
88 EXCEPTION_REGISTRATION_RECORD** dispatch)
90 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
91 return ExceptionContinueSearch;
93 return ExceptionCollidedUnwind;
97 /*********************************************************************
98 * _EH_prolog (MSVCRT.@)
101 /* Provided for VC++ binary compatibility only */
102 __ASM_GLOBAL_FUNC(_EH_prolog,
106 "movl %esp, %fs:0\n\t"
107 "movl 12(%esp), %eax\n\t"
108 "movl %ebp, 12(%esp)\n\t"
109 "leal 12(%esp), %ebp\n\t"
114 /*******************************************************************
115 * _global_unwind2 (MSVCRT.@)
117 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
119 TRACE("(%p)\n",frame);
120 RtlUnwind( frame, 0, 0, 0 );
123 /*******************************************************************
124 * _local_unwind2 (MSVCRT.@)
126 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
128 EXCEPTION_REGISTRATION_RECORD reg;
130 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
132 /* Register a handler in case of a nested exception */
133 reg.Handler = MSVCRT_nested_handler;
134 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
135 __wine_push_frame(®);
137 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
139 int level = frame->trylevel;
140 frame->trylevel = frame->scopetable[level].previousTryLevel;
141 if (!frame->scopetable[level].lpfnFilter)
143 FIXME( "__try block cleanup level %d handler %p not fully implemented\n",
144 level, frame->scopetable[level].lpfnHandler );
145 /* FIXME: should probably set ebp to frame->_ebp */
146 frame->scopetable[level].lpfnHandler();
149 __wine_pop_frame(®);
150 TRACE("unwound OK\n");
153 /*********************************************************************
154 * _except_handler2 (MSVCRT.@)
156 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
157 EXCEPTION_REGISTRATION_RECORD* frame,
159 EXCEPTION_REGISTRATION_RECORD** dispatcher)
161 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
162 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
163 frame->Handler, context, dispatcher);
164 return ExceptionContinueSearch;
167 /*********************************************************************
168 * _except_handler3 (MSVCRT.@)
170 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
171 MSVCRT_EXCEPTION_FRAME* frame,
172 PCONTEXT context, void* dispatcher)
174 #if defined(__GNUC__) && defined(__i386__)
177 EXCEPTION_POINTERS exceptPtrs;
178 PSCOPETABLE pScopeTable;
180 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
181 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
182 frame->handler, context, dispatcher);
184 __asm__ __volatile__ ("cld");
186 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
188 /* Unwinding the current frame */
189 _local_unwind2(frame, TRYLEVEL_END);
190 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
191 return ExceptionContinueSearch;
195 /* Hunting for handler */
196 exceptPtrs.ExceptionRecord = rec;
197 exceptPtrs.ContextRecord = context;
198 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
199 trylevel = frame->trylevel;
200 pScopeTable = frame->scopetable;
202 while (trylevel != TRYLEVEL_END)
204 if (pScopeTable[trylevel].lpfnFilter)
206 TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
208 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
210 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
211 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
212 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
214 if (retval == EXCEPTION_CONTINUE_EXECUTION)
215 return ExceptionContinueExecution;
217 if (retval == EXCEPTION_EXECUTE_HANDLER)
219 /* Unwind all higher frames, this one will handle the exception */
220 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
221 _local_unwind2(frame, trylevel);
223 /* Set our trylevel to the enclosing block, and call the __finally
224 * code, which won't return
226 frame->trylevel = pScopeTable->previousTryLevel;
227 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
228 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
229 ERR("Returned from __finally block - expect crash!\n");
232 trylevel = pScopeTable->previousTryLevel;
236 FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
237 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
238 frame->handler, context, dispatcher);
240 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
241 return ExceptionContinueSearch;
244 /*********************************************************************
245 * _abnormal_termination (MSVCRT.@)
247 int CDECL _abnormal_termination(void)
249 FIXME("(void)stub\n");
254 * setjmp/longjmp implementation
258 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
259 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
261 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
262 /* and then jumps to the C backend function */
263 #define DEFINE_SETJMP_ENTRYPOINT(name) \
264 __ASM_GLOBAL_FUNC( name, \
265 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
266 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
267 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
268 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
269 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
270 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
271 "movl 0(%esp),%eax\n\t" \
272 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
273 "jmp " __ASM_NAME("__regs_") # name )
275 /* restore the registers from the jmp buf upon longjmp */
276 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
277 __ASM_GLOBAL_FUNC( longjmp_set_regs,
278 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
279 "movl 8(%esp),%eax\n\t" /* retval */
280 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
281 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
282 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
283 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
284 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
285 "addl $4,%esp\n\t" /* get rid of return address */
286 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
289 * The signatures of the setjmp/longjmp functions do not match that
290 * declared in the setjmp header so they don't follow the regular naming
291 * convention to avoid conflicts.
294 /*******************************************************************
297 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
298 int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
300 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
301 if (jmp->Registration == TRYLEVEL_END)
302 jmp->TryLevel = TRYLEVEL_END;
304 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
306 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
307 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
311 /*******************************************************************
312 * _setjmp3 (MSVCRT.@)
314 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
315 int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
317 jmp->Cookie = MSVCRT_JMP_MAGIC;
319 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
320 if (jmp->Registration == TRYLEVEL_END)
322 jmp->TryLevel = TRYLEVEL_END;
329 va_start( args, nb_args );
330 if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
331 if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
332 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
333 for (i = 0; i < 6 && i < nb_args - 2; i++)
334 jmp->UnwindData[i] = va_arg( args, unsigned long );
338 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
339 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
343 /*********************************************************************
346 int CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
348 unsigned long cur_frame = 0;
350 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
351 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
353 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
354 TRACE("cur_frame=%lx\n",cur_frame);
356 if (cur_frame != jmp->Registration)
357 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
359 if (jmp->Registration)
361 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
362 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
364 MSVCRT_unwind_function unwind_func;
366 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
370 _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
377 longjmp_set_regs( jmp, retval );
380 /*********************************************************************
381 * _seh_longjmp_unwind (MSVCRT.@)
383 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
385 _local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel );
389 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
391 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
398 if (sighandlers[MSVCRT_SIGINT])
400 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
401 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
409 typedef void (*float_handler)(int, int);
411 /* The exception codes are actually NTSTATUS values */
416 } float_exception_map[] = {
417 { EXCEPTION_FLT_DENORMAL_OPERAND, _FPE_DENORMAL },
418 { EXCEPTION_FLT_DIVIDE_BY_ZERO, _FPE_ZERODIVIDE },
419 { EXCEPTION_FLT_INEXACT_RESULT, _FPE_INEXACT },
420 { EXCEPTION_FLT_INVALID_OPERATION, _FPE_INVALID },
421 { EXCEPTION_FLT_OVERFLOW, _FPE_OVERFLOW },
422 { EXCEPTION_FLT_STACK_CHECK, _FPE_STACKOVERFLOW },
423 { EXCEPTION_FLT_UNDERFLOW, _FPE_UNDERFLOW },
426 static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
428 LONG ret = EXCEPTION_CONTINUE_SEARCH;
429 MSVCRT___sighandler_t handler;
431 if (!except || !except->ExceptionRecord)
432 return EXCEPTION_CONTINUE_SEARCH;
434 switch (except->ExceptionRecord->ExceptionCode)
436 case EXCEPTION_ACCESS_VIOLATION:
437 if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
439 if (handler != MSVCRT_SIG_IGN)
441 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
442 handler(MSVCRT_SIGSEGV);
444 ret = EXCEPTION_CONTINUE_EXECUTION;
448 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
449 * the FPE signal handler takes as a second argument the type of
450 * floating point exception.
452 case EXCEPTION_FLT_DENORMAL_OPERAND:
453 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
454 case EXCEPTION_FLT_INEXACT_RESULT:
455 case EXCEPTION_FLT_INVALID_OPERATION:
456 case EXCEPTION_FLT_OVERFLOW:
457 case EXCEPTION_FLT_STACK_CHECK:
458 case EXCEPTION_FLT_UNDERFLOW:
459 if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
461 if (handler != MSVCRT_SIG_IGN)
463 int i, float_signal = _FPE_INVALID;
465 sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
466 for (i = 0; i < sizeof(float_exception_map) /
467 sizeof(float_exception_map[0]); i++)
469 if (float_exception_map[i].status ==
470 except->ExceptionRecord->ExceptionCode)
472 float_signal = float_exception_map[i].signal;
476 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
478 ret = EXCEPTION_CONTINUE_EXECUTION;
481 case EXCEPTION_ILLEGAL_INSTRUCTION:
482 if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
484 if (handler != MSVCRT_SIG_IGN)
486 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
487 handler(MSVCRT_SIGILL);
489 ret = EXCEPTION_CONTINUE_EXECUTION;
496 void msvcrt_init_signals(void)
498 SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
499 SetUnhandledExceptionFilter(msvcrt_exception_filter);
502 void msvcrt_free_signals(void)
504 SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
505 SetUnhandledExceptionFilter(NULL);
508 /*********************************************************************
510 * MS signal handling is described here:
511 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
512 * Some signals may never be generated except through an explicit call to
515 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
517 MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
519 TRACE("(%d, %p)\n", sig, func);
521 if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
525 /* Cases handled internally. Note SIGTERM is never generated by Windows,
526 * so we effectively mask it.
534 ret = sighandlers[sig];
535 sighandlers[sig] = func;
538 ret = MSVCRT_SIG_ERR;
543 /*********************************************************************
546 int CDECL MSVCRT_raise(int sig)
548 MSVCRT___sighandler_t handler;
550 TRACE("(%d)\n", sig);
560 handler = sighandlers[sig];
561 if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
562 if (handler != MSVCRT_SIG_IGN)
564 sighandlers[sig] = MSVCRT_SIG_DFL;
565 if (sig == MSVCRT_SIGFPE)
566 ((float_handler)handler)(sig, _FPE_EXPLICITGEN);
577 /*********************************************************************
578 * _XcptFilter (MSVCRT.@)
580 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
582 TRACE("(%08x,%p)\n", ex, ptr);
583 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
584 return msvcrt_exception_filter(ptr);