-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathCrashHandlerMacOS.cpp
277 lines (212 loc) · 6.73 KB
/
CrashHandlerMacOS.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
//===--- CrashHandlerMacOS.cpp - Swift crash handler for macOS ----------- ===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// The macOS crash handler implementation.
//
// We use signal handling rather than trying to use Mach exceptions here,
// because the latter would entail running a separate Mach server thread, and
// creates a much greater risk of interfering with the system wide Crash
// Reporter, which is a no-no.
//
//===----------------------------------------------------------------------===//
#ifdef __APPLE__
#include <TargetConditionals.h>
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
#include <mach/mach.h>
#include <mach/task.h>
#include <mach/thread_act.h>
#include <sys/mman.h>
#include <sys/ucontext.h>
#include <sys/wait.h>
#include <os/lock.h>
#include <errno.h>
#include <signal.h>
#include <spawn.h>
#include <unistd.h>
#include "swift/Runtime/Backtrace.h"
#include <cstring>
#include "BacktracePrivate.h"
#ifndef lengthof
#define lengthof(x) (sizeof(x) / sizeof(x[0]))
#endif
using namespace swift::runtime::backtrace;
namespace {
void handle_fatal_signal(int signum, siginfo_t *pinfo, void *uctx);
void suspend_other_threads();
void resume_other_threads();
CrashInfo crashInfo;
os_unfair_lock crashLock = OS_UNFAIR_LOCK_INIT;
const int signalsToHandle[] = {
SIGQUIT,
SIGABRT,
SIGBUS,
SIGFPE,
SIGILL,
SIGSEGV,
SIGTRAP
};
} // namespace
namespace swift {
namespace runtime {
namespace backtrace {
SWIFT_RUNTIME_STDLIB_INTERNAL int
_swift_installCrashHandler()
{
stack_t ss;
// See if an alternate signal stack already exists
if (sigaltstack(NULL, &ss) < 0)
return errno;
if (ss.ss_sp == 0) {
// No, so set one up
ss.ss_flags = 0;
ss.ss_size = SIGSTKSZ;
ss.ss_sp = mmap(0, ss.ss_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ss.ss_sp == MAP_FAILED)
return errno;
if (sigaltstack(&ss, NULL) < 0)
return errno;
}
// Now register signal handlers
struct sigaction sa;
sigfillset(&sa.sa_mask);
for (unsigned n = 0; n < lengthof(signalsToHandle); ++n) {
sigdelset(&sa.sa_mask, signalsToHandle[n]);
}
sa.sa_handler = NULL;
sa.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_NODEFER;
sa.sa_sigaction = handle_fatal_signal;
for (unsigned n = 0; n < lengthof(signalsToHandle); ++n) {
struct sigaction osa;
// See if a signal handler for this signal is already installed
if (sigaction(signalsToHandle[n], NULL, &osa) < 0)
return errno;
if (osa.sa_handler == SIG_DFL) {
// No, so install ours
if (sigaction(signalsToHandle[n], &sa, NULL) < 0)
return errno;
}
}
return 0;
}
} // namespace backtrace
} // namespace runtime
} // namespace swift
namespace {
void
suspend_other_threads()
{
os_unfair_lock_lock(&crashLock);
thread_t self = mach_thread_self();
thread_act_array_t threads;
mach_msg_type_number_t count = 0;
kern_return_t kr = task_threads(mach_task_self(), &threads, &count);
if (kr != KERN_SUCCESS)
return;
for (unsigned n = 0; n < count; ++n) {
if (threads[n] == self)
continue;
// Ignore the results of these two; if they fail there's nothing we can do
(void)thread_suspend(threads[n]);
(void)mach_port_deallocate(mach_task_self(), threads[n]);
}
vm_deallocate(mach_task_self(),
(vm_address_t)threads,
count * sizeof(threads[0]));
os_unfair_lock_unlock(&crashLock);
}
void
resume_other_threads()
{
os_unfair_lock_lock(&crashLock);
thread_t self = mach_thread_self();
thread_act_array_t threads;
mach_msg_type_number_t count = 0;
kern_return_t kr = task_threads(mach_task_self(), &threads, &count);
if (kr != KERN_SUCCESS)
return;
for (unsigned n = 0; n < count; ++n) {
if (threads[n] == self)
continue;
// Ignore the results of these two; if they fail there's nothing we can do
(void)thread_resume(threads[n]);
(void)mach_port_deallocate(mach_task_self(), threads[n]);
}
vm_deallocate(mach_task_self(),
(vm_address_t)threads,
count * sizeof(threads[0]));
os_unfair_lock_unlock(&crashLock);
}
void
handle_fatal_signal(int signum,
siginfo_t *pinfo,
void *uctx)
{
int old_err = errno;
// Prevent this from exploding if more than one thread gets here at once
suspend_other_threads();
// Remove our signal handlers; crashes should kill us here
for (unsigned n = 0; n < lengthof(signalsToHandle); ++n)
signal(signalsToHandle[n], SIG_DFL);
// Get our thread identifier
thread_identifier_info_data_t ident_info;
mach_msg_type_number_t ident_size = THREAD_IDENTIFIER_INFO_COUNT;
int ret = thread_info(mach_thread_self(),
THREAD_IDENTIFIER_INFO,
(int *)&ident_info,
&ident_size);
if (ret != KERN_SUCCESS)
return;
// Fill in crash info
crashInfo.crashing_thread = ident_info.thread_id;
crashInfo.signal = signum;
crashInfo.fault_address = (uint64_t)pinfo->si_addr;
crashInfo.mctx = (uint64_t)(((ucontext_t *)uctx)->uc_mcontext);
// Display a progress message
void *pc = 0;
ucontext_t *ctx = (ucontext_t *)uctx;
#if defined(__arm64__) && __DARWIN_OPAQUE_ARM_THREAD_STATE64
#define THREAD_STATE_MEMBER(x) __opaque_##x
#elif __DARWIN_UNIX03
#define THREAD_STATE_MEMBER(x) __##x
#else
#define THREAD_STATE_MEMBER(x) x
#endif
#if __DARWIN_UNIX03
#define CTX_MEMBER(x) __##x
#else
#define CTX_MEMBER(x) x
#endif
#if defined(__x86_64__)
pc = (void *)(ctx->uc_mcontext->CTX_MEMBER(ss).THREAD_STATE_MEMBER(rip));
#elif defined(__arm64__)
pc = (void *)(ctx->uc_mcontext->CTX_MEMBER(ss).THREAD_STATE_MEMBER(pc));
#endif
_swift_displayCrashMessage(signum, pc);
/* Start the backtracer; this will suspend the process, so there's no need
to try to suspend other threads from here. */
if (!_swift_spawnBacktracer(&crashInfo)) {
const char *message = _swift_backtraceSettings.color == OnOffTty::On
? " failed\n\n" : " failed ***\n\n";
if (_swift_backtraceSettings.outputTo == OutputTo::Stderr)
write(STDERR_FILENO, message, strlen(message));
else
write(STDOUT_FILENO, message, strlen(message));
}
// Restart the other threads
resume_other_threads();
// Restore errno and exit (to crash)
errno = old_err;
}
} // namespace
#endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
#endif // __APPLE__