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