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"
36 #include "wine/exception.h"
38 #include "wine/debug.h"
40 #include "cppexcept.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(seh);
44 #ifdef __i386__ /* CxxFrameHandler is not supported on non-i386 */
46 static DWORD cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
47 PCONTEXT exc_context, EXCEPTION_REGISTRATION_RECORD** dispatch,
48 cxx_function_descr *descr, EXCEPTION_REGISTRATION_RECORD* nested_frame,
49 int nested_trylevel, CONTEXT86 *context );
51 /* call a function with a given ebp */
52 inline static void *call_ebp_func( void *func, void *ebp )
55 __asm__ __volatile__ ("pushl %%ebp; movl %2,%%ebp; call *%%eax; popl %%ebp" \
56 : "=a" (ret) : "0" (func), "r" (ebp) : "ecx", "edx", "memory" );
60 /* call a copy constructor */
61 inline static void call_copy_ctor( void *func, void *this, void *src, int has_vbase )
63 TRACE( "calling copy ctor %p object %p src %p\n", func, this, src );
65 /* in that case copy ctor takes an extra bool indicating whether to copy the base class */
66 __asm__ __volatile__("pushl $1; pushl %2; call *%0"
67 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
69 __asm__ __volatile__("pushl %2; call *%0"
70 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
73 /* call the destructor of the exception object */
74 inline static void call_dtor( void *func, void *object )
76 __asm__ __volatile__("call *%0" : : "r" (func), "c" (object) : "eax", "edx", "memory" );
79 static inline void dump_type( const cxx_type_info *type )
81 DPRINTF( "flags %x type %p %s offsets %d,%d,%d size %d copy ctor %p\n",
82 type->flags, type->type_info, dbgstr_type_info(type->type_info),
83 type->offsets.this_offset, type->offsets.vbase_descr, type->offsets.vbase_offset,
84 type->size, type->copy_ctor );
87 static void dump_exception_type( const cxx_exception_type *type )
91 DPRINTF( "exception type:\n" );
92 DPRINTF( "flags %x destr %p handler %p type info %p\n",
93 type->flags, type->destructor, type->custom_handler, type->type_info_table );
94 for (i = 0; i < type->type_info_table->count; i++)
96 DPRINTF( " %d: ", i );
97 dump_type( type->type_info_table->info[i] );
101 static void dump_function_descr( const cxx_function_descr *descr, const cxx_exception_type *info )
106 DPRINTF( "function descr:\n" );
107 DPRINTF( "magic %x\n", descr->magic );
108 DPRINTF( "unwind table: %p %d\n", descr->unwind_table, descr->unwind_count );
109 for (i = 0; i < descr->unwind_count; i++)
111 DPRINTF( " %d: prev %d func %p\n", i,
112 descr->unwind_table[i].prev, descr->unwind_table[i].handler );
114 DPRINTF( "try table: %p %d\n", descr->tryblock, descr->tryblock_count );
115 for (i = 0; i < descr->tryblock_count; i++)
117 DPRINTF( " %d: start %d end %d catchlevel %d catch %p %d\n", i,
118 descr->tryblock[i].start_level, descr->tryblock[i].end_level,
119 descr->tryblock[i].catch_level, descr->tryblock[i].catchblock,
120 descr->tryblock[i].catchblock_count );
121 for (j = 0; j < descr->tryblock[i].catchblock_count; j++)
123 catchblock_info *ptr = &descr->tryblock[i].catchblock[j];
124 DPRINTF( " %d: flags %x offset %d handler %p type %p %s\n",
125 j, ptr->flags, ptr->offset, ptr->handler,
126 ptr->type_info, dbgstr_type_info( ptr->type_info ) );
131 /* check if the exception type is caught by a given catch block, and return the type that matched */
132 static const cxx_type_info *find_caught_type( cxx_exception_type *exc_type, catchblock_info *catchblock )
136 for (i = 0; i < exc_type->type_info_table->count; i++)
138 const cxx_type_info *type = exc_type->type_info_table->info[i];
140 if (!catchblock->type_info) return type; /* catch(...) matches any type */
141 if (catchblock->type_info != type->type_info)
143 if (strcmp( catchblock->type_info->mangled, type->type_info->mangled )) continue;
145 /* type is the same, now check the flags */
146 if ((exc_type->flags & TYPE_FLAG_CONST) &&
147 !(catchblock->flags & TYPE_FLAG_CONST)) continue;
148 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
149 !(catchblock->flags & TYPE_FLAG_VOLATILE)) continue;
150 return type; /* it matched */
156 /* copy the exception object where the catch block wants it */
157 static void copy_exception( void *object, cxx_exception_frame *frame,
158 catchblock_info *catchblock, const cxx_type_info *type )
162 if (!catchblock->type_info || !catchblock->type_info->mangled[0]) return;
163 if (!catchblock->offset) return;
164 dest_ptr = (void **)((char *)&frame->ebp + catchblock->offset);
166 if (catchblock->flags & TYPE_FLAG_REFERENCE)
168 *dest_ptr = get_this_pointer( &type->offsets, object );
170 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
172 memmove( dest_ptr, object, type->size );
173 /* if it is a pointer, adjust it */
174 if (type->size == sizeof(void *)) *dest_ptr = get_this_pointer( &type->offsets, *dest_ptr );
176 else /* copy the object */
179 call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(&type->offsets,object),
180 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
182 memmove( dest_ptr, get_this_pointer(&type->offsets,object), type->size );
186 /* unwind the local function up to a given trylevel */
187 static void cxx_local_unwind( cxx_exception_frame* frame, cxx_function_descr *descr, int last_level)
190 int trylevel = frame->trylevel;
192 while (trylevel != last_level)
194 if (trylevel < 0 || trylevel >= descr->unwind_count)
196 ERR( "invalid trylevel %d\n", trylevel );
199 handler = descr->unwind_table[trylevel].handler;
202 TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
203 handler, trylevel, last_level, &frame->ebp );
204 call_ebp_func( handler, &frame->ebp );
206 trylevel = descr->unwind_table[trylevel].prev;
208 frame->trylevel = last_level;
211 /* exception frame for nested exceptions in catch block */
212 struct catch_func_nested_frame
214 EXCEPTION_REGISTRATION_RECORD frame; /* standard exception frame */
215 EXCEPTION_RECORD *prev_rec; /* previous record to restore in thread data */
216 cxx_exception_frame *cxx_frame; /* frame of parent exception */
217 cxx_function_descr *descr; /* descriptor of parent exception */
218 int trylevel; /* current try level */
221 /* handler for exceptions happening while calling a catch function */
222 static DWORD catch_function_nested_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
223 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
225 struct catch_func_nested_frame *nested_frame = (struct catch_func_nested_frame *)frame;
227 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
229 msvcrt_get_thread_data()->exc_record = nested_frame->prev_rec;
230 return ExceptionContinueSearch;
234 TRACE( "got nested exception in catch function\n" );
235 return cxx_frame_handler( rec, nested_frame->cxx_frame, context,
236 NULL, nested_frame->descr, &nested_frame->frame,
237 nested_frame->trylevel, context );
241 /* find and call the appropriate catch block for an exception */
242 /* returns the address to continue execution to after the catch block was called */
243 inline static void *call_catch_block( PEXCEPTION_RECORD rec, cxx_exception_frame *frame,
244 cxx_function_descr *descr, int nested_trylevel,
245 cxx_exception_type *info )
249 void *addr, *object = (void *)rec->ExceptionInformation[1];
250 struct catch_func_nested_frame nested_frame;
251 int trylevel = frame->trylevel;
252 thread_data_t *thread_data = msvcrt_get_thread_data();
254 for (i = 0; i < descr->tryblock_count; i++)
256 tryblock_info *tryblock = &descr->tryblock[i];
258 if (trylevel < tryblock->start_level) continue;
259 if (trylevel > tryblock->end_level) continue;
261 /* got a try block */
262 for (j = 0; j < tryblock->catchblock_count; j++)
264 catchblock_info *catchblock = &tryblock->catchblock[j];
265 const cxx_type_info *type = find_caught_type( info, catchblock );
268 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
270 /* copy the exception to its destination on the stack */
271 copy_exception( object, frame, catchblock, type );
273 /* unwind the stack */
274 RtlUnwind( frame, 0, rec, 0 );
275 cxx_local_unwind( frame, descr, tryblock->start_level );
276 frame->trylevel = tryblock->end_level + 1;
278 /* call the catch block */
279 TRACE( "calling catch block %p for type %p addr %p ebp %p\n",
280 catchblock, type, catchblock->handler, &frame->ebp );
282 /* setup an exception block for nested exceptions */
284 nested_frame.frame.Handler = catch_function_nested_handler;
285 nested_frame.prev_rec = thread_data->exc_record;
286 nested_frame.cxx_frame = frame;
287 nested_frame.descr = descr;
288 nested_frame.trylevel = nested_trylevel + 1;
290 __wine_push_frame( &nested_frame.frame );
291 thread_data->exc_record = rec;
292 addr = call_ebp_func( catchblock->handler, &frame->ebp );
293 thread_data->exc_record = nested_frame.prev_rec;
294 __wine_pop_frame( &nested_frame.frame );
296 if (info->destructor) call_dtor( info->destructor, object );
297 TRACE( "done, continuing at %p\n", addr );
305 /*********************************************************************
308 * Implementation of __CxxFrameHandler.
310 static DWORD cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
311 PCONTEXT exc_context, EXCEPTION_REGISTRATION_RECORD** dispatch,
312 cxx_function_descr *descr, EXCEPTION_REGISTRATION_RECORD* nested_frame,
313 int nested_trylevel, CONTEXT86 *context )
315 cxx_exception_type *exc_type;
318 if (descr->magic != CXX_FRAME_MAGIC)
320 ERR( "invalid frame magic %x\n", descr->magic );
321 return ExceptionContinueSearch;
323 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
325 if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
326 return ExceptionContinueSearch;
328 if (!descr->tryblock_count) return ExceptionContinueSearch;
330 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
331 if (rec->ExceptionCode == CXX_EXCEPTION &&
332 rec->ExceptionInformation[0] > CXX_FRAME_MAGIC &&
333 exc_type->custom_handler)
335 return exc_type->custom_handler( rec, frame, exc_context, dispatch,
336 descr, nested_trylevel, nested_frame, 0 );
339 if (!exc_type) /* nested exception, fetch info from original exception */
341 rec = msvcrt_get_thread_data()->exc_record;
342 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
347 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
348 rec, frame, frame->trylevel, descr, nested_frame );
349 dump_exception_type( exc_type );
350 dump_function_descr( descr, exc_type );
353 next_ip = call_catch_block( rec, frame, descr, frame->trylevel, exc_type );
355 if (!next_ip) return ExceptionContinueSearch;
356 rec->ExceptionFlags &= ~EH_NONCONTINUABLE;
357 context->Eip = (DWORD)next_ip;
358 context->Ebp = (DWORD)&frame->ebp;
359 context->Esp = ((DWORD*)frame)[-1];
360 return ExceptionContinueExecution;
364 /*********************************************************************
365 * __CxxFrameHandler (MSVCRT.@)
367 void WINAPI __regs___CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_REGISTRATION_RECORD* frame,
368 PCONTEXT exc_context, EXCEPTION_REGISTRATION_RECORD** dispatch,
371 cxx_function_descr *descr = (cxx_function_descr *)context->Eax;
372 context->Eax = cxx_frame_handler( rec, (cxx_exception_frame *)frame,
373 exc_context, dispatch, descr, NULL, 0, context );
375 DEFINE_REGS_ENTRYPOINT( __CxxFrameHandler, 16, 0 );
377 #endif /* __i386__ */
379 /*********************************************************************
380 * _CxxThrowException (MSVCRT.@)
382 void _CxxThrowException( exception *object, const cxx_exception_type *type )
386 args[0] = CXX_FRAME_MAGIC;
387 args[1] = (DWORD)object;
388 args[2] = (DWORD)type;
389 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );
392 /*********************************************************************
393 * __CxxDetectRethrow (MSVCRT.@)
395 BOOL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
397 PEXCEPTION_RECORD rec;
402 rec = ptrs->ExceptionRecord;
404 if (rec->ExceptionCode == CXX_EXCEPTION &&
405 rec->NumberParameters == 3 &&
406 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC &&
407 rec->ExceptionInformation[2])
409 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
412 return (msvcrt_get_thread_data()->exc_record == rec);
415 /*********************************************************************
416 * __CxxQueryExceptionSize (MSVCRT.@)
418 unsigned int __CxxQueryExceptionSize(void)
420 return sizeof(cxx_exception_type);