blob: ed41ede06a1135edeacdbec9685792bd04febc14 [file] [log] [blame]
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +00001/*
2 * msvcrt C++ exception handling
3 *
4 * Copyright 2002 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 *
20 * NOTES
21 * A good reference is the article "How a C++ compiler implements
22 * exception handling" by Vishal Kochhar, available on
23 * www.thecodeproject.com.
24 */
25
26#include "config.h"
27#include "wine/port.h"
28
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000029#include <stdarg.h>
30
31#include "windef.h"
32#include "winbase.h"
33#include "winreg.h"
Patrik Stridvall9c1de6d2002-09-12 22:07:02 +000034#include "winternl.h"
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000035#include "msvcrt.h"
36#include "wine/exception.h"
Dimitrie O. Paun737d4be2002-12-12 23:34:01 +000037#include "excpt.h"
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000038#include "wine/debug.h"
39
Jon Griffithsc62c1c02003-03-18 18:26:05 +000040#include "cppexcept.h"
41
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000042WINE_DEFAULT_DEBUG_CHANNEL(seh);
43
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000044#ifdef __i386__ /* CxxFrameHandler is not supported on non-i386 */
45
Alexandre Julliard5ad69f12002-10-31 02:10:15 +000046static DWORD cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
Alexandre Julliardee106782003-08-28 03:07:56 +000047 PCONTEXT exc_context, EXCEPTION_REGISTRATION_RECORD** dispatch,
48 cxx_function_descr *descr, EXCEPTION_REGISTRATION_RECORD* nested_frame,
Alexandre Julliard5ad69f12002-10-31 02:10:15 +000049 int nested_trylevel, CONTEXT86 *context );
50
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000051/* call a function with a given ebp */
52inline static void *call_ebp_func( void *func, void *ebp )
53{
54 void *ret;
Alexandre Julliard6bd508f2002-11-01 01:50:51 +000055 __asm__ __volatile__ ("pushl %%ebp; movl %2,%%ebp; call *%%eax; popl %%ebp" \
56 : "=a" (ret) : "0" (func), "g" (ebp) : "ecx", "edx", "memory" );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000057 return ret;
58}
59
60/* call a copy constructor */
61inline static void call_copy_ctor( void *func, void *this, void *src, int has_vbase )
62{
63 TRACE( "calling copy ctor %p object %p src %p\n", func, this, src );
64 if (has_vbase)
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"
Alexandre Julliard6bd508f2002-11-01 01:50:51 +000067 : : "r" (func), "c" (this), "g" (src) : "eax", "edx", "memory" );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000068 else
69 __asm__ __volatile__("pushl %2; call *%0"
Alexandre Julliard6bd508f2002-11-01 01:50:51 +000070 : : "r" (func), "c" (this), "g" (src) : "eax", "edx", "memory" );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000071}
72
73/* call the destructor of the exception object */
74inline static void call_dtor( void *func, void *object )
75{
Alexandre Julliard6bd508f2002-11-01 01:50:51 +000076 __asm__ __volatile__("call *%0" : : "r" (func), "c" (object) : "eax", "edx", "memory" );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000077}
78
Jon Griffithsc62c1c02003-03-18 18:26:05 +000079static void dump_type( const cxx_type_info *type )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000080{
81 DPRINTF( "flags %x type %p", type->flags, type->type_info );
Jon Griffithsc62c1c02003-03-18 18:26:05 +000082 if (type->type_info) DPRINTF( " (%p %s)", type->type_info->name, type->type_info->mangled );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000083 DPRINTF( " offset %d vbase %d,%d size %d copy ctor %p\n", type->this_offset,
84 type->vbase_descr, type->vbase_offset, type->size, type->copy_ctor );
85}
86
Jon Griffithsc62c1c02003-03-18 18:26:05 +000087static void dump_exception_type( const cxx_exception_type *type )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000088{
Jon Griffithsc62c1c02003-03-18 18:26:05 +000089 UINT i;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000090
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++)
95 {
96 DPRINTF( " %d: ", i );
97 dump_type( type->type_info_table->info[i] );
98 }
99}
100
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000101static void dump_function_descr( const cxx_function_descr *descr, const cxx_exception_type *info )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000102{
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000103 UINT i;
104 int j;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000105
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++)
110 {
111 DPRINTF( " %d: prev %d func %p\n", i,
112 descr->unwind_table[i].prev, descr->unwind_table[i].handler );
113 }
114 DPRINTF( "try table: %p %d\n", descr->tryblock, descr->tryblock_count );
115 for (i = 0; i < descr->tryblock_count; i++)
116 {
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++)
122 {
123 catchblock_info *ptr = &descr->tryblock[i].catchblock[j];
124 DPRINTF( " %d: flags %x offset %d handler %p type %p",
125 j, ptr->flags, ptr->offset, ptr->handler, ptr->type_info );
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000126 if (ptr->type_info) DPRINTF( " (%p %s)", ptr->type_info->name, ptr->type_info->mangled );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000127 DPRINTF( "\n" );
128 }
129 }
130}
131
132/* compute the this pointer for a base class of a given type */
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000133static void *get_this_pointer( const cxx_type_info *type, void *object )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000134{
135 void *this_ptr;
136 int *offset_ptr;
137
138 if (!object) return NULL;
139 this_ptr = (char *)object + type->this_offset;
140 if (type->vbase_descr >= 0)
141 {
142 /* move this ptr to vbase descriptor */
143 this_ptr = (char *)this_ptr + type->vbase_descr;
144 /* and fetch additional offset from vbase descriptor */
145 offset_ptr = (int *)(*(char **)this_ptr + type->vbase_offset);
146 this_ptr = (char *)this_ptr + *offset_ptr;
147 }
148 return this_ptr;
149}
150
151/* check if the exception type is caught by a given catch block, and return the type that matched */
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000152static const cxx_type_info *find_caught_type( cxx_exception_type *exc_type, catchblock_info *catchblock )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000153{
154 UINT i;
155
156 for (i = 0; i < exc_type->type_info_table->count; i++)
157 {
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000158 const cxx_type_info *type = exc_type->type_info_table->info[i];
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000159
160 if (!catchblock->type_info) return type; /* catch(...) matches any type */
161 if (catchblock->type_info != type->type_info)
162 {
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000163 if (strcmp( catchblock->type_info->mangled, type->type_info->mangled )) continue;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000164 }
165 /* type is the same, now check the flags */
166 if ((exc_type->flags & TYPE_FLAG_CONST) &&
167 !(catchblock->flags & TYPE_FLAG_CONST)) continue;
168 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
169 !(catchblock->flags & TYPE_FLAG_VOLATILE)) continue;
170 return type; /* it matched */
171 }
172 return NULL;
173}
174
175
176/* copy the exception object where the catch block wants it */
177static void copy_exception( void *object, cxx_exception_frame *frame,
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000178 catchblock_info *catchblock, const cxx_type_info *type )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000179{
180 void **dest_ptr;
181
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000182 if (!catchblock->type_info || !catchblock->type_info->mangled[0]) return;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000183 if (!catchblock->offset) return;
184 dest_ptr = (void **)((char *)&frame->ebp + catchblock->offset);
185
186 if (catchblock->flags & TYPE_FLAG_REFERENCE)
187 {
188 *dest_ptr = get_this_pointer( type, object );
189 }
190 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
191 {
192 memmove( dest_ptr, object, type->size );
193 /* if it is a pointer, adjust it */
194 if (type->size == sizeof(void *)) *dest_ptr = get_this_pointer( type, *dest_ptr );
195 }
196 else /* copy the object */
197 {
198 if (type->copy_ctor)
199 call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(type,object),
200 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
201 else
202 memmove( dest_ptr, get_this_pointer(type,object), type->size );
203 }
204}
205
206/* unwind the local function up to a given trylevel */
207static void cxx_local_unwind( cxx_exception_frame* frame, cxx_function_descr *descr, int last_level)
208{
209 void (*handler)();
210 int trylevel = frame->trylevel;
211
212 while (trylevel != last_level)
213 {
214 if (trylevel < 0 || trylevel >= descr->unwind_count)
215 {
216 ERR( "invalid trylevel %d\n", trylevel );
217 MSVCRT_terminate();
218 }
219 handler = descr->unwind_table[trylevel].handler;
220 if (handler)
221 {
222 TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
223 handler, trylevel, last_level, &frame->ebp );
224 call_ebp_func( handler, &frame->ebp );
225 }
226 trylevel = descr->unwind_table[trylevel].prev;
227 }
228 frame->trylevel = last_level;
229}
230
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000231/* exception frame for nested exceptions in catch block */
232struct catch_func_nested_frame
233{
Alexandre Julliardee106782003-08-28 03:07:56 +0000234 EXCEPTION_REGISTRATION_RECORD frame; /* standard exception frame */
235 EXCEPTION_RECORD *prev_rec; /* previous record to restore in thread data */
236 cxx_exception_frame *cxx_frame; /* frame of parent exception */
237 cxx_function_descr *descr; /* descriptor of parent exception */
238 int trylevel; /* current try level */
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000239};
240
241/* handler for exceptions happening while calling a catch function */
Alexandre Julliardee106782003-08-28 03:07:56 +0000242static DWORD catch_function_nested_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
243 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000244{
245 struct catch_func_nested_frame *nested_frame = (struct catch_func_nested_frame *)frame;
246
247 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
248 {
249 msvcrt_get_thread_data()->exc_record = nested_frame->prev_rec;
250 return ExceptionContinueSearch;
251 }
252 else
253 {
254 TRACE( "got nested exception in catch function\n" );
255 return cxx_frame_handler( rec, nested_frame->cxx_frame, context,
256 NULL, nested_frame->descr, &nested_frame->frame,
257 nested_frame->trylevel, context );
258 }
259}
260
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000261/* find and call the appropriate catch block for an exception */
262/* returns the address to continue execution to after the catch block was called */
263inline static void *call_catch_block( PEXCEPTION_RECORD rec, cxx_exception_frame *frame,
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000264 cxx_function_descr *descr, int nested_trylevel,
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000265 cxx_exception_type *info )
266{
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000267 UINT i;
268 int j;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000269 void *addr, *object = (void *)rec->ExceptionInformation[1];
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000270 struct catch_func_nested_frame nested_frame;
271 int trylevel = frame->trylevel;
272 MSVCRT_thread_data *thread_data = msvcrt_get_thread_data();
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000273
274 for (i = 0; i < descr->tryblock_count; i++)
275 {
276 tryblock_info *tryblock = &descr->tryblock[i];
277
278 if (trylevel < tryblock->start_level) continue;
279 if (trylevel > tryblock->end_level) continue;
280
281 /* got a try block */
282 for (j = 0; j < tryblock->catchblock_count; j++)
283 {
284 catchblock_info *catchblock = &tryblock->catchblock[j];
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000285 const cxx_type_info *type = find_caught_type( info, catchblock );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000286 if (!type) continue;
287
288 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
289
290 /* copy the exception to its destination on the stack */
291 copy_exception( object, frame, catchblock, type );
292
293 /* unwind the stack */
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000294 RtlUnwind( frame, 0, rec, 0 );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000295 cxx_local_unwind( frame, descr, tryblock->start_level );
296 frame->trylevel = tryblock->end_level + 1;
297
298 /* call the catch block */
299 TRACE( "calling catch block %p for type %p addr %p ebp %p\n",
300 catchblock, type, catchblock->handler, &frame->ebp );
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000301
302 /* setup an exception block for nested exceptions */
303
304 nested_frame.frame.Handler = catch_function_nested_handler;
305 nested_frame.prev_rec = thread_data->exc_record;
306 nested_frame.cxx_frame = frame;
307 nested_frame.descr = descr;
308 nested_frame.trylevel = nested_trylevel + 1;
309
310 __wine_push_frame( &nested_frame.frame );
311 thread_data->exc_record = rec;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000312 addr = call_ebp_func( catchblock->handler, &frame->ebp );
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000313 thread_data->exc_record = nested_frame.prev_rec;
314 __wine_pop_frame( &nested_frame.frame );
315
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000316 if (info->destructor) call_dtor( info->destructor, object );
317 TRACE( "done, continuing at %p\n", addr );
318 return addr;
319 }
320 }
321 return NULL;
322}
323
324
325/*********************************************************************
326 * cxx_frame_handler
327 *
328 * Implementation of __CxxFrameHandler.
329 */
330static DWORD cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
Alexandre Julliardee106782003-08-28 03:07:56 +0000331 PCONTEXT exc_context, EXCEPTION_REGISTRATION_RECORD** dispatch,
332 cxx_function_descr *descr, EXCEPTION_REGISTRATION_RECORD* nested_frame,
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000333 int nested_trylevel, CONTEXT86 *context )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000334{
335 cxx_exception_type *exc_type;
336 void *next_ip;
337
338 if (descr->magic != CXX_FRAME_MAGIC)
339 {
340 ERR( "invalid frame magic %x\n", descr->magic );
341 return ExceptionContinueSearch;
342 }
343 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
344 {
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000345 if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000346 return ExceptionContinueSearch;
347 }
348 if (!descr->tryblock_count) return ExceptionContinueSearch;
349
350 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000351 if (rec->ExceptionCode == CXX_EXCEPTION &&
352 rec->ExceptionInformation[0] > CXX_FRAME_MAGIC &&
353 exc_type->custom_handler)
354 {
355 return exc_type->custom_handler( rec, frame, exc_context, dispatch,
356 descr, nested_trylevel, nested_frame, 0 );
357 }
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000358
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000359 if (!exc_type) /* nested exception, fetch info from original exception */
360 {
361 rec = msvcrt_get_thread_data()->exc_record;
362 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
363 }
364
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000365 if (TRACE_ON(seh))
366 {
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000367 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
368 rec, frame, frame->trylevel, descr, nested_frame );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000369 dump_exception_type( exc_type );
370 dump_function_descr( descr, exc_type );
371 }
372
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000373 next_ip = call_catch_block( rec, frame, descr, frame->trylevel, exc_type );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000374
375 if (!next_ip) return ExceptionContinueSearch;
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000376 rec->ExceptionFlags &= ~EH_NONCONTINUABLE;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000377 context->Eip = (DWORD)next_ip;
378 context->Ebp = (DWORD)&frame->ebp;
379 context->Esp = ((DWORD*)frame)[-1];
380 return ExceptionContinueExecution;
381}
382
383
384/*********************************************************************
385 * __CxxFrameHandler (MSVCRT.@)
386 */
Alexandre Julliardf752be82003-12-04 05:51:01 +0000387void MSVCRT__CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_REGISTRATION_RECORD* frame,
388 PCONTEXT exc_context, EXCEPTION_REGISTRATION_RECORD** dispatch,
389 CONTEXT86 *context )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000390{
391 cxx_function_descr *descr = (cxx_function_descr *)context->Eax;
392 context->Eax = cxx_frame_handler( rec, (cxx_exception_frame *)frame,
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000393 exc_context, dispatch, descr, NULL, 0, context );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000394}
Alexandre Julliardf752be82003-12-04 05:51:01 +0000395DEFINE_REGS_ENTRYPOINT( __CxxFrameHandler, MSVCRT__CxxFrameHandler, 16, 0 );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000396
397#endif /* __i386__ */
398
399/*********************************************************************
400 * _CxxThrowException (MSVCRT.@)
401 */
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000402void _CxxThrowException( void *object, const cxx_exception_type *type )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000403{
404 DWORD args[3];
405
406 args[0] = CXX_FRAME_MAGIC;
407 args[1] = (DWORD)object;
408 args[2] = (DWORD)type;
409 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );
410}
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000411
412/*********************************************************************
413 * __CxxDetectRethrow (MSVCRT.@)
414 */
415BOOL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
416{
417 PEXCEPTION_RECORD rec;
418
419 if (!ptrs)
420 return FALSE;
421
422 rec = ptrs->ExceptionRecord;
423
424 if (rec->ExceptionCode == CXX_EXCEPTION &&
425 rec->NumberParameters == 3 &&
426 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC &&
427 rec->ExceptionInformation[2])
428 {
429 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
430 return TRUE;
431 }
432 return (msvcrt_get_thread_data()->exc_record == rec);
433}
434
435/*********************************************************************
436 * __CxxQueryExceptionSize (MSVCRT.@)
437 */
438unsigned int __CxxQueryExceptionSize(void)
439{
440 return sizeof(cxx_exception_type);
441}