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,
123 "movl %esp, %fs:0\n\t"
124 "movl 12(%esp), %eax\n\t"
125 "movl %ebp, 12(%esp)\n\t"
126 "leal 12(%esp), %ebp\n\t"
130 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp)
132 EXCEPTION_REGISTRATION_RECORD reg;
134 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
136 /* Register a handler in case of a nested exception */
137 reg.Handler = MSVCRT_nested_handler;
138 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
139 __wine_push_frame(®);
141 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
143 int level = frame->trylevel;
144 frame->trylevel = frame->scopetable[level].previousTryLevel;
145 if (!frame->scopetable[level].lpfnFilter)
147 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
148 level, frame->scopetable[level].lpfnHandler, ebp );
149 call_unwind_func( frame->scopetable[level].lpfnHandler, ebp );
152 __wine_pop_frame(®);
153 TRACE("unwound OK\n");
156 /*******************************************************************
157 * _local_unwind2 (MSVCRT.@)
159 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
161 msvcrt_local_unwind2( frame, trylevel, &frame->_ebp );
164 #endif /* __i386__ */
166 /*******************************************************************
167 * _global_unwind2 (MSVCRT.@)
169 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
171 TRACE("(%p)\n",frame);
172 RtlUnwind( frame, 0, 0, 0 );
175 /*********************************************************************
176 * _except_handler2 (MSVCRT.@)
178 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
179 EXCEPTION_REGISTRATION_RECORD* frame,
181 EXCEPTION_REGISTRATION_RECORD** dispatcher)
183 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
184 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
185 frame->Handler, context, dispatcher);
186 return ExceptionContinueSearch;
189 /*********************************************************************
190 * _except_handler3 (MSVCRT.@)
192 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
193 MSVCRT_EXCEPTION_FRAME* frame,
194 PCONTEXT context, void* dispatcher)
196 #if defined(__GNUC__) && defined(__i386__)
197 int retval, trylevel;
198 EXCEPTION_POINTERS exceptPtrs;
199 PSCOPETABLE pScopeTable;
201 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
202 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
203 frame->handler, context, dispatcher);
205 __asm__ __volatile__ ("cld");
207 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
209 /* Unwinding the current frame */
210 msvcrt_local_unwind2(frame, TRYLEVEL_END, &frame->_ebp);
211 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
212 return ExceptionContinueSearch;
216 /* Hunting for handler */
217 exceptPtrs.ExceptionRecord = rec;
218 exceptPtrs.ContextRecord = context;
219 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
220 trylevel = frame->trylevel;
221 pScopeTable = frame->scopetable;
223 while (trylevel != TRYLEVEL_END)
225 TRACE( "level %d prev %d filter %p\n", trylevel, pScopeTable[trylevel].previousTryLevel,
226 pScopeTable[trylevel].lpfnFilter );
227 if (pScopeTable[trylevel].lpfnFilter)
229 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
231 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
232 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
233 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
235 if (retval == EXCEPTION_CONTINUE_EXECUTION)
236 return ExceptionContinueExecution;
238 if (retval == EXCEPTION_EXECUTE_HANDLER)
240 /* Unwind all higher frames, this one will handle the exception */
241 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
242 msvcrt_local_unwind2(frame, trylevel, &frame->_ebp);
244 /* Set our trylevel to the enclosing block, and call the __finally
245 * code, which won't return
247 frame->trylevel = pScopeTable[trylevel].previousTryLevel;
248 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
249 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
250 ERR("Returned from __finally block - expect crash!\n");
253 trylevel = pScopeTable[trylevel].previousTryLevel;
257 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
258 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
259 frame->handler, context, dispatcher);
261 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
262 return ExceptionContinueSearch;
265 /*********************************************************************
266 * _abnormal_termination (MSVCRT.@)
268 int CDECL _abnormal_termination(void)
270 FIXME("(void)stub\n");
275 * setjmp/longjmp implementation
279 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
280 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
282 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
283 /* and then jumps to the C backend function */
284 #define DEFINE_SETJMP_ENTRYPOINT(name) \
285 __ASM_GLOBAL_FUNC( name, \
286 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
287 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
288 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
289 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
290 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
291 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
292 "movl 0(%esp),%eax\n\t" \
293 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
294 "jmp " __ASM_NAME("__regs_") # name )
296 /* restore the registers from the jmp buf upon longjmp */
297 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
298 __ASM_GLOBAL_FUNC( longjmp_set_regs,
299 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
300 "movl 8(%esp),%eax\n\t" /* retval */
301 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
302 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
303 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
304 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
305 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
306 "addl $4,%esp\n\t" /* get rid of return address */
307 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
310 * The signatures of the setjmp/longjmp functions do not match that
311 * declared in the setjmp header so they don't follow the regular naming
312 * convention to avoid conflicts.
315 /*******************************************************************
318 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
319 int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
321 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
322 if (jmp->Registration == ~0UL)
323 jmp->TryLevel = TRYLEVEL_END;
325 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
327 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
328 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
332 /*******************************************************************
333 * _setjmp3 (MSVCRT.@)
335 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
336 int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
338 jmp->Cookie = MSVCRT_JMP_MAGIC;
340 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
341 if (jmp->Registration == ~0UL)
343 jmp->TryLevel = TRYLEVEL_END;
350 va_start( args, nb_args );
351 if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
352 if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
353 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
354 for (i = 0; i < 6 && i < nb_args - 2; i++)
355 jmp->UnwindData[i] = va_arg( args, unsigned long );
359 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
360 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
364 /*********************************************************************
367 int CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
369 unsigned long cur_frame = 0;
371 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
372 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
374 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
375 TRACE("cur_frame=%lx\n",cur_frame);
377 if (cur_frame != jmp->Registration)
378 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
380 if (jmp->Registration)
382 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
383 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
385 MSVCRT_unwind_function unwind_func;
387 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
391 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
392 jmp->TryLevel, (void *)jmp->Ebp);
398 longjmp_set_regs( jmp, retval );
401 /*********************************************************************
402 * _seh_longjmp_unwind (MSVCRT.@)
404 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
406 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel, (void *)jmp->Ebp );
410 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
412 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
419 if (sighandlers[MSVCRT_SIGINT])
421 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
422 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
430 typedef void (*float_handler)(int, int);
432 /* The exception codes are actually NTSTATUS values */
437 } float_exception_map[] = {
438 { EXCEPTION_FLT_DENORMAL_OPERAND, MSVCRT__FPE_DENORMAL },
439 { EXCEPTION_FLT_DIVIDE_BY_ZERO, MSVCRT__FPE_ZERODIVIDE },
440 { EXCEPTION_FLT_INEXACT_RESULT, MSVCRT__FPE_INEXACT },
441 { EXCEPTION_FLT_INVALID_OPERATION, MSVCRT__FPE_INVALID },
442 { EXCEPTION_FLT_OVERFLOW, MSVCRT__FPE_OVERFLOW },
443 { EXCEPTION_FLT_STACK_CHECK, MSVCRT__FPE_STACKOVERFLOW },
444 { EXCEPTION_FLT_UNDERFLOW, MSVCRT__FPE_UNDERFLOW },
447 static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
449 LONG ret = EXCEPTION_CONTINUE_SEARCH;
450 MSVCRT___sighandler_t handler;
452 if (!except || !except->ExceptionRecord)
453 return EXCEPTION_CONTINUE_SEARCH;
455 switch (except->ExceptionRecord->ExceptionCode)
457 case EXCEPTION_ACCESS_VIOLATION:
458 if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
460 if (handler != MSVCRT_SIG_IGN)
462 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
463 handler(MSVCRT_SIGSEGV);
465 ret = EXCEPTION_CONTINUE_EXECUTION;
468 /* According to msdn,
469 * the FPE signal handler takes as a second argument the type of
470 * floating point exception.
472 case EXCEPTION_FLT_DENORMAL_OPERAND:
473 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
474 case EXCEPTION_FLT_INEXACT_RESULT:
475 case EXCEPTION_FLT_INVALID_OPERATION:
476 case EXCEPTION_FLT_OVERFLOW:
477 case EXCEPTION_FLT_STACK_CHECK:
478 case EXCEPTION_FLT_UNDERFLOW:
479 if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
481 if (handler != MSVCRT_SIG_IGN)
484 int float_signal = MSVCRT__FPE_INVALID;
486 sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
487 for (i = 0; i < sizeof(float_exception_map) /
488 sizeof(float_exception_map[0]); i++)
490 if (float_exception_map[i].status ==
491 except->ExceptionRecord->ExceptionCode)
493 float_signal = float_exception_map[i].signal;
497 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
499 ret = EXCEPTION_CONTINUE_EXECUTION;
502 case EXCEPTION_ILLEGAL_INSTRUCTION:
503 case EXCEPTION_PRIV_INSTRUCTION:
504 if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
506 if (handler != MSVCRT_SIG_IGN)
508 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
509 handler(MSVCRT_SIGILL);
511 ret = EXCEPTION_CONTINUE_EXECUTION;
518 void msvcrt_init_signals(void)
520 SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
521 SetUnhandledExceptionFilter(msvcrt_exception_filter);
524 void msvcrt_free_signals(void)
526 SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
527 SetUnhandledExceptionFilter(NULL);
530 /*********************************************************************
532 * Some signals may never be generated except through an explicit call to
535 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
537 MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
539 TRACE("(%d, %p)\n", sig, func);
541 if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
545 /* Cases handled internally. Note SIGTERM is never generated by Windows,
546 * so we effectively mask it.
554 ret = sighandlers[sig];
555 sighandlers[sig] = func;
558 ret = MSVCRT_SIG_ERR;
563 /*********************************************************************
566 int CDECL MSVCRT_raise(int sig)
568 MSVCRT___sighandler_t handler;
570 TRACE("(%d)\n", sig);
580 handler = sighandlers[sig];
581 if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
582 if (handler != MSVCRT_SIG_IGN)
584 sighandlers[sig] = MSVCRT_SIG_DFL;
585 if (sig == MSVCRT_SIGFPE)
586 ((float_handler)handler)(sig, MSVCRT__FPE_EXPLICITGEN);
597 /*********************************************************************
598 * _XcptFilter (MSVCRT.@)
600 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
602 TRACE("(%08x,%p)\n", ex, ptr);
603 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
604 return msvcrt_exception_filter(ptr);