2 * msvcrt C++ exception handling
4 * Copyright 2002 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * A good reference is the article "How a C++ compiler implements
22 * exception handling" by Vishal Kochhar, available on
23 * www.thecodeproject.com.
27 #include "wine/port.h"
31 #include "wine/exception.h"
33 #include "wine/debug.h"
35 #include "cppexcept.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(seh);
39 #ifdef __i386__ /* CxxFrameHandler is not supported on non-i386 */
41 static DWORD cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
42 PCONTEXT exc_context, EXCEPTION_FRAME** dispatch,
43 cxx_function_descr *descr, EXCEPTION_FRAME* nested_frame,
44 int nested_trylevel, CONTEXT86 *context );
46 /* call a function with a given ebp */
47 inline static void *call_ebp_func( void *func, void *ebp )
50 __asm__ __volatile__ ("pushl %%ebp; movl %2,%%ebp; call *%%eax; popl %%ebp" \
51 : "=a" (ret) : "0" (func), "g" (ebp) : "ecx", "edx", "memory" );
55 /* call a copy constructor */
56 inline static void call_copy_ctor( void *func, void *this, void *src, int has_vbase )
58 TRACE( "calling copy ctor %p object %p src %p\n", func, this, src );
60 /* in that case copy ctor takes an extra bool indicating whether to copy the base class */
61 __asm__ __volatile__("pushl $1; pushl %2; call *%0"
62 : : "r" (func), "c" (this), "g" (src) : "eax", "edx", "memory" );
64 __asm__ __volatile__("pushl %2; call *%0"
65 : : "r" (func), "c" (this), "g" (src) : "eax", "edx", "memory" );
68 /* call the destructor of the exception object */
69 inline static void call_dtor( void *func, void *object )
71 __asm__ __volatile__("call *%0" : : "r" (func), "c" (object) : "eax", "edx", "memory" );
74 static void dump_type( const cxx_type_info *type )
76 DPRINTF( "flags %x type %p", type->flags, type->type_info );
77 if (type->type_info) DPRINTF( " (%p %s)", type->type_info->name, type->type_info->mangled );
78 DPRINTF( " offset %d vbase %d,%d size %d copy ctor %p\n", type->this_offset,
79 type->vbase_descr, type->vbase_offset, type->size, type->copy_ctor );
82 static void dump_exception_type( const cxx_exception_type *type )
86 DPRINTF( "exception type:\n" );
87 DPRINTF( "flags %x destr %p handler %p type info %p\n",
88 type->flags, type->destructor, type->custom_handler, type->type_info_table );
89 for (i = 0; i < type->type_info_table->count; i++)
91 DPRINTF( " %d: ", i );
92 dump_type( type->type_info_table->info[i] );
96 static void dump_function_descr( const cxx_function_descr *descr, const cxx_exception_type *info )
101 DPRINTF( "function descr:\n" );
102 DPRINTF( "magic %x\n", descr->magic );
103 DPRINTF( "unwind table: %p %d\n", descr->unwind_table, descr->unwind_count );
104 for (i = 0; i < descr->unwind_count; i++)
106 DPRINTF( " %d: prev %d func %p\n", i,
107 descr->unwind_table[i].prev, descr->unwind_table[i].handler );
109 DPRINTF( "try table: %p %d\n", descr->tryblock, descr->tryblock_count );
110 for (i = 0; i < descr->tryblock_count; i++)
112 DPRINTF( " %d: start %d end %d catchlevel %d catch %p %d\n", i,
113 descr->tryblock[i].start_level, descr->tryblock[i].end_level,
114 descr->tryblock[i].catch_level, descr->tryblock[i].catchblock,
115 descr->tryblock[i].catchblock_count );
116 for (j = 0; j < descr->tryblock[i].catchblock_count; j++)
118 catchblock_info *ptr = &descr->tryblock[i].catchblock[j];
119 DPRINTF( " %d: flags %x offset %d handler %p type %p",
120 j, ptr->flags, ptr->offset, ptr->handler, ptr->type_info );
121 if (ptr->type_info) DPRINTF( " (%p %s)", ptr->type_info->name, ptr->type_info->mangled );
127 /* compute the this pointer for a base class of a given type */
128 static void *get_this_pointer( const cxx_type_info *type, void *object )
133 if (!object) return NULL;
134 this_ptr = (char *)object + type->this_offset;
135 if (type->vbase_descr >= 0)
137 /* move this ptr to vbase descriptor */
138 this_ptr = (char *)this_ptr + type->vbase_descr;
139 /* and fetch additional offset from vbase descriptor */
140 offset_ptr = (int *)(*(char **)this_ptr + type->vbase_offset);
141 this_ptr = (char *)this_ptr + *offset_ptr;
146 /* check if the exception type is caught by a given catch block, and return the type that matched */
147 static const cxx_type_info *find_caught_type( cxx_exception_type *exc_type, catchblock_info *catchblock )
151 for (i = 0; i < exc_type->type_info_table->count; i++)
153 const cxx_type_info *type = exc_type->type_info_table->info[i];
155 if (!catchblock->type_info) return type; /* catch(...) matches any type */
156 if (catchblock->type_info != type->type_info)
158 if (strcmp( catchblock->type_info->mangled, type->type_info->mangled )) continue;
160 /* type is the same, now check the flags */
161 if ((exc_type->flags & TYPE_FLAG_CONST) &&
162 !(catchblock->flags & TYPE_FLAG_CONST)) continue;
163 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
164 !(catchblock->flags & TYPE_FLAG_VOLATILE)) continue;
165 return type; /* it matched */
171 /* copy the exception object where the catch block wants it */
172 static void copy_exception( void *object, cxx_exception_frame *frame,
173 catchblock_info *catchblock, const cxx_type_info *type )
177 if (!catchblock->type_info || !catchblock->type_info->mangled[0]) return;
178 if (!catchblock->offset) return;
179 dest_ptr = (void **)((char *)&frame->ebp + catchblock->offset);
181 if (catchblock->flags & TYPE_FLAG_REFERENCE)
183 *dest_ptr = get_this_pointer( type, object );
185 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
187 memmove( dest_ptr, object, type->size );
188 /* if it is a pointer, adjust it */
189 if (type->size == sizeof(void *)) *dest_ptr = get_this_pointer( type, *dest_ptr );
191 else /* copy the object */
194 call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(type,object),
195 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
197 memmove( dest_ptr, get_this_pointer(type,object), type->size );
201 /* unwind the local function up to a given trylevel */
202 static void cxx_local_unwind( cxx_exception_frame* frame, cxx_function_descr *descr, int last_level)
205 int trylevel = frame->trylevel;
207 while (trylevel != last_level)
209 if (trylevel < 0 || trylevel >= descr->unwind_count)
211 ERR( "invalid trylevel %d\n", trylevel );
214 handler = descr->unwind_table[trylevel].handler;
217 TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
218 handler, trylevel, last_level, &frame->ebp );
219 call_ebp_func( handler, &frame->ebp );
221 trylevel = descr->unwind_table[trylevel].prev;
223 frame->trylevel = last_level;
226 /* exception frame for nested exceptions in catch block */
227 struct catch_func_nested_frame
229 EXCEPTION_FRAME frame; /* standard exception frame */
230 EXCEPTION_RECORD *prev_rec; /* previous record to restore in thread data */
231 cxx_exception_frame *cxx_frame; /* frame of parent exception */
232 cxx_function_descr *descr; /* descriptor of parent exception */
233 int trylevel; /* current try level */
236 /* handler for exceptions happening while calling a catch function */
237 static DWORD catch_function_nested_handler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
238 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
240 struct catch_func_nested_frame *nested_frame = (struct catch_func_nested_frame *)frame;
242 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
244 msvcrt_get_thread_data()->exc_record = nested_frame->prev_rec;
245 return ExceptionContinueSearch;
249 TRACE( "got nested exception in catch function\n" );
250 return cxx_frame_handler( rec, nested_frame->cxx_frame, context,
251 NULL, nested_frame->descr, &nested_frame->frame,
252 nested_frame->trylevel, context );
256 /* find and call the appropriate catch block for an exception */
257 /* returns the address to continue execution to after the catch block was called */
258 inline static void *call_catch_block( PEXCEPTION_RECORD rec, cxx_exception_frame *frame,
259 cxx_function_descr *descr, int nested_trylevel,
260 cxx_exception_type *info )
264 void *addr, *object = (void *)rec->ExceptionInformation[1];
265 struct catch_func_nested_frame nested_frame;
266 int trylevel = frame->trylevel;
267 MSVCRT_thread_data *thread_data = msvcrt_get_thread_data();
269 for (i = 0; i < descr->tryblock_count; i++)
271 tryblock_info *tryblock = &descr->tryblock[i];
273 if (trylevel < tryblock->start_level) continue;
274 if (trylevel > tryblock->end_level) continue;
276 /* got a try block */
277 for (j = 0; j < tryblock->catchblock_count; j++)
279 catchblock_info *catchblock = &tryblock->catchblock[j];
280 const cxx_type_info *type = find_caught_type( info, catchblock );
283 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
285 /* copy the exception to its destination on the stack */
286 copy_exception( object, frame, catchblock, type );
288 /* unwind the stack */
289 RtlUnwind( frame, 0, rec, 0 );
290 cxx_local_unwind( frame, descr, tryblock->start_level );
291 frame->trylevel = tryblock->end_level + 1;
293 /* call the catch block */
294 TRACE( "calling catch block %p for type %p addr %p ebp %p\n",
295 catchblock, type, catchblock->handler, &frame->ebp );
297 /* setup an exception block for nested exceptions */
299 nested_frame.frame.Handler = catch_function_nested_handler;
300 nested_frame.prev_rec = thread_data->exc_record;
301 nested_frame.cxx_frame = frame;
302 nested_frame.descr = descr;
303 nested_frame.trylevel = nested_trylevel + 1;
305 __wine_push_frame( &nested_frame.frame );
306 thread_data->exc_record = rec;
307 addr = call_ebp_func( catchblock->handler, &frame->ebp );
308 thread_data->exc_record = nested_frame.prev_rec;
309 __wine_pop_frame( &nested_frame.frame );
311 if (info->destructor) call_dtor( info->destructor, object );
312 TRACE( "done, continuing at %p\n", addr );
320 /*********************************************************************
323 * Implementation of __CxxFrameHandler.
325 static DWORD cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
326 PCONTEXT exc_context, EXCEPTION_FRAME** dispatch,
327 cxx_function_descr *descr, EXCEPTION_FRAME* nested_frame,
328 int nested_trylevel, CONTEXT86 *context )
330 cxx_exception_type *exc_type;
333 if (descr->magic != CXX_FRAME_MAGIC)
335 ERR( "invalid frame magic %x\n", descr->magic );
336 return ExceptionContinueSearch;
338 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
340 if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
341 return ExceptionContinueSearch;
343 if (!descr->tryblock_count) return ExceptionContinueSearch;
345 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
346 if (rec->ExceptionCode == CXX_EXCEPTION &&
347 rec->ExceptionInformation[0] > CXX_FRAME_MAGIC &&
348 exc_type->custom_handler)
350 return exc_type->custom_handler( rec, frame, exc_context, dispatch,
351 descr, nested_trylevel, nested_frame, 0 );
354 if (!exc_type) /* nested exception, fetch info from original exception */
356 rec = msvcrt_get_thread_data()->exc_record;
357 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
362 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
363 rec, frame, frame->trylevel, descr, nested_frame );
364 dump_exception_type( exc_type );
365 dump_function_descr( descr, exc_type );
368 next_ip = call_catch_block( rec, frame, descr, frame->trylevel, exc_type );
370 if (!next_ip) return ExceptionContinueSearch;
371 rec->ExceptionFlags &= ~EH_NONCONTINUABLE;
372 context->Eip = (DWORD)next_ip;
373 context->Ebp = (DWORD)&frame->ebp;
374 context->Esp = ((DWORD*)frame)[-1];
375 return ExceptionContinueExecution;
379 /*********************************************************************
380 * __CxxFrameHandler (MSVCRT.@)
382 void __CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_FRAME* frame,
383 PCONTEXT exc_context, EXCEPTION_FRAME** dispatch,
386 cxx_function_descr *descr = (cxx_function_descr *)context->Eax;
387 context->Eax = cxx_frame_handler( rec, (cxx_exception_frame *)frame,
388 exc_context, dispatch, descr, NULL, 0, context );
391 #endif /* __i386__ */
393 /*********************************************************************
394 * _CxxThrowException (MSVCRT.@)
396 void _CxxThrowException( void *object, const cxx_exception_type *type )
400 args[0] = CXX_FRAME_MAGIC;
401 args[1] = (DWORD)object;
402 args[2] = (DWORD)type;
403 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );
406 /*********************************************************************
407 * __CxxDetectRethrow (MSVCRT.@)
409 BOOL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
411 PEXCEPTION_RECORD rec;
416 rec = ptrs->ExceptionRecord;
418 if (rec->ExceptionCode == CXX_EXCEPTION &&
419 rec->NumberParameters == 3 &&
420 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC &&
421 rec->ExceptionInformation[2])
423 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
426 return (msvcrt_get_thread_data()->exc_record == rec);
429 /*********************************************************************
430 * __CxxQueryExceptionSize (MSVCRT.@)
432 unsigned int __CxxQueryExceptionSize(void)
434 return sizeof(cxx_exception_type);