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