forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThrow.cpp
More file actions
325 lines (284 loc) · 9.96 KB
/
Throw.cpp
File metadata and controls
325 lines (284 loc) · 9.96 KB
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "CommonExceptionsPch.h"
#ifndef USING_PAL_STDLIB
// === C Runtime Header Files ===
#pragma warning(push)
#pragma warning(disable: 4995) /* 'function': name was marked as #pragma deprecated */
#include <strsafe.h>
#pragma warning(pop)
#endif
#include "StackOverflowException.h"
#include "AsmJsParseException.h"
#include "OutOfMemoryException.h"
#include "NotImplementedException.h"
// Header files required before including ConfigFlagsTable.h
#include "EnumHelp.h"
#include "Common/MathUtil.h"
#include "Core/AllocSizeMath.h"
#include "Core/FaultInjection.h"
#include "Core/BasePtr.h"
#include "Core/AutoFile.h"
#include "Core/Output.h"
// Memory Management
namespace Memory {
class ArenaAllocator;
}
using namespace Memory;
#include "Memory/Allocator.h"
#include "Memory/HeapAllocator.h"
// Data structure
#include "DataStructures/Comparer.h"
#include "DataStructures/SizePolicy.h"
#include "DataStructures/SList.h"
#include "DataStructures/KeyValuePair.h"
#include "DataStructures/DefaultContainerLockPolicy.h"
#include "DataStructures/BaseDictionary.h"
#include "Core/ConfigFlagsTable.h"
#include "Core/StackBackTrace.h"
#ifdef GENERATE_DUMP
// dbghelp.h is not clean with warning 4091
#pragma warning(push)
#pragma warning(disable: 4091) /* warning C4091: 'typedef ': ignored on left of '' when no variable is declared */
#include <dbghelp.h>
#pragma warning(pop)
#endif // GENERATE_DUMP
extern "C"{
BOOLEAN IsMessageBoxWPresent();
}
namespace Js {
#if defined(GENERATE_DUMP) && defined(STACK_BACK_TRACE)
THREAD_LOCAL StackBackTrace * Throw::stackBackTrace = nullptr;
#endif
void Throw::FatalInternalError()
{
int scenario = 2;
ReportFatalException(NULL, E_FAIL, Fatal_Internal_Error, scenario);
}
void Throw::FatalInternalErrorEx(int scenario)
{
ReportFatalException(NULL, E_FAIL, Fatal_Internal_Error, scenario);
}
void Throw::FatalProjectionError()
{
RaiseException((DWORD)DBG_TERMINATE_PROCESS, EXCEPTION_NONCONTINUABLE, 0, NULL);
}
void Throw::InternalError()
{
AssertOrFailFastMsg(false, "Internal error!!");
}
void Throw::OutOfMemory()
{
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
if (CONFIG_FLAG(PrintSystemException))
{
Output::Print(_u("SystemException: OutOfMemory\n"));
Output::Flush();
}
#endif
if (JsUtil::ExternalApi::RaiseOutOfMemoryIfScriptActive())
{
AssertMsg(false, "We shouldn't be here");
}
throw OutOfMemoryException();
}
void Throw::CheckAndThrowOutOfMemory(BOOLEAN status)
{
if (!status)
{
OutOfMemory();
}
}
void Throw::StackOverflow(ScriptContext *scriptContext, PVOID returnAddress)
{
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
if (CONFIG_FLAG(PrintSystemException))
{
Output::Print(_u("SystemException: StackOverflow\n"));
Output::Flush();
}
#endif
if (JsUtil::ExternalApi::RaiseStackOverflowIfScriptActive(scriptContext, returnAddress))
{
AssertMsg(false, "We shouldn't be here");
}
throw StackOverflowException();
}
void Throw::NotImplemented()
{
AssertMsg(false, "This functionality is not yet implemented");
throw NotImplementedException();
}
#ifdef GENERATE_DUMP
CriticalSection Throw::csGenerateDump;
void Throw::GenerateDump(LPCWSTR filePath, bool terminate, bool needLock)
{
__try
{
if (terminate)
{
RaiseException((DWORD)DBG_TERMINATE_PROCESS, EXCEPTION_NONCONTINUABLE, 0, NULL);
}
else
{
RaiseException(0, 0, 0, NULL);
}
}
__except(Throw::GenerateDump(GetExceptionInformation(), filePath,
terminate? EXCEPTION_CONTINUE_SEARCH : EXCEPTION_EXECUTE_HANDLER), needLock)
{
// we don't do anything interesting in this handler
}
}
void Throw::GenerateDumpForAssert(LPCWSTR filePath)
{
__try
{
RaiseException(STATUS_ASSERTION_FAILURE, EXCEPTION_NONCONTINUABLE, 0, NULL);
}
__except (Throw::GenerateDump(GetExceptionInformation(), filePath, EXCEPTION_CONTINUE_SEARCH), false)
{
// no-op
}
}
int Throw::GenerateDump(PEXCEPTION_POINTERS exceptInfo, LPCWSTR filePath, int ret, bool needLock)
{
WCHAR tempFilePath[MAX_PATH];
WCHAR tempFileName[MAX_PATH];
HANDLE hTempFile;
DWORD retVal;
if (filePath == NULL)
{
retVal = GetTempPath(MAX_PATH, tempFilePath);
if (retVal > MAX_PATH || (retVal == 0))
{
return ret;
}
filePath = tempFilePath;
}
StringCchPrintf(tempFileName, _countof(tempFileName), _u("%s\\CH_%u_%u.dmp"), filePath, GetCurrentProcessId(), GetCurrentThreadId());
Output::Print(_u("dump filename %s \n"), tempFileName);
Output::Flush();
hTempFile = CreateFile(tempFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hTempFile == INVALID_HANDLE_VALUE)
{
return GetLastError();
}
MINIDUMP_EXCEPTION_INFORMATION dumpExceptInfo;
dumpExceptInfo.ThreadId = GetCurrentThreadId();
dumpExceptInfo.ExceptionPointers = exceptInfo;
dumpExceptInfo.ClientPointers = FALSE;
{
MINIDUMP_TYPE dumpType = static_cast<MINIDUMP_TYPE>(MiniDumpWithDataSegs | MiniDumpWithPrivateReadWriteMemory);
// Generating full dump for the TE process (reason : it contains both managed and native memory)
if (CONFIG_FLAG(FullMemoryDump))
{
dumpType = static_cast<MINIDUMP_TYPE>(dumpType | MiniDumpWithFullMemory);
}
BOOL dumpGenerated = false;
if (needLock)
{
// the critical section might have been destructed at process shutdown time. At that time we don't need
// to lock.
AutoCriticalSection autocs(&csGenerateDump);
dumpGenerated = MiniDumpWriteDump(GetCurrentProcess(),
GetCurrentProcessId(),
hTempFile,
dumpType,
&dumpExceptInfo,
NULL,
NULL);
}
else
{
dumpGenerated = MiniDumpWriteDump(GetCurrentProcess(),
GetCurrentProcessId(),
hTempFile,
dumpType,
&dumpExceptInfo,
NULL,
NULL);
}
if (!dumpGenerated)
{
Output::Print(_u("Unable to write minidump (0x%08X)\n"), GetLastError());
Output::Flush();
}
}
FlushFileBuffers(hTempFile);
CloseHandle(hTempFile);
return ret;
}
#endif // GENERATE_DUMP
#if DBG
// After assert the program should terminate. Sometime we saw the program continue somehow
// log the existence of assert for debugging.
void Throw::LogAssert()
{
IsInAssert = true;
#ifdef STACK_BACK_TRACE
// This should be the last thing to happen in the process. Therefore, leaks are not an issue.
stackBackTrace = StackBackTrace::Capture(&NoCheckHeapAllocator::Instance, Throw::StackToSkip, Throw::StackTraceDepth);
#endif
}
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
#define CHAKRA_ASSERT_CAPTION _u("CHAKRA ASSERT")
#endif
bool Throw::ReportAssert(__in LPCSTR fileName, uint lineNumber, __in LPCSTR error, __in LPCSTR message)
{
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
if (Js::Configuration::Global.flags.IsEnabled(Js::AssertBreakFlag))
{
DebugBreak();
return false;
}
if (Js::Configuration::Global.flags.IsEnabled(Js::AssertIgnoreFlag))
{
return true;
}
#endif
if (AssertsToConsole)
{
fprintf(stderr, "ASSERTION %u: (%s, line %u) %s\n Failure: %s\n", GetCurrentProcessId(), fileName, lineNumber, message, error);
fflush(stderr);
#ifdef GENERATE_DUMP
// force dump if we have assert in jc.exe. check build only.
if (!Js::Configuration::Global.flags.IsEnabled(Js::DumpOnCrashFlag))
{
return false;
}
Throw::GenerateDumpForAssert(Js::Configuration::Global.flags.DumpOnCrash);
#else
return false;
#endif
}
// The following code is applicable only when we are hosted in an
// GUI environment
#if defined(ENABLE_DEBUG_CONFIG_OPTIONS) && defined(_WIN32)
// Then if DumpOncrashFlag is not specified it directly returns,
// otherwise if will raise a non-continuable exception, generate the dump and terminate the process.
// the popup message box might be useful when testing in IE
if (Js::Configuration::Global.flags.AssertPopUp && IsMessageBoxWPresent())
{
char16 buff[1024];
swprintf_s(buff, _countof(buff), _u("%S (%u)\n%S\n%S"), fileName, lineNumber, message, error);
buff[_countof(buff)-1] = 0;
int ret = MessageBox(nullptr, buff, CHAKRA_ASSERT_CAPTION, MB_ABORTRETRYIGNORE);
switch (ret)
{
case IDIGNORE:
return true;
case IDABORT:
Throw::FatalInternalError();
default:
return false;
}
}
#endif
return false;
}
#endif
} // namespace Js