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 
21 #ifndef __MSVCRT_CPPEXCEPT_H
22 #define __MSVCRT_CPPEXCEPT_H
23 
24 #include <pseh/pseh2.h>
25 
26 #define CXX_FRAME_MAGIC_VC6 0x19930520
27 #define CXX_FRAME_MAGIC_VC7 0x19930521
28 #define CXX_FRAME_MAGIC_VC8 0x19930522
29 #define CXX_EXCEPTION       0xe06d7363
30 
31 /* Macros to define assembler functions somewhat portably */
32 
33 #define EH_NONCONTINUABLE   0x01
34 #define EH_UNWINDING        0x02
35 #define EH_EXIT_UNWIND      0x04
36 #define EH_STACK_INVALID    0x08
37 #define EH_NESTED_CALL      0x10
38 
39 typedef void (*vtable_ptr)();
40 
41 /* type_info object, see cpp.c for inplementation */
42 typedef struct __type_info
43 {
44   const vtable_ptr *vtable;
45   char              *name;        /* Unmangled name, allocated lazily */
46   char               mangled[32]; /* Variable length, but we declare it large enough for static RTTI */
47 } type_info;
48 
49 /* exception object */
50 typedef struct __exception
51 {
52   const vtable_ptr *vtable;
53   char             *name;    /* Name of this exception, always a new copy for each object */
54   int               do_free; /* Whether to free 'name' in our dtor */
55 } exception;
56 
57 /* the exception frame used by CxxFrameHandler */
58 typedef struct __cxx_exception_frame
59 {
60     EXCEPTION_REGISTRATION_RECORD  frame;    /* the standard exception frame */
61     int                            trylevel;
62     DWORD                          ebp;
63 } cxx_exception_frame;
64 
65 /* info about a single catch {} block */
66 typedef struct __catchblock_info
67 {
68     UINT             flags;         /* flags (see below) */
69     const type_info *type_info;     /* C++ type caught by this block */
70     int              offset;        /* stack offset to copy exception object to */
71     void           (*handler)(void);/* catch block handler code */
72 } catchblock_info;
73 #define TYPE_FLAG_CONST      1
74 #define TYPE_FLAG_VOLATILE   2
75 #define TYPE_FLAG_REFERENCE  8
76 
77 /* info about a single try {} block */
78 typedef struct __tryblock_info
79 {
80     int                    start_level;      /* start trylevel of that block */
81     int                    end_level;        /* end trylevel of that block */
82     int                    catch_level;      /* initial trylevel of the catch block */
83     int                    catchblock_count; /* count of catch blocks in array */
84     const catchblock_info *catchblock;       /* array of catch blocks */
85 } tryblock_info;
86 
87 /* info about the unwind handler for a given trylevel */
88 typedef struct __unwind_info
89 {
90     int    prev;          /* prev trylevel unwind handler, to run after this one */
91     void (*handler)(void);/* unwind handler */
92 } unwind_info;
93 
94 /* descriptor of all try blocks of a given function */
95 typedef struct __cxx_function_descr
96 {
97     UINT                 magic;          /* must be CXX_FRAME_MAGIC */
98     UINT                 unwind_count;   /* number of unwind handlers */
99     const unwind_info   *unwind_table;   /* array of unwind handlers */
100     UINT                 tryblock_count; /* number of try blocks */
101     const tryblock_info *tryblock;       /* array of try blocks */
102     UINT                 ipmap_count;
103     const void          *ipmap;
104     const void          *expect_list;    /* expected exceptions list when magic >= VC7 */
105     UINT                 flags;          /* flags when magic >= VC8 */
106 } cxx_function_descr;
107 
108 #define FUNC_DESCR_SYNCHRONOUS  1        /* synchronous exceptions only (built with /EHs) */
109 
110 typedef void (*cxx_copy_ctor)(void);
111 
112 /* offsets for computing the this pointer */
113 typedef struct
114 {
115     int         this_offset;   /* offset of base class this pointer from start of object */
116     int         vbase_descr;   /* offset of virtual base class descriptor */
117     int         vbase_offset;  /* offset of this pointer offset in virtual base class descriptor */
118 } this_ptr_offsets;
119 
120 /* complete information about a C++ type */
121 typedef struct __cxx_type_info
122 {
123     UINT             flags;        /* flags (see CLASS_* flags below) */
124     const type_info *type_info;    /* C++ type info */
125     this_ptr_offsets offsets;      /* offsets for computing the this pointer */
126     unsigned int     size;         /* object size */
127     cxx_copy_ctor    copy_ctor;    /* copy constructor */
128 } cxx_type_info;
129 #define CLASS_IS_SIMPLE_TYPE          1
130 #define CLASS_HAS_VIRTUAL_BASE_CLASS  4
131 
132 /* table of C++ types that apply for a given object */
133 typedef struct __cxx_type_info_table
134 {
135     UINT                 count;     /* number of types */
136     const cxx_type_info *info[3];   /* variable length, we declare it large enough for static RTTI */
137 } cxx_type_info_table;
138 
139 typedef DWORD (*cxx_exc_custom_handler)( PEXCEPTION_RECORD, cxx_exception_frame*,
140                                          PCONTEXT, EXCEPTION_REGISTRATION_RECORD**,
141                                          const cxx_function_descr*, int nested_trylevel,
142                                          EXCEPTION_REGISTRATION_RECORD *nested_frame, DWORD unknown3 );
143 
144 /* type information for an exception object */
145 typedef struct __cxx_exception_type
146 {
147     UINT                       flags;            /* TYPE_FLAG flags */
148     void                     (*destructor)(void);/* exception object destructor */
149     cxx_exc_custom_handler     custom_handler;   /* custom handler for this exception */
150     const cxx_type_info_table *type_info_table;  /* list of types for this exception object */
151 } cxx_exception_type;
152 
153 void WINAPI _CxxThrowException(exception*,const cxx_exception_type*);
154 int CDECL _XcptFilter(NTSTATUS, PEXCEPTION_POINTERS);
155 int CDECL __CppXcptFilter(NTSTATUS, PEXCEPTION_POINTERS);
156 
157 static inline const char *dbgstr_type_info( const type_info *info )
158 {
159     if (!info) return "{}";
160     return wine_dbg_sprintf( "{vtable=%p name=%s (%s)}",
161                              info->vtable, info->mangled, info->name ? info->name : "" );
162 }
163 
164 /* compute the this pointer for a base class of a given type */
165 static inline void *get_this_pointer( const this_ptr_offsets *off, void *object )
166 {
167     if (!object) return NULL;
168 
169     if (off->vbase_descr >= 0)
170     {
171         int *offset_ptr;
172 
173         /* move this ptr to vbase descriptor */
174         object = (char *)object + off->vbase_descr;
175         /* and fetch additional offset from vbase descriptor */
176         offset_ptr = (int *)(*(char **)object + off->vbase_offset);
177         object = (char *)object + *offset_ptr;
178     }
179 
180     object = (char *)object + off->this_offset;
181     return object;
182 }
183 
184 #endif /* __MSVCRT_CPPEXCEPT_H */
185