push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / dlls / msvcrt / except.c
blob9b254f858a7955ce3504727da6ff84687590d43b
1 /*
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.
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winternl.h"
32 #include "wine/exception.h"
33 #include "msvcrt.h"
34 #include "excpt.h"
35 #include "wincon.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(seh);
40 /* VC++ extensions to Win32 SEH */
41 typedef struct _SCOPETABLE
43 int previousTryLevel;
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;
54 int trylevel;
55 int _ebp;
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 )
70 int ret;
71 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
72 : "=a" (ret)
73 : "0" (func), "r" (ebp), "r" (arg)
74 : "ecx", "edx", "memory" );
75 return ret;
78 static inline int call_unwind_func( int (*func)(void), void *ebp )
80 int ret;
81 __asm__ __volatile__ ("pushl %%ebp\n\t"
82 "pushl %%ebx\n\t"
83 "pushl %%esi\n\t"
84 "pushl %%edi\n\t"
85 "movl %2,%%ebp\n\t"
86 "call *%0\n\t"
87 "popl %%edi\n\t"
88 "popl %%esi\n\t"
89 "popl %%ebx\n\t"
90 "popl %%ebp"
91 : "=a" (ret)
92 : "0" (func), "r" (ebp)
93 : "ecx", "edx", "memory" );
94 return ret;
97 #endif
100 #ifdef __i386__
102 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
103 EXCEPTION_REGISTRATION_RECORD* frame,
104 PCONTEXT context,
105 EXCEPTION_REGISTRATION_RECORD** dispatch)
107 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
108 return ExceptionContinueSearch;
109 *dispatch = frame;
110 return ExceptionCollidedUnwind;
114 /*********************************************************************
115 * _EH_prolog (MSVCRT.@)
118 /* Provided for VC++ binary compatibility only */
119 __ASM_GLOBAL_FUNC(_EH_prolog,
120 "pushl $-1\n\t"
121 "pushl %eax\n\t"
122 "pushl %fs:0\n\t"
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"
127 "pushl %eax\n\t"
128 "ret")
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(&reg);
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(&reg);
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,
180 PCONTEXT context,
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;
214 else
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;
256 #else
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);
260 #endif
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");
271 return 0;
275 * setjmp/longjmp implementation
278 #ifdef __i386__
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 /*******************************************************************
316 * _setjmp (MSVCRT.@)
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;
324 else
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 );
329 return 0;
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;
339 jmp->UnwindFunc = 0;
340 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
341 if (jmp->Registration == ~0UL)
343 jmp->TryLevel = TRYLEVEL_END;
345 else
347 int i;
348 va_list args;
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 );
356 va_end( args );
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 );
361 return 0;
364 /*********************************************************************
365 * longjmp (MSVCRT.@)
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;
388 unwind_func(jmp);
390 else
391 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
392 jmp->TryLevel, (void *)jmp->Ebp);
395 if (!retval)
396 retval = 1;
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 );
408 #endif /* i386 */
410 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
412 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
414 BOOL ret = FALSE;
416 switch (ctrlType)
418 case CTRL_C_EVENT:
419 if (sighandlers[MSVCRT_SIGINT])
421 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
422 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
423 ret = TRUE;
425 break;
427 return ret;
430 typedef void (*float_handler)(int, int);
432 /* The exception codes are actually NTSTATUS values */
433 static const struct
435 NTSTATUS status;
436 int signal;
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;
467 break;
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)
483 unsigned int i;
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;
494 break;
497 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
499 ret = EXCEPTION_CONTINUE_EXECUTION;
501 break;
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;
513 break;
515 return ret;
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 /*********************************************************************
531 * signal (MSVCRT.@)
532 * Some signals may never be generated except through an explicit call to
533 * raise.
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;
543 switch (sig)
545 /* Cases handled internally. Note SIGTERM is never generated by Windows,
546 * so we effectively mask it.
548 case MSVCRT_SIGABRT:
549 case MSVCRT_SIGFPE:
550 case MSVCRT_SIGILL:
551 case MSVCRT_SIGSEGV:
552 case MSVCRT_SIGINT:
553 case MSVCRT_SIGTERM:
554 ret = sighandlers[sig];
555 sighandlers[sig] = func;
556 break;
557 default:
558 ret = MSVCRT_SIG_ERR;
560 return ret;
563 /*********************************************************************
564 * raise (MSVCRT.@)
566 int CDECL MSVCRT_raise(int sig)
568 MSVCRT___sighandler_t handler;
570 TRACE("(%d)\n", sig);
572 switch (sig)
574 case MSVCRT_SIGABRT:
575 case MSVCRT_SIGFPE:
576 case MSVCRT_SIGILL:
577 case MSVCRT_SIGSEGV:
578 case MSVCRT_SIGINT:
579 case MSVCRT_SIGTERM:
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);
587 else
588 handler(sig);
590 break;
591 default:
592 return -1;
594 return 0;
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);