ReactOS 0.4.16-dev-2122-g1628f5e
exception_ptr.c
Go to the documentation of this file.
1/*
2 * std::exception_ptr helper functions
3 *
4 * Copyright 2022 Torge Matthies for CodeWeavers
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#include <stdarg.h>
22#include <stdbool.h>
23
24#include "windef.h"
25#include "winternl.h"
26#include "wine/exception.h"
27#include "wine/debug.h"
28#include "msvcrt.h"
29#include "cppexcept.h"
30
31/* call a copy constructor */
32#ifdef __ASM_USE_THISCALL_WRAPPER
34 "pushl %ebp\n\t"
35 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
36 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
37 "movl %esp, %ebp\n\t"
38 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
39 "pushl $1\n\t"
40 "movl 12(%ebp), %ecx\n\t"
41 "pushl 16(%ebp)\n\t"
42 "call *8(%ebp)\n\t"
43 "leave\n"
44 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
45 __ASM_CFI(".cfi_same_value %ebp\n\t")
46 "ret" )
48 "movl 8(%esp),%ecx\n\t"
49 "call *4(%esp)\n\t"
50 "ret" )
51#endif
52
53#if _MSVCR_VER >= 100
54
56
57/*********************************************************************
58 * ?__ExceptionPtrCreate@@YAXPAX@Z
59 * ?__ExceptionPtrCreate@@YAXPEAX@Z
60 */
62{
63 TRACE("(%p)\n", ep);
64
65 ep->rec = NULL;
66 ep->ref = NULL;
67}
68
69/*********************************************************************
70 * ?__ExceptionPtrDestroy@@YAXPAX@Z
71 * ?__ExceptionPtrDestroy@@YAXPEAX@Z
72 */
74{
75 TRACE("(%p)\n", ep);
76
77 if (!ep->rec)
78 return;
79
80 if (!InterlockedDecrement(ep->ref))
81 {
83 {
84 const cxx_exception_type *type = (void*)ep->rec->ExceptionInformation[2];
85 void *obj = (void*)ep->rec->ExceptionInformation[1];
87
88 if (type && type->destructor) call_dtor( rtti_rva(type->destructor, base), obj );
90 }
91
92 HeapFree(GetProcessHeap(), 0, ep->rec);
93 HeapFree(GetProcessHeap(), 0, ep->ref);
94 }
95}
96
97#ifndef _CONCRT
98
99/*********************************************************************
100 * ?__ExceptionPtrCopy@@YAXPAXPBX@Z
101 * ?__ExceptionPtrCopy@@YAXPEAXPEBX@Z
102 */
103void __cdecl __ExceptionPtrCopy(exception_ptr *ep, const exception_ptr *copy)
104{
105 TRACE("(%p %p)\n", ep, copy);
106
107 /* don't destroy object stored in ep */
108 *ep = *copy;
109 if (ep->ref)
111}
112
113/*********************************************************************
114 * ?__ExceptionPtrAssign@@YAXPAXPBX@Z
115 * ?__ExceptionPtrAssign@@YAXPEAXPEBX@Z
116 */
117void __cdecl __ExceptionPtrAssign(exception_ptr *ep, const exception_ptr *assign)
118{
119 TRACE("(%p %p)\n", ep, assign);
120
121 /* don't destroy object stored in ep */
122 if (ep->ref)
124
125 *ep = *assign;
126 if (ep->ref)
128}
129
130#endif
131
132/*********************************************************************
133 * ?__ExceptionPtrRethrow@@YAXPBX@Z
134 * ?__ExceptionPtrRethrow@@YAXPEBX@Z
135 */
137{
138 TRACE("(%p)\n", ep);
139
140 if (!ep->rec)
141 {
142 throw_exception("bad exception");
143 return;
144 }
145
148}
149
151{
152 TRACE("(%p)\n", ep);
153
154 if (!rec)
155 {
156 ep->rec = NULL;
157 ep->ref = NULL;
158 return;
159 }
160
161 ep->rec = HeapAlloc(GetProcessHeap(), 0, sizeof(EXCEPTION_RECORD));
162 ep->ref = HeapAlloc(GetProcessHeap(), 0, sizeof(int));
163
164 *ep->rec = *rec;
165 *ep->ref = 1;
166
167 if (ep->rec->ExceptionCode == CXX_EXCEPTION)
168 {
169 void *obj = (void*)ep->rec->ExceptionInformation[1];
170 const cxx_exception_type *et = (void*)ep->rec->ExceptionInformation[2];
172 const cxx_type_info_table *table = rtti_rva( et->type_info_table, base );
173 const cxx_type_info *ti = rtti_rva( table->info[0], base );
174 void **data = HeapAlloc(GetProcessHeap(), 0, ti->size);
175
176 if (ti->flags & CLASS_IS_SIMPLE_TYPE)
177 {
178 memcpy(data, obj, ti->size);
179 if (ti->size == sizeof(void *)) *data = get_this_pointer(&ti->offsets, *data);
180 }
181 else if (ti->copy_ctor)
182 {
185 }
186 else
189 }
190 return;
191}
192
193#ifndef _CONCRT
194
195/*********************************************************************
196 * ?__ExceptionPtrCurrentException@@YAXPAX@Z
197 * ?__ExceptionPtrCurrentException@@YAXPEAX@Z
198 */
199void __cdecl __ExceptionPtrCurrentException(exception_ptr *ep)
200{
201 TRACE("(%p)\n", ep);
203}
204
205#if _MSVCR_VER >= 110
206/*********************************************************************
207 * ?__ExceptionPtrToBool@@YA_NPBX@Z
208 * ?__ExceptionPtrToBool@@YA_NPEBX@Z
209 */
210bool __cdecl __ExceptionPtrToBool(exception_ptr *ep)
211{
212 return !!ep->rec;
213}
214#endif
215
216/*********************************************************************
217 * ?__ExceptionPtrCopyException@@YAXPAXPBX1@Z
218 * ?__ExceptionPtrCopyException@@YAXPEAXPEBX1@Z
219 */
220void __cdecl __ExceptionPtrCopyException(exception_ptr *ep,
221 exception *object, const cxx_exception_type *type)
222{
224 const cxx_type_info *ti;
225 void **data;
227
229
230 ep->rec = HeapAlloc(GetProcessHeap(), 0, sizeof(EXCEPTION_RECORD));
231 ep->ref = HeapAlloc(GetProcessHeap(), 0, sizeof(int));
232 *ep->ref = 1;
233
234 memset(ep->rec, 0, sizeof(EXCEPTION_RECORD));
241
242 table = rtti_rva( type->type_info_table, base );
243 ti = rtti_rva( table->info[0], base );
244 data = HeapAlloc(GetProcessHeap(), 0, ti->size);
245 if (ti->flags & CLASS_IS_SIMPLE_TYPE)
246 {
247 memcpy(data, object, ti->size);
248 if (ti->size == sizeof(void *)) *data = get_this_pointer(&ti->offsets, *data);
249 }
250 else if (ti->copy_ctor)
251 {
254 }
255 else
256 memcpy(data, get_this_pointer(&ti->offsets, object), ti->size);
258}
259
260/*********************************************************************
261 * ?__ExceptionPtrCompare@@YA_NPBX0@Z
262 * ?__ExceptionPtrCompare@@YA_NPEBX0@Z
263 */
264bool __cdecl __ExceptionPtrCompare(const exception_ptr *ep1, const exception_ptr *ep2)
265{
266 return ep1->rec == ep2->rec;
267}
268
269#endif
270
271#endif /* _MSVCR_VER >= 100 */
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
INT copy(TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], INT append, DWORD lpdwFlags, BOOL bTouch)
Definition: copy.c:51
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
VOID WINAPI RaiseException(_In_ DWORD dwExceptionCode, _In_ DWORD dwExceptionFlags, _In_ DWORD nNumberOfArguments, _In_opt_ const ULONG_PTR *lpArguments)
Definition: except.c:700
void throw_exception(const char *msg)
Definition: cpp.c:606
#define CXX_EXCEPTION
Definition: cppexcept.h:34
void __cdecl __ExceptionPtrCreate(exception_ptr *)
#define CLASS_IS_SIMPLE_TYPE
Definition: cppexcept.h:137
static void call_dtor(void *func, void *this)
Definition: cppexcept.h:207
#define CXX_FRAME_MAGIC_VC6
Definition: cppexcept.h:31
#define CLASS_HAS_VIRTUAL_BASE_CLASS
Definition: cppexcept.h:138
static void call_copy_ctor(void *func, void *this, void *src, int has_vbase)
Definition: cppexcept.h:200
void exception_ptr_from_record(exception_ptr *, EXCEPTION_RECORD *)
void __cdecl __ExceptionPtrDestroy(exception_ptr *)
void __cdecl __ExceptionPtrRethrow(const exception_ptr *)
Definition: cpp.c:1245
#define CXX_EXCEPTION_PARAMS
Definition: cppexcept.h:89
static void * get_this_pointer(const this_ptr_offsets *off, void *object)
Definition: cppexcept.h:177
static void * rtti_rva(unsigned int rva, uintptr_t base)
Definition: cxx.h:469
static uintptr_t rtti_rva_base(const void *ptr)
Definition: cxx.h:463
#define __cdecl
Definition: corecrt.h:121
unsigned int uintptr_t
Definition: corecrt.h:185
thread_data_t *CDECL msvcrt_get_thread_data(void)
Definition: thread.c:45
return ret
Definition: mutex.c:146
#define ULONG_PTR
Definition: config.h:101
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLdouble GLdouble t
Definition: gl.h:2047
GLdouble n
Definition: glext.h:7729
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define __ASM_GLOBAL_FUNC(name, code)
Definition: port.h:201
#define memset(x, y, z)
Definition: compat.h:39
#define TRACE(s)
Definition: solgame.cpp:4
DWORD ExceptionCode
Definition: compat.h:208
DWORD NumberParameters
Definition: compat.h:212
DWORD ExceptionFlags
Definition: compat.h:209
ULONG_PTR ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]
Definition: compat.h:213
unsigned int size
Definition: cxx.h:439
this_ptr_offsets offsets
Definition: cxx.h:438
UINT flags
Definition: cxx.h:436
unsigned int copy_ctor
Definition: cxx.h:440
LONG * ref
Definition: cppexcept.h:157
EXCEPTION_RECORD * rec
Definition: cppexcept.h:156
#define EXCEPTION_NONCONTINUABLE
Definition: stubs.h:23
movl(%ebp)
#define __ASM_CFI(str)
Definition: asm.h:39
#define EXCEPTION_UNWINDING
Definition: rtltypes.h:155