msvcrt: Correctly walk up the list of trylevels in except_handler3.
[wine] / dlls / msvcrt / except.c
1 /*
2  * msvcrt.dll exception handling
3  *
4  * Copyright 2000 Jon Griffiths
5  * Copyright 2005 Juan Lang
6  *
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.
11  *
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.
16  *
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
20  *
21  * FIXME: Incomplete support for nested exceptions/try block cleanup.
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdarg.h>
28
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"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(seh);
39
40 /* VC++ extensions to Win32 SEH */
41 typedef struct _SCOPETABLE
42 {
43   int previousTryLevel;
44   int (*lpfnFilter)(PEXCEPTION_POINTERS);
45   int (*lpfnHandler)(void);
46 } SCOPETABLE, *PSCOPETABLE;
47
48 typedef struct _MSVCRT_EXCEPTION_FRAME
49 {
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;
58
59 #define TRYLEVEL_END (-1) /* End of trylevel list */
60
61 #if defined(__GNUC__) && defined(__i386__)
62 static inline void call_finally_block( void *code_block, void *base_ptr )
63 {
64     __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax"
65                           : : "a" (code_block), "g" (base_ptr));
66 }
67
68 static inline int call_filter( int (*func)(PEXCEPTION_POINTERS), void *arg, void *ebp )
69 {
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;
76 }
77
78 static inline int call_unwind_func( int (*func)(void), void *ebp )
79 {
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;
95 }
96
97 #endif
98
99 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
100                                    EXCEPTION_REGISTRATION_RECORD* frame,
101                                    PCONTEXT context,
102                                    EXCEPTION_REGISTRATION_RECORD** dispatch)
103 {
104   if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
105     return ExceptionContinueSearch;
106   *dispatch = frame;
107   return ExceptionCollidedUnwind;
108 }
109
110
111 /*********************************************************************
112  *              _EH_prolog (MSVCRT.@)
113  */
114 #ifdef __i386__
115 /* Provided for VC++ binary compatibility only */
116 __ASM_GLOBAL_FUNC(_EH_prolog,
117                   "pushl $-1\n\t"
118                   "pushl %eax\n\t"
119                   "pushl %fs:0\n\t"
120                   "movl  %esp, %fs:0\n\t"
121                   "movl  12(%esp), %eax\n\t"
122                   "movl  %ebp, 12(%esp)\n\t"
123                   "leal  12(%esp), %ebp\n\t"
124                   "pushl %eax\n\t"
125                   "ret")
126
127 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp)
128 {
129   EXCEPTION_REGISTRATION_RECORD reg;
130
131   TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
132
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);
137
138   while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
139   {
140       int level = frame->trylevel;
141       frame->trylevel = frame->scopetable[level].previousTryLevel;
142       if (!frame->scopetable[level].lpfnFilter)
143       {
144           TRACE( "__try block cleanup level %d handler %p ebp %p\n",
145                  level, frame->scopetable[level].lpfnHandler, ebp );
146           call_unwind_func( frame->scopetable[level].lpfnHandler, ebp );
147       }
148   }
149   __wine_pop_frame(&reg);
150   TRACE("unwound OK\n");
151 }
152
153 /*******************************************************************
154  *              _local_unwind2 (MSVCRT.@)
155  */
156 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
157 {
158     msvcrt_local_unwind2( frame, trylevel, &frame->_ebp );
159 }
160
161 #endif  /* __i386__ */
162
163 /*******************************************************************
164  *              _global_unwind2 (MSVCRT.@)
165  */
166 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
167 {
168     TRACE("(%p)\n",frame);
169     RtlUnwind( frame, 0, 0, 0 );
170 }
171
172 /*********************************************************************
173  *              _except_handler2 (MSVCRT.@)
174  */
175 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
176                            EXCEPTION_REGISTRATION_RECORD* frame,
177                            PCONTEXT context,
178                            EXCEPTION_REGISTRATION_RECORD** dispatcher)
179 {
180   FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
181         rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
182         frame->Handler, context, dispatcher);
183   return ExceptionContinueSearch;
184 }
185
186 /*********************************************************************
187  *              _except_handler3 (MSVCRT.@)
188  */
189 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
190                            MSVCRT_EXCEPTION_FRAME* frame,
191                            PCONTEXT context, void* dispatcher)
192 {
193 #if defined(__GNUC__) && defined(__i386__)
194   int retval, trylevel;
195   EXCEPTION_POINTERS exceptPtrs;
196   PSCOPETABLE pScopeTable;
197
198   TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
199         rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
200         frame->handler, context, dispatcher);
201
202   __asm__ __volatile__ ("cld");
203
204   if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
205   {
206     /* Unwinding the current frame */
207     msvcrt_local_unwind2(frame, TRYLEVEL_END, &frame->_ebp);
208     TRACE("unwound current frame, returning ExceptionContinueSearch\n");
209     return ExceptionContinueSearch;
210   }
211   else
212   {
213     /* Hunting for handler */
214     exceptPtrs.ExceptionRecord = rec;
215     exceptPtrs.ContextRecord = context;
216     *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
217     trylevel = frame->trylevel;
218     pScopeTable = frame->scopetable;
219
220     while (trylevel != TRYLEVEL_END)
221     {
222       TRACE( "level %d prev %d filter %p\n", trylevel, pScopeTable[trylevel].previousTryLevel,
223              pScopeTable[trylevel].lpfnFilter );
224       if (pScopeTable[trylevel].lpfnFilter)
225       {
226         retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
227
228         TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
229               "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
230               "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
231
232         if (retval == EXCEPTION_CONTINUE_EXECUTION)
233           return ExceptionContinueExecution;
234
235         if (retval == EXCEPTION_EXECUTE_HANDLER)
236         {
237           /* Unwind all higher frames, this one will handle the exception */
238           _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
239           msvcrt_local_unwind2(frame, trylevel, &frame->_ebp);
240
241           /* Set our trylevel to the enclosing block, and call the __finally
242            * code, which won't return
243            */
244           frame->trylevel = pScopeTable[trylevel].previousTryLevel;
245           TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
246           call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
247           ERR("Returned from __finally block - expect crash!\n");
248        }
249       }
250       trylevel = pScopeTable[trylevel].previousTryLevel;
251     }
252   }
253 #else
254   FIXME("exception %lx flags=%lx at %p handler=%p %p %p stub\n",
255         rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
256         frame->handler, context, dispatcher);
257 #endif
258   TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
259   return ExceptionContinueSearch;
260 }
261
262 /*********************************************************************
263  *              _abnormal_termination (MSVCRT.@)
264  */
265 int CDECL _abnormal_termination(void)
266 {
267   FIXME("(void)stub\n");
268   return 0;
269 }
270
271 /*
272  * setjmp/longjmp implementation
273  */
274
275 #ifdef __i386__
276 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
277 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
278
279 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
280 /* and then jumps to the C backend function */
281 #define DEFINE_SETJMP_ENTRYPOINT(name) \
282     __ASM_GLOBAL_FUNC( name, \
283                        "movl 4(%esp),%ecx\n\t"   /* jmp_buf */      \
284                        "movl %ebp,0(%ecx)\n\t"   /* jmp_buf.Ebp */  \
285                        "movl %ebx,4(%ecx)\n\t"   /* jmp_buf.Ebx */  \
286                        "movl %edi,8(%ecx)\n\t"   /* jmp_buf.Edi */  \
287                        "movl %esi,12(%ecx)\n\t"  /* jmp_buf.Esi */  \
288                        "movl %esp,16(%ecx)\n\t"  /* jmp_buf.Esp */  \
289                        "movl 0(%esp),%eax\n\t"                      \
290                        "movl %eax,20(%ecx)\n\t"  /* jmp_buf.Eip */  \
291                        "jmp " __ASM_NAME("__regs_") # name )
292
293 /* restore the registers from the jmp buf upon longjmp */
294 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
295 __ASM_GLOBAL_FUNC( longjmp_set_regs,
296                    "movl 4(%esp),%ecx\n\t"   /* jmp_buf */
297                    "movl 8(%esp),%eax\n\t"   /* retval */
298                    "movl 0(%ecx),%ebp\n\t"   /* jmp_buf.Ebp */
299                    "movl 4(%ecx),%ebx\n\t"   /* jmp_buf.Ebx */
300                    "movl 8(%ecx),%edi\n\t"   /* jmp_buf.Edi */
301                    "movl 12(%ecx),%esi\n\t"  /* jmp_buf.Esi */
302                    "movl 16(%ecx),%esp\n\t"  /* jmp_buf.Esp */
303                    "addl $4,%esp\n\t"        /* get rid of return address */
304                    "jmp *20(%ecx)\n\t"       /* jmp_buf.Eip */ )
305
306 /*
307  * The signatures of the setjmp/longjmp functions do not match that
308  * declared in the setjmp header so they don't follow the regular naming
309  * convention to avoid conflicts.
310  */
311
312 /*******************************************************************
313  *              _setjmp (MSVCRT.@)
314  */
315 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
316 int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
317 {
318     jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
319     if (jmp->Registration == ~0UL)
320         jmp->TryLevel = TRYLEVEL_END;
321     else
322         jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
323
324     TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
325           jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
326     return 0;
327 }
328
329 /*******************************************************************
330  *              _setjmp3 (MSVCRT.@)
331  */
332 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
333 int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
334 {
335     jmp->Cookie = MSVCRT_JMP_MAGIC;
336     jmp->UnwindFunc = 0;
337     jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
338     if (jmp->Registration == ~0UL)
339     {
340         jmp->TryLevel = TRYLEVEL_END;
341     }
342     else
343     {
344         int i;
345         va_list args;
346
347         va_start( args, nb_args );
348         if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
349         if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
350         else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
351         for (i = 0; i < 6 && i < nb_args - 2; i++)
352             jmp->UnwindData[i] = va_arg( args, unsigned long );
353         va_end( args );
354     }
355
356     TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
357           jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
358     return 0;
359 }
360
361 /*********************************************************************
362  *              longjmp (MSVCRT.@)
363  */
364 int CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
365 {
366     unsigned long cur_frame = 0;
367
368     TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
369           jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
370
371     cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
372     TRACE("cur_frame=%lx\n",cur_frame);
373
374     if (cur_frame != jmp->Registration)
375         _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
376
377     if (jmp->Registration)
378     {
379         if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
380             jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
381         {
382             MSVCRT_unwind_function unwind_func;
383
384             unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
385             unwind_func(jmp);
386         }
387         else
388             msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
389                                  jmp->TryLevel, (void *)jmp->Ebp);
390     }
391
392     if (!retval)
393         retval = 1;
394
395     longjmp_set_regs( jmp, retval );
396 }
397
398 /*********************************************************************
399  *              _seh_longjmp_unwind (MSVCRT.@)
400  */
401 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
402 {
403     msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel, (void *)jmp->Ebp );
404 }
405 #endif /* i386 */
406
407 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
408
409 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
410 {
411     BOOL ret = FALSE;
412
413     switch (ctrlType)
414     {
415     case CTRL_C_EVENT:
416         if (sighandlers[MSVCRT_SIGINT])
417         {
418             if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
419                 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
420             ret = TRUE;
421         }
422         break;
423     }
424     return ret;
425 }
426
427 typedef void (*float_handler)(int, int);
428
429 /* The exception codes are actually NTSTATUS values */
430 static const struct
431 {
432     NTSTATUS status;
433     int signal;
434 } float_exception_map[] = {
435  { EXCEPTION_FLT_DENORMAL_OPERAND, MSVCRT__FPE_DENORMAL },
436  { EXCEPTION_FLT_DIVIDE_BY_ZERO, MSVCRT__FPE_ZERODIVIDE },
437  { EXCEPTION_FLT_INEXACT_RESULT, MSVCRT__FPE_INEXACT },
438  { EXCEPTION_FLT_INVALID_OPERATION, MSVCRT__FPE_INVALID },
439  { EXCEPTION_FLT_OVERFLOW, MSVCRT__FPE_OVERFLOW },
440  { EXCEPTION_FLT_STACK_CHECK, MSVCRT__FPE_STACKOVERFLOW },
441  { EXCEPTION_FLT_UNDERFLOW, MSVCRT__FPE_UNDERFLOW },
442 };
443
444 static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
445 {
446     LONG ret = EXCEPTION_CONTINUE_SEARCH;
447     MSVCRT___sighandler_t handler;
448
449     if (!except || !except->ExceptionRecord)
450         return EXCEPTION_CONTINUE_SEARCH;
451
452     switch (except->ExceptionRecord->ExceptionCode)
453     {
454     case EXCEPTION_ACCESS_VIOLATION:
455         if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
456         {
457             if (handler != MSVCRT_SIG_IGN)
458             {
459                 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
460                 handler(MSVCRT_SIGSEGV);
461             }
462             ret = EXCEPTION_CONTINUE_EXECUTION;
463         }
464         break;
465     /* According to msdn,
466      * the FPE signal handler takes as a second argument the type of
467      * floating point exception.
468      */
469     case EXCEPTION_FLT_DENORMAL_OPERAND:
470     case EXCEPTION_FLT_DIVIDE_BY_ZERO:
471     case EXCEPTION_FLT_INEXACT_RESULT:
472     case EXCEPTION_FLT_INVALID_OPERATION:
473     case EXCEPTION_FLT_OVERFLOW:
474     case EXCEPTION_FLT_STACK_CHECK:
475     case EXCEPTION_FLT_UNDERFLOW:
476         if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
477         {
478             if (handler != MSVCRT_SIG_IGN)
479             {
480                 unsigned int i;
481                 int float_signal = MSVCRT__FPE_INVALID;
482
483                 sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
484                 for (i = 0; i < sizeof(float_exception_map) /
485                          sizeof(float_exception_map[0]); i++)
486                 {
487                     if (float_exception_map[i].status ==
488                         except->ExceptionRecord->ExceptionCode)
489                     {
490                         float_signal = float_exception_map[i].signal;
491                         break;
492                     }
493                 }
494                 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
495             }
496             ret = EXCEPTION_CONTINUE_EXECUTION;
497         }
498         break;
499     case EXCEPTION_ILLEGAL_INSTRUCTION:
500     case EXCEPTION_PRIV_INSTRUCTION:
501         if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
502         {
503             if (handler != MSVCRT_SIG_IGN)
504             {
505                 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
506                 handler(MSVCRT_SIGILL);
507             }
508             ret = EXCEPTION_CONTINUE_EXECUTION;
509         }
510         break;
511     }
512     return ret;
513 }
514
515 void msvcrt_init_signals(void)
516 {
517     SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
518     SetUnhandledExceptionFilter(msvcrt_exception_filter);
519 }
520
521 void msvcrt_free_signals(void)
522 {
523     SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
524     SetUnhandledExceptionFilter(NULL);
525 }
526
527 /*********************************************************************
528  *              signal (MSVCRT.@)
529  * Some signals may never be generated except through an explicit call to
530  * raise.
531  */
532 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
533 {
534     MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
535
536     TRACE("(%d, %p)\n", sig, func);
537
538     if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
539
540     switch (sig)
541     {
542     /* Cases handled internally.  Note SIGTERM is never generated by Windows,
543      * so we effectively mask it.
544      */
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;
556     }
557     return ret;
558 }
559
560 /*********************************************************************
561  *              raise (MSVCRT.@)
562  */
563 int CDECL MSVCRT_raise(int sig)
564 {
565     MSVCRT___sighandler_t handler;
566
567     TRACE("(%d)\n", sig);
568
569     switch (sig)
570     {
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)
580         {
581             sighandlers[sig] = MSVCRT_SIG_DFL;
582             if (sig == MSVCRT_SIGFPE)
583                 ((float_handler)handler)(sig, MSVCRT__FPE_EXPLICITGEN);
584             else
585                 handler(sig);
586         }
587         break;
588     default:
589         return -1;
590     }
591     return 0;
592 }
593
594 /*********************************************************************
595  *              _XcptFilter (MSVCRT.@)
596  */
597 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
598 {
599     TRACE("(%08x,%p)\n", ex, ptr);
600     /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
601     return msvcrt_exception_filter(ptr);
602 }