blob: 08d928a21a873f754a8b85905d6cd1e3ea642e76 [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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000019 *
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"
Patrik Stridvall9c1de6d2002-09-12 22:07:02 +000033#include "winternl.h"
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000034#include "msvcrt.h"
35#include "wine/exception.h"
Dimitrie O. Paun737d4be2002-12-12 23:34:01 +000036#include "excpt.h"
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000037#include "wine/debug.h"
38
Jon Griffithsc62c1c02003-03-18 18:26:05 +000039#include "cppexcept.h"
40
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000041#ifdef __i386__ /* CxxFrameHandler is not supported on non-i386 */
42
Andrew Talbot6d9f0c22006-12-30 20:57:50 +000043WINE_DEFAULT_DEBUG_CHANNEL(seh);
44
Alexandre Julliard24beabf2006-06-13 11:21:19 +020045DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
46 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
Alexandre Julliard8592c4b2006-12-15 13:41:31 +010047 const cxx_function_descr *descr,
48 EXCEPTION_REGISTRATION_RECORD* nested_frame, int nested_trylevel );
Alexandre Julliard5ad69f12002-10-31 02:10:15 +000049
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000050/* call a function with a given ebp */
Andrew Talbot7b103482007-03-19 19:48:07 +000051static inline void *call_ebp_func( void *func, void *ebp )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000052{
53 void *ret;
Alexandre Julliardc0165092006-01-21 19:23:02 +010054 int dummy;
55 __asm__ __volatile__ ("pushl %%ebx\n\t"
56 "pushl %%ebp\n\t"
57 "movl %4,%%ebp\n\t"
58 "call *%%eax\n\t"
59 "popl %%ebp\n\t"
60 "popl %%ebx"
61 : "=a" (ret), "=S" (dummy), "=D" (dummy)
62 : "0" (func), "1" (ebp) : "ecx", "edx", "memory" );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000063 return ret;
64}
65
66/* call a copy constructor */
Andrew Talbot7b103482007-03-19 19:48:07 +000067static inline void call_copy_ctor( void *func, void *this, void *src, int has_vbase )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000068{
69 TRACE( "calling copy ctor %p object %p src %p\n", func, this, src );
70 if (has_vbase)
71 /* in that case copy ctor takes an extra bool indicating whether to copy the base class */
72 __asm__ __volatile__("pushl $1; pushl %2; call *%0"
Peter Chapman505dfde2004-12-02 18:19:25 +000073 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000074 else
75 __asm__ __volatile__("pushl %2; call *%0"
Peter Chapman505dfde2004-12-02 18:19:25 +000076 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000077}
78
79/* call the destructor of the exception object */
Andrew Talbot7b103482007-03-19 19:48:07 +000080static inline void call_dtor( void *func, void *object )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000081{
Alexandre Julliard6bd508f2002-11-01 01:50:51 +000082 __asm__ __volatile__("call *%0" : : "r" (func), "c" (object) : "eax", "edx", "memory" );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000083}
84
Alexandre Julliardabb170f2006-01-05 13:56:11 +010085/* continue execution to the specified address after exception is caught */
Andrew Talbot7b103482007-03-19 19:48:07 +000086static inline void DECLSPEC_NORETURN continue_after_catch( cxx_exception_frame* frame, void *addr )
Alexandre Julliardabb170f2006-01-05 13:56:11 +010087{
88 __asm__ __volatile__("movl -4(%0),%%esp; leal 12(%0),%%ebp; jmp *%1"
89 : : "r" (frame), "a" (addr) );
90 for (;;) ; /* unreached */
91}
92
Alexandre Julliardb8d5d962004-10-18 23:13:55 +000093static inline void dump_type( const cxx_type_info *type )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000094{
Alexandre Julliard9f859692005-09-25 15:23:21 +000095 TRACE( "flags %x type %p %s offsets %d,%d,%d size %d copy ctor %p\n",
Alexandre Julliardb8d5d962004-10-18 23:13:55 +000096 type->flags, type->type_info, dbgstr_type_info(type->type_info),
97 type->offsets.this_offset, type->offsets.vbase_descr, type->offsets.vbase_offset,
98 type->size, type->copy_ctor );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +000099}
100
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000101static void dump_exception_type( const cxx_exception_type *type )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000102{
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000103 UINT i;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000104
Alexandre Julliard9f859692005-09-25 15:23:21 +0000105 TRACE( "flags %x destr %p handler %p type info %p\n",
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000106 type->flags, type->destructor, type->custom_handler, type->type_info_table );
107 for (i = 0; i < type->type_info_table->count; i++)
108 {
Alexandre Julliard9f859692005-09-25 15:23:21 +0000109 TRACE( " %d: ", i );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000110 dump_type( type->type_info_table->info[i] );
111 }
112}
113
Alexandre Julliard8592c4b2006-12-15 13:41:31 +0100114static void dump_function_descr( const cxx_function_descr *descr )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000115{
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000116 UINT i;
117 int j;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000118
Alexandre Julliard9f859692005-09-25 15:23:21 +0000119 TRACE( "magic %x\n", descr->magic );
120 TRACE( "unwind table: %p %d\n", descr->unwind_table, descr->unwind_count );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000121 for (i = 0; i < descr->unwind_count; i++)
122 {
Alexandre Julliard9f859692005-09-25 15:23:21 +0000123 TRACE( " %d: prev %d func %p\n", i,
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000124 descr->unwind_table[i].prev, descr->unwind_table[i].handler );
125 }
Alexandre Julliard9f859692005-09-25 15:23:21 +0000126 TRACE( "try table: %p %d\n", descr->tryblock, descr->tryblock_count );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000127 for (i = 0; i < descr->tryblock_count; i++)
128 {
Alexandre Julliard9f859692005-09-25 15:23:21 +0000129 TRACE( " %d: start %d end %d catchlevel %d catch %p %d\n", i,
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000130 descr->tryblock[i].start_level, descr->tryblock[i].end_level,
131 descr->tryblock[i].catch_level, descr->tryblock[i].catchblock,
132 descr->tryblock[i].catchblock_count );
133 for (j = 0; j < descr->tryblock[i].catchblock_count; j++)
134 {
Alexandre Julliard8592c4b2006-12-15 13:41:31 +0100135 const catchblock_info *ptr = &descr->tryblock[i].catchblock[j];
Alexandre Julliard9f859692005-09-25 15:23:21 +0000136 TRACE( " %d: flags %x offset %d handler %p type %p %s\n",
Alexandre Julliardb8d5d962004-10-18 23:13:55 +0000137 j, ptr->flags, ptr->offset, ptr->handler,
138 ptr->type_info, dbgstr_type_info( ptr->type_info ) );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000139 }
140 }
141}
142
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000143/* check if the exception type is caught by a given catch block, and return the type that matched */
Alexandre Julliard8592c4b2006-12-15 13:41:31 +0100144static const cxx_type_info *find_caught_type( cxx_exception_type *exc_type,
145 const catchblock_info *catchblock )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000146{
147 UINT i;
148
149 for (i = 0; i < exc_type->type_info_table->count; i++)
150 {
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000151 const cxx_type_info *type = exc_type->type_info_table->info[i];
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000152
153 if (!catchblock->type_info) return type; /* catch(...) matches any type */
154 if (catchblock->type_info != type->type_info)
155 {
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000156 if (strcmp( catchblock->type_info->mangled, type->type_info->mangled )) continue;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000157 }
158 /* type is the same, now check the flags */
159 if ((exc_type->flags & TYPE_FLAG_CONST) &&
160 !(catchblock->flags & TYPE_FLAG_CONST)) continue;
161 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
162 !(catchblock->flags & TYPE_FLAG_VOLATILE)) continue;
163 return type; /* it matched */
164 }
165 return NULL;
166}
167
168
169/* copy the exception object where the catch block wants it */
170static void copy_exception( void *object, cxx_exception_frame *frame,
Alexandre Julliard8592c4b2006-12-15 13:41:31 +0100171 const catchblock_info *catchblock, const cxx_type_info *type )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000172{
173 void **dest_ptr;
174
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000175 if (!catchblock->type_info || !catchblock->type_info->mangled[0]) return;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000176 if (!catchblock->offset) return;
177 dest_ptr = (void **)((char *)&frame->ebp + catchblock->offset);
178
179 if (catchblock->flags & TYPE_FLAG_REFERENCE)
180 {
Alexandre Julliardb8d5d962004-10-18 23:13:55 +0000181 *dest_ptr = get_this_pointer( &type->offsets, object );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000182 }
183 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
184 {
185 memmove( dest_ptr, object, type->size );
186 /* if it is a pointer, adjust it */
Alexandre Julliardb8d5d962004-10-18 23:13:55 +0000187 if (type->size == sizeof(void *)) *dest_ptr = get_this_pointer( &type->offsets, *dest_ptr );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000188 }
189 else /* copy the object */
190 {
191 if (type->copy_ctor)
Alexandre Julliardb8d5d962004-10-18 23:13:55 +0000192 call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(&type->offsets,object),
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000193 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
194 else
Alexandre Julliardb8d5d962004-10-18 23:13:55 +0000195 memmove( dest_ptr, get_this_pointer(&type->offsets,object), type->size );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000196 }
197}
198
199/* unwind the local function up to a given trylevel */
Alexandre Julliard8592c4b2006-12-15 13:41:31 +0100200static void cxx_local_unwind( cxx_exception_frame* frame, const cxx_function_descr *descr, int last_level)
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000201{
202 void (*handler)();
203 int trylevel = frame->trylevel;
204
205 while (trylevel != last_level)
206 {
207 if (trylevel < 0 || trylevel >= descr->unwind_count)
208 {
209 ERR( "invalid trylevel %d\n", trylevel );
210 MSVCRT_terminate();
211 }
212 handler = descr->unwind_table[trylevel].handler;
213 if (handler)
214 {
215 TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
216 handler, trylevel, last_level, &frame->ebp );
217 call_ebp_func( handler, &frame->ebp );
218 }
219 trylevel = descr->unwind_table[trylevel].prev;
220 }
221 frame->trylevel = last_level;
222}
223
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000224/* exception frame for nested exceptions in catch block */
225struct catch_func_nested_frame
226{
Alexandre Julliardee106782003-08-28 03:07:56 +0000227 EXCEPTION_REGISTRATION_RECORD frame; /* standard exception frame */
228 EXCEPTION_RECORD *prev_rec; /* previous record to restore in thread data */
229 cxx_exception_frame *cxx_frame; /* frame of parent exception */
Alexandre Julliard8592c4b2006-12-15 13:41:31 +0100230 const cxx_function_descr *descr; /* descriptor of parent exception */
Alexandre Julliardee106782003-08-28 03:07:56 +0000231 int trylevel; /* current try level */
Peter Beutnerbe07b6d2006-02-22 12:06:56 +0100232 EXCEPTION_RECORD *rec; /* rec associated with frame */
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000233};
234
235/* handler for exceptions happening while calling a catch function */
Alexandre Julliardee106782003-08-28 03:07:56 +0000236static DWORD catch_function_nested_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
237 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000238{
239 struct catch_func_nested_frame *nested_frame = (struct catch_func_nested_frame *)frame;
240
241 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
242 {
243 msvcrt_get_thread_data()->exc_record = nested_frame->prev_rec;
244 return ExceptionContinueSearch;
245 }
Peter Beutner62dc7f52006-02-22 12:04:13 +0100246
247 TRACE( "got nested exception in catch function\n" );
248
249 if(rec->ExceptionCode == CXX_EXCEPTION)
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000250 {
Peter Beutnerbe07b6d2006-02-22 12:06:56 +0100251 PEXCEPTION_RECORD prev_rec = nested_frame->rec;
Peter Beutner62dc7f52006-02-22 12:04:13 +0100252 if(rec->ExceptionInformation[1] == 0 && rec->ExceptionInformation[2] == 0)
253 {
254 /* exception was rethrown */
255 rec->ExceptionInformation[1] = prev_rec->ExceptionInformation[1];
256 rec->ExceptionInformation[2] = prev_rec->ExceptionInformation[2];
257 TRACE("detect rethrow: re-propagate: obj: %lx, type: %lx\n",
258 rec->ExceptionInformation[1], rec->ExceptionInformation[2]);
259 }
260 else {
261 /* new exception in exception handler, destroy old */
262 void *object = (void*)prev_rec->ExceptionInformation[1];
263 cxx_exception_type *info = (cxx_exception_type*) prev_rec->ExceptionInformation[2];
264 TRACE("detect threw new exception in catch block - destroy old(obj: %p type: %p)\n",
265 object, info);
266 if(info && info->destructor)
267 call_dtor( info->destructor, object );
268 }
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000269 }
Peter Beutner62dc7f52006-02-22 12:04:13 +0100270
271 return cxx_frame_handler( rec, nested_frame->cxx_frame, context,
272 NULL, nested_frame->descr, &nested_frame->frame,
273 nested_frame->trylevel );
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000274}
275
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000276/* find and call the appropriate catch block for an exception */
277/* returns the address to continue execution to after the catch block was called */
Andrew Talbot7b103482007-03-19 19:48:07 +0000278static inline void call_catch_block( PEXCEPTION_RECORD rec, cxx_exception_frame *frame,
Alexandre Julliard8592c4b2006-12-15 13:41:31 +0100279 const cxx_function_descr *descr, int nested_trylevel,
Alexandre Julliardabb170f2006-01-05 13:56:11 +0100280 cxx_exception_type *info )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000281{
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000282 UINT i;
283 int j;
Peter Beutner916c4b62006-02-17 17:37:26 +0100284 void *addr, *object = (void *)rec->ExceptionInformation[1];
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000285 struct catch_func_nested_frame nested_frame;
286 int trylevel = frame->trylevel;
Dimitrie O. Paun03774622004-06-25 01:19:15 +0000287 thread_data_t *thread_data = msvcrt_get_thread_data();
Peter Beutner82818282006-02-22 12:21:00 +0100288 DWORD save_esp = ((DWORD*)frame)[-1];
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000289
290 for (i = 0; i < descr->tryblock_count; i++)
291 {
Alexandre Julliard8592c4b2006-12-15 13:41:31 +0100292 const tryblock_info *tryblock = &descr->tryblock[i];
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000293
294 if (trylevel < tryblock->start_level) continue;
295 if (trylevel > tryblock->end_level) continue;
296
297 /* got a try block */
298 for (j = 0; j < tryblock->catchblock_count; j++)
299 {
Alexandre Julliard8592c4b2006-12-15 13:41:31 +0100300 const catchblock_info *catchblock = &tryblock->catchblock[j];
Peter Beutner78ea87c2005-10-30 19:03:43 +0000301 if(info)
302 {
303 const cxx_type_info *type = find_caught_type( info, catchblock );
304 if (!type) continue;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000305
Peter Beutner78ea87c2005-10-30 19:03:43 +0000306 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000307
Peter Beutner78ea87c2005-10-30 19:03:43 +0000308 /* copy the exception to its destination on the stack */
309 copy_exception( object, frame, catchblock, type );
310 }
311 else
312 {
313 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
314 if(catchblock->type_info)
315 continue;
316 TRACE("found catch(...) block\n");
317 }
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000318
319 /* unwind the stack */
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000320 RtlUnwind( frame, 0, rec, 0 );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000321 cxx_local_unwind( frame, descr, tryblock->start_level );
322 frame->trylevel = tryblock->end_level + 1;
323
324 /* call the catch block */
Peter Beutner78ea87c2005-10-30 19:03:43 +0000325 TRACE( "calling catch block %p addr %p ebp %p\n",
326 catchblock, catchblock->handler, &frame->ebp );
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000327
328 /* setup an exception block for nested exceptions */
329
330 nested_frame.frame.Handler = catch_function_nested_handler;
331 nested_frame.prev_rec = thread_data->exc_record;
332 nested_frame.cxx_frame = frame;
333 nested_frame.descr = descr;
334 nested_frame.trylevel = nested_trylevel + 1;
Peter Beutnerbe07b6d2006-02-22 12:06:56 +0100335 nested_frame.rec = rec;
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000336
337 __wine_push_frame( &nested_frame.frame );
338 thread_data->exc_record = rec;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000339 addr = call_ebp_func( catchblock->handler, &frame->ebp );
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000340 thread_data->exc_record = nested_frame.prev_rec;
341 __wine_pop_frame( &nested_frame.frame );
342
Peter Beutner82818282006-02-22 12:21:00 +0100343 ((DWORD*)frame)[-1] = save_esp;
Peter Beutner78ea87c2005-10-30 19:03:43 +0000344 if (info && info->destructor) call_dtor( info->destructor, object );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000345 TRACE( "done, continuing at %p\n", addr );
Peter Beutner916c4b62006-02-17 17:37:26 +0100346
Alexandre Julliardabb170f2006-01-05 13:56:11 +0100347 continue_after_catch( frame, addr );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000348 }
349 }
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000350}
351
352
353/*********************************************************************
354 * cxx_frame_handler
355 *
356 * Implementation of __CxxFrameHandler.
357 */
Alexandre Julliard24beabf2006-06-13 11:21:19 +0200358DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
359 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
Alexandre Julliard8592c4b2006-12-15 13:41:31 +0100360 const cxx_function_descr *descr,
361 EXCEPTION_REGISTRATION_RECORD* nested_frame,
Alexandre Julliard24beabf2006-06-13 11:21:19 +0200362 int nested_trylevel )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000363{
364 cxx_exception_type *exc_type;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000365
366 if (descr->magic != CXX_FRAME_MAGIC)
367 {
368 ERR( "invalid frame magic %x\n", descr->magic );
369 return ExceptionContinueSearch;
370 }
371 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
372 {
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000373 if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000374 return ExceptionContinueSearch;
375 }
376 if (!descr->tryblock_count) return ExceptionContinueSearch;
377
Peter Beutner78ea87c2005-10-30 19:03:43 +0000378 if(rec->ExceptionCode == CXX_EXCEPTION)
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000379 {
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000380 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
Alexandre Julliard5ad69f12002-10-31 02:10:15 +0000381
Peter Beutner78ea87c2005-10-30 19:03:43 +0000382 if (rec->ExceptionInformation[0] > CXX_FRAME_MAGIC &&
383 exc_type->custom_handler)
384 {
Alexandre Julliardabb170f2006-01-05 13:56:11 +0100385 return exc_type->custom_handler( rec, frame, context, dispatch,
Peter Beutner78ea87c2005-10-30 19:03:43 +0000386 descr, nested_trylevel, nested_frame, 0 );
387 }
Peter Beutner62dc7f52006-02-22 12:04:13 +0100388
Peter Beutner78ea87c2005-10-30 19:03:43 +0000389 if (TRACE_ON(seh))
390 {
391 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
392 rec, frame, frame->trylevel, descr, nested_frame );
393 dump_exception_type( exc_type );
Alexandre Julliard8592c4b2006-12-15 13:41:31 +0100394 dump_function_descr( descr );
Peter Beutner78ea87c2005-10-30 19:03:43 +0000395 }
396 }
397 else
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000398 {
Peter Beutner78ea87c2005-10-30 19:03:43 +0000399 exc_type = NULL;
Michael Stefaniuc6520ee02006-10-03 20:39:05 +0200400 TRACE("handling C exception code %x rec %p frame %p trylevel %d descr %p nested_frame %p\n",
Peter Beutner78ea87c2005-10-30 19:03:43 +0000401 rec->ExceptionCode, rec, frame, frame->trylevel, descr, nested_frame );
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000402 }
403
Alexandre Julliardabb170f2006-01-05 13:56:11 +0100404 call_catch_block( rec, frame, descr, frame->trylevel, exc_type );
405 return ExceptionContinueSearch;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000406}
407
408
409/*********************************************************************
410 * __CxxFrameHandler (MSVCRT.@)
411 */
Alexandre Julliard24beabf2006-06-13 11:21:19 +0200412extern DWORD CDECL __CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_REGISTRATION_RECORD* frame,
413 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch );
Alexandre Julliardabb170f2006-01-05 13:56:11 +0100414__ASM_GLOBAL_FUNC( __CxxFrameHandler,
415 "pushl $0\n\t" /* nested_trylevel */
416 "pushl $0\n\t" /* nested_frame */
417 "pushl %eax\n\t" /* descr */
418 "pushl 28(%esp)\n\t" /* dispatch */
419 "pushl 28(%esp)\n\t" /* context */
420 "pushl 28(%esp)\n\t" /* frame */
421 "pushl 28(%esp)\n\t" /* rec */
422 "call " __ASM_NAME("cxx_frame_handler") "\n\t"
423 "add $28,%esp\n\t"
Joel Parker23199992007-01-12 17:38:07 -0600424 "ret" )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000425
Alexandre Julliard0689e9e2006-12-16 17:37:20 +0100426
427/*********************************************************************
428 * __CxxLongjmpUnwind (MSVCRT.@)
429 *
430 * Callback meant to be used as UnwindFunc for setjmp/longjmp.
431 */
432void __stdcall __CxxLongjmpUnwind( const struct MSVCRT___JUMP_BUFFER *buf )
433{
434 cxx_exception_frame *frame = (cxx_exception_frame *)buf->Registration;
435 const cxx_function_descr *descr = (const cxx_function_descr *)buf->UnwindData[0];
436
437 TRACE( "unwinding frame %p descr %p trylevel %ld\n", frame, descr, buf->TryLevel );
438 cxx_local_unwind( frame, descr, buf->TryLevel );
439}
440
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000441#endif /* __i386__ */
442
443/*********************************************************************
444 * _CxxThrowException (MSVCRT.@)
445 */
Alexandre Julliard24beabf2006-06-13 11:21:19 +0200446void CDECL _CxxThrowException( exception *object, const cxx_exception_type *type )
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000447{
Alexandre Julliard261e3762005-09-12 15:14:06 +0000448 ULONG_PTR args[3];
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000449
450 args[0] = CXX_FRAME_MAGIC;
Alexandre Julliard261e3762005-09-12 15:14:06 +0000451 args[1] = (ULONG_PTR)object;
452 args[2] = (ULONG_PTR)type;
Alexandre Julliard37a4c9b2002-07-24 03:02:51 +0000453 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );
454}
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000455
456/*********************************************************************
457 * __CxxDetectRethrow (MSVCRT.@)
458 */
Alexandre Julliard24beabf2006-06-13 11:21:19 +0200459BOOL CDECL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000460{
461 PEXCEPTION_RECORD rec;
462
463 if (!ptrs)
464 return FALSE;
465
466 rec = ptrs->ExceptionRecord;
467
468 if (rec->ExceptionCode == CXX_EXCEPTION &&
469 rec->NumberParameters == 3 &&
470 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC &&
471 rec->ExceptionInformation[2])
472 {
473 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
474 return TRUE;
475 }
476 return (msvcrt_get_thread_data()->exc_record == rec);
477}
478
479/*********************************************************************
480 * __CxxQueryExceptionSize (MSVCRT.@)
481 */
Alexandre Julliard24beabf2006-06-13 11:21:19 +0200482unsigned int CDECL __CxxQueryExceptionSize(void)
Jon Griffithsc62c1c02003-03-18 18:26:05 +0000483{
484 return sizeof(cxx_exception_type);
485}