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
|
#include "StarMiniDump.hpp"
#include "StarSignalHandler.hpp"
#include "StarFormat.hpp"
#include "StarString.hpp"
#include "StarLogging.hpp"
#include <windows.h>
namespace Star {
String g_sehMessage;
struct SignalHandlerImpl {
bool handlingFatal;
bool handlingInterrupt;
bool interrupted;
PVOID handler;
SignalHandlerImpl() : handlingFatal(false), handlingInterrupt(false), interrupted(false) {}
~SignalHandlerImpl() {
setHandleFatal(false);
setHandleInterrupt(false);
}
void setHandleFatal(bool b) {
handlingFatal = b;
if (handler) {
RemoveVectoredExceptionHandler(handler);
handler = nullptr;
}
if (handlingFatal)
handler = AddVectoredExceptionHandler(1, vectoredExceptionHandler);
}
void setHandleInterrupt(bool b) {
handlingInterrupt = b;
SetConsoleCtrlHandler(nullptr, false);
if (handlingInterrupt)
SetConsoleCtrlHandler((PHANDLER_ROUTINE)consoleCtrlHandler, true);
}
static void sehTrampoline() {
fatalError(g_sehMessage.utf8Ptr(), true);
}
static void handleFatalError(String const& msg, PEXCEPTION_POINTERS ExceptionInfo) {
if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) {
String mode;
DWORD modeFlag = ExceptionInfo->ExceptionRecord->ExceptionInformation[0];
if (modeFlag == 0)
mode = "Read";
else if (modeFlag == 1)
mode = "Write";
else if (modeFlag == 8)
mode = "Execute";
else
mode = strf("Mode({})", modeFlag);
g_sehMessage = strf("Access violation detected at {} ({} of address {})",
ExceptionInfo->ExceptionRecord->ExceptionAddress,
mode,
(PVOID)ExceptionInfo->ExceptionRecord->ExceptionInformation[1]);
} else {
g_sehMessage = msg;
g_sehMessage = strf("{} (%p @ {})",
g_sehMessage,
ExceptionInfo->ExceptionRecord->ExceptionCode,
ExceptionInfo->ExceptionRecord->ExceptionAddress);
for (DWORD i = 0; i < ExceptionInfo->ExceptionRecord->NumberParameters; i++)
g_sehMessage = strf("{} [{}]", g_sehMessage, (PVOID)ExceptionInfo->ExceptionRecord->ExceptionInformation[i]);
}
// setup a hijack into our own trampoline as if the failure actually was a
// function call
#ifdef STAR_ARCHITECTURE_X86_64
DWORD64 rsp = ExceptionInfo->ContextRecord->Rsp - 8;
DWORD64 rip = ExceptionInfo->ContextRecord->Rip; // an offset avoid the issue of gdb thinking
// the error is one statement too early, but
// the offset is instruction dependent, and we
// don't know its size + 1;
*((DWORD64*)rsp) = rip;
ExceptionInfo->ContextRecord->Rsp = rsp;
ExceptionInfo->ContextRecord->Rip = (DWORD64)&sehTrampoline;
#else
DWORD esp = ExceptionInfo->ContextRecord->Esp - 4;
DWORD eip = ExceptionInfo->ContextRecord->Eip; // an offset avoid the issue of gdb thinking the
// error is one statement too early, but the
// offset is instruction dependent, and we don't
// know its size + 1;
*((DWORD*)esp) = eip;
ExceptionInfo->ContextRecord->Esp = esp;
ExceptionInfo->ContextRecord->Eip = (DWORD)&sehTrampoline;
#endif
}
static LONG CALLBACK vectoredExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) {
LONG result = EXCEPTION_CONTINUE_SEARCH;
if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
if (HANDLE thread = CreateThread(NULL, 0, writeMiniDump, (void*)ExceptionInfo, 0, NULL)) {
WaitForSingleObject(thread, 10000);
CloseHandle(thread);
}
handleFatalError("Stack overflow detected", ExceptionInfo);
result = EXCEPTION_CONTINUE_EXECUTION;
}
if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) {
handleFatalError("Access violation detected", ExceptionInfo);
result = EXCEPTION_CONTINUE_EXECUTION;
}
if ((ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)) {
handleFatalError("Illegal instruction encountered", ExceptionInfo);
result = EXCEPTION_CONTINUE_EXECUTION;
}
if ((ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_DENORMAL_OPERAND)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_DIVIDE_BY_ZERO)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_INEXACT_RESULT)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_INVALID_OPERATION)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_OVERFLOW)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_STACK_CHECK)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_FLT_UNDERFLOW)
) {
handleFatalError("Floating point exception", ExceptionInfo);
result = EXCEPTION_CONTINUE_EXECUTION;
}
if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO) {
handleFatalError("Division by zero", ExceptionInfo);
result = EXCEPTION_CONTINUE_EXECUTION;
}
if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_INT_OVERFLOW) {
handleFatalError("Integer overflow", ExceptionInfo);
result = EXCEPTION_CONTINUE_EXECUTION;
}
if ((ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_DATATYPE_MISALIGNMENT)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ARRAY_BOUNDS_EXCEEDED)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_IN_PAGE_ERROR)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_INVALID_DISPOSITION)
|| (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_INVALID_HANDLE)) {
handleFatalError("Error occurred", ExceptionInfo);
result = EXCEPTION_CONTINUE_EXECUTION;
}
return result;
}
static BOOL WINAPI consoleCtrlHandler(DWORD) {
if (SignalHandler::s_singleton)
SignalHandler::s_singleton->interrupted = true;
return true;
}
};
SignalHandlerImplUPtr SignalHandler::s_singleton;
SignalHandler::SignalHandler() {
if (s_singleton)
throw StarException("Singleton SignalHandler has been constructed twice!");
s_singleton = make_unique<SignalHandlerImpl>();
}
SignalHandler::~SignalHandler() {
s_singleton.reset();
}
void SignalHandler::setHandleFatal(bool handleFatal) {
s_singleton->setHandleFatal(handleFatal);
}
bool SignalHandler::handlingFatal() const {
return s_singleton->handlingFatal;
}
void SignalHandler::setHandleInterrupt(bool handleInterrupt) {
s_singleton->setHandleInterrupt(handleInterrupt);
}
bool SignalHandler::handlingInterrupt() const {
return s_singleton->handlingInterrupt;
}
bool SignalHandler::interruptCaught() const {
return s_singleton->interrupted;
}
}
|