-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmgdframe.cpp
325 lines (307 loc) · 10.9 KB
/
mgdframe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/***
* mgdframe.cpp - Managed exception frame support.
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* These routines are called by managed code during exception propagation.
*
****/
#include <eh.h> // User-visible routines for eh
#include <ehassert.h> // This project's versions of standard assert macros
#include <ehdata.h> // Declarations of all types used for EH
#include <ehdata4.h>
#include <ehhooks.h> // Declarations of hook variables and callbacks
#include <trnsctrl.h> // Routines to handle transfer of control (trnsctrl.asm)
#include <vcruntime_exception.h>
#include <vcruntime_typeinfo.h>
#include "ehhelpers.h"
////////////////////////////////////////////////////////////////////////////////
// Model of C++ eh in COM+
//
// void func()
// {
// try {
// TryBody();
// } catch (cpp_object o)
// {
// CatchOBody();
// } catch (...)
// {
// CatchAllBody();
// }
// }
//
// Turns into this:
//
//
// void func()
// {
// int rethrow;
// // One per try block
// int isCxxException;
// // One per catch(...)
// __try {
// TryBody();
// }
// __except(__CxxExceptionFilter(exception,
// typeinfo(cpp_object),
// flags,
// &o))
// // This is how it's done already
// {
// // Begin catch(object) prefix
// char *storage = _alloca(__CxxQueryExceptionSize());
// rethrow = false;
// __CxxRegisterExceptionObject(exception,
// storage);
// __try {
// __try {
// // End catch(object) prefix
// CatchOBody();
// // Begin catch(object) suffix
// } __except(rethrow = __CxxDetectRethrow(exception),
// EXCEPTION_CONTINUE_SEARCH)
// {}
// }
// __finally
// {
// __CxxUnregisterExceptionObject(storage,
// rethrow);
// }
// // End catch(object) suffix
// }
// __except(1)
// {
// // Begin catch(...) prefix
// char *storage = _alloca(__CxxQueryExceptionSize());
// rethrow = false;
// isCxxException = __CxxRegisterExceptionObject(exception,
// storage);
// __try
// {
// __try
// {
// // End catch(...) prefix
// CatchAllBody();
// // Begin catch(...) suffix
// } __except(rethrow = __CxxDetectRethrow(exception),
// EXCEPTION_CONTINUE_SEARCH)
// {}
// } __finally
// {
// if (isCxxException)
// __CxxUnregisterExceptionObject(storage, rethrow);
// }
// // End catch(...) suffix
// }
// }
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// __CxxExceptionFilter() - Returns EXCEPTION_EXECUTE_HANDLER when the pType
// matches with the objects we can catch. Returns
// EXCEPTION_CONTINUE_SEARCH when pType is not one of
// the catchable type for the thrown object. This
// function is made for use with COM+ EH, where they
// attempt to do C++ EH as well.
//
extern "C" int __cdecl __CxxExceptionFilter(
void *ppExcept, // Information for this (logical)
// exception
void *pType, // Info about the datatype.
int adjectives, // Extra Info about the datatype.
void *pBuildObj // Pointer to datatype.
)
{
#if _EH_RELATIVE_TYPEINFO
__int32 const *ppCatchable;
#elif defined(_WIN64)
CatchableType * UNALIGNED const *ppCatchable;
#else
CatchableType * const *ppCatchable;
#endif
CatchableType *pCatchable;
int catchables;
EHExceptionRecord *pExcept;
if (!ppExcept) {
return EXCEPTION_CONTINUE_SEARCH;
}
pExcept = *(EHExceptionRecord **)ppExcept;
// If catch all, always return EXCEPTION_EXECUTE_HANDLER
// Note that if the adjective has HT_IsStdDotDot flag on, we comply Std
// C++ eh behaviour, i.e. only catching C++ objects.
if ( TD_IS_TYPE_ELLIPSIS((TypeDescriptor *)pType) &&
(pExcept->ExceptionCode == MANAGED_EXCEPTION_CODE ||
pExcept->ExceptionCode == MANAGED_EXCEPTION_CODE_V4 ||
!(adjectives & HT_IsStdDotDot)))
{
if (PER_IS_MSVC_EH(pExcept))
{
if ( PER_PTHROW(pExcept) == nullptr)
{
if ( _pCurrentException == nullptr) {
return EXCEPTION_CONTINUE_SEARCH;
}
}
}
__ProcessingThrow++;
return EXCEPTION_EXECUTE_HANDLER;
}
if (PER_IS_MSVC_EH(pExcept))
{
if ( PER_PTHROW(pExcept) == nullptr) {
if (_pCurrentException == nullptr)
return EXCEPTION_CONTINUE_SEARCH;
pExcept = _pCurrentException;
}
/*
* Note that we will only be using pType or dispType
* and adjective field of pCatch.
*/
struct _s_HandlerType pCatch;
#if _EH_RELATIVE_TYPEINFO
_SetThrowImageBase((ptrdiff_t)pExcept->params.pThrowImageBase);
#endif
#if _EH_RELATIVE_FUNCINFO
ptrdiff_t ImageBaseBak = _GetImageBase();
__try //Try finally for restoring _ImageBase;
#endif
{
#if _EH_RELATIVE_FUNCINFO
void *CatchImageBase;
// Set the image base using Rtl API.
CatchImageBase = RtlPcToFileHeader((PVOID)pType, &CatchImageBase);
_SetImageBase((ptrdiff_t)CatchImageBase);
pCatch.dispType = (__int32)(
(ptrdiff_t)pType - (ptrdiff_t)CatchImageBase);
#else
pCatch.pType = (TypeDescriptor *)pType;
#endif
pCatch.adjectives = adjectives | HT_IsComplusEh;
// Scan all types that thrown object can be converted to:
ppCatchable = THROW_CTLIST(*PER_PTHROW(pExcept));
for (catchables = THROW_COUNT(*PER_PTHROW(pExcept));
catchables > 0; catchables--, ppCatchable++) {
#if _EH_RELATIVE_TYPEINFO
pCatchable = (CatchableType *)(_GetThrowImageBase() + *ppCatchable);
#else
pCatchable = *ppCatchable;
#endif
if (__TypeMatch(&pCatch, pCatchable, PER_PTHROW(pExcept))) {
// Successful. Now build the object.
__ProcessingThrow++;
if (pBuildObj != nullptr)
__BuildCatchObject(pExcept, pBuildObj, &pCatch, pCatchable);
return EXCEPTION_EXECUTE_HANDLER;
}
} // Scan possible conversions
}
#if _EH_RELATIVE_FUNCINFO
__finally {
_SetImageBase(ImageBaseBak);
}
#endif
}
return EXCEPTION_CONTINUE_SEARCH;
}
////////////////////////////////////////////////////////////////////////////////
//
// __CxxRegisterExceptionObject() - Registers an Exception object for managed
// C++ exception handling.
//
extern "C" int __cdecl __CxxRegisterExceptionObject(
void *ppExcept,
void *pStorage
)
{
// This function is only called for C++ EH.
EHExceptionRecord *pExcept = nullptr;
FRAMEINFO *pFrameInfo = (FRAMEINFO *)pStorage;
EHExceptionRecord **ppSaveException;
CONTEXT **ppSaveExContext;
ppSaveException = (EHExceptionRecord **)(&pFrameInfo[1]);
ppSaveExContext = (CONTEXT **)(&ppSaveException[1]);
if (ppExcept != nullptr && (*(void **)ppExcept) != nullptr) {
pExcept = *(EHExceptionRecord **)ppExcept;
if (PER_IS_MSVC_EH(pExcept)) {
if ( PER_PTHROW(pExcept) == nullptr) {
// was a rethrow
pExcept = _pCurrentException;
}
}
pFrameInfo = _CreateFrameInfo(pFrameInfo, PER_PEXCEPTOBJ(pExcept));
*ppSaveException = _pCurrentException;
*ppSaveExContext = _pCurrentExContext;
_pCurrentException = pExcept;
} else {
*ppSaveException = (EHExceptionRecord *)-1;
*ppSaveExContext = (CONTEXT *)-1;
}
__ProcessingThrow--;
if ( __ProcessingThrow < 0)
__ProcessingThrow = 0;
return 1;
}
////////////////////////////////////////////////////////////////////////////////
//
// __CxxDetectRethrow() - Looks at the Exception and returns true if rethrow,
// false if not a rethrow. This is then used for
// destructing the exception object in
// __CxxUnregisterExceptionObject().
//
extern "C" int __cdecl __CxxDetectRethrow(
void *ppExcept
)
{
EHExceptionRecord *pExcept;
if (!ppExcept)
return EXCEPTION_CONTINUE_SEARCH;
pExcept = *(EHExceptionRecord **)ppExcept;
if (PER_IS_MSVC_EH(pExcept) && PER_PTHROW(pExcept) == nullptr) {
__ProcessingThrow++;
return EXCEPTION_EXECUTE_HANDLER;
}
return EXCEPTION_CONTINUE_SEARCH;
}
////////////////////////////////////////////////////////////////////////////////
//
// __CxxUnregisterExceptionObject - Destructs Exception Objects if rethrow ==
// true. Also set __pCurrentException and
// __pCurrentExContext() to current value.
//
extern "C" void __cdecl __CxxUnregisterExceptionObject(
void *pStorage,
int rethrow
)
{
FRAMEINFO *pFrameInfo = (FRAMEINFO *)pStorage;
EHExceptionRecord **ppSaveException;
CONTEXT **ppSaveExContext;
ppSaveException = (EHExceptionRecord **)(&pFrameInfo[1]);
ppSaveExContext = (CONTEXT **)(&ppSaveException[1]);
if (*ppSaveException != (void *)-1) {
_FindAndUnlinkFrame(pFrameInfo);
if ( !rethrow && PER_IS_MSVC_EH(_pCurrentException) &&
_IsExceptionObjectToBeDestroyed(PER_PEXCEPTOBJ(_pCurrentException))
) {
__DestructExceptionObject(_pCurrentException, TRUE);
}
if (PER_IS_MSVC_EH(_pCurrentException) && rethrow)
__ProcessingThrow--;
_pCurrentException = *ppSaveException;
_pCurrentExContext = *ppSaveExContext;
}
}
////////////////////////////////////////////////////////////////////////////////
//
// __CxxQueryExceptionSize - returns the value of Storage needed to save
// FrameInfo + two pointers.
//
extern "C" int __cdecl __CxxQueryExceptionSize(
void
)
{
return sizeof(FRAMEINFO) + sizeof(void *) + sizeof(void *);
}