Release 5.0-rc1.
[wine.git] / dlls / msvcrt / except.c
blobda0ac9219e819c33cb0e0802111a9d7361501e16
1 /*
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.
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
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"
40 #include "cppexcept.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(seh);
44 #if _MSVCR_VER>=70 && _MSVCR_VER<=71
45 static MSVCRT_security_error_handler security_error_handler;
46 #endif
48 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
50 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
52 BOOL ret = FALSE;
54 switch (ctrlType)
56 case CTRL_C_EVENT:
57 if (sighandlers[MSVCRT_SIGINT])
59 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
60 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
61 ret = TRUE;
63 break;
65 return ret;
68 /*********************************************************************
69 * __pxcptinfoptrs (MSVCRT.@)
71 void** CDECL MSVCRT___pxcptinfoptrs(void)
73 return (void**)&msvcrt_get_thread_data()->xcptinfo;
76 typedef void (CDECL *float_handler)(int, int);
78 /* The exception codes are actually NTSTATUS values */
79 static const struct
81 NTSTATUS status;
82 int signal;
83 } float_exception_map[] = {
84 { EXCEPTION_FLT_DENORMAL_OPERAND, MSVCRT__FPE_DENORMAL },
85 { EXCEPTION_FLT_DIVIDE_BY_ZERO, MSVCRT__FPE_ZERODIVIDE },
86 { EXCEPTION_FLT_INEXACT_RESULT, MSVCRT__FPE_INEXACT },
87 { EXCEPTION_FLT_INVALID_OPERATION, MSVCRT__FPE_INVALID },
88 { EXCEPTION_FLT_OVERFLOW, MSVCRT__FPE_OVERFLOW },
89 { EXCEPTION_FLT_STACK_CHECK, MSVCRT__FPE_STACKOVERFLOW },
90 { EXCEPTION_FLT_UNDERFLOW, MSVCRT__FPE_UNDERFLOW },
93 static LONG msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
95 LONG ret = EXCEPTION_CONTINUE_SEARCH;
96 MSVCRT___sighandler_t handler;
98 if (!except || !except->ExceptionRecord)
99 return EXCEPTION_CONTINUE_SEARCH;
101 switch (except->ExceptionRecord->ExceptionCode)
103 case EXCEPTION_ACCESS_VIOLATION:
104 if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
106 if (handler != MSVCRT_SIG_IGN)
108 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)MSVCRT___pxcptinfoptrs(), *old_ep;
110 old_ep = *ep;
111 *ep = except;
112 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
113 handler(MSVCRT_SIGSEGV);
114 *ep = old_ep;
116 ret = EXCEPTION_CONTINUE_EXECUTION;
118 break;
119 /* According to msdn,
120 * the FPE signal handler takes as a second argument the type of
121 * floating point exception.
123 case EXCEPTION_FLT_DENORMAL_OPERAND:
124 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
125 case EXCEPTION_FLT_INEXACT_RESULT:
126 case EXCEPTION_FLT_INVALID_OPERATION:
127 case EXCEPTION_FLT_OVERFLOW:
128 case EXCEPTION_FLT_STACK_CHECK:
129 case EXCEPTION_FLT_UNDERFLOW:
130 if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
132 if (handler != MSVCRT_SIG_IGN)
134 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)MSVCRT___pxcptinfoptrs(), *old_ep;
135 unsigned int i;
136 int float_signal = MSVCRT__FPE_INVALID;
138 sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
139 for (i = 0; i < ARRAY_SIZE(float_exception_map); i++)
141 if (float_exception_map[i].status ==
142 except->ExceptionRecord->ExceptionCode)
144 float_signal = float_exception_map[i].signal;
145 break;
149 old_ep = *ep;
150 *ep = except;
151 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
152 *ep = old_ep;
154 ret = EXCEPTION_CONTINUE_EXECUTION;
156 break;
157 case EXCEPTION_ILLEGAL_INSTRUCTION:
158 case EXCEPTION_PRIV_INSTRUCTION:
159 if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
161 if (handler != MSVCRT_SIG_IGN)
163 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)MSVCRT___pxcptinfoptrs(), *old_ep;
165 old_ep = *ep;
166 *ep = except;
167 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
168 handler(MSVCRT_SIGILL);
169 *ep = old_ep;
171 ret = EXCEPTION_CONTINUE_EXECUTION;
173 break;
175 return ret;
178 void msvcrt_init_signals(void)
180 SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
183 void msvcrt_free_signals(void)
185 SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
188 /*********************************************************************
189 * signal (MSVCRT.@)
190 * Some signals may never be generated except through an explicit call to
191 * raise.
193 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
195 MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
197 TRACE("(%d, %p)\n", sig, func);
199 if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
201 switch (sig)
203 /* Cases handled internally. Note SIGTERM is never generated by Windows,
204 * so we effectively mask it.
206 case MSVCRT_SIGABRT:
207 case MSVCRT_SIGFPE:
208 case MSVCRT_SIGILL:
209 case MSVCRT_SIGSEGV:
210 case MSVCRT_SIGINT:
211 case MSVCRT_SIGTERM:
212 case MSVCRT_SIGBREAK:
213 ret = sighandlers[sig];
214 sighandlers[sig] = func;
215 break;
216 default:
217 ret = MSVCRT_SIG_ERR;
219 return ret;
222 /*********************************************************************
223 * raise (MSVCRT.@)
225 int CDECL MSVCRT_raise(int sig)
227 MSVCRT___sighandler_t handler;
229 TRACE("(%d)\n", sig);
231 switch (sig)
233 case MSVCRT_SIGFPE:
234 case MSVCRT_SIGILL:
235 case MSVCRT_SIGSEGV:
236 handler = sighandlers[sig];
237 if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
238 if (handler != MSVCRT_SIG_IGN)
240 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)MSVCRT___pxcptinfoptrs(), *old_ep;
242 sighandlers[sig] = MSVCRT_SIG_DFL;
244 old_ep = *ep;
245 *ep = NULL;
246 if (sig == MSVCRT_SIGFPE)
247 ((float_handler)handler)(sig, MSVCRT__FPE_EXPLICITGEN);
248 else
249 handler(sig);
250 *ep = old_ep;
252 break;
253 case MSVCRT_SIGABRT:
254 case MSVCRT_SIGINT:
255 case MSVCRT_SIGTERM:
256 case MSVCRT_SIGBREAK:
257 handler = sighandlers[sig];
258 if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
259 if (handler != MSVCRT_SIG_IGN)
261 sighandlers[sig] = MSVCRT_SIG_DFL;
262 handler(sig);
264 break;
265 default:
266 return -1;
268 return 0;
271 /*********************************************************************
272 * _XcptFilter (MSVCRT.@)
274 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
276 TRACE("(%08x,%p)\n", ex, ptr);
277 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
278 return msvcrt_exception_filter(ptr);
281 /*********************************************************************
282 * _abnormal_termination (MSVCRT.@)
284 int CDECL _abnormal_termination(void)
286 FIXME("(void)stub\n");
287 return 0;
290 /******************************************************************
291 * __uncaught_exception (MSVCRT.@)
293 BOOL CDECL MSVCRT___uncaught_exception(void)
295 return msvcrt_get_thread_data()->processing_throw != 0;
298 #if _MSVCR_VER>=70 && _MSVCR_VER<=71
300 /*********************************************************************
301 * _set_security_error_handler (MSVCR70.@)
303 MSVCRT_security_error_handler CDECL _set_security_error_handler(
304 MSVCRT_security_error_handler handler )
306 MSVCRT_security_error_handler old = security_error_handler;
308 TRACE("(%p)\n", handler);
310 security_error_handler = handler;
311 return old;
314 /*********************************************************************
315 * __security_error_handler (MSVCR70.@)
317 void CDECL __security_error_handler(int code, void *data)
319 if(security_error_handler)
320 security_error_handler(code, data);
321 else
322 FIXME("(%d, %p) stub\n", code, data);
324 MSVCRT__exit(3);
327 #endif /* _MSVCR_VER>=70 && _MSVCR_VER<=71 */
329 #if _MSVCR_VER>=110
330 /*********************************************************************
331 * __crtSetUnhandledExceptionFilter (MSVCR110.@)
333 LPTOP_LEVEL_EXCEPTION_FILTER CDECL MSVCR110__crtSetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER filter)
335 return SetUnhandledExceptionFilter(filter);
337 #endif
339 /*********************************************************************
340 * _CreateFrameInfo (MSVCR80.@)
342 frame_info* CDECL _CreateFrameInfo(frame_info *fi, void *obj)
344 thread_data_t *data = msvcrt_get_thread_data();
346 TRACE("(%p, %p)\n", fi, obj);
348 fi->next = data->frame_info_head;
349 data->frame_info_head = fi;
350 fi->object = obj;
351 return fi;
354 /*********************************************************************
355 * _FindAndUnlinkFrame (MSVCR80.@)
357 void CDECL _FindAndUnlinkFrame(frame_info *fi)
359 thread_data_t *data = msvcrt_get_thread_data();
360 frame_info *cur = data->frame_info_head;
362 TRACE("(%p)\n", fi);
364 if (cur == fi)
366 data->frame_info_head = cur->next;
367 return;
370 for (; cur->next; cur = cur->next)
372 if (cur->next == fi)
374 cur->next = fi->next;
375 return;
379 ERR("frame not found, native crashes in this case\n");
382 /*********************************************************************
383 * _IsExceptionObjectToBeDestroyed (MSVCR80.@)
385 BOOL __cdecl _IsExceptionObjectToBeDestroyed(const void *obj)
387 frame_info *cur;
389 TRACE( "%p\n", obj );
391 for (cur = msvcrt_get_thread_data()->frame_info_head; cur; cur = cur->next)
393 if (cur->object == obj)
394 return FALSE;
397 return TRUE;
400 /*********************************************************************
401 * __DestructExceptionObject (MSVCRT.@)
403 void CDECL __DestructExceptionObject(EXCEPTION_RECORD *rec)
405 cxx_exception_type *info = (cxx_exception_type*) rec->ExceptionInformation[2];
406 void *object = (void*)rec->ExceptionInformation[1];
408 TRACE("(%p)\n", rec);
410 if (rec->ExceptionCode != CXX_EXCEPTION) return;
411 #ifndef __x86_64__
412 if (rec->NumberParameters != 3) return;
413 #else
414 if (rec->NumberParameters != 4) return;
415 #endif
416 if (rec->ExceptionInformation[0] < CXX_FRAME_MAGIC_VC6 ||
417 rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8) return;
419 if (!info || !info->destructor)
420 return;
422 #if defined(__i386__)
423 __asm__ __volatile__("call *%0" : : "r" (info->destructor), "c" (object) : "eax", "edx", "memory" );
424 #elif defined(__x86_64__)
425 ((void (__cdecl*)(void*))(info->destructor+rec->ExceptionInformation[3]))(object);
426 #else
427 ((void (__cdecl*)(void*))info->destructor)(object);
428 #endif
431 /*********************************************************************
432 * __CxxRegisterExceptionObject (MSVCRT.@)
434 BOOL CDECL __CxxRegisterExceptionObject(EXCEPTION_POINTERS *ep, cxx_frame_info *frame_info)
436 thread_data_t *data = msvcrt_get_thread_data();
438 TRACE("(%p, %p)\n", ep, frame_info);
440 if (!ep || !ep->ExceptionRecord)
442 frame_info->rec = (void*)-1;
443 frame_info->context = (void*)-1;
444 return TRUE;
447 frame_info->rec = data->exc_record;
448 frame_info->context = data->ctx_record;
449 data->exc_record = ep->ExceptionRecord;
450 data->ctx_record = ep->ContextRecord;
451 _CreateFrameInfo(&frame_info->frame_info, (void*)ep->ExceptionRecord->ExceptionInformation[1]);
452 return TRUE;
455 /*********************************************************************
456 * __CxxUnregisterExceptionObject (MSVCRT.@)
458 void CDECL __CxxUnregisterExceptionObject(cxx_frame_info *frame_info, BOOL in_use)
460 thread_data_t *data = msvcrt_get_thread_data();
462 TRACE("(%p)\n", frame_info);
464 if(frame_info->rec == (void*)-1)
465 return;
467 _FindAndUnlinkFrame(&frame_info->frame_info);
468 if(data->exc_record->ExceptionCode == CXX_EXCEPTION && !in_use
469 && _IsExceptionObjectToBeDestroyed((void*)data->exc_record->ExceptionInformation[1]))
470 __DestructExceptionObject(data->exc_record);
471 data->exc_record = frame_info->rec;
472 data->ctx_record = frame_info->context;
475 struct __std_exception_data {
476 char *what;
477 MSVCRT_bool dofree;
480 #if _MSVCR_VER>=140
482 /*********************************************************************
483 * __std_exception_copy (UCRTBASE.@)
485 void CDECL MSVCRT___std_exception_copy(const struct __std_exception_data *src,
486 struct __std_exception_data *dst)
488 TRACE("(%p %p)\n", src, dst);
490 if(src->dofree && src->what) {
491 dst->what = MSVCRT__strdup(src->what);
492 dst->dofree = 1;
493 } else {
494 dst->what = src->what;
495 dst->dofree = 0;
499 /*********************************************************************
500 * __std_exception_destroy (UCRTBASE.@)
502 void CDECL MSVCRT___std_exception_destroy(struct __std_exception_data *data)
504 TRACE("(%p)\n", data);
506 if(data->dofree)
507 MSVCRT_free(data->what);
508 data->what = NULL;
509 data->dofree = 0;
512 /*********************************************************************
513 * __current_exception (UCRTBASE.@)
515 void** CDECL __current_exception(void)
517 TRACE("()\n");
518 return (void**)&msvcrt_get_thread_data()->exc_record;
521 /*********************************************************************
522 * __current_exception_context (UCRTBASE.@)
524 void** CDECL __current_exception_context(void)
526 TRACE("()\n");
527 return (void**)&msvcrt_get_thread_data()->ctx_record;
530 /*********************************************************************
531 * __processing_throw (UCRTBASE.@)
533 int* CDECL __processing_throw(void)
535 TRACE("()\n");
536 return &msvcrt_get_thread_data()->processing_throw;
539 #endif /* _MSVCR_VER>=140 */