widl: Silence a gcc warning.
[wine.git] / dlls / msvcrt / except_x86_64.c
blob9cd19398a87aac937594a31990639591932a03a3
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 CONTEXT *context = (void*)rec->ExceptionInformation[7];
356 void* (__cdecl *handler)(ULONG64 unk, ULONG64 rbp) = (void*)rec->ExceptionInformation[5];
357 int *unwind_help = rva_to_ptr(descr->unwind_help, frame);
358 EXCEPTION_POINTERS ep = { prev_rec, context };
359 cxx_catch_ctx ctx;
360 void *ret_addr = NULL;
362 TRACE("calling handler %p\n", handler);
364 ctx.rethrow = FALSE;
365 __CxxRegisterExceptionObject(&ep, &ctx.frame_info);
366 msvcrt_get_thread_data()->processing_throw--;
367 __TRY
369 __TRY
371 ret_addr = handler(0, frame);
373 __EXCEPT_CTX(cxx_rethrow_filter, &ctx)
375 TRACE("detect rethrow: exception code: %x\n", prev_rec->ExceptionCode);
376 ctx.rethrow = TRUE;
378 if (untrans_rec)
380 __DestructExceptionObject(prev_rec);
381 RaiseException(untrans_rec->ExceptionCode, untrans_rec->ExceptionFlags,
382 untrans_rec->NumberParameters, untrans_rec->ExceptionInformation);
384 else
386 RaiseException(prev_rec->ExceptionCode, prev_rec->ExceptionFlags,
387 prev_rec->NumberParameters, prev_rec->ExceptionInformation);
390 __ENDTRY
392 __FINALLY_CTX(cxx_catch_cleanup, &ctx)
394 unwind_help[0] = -2;
395 unwind_help[1] = -1;
396 return ret_addr;
399 static inline BOOL cxx_is_consolidate(const EXCEPTION_RECORD *rec)
401 return rec->ExceptionCode==STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters==8 &&
402 rec->ExceptionInformation[0]==(ULONG_PTR)call_catch_block;
405 static inline void find_catch_block(EXCEPTION_RECORD *rec, CONTEXT *context,
406 EXCEPTION_RECORD *untrans_rec,
407 ULONG64 frame, DISPATCHER_CONTEXT *dispatch,
408 const cxx_function_descr *descr,
409 cxx_exception_type *info, ULONG64 orig_frame)
411 ULONG64 exc_base = (rec->NumberParameters == 4 ? rec->ExceptionInformation[3] : 0);
412 int trylevel = ip_to_state(rva_to_ptr(descr->ipmap, dispatch->ImageBase),
413 descr->ipmap_count, dispatch->ControlPc-dispatch->ImageBase);
414 thread_data_t *data = msvcrt_get_thread_data();
415 const tryblock_info *in_catch;
416 EXCEPTION_RECORD catch_record;
417 CONTEXT ctx;
418 UINT i, j;
419 INT *unwind_help;
421 data->processing_throw++;
422 for (i=descr->tryblock_count; i>0; i--)
424 in_catch = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
425 in_catch = &in_catch[i-1];
427 if (trylevel>in_catch->end_level && trylevel<=in_catch->catch_level)
428 break;
430 if (!i)
431 in_catch = NULL;
433 unwind_help = rva_to_ptr(descr->unwind_help, orig_frame);
434 if (trylevel > unwind_help[1])
435 unwind_help[0] = unwind_help[1] = trylevel;
436 else
437 trylevel = unwind_help[1];
438 TRACE("current trylevel: %d\n", trylevel);
440 for (i=0; i<descr->tryblock_count; i++)
442 const tryblock_info *tryblock = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
443 tryblock = &tryblock[i];
445 if (trylevel < tryblock->start_level) continue;
446 if (trylevel > tryblock->end_level) continue;
448 if (in_catch)
450 if(tryblock->start_level <= in_catch->end_level) continue;
451 if(tryblock->end_level > in_catch->catch_level) continue;
454 /* got a try block */
455 for (j=0; j<tryblock->catchblock_count; j++)
457 const catchblock_info *catchblock = rva_to_ptr(tryblock->catchblock, dispatch->ImageBase);
458 catchblock = &catchblock[j];
460 if (info)
462 const cxx_type_info *type = find_caught_type(info, exc_base,
463 rva_to_ptr(catchblock->type_info, dispatch->ImageBase),
464 catchblock->flags);
465 if (!type) continue;
467 TRACE("matched type %p in tryblock %d catchblock %d\n", type, i, j);
469 /* copy the exception to its destination on the stack */
470 copy_exception((void*)rec->ExceptionInformation[1],
471 orig_frame, dispatch, catchblock, type, exc_base);
473 else
475 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
476 if (catchblock->type_info)
477 continue;
478 TRACE("found catch(...) block\n");
481 /* unwind stack and call catch */
482 memset(&catch_record, 0, sizeof(catch_record));
483 catch_record.ExceptionCode = STATUS_UNWIND_CONSOLIDATE;
484 catch_record.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
485 catch_record.NumberParameters = 8;
486 catch_record.ExceptionInformation[0] = (ULONG_PTR)call_catch_block;
487 catch_record.ExceptionInformation[1] = orig_frame;
488 catch_record.ExceptionInformation[2] = (ULONG_PTR)descr;
489 catch_record.ExceptionInformation[3] = tryblock->start_level;
490 catch_record.ExceptionInformation[4] = (ULONG_PTR)rec;
491 catch_record.ExceptionInformation[5] =
492 (ULONG_PTR)rva_to_ptr(catchblock->handler, dispatch->ImageBase);
493 catch_record.ExceptionInformation[6] = (ULONG_PTR)untrans_rec;
494 catch_record.ExceptionInformation[7] = (ULONG_PTR)context;
495 RtlUnwindEx((void*)frame, (void*)dispatch->ControlPc, &catch_record, NULL, &ctx, NULL);
499 TRACE("no matching catch block found\n");
500 data->processing_throw--;
503 static LONG CALLBACK se_translation_filter(EXCEPTION_POINTERS *ep, void *c)
505 se_translator_ctx *ctx = (se_translator_ctx *)c;
506 EXCEPTION_RECORD *rec = ep->ExceptionRecord;
507 cxx_exception_type *exc_type;
509 if (rec->ExceptionCode != CXX_EXCEPTION)
511 TRACE("non-c++ exception thrown in SEH handler: %x\n", rec->ExceptionCode);
512 MSVCRT_terminate();
515 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
516 find_catch_block(rec, ep->ContextRecord, ctx->seh_rec, ctx->dest_frame, ctx->dispatch,
517 ctx->descr, exc_type, ctx->orig_frame);
519 __DestructExceptionObject(rec);
520 return ExceptionContinueSearch;
523 static DWORD cxx_frame_handler(EXCEPTION_RECORD *rec, ULONG64 frame,
524 CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
525 const cxx_function_descr *descr)
527 int trylevel = ip_to_state(rva_to_ptr(descr->ipmap, dispatch->ImageBase),
528 descr->ipmap_count, dispatch->ControlPc-dispatch->ImageBase);
529 cxx_exception_type *exc_type;
530 ULONG64 orig_frame = frame;
531 ULONG64 throw_base;
532 DWORD throw_func_off;
533 void *throw_func;
534 UINT i, j;
535 int unwindlevel = -1;
537 if (descr->magic<CXX_FRAME_MAGIC_VC6 || descr->magic>CXX_FRAME_MAGIC_VC8)
539 FIXME("unhandled frame magic %x\n", descr->magic);
540 return ExceptionContinueSearch;
543 if (descr->magic >= CXX_FRAME_MAGIC_VC8 &&
544 (descr->flags & FUNC_DESCR_SYNCHRONOUS) &&
545 (rec->ExceptionCode != CXX_EXCEPTION &&
546 !cxx_is_consolidate(rec) &&
547 rec->ExceptionCode != STATUS_LONGJUMP))
548 return ExceptionContinueSearch; /* handle only c++ exceptions */
550 /* update orig_frame if it's a nested exception */
551 throw_func_off = RtlLookupFunctionEntry(dispatch->ControlPc, &throw_base, NULL)->BeginAddress;
552 throw_func = rva_to_ptr(throw_func_off, throw_base);
553 TRACE("reconstructed handler pointer: %p\n", throw_func);
554 for (i=descr->tryblock_count; i>0; i--)
556 const tryblock_info *tryblock = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
557 tryblock = &tryblock[i-1];
559 if (trylevel>tryblock->end_level && trylevel<=tryblock->catch_level)
561 for (j=0; j<tryblock->catchblock_count; j++)
563 const catchblock_info *catchblock = rva_to_ptr(tryblock->catchblock, dispatch->ImageBase);
564 catchblock = &catchblock[j];
566 if (rva_to_ptr(catchblock->handler, dispatch->ImageBase) == throw_func)
568 TRACE("nested exception detected\n");
569 unwindlevel = tryblock->end_level;
570 orig_frame = *(ULONG64*)rva_to_ptr(catchblock->frame, frame);
571 TRACE("setting orig_frame to %lx\n", orig_frame);
577 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
579 if (rec->ExceptionFlags & EH_TARGET_UNWIND)
580 cxx_local_unwind(orig_frame, dispatch, descr,
581 cxx_is_consolidate(rec) ? rec->ExceptionInformation[3] : trylevel);
582 else
583 cxx_local_unwind(orig_frame, dispatch, descr, unwindlevel);
584 return ExceptionContinueSearch;
586 if (!descr->tryblock_count) return ExceptionContinueSearch;
588 if (rec->ExceptionCode == CXX_EXCEPTION)
590 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
592 if (TRACE_ON(seh))
594 TRACE("handling C++ exception rec %p frame %lx descr %p\n", rec, frame, descr);
595 dump_exception_type(exc_type, rec->ExceptionInformation[3]);
596 dump_function_descr(descr, dispatch->ImageBase);
599 else
601 thread_data_t *data = msvcrt_get_thread_data();
603 exc_type = NULL;
604 TRACE("handling C exception code %x rec %p frame %lx descr %p\n",
605 rec->ExceptionCode, rec, frame, descr);
607 if (data->se_translator) {
608 EXCEPTION_POINTERS except_ptrs;
609 se_translator_ctx ctx;
611 ctx.dest_frame = frame;
612 ctx.orig_frame = orig_frame;
613 ctx.seh_rec = rec;
614 ctx.dispatch = dispatch;
615 ctx.descr = descr;
616 __TRY
618 except_ptrs.ExceptionRecord = rec;
619 except_ptrs.ContextRecord = context;
620 data->se_translator(rec->ExceptionCode, &except_ptrs);
622 __EXCEPT_CTX(se_translation_filter, &ctx)
625 __ENDTRY
629 find_catch_block(rec, context, NULL, frame, dispatch, descr, exc_type, orig_frame);
630 return ExceptionContinueSearch;
633 /*********************************************************************
634 * __CxxExceptionFilter (MSVCRT.@)
636 int CDECL __CxxExceptionFilter( PEXCEPTION_POINTERS ptrs,
637 const type_info *ti, int flags, void **copy )
639 FIXME( "%p %p %x %p: not implemented\n", ptrs, ti, flags, copy );
640 return EXCEPTION_CONTINUE_SEARCH;
643 /*********************************************************************
644 * __CxxFrameHandler (MSVCRT.@)
646 EXCEPTION_DISPOSITION CDECL __CxxFrameHandler( EXCEPTION_RECORD *rec, ULONG64 frame,
647 CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
649 TRACE( "%p %lx %p %p\n", rec, frame, context, dispatch );
650 return cxx_frame_handler( rec, frame, context, dispatch,
651 rva_to_ptr(*(UINT*)dispatch->HandlerData, dispatch->ImageBase) );
655 /*********************************************************************
656 * __CppXcptFilter (MSVCRT.@)
658 int CDECL __CppXcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
660 /* only filter c++ exceptions */
661 if (ex != CXX_EXCEPTION) return EXCEPTION_CONTINUE_SEARCH;
662 return _XcptFilter( ex, ptr );
666 /*********************************************************************
667 * __CxxDetectRethrow (MSVCRT.@)
669 BOOL CDECL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
671 PEXCEPTION_RECORD rec;
673 if (!ptrs)
674 return FALSE;
676 rec = ptrs->ExceptionRecord;
678 if (rec->ExceptionCode == CXX_EXCEPTION &&
679 rec->NumberParameters == 4 &&
680 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC_VC6 &&
681 rec->ExceptionInformation[2])
683 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
684 return TRUE;
686 return (msvcrt_get_thread_data()->exc_record == rec);
690 /*********************************************************************
691 * __CxxQueryExceptionSize (MSVCRT.@)
693 unsigned int CDECL __CxxQueryExceptionSize(void)
695 return sizeof(cxx_exception_type);
699 /*******************************************************************
700 * _setjmp (MSVCRT.@)
702 __ASM_GLOBAL_FUNC( MSVCRT__setjmp,
703 "jmp " __ASM_NAME("MSVCRT__setjmpex") );
705 /*******************************************************************
706 * _setjmpex (MSVCRT.@)
708 __ASM_GLOBAL_FUNC( MSVCRT__setjmpex,
709 "movq %rdx,(%rcx)\n\t" /* jmp_buf->Frame */
710 "movq %rbx,0x8(%rcx)\n\t" /* jmp_buf->Rbx */
711 "leaq 0x8(%rsp),%rax\n\t"
712 "movq %rax,0x10(%rcx)\n\t" /* jmp_buf->Rsp */
713 "movq %rbp,0x18(%rcx)\n\t" /* jmp_buf->Rbp */
714 "movq %rsi,0x20(%rcx)\n\t" /* jmp_buf->Rsi */
715 "movq %rdi,0x28(%rcx)\n\t" /* jmp_buf->Rdi */
716 "movq %r12,0x30(%rcx)\n\t" /* jmp_buf->R12 */
717 "movq %r13,0x38(%rcx)\n\t" /* jmp_buf->R13 */
718 "movq %r14,0x40(%rcx)\n\t" /* jmp_buf->R14 */
719 "movq %r15,0x48(%rcx)\n\t" /* jmp_buf->R15 */
720 "movq (%rsp),%rax\n\t"
721 "movq %rax,0x50(%rcx)\n\t" /* jmp_buf->Rip */
722 "movdqa %xmm6,0x60(%rcx)\n\t" /* jmp_buf->Xmm6 */
723 "movdqa %xmm7,0x70(%rcx)\n\t" /* jmp_buf->Xmm7 */
724 "movdqa %xmm8,0x80(%rcx)\n\t" /* jmp_buf->Xmm8 */
725 "movdqa %xmm9,0x90(%rcx)\n\t" /* jmp_buf->Xmm9 */
726 "movdqa %xmm10,0xa0(%rcx)\n\t" /* jmp_buf->Xmm10 */
727 "movdqa %xmm11,0xb0(%rcx)\n\t" /* jmp_buf->Xmm11 */
728 "movdqa %xmm12,0xc0(%rcx)\n\t" /* jmp_buf->Xmm12 */
729 "movdqa %xmm13,0xd0(%rcx)\n\t" /* jmp_buf->Xmm13 */
730 "movdqa %xmm14,0xe0(%rcx)\n\t" /* jmp_buf->Xmm14 */
731 "movdqa %xmm15,0xf0(%rcx)\n\t" /* jmp_buf->Xmm15 */
732 "xorq %rax,%rax\n\t"
733 "retq" );
736 extern void DECLSPEC_NORETURN CDECL longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
737 __ASM_GLOBAL_FUNC( longjmp_set_regs,
738 "movq %rdx,%rax\n\t" /* retval */
739 "movq 0x8(%rcx),%rbx\n\t" /* jmp_buf->Rbx */
740 "movq 0x18(%rcx),%rbp\n\t" /* jmp_buf->Rbp */
741 "movq 0x20(%rcx),%rsi\n\t" /* jmp_buf->Rsi */
742 "movq 0x28(%rcx),%rdi\n\t" /* jmp_buf->Rdi */
743 "movq 0x30(%rcx),%r12\n\t" /* jmp_buf->R12 */
744 "movq 0x38(%rcx),%r13\n\t" /* jmp_buf->R13 */
745 "movq 0x40(%rcx),%r14\n\t" /* jmp_buf->R14 */
746 "movq 0x48(%rcx),%r15\n\t" /* jmp_buf->R15 */
747 "movdqa 0x60(%rcx),%xmm6\n\t" /* jmp_buf->Xmm6 */
748 "movdqa 0x70(%rcx),%xmm7\n\t" /* jmp_buf->Xmm7 */
749 "movdqa 0x80(%rcx),%xmm8\n\t" /* jmp_buf->Xmm8 */
750 "movdqa 0x90(%rcx),%xmm9\n\t" /* jmp_buf->Xmm9 */
751 "movdqa 0xa0(%rcx),%xmm10\n\t" /* jmp_buf->Xmm10 */
752 "movdqa 0xb0(%rcx),%xmm11\n\t" /* jmp_buf->Xmm11 */
753 "movdqa 0xc0(%rcx),%xmm12\n\t" /* jmp_buf->Xmm12 */
754 "movdqa 0xd0(%rcx),%xmm13\n\t" /* jmp_buf->Xmm13 */
755 "movdqa 0xe0(%rcx),%xmm14\n\t" /* jmp_buf->Xmm14 */
756 "movdqa 0xf0(%rcx),%xmm15\n\t" /* jmp_buf->Xmm15 */
757 "movq 0x50(%rcx),%rdx\n\t" /* jmp_buf->Rip */
758 "movq 0x10(%rcx),%rsp\n\t" /* jmp_buf->Rsp */
759 "jmp *%rdx" );
761 /*******************************************************************
762 * longjmp (MSVCRT.@)
764 void __cdecl MSVCRT_longjmp( struct MSVCRT___JUMP_BUFFER *jmp, int retval )
766 EXCEPTION_RECORD rec;
768 if (!retval) retval = 1;
769 if (jmp->Frame)
771 rec.ExceptionCode = STATUS_LONGJUMP;
772 rec.ExceptionFlags = 0;
773 rec.ExceptionRecord = NULL;
774 rec.ExceptionAddress = NULL;
775 rec.NumberParameters = 1;
776 rec.ExceptionInformation[0] = (DWORD_PTR)jmp;
777 RtlUnwind( (void *)jmp->Frame, (void *)jmp->Rip, &rec, IntToPtr(retval) );
779 longjmp_set_regs( jmp, retval );
782 /*******************************************************************
783 * _local_unwind (MSVCRT.@)
785 void __cdecl _local_unwind( void *frame, void *target )
787 RtlUnwind( frame, target, NULL, 0 );
790 /*********************************************************************
791 * _fpieee_flt (MSVCRT.@)
793 int __cdecl _fpieee_flt(ULONG exception_code, EXCEPTION_POINTERS *ep,
794 int (__cdecl *handler)(_FPIEEE_RECORD*))
796 FIXME("(%x %p %p) opcode: %s\n", exception_code, ep, handler,
797 wine_dbgstr_longlong(*(ULONG64*)ep->ContextRecord->Rip));
798 return EXCEPTION_CONTINUE_SEARCH;
801 #if _MSVCR_VER>=110 && _MSVCR_VER<=120
802 /*********************************************************************
803 * __crtCapturePreviousContext (MSVCR110.@)
805 void __cdecl get_prev_context(CONTEXT *ctx, DWORD64 rip)
807 ULONG64 frame, image_base;
808 RUNTIME_FUNCTION *rf;
809 void *data;
811 TRACE("(%p)\n", ctx);
813 ctx->Rip = rip;
814 ctx->Rsp += 3*8; /* Rip, Rcx, return address */
816 rf = RtlLookupFunctionEntry(ctx->Rip, &image_base, NULL);
817 if(!rf) {
818 FIXME("RtlLookupFunctionEntry failed\n");
819 return;
822 RtlVirtualUnwind(UNW_FLAG_NHANDLER, image_base, ctx->Rip,
823 rf, ctx, &data, &frame, NULL);
826 __ASM_GLOBAL_FUNC( __crtCapturePreviousContext,
827 "pushq (%rsp)\n\t" /* save Rip */
828 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
829 "pushq %rcx\n\t"
830 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
831 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
832 "popq %rcx\n\t"
833 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
834 "popq %rdx\n\t"
835 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
836 "jmp " __ASM_NAME("get_prev_context") );
837 #endif
839 #endif /* __x86_64__ */