shdocvw: Added GetWindow implementation.
[wine] / dlls / msvcrt / cppexcept.c
1 /*
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
29 #include <stdarg.h>
30
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"
39
40 #include "cppexcept.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(seh);
43
44 #ifdef __i386__  /* CxxFrameHandler is not supported on non-i386 */
45
46 DWORD cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
47                          PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
48                          cxx_function_descr *descr, EXCEPTION_REGISTRATION_RECORD* nested_frame,
49                          int nested_trylevel );
50
51 /* call a function with a given ebp */
52 inline static void *call_ebp_func( void *func, void *ebp )
53 {
54     void *ret;
55     __asm__ __volatile__ ("pushl %%ebp; movl %2,%%ebp; call *%%eax; popl %%ebp" \
56                           : "=a" (ret) : "0" (func), "r" (ebp) : "ecx", "edx", "memory" );
57     return ret;
58 }
59
60 /* call a copy constructor */
61 inline 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"
67                              : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
68     else
69         __asm__ __volatile__("pushl %2; call *%0"
70                              : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
71 }
72
73 /* call the destructor of the exception object */
74 inline static void call_dtor( void *func, void *object )
75 {
76     __asm__ __volatile__("call *%0" : : "r" (func), "c" (object) : "eax", "edx", "memory" );
77 }
78
79 /* continue execution to the specified address after exception is caught */
80 inline static void DECLSPEC_NORETURN continue_after_catch( cxx_exception_frame* frame, void *addr )
81 {
82     __asm__ __volatile__("movl -4(%0),%%esp; leal 12(%0),%%ebp; jmp *%1"
83                          : : "r" (frame), "a" (addr) );
84     for (;;) ; /* unreached */
85 }
86
87 static inline void dump_type( const cxx_type_info *type )
88 {
89     TRACE( "flags %x type %p %s offsets %d,%d,%d size %d copy ctor %p\n",
90              type->flags, type->type_info, dbgstr_type_info(type->type_info),
91              type->offsets.this_offset, type->offsets.vbase_descr, type->offsets.vbase_offset,
92              type->size, type->copy_ctor );
93 }
94
95 static void dump_exception_type( const cxx_exception_type *type )
96 {
97     UINT i;
98
99     TRACE( "flags %x destr %p handler %p type info %p\n",
100              type->flags, type->destructor, type->custom_handler, type->type_info_table );
101     for (i = 0; i < type->type_info_table->count; i++)
102     {
103         TRACE( "    %d: ", i );
104         dump_type( type->type_info_table->info[i] );
105     }
106 }
107
108 static void dump_function_descr( const cxx_function_descr *descr, const cxx_exception_type *info )
109 {
110     UINT i;
111     int j;
112
113     TRACE( "magic %x\n", descr->magic );
114     TRACE( "unwind table: %p %d\n", descr->unwind_table, descr->unwind_count );
115     for (i = 0; i < descr->unwind_count; i++)
116     {
117         TRACE( "    %d: prev %d func %p\n", i,
118                  descr->unwind_table[i].prev, descr->unwind_table[i].handler );
119     }
120     TRACE( "try table: %p %d\n", descr->tryblock, descr->tryblock_count );
121     for (i = 0; i < descr->tryblock_count; i++)
122     {
123         TRACE( "    %d: start %d end %d catchlevel %d catch %p %d\n", i,
124                  descr->tryblock[i].start_level, descr->tryblock[i].end_level,
125                  descr->tryblock[i].catch_level, descr->tryblock[i].catchblock,
126                  descr->tryblock[i].catchblock_count );
127         for (j = 0; j < descr->tryblock[i].catchblock_count; j++)
128         {
129             catchblock_info *ptr = &descr->tryblock[i].catchblock[j];
130             TRACE( "        %d: flags %x offset %d handler %p type %p %s\n",
131                      j, ptr->flags, ptr->offset, ptr->handler,
132                      ptr->type_info, dbgstr_type_info( ptr->type_info ) );
133         }
134     }
135 }
136
137 /* check if the exception type is caught by a given catch block, and return the type that matched */
138 static const cxx_type_info *find_caught_type( cxx_exception_type *exc_type, catchblock_info *catchblock )
139 {
140     UINT i;
141
142     for (i = 0; i < exc_type->type_info_table->count; i++)
143     {
144         const cxx_type_info *type = exc_type->type_info_table->info[i];
145
146         if (!catchblock->type_info) return type;   /* catch(...) matches any type */
147         if (catchblock->type_info != type->type_info)
148         {
149             if (strcmp( catchblock->type_info->mangled, type->type_info->mangled )) continue;
150         }
151         /* type is the same, now check the flags */
152         if ((exc_type->flags & TYPE_FLAG_CONST) &&
153             !(catchblock->flags & TYPE_FLAG_CONST)) continue;
154         if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
155             !(catchblock->flags & TYPE_FLAG_VOLATILE)) continue;
156         return type;  /* it matched */
157     }
158     return NULL;
159 }
160
161
162 /* copy the exception object where the catch block wants it */
163 static void copy_exception( void *object, cxx_exception_frame *frame,
164                             catchblock_info *catchblock, const cxx_type_info *type )
165 {
166     void **dest_ptr;
167
168     if (!catchblock->type_info || !catchblock->type_info->mangled[0]) return;
169     if (!catchblock->offset) return;
170     dest_ptr = (void **)((char *)&frame->ebp + catchblock->offset);
171
172     if (catchblock->flags & TYPE_FLAG_REFERENCE)
173     {
174         *dest_ptr = get_this_pointer( &type->offsets, object );
175     }
176     else if (type->flags & CLASS_IS_SIMPLE_TYPE)
177     {
178         memmove( dest_ptr, object, type->size );
179         /* if it is a pointer, adjust it */
180         if (type->size == sizeof(void *)) *dest_ptr = get_this_pointer( &type->offsets, *dest_ptr );
181     }
182     else  /* copy the object */
183     {
184         if (type->copy_ctor)
185             call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(&type->offsets,object),
186                             (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
187         else
188             memmove( dest_ptr, get_this_pointer(&type->offsets,object), type->size );
189     }
190 }
191
192 /* unwind the local function up to a given trylevel */
193 static void cxx_local_unwind( cxx_exception_frame* frame, cxx_function_descr *descr, int last_level)
194 {
195     void (*handler)();
196     int trylevel = frame->trylevel;
197
198     while (trylevel != last_level)
199     {
200         if (trylevel < 0 || trylevel >= descr->unwind_count)
201         {
202             ERR( "invalid trylevel %d\n", trylevel );
203             MSVCRT_terminate();
204         }
205         handler = descr->unwind_table[trylevel].handler;
206         if (handler)
207         {
208             TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
209                    handler, trylevel, last_level, &frame->ebp );
210             call_ebp_func( handler, &frame->ebp );
211         }
212         trylevel = descr->unwind_table[trylevel].prev;
213     }
214     frame->trylevel = last_level;
215 }
216
217 /* exception frame for nested exceptions in catch block */
218 struct catch_func_nested_frame
219 {
220     EXCEPTION_REGISTRATION_RECORD frame;     /* standard exception frame */
221     EXCEPTION_RECORD             *prev_rec;  /* previous record to restore in thread data */
222     cxx_exception_frame          *cxx_frame; /* frame of parent exception */
223     cxx_function_descr           *descr;     /* descriptor of parent exception */
224     int                           trylevel;  /* current try level */
225 };
226
227 /* handler for exceptions happening while calling a catch function */
228 static DWORD catch_function_nested_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
229                                             CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
230 {
231     struct catch_func_nested_frame *nested_frame = (struct catch_func_nested_frame *)frame;
232
233     if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
234     {
235         msvcrt_get_thread_data()->exc_record = nested_frame->prev_rec;
236         return ExceptionContinueSearch;
237     }
238     else
239     {
240         TRACE( "got nested exception in catch function\n" );
241         return cxx_frame_handler( rec, nested_frame->cxx_frame, context,
242                                   NULL, nested_frame->descr, &nested_frame->frame,
243                                   nested_frame->trylevel );
244     }
245 }
246
247 /* find and call the appropriate catch block for an exception */
248 /* returns the address to continue execution to after the catch block was called */
249 inline static void call_catch_block( PEXCEPTION_RECORD rec, cxx_exception_frame *frame,
250                                      cxx_function_descr *descr, int nested_trylevel,
251                                      cxx_exception_type *info )
252 {
253     UINT i;
254     int j;
255     void *addr, *prev_frame, *object = (void *)rec->ExceptionInformation[1];
256     struct catch_func_nested_frame nested_frame;
257     int trylevel = frame->trylevel;
258     thread_data_t *thread_data = msvcrt_get_thread_data();
259
260     for (i = 0; i < descr->tryblock_count; i++)
261     {
262         tryblock_info *tryblock = &descr->tryblock[i];
263
264         if (trylevel < tryblock->start_level) continue;
265         if (trylevel > tryblock->end_level) continue;
266
267         /* got a try block */
268         for (j = 0; j < tryblock->catchblock_count; j++)
269         {
270             catchblock_info *catchblock = &tryblock->catchblock[j];
271             if(info)
272             {
273                 const cxx_type_info *type = find_caught_type( info, catchblock );
274                 if (!type) continue;
275
276                 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
277
278                 /* copy the exception to its destination on the stack */
279                 copy_exception( object, frame, catchblock, type );
280             }
281             else
282             {
283                 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
284                 if(catchblock->type_info)
285                     continue;
286                 TRACE("found catch(...) block\n");
287             }
288
289             /* unwind the stack */
290             RtlUnwind( frame, 0, rec, 0 );
291             cxx_local_unwind( frame, descr, tryblock->start_level );
292             frame->trylevel = tryblock->end_level + 1;
293
294             /* call the catch block */
295             TRACE( "calling catch block %p addr %p ebp %p\n",
296                    catchblock, catchblock->handler, &frame->ebp );
297
298             /* setup an exception block for nested exceptions */
299
300             nested_frame.frame.Handler = catch_function_nested_handler;
301             nested_frame.prev_rec  = thread_data->exc_record;
302             nested_frame.cxx_frame = frame;
303             nested_frame.descr     = descr;
304             nested_frame.trylevel  = nested_trylevel + 1;
305
306             __wine_push_frame( &nested_frame.frame );
307             thread_data->exc_record = rec;
308             addr = call_ebp_func( catchblock->handler, &frame->ebp );
309             thread_data->exc_record = nested_frame.prev_rec;
310             __wine_pop_frame( &nested_frame.frame );
311
312             if (info && info->destructor) call_dtor( info->destructor, object );
313             TRACE( "done, continuing at %p\n", addr );
314             prev_frame = NtCurrentTeb()->Tib.ExceptionList;
315             __wine_pop_frame( prev_frame );
316             continue_after_catch( frame, addr );
317         }
318     }
319 }
320
321
322 /*********************************************************************
323  *              cxx_frame_handler
324  *
325  * Implementation of __CxxFrameHandler.
326  */
327 DWORD cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
328                          PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
329                          cxx_function_descr *descr, EXCEPTION_REGISTRATION_RECORD* nested_frame,
330                          int nested_trylevel )
331 {
332     cxx_exception_type *exc_type;
333
334     if (descr->magic != CXX_FRAME_MAGIC)
335     {
336         ERR( "invalid frame magic %x\n", descr->magic );
337         return ExceptionContinueSearch;
338     }
339     if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
340     {
341         if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
342         return ExceptionContinueSearch;
343     }
344     if (!descr->tryblock_count) return ExceptionContinueSearch;
345
346     if(rec->ExceptionCode == CXX_EXCEPTION)
347     {
348         exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
349
350         if (rec->ExceptionInformation[0] > CXX_FRAME_MAGIC &&
351                 exc_type->custom_handler)
352         {
353             return exc_type->custom_handler( rec, frame, context, dispatch,
354                                          descr, nested_trylevel, nested_frame, 0 );
355         }
356         if (!exc_type)  /* nested exception, fetch info from original exception */
357         {
358             rec = msvcrt_get_thread_data()->exc_record;
359             exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
360         }
361         if (TRACE_ON(seh))
362         {
363             TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
364                   rec, frame, frame->trylevel, descr, nested_frame );
365             dump_exception_type( exc_type );
366             dump_function_descr( descr, exc_type );
367         }
368     }
369     else
370     {
371         exc_type = NULL;
372         TRACE("handling C exception code %lx  rec %p frame %p trylevel %d descr %p nested_frame %p\n",
373               rec->ExceptionCode,  rec, frame, frame->trylevel, descr, nested_frame );
374     }
375
376     call_catch_block( rec, frame, descr, frame->trylevel, exc_type );
377     return ExceptionContinueSearch;
378 }
379
380
381 /*********************************************************************
382  *              __CxxFrameHandler (MSVCRT.@)
383  */
384 extern DWORD __CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_REGISTRATION_RECORD* frame,
385                                 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch );
386 __ASM_GLOBAL_FUNC( __CxxFrameHandler,
387                    "pushl $0\n\t"        /* nested_trylevel */
388                    "pushl $0\n\t"        /* nested_frame */
389                    "pushl %eax\n\t"      /* descr */
390                    "pushl 28(%esp)\n\t"  /* dispatch */
391                    "pushl 28(%esp)\n\t"  /* context */
392                    "pushl 28(%esp)\n\t"  /* frame */
393                    "pushl 28(%esp)\n\t"  /* rec */
394                    "call " __ASM_NAME("cxx_frame_handler") "\n\t"
395                    "add $28,%esp\n\t"
396                    "ret" );
397
398 #endif  /* __i386__ */
399
400 /*********************************************************************
401  *              _CxxThrowException (MSVCRT.@)
402  */
403 void _CxxThrowException( exception *object, const cxx_exception_type *type )
404 {
405     ULONG_PTR args[3];
406
407     args[0] = CXX_FRAME_MAGIC;
408     args[1] = (ULONG_PTR)object;
409     args[2] = (ULONG_PTR)type;
410     RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );
411 }
412
413 /*********************************************************************
414  *              __CxxDetectRethrow (MSVCRT.@)
415  */
416 BOOL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
417 {
418   PEXCEPTION_RECORD rec;
419
420   if (!ptrs)
421     return FALSE;
422
423   rec = ptrs->ExceptionRecord;
424
425   if (rec->ExceptionCode == CXX_EXCEPTION &&
426       rec->NumberParameters == 3 &&
427       rec->ExceptionInformation[0] == CXX_FRAME_MAGIC &&
428       rec->ExceptionInformation[2])
429   {
430     ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
431     return TRUE;
432   }
433   return (msvcrt_get_thread_data()->exc_record == rec);
434 }
435
436 /*********************************************************************
437  *              __CxxQueryExceptionSize (MSVCRT.@)
438  */
439 unsigned int __CxxQueryExceptionSize(void)
440 {
441   return sizeof(cxx_exception_type);
442 }