ddraw/tests: Add another invalid arguments test for surface QI.
[wine.git] / dlls / msvcrt / except_x86_64.c
blob0820aaded86c265a30142e48dcb1a5511623b562
1 /*
2 * msvcrt C++ exception handling
4 * Copyright 2011 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #ifdef __x86_64__
26 #include <stdarg.h>
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winternl.h"
33 #include "msvcrt.h"
34 #include "wine/exception.h"
35 #include "excpt.h"
36 #include "wine/debug.h"
38 #include "cppexcept.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(seh);
42 struct _DISPATCHER_CONTEXT;
44 typedef LONG (WINAPI *PC_LANGUAGE_EXCEPTION_HANDLER)( EXCEPTION_POINTERS *ptrs, ULONG64 frame );
45 typedef EXCEPTION_DISPOSITION (WINAPI *PEXCEPTION_ROUTINE)( EXCEPTION_RECORD *rec,
46 ULONG64 frame,
47 CONTEXT *context,
48 struct _DISPATCHER_CONTEXT *dispatch );
50 typedef struct _DISPATCHER_CONTEXT
52 ULONG64 ControlPc;
53 ULONG64 ImageBase;
54 PRUNTIME_FUNCTION FunctionEntry;
55 ULONG64 EstablisherFrame;
56 ULONG64 TargetIp;
57 PCONTEXT ContextRecord;
58 PEXCEPTION_ROUTINE LanguageHandler;
59 PVOID HandlerData;
60 PUNWIND_HISTORY_TABLE HistoryTable;
61 ULONG ScopeIndex;
62 } DISPATCHER_CONTEXT;
64 typedef struct
66 int prev;
67 UINT handler;
68 } unwind_info;
70 typedef struct
72 UINT flags;
73 UINT type_info;
74 int offset;
75 UINT handler;
76 UINT frame;
77 } catchblock_info;
78 #define TYPE_FLAG_CONST 1
79 #define TYPE_FLAG_VOLATILE 2
80 #define TYPE_FLAG_REFERENCE 8
82 typedef struct
84 int start_level;
85 int end_level;
86 int catch_level;
87 int catchblock_count;
88 UINT catchblock;
89 } tryblock_info;
91 typedef struct
93 int ip;
94 int state;
95 } ipmap_info;
97 typedef struct __cxx_function_descr
99 UINT magic;
100 UINT unwind_count;
101 UINT unwind_table;
102 UINT tryblock_count;
103 UINT tryblock;
104 UINT ipmap_count;
105 UINT ipmap;
106 UINT unwind_help;
107 UINT expect_list;
108 UINT flags;
109 } cxx_function_descr;
111 typedef struct
113 cxx_frame_info frame_info;
114 BOOL rethrow;
115 } cxx_catch_ctx;
117 typedef struct
119 ULONG64 dest_frame;
120 ULONG64 orig_frame;
121 EXCEPTION_RECORD *seh_rec;
122 DISPATCHER_CONTEXT *dispatch;
123 const cxx_function_descr *descr;
124 } se_translator_ctx;
126 static inline void* rva_to_ptr(UINT rva, ULONG64 base)
128 return rva ? (void*)(base+rva) : NULL;
131 static inline void dump_type(UINT type_rva, ULONG64 base)
133 const cxx_type_info *type = rva_to_ptr(type_rva, base);
135 TRACE("flags %x type %x %s offsets %d,%d,%d size %d copy ctor %x(%p)\n",
136 type->flags, type->type_info, dbgstr_type_info(rva_to_ptr(type->type_info, base)),
137 type->offsets.this_offset, type->offsets.vbase_descr, type->offsets.vbase_offset,
138 type->size, type->copy_ctor, rva_to_ptr(type->copy_ctor, base));
141 static void dump_exception_type(const cxx_exception_type *type, ULONG64 base)
143 const cxx_type_info_table *type_info_table = rva_to_ptr(type->type_info_table, base);
144 UINT i;
146 TRACE("flags %x destr %x(%p) handler %x(%p) type info %x(%p)\n",
147 type->flags, type->destructor, rva_to_ptr(type->destructor, base),
148 type->custom_handler, rva_to_ptr(type->custom_handler, base),
149 type->type_info_table, type_info_table);
150 for (i = 0; i < type_info_table->count; i++)
152 TRACE(" %d: ", i);
153 dump_type(type_info_table->info[i], base);
157 static void dump_function_descr(const cxx_function_descr *descr, ULONG64 image_base)
159 unwind_info *unwind_table = rva_to_ptr(descr->unwind_table, image_base);
160 tryblock_info *tryblock = rva_to_ptr(descr->tryblock, image_base);
161 ipmap_info *ipmap = rva_to_ptr(descr->ipmap, image_base);
162 UINT i, j;
164 TRACE("magic %x\n", descr->magic);
165 TRACE("unwind table: %x(%p) %d\n", descr->unwind_table, unwind_table, descr->unwind_count);
166 for (i=0; i<descr->unwind_count; i++)
168 TRACE(" %d: prev %d func %x(%p)\n", i, unwind_table[i].prev,
169 unwind_table[i].handler, rva_to_ptr(unwind_table[i].handler, image_base));
171 TRACE("try table: %x(%p) %d\n", descr->tryblock, tryblock, descr->tryblock_count);
172 for (i=0; i<descr->tryblock_count; i++)
174 catchblock_info *catchblock = rva_to_ptr(tryblock[i].catchblock, image_base);
176 TRACE(" %d: start %d end %d catchlevel %d catch %x(%p) %d\n", i,
177 tryblock[i].start_level, tryblock[i].end_level,
178 tryblock[i].catch_level, tryblock[i].catchblock,
179 catchblock, tryblock[i].catchblock_count);
180 for (j=0; j<tryblock[i].catchblock_count; j++)
182 TRACE(" %d: flags %x offset %d handler %x(%p) frame %x type %x %s\n",
183 j, catchblock[j].flags, catchblock[j].offset, catchblock[j].handler,
184 rva_to_ptr(catchblock[j].handler, image_base), catchblock[j].frame,
185 catchblock[j].type_info,
186 dbgstr_type_info(rva_to_ptr(catchblock[j].type_info, image_base)));
189 TRACE("ipmap: %x(%p) %d\n", descr->ipmap, ipmap, descr->ipmap_count);
190 for (i=0; i<descr->ipmap_count; i++)
192 TRACE(" %d: ip %x state %d\n", i, ipmap[i].ip, ipmap[i].state);
194 TRACE("unwind_help %d\n", descr->unwind_help);
195 if (descr->magic <= CXX_FRAME_MAGIC_VC6) return;
196 TRACE("expect list: %x\n", descr->expect_list);
197 if (descr->magic <= CXX_FRAME_MAGIC_VC7) return;
198 TRACE("flags: %08x\n", descr->flags);
201 static inline int ip_to_state(ipmap_info *ipmap, UINT count, int ip)
203 UINT low = 0, high = count-1, med;
205 while (low < high) {
206 med = low + (high-low)/2;
208 if (ipmap[med].ip <= ip && ipmap[med+1].ip > ip)
210 low = med;
211 break;
213 if (ipmap[med].ip < ip) low = med+1;
214 else high = med-1;
217 TRACE("%x -> %d\n", ip, ipmap[low].state);
218 return ipmap[low].state;
221 /* check if the exception type is caught by a given catch block, and return the type that matched */
222 static const cxx_type_info *find_caught_type(cxx_exception_type *exc_type, ULONG64 exc_base,
223 const type_info *catch_ti, UINT catch_flags)
225 const cxx_type_info_table *type_info_table = rva_to_ptr(exc_type->type_info_table, exc_base);
226 UINT i;
228 for (i = 0; i < type_info_table->count; i++)
230 const cxx_type_info *type = rva_to_ptr(type_info_table->info[i], exc_base);
231 const type_info *ti = rva_to_ptr(type->type_info, exc_base);
233 if (!catch_ti) return type; /* catch(...) matches any type */
234 if (catch_ti != ti)
236 if (strcmp( catch_ti->mangled, ti->mangled )) continue;
238 /* type is the same, now check the flags */
239 if ((exc_type->flags & TYPE_FLAG_CONST) &&
240 !(catch_flags & TYPE_FLAG_CONST)) continue;
241 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
242 !(catch_flags & TYPE_FLAG_VOLATILE)) continue;
243 return type; /* it matched */
245 return NULL;
248 static inline void copy_exception(void *object, ULONG64 frame,
249 DISPATCHER_CONTEXT *dispatch,
250 const catchblock_info *catchblock,
251 const cxx_type_info *type, ULONG64 exc_base)
253 const type_info *catch_ti = rva_to_ptr(catchblock->type_info, dispatch->ImageBase);
254 void **dest = rva_to_ptr(catchblock->offset, frame);
256 if (!catch_ti || !catch_ti->mangled[0]) return;
257 if (!catchblock->offset) return;
259 if (catchblock->flags & TYPE_FLAG_REFERENCE)
261 *dest = get_this_pointer(&type->offsets, object);
263 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
265 memmove(dest, object, type->size);
266 /* if it is a pointer, adjust it */
267 if (type->size == sizeof(void*)) *dest = get_this_pointer(&type->offsets, *dest);
269 else /* copy the object */
271 if (type->copy_ctor)
273 if (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS)
275 void (__cdecl *copy_ctor)(void*, void*, int) =
276 rva_to_ptr(type->copy_ctor, exc_base);
277 copy_ctor(dest, get_this_pointer(&type->offsets, object), 1);
279 else
281 void (__cdecl *copy_ctor)(void*, void*) =
282 rva_to_ptr(type->copy_ctor, exc_base);
283 copy_ctor(dest, get_this_pointer(&type->offsets, object));
286 else
287 memmove(dest, get_this_pointer(&type->offsets,object), type->size);
291 static void cxx_local_unwind(ULONG64 frame, DISPATCHER_CONTEXT *dispatch,
292 const cxx_function_descr *descr, int last_level)
294 const unwind_info *unwind_table = rva_to_ptr(descr->unwind_table, dispatch->ImageBase);
295 void (__cdecl *handler)(ULONG64 unk, ULONG64 rbp);
296 int *unwind_help = rva_to_ptr(descr->unwind_help, frame);
297 int trylevel;
299 if (unwind_help[0] == -2)
301 trylevel = ip_to_state(rva_to_ptr(descr->ipmap, dispatch->ImageBase),
302 descr->ipmap_count, dispatch->ControlPc-dispatch->ImageBase);
304 else
306 trylevel = unwind_help[0];
309 TRACE("current level: %d, last level: %d\n", trylevel, last_level);
310 while (trylevel > last_level)
312 if (trylevel<0 || trylevel>=descr->unwind_count)
314 ERR("invalid trylevel %d\n", trylevel);
315 MSVCRT_terminate();
317 handler = rva_to_ptr(unwind_table[trylevel].handler, dispatch->ImageBase);
318 if (handler)
320 TRACE("handler: %p\n", handler);
321 handler(0, frame);
323 trylevel = unwind_table[trylevel].prev;
325 unwind_help[0] = trylevel;
328 static LONG CALLBACK cxx_rethrow_filter(PEXCEPTION_POINTERS eptrs, void *c)
330 EXCEPTION_RECORD *rec = eptrs->ExceptionRecord;
331 thread_data_t *data = msvcrt_get_thread_data();
332 cxx_catch_ctx *ctx = c;
334 if (rec->ExceptionCode != CXX_EXCEPTION)
335 return EXCEPTION_CONTINUE_SEARCH;
336 if (!rec->ExceptionInformation[1] && !rec->ExceptionInformation[2])
337 return EXCEPTION_EXECUTE_HANDLER;
338 if (rec->ExceptionInformation[1] == data->exc_record->ExceptionInformation[1])
339 ctx->rethrow = TRUE;
340 return EXCEPTION_CONTINUE_SEARCH;
343 static void CALLBACK cxx_catch_cleanup(BOOL normal, void *c)
345 cxx_catch_ctx *ctx = c;
346 __CxxUnregisterExceptionObject(&ctx->frame_info, ctx->rethrow);
349 static void* WINAPI call_catch_block(EXCEPTION_RECORD *rec)
351 ULONG64 frame = rec->ExceptionInformation[1];
352 const cxx_function_descr *descr = (void*)rec->ExceptionInformation[2];
353 EXCEPTION_RECORD *prev_rec = (void*)rec->ExceptionInformation[4];
354 EXCEPTION_RECORD *untrans_rec = (void*)rec->ExceptionInformation[6];
355 void* (__cdecl *handler)(ULONG64 unk, ULONG64 rbp) = (void*)rec->ExceptionInformation[5];
356 int *unwind_help = rva_to_ptr(descr->unwind_help, frame);
357 cxx_catch_ctx ctx;
358 void *ret_addr = NULL;
360 TRACE("calling handler %p\n", handler);
362 ctx.rethrow = FALSE;
363 __CxxRegisterExceptionObject(&prev_rec, &ctx.frame_info);
364 __TRY
366 __TRY
368 ret_addr = handler(0, frame);
370 __EXCEPT_CTX(cxx_rethrow_filter, &ctx)
372 TRACE("detect rethrow: exception code: %x\n", prev_rec->ExceptionCode);
373 ctx.rethrow = TRUE;
375 if (untrans_rec)
377 __DestructExceptionObject(prev_rec);
378 RaiseException(untrans_rec->ExceptionCode, untrans_rec->ExceptionFlags,
379 untrans_rec->NumberParameters, untrans_rec->ExceptionInformation);
381 else
383 RaiseException(prev_rec->ExceptionCode, prev_rec->ExceptionFlags,
384 prev_rec->NumberParameters, prev_rec->ExceptionInformation);
387 __ENDTRY
389 __FINALLY_CTX(cxx_catch_cleanup, &ctx)
391 unwind_help[0] = -2;
392 unwind_help[1] = -1;
393 return ret_addr;
396 static inline BOOL cxx_is_consolidate(const EXCEPTION_RECORD *rec)
398 return rec->ExceptionCode==STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters==7 &&
399 rec->ExceptionInformation[0]==(ULONG_PTR)call_catch_block;
402 static inline void find_catch_block(EXCEPTION_RECORD *rec, EXCEPTION_RECORD *untrans_rec,
403 ULONG64 frame, DISPATCHER_CONTEXT *dispatch,
404 const cxx_function_descr *descr,
405 cxx_exception_type *info, ULONG64 orig_frame)
407 ULONG64 exc_base = (rec->NumberParameters == 4 ? rec->ExceptionInformation[3] : 0);
408 int trylevel = ip_to_state(rva_to_ptr(descr->ipmap, dispatch->ImageBase),
409 descr->ipmap_count, dispatch->ControlPc-dispatch->ImageBase);
410 const tryblock_info *in_catch;
411 EXCEPTION_RECORD catch_record;
412 CONTEXT context;
413 UINT i, j;
414 INT *unwind_help;
416 for (i=descr->tryblock_count; i>0; i--)
418 in_catch = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
419 in_catch = &in_catch[i-1];
421 if (trylevel>in_catch->end_level && trylevel<=in_catch->catch_level)
422 break;
424 if (!i)
425 in_catch = NULL;
427 unwind_help = rva_to_ptr(descr->unwind_help, orig_frame);
428 if (trylevel > unwind_help[1])
429 unwind_help[0] = unwind_help[1] = trylevel;
430 else
431 trylevel = unwind_help[1];
432 TRACE("current trylevel: %d\n", trylevel);
434 for (i=0; i<descr->tryblock_count; i++)
436 const tryblock_info *tryblock = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
437 tryblock = &tryblock[i];
439 if (trylevel < tryblock->start_level) continue;
440 if (trylevel > tryblock->end_level) continue;
442 if (in_catch)
444 if(tryblock->start_level <= in_catch->end_level) continue;
445 if(tryblock->end_level > in_catch->catch_level) continue;
448 /* got a try block */
449 for (j=0; j<tryblock->catchblock_count; j++)
451 const catchblock_info *catchblock = rva_to_ptr(tryblock->catchblock, dispatch->ImageBase);
452 catchblock = &catchblock[j];
454 if (info)
456 const cxx_type_info *type = find_caught_type(info, exc_base,
457 rva_to_ptr(catchblock->type_info, dispatch->ImageBase),
458 catchblock->flags);
459 if (!type) continue;
461 TRACE("matched type %p in tryblock %d catchblock %d\n", type, i, j);
463 /* copy the exception to its destination on the stack */
464 copy_exception((void*)rec->ExceptionInformation[1],
465 orig_frame, dispatch, catchblock, type, exc_base);
467 else
469 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
470 if (catchblock->type_info)
471 continue;
472 TRACE("found catch(...) block\n");
475 /* unwind stack and call catch */
476 memset(&catch_record, 0, sizeof(catch_record));
477 catch_record.ExceptionCode = STATUS_UNWIND_CONSOLIDATE;
478 catch_record.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
479 catch_record.NumberParameters = 7;
480 catch_record.ExceptionInformation[0] = (ULONG_PTR)call_catch_block;
481 catch_record.ExceptionInformation[1] = orig_frame;
482 catch_record.ExceptionInformation[2] = (ULONG_PTR)descr;
483 catch_record.ExceptionInformation[3] = tryblock->start_level;
484 catch_record.ExceptionInformation[4] = (ULONG_PTR)rec;
485 catch_record.ExceptionInformation[5] =
486 (ULONG_PTR)rva_to_ptr(catchblock->handler, dispatch->ImageBase);
487 catch_record.ExceptionInformation[6] = (ULONG_PTR)untrans_rec;
488 RtlUnwindEx((void*)frame, (void*)dispatch->ControlPc, &catch_record, NULL, &context, NULL);
492 TRACE("no matching catch block found\n");
495 static LONG CALLBACK se_translation_filter(EXCEPTION_POINTERS *ep, void *c)
497 se_translator_ctx *ctx = (se_translator_ctx *)c;
498 EXCEPTION_RECORD *rec = ep->ExceptionRecord;
499 cxx_exception_type *exc_type;
501 if (rec->ExceptionCode != CXX_EXCEPTION)
503 TRACE("non-c++ exception thrown in SEH handler: %x\n", rec->ExceptionCode);
504 MSVCRT_terminate();
507 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
508 find_catch_block(rec, ctx->seh_rec, ctx->dest_frame, ctx->dispatch,
509 ctx->descr, exc_type, ctx->orig_frame);
511 __DestructExceptionObject(rec);
512 return ExceptionContinueSearch;
515 static DWORD cxx_frame_handler(EXCEPTION_RECORD *rec, ULONG64 frame,
516 CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
517 const cxx_function_descr *descr)
519 int trylevel = ip_to_state(rva_to_ptr(descr->ipmap, dispatch->ImageBase),
520 descr->ipmap_count, dispatch->ControlPc-dispatch->ImageBase);
521 cxx_exception_type *exc_type;
522 ULONG64 orig_frame = frame;
523 ULONG64 throw_base;
524 DWORD throw_func_off;
525 void *throw_func;
526 UINT i, j;
527 int unwindlevel = -1;
529 if (descr->magic<CXX_FRAME_MAGIC_VC6 || descr->magic>CXX_FRAME_MAGIC_VC8)
531 FIXME("unhandled frame magic %x\n", descr->magic);
532 return ExceptionContinueSearch;
535 if (descr->magic >= CXX_FRAME_MAGIC_VC8 &&
536 (descr->flags & FUNC_DESCR_SYNCHRONOUS) &&
537 (rec->ExceptionCode != CXX_EXCEPTION &&
538 !cxx_is_consolidate(rec) &&
539 rec->ExceptionCode != STATUS_LONGJUMP))
540 return ExceptionContinueSearch; /* handle only c++ exceptions */
542 /* update orig_frame if it's a nested exception */
543 throw_func_off = RtlLookupFunctionEntry(dispatch->ControlPc, &throw_base, NULL)->BeginAddress;
544 throw_func = rva_to_ptr(throw_func_off, throw_base);
545 TRACE("reconstructed handler pointer: %p\n", throw_func);
546 for (i=descr->tryblock_count; i>0; i--)
548 const tryblock_info *tryblock = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
549 tryblock = &tryblock[i-1];
551 if (trylevel>tryblock->end_level && trylevel<=tryblock->catch_level)
553 for (j=0; j<tryblock->catchblock_count; j++)
555 const catchblock_info *catchblock = rva_to_ptr(tryblock->catchblock, dispatch->ImageBase);
556 catchblock = &catchblock[j];
558 if (rva_to_ptr(catchblock->handler, dispatch->ImageBase) == throw_func)
560 TRACE("nested exception detected\n");
561 unwindlevel = tryblock->end_level;
562 orig_frame = *(ULONG64*)rva_to_ptr(catchblock->frame, frame);
563 TRACE("setting orig_frame to %lx\n", orig_frame);
569 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
571 if (rec->ExceptionFlags & EH_TARGET_UNWIND)
572 cxx_local_unwind(orig_frame, dispatch, descr,
573 cxx_is_consolidate(rec) ? rec->ExceptionInformation[3] : trylevel);
574 else
575 cxx_local_unwind(orig_frame, dispatch, descr, unwindlevel);
576 return ExceptionContinueSearch;
578 if (!descr->tryblock_count) return ExceptionContinueSearch;
580 if (rec->ExceptionCode == CXX_EXCEPTION)
582 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
584 if (TRACE_ON(seh))
586 TRACE("handling C++ exception rec %p frame %lx descr %p\n", rec, frame, descr);
587 dump_exception_type(exc_type, rec->ExceptionInformation[3]);
588 dump_function_descr(descr, dispatch->ImageBase);
591 else
593 thread_data_t *data = msvcrt_get_thread_data();
595 exc_type = NULL;
596 TRACE("handling C exception code %x rec %p frame %lx descr %p\n",
597 rec->ExceptionCode, rec, frame, descr);
599 if (data->se_translator) {
600 EXCEPTION_POINTERS except_ptrs;
601 se_translator_ctx ctx;
603 ctx.dest_frame = frame;
604 ctx.orig_frame = orig_frame;
605 ctx.seh_rec = rec;
606 ctx.dispatch = dispatch;
607 ctx.descr = descr;
608 __TRY
610 except_ptrs.ExceptionRecord = rec;
611 except_ptrs.ContextRecord = context;
612 data->se_translator(rec->ExceptionCode, &except_ptrs);
614 __EXCEPT_CTX(se_translation_filter, &ctx)
617 __ENDTRY
621 find_catch_block(rec, NULL, frame, dispatch, descr, exc_type, orig_frame);
622 return ExceptionContinueSearch;
625 /*********************************************************************
626 * __CxxExceptionFilter (MSVCRT.@)
628 int CDECL __CxxExceptionFilter( PEXCEPTION_POINTERS ptrs,
629 const type_info *ti, int flags, void **copy )
631 FIXME( "%p %p %x %p: not implemented\n", ptrs, ti, flags, copy );
632 return EXCEPTION_CONTINUE_SEARCH;
635 /*********************************************************************
636 * __CxxFrameHandler (MSVCRT.@)
638 EXCEPTION_DISPOSITION CDECL __CxxFrameHandler( EXCEPTION_RECORD *rec, ULONG64 frame,
639 CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
641 TRACE( "%p %lx %p %p\n", rec, frame, context, dispatch );
642 return cxx_frame_handler( rec, frame, context, dispatch,
643 rva_to_ptr(*(UINT*)dispatch->HandlerData, dispatch->ImageBase) );
647 /*********************************************************************
648 * __CppXcptFilter (MSVCRT.@)
650 int CDECL __CppXcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
652 /* only filter c++ exceptions */
653 if (ex != CXX_EXCEPTION) return EXCEPTION_CONTINUE_SEARCH;
654 return _XcptFilter( ex, ptr );
658 /*********************************************************************
659 * __CxxDetectRethrow (MSVCRT.@)
661 BOOL CDECL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
663 PEXCEPTION_RECORD rec;
665 if (!ptrs)
666 return FALSE;
668 rec = ptrs->ExceptionRecord;
670 if (rec->ExceptionCode == CXX_EXCEPTION &&
671 rec->NumberParameters == 4 &&
672 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC_VC6 &&
673 rec->ExceptionInformation[2])
675 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
676 return TRUE;
678 return (msvcrt_get_thread_data()->exc_record == rec);
682 /*********************************************************************
683 * __CxxQueryExceptionSize (MSVCRT.@)
685 unsigned int CDECL __CxxQueryExceptionSize(void)
687 return sizeof(cxx_exception_type);
691 /*******************************************************************
692 * _setjmp (MSVCRT.@)
694 __ASM_GLOBAL_FUNC( MSVCRT__setjmp,
695 "jmp " __ASM_NAME("MSVCRT__setjmpex") );
697 /*******************************************************************
698 * _setjmpex (MSVCRT.@)
700 __ASM_GLOBAL_FUNC( MSVCRT__setjmpex,
701 "movq %rdx,(%rcx)\n\t" /* jmp_buf->Frame */
702 "movq %rbx,0x8(%rcx)\n\t" /* jmp_buf->Rbx */
703 "leaq 0x8(%rsp),%rax\n\t"
704 "movq %rax,0x10(%rcx)\n\t" /* jmp_buf->Rsp */
705 "movq %rbp,0x18(%rcx)\n\t" /* jmp_buf->Rbp */
706 "movq %rsi,0x20(%rcx)\n\t" /* jmp_buf->Rsi */
707 "movq %rdi,0x28(%rcx)\n\t" /* jmp_buf->Rdi */
708 "movq %r12,0x30(%rcx)\n\t" /* jmp_buf->R12 */
709 "movq %r13,0x38(%rcx)\n\t" /* jmp_buf->R13 */
710 "movq %r14,0x40(%rcx)\n\t" /* jmp_buf->R14 */
711 "movq %r15,0x48(%rcx)\n\t" /* jmp_buf->R15 */
712 "movq (%rsp),%rax\n\t"
713 "movq %rax,0x50(%rcx)\n\t" /* jmp_buf->Rip */
714 "movdqa %xmm6,0x60(%rcx)\n\t" /* jmp_buf->Xmm6 */
715 "movdqa %xmm7,0x70(%rcx)\n\t" /* jmp_buf->Xmm7 */
716 "movdqa %xmm8,0x80(%rcx)\n\t" /* jmp_buf->Xmm8 */
717 "movdqa %xmm9,0x90(%rcx)\n\t" /* jmp_buf->Xmm9 */
718 "movdqa %xmm10,0xa0(%rcx)\n\t" /* jmp_buf->Xmm10 */
719 "movdqa %xmm11,0xb0(%rcx)\n\t" /* jmp_buf->Xmm11 */
720 "movdqa %xmm12,0xc0(%rcx)\n\t" /* jmp_buf->Xmm12 */
721 "movdqa %xmm13,0xd0(%rcx)\n\t" /* jmp_buf->Xmm13 */
722 "movdqa %xmm14,0xe0(%rcx)\n\t" /* jmp_buf->Xmm14 */
723 "movdqa %xmm15,0xf0(%rcx)\n\t" /* jmp_buf->Xmm15 */
724 "xorq %rax,%rax\n\t"
725 "retq" );
728 extern void DECLSPEC_NORETURN CDECL longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
729 __ASM_GLOBAL_FUNC( longjmp_set_regs,
730 "movq %rdx,%rax\n\t" /* retval */
731 "movq 0x8(%rcx),%rbx\n\t" /* jmp_buf->Rbx */
732 "movq 0x18(%rcx),%rbp\n\t" /* jmp_buf->Rbp */
733 "movq 0x20(%rcx),%rsi\n\t" /* jmp_buf->Rsi */
734 "movq 0x28(%rcx),%rdi\n\t" /* jmp_buf->Rdi */
735 "movq 0x30(%rcx),%r12\n\t" /* jmp_buf->R12 */
736 "movq 0x38(%rcx),%r13\n\t" /* jmp_buf->R13 */
737 "movq 0x40(%rcx),%r14\n\t" /* jmp_buf->R14 */
738 "movq 0x48(%rcx),%r15\n\t" /* jmp_buf->R15 */
739 "movdqa 0x60(%rcx),%xmm6\n\t" /* jmp_buf->Xmm6 */
740 "movdqa 0x70(%rcx),%xmm7\n\t" /* jmp_buf->Xmm7 */
741 "movdqa 0x80(%rcx),%xmm8\n\t" /* jmp_buf->Xmm8 */
742 "movdqa 0x90(%rcx),%xmm9\n\t" /* jmp_buf->Xmm9 */
743 "movdqa 0xa0(%rcx),%xmm10\n\t" /* jmp_buf->Xmm10 */
744 "movdqa 0xb0(%rcx),%xmm11\n\t" /* jmp_buf->Xmm11 */
745 "movdqa 0xc0(%rcx),%xmm12\n\t" /* jmp_buf->Xmm12 */
746 "movdqa 0xd0(%rcx),%xmm13\n\t" /* jmp_buf->Xmm13 */
747 "movdqa 0xe0(%rcx),%xmm14\n\t" /* jmp_buf->Xmm14 */
748 "movdqa 0xf0(%rcx),%xmm15\n\t" /* jmp_buf->Xmm15 */
749 "movq 0x50(%rcx),%rdx\n\t" /* jmp_buf->Rip */
750 "movq 0x10(%rcx),%rsp\n\t" /* jmp_buf->Rsp */
751 "jmp *%rdx" );
753 /*******************************************************************
754 * longjmp (MSVCRT.@)
756 void __cdecl MSVCRT_longjmp( struct MSVCRT___JUMP_BUFFER *jmp, int retval )
758 EXCEPTION_RECORD rec;
760 if (!retval) retval = 1;
761 if (jmp->Frame)
763 rec.ExceptionCode = STATUS_LONGJUMP;
764 rec.ExceptionFlags = 0;
765 rec.ExceptionRecord = NULL;
766 rec.ExceptionAddress = NULL;
767 rec.NumberParameters = 1;
768 rec.ExceptionInformation[0] = (DWORD_PTR)jmp;
769 RtlUnwind( (void *)jmp->Frame, (void *)jmp->Rip, &rec, IntToPtr(retval) );
771 longjmp_set_regs( jmp, retval );
774 /*******************************************************************
775 * _local_unwind (MSVCRT.@)
777 void __cdecl _local_unwind( void *frame, void *target )
779 RtlUnwind( frame, target, NULL, 0 );
782 /*********************************************************************
783 * _fpieee_flt (MSVCRT.@)
785 int __cdecl _fpieee_flt(ULONG exception_code, EXCEPTION_POINTERS *ep,
786 int (__cdecl *handler)(_FPIEEE_RECORD*))
788 FIXME("(%x %p %p) opcode: %s\n", exception_code, ep, handler,
789 wine_dbgstr_longlong(*(ULONG64*)ep->ContextRecord->Rip));
790 return EXCEPTION_CONTINUE_SEARCH;
793 #endif /* __x86_64__ */