[CRT]
[reactos.git] / reactos / lib / sdk / crt / except / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #define __WINE_DEBUG_CHANNEL__
27 #include <precomp.h>
28 #include <stdarg.h>
29
30 #include <wine/exception.h>
31 #include <internal/wine/msvcrt.h>
32 #include <internal/wine/cppexcept.h>
33
34 #ifdef __i386__ /* CxxFrameHandler is not supported on non-i386 */
35
36 WINE_DEFAULT_DEBUG_CHANNEL(seh);
37
38 DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
39 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
40 const cxx_function_descr *descr,
41 EXCEPTION_REGISTRATION_RECORD* nested_frame, int nested_trylevel );
42
43 /* call a function with a given ebp */
44 static inline void *call_ebp_func( void *func, void *ebp )
45 {
46 void *ret;
47 int dummy;
48 __asm__ __volatile__ ("pushl %%ebx\n\t"
49 "pushl %%ebp\n\t"
50 "movl %4,%%ebp\n\t"
51 "call *%%eax\n\t"
52 "popl %%ebp\n\t"
53 "popl %%ebx"
54 : "=a" (ret), "=S" (dummy), "=D" (dummy)
55 : "0" (func), "1" (ebp) : "ecx", "edx", "memory" );
56 return ret;
57 }
58
59 /* call a copy constructor */
60 static inline void call_copy_ctor( void *func, void *this, void *src, int has_vbase )
61 {
62 TRACE( "calling copy ctor %p object %p src %p\n", func, this, src );
63 if (has_vbase)
64 /* in that case copy ctor takes an extra bool indicating whether to copy the base class */
65 __asm__ __volatile__("pushl $1; pushl %2; call *%0"
66 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
67 else
68 __asm__ __volatile__("pushl %2; call *%0"
69 : : "r" (func), "c" (this), "r" (src) : "eax", "edx", "memory" );
70 }
71
72 /* call the destructor of the exception object */
73 static inline void call_dtor( void *func, void *object )
74 {
75 __asm__ __volatile__("call *%0" : : "r" (func), "c" (object) : "eax", "edx", "memory" );
76 }
77
78 /* continue execution to the specified address after exception is caught */
79 static inline void DECLSPEC_NORETURN continue_after_catch( cxx_exception_frame* frame, void *addr )
80 {
81 __asm__ __volatile__("movl -4(%0),%%esp; leal 12(%0),%%ebp; jmp *%1"
82 : : "r" (frame), "a" (addr) );
83 for (;;) ; /* unreached */
84 }
85
86 static inline void dump_type( const cxx_type_info *type )
87 {
88 TRACE( "flags %x type %p %s offsets %d,%d,%d size %d copy ctor %p\n",
89 type->flags, type->type_info, dbgstr_type_info(type->type_info),
90 type->offsets.this_offset, type->offsets.vbase_descr, type->offsets.vbase_offset,
91 type->size, type->copy_ctor );
92 }
93
94 static void dump_exception_type( const cxx_exception_type *type )
95 {
96 UINT i;
97
98 TRACE( "flags %x destr %p handler %p type info %p\n",
99 type->flags, type->destructor, type->custom_handler, type->type_info_table );
100 for (i = 0; i < type->type_info_table->count; i++)
101 {
102 TRACE( " %d: ", i );
103 dump_type( type->type_info_table->info[i] );
104 }
105 }
106
107 static void dump_function_descr( const cxx_function_descr *descr )
108 {
109 #ifndef WINE_NO_TRACE_MSGS
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 const 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 #endif
136 if (descr->magic <= CXX_FRAME_MAGIC_VC6) return;
137 TRACE( "expect list: %p\n", descr->expect_list );
138 if (descr->magic <= CXX_FRAME_MAGIC_VC7) return;
139 TRACE( "flags: %08x\n", descr->flags );
140 }
141
142 /* check if the exception type is caught by a given catch block, and return the type that matched */
143 static const cxx_type_info *find_caught_type( cxx_exception_type *exc_type,
144 const catchblock_info *catchblock )
145 {
146 UINT i;
147
148 for (i = 0; i < exc_type->type_info_table->count; i++)
149 {
150 const cxx_type_info *type = exc_type->type_info_table->info[i];
151
152 if (!catchblock->type_info) return type; /* catch(...) matches any type */
153 if (catchblock->type_info != type->type_info)
154 {
155 if (strcmp( catchblock->type_info->mangled, type->type_info->mangled )) continue;
156 }
157 /* type is the same, now check the flags */
158 if ((exc_type->flags & TYPE_FLAG_CONST) &&
159 !(catchblock->flags & TYPE_FLAG_CONST)) continue;
160 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
161 !(catchblock->flags & TYPE_FLAG_VOLATILE)) continue;
162 return type; /* it matched */
163 }
164 return NULL;
165 }
166
167
168 /* copy the exception object where the catch block wants it */
169 static void copy_exception( void *object, cxx_exception_frame *frame,
170 const catchblock_info *catchblock, const cxx_type_info *type )
171 {
172 void **dest_ptr;
173
174 if (!catchblock->type_info || !catchblock->type_info->mangled[0]) return;
175 if (!catchblock->offset) return;
176 dest_ptr = (void **)((char *)&frame->ebp + catchblock->offset);
177
178 if (catchblock->flags & TYPE_FLAG_REFERENCE)
179 {
180 *dest_ptr = get_this_pointer( &type->offsets, object );
181 }
182 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
183 {
184 memmove( dest_ptr, object, type->size );
185 /* if it is a pointer, adjust it */
186 if (type->size == sizeof(void *)) *dest_ptr = get_this_pointer( &type->offsets, *dest_ptr );
187 }
188 else /* copy the object */
189 {
190 if (type->copy_ctor)
191 call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(&type->offsets,object),
192 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
193 else
194 memmove( dest_ptr, get_this_pointer(&type->offsets,object), type->size );
195 }
196 }
197
198 /* unwind the local function up to a given trylevel */
199 static void cxx_local_unwind( cxx_exception_frame* frame, const cxx_function_descr *descr, int last_level)
200 {
201 void (*handler)(void);
202 int trylevel = frame->trylevel;
203
204 while (trylevel != last_level)
205 {
206 if (trylevel < 0 || trylevel >= descr->unwind_count)
207 {
208 ERR( "invalid trylevel %d\n", trylevel );
209 MSVCRT_terminate();
210 }
211 handler = descr->unwind_table[trylevel].handler;
212 if (handler)
213 {
214 TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
215 handler, trylevel, last_level, &frame->ebp );
216 call_ebp_func( handler, &frame->ebp );
217 }
218 trylevel = descr->unwind_table[trylevel].prev;
219 }
220 frame->trylevel = last_level;
221 }
222
223 /* exception frame for nested exceptions in catch block */
224 struct catch_func_nested_frame
225 {
226 EXCEPTION_REGISTRATION_RECORD frame; /* standard exception frame */
227 EXCEPTION_RECORD *prev_rec; /* previous record to restore in thread data */
228 cxx_exception_frame *cxx_frame; /* frame of parent exception */
229 const cxx_function_descr *descr; /* descriptor of parent exception */
230 int trylevel; /* current try level */
231 EXCEPTION_RECORD *rec; /* rec associated with frame */
232 };
233
234 /* handler for exceptions happening while calling a catch function */
235 static DWORD catch_function_nested_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
236 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
237 {
238 struct catch_func_nested_frame *nested_frame = (struct catch_func_nested_frame *)frame;
239
240 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
241 {
242 msvcrt_get_thread_data()->exc_record = nested_frame->prev_rec;
243 return ExceptionContinueSearch;
244 }
245
246 TRACE( "got nested exception in catch function\n" );
247
248 if(rec->ExceptionCode == CXX_EXCEPTION)
249 {
250 PEXCEPTION_RECORD prev_rec = nested_frame->rec;
251 if(rec->ExceptionInformation[1] == 0 && rec->ExceptionInformation[2] == 0)
252 {
253 /* exception was rethrown */
254 rec->ExceptionInformation[1] = prev_rec->ExceptionInformation[1];
255 rec->ExceptionInformation[2] = prev_rec->ExceptionInformation[2];
256 TRACE("detect rethrow: re-propagate: obj: %lx, type: %lx\n",
257 rec->ExceptionInformation[1], rec->ExceptionInformation[2]);
258 }
259 else {
260 /* new exception in exception handler, destroy old */
261 void *object = (void*)prev_rec->ExceptionInformation[1];
262 cxx_exception_type *info = (cxx_exception_type*) prev_rec->ExceptionInformation[2];
263 TRACE("detect threw new exception in catch block - destroy old(obj: %p type: %p)\n",
264 object, info);
265 if(info && info->destructor)
266 call_dtor( info->destructor, object );
267 }
268 }
269
270 return cxx_frame_handler( rec, nested_frame->cxx_frame, context,
271 NULL, nested_frame->descr, &nested_frame->frame,
272 nested_frame->trylevel );
273 }
274
275 /* find and call the appropriate catch block for an exception */
276 /* returns the address to continue execution to after the catch block was called */
277 static inline void call_catch_block( PEXCEPTION_RECORD rec, cxx_exception_frame *frame,
278 const cxx_function_descr *descr, int nested_trylevel,
279 cxx_exception_type *info )
280 {
281 UINT i;
282 int j;
283 void *addr, *object = (void *)rec->ExceptionInformation[1];
284 struct catch_func_nested_frame nested_frame;
285 int trylevel = frame->trylevel;
286 MSVCRT_thread_data *thread_data = msvcrt_get_thread_data();
287 DWORD save_esp = ((DWORD*)frame)[-1];
288
289 for (i = 0; i < descr->tryblock_count; i++)
290 {
291 const tryblock_info *tryblock = &descr->tryblock[i];
292
293 if (trylevel < tryblock->start_level) continue;
294 if (trylevel > tryblock->end_level) continue;
295
296 /* got a try block */
297 for (j = 0; j < tryblock->catchblock_count; j++)
298 {
299 const catchblock_info *catchblock = &tryblock->catchblock[j];
300 if(info)
301 {
302 const cxx_type_info *type = find_caught_type( info, catchblock );
303 if (!type) continue;
304
305 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
306
307 /* copy the exception to its destination on the stack */
308 copy_exception( object, frame, catchblock, type );
309 }
310 else
311 {
312 /* no CXX_EXCEPTION only proceed with a catch(...) block*/
313 if(catchblock->type_info)
314 continue;
315 TRACE("found catch(...) block\n");
316 }
317
318 /* unwind the stack */
319 RtlUnwind( frame, 0, rec, 0 );
320 cxx_local_unwind( frame, descr, tryblock->start_level );
321 frame->trylevel = tryblock->end_level + 1;
322
323 /* call the catch block */
324 TRACE( "calling catch block %p addr %p ebp %p\n",
325 catchblock, catchblock->handler, &frame->ebp );
326
327 /* setup an exception block for nested exceptions */
328
329 nested_frame.frame.Handler = (PEXCEPTION_ROUTINE)catch_function_nested_handler;
330 nested_frame.prev_rec = thread_data->exc_record;
331 nested_frame.cxx_frame = frame;
332 nested_frame.descr = descr;
333 nested_frame.trylevel = nested_trylevel + 1;
334 nested_frame.rec = rec;
335
336 __wine_push_frame( &nested_frame.frame );
337 thread_data->exc_record = rec;
338 addr = call_ebp_func( catchblock->handler, &frame->ebp );
339 thread_data->exc_record = nested_frame.prev_rec;
340 __wine_pop_frame( &nested_frame.frame );
341
342 ((DWORD*)frame)[-1] = save_esp;
343 if (info && info->destructor) call_dtor( info->destructor, object );
344 TRACE( "done, continuing at %p\n", addr );
345
346 continue_after_catch( frame, addr );
347 }
348 }
349 }
350
351
352 /*********************************************************************
353 * cxx_frame_handler
354 *
355 * Implementation of __CxxFrameHandler.
356 */
357 DWORD CDECL cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
358 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch,
359 const cxx_function_descr *descr,
360 EXCEPTION_REGISTRATION_RECORD* nested_frame,
361 int nested_trylevel )
362 {
363 cxx_exception_type *exc_type;
364
365 if (descr->magic < CXX_FRAME_MAGIC_VC6 || descr->magic > CXX_FRAME_MAGIC_VC8)
366 {
367 ERR( "invalid frame magic %x\n", descr->magic );
368 return ExceptionContinueSearch;
369 }
370 if (descr->magic >= CXX_FRAME_MAGIC_VC8 &&
371 (descr->flags & FUNC_DESCR_SYNCHRONOUS) &&
372 (rec->ExceptionCode != CXX_EXCEPTION))
373 return ExceptionContinueSearch; /* handle only c++ exceptions */
374
375 if (rec->ExceptionFlags & (EH_UNWINDING|EH_EXIT_UNWIND))
376 {
377 if (descr->unwind_count && !nested_trylevel) cxx_local_unwind( frame, descr, -1 );
378 return ExceptionContinueSearch;
379 }
380 if (!descr->tryblock_count) return ExceptionContinueSearch;
381
382 if(rec->ExceptionCode == CXX_EXCEPTION)
383 {
384 exc_type = (cxx_exception_type *)rec->ExceptionInformation[2];
385
386 if (rec->ExceptionInformation[0] > CXX_FRAME_MAGIC_VC8 &&
387 exc_type->custom_handler)
388 {
389 return exc_type->custom_handler( rec, frame, context, dispatch,
390 descr, nested_trylevel, nested_frame, 0 );
391 }
392
393 if (TRACE_ON(seh))
394 {
395 TRACE("handling C++ exception rec %p frame %p trylevel %d descr %p nested_frame %p\n",
396 rec, frame, frame->trylevel, descr, nested_frame );
397 dump_exception_type( exc_type );
398 dump_function_descr( descr );
399 }
400 }
401 else
402 {
403 exc_type = NULL;
404 TRACE("handling C exception code %x rec %p frame %p trylevel %d descr %p nested_frame %p\n",
405 rec->ExceptionCode, rec, frame, frame->trylevel, descr, nested_frame );
406 }
407
408 call_catch_block( rec, frame, descr, frame->trylevel, exc_type );
409 return ExceptionContinueSearch;
410 }
411
412
413 /*********************************************************************
414 * __CxxFrameHandler (MSVCRT.@)
415 */
416 extern DWORD CDECL __CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_REGISTRATION_RECORD* frame,
417 PCONTEXT context, EXCEPTION_REGISTRATION_RECORD** dispatch );
418 __ASM_GLOBAL_FUNC( __CxxFrameHandler,
419 "pushl $0\n\t" /* nested_trylevel */
420 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
421 "pushl $0\n\t" /* nested_frame */
422 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
423 "pushl %eax\n\t" /* descr */
424 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
425 "pushl 28(%esp)\n\t" /* dispatch */
426 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
427 "pushl 28(%esp)\n\t" /* context */
428 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
429 "pushl 28(%esp)\n\t" /* frame */
430 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
431 "pushl 28(%esp)\n\t" /* rec */
432 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
433 "call " __ASM_NAME("cxx_frame_handler") "\n\t"
434 "add $28,%esp\n\t"
435 __ASM_CFI(".cfi_adjust_cfa_offset -28\n\t")
436 "ret" )
437
438
439 /*********************************************************************
440 * __CxxLongjmpUnwind (MSVCRT.@)
441 *
442 * Callback meant to be used as UnwindFunc for setjmp/longjmp.
443 */
444 void __stdcall __CxxLongjmpUnwind( const struct MSVCRT___JUMP_BUFFER *buf )
445 {
446 cxx_exception_frame *frame = (cxx_exception_frame *)buf->Registration;
447 const cxx_function_descr *descr = (const cxx_function_descr *)buf->UnwindData[0];
448
449 TRACE( "unwinding frame %p descr %p trylevel %ld\n", frame, descr, buf->TryLevel );
450 cxx_local_unwind( frame, descr, buf->TryLevel );
451 }
452
453 #endif /* __i386__ */
454
455
456 /*********************************************************************
457 * __CppXcptFilter (MSVCRT.@)
458 */
459 int CDECL __CppXcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
460 {
461 /* only filter c++ exceptions */
462 if (ex != CXX_EXCEPTION) return EXCEPTION_CONTINUE_SEARCH;
463 return _XcptFilter( ex, ptr );
464 }
465
466 /*********************************************************************
467 * _CxxThrowException (MSVCRT.@)
468 */
469 void CDECL _CxxThrowException( exception *object, const cxx_exception_type *type )
470 {
471 ULONG_PTR args[3];
472
473 args[0] = CXX_FRAME_MAGIC_VC6;
474 args[1] = (ULONG_PTR)object;
475 args[2] = (ULONG_PTR)type;
476 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );
477 }
478
479 /*********************************************************************
480 * __CxxDetectRethrow (MSVCRT.@)
481 */
482 BOOL CDECL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
483 {
484 PEXCEPTION_RECORD rec;
485
486 if (!ptrs)
487 return FALSE;
488
489 rec = ptrs->ExceptionRecord;
490
491 if (rec->ExceptionCode == CXX_EXCEPTION &&
492 rec->NumberParameters == 3 &&
493 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC_VC6 &&
494 rec->ExceptionInformation[2])
495 {
496 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
497 return TRUE;
498 }
499 return (msvcrt_get_thread_data()->exc_record == rec);
500 }
501
502 /*********************************************************************
503 * __CxxQueryExceptionSize (MSVCRT.@)
504 */
505 unsigned int CDECL __CxxQueryExceptionSize(void)
506 {
507 return sizeof(cxx_exception_type);
508 }