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