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