ddraw/tests: Add another invalid arguments test for surface QI.
[wine.git] / dlls / msvcrt / except.c
blob18c2f1534843df070bb5af8b0eb41618e428be88
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 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)
50 BOOL ret = FALSE;
52 switch (ctrlType)
54 case CTRL_C_EVENT:
55 if (sighandlers[MSVCRT_SIGINT])
57 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
58 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
59 ret = TRUE;
61 break;
63 return ret;
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 */
77 static const struct
79 NTSTATUS status;
80 int signal;
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;
108 old_ep = *ep;
109 *ep = except;
110 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
111 handler(MSVCRT_SIGSEGV);
112 *ep = old_ep;
114 ret = EXCEPTION_CONTINUE_EXECUTION;
116 break;
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;
133 unsigned int i;
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;
144 break;
148 old_ep = *ep;
149 *ep = except;
150 ((float_handler)handler)(MSVCRT_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[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
160 if (handler != MSVCRT_SIG_IGN)
162 EXCEPTION_POINTERS **ep = (EXCEPTION_POINTERS**)MSVCRT___pxcptinfoptrs(), *old_ep;
164 old_ep = *ep;
165 *ep = except;
166 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
167 handler(MSVCRT_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 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;
200 switch (sig)
202 /* Cases handled internally. Note SIGTERM is never generated by Windows,
203 * so we effectively mask it.
205 case MSVCRT_SIGABRT:
206 case MSVCRT_SIGFPE:
207 case MSVCRT_SIGILL:
208 case MSVCRT_SIGSEGV:
209 case MSVCRT_SIGINT:
210 case MSVCRT_SIGTERM:
211 case MSVCRT_SIGBREAK:
212 ret = sighandlers[sig];
213 sighandlers[sig] = func;
214 break;
215 default:
216 ret = MSVCRT_SIG_ERR;
218 return ret;
221 /*********************************************************************
222 * raise (MSVCRT.@)
224 int CDECL MSVCRT_raise(int sig)
226 MSVCRT___sighandler_t handler;
228 TRACE("(%d)\n", sig);
230 switch (sig)
232 case MSVCRT_SIGFPE:
233 case MSVCRT_SIGILL:
234 case MSVCRT_SIGSEGV:
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;
243 old_ep = *ep;
244 *ep = NULL;
245 if (sig == MSVCRT_SIGFPE)
246 ((float_handler)handler)(sig, MSVCRT__FPE_EXPLICITGEN);
247 else
248 handler(sig);
249 *ep = old_ep;
251 break;
252 case MSVCRT_SIGABRT:
253 case MSVCRT_SIGINT:
254 case MSVCRT_SIGTERM:
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;
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("(%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");
286 return 0;
289 /******************************************************************
290 * __uncaught_exception (MSVCRT.@)
292 BOOL CDECL MSVCRT___uncaught_exception(void)
294 return FALSE;
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;
308 return old;
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);
318 else
319 FIXME("(%d, %p) stub\n", code, data);
321 MSVCRT__exit(3);
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;
343 fi->object = obj;
344 return 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;
355 TRACE("(%p)\n", fi);
357 if (cur == fi)
359 data->frame_info_head = cur->next;
360 return;
363 for (; cur->next; cur = cur->next)
365 if (cur->next == fi)
367 cur->next = fi->next;
368 return;
372 ERR("frame not found, native crashes in this case\n");
375 /*********************************************************************
376 * _IsExceptionObjectToBeDestroyed (MSVCR80.@)
378 BOOL __cdecl _IsExceptionObjectToBeDestroyed(const void *obj)
380 frame_info *cur;
382 TRACE( "%p\n", obj );
384 for (cur = msvcrt_get_thread_data()->frame_info_head; cur; cur = cur->next)
386 if (cur->object == obj)
387 return FALSE;
390 return TRUE;
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;
404 #ifndef __x86_64__
405 if (rec->NumberParameters != 3) return;
406 #else
407 if (rec->NumberParameters != 4) return;
408 #endif
409 if (rec->ExceptionInformation[0] < CXX_FRAME_MAGIC_VC6 ||
410 rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8) return;
412 if (!info || !info->destructor)
413 return;
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);
419 #else
420 ((void (__cdecl*)(void*))info->destructor)(object);
421 #endif
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);
433 if (!rec || !*rec)
435 frame_info->rec = (void*)-1;
436 frame_info->unk = (void*)-1;
437 return TRUE;
440 frame_info->rec = data->exc_record;
441 frame_info->unk = 0;
442 data->exc_record = *rec;
443 _CreateFrameInfo(&frame_info->frame_info, (void*)(*rec)->ExceptionInformation[1]);
444 return TRUE;
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)
457 return;
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 {
467 char *what;
468 MSVCRT_bool dofree;
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);
481 dst->dofree = 1;
482 } else {
483 dst->what = src->what;
484 dst->dofree = 0;
488 /*********************************************************************
489 * __std_exception_destroy (MSVCRT.@)
491 void CDECL MSVCRT___std_exception_destroy(struct __std_exception_data *data)
493 TRACE("(%p)\n", data);
495 if(data->dofree)
496 MSVCRT_free(data->what);
497 data->what = NULL;
498 data->dofree = 0;