msvcrt: Destroy exception object if it's no longer used when exiting catch.
[wine.git] / dlls / msvcrt / except_i386.c
blob116a429054d12ca98562ec218850b628dad723f4
1 /*
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
22 * NOTES
23 * A good reference is the article "How a C++ compiler implements
24 * exception handling" by Vishal Kochhar, available on
25 * www.thecodeproject.com.
28 #include "config.h"
29 #include "wine/port.h"
31 #ifdef __i386__
33 #include <stdarg.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winternl.h"
38 #include "msvcrt.h"
39 #include "wine/exception.h"
40 #include "excpt.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 */
52 int trylevel;
53 DWORD ebp;
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 */
63 } catchblock_info;
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 */
76 } tryblock_info;
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 */
83 } unwind_info;
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 */
93 UINT ipmap_count;
94 const void *ipmap;
95 const void *expect_list; /* expected exceptions list when magic >= VC7 */
96 UINT flags; /* flags when magic >= VC8 */
97 } cxx_function_descr;
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;
114 int trylevel;
115 int _ebp;
116 PEXCEPTION_POINTERS xpointers;
117 } MSVCRT_EXCEPTION_FRAME;
119 typedef struct
121 int gs_cookie_offset;
122 ULONG gs_cookie_xor;
123 int eh_cookie_offset;
124 ULONG eh_cookie_xor;
125 SCOPETABLE entries[1];
126 } SCOPETABLE_V4;
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 )
138 void *ret;
139 int dummy;
140 __asm__ __volatile__ ("pushl %%ebx\n\t"
141 "pushl %%ebp\n\t"
142 "movl %4,%%ebp\n\t"
143 "call *%%eax\n\t"
144 "popl %%ebp\n\t"
145 "popl %%ebx"
146 : "=a" (ret), "=S" (dummy), "=D" (dummy)
147 : "0" (func), "1" (ebp) : "ecx", "edx", "memory" );
148 return ret;
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 );
155 if (has_vbase)
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" );
159 else
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 )
186 int ret;
187 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
188 : "=a" (ret)
189 : "0" (func), "r" (ebp), "r" (arg)
190 : "ecx", "edx", "memory" );
191 return ret;
194 static inline int call_unwind_func( int (*func)(void), void *ebp )
196 int ret;
197 __asm__ __volatile__ ("pushl %%ebp\n\t"
198 "pushl %%ebx\n\t"
199 "pushl %%esi\n\t"
200 "pushl %%edi\n\t"
201 "movl %2,%%ebp\n\t"
202 "call *%0\n\t"
203 "popl %%edi\n\t"
204 "popl %%esi\n\t"
205 "popl %%ebx\n\t"
206 "popl %%ebp"
207 : "=a" (ret)
208 : "0" (func), "r" (ebp)
209 : "ecx", "edx", "memory" );
210 return ret;
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 )
223 UINT i;
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++)
229 TRACE( " %d: ", i );
230 dump_type( type->type_info_table->info[i] );
234 static void dump_function_descr( const cxx_function_descr *descr )
236 UINT i;
237 int j;
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 type_info *catch_ti, UINT catch_flags )
271 UINT i;
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 (!catch_ti) return type; /* catch(...) matches any type */
278 if (catch_ti != type->type_info)
280 if (strcmp( catch_ti->mangled, type->type_info->mangled )) continue;
282 /* type is the same, now check the flags */
283 if ((exc_type->flags & TYPE_FLAG_CONST) &&
284 !(catch_flags & TYPE_FLAG_CONST)) continue;
285 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
286 !(catch_flags & TYPE_FLAG_VOLATILE)) continue;
287 return type; /* it matched */
289 return NULL;
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 )
297 void **dest_ptr;
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 */
315 if (type->copy_ctor)
316 call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(&type->offsets,object),
317 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
318 else
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 );
334 MSVCRT_terminate();
336 handler = descr->unwind_table[trylevel].handler;
337 if (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) ||
377 (prev_rec->ExceptionCode == CXX_EXCEPTION &&
378 rec->ExceptionInformation[1] == prev_rec->ExceptionInformation[1] &&
379 rec->ExceptionInformation[2] == prev_rec->ExceptionInformation[2]))
381 /* exception was rethrown */
382 *rec = *prev_rec;
383 rec->ExceptionFlags &= ~EH_UNWINDING;
384 if(TRACE_ON(seh)) {
385 TRACE("detect rethrow: exception code: %x\n", rec->ExceptionCode);
386 if(rec->ExceptionCode == CXX_EXCEPTION)
387 TRACE("re-propage: obj: %lx, type: %lx\n",
388 rec->ExceptionInformation[1], rec->ExceptionInformation[2]);
391 else if (nested_frame->prev_rec && nested_frame->prev_rec->ExceptionCode == CXX_EXCEPTION &&
392 nested_frame->prev_rec->ExceptionInformation[1] == prev_rec->ExceptionInformation[1] &&
393 nested_frame->prev_rec->ExceptionInformation[2] == prev_rec->ExceptionInformation[2])
395 TRACE("detect threw new exception in catch block - not owning old(obj: %lx type: %lx)\n",
396 prev_rec->ExceptionInformation[1], prev_rec->ExceptionInformation[2]);
398 else if (prev_rec->ExceptionCode == CXX_EXCEPTION) {
399 /* new exception in exception handler, destroy old */
400 void *object = (void*)prev_rec->ExceptionInformation[1];
401 cxx_exception_type *info = (cxx_exception_type*) prev_rec->ExceptionInformation[2];
402 TRACE("detect threw new exception in catch block - destroy old(obj: %p type: %p)\n",
403 object, info);
404 if(info && info->destructor)
405 call_dtor( info->destructor, object );
407 else
409 TRACE("detect threw new exception in catch block\n");
413 return cxx_frame_handler( rec, nested_frame->cxx_frame, context,
414 NULL, nested_frame->descr, &nested_frame->frame,
415 nested_frame->trylevel );
418 /*********************************************************************
419 * _IsExceptionObjectToBeDestroyed (MSVCR80.@)
421 BOOL __cdecl _IsExceptionObjectToBeDestroyed(const void *obj)
423 EXCEPTION_REGISTRATION_RECORD *reg = NtCurrentTeb()->Tib.ExceptionList;
425 TRACE( "%p\n", obj );
427 while (reg != (EXCEPTION_REGISTRATION_RECORD*)-1)
429 if (reg->Handler == catch_function_nested_handler)
431 EXCEPTION_RECORD *rec = ((struct catch_func_nested_frame*)reg)->rec;
433 if(!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
434 && rec->ExceptionCode == CXX_EXCEPTION && rec->NumberParameters == 3
435 && rec->ExceptionInformation[1] == (LONG_PTR)obj)
436 return FALSE;
439 reg = reg->Prev;
442 return TRUE;
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 )
452 UINT i;
453 int j;
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 if (trylevel < tryblock->start_level) continue;
465 if (trylevel > tryblock->end_level) continue;
467 /* got a try block */
468 for (j = 0; j < tryblock->catchblock_count; j++)
470 const catchblock_info *catchblock = &tryblock->catchblock[j];
471 if(info)
473 const cxx_type_info *type = find_caught_type( info,
474 catchblock->type_info, catchblock->flags );
475 if (!type) continue;
477 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
479 /* copy the exception to its destination on the stack */
480 copy_exception( object, frame, catchblock, type );
482 else
484 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
485 if(catchblock->type_info)
486 continue;
487 TRACE("found catch(...) block\n");
490 /* unwind the stack */
491 RtlUnwind( catch_frame ? catch_frame : &frame->frame, 0, rec, 0 );
492 cxx_local_unwind( frame, descr, tryblock->start_level );
493 frame->trylevel = tryblock->end_level + 1;
495 /* call the catch block */
496 TRACE( "calling catch block %p addr %p ebp %p\n",
497 catchblock, catchblock->handler, &frame->ebp );
499 /* setup an exception block for nested exceptions */
501 nested_frame.frame.Handler = catch_function_nested_handler;
502 nested_frame.prev_rec = thread_data->exc_record;
503 nested_frame.cxx_frame = frame;
504 nested_frame.descr = descr;
505 nested_frame.trylevel = nested_trylevel + 1;
506 nested_frame.rec = rec;
508 __wine_push_frame( &nested_frame.frame );
509 thread_data->exc_record = rec;
510 addr = call_ebp_func( catchblock->handler, &frame->ebp );
511 thread_data->exc_record = nested_frame.prev_rec;
512 __wine_pop_frame( &nested_frame.frame );
514 ((DWORD*)frame)[-1] = save_esp;
515 if (info && info->destructor && _IsExceptionObjectToBeDestroyed(object))
516 call_dtor( info->destructor, object );
517 TRACE( "done, continuing at %p\n", addr );
519 continue_after_catch( frame, addr );
524 /*********************************************************************
525 * __CxxExceptionFilter (MSVCRT.@)
527 int CDECL __CxxExceptionFilter( PEXCEPTION_POINTERS ptrs,
528 const type_info *ti, int flags, void **copy)
530 const cxx_type_info *type;
531 PEXCEPTION_RECORD rec;
533 TRACE( "%p %p %x %p\n", ptrs, ti, flags, copy );
535 if (!ptrs) return EXCEPTION_CONTINUE_SEARCH;
537 /* handle catch(...) */
538 if (!ti) return EXCEPTION_EXECUTE_HANDLER;
540 rec = ptrs->ExceptionRecord;
541 if (rec->ExceptionCode != CXX_EXCEPTION || rec->NumberParameters != 3 ||
542 rec->ExceptionInformation[0] < CXX_FRAME_MAGIC_VC6 ||
543 rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8)
544 return EXCEPTION_CONTINUE_SEARCH;
546 if (rec->ExceptionInformation[1] == 0 && rec->ExceptionInformation[2] == 0)
548 rec = msvcrt_get_thread_data()->exc_record;
549 if (!rec) return EXCEPTION_CONTINUE_SEARCH;
552 type = find_caught_type( (cxx_exception_type*)rec->ExceptionInformation[2], ti, flags );
553 if (!type) return EXCEPTION_CONTINUE_SEARCH;
555 if (copy)
557 void *object = (void *)rec->ExceptionInformation[1];
559 if (flags & TYPE_FLAG_REFERENCE)
561 *copy = get_this_pointer( &type->offsets, object );
563 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
565 memmove( copy, object, type->size );
566 /* if it is a pointer, adjust it */
567 if (type->size == sizeof(void*)) *copy = get_this_pointer( &type->offsets, *copy );
569 else /* copy the object */
571 if (type->copy_ctor)
572 call_copy_ctor( type->copy_ctor, copy, get_this_pointer(&type->offsets,object),
573 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
574 else
575 memmove( copy, get_this_pointer(&type->offsets,object), type->size );
578 return EXCEPTION_EXECUTE_HANDLER;
581 /*********************************************************************
582 * cxx_frame_handler
584 * Implementation of __CxxFrameHandler.
586 DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
587 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
588 const cxx_function_descr *descr,
589 EXCEPTION_REGISTRATION_RECORD* nested_frame,
590 int nested_trylevel )
592 cxx_exception_type *exc_type;
594 if (descr->magic < CXX_FRAME_MAGIC_VC6 || descr->magic > CXX_FRAME_MAGIC_VC8)
596 ERR( "invalid frame magic %x\n", descr->magic );
597 return ExceptionContinueSearch;
599 if (descr->magic >= CXX_FRAME_MAGIC_VC8 &&
600 (descr->flags & FUNC_DESCR_SYNCHRONOUS) &&
601 (rec->ExceptionCode != CXX_EXCEPTION))
602 return ExceptionContinueSearch; /* handle only c++ exceptions */
604 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
606 if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
607 return ExceptionContinueSearch;
609 if (!descr->tryblock_count) return ExceptionContinueSearch;
611 if(rec->ExceptionCode == CXX_EXCEPTION &&
612 rec->ExceptionInformation[1] == 0 && rec->ExceptionInformation[2] == 0)
614 *rec = *msvcrt_get_thread_data()->exc_record;
615 rec->ExceptionFlags &= ~EH_UNWINDING;
616 if(TRACE_ON(seh)) {
617 TRACE("detect rethrow: exception code: %x\n", rec->ExceptionCode);
618 if(rec->ExceptionCode == CXX_EXCEPTION)
619 TRACE("re-propage: obj: %lx, type: %lx\n",
620 rec->ExceptionInformation[1], rec->ExceptionInformation[2]);
624 if(rec->ExceptionCode == CXX_EXCEPTION)
626 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
628 if (rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8 &&
629 exc_type->custom_handler)
631 return exc_type->custom_handler( rec, frame, context, dispatch,
632 descr, nested_trylevel, nested_frame, 0 );
635 if (TRACE_ON(seh))
637 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
638 rec, frame, frame->trylevel, descr, nested_frame );
639 dump_exception_type( exc_type );
640 dump_function_descr( descr );
643 else
645 thread_data_t *data = msvcrt_get_thread_data();
647 exc_type = NULL;
648 TRACE("handling C exception code %x rec %p frame %p trylevel %d descr %p nested_frame %p\n",
649 rec->ExceptionCode, rec, frame, frame->trylevel, descr, nested_frame );
651 if (data->se_translator) {
652 EXCEPTION_POINTERS except_ptrs;
654 except_ptrs.ExceptionRecord = rec;
655 except_ptrs.ContextRecord = context;
656 data->se_translator(rec->ExceptionCode, &except_ptrs);
660 call_catch_block( rec, frame, descr, frame->trylevel, nested_frame, exc_type );
661 return ExceptionContinueSearch;
665 /*********************************************************************
666 * __CxxFrameHandler (MSVCRT.@)
668 extern DWORD CDECL __CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_REGISTRATION_RECORD* frame,
669 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch );
670 __ASM_GLOBAL_FUNC( __CxxFrameHandler,
671 "pushl $0\n\t" /* nested_trylevel */
672 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
673 "pushl $0\n\t" /* nested_frame */
674 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
675 "pushl %eax\n\t" /* descr */
676 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
677 "pushl 28(%esp)\n\t" /* dispatch */
678 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
679 "pushl 28(%esp)\n\t" /* context */
680 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
681 "pushl 28(%esp)\n\t" /* frame */
682 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
683 "pushl 28(%esp)\n\t" /* rec */
684 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
685 "call " __ASM_NAME("cxx_frame_handler") "\n\t"
686 "add $28,%esp\n\t"
687 __ASM_CFI(".cfi_adjust_cfa_offset -28\n\t")
688 "ret" )
691 /*********************************************************************
692 * __CxxLongjmpUnwind (MSVCRT.@)
694 * Callback meant to be used as UnwindFunc for setjmp/longjmp.
696 void __stdcall __CxxLongjmpUnwind( const struct MSVCRT___JUMP_BUFFER *buf )
698 cxx_exception_frame *frame = (cxx_exception_frame *)buf->Registration;
699 const cxx_function_descr *descr = (const cxx_function_descr *)buf->UnwindData[0];
701 TRACE( "unwinding frame %p descr %p trylevel %ld\n", frame, descr, buf->TryLevel );
702 cxx_local_unwind( frame, descr, buf->TryLevel );
705 /*********************************************************************
706 * __CppXcptFilter (MSVCRT.@)
708 int CDECL __CppXcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
710 /* only filter c++ exceptions */
711 if (ex != CXX_EXCEPTION) return EXCEPTION_CONTINUE_SEARCH;
712 return _XcptFilter( ex, ptr );
715 /*********************************************************************
716 * __CxxDetectRethrow (MSVCRT.@)
718 BOOL CDECL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
720 PEXCEPTION_RECORD rec;
722 if (!ptrs)
723 return FALSE;
725 rec = ptrs->ExceptionRecord;
727 if (rec->ExceptionCode == CXX_EXCEPTION &&
728 rec->NumberParameters == 3 &&
729 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC_VC6 &&
730 rec->ExceptionInformation[2])
732 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
733 return TRUE;
735 return (msvcrt_get_thread_data()->exc_record == rec);
738 /*********************************************************************
739 * __CxxQueryExceptionSize (MSVCRT.@)
741 unsigned int CDECL __CxxQueryExceptionSize(void)
743 return sizeof(cxx_exception_type);
747 /*********************************************************************
748 * _EH_prolog (MSVCRT.@)
751 /* Provided for VC++ binary compatibility only */
752 __ASM_GLOBAL_FUNC(_EH_prolog,
753 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") /* skip ret addr */
754 "pushl $-1\n\t"
755 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
756 "pushl %eax\n\t"
757 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
758 "pushl %fs:0\n\t"
759 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
760 "movl %esp, %fs:0\n\t"
761 "movl 12(%esp), %eax\n\t"
762 "movl %ebp, 12(%esp)\n\t"
763 "leal 12(%esp), %ebp\n\t"
764 "pushl %eax\n\t"
765 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
766 "ret")
768 static const SCOPETABLE_V4 *get_scopetable_v4( MSVCRT_EXCEPTION_FRAME *frame, ULONG_PTR cookie )
770 return (const SCOPETABLE_V4 *)((ULONG_PTR)frame->scopetable ^ cookie);
773 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
774 EXCEPTION_REGISTRATION_RECORD* frame,
775 PCONTEXT context,
776 EXCEPTION_REGISTRATION_RECORD** dispatch)
778 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
779 return ExceptionContinueSearch;
780 *dispatch = frame;
781 return ExceptionCollidedUnwind;
784 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp)
786 EXCEPTION_REGISTRATION_RECORD reg;
788 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
790 /* Register a handler in case of a nested exception */
791 reg.Handler = MSVCRT_nested_handler;
792 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
793 __wine_push_frame(&reg);
795 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
797 int level = frame->trylevel;
798 frame->trylevel = frame->scopetable[level].previousTryLevel;
799 if (!frame->scopetable[level].lpfnFilter)
801 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
802 level, frame->scopetable[level].lpfnHandler, ebp );
803 call_unwind_func( frame->scopetable[level].lpfnHandler, ebp );
806 __wine_pop_frame(&reg);
807 TRACE("unwound OK\n");
810 static void msvcrt_local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp )
812 EXCEPTION_REGISTRATION_RECORD reg;
813 const SCOPETABLE_V4 *scopetable = get_scopetable_v4( frame, *cookie );
815 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
817 /* Register a handler in case of a nested exception */
818 reg.Handler = MSVCRT_nested_handler;
819 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
820 __wine_push_frame(&reg);
822 while (frame->trylevel != -2 && frame->trylevel != trylevel)
824 int level = frame->trylevel;
825 frame->trylevel = scopetable->entries[level].previousTryLevel;
826 if (!scopetable->entries[level].lpfnFilter)
828 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
829 level, scopetable->entries[level].lpfnHandler, ebp );
830 call_unwind_func( scopetable->entries[level].lpfnHandler, ebp );
833 __wine_pop_frame(&reg);
834 TRACE("unwound OK\n");
837 /*******************************************************************
838 * _local_unwind2 (MSVCRT.@)
840 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
842 msvcrt_local_unwind2( frame, trylevel, &frame->_ebp );
845 /*******************************************************************
846 * _local_unwind4 (MSVCRT.@)
848 void CDECL _local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel )
850 msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
853 /*******************************************************************
854 * _global_unwind2 (MSVCRT.@)
856 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
858 TRACE("(%p)\n",frame);
859 RtlUnwind( frame, 0, 0, 0 );
862 /*********************************************************************
863 * _except_handler2 (MSVCRT.@)
865 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
866 EXCEPTION_REGISTRATION_RECORD* frame,
867 PCONTEXT context,
868 EXCEPTION_REGISTRATION_RECORD** dispatcher)
870 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
871 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
872 frame->Handler, context, dispatcher);
873 return ExceptionContinueSearch;
876 /*********************************************************************
877 * _except_handler3 (MSVCRT.@)
879 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
880 MSVCRT_EXCEPTION_FRAME* frame,
881 PCONTEXT context, void* dispatcher)
883 int retval, trylevel;
884 EXCEPTION_POINTERS exceptPtrs;
885 PSCOPETABLE pScopeTable;
887 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
888 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
889 frame->handler, context, dispatcher);
891 __asm__ __volatile__ ("cld");
893 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
895 /* Unwinding the current frame */
896 msvcrt_local_unwind2(frame, TRYLEVEL_END, &frame->_ebp);
897 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
898 return ExceptionContinueSearch;
900 else
902 /* Hunting for handler */
903 exceptPtrs.ExceptionRecord = rec;
904 exceptPtrs.ContextRecord = context;
905 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
906 trylevel = frame->trylevel;
907 pScopeTable = frame->scopetable;
909 while (trylevel != TRYLEVEL_END)
911 TRACE( "level %d prev %d filter %p\n", trylevel, pScopeTable[trylevel].previousTryLevel,
912 pScopeTable[trylevel].lpfnFilter );
913 if (pScopeTable[trylevel].lpfnFilter)
915 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
917 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
918 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
919 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
921 if (retval == EXCEPTION_CONTINUE_EXECUTION)
922 return ExceptionContinueExecution;
924 if (retval == EXCEPTION_EXECUTE_HANDLER)
926 /* Unwind all higher frames, this one will handle the exception */
927 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
928 msvcrt_local_unwind2(frame, trylevel, &frame->_ebp);
930 /* Set our trylevel to the enclosing block, and call the __finally
931 * code, which won't return
933 frame->trylevel = pScopeTable[trylevel].previousTryLevel;
934 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
935 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
936 ERR("Returned from __finally block - expect crash!\n");
939 trylevel = pScopeTable[trylevel].previousTryLevel;
942 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
943 return ExceptionContinueSearch;
946 /*********************************************************************
947 * _except_handler4_common (MSVCRT.@)
949 int CDECL _except_handler4_common( ULONG *cookie, void (*check_cookie)(void),
950 EXCEPTION_RECORD *rec, MSVCRT_EXCEPTION_FRAME *frame,
951 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
953 int retval, trylevel;
954 EXCEPTION_POINTERS exceptPtrs;
955 const SCOPETABLE_V4 *scope_table = get_scopetable_v4( frame, *cookie );
957 TRACE( "exception %x flags=%x at %p handler=%p %p %p cookie=%x scope table=%p cookies=%d/%x,%d/%x\n",
958 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
959 frame->handler, context, dispatcher, *cookie, scope_table,
960 scope_table->gs_cookie_offset, scope_table->gs_cookie_xor,
961 scope_table->eh_cookie_offset, scope_table->eh_cookie_xor );
963 /* FIXME: no cookie validation yet */
965 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
967 /* Unwinding the current frame */
968 msvcrt_local_unwind4( cookie, frame, -2, &frame->_ebp );
969 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
970 return ExceptionContinueSearch;
972 else
974 /* Hunting for handler */
975 exceptPtrs.ExceptionRecord = rec;
976 exceptPtrs.ContextRecord = context;
977 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
978 trylevel = frame->trylevel;
980 while (trylevel != -2)
982 TRACE( "level %d prev %d filter %p\n", trylevel,
983 scope_table->entries[trylevel].previousTryLevel,
984 scope_table->entries[trylevel].lpfnFilter );
985 if (scope_table->entries[trylevel].lpfnFilter)
987 retval = call_filter( scope_table->entries[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
989 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
990 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
991 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
993 if (retval == EXCEPTION_CONTINUE_EXECUTION)
994 return ExceptionContinueExecution;
996 if (retval == EXCEPTION_EXECUTE_HANDLER)
998 /* Unwind all higher frames, this one will handle the exception */
999 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
1000 msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
1002 /* Set our trylevel to the enclosing block, and call the __finally
1003 * code, which won't return
1005 frame->trylevel = scope_table->entries[trylevel].previousTryLevel;
1006 TRACE("__finally block %p\n",scope_table->entries[trylevel].lpfnHandler);
1007 call_finally_block(scope_table->entries[trylevel].lpfnHandler, &frame->_ebp);
1008 ERR("Returned from __finally block - expect crash!\n");
1011 trylevel = scope_table->entries[trylevel].previousTryLevel;
1014 TRACE("reached -2, returning ExceptionContinueSearch\n");
1015 return ExceptionContinueSearch;
1020 * setjmp/longjmp implementation
1023 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
1024 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
1026 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
1027 /* and then jumps to the C backend function */
1028 #define DEFINE_SETJMP_ENTRYPOINT(name) \
1029 __ASM_GLOBAL_FUNC( name, \
1030 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
1031 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
1032 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
1033 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
1034 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
1035 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
1036 "movl 0(%esp),%eax\n\t" \
1037 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
1038 "jmp " __ASM_NAME("__regs_") # name )
1040 /* restore the registers from the jmp buf upon longjmp */
1041 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
1042 __ASM_GLOBAL_FUNC( longjmp_set_regs,
1043 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
1044 "movl 8(%esp),%eax\n\t" /* retval */
1045 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
1046 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
1047 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
1048 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
1049 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
1050 "addl $4,%esp\n\t" /* get rid of return address */
1051 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
1054 * The signatures of the setjmp/longjmp functions do not match that
1055 * declared in the setjmp header so they don't follow the regular naming
1056 * convention to avoid conflicts.
1059 /*******************************************************************
1060 * _setjmp (MSVCRT.@)
1062 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
1063 int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
1065 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
1066 if (jmp->Registration == ~0UL)
1067 jmp->TryLevel = TRYLEVEL_END;
1068 else
1069 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
1071 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
1072 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
1073 return 0;
1076 /*******************************************************************
1077 * _setjmp3 (MSVCRT.@)
1079 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
1080 int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
1082 jmp->Cookie = MSVCRT_JMP_MAGIC;
1083 jmp->UnwindFunc = 0;
1084 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
1085 if (jmp->Registration == ~0UL)
1087 jmp->TryLevel = TRYLEVEL_END;
1089 else
1091 int i;
1092 va_list args;
1094 va_start( args, nb_args );
1095 if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
1096 if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
1097 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
1098 for (i = 0; i < 6 && i < nb_args - 2; i++)
1099 jmp->UnwindData[i] = va_arg( args, unsigned long );
1100 va_end( args );
1103 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
1104 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
1105 return 0;
1108 /*********************************************************************
1109 * longjmp (MSVCRT.@)
1111 void CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
1113 unsigned long cur_frame = 0;
1115 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
1116 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
1118 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
1119 TRACE("cur_frame=%lx\n",cur_frame);
1121 if (cur_frame != jmp->Registration)
1122 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
1124 if (jmp->Registration)
1126 if (IsBadReadPtr(&jmp->Cookie, sizeof(long)) || jmp->Cookie != MSVCRT_JMP_MAGIC)
1128 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
1129 jmp->TryLevel, (void *)jmp->Ebp);
1131 else if(jmp->UnwindFunc)
1133 MSVCRT_unwind_function unwind_func;
1135 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
1136 unwind_func(jmp);
1140 if (!retval)
1141 retval = 1;
1143 longjmp_set_regs( jmp, retval );
1146 /*********************************************************************
1147 * _seh_longjmp_unwind (MSVCRT.@)
1149 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
1151 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel, (void *)jmp->Ebp );
1154 /*********************************************************************
1155 * _seh_longjmp_unwind4 (MSVCRT.@)
1157 void __stdcall _seh_longjmp_unwind4(struct MSVCRT___JUMP_BUFFER *jmp)
1159 msvcrt_local_unwind4( (ULONG *)&jmp->Cookie, (MSVCRT_EXCEPTION_FRAME *)jmp->Registration,
1160 jmp->TryLevel, (void *)jmp->Ebp );
1163 #endif /* __i386__ */