Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/ExceptionHandlerSetup.cpp
3185 views
1
// Copyright 2008 Dolphin Emulator Project
2
// Licensed under GPLv2+
3
// Refer to the license.txt file included.
4
5
// The corresponding file is called MemTools in the Dolphin project.
6
7
#include "ppsspp_config.h"
8
9
#include <cstdio>
10
#include <cstdlib>
11
#include <cstring>
12
#include <vector>
13
#include <thread>
14
15
#include "Common/CommonFuncs.h"
16
#include "Common/CommonTypes.h"
17
#include "Common/Log.h"
18
#include "Common/Thread/ThreadUtil.h"
19
#include "Common/MachineContext.h"
20
#include "Common/ExceptionHandlerSetup.h"
21
22
static BadAccessHandler g_badAccessHandler;
23
static void *altStack = nullptr;
24
25
#ifdef MACHINE_CONTEXT_SUPPORTED
26
27
// We cannot handle exceptions in UWP builds. Bleh.
28
#if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP)
29
30
static PVOID g_vectoredExceptionHandle;
31
32
static LONG NTAPI GlobalExceptionHandler(PEXCEPTION_POINTERS pPtrs) {
33
switch (pPtrs->ExceptionRecord->ExceptionCode) {
34
case EXCEPTION_ACCESS_VIOLATION:
35
{
36
int accessType = (int)pPtrs->ExceptionRecord->ExceptionInformation[0];
37
if (accessType == 8) { // Rule out DEP
38
return (DWORD)EXCEPTION_CONTINUE_SEARCH;
39
}
40
41
// virtual address of the inaccessible data
42
uintptr_t badAddress = (uintptr_t)pPtrs->ExceptionRecord->ExceptionInformation[1];
43
CONTEXT* ctx = pPtrs->ContextRecord;
44
45
if (g_badAccessHandler(badAddress, ctx)) {
46
return (DWORD)EXCEPTION_CONTINUE_EXECUTION;
47
} else {
48
// Let's not prevent debugging.
49
return (DWORD)EXCEPTION_CONTINUE_SEARCH;
50
}
51
}
52
53
case EXCEPTION_STACK_OVERFLOW:
54
// Dolphin has some handling of this for the RET optimization emulation.
55
return EXCEPTION_CONTINUE_SEARCH;
56
57
case EXCEPTION_ILLEGAL_INSTRUCTION:
58
// No SSE support? Or simply bad codegen?
59
return EXCEPTION_CONTINUE_SEARCH;
60
61
case EXCEPTION_PRIV_INSTRUCTION:
62
// okay, dynarec codegen is obviously broken.
63
return EXCEPTION_CONTINUE_SEARCH;
64
65
case EXCEPTION_IN_PAGE_ERROR:
66
// okay, something went seriously wrong, out of memory?
67
return EXCEPTION_CONTINUE_SEARCH;
68
69
case EXCEPTION_BREAKPOINT:
70
// might want to do something fun with this one day?
71
return EXCEPTION_CONTINUE_SEARCH;
72
73
default:
74
return EXCEPTION_CONTINUE_SEARCH;
75
}
76
}
77
78
void InstallExceptionHandler(BadAccessHandler badAccessHandler) {
79
if (g_vectoredExceptionHandle) {
80
g_badAccessHandler = badAccessHandler;
81
return;
82
}
83
84
INFO_LOG(Log::System, "Installing exception handler");
85
g_badAccessHandler = badAccessHandler;
86
#ifdef USE_ASAN
87
g_vectoredExceptionHandle = AddVectoredExceptionHandler(FALSE, GlobalExceptionHandler);
88
#else
89
g_vectoredExceptionHandle = AddVectoredExceptionHandler(TRUE, GlobalExceptionHandler);
90
#endif
91
}
92
93
void UninstallExceptionHandler() {
94
if (g_vectoredExceptionHandle) {
95
RemoveVectoredExceptionHandler(g_vectoredExceptionHandle);
96
INFO_LOG(Log::System, "Removed exception handler");
97
g_vectoredExceptionHandle = nullptr;
98
}
99
g_badAccessHandler = nullptr;
100
}
101
102
#elif defined(__APPLE__)
103
104
static void CheckKR(const char* name, kern_return_t kr) {
105
_assert_msg_(kr == 0, "%s failed: kr=%x", name, kr);
106
}
107
108
static void ExceptionThread(mach_port_t port) {
109
SetCurrentThreadName("Mach exception thread");
110
#pragma pack(4)
111
struct {
112
mach_msg_header_t Head;
113
NDR_record_t NDR;
114
exception_type_t exception;
115
mach_msg_type_number_t codeCnt;
116
int64_t code[2];
117
int flavor;
118
mach_msg_type_number_t old_stateCnt;
119
natural_t old_state[x86_THREAD_STATE64_COUNT];
120
mach_msg_trailer_t trailer;
121
} msg_in;
122
123
struct {
124
mach_msg_header_t Head;
125
NDR_record_t NDR;
126
kern_return_t RetCode;
127
int flavor;
128
mach_msg_type_number_t new_stateCnt;
129
natural_t new_state[x86_THREAD_STATE64_COUNT];
130
} msg_out;
131
#pragma pack()
132
memset(&msg_in, 0xee, sizeof(msg_in));
133
memset(&msg_out, 0xee, sizeof(msg_out));
134
mach_msg_header_t* send_msg = &msg_out.Head;
135
mach_msg_size_t send_size = 0;
136
mach_msg_option_t option = MACH_RCV_MSG;
137
while (true) {
138
// If this isn't the first run, send the reply message. Then, receive
139
// a message: either a mach_exception_raise_state RPC due to
140
// thread_set_exception_ports, or MACH_NOTIFY_NO_SENDERS due to
141
// mach_port_request_notification.
142
CheckKR("mach_msg_overwrite",
143
mach_msg_overwrite(send_msg, option, send_size, sizeof(msg_in), port,
144
MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL, &msg_in.Head, 0));
145
146
if (msg_in.Head.msgh_id == MACH_NOTIFY_NO_SENDERS) {
147
// the other thread exited
148
mach_port_destroy(mach_task_self(), port);
149
return;
150
}
151
152
_assert_msg_(msg_in.Head.msgh_id == 2406, "unknown message received");
153
_assert_msg_(msg_in.flavor == x86_THREAD_STATE64, "unknown flavor %d (expected %d)", msg_in.flavor, x86_THREAD_STATE64);
154
155
x86_thread_state64_t* state = (x86_thread_state64_t*)msg_in.old_state;
156
157
bool ok = g_badAccessHandler((uintptr_t)msg_in.code[1], state);
158
159
// Set up the reply.
160
msg_out.Head.msgh_bits = MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(msg_in.Head.msgh_bits), 0);
161
msg_out.Head.msgh_remote_port = msg_in.Head.msgh_remote_port;
162
msg_out.Head.msgh_local_port = MACH_PORT_NULL;
163
msg_out.Head.msgh_id = msg_in.Head.msgh_id + 100;
164
msg_out.NDR = msg_in.NDR;
165
if (ok) {
166
msg_out.RetCode = KERN_SUCCESS;
167
msg_out.flavor = x86_THREAD_STATE64;
168
msg_out.new_stateCnt = x86_THREAD_STATE64_COUNT;
169
memcpy(msg_out.new_state, msg_in.old_state, x86_THREAD_STATE64_COUNT * sizeof(natural_t));
170
} else {
171
// Pass the exception to the next handler (debugger or crash).
172
msg_out.RetCode = KERN_FAILURE;
173
msg_out.flavor = 0;
174
msg_out.new_stateCnt = 0;
175
}
176
msg_out.Head.msgh_size =
177
offsetof(__typeof__(msg_out), new_state) + msg_out.new_stateCnt * sizeof(natural_t);
178
179
send_msg = &msg_out.Head;
180
send_size = msg_out.Head.msgh_size;
181
option |= MACH_SEND_MSG;
182
}
183
}
184
185
void InstallExceptionHandler(BadAccessHandler badAccessHandler) {
186
if (g_badAccessHandler) {
187
// The rest of the setup we don't need to do again.
188
g_badAccessHandler = badAccessHandler;
189
return;
190
}
191
g_badAccessHandler = badAccessHandler;
192
193
INFO_LOG(Log::System, "Installing exception handler");
194
mach_port_t port;
195
CheckKR("mach_port_allocate",
196
mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port));
197
std::thread exc_thread(ExceptionThread, port);
198
exc_thread.detach();
199
// Obtain a send right for thread_set_exception_ports to copy...
200
CheckKR("mach_port_insert_right",
201
mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND));
202
// Mach tries the following exception ports in order: thread, task, host.
203
// Debuggers set the task port, so we grab the thread port.
204
CheckKR("thread_set_exception_ports",
205
thread_set_exception_ports(mach_thread_self(), EXC_MASK_BAD_ACCESS, port,
206
EXCEPTION_STATE | MACH_EXCEPTION_CODES, x86_THREAD_STATE64));
207
// ...and get rid of our copy so that MACH_NOTIFY_NO_SENDERS works.
208
CheckKR("mach_port_mod_refs",
209
mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, -1));
210
mach_port_t previous;
211
CheckKR("mach_port_request_notification",
212
mach_port_request_notification(mach_task_self(), port, MACH_NOTIFY_NO_SENDERS, 0, port,
213
MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous));
214
}
215
216
void UninstallExceptionHandler() {
217
}
218
219
#else
220
221
#include <signal.h>
222
223
static struct sigaction old_sa_segv;
224
static struct sigaction old_sa_bus;
225
226
static void sigsegv_handler(int sig, siginfo_t* info, void* raw_context) {
227
if (sig != SIGSEGV && sig != SIGBUS) {
228
// We are not interested in other signals - handle it as usual.
229
return;
230
}
231
ucontext_t* context = (ucontext_t*)raw_context;
232
int sicode = info->si_code;
233
if (sicode != SEGV_MAPERR && sicode != SEGV_ACCERR) {
234
// Huh? Return.
235
return;
236
}
237
uintptr_t bad_address = (uintptr_t)info->si_addr;
238
239
// Get all the information we can out of the context.
240
#ifdef __OpenBSD__
241
ucontext_t* ctx = context;
242
#else
243
mcontext_t* ctx = &context->uc_mcontext;
244
#endif
245
// assume it's not a write
246
if (!g_badAccessHandler(bad_address,
247
#ifdef __APPLE__
248
*ctx
249
#else
250
ctx
251
#endif
252
)) {
253
// retry and crash
254
// According to the sigaction man page, if sa_flags "SA_SIGINFO" is set to the sigaction
255
// function pointer, otherwise sa_handler contains one of:
256
// SIG_DEF: The 'default' action is performed
257
// SIG_IGN: The signal is ignored
258
// Any other value is a function pointer to a signal handler
259
260
struct sigaction* old_sa;
261
if (sig == SIGSEGV) {
262
old_sa = &old_sa_segv;
263
} else {
264
old_sa = &old_sa_bus;
265
}
266
267
if (old_sa->sa_flags & SA_SIGINFO) {
268
old_sa->sa_sigaction(sig, info, raw_context);
269
return;
270
}
271
if (old_sa->sa_handler == SIG_DFL) {
272
signal(sig, SIG_DFL);
273
return;
274
}
275
if (old_sa->sa_handler == SIG_IGN) {
276
// Ignore signal
277
return;
278
}
279
old_sa->sa_handler(sig);
280
}
281
}
282
283
void InstallExceptionHandler(BadAccessHandler badAccessHandler) {
284
if (!badAccessHandler) {
285
return;
286
}
287
if (g_badAccessHandler) {
288
g_badAccessHandler = badAccessHandler;
289
return;
290
}
291
292
size_t altStackSize = SIGSTKSZ;
293
294
// Add some extra room.
295
altStackSize += 65536;
296
297
INFO_LOG(Log::System, "Installed exception handler. stack size: %d", (int)altStackSize);
298
g_badAccessHandler = badAccessHandler;
299
300
stack_t signal_stack{};
301
altStack = malloc(altStackSize);
302
#ifdef __FreeBSD__
303
signal_stack.ss_sp = (char*)altStack;
304
#else
305
signal_stack.ss_sp = altStack;
306
#endif
307
signal_stack.ss_size = altStackSize;
308
signal_stack.ss_flags = 0;
309
if (sigaltstack(&signal_stack, nullptr)) {
310
_assert_msg_(false, "sigaltstack failed");
311
}
312
struct sigaction sa{};
313
sa.sa_handler = nullptr;
314
sa.sa_sigaction = &sigsegv_handler;
315
sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
316
sigemptyset(&sa.sa_mask);
317
sigaction(SIGSEGV, &sa, &old_sa_segv);
318
#ifdef __APPLE__
319
sigaction(SIGBUS, &sa, &old_sa_bus);
320
#endif
321
}
322
323
void UninstallExceptionHandler() {
324
if (!g_badAccessHandler) {
325
return;
326
}
327
stack_t signal_stack{};
328
signal_stack.ss_flags = SS_DISABLE;
329
if (0 != sigaltstack(&signal_stack, nullptr)) {
330
ERROR_LOG(Log::System, "Could not remove signal altstack");
331
}
332
if (altStack) {
333
free(altStack);
334
altStack = nullptr;
335
}
336
sigaction(SIGSEGV, &old_sa_segv, nullptr);
337
#ifdef __APPLE__
338
sigaction(SIGBUS, &old_sa_bus, nullptr);
339
#endif
340
INFO_LOG(Log::System, "Uninstalled exception handler");
341
g_badAccessHandler = nullptr;
342
}
343
344
#endif
345
346
#else // !MACHINE_CONTEXT_SUPPORTED
347
348
void InstallExceptionHandler(BadAccessHandler badAccessHandler) {
349
ERROR_LOG(Log::System, "Exception handler not implemented on this platform, can't install");
350
}
351
void UninstallExceptionHandler() { }
352
353
#endif // MACHINE_CONTEXT_SUPPORTED
354
355