2 * msvcrt C++ exception handling
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2002 Alexandre Julliard
6 * Copyright 2005 Juan Lang
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * A good reference is the article "How a C++ compiler implements
24 * exception handling" by Vishal Kochhar, available on
25 * www.thecodeproject.com.
29 #include "wine/port.h"
39 #include "wine/exception.h"
41 #include "wine/debug.h"
43 #include "cppexcept.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(seh);
48 /* the exception frame used by CxxFrameHandler */
49 typedef struct __cxx_exception_frame
51 EXCEPTION_REGISTRATION_RECORD frame; /* the standard exception frame */
54 } cxx_exception_frame;
56 /* info about a single catch {} block */
57 typedef struct __catchblock_info
59 UINT flags; /* flags (see below) */
60 const type_info *type_info; /* C++ type caught by this block */
61 int offset; /* stack offset to copy exception object to */
62 void (*handler)(void);/* catch block handler code */
64 #define TYPE_FLAG_CONST 1
65 #define TYPE_FLAG_VOLATILE 2
66 #define TYPE_FLAG_REFERENCE 8
68 /* info about a single try {} block */
69 typedef struct __tryblock_info
71 int start_level; /* start trylevel of that block */
72 int end_level; /* end trylevel of that block */
73 int catch_level; /* initial trylevel of the catch block */
74 int catchblock_count; /* count of catch blocks in array */
75 const catchblock_info *catchblock; /* array of catch blocks */
78 /* info about the unwind handler for a given trylevel */
79 typedef struct __unwind_info
81 int prev; /* prev trylevel unwind handler, to run after this one */
82 void (*handler)(void);/* unwind handler */
85 /* descriptor of all try blocks of a given function */
86 typedef struct __cxx_function_descr
88 UINT magic; /* must be CXX_FRAME_MAGIC */
89 UINT unwind_count; /* number of unwind handlers */
90 const unwind_info *unwind_table; /* array of unwind handlers */
91 UINT tryblock_count; /* number of try blocks */
92 const tryblock_info *tryblock; /* array of try blocks */
95 const void *expect_list; /* expected exceptions list when magic >= VC7 */
96 UINT flags; /* flags when magic >= VC8 */
99 #define FUNC_DESCR_SYNCHRONOUS 1 /* synchronous exceptions only (built with /EHs) */
101 typedef struct _SCOPETABLE
103 int previousTryLevel;
104 int (*lpfnFilter)(PEXCEPTION_POINTERS);
105 int (*lpfnHandler)(void);
106 } SCOPETABLE, *PSCOPETABLE;
108 typedef struct _MSVCRT_EXCEPTION_FRAME
110 EXCEPTION_REGISTRATION_RECORD *prev;
111 void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
112 PCONTEXT, PEXCEPTION_RECORD);
113 PSCOPETABLE scopetable;
116 PEXCEPTION_POINTERS xpointers;
117 } MSVCRT_EXCEPTION_FRAME;
121 int gs_cookie_offset;
123 int eh_cookie_offset;
125 SCOPETABLE entries[1];
128 #define TRYLEVEL_END (-1) /* End of trylevel list */
130 DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
131 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
132 const cxx_function_descr *descr,
133 EXCEPTION_REGISTRATION_RECORD* nested_frame, int nested_trylevel );
135 /* call a function with a given ebp */
136 static inline void *call_ebp_func( void *func, void *ebp )
140 __asm__ __volatile__ ("pushl %%ebx\n\t"
146 : "=a" (ret), "=S" (dummy), "=D" (dummy)
147 : "0" (func), "1" (ebp) : "ecx", "edx", "memory" );
151 /* call a copy constructor */
152 static inline void call_copy_ctor( void *func, void *this, void *src, int has_vbase )
154 TRACE( "calling copy ctor %p object %p src %p\n", func, this, src );
156 /* in that case copy ctor takes an extra bool indicating whether to copy the base class */
157 __asm__ __volatile__("pushl $1; pushl %2; call *%0"
158 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
160 __asm__ __volatile__("pushl %2; call *%0"
161 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
164 /* call the destructor of the exception object */
165 static inline void call_dtor( void *func, void *object )
167 __asm__ __volatile__("call *%0" : : "r" (func), "c" (object) : "eax", "edx", "memory" );
170 /* continue execution to the specified address after exception is caught */
171 static inline void DECLSPEC_NORETURN continue_after_catch( cxx_exception_frame* frame, void *addr )
173 __asm__ __volatile__("movl -4(%0),%%esp; leal 12(%0),%%ebp; jmp *%1"
174 : : "r" (frame), "a" (addr) );
175 for (;;) ; /* unreached */
178 static inline void call_finally_block( void *code_block, void *base_ptr )
180 __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax"
181 : : "a" (code_block), "g" (base_ptr));
184 static inline int call_filter( int (*func)(PEXCEPTION_POINTERS), void *arg, void *ebp )
187 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
189 : "0" (func), "r" (ebp), "r" (arg)
190 : "ecx", "edx", "memory" );
194 static inline int call_unwind_func( int (*func)(void), void *ebp )
197 __asm__ __volatile__ ("pushl %%ebp\n\t"
208 : "0" (func), "r" (ebp)
209 : "ecx", "edx", "memory" );
213 static inline void dump_type( const cxx_type_info *type )
215 TRACE( "flags %x type %p %s offsets %d,%d,%d size %d copy ctor %p\n",
216 type->flags, type->type_info, dbgstr_type_info(type->type_info),
217 type->offsets.this_offset, type->offsets.vbase_descr, type->offsets.vbase_offset,
218 type->size, type->copy_ctor );
221 static void dump_exception_type( const cxx_exception_type *type )
225 TRACE( "flags %x destr %p handler %p type info %p\n",
226 type->flags, type->destructor, type->custom_handler, type->type_info_table );
227 for (i = 0; i < type->type_info_table->count; i++)
230 dump_type( type->type_info_table->info[i] );
234 static void dump_function_descr( const cxx_function_descr *descr )
239 TRACE( "magic %x\n", descr->magic );
240 TRACE( "unwind table: %p %d\n", descr->unwind_table, descr->unwind_count );
241 for (i = 0; i < descr->unwind_count; i++)
243 TRACE( " %d: prev %d func %p\n", i,
244 descr->unwind_table[i].prev, descr->unwind_table[i].handler );
246 TRACE( "try table: %p %d\n", descr->tryblock, descr->tryblock_count );
247 for (i = 0; i < descr->tryblock_count; i++)
249 TRACE( " %d: start %d end %d catchlevel %d catch %p %d\n", i,
250 descr->tryblock[i].start_level, descr->tryblock[i].end_level,
251 descr->tryblock[i].catch_level, descr->tryblock[i].catchblock,
252 descr->tryblock[i].catchblock_count );
253 for (j = 0; j < descr->tryblock[i].catchblock_count; j++)
255 const catchblock_info *ptr = &descr->tryblock[i].catchblock[j];
256 TRACE( " %d: flags %x offset %d handler %p type %p %s\n",
257 j, ptr->flags, ptr->offset, ptr->handler,
258 ptr->type_info, dbgstr_type_info( ptr->type_info ) );
261 if (descr->magic <= CXX_FRAME_MAGIC_VC6) return;
262 TRACE( "expect list: %p\n", descr->expect_list );
263 if (descr->magic <= CXX_FRAME_MAGIC_VC7) return;
264 TRACE( "flags: %08x\n", descr->flags );
267 /* check if the exception type is caught by a given catch block, and return the type that matched */
268 static const cxx_type_info *find_caught_type( cxx_exception_type *exc_type,
269 const catchblock_info *catchblock )
273 for (i = 0; i < exc_type->type_info_table->count; i++)
275 const cxx_type_info *type = exc_type->type_info_table->info[i];
277 if (!catchblock->type_info) return type; /* catch(...) matches any type */
278 if (catchblock->type_info != type->type_info)
280 if (strcmp( catchblock->type_info->mangled, type->type_info->mangled )) continue;
282 /* type is the same, now check the flags */
283 if ((exc_type->flags & TYPE_FLAG_CONST) &&
284 !(catchblock->flags & TYPE_FLAG_CONST)) continue;
285 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
286 !(catchblock->flags & TYPE_FLAG_VOLATILE)) continue;
287 return type; /* it matched */
293 /* copy the exception object where the catch block wants it */
294 static void copy_exception( void *object, cxx_exception_frame *frame,
295 const catchblock_info *catchblock, const cxx_type_info *type )
299 if (!catchblock->type_info || !catchblock->type_info->mangled[0]) return;
300 if (!catchblock->offset) return;
301 dest_ptr = (void **)((char *)&frame->ebp + catchblock->offset);
303 if (catchblock->flags & TYPE_FLAG_REFERENCE)
305 *dest_ptr = get_this_pointer( &type->offsets, object );
307 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
309 memmove( dest_ptr, object, type->size );
310 /* if it is a pointer, adjust it */
311 if (type->size == sizeof(void *)) *dest_ptr = get_this_pointer( &type->offsets, *dest_ptr );
313 else /* copy the object */
316 call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(&type->offsets,object),
317 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
319 memmove( dest_ptr, get_this_pointer(&type->offsets,object), type->size );
323 /* unwind the local function up to a given trylevel */
324 static void cxx_local_unwind( cxx_exception_frame* frame, const cxx_function_descr *descr, int last_level)
326 void (*handler)(void);
327 int trylevel = frame->trylevel;
329 while (trylevel != last_level)
331 if (trylevel < 0 || trylevel >= descr->unwind_count)
333 ERR( "invalid trylevel %d\n", trylevel );
336 handler = descr->unwind_table[trylevel].handler;
339 TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
340 handler, trylevel, last_level, &frame->ebp );
341 call_ebp_func( handler, &frame->ebp );
343 trylevel = descr->unwind_table[trylevel].prev;
345 frame->trylevel = last_level;
348 /* exception frame for nested exceptions in catch block */
349 struct catch_func_nested_frame
351 EXCEPTION_REGISTRATION_RECORD frame; /* standard exception frame */
352 EXCEPTION_RECORD *prev_rec; /* previous record to restore in thread data */
353 cxx_exception_frame *cxx_frame; /* frame of parent exception */
354 const cxx_function_descr *descr; /* descriptor of parent exception */
355 int trylevel; /* current try level */
356 EXCEPTION_RECORD *rec; /* rec associated with frame */
359 /* handler for exceptions happening while calling a catch function */
360 static DWORD catch_function_nested_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
361 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
363 struct catch_func_nested_frame *nested_frame = (struct catch_func_nested_frame *)frame;
365 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
367 msvcrt_get_thread_data()->exc_record = nested_frame->prev_rec;
368 return ExceptionContinueSearch;
371 TRACE( "got nested exception in catch function\n" );
373 if(rec->ExceptionCode == CXX_EXCEPTION)
375 PEXCEPTION_RECORD prev_rec = nested_frame->rec;
376 if(rec->ExceptionInformation[1] == 0 && rec->ExceptionInformation[2] == 0)
378 /* exception was rethrown */
379 rec->ExceptionInformation[1] = prev_rec->ExceptionInformation[1];
380 rec->ExceptionInformation[2] = prev_rec->ExceptionInformation[2];
381 TRACE("detect rethrow: re-propagate: obj: %lx, type: %lx\n",
382 rec->ExceptionInformation[1], rec->ExceptionInformation[2]);
385 /* new exception in exception handler, destroy old */
386 void *object = (void*)prev_rec->ExceptionInformation[1];
387 cxx_exception_type *info = (cxx_exception_type*) prev_rec->ExceptionInformation[2];
388 TRACE("detect threw new exception in catch block - destroy old(obj: %p type: %p)\n",
390 if(info && info->destructor)
391 call_dtor( info->destructor, object );
395 return cxx_frame_handler( rec, nested_frame->cxx_frame, context,
396 NULL, nested_frame->descr, &nested_frame->frame,
397 nested_frame->trylevel );
400 /* find and call the appropriate catch block for an exception */
401 /* returns the address to continue execution to after the catch block was called */
402 static inline void call_catch_block( PEXCEPTION_RECORD rec, cxx_exception_frame *frame,
403 const cxx_function_descr *descr, int nested_trylevel,
404 cxx_exception_type *info )
408 void *addr, *object = (void *)rec->ExceptionInformation[1];
409 struct catch_func_nested_frame nested_frame;
410 int trylevel = frame->trylevel;
411 thread_data_t *thread_data = msvcrt_get_thread_data();
412 DWORD save_esp = ((DWORD*)frame)[-1];
414 for (i = 0; i < descr->tryblock_count; i++)
416 const tryblock_info *tryblock = &descr->tryblock[i];
418 if (trylevel < tryblock->start_level) continue;
419 if (trylevel > tryblock->end_level) continue;
421 /* got a try block */
422 for (j = 0; j < tryblock->catchblock_count; j++)
424 const catchblock_info *catchblock = &tryblock->catchblock[j];
427 const cxx_type_info *type = find_caught_type( info, catchblock );
430 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
432 /* copy the exception to its destination on the stack */
433 copy_exception( object, frame, catchblock, type );
437 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
438 if(catchblock->type_info)
440 TRACE("found catch(...) block\n");
443 /* unwind the stack */
444 RtlUnwind( frame, 0, rec, 0 );
445 cxx_local_unwind( frame, descr, tryblock->start_level );
446 frame->trylevel = tryblock->end_level + 1;
448 /* call the catch block */
449 TRACE( "calling catch block %p addr %p ebp %p\n",
450 catchblock, catchblock->handler, &frame->ebp );
452 /* setup an exception block for nested exceptions */
454 nested_frame.frame.Handler = catch_function_nested_handler;
455 nested_frame.prev_rec = thread_data->exc_record;
456 nested_frame.cxx_frame = frame;
457 nested_frame.descr = descr;
458 nested_frame.trylevel = nested_trylevel + 1;
459 nested_frame.rec = rec;
461 __wine_push_frame( &nested_frame.frame );
462 thread_data->exc_record = rec;
463 addr = call_ebp_func( catchblock->handler, &frame->ebp );
464 thread_data->exc_record = nested_frame.prev_rec;
465 __wine_pop_frame( &nested_frame.frame );
467 ((DWORD*)frame)[-1] = save_esp;
468 if (info && info->destructor) call_dtor( info->destructor, object );
469 TRACE( "done, continuing at %p\n", addr );
471 continue_after_catch( frame, addr );
477 /*********************************************************************
480 * Implementation of __CxxFrameHandler.
482 DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
483 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
484 const cxx_function_descr *descr,
485 EXCEPTION_REGISTRATION_RECORD* nested_frame,
486 int nested_trylevel )
488 cxx_exception_type *exc_type;
490 if (descr->magic < CXX_FRAME_MAGIC_VC6 || descr->magic > CXX_FRAME_MAGIC_VC8)
492 ERR( "invalid frame magic %x\n", descr->magic );
493 return ExceptionContinueSearch;
495 if (descr->magic >= CXX_FRAME_MAGIC_VC8 &&
496 (descr->flags & FUNC_DESCR_SYNCHRONOUS) &&
497 (rec->ExceptionCode != CXX_EXCEPTION))
498 return ExceptionContinueSearch; /* handle only c++ exceptions */
500 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
502 if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
503 return ExceptionContinueSearch;
505 if (!descr->tryblock_count) return ExceptionContinueSearch;
507 if(rec->ExceptionCode == CXX_EXCEPTION)
509 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
511 if (rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8 &&
512 exc_type->custom_handler)
514 return exc_type->custom_handler( rec, frame, context, dispatch,
515 descr, nested_trylevel, nested_frame, 0 );
520 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
521 rec, frame, frame->trylevel, descr, nested_frame );
522 dump_exception_type( exc_type );
523 dump_function_descr( descr );
529 TRACE("handling C exception code %x rec %p frame %p trylevel %d descr %p nested_frame %p\n",
530 rec->ExceptionCode, rec, frame, frame->trylevel, descr, nested_frame );
533 call_catch_block( rec, frame, descr, frame->trylevel, exc_type );
534 return ExceptionContinueSearch;
538 /*********************************************************************
539 * __CxxFrameHandler (MSVCRT.@)
541 extern DWORD CDECL __CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_REGISTRATION_RECORD* frame,
542 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch );
543 __ASM_GLOBAL_FUNC( __CxxFrameHandler,
544 "pushl $0\n\t" /* nested_trylevel */
545 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
546 "pushl $0\n\t" /* nested_frame */
547 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
548 "pushl %eax\n\t" /* descr */
549 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
550 "pushl 28(%esp)\n\t" /* dispatch */
551 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
552 "pushl 28(%esp)\n\t" /* context */
553 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
554 "pushl 28(%esp)\n\t" /* frame */
555 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
556 "pushl 28(%esp)\n\t" /* rec */
557 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
558 "call " __ASM_NAME("cxx_frame_handler") "\n\t"
560 __ASM_CFI(".cfi_adjust_cfa_offset -28\n\t")
564 /*********************************************************************
565 * __CxxLongjmpUnwind (MSVCRT.@)
567 * Callback meant to be used as UnwindFunc for setjmp/longjmp.
569 void __stdcall __CxxLongjmpUnwind( const struct MSVCRT___JUMP_BUFFER *buf )
571 cxx_exception_frame *frame = (cxx_exception_frame *)buf->Registration;
572 const cxx_function_descr *descr = (const cxx_function_descr *)buf->UnwindData[0];
574 TRACE( "unwinding frame %p descr %p trylevel %ld\n", frame, descr, buf->TryLevel );
575 cxx_local_unwind( frame, descr, buf->TryLevel );
578 /*********************************************************************
579 * __CppXcptFilter (MSVCRT.@)
581 int CDECL __CppXcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
583 /* only filter c++ exceptions */
584 if (ex != CXX_EXCEPTION) return EXCEPTION_CONTINUE_SEARCH;
585 return _XcptFilter( ex, ptr );
588 /*********************************************************************
589 * __CxxDetectRethrow (MSVCRT.@)
591 BOOL CDECL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
593 PEXCEPTION_RECORD rec;
598 rec = ptrs->ExceptionRecord;
600 if (rec->ExceptionCode == CXX_EXCEPTION &&
601 rec->NumberParameters == 3 &&
602 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC_VC6 &&
603 rec->ExceptionInformation[2])
605 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
608 return (msvcrt_get_thread_data()->exc_record == rec);
611 /*********************************************************************
612 * __CxxQueryExceptionSize (MSVCRT.@)
614 unsigned int CDECL __CxxQueryExceptionSize(void)
616 return sizeof(cxx_exception_type);
620 /*********************************************************************
621 * _EH_prolog (MSVCRT.@)
624 /* Provided for VC++ binary compatibility only */
625 __ASM_GLOBAL_FUNC(_EH_prolog,
626 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") /* skip ret addr */
628 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
630 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
632 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
633 "movl %esp, %fs:0\n\t"
634 "movl 12(%esp), %eax\n\t"
635 "movl %ebp, 12(%esp)\n\t"
636 "leal 12(%esp), %ebp\n\t"
638 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
641 static const SCOPETABLE_V4 *get_scopetable_v4( MSVCRT_EXCEPTION_FRAME *frame, ULONG_PTR cookie )
643 return (const SCOPETABLE_V4 *)((ULONG_PTR)frame->scopetable ^ cookie);
646 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
647 EXCEPTION_REGISTRATION_RECORD* frame,
649 EXCEPTION_REGISTRATION_RECORD** dispatch)
651 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
652 return ExceptionContinueSearch;
654 return ExceptionCollidedUnwind;
657 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp)
659 EXCEPTION_REGISTRATION_RECORD reg;
661 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
663 /* Register a handler in case of a nested exception */
664 reg.Handler = MSVCRT_nested_handler;
665 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
666 __wine_push_frame(®);
668 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
670 int level = frame->trylevel;
671 frame->trylevel = frame->scopetable[level].previousTryLevel;
672 if (!frame->scopetable[level].lpfnFilter)
674 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
675 level, frame->scopetable[level].lpfnHandler, ebp );
676 call_unwind_func( frame->scopetable[level].lpfnHandler, ebp );
679 __wine_pop_frame(®);
680 TRACE("unwound OK\n");
683 static void msvcrt_local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp )
685 EXCEPTION_REGISTRATION_RECORD reg;
686 const SCOPETABLE_V4 *scopetable = get_scopetable_v4( frame, *cookie );
688 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
690 /* Register a handler in case of a nested exception */
691 reg.Handler = MSVCRT_nested_handler;
692 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
693 __wine_push_frame(®);
695 while (frame->trylevel != -2 && frame->trylevel != trylevel)
697 int level = frame->trylevel;
698 frame->trylevel = scopetable->entries[level].previousTryLevel;
699 if (!scopetable->entries[level].lpfnFilter)
701 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
702 level, scopetable->entries[level].lpfnHandler, ebp );
703 call_unwind_func( scopetable->entries[level].lpfnHandler, ebp );
706 __wine_pop_frame(®);
707 TRACE("unwound OK\n");
710 /*******************************************************************
711 * _local_unwind2 (MSVCRT.@)
713 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
715 msvcrt_local_unwind2( frame, trylevel, &frame->_ebp );
718 /*******************************************************************
719 * _local_unwind4 (MSVCRT.@)
721 void CDECL _local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel )
723 msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
726 /*******************************************************************
727 * _global_unwind2 (MSVCRT.@)
729 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
731 TRACE("(%p)\n",frame);
732 RtlUnwind( frame, 0, 0, 0 );
735 /*********************************************************************
736 * _except_handler2 (MSVCRT.@)
738 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
739 EXCEPTION_REGISTRATION_RECORD* frame,
741 EXCEPTION_REGISTRATION_RECORD** dispatcher)
743 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
744 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
745 frame->Handler, context, dispatcher);
746 return ExceptionContinueSearch;
749 /*********************************************************************
750 * _except_handler3 (MSVCRT.@)
752 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
753 MSVCRT_EXCEPTION_FRAME* frame,
754 PCONTEXT context, void* dispatcher)
756 int retval, trylevel;
757 EXCEPTION_POINTERS exceptPtrs;
758 PSCOPETABLE pScopeTable;
760 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
761 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
762 frame->handler, context, dispatcher);
764 __asm__ __volatile__ ("cld");
766 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
768 /* Unwinding the current frame */
769 msvcrt_local_unwind2(frame, TRYLEVEL_END, &frame->_ebp);
770 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
771 return ExceptionContinueSearch;
775 /* Hunting for handler */
776 exceptPtrs.ExceptionRecord = rec;
777 exceptPtrs.ContextRecord = context;
778 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
779 trylevel = frame->trylevel;
780 pScopeTable = frame->scopetable;
782 while (trylevel != TRYLEVEL_END)
784 TRACE( "level %d prev %d filter %p\n", trylevel, pScopeTable[trylevel].previousTryLevel,
785 pScopeTable[trylevel].lpfnFilter );
786 if (pScopeTable[trylevel].lpfnFilter)
788 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
790 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
791 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
792 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
794 if (retval == EXCEPTION_CONTINUE_EXECUTION)
795 return ExceptionContinueExecution;
797 if (retval == EXCEPTION_EXECUTE_HANDLER)
799 /* Unwind all higher frames, this one will handle the exception */
800 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
801 msvcrt_local_unwind2(frame, trylevel, &frame->_ebp);
803 /* Set our trylevel to the enclosing block, and call the __finally
804 * code, which won't return
806 frame->trylevel = pScopeTable[trylevel].previousTryLevel;
807 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
808 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
809 ERR("Returned from __finally block - expect crash!\n");
812 trylevel = pScopeTable[trylevel].previousTryLevel;
815 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
816 return ExceptionContinueSearch;
819 /*********************************************************************
820 * _except_handler4_common (MSVCRT.@)
822 int CDECL _except_handler4_common( ULONG *cookie, void (*check_cookie)(void),
823 EXCEPTION_RECORD *rec, MSVCRT_EXCEPTION_FRAME *frame,
824 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
826 int retval, trylevel;
827 EXCEPTION_POINTERS exceptPtrs;
828 const SCOPETABLE_V4 *scope_table = get_scopetable_v4( frame, *cookie );
830 TRACE( "exception %x flags=%x at %p handler=%p %p %p cookie=%x scope table=%p cookies=%d/%x,%d/%x\n",
831 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
832 frame->handler, context, dispatcher, *cookie, scope_table,
833 scope_table->gs_cookie_offset, scope_table->gs_cookie_xor,
834 scope_table->eh_cookie_offset, scope_table->eh_cookie_xor );
836 /* FIXME: no cookie validation yet */
838 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
840 /* Unwinding the current frame */
841 msvcrt_local_unwind4( cookie, frame, -2, &frame->_ebp );
842 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
843 return ExceptionContinueSearch;
847 /* Hunting for handler */
848 exceptPtrs.ExceptionRecord = rec;
849 exceptPtrs.ContextRecord = context;
850 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
851 trylevel = frame->trylevel;
853 while (trylevel != -2)
855 TRACE( "level %d prev %d filter %p\n", trylevel,
856 scope_table->entries[trylevel].previousTryLevel,
857 scope_table->entries[trylevel].lpfnFilter );
858 if (scope_table->entries[trylevel].lpfnFilter)
860 retval = call_filter( scope_table->entries[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
862 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
863 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
864 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
866 if (retval == EXCEPTION_CONTINUE_EXECUTION)
867 return ExceptionContinueExecution;
869 if (retval == EXCEPTION_EXECUTE_HANDLER)
871 /* Unwind all higher frames, this one will handle the exception */
872 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
873 msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
875 /* Set our trylevel to the enclosing block, and call the __finally
876 * code, which won't return
878 frame->trylevel = scope_table->entries[trylevel].previousTryLevel;
879 TRACE("__finally block %p\n",scope_table->entries[trylevel].lpfnHandler);
880 call_finally_block(scope_table->entries[trylevel].lpfnHandler, &frame->_ebp);
881 ERR("Returned from __finally block - expect crash!\n");
884 trylevel = scope_table->entries[trylevel].previousTryLevel;
887 TRACE("reached -2, returning ExceptionContinueSearch\n");
888 return ExceptionContinueSearch;
893 * setjmp/longjmp implementation
896 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
897 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
899 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
900 /* and then jumps to the C backend function */
901 #define DEFINE_SETJMP_ENTRYPOINT(name) \
902 __ASM_GLOBAL_FUNC( name, \
903 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
904 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
905 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
906 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
907 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
908 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
909 "movl 0(%esp),%eax\n\t" \
910 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
911 "jmp " __ASM_NAME("__regs_") # name )
913 /* restore the registers from the jmp buf upon longjmp */
914 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
915 __ASM_GLOBAL_FUNC( longjmp_set_regs,
916 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
917 "movl 8(%esp),%eax\n\t" /* retval */
918 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
919 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
920 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
921 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
922 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
923 "addl $4,%esp\n\t" /* get rid of return address */
924 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
927 * The signatures of the setjmp/longjmp functions do not match that
928 * declared in the setjmp header so they don't follow the regular naming
929 * convention to avoid conflicts.
932 /*******************************************************************
935 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
936 int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
938 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
939 if (jmp->Registration == ~0UL)
940 jmp->TryLevel = TRYLEVEL_END;
942 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
944 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
945 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
949 /*******************************************************************
950 * _setjmp3 (MSVCRT.@)
952 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
953 int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
955 jmp->Cookie = MSVCRT_JMP_MAGIC;
957 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
958 if (jmp->Registration == ~0UL)
960 jmp->TryLevel = TRYLEVEL_END;
967 va_start( args, nb_args );
968 if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
969 if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
970 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
971 for (i = 0; i < 6 && i < nb_args - 2; i++)
972 jmp->UnwindData[i] = va_arg( args, unsigned long );
976 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
977 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
981 /*********************************************************************
984 void CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
986 unsigned long cur_frame = 0;
988 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
989 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
991 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
992 TRACE("cur_frame=%lx\n",cur_frame);
994 if (cur_frame != jmp->Registration)
995 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
997 if (jmp->Registration)
999 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
1000 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
1002 MSVCRT_unwind_function unwind_func;
1004 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
1008 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
1009 jmp->TryLevel, (void *)jmp->Ebp);
1015 longjmp_set_regs( jmp, retval );
1018 /*********************************************************************
1019 * _seh_longjmp_unwind (MSVCRT.@)
1021 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
1023 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel, (void *)jmp->Ebp );
1026 /*********************************************************************
1027 * _seh_longjmp_unwind4 (MSVCRT.@)
1029 void __stdcall _seh_longjmp_unwind4(struct MSVCRT___JUMP_BUFFER *jmp)
1031 msvcrt_local_unwind4( (void *)jmp->Cookie, (MSVCRT_EXCEPTION_FRAME *)jmp->Registration,
1032 jmp->TryLevel, (void *)jmp->Ebp );
1035 #endif /* __i386__ */