1 //
2 // Copyright 2018 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "absl/debugging/failure_signal_handler.h"
18
19 #include "absl/base/config.h"
20
21 #ifdef _WIN32
22 #include <windows.h>
23 #else
24 #include <sched.h>
25 #include <unistd.h>
26 #endif
27
28 #ifdef __APPLE__
29 #include <TargetConditionals.h>
30 #endif
31
32 #ifdef ABSL_HAVE_MMAP
33 #include <sys/mman.h>
34 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
35 #define MAP_ANONYMOUS MAP_ANON
36 #endif
37 #endif
38
39 #ifdef __linux__
40 #include <sys/prctl.h>
41 #endif
42
43 #include <algorithm>
44 #include <atomic>
45 #include <cerrno>
46 #include <csignal>
47 #include <cstdio>
48 #include <cstring>
49 #include <ctime>
50
51 #include "absl/base/attributes.h"
52 #include "absl/base/internal/raw_logging.h"
53 #include "absl/base/internal/sysinfo.h"
54 #include "absl/debugging/internal/examine_stack.h"
55 #include "absl/debugging/stacktrace.h"
56
57 #if !defined(_WIN32) && !defined(__wasi__)
58 #define ABSL_HAVE_SIGACTION
59 // Apple WatchOS and TVOS don't allow sigaltstack
60 // Apple macOS has sigaltstack, but using it makes backtrace() unusable.
61 #if !(defined(TARGET_OS_OSX) && TARGET_OS_OSX) && \
62 !(defined(TARGET_OS_WATCH) && TARGET_OS_WATCH) && \
63 !(defined(TARGET_OS_TV) && TARGET_OS_TV) && !defined(__QNX__)
64 #define ABSL_HAVE_SIGALTSTACK
65 #endif
66 #endif
67
68 namespace absl {
69 ABSL_NAMESPACE_BEGIN
70
71 ABSL_CONST_INIT static FailureSignalHandlerOptions fsh_options;
72
73 // Resets the signal handler for signo to the default action for that
74 // signal, then raises the signal.
RaiseToDefaultHandler(int signo)75 static void RaiseToDefaultHandler(int signo) {
76 signal(signo, SIG_DFL);
77 raise(signo);
78 }
79
80 struct FailureSignalData {
81 const int signo;
82 const char* const as_string;
83 #ifdef ABSL_HAVE_SIGACTION
84 struct sigaction previous_action;
85 // StructSigaction is used to silence -Wmissing-field-initializers.
86 using StructSigaction = struct sigaction;
87 #define FSD_PREVIOUS_INIT FailureSignalData::StructSigaction()
88 #else
89 void (*previous_handler)(int);
90 #define FSD_PREVIOUS_INIT SIG_DFL
91 #endif
92 };
93
94 ABSL_CONST_INIT static FailureSignalData failure_signal_data[] = {
95 {SIGSEGV, "SIGSEGV", FSD_PREVIOUS_INIT},
96 {SIGILL, "SIGILL", FSD_PREVIOUS_INIT},
97 {SIGFPE, "SIGFPE", FSD_PREVIOUS_INIT},
98 {SIGABRT, "SIGABRT", FSD_PREVIOUS_INIT},
99 {SIGTERM, "SIGTERM", FSD_PREVIOUS_INIT},
100 #ifndef _WIN32
101 {SIGBUS, "SIGBUS", FSD_PREVIOUS_INIT},
102 {SIGTRAP, "SIGTRAP", FSD_PREVIOUS_INIT},
103 #endif
104 };
105
106 #undef FSD_PREVIOUS_INIT
107
RaiseToPreviousHandler(int signo)108 static void RaiseToPreviousHandler(int signo) {
109 // Search for the previous handler.
110 for (const auto& it : failure_signal_data) {
111 if (it.signo == signo) {
112 #ifdef ABSL_HAVE_SIGACTION
113 sigaction(signo, &it.previous_action, nullptr);
114 #else
115 signal(signo, it.previous_handler);
116 #endif
117 raise(signo);
118 return;
119 }
120 }
121
122 // Not found, use the default handler.
123 RaiseToDefaultHandler(signo);
124 }
125
126 namespace debugging_internal {
127
FailureSignalToString(int signo)128 const char* FailureSignalToString(int signo) {
129 for (const auto& it : failure_signal_data) {
130 if (it.signo == signo) {
131 return it.as_string;
132 }
133 }
134 return "";
135 }
136
137 } // namespace debugging_internal
138
139 #ifdef ABSL_HAVE_SIGALTSTACK
140
SetupAlternateStackOnce()141 static bool SetupAlternateStackOnce() {
142 #if defined(__wasm__) || defined(__asjms__)
143 const size_t page_mask = getpagesize() - 1;
144 #else
145 const size_t page_mask = static_cast<size_t>(sysconf(_SC_PAGESIZE)) - 1;
146 #endif
147 size_t stack_size =
148 (std::max(static_cast<size_t>(SIGSTKSZ), size_t{65536}) + page_mask) &
149 ~page_mask;
150 #if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
151 defined(ABSL_HAVE_MEMORY_SANITIZER) || defined(ABSL_HAVE_THREAD_SANITIZER)
152 // Account for sanitizer instrumentation requiring additional stack space.
153 stack_size *= 5;
154 #endif
155
156 stack_t sigstk;
157 memset(&sigstk, 0, sizeof(sigstk));
158 sigstk.ss_size = stack_size;
159
160 #ifdef ABSL_HAVE_MMAP
161 #ifndef MAP_STACK
162 #define MAP_STACK 0
163 #endif
164 sigstk.ss_sp = mmap(nullptr, sigstk.ss_size, PROT_READ | PROT_WRITE,
165 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
166 if (sigstk.ss_sp == MAP_FAILED) {
167 ABSL_RAW_LOG(FATAL, "mmap() for alternate signal stack failed");
168 }
169 #else
170 sigstk.ss_sp = malloc(sigstk.ss_size);
171 if (sigstk.ss_sp == nullptr) {
172 ABSL_RAW_LOG(FATAL, "malloc() for alternate signal stack failed");
173 }
174 #endif
175
176 if (sigaltstack(&sigstk, nullptr) != 0) {
177 ABSL_RAW_LOG(FATAL, "sigaltstack() failed with errno=%d", errno);
178 }
179
180 #ifdef __linux__
181 #if defined(PR_SET_VMA) && defined(PR_SET_VMA_ANON_NAME)
182 // Make a best-effort attempt to name the allocated region in
183 // /proc/$PID/smaps.
184 //
185 // The call to prctl() may fail if the kernel was not configured with the
186 // CONFIG_ANON_VMA_NAME kernel option. This is OK since the call is
187 // primarily a debugging aid.
188 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, sigstk.ss_sp, sigstk.ss_size,
189 "absl-signalstack");
190 #endif
191 #endif // __linux__
192
193 return true;
194 }
195
196 #endif
197
198 #ifdef ABSL_HAVE_SIGACTION
199
200 // Sets up an alternate stack for signal handlers once.
201 // Returns the appropriate flag for sig_action.sa_flags
202 // if the system supports using an alternate stack.
MaybeSetupAlternateStack()203 static int MaybeSetupAlternateStack() {
204 #ifdef ABSL_HAVE_SIGALTSTACK
205 ABSL_ATTRIBUTE_UNUSED static const bool kOnce = SetupAlternateStackOnce();
206 return SA_ONSTACK;
207 #else
208 return 0;
209 #endif
210 }
211
InstallOneFailureHandler(FailureSignalData * data,void (* handler)(int,siginfo_t *,void *))212 static void InstallOneFailureHandler(FailureSignalData* data,
213 void (*handler)(int, siginfo_t*, void*)) {
214 struct sigaction act;
215 memset(&act, 0, sizeof(act));
216 sigemptyset(&act.sa_mask);
217 act.sa_flags |= SA_SIGINFO;
218 // SA_NODEFER is required to handle SIGABRT from
219 // ImmediateAbortSignalHandler().
220 act.sa_flags |= SA_NODEFER;
221 if (fsh_options.use_alternate_stack) {
222 act.sa_flags |= MaybeSetupAlternateStack();
223 }
224 act.sa_sigaction = handler;
225 ABSL_RAW_CHECK(sigaction(data->signo, &act, &data->previous_action) == 0,
226 "sigaction() failed");
227 }
228
229 #else
230
InstallOneFailureHandler(FailureSignalData * data,void (* handler)(int))231 static void InstallOneFailureHandler(FailureSignalData* data,
232 void (*handler)(int)) {
233 data->previous_handler = signal(data->signo, handler);
234 ABSL_RAW_CHECK(data->previous_handler != SIG_ERR, "signal() failed");
235 }
236
237 #endif
238
WriteSignalMessage(int signo,int cpu,void (* writerfn)(const char *))239 static void WriteSignalMessage(int signo, int cpu,
240 void (*writerfn)(const char*)) {
241 char buf[96];
242 char on_cpu[32] = {0};
243 if (cpu != -1) {
244 snprintf(on_cpu, sizeof(on_cpu), " on cpu %d", cpu);
245 }
246 const char* const signal_string =
247 debugging_internal::FailureSignalToString(signo);
248 if (signal_string != nullptr && signal_string[0] != '\0') {
249 snprintf(buf, sizeof(buf), "*** %s received at time=%ld%s ***\n",
250 signal_string,
251 static_cast<long>(time(nullptr)), // NOLINT(runtime/int)
252 on_cpu);
253 } else {
254 snprintf(buf, sizeof(buf), "*** Signal %d received at time=%ld%s ***\n",
255 signo, static_cast<long>(time(nullptr)), // NOLINT(runtime/int)
256 on_cpu);
257 }
258 writerfn(buf);
259 }
260
261 // `void*` might not be big enough to store `void(*)(const char*)`.
262 struct WriterFnStruct {
263 void (*writerfn)(const char*);
264 };
265
266 // Many of the absl::debugging_internal::Dump* functions in
267 // examine_stack.h take a writer function pointer that has a void* arg
268 // for historical reasons. failure_signal_handler_writer only takes a
269 // data pointer. This function converts between these types.
WriterFnWrapper(const char * data,void * arg)270 static void WriterFnWrapper(const char* data, void* arg) {
271 static_cast<WriterFnStruct*>(arg)->writerfn(data);
272 }
273
274 // Convenient wrapper around DumpPCAndFrameSizesAndStackTrace() for signal
275 // handlers. "noinline" so that GetStackFrames() skips the top-most stack
276 // frame for this function.
WriteStackTrace(void * ucontext,bool symbolize_stacktrace,void (* writerfn)(const char *,void *),void * writerfn_arg)277 ABSL_ATTRIBUTE_NOINLINE static void WriteStackTrace(
278 void* ucontext, bool symbolize_stacktrace,
279 void (*writerfn)(const char*, void*), void* writerfn_arg) {
280 constexpr int kNumStackFrames = 32;
281 void* stack[kNumStackFrames];
282 int frame_sizes[kNumStackFrames];
283 int min_dropped_frames;
284 int depth = absl::GetStackFramesWithContext(
285 stack, frame_sizes, kNumStackFrames,
286 1, // Do not include this function in stack trace.
287 ucontext, &min_dropped_frames);
288 absl::debugging_internal::DumpPCAndFrameSizesAndStackTrace(
289 absl::debugging_internal::GetProgramCounter(ucontext), stack, frame_sizes,
290 depth, min_dropped_frames, symbolize_stacktrace, writerfn, writerfn_arg);
291 }
292
293 // Called by AbslFailureSignalHandler() to write the failure info. It is
294 // called once with writerfn set to WriteToStderr() and then possibly
295 // with writerfn set to the user provided function.
WriteFailureInfo(int signo,void * ucontext,int cpu,void (* writerfn)(const char *))296 static void WriteFailureInfo(int signo, void* ucontext, int cpu,
297 void (*writerfn)(const char*)) {
298 WriterFnStruct writerfn_struct{writerfn};
299 WriteSignalMessage(signo, cpu, writerfn);
300 WriteStackTrace(ucontext, fsh_options.symbolize_stacktrace, WriterFnWrapper,
301 &writerfn_struct);
302 }
303
304 // absl::SleepFor() can't be used here since AbslInternalSleepFor()
305 // may be overridden to do something that isn't async-signal-safe on
306 // some platforms.
PortableSleepForSeconds(int seconds)307 static void PortableSleepForSeconds(int seconds) {
308 #ifdef _WIN32
309 Sleep(static_cast<DWORD>(seconds * 1000));
310 #else
311 struct timespec sleep_time;
312 sleep_time.tv_sec = seconds;
313 sleep_time.tv_nsec = 0;
314 while (nanosleep(&sleep_time, &sleep_time) != 0 && errno == EINTR) {
315 }
316 #endif
317 }
318
319 #ifdef ABSL_HAVE_ALARM
320 // AbslFailureSignalHandler() installs this as a signal handler for
321 // SIGALRM, then sets an alarm to be delivered to the program after a
322 // set amount of time. If AbslFailureSignalHandler() hangs for more than
323 // the alarm timeout, ImmediateAbortSignalHandler() will abort the
324 // program.
ImmediateAbortSignalHandler(int)325 static void ImmediateAbortSignalHandler(int) { RaiseToDefaultHandler(SIGABRT); }
326 #endif
327
328 // absl::base_internal::GetTID() returns pid_t on most platforms, but
329 // returns absl::base_internal::pid_t on Windows.
330 using GetTidType = decltype(absl::base_internal::GetTID());
331 ABSL_CONST_INIT static std::atomic<GetTidType> failed_tid(0);
332
333 #ifndef ABSL_HAVE_SIGACTION
AbslFailureSignalHandler(int signo)334 static void AbslFailureSignalHandler(int signo) {
335 void* ucontext = nullptr;
336 #else
337 static void AbslFailureSignalHandler(int signo, siginfo_t*, void* ucontext) {
338 #endif
339
340 const GetTidType this_tid = absl::base_internal::GetTID();
341 GetTidType previous_failed_tid = 0;
342 if (!failed_tid.compare_exchange_strong(previous_failed_tid, this_tid,
343 std::memory_order_acq_rel,
344 std::memory_order_relaxed)) {
345 ABSL_RAW_LOG(
346 ERROR,
347 "Signal %d raised at PC=%p while already in AbslFailureSignalHandler()",
348 signo, absl::debugging_internal::GetProgramCounter(ucontext));
349 if (this_tid != previous_failed_tid) {
350 // Another thread is already in AbslFailureSignalHandler(), so wait
351 // a bit for it to finish. If the other thread doesn't kill us,
352 // we do so after sleeping.
353 PortableSleepForSeconds(3);
354 RaiseToDefaultHandler(signo);
355 // The recursively raised signal may be blocked until we return.
356 return;
357 }
358 }
359
360 // Increase the chance that the CPU we report was the same CPU on which the
361 // signal was received by doing this as early as possible, i.e. after
362 // verifying that this is not a recursive signal handler invocation.
363 int my_cpu = -1;
364 #ifdef ABSL_HAVE_SCHED_GETCPU
365 my_cpu = sched_getcpu();
366 #endif
367
368 #ifdef ABSL_HAVE_ALARM
369 // Set an alarm to abort the program in case this code hangs or deadlocks.
370 if (fsh_options.alarm_on_failure_secs > 0) {
371 alarm(0); // Cancel any existing alarms.
372 signal(SIGALRM, ImmediateAbortSignalHandler);
373 alarm(static_cast<unsigned int>(fsh_options.alarm_on_failure_secs));
374 }
375 #endif
376
377 // First write to stderr.
378 WriteFailureInfo(
379 signo, ucontext, my_cpu, +[](const char* data) {
380 absl::raw_log_internal::AsyncSignalSafeWriteError(data, strlen(data));
381 });
382
383 // Riskier code (because it is less likely to be async-signal-safe)
384 // goes after this point.
385 if (fsh_options.writerfn != nullptr) {
386 WriteFailureInfo(signo, ucontext, my_cpu, fsh_options.writerfn);
387 fsh_options.writerfn(nullptr);
388 }
389
390 if (fsh_options.call_previous_handler) {
391 RaiseToPreviousHandler(signo);
392 } else {
393 RaiseToDefaultHandler(signo);
394 }
395 }
396
397 void InstallFailureSignalHandler(const FailureSignalHandlerOptions& options) {
398 fsh_options = options;
399 for (auto& it : failure_signal_data) {
400 InstallOneFailureHandler(&it, AbslFailureSignalHandler);
401 }
402 }
403
404 ABSL_NAMESPACE_END
405 } // namespace absl
406