win32u: Move NtUserGetAsyncKeyState implementation from user32.
[wine.git] / dlls / msvcrt / except.c
bloba7140a9011f4dbe606dd97a61b958b9b55cb504a
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 <float.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdbool.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winternl.h"
34 #include "msvcrt.h"
35 #include "excpt.h"
36 #include "wincon.h"
37 #include "wine/debug.h"
39 #include "cppexcept.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(seh);
43 #if _MSVCR_VER>=70 && _MSVCR_VER<=71
44 static MSVCRT_security_error_handler security_error_handler;
45 #endif
47 static __sighandler_t sighandlers[NSIG] = { SIG_DFL };
49 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
51 BOOL ret = FALSE;
53 switch (ctrlType)
55 case CTRL_C_EVENT:
56 if (sighandlers[SIGINT])
58 if (sighandlers[SIGINT] != SIG_IGN)
59 sighandlers[SIGINT](SIGINT);
60 ret = TRUE;
62 break;
64 return ret;
67 /*********************************************************************
68 * __pxcptinfoptrs (MSVCRT.@)
70 void** CDECL __pxcptinfoptrs(void)
72 return (void**)&msvcrt_get_thread_data()->xcptinfo;
75 typedef void (CDECL *float_handler)(int, int);
77 /* The exception codes are actually NTSTATUS values */
78 static const struct
80 NTSTATUS status;
81 int signal;
82 } float_exception_map[] = {
83 { EXCEPTION_FLT_DENORMAL_OPERAND, _FPE_DENORMAL },
84 { EXCEPTION_FLT_DIVIDE_BY_ZERO, _FPE_ZERODIVIDE },
85 { EXCEPTION_FLT_INEXACT_RESULT, _FPE_INEXACT },
86 { EXCEPTION_FLT_INVALID_OPERATION, _FPE_INVALID },
87 { EXCEPTION_FLT_OVERFLOW, _FPE_OVERFLOW },
88 { EXCEPTION_FLT_STACK_CHECK, _FPE_STACKOVERFLOW },
89 { EXCEPTION_FLT_UNDERFLOW, _FPE_UNDERFLOW },
92 static LONG msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
94 LONG ret = EXCEPTION_CONTINUE_SEARCH;
95 __sighandler_t handler;
97 if (!except || !except->ExceptionRecord)
98 return EXCEPTION_CONTINUE_SEARCH;
100 switch (except->ExceptionRecord->ExceptionCode)
102 case EXCEPTION_ACCESS_VIOLATION:
103 if ((handler = sighandlers[SIGSEGV]) != SIG_DFL)
105 if (handler != SIG_IGN)
107 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)__pxcptinfoptrs(), *old_ep;
109 old_ep = *ep;
110 *ep = except;
111 sighandlers[SIGSEGV] = SIG_DFL;
112 handler(SIGSEGV);
113 *ep = old_ep;
115 ret = EXCEPTION_CONTINUE_EXECUTION;
117 break;
118 /* According to msdn,
119 * the FPE signal handler takes as a second argument the type of
120 * floating point exception.
122 case EXCEPTION_FLT_DENORMAL_OPERAND:
123 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
124 case EXCEPTION_FLT_INEXACT_RESULT:
125 case EXCEPTION_FLT_INVALID_OPERATION:
126 case EXCEPTION_FLT_OVERFLOW:
127 case EXCEPTION_FLT_STACK_CHECK:
128 case EXCEPTION_FLT_UNDERFLOW:
129 if ((handler = sighandlers[SIGFPE]) != SIG_DFL)
131 if (handler != SIG_IGN)
133 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)__pxcptinfoptrs(), *old_ep;
134 unsigned int i;
135 int float_signal = _FPE_INVALID;
137 sighandlers[SIGFPE] = SIG_DFL;
138 for (i = 0; i < ARRAY_SIZE(float_exception_map); i++)
140 if (float_exception_map[i].status ==
141 except->ExceptionRecord->ExceptionCode)
143 float_signal = float_exception_map[i].signal;
144 break;
148 old_ep = *ep;
149 *ep = except;
150 ((float_handler)handler)(SIGFPE, float_signal);
151 *ep = old_ep;
153 ret = EXCEPTION_CONTINUE_EXECUTION;
155 break;
156 case EXCEPTION_ILLEGAL_INSTRUCTION:
157 case EXCEPTION_PRIV_INSTRUCTION:
158 if ((handler = sighandlers[SIGILL]) != SIG_DFL)
160 if (handler != SIG_IGN)
162 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)__pxcptinfoptrs(), *old_ep;
164 old_ep = *ep;
165 *ep = except;
166 sighandlers[SIGILL] = SIG_DFL;
167 handler(SIGILL);
168 *ep = old_ep;
170 ret = EXCEPTION_CONTINUE_EXECUTION;
172 break;
174 return ret;
177 void msvcrt_init_signals(void)
179 SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
182 void msvcrt_free_signals(void)
184 SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
187 /*********************************************************************
188 * signal (MSVCRT.@)
189 * Some signals may never be generated except through an explicit call to
190 * raise.
192 __sighandler_t CDECL signal(int sig, __sighandler_t func)
194 __sighandler_t ret = SIG_ERR;
196 TRACE("(%d, %p)\n", sig, func);
198 if (func == SIG_ERR) return SIG_ERR;
200 switch (sig)
202 /* Cases handled internally. Note SIGTERM is never generated by Windows,
203 * so we effectively mask it.
205 case SIGABRT:
206 case SIGFPE:
207 case SIGILL:
208 case SIGSEGV:
209 case SIGINT:
210 case SIGTERM:
211 case SIGBREAK:
212 ret = sighandlers[sig];
213 sighandlers[sig] = func;
214 break;
215 default:
216 ret = SIG_ERR;
218 return ret;
221 /*********************************************************************
222 * raise (MSVCRT.@)
224 int CDECL raise(int sig)
226 __sighandler_t handler;
228 TRACE("(%d)\n", sig);
230 switch (sig)
232 case SIGFPE:
233 case SIGILL:
234 case SIGSEGV:
235 handler = sighandlers[sig];
236 if (handler == SIG_DFL) _exit(3);
237 if (handler != SIG_IGN)
239 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)__pxcptinfoptrs(), *old_ep;
241 sighandlers[sig] = SIG_DFL;
243 old_ep = *ep;
244 *ep = NULL;
245 if (sig == SIGFPE)
246 ((float_handler)handler)(sig, _FPE_EXPLICITGEN);
247 else
248 handler(sig);
249 *ep = old_ep;
251 break;
252 case SIGABRT:
253 case SIGINT:
254 case SIGTERM:
255 case SIGBREAK:
256 handler = sighandlers[sig];
257 if (handler == SIG_DFL) _exit(3);
258 if (handler != SIG_IGN)
260 sighandlers[sig] = SIG_DFL;
261 handler(sig);
263 break;
264 default:
265 return -1;
267 return 0;
270 /*********************************************************************
271 * _XcptFilter (MSVCRT.@)
273 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
275 TRACE("(%08lx,%p)\n", ex, ptr);
276 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
277 return msvcrt_exception_filter(ptr);
280 /*********************************************************************
281 * _abnormal_termination (MSVCRT.@)
283 int CDECL __intrinsic_abnormal_termination(void)
285 FIXME("(void)stub\n");
286 return 0;
289 /******************************************************************
290 * __uncaught_exception (MSVCRT.@)
292 BOOL CDECL __uncaught_exception(void)
294 return msvcrt_get_thread_data()->processing_throw != 0;
297 #if _MSVCR_VER>=70 && _MSVCR_VER<=71
299 /*********************************************************************
300 * _set_security_error_handler (MSVCR70.@)
302 MSVCRT_security_error_handler CDECL _set_security_error_handler(
303 MSVCRT_security_error_handler handler )
305 MSVCRT_security_error_handler old = security_error_handler;
307 TRACE("(%p)\n", handler);
309 security_error_handler = handler;
310 return old;
313 /*********************************************************************
314 * __security_error_handler (MSVCR70.@)
316 void CDECL __security_error_handler(int code, void *data)
318 if(security_error_handler)
319 security_error_handler(code, data);
320 else
321 FIXME("(%d, %p) stub\n", code, data);
323 _exit(3);
326 #endif /* _MSVCR_VER>=70 && _MSVCR_VER<=71 */
328 #if _MSVCR_VER>=110
329 /*********************************************************************
330 * __crtSetUnhandledExceptionFilter (MSVCR110.@)
332 LPTOP_LEVEL_EXCEPTION_FILTER CDECL __crtSetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER filter)
334 return SetUnhandledExceptionFilter(filter);
336 #endif
338 /*********************************************************************
339 * _CreateFrameInfo (MSVCR80.@)
341 frame_info* CDECL _CreateFrameInfo(frame_info *fi, void *obj)
343 thread_data_t *data = msvcrt_get_thread_data();
345 TRACE("(%p, %p)\n", fi, obj);
347 fi->next = data->frame_info_head;
348 data->frame_info_head = fi;
349 fi->object = obj;
350 return fi;
353 /*********************************************************************
354 * _FindAndUnlinkFrame (MSVCR80.@)
356 void CDECL _FindAndUnlinkFrame(frame_info *fi)
358 thread_data_t *data = msvcrt_get_thread_data();
359 frame_info *cur = data->frame_info_head;
361 TRACE("(%p)\n", fi);
363 if (cur == fi)
365 data->frame_info_head = cur->next;
366 return;
369 for (; cur->next; cur = cur->next)
371 if (cur->next == fi)
373 cur->next = fi->next;
374 return;
378 ERR("frame not found, native crashes in this case\n");
381 /*********************************************************************
382 * _IsExceptionObjectToBeDestroyed (MSVCR80.@)
384 BOOL __cdecl _IsExceptionObjectToBeDestroyed(const void *obj)
386 frame_info *cur;
388 TRACE( "%p\n", obj );
390 for (cur = msvcrt_get_thread_data()->frame_info_head; cur; cur = cur->next)
392 if (cur->object == obj)
393 return FALSE;
396 return TRUE;
399 /*********************************************************************
400 * __DestructExceptionObject (MSVCRT.@)
402 void CDECL __DestructExceptionObject(EXCEPTION_RECORD *rec)
404 cxx_exception_type *info = (cxx_exception_type*) rec->ExceptionInformation[2];
405 void *object = (void*)rec->ExceptionInformation[1];
407 TRACE("(%p)\n", rec);
409 if (rec->ExceptionCode != CXX_EXCEPTION) return;
410 #ifndef __x86_64__
411 if (rec->NumberParameters != 3) return;
412 #else
413 if (rec->NumberParameters != 4) return;
414 #endif
415 if (rec->ExceptionInformation[0] < CXX_FRAME_MAGIC_VC6 ||
416 rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8) return;
418 if (!info || !info->destructor)
419 return;
421 #if defined(__i386__)
422 __asm__ __volatile__("call *%0" : : "r" (info->destructor), "c" (object) : "eax", "edx", "memory" );
423 #elif defined(__x86_64__)
424 ((void (__cdecl*)(void*))(info->destructor+rec->ExceptionInformation[3]))(object);
425 #else
426 ((void (__cdecl*)(void*))info->destructor)(object);
427 #endif
430 /*********************************************************************
431 * __CxxRegisterExceptionObject (MSVCRT.@)
433 BOOL CDECL __CxxRegisterExceptionObject(EXCEPTION_POINTERS *ep, cxx_frame_info *frame_info)
435 thread_data_t *data = msvcrt_get_thread_data();
437 TRACE("(%p, %p)\n", ep, frame_info);
439 if (!ep || !ep->ExceptionRecord)
441 frame_info->rec = (void*)-1;
442 frame_info->context = (void*)-1;
443 return TRUE;
446 frame_info->rec = data->exc_record;
447 frame_info->context = data->ctx_record;
448 data->exc_record = ep->ExceptionRecord;
449 data->ctx_record = ep->ContextRecord;
450 _CreateFrameInfo(&frame_info->frame_info, (void*)ep->ExceptionRecord->ExceptionInformation[1]);
451 return TRUE;
454 /*********************************************************************
455 * __CxxUnregisterExceptionObject (MSVCRT.@)
457 void CDECL __CxxUnregisterExceptionObject(cxx_frame_info *frame_info, BOOL in_use)
459 thread_data_t *data = msvcrt_get_thread_data();
461 TRACE("(%p)\n", frame_info);
463 if(frame_info->rec == (void*)-1)
464 return;
466 _FindAndUnlinkFrame(&frame_info->frame_info);
467 if(data->exc_record->ExceptionCode == CXX_EXCEPTION && !in_use
468 && _IsExceptionObjectToBeDestroyed((void*)data->exc_record->ExceptionInformation[1]))
469 __DestructExceptionObject(data->exc_record);
470 data->exc_record = frame_info->rec;
471 data->ctx_record = frame_info->context;
474 struct __std_exception_data {
475 char *what;
476 char dofree;
479 #if _MSVCR_VER>=140
481 /*********************************************************************
482 * __std_exception_copy (UCRTBASE.@)
484 void CDECL __std_exception_copy(const struct __std_exception_data *src,
485 struct __std_exception_data *dst)
487 TRACE("(%p %p)\n", src, dst);
489 if(src->dofree && src->what) {
490 dst->what = _strdup(src->what);
491 dst->dofree = 1;
492 } else {
493 dst->what = src->what;
494 dst->dofree = 0;
498 /*********************************************************************
499 * __std_exception_destroy (UCRTBASE.@)
501 void CDECL __std_exception_destroy(struct __std_exception_data *data)
503 TRACE("(%p)\n", data);
505 if(data->dofree)
506 free(data->what);
507 data->what = NULL;
508 data->dofree = 0;
511 /*********************************************************************
512 * __current_exception (UCRTBASE.@)
514 void** CDECL __current_exception(void)
516 TRACE("()\n");
517 return (void**)&msvcrt_get_thread_data()->exc_record;
520 /*********************************************************************
521 * __current_exception_context (UCRTBASE.@)
523 void** CDECL __current_exception_context(void)
525 TRACE("()\n");
526 return (void**)&msvcrt_get_thread_data()->ctx_record;
529 /*********************************************************************
530 * __processing_throw (UCRTBASE.@)
532 int* CDECL __processing_throw(void)
534 TRACE("()\n");
535 return &msvcrt_get_thread_data()->processing_throw;
538 #endif /* _MSVCR_VER>=140 */