gdi32: Try to parse font names without FreeType.
[wine/zf.git] / dlls / msvcrt / except_x86_64.c
bloba1a1bc14543193659c8ec5510a13a93df1da15b8
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 #ifdef __x86_64__
23 #include <stdarg.h>
24 #include <fpieee.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winternl.h"
31 #include "msvcrt.h"
32 #include "wine/exception.h"
33 #include "excpt.h"
34 #include "wine/debug.h"
36 #include "cppexcept.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(seh);
40 typedef struct
42 int prev;
43 UINT handler;
44 } unwind_info;
46 typedef struct
48 UINT flags;
49 UINT type_info;
50 int offset;
51 UINT handler;
52 UINT frame;
53 } catchblock_info;
54 #define TYPE_FLAG_CONST 1
55 #define TYPE_FLAG_VOLATILE 2
56 #define TYPE_FLAG_REFERENCE 8
58 typedef struct
60 int start_level;
61 int end_level;
62 int catch_level;
63 int catchblock_count;
64 UINT catchblock;
65 } tryblock_info;
67 typedef struct
69 int ip;
70 int state;
71 } ipmap_info;
73 typedef struct __cxx_function_descr
75 UINT magic;
76 UINT unwind_count;
77 UINT unwind_table;
78 UINT tryblock_count;
79 UINT tryblock;
80 UINT ipmap_count;
81 UINT ipmap;
82 UINT unwind_help;
83 UINT expect_list;
84 UINT flags;
85 } cxx_function_descr;
87 typedef struct
89 cxx_frame_info frame_info;
90 BOOL rethrow;
91 EXCEPTION_RECORD *prev_rec;
92 } cxx_catch_ctx;
94 typedef struct
96 ULONG64 dest_frame;
97 ULONG64 orig_frame;
98 EXCEPTION_RECORD *seh_rec;
99 DISPATCHER_CONTEXT *dispatch;
100 const cxx_function_descr *descr;
101 } se_translator_ctx;
103 static inline void* rva_to_ptr(UINT rva, ULONG64 base)
105 return rva ? (void*)(base+rva) : NULL;
108 static inline void dump_type(UINT type_rva, ULONG64 base)
110 const cxx_type_info *type = rva_to_ptr(type_rva, base);
112 TRACE("flags %x type %x %s offsets %d,%d,%d size %d copy ctor %x(%p)\n",
113 type->flags, type->type_info, dbgstr_type_info(rva_to_ptr(type->type_info, base)),
114 type->offsets.this_offset, type->offsets.vbase_descr, type->offsets.vbase_offset,
115 type->size, type->copy_ctor, rva_to_ptr(type->copy_ctor, base));
118 static void dump_exception_type(const cxx_exception_type *type, ULONG64 base)
120 const cxx_type_info_table *type_info_table = rva_to_ptr(type->type_info_table, base);
121 UINT i;
123 TRACE("flags %x destr %x(%p) handler %x(%p) type info %x(%p)\n",
124 type->flags, type->destructor, rva_to_ptr(type->destructor, base),
125 type->custom_handler, rva_to_ptr(type->custom_handler, base),
126 type->type_info_table, type_info_table);
127 for (i = 0; i < type_info_table->count; i++)
129 TRACE(" %d: ", i);
130 dump_type(type_info_table->info[i], base);
134 static void dump_function_descr(const cxx_function_descr *descr, ULONG64 image_base)
136 unwind_info *unwind_table = rva_to_ptr(descr->unwind_table, image_base);
137 tryblock_info *tryblock = rva_to_ptr(descr->tryblock, image_base);
138 ipmap_info *ipmap = rva_to_ptr(descr->ipmap, image_base);
139 UINT i, j;
141 TRACE("magic %x\n", descr->magic);
142 TRACE("unwind table: %x(%p) %d\n", descr->unwind_table, unwind_table, descr->unwind_count);
143 for (i=0; i<descr->unwind_count; i++)
145 TRACE(" %d: prev %d func %x(%p)\n", i, unwind_table[i].prev,
146 unwind_table[i].handler, rva_to_ptr(unwind_table[i].handler, image_base));
148 TRACE("try table: %x(%p) %d\n", descr->tryblock, tryblock, descr->tryblock_count);
149 for (i=0; i<descr->tryblock_count; i++)
151 catchblock_info *catchblock = rva_to_ptr(tryblock[i].catchblock, image_base);
153 TRACE(" %d: start %d end %d catchlevel %d catch %x(%p) %d\n", i,
154 tryblock[i].start_level, tryblock[i].end_level,
155 tryblock[i].catch_level, tryblock[i].catchblock,
156 catchblock, tryblock[i].catchblock_count);
157 for (j=0; j<tryblock[i].catchblock_count; j++)
159 TRACE(" %d: flags %x offset %d handler %x(%p) frame %x type %x %s\n",
160 j, catchblock[j].flags, catchblock[j].offset, catchblock[j].handler,
161 rva_to_ptr(catchblock[j].handler, image_base), catchblock[j].frame,
162 catchblock[j].type_info,
163 dbgstr_type_info(rva_to_ptr(catchblock[j].type_info, image_base)));
166 TRACE("ipmap: %x(%p) %d\n", descr->ipmap, ipmap, descr->ipmap_count);
167 for (i=0; i<descr->ipmap_count; i++)
169 TRACE(" %d: ip %x state %d\n", i, ipmap[i].ip, ipmap[i].state);
171 TRACE("unwind_help %d\n", descr->unwind_help);
172 if (descr->magic <= CXX_FRAME_MAGIC_VC6) return;
173 TRACE("expect list: %x\n", descr->expect_list);
174 if (descr->magic <= CXX_FRAME_MAGIC_VC7) return;
175 TRACE("flags: %08x\n", descr->flags);
178 static inline int ip_to_state(ipmap_info *ipmap, UINT count, int ip)
180 UINT low = 0, high = count-1, med;
182 while (low < high) {
183 med = low + (high-low)/2;
185 if (ipmap[med].ip <= ip && ipmap[med+1].ip > ip)
187 low = med;
188 break;
190 if (ipmap[med].ip < ip) low = med+1;
191 else high = med-1;
194 TRACE("%x -> %d\n", ip, ipmap[low].state);
195 return ipmap[low].state;
198 /* check if the exception type is caught by a given catch block, and return the type that matched */
199 static const cxx_type_info *find_caught_type(cxx_exception_type *exc_type, ULONG64 exc_base,
200 const type_info *catch_ti, UINT catch_flags)
202 const cxx_type_info_table *type_info_table = rva_to_ptr(exc_type->type_info_table, exc_base);
203 UINT i;
205 for (i = 0; i < type_info_table->count; i++)
207 const cxx_type_info *type = rva_to_ptr(type_info_table->info[i], exc_base);
208 const type_info *ti = rva_to_ptr(type->type_info, exc_base);
210 if (!catch_ti) return type; /* catch(...) matches any type */
211 if (catch_ti != ti)
213 if (strcmp( catch_ti->mangled, ti->mangled )) continue;
215 /* type is the same, now check the flags */
216 if ((exc_type->flags & TYPE_FLAG_CONST) &&
217 !(catch_flags & TYPE_FLAG_CONST)) continue;
218 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
219 !(catch_flags & TYPE_FLAG_VOLATILE)) continue;
220 return type; /* it matched */
222 return NULL;
225 static inline void copy_exception(void *object, ULONG64 frame,
226 DISPATCHER_CONTEXT *dispatch,
227 const catchblock_info *catchblock,
228 const cxx_type_info *type, ULONG64 exc_base)
230 const type_info *catch_ti = rva_to_ptr(catchblock->type_info, dispatch->ImageBase);
231 void **dest = rva_to_ptr(catchblock->offset, frame);
233 if (!catch_ti || !catch_ti->mangled[0]) return;
234 if (!catchblock->offset) return;
236 if (catchblock->flags & TYPE_FLAG_REFERENCE)
238 *dest = get_this_pointer(&type->offsets, object);
240 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
242 memmove(dest, object, type->size);
243 /* if it is a pointer, adjust it */
244 if (type->size == sizeof(void*)) *dest = get_this_pointer(&type->offsets, *dest);
246 else /* copy the object */
248 if (type->copy_ctor)
250 if (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS)
252 void (__cdecl *copy_ctor)(void*, void*, int) =
253 rva_to_ptr(type->copy_ctor, exc_base);
254 copy_ctor(dest, get_this_pointer(&type->offsets, object), 1);
256 else
258 void (__cdecl *copy_ctor)(void*, void*) =
259 rva_to_ptr(type->copy_ctor, exc_base);
260 copy_ctor(dest, get_this_pointer(&type->offsets, object));
263 else
264 memmove(dest, get_this_pointer(&type->offsets,object), type->size);
268 static void cxx_local_unwind(ULONG64 frame, DISPATCHER_CONTEXT *dispatch,
269 const cxx_function_descr *descr, int last_level)
271 const unwind_info *unwind_table = rva_to_ptr(descr->unwind_table, dispatch->ImageBase);
272 void (__cdecl *handler)(ULONG64 unk, ULONG64 rbp);
273 int *unwind_help = rva_to_ptr(descr->unwind_help, frame);
274 int trylevel;
276 if (unwind_help[0] == -2)
278 trylevel = ip_to_state(rva_to_ptr(descr->ipmap, dispatch->ImageBase),
279 descr->ipmap_count, dispatch->ControlPc-dispatch->ImageBase);
281 else
283 trylevel = unwind_help[0];
286 TRACE("current level: %d, last level: %d\n", trylevel, last_level);
287 while (trylevel > last_level)
289 if (trylevel<0 || trylevel>=descr->unwind_count)
291 ERR("invalid trylevel %d\n", trylevel);
292 terminate();
294 handler = rva_to_ptr(unwind_table[trylevel].handler, dispatch->ImageBase);
295 if (handler)
297 TRACE("handler: %p\n", handler);
298 handler(0, frame);
300 trylevel = unwind_table[trylevel].prev;
302 unwind_help[0] = trylevel;
305 static LONG CALLBACK cxx_rethrow_filter(PEXCEPTION_POINTERS eptrs, void *c)
307 EXCEPTION_RECORD *rec = eptrs->ExceptionRecord;
308 cxx_catch_ctx *ctx = c;
310 if (rec->ExceptionCode != CXX_EXCEPTION)
311 return EXCEPTION_CONTINUE_SEARCH;
312 if (!rec->ExceptionInformation[1] && !rec->ExceptionInformation[2])
313 return EXCEPTION_EXECUTE_HANDLER;
314 if (rec->ExceptionInformation[1] == ctx->prev_rec->ExceptionInformation[1])
315 ctx->rethrow = TRUE;
316 return EXCEPTION_CONTINUE_SEARCH;
319 static void CALLBACK cxx_catch_cleanup(BOOL normal, void *c)
321 cxx_catch_ctx *ctx = c;
322 __CxxUnregisterExceptionObject(&ctx->frame_info, ctx->rethrow);
325 static void* WINAPI call_catch_block(EXCEPTION_RECORD *rec)
327 ULONG64 frame = rec->ExceptionInformation[1];
328 const cxx_function_descr *descr = (void*)rec->ExceptionInformation[2];
329 EXCEPTION_RECORD *prev_rec = (void*)rec->ExceptionInformation[4];
330 EXCEPTION_RECORD *untrans_rec = (void*)rec->ExceptionInformation[6];
331 CONTEXT *context = (void*)rec->ExceptionInformation[7];
332 void* (__cdecl *handler)(ULONG64 unk, ULONG64 rbp) = (void*)rec->ExceptionInformation[5];
333 int *unwind_help = rva_to_ptr(descr->unwind_help, frame);
334 EXCEPTION_POINTERS ep = { prev_rec, context };
335 cxx_catch_ctx ctx;
336 void *ret_addr = NULL;
338 TRACE("calling handler %p\n", handler);
340 ctx.rethrow = FALSE;
341 ctx.prev_rec = prev_rec;
342 __CxxRegisterExceptionObject(&ep, &ctx.frame_info);
343 msvcrt_get_thread_data()->processing_throw--;
344 __TRY
346 __TRY
348 ret_addr = handler(0, frame);
350 __EXCEPT_CTX(cxx_rethrow_filter, &ctx)
352 TRACE("detect rethrow: exception code: %x\n", prev_rec->ExceptionCode);
353 ctx.rethrow = TRUE;
355 if (untrans_rec)
357 __DestructExceptionObject(prev_rec);
358 RaiseException(untrans_rec->ExceptionCode, untrans_rec->ExceptionFlags,
359 untrans_rec->NumberParameters, untrans_rec->ExceptionInformation);
361 else
363 RaiseException(prev_rec->ExceptionCode, prev_rec->ExceptionFlags,
364 prev_rec->NumberParameters, prev_rec->ExceptionInformation);
367 __ENDTRY
369 __FINALLY_CTX(cxx_catch_cleanup, &ctx)
371 unwind_help[0] = -2;
372 unwind_help[1] = -1;
373 return ret_addr;
376 static inline BOOL cxx_is_consolidate(const EXCEPTION_RECORD *rec)
378 return rec->ExceptionCode==STATUS_UNWIND_CONSOLIDATE && rec->NumberParameters==8 &&
379 rec->ExceptionInformation[0]==(ULONG_PTR)call_catch_block;
382 static inline void find_catch_block(EXCEPTION_RECORD *rec, CONTEXT *context,
383 EXCEPTION_RECORD *untrans_rec,
384 ULONG64 frame, DISPATCHER_CONTEXT *dispatch,
385 const cxx_function_descr *descr,
386 cxx_exception_type *info, ULONG64 orig_frame)
388 ULONG64 exc_base = (rec->NumberParameters == 4 ? rec->ExceptionInformation[3] : 0);
389 int trylevel = ip_to_state(rva_to_ptr(descr->ipmap, dispatch->ImageBase),
390 descr->ipmap_count, dispatch->ControlPc-dispatch->ImageBase);
391 thread_data_t *data = msvcrt_get_thread_data();
392 const tryblock_info *in_catch;
393 EXCEPTION_RECORD catch_record;
394 CONTEXT ctx;
395 UINT i, j;
396 INT *unwind_help;
398 data->processing_throw++;
399 for (i=descr->tryblock_count; i>0; i--)
401 in_catch = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
402 in_catch = &in_catch[i-1];
404 if (trylevel>in_catch->end_level && trylevel<=in_catch->catch_level)
405 break;
407 if (!i)
408 in_catch = NULL;
410 unwind_help = rva_to_ptr(descr->unwind_help, orig_frame);
411 if (trylevel > unwind_help[1])
412 unwind_help[0] = unwind_help[1] = trylevel;
413 else
414 trylevel = unwind_help[1];
415 TRACE("current trylevel: %d\n", trylevel);
417 for (i=0; i<descr->tryblock_count; i++)
419 const tryblock_info *tryblock = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
420 tryblock = &tryblock[i];
422 if (trylevel < tryblock->start_level) continue;
423 if (trylevel > tryblock->end_level) continue;
425 if (in_catch)
427 if(tryblock->start_level <= in_catch->end_level) continue;
428 if(tryblock->end_level > in_catch->catch_level) continue;
431 /* got a try block */
432 for (j=0; j<tryblock->catchblock_count; j++)
434 const catchblock_info *catchblock = rva_to_ptr(tryblock->catchblock, dispatch->ImageBase);
435 catchblock = &catchblock[j];
437 if (info)
439 const cxx_type_info *type = find_caught_type(info, exc_base,
440 rva_to_ptr(catchblock->type_info, dispatch->ImageBase),
441 catchblock->flags);
442 if (!type) continue;
444 TRACE("matched type %p in tryblock %d catchblock %d\n", type, i, j);
446 /* copy the exception to its destination on the stack */
447 copy_exception((void*)rec->ExceptionInformation[1],
448 orig_frame, dispatch, catchblock, type, exc_base);
450 else
452 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
453 if (catchblock->type_info)
454 continue;
455 TRACE("found catch(...) block\n");
458 /* unwind stack and call catch */
459 memset(&catch_record, 0, sizeof(catch_record));
460 catch_record.ExceptionCode = STATUS_UNWIND_CONSOLIDATE;
461 catch_record.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
462 catch_record.NumberParameters = 8;
463 catch_record.ExceptionInformation[0] = (ULONG_PTR)call_catch_block;
464 catch_record.ExceptionInformation[1] = orig_frame;
465 catch_record.ExceptionInformation[2] = (ULONG_PTR)descr;
466 catch_record.ExceptionInformation[3] = tryblock->start_level;
467 catch_record.ExceptionInformation[4] = (ULONG_PTR)rec;
468 catch_record.ExceptionInformation[5] =
469 (ULONG_PTR)rva_to_ptr(catchblock->handler, dispatch->ImageBase);
470 catch_record.ExceptionInformation[6] = (ULONG_PTR)untrans_rec;
471 catch_record.ExceptionInformation[7] = (ULONG_PTR)context;
472 RtlUnwindEx((void*)frame, (void*)dispatch->ControlPc, &catch_record, NULL, &ctx, NULL);
476 TRACE("no matching catch block found\n");
477 data->processing_throw--;
480 static LONG CALLBACK se_translation_filter(EXCEPTION_POINTERS *ep, void *c)
482 se_translator_ctx *ctx = (se_translator_ctx *)c;
483 EXCEPTION_RECORD *rec = ep->ExceptionRecord;
484 cxx_exception_type *exc_type;
486 if (rec->ExceptionCode != CXX_EXCEPTION)
488 TRACE("non-c++ exception thrown in SEH handler: %x\n", rec->ExceptionCode);
489 terminate();
492 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
493 find_catch_block(rec, ep->ContextRecord, ctx->seh_rec, ctx->dest_frame, ctx->dispatch,
494 ctx->descr, exc_type, ctx->orig_frame);
496 __DestructExceptionObject(rec);
497 return ExceptionContinueSearch;
500 static void check_noexcept( PEXCEPTION_RECORD rec,
501 const cxx_function_descr *descr, BOOL nested )
503 if (!nested && rec->ExceptionCode == CXX_EXCEPTION &&
504 descr->magic >= CXX_FRAME_MAGIC_VC8 &&
505 (descr->flags & FUNC_DESCR_NOEXCEPT))
507 ERR("noexcept function propagating exception\n");
508 terminate();
512 static DWORD cxx_frame_handler(EXCEPTION_RECORD *rec, ULONG64 frame,
513 CONTEXT *context, DISPATCHER_CONTEXT *dispatch,
514 const cxx_function_descr *descr)
516 int trylevel = ip_to_state(rva_to_ptr(descr->ipmap, dispatch->ImageBase),
517 descr->ipmap_count, dispatch->ControlPc-dispatch->ImageBase);
518 cxx_exception_type *exc_type;
519 ULONG64 orig_frame = frame;
520 ULONG64 throw_base;
521 DWORD throw_func_off;
522 void *throw_func;
523 UINT i, j;
524 int unwindlevel = -1;
526 if (descr->magic<CXX_FRAME_MAGIC_VC6 || descr->magic>CXX_FRAME_MAGIC_VC8)
528 FIXME("unhandled frame magic %x\n", descr->magic);
529 return ExceptionContinueSearch;
532 if (descr->magic >= CXX_FRAME_MAGIC_VC8 &&
533 (descr->flags & FUNC_DESCR_SYNCHRONOUS) &&
534 (rec->ExceptionCode != CXX_EXCEPTION &&
535 !cxx_is_consolidate(rec) &&
536 rec->ExceptionCode != STATUS_LONGJUMP))
537 return ExceptionContinueSearch; /* handle only c++ exceptions */
539 /* update orig_frame if it's a nested exception */
540 throw_func_off = RtlLookupFunctionEntry(dispatch->ControlPc, &throw_base, NULL)->BeginAddress;
541 throw_func = rva_to_ptr(throw_func_off, throw_base);
542 TRACE("reconstructed handler pointer: %p\n", throw_func);
543 for (i=descr->tryblock_count; i>0; i--)
545 const tryblock_info *tryblock = rva_to_ptr(descr->tryblock, dispatch->ImageBase);
546 tryblock = &tryblock[i-1];
548 if (trylevel>tryblock->end_level && trylevel<=tryblock->catch_level)
550 for (j=0; j<tryblock->catchblock_count; j++)
552 const catchblock_info *catchblock = rva_to_ptr(tryblock->catchblock, dispatch->ImageBase);
553 catchblock = &catchblock[j];
555 if (rva_to_ptr(catchblock->handler, dispatch->ImageBase) == throw_func)
557 TRACE("nested exception detected\n");
558 unwindlevel = tryblock->end_level;
559 orig_frame = *(ULONG64*)rva_to_ptr(catchblock->frame, frame);
560 TRACE("setting orig_frame to %lx\n", orig_frame);
566 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
568 if (rec->ExceptionFlags & EH_TARGET_UNWIND)
569 cxx_local_unwind(orig_frame, dispatch, descr,
570 cxx_is_consolidate(rec) ? rec->ExceptionInformation[3] : trylevel);
571 else
572 cxx_local_unwind(orig_frame, dispatch, descr, unwindlevel);
573 return ExceptionContinueSearch;
575 if (!descr->tryblock_count)
577 check_noexcept(rec, descr, orig_frame != frame);
578 return ExceptionContinueSearch;
581 if (rec->ExceptionCode == CXX_EXCEPTION)
583 if (!rec->ExceptionInformation[1] && !rec->ExceptionInformation[2])
585 TRACE("rethrow detected.\n");
586 *rec = *msvcrt_get_thread_data()->exc_record;
589 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
591 if (TRACE_ON(seh))
593 TRACE("handling C++ exception rec %p frame %lx descr %p\n", rec, frame, descr);
594 dump_exception_type(exc_type, rec->ExceptionInformation[3]);
595 dump_function_descr(descr, dispatch->ImageBase);
598 else
600 thread_data_t *data = msvcrt_get_thread_data();
602 exc_type = NULL;
603 TRACE("handling C exception code %x rec %p frame %lx descr %p\n",
604 rec->ExceptionCode, rec, frame, descr);
606 if (data->se_translator) {
607 EXCEPTION_POINTERS except_ptrs;
608 se_translator_ctx ctx;
610 ctx.dest_frame = frame;
611 ctx.orig_frame = orig_frame;
612 ctx.seh_rec = rec;
613 ctx.dispatch = dispatch;
614 ctx.descr = descr;
615 __TRY
617 except_ptrs.ExceptionRecord = rec;
618 except_ptrs.ContextRecord = context;
619 data->se_translator(rec->ExceptionCode, &except_ptrs);
621 __EXCEPT_CTX(se_translation_filter, &ctx)
624 __ENDTRY
628 find_catch_block(rec, context, NULL, frame, dispatch, descr, exc_type, orig_frame);
629 check_noexcept(rec, descr, orig_frame != 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("__wine_setjmpex") );
705 /*******************************************************************
706 * longjmp (MSVCRT.@)
708 void __cdecl MSVCRT_longjmp( struct MSVCRT___JUMP_BUFFER *jmp, int retval )
710 EXCEPTION_RECORD rec;
712 if (!retval) retval = 1;
713 if (jmp->Frame)
715 rec.ExceptionCode = STATUS_LONGJUMP;
716 rec.ExceptionFlags = 0;
717 rec.ExceptionRecord = NULL;
718 rec.ExceptionAddress = NULL;
719 rec.NumberParameters = 1;
720 rec.ExceptionInformation[0] = (DWORD_PTR)jmp;
721 RtlUnwind( (void *)jmp->Frame, (void *)jmp->Rip, &rec, IntToPtr(retval) );
723 __wine_longjmp( (__wine_jmp_buf *)jmp, retval );
726 /*******************************************************************
727 * _local_unwind (MSVCRT.@)
729 void __cdecl _local_unwind( void *frame, void *target )
731 RtlUnwind( frame, target, NULL, 0 );
734 /*********************************************************************
735 * _fpieee_flt (MSVCRT.@)
737 int __cdecl _fpieee_flt(__msvcrt_ulong exception_code, EXCEPTION_POINTERS *ep,
738 int (__cdecl *handler)(_FPIEEE_RECORD*))
740 FIXME("(%lx %p %p) opcode: %s\n", exception_code, ep, handler,
741 wine_dbgstr_longlong(*(ULONG64*)ep->ContextRecord->Rip));
742 return EXCEPTION_CONTINUE_SEARCH;
745 #if _MSVCR_VER>=110 && _MSVCR_VER<=120
746 /*********************************************************************
747 * __crtCapturePreviousContext (MSVCR110.@)
749 void __cdecl get_prev_context(CONTEXT *ctx, DWORD64 rip)
751 ULONG64 frame, image_base;
752 RUNTIME_FUNCTION *rf;
753 void *data;
755 TRACE("(%p)\n", ctx);
757 rf = RtlLookupFunctionEntry(ctx->Rip, &image_base, NULL);
758 if(!rf) {
759 FIXME("RtlLookupFunctionEntry failed\n");
760 return;
763 RtlVirtualUnwind(UNW_FLAG_NHANDLER, image_base, ctx->Rip,
764 rf, ctx, &data, &frame, NULL);
767 __ASM_GLOBAL_FUNC( __crtCapturePreviousContext,
768 "movq %rcx,8(%rsp)\n\t"
769 "call " __ASM_NAME("RtlCaptureContext") "\n\t"
770 "movq 8(%rsp),%rcx\n\t" /* context */
771 "leaq 8(%rsp),%rax\n\t"
772 "movq %rax,0x98(%rcx)\n\t" /* context->Rsp */
773 "movq (%rsp),%rax\n\t"
774 "movq %rax,0xf8(%rcx)\n\t" /* context->Rip */
775 "jmp " __ASM_NAME("get_prev_context") )
776 #endif
778 #endif /* __x86_64__ */