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
);
134 BOOL __cdecl
_IsExceptionObjectToBeDestroyed(const void*);
136 /* call a function with a given ebp */
137 static inline void *call_ebp_func( void *func
, void *ebp
)
141 __asm__
__volatile__ ("pushl %%ebx\n\t"
147 : "=a" (ret
), "=S" (dummy
), "=D" (dummy
)
148 : "0" (func
), "1" (ebp
) : "ecx", "edx", "memory" );
152 /* call a copy constructor */
153 static inline void call_copy_ctor( void *func
, void *this, void *src
, int has_vbase
)
155 TRACE( "calling copy ctor %p object %p src %p\n", func
, this, src
);
157 /* in that case copy ctor takes an extra bool indicating whether to copy the base class */
158 __asm__
__volatile__("pushl $1; pushl %2; call *%0"
159 : : "r" (func
), "c" (this), "r" (src
) : "eax", "edx", "memory" );
161 __asm__
__volatile__("pushl %2; call *%0"
162 : : "r" (func
), "c" (this), "r" (src
) : "eax", "edx", "memory" );
165 /* call the destructor of the exception object */
166 static inline void call_dtor( void *func
, void *object
)
168 __asm__
__volatile__("call *%0" : : "r" (func
), "c" (object
) : "eax", "edx", "memory" );
171 /* continue execution to the specified address after exception is caught */
172 static inline void DECLSPEC_NORETURN
continue_after_catch( cxx_exception_frame
* frame
, void *addr
)
174 __asm__
__volatile__("movl -4(%0),%%esp; leal 12(%0),%%ebp; jmp *%1"
175 : : "r" (frame
), "a" (addr
) );
176 for (;;) ; /* unreached */
179 static inline void call_finally_block( void *code_block
, void *base_ptr
)
181 __asm__
__volatile__ ("movl %1,%%ebp; call *%%eax"
182 : : "a" (code_block
), "g" (base_ptr
));
185 static inline int call_filter( int (*func
)(PEXCEPTION_POINTERS
), void *arg
, void *ebp
)
188 __asm__
__volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
190 : "0" (func
), "r" (ebp
), "r" (arg
)
191 : "ecx", "edx", "memory" );
195 static inline int call_unwind_func( int (*func
)(void), void *ebp
)
198 __asm__
__volatile__ ("pushl %%ebp\n\t"
209 : "0" (func
), "r" (ebp
)
210 : "ecx", "edx", "memory" );
214 static inline void dump_type( const cxx_type_info
*type
)
216 TRACE( "flags %x type %p %s offsets %d,%d,%d size %d copy ctor %p\n",
217 type
->flags
, type
->type_info
, dbgstr_type_info(type
->type_info
),
218 type
->offsets
.this_offset
, type
->offsets
.vbase_descr
, type
->offsets
.vbase_offset
,
219 type
->size
, type
->copy_ctor
);
222 static void dump_exception_type( const cxx_exception_type
*type
)
226 TRACE( "flags %x destr %p handler %p type info %p\n",
227 type
->flags
, type
->destructor
, type
->custom_handler
, type
->type_info_table
);
228 for (i
= 0; i
< type
->type_info_table
->count
; i
++)
231 dump_type( type
->type_info_table
->info
[i
] );
235 static void dump_function_descr( const cxx_function_descr
*descr
)
240 TRACE( "magic %x\n", descr
->magic
);
241 TRACE( "unwind table: %p %d\n", descr
->unwind_table
, descr
->unwind_count
);
242 for (i
= 0; i
< descr
->unwind_count
; i
++)
244 TRACE( " %d: prev %d func %p\n", i
,
245 descr
->unwind_table
[i
].prev
, descr
->unwind_table
[i
].handler
);
247 TRACE( "try table: %p %d\n", descr
->tryblock
, descr
->tryblock_count
);
248 for (i
= 0; i
< descr
->tryblock_count
; i
++)
250 TRACE( " %d: start %d end %d catchlevel %d catch %p %d\n", i
,
251 descr
->tryblock
[i
].start_level
, descr
->tryblock
[i
].end_level
,
252 descr
->tryblock
[i
].catch_level
, descr
->tryblock
[i
].catchblock
,
253 descr
->tryblock
[i
].catchblock_count
);
254 for (j
= 0; j
< descr
->tryblock
[i
].catchblock_count
; j
++)
256 const catchblock_info
*ptr
= &descr
->tryblock
[i
].catchblock
[j
];
257 TRACE( " %d: flags %x offset %d handler %p type %p %s\n",
258 j
, ptr
->flags
, ptr
->offset
, ptr
->handler
,
259 ptr
->type_info
, dbgstr_type_info( ptr
->type_info
) );
262 if (descr
->magic
<= CXX_FRAME_MAGIC_VC6
) return;
263 TRACE( "expect list: %p\n", descr
->expect_list
);
264 if (descr
->magic
<= CXX_FRAME_MAGIC_VC7
) return;
265 TRACE( "flags: %08x\n", descr
->flags
);
268 /* check if the exception type is caught by a given catch block, and return the type that matched */
269 static const cxx_type_info
*find_caught_type( cxx_exception_type
*exc_type
,
270 const type_info
*catch_ti
, UINT catch_flags
)
274 for (i
= 0; i
< exc_type
->type_info_table
->count
; i
++)
276 const cxx_type_info
*type
= exc_type
->type_info_table
->info
[i
];
278 if (!catch_ti
) return type
; /* catch(...) matches any type */
279 if (catch_ti
!= type
->type_info
)
281 if (strcmp( catch_ti
->mangled
, type
->type_info
->mangled
)) continue;
283 /* type is the same, now check the flags */
284 if ((exc_type
->flags
& TYPE_FLAG_CONST
) &&
285 !(catch_flags
& TYPE_FLAG_CONST
)) continue;
286 if ((exc_type
->flags
& TYPE_FLAG_VOLATILE
) &&
287 !(catch_flags
& TYPE_FLAG_VOLATILE
)) continue;
288 return type
; /* it matched */
294 /* copy the exception object where the catch block wants it */
295 static void copy_exception( void *object
, cxx_exception_frame
*frame
,
296 const catchblock_info
*catchblock
, const cxx_type_info
*type
)
300 if (!catchblock
->type_info
|| !catchblock
->type_info
->mangled
[0]) return;
301 if (!catchblock
->offset
) return;
302 dest_ptr
= (void **)((char *)&frame
->ebp
+ catchblock
->offset
);
304 if (catchblock
->flags
& TYPE_FLAG_REFERENCE
)
306 *dest_ptr
= get_this_pointer( &type
->offsets
, object
);
308 else if (type
->flags
& CLASS_IS_SIMPLE_TYPE
)
310 memmove( dest_ptr
, object
, type
->size
);
311 /* if it is a pointer, adjust it */
312 if (type
->size
== sizeof(void *)) *dest_ptr
= get_this_pointer( &type
->offsets
, *dest_ptr
);
314 else /* copy the object */
317 call_copy_ctor( type
->copy_ctor
, dest_ptr
, get_this_pointer(&type
->offsets
,object
),
318 (type
->flags
& CLASS_HAS_VIRTUAL_BASE_CLASS
) );
320 memmove( dest_ptr
, get_this_pointer(&type
->offsets
,object
), type
->size
);
324 /* unwind the local function up to a given trylevel */
325 static void cxx_local_unwind( cxx_exception_frame
* frame
, const cxx_function_descr
*descr
, int last_level
)
327 void (*handler
)(void);
328 int trylevel
= frame
->trylevel
;
330 while (trylevel
!= last_level
)
332 if (trylevel
< 0 || trylevel
>= descr
->unwind_count
)
334 ERR( "invalid trylevel %d\n", trylevel
);
337 handler
= descr
->unwind_table
[trylevel
].handler
;
340 TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
341 handler
, trylevel
, last_level
, &frame
->ebp
);
342 call_ebp_func( handler
, &frame
->ebp
);
344 trylevel
= descr
->unwind_table
[trylevel
].prev
;
346 frame
->trylevel
= last_level
;
349 /* exception frame for nested exceptions in catch block */
350 struct catch_func_nested_frame
352 EXCEPTION_REGISTRATION_RECORD frame
; /* standard exception frame */
353 EXCEPTION_RECORD
*prev_rec
; /* previous record to restore in thread data */
354 cxx_exception_frame
*cxx_frame
; /* frame of parent exception */
355 const cxx_function_descr
*descr
; /* descriptor of parent exception */
356 int trylevel
; /* current try level */
357 EXCEPTION_RECORD
*rec
; /* rec associated with frame */
360 /* handler for exceptions happening while calling a catch function */
361 static DWORD
catch_function_nested_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
362 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
364 struct catch_func_nested_frame
*nested_frame
= (struct catch_func_nested_frame
*)frame
;
365 PEXCEPTION_RECORD prev_rec
= nested_frame
->rec
;
367 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
369 if (prev_rec
->ExceptionCode
== CXX_EXCEPTION
)
371 void *object
= (void*)prev_rec
->ExceptionInformation
[1];
372 cxx_exception_type
*info
= (cxx_exception_type
*) prev_rec
->ExceptionInformation
[2];
373 if (info
&& info
->destructor
&& _IsExceptionObjectToBeDestroyed(object
))
374 call_dtor( info
->destructor
, object
);
377 msvcrt_get_thread_data()->exc_record
= nested_frame
->prev_rec
;
378 return ExceptionContinueSearch
;
381 TRACE( "got nested exception in catch function\n" );
383 if(rec
->ExceptionCode
== CXX_EXCEPTION
)
385 if((rec
->ExceptionInformation
[1] == 0 && rec
->ExceptionInformation
[2] == 0) ||
386 (prev_rec
->ExceptionCode
== CXX_EXCEPTION
&&
387 rec
->ExceptionInformation
[1] == prev_rec
->ExceptionInformation
[1] &&
388 rec
->ExceptionInformation
[2] == prev_rec
->ExceptionInformation
[2]))
390 /* exception was rethrown */
392 rec
->ExceptionFlags
&= ~EH_UNWINDING
;
394 TRACE("detect rethrow: exception code: %x\n", rec
->ExceptionCode
);
395 if(rec
->ExceptionCode
== CXX_EXCEPTION
)
396 TRACE("re-propage: obj: %lx, type: %lx\n",
397 rec
->ExceptionInformation
[1], rec
->ExceptionInformation
[2]);
402 TRACE("detect threw new exception in catch block\n");
406 return cxx_frame_handler( rec
, nested_frame
->cxx_frame
, context
,
407 NULL
, nested_frame
->descr
, &nested_frame
->frame
,
408 nested_frame
->trylevel
);
411 /*********************************************************************
412 * _IsExceptionObjectToBeDestroyed (MSVCR80.@)
414 BOOL __cdecl
_IsExceptionObjectToBeDestroyed(const void *obj
)
416 EXCEPTION_REGISTRATION_RECORD
*reg
= NtCurrentTeb()->Tib
.ExceptionList
;
419 TRACE( "%p\n", obj
);
421 while (reg
!= (EXCEPTION_REGISTRATION_RECORD
*)-1)
423 if (reg
->Handler
== catch_function_nested_handler
)
425 EXCEPTION_RECORD
*rec
= ((struct catch_func_nested_frame
*)reg
)->rec
;
427 if(!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
428 && rec
->ExceptionCode
== CXX_EXCEPTION
&& rec
->NumberParameters
== 3
429 && rec
->ExceptionInformation
[1] == (LONG_PTR
)obj
)
436 for (cur
= msvcrt_get_thread_data()->frame_info_head
; cur
; cur
= cur
->next
)
438 if (cur
->object
== obj
)
445 /* find and call the appropriate catch block for an exception */
446 /* returns the address to continue execution to after the catch block was called */
447 static inline void call_catch_block( PEXCEPTION_RECORD rec
, cxx_exception_frame
*frame
,
448 const cxx_function_descr
*descr
, int nested_trylevel
,
449 EXCEPTION_REGISTRATION_RECORD
*catch_frame
,
450 cxx_exception_type
*info
)
454 void *addr
, *object
= (void *)rec
->ExceptionInformation
[1];
455 struct catch_func_nested_frame nested_frame
;
456 int trylevel
= frame
->trylevel
;
457 thread_data_t
*thread_data
= msvcrt_get_thread_data();
458 DWORD save_esp
= ((DWORD
*)frame
)[-1];
460 for (i
= 0; i
< descr
->tryblock_count
; i
++)
462 const tryblock_info
*tryblock
= &descr
->tryblock
[i
];
464 /* only handle try blocks inside current catch block */
465 if (catch_frame
&& nested_trylevel
> tryblock
->start_level
) continue;
467 if (trylevel
< tryblock
->start_level
) continue;
468 if (trylevel
> tryblock
->end_level
) continue;
470 /* got a try block */
471 for (j
= 0; j
< tryblock
->catchblock_count
; j
++)
473 const catchblock_info
*catchblock
= &tryblock
->catchblock
[j
];
476 const cxx_type_info
*type
= find_caught_type( info
,
477 catchblock
->type_info
, catchblock
->flags
);
480 TRACE( "matched type %p in tryblock %d catchblock %d\n", type
, i
, j
);
482 /* copy the exception to its destination on the stack */
483 copy_exception( object
, frame
, catchblock
, type
);
487 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
488 if(catchblock
->type_info
)
490 TRACE("found catch(...) block\n");
493 /* unwind the stack */
494 RtlUnwind( catch_frame
? catch_frame
: &frame
->frame
, 0, rec
, 0 );
495 cxx_local_unwind( frame
, descr
, tryblock
->start_level
);
496 frame
->trylevel
= tryblock
->end_level
+ 1;
498 /* call the catch block */
499 TRACE( "calling catch block %p addr %p ebp %p\n",
500 catchblock
, catchblock
->handler
, &frame
->ebp
);
502 /* setup an exception block for nested exceptions */
504 nested_frame
.frame
.Handler
= catch_function_nested_handler
;
505 nested_frame
.prev_rec
= thread_data
->exc_record
;
506 nested_frame
.cxx_frame
= frame
;
507 nested_frame
.descr
= descr
;
508 nested_frame
.trylevel
= nested_trylevel
+ 1;
509 nested_frame
.rec
= rec
;
511 __wine_push_frame( &nested_frame
.frame
);
512 thread_data
->exc_record
= rec
;
513 addr
= call_ebp_func( catchblock
->handler
, &frame
->ebp
);
514 thread_data
->exc_record
= nested_frame
.prev_rec
;
515 __wine_pop_frame( &nested_frame
.frame
);
517 ((DWORD
*)frame
)[-1] = save_esp
;
518 if (info
&& info
->destructor
&& _IsExceptionObjectToBeDestroyed(object
))
519 call_dtor( info
->destructor
, object
);
520 TRACE( "done, continuing at %p\n", addr
);
522 continue_after_catch( frame
, addr
);
527 /*********************************************************************
528 * __CxxExceptionFilter (MSVCRT.@)
530 int CDECL
__CxxExceptionFilter( PEXCEPTION_POINTERS ptrs
,
531 const type_info
*ti
, int flags
, void **copy
)
533 const cxx_type_info
*type
;
534 PEXCEPTION_RECORD rec
;
536 TRACE( "%p %p %x %p\n", ptrs
, ti
, flags
, copy
);
538 if (!ptrs
) return EXCEPTION_CONTINUE_SEARCH
;
540 /* handle catch(...) */
541 if (!ti
) return EXCEPTION_EXECUTE_HANDLER
;
543 rec
= ptrs
->ExceptionRecord
;
544 if (rec
->ExceptionCode
!= CXX_EXCEPTION
|| rec
->NumberParameters
!= 3 ||
545 rec
->ExceptionInformation
[0] < CXX_FRAME_MAGIC_VC6
||
546 rec
->ExceptionInformation
[0] > CXX_FRAME_MAGIC_VC8
)
547 return EXCEPTION_CONTINUE_SEARCH
;
549 if (rec
->ExceptionInformation
[1] == 0 && rec
->ExceptionInformation
[2] == 0)
551 rec
= msvcrt_get_thread_data()->exc_record
;
552 if (!rec
) return EXCEPTION_CONTINUE_SEARCH
;
555 type
= find_caught_type( (cxx_exception_type
*)rec
->ExceptionInformation
[2], ti
, flags
);
556 if (!type
) return EXCEPTION_CONTINUE_SEARCH
;
560 void *object
= (void *)rec
->ExceptionInformation
[1];
562 if (flags
& TYPE_FLAG_REFERENCE
)
564 *copy
= get_this_pointer( &type
->offsets
, object
);
566 else if (type
->flags
& CLASS_IS_SIMPLE_TYPE
)
568 memmove( copy
, object
, type
->size
);
569 /* if it is a pointer, adjust it */
570 if (type
->size
== sizeof(void*)) *copy
= get_this_pointer( &type
->offsets
, *copy
);
572 else /* copy the object */
575 call_copy_ctor( type
->copy_ctor
, copy
, get_this_pointer(&type
->offsets
,object
),
576 (type
->flags
& CLASS_HAS_VIRTUAL_BASE_CLASS
) );
578 memmove( copy
, get_this_pointer(&type
->offsets
,object
), type
->size
);
581 return EXCEPTION_EXECUTE_HANDLER
;
584 /*********************************************************************
587 * Implementation of __CxxFrameHandler.
589 DWORD CDECL
cxx_frame_handler( PEXCEPTION_RECORD rec
, cxx_exception_frame
* frame
,
590 PCONTEXT context
, EXCEPTION_REGISTRATION_RECORD
** dispatch
,
591 const cxx_function_descr
*descr
,
592 EXCEPTION_REGISTRATION_RECORD
* nested_frame
,
593 int nested_trylevel
)
595 cxx_exception_type
*exc_type
;
597 if (descr
->magic
< CXX_FRAME_MAGIC_VC6
|| descr
->magic
> CXX_FRAME_MAGIC_VC8
)
599 ERR( "invalid frame magic %x\n", descr
->magic
);
600 return ExceptionContinueSearch
;
602 if (descr
->magic
>= CXX_FRAME_MAGIC_VC8
&&
603 (descr
->flags
& FUNC_DESCR_SYNCHRONOUS
) &&
604 (rec
->ExceptionCode
!= CXX_EXCEPTION
))
605 return ExceptionContinueSearch
; /* handle only c++ exceptions */
607 if (rec
->ExceptionFlags
& (EH_UNWINDING
|EH_EXIT_UNWIND
))
609 if (descr
->unwind_count
&& !nested_trylevel
) cxx_local_unwind( frame
, descr
, -1 );
610 return ExceptionContinueSearch
;
612 if (!descr
->tryblock_count
) return ExceptionContinueSearch
;
614 if(rec
->ExceptionCode
== CXX_EXCEPTION
&&
615 rec
->ExceptionInformation
[1] == 0 && rec
->ExceptionInformation
[2] == 0)
617 *rec
= *msvcrt_get_thread_data()->exc_record
;
618 rec
->ExceptionFlags
&= ~EH_UNWINDING
;
620 TRACE("detect rethrow: exception code: %x\n", rec
->ExceptionCode
);
621 if(rec
->ExceptionCode
== CXX_EXCEPTION
)
622 TRACE("re-propage: obj: %lx, type: %lx\n",
623 rec
->ExceptionInformation
[1], rec
->ExceptionInformation
[2]);
627 if(rec
->ExceptionCode
== CXX_EXCEPTION
)
629 exc_type
= (cxx_exception_type
*)rec
->ExceptionInformation
[2];
631 if (rec
->ExceptionInformation
[0] > CXX_FRAME_MAGIC_VC8
&&
632 exc_type
->custom_handler
)
634 return exc_type
->custom_handler( rec
, frame
, context
, dispatch
,
635 descr
, nested_trylevel
, nested_frame
, 0 );
640 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
641 rec
, frame
, frame
->trylevel
, descr
, nested_frame
);
642 dump_exception_type( exc_type
);
643 dump_function_descr( descr
);
648 thread_data_t
*data
= msvcrt_get_thread_data();
651 TRACE("handling C exception code %x rec %p frame %p trylevel %d descr %p nested_frame %p\n",
652 rec
->ExceptionCode
, rec
, frame
, frame
->trylevel
, descr
, nested_frame
);
654 if (data
->se_translator
) {
655 EXCEPTION_POINTERS except_ptrs
;
657 except_ptrs
.ExceptionRecord
= rec
;
658 except_ptrs
.ContextRecord
= context
;
659 data
->se_translator(rec
->ExceptionCode
, &except_ptrs
);
663 call_catch_block( rec
, frame
, descr
, frame
->trylevel
, nested_frame
, exc_type
);
664 return ExceptionContinueSearch
;
668 /*********************************************************************
669 * __CxxFrameHandler (MSVCRT.@)
671 extern DWORD CDECL
__CxxFrameHandler( PEXCEPTION_RECORD rec
, EXCEPTION_REGISTRATION_RECORD
* frame
,
672 PCONTEXT context
, EXCEPTION_REGISTRATION_RECORD
** dispatch
);
673 __ASM_GLOBAL_FUNC( __CxxFrameHandler
,
674 "pushl $0\n\t" /* nested_trylevel */
675 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
676 "pushl $0\n\t" /* nested_frame */
677 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
678 "pushl %eax\n\t" /* descr */
679 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
680 "pushl 28(%esp)\n\t" /* dispatch */
681 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
682 "pushl 28(%esp)\n\t" /* context */
683 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
684 "pushl 28(%esp)\n\t" /* frame */
685 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
686 "pushl 28(%esp)\n\t" /* rec */
687 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
688 "call " __ASM_NAME("cxx_frame_handler") "\n\t"
690 __ASM_CFI(".cfi_adjust_cfa_offset -28\n\t")
694 /*********************************************************************
695 * __CxxLongjmpUnwind (MSVCRT.@)
697 * Callback meant to be used as UnwindFunc for setjmp/longjmp.
699 void __stdcall
__CxxLongjmpUnwind( const struct MSVCRT___JUMP_BUFFER
*buf
)
701 cxx_exception_frame
*frame
= (cxx_exception_frame
*)buf
->Registration
;
702 const cxx_function_descr
*descr
= (const cxx_function_descr
*)buf
->UnwindData
[0];
704 TRACE( "unwinding frame %p descr %p trylevel %ld\n", frame
, descr
, buf
->TryLevel
);
705 cxx_local_unwind( frame
, descr
, buf
->TryLevel
);
708 /*********************************************************************
709 * __CppXcptFilter (MSVCRT.@)
711 int CDECL
__CppXcptFilter(NTSTATUS ex
, PEXCEPTION_POINTERS ptr
)
713 /* only filter c++ exceptions */
714 if (ex
!= CXX_EXCEPTION
) return EXCEPTION_CONTINUE_SEARCH
;
715 return _XcptFilter( ex
, ptr
);
718 /*********************************************************************
719 * __CxxDetectRethrow (MSVCRT.@)
721 BOOL CDECL
__CxxDetectRethrow(PEXCEPTION_POINTERS ptrs
)
723 PEXCEPTION_RECORD rec
;
728 rec
= ptrs
->ExceptionRecord
;
730 if (rec
->ExceptionCode
== CXX_EXCEPTION
&&
731 rec
->NumberParameters
== 3 &&
732 rec
->ExceptionInformation
[0] == CXX_FRAME_MAGIC_VC6
&&
733 rec
->ExceptionInformation
[2])
735 ptrs
->ExceptionRecord
= msvcrt_get_thread_data()->exc_record
;
738 return (msvcrt_get_thread_data()->exc_record
== rec
);
741 /*********************************************************************
742 * __CxxQueryExceptionSize (MSVCRT.@)
744 unsigned int CDECL
__CxxQueryExceptionSize(void)
746 return sizeof(cxx_exception_type
);
750 /*********************************************************************
751 * _EH_prolog (MSVCRT.@)
754 /* Provided for VC++ binary compatibility only */
755 __ASM_GLOBAL_FUNC(_EH_prolog
,
756 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") /* skip ret addr */
758 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
760 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
762 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
763 "movl %esp, %fs:0\n\t"
764 "movl 12(%esp), %eax\n\t"
765 "movl %ebp, 12(%esp)\n\t"
766 "leal 12(%esp), %ebp\n\t"
768 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
771 static const SCOPETABLE_V4
*get_scopetable_v4( MSVCRT_EXCEPTION_FRAME
*frame
, ULONG_PTR cookie
)
773 return (const SCOPETABLE_V4
*)((ULONG_PTR
)frame
->scopetable
^ cookie
);
776 static DWORD
MSVCRT_nested_handler(PEXCEPTION_RECORD rec
,
777 EXCEPTION_REGISTRATION_RECORD
* frame
,
779 EXCEPTION_REGISTRATION_RECORD
** dispatch
)
781 if (!(rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
)))
782 return ExceptionContinueSearch
;
784 return ExceptionCollidedUnwind
;
787 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME
* frame
, int trylevel
, void *ebp
)
789 EXCEPTION_REGISTRATION_RECORD reg
;
791 TRACE("(%p,%d,%d)\n",frame
, frame
->trylevel
, trylevel
);
793 /* Register a handler in case of a nested exception */
794 reg
.Handler
= MSVCRT_nested_handler
;
795 reg
.Prev
= NtCurrentTeb()->Tib
.ExceptionList
;
796 __wine_push_frame(®
);
798 while (frame
->trylevel
!= TRYLEVEL_END
&& frame
->trylevel
!= trylevel
)
800 int level
= frame
->trylevel
;
801 frame
->trylevel
= frame
->scopetable
[level
].previousTryLevel
;
802 if (!frame
->scopetable
[level
].lpfnFilter
)
804 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
805 level
, frame
->scopetable
[level
].lpfnHandler
, ebp
);
806 call_unwind_func( frame
->scopetable
[level
].lpfnHandler
, ebp
);
809 __wine_pop_frame(®
);
810 TRACE("unwound OK\n");
813 static void msvcrt_local_unwind4( ULONG
*cookie
, MSVCRT_EXCEPTION_FRAME
* frame
, int trylevel
, void *ebp
)
815 EXCEPTION_REGISTRATION_RECORD reg
;
816 const SCOPETABLE_V4
*scopetable
= get_scopetable_v4( frame
, *cookie
);
818 TRACE("(%p,%d,%d)\n",frame
, frame
->trylevel
, trylevel
);
820 /* Register a handler in case of a nested exception */
821 reg
.Handler
= MSVCRT_nested_handler
;
822 reg
.Prev
= NtCurrentTeb()->Tib
.ExceptionList
;
823 __wine_push_frame(®
);
825 while (frame
->trylevel
!= -2 && frame
->trylevel
!= trylevel
)
827 int level
= frame
->trylevel
;
828 frame
->trylevel
= scopetable
->entries
[level
].previousTryLevel
;
829 if (!scopetable
->entries
[level
].lpfnFilter
)
831 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
832 level
, scopetable
->entries
[level
].lpfnHandler
, ebp
);
833 call_unwind_func( scopetable
->entries
[level
].lpfnHandler
, ebp
);
836 __wine_pop_frame(®
);
837 TRACE("unwound OK\n");
840 /*******************************************************************
841 * _local_unwind2 (MSVCRT.@)
843 void CDECL
_local_unwind2(MSVCRT_EXCEPTION_FRAME
* frame
, int trylevel
)
845 msvcrt_local_unwind2( frame
, trylevel
, &frame
->_ebp
);
848 /*******************************************************************
849 * _local_unwind4 (MSVCRT.@)
851 void CDECL
_local_unwind4( ULONG
*cookie
, MSVCRT_EXCEPTION_FRAME
* frame
, int trylevel
)
853 msvcrt_local_unwind4( cookie
, frame
, trylevel
, &frame
->_ebp
);
856 /*******************************************************************
857 * _global_unwind2 (MSVCRT.@)
859 void CDECL
_global_unwind2(EXCEPTION_REGISTRATION_RECORD
* frame
)
861 TRACE("(%p)\n",frame
);
862 RtlUnwind( frame
, 0, 0, 0 );
865 /*********************************************************************
866 * _except_handler2 (MSVCRT.@)
868 int CDECL
_except_handler2(PEXCEPTION_RECORD rec
,
869 EXCEPTION_REGISTRATION_RECORD
* frame
,
871 EXCEPTION_REGISTRATION_RECORD
** dispatcher
)
873 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
874 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
875 frame
->Handler
, context
, dispatcher
);
876 return ExceptionContinueSearch
;
879 /*********************************************************************
880 * _except_handler3 (MSVCRT.@)
882 int CDECL
_except_handler3(PEXCEPTION_RECORD rec
,
883 MSVCRT_EXCEPTION_FRAME
* frame
,
884 PCONTEXT context
, void* dispatcher
)
886 int retval
, trylevel
;
887 EXCEPTION_POINTERS exceptPtrs
;
888 PSCOPETABLE pScopeTable
;
890 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
891 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
892 frame
->handler
, context
, dispatcher
);
894 __asm__
__volatile__ ("cld");
896 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
898 /* Unwinding the current frame */
899 msvcrt_local_unwind2(frame
, TRYLEVEL_END
, &frame
->_ebp
);
900 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
901 return ExceptionContinueSearch
;
905 /* Hunting for handler */
906 exceptPtrs
.ExceptionRecord
= rec
;
907 exceptPtrs
.ContextRecord
= context
;
908 *((DWORD
*)frame
-1) = (DWORD
)&exceptPtrs
;
909 trylevel
= frame
->trylevel
;
910 pScopeTable
= frame
->scopetable
;
912 while (trylevel
!= TRYLEVEL_END
)
914 TRACE( "level %d prev %d filter %p\n", trylevel
, pScopeTable
[trylevel
].previousTryLevel
,
915 pScopeTable
[trylevel
].lpfnFilter
);
916 if (pScopeTable
[trylevel
].lpfnFilter
)
918 retval
= call_filter( pScopeTable
[trylevel
].lpfnFilter
, &exceptPtrs
, &frame
->_ebp
);
920 TRACE("filter returned %s\n", retval
== EXCEPTION_CONTINUE_EXECUTION
?
921 "CONTINUE_EXECUTION" : retval
== EXCEPTION_EXECUTE_HANDLER
?
922 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
924 if (retval
== EXCEPTION_CONTINUE_EXECUTION
)
925 return ExceptionContinueExecution
;
927 if (retval
== EXCEPTION_EXECUTE_HANDLER
)
929 /* Unwind all higher frames, this one will handle the exception */
930 _global_unwind2((EXCEPTION_REGISTRATION_RECORD
*)frame
);
931 msvcrt_local_unwind2(frame
, trylevel
, &frame
->_ebp
);
933 /* Set our trylevel to the enclosing block, and call the __finally
934 * code, which won't return
936 frame
->trylevel
= pScopeTable
[trylevel
].previousTryLevel
;
937 TRACE("__finally block %p\n",pScopeTable
[trylevel
].lpfnHandler
);
938 call_finally_block(pScopeTable
[trylevel
].lpfnHandler
, &frame
->_ebp
);
939 ERR("Returned from __finally block - expect crash!\n");
942 trylevel
= pScopeTable
[trylevel
].previousTryLevel
;
945 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
946 return ExceptionContinueSearch
;
949 /*********************************************************************
950 * _except_handler4_common (MSVCRT.@)
952 int CDECL
_except_handler4_common( ULONG
*cookie
, void (*check_cookie
)(void),
953 EXCEPTION_RECORD
*rec
, MSVCRT_EXCEPTION_FRAME
*frame
,
954 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
956 int retval
, trylevel
;
957 EXCEPTION_POINTERS exceptPtrs
;
958 const SCOPETABLE_V4
*scope_table
= get_scopetable_v4( frame
, *cookie
);
960 TRACE( "exception %x flags=%x at %p handler=%p %p %p cookie=%x scope table=%p cookies=%d/%x,%d/%x\n",
961 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
,
962 frame
->handler
, context
, dispatcher
, *cookie
, scope_table
,
963 scope_table
->gs_cookie_offset
, scope_table
->gs_cookie_xor
,
964 scope_table
->eh_cookie_offset
, scope_table
->eh_cookie_xor
);
966 /* FIXME: no cookie validation yet */
968 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
970 /* Unwinding the current frame */
971 msvcrt_local_unwind4( cookie
, frame
, -2, &frame
->_ebp
);
972 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
973 return ExceptionContinueSearch
;
977 /* Hunting for handler */
978 exceptPtrs
.ExceptionRecord
= rec
;
979 exceptPtrs
.ContextRecord
= context
;
980 *((DWORD
*)frame
-1) = (DWORD
)&exceptPtrs
;
981 trylevel
= frame
->trylevel
;
983 while (trylevel
!= -2)
985 TRACE( "level %d prev %d filter %p\n", trylevel
,
986 scope_table
->entries
[trylevel
].previousTryLevel
,
987 scope_table
->entries
[trylevel
].lpfnFilter
);
988 if (scope_table
->entries
[trylevel
].lpfnFilter
)
990 retval
= call_filter( scope_table
->entries
[trylevel
].lpfnFilter
, &exceptPtrs
, &frame
->_ebp
);
992 TRACE("filter returned %s\n", retval
== EXCEPTION_CONTINUE_EXECUTION
?
993 "CONTINUE_EXECUTION" : retval
== EXCEPTION_EXECUTE_HANDLER
?
994 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
996 if (retval
== EXCEPTION_CONTINUE_EXECUTION
)
997 return ExceptionContinueExecution
;
999 if (retval
== EXCEPTION_EXECUTE_HANDLER
)
1001 /* Unwind all higher frames, this one will handle the exception */
1002 _global_unwind2((EXCEPTION_REGISTRATION_RECORD
*)frame
);
1003 msvcrt_local_unwind4( cookie
, frame
, trylevel
, &frame
->_ebp
);
1005 /* Set our trylevel to the enclosing block, and call the __finally
1006 * code, which won't return
1008 frame
->trylevel
= scope_table
->entries
[trylevel
].previousTryLevel
;
1009 TRACE("__finally block %p\n",scope_table
->entries
[trylevel
].lpfnHandler
);
1010 call_finally_block(scope_table
->entries
[trylevel
].lpfnHandler
, &frame
->_ebp
);
1011 ERR("Returned from __finally block - expect crash!\n");
1014 trylevel
= scope_table
->entries
[trylevel
].previousTryLevel
;
1017 TRACE("reached -2, returning ExceptionContinueSearch\n");
1018 return ExceptionContinueSearch
;
1023 * setjmp/longjmp implementation
1026 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
1027 typedef void (__stdcall
*MSVCRT_unwind_function
)(const struct MSVCRT___JUMP_BUFFER
*);
1029 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
1030 /* and then jumps to the C backend function */
1031 #define DEFINE_SETJMP_ENTRYPOINT(name) \
1032 __ASM_GLOBAL_FUNC( name, \
1033 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
1034 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
1035 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
1036 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
1037 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
1038 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
1039 "movl 0(%esp),%eax\n\t" \
1040 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
1041 "jmp " __ASM_NAME("__regs_") # name )
1043 /* restore the registers from the jmp buf upon longjmp */
1044 extern void DECLSPEC_NORETURN
longjmp_set_regs( struct MSVCRT___JUMP_BUFFER
*jmp
, int retval
);
1045 __ASM_GLOBAL_FUNC( longjmp_set_regs
,
1046 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
1047 "movl 8(%esp),%eax\n\t" /* retval */
1048 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
1049 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
1050 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
1051 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
1052 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
1053 "addl $4,%esp\n\t" /* get rid of return address */
1054 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
1057 * The signatures of the setjmp/longjmp functions do not match that
1058 * declared in the setjmp header so they don't follow the regular naming
1059 * convention to avoid conflicts.
1062 /*******************************************************************
1063 * _setjmp (MSVCRT.@)
1065 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp
)
1066 int CDECL
__regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER
*jmp
)
1068 jmp
->Registration
= (unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
1069 if (jmp
->Registration
== ~0UL)
1070 jmp
->TryLevel
= TRYLEVEL_END
;
1072 jmp
->TryLevel
= ((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
)->trylevel
;
1074 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
1075 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
);
1079 /*******************************************************************
1080 * _setjmp3 (MSVCRT.@)
1082 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3
)
1083 int CDECL
__regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER
*jmp
, int nb_args
, ...)
1085 jmp
->Cookie
= MSVCRT_JMP_MAGIC
;
1086 jmp
->UnwindFunc
= 0;
1087 jmp
->Registration
= (unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
1088 if (jmp
->Registration
== ~0UL)
1090 jmp
->TryLevel
= TRYLEVEL_END
;
1097 va_start( args
, nb_args
);
1098 if (nb_args
> 0) jmp
->UnwindFunc
= va_arg( args
, unsigned long );
1099 if (nb_args
> 1) jmp
->TryLevel
= va_arg( args
, unsigned long );
1100 else jmp
->TryLevel
= ((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
)->trylevel
;
1101 for (i
= 0; i
< 6 && i
< nb_args
- 2; i
++)
1102 jmp
->UnwindData
[i
] = va_arg( args
, unsigned long );
1106 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
1107 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
);
1111 /*********************************************************************
1112 * longjmp (MSVCRT.@)
1114 void CDECL
MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER
*jmp
, int retval
)
1116 unsigned long cur_frame
= 0;
1118 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
1119 jmp
, jmp
->Ebx
, jmp
->Esi
, jmp
->Edi
, jmp
->Ebp
, jmp
->Esp
, jmp
->Eip
, jmp
->Registration
, retval
);
1121 cur_frame
=(unsigned long)NtCurrentTeb()->Tib
.ExceptionList
;
1122 TRACE("cur_frame=%lx\n",cur_frame
);
1124 if (cur_frame
!= jmp
->Registration
)
1125 _global_unwind2((EXCEPTION_REGISTRATION_RECORD
*)jmp
->Registration
);
1127 if (jmp
->Registration
)
1129 if (IsBadReadPtr(&jmp
->Cookie
, sizeof(long)) || jmp
->Cookie
!= MSVCRT_JMP_MAGIC
)
1131 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
,
1132 jmp
->TryLevel
, (void *)jmp
->Ebp
);
1134 else if(jmp
->UnwindFunc
)
1136 MSVCRT_unwind_function unwind_func
;
1138 unwind_func
=(MSVCRT_unwind_function
)jmp
->UnwindFunc
;
1146 longjmp_set_regs( jmp
, retval
);
1149 /*********************************************************************
1150 * _seh_longjmp_unwind (MSVCRT.@)
1152 void __stdcall
_seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER
*jmp
)
1154 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
, jmp
->TryLevel
, (void *)jmp
->Ebp
);
1157 /*********************************************************************
1158 * _seh_longjmp_unwind4 (MSVCRT.@)
1160 void __stdcall
_seh_longjmp_unwind4(struct MSVCRT___JUMP_BUFFER
*jmp
)
1162 msvcrt_local_unwind4( (ULONG
*)&jmp
->Cookie
, (MSVCRT_EXCEPTION_FRAME
*)jmp
->Registration
,
1163 jmp
->TryLevel
, (void *)jmp
->Ebp
);
1166 #endif /* __i386__ */