Release 1.5.29.
[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 "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winternl.h"
34 #include "wine/exception.h"
35 #include "msvcrt.h"
36 #include "excpt.h"
37 #include "wincon.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(seh);
41
42 static MSVCRT_security_error_handler security_error_handler;
43
44 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
45
46 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
47 {
48     BOOL ret = FALSE;
49
50     switch (ctrlType)
51     {
52     case CTRL_C_EVENT:
53         if (sighandlers[MSVCRT_SIGINT])
54         {
55             if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
56                 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
57             ret = TRUE;
58         }
59         break;
60     }
61     return ret;
62 }
63
64 /*********************************************************************
65  *              __pxcptinfoptrs (MSVCRT.@)
66  */
67 void** CDECL MSVCRT___pxcptinfoptrs(void)
68 {
69     return (void**)&msvcrt_get_thread_data()->xcptinfo;
70 }
71
72 typedef void (CDECL *float_handler)(int, int);
73
74 /* The exception codes are actually NTSTATUS values */
75 static const struct
76 {
77     NTSTATUS status;
78     int signal;
79 } float_exception_map[] = {
80  { EXCEPTION_FLT_DENORMAL_OPERAND, MSVCRT__FPE_DENORMAL },
81  { EXCEPTION_FLT_DIVIDE_BY_ZERO, MSVCRT__FPE_ZERODIVIDE },
82  { EXCEPTION_FLT_INEXACT_RESULT, MSVCRT__FPE_INEXACT },
83  { EXCEPTION_FLT_INVALID_OPERATION, MSVCRT__FPE_INVALID },
84  { EXCEPTION_FLT_OVERFLOW, MSVCRT__FPE_OVERFLOW },
85  { EXCEPTION_FLT_STACK_CHECK, MSVCRT__FPE_STACKOVERFLOW },
86  { EXCEPTION_FLT_UNDERFLOW, MSVCRT__FPE_UNDERFLOW },
87 };
88
89 static LONG msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
90 {
91     LONG ret = EXCEPTION_CONTINUE_SEARCH;
92     MSVCRT___sighandler_t handler;
93
94     if (!except || !except->ExceptionRecord)
95         return EXCEPTION_CONTINUE_SEARCH;
96
97     switch (except->ExceptionRecord->ExceptionCode)
98     {
99     case EXCEPTION_ACCESS_VIOLATION:
100         if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
101         {
102             if (handler != MSVCRT_SIG_IGN)
103             {
104                 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)MSVCRT___pxcptinfoptrs(), *old_ep;
105
106                 old_ep = *ep;
107                 *ep = except;
108                 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
109                 handler(MSVCRT_SIGSEGV);
110                 *ep = old_ep;
111             }
112             ret = EXCEPTION_CONTINUE_EXECUTION;
113         }
114         break;
115     /* According to msdn,
116      * the FPE signal handler takes as a second argument the type of
117      * floating point exception.
118      */
119     case EXCEPTION_FLT_DENORMAL_OPERAND:
120     case EXCEPTION_FLT_DIVIDE_BY_ZERO:
121     case EXCEPTION_FLT_INEXACT_RESULT:
122     case EXCEPTION_FLT_INVALID_OPERATION:
123     case EXCEPTION_FLT_OVERFLOW:
124     case EXCEPTION_FLT_STACK_CHECK:
125     case EXCEPTION_FLT_UNDERFLOW:
126         if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
127         {
128             if (handler != MSVCRT_SIG_IGN)
129             {
130                 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)MSVCRT___pxcptinfoptrs(), *old_ep;
131                 unsigned int i;
132                 int float_signal = MSVCRT__FPE_INVALID;
133
134                 sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
135                 for (i = 0; i < sizeof(float_exception_map) /
136                          sizeof(float_exception_map[0]); i++)
137                 {
138                     if (float_exception_map[i].status ==
139                         except->ExceptionRecord->ExceptionCode)
140                     {
141                         float_signal = float_exception_map[i].signal;
142                         break;
143                     }
144                 }
145
146                 old_ep = *ep;
147                 *ep = except;
148                 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
149                 *ep = old_ep;
150             }
151             ret = EXCEPTION_CONTINUE_EXECUTION;
152         }
153         break;
154     case EXCEPTION_ILLEGAL_INSTRUCTION:
155     case EXCEPTION_PRIV_INSTRUCTION:
156         if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
157         {
158             if (handler != MSVCRT_SIG_IGN)
159             {
160                 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)MSVCRT___pxcptinfoptrs(), *old_ep;
161
162                 old_ep = *ep;
163                 *ep = except;
164                 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
165                 handler(MSVCRT_SIGILL);
166                 *ep = old_ep;
167             }
168             ret = EXCEPTION_CONTINUE_EXECUTION;
169         }
170         break;
171     }
172     return ret;
173 }
174
175 void msvcrt_init_signals(void)
176 {
177     SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
178 }
179
180 void msvcrt_free_signals(void)
181 {
182     SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
183 }
184
185 /*********************************************************************
186  *              signal (MSVCRT.@)
187  * Some signals may never be generated except through an explicit call to
188  * raise.
189  */
190 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
191 {
192     MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
193
194     TRACE("(%d, %p)\n", sig, func);
195
196     if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
197
198     switch (sig)
199     {
200     /* Cases handled internally.  Note SIGTERM is never generated by Windows,
201      * so we effectively mask it.
202      */
203     case MSVCRT_SIGABRT:
204     case MSVCRT_SIGFPE:
205     case MSVCRT_SIGILL:
206     case MSVCRT_SIGSEGV:
207     case MSVCRT_SIGINT:
208     case MSVCRT_SIGTERM:
209     case MSVCRT_SIGBREAK:
210         ret = sighandlers[sig];
211         sighandlers[sig] = func;
212         break;
213     default:
214         ret = MSVCRT_SIG_ERR;
215     }
216     return ret;
217 }
218
219 /*********************************************************************
220  *              raise (MSVCRT.@)
221  */
222 int CDECL MSVCRT_raise(int sig)
223 {
224     MSVCRT___sighandler_t handler;
225
226     TRACE("(%d)\n", sig);
227
228     switch (sig)
229     {
230     case MSVCRT_SIGFPE:
231     case MSVCRT_SIGILL:
232     case MSVCRT_SIGSEGV:
233         handler = sighandlers[sig];
234         if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
235         if (handler != MSVCRT_SIG_IGN)
236         {
237             EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)MSVCRT___pxcptinfoptrs(), *old_ep;
238
239             sighandlers[sig] = MSVCRT_SIG_DFL;
240
241             old_ep = *ep;
242             *ep = NULL;
243             if (sig == MSVCRT_SIGFPE)
244                 ((float_handler)handler)(sig, MSVCRT__FPE_EXPLICITGEN);
245             else
246                 handler(sig);
247             *ep = old_ep;
248         }
249         break;
250     case MSVCRT_SIGABRT:
251     case MSVCRT_SIGINT:
252     case MSVCRT_SIGTERM:
253     case MSVCRT_SIGBREAK:
254         handler = sighandlers[sig];
255         if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
256         if (handler != MSVCRT_SIG_IGN)
257         {
258             sighandlers[sig] = MSVCRT_SIG_DFL;
259             handler(sig);
260         }
261         break;
262     default:
263         return -1;
264     }
265     return 0;
266 }
267
268 /*********************************************************************
269  *              _XcptFilter (MSVCRT.@)
270  */
271 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
272 {
273     TRACE("(%08x,%p)\n", ex, ptr);
274     /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
275     return msvcrt_exception_filter(ptr);
276 }
277
278 /*********************************************************************
279  *              _abnormal_termination (MSVCRT.@)
280  */
281 int CDECL _abnormal_termination(void)
282 {
283   FIXME("(void)stub\n");
284   return 0;
285 }
286
287 /******************************************************************
288  *              MSVCRT___uncaught_exception
289  */
290 BOOL CDECL MSVCRT___uncaught_exception(void)
291 {
292     return FALSE;
293 }
294
295 /* _set_security_error_handler - not exported in native msvcrt, added in msvcr70 */
296 MSVCRT_security_error_handler CDECL _set_security_error_handler(
297     MSVCRT_security_error_handler handler )
298 {
299     MSVCRT_security_error_handler old = security_error_handler;
300
301     TRACE("(%p)\n", handler);
302
303     security_error_handler = handler;
304     return old;
305 }