push(f) 25359c458b329fe03f66bef506b8062c200b9eb4
[wine/hacks.git] / dlls / msvcrt / except.c
blob5de42db376c5dc7359d6af584d15e7d24b280ecb
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 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") /* skip ret addr */
121 "pushl $-1\n\t"
122 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
123 "pushl %eax\n\t"
124 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
125 "pushl %fs:0\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"
131 "pushl %eax\n\t"
132 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
133 "ret")
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(&reg);
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(&reg);
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,
185 PCONTEXT context,
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;
219 else
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;
261 #else
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);
265 #endif
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");
276 return 0;
280 * setjmp/longjmp implementation
283 #ifdef __i386__
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 /*******************************************************************
321 * _setjmp (MSVCRT.@)
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;
329 else
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 );
334 return 0;
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;
344 jmp->UnwindFunc = 0;
345 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
346 if (jmp->Registration == ~0UL)
348 jmp->TryLevel = TRYLEVEL_END;
350 else
352 int i;
353 va_list args;
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 );
361 va_end( args );
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 );
366 return 0;
369 /*********************************************************************
370 * longjmp (MSVCRT.@)
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;
393 unwind_func(jmp);
395 else
396 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
397 jmp->TryLevel, (void *)jmp->Ebp);
400 if (!retval)
401 retval = 1;
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 );
413 #endif /* i386 */
415 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
417 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
419 BOOL ret = FALSE;
421 switch (ctrlType)
423 case CTRL_C_EVENT:
424 if (sighandlers[MSVCRT_SIGINT])
426 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
427 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
428 ret = TRUE;
430 break;
432 return ret;
435 typedef void (*float_handler)(int, int);
437 /* The exception codes are actually NTSTATUS values */
438 static const struct
440 NTSTATUS status;
441 int signal;
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;
472 break;
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)
488 unsigned int i;
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;
499 break;
502 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
504 ret = EXCEPTION_CONTINUE_EXECUTION;
506 break;
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;
518 break;
520 return ret;
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 /*********************************************************************
536 * signal (MSVCRT.@)
537 * Some signals may never be generated except through an explicit call to
538 * raise.
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;
548 switch (sig)
550 /* Cases handled internally. Note SIGTERM is never generated by Windows,
551 * so we effectively mask it.
553 case MSVCRT_SIGABRT:
554 case MSVCRT_SIGFPE:
555 case MSVCRT_SIGILL:
556 case MSVCRT_SIGSEGV:
557 case MSVCRT_SIGINT:
558 case MSVCRT_SIGTERM:
559 ret = sighandlers[sig];
560 sighandlers[sig] = func;
561 break;
562 default:
563 ret = MSVCRT_SIG_ERR;
565 return ret;
568 /*********************************************************************
569 * raise (MSVCRT.@)
571 int CDECL MSVCRT_raise(int sig)
573 MSVCRT___sighandler_t handler;
575 TRACE("(%d)\n", sig);
577 switch (sig)
579 case MSVCRT_SIGABRT:
580 case MSVCRT_SIGFPE:
581 case MSVCRT_SIGILL:
582 case MSVCRT_SIGSEGV:
583 case MSVCRT_SIGINT:
584 case MSVCRT_SIGTERM:
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);
592 else
593 handler(sig);
595 break;
596 default:
597 return -1;
599 return 0;
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);