xref: /reactos/sdk/lib/crt/wine/except.c (revision 3a61dd7f)
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 <float.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdbool.h>
28 
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winternl.h"
34 #ifdef __REACTOS__ // FIXME: Clean up wine headers!
35 #include "wine/exception.h"
36 #endif // __REACTOS__
37 #include "msvcrt.h"
38 #include "excpt.h"
39 #include "wincon.h"
40 #include "wine/debug.h"
41 
42 #include "cppexcept.h"
43 
44 WINE_DEFAULT_DEBUG_CHANNEL(seh);
45 
46 #if _MSVCR_VER>=70 && _MSVCR_VER<=71
47 static MSVCRT_security_error_handler security_error_handler;
48 #endif
49 
50 static __sighandler_t sighandlers[NSIG] = { SIG_DFL };
51 
msvcrt_console_handler(DWORD ctrlType)52 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
53 {
54     BOOL ret = FALSE;
55 
56     switch (ctrlType)
57     {
58     case CTRL_C_EVENT:
59         if (sighandlers[SIGINT])
60         {
61             if (sighandlers[SIGINT] != SIG_IGN)
62                 sighandlers[SIGINT](SIGINT);
63             ret = TRUE;
64         }
65         break;
66     }
67     return ret;
68 }
69 
70 /*********************************************************************
71  *              __pxcptinfoptrs (MSVCRT.@)
72  */
__pxcptinfoptrs(void)73 void** CDECL __pxcptinfoptrs(void)
74 {
75     return (void**)&msvcrt_get_thread_data()->xcptinfo;
76 }
77 
78 typedef void (CDECL *float_handler)(int, int);
79 
80 /* The exception codes are actually NTSTATUS values */
81 static const struct
82 {
83     NTSTATUS status;
84     int signal;
85 } float_exception_map[] = {
86  { EXCEPTION_FLT_DENORMAL_OPERAND, _FPE_DENORMAL },
87  { EXCEPTION_FLT_DIVIDE_BY_ZERO, _FPE_ZERODIVIDE },
88  { EXCEPTION_FLT_INEXACT_RESULT, _FPE_INEXACT },
89  { EXCEPTION_FLT_INVALID_OPERATION, _FPE_INVALID },
90  { EXCEPTION_FLT_OVERFLOW, _FPE_OVERFLOW },
91  { EXCEPTION_FLT_STACK_CHECK, _FPE_STACKOVERFLOW },
92  { EXCEPTION_FLT_UNDERFLOW, _FPE_UNDERFLOW },
93 };
94 
msvcrt_exception_filter(struct _EXCEPTION_POINTERS * except)95 static LONG msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
96 {
97     LONG ret = EXCEPTION_CONTINUE_SEARCH;
98     __sighandler_t handler;
99 
100     if (!except || !except->ExceptionRecord)
101         return EXCEPTION_CONTINUE_SEARCH;
102 
103     switch (except->ExceptionRecord->ExceptionCode)
104     {
105     case EXCEPTION_ACCESS_VIOLATION:
106         if ((handler = sighandlers[SIGSEGV]) != SIG_DFL)
107         {
108             if (handler != SIG_IGN)
109             {
110                 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)__pxcptinfoptrs(), *old_ep;
111 
112                 old_ep = *ep;
113                 *ep = except;
114                 sighandlers[SIGSEGV] = SIG_DFL;
115                 handler(SIGSEGV);
116                 *ep = old_ep;
117             }
118             ret = EXCEPTION_CONTINUE_EXECUTION;
119         }
120         break;
121     /* According to msdn,
122      * the FPE signal handler takes as a second argument the type of
123      * floating point exception.
124      */
125     case EXCEPTION_FLT_DENORMAL_OPERAND:
126     case EXCEPTION_FLT_DIVIDE_BY_ZERO:
127     case EXCEPTION_FLT_INEXACT_RESULT:
128     case EXCEPTION_FLT_INVALID_OPERATION:
129     case EXCEPTION_FLT_OVERFLOW:
130     case EXCEPTION_FLT_STACK_CHECK:
131     case EXCEPTION_FLT_UNDERFLOW:
132         if ((handler = sighandlers[SIGFPE]) != SIG_DFL)
133         {
134             if (handler != SIG_IGN)
135             {
136                 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)__pxcptinfoptrs(), *old_ep;
137                 unsigned int i;
138                 int float_signal = _FPE_INVALID;
139 
140                 sighandlers[SIGFPE] = SIG_DFL;
141                 for (i = 0; i < ARRAY_SIZE(float_exception_map); i++)
142                 {
143                     if (float_exception_map[i].status ==
144                         except->ExceptionRecord->ExceptionCode)
145                     {
146                         float_signal = float_exception_map[i].signal;
147                         break;
148                     }
149                 }
150 
151                 old_ep = *ep;
152                 *ep = except;
153                 ((float_handler)handler)(SIGFPE, float_signal);
154                 *ep = old_ep;
155             }
156             ret = EXCEPTION_CONTINUE_EXECUTION;
157         }
158         break;
159     case EXCEPTION_ILLEGAL_INSTRUCTION:
160     case EXCEPTION_PRIV_INSTRUCTION:
161         if ((handler = sighandlers[SIGILL]) != SIG_DFL)
162         {
163             if (handler != SIG_IGN)
164             {
165                 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)__pxcptinfoptrs(), *old_ep;
166 
167                 old_ep = *ep;
168                 *ep = except;
169                 sighandlers[SIGILL] = SIG_DFL;
170                 handler(SIGILL);
171                 *ep = old_ep;
172             }
173             ret = EXCEPTION_CONTINUE_EXECUTION;
174         }
175         break;
176     }
177     return ret;
178 }
179 
msvcrt_init_signals(void)180 void msvcrt_init_signals(void)
181 {
182     SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
183 }
184 
msvcrt_free_signals(void)185 void msvcrt_free_signals(void)
186 {
187     SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
188 }
189 
190 #ifndef __REACTOS__ // Own implementation in signal/signal.c
191 /*********************************************************************
192  *		signal (MSVCRT.@)
193  * Some signals may never be generated except through an explicit call to
194  * raise.
195  */
signal(int sig,__sighandler_t func)196 __sighandler_t CDECL signal(int sig, __sighandler_t func)
197 {
198     __sighandler_t ret = SIG_ERR;
199 
200     TRACE("(%d, %p)\n", sig, func);
201 
202     if (func == SIG_ERR) return SIG_ERR;
203 
204     switch (sig)
205     {
206     /* Cases handled internally.  Note SIGTERM is never generated by Windows,
207      * so we effectively mask it.
208      */
209     case SIGABRT:
210     case SIGFPE:
211     case SIGILL:
212     case SIGSEGV:
213     case SIGINT:
214     case SIGTERM:
215     case SIGBREAK:
216         ret = sighandlers[sig];
217         sighandlers[sig] = func;
218         break;
219     default:
220         ret = SIG_ERR;
221     }
222     return ret;
223 }
224 
225 /*********************************************************************
226  *		raise (MSVCRT.@)
227  */
raise(int sig)228 int CDECL raise(int sig)
229 {
230     __sighandler_t handler;
231 
232     TRACE("(%d)\n", sig);
233 
234     switch (sig)
235     {
236     case SIGFPE:
237     case SIGILL:
238     case SIGSEGV:
239         handler = sighandlers[sig];
240         if (handler == SIG_DFL) _exit(3);
241         if (handler != SIG_IGN)
242         {
243             EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)__pxcptinfoptrs(), *old_ep;
244 
245             sighandlers[sig] = SIG_DFL;
246 
247             old_ep = *ep;
248             *ep = NULL;
249             if (sig == SIGFPE)
250                 ((float_handler)handler)(sig, _FPE_EXPLICITGEN);
251             else
252                 handler(sig);
253             *ep = old_ep;
254         }
255         break;
256     case SIGABRT:
257     case SIGINT:
258     case SIGTERM:
259     case SIGBREAK:
260         handler = sighandlers[sig];
261         if (handler == SIG_DFL) _exit(3);
262         if (handler != SIG_IGN)
263         {
264             sighandlers[sig] = SIG_DFL;
265             handler(sig);
266         }
267         break;
268     default:
269         return -1;
270     }
271     return 0;
272 }
273 #endif // __REACTOS__
274 
275 /*********************************************************************
276  *		_XcptFilter (MSVCRT.@)
277  */
_XcptFilter(NTSTATUS ex,PEXCEPTION_POINTERS ptr)278 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
279 {
280     TRACE("(%08x,%p)\n", ex, ptr);
281     /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
282     return msvcrt_exception_filter(ptr);
283 }
284 
285 #ifndef __REACTOS__
286 /*********************************************************************
287  *		_abnormal_termination (MSVCRT.@)
288  */
__intrinsic_abnormal_termination(void)289 int CDECL __intrinsic_abnormal_termination(void)
290 {
291   FIXME("(void)stub\n");
292   return 0;
293 }
294 #endif /* __REACTOS__ */
295 
296 /******************************************************************
297  *		__uncaught_exception (MSVCRT.@)
298  */
__uncaught_exception(void)299 BOOL CDECL __uncaught_exception(void)
300 {
301     return msvcrt_get_thread_data()->processing_throw != 0;
302 }
303 
304 #if _MSVCR_VER>=70 && _MSVCR_VER<=71
305 
306 /*********************************************************************
307  *		_set_security_error_handler (MSVCR70.@)
308  */
_set_security_error_handler(MSVCRT_security_error_handler handler)309 MSVCRT_security_error_handler CDECL _set_security_error_handler(
310     MSVCRT_security_error_handler handler )
311 {
312     MSVCRT_security_error_handler old = security_error_handler;
313 
314     TRACE("(%p)\n", handler);
315 
316     security_error_handler = handler;
317     return old;
318 }
319 
320 /*********************************************************************
321  *		__security_error_handler (MSVCR70.@)
322  */
__security_error_handler(int code,void * data)323 void CDECL __security_error_handler(int code, void *data)
324 {
325     if(security_error_handler)
326         security_error_handler(code, data);
327     else
328         FIXME("(%d, %p) stub\n", code, data);
329 
330     _exit(3);
331 }
332 
333 #endif /* _MSVCR_VER>=70 && _MSVCR_VER<=71 */
334 
335 #if _MSVCR_VER>=110
336 /*********************************************************************
337  *  __crtSetUnhandledExceptionFilter (MSVCR110.@)
338  */
__crtSetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER filter)339 LPTOP_LEVEL_EXCEPTION_FILTER CDECL __crtSetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER filter)
340 {
341     return SetUnhandledExceptionFilter(filter);
342 }
343 #endif
344 
345 /*********************************************************************
346  * _CreateFrameInfo (MSVCR80.@)
347  */
_CreateFrameInfo(frame_info * fi,void * obj)348 frame_info* CDECL _CreateFrameInfo(frame_info *fi, void *obj)
349 {
350     thread_data_t *data = msvcrt_get_thread_data();
351 
352     TRACE("(%p, %p)\n", fi, obj);
353 
354     fi->next = data->frame_info_head;
355     data->frame_info_head = fi;
356     fi->object = obj;
357     return fi;
358 }
359 
360 /*********************************************************************
361  * _FindAndUnlinkFrame (MSVCR80.@)
362  */
_FindAndUnlinkFrame(frame_info * fi)363 void CDECL _FindAndUnlinkFrame(frame_info *fi)
364 {
365     thread_data_t *data = msvcrt_get_thread_data();
366     frame_info *cur = data->frame_info_head;
367 
368     TRACE("(%p)\n", fi);
369 
370     if (cur == fi)
371     {
372         data->frame_info_head = cur->next;
373         return;
374     }
375 
376     for (; cur->next; cur = cur->next)
377     {
378         if (cur->next == fi)
379         {
380             cur->next = fi->next;
381             return;
382         }
383     }
384 
385     ERR("frame not found, native crashes in this case\n");
386 }
387 
388 /*********************************************************************
389  *              _IsExceptionObjectToBeDestroyed (MSVCR80.@)
390  */
_IsExceptionObjectToBeDestroyed(const void * obj)391 BOOL __cdecl _IsExceptionObjectToBeDestroyed(const void *obj)
392 {
393     frame_info *cur;
394 
395     TRACE( "%p\n", obj );
396 
397     for (cur = msvcrt_get_thread_data()->frame_info_head; cur; cur = cur->next)
398     {
399         if (cur->object == obj)
400             return FALSE;
401     }
402 
403     return TRUE;
404 }
405 
406 /*********************************************************************
407  * __DestructExceptionObject (MSVCRT.@)
408  */
__DestructExceptionObject(EXCEPTION_RECORD * rec)409 void CDECL __DestructExceptionObject(EXCEPTION_RECORD *rec)
410 {
411     cxx_exception_type *info = (cxx_exception_type*) rec->ExceptionInformation[2];
412     void *object = (void*)rec->ExceptionInformation[1];
413 
414     TRACE("(%p)\n", rec);
415 
416     if (rec->ExceptionCode != CXX_EXCEPTION) return;
417 #ifndef __x86_64__
418     if (rec->NumberParameters != 3) return;
419 #else
420     if (rec->NumberParameters != 4) return;
421 #endif
422     if (rec->ExceptionInformation[0] < CXX_FRAME_MAGIC_VC6 ||
423             rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8) return;
424 
425     if (!info || !info->destructor)
426         return;
427 
428 #if defined(__i386__)
429 #ifdef _MSC_VER
430     ((void(__fastcall*)(void*))info->destructor)(object);
431 #else
432     __asm__ __volatile__("call *%0" : : "r" (info->destructor), "c" (object) : "eax", "edx", "memory");
433 #endif
434 #elif defined(__x86_64__)
435     ((void (__cdecl*)(void*))(info->destructor+rec->ExceptionInformation[3]))(object);
436 #else
437     ((void (__cdecl*)(void*))info->destructor)(object);
438 #endif
439 }
440 
441 /*********************************************************************
442  *  __CxxRegisterExceptionObject (MSVCRT.@)
443  */
__CxxRegisterExceptionObject(EXCEPTION_POINTERS * ep,cxx_frame_info * frame_info)444 BOOL CDECL __CxxRegisterExceptionObject(EXCEPTION_POINTERS *ep, cxx_frame_info *frame_info)
445 {
446     thread_data_t *data = msvcrt_get_thread_data();
447 
448     TRACE("(%p, %p)\n", ep, frame_info);
449 
450     if (!ep || !ep->ExceptionRecord)
451     {
452         frame_info->rec = (void*)-1;
453         frame_info->context = (void*)-1;
454         return TRUE;
455     }
456 
457     frame_info->rec = data->exc_record;
458     frame_info->context = data->ctx_record;
459     data->exc_record = ep->ExceptionRecord;
460     data->ctx_record = ep->ContextRecord;
461     _CreateFrameInfo(&frame_info->frame_info, (void*)ep->ExceptionRecord->ExceptionInformation[1]);
462     return TRUE;
463 }
464 
465 /*********************************************************************
466  *  __CxxUnregisterExceptionObject (MSVCRT.@)
467  */
__CxxUnregisterExceptionObject(cxx_frame_info * frame_info,BOOL in_use)468 void CDECL __CxxUnregisterExceptionObject(cxx_frame_info *frame_info, BOOL in_use)
469 {
470     thread_data_t *data = msvcrt_get_thread_data();
471 
472     TRACE("(%p)\n", frame_info);
473 
474     if(frame_info->rec == (void*)-1)
475         return;
476 
477     _FindAndUnlinkFrame(&frame_info->frame_info);
478     if(data->exc_record->ExceptionCode == CXX_EXCEPTION && !in_use
479             && _IsExceptionObjectToBeDestroyed((void*)data->exc_record->ExceptionInformation[1]))
480         __DestructExceptionObject(data->exc_record);
481     data->exc_record = frame_info->rec;
482     data->ctx_record = frame_info->context;
483 }
484 
485 struct __std_exception_data {
486     char *what;
487     char dofree;
488 };
489 
490 #if _MSVCR_VER>=140
491 
492 /*********************************************************************
493  *  __std_exception_copy (UCRTBASE.@)
494  */
__std_exception_copy(const struct __std_exception_data * src,struct __std_exception_data * dst)495 void CDECL __std_exception_copy(const struct __std_exception_data *src,
496                                        struct __std_exception_data *dst)
497 {
498     TRACE("(%p %p)\n", src, dst);
499 
500     if(src->dofree && src->what) {
501         dst->what   = _strdup(src->what);
502         dst->dofree = 1;
503     } else {
504         dst->what   = src->what;
505         dst->dofree = 0;
506     }
507 }
508 
509 /*********************************************************************
510  *  __std_exception_destroy (UCRTBASE.@)
511  */
__std_exception_destroy(struct __std_exception_data * data)512 void CDECL __std_exception_destroy(struct __std_exception_data *data)
513 {
514     TRACE("(%p)\n", data);
515 
516     if(data->dofree)
517         free(data->what);
518     data->what   = NULL;
519     data->dofree = 0;
520 }
521 
522 /*********************************************************************
523  *  __current_exception (UCRTBASE.@)
524  */
__current_exception(void)525 void** CDECL __current_exception(void)
526 {
527     TRACE("()\n");
528     return (void**)&msvcrt_get_thread_data()->exc_record;
529 }
530 
531 /*********************************************************************
532  *  __current_exception_context (UCRTBASE.@)
533  */
__current_exception_context(void)534 void** CDECL __current_exception_context(void)
535 {
536     TRACE("()\n");
537     return (void**)&msvcrt_get_thread_data()->ctx_record;
538 }
539 
540 /*********************************************************************
541  *  __processing_throw (UCRTBASE.@)
542  */
__processing_throw(void)543 int* CDECL __processing_throw(void)
544 {
545     TRACE("()\n");
546     return &msvcrt_get_thread_data()->processing_throw;
547 }
548 
549 #endif /* _MSVCR_VER>=140 */
550