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 * FIXME: Incomplete support for nested exceptions/try block cleanup.
25 #include "wine/port.h"
30 #define WIN32_NO_STATUS
34 #include "wine/exception.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(seh);
42 static MSVCRT_security_error_handler security_error_handler;
44 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
46 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
53 if (sighandlers[MSVCRT_SIGINT])
55 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
56 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
64 typedef void (CDECL *float_handler)(int, int);
66 /* The exception codes are actually NTSTATUS values */
71 } float_exception_map[] = {
72 { EXCEPTION_FLT_DENORMAL_OPERAND, MSVCRT__FPE_DENORMAL },
73 { EXCEPTION_FLT_DIVIDE_BY_ZERO, MSVCRT__FPE_ZERODIVIDE },
74 { EXCEPTION_FLT_INEXACT_RESULT, MSVCRT__FPE_INEXACT },
75 { EXCEPTION_FLT_INVALID_OPERATION, MSVCRT__FPE_INVALID },
76 { EXCEPTION_FLT_OVERFLOW, MSVCRT__FPE_OVERFLOW },
77 { EXCEPTION_FLT_STACK_CHECK, MSVCRT__FPE_STACKOVERFLOW },
78 { EXCEPTION_FLT_UNDERFLOW, MSVCRT__FPE_UNDERFLOW },
81 static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
83 LONG ret = EXCEPTION_CONTINUE_SEARCH;
84 MSVCRT___sighandler_t handler;
86 if (!except || !except->ExceptionRecord)
87 return EXCEPTION_CONTINUE_SEARCH;
89 switch (except->ExceptionRecord->ExceptionCode)
91 case EXCEPTION_ACCESS_VIOLATION:
92 if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
94 if (handler != MSVCRT_SIG_IGN)
96 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
97 handler(MSVCRT_SIGSEGV);
99 ret = EXCEPTION_CONTINUE_EXECUTION;
102 /* According to msdn,
103 * the FPE signal handler takes as a second argument the type of
104 * floating point exception.
106 case EXCEPTION_FLT_DENORMAL_OPERAND:
107 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
108 case EXCEPTION_FLT_INEXACT_RESULT:
109 case EXCEPTION_FLT_INVALID_OPERATION:
110 case EXCEPTION_FLT_OVERFLOW:
111 case EXCEPTION_FLT_STACK_CHECK:
112 case EXCEPTION_FLT_UNDERFLOW:
113 if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
115 if (handler != MSVCRT_SIG_IGN)
118 int float_signal = MSVCRT__FPE_INVALID;
120 sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
121 for (i = 0; i < sizeof(float_exception_map) /
122 sizeof(float_exception_map[0]); i++)
124 if (float_exception_map[i].status ==
125 except->ExceptionRecord->ExceptionCode)
127 float_signal = float_exception_map[i].signal;
131 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
133 ret = EXCEPTION_CONTINUE_EXECUTION;
136 case EXCEPTION_ILLEGAL_INSTRUCTION:
137 case EXCEPTION_PRIV_INSTRUCTION:
138 if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
140 if (handler != MSVCRT_SIG_IGN)
142 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
143 handler(MSVCRT_SIGILL);
145 ret = EXCEPTION_CONTINUE_EXECUTION;
152 void msvcrt_init_signals(void)
154 SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
155 SetUnhandledExceptionFilter(msvcrt_exception_filter);
158 void msvcrt_free_signals(void)
160 SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
161 SetUnhandledExceptionFilter(NULL);
164 /*********************************************************************
166 * Some signals may never be generated except through an explicit call to
169 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
171 MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
173 TRACE("(%d, %p)\n", sig, func);
175 if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
179 /* Cases handled internally. Note SIGTERM is never generated by Windows,
180 * so we effectively mask it.
188 case MSVCRT_SIGBREAK:
189 ret = sighandlers[sig];
190 sighandlers[sig] = func;
193 ret = MSVCRT_SIG_ERR;
198 /*********************************************************************
201 int CDECL MSVCRT_raise(int sig)
203 MSVCRT___sighandler_t handler;
205 TRACE("(%d)\n", sig);
215 case MSVCRT_SIGBREAK:
216 handler = sighandlers[sig];
217 if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
218 if (handler != MSVCRT_SIG_IGN)
220 sighandlers[sig] = MSVCRT_SIG_DFL;
221 if (sig == MSVCRT_SIGFPE)
222 ((float_handler)handler)(sig, MSVCRT__FPE_EXPLICITGEN);
233 /*********************************************************************
234 * _XcptFilter (MSVCRT.@)
236 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
238 TRACE("(%08x,%p)\n", ex, ptr);
239 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
240 return msvcrt_exception_filter(ptr);
243 /*********************************************************************
244 * _abnormal_termination (MSVCRT.@)
246 int CDECL _abnormal_termination(void)
248 FIXME("(void)stub\n");
252 /******************************************************************
253 * MSVCRT___uncaught_exception
255 BOOL CDECL MSVCRT___uncaught_exception(void)
260 /* _set_security_error_handler - not exported in native msvcrt, added in msvcr70 */
261 MSVCRT_security_error_handler CDECL _set_security_error_handler(
262 MSVCRT_security_error_handler handler )
264 MSVCRT_security_error_handler old = security_error_handler;
266 TRACE("(%p)\n", handler);
268 security_error_handler = handler;