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.
26 #include "wine/config.h"
27 #include "wine/port.h"
35 #include <internal/wine/msvcrt.h>
36 #include "wine/exception.h"
38 #include "wine/debug.h"
40 #include <internal/wine/cppexcept.h>
42 WINE_DEFAULT_DEBUG_CHANNEL(seh
);
44 #ifdef __i386__ /* CxxFrameHandler is not supported on non-i386 */
45 #define CONTEXT86 CONTEXT //Mingw Hack
47 static DWORD
cxx_frame_handler( PEXCEPTION_RECORD rec
, cxx_exception_frame
* frame
,
48 PCONTEXT exc_context
, EXCEPTION_REGISTRATION_RECORD
** dispatch
,
49 cxx_function_descr
*descr
, EXCEPTION_REGISTRATION_RECORD
* nested_frame
,
50 int nested_trylevel
, CONTEXT86
*context
);
52 /* call a function with a given ebp */
53 inline static void *call_ebp_func( void *func
, void *ebp
)
56 __asm__
__volatile__ ("pushl %%ebp; movl %2,%%ebp; call *%%eax; popl %%ebp" \
57 : "=a" (ret
) : "0" (func
), "g" (ebp
) : "ecx", "edx", "memory" );
61 /* call a copy constructor */
62 inline static void call_copy_ctor( void *func
, void *this, void *src
, int has_vbase
)
64 TRACE( "calling copy ctor %p object %p src %p\n", func
, this, src
);
66 /* in that case copy ctor takes an extra bool indicating whether to copy the base class */
67 __asm__
__volatile__("pushl $1; pushl %2; call *%0"
68 : : "r" (func
), "c" (this), "g" (src
) : "eax", "edx", "memory" );
70 __asm__
__volatile__("pushl %2; call *%0"
71 : : "r" (func
), "c" (this), "g" (src
) : "eax", "edx", "memory" );
74 /* call the destructor of the exception object */
75 inline static void call_dtor( void *func
, void *object
)
77 __asm__
__volatile__("call *%0" : : "r" (func
), "c" (object
) : "eax", "edx", "memory" );
80 static void dump_type( const cxx_type_info
*type
)
82 DPRINTF( "flags %x type %p", type
->flags
, type
->type_info
);
83 if (type
->type_info
) DPRINTF( " (%p %s)", type
->type_info
->name
, type
->type_info
->mangled
);
84 DPRINTF( " offset %d vbase %d,%d size %d copy ctor %p\n", type
->this_offset
,
85 type
->vbase_descr
, type
->vbase_offset
, type
->size
, type
->copy_ctor
);
88 static void dump_exception_type( const cxx_exception_type
*type
)
92 DPRINTF( "exception type:\n" );
93 DPRINTF( "flags %x destr %p handler %p type info %p\n",
94 type
->flags
, type
->destructor
, type
->custom_handler
, type
->type_info_table
);
95 for (i
= 0; i
< type
->type_info_table
->count
; i
++)
97 DPRINTF( " %d: ", i
);
98 dump_type( type
->type_info_table
->info
[i
] );
102 static void dump_function_descr( const cxx_function_descr
*descr
, const cxx_exception_type
*info
)
107 DPRINTF( "function descr:\n" );
108 DPRINTF( "magic %x\n", descr
->magic
);
109 DPRINTF( "unwind table: %p %d\n", descr
->unwind_table
, descr
->unwind_count
);
110 for (i
= 0; i
< descr
->unwind_count
; i
++)
112 DPRINTF( " %d: prev %d func %p\n", i
,
113 descr
->unwind_table
[i
].prev
, descr
->unwind_table
[i
].handler
);
115 DPRINTF( "try table: %p %d\n", descr
->tryblock
, descr
->tryblock_count
);
116 for (i
= 0; i
< descr
->tryblock_count
; i
++)
118 DPRINTF( " %d: start %d end %d catchlevel %d catch %p %d\n", i
,
119 descr
->tryblock
[i
].start_level
, descr
->tryblock
[i
].end_level
,
120 descr
->tryblock
[i
].catch_level
, descr
->tryblock
[i
].catchblock
,
121 descr
->tryblock
[i
].catchblock_count
);
122 for (j
= 0; j
< descr
->tryblock
[i
].catchblock_count
; j
++)
124 catchblock_info
*ptr
= &descr
->tryblock
[i
].catchblock
[j
];
125 DPRINTF( " %d: flags %x offset %d handler %p type %p",
126 j
, ptr
->flags
, ptr
->offset
, ptr
->handler
, ptr
->type_info
);
127 if (ptr
->type_info
) DPRINTF( " (%p %s)", ptr
->type_info
->name
, ptr
->type_info
->mangled
);
133 /* compute the this pointer for a base class of a given type */
134 static void *get_this_pointer( const cxx_type_info
*type
, void *object
)
139 if (!object
) return NULL
;
140 this_ptr
= (char *)object
+ type
->this_offset
;
141 if (type
->vbase_descr
>= 0)
143 /* move this ptr to vbase descriptor */
144 this_ptr
= (char *)this_ptr
+ type
->vbase_descr
;
145 /* and fetch additional offset from vbase descriptor */
146 offset_ptr
= (int *)(*(char **)this_ptr
+ type
->vbase_offset
);
147 this_ptr
= (char *)this_ptr
+ *offset_ptr
;
152 /* check if the exception type is caught by a given catch block, and return the type that matched */
153 static const cxx_type_info
*find_caught_type( cxx_exception_type
*exc_type
, catchblock_info
*catchblock
)
157 for (i
= 0; i
< exc_type
->type_info_table
->count
; i
++)
159 const cxx_type_info
*type
= exc_type
->type_info_table
->info
[i
];
161 if (!catchblock
->type_info
) return type
; /* catch(...) matches any type */
162 if (catchblock
->type_info
!= type
->type_info
)
164 if (strcmp( catchblock
->type_info
->mangled
, type
->type_info
->mangled
)) continue;
166 /* type is the same, now check the flags */
167 if ((exc_type
->flags
& TYPE_FLAG_CONST
) &&
168 !(catchblock
->flags
& TYPE_FLAG_CONST
)) continue;
169 if ((exc_type
->flags
& TYPE_FLAG_VOLATILE
) &&
170 !(catchblock
->flags
& TYPE_FLAG_VOLATILE
)) continue;
171 return type
; /* it matched */
177 /* copy the exception object where the catch block wants it */
178 static void copy_exception( void *object
, cxx_exception_frame
*frame
,
179 catchblock_info
*catchblock
, const cxx_type_info
*type
)
183 if (!catchblock
->type_info
|| !catchblock
->type_info
->mangled
[0]) return;
184 if (!catchblock
->offset
) return;
185 dest_ptr
= (void **)((char *)&frame
->ebp
+ catchblock
->offset
);
187 if (catchblock
->flags
& TYPE_FLAG_REFERENCE
)
189 *dest_ptr
= get_this_pointer( type
, object
);
191 else if (type
->flags
& CLASS_IS_SIMPLE_TYPE
)
193 memmove( dest_ptr
, object
, type
->size
);
194 /* if it is a pointer, adjust it */
195 if (type
->size
== sizeof(void *)) *dest_ptr
= get_this_pointer( type
, *dest_ptr
);
197 else /* copy the object */
200 call_copy_ctor( type
->copy_ctor
, dest_ptr
, get_this_pointer(type
,object
),
201 (type
->flags
& CLASS_HAS_VIRTUAL_BASE_CLASS
) );
203 memmove( dest_ptr
, get_this_pointer(type
,object
), type
->size
);
207 /* unwind the local function up to a given trylevel */
208 static void cxx_local_unwind( cxx_exception_frame
* frame
, cxx_function_descr
*descr
, int last_level
)
211 int trylevel
= frame
->trylevel
;
213 while (trylevel
!= last_level
)
215 if (trylevel
< 0 || trylevel
>= (int)descr
->unwind_count
)
217 ERR( "invalid trylevel %d\n", trylevel
);
220 handler
= descr
->unwind_table
[trylevel
].handler
;
223 TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
224 handler
, trylevel
, last_level
, &frame
->ebp
);
225 call_ebp_func( handler
, &frame
->ebp
);
227 trylevel
= descr
->unwind_table
[trylevel
].prev
;
229 frame
->trylevel
= last_level
;
232 /* exception frame for nested exceptions in catch block */
233 struct catch_func_nested_frame
235 EXCEPTION_REGISTRATION_RECORD frame
; /* standard exception frame */
236 EXCEPTION_RECORD
*prev_rec
; /* previous record to restore in thread data */
237 cxx_exception_frame
*cxx_frame
; /* frame of parent exception */
238 cxx_function_descr
*descr
; /* descriptor of parent exception */
239 int trylevel
; /* current try level */
242 /* handler for exceptions happening while calling a catch function */
243 static DWORD
catch_function_nested_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
244 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
246 struct catch_func_nested_frame
*nested_frame
= (struct catch_func_nested_frame
*)frame
;
248 if (rec
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
250 msvcrt_get_thread_data()->exc_record
= nested_frame
->prev_rec
;
251 return ExceptionContinueSearch
;
255 TRACE( "got nested exception in catch function\n" );
256 return cxx_frame_handler( rec
, nested_frame
->cxx_frame
, context
,
257 NULL
, nested_frame
->descr
, &nested_frame
->frame
,
258 nested_frame
->trylevel
, context
);
262 /* find and call the appropriate catch block for an exception */
263 /* returns the address to continue execution to after the catch block was called */
264 inline static void *call_catch_block( PEXCEPTION_RECORD rec
, cxx_exception_frame
*frame
,
265 cxx_function_descr
*descr
, int nested_trylevel
,
266 cxx_exception_type
*info
)
270 void *addr
, *object
= (void *)rec
->ExceptionInformation
[1];
271 struct catch_func_nested_frame nested_frame
;
272 int trylevel
= frame
->trylevel
;
273 MSVCRT_thread_data
*thread_data
= msvcrt_get_thread_data();
275 for (i
= 0; i
< descr
->tryblock_count
; i
++)
277 tryblock_info
*tryblock
= &descr
->tryblock
[i
];
279 if (trylevel
< tryblock
->start_level
) continue;
280 if (trylevel
> tryblock
->end_level
) continue;
282 /* got a try block */
283 for (j
= 0; j
< tryblock
->catchblock_count
; j
++)
285 catchblock_info
*catchblock
= &tryblock
->catchblock
[j
];
286 const cxx_type_info
*type
= find_caught_type( info
, catchblock
);
289 TRACE( "matched type %p in tryblock %d catchblock %d\n", type
, i
, j
);
291 /* copy the exception to its destination on the stack */
292 copy_exception( object
, frame
, catchblock
, type
);
294 /* unwind the stack */
295 RtlUnwind( frame
, 0, rec
, 0 );
296 cxx_local_unwind( frame
, descr
, tryblock
->start_level
);
297 frame
->trylevel
= tryblock
->end_level
+ 1;
299 /* call the catch block */
300 TRACE( "calling catch block %p for type %p addr %p ebp %p\n",
301 catchblock
, type
, catchblock
->handler
, &frame
->ebp
);
303 /* setup an exception block for nested exceptions */
305 //nested_frame.frame.Handler = catch_function_nested_handler;
306 nested_frame
.frame
.handler
= (PEXCEPTION_HANDLER
)catch_function_nested_handler
;
307 nested_frame
.prev_rec
= thread_data
->exc_record
;
308 nested_frame
.cxx_frame
= frame
;
309 nested_frame
.descr
= descr
;
310 nested_frame
.trylevel
= nested_trylevel
+ 1;
312 __wine_push_frame( &nested_frame
.frame
);
313 thread_data
->exc_record
= rec
;
314 addr
= call_ebp_func( catchblock
->handler
, &frame
->ebp
);
315 thread_data
->exc_record
= nested_frame
.prev_rec
;
316 __wine_pop_frame( &nested_frame
.frame
);
318 if (info
->destructor
) call_dtor( info
->destructor
, object
);
319 TRACE( "done, continuing at %p\n", addr
);
327 /*********************************************************************
330 * Implementation of __CxxFrameHandler.
332 static DWORD
cxx_frame_handler( PEXCEPTION_RECORD rec
, cxx_exception_frame
* frame
,
333 PCONTEXT exc_context
, EXCEPTION_REGISTRATION_RECORD
** dispatch
,
334 cxx_function_descr
*descr
, EXCEPTION_REGISTRATION_RECORD
* nested_frame
,
335 int nested_trylevel
, CONTEXT86
*context
)
337 cxx_exception_type
*exc_type
;
340 if (descr
->magic
!= CXX_FRAME_MAGIC
)
342 ERR( "invalid frame magic %x\n", descr
->magic
);
343 return ExceptionContinueSearch
;
345 if (rec
->ExceptionFlags
& (EH_UNWINDING
|EH_EXIT_UNWIND
))
347 if (descr
->unwind_count
&& !nested_trylevel
) cxx_local_unwind( frame
, descr
, -1 );
348 return ExceptionContinueSearch
;
350 if (!descr
->tryblock_count
) return ExceptionContinueSearch
;
352 exc_type
= (cxx_exception_type
*)rec
->ExceptionInformation
[2];
353 if (rec
->ExceptionCode
== CXX_EXCEPTION
&&
354 rec
->ExceptionInformation
[0] > CXX_FRAME_MAGIC
&&
355 exc_type
->custom_handler
)
357 return exc_type
->custom_handler( rec
, frame
, exc_context
, dispatch
,
358 descr
, nested_trylevel
, nested_frame
, 0 );
361 if (!exc_type
) /* nested exception, fetch info from original exception */
363 rec
= msvcrt_get_thread_data()->exc_record
;
364 exc_type
= (cxx_exception_type
*)rec
->ExceptionInformation
[2];
369 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
370 rec
, frame
, frame
->trylevel
, descr
, nested_frame
);
371 dump_exception_type( exc_type
);
372 dump_function_descr( descr
, exc_type
);
375 next_ip
= call_catch_block( rec
, frame
, descr
, frame
->trylevel
, exc_type
);
377 if (!next_ip
) return ExceptionContinueSearch
;
378 rec
->ExceptionFlags
&= ~EH_NONCONTINUABLE
;
379 context
->Eip
= (DWORD
)next_ip
;
380 context
->Ebp
= (DWORD
)&frame
->ebp
;
381 context
->Esp
= ((DWORD
*)frame
)[-1];
382 return ExceptionContinueExecution
;
386 /*********************************************************************
387 * __CxxFrameHandler (MSVCRT.@)
389 void __CxxFrameHandler( PEXCEPTION_RECORD rec
, EXCEPTION_REGISTRATION_RECORD
* frame
,
390 PCONTEXT exc_context
, EXCEPTION_REGISTRATION_RECORD
** dispatch
,
393 cxx_function_descr
*descr
= (cxx_function_descr
*)context
->Eax
;
394 context
->Eax
= cxx_frame_handler( rec
, (cxx_exception_frame
*)frame
,
395 exc_context
, dispatch
, descr
, NULL
, 0, context
);
397 //DEFINE_REGS_ENTRYPOINT( __CxxFrameHandler, MSVCRT__CxxFrameHandler, 16, 0 );
399 #endif /* __i386__ */
401 /*********************************************************************
402 * _CxxThrowException (MSVCRT.@)
404 void _CxxThrowException( void *object
, const cxx_exception_type
*type
)
408 args
[0] = CXX_FRAME_MAGIC
;
409 args
[1] = (DWORD
)object
;
410 args
[2] = (DWORD
)type
;
411 RaiseException( CXX_EXCEPTION
, EH_NONCONTINUABLE
, 3, args
);
414 /*********************************************************************
415 * __CxxDetectRethrow (MSVCRT.@)
417 BOOL
__CxxDetectRethrow(PEXCEPTION_POINTERS ptrs
)
419 PEXCEPTION_RECORD rec
;
424 rec
= ptrs
->ExceptionRecord
;
426 if (rec
->ExceptionCode
== CXX_EXCEPTION
&&
427 rec
->NumberParameters
== 3 &&
428 rec
->ExceptionInformation
[0] == CXX_FRAME_MAGIC
&&
429 rec
->ExceptionInformation
[2])
431 ptrs
->ExceptionRecord
= msvcrt_get_thread_data()->exc_record
;
434 return (msvcrt_get_thread_data()->exc_record
== rec
);
437 /*********************************************************************
438 * __CxxQueryExceptionSize (MSVCRT.@)
440 unsigned int __CxxQueryExceptionSize(void)
442 return sizeof(cxx_exception_type
);