rpcrt4: Use the thread pool in the RPC server for processing packets.
[wine/multimedia.git] / dlls / msvcrt / except.c
blobd8691a847c5c63ba7eea53aad9e64a9279fb541a
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 * NOTES:
23 * See http://www.microsoft.com/msj/0197/exception/exception.htm,
24 * but don't believe all of it.
26 * FIXME: Incomplete support for nested exceptions/try block cleanup.
29 #include "config.h"
30 #include "wine/port.h"
32 #include <stdarg.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winreg.h"
37 #include "winternl.h"
38 #include "wine/exception.h"
39 #include "msvcrt.h"
40 #include "excpt.h"
41 #include "wincon.h"
42 #include "msvcrt/float.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(seh);
47 /* VC++ extensions to Win32 SEH */
48 typedef struct _SCOPETABLE
50 int previousTryLevel;
51 int (*lpfnFilter)(PEXCEPTION_POINTERS);
52 int (*lpfnHandler)(void);
53 } SCOPETABLE, *PSCOPETABLE;
55 typedef struct _MSVCRT_EXCEPTION_FRAME
57 EXCEPTION_REGISTRATION_RECORD *prev;
58 void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
59 PCONTEXT, PEXCEPTION_RECORD);
60 PSCOPETABLE scopetable;
61 int trylevel;
62 int _ebp;
63 PEXCEPTION_POINTERS xpointers;
64 } MSVCRT_EXCEPTION_FRAME;
66 #define TRYLEVEL_END (-1) /* End of trylevel list */
68 #if defined(__GNUC__) && defined(__i386__)
69 inline static void call_finally_block( void *code_block, void *base_ptr )
71 __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax" \
72 : : "a" (code_block), "g" (base_ptr));
75 inline static DWORD call_filter( void *func, void *arg, void *ebp )
77 DWORD ret;
78 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
79 : "=a" (ret)
80 : "0" (func), "r" (ebp), "r" (arg)
81 : "ecx", "edx", "memory" );
82 return ret;
84 #endif
86 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
87 EXCEPTION_REGISTRATION_RECORD* frame,
88 PCONTEXT context,
89 EXCEPTION_REGISTRATION_RECORD** dispatch)
91 if (rec->ExceptionFlags & 0x6)
92 return ExceptionContinueSearch;
93 *dispatch = frame;
94 return ExceptionCollidedUnwind;
98 /*********************************************************************
99 * _EH_prolog (MSVCRT.@)
101 #ifdef __i386__
102 /* Provided for VC++ binary compatibility only */
103 __ASM_GLOBAL_FUNC(_EH_prolog,
104 "pushl $-1\n\t"
105 "pushl %eax\n\t"
106 "pushl %fs:0\n\t"
107 "movl %esp, %fs:0\n\t"
108 "movl 12(%esp), %eax\n\t"
109 "movl %ebp, 12(%esp)\n\t"
110 "leal 12(%esp), %ebp\n\t"
111 "pushl %eax\n\t"
112 "ret");
113 #endif
115 /*******************************************************************
116 * _global_unwind2 (MSVCRT.@)
118 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
120 TRACE("(%p)\n",frame);
121 RtlUnwind( frame, 0, 0, 0 );
124 /*******************************************************************
125 * _local_unwind2 (MSVCRT.@)
127 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
129 MSVCRT_EXCEPTION_FRAME *curframe = frame;
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 = (PEXCEPTION_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 curtrylevel = frame->scopetable[frame->trylevel].previousTryLevel;
142 curframe = frame;
143 curframe->trylevel = curtrylevel;
144 if (!frame->scopetable[curtrylevel].lpfnFilter)
146 ERR("__try block cleanup not implemented - expect crash!\n");
147 /* FIXME: Remove current frame, set ebp, call
148 * frame->scopetable[curtrylevel].lpfnHandler()
152 __wine_pop_frame(&reg);
153 TRACE("unwound OK\n");
156 /*********************************************************************
157 * _except_handler2 (MSVCRT.@)
159 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
160 EXCEPTION_REGISTRATION_RECORD* frame,
161 PCONTEXT context,
162 EXCEPTION_REGISTRATION_RECORD** dispatcher)
164 FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
165 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
166 frame->Handler, context, dispatcher);
167 return ExceptionContinueSearch;
170 /*********************************************************************
171 * _except_handler3 (MSVCRT.@)
173 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
174 MSVCRT_EXCEPTION_FRAME* frame,
175 PCONTEXT context, void* dispatcher)
177 #if defined(__GNUC__) && defined(__i386__)
178 long retval;
179 int trylevel;
180 EXCEPTION_POINTERS exceptPtrs;
181 PSCOPETABLE pScopeTable;
183 TRACE("exception %lx flags=%lx at %p handler=%p %p %p semi-stub\n",
184 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
185 frame->handler, context, dispatcher);
187 __asm__ __volatile__ ("cld");
189 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
191 /* Unwinding the current frame */
192 _local_unwind2(frame, TRYLEVEL_END);
193 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
194 return ExceptionContinueSearch;
196 else
198 /* Hunting for handler */
199 exceptPtrs.ExceptionRecord = rec;
200 exceptPtrs.ContextRecord = context;
201 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
202 trylevel = frame->trylevel;
203 pScopeTable = frame->scopetable;
205 while (trylevel != TRYLEVEL_END)
207 if (pScopeTable[trylevel].lpfnFilter)
209 TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
211 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
213 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
214 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
215 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
217 if (retval == EXCEPTION_CONTINUE_EXECUTION)
218 return ExceptionContinueExecution;
220 if (retval == EXCEPTION_EXECUTE_HANDLER)
222 /* Unwind all higher frames, this one will handle the exception */
223 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
224 _local_unwind2(frame, trylevel);
226 /* Set our trylevel to the enclosing block, and call the __finally
227 * code, which won't return
229 frame->trylevel = pScopeTable->previousTryLevel;
230 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
231 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
232 ERR("Returned from __finally block - expect crash!\n");
235 trylevel = pScopeTable->previousTryLevel;
238 #else
239 TRACE("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
240 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
241 frame->handler, context, dispatcher);
242 #endif
243 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
244 return ExceptionContinueSearch;
247 /*********************************************************************
248 * _abnormal_termination (MSVCRT.@)
250 int CDECL _abnormal_termination(void)
252 FIXME("(void)stub\n");
253 return 0;
257 * setjmp/longjmp implementation
260 #ifdef __i386__
261 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
262 typedef void (*MSVCRT_unwind_function)(const void*);
264 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
265 /* and then jumps to the C backend function */
266 #define DEFINE_SETJMP_ENTRYPOINT(name) \
267 __ASM_GLOBAL_FUNC( name, \
268 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
269 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
270 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
271 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
272 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
273 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
274 "movl 0(%esp),%eax\n\t" \
275 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
276 "jmp " __ASM_NAME("__regs_") # name )
278 /* restore the registers from the jmp buf upon longjmp */
279 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
280 __ASM_GLOBAL_FUNC( longjmp_set_regs,
281 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
282 "movl 8(%esp),%eax\n\t" /* retval */
283 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
284 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
285 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
286 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
287 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
288 "addl $4,%esp\n\t" /* get rid of return address */
289 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ );
292 * The signatures of the setjmp/longjmp functions do not match that
293 * declared in the setjmp header so they don't follow the regular naming
294 * convention to avoid conflicts.
297 /*******************************************************************
298 * _setjmp (MSVCRT.@)
300 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp);
301 int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
303 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
304 if (jmp->Registration == TRYLEVEL_END)
305 jmp->TryLevel = TRYLEVEL_END;
306 else
307 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
309 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
310 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
311 return 0;
314 /*******************************************************************
315 * _setjmp3 (MSVCRT.@)
317 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 );
318 int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args)
320 jmp->Cookie = MSVCRT_JMP_MAGIC;
321 jmp->UnwindFunc = 0;
322 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
323 if (jmp->Registration == TRYLEVEL_END)
325 jmp->TryLevel = TRYLEVEL_END;
327 else
329 void **args = ((void**)jmp->Esp)+2;
331 if (nb_args > 0) jmp->UnwindFunc = (unsigned long)*args++;
332 if (nb_args > 1) jmp->TryLevel = (unsigned long)*args++;
333 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
334 if (nb_args > 2)
336 size_t size = (nb_args - 2) * sizeof(DWORD);
337 memcpy( jmp->UnwindData, args, min( size, sizeof(jmp->UnwindData) ));
341 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
342 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
343 return 0;
346 /*********************************************************************
347 * longjmp (MSVCRT.@)
349 int CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
351 unsigned long cur_frame = 0;
353 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
354 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
356 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
357 TRACE("cur_frame=%lx\n",cur_frame);
359 if (cur_frame != jmp->Registration)
360 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
362 if (jmp->Registration)
364 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
365 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
367 MSVCRT_unwind_function unwind_func;
369 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
370 unwind_func(jmp);
372 else
373 _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
374 jmp->TryLevel);
377 if (!retval)
378 retval = 1;
380 longjmp_set_regs( jmp, retval );
383 /*********************************************************************
384 * _seh_longjmp_unwind (MSVCRT.@)
386 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
388 _local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel );
390 #endif /* i386 */
392 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
394 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
396 BOOL ret = FALSE;
398 switch (ctrlType)
400 case CTRL_C_EVENT:
401 if (sighandlers[MSVCRT_SIGINT])
403 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
404 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
405 ret = TRUE;
407 break;
409 return ret;
412 typedef void (*float_handler)(int, int);
414 /* The exception codes are actually NTSTATUS values */
415 struct
417 NTSTATUS status;
418 int signal;
419 } float_exception_map[] = {
420 { EXCEPTION_FLT_DENORMAL_OPERAND, _FPE_DENORMAL },
421 { EXCEPTION_FLT_DIVIDE_BY_ZERO, _FPE_ZERODIVIDE },
422 { EXCEPTION_FLT_INEXACT_RESULT, _FPE_INEXACT },
423 { EXCEPTION_FLT_INVALID_OPERATION, _FPE_INVALID },
424 { EXCEPTION_FLT_OVERFLOW, _FPE_OVERFLOW },
425 { EXCEPTION_FLT_STACK_CHECK, _FPE_STACKOVERFLOW },
426 { EXCEPTION_FLT_UNDERFLOW, _FPE_UNDERFLOW },
429 static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
431 LONG ret = EXCEPTION_CONTINUE_SEARCH;
433 if (!except || !except->ExceptionRecord)
434 return EXCEPTION_CONTINUE_SEARCH;
436 switch (except->ExceptionRecord->ExceptionCode)
438 case EXCEPTION_ACCESS_VIOLATION:
439 if (sighandlers[MSVCRT_SIGSEGV])
441 if (sighandlers[MSVCRT_SIGSEGV] != MSVCRT_SIG_IGN)
442 sighandlers[MSVCRT_SIGSEGV](MSVCRT_SIGSEGV);
443 ret = EXCEPTION_CONTINUE_EXECUTION;
445 break;
446 /* According to
447 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
448 * the FPE signal handler takes as a second argument the type of
449 * floating point exception.
451 case EXCEPTION_FLT_DENORMAL_OPERAND:
452 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
453 case EXCEPTION_FLT_INEXACT_RESULT:
454 case EXCEPTION_FLT_INVALID_OPERATION:
455 case EXCEPTION_FLT_OVERFLOW:
456 case EXCEPTION_FLT_STACK_CHECK:
457 case EXCEPTION_FLT_UNDERFLOW:
458 if (sighandlers[MSVCRT_SIGFPE])
460 if (sighandlers[MSVCRT_SIGFPE] != MSVCRT_SIG_IGN)
462 int i, float_signal = _FPE_INVALID;
464 float_handler handler = (float_handler)sighandlers[MSVCRT_SIGFPE];
465 for (i = 0; i < sizeof(float_exception_map) /
466 sizeof(float_exception_map[0]); i++)
467 if (float_exception_map[i].status ==
468 except->ExceptionRecord->ExceptionCode)
470 float_signal = float_exception_map[i].signal;
471 break;
473 handler(MSVCRT_SIGFPE, float_signal);
475 ret = EXCEPTION_CONTINUE_EXECUTION;
477 break;
478 case EXCEPTION_ILLEGAL_INSTRUCTION:
479 if (sighandlers[MSVCRT_SIGILL])
481 if (sighandlers[MSVCRT_SIGILL] != MSVCRT_SIG_IGN)
482 sighandlers[MSVCRT_SIGILL](MSVCRT_SIGILL);
483 ret = EXCEPTION_CONTINUE_EXECUTION;
485 break;
487 return ret;
490 void msvcrt_init_signals(void)
492 SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
493 SetUnhandledExceptionFilter(msvcrt_exception_filter);
496 void msvcrt_free_signals(void)
498 SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
499 SetUnhandledExceptionFilter(NULL);
502 /*********************************************************************
503 * signal (MSVCRT.@)
504 * MS signal handling is described here:
505 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
506 * Some signals may never be generated except through an explicit call to
507 * raise.
509 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
511 MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
513 TRACE("(%d, %p)\n", sig, func);
515 if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
517 switch (sig)
519 /* Cases handled internally. Note SIGTERM is never generated by Windows,
520 * so we effectively mask it.
522 case MSVCRT_SIGABRT:
523 case MSVCRT_SIGFPE:
524 case MSVCRT_SIGILL:
525 case MSVCRT_SIGSEGV:
526 case MSVCRT_SIGINT:
527 case MSVCRT_SIGTERM:
528 ret = sighandlers[sig];
529 sighandlers[sig] = func;
530 break;
531 default:
532 ret = MSVCRT_SIG_ERR;
534 return ret;
537 /*********************************************************************
538 * _XcptFilter (MSVCRT.@)
540 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
542 TRACE("(%08lx,%p)\n", ex, ptr);
543 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
544 return msvcrt_exception_filter(ptr);