This repository was archived by the owner on Jan 23, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy patherror.h
269 lines (211 loc) · 12.9 KB
/
error.h
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/*****************************************************************************/
#ifndef _ERROR_H_
#define_ERROR_H_
/*****************************************************************************/
#include<corjit.h>// for CORJIT_INTERNALERROR
#include<safemath.h>// For FitsIn, used by SafeCvt methods.
#defineFATAL_JIT_EXCEPTION0x02345678
classCompiler;
structErrorTrapParam
{
int errc;
ICorJitInfo* jitInfo;
EXCEPTION_POINTERS exceptionPointers;
ErrorTrapParam()
{
jitInfo = nullptr;
}
};
// Only catch JIT internal errors (will not catch EE generated Errors)
extern LONG __JITfilter(PEXCEPTION_POINTERS pExceptionPointers, LPVOID lpvParam);
#definesetErrorTrap(compHnd, ParamType, paramDef, paramRef) \
struct__JITParam : ErrorTrapParam \
{ \
ParamType param; \
} __JITparam; \
__JITparam.errc = CORJIT_INTERNALERROR; \
__JITparam.jitInfo = compHnd; \
__JITparam.param = paramRef; \
PAL_TRY(__JITParam*, __JITpParam, &__JITparam) \
{ \
ParamType paramDef = __JITpParam->param;
// Only catch JIT internal errors (will not catch EE generated Errors)
#defineimpJitErrorTrap() \
} \
PAL_EXCEPT_FILTER(__JITfilter) \
{ \
int __errc = __JITparam.errc; \
(void)__errc;
#defineendErrorTrap() \
} \
PAL_ENDTRY
#definefinallyErrorTrap() \
} \
PAL_FINALLY \
{
/*****************************************************************************/
// clang-format off
externvoiddebugError(constchar* msg, constchar* file, unsigned line);
externvoid DECLSPEC_NORETURN badCode();
externvoid DECLSPEC_NORETURN badCode3(constchar* msg, constchar* msg2, int arg, __in_z constchar* file, unsigned line);
externvoid DECLSPEC_NORETURN noWay();
externvoid DECLSPEC_NORETURN NOMEM();
externvoid DECLSPEC_NORETURN fatal(int errCode);
externvoid DECLSPEC_NORETURN noWayAssertBody();
externvoid DECLSPEC_NORETURN noWayAssertBody(constchar* cond, constchar* file, unsigned line);
// Conditionally invoke the noway assert body. The conditional predicate is evaluated using a method on the tlsCompiler.
// If a noway_assert is hit, we ask the Compiler whether to raise an exception (i.e., conditionally raise exception.)
// To have backward compatibility between v4.5 and v4.0, in min-opts we take a shot at codegen rather than rethrow.
externvoid ANALYZER_NORETURN noWayAssertBodyConditional(
#ifdef FEATURE_TRACELOGGING
constchar* file, unsigned line
#endif
);
externvoid ANALYZER_NORETURN noWayAssertBodyConditional(constchar* cond, constchar* file, unsigned line);
// Define MEASURE_NOWAY to 1 to enable code to count and rank individual noway_assert calls by occurrence.
// These asserts would be dynamically executed, but not necessarily fail. The provides some insight into
// the dynamic prevalence of these (if not a direct measure of their cost), which exist in non-DEBUG as
// well as DEBUG builds.
#ifdef DEBUG
#defineMEASURE_NOWAY1
#else// !DEBUG
#defineMEASURE_NOWAY0
#endif// !DEBUG
#if MEASURE_NOWAY
externvoidRecordNowayAssertGlobal(constchar* filename, unsigned line, constchar* condStr);
#defineRECORD_NOWAY_ASSERT(condStr) RecordNowayAssertGlobal(__FILE__, __LINE__, condStr);
#else
#defineRECORD_NOWAY_ASSERT(condStr)
#endif
#ifdef DEBUG
#defineNO_WAY(msg) (debugError(msg, __FILE__, __LINE__), noWay())
// Used for fallback stress mode
#defineNO_WAY_NOASSERT(msg) noWay()
#defineBADCODE(msg) (debugError(msg, __FILE__, __LINE__), badCode())
#defineBADCODE3(msg, msg2, arg) badCode3(msg, msg2, arg, __FILE__, __LINE__)
// Used for an assert that we want to convert into BADCODE to force minopts, or in minopts to force codegen.
#definenoway_assert(cond) \
do \
{ \
RECORD_NOWAY_ASSERT(#cond) \
if (!(cond)) \
{ \
noWayAssertBodyConditional(#cond, __FILE__, __LINE__); \
} \
} while (0)
#defineunreached() noWayAssertBody("unreached", __FILE__, __LINE__)
#defineNOWAY_MSG(msg) noWayAssertBodyConditional(msg, __FILE__, __LINE__)
#else// !DEBUG
#defineNO_WAY(msg) noWay()
#defineBADCODE(msg) badCode()
#defineBADCODE3(msg, msg2, arg) badCode()
#ifdef FEATURE_TRACELOGGING
#defineNOWAY_ASSERT_BODY_ARGUMENTS __FILE__, __LINE__
#else
#defineNOWAY_ASSERT_BODY_ARGUMENTS
#endif
#definenoway_assert(cond) \
do \
{ \
RECORD_NOWAY_ASSERT(#cond) \
if (!(cond)) \
{ \
noWayAssertBodyConditional(NOWAY_ASSERT_BODY_ARGUMENTS); \
} \
} while (0)
#defineunreached() noWayAssertBody()
#defineNOWAY_MSG(msg) noWayAssertBodyConditional(NOWAY_ASSERT_BODY_ARGUMENTS)
#endif// !DEBUG
// IMPL_LIMITATION is called when we encounter valid IL that is not
// supported by our current implementation because of various
// limitations (that could be removed in the future)
#defineIMPL_LIMITATION(msg) NO_WAY(msg)
#if1// All platforms currently enable NYI; this should be a tighter condition to exclude some platforms from NYI
#if defined(ALT_JIT)
// This guy can return based on Config flag/Debugger
externvoidnotYetImplemented(constchar* msg, constchar* file, unsigned line);
#defineNYIRAW(msg) notYetImplemented(msg, __FILE__, __LINE__)
#else// !defined(ALT_JIT)
#defineNYIRAW(msg) NOWAY_MSG(msg)
#endif// !defined(ALT_JIT)
#defineNYI(msg) NYIRAW("NYI: " msg)
#defineNYI_IF(cond, msg) if (cond) NYIRAW("NYI: " msg)
#ifdef _TARGET_AMD64_
#defineNYI_AMD64(msg) NYIRAW("NYI_AMD64: " msg)
#defineNYI_X86(msg) do { } while (0)
#defineNYI_ARM(msg) do { } while (0)
#defineNYI_ARM64(msg) do { } while (0)
#elif defined(_TARGET_X86_)
#defineNYI_AMD64(msg) do { } while (0)
#defineNYI_X86(msg) NYIRAW("NYI_X86: " msg)
#defineNYI_ARM(msg) do { } while (0)
#defineNYI_ARM64(msg) do { } while (0)
#elif defined(_TARGET_ARM_)
#defineNYI_AMD64(msg) do { } while (0)
#defineNYI_X86(msg) do { } while (0)
#defineNYI_ARM(msg) NYIRAW("NYI_ARM: " msg)
#defineNYI_ARM64(msg) do { } while (0)
#elif defined(_TARGET_ARM64_)
#defineNYI_AMD64(msg) do { } while (0)
#defineNYI_X86(msg) do { } while (0)
#defineNYI_ARM(msg) do { } while (0)
#defineNYI_ARM64(msg) NYIRAW("NYI_ARM64: " msg)
#else
#error "Unknown platform, not x86, ARM, or AMD64?"
#endif
#else // NYI not available; make it an assert.
#define NYI(msg) assert(!(msg))
#define NYI_AMD64(msg) do { } while (0)
#define NYI_ARM(msg) do { } while (0)
#define NYI_ARM64(msg) do { } while (0)
#endif// NYI not available
// clang-format on
#if defined(_HOST_X86_) && !defined(FEATURE_PAL)
// While debugging in an Debugger, the "int 3" will cause the program to break
// Outside, the exception handler will just filter out the "int 3".
#defineBreakIfDebuggerPresent() \
do \
{ \
__try \
{ \
__asm {int3} \
} \
__except (EXCEPTION_EXECUTE_HANDLER) \
{ \
} \
} while (0)
#else
#defineBreakIfDebuggerPresent() \
do \
{ \
if (IsDebuggerPresent()) \
DebugBreak(); \
} while (0)
#endif
#ifdef DEBUG
DWORD getBreakOnBadCode();
#endif
// For narrowing numeric conversions, the following two methods ensure that the
// source value fits in the destination type, using either "assert" or
// "noway_assert" to validate the conversion. Obviously, each returns the source value as
// the destination type.
// (There is an argument that these should be macros, to let the preprocessor capture
// a more useful file/line for the error message. But then we have to use comma expressions
// so that these can be used in expressions, etc., which is ugly. So I propose we rely on
// getting stack traces in other ways.)
template <typename Dst, typename Src>
inline Dst SafeCvtAssert(Src val)
{
assert(FitsIn<Dst>(val));
returnstatic_cast<Dst>(val);
}
template <typename Dst, typename Src>
inline Dst SafeCvtNowayAssert(Src val)
{
noway_assert(FitsIn<Dst>(val));
returnstatic_cast<Dst>(val);
}
#endif