Set both dwVersionICM and dwVersion to ICVERSION in Wine builtin
[wine/wine-kai.git] / dlls / msvcrt / except.c
blob51b15f07efb43e79e2537c6e70a7367e541f3bb4
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "msvcrt/signal.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
48 /* VC++ extensions to Win32 SEH */
49 typedef struct _SCOPETABLE
51 int previousTryLevel;
52 int (*lpfnFilter)(PEXCEPTION_POINTERS);
53 int (*lpfnHandler)(void);
54 } SCOPETABLE, *PSCOPETABLE;
56 typedef struct _MSVCRT_EXCEPTION_FRAME
58 EXCEPTION_REGISTRATION_RECORD *prev;
59 void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
60 PCONTEXT, PEXCEPTION_RECORD);
61 PSCOPETABLE scopetable;
62 int trylevel;
63 int _ebp;
64 PEXCEPTION_POINTERS xpointers;
65 } MSVCRT_EXCEPTION_FRAME;
67 #define TRYLEVEL_END (-1) /* End of trylevel list */
69 #if defined(__GNUC__) && defined(__i386__)
70 inline static void call_finally_block( void *code_block, void *base_ptr )
72 __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax" \
73 : : "a" (code_block), "g" (base_ptr));
76 inline static DWORD call_filter( void *func, void *arg, void *ebp )
78 DWORD ret;
79 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
80 : "=a" (ret)
81 : "0" (func), "r" (ebp), "r" (arg)
82 : "ecx", "edx", "memory" );
83 return ret;
85 #endif
87 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
88 EXCEPTION_REGISTRATION_RECORD* frame,
89 PCONTEXT context,
90 EXCEPTION_REGISTRATION_RECORD** dispatch)
92 if (rec->ExceptionFlags & 0x6)
93 return ExceptionContinueSearch;
94 *dispatch = frame;
95 return ExceptionCollidedUnwind;
99 /*********************************************************************
100 * _EH_prolog (MSVCRT.@)
102 #ifdef __i386__
103 /* Provided for VC++ binary compatibility only */
104 __ASM_GLOBAL_FUNC(_EH_prolog,
105 "pushl $-1\n\t"
106 "pushl %eax\n\t"
107 "pushl %fs:0\n\t"
108 "movl %esp, %fs:0\n\t"
109 "movl 12(%esp), %eax\n\t"
110 "movl %ebp, 12(%esp)\n\t"
111 "leal 12(%esp), %ebp\n\t"
112 "pushl %eax\n\t"
113 "ret");
114 #endif
116 /*******************************************************************
117 * _global_unwind2 (MSVCRT.@)
119 void _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
121 TRACE("(%p)\n",frame);
122 RtlUnwind( frame, 0, 0, 0 );
125 /*******************************************************************
126 * _local_unwind2 (MSVCRT.@)
128 void _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
130 MSVCRT_EXCEPTION_FRAME *curframe = frame;
131 EXCEPTION_REGISTRATION_RECORD reg;
133 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
135 /* Register a handler in case of a nested exception */
136 reg.Handler = (PEXCEPTION_HANDLER)MSVCRT_nested_handler;
137 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
138 __wine_push_frame(&reg);
140 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
142 int curtrylevel = frame->scopetable[frame->trylevel].previousTryLevel;
143 curframe = frame;
144 curframe->trylevel = curtrylevel;
145 if (!frame->scopetable[curtrylevel].lpfnFilter)
147 ERR("__try block cleanup not implemented - expect crash!\n");
148 /* FIXME: Remove current frame, set ebp, call
149 * frame->scopetable[curtrylevel].lpfnHandler()
153 __wine_pop_frame(&reg);
154 TRACE("unwound OK\n");
157 /*********************************************************************
158 * _except_handler2 (MSVCRT.@)
160 int _except_handler2(PEXCEPTION_RECORD rec,
161 EXCEPTION_REGISTRATION_RECORD* frame,
162 PCONTEXT context,
163 EXCEPTION_REGISTRATION_RECORD** dispatcher)
165 FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
166 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
167 frame->Handler, context, dispatcher);
168 return ExceptionContinueSearch;
171 /*********************************************************************
172 * _except_handler3 (MSVCRT.@)
174 int _except_handler3(PEXCEPTION_RECORD rec,
175 MSVCRT_EXCEPTION_FRAME* frame,
176 PCONTEXT context, void* dispatcher)
178 #if defined(__GNUC__) && defined(__i386__)
179 long retval;
180 int trylevel;
181 EXCEPTION_POINTERS exceptPtrs;
182 PSCOPETABLE pScopeTable;
184 TRACE("exception %lx flags=%lx at %p handler=%p %p %p semi-stub\n",
185 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
186 frame->handler, context, dispatcher);
188 __asm__ __volatile__ ("cld");
190 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
192 /* Unwinding the current frame */
193 _local_unwind2(frame, TRYLEVEL_END);
194 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
195 return ExceptionContinueSearch;
197 else
199 /* Hunting for handler */
200 exceptPtrs.ExceptionRecord = rec;
201 exceptPtrs.ContextRecord = context;
202 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
203 trylevel = frame->trylevel;
204 pScopeTable = frame->scopetable;
206 while (trylevel != TRYLEVEL_END)
208 if (pScopeTable[trylevel].lpfnFilter)
210 TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
212 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
214 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
215 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
216 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
218 if (retval == EXCEPTION_CONTINUE_EXECUTION)
219 return ExceptionContinueExecution;
221 if (retval == EXCEPTION_EXECUTE_HANDLER)
223 /* Unwind all higher frames, this one will handle the exception */
224 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
225 _local_unwind2(frame, trylevel);
227 /* Set our trylevel to the enclosing block, and call the __finally
228 * code, which won't return
230 frame->trylevel = pScopeTable->previousTryLevel;
231 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
232 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
233 ERR("Returned from __finally block - expect crash!\n");
236 trylevel = pScopeTable->previousTryLevel;
239 #else
240 TRACE("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
241 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
242 frame->handler, context, dispatcher);
243 #endif
244 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
245 return ExceptionContinueSearch;
248 /*********************************************************************
249 * _abnormal_termination (MSVCRT.@)
251 int _abnormal_termination(void)
253 FIXME("(void)stub\n");
254 return 0;
258 * setjmp/longjmp implementation
261 #ifdef __i386__
262 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
263 typedef void (*MSVCRT_unwind_function)(const void*);
266 * The signatures of the setjmp/longjmp functions do not match that
267 * declared in the setjmp header so they don't follow the regular naming
268 * convention to avoid conflicts.
271 /*******************************************************************
272 * _setjmp (MSVCRT.@)
274 DEFINE_REGS_ENTRYPOINT( MSVCRT__setjmp, 4, 0 );
275 void WINAPI __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp, CONTEXT86* context)
277 TRACE("(%p)\n",jmp);
278 jmp->Ebp = context->Ebp;
279 jmp->Ebx = context->Ebx;
280 jmp->Edi = context->Edi;
281 jmp->Esi = context->Esi;
282 jmp->Esp = context->Esp;
283 jmp->Eip = context->Eip;
284 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
285 if (jmp->Registration == TRYLEVEL_END)
286 jmp->TryLevel = TRYLEVEL_END;
287 else
288 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
289 TRACE("returning 0\n");
290 context->Eax=0;
293 /*******************************************************************
294 * _setjmp3 (MSVCRT.@)
296 DEFINE_REGS_ENTRYPOINT( MSVCRT__setjmp3, 8, 0 );
297 void WINAPI __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, CONTEXT86* context)
299 TRACE("(%p,%d)\n",jmp,nb_args);
300 jmp->Ebp = context->Ebp;
301 jmp->Ebx = context->Ebx;
302 jmp->Edi = context->Edi;
303 jmp->Esi = context->Esi;
304 jmp->Esp = context->Esp;
305 jmp->Eip = context->Eip;
306 jmp->Cookie = MSVCRT_JMP_MAGIC;
307 jmp->UnwindFunc = 0;
308 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
309 if (jmp->Registration == TRYLEVEL_END)
311 jmp->TryLevel = TRYLEVEL_END;
313 else
315 void **args = ((void**)context->Esp)+2;
317 if (nb_args > 0) jmp->UnwindFunc = (unsigned long)*args++;
318 if (nb_args > 1) jmp->TryLevel = (unsigned long)*args++;
319 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
320 if (nb_args > 2)
322 size_t size = (nb_args - 2) * sizeof(DWORD);
323 memcpy( jmp->UnwindData, args, min( size, sizeof(jmp->UnwindData) ));
326 TRACE("returning 0\n");
327 context->Eax = 0;
330 /*********************************************************************
331 * longjmp (MSVCRT.@)
333 DEFINE_REGS_ENTRYPOINT( MSVCRT_longjmp, 8, 0 );
334 void WINAPI __regs_MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval, CONTEXT86* context)
336 unsigned long cur_frame = 0;
338 TRACE("(%p,%d)\n", jmp, retval);
340 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
341 TRACE("cur_frame=%lx\n",cur_frame);
343 if (cur_frame != jmp->Registration)
344 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
346 if (jmp->Registration)
348 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
349 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
351 MSVCRT_unwind_function unwind_func;
353 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
354 unwind_func(jmp);
356 else
357 _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
358 jmp->TryLevel);
361 if (!retval)
362 retval = 1;
364 TRACE("Jump to %lx returning %d\n",jmp->Eip,retval);
365 context->Ebp = jmp->Ebp;
366 context->Ebx = jmp->Ebx;
367 context->Edi = jmp->Edi;
368 context->Esi = jmp->Esi;
369 context->Esp = jmp->Esp;
370 context->Eip = jmp->Eip;
371 context->Eax = retval;
374 /*********************************************************************
375 * _seh_longjmp_unwind (MSVCRT.@)
377 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
379 _local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel );
381 #endif /* i386 */
383 static __sighandler_t sighandlers[NSIG] = { SIG_DFL };
385 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
387 BOOL ret = FALSE;
389 switch (ctrlType)
391 case CTRL_C_EVENT:
392 if (sighandlers[SIGINT])
394 if (sighandlers[SIGINT] != SIG_IGN)
395 sighandlers[SIGINT](SIGINT);
396 ret = TRUE;
398 break;
400 return ret;
403 typedef void (*float_handler)(int, int);
405 /* The exception codes are actually NTSTATUS values */
406 struct
408 NTSTATUS status;
409 int signal;
410 } float_exception_map[] = {
411 { EXCEPTION_FLT_DENORMAL_OPERAND, _FPE_DENORMAL },
412 { EXCEPTION_FLT_DIVIDE_BY_ZERO, _FPE_ZERODIVIDE },
413 { EXCEPTION_FLT_INEXACT_RESULT, _FPE_INEXACT },
414 { EXCEPTION_FLT_INVALID_OPERATION, _FPE_INVALID },
415 { EXCEPTION_FLT_OVERFLOW, _FPE_OVERFLOW },
416 { EXCEPTION_FLT_STACK_CHECK, _FPE_STACKOVERFLOW },
417 { EXCEPTION_FLT_UNDERFLOW, _FPE_UNDERFLOW },
420 static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
422 LONG ret = EXCEPTION_CONTINUE_SEARCH;
424 if (!except || !except->ExceptionRecord)
425 return EXCEPTION_CONTINUE_SEARCH;
427 switch (except->ExceptionRecord->ExceptionCode)
429 case EXCEPTION_ACCESS_VIOLATION:
430 if (sighandlers[SIGSEGV])
432 if (sighandlers[SIGSEGV] != SIG_IGN)
433 sighandlers[SIGSEGV](SIGSEGV);
434 ret = EXCEPTION_CONTINUE_EXECUTION;
436 break;
437 /* According to
438 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
439 * the FPE signal handler takes as a second argument the type of
440 * floating point exception.
442 case EXCEPTION_FLT_DENORMAL_OPERAND:
443 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
444 case EXCEPTION_FLT_INEXACT_RESULT:
445 case EXCEPTION_FLT_INVALID_OPERATION:
446 case EXCEPTION_FLT_OVERFLOW:
447 case EXCEPTION_FLT_STACK_CHECK:
448 case EXCEPTION_FLT_UNDERFLOW:
449 if (sighandlers[SIGFPE])
451 if (sighandlers[SIGFPE] != SIG_IGN)
453 int i, float_signal = _FPE_INVALID;
455 float_handler handler = (float_handler)sighandlers[SIGFPE];
456 for (i = 0; i < sizeof(float_exception_map) /
457 sizeof(float_exception_map[0]); i++)
458 if (float_exception_map[i].status ==
459 except->ExceptionRecord->ExceptionCode)
461 float_signal = float_exception_map[i].signal;
462 break;
464 handler(SIGFPE, float_signal);
466 ret = EXCEPTION_CONTINUE_EXECUTION;
468 break;
469 case EXCEPTION_ILLEGAL_INSTRUCTION:
470 if (sighandlers[SIGILL])
472 if (sighandlers[SIGILL] != SIG_IGN)
473 sighandlers[SIGILL](SIGILL);
474 ret = EXCEPTION_CONTINUE_EXECUTION;
476 break;
478 return ret;
481 void msvcrt_init_signals(void)
483 SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
484 SetUnhandledExceptionFilter(msvcrt_exception_filter);
487 void msvcrt_free_signals(void)
489 SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
490 SetUnhandledExceptionFilter(NULL);
493 /*********************************************************************
494 * signal (MSVCRT.@)
495 * MS signal handling is described here:
496 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
497 * Some signals may never be generated except through an explicit call to
498 * raise.
500 __sighandler_t MSVCRT_signal(int sig, __sighandler_t func)
502 __sighandler_t ret = SIG_ERR;
504 TRACE("(%d, %p)\n", sig, func);
506 if (func == SIG_ERR) return SIG_ERR;
508 switch (sig)
510 /* Cases handled internally. Note SIGTERM is never generated by Windows,
511 * so we effectively mask it.
513 case SIGABRT:
514 case SIGFPE:
515 case SIGILL:
516 case SIGSEGV:
517 case SIGINT:
518 case SIGTERM:
519 ret = sighandlers[sig];
520 sighandlers[sig] = func;
521 break;
522 default:
523 ret = SIG_ERR;
525 return ret;
528 /*********************************************************************
529 * _XcptFilter (MSVCRT.@)
531 int _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
533 TRACE("(%ld,%p)\n", ex, ptr);
534 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
535 return msvcrt_exception_filter(ptr);