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
21 * FIXME: Incomplete support for nested exceptions/try block cleanup.
25 #include "wine/port.h"
32 #include "wine/exception.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(seh);
40 /* VC++ extensions to Win32 SEH */
41 typedef struct _SCOPETABLE
44 int (*lpfnFilter)(PEXCEPTION_POINTERS);
45 int (*lpfnHandler)(void);
46 } SCOPETABLE, *PSCOPETABLE;
48 typedef struct _MSVCRT_EXCEPTION_FRAME
50 EXCEPTION_REGISTRATION_RECORD *prev;
51 void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
52 PCONTEXT, PEXCEPTION_RECORD);
53 PSCOPETABLE scopetable;
56 PEXCEPTION_POINTERS xpointers;
57 } MSVCRT_EXCEPTION_FRAME;
59 #define TRYLEVEL_END (-1) /* End of trylevel list */
61 #if defined(__GNUC__) && defined(__i386__)
62 static inline void call_finally_block( void *code_block, void *base_ptr )
64 __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax"
65 : : "a" (code_block), "g" (base_ptr));
68 static inline int call_filter( int (*func)(PEXCEPTION_POINTERS), void *arg, void *ebp )
71 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
73 : "0" (func), "r" (ebp), "r" (arg)
74 : "ecx", "edx", "memory" );
78 static inline int call_unwind_func( int (*func)(void), void *ebp )
81 __asm__ __volatile__ ("pushl %%ebp\n\t"
92 : "0" (func), "r" (ebp)
93 : "ecx", "edx", "memory" );
102 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
103 EXCEPTION_REGISTRATION_RECORD* frame,
105 EXCEPTION_REGISTRATION_RECORD** dispatch)
107 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
108 return ExceptionContinueSearch;
110 return ExceptionCollidedUnwind;
114 /*********************************************************************
115 * _EH_prolog (MSVCRT.@)
118 /* Provided for VC++ binary compatibility only */
119 __ASM_GLOBAL_FUNC(_EH_prolog,
120 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") /* skip ret addr */
122 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
124 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
126 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
127 "movl %esp, %fs:0\n\t"
128 "movl 12(%esp), %eax\n\t"
129 "movl %ebp, 12(%esp)\n\t"
130 "leal 12(%esp), %ebp\n\t"
132 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
135 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp)
137 EXCEPTION_REGISTRATION_RECORD reg;
139 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
141 /* Register a handler in case of a nested exception */
142 reg.Handler = MSVCRT_nested_handler;
143 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
144 __wine_push_frame(®);
146 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
148 int level = frame->trylevel;
149 frame->trylevel = frame->scopetable[level].previousTryLevel;
150 if (!frame->scopetable[level].lpfnFilter)
152 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
153 level, frame->scopetable[level].lpfnHandler, ebp );
154 call_unwind_func( frame->scopetable[level].lpfnHandler, ebp );
157 __wine_pop_frame(®);
158 TRACE("unwound OK\n");
161 /*******************************************************************
162 * _local_unwind2 (MSVCRT.@)
164 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
166 msvcrt_local_unwind2( frame, trylevel, &frame->_ebp );
169 #endif /* __i386__ */
171 /*******************************************************************
172 * _global_unwind2 (MSVCRT.@)
174 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
176 TRACE("(%p)\n",frame);
177 RtlUnwind( frame, 0, 0, 0 );
180 /*********************************************************************
181 * _except_handler2 (MSVCRT.@)
183 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
184 EXCEPTION_REGISTRATION_RECORD* frame,
186 EXCEPTION_REGISTRATION_RECORD** dispatcher)
188 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
189 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
190 frame->Handler, context, dispatcher);
191 return ExceptionContinueSearch;
194 /*********************************************************************
195 * _except_handler3 (MSVCRT.@)
197 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
198 MSVCRT_EXCEPTION_FRAME* frame,
199 PCONTEXT context, void* dispatcher)
201 #if defined(__GNUC__) && defined(__i386__)
202 int retval, trylevel;
203 EXCEPTION_POINTERS exceptPtrs;
204 PSCOPETABLE pScopeTable;
206 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
207 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
208 frame->handler, context, dispatcher);
210 __asm__ __volatile__ ("cld");
212 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
214 /* Unwinding the current frame */
215 msvcrt_local_unwind2(frame, TRYLEVEL_END, &frame->_ebp);
216 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
217 return ExceptionContinueSearch;
221 /* Hunting for handler */
222 exceptPtrs.ExceptionRecord = rec;
223 exceptPtrs.ContextRecord = context;
224 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
225 trylevel = frame->trylevel;
226 pScopeTable = frame->scopetable;
228 while (trylevel != TRYLEVEL_END)
230 TRACE( "level %d prev %d filter %p\n", trylevel, pScopeTable[trylevel].previousTryLevel,
231 pScopeTable[trylevel].lpfnFilter );
232 if (pScopeTable[trylevel].lpfnFilter)
234 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
236 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
237 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
238 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
240 if (retval == EXCEPTION_CONTINUE_EXECUTION)
241 return ExceptionContinueExecution;
243 if (retval == EXCEPTION_EXECUTE_HANDLER)
245 /* Unwind all higher frames, this one will handle the exception */
246 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
247 msvcrt_local_unwind2(frame, trylevel, &frame->_ebp);
249 /* Set our trylevel to the enclosing block, and call the __finally
250 * code, which won't return
252 frame->trylevel = pScopeTable[trylevel].previousTryLevel;
253 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
254 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
255 ERR("Returned from __finally block - expect crash!\n");
258 trylevel = pScopeTable[trylevel].previousTryLevel;
262 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
263 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
264 frame->handler, context, dispatcher);
266 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
267 return ExceptionContinueSearch;
270 /*********************************************************************
271 * _abnormal_termination (MSVCRT.@)
273 int CDECL _abnormal_termination(void)
275 FIXME("(void)stub\n");
280 * setjmp/longjmp implementation
284 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
285 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
287 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
288 /* and then jumps to the C backend function */
289 #define DEFINE_SETJMP_ENTRYPOINT(name) \
290 __ASM_GLOBAL_FUNC( name, \
291 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
292 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
293 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
294 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
295 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
296 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
297 "movl 0(%esp),%eax\n\t" \
298 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
299 "jmp " __ASM_NAME("__regs_") # name )
301 /* restore the registers from the jmp buf upon longjmp */
302 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
303 __ASM_GLOBAL_FUNC( longjmp_set_regs,
304 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
305 "movl 8(%esp),%eax\n\t" /* retval */
306 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
307 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
308 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
309 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
310 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
311 "addl $4,%esp\n\t" /* get rid of return address */
312 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
315 * The signatures of the setjmp/longjmp functions do not match that
316 * declared in the setjmp header so they don't follow the regular naming
317 * convention to avoid conflicts.
320 /*******************************************************************
323 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
324 int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
326 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
327 if (jmp->Registration == ~0UL)
328 jmp->TryLevel = TRYLEVEL_END;
330 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
332 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
333 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
337 /*******************************************************************
338 * _setjmp3 (MSVCRT.@)
340 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
341 int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
343 jmp->Cookie = MSVCRT_JMP_MAGIC;
345 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
346 if (jmp->Registration == ~0UL)
348 jmp->TryLevel = TRYLEVEL_END;
355 va_start( args, nb_args );
356 if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
357 if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
358 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
359 for (i = 0; i < 6 && i < nb_args - 2; i++)
360 jmp->UnwindData[i] = va_arg( args, unsigned long );
364 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
365 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
369 /*********************************************************************
372 int CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
374 unsigned long cur_frame = 0;
376 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
377 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
379 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
380 TRACE("cur_frame=%lx\n",cur_frame);
382 if (cur_frame != jmp->Registration)
383 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
385 if (jmp->Registration)
387 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
388 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
390 MSVCRT_unwind_function unwind_func;
392 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
396 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
397 jmp->TryLevel, (void *)jmp->Ebp);
403 longjmp_set_regs( jmp, retval );
406 /*********************************************************************
407 * _seh_longjmp_unwind (MSVCRT.@)
409 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
411 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel, (void *)jmp->Ebp );
415 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
417 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
424 if (sighandlers[MSVCRT_SIGINT])
426 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
427 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
435 typedef void (*float_handler)(int, int);
437 /* The exception codes are actually NTSTATUS values */
442 } float_exception_map[] = {
443 { EXCEPTION_FLT_DENORMAL_OPERAND, MSVCRT__FPE_DENORMAL },
444 { EXCEPTION_FLT_DIVIDE_BY_ZERO, MSVCRT__FPE_ZERODIVIDE },
445 { EXCEPTION_FLT_INEXACT_RESULT, MSVCRT__FPE_INEXACT },
446 { EXCEPTION_FLT_INVALID_OPERATION, MSVCRT__FPE_INVALID },
447 { EXCEPTION_FLT_OVERFLOW, MSVCRT__FPE_OVERFLOW },
448 { EXCEPTION_FLT_STACK_CHECK, MSVCRT__FPE_STACKOVERFLOW },
449 { EXCEPTION_FLT_UNDERFLOW, MSVCRT__FPE_UNDERFLOW },
452 static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
454 LONG ret = EXCEPTION_CONTINUE_SEARCH;
455 MSVCRT___sighandler_t handler;
457 if (!except || !except->ExceptionRecord)
458 return EXCEPTION_CONTINUE_SEARCH;
460 switch (except->ExceptionRecord->ExceptionCode)
462 case EXCEPTION_ACCESS_VIOLATION:
463 if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
465 if (handler != MSVCRT_SIG_IGN)
467 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
468 handler(MSVCRT_SIGSEGV);
470 ret = EXCEPTION_CONTINUE_EXECUTION;
473 /* According to msdn,
474 * the FPE signal handler takes as a second argument the type of
475 * floating point exception.
477 case EXCEPTION_FLT_DENORMAL_OPERAND:
478 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
479 case EXCEPTION_FLT_INEXACT_RESULT:
480 case EXCEPTION_FLT_INVALID_OPERATION:
481 case EXCEPTION_FLT_OVERFLOW:
482 case EXCEPTION_FLT_STACK_CHECK:
483 case EXCEPTION_FLT_UNDERFLOW:
484 if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
486 if (handler != MSVCRT_SIG_IGN)
489 int float_signal = MSVCRT__FPE_INVALID;
491 sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
492 for (i = 0; i < sizeof(float_exception_map) /
493 sizeof(float_exception_map[0]); i++)
495 if (float_exception_map[i].status ==
496 except->ExceptionRecord->ExceptionCode)
498 float_signal = float_exception_map[i].signal;
502 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
504 ret = EXCEPTION_CONTINUE_EXECUTION;
507 case EXCEPTION_ILLEGAL_INSTRUCTION:
508 case EXCEPTION_PRIV_INSTRUCTION:
509 if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
511 if (handler != MSVCRT_SIG_IGN)
513 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
514 handler(MSVCRT_SIGILL);
516 ret = EXCEPTION_CONTINUE_EXECUTION;
523 void msvcrt_init_signals(void)
525 SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
526 SetUnhandledExceptionFilter(msvcrt_exception_filter);
529 void msvcrt_free_signals(void)
531 SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
532 SetUnhandledExceptionFilter(NULL);
535 /*********************************************************************
537 * Some signals may never be generated except through an explicit call to
540 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
542 MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
544 TRACE("(%d, %p)\n", sig, func);
546 if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
550 /* Cases handled internally. Note SIGTERM is never generated by Windows,
551 * so we effectively mask it.
559 ret = sighandlers[sig];
560 sighandlers[sig] = func;
563 ret = MSVCRT_SIG_ERR;
568 /*********************************************************************
571 int CDECL MSVCRT_raise(int sig)
573 MSVCRT___sighandler_t handler;
575 TRACE("(%d)\n", sig);
585 handler = sighandlers[sig];
586 if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
587 if (handler != MSVCRT_SIG_IGN)
589 sighandlers[sig] = MSVCRT_SIG_DFL;
590 if (sig == MSVCRT_SIGFPE)
591 ((float_handler)handler)(sig, MSVCRT__FPE_EXPLICITGEN);
602 /*********************************************************************
603 * _XcptFilter (MSVCRT.@)
605 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
607 TRACE("(%08x,%p)\n", ex, ptr);
608 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
609 return msvcrt_exception_filter(ptr);