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