fixed some signed/unsigned comparison warnings with -Wsign-compare
[reactos.git] / reactos / lib / crt / wine / 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 "wine/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 <internal/wine/msvcrt.h>
36 #include "wine/exception.h"
37 #include "excpt.h"
38 #include "wine/debug.h"
39
40 #include <internal/wine/cppexcept.h>
41
42 WINE_DEFAULT_DEBUG_CHANNEL(seh);
43
44 #ifdef __i386__ /* CxxFrameHandler is not supported on non-i386 */
45 #define CONTEXT86 CONTEXT //Mingw Hack
46
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 );
51
52 /* call a function with a given ebp */
53 inline static void *call_ebp_func( void *func, void *ebp )
54 {
55 void *ret;
56 __asm__ __volatile__ ("pushl %%ebp; movl %2,%%ebp; call *%%eax; popl %%ebp" \
57 : "=a" (ret) : "0" (func), "g" (ebp) : "ecx", "edx", "memory" );
58 return ret;
59 }
60
61 /* call a copy constructor */
62 inline static void call_copy_ctor( void *func, void *this, void *src, int has_vbase )
63 {
64 TRACE( "calling copy ctor %p object %p src %p\n", func, this, src );
65 if (has_vbase)
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" );
69 else
70 __asm__ __volatile__("pushl %2; call *%0"
71 : : "r" (func), "c" (this), "g" (src) : "eax", "edx", "memory" );
72 }
73
74 /* call the destructor of the exception object */
75 inline static void call_dtor( void *func, void *object )
76 {
77 __asm__ __volatile__("call *%0" : : "r" (func), "c" (object) : "eax", "edx", "memory" );
78 }
79
80 static void dump_type( const cxx_type_info *type )
81 {
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 );
86 }
87
88 static void dump_exception_type( const cxx_exception_type *type )
89 {
90 UINT i;
91
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++)
96 {
97 DPRINTF( " %d: ", i );
98 dump_type( type->type_info_table->info[i] );
99 }
100 }
101
102 static void dump_function_descr( const cxx_function_descr *descr, const cxx_exception_type *info )
103 {
104 UINT i;
105 int j;
106
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++)
111 {
112 DPRINTF( " %d: prev %d func %p\n", i,
113 descr->unwind_table[i].prev, descr->unwind_table[i].handler );
114 }
115 DPRINTF( "try table: %p %d\n", descr->tryblock, descr->tryblock_count );
116 for (i = 0; i < descr->tryblock_count; i++)
117 {
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++)
123 {
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 );
128 DPRINTF( "\n" );
129 }
130 }
131 }
132
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 )
135 {
136 void *this_ptr;
137 int *offset_ptr;
138
139 if (!object) return NULL;
140 this_ptr = (char *)object + type->this_offset;
141 if (type->vbase_descr >= 0)
142 {
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;
148 }
149 return this_ptr;
150 }
151
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 )
154 {
155 UINT i;
156
157 for (i = 0; i < exc_type->type_info_table->count; i++)
158 {
159 const cxx_type_info *type = exc_type->type_info_table->info[i];
160
161 if (!catchblock->type_info) return type; /* catch(...) matches any type */
162 if (catchblock->type_info != type->type_info)
163 {
164 if (strcmp( catchblock->type_info->mangled, type->type_info->mangled )) continue;
165 }
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 */
172 }
173 return NULL;
174 }
175
176
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 )
180 {
181 void **dest_ptr;
182
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);
186
187 if (catchblock->flags & TYPE_FLAG_REFERENCE)
188 {
189 *dest_ptr = get_this_pointer( type, object );
190 }
191 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
192 {
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 );
196 }
197 else /* copy the object */
198 {
199 if (type->copy_ctor)
200 call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(type,object),
201 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
202 else
203 memmove( dest_ptr, get_this_pointer(type,object), type->size );
204 }
205 }
206
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)
209 {
210 void (*handler)();
211 int trylevel = frame->trylevel;
212
213 while (trylevel != last_level)
214 {
215 if (trylevel < 0 || trylevel >= (int)descr->unwind_count)
216 {
217 ERR( "invalid trylevel %d\n", trylevel );
218 MSVCRT_terminate();
219 }
220 handler = descr->unwind_table[trylevel].handler;
221 if (handler)
222 {
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 );
226 }
227 trylevel = descr->unwind_table[trylevel].prev;
228 }
229 frame->trylevel = last_level;
230 }
231
232 /* exception frame for nested exceptions in catch block */
233 struct catch_func_nested_frame
234 {
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 */
240 };
241
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 )
245 {
246 struct catch_func_nested_frame *nested_frame = (struct catch_func_nested_frame *)frame;
247
248 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
249 {
250 msvcrt_get_thread_data()->exc_record = nested_frame->prev_rec;
251 return ExceptionContinueSearch;
252 }
253 else
254 {
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 );
259 }
260 }
261
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 )
267 {
268 UINT i;
269 int j;
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();
274
275 for (i = 0; i < descr->tryblock_count; i++)
276 {
277 tryblock_info *tryblock = &descr->tryblock[i];
278
279 if (trylevel < tryblock->start_level) continue;
280 if (trylevel > tryblock->end_level) continue;
281
282 /* got a try block */
283 for (j = 0; j < tryblock->catchblock_count; j++)
284 {
285 catchblock_info *catchblock = &tryblock->catchblock[j];
286 const cxx_type_info *type = find_caught_type( info, catchblock );
287 if (!type) continue;
288
289 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
290
291 /* copy the exception to its destination on the stack */
292 copy_exception( object, frame, catchblock, type );
293
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;
298
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 );
302
303 /* setup an exception block for nested exceptions */
304
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;
311
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 );
317
318 if (info->destructor) call_dtor( info->destructor, object );
319 TRACE( "done, continuing at %p\n", addr );
320 return addr;
321 }
322 }
323 return NULL;
324 }
325
326
327 /*********************************************************************
328 * cxx_frame_handler
329 *
330 * Implementation of __CxxFrameHandler.
331 */
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 )
336 {
337 cxx_exception_type *exc_type;
338 void *next_ip;
339
340 if (descr->magic != CXX_FRAME_MAGIC)
341 {
342 ERR( "invalid frame magic %x\n", descr->magic );
343 return ExceptionContinueSearch;
344 }
345 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
346 {
347 if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
348 return ExceptionContinueSearch;
349 }
350 if (!descr->tryblock_count) return ExceptionContinueSearch;
351
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)
356 {
357 return exc_type->custom_handler( rec, frame, exc_context, dispatch,
358 descr, nested_trylevel, nested_frame, 0 );
359 }
360
361 if (!exc_type) /* nested exception, fetch info from original exception */
362 {
363 rec = msvcrt_get_thread_data()->exc_record;
364 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
365 }
366
367 if (TRACE_ON(seh))
368 {
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 );
373 }
374
375 next_ip = call_catch_block( rec, frame, descr, frame->trylevel, exc_type );
376
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;
383 }
384
385
386 /*********************************************************************
387 * __CxxFrameHandler (MSVCRT.@)
388 */
389 void __CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_REGISTRATION_RECORD* frame,
390 PCONTEXT exc_context, EXCEPTION_REGISTRATION_RECORD** dispatch,
391 CONTEXT86 *context )
392 {
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 );
396 }
397 //DEFINE_REGS_ENTRYPOINT( __CxxFrameHandler, MSVCRT__CxxFrameHandler, 16, 0 );
398 // ROS
399 #endif /* __i386__ */
400
401 /*********************************************************************
402 * _CxxThrowException (MSVCRT.@)
403 */
404 void _CxxThrowException( void *object, const cxx_exception_type *type )
405 {
406 DWORD args[3];
407
408 args[0] = CXX_FRAME_MAGIC;
409 args[1] = (DWORD)object;
410 args[2] = (DWORD)type;
411 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );
412 }
413
414 /*********************************************************************
415 * __CxxDetectRethrow (MSVCRT.@)
416 */
417 BOOL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
418 {
419 PEXCEPTION_RECORD rec;
420
421 if (!ptrs)
422 return FALSE;
423
424 rec = ptrs->ExceptionRecord;
425
426 if (rec->ExceptionCode == CXX_EXCEPTION &&
427 rec->NumberParameters == 3 &&
428 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC &&
429 rec->ExceptionInformation[2])
430 {
431 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
432 return TRUE;
433 }
434 return (msvcrt_get_thread_data()->exc_record == rec);
435 }
436
437 /*********************************************************************
438 * __CxxQueryExceptionSize (MSVCRT.@)
439 */
440 unsigned int __CxxQueryExceptionSize(void)
441 {
442 return sizeof(cxx_exception_type);
443 }