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 "msvcrt/float.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(seh);
41 /* VC++ extensions to Win32 SEH */
42 typedef struct _SCOPETABLE
45 int (*lpfnFilter)(PEXCEPTION_POINTERS);
46 int (*lpfnHandler)(void);
47 } SCOPETABLE, *PSCOPETABLE;
49 typedef struct _MSVCRT_EXCEPTION_FRAME
51 EXCEPTION_REGISTRATION_RECORD *prev;
52 void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
53 PCONTEXT, PEXCEPTION_RECORD);
54 PSCOPETABLE scopetable;
57 PEXCEPTION_POINTERS xpointers;
58 } MSVCRT_EXCEPTION_FRAME;
60 #define TRYLEVEL_END (-1) /* End of trylevel list */
62 #if defined(__GNUC__) && defined(__i386__)
63 static inline void call_finally_block( void *code_block, void *base_ptr )
65 __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax"
66 : : "a" (code_block), "g" (base_ptr));
69 static inline int call_filter( int (*func)(PEXCEPTION_POINTERS), void *arg, void *ebp )
72 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
74 : "0" (func), "r" (ebp), "r" (arg)
75 : "ecx", "edx", "memory" );
79 static inline int call_unwind_func( int (*func)(void), void *ebp )
82 __asm__ __volatile__ ("pushl %%ebp\n\t"
93 : "0" (func), "r" (ebp)
94 : "ecx", "edx", "memory" );
100 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
101 EXCEPTION_REGISTRATION_RECORD* frame,
103 EXCEPTION_REGISTRATION_RECORD** dispatch)
105 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
106 return ExceptionContinueSearch;
108 return ExceptionCollidedUnwind;
112 /*********************************************************************
113 * _EH_prolog (MSVCRT.@)
116 /* Provided for VC++ binary compatibility only */
117 __ASM_GLOBAL_FUNC(_EH_prolog,
121 "movl %esp, %fs:0\n\t"
122 "movl 12(%esp), %eax\n\t"
123 "movl %ebp, 12(%esp)\n\t"
124 "leal 12(%esp), %ebp\n\t"
128 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp)
130 EXCEPTION_REGISTRATION_RECORD reg;
132 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
134 /* Register a handler in case of a nested exception */
135 reg.Handler = MSVCRT_nested_handler;
136 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
137 __wine_push_frame(®);
139 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
141 int level = frame->trylevel;
142 frame->trylevel = frame->scopetable[level].previousTryLevel;
143 if (!frame->scopetable[level].lpfnFilter)
145 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
146 level, frame->scopetable[level].lpfnHandler, ebp );
147 call_unwind_func( frame->scopetable[level].lpfnHandler, ebp );
150 __wine_pop_frame(®);
151 TRACE("unwound OK\n");
154 /*******************************************************************
155 * _local_unwind2 (MSVCRT.@)
157 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
159 msvcrt_local_unwind2( frame, trylevel, &frame->_ebp );
162 #endif /* __i386__ */
164 /*******************************************************************
165 * _global_unwind2 (MSVCRT.@)
167 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
169 TRACE("(%p)\n",frame);
170 RtlUnwind( frame, 0, 0, 0 );
173 /*********************************************************************
174 * _except_handler2 (MSVCRT.@)
176 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
177 EXCEPTION_REGISTRATION_RECORD* frame,
179 EXCEPTION_REGISTRATION_RECORD** dispatcher)
181 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
182 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
183 frame->Handler, context, dispatcher);
184 return ExceptionContinueSearch;
187 /*********************************************************************
188 * _except_handler3 (MSVCRT.@)
190 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
191 MSVCRT_EXCEPTION_FRAME* frame,
192 PCONTEXT context, void* dispatcher)
194 #if defined(__GNUC__) && defined(__i386__)
195 int retval, trylevel;
196 EXCEPTION_POINTERS exceptPtrs;
197 PSCOPETABLE pScopeTable;
199 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
200 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
201 frame->handler, context, dispatcher);
203 __asm__ __volatile__ ("cld");
205 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
207 /* Unwinding the current frame */
208 msvcrt_local_unwind2(frame, TRYLEVEL_END, &frame->_ebp);
209 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
210 return ExceptionContinueSearch;
214 /* Hunting for handler */
215 exceptPtrs.ExceptionRecord = rec;
216 exceptPtrs.ContextRecord = context;
217 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
218 trylevel = frame->trylevel;
219 pScopeTable = frame->scopetable;
221 while (trylevel != TRYLEVEL_END)
223 if (pScopeTable[trylevel].lpfnFilter)
225 TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
227 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
229 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
230 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
231 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
233 if (retval == EXCEPTION_CONTINUE_EXECUTION)
234 return ExceptionContinueExecution;
236 if (retval == EXCEPTION_EXECUTE_HANDLER)
238 /* Unwind all higher frames, this one will handle the exception */
239 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
240 msvcrt_local_unwind2(frame, trylevel, &frame->_ebp);
242 /* Set our trylevel to the enclosing block, and call the __finally
243 * code, which won't return
245 frame->trylevel = pScopeTable->previousTryLevel;
246 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
247 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
248 ERR("Returned from __finally block - expect crash!\n");
251 trylevel = pScopeTable->previousTryLevel;
255 FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
256 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
257 frame->handler, context, dispatcher);
259 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
260 return ExceptionContinueSearch;
263 /*********************************************************************
264 * _abnormal_termination (MSVCRT.@)
266 int CDECL _abnormal_termination(void)
268 FIXME("(void)stub\n");
273 * setjmp/longjmp implementation
277 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
278 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
280 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
281 /* and then jumps to the C backend function */
282 #define DEFINE_SETJMP_ENTRYPOINT(name) \
283 __ASM_GLOBAL_FUNC( name, \
284 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
285 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
286 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
287 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
288 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
289 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
290 "movl 0(%esp),%eax\n\t" \
291 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
292 "jmp " __ASM_NAME("__regs_") # name )
294 /* restore the registers from the jmp buf upon longjmp */
295 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
296 __ASM_GLOBAL_FUNC( longjmp_set_regs,
297 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
298 "movl 8(%esp),%eax\n\t" /* retval */
299 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
300 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
301 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
302 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
303 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
304 "addl $4,%esp\n\t" /* get rid of return address */
305 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
308 * The signatures of the setjmp/longjmp functions do not match that
309 * declared in the setjmp header so they don't follow the regular naming
310 * convention to avoid conflicts.
313 /*******************************************************************
316 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
317 int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
319 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
320 if (jmp->Registration == ~0UL)
321 jmp->TryLevel = TRYLEVEL_END;
323 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
325 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
326 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
330 /*******************************************************************
331 * _setjmp3 (MSVCRT.@)
333 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
334 int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
336 jmp->Cookie = MSVCRT_JMP_MAGIC;
338 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
339 if (jmp->Registration == ~0UL)
341 jmp->TryLevel = TRYLEVEL_END;
348 va_start( args, nb_args );
349 if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
350 if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
351 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
352 for (i = 0; i < 6 && i < nb_args - 2; i++)
353 jmp->UnwindData[i] = va_arg( args, unsigned long );
357 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
358 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
362 /*********************************************************************
365 int CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
367 unsigned long cur_frame = 0;
369 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
370 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
372 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
373 TRACE("cur_frame=%lx\n",cur_frame);
375 if (cur_frame != jmp->Registration)
376 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
378 if (jmp->Registration)
380 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
381 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
383 MSVCRT_unwind_function unwind_func;
385 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
389 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
390 jmp->TryLevel, (void *)jmp->Ebp);
396 longjmp_set_regs( jmp, retval );
399 /*********************************************************************
400 * _seh_longjmp_unwind (MSVCRT.@)
402 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
404 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel, (void *)jmp->Ebp );
408 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
410 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
417 if (sighandlers[MSVCRT_SIGINT])
419 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
420 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
428 typedef void (*float_handler)(int, int);
430 /* The exception codes are actually NTSTATUS values */
435 } float_exception_map[] = {
436 { EXCEPTION_FLT_DENORMAL_OPERAND, _FPE_DENORMAL },
437 { EXCEPTION_FLT_DIVIDE_BY_ZERO, _FPE_ZERODIVIDE },
438 { EXCEPTION_FLT_INEXACT_RESULT, _FPE_INEXACT },
439 { EXCEPTION_FLT_INVALID_OPERATION, _FPE_INVALID },
440 { EXCEPTION_FLT_OVERFLOW, _FPE_OVERFLOW },
441 { EXCEPTION_FLT_STACK_CHECK, _FPE_STACKOVERFLOW },
442 { EXCEPTION_FLT_UNDERFLOW, _FPE_UNDERFLOW },
445 static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
447 LONG ret = EXCEPTION_CONTINUE_SEARCH;
448 MSVCRT___sighandler_t handler;
450 if (!except || !except->ExceptionRecord)
451 return EXCEPTION_CONTINUE_SEARCH;
453 switch (except->ExceptionRecord->ExceptionCode)
455 case EXCEPTION_ACCESS_VIOLATION:
456 if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
458 if (handler != MSVCRT_SIG_IGN)
460 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
461 handler(MSVCRT_SIGSEGV);
463 ret = EXCEPTION_CONTINUE_EXECUTION;
466 /* According to msdn,
467 * the FPE signal handler takes as a second argument the type of
468 * floating point exception.
470 case EXCEPTION_FLT_DENORMAL_OPERAND:
471 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
472 case EXCEPTION_FLT_INEXACT_RESULT:
473 case EXCEPTION_FLT_INVALID_OPERATION:
474 case EXCEPTION_FLT_OVERFLOW:
475 case EXCEPTION_FLT_STACK_CHECK:
476 case EXCEPTION_FLT_UNDERFLOW:
477 if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
479 if (handler != MSVCRT_SIG_IGN)
481 int i, float_signal = _FPE_INVALID;
483 sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
484 for (i = 0; i < sizeof(float_exception_map) /
485 sizeof(float_exception_map[0]); i++)
487 if (float_exception_map[i].status ==
488 except->ExceptionRecord->ExceptionCode)
490 float_signal = float_exception_map[i].signal;
494 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
496 ret = EXCEPTION_CONTINUE_EXECUTION;
499 case EXCEPTION_ILLEGAL_INSTRUCTION:
500 case EXCEPTION_PRIV_INSTRUCTION:
501 if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
503 if (handler != MSVCRT_SIG_IGN)
505 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
506 handler(MSVCRT_SIGILL);
508 ret = EXCEPTION_CONTINUE_EXECUTION;
515 void msvcrt_init_signals(void)
517 SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
518 SetUnhandledExceptionFilter(msvcrt_exception_filter);
521 void msvcrt_free_signals(void)
523 SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
524 SetUnhandledExceptionFilter(NULL);
527 /*********************************************************************
529 * Some signals may never be generated except through an explicit call to
532 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
534 MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
536 TRACE("(%d, %p)\n", sig, func);
538 if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
542 /* Cases handled internally. Note SIGTERM is never generated by Windows,
543 * so we effectively mask it.
551 ret = sighandlers[sig];
552 sighandlers[sig] = func;
555 ret = MSVCRT_SIG_ERR;
560 /*********************************************************************
563 int CDECL MSVCRT_raise(int sig)
565 MSVCRT___sighandler_t handler;
567 TRACE("(%d)\n", sig);
577 handler = sighandlers[sig];
578 if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
579 if (handler != MSVCRT_SIG_IGN)
581 sighandlers[sig] = MSVCRT_SIG_DFL;
582 if (sig == MSVCRT_SIGFPE)
583 ((float_handler)handler)(sig, _FPE_EXPLICITGEN);
594 /*********************************************************************
595 * _XcptFilter (MSVCRT.@)
597 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
599 TRACE("(%08x,%p)\n", ex, ptr);
600 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
601 return msvcrt_exception_filter(ptr);