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 #include "cppexcept.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
44 static MSVCRT_security_error_handler security_error_handler
;
46 static MSVCRT___sighandler_t sighandlers
[MSVCRT_NSIG
] = { MSVCRT_SIG_DFL
};
48 static BOOL WINAPI
msvcrt_console_handler(DWORD ctrlType
)
55 if (sighandlers
[MSVCRT_SIGINT
])
57 if (sighandlers
[MSVCRT_SIGINT
] != MSVCRT_SIG_IGN
)
58 sighandlers
[MSVCRT_SIGINT
](MSVCRT_SIGINT
);
66 /*********************************************************************
67 * __pxcptinfoptrs (MSVCRT.@)
69 void** CDECL
MSVCRT___pxcptinfoptrs(void)
71 return (void**)&msvcrt_get_thread_data()->xcptinfo
;
74 typedef void (CDECL
*float_handler
)(int, int);
76 /* The exception codes are actually NTSTATUS values */
81 } float_exception_map
[] = {
82 { EXCEPTION_FLT_DENORMAL_OPERAND
, MSVCRT__FPE_DENORMAL
},
83 { EXCEPTION_FLT_DIVIDE_BY_ZERO
, MSVCRT__FPE_ZERODIVIDE
},
84 { EXCEPTION_FLT_INEXACT_RESULT
, MSVCRT__FPE_INEXACT
},
85 { EXCEPTION_FLT_INVALID_OPERATION
, MSVCRT__FPE_INVALID
},
86 { EXCEPTION_FLT_OVERFLOW
, MSVCRT__FPE_OVERFLOW
},
87 { EXCEPTION_FLT_STACK_CHECK
, MSVCRT__FPE_STACKOVERFLOW
},
88 { EXCEPTION_FLT_UNDERFLOW
, MSVCRT__FPE_UNDERFLOW
},
91 static LONG
msvcrt_exception_filter(struct _EXCEPTION_POINTERS
*except
)
93 LONG ret
= EXCEPTION_CONTINUE_SEARCH
;
94 MSVCRT___sighandler_t handler
;
96 if (!except
|| !except
->ExceptionRecord
)
97 return EXCEPTION_CONTINUE_SEARCH
;
99 switch (except
->ExceptionRecord
->ExceptionCode
)
101 case EXCEPTION_ACCESS_VIOLATION
:
102 if ((handler
= sighandlers
[MSVCRT_SIGSEGV
]) != MSVCRT_SIG_DFL
)
104 if (handler
!= MSVCRT_SIG_IGN
)
106 EXCEPTION_POINTERS
**ep
= (EXCEPTION_POINTERS
**)MSVCRT___pxcptinfoptrs(), *old_ep
;
110 sighandlers
[MSVCRT_SIGSEGV
] = MSVCRT_SIG_DFL
;
111 handler(MSVCRT_SIGSEGV
);
114 ret
= EXCEPTION_CONTINUE_EXECUTION
;
117 /* According to msdn,
118 * the FPE signal handler takes as a second argument the type of
119 * floating point exception.
121 case EXCEPTION_FLT_DENORMAL_OPERAND
:
122 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
123 case EXCEPTION_FLT_INEXACT_RESULT
:
124 case EXCEPTION_FLT_INVALID_OPERATION
:
125 case EXCEPTION_FLT_OVERFLOW
:
126 case EXCEPTION_FLT_STACK_CHECK
:
127 case EXCEPTION_FLT_UNDERFLOW
:
128 if ((handler
= sighandlers
[MSVCRT_SIGFPE
]) != MSVCRT_SIG_DFL
)
130 if (handler
!= MSVCRT_SIG_IGN
)
132 EXCEPTION_POINTERS
**ep
= (EXCEPTION_POINTERS
**)MSVCRT___pxcptinfoptrs(), *old_ep
;
134 int float_signal
= MSVCRT__FPE_INVALID
;
136 sighandlers
[MSVCRT_SIGFPE
] = MSVCRT_SIG_DFL
;
137 for (i
= 0; i
< sizeof(float_exception_map
) /
138 sizeof(float_exception_map
[0]); i
++)
140 if (float_exception_map
[i
].status
==
141 except
->ExceptionRecord
->ExceptionCode
)
143 float_signal
= float_exception_map
[i
].signal
;
150 ((float_handler
)handler
)(MSVCRT_SIGFPE
, float_signal
);
153 ret
= EXCEPTION_CONTINUE_EXECUTION
;
156 case EXCEPTION_ILLEGAL_INSTRUCTION
:
157 case EXCEPTION_PRIV_INSTRUCTION
:
158 if ((handler
= sighandlers
[MSVCRT_SIGILL
]) != MSVCRT_SIG_DFL
)
160 if (handler
!= MSVCRT_SIG_IGN
)
162 EXCEPTION_POINTERS
**ep
= (EXCEPTION_POINTERS
**)MSVCRT___pxcptinfoptrs(), *old_ep
;
166 sighandlers
[MSVCRT_SIGILL
] = MSVCRT_SIG_DFL
;
167 handler(MSVCRT_SIGILL
);
170 ret
= EXCEPTION_CONTINUE_EXECUTION
;
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 /*********************************************************************
189 * Some signals may never be generated except through an explicit call to
192 MSVCRT___sighandler_t CDECL
MSVCRT_signal(int sig
, MSVCRT___sighandler_t func
)
194 MSVCRT___sighandler_t ret
= MSVCRT_SIG_ERR
;
196 TRACE("(%d, %p)\n", sig
, func
);
198 if (func
== MSVCRT_SIG_ERR
) return MSVCRT_SIG_ERR
;
202 /* Cases handled internally. Note SIGTERM is never generated by Windows,
203 * so we effectively mask it.
211 case MSVCRT_SIGBREAK
:
212 ret
= sighandlers
[sig
];
213 sighandlers
[sig
] = func
;
216 ret
= MSVCRT_SIG_ERR
;
221 /*********************************************************************
224 int CDECL
MSVCRT_raise(int sig
)
226 MSVCRT___sighandler_t handler
;
228 TRACE("(%d)\n", sig
);
235 handler
= sighandlers
[sig
];
236 if (handler
== MSVCRT_SIG_DFL
) MSVCRT__exit(3);
237 if (handler
!= MSVCRT_SIG_IGN
)
239 EXCEPTION_POINTERS
**ep
= (EXCEPTION_POINTERS
**)MSVCRT___pxcptinfoptrs(), *old_ep
;
241 sighandlers
[sig
] = MSVCRT_SIG_DFL
;
245 if (sig
== MSVCRT_SIGFPE
)
246 ((float_handler
)handler
)(sig
, MSVCRT__FPE_EXPLICITGEN
);
255 case MSVCRT_SIGBREAK
:
256 handler
= sighandlers
[sig
];
257 if (handler
== MSVCRT_SIG_DFL
) MSVCRT__exit(3);
258 if (handler
!= MSVCRT_SIG_IGN
)
260 sighandlers
[sig
] = MSVCRT_SIG_DFL
;
270 /*********************************************************************
271 * _XcptFilter (MSVCRT.@)
273 int CDECL
_XcptFilter(NTSTATUS ex
, PEXCEPTION_POINTERS ptr
)
275 TRACE("(%08x,%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
_abnormal_termination(void)
285 FIXME("(void)stub\n");
289 /******************************************************************
290 * __uncaught_exception (MSVCRT.@)
292 BOOL CDECL
MSVCRT___uncaught_exception(void)
297 /*********************************************************************
298 * _set_security_error_handler (MSVCR70.@)
300 MSVCRT_security_error_handler CDECL
_set_security_error_handler(
301 MSVCRT_security_error_handler handler
)
303 MSVCRT_security_error_handler old
= security_error_handler
;
305 TRACE("(%p)\n", handler
);
307 security_error_handler
= handler
;
311 /*********************************************************************
312 * __security_error_handler (MSVCR70.@)
314 void CDECL
__security_error_handler(int code
, void *data
)
316 if(security_error_handler
)
317 security_error_handler(code
, data
);
319 FIXME("(%d, %p) stub\n", code
, data
);
324 /*********************************************************************
325 * __crtSetUnhandledExceptionFilter (MSVCR110.@)
327 LPTOP_LEVEL_EXCEPTION_FILTER CDECL
MSVCR110__crtSetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER filter
)
329 return SetUnhandledExceptionFilter(filter
);
332 /*********************************************************************
333 * _CreateFrameInfo (MSVCR80.@)
335 frame_info
* CDECL
_CreateFrameInfo(frame_info
*fi
, void *obj
)
337 thread_data_t
*data
= msvcrt_get_thread_data();
339 TRACE("(%p, %p)\n", fi
, obj
);
341 fi
->next
= data
->frame_info_head
;
342 data
->frame_info_head
= fi
;
347 /*********************************************************************
348 * _FindAndUnlinkFrame (MSVCR80.@)
350 void CDECL
_FindAndUnlinkFrame(frame_info
*fi
)
352 thread_data_t
*data
= msvcrt_get_thread_data();
353 frame_info
*cur
= data
->frame_info_head
;
359 data
->frame_info_head
= cur
->next
;
363 for (; cur
->next
; cur
= cur
->next
)
367 cur
->next
= fi
->next
;
372 ERR("frame not found, native crashes in this case\n");
375 /*********************************************************************
376 * _IsExceptionObjectToBeDestroyed (MSVCR80.@)
378 BOOL __cdecl
_IsExceptionObjectToBeDestroyed(const void *obj
)
382 TRACE( "%p\n", obj
);
384 for (cur
= msvcrt_get_thread_data()->frame_info_head
; cur
; cur
= cur
->next
)
386 if (cur
->object
== obj
)
393 /*********************************************************************
394 * __DestructExceptionObject (MSVCRT.@)
396 void CDECL
__DestructExceptionObject(EXCEPTION_RECORD
*rec
)
398 cxx_exception_type
*info
= (cxx_exception_type
*) rec
->ExceptionInformation
[2];
399 void *object
= (void*)rec
->ExceptionInformation
[1];
401 TRACE("(%p)\n", rec
);
403 if (rec
->ExceptionCode
!= CXX_EXCEPTION
) return;
405 if (rec
->NumberParameters
!= 3) return;
407 if (rec
->NumberParameters
!= 4) return;
409 if (rec
->ExceptionInformation
[0] < CXX_FRAME_MAGIC_VC6
||
410 rec
->ExceptionInformation
[0] > CXX_FRAME_MAGIC_VC8
) return;
412 if (!info
|| !info
->destructor
)
415 #if defined(__i386__)
416 __asm__
__volatile__("call *%0" : : "r" (info
->destructor
), "c" (object
) : "eax", "edx", "memory" );
417 #elif defined(__x86_64__)
418 ((void (__cdecl
*)(void*))(info
->destructor
+rec
->ExceptionInformation
[3]))(object
);
420 ((void (__cdecl
*)(void*))info
->destructor
)(object
);
424 /*********************************************************************
425 * __CxxRegisterExceptionObject (MSVCRT.@)
427 BOOL CDECL
__CxxRegisterExceptionObject(EXCEPTION_RECORD
**rec
, cxx_frame_info
*frame_info
)
429 thread_data_t
*data
= msvcrt_get_thread_data();
431 TRACE("(%p, %p)\n", rec
, frame_info
);
435 frame_info
->rec
= (void*)-1;
436 frame_info
->unk
= (void*)-1;
440 frame_info
->rec
= data
->exc_record
;
442 data
->exc_record
= *rec
;
443 _CreateFrameInfo(&frame_info
->frame_info
, (void*)(*rec
)->ExceptionInformation
[1]);
447 /*********************************************************************
448 * __CxxUnregisterExceptionObject (MSVCRT.@)
450 void CDECL
__CxxUnregisterExceptionObject(cxx_frame_info
*frame_info
, BOOL in_use
)
452 thread_data_t
*data
= msvcrt_get_thread_data();
454 TRACE("(%p)\n", frame_info
);
456 if(frame_info
->rec
== (void*)-1)
459 _FindAndUnlinkFrame(&frame_info
->frame_info
);
460 if(data
->exc_record
->ExceptionCode
== CXX_EXCEPTION
&& !in_use
461 && _IsExceptionObjectToBeDestroyed((void*)data
->exc_record
->ExceptionInformation
[1]))
462 __DestructExceptionObject(data
->exc_record
);
463 data
->exc_record
= frame_info
->rec
;
466 struct __std_exception_data
{
471 /*********************************************************************
472 * __std_exception_copy (MSVCRT.@)
474 void CDECL
MSVCRT___std_exception_copy(const struct __std_exception_data
*src
,
475 struct __std_exception_data
*dst
)
477 TRACE("(%p %p)\n", src
, dst
);
479 if(src
->dofree
&& src
->what
) {
480 dst
->what
= MSVCRT__strdup(src
->what
);
483 dst
->what
= src
->what
;
488 /*********************************************************************
489 * __std_exception_destroy (MSVCRT.@)
491 void CDECL
MSVCRT___std_exception_destroy(struct __std_exception_data
*data
)
493 TRACE("(%p)\n", data
);
496 MSVCRT_free(data
->what
);