mf/session: Implement support for sinks that provide sample allocators.
[wine.git] / dlls / msvcrt / except_x86_64.c
blobb7e77f79494db553dd7660cd6d5544317b6947a5
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 typedef struct
44 int prev;
45 UINT handler;
46 } unwind_info;
48 typedef struct
50 UINT flags;
51 UINT type_info;
52 int offset;
53 UINT handler;
54 UINT frame;
55 } catchblock_info;
56 #define TYPE_FLAG_CONST 1
57 #define TYPE_FLAG_VOLATILE 2
58 #define TYPE_FLAG_REFERENCE 8
60 typedef struct
62 int start_level;
63 int end_level;
64 int catch_level;
65 int catchblock_count;
66 UINT catchblock;
67 } tryblock_info;
69 typedef struct
71 int ip;
72 int state;
73 } ipmap_info;
75 typedef struct __cxx_function_descr
77 UINT magic;
78 UINT unwind_count;
79 UINT unwind_table;
80 UINT tryblock_count;
81 UINT tryblock;
82 UINT ipmap_count;
83 UINT ipmap;
84 UINT unwind_help;
85 UINT expect_list;
86 UINT flags;
87 } cxx_function_descr;
89 typedef struct
91 cxx_frame_info frame_info;
92 BOOL rethrow;
93 EXCEPTION_RECORD *prev_rec;
94 } cxx_catch_ctx;
96 typedef struct
98 ULONG64 dest_frame;
99 ULONG64 orig_frame;
100 EXCEPTION_RECORD *seh_rec;
101 DISPATCHER_CONTEXT *dispatch;
102 const cxx_function_descr *descr;
103 } se_translator_ctx;
105 static inline void* rva_to_ptr(UINT rva, ULONG64 base)
107 return rva ? (void*)(base+rva) : NULL;
110 static inline void dump_type(UINT type_rva, ULONG64 base)
112 const cxx_type_info *type = rva_to_ptr(type_rva, base);
114 TRACE("flags %x type %x %s offsets %d,%d,%d size %d copy ctor %x(%p)\n",
115 type->flags, type->type_info, dbgstr_type_info(rva_to_ptr(type->type_info, base)),
116 type->offsets.this_offset, type->offsets.vbase_descr, type->offsets.vbase_offset,
117 type->size, type->copy_ctor, rva_to_ptr(type->copy_ctor, base));
120 static void dump_exception_type(const cxx_exception_type *type, ULONG64 base)
122 const cxx_type_info_table *type_info_table = rva_to_ptr(type->type_info_table, base);
123 UINT i;
125 TRACE("flags %x destr %x(%p) handler %x(%p) type info %x(%p)\n",
126 type->flags, type->destructor, rva_to_ptr(type->destructor, base),
127 type->custom_handler, rva_to_ptr(type->custom_handler, base),
128 type->type_info_table, type_info_table);
129 for (i = 0; i < type_info_table->count; i++)
131 TRACE(" %d: ", i);
132 dump_type(type_info_table->info[i], base);
136 static void dump_function_descr(const cxx_function_descr *descr, ULONG64 image_base)
138 unwind_info *unwind_table = rva_to_ptr(descr->unwind_table, image_base);
139 tryblock_info *tryblock = rva_to_ptr(descr->tryblock, image_base);
140 ipmap_info *ipmap = rva_to_ptr(descr->ipmap, image_base);
141 UINT i, j;
143 TRACE("magic %x\n", descr->magic);
144 TRACE("unwind table: %x(%p) %d\n", descr->unwind_table, unwind_table, descr->unwind_count);
145 for (i=0; i<descr->unwind_count; i++)
147 TRACE(" %d: prev %d func %x(%p)\n", i, unwind_table[i].prev,
148 unwind_table[i].handler, rva_to_ptr(unwind_table[i].handler, image_base));
150 TRACE("try table: %x(%p) %d\n", descr->tryblock, tryblock, descr->tryblock_count);
151 for (i=0; i<descr->tryblock_count; i++)
153 catchblock_info *catchblock = rva_to_ptr(tryblock[i].catchblock, image_base);
155 TRACE(" %d: start %d end %d catchlevel %d catch %x(%p) %d\n", i,
156 tryblock[i].start_level, tryblock[i].end_level,
157 tryblock[i].catch_level, tryblock[i].catchblock,
158 catchblock, tryblock[i].catchblock_count);
159 for (j=0; j<tryblock[i].catchblock_count; j++)
161 TRACE(" %d: flags %x offset %d handler %x(%p) frame %x type %x %s\n",
162 j, catchblock[j].flags, catchblock[j].offset, catchblock[j].handler,
163 rva_to_ptr(catchblock[j].handler, image_base), catchblock[j].frame,
164 catchblock[j].type_info,
165 dbgstr_type_info(rva_to_ptr(catchblock[j].type_info, image_base)));
168 TRACE("ipmap: %x(%p) %d\n", descr->ipmap, ipmap, descr->ipmap_count);
169 for (i=0; i<descr->ipmap_count; i++)
171 TRACE(" %d: ip %x state %d\n", i, ipmap[i].ip, ipmap[i].state);
173 TRACE("unwind_help %d\n", descr->unwind_help);
174 if (descr->magic <= CXX_FRAME_MAGIC_VC6) return;
175 TRACE("expect list: %x\n", descr->expect_list);
176 if (descr->magic <= CXX_FRAME_MAGIC_VC7) return;
177 TRACE("flags: %08x\n", descr->flags);
180 static inline int ip_to_state(ipmap_info *ipmap, UINT count, int ip)
182 UINT low = 0, high = count-1, med;
184 while (low < high) {
185 med = low + (high-low)/2;
187 if (ipmap[med].ip <= ip && ipmap[med+1].ip > ip)
189 low = med;
190 break;
192 if (ipmap[med].ip < ip) low = med+1;
193 else high = med-1;
196 TRACE("%x -> %d\n", ip, ipmap[low].state);
197 return ipmap[low].state;
200 /* check if the exception type is caught by a given catch block, and return the type that matched */
201 static const cxx_type_info *find_caught_type(cxx_exception_type *exc_type, ULONG64 exc_base,
202 const type_info *catch_ti, UINT catch_flags)
204 const cxx_type_info_table *type_info_table = rva_to_ptr(exc_type->type_info_table, exc_base);
205 UINT i;
207 for (i = 0; i < type_info_table->count; i++)
209 const cxx_type_info *type = rva_to_ptr(type_info_table->info[i], exc_base);
210 const type_info *ti = rva_to_ptr(type->type_info, exc_base);
212 if (!catch_ti) return type; /* catch(...) matches any type */
213 if (catch_ti != ti)
215 if (strcmp( catch_ti->mangled, ti->mangled )) continue;
217 /* type is the same, now check the flags */
218 if ((exc_type->flags & TYPE_FLAG_CONST) &&
219 !(catch_flags & TYPE_FLAG_CONST)) continue;
220 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
221 !(catch_flags & TYPE_FLAG_VOLATILE)) continue;
222 return type; /* it matched */
224 return NULL;
227 static inline void copy_exception(void *object, ULONG64 frame,
228 DISPATCHER_CONTEXT *dispatch,
229 const catchblock_info *catchblock,
230 const cxx_type_info *type, ULONG64 exc_base)
232 const type_info *catch_ti = rva_to_ptr(catchblock->type_info, dispatch->ImageBase);
233 void **dest = rva_to_ptr(catchblock->offset, frame);
235 if (!catch_ti || !catch_ti->mangled[0]) return;
236 if (!catchblock->offset) return;
238 if (catchblock->flags & TYPE_FLAG_REFERENCE)
240 *dest = get_this_pointer(&type->offsets, object);
242 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
244 memmove(dest, object, type->size);
245 /* if it is a pointer, adjust it */
246 if (type->size == sizeof(void*)) *dest = get_this_pointer(&type->offsets, *dest);
248 else /* copy the object */
250 if (type->copy_ctor)
252 if (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS)
254 void (__cdecl *copy_ctor)(void*, void*, int) =
255 rva_to_ptr(type->copy_ctor, exc_base);
256 copy_ctor(dest, get_this_pointer(&type->offsets, object), 1);
258 else
260 void (__cdecl *copy_ctor)(void*, void*) =
261 rva_to_ptr(type->copy_ctor, exc_base);
262 copy_ctor(dest, get_this_pointer(&type->offsets, object));
265 else
266 memmove(dest, get_this_pointer(&type->offsets,object), type->size);
270 static void cxx_local_unwind(ULONG64 frame, DISPATCHER_CONTEXT *dispatch,
271 const cxx_function_descr *descr, int last_level)
273 const unwind_info *unwind_table = rva_to_ptr(descr->unwind_table, dispatch->ImageBase);
274 void (__cdecl *handler)(ULONG64 unk, ULONG64 rbp);
275 int *unwind_help = rva_to_ptr(descr->unwind_help, frame);
276 int trylevel;
278 if (unwind_help[0] == -2)
280 trylevel = ip_to_state(rva_to_ptr(descr->ipmap, dispatch->ImageBase),
281 descr->ipmap_count, dispatch->ControlPc-dispatch->ImageBase);
283 else
285 trylevel = unwind_help[0];
288 TRACE("current level: %d, last level: %d\n", trylevel, last_level);
289 while (trylevel > last_level)
291 if (trylevel<0 || trylevel>=descr->unwind_count)
293 ERR("invalid trylevel %d\n", trylevel);
294 MSVCRT_terminate();
296 handler = rva_to_ptr(unwind_table[trylevel].handler, dispatch->ImageBase);
297 if (handler)
299 TRACE("handler: %p\n", handler);
300 handler(0, frame);
302 trylevel = unwind_table[trylevel].prev;
304 unwind_help[0] = trylevel;
307 static LONG CALLBACK cxx_rethrow_filter(PEXCEPTION_POINTERS eptrs, void *c)
309 EXCEPTION_RECORD *rec = eptrs->ExceptionRecord;
310 cxx_catch_ctx *ctx = c;
312 if (rec->ExceptionCode != CXX_EXCEPTION)
313 return EXCEPTION_CONTINUE_SEARCH;
314 if (!rec->ExceptionInformation[1] && !rec->ExceptionInformation[2])
315 return EXCEPTION_EXECUTE_HANDLER;
316 if (rec->ExceptionInformation[1] == ctx->prev_rec->ExceptionInformation[1])
317 ctx->rethrow = TRUE;
318 return EXCEPTION_CONTINUE_SEARCH;
321 static void CALLBACK cxx_catch_cleanup(BOOL normal, void *c)
323 cxx_catch_ctx *ctx = c;
324 __CxxUnregisterExceptionObject(&ctx->frame_info, ctx->rethrow);
327 static void* WINAPI call_catch_block(EXCEPTION_RECORD *rec)
329 ULONG64 frame = rec->ExceptionInformation[1];
330 const cxx_function_descr *descr = (void*)rec->ExceptionInformation[2];
331 EXCEPTION_RECORD *prev_rec = (void*)rec->ExceptionInformation[4];
332 EXCEPTION_RECORD *untrans_rec = (void*)rec->ExceptionInformation[6];
333 CONTEXT *context = (void*)rec->ExceptionInformation[7];
334 void* (__cdecl *handler)(ULONG64 unk, ULONG64 rbp) = (void*)rec->ExceptionInformation[5];
335 int *unwind_help = rva_to_ptr(descr->unwind_help, frame);
336 EXCEPTION_POINTERS ep = { prev_rec, context };
337 cxx_catch_ctx ctx;
338 void *ret_addr = NULL;
340 TRACE("calling handler %p\n", handler);
342 ctx.rethrow = FALSE;
343 ctx.prev_rec = prev_rec;
344 __CxxRegisterExceptionObject(&ep, &ctx.frame_info);
345 msvcrt_get_thread_data()->processing_throw--;
346 __TRY
348 __TRY
350 ret_addr = handler(0, frame);
352 __EXCEPT_CTX(cxx_rethrow_filter, &ctx)
354 TRACE("detect rethrow: exception code: %x\n", prev_rec->ExceptionCode);
355 ctx.rethrow = TRUE;
357 if (untrans_rec)
359 __DestructExceptionObject(prev_rec);
360 RaiseException(untrans_rec->ExceptionCode, untrans_rec->ExceptionFlags,
361 untrans_rec->NumberParameters, untrans_rec->ExceptionInformation);
363 else
365 RaiseException(prev_rec->ExceptionCode, prev_rec->ExceptionFlags,
366 prev_rec->NumberParameters, prev_rec->ExceptionInformation);
369 __ENDTRY
371 __FINALLY_CTX(cxx_catch_cleanup, &ctx)
373 unwind_help[0] = -2;
374 unwind_help[1] = -1;
375 return ret_addr;
378 static inline BOOL cxx_is_consolidate(const EXCEPTION_RECORD *rec)
380 return rec->ExceptionCode==STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters==8 &&
381 rec->ExceptionInformation[0]==(ULONG_PTR)call_catch_block;
384 static inline void find_catch_block(EXCEPTION_RECORD *rec, CONTEXT *context,
385 EXCEPTION_RECORD *untrans_rec,
386 ULONG64 frame, DISPATCHER_CONTEXT *dispatch,
387 const cxx_function_descr *descr,
388 cxx_exception_type *info, ULONG64 orig_frame)
390 ULONG64 exc_base = (rec->NumberParameters == 4 ? rec->ExceptionInformation[3] : 0);
391 int trylevel = ip_to_state(rva_to_ptr(descr->ipmap, dispatch->ImageBase),
392 descr->ipmap_count, dispatch->ControlPc-dispatch->ImageBase);
393 thread_data_t *data = msvcrt_get_thread_data();
394 const tryblock_info *in_catch;
395 EXCEPTION_RECORD catch_record;
396 CONTEXT ctx;
397 UINT i, j;
398 INT *unwind_help;
400 data->processing_throw++;
401 for (i=descr->tryblock_count; i>0; i--)
403 in_catch = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
404 in_catch = &in_catch[i-1];
406 if (trylevel>in_catch->end_level && trylevel<=in_catch->catch_level)
407 break;
409 if (!i)
410 in_catch = NULL;
412 unwind_help = rva_to_ptr(descr->unwind_help, orig_frame);
413 if (trylevel > unwind_help[1])
414 unwind_help[0] = unwind_help[1] = trylevel;
415 else
416 trylevel = unwind_help[1];
417 TRACE("current trylevel: %d\n", trylevel);
419 for (i=0; i<descr->tryblock_count; i++)
421 const tryblock_info *tryblock = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
422 tryblock = &tryblock[i];
424 if (trylevel < tryblock->start_level) continue;
425 if (trylevel > tryblock->end_level) continue;
427 if (in_catch)
429 if(tryblock->start_level <= in_catch->end_level) continue;
430 if(tryblock->end_level > in_catch->catch_level) continue;
433 /* got a try block */
434 for (j=0; j<tryblock->catchblock_count; j++)
436 const catchblock_info *catchblock = rva_to_ptr(tryblock->catchblock, dispatch->ImageBase);
437 catchblock = &catchblock[j];
439 if (info)
441 const cxx_type_info *type = find_caught_type(info, exc_base,
442 rva_to_ptr(catchblock->type_info, dispatch->ImageBase),
443 catchblock->flags);
444 if (!type) continue;
446 TRACE("matched type %p in tryblock %d catchblock %d\n", type, i, j);
448 /* copy the exception to its destination on the stack */
449 copy_exception((void*)rec->ExceptionInformation[1],
450 orig_frame, dispatch, catchblock, type, exc_base);
452 else
454 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
455 if (catchblock->type_info)
456 continue;
457 TRACE("found catch(...) block\n");
460 /* unwind stack and call catch */
461 memset(&catch_record, 0, sizeof(catch_record));
462 catch_record.ExceptionCode = STATUS_UNWIND_CONSOLIDATE;
463 catch_record.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
464 catch_record.NumberParameters = 8;
465 catch_record.ExceptionInformation[0] = (ULONG_PTR)call_catch_block;
466 catch_record.ExceptionInformation[1] = orig_frame;
467 catch_record.ExceptionInformation[2] = (ULONG_PTR)descr;
468 catch_record.ExceptionInformation[3] = tryblock->start_level;
469 catch_record.ExceptionInformation[4] = (ULONG_PTR)rec;
470 catch_record.ExceptionInformation[5] =
471 (ULONG_PTR)rva_to_ptr(catchblock->handler, dispatch->ImageBase);
472 catch_record.ExceptionInformation[6] = (ULONG_PTR)untrans_rec;
473 catch_record.ExceptionInformation[7] = (ULONG_PTR)context;
474 RtlUnwindEx((void*)frame, (void*)dispatch->ControlPc, &catch_record, NULL, &ctx, NULL);
478 TRACE("no matching catch block found\n");
479 data->processing_throw--;
482 static LONG CALLBACK se_translation_filter(EXCEPTION_POINTERS *ep, void *c)
484 se_translator_ctx *ctx = (se_translator_ctx *)c;
485 EXCEPTION_RECORD *rec = ep->ExceptionRecord;
486 cxx_exception_type *exc_type;
488 if (rec->ExceptionCode != CXX_EXCEPTION)
490 TRACE("non-c++ exception thrown in SEH handler: %x\n", rec->ExceptionCode);
491 MSVCRT_terminate();
494 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
495 find_catch_block(rec, ep->ContextRecord, ctx->seh_rec, ctx->dest_frame, ctx->dispatch,
496 ctx->descr, exc_type, ctx->orig_frame);
498 __DestructExceptionObject(rec);
499 return ExceptionContinueSearch;
502 static void check_noexcept( PEXCEPTION_RECORD rec,
503 const cxx_function_descr *descr, BOOL nested )
505 if (!nested && rec->ExceptionCode == CXX_EXCEPTION &&
506 descr->magic >= CXX_FRAME_MAGIC_VC8 &&
507 (descr->flags & FUNC_DESCR_NOEXCEPT))
509 ERR("noexcept function propagating exception\n");
510 MSVCRT_terminate();
514 static DWORD cxx_frame_handler(EXCEPTION_RECORD *rec, ULONG64 frame,
515 CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
516 const cxx_function_descr *descr)
518 int trylevel = ip_to_state(rva_to_ptr(descr->ipmap, dispatch->ImageBase),
519 descr->ipmap_count, dispatch->ControlPc-dispatch->ImageBase);
520 cxx_exception_type *exc_type;
521 ULONG64 orig_frame = frame;
522 ULONG64 throw_base;
523 DWORD throw_func_off;
524 void *throw_func;
525 UINT i, j;
526 int unwindlevel = -1;
528 if (descr->magic<CXX_FRAME_MAGIC_VC6 || descr->magic>CXX_FRAME_MAGIC_VC8)
530 FIXME("unhandled frame magic %x\n", descr->magic);
531 return ExceptionContinueSearch;
534 if (descr->magic >= CXX_FRAME_MAGIC_VC8 &&
535 (descr->flags & FUNC_DESCR_SYNCHRONOUS) &&
536 (rec->ExceptionCode != CXX_EXCEPTION &&
537 !cxx_is_consolidate(rec) &&
538 rec->ExceptionCode != STATUS_LONGJUMP))
539 return ExceptionContinueSearch; /* handle only c++ exceptions */
541 /* update orig_frame if it's a nested exception */
542 throw_func_off = RtlLookupFunctionEntry(dispatch->ControlPc, &throw_base, NULL)->BeginAddress;
543 throw_func = rva_to_ptr(throw_func_off, throw_base);
544 TRACE("reconstructed handler pointer: %p\n", throw_func);
545 for (i=descr->tryblock_count; i>0; i--)
547 const tryblock_info *tryblock = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
548 tryblock = &tryblock[i-1];
550 if (trylevel>tryblock->end_level && trylevel<=tryblock->catch_level)
552 for (j=0; j<tryblock->catchblock_count; j++)
554 const catchblock_info *catchblock = rva_to_ptr(tryblock->catchblock, dispatch->ImageBase);
555 catchblock = &catchblock[j];
557 if (rva_to_ptr(catchblock->handler, dispatch->ImageBase) == throw_func)
559 TRACE("nested exception detected\n");
560 unwindlevel = tryblock->end_level;
561 orig_frame = *(ULONG64*)rva_to_ptr(catchblock->frame, frame);
562 TRACE("setting orig_frame to %lx\n", orig_frame);
568 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
570 if (rec->ExceptionFlags & EH_TARGET_UNWIND)
571 cxx_local_unwind(orig_frame, dispatch, descr,
572 cxx_is_consolidate(rec) ? rec->ExceptionInformation[3] : trylevel);
573 else
574 cxx_local_unwind(orig_frame, dispatch, descr, unwindlevel);
575 return ExceptionContinueSearch;
577 if (!descr->tryblock_count)
579 check_noexcept(rec, descr, orig_frame != frame);
580 return ExceptionContinueSearch;
583 if (rec->ExceptionCode == CXX_EXCEPTION)
585 if (!rec->ExceptionInformation[1] && !rec->ExceptionInformation[2])
587 TRACE("rethrow detected.\n");
588 *rec = *msvcrt_get_thread_data()->exc_record;
591 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
593 if (TRACE_ON(seh))
595 TRACE("handling C++ exception rec %p frame %lx descr %p\n", rec, frame, descr);
596 dump_exception_type(exc_type, rec->ExceptionInformation[3]);
597 dump_function_descr(descr, dispatch->ImageBase);
600 else
602 thread_data_t *data = msvcrt_get_thread_data();
604 exc_type = NULL;
605 TRACE("handling C exception code %x rec %p frame %lx descr %p\n",
606 rec->ExceptionCode, rec, frame, descr);
608 if (data->se_translator) {
609 EXCEPTION_POINTERS except_ptrs;
610 se_translator_ctx ctx;
612 ctx.dest_frame = frame;
613 ctx.orig_frame = orig_frame;
614 ctx.seh_rec = rec;
615 ctx.dispatch = dispatch;
616 ctx.descr = descr;
617 __TRY
619 except_ptrs.ExceptionRecord = rec;
620 except_ptrs.ContextRecord = context;
621 data->se_translator(rec->ExceptionCode, &except_ptrs);
623 __EXCEPT_CTX(se_translation_filter, &ctx)
626 __ENDTRY
630 find_catch_block(rec, context, NULL, frame, dispatch, descr, exc_type, orig_frame);
631 check_noexcept(rec, descr, orig_frame != frame);
632 return ExceptionContinueSearch;
635 /*********************************************************************
636 * __CxxExceptionFilter (MSVCRT.@)
638 int CDECL __CxxExceptionFilter( PEXCEPTION_POINTERS ptrs,
639 const type_info *ti, int flags, void **copy )
641 FIXME( "%p %p %x %p: not implemented\n", ptrs, ti, flags, copy );
642 return EXCEPTION_CONTINUE_SEARCH;
645 /*********************************************************************
646 * __CxxFrameHandler (MSVCRT.@)
648 EXCEPTION_DISPOSITION CDECL __CxxFrameHandler( EXCEPTION_RECORD *rec, ULONG64 frame,
649 CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
651 TRACE( "%p %lx %p %p\n", rec, frame, context, dispatch );
652 return cxx_frame_handler( rec, frame, context, dispatch,
653 rva_to_ptr(*(UINT*)dispatch->HandlerData, dispatch->ImageBase) );
657 /*********************************************************************
658 * __CppXcptFilter (MSVCRT.@)
660 int CDECL __CppXcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
662 /* only filter c++ exceptions */
663 if (ex != CXX_EXCEPTION) return EXCEPTION_CONTINUE_SEARCH;
664 return _XcptFilter( ex, ptr );
668 /*********************************************************************
669 * __CxxDetectRethrow (MSVCRT.@)
671 BOOL CDECL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
673 PEXCEPTION_RECORD rec;
675 if (!ptrs)
676 return FALSE;
678 rec = ptrs->ExceptionRecord;
680 if (rec->ExceptionCode == CXX_EXCEPTION &&
681 rec->NumberParameters == 4 &&
682 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC_VC6 &&
683 rec->ExceptionInformation[2])
685 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
686 return TRUE;
688 return (msvcrt_get_thread_data()->exc_record == rec);
692 /*********************************************************************
693 * __CxxQueryExceptionSize (MSVCRT.@)
695 unsigned int CDECL __CxxQueryExceptionSize(void)
697 return sizeof(cxx_exception_type);
701 /*******************************************************************
702 * _setjmp (MSVCRT.@)
704 __ASM_GLOBAL_FUNC( MSVCRT__setjmp,
705 "jmp " __ASM_NAME("__wine_setjmpex") );
707 /*******************************************************************
708 * longjmp (MSVCRT.@)
710 void __cdecl MSVCRT_longjmp( struct MSVCRT___JUMP_BUFFER *jmp, int retval )
712 EXCEPTION_RECORD rec;
714 if (!retval) retval = 1;
715 if (jmp->Frame)
717 rec.ExceptionCode = STATUS_LONGJUMP;
718 rec.ExceptionFlags = 0;
719 rec.ExceptionRecord = NULL;
720 rec.ExceptionAddress = NULL;
721 rec.NumberParameters = 1;
722 rec.ExceptionInformation[0] = (DWORD_PTR)jmp;
723 RtlUnwind( (void *)jmp->Frame, (void *)jmp->Rip, &rec, IntToPtr(retval) );
725 __wine_longjmp( (__wine_jmp_buf *)jmp, retval );
728 /*******************************************************************
729 * _local_unwind (MSVCRT.@)
731 void __cdecl _local_unwind( void *frame, void *target )
733 RtlUnwind( frame, target, NULL, 0 );
736 /*********************************************************************
737 * _fpieee_flt (MSVCRT.@)
739 int __cdecl _fpieee_flt(ULONG exception_code, EXCEPTION_POINTERS *ep,
740 int (__cdecl *handler)(_FPIEEE_RECORD*))
742 FIXME("(%x %p %p) opcode: %s\n", exception_code, ep, handler,
743 wine_dbgstr_longlong(*(ULONG64*)ep->ContextRecord->Rip));
744 return EXCEPTION_CONTINUE_SEARCH;
747 #if _MSVCR_VER>=110 && _MSVCR_VER<=120
748 /*********************************************************************
749 * __crtCapturePreviousContext (MSVCR110.@)
751 void __cdecl get_prev_context(CONTEXT *ctx, DWORD64 rip)
753 ULONG64 frame, image_base;
754 RUNTIME_FUNCTION *rf;
755 void *data;
757 TRACE("(%p)\n", ctx);
759 rf = RtlLookupFunctionEntry(ctx->Rip, &image_base, NULL);
760 if(!rf) {
761 FIXME("RtlLookupFunctionEntry failed\n");
762 return;
765 RtlVirtualUnwind(UNW_FLAG_NHANDLER, image_base, ctx->Rip,
766 rf, ctx, &data, &frame, NULL);
769 __ASM_GLOBAL_FUNC( __crtCapturePreviousContext,
770 "movq %rcx,8(%rsp)\n\t"
771 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
772 "movq 8(%rsp),%rcx\n\t" /* context */
773 "leaq 8(%rsp),%rax\n\t"
774 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
775 "movq (%rsp),%rax\n\t"
776 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
777 "jmp " __ASM_NAME("get_prev_context") )
778 #endif
780 #endif /* __x86_64__ */