source: trunk/src/msvcrt/cppexcept.c@ 10005

Last change on this file since 10005 was 10005, checked in by sandervl, 22 years ago

PF: MSVCRT update

File size: 16.1 KB
Line 
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#ifdef __WIN32OS2__
26#include <emxheader.h>
27#include <string.h>
28#include "winbase.h"
29#else
30#include "config.h"
31#endif
32
33#include "wine/port.h"
34
35#include "winternl.h"
36#include "msvcrt.h"
37#include "wine/exception.h"
38#include "excpt.h"
39#include "wine/debug.h"
40
41#include "cppexcept.h"
42
43WINE_DEFAULT_DEBUG_CHANNEL(seh);
44
45#ifdef __i386__ /* CxxFrameHandler is not supported on non-i386 */
46
47static DWORD cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
48 PCONTEXT exc_context, EXCEPTION_FRAME** dispatch,
49 cxx_function_descr *descr, EXCEPTION_FRAME* nested_frame,
50 int nested_trylevel, CONTEXT86 *context );
51
52/* call a function with a given ebp */
53inline 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 */
62inline 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 */
75inline static void call_dtor( void *func, void *object )
76{
77 __asm__ __volatile__("call *%0" : : "r" (func), "c" (object) : "eax", "edx", "memory" );
78}
79
80
81static void dump_type( const cxx_type_info *type )
82{
83 DPRINTF( "flags %x type %p", type->flags, type->type_info );
84 if (type->type_info) DPRINTF( " (%p %s)", type->type_info->name, type->type_info->mangled );
85 DPRINTF( " offset %d vbase %d,%d size %d copy ctor %p\n", type->this_offset,
86 type->vbase_descr, type->vbase_offset, type->size, type->copy_ctor );
87}
88
89static void dump_exception_type( const cxx_exception_type *type )
90{
91 UINT i;
92
93 DPRINTF( "exception type:\n" );
94 DPRINTF( "flags %x destr %p handler %p type info %p\n",
95 type->flags, type->destructor, type->custom_handler, type->type_info_table );
96 for (i = 0; i < type->type_info_table->count; i++)
97 {
98 DPRINTF( " %d: ", i );
99 dump_type( type->type_info_table->info[i] );
100 }
101}
102
103static void dump_function_descr( const cxx_function_descr *descr, const cxx_exception_type *info )
104{
105 UINT i;
106 int j;
107
108 DPRINTF( "function descr:\n" );
109 DPRINTF( "magic %x\n", descr->magic );
110 DPRINTF( "unwind table: %p %d\n", descr->unwind_table, descr->unwind_count );
111 for (i = 0; i < descr->unwind_count; i++)
112 {
113 DPRINTF( " %d: prev %d func %p\n", i,
114 descr->unwind_table[i].prev, descr->unwind_table[i].handler );
115 }
116 DPRINTF( "try table: %p %d\n", descr->tryblock, descr->tryblock_count );
117 for (i = 0; i < descr->tryblock_count; i++)
118 {
119 DPRINTF( " %d: start %d end %d catchlevel %d catch %p %d\n", i,
120 descr->tryblock[i].start_level, descr->tryblock[i].end_level,
121 descr->tryblock[i].catch_level, descr->tryblock[i].catchblock,
122 descr->tryblock[i].catchblock_count );
123 for (j = 0; j < descr->tryblock[i].catchblock_count; j++)
124 {
125 catchblock_info *ptr = &descr->tryblock[i].catchblock[j];
126 DPRINTF( " %d: flags %x offset %d handler %p type %p",
127 j, ptr->flags, ptr->offset, ptr->handler, ptr->type_info );
128 if (ptr->type_info) DPRINTF( " (%p %s)", ptr->type_info->name, ptr->type_info->mangled );
129 DPRINTF( "\n" );
130 }
131 }
132}
133
134/* compute the this pointer for a base class of a given type */
135static void *get_this_pointer( const cxx_type_info *type, void *object )
136{
137 void *this_ptr;
138 int *offset_ptr;
139
140 if (!object) return NULL;
141 this_ptr = (char *)object + type->this_offset;
142 if (type->vbase_descr >= 0)
143 {
144 /* move this ptr to vbase descriptor */
145 this_ptr = (char *)this_ptr + type->vbase_descr;
146 /* and fetch additional offset from vbase descriptor */
147 offset_ptr = (int *)(*(char **)this_ptr + type->vbase_offset);
148 this_ptr = (char *)this_ptr + *offset_ptr;
149 }
150 return this_ptr;
151}
152
153/* check if the exception type is caught by a given catch block, and return the type that matched */
154static const cxx_type_info *find_caught_type( cxx_exception_type *exc_type, catchblock_info *catchblock )
155{
156 UINT i;
157
158 for (i = 0; i < exc_type->type_info_table->count; i++)
159 {
160 const cxx_type_info *type = exc_type->type_info_table->info[i];
161
162 if (!catchblock->type_info) return type; /* catch(...) matches any type */
163 if (catchblock->type_info != type->type_info)
164 {
165 if (strcmp( catchblock->type_info->mangled, type->type_info->mangled )) continue;
166 }
167 /* type is the same, now check the flags */
168 if ((exc_type->flags & TYPE_FLAG_CONST) &&
169 !(catchblock->flags & TYPE_FLAG_CONST)) continue;
170 if ((exc_type->flags & TYPE_FLAG_VOLATILE) &&
171 !(catchblock->flags & TYPE_FLAG_VOLATILE)) continue;
172 return type; /* it matched */
173 }
174 return NULL;
175}
176
177
178/* copy the exception object where the catch block wants it */
179static void copy_exception( void *object, cxx_exception_frame *frame,
180 catchblock_info *catchblock, const cxx_type_info *type )
181{
182 void **dest_ptr;
183
184 if (!catchblock->type_info || !catchblock->type_info->mangled[0]) return;
185 if (!catchblock->offset) return;
186 dest_ptr = (void **)((char *)&frame->ebp + catchblock->offset);
187
188 if (catchblock->flags & TYPE_FLAG_REFERENCE)
189 {
190 *dest_ptr = get_this_pointer( type, object );
191 }
192 else if (type->flags & CLASS_IS_SIMPLE_TYPE)
193 {
194 memmove( dest_ptr, object, type->size );
195 /* if it is a pointer, adjust it */
196 if (type->size == sizeof(void *)) *dest_ptr = get_this_pointer( type, *dest_ptr );
197 }
198 else /* copy the object */
199 {
200 if (type->copy_ctor)
201 call_copy_ctor( type->copy_ctor, dest_ptr, get_this_pointer(type,object),
202 (type->flags & CLASS_HAS_VIRTUAL_BASE_CLASS) );
203 else
204 memmove( dest_ptr, get_this_pointer(type,object), type->size );
205 }
206}
207
208/* unwind the local function up to a given trylevel */
209static void cxx_local_unwind( cxx_exception_frame* frame, cxx_function_descr *descr, int last_level)
210{
211 void (*handler)();
212 int trylevel = frame->trylevel;
213
214 while (trylevel != last_level)
215 {
216 if (trylevel < 0 || trylevel >= descr->unwind_count)
217 {
218 ERR( "invalid trylevel %d\n", trylevel );
219 MSVCRT_terminate();
220 }
221 handler = descr->unwind_table[trylevel].handler;
222 if (handler)
223 {
224 TRACE( "calling unwind handler %p trylevel %d last %d ebp %p\n",
225 handler, trylevel, last_level, &frame->ebp );
226 call_ebp_func( handler, &frame->ebp );
227 }
228 trylevel = descr->unwind_table[trylevel].prev;
229 }
230 frame->trylevel = last_level;
231}
232
233/* exception frame for nested exceptions in catch block */
234struct catch_func_nested_frame
235{
236 EXCEPTION_FRAME frame; /* standard exception frame */
237 EXCEPTION_RECORD *prev_rec; /* previous record to restore in thread data */
238 cxx_exception_frame *cxx_frame; /* frame of parent exception */
239 cxx_function_descr *descr; /* descriptor of parent exception */
240 int trylevel; /* current try level */
241};
242
243/* handler for exceptions happening while calling a catch function */
244static DWORD catch_function_nested_handler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
245 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
246{
247 struct catch_func_nested_frame *nested_frame = (struct catch_func_nested_frame *)frame;
248
249 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
250 {
251 msvcrt_get_thread_data()->exc_record = nested_frame->prev_rec;
252 return ExceptionContinueSearch;
253 }
254 else
255 {
256 TRACE( "got nested exception in catch function\n" );
257 return cxx_frame_handler( rec, nested_frame->cxx_frame, context,
258 NULL, nested_frame->descr, &nested_frame->frame,
259 nested_frame->trylevel, context );
260 }
261}
262
263/* find and call the appropriate catch block for an exception */
264/* returns the address to continue execution to after the catch block was called */
265inline static void *call_catch_block( PEXCEPTION_RECORD rec, cxx_exception_frame *frame,
266 cxx_function_descr *descr, int nested_trylevel,
267 cxx_exception_type *info )
268{
269 UINT i;
270 int j;
271 void *addr, *object = (void *)rec->ExceptionInformation[1];
272 struct catch_func_nested_frame nested_frame;
273 int trylevel = frame->trylevel;
274 MSVCRT_thread_data *thread_data = msvcrt_get_thread_data();
275
276 for (i = 0; i < descr->tryblock_count; i++)
277 {
278 tryblock_info *tryblock = &descr->tryblock[i];
279
280 if (trylevel < tryblock->start_level) continue;
281 if (trylevel > tryblock->end_level) continue;
282
283 /* got a try block */
284 for (j = 0; j < tryblock->catchblock_count; j++)
285 {
286 catchblock_info *catchblock = &tryblock->catchblock[j];
287 const cxx_type_info *type = find_caught_type( info, catchblock );
288 if (!type) continue;
289
290 TRACE( "matched type %p in tryblock %d catchblock %d\n", type, i, j );
291
292 /* copy the exception to its destination on the stack */
293 copy_exception( object, frame, catchblock, type );
294
295 /* unwind the stack */
296 RtlUnwind( frame, 0, rec, 0 );
297 cxx_local_unwind( frame, descr, tryblock->start_level );
298 frame->trylevel = tryblock->end_level + 1;
299
300 /* call the catch block */
301 TRACE( "calling catch block %p for type %p addr %p ebp %p\n",
302 catchblock, type, catchblock->handler, &frame->ebp );
303
304 /* setup an exception block for nested exceptions */
305
306 nested_frame.frame.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 */
332static DWORD cxx_frame_handler( PEXCEPTION_RECORD rec, cxx_exception_frame* frame,
333 PCONTEXT exc_context, EXCEPTION_FRAME** dispatch,
334 cxx_function_descr *descr, EXCEPTION_FRAME* 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 * __CxxFrameHandler (MSVCRT.@)
387 */
388
389#ifdef __WIN32OS2__
390DEFINE_REGS_ENTRYPOINT_4( __CxxFrameHandler, EXC__CxxFrameHandler, PEXCEPTION_RECORD,
391 EXCEPTION_FRAME*, PCONTEXT ,EXCEPTION_FRAME**);
392
393void EXC__CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_FRAME* frame,
394#else
395void __CxxFrameHandler( PEXCEPTION_RECORD rec, EXCEPTION_FRAME* frame,
396#endif
397 PCONTEXT exc_context, EXCEPTION_FRAME** dispatch,
398 CONTEXT86 *context )
399{
400 cxx_function_descr *descr = (cxx_function_descr *)context->Eax;
401 context->Eax = cxx_frame_handler( rec, (cxx_exception_frame *)frame,
402 exc_context, dispatch, descr, NULL, 0, context );
403}
404
405#endif /* __i386__ */
406
407/*********************************************************************
408 * _CxxThrowException (MSVCRT.@)
409 */
410void _CxxThrowException( void *object, const cxx_exception_type *type )
411{
412 DWORD args[3];
413
414 args[0] = CXX_FRAME_MAGIC;
415 args[1] = (DWORD)object;
416 args[2] = (DWORD)type;
417 RaiseException( CXX_EXCEPTION, EH_NONCONTINUABLE, 3, args );
418}
419
420/*********************************************************************
421 * __CxxDetectRethrow (MSVCRT.@)
422 */
423BOOL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
424{
425 PEXCEPTION_RECORD rec;
426
427 if (!ptrs)
428 return FALSE;
429
430 rec = ptrs->ExceptionRecord;
431
432 if (rec->ExceptionCode == CXX_EXCEPTION &&
433 rec->NumberParameters == 3 &&
434 rec->ExceptionInformation[0] == CXX_FRAME_MAGIC &&
435 rec->ExceptionInformation[2])
436 {
437 ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
438 return TRUE;
439 }
440 return (msvcrt_get_thread_data()->exc_record == rec);
441}
442
443/*********************************************************************
444 * __CxxQueryExceptionSize (MSVCRT.@)
445 */
446unsigned int __CxxQueryExceptionSize(void)
447{
448 return sizeof(cxx_exception_type);
449}
Note: See TracBrowser for help on using the repository browser.