quartz: Only allocate 1 buffer in transform filter.
[wine/wine64.git] / dlls / msvcrt / except.c
blob296c65602130f6c36368baf8abab1787f027ad9d
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 "msvcrt/float.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(seh);
41 /* VC++ extensions to Win32 SEH */
42 typedef struct _SCOPETABLE
44 int previousTryLevel;
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;
55 int trylevel;
56 int _ebp;
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 )
71 int ret;
72 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
73 : "=a" (ret)
74 : "0" (func), "r" (ebp), "r" (arg)
75 : "ecx", "edx", "memory" );
76 return ret;
79 static inline int call_unwind_func( int (*func)(void), void *ebp )
81 int ret;
82 __asm__ __volatile__ ("pushl %%ebp\n\t"
83 "pushl %%ebx\n\t"
84 "pushl %%esi\n\t"
85 "pushl %%edi\n\t"
86 "movl %2,%%ebp\n\t"
87 "call *%0\n\t"
88 "popl %%edi\n\t"
89 "popl %%esi\n\t"
90 "popl %%ebx\n\t"
91 "popl %%ebp"
92 : "=a" (ret)
93 : "0" (func), "r" (ebp)
94 : "ecx", "edx", "memory" );
95 return ret;
98 #endif
100 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
101 EXCEPTION_REGISTRATION_RECORD* frame,
102 PCONTEXT context,
103 EXCEPTION_REGISTRATION_RECORD** dispatch)
105 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
106 return ExceptionContinueSearch;
107 *dispatch = frame;
108 return ExceptionCollidedUnwind;
112 /*********************************************************************
113 * _EH_prolog (MSVCRT.@)
115 #ifdef __i386__
116 /* Provided for VC++ binary compatibility only */
117 __ASM_GLOBAL_FUNC(_EH_prolog,
118 "pushl $-1\n\t"
119 "pushl %eax\n\t"
120 "pushl %fs:0\n\t"
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"
125 "pushl %eax\n\t"
126 "ret")
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(&reg);
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(&reg);
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,
178 PCONTEXT context,
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;
212 else
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;
254 #else
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);
258 #endif
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");
269 return 0;
273 * setjmp/longjmp implementation
276 #ifdef __i386__
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 /*******************************************************************
314 * _setjmp (MSVCRT.@)
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;
322 else
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 );
327 return 0;
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;
337 jmp->UnwindFunc = 0;
338 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
339 if (jmp->Registration == ~0UL)
341 jmp->TryLevel = TRYLEVEL_END;
343 else
345 int i;
346 va_list args;
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 );
354 va_end( args );
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 );
359 return 0;
362 /*********************************************************************
363 * longjmp (MSVCRT.@)
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;
386 unwind_func(jmp);
388 else
389 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
390 jmp->TryLevel, (void *)jmp->Ebp);
393 if (!retval)
394 retval = 1;
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 );
406 #endif /* i386 */
408 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
410 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
412 BOOL ret = FALSE;
414 switch (ctrlType)
416 case CTRL_C_EVENT:
417 if (sighandlers[MSVCRT_SIGINT])
419 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
420 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
421 ret = TRUE;
423 break;
425 return ret;
428 typedef void (*float_handler)(int, int);
430 /* The exception codes are actually NTSTATUS values */
431 static const struct
433 NTSTATUS status;
434 int signal;
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;
465 break;
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;
491 break;
494 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
496 ret = EXCEPTION_CONTINUE_EXECUTION;
498 break;
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;
510 break;
512 return ret;
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 /*********************************************************************
528 * signal (MSVCRT.@)
529 * Some signals may never be generated except through an explicit call to
530 * raise.
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;
540 switch (sig)
542 /* Cases handled internally. Note SIGTERM is never generated by Windows,
543 * so we effectively mask it.
545 case MSVCRT_SIGABRT:
546 case MSVCRT_SIGFPE:
547 case MSVCRT_SIGILL:
548 case MSVCRT_SIGSEGV:
549 case MSVCRT_SIGINT:
550 case MSVCRT_SIGTERM:
551 ret = sighandlers[sig];
552 sighandlers[sig] = func;
553 break;
554 default:
555 ret = MSVCRT_SIG_ERR;
557 return ret;
560 /*********************************************************************
561 * raise (MSVCRT.@)
563 int CDECL MSVCRT_raise(int sig)
565 MSVCRT___sighandler_t handler;
567 TRACE("(%d)\n", sig);
569 switch (sig)
571 case MSVCRT_SIGABRT:
572 case MSVCRT_SIGFPE:
573 case MSVCRT_SIGILL:
574 case MSVCRT_SIGSEGV:
575 case MSVCRT_SIGINT:
576 case MSVCRT_SIGTERM:
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);
584 else
585 handler(sig);
587 break;
588 default:
589 return -1;
591 return 0;
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);