advapi32/tests: No need to check return value of GetModuleHandle.
[wine/testsucceed.git] / dlls / msvcrt / except.c
blobf204e8b8a9322403a407d5cda14bb9c7174fe345
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 & (EH_UNWINDING | EH_EXIT_UNWIND)))
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 EXCEPTION_REGISTRATION_RECORD reg;
131 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
133 /* Register a handler in case of a nested exception */
134 reg.Handler = MSVCRT_nested_handler;
135 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
136 __wine_push_frame(&reg);
138 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
140 int level = frame->trylevel;
141 frame->trylevel = frame->scopetable[level].previousTryLevel;
142 if (!frame->scopetable[level].lpfnFilter)
144 FIXME( "__try block cleanup level %d handler %p not fully implemented\n",
145 level, frame->scopetable[level].lpfnHandler );
146 /* FIXME: should probably set ebp to frame->_ebp */
147 frame->scopetable[level].lpfnHandler();
150 __wine_pop_frame(&reg);
151 TRACE("unwound OK\n");
154 /*********************************************************************
155 * _except_handler2 (MSVCRT.@)
157 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
158 EXCEPTION_REGISTRATION_RECORD* frame,
159 PCONTEXT context,
160 EXCEPTION_REGISTRATION_RECORD** dispatcher)
162 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
163 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
164 frame->Handler, context, dispatcher);
165 return ExceptionContinueSearch;
168 /*********************************************************************
169 * _except_handler3 (MSVCRT.@)
171 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
172 MSVCRT_EXCEPTION_FRAME* frame,
173 PCONTEXT context, void* dispatcher)
175 #if defined(__GNUC__) && defined(__i386__)
176 long retval;
177 int trylevel;
178 EXCEPTION_POINTERS exceptPtrs;
179 PSCOPETABLE pScopeTable;
181 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
182 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
183 frame->handler, context, dispatcher);
185 __asm__ __volatile__ ("cld");
187 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
189 /* Unwinding the current frame */
190 _local_unwind2(frame, TRYLEVEL_END);
191 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
192 return ExceptionContinueSearch;
194 else
196 /* Hunting for handler */
197 exceptPtrs.ExceptionRecord = rec;
198 exceptPtrs.ContextRecord = context;
199 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
200 trylevel = frame->trylevel;
201 pScopeTable = frame->scopetable;
203 while (trylevel != TRYLEVEL_END)
205 if (pScopeTable[trylevel].lpfnFilter)
207 TRACE("filter = %p\n", pScopeTable[trylevel].lpfnFilter);
209 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
211 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
212 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
213 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
215 if (retval == EXCEPTION_CONTINUE_EXECUTION)
216 return ExceptionContinueExecution;
218 if (retval == EXCEPTION_EXECUTE_HANDLER)
220 /* Unwind all higher frames, this one will handle the exception */
221 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
222 _local_unwind2(frame, trylevel);
224 /* Set our trylevel to the enclosing block, and call the __finally
225 * code, which won't return
227 frame->trylevel = pScopeTable->previousTryLevel;
228 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
229 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
230 ERR("Returned from __finally block - expect crash!\n");
233 trylevel = pScopeTable->previousTryLevel;
236 #else
237 TRACE("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
238 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
239 frame->handler, context, dispatcher);
240 #endif
241 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
242 return ExceptionContinueSearch;
245 /*********************************************************************
246 * _abnormal_termination (MSVCRT.@)
248 int CDECL _abnormal_termination(void)
250 FIXME("(void)stub\n");
251 return 0;
255 * setjmp/longjmp implementation
258 #ifdef __i386__
259 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
260 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
262 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
263 /* and then jumps to the C backend function */
264 #define DEFINE_SETJMP_ENTRYPOINT(name) \
265 __ASM_GLOBAL_FUNC( name, \
266 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
267 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
268 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
269 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
270 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
271 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
272 "movl 0(%esp),%eax\n\t" \
273 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
274 "jmp " __ASM_NAME("__regs_") # name )
276 /* restore the registers from the jmp buf upon longjmp */
277 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
278 __ASM_GLOBAL_FUNC( longjmp_set_regs,
279 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
280 "movl 8(%esp),%eax\n\t" /* retval */
281 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
282 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
283 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
284 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
285 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
286 "addl $4,%esp\n\t" /* get rid of return address */
287 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
290 * The signatures of the setjmp/longjmp functions do not match that
291 * declared in the setjmp header so they don't follow the regular naming
292 * convention to avoid conflicts.
295 /*******************************************************************
296 * _setjmp (MSVCRT.@)
298 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
299 int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
301 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
302 if (jmp->Registration == TRYLEVEL_END)
303 jmp->TryLevel = TRYLEVEL_END;
304 else
305 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
307 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
308 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
309 return 0;
312 /*******************************************************************
313 * _setjmp3 (MSVCRT.@)
315 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
316 int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
318 jmp->Cookie = MSVCRT_JMP_MAGIC;
319 jmp->UnwindFunc = 0;
320 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
321 if (jmp->Registration == TRYLEVEL_END)
323 jmp->TryLevel = TRYLEVEL_END;
325 else
327 int i;
328 va_list args;
330 va_start( args, nb_args );
331 if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
332 if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
333 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
334 for (i = 0; i < 6 && i < nb_args - 2; i++)
335 jmp->UnwindData[i] = va_arg( args, unsigned long );
336 va_end( args );
339 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
340 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
341 return 0;
344 /*********************************************************************
345 * longjmp (MSVCRT.@)
347 int CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
349 unsigned long cur_frame = 0;
351 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
352 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
354 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
355 TRACE("cur_frame=%lx\n",cur_frame);
357 if (cur_frame != jmp->Registration)
358 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
360 if (jmp->Registration)
362 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
363 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
365 MSVCRT_unwind_function unwind_func;
367 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
368 unwind_func(jmp);
370 else
371 _local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
372 jmp->TryLevel);
375 if (!retval)
376 retval = 1;
378 longjmp_set_regs( jmp, retval );
381 /*********************************************************************
382 * _seh_longjmp_unwind (MSVCRT.@)
384 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
386 _local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel );
388 #endif /* i386 */
390 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
392 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
394 BOOL ret = FALSE;
396 switch (ctrlType)
398 case CTRL_C_EVENT:
399 if (sighandlers[MSVCRT_SIGINT])
401 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
402 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
403 ret = TRUE;
405 break;
407 return ret;
410 typedef void (*float_handler)(int, int);
412 /* The exception codes are actually NTSTATUS values */
413 static const struct
415 NTSTATUS status;
416 int signal;
417 } float_exception_map[] = {
418 { EXCEPTION_FLT_DENORMAL_OPERAND, _FPE_DENORMAL },
419 { EXCEPTION_FLT_DIVIDE_BY_ZERO, _FPE_ZERODIVIDE },
420 { EXCEPTION_FLT_INEXACT_RESULT, _FPE_INEXACT },
421 { EXCEPTION_FLT_INVALID_OPERATION, _FPE_INVALID },
422 { EXCEPTION_FLT_OVERFLOW, _FPE_OVERFLOW },
423 { EXCEPTION_FLT_STACK_CHECK, _FPE_STACKOVERFLOW },
424 { EXCEPTION_FLT_UNDERFLOW, _FPE_UNDERFLOW },
427 static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
429 LONG ret = EXCEPTION_CONTINUE_SEARCH;
430 MSVCRT___sighandler_t handler;
432 if (!except || !except->ExceptionRecord)
433 return EXCEPTION_CONTINUE_SEARCH;
435 switch (except->ExceptionRecord->ExceptionCode)
437 case EXCEPTION_ACCESS_VIOLATION:
438 if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
440 if (handler != MSVCRT_SIG_IGN)
442 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
443 handler(MSVCRT_SIGSEGV);
445 ret = EXCEPTION_CONTINUE_EXECUTION;
447 break;
448 /* According to
449 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
450 * the FPE signal handler takes as a second argument the type of
451 * floating point exception.
453 case EXCEPTION_FLT_DENORMAL_OPERAND:
454 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
455 case EXCEPTION_FLT_INEXACT_RESULT:
456 case EXCEPTION_FLT_INVALID_OPERATION:
457 case EXCEPTION_FLT_OVERFLOW:
458 case EXCEPTION_FLT_STACK_CHECK:
459 case EXCEPTION_FLT_UNDERFLOW:
460 if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
462 if (handler != MSVCRT_SIG_IGN)
464 int i, float_signal = _FPE_INVALID;
466 sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
467 for (i = 0; i < sizeof(float_exception_map) /
468 sizeof(float_exception_map[0]); i++)
470 if (float_exception_map[i].status ==
471 except->ExceptionRecord->ExceptionCode)
473 float_signal = float_exception_map[i].signal;
474 break;
477 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
479 ret = EXCEPTION_CONTINUE_EXECUTION;
481 break;
482 case EXCEPTION_ILLEGAL_INSTRUCTION:
483 if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
485 if (handler != MSVCRT_SIG_IGN)
487 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
488 handler(MSVCRT_SIGILL);
490 ret = EXCEPTION_CONTINUE_EXECUTION;
492 break;
494 return ret;
497 void msvcrt_init_signals(void)
499 SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
500 SetUnhandledExceptionFilter(msvcrt_exception_filter);
503 void msvcrt_free_signals(void)
505 SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
506 SetUnhandledExceptionFilter(NULL);
509 /*********************************************************************
510 * signal (MSVCRT.@)
511 * MS signal handling is described here:
512 * http://msdn.microsoft.com/library/en-us/vclib/html/_CRT_signal.asp
513 * Some signals may never be generated except through an explicit call to
514 * raise.
516 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
518 MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
520 TRACE("(%d, %p)\n", sig, func);
522 if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
524 switch (sig)
526 /* Cases handled internally. Note SIGTERM is never generated by Windows,
527 * so we effectively mask it.
529 case MSVCRT_SIGABRT:
530 case MSVCRT_SIGFPE:
531 case MSVCRT_SIGILL:
532 case MSVCRT_SIGSEGV:
533 case MSVCRT_SIGINT:
534 case MSVCRT_SIGTERM:
535 ret = sighandlers[sig];
536 sighandlers[sig] = func;
537 break;
538 default:
539 ret = MSVCRT_SIG_ERR;
541 return ret;
544 /*********************************************************************
545 * raise (MSVCRT.@)
547 int CDECL MSVCRT_raise(int sig)
549 MSVCRT___sighandler_t handler;
551 TRACE("(%d)\n", sig);
553 switch (sig)
555 case MSVCRT_SIGABRT:
556 case MSVCRT_SIGFPE:
557 case MSVCRT_SIGILL:
558 case MSVCRT_SIGSEGV:
559 case MSVCRT_SIGINT:
560 case MSVCRT_SIGTERM:
561 handler = sighandlers[sig];
562 if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
563 if (handler != MSVCRT_SIG_IGN)
565 sighandlers[sig] = MSVCRT_SIG_DFL;
566 if (sig == MSVCRT_SIGFPE)
567 ((float_handler)handler)(sig, _FPE_EXPLICITGEN);
568 else
569 handler(sig);
571 break;
572 default:
573 return -1;
575 return 0;
578 /*********************************************************************
579 * _XcptFilter (MSVCRT.@)
581 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
583 TRACE("(%08x,%p)\n", ex, ptr);
584 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
585 return msvcrt_exception_filter(ptr);