comctl32: toolbar: Don't execute TB_GETBUTTONINFO if cbSize is invalid.
[wine/multimedia.git] / dlls / msvcrt / cppexcept.c
blob3b25438c710f1731af8978027b01f17cdd272ed0
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
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.
26 #include "config.h"
27 #include "wine/port.h"
29 #include <stdarg.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winreg.h"
34 #include "winternl.h"
35 #include "msvcrt.h"
36 #include "wine/exception.h"
37 #include "excpt.h"
38 #include "wine/debug.h"
40 #include "cppexcept.h"
42 #ifdef __i386__ /* CxxFrameHandler is not supported on non-i386 */
44 WINE_DEFAULT_DEBUG_CHANNEL(seh);
46 DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
47 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
48 const cxx_function_descr *descr,
49 EXCEPTION_REGISTRATION_RECORD* nested_frame, int nested_trylevel );
51 /* call a function with a given ebp */
52 static inline void *call_ebp_func( void *func, void *ebp )
54 void *ret;
55 int dummy;
56 __asm__ __volatile__ ("pushl %%ebx\n\t"
57 "pushl %%ebp\n\t"
58 "movl %4,%%ebp\n\t"
59 "call *%%eax\n\t"
60 "popl %%ebp\n\t"
61 "popl %%ebx"
62 : "=a" (ret), "=S" (dummy), "=D" (dummy)
63 : "0" (func), "1" (ebp) : "ecx", "edx", "memory" );
64 return ret;
67 /* call a copy constructor */
68 static inline void call_copy_ctor( void *func, void *this, void *src, int has_vbase )
70 TRACE( "calling copy ctor %p object %p src %p\n", func, this, src );
71 if (has_vbase)
72 /* in that case copy ctor takes an extra bool indicating whether to copy the base class */
73 __asm__ __volatile__("pushl $1; pushl %2; call *%0"
74 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
75 else
76 __asm__ __volatile__("pushl %2; call *%0"
77 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
80 /* call the destructor of the exception object */
81 static inline void call_dtor( void *func, void *object )
83 __asm__ __volatile__("call *%0" : : "r" (func), "c" (object) : "eax", "edx", "memory" );
86 /* continue execution to the specified address after exception is caught */
87 static inline void DECLSPEC_NORETURN continue_after_catch( cxx_exception_frame* frame, void *addr )
89 __asm__ __volatile__("movl -4(%0),%%esp; leal 12(%0),%%ebp; jmp *%1"
90 : : "r" (frame), "a" (addr) );
91 for (;;) ; /* unreached */
94 static inline void dump_type( const cxx_type_info *type )
96 TRACE( "flags %x type %p %s offsets %d,%d,%d size %d copy ctor %p\n",
97 type->flags, type->type_info, dbgstr_type_info(type->type_info),
98 type->offsets.this_offset, type->offsets.vbase_descr, type->offsets.vbase_offset,
99 type->size, type->copy_ctor );
102 static void dump_exception_type( const cxx_exception_type *type )
104 UINT i;
106 TRACE( "flags %x destr %p handler %p type info %p\n",
107 type->flags, type->destructor, type->custom_handler, type->type_info_table );
108 for (i = 0; i < type->type_info_table->count; i++)
110 TRACE( " %d: ", i );
111 dump_type( type->type_info_table->info[i] );
115 static void dump_function_descr( const cxx_function_descr *descr )
117 UINT i;
118 int j;
120 TRACE( "magic %x\n", descr->magic );
121 TRACE( "unwind table: %p %d\n", descr->unwind_table, descr->unwind_count );
122 for (i = 0; i < descr->unwind_count; i++)
124 TRACE( " %d: prev %d func %p\n", i,
125 descr->unwind_table[i].prev, descr->unwind_table[i].handler );
127 TRACE( "try table: %p %d\n", descr->tryblock, descr->tryblock_count );
128 for (i = 0; i < descr->tryblock_count; i++)
130 TRACE( " %d: start %d end %d catchlevel %d catch %p %d\n", i,
131 descr->tryblock[i].start_level, descr->tryblock[i].end_level,
132 descr->tryblock[i].catch_level, descr->tryblock[i].catchblock,
133 descr->tryblock[i].catchblock_count );
134 for (j = 0; j < descr->tryblock[i].catchblock_count; j++)
136 const catchblock_info *ptr = &descr->tryblock[i].catchblock[j];
137 TRACE( " %d: flags %x offset %d handler %p type %p %s\n",
138 j, ptr->flags, ptr->offset, ptr->handler,
139 ptr->type_info, dbgstr_type_info( ptr->type_info ) );
144 /* check if the exception type is caught by a given catch block, and return the type that matched */
145 static const cxx_type_info *find_caught_type( cxx_exception_type *exc_type,
146 const catchblock_info *catchblock )
148 UINT i;
150 for (i = 0; i < exc_type->type_info_table->count; i++)
152 const cxx_type_info *type = exc_type->type_info_table->info[i];
154 if (!catchblock->type_info) return type; /* catch(...) matches any type */
155 if (catchblock->type_info != type->type_info)
157 if (strcmp( catchblock->type_info->mangled, type->type_info->mangled )) continue;
159 /* type is the same, now check the flags */
160 if ((exc_type->flags & TYPE_FLAG_CONST) &&
161 !(catchblock->flags & TYPE_FLAG_CONST)) continue;
162 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
163 !(catchblock->flags & TYPE_FLAG_VOLATILE)) continue;
164 return type; /* it matched */
166 return NULL;
170 /* copy the exception object where the catch block wants it */
171 static void copy_exception( void *object, cxx_exception_frame *frame,
172 const catchblock_info *catchblock, const cxx_type_info *type )
174 void **dest_ptr;
176 if (!catchblock->type_info || !catchblock->type_info->mangled[0]) return;
177 if (!catchblock->offset) return;
178 dest_ptr = (void **)((char *)&frame->ebp + catchblock->offset);
180 if (catchblock->flags & TYPE_FLAG_REFERENCE)
182 *dest_ptr = get_this_pointer( &type->offsets, object );
184 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
186 memmove( dest_ptr, object, type->size );
187 /* if it is a pointer, adjust it */
188 if (type->size == sizeof(void *)) *dest_ptr = get_this_pointer( &type->offsets, *dest_ptr );
190 else /* copy the object */
192 if (type->copy_ctor)
193 call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(&type->offsets,object),
194 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
195 else
196 memmove( dest_ptr, get_this_pointer(&type->offsets,object), type->size );
200 /* unwind the local function up to a given trylevel */
201 static void cxx_local_unwind( cxx_exception_frame* frame, const cxx_function_descr *descr, int last_level)
203 void (*handler)();
204 int trylevel = frame->trylevel;
206 while (trylevel != last_level)
208 if (trylevel < 0 || trylevel >= descr->unwind_count)
210 ERR( "invalid trylevel %d\n", trylevel );
211 MSVCRT_terminate();
213 handler = descr->unwind_table[trylevel].handler;
214 if (handler)
216 TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
217 handler, trylevel, last_level, &frame->ebp );
218 call_ebp_func( handler, &frame->ebp );
220 trylevel = descr->unwind_table[trylevel].prev;
222 frame->trylevel = last_level;
225 /* exception frame for nested exceptions in catch block */
226 struct catch_func_nested_frame
228 EXCEPTION_REGISTRATION_RECORD frame; /* standard exception frame */
229 EXCEPTION_RECORD *prev_rec; /* previous record to restore in thread data */
230 cxx_exception_frame *cxx_frame; /* frame of parent exception */
231 const cxx_function_descr *descr; /* descriptor of parent exception */
232 int trylevel; /* current try level */
233 EXCEPTION_RECORD *rec; /* rec associated with frame */
236 /* handler for exceptions happening while calling a catch function */
237 static DWORD catch_function_nested_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
238 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **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;
248 TRACE( "got nested exception in catch function\n" );
250 if(rec->ExceptionCode == CXX_EXCEPTION)
252 PEXCEPTION_RECORD prev_rec = nested_frame->rec;
253 if(rec->ExceptionInformation[1] == 0 && rec->ExceptionInformation[2] == 0)
255 /* exception was rethrown */
256 rec->ExceptionInformation[1] = prev_rec->ExceptionInformation[1];
257 rec->ExceptionInformation[2] = prev_rec->ExceptionInformation[2];
258 TRACE("detect rethrow: re-propagate: obj: %lx, type: %lx\n",
259 rec->ExceptionInformation[1], rec->ExceptionInformation[2]);
261 else {
262 /* new exception in exception handler, destroy old */
263 void *object = (void*)prev_rec->ExceptionInformation[1];
264 cxx_exception_type *info = (cxx_exception_type*) prev_rec->ExceptionInformation[2];
265 TRACE("detect threw new exception in catch block - destroy old(obj: %p type: %p)\n",
266 object, info);
267 if(info && info->destructor)
268 call_dtor( info->destructor, object );
272 return cxx_frame_handler( rec, nested_frame->cxx_frame, context,
273 NULL, nested_frame->descr, &nested_frame->frame,
274 nested_frame->trylevel );
277 /* find and call the appropriate catch block for an exception */
278 /* returns the address to continue execution to after the catch block was called */
279 static inline void call_catch_block( PEXCEPTION_RECORD rec, cxx_exception_frame *frame,
280 const cxx_function_descr *descr, int nested_trylevel,
281 cxx_exception_type *info )
283 UINT i;
284 int j;
285 void *addr, *object = (void *)rec->ExceptionInformation[1];
286 struct catch_func_nested_frame nested_frame;
287 int trylevel = frame->trylevel;
288 thread_data_t *thread_data = msvcrt_get_thread_data();
289 DWORD save_esp = ((DWORD*)frame)[-1];
291 for (i = 0; i < descr->tryblock_count; i++)
293 const tryblock_info *tryblock = &descr->tryblock[i];
295 if (trylevel < tryblock->start_level) continue;
296 if (trylevel > tryblock->end_level) continue;
298 /* got a try block */
299 for (j = 0; j < tryblock->catchblock_count; j++)
301 const catchblock_info *catchblock = &tryblock->catchblock[j];
302 if(info)
304 const cxx_type_info *type = find_caught_type( info, catchblock );
305 if (!type) continue;
307 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
309 /* copy the exception to its destination on the stack */
310 copy_exception( object, frame, catchblock, type );
312 else
314 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
315 if(catchblock->type_info)
316 continue;
317 TRACE("found catch(...) block\n");
320 /* unwind the stack */
321 RtlUnwind( frame, 0, rec, 0 );
322 cxx_local_unwind( frame, descr, tryblock->start_level );
323 frame->trylevel = tryblock->end_level + 1;
325 /* call the catch block */
326 TRACE( "calling catch block %p addr %p ebp %p\n",
327 catchblock, catchblock->handler, &frame->ebp );
329 /* setup an exception block for nested exceptions */
331 nested_frame.frame.Handler = catch_function_nested_handler;
332 nested_frame.prev_rec = thread_data->exc_record;
333 nested_frame.cxx_frame = frame;
334 nested_frame.descr = descr;
335 nested_frame.trylevel = nested_trylevel + 1;
336 nested_frame.rec = rec;
338 __wine_push_frame( &nested_frame.frame );
339 thread_data->exc_record = rec;
340 addr = call_ebp_func( catchblock->handler, &frame->ebp );
341 thread_data->exc_record = nested_frame.prev_rec;
342 __wine_pop_frame( &nested_frame.frame );
344 ((DWORD*)frame)[-1] = save_esp;
345 if (info && info->destructor) call_dtor( info->destructor, object );
346 TRACE( "done, continuing at %p\n", addr );
348 continue_after_catch( frame, addr );
354 /*********************************************************************
355 * cxx_frame_handler
357 * Implementation of __CxxFrameHandler.
359 DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
360 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
361 const cxx_function_descr *descr,
362 EXCEPTION_REGISTRATION_RECORD* nested_frame,
363 int nested_trylevel )
365 cxx_exception_type *exc_type;
367 if (descr->magic != CXX_FRAME_MAGIC)
369 ERR( "invalid frame magic %x\n", descr->magic );
370 return ExceptionContinueSearch;
372 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
374 if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
375 return ExceptionContinueSearch;
377 if (!descr->tryblock_count) return ExceptionContinueSearch;
379 if(rec->ExceptionCode == CXX_EXCEPTION)
381 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
383 if (rec->ExceptionInformation[0] > CXX_FRAME_MAGIC &&
384 exc_type->custom_handler)
386 return exc_type->custom_handler( rec, frame, context, dispatch,
387 descr, nested_trylevel, nested_frame, 0 );
390 if (TRACE_ON(seh))
392 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
393 rec, frame, frame->trylevel, descr, nested_frame );
394 dump_exception_type( exc_type );
395 dump_function_descr( descr );
398 else
400 exc_type = NULL;
401 TRACE("handling C exception code %x rec %p frame %p trylevel %d descr %p nested_frame %p\n",
402 rec->ExceptionCode, rec, frame, frame->trylevel, descr, nested_frame );
405 call_catch_block( rec, frame, descr, frame->trylevel, exc_type );
406 return ExceptionContinueSearch;
410 /*********************************************************************
411 * __CxxFrameHandler (MSVCRT.@)
413 extern DWORD CDECL __CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_REGISTRATION_RECORD* frame,
414 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch );
415 __ASM_GLOBAL_FUNC( __CxxFrameHandler,
416 "pushl $0\n\t" /* nested_trylevel */
417 "pushl $0\n\t" /* nested_frame */
418 "pushl %eax\n\t" /* descr */
419 "pushl 28(%esp)\n\t" /* dispatch */
420 "pushl 28(%esp)\n\t" /* context */
421 "pushl 28(%esp)\n\t" /* frame */
422 "pushl 28(%esp)\n\t" /* rec */
423 "call " __ASM_NAME("cxx_frame_handler") "\n\t"
424 "add $28,%esp\n\t"
425 "ret" )
428 /*********************************************************************
429 * __CxxLongjmpUnwind (MSVCRT.@)
431 * Callback meant to be used as UnwindFunc for setjmp/longjmp.
433 void __stdcall __CxxLongjmpUnwind( const struct MSVCRT___JUMP_BUFFER *buf )
435 cxx_exception_frame *frame = (cxx_exception_frame *)buf->Registration;
436 const cxx_function_descr *descr = (const cxx_function_descr *)buf->UnwindData[0];
438 TRACE( "unwinding frame %p descr %p trylevel %ld\n", frame, descr, buf->TryLevel );
439 cxx_local_unwind( frame, descr, buf->TryLevel );
442 #endif /* __i386__ */
444 /*********************************************************************
445 * _CxxThrowException (MSVCRT.@)
447 void CDECL _CxxThrowException( exception *object, const cxx_exception_type *type )
449 ULONG_PTR args[3];
451 args[0] = CXX_FRAME_MAGIC;
452 args[1] = (ULONG_PTR)object;
453 args[2] = (ULONG_PTR)type;
454 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );
457 /*********************************************************************
458 * __CxxDetectRethrow (MSVCRT.@)
460 BOOL CDECL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
462 PEXCEPTION_RECORD rec;
464 if (!ptrs)
465 return FALSE;
467 rec = ptrs->ExceptionRecord;
469 if (rec->ExceptionCode == CXX_EXCEPTION &&
470 rec->NumberParameters == 3 &&
471 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC &&
472 rec->ExceptionInformation[2])
474 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
475 return TRUE;
477 return (msvcrt_get_thread_data()->exc_record == rec);
480 /*********************************************************************
481 * __CxxQueryExceptionSize (MSVCRT.@)
483 unsigned int CDECL __CxxQueryExceptionSize(void)
485 return sizeof(cxx_exception_type);