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