| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
| 2 | // Copyright (C) 2024 Intel Corporation. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #include <QtTest/private/qtestcrashhandler_p.h> |
| 6 | |
| 7 | #include <QtCore/qbytearray.h> |
| 8 | #include <QtCore/qstring.h> |
| 9 | #include <QtCore/private/qcore_unix_p.h> |
| 10 | |
| 11 | #include <QtTest/qtestcase.h> |
| 12 | #include <QtTest/private/qtestlog_p.h> |
| 13 | |
| 14 | #if !defined(Q_OS_INTEGRITY) || __GHS_VERSION_NUMBER > 202014 |
| 15 | # include <charconv> |
| 16 | #else |
| 17 | // Broken implementation, causes link failures just by #include'ing! |
| 18 | # undef __cpp_lib_to_chars // in case <version> was included |
| 19 | #endif |
| 20 | #include <string_view> |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | #include <errno.h> |
| 26 | #if __has_include(<paths.h>) |
| 27 | # include <paths.h> |
| 28 | #endif |
| 29 | #include <signal.h> |
| 30 | #include <time.h> |
| 31 | #include <sys/mman.h> |
| 32 | #include <sys/uio.h> |
| 33 | #include <sys/wait.h> |
| 34 | #include <unistd.h> |
| 35 | # if !defined(Q_OS_INTEGRITY) |
| 36 | # include <sys/resource.h> |
| 37 | # endif |
| 38 | # if __has_include(<sys/ucontext.h>) |
| 39 | # include <sys/ucontext.h> |
| 40 | # elif __has_include(<ucontext.h>) |
| 41 | # include <ucontext.h> |
| 42 | # else |
| 43 | using ucontext_t = void; |
| 44 | # endif |
| 45 | |
| 46 | #if defined(Q_OS_MACOS) |
| 47 | #include <QtCore/private/qcore_mac_p.h> |
| 48 | #include <QtTest/private/qtestutil_macos_p.h> |
| 49 | |
| 50 | #include <IOKit/pwr_mgt/IOPMLib.h> |
| 51 | #include <mach/task.h> |
| 52 | #include <mach/mach_init.h> |
| 53 | #include <CoreFoundation/CFPreferences.h> |
| 54 | |
| 55 | #define CSR_ALLOW_UNRESTRICTED_FS (1 << 1) |
| 56 | #endif |
| 57 | |
| 58 | #if defined(Q_OS_LINUX) |
| 59 | #include <sys/prctl.h> |
| 60 | #include <sys/types.h> |
| 61 | #include <fcntl.h> |
| 62 | #endif |
| 63 | |
| 64 | #if defined(Q_OS_WASM) |
| 65 | #include <emscripten.h> |
| 66 | #endif |
| 67 | |
| 68 | #ifndef _PATH_DEFPATH |
| 69 | # define _PATH_DEFPATH "/usr/bin:/bin" |
| 70 | #endif |
| 71 | #ifndef SIGSTKSZ |
| 72 | # define SIGSTKSZ 0 /* we have code to set the minimum */ |
| 73 | #endif |
| 74 | #ifndef SA_RESETHAND |
| 75 | # define SA_RESETHAND 0 |
| 76 | #endif |
| 77 | |
| 78 | QT_BEGIN_NAMESPACE |
| 79 | |
| 80 | using namespace Qt::StringLiterals; |
| 81 | |
| 82 | # define OUR_SIGNALS(F) \ |
| 83 | F(HUP) \ |
| 84 | F(INT) \ |
| 85 | F(QUIT) \ |
| 86 | F(ABRT) \ |
| 87 | F(ILL) \ |
| 88 | F(BUS) \ |
| 89 | F(FPE) \ |
| 90 | F(SEGV) \ |
| 91 | F(PIPE) \ |
| 92 | F(TERM) \ |
| 93 | /**/ |
| 94 | # define CASE_LABEL(S) case SIG ## S: return QT_STRINGIFY(S); |
| 95 | # define ENUMERATE_SIGNALS(S) SIG ## S, |
| 96 | static const char *signalName(int signum) noexcept |
| 97 | { |
| 98 | switch (signum) { |
| 99 | OUR_SIGNALS(CASE_LABEL) |
| 100 | } |
| 101 | |
| 102 | # if defined(__GLIBC_MINOR__) && (__GLIBC_MINOR__ >= 32 || __GLIBC__ > 2) |
| 103 | // get the other signal names from glibc 2.32 |
| 104 | // (accessing the sys_sigabbrev variable causes linker warnings) |
| 105 | if (const char *p = sigabbrev_np(sig: signum)) |
| 106 | return p; |
| 107 | # endif |
| 108 | return "???" ; |
| 109 | } |
| 110 | static constexpr std::array fatalSignals = { |
| 111 | OUR_SIGNALS(ENUMERATE_SIGNALS) |
| 112 | }; |
| 113 | # undef CASE_LABEL |
| 114 | # undef ENUMERATE_SIGNALS |
| 115 | |
| 116 | static constexpr std::array crashingSignals = { |
| 117 | // Crash signals are special, because if we return from the handler |
| 118 | // without adjusting the machine state, the same instruction that |
| 119 | // originally caused the crash will get re-executed and will thus cause |
| 120 | // the same crash again. This is useful if our parent process logs the |
| 121 | // exit result or if core dumps are enabled: the core file will point |
| 122 | // to the actual instruction that crashed. |
| 123 | SIGILL, SIGBUS, SIGFPE, SIGSEGV |
| 124 | }; |
| 125 | using OldActionsArray = std::array<struct sigaction, fatalSignals.size()>; |
| 126 | |
| 127 | template <typename... Args> static ssize_t writeToStderr(Args &&... args) |
| 128 | { |
| 129 | auto makeIovec = [](std::string_view arg) { |
| 130 | struct iovec r = {}; |
| 131 | r.iov_base = const_cast<char *>(arg.data()); |
| 132 | r.iov_len = arg.size(); |
| 133 | return r; |
| 134 | }; |
| 135 | struct iovec vec[] = { makeIovec(std::forward<Args>(args))... }; |
| 136 | return ::writev(STDERR_FILENO, iovec: vec, count: std::size(vec)); |
| 137 | } |
| 138 | |
| 139 | namespace { |
| 140 | // async-signal-safe conversion from int to string |
| 141 | struct AsyncSafeIntBuffer |
| 142 | { |
| 143 | // digits10 + 1 for all possible digits |
| 144 | // +1 for the sign |
| 145 | // +1 for the terminating null |
| 146 | static constexpr int Digits10 = std::numeric_limits<int>::digits10 + 3; |
| 147 | std::array<char, Digits10> array; |
| 148 | constexpr AsyncSafeIntBuffer() : array{} {} // initializes array |
| 149 | AsyncSafeIntBuffer(Qt::Initialization) {} // leaves array uninitialized |
| 150 | }; |
| 151 | |
| 152 | std::string_view asyncSafeToString(int n, AsyncSafeIntBuffer &&result = Qt::Uninitialized) |
| 153 | { |
| 154 | char *ptr = result.array.data(); |
| 155 | if (false) { |
| 156 | #ifdef __cpp_lib_to_chars |
| 157 | } else if (auto r = std::to_chars(first: ptr, last: ptr + result.array.size(), value: n, base: 10); r.ec == std::errc{}) { |
| 158 | ptr = r.ptr; |
| 159 | #endif |
| 160 | } else { |
| 161 | // handle the sign |
| 162 | if (n < 0) { |
| 163 | *ptr++ = '-'; |
| 164 | n = -n; |
| 165 | } |
| 166 | |
| 167 | // find the highest power of the base that is less than this number |
| 168 | static constexpr int StartingDivider = ([]() { |
| 169 | int divider = 1; |
| 170 | for (int i = 0; i < std::numeric_limits<int>::digits10; ++i) |
| 171 | divider *= 10; |
| 172 | return divider; |
| 173 | }()); |
| 174 | int divider = StartingDivider; |
| 175 | while (divider && n < divider) |
| 176 | divider /= 10; |
| 177 | |
| 178 | // now convert to string |
| 179 | while (divider > 1) { |
| 180 | int quot = n / divider; |
| 181 | n = n % divider; |
| 182 | divider /= 10; |
| 183 | *ptr++ = quot + '0'; |
| 184 | } |
| 185 | *ptr++ = n + '0'; |
| 186 | } |
| 187 | |
| 188 | #ifndef QT_NO_DEBUG |
| 189 | // this isn't necessary, it just helps in the debugger |
| 190 | *ptr = '\0'; |
| 191 | #endif |
| 192 | return std::string_view(result.array.data(), ptr - result.array.data()); |
| 193 | }; |
| 194 | |
| 195 | std::string_view asyncSafeToHexString(quintptr u, char *ptr) |
| 196 | { |
| 197 | // We format with leading zeroes so the output is of fixed length. |
| 198 | // Formatting to shorter is more complex and unnecessary here (unlike |
| 199 | // decimals above). |
| 200 | int shift = sizeof(quintptr) * 8 - 4; |
| 201 | ptr[0] = '0'; |
| 202 | ptr[1] = 'x'; |
| 203 | for (size_t i = 0; i < sizeof(quintptr) * 2; ++i, shift -= 4) |
| 204 | ptr[i + 2] = QtMiscUtils::toHexLower(value: u >> shift); |
| 205 | |
| 206 | return std::string_view(ptr, sizeof(quintptr) * 2 + 2); |
| 207 | } |
| 208 | } // unnamed namespace |
| 209 | |
| 210 | namespace QTest { |
| 211 | namespace CrashHandler { |
| 212 | Q_CONSTINIT static OldActionsArray oldActions {}; |
| 213 | static bool pauseOnCrash = false; |
| 214 | |
| 215 | static void actionHandler(int signum, siginfo_t *info, void * /* ucontext */); |
| 216 | |
| 217 | bool alreadyDebugging() |
| 218 | { |
| 219 | #if defined(Q_OS_LINUX) |
| 220 | int fd = open(file: "/proc/self/status" , O_RDONLY); |
| 221 | if (fd == -1) |
| 222 | return false; |
| 223 | char buffer[2048]; |
| 224 | ssize_t size = read(fd: fd, buf: buffer, nbytes: sizeof(buffer) - 1); |
| 225 | if (size == -1) { |
| 226 | close(fd: fd); |
| 227 | return false; |
| 228 | } |
| 229 | buffer[size] = 0; |
| 230 | const char tracerPidToken[] = "\nTracerPid:" ; |
| 231 | char *tracerPid = strstr(haystack: buffer, needle: tracerPidToken); |
| 232 | if (!tracerPid) { |
| 233 | close(fd: fd); |
| 234 | return false; |
| 235 | } |
| 236 | tracerPid += sizeof(tracerPidToken); |
| 237 | long int pid = strtol(nptr: tracerPid, endptr: &tracerPid, base: 10); |
| 238 | close(fd: fd); |
| 239 | return pid != 0; |
| 240 | #elif defined(Q_OS_MACOS) |
| 241 | // Check if there is an exception handler for the process: |
| 242 | mach_msg_type_number_t portCount = 0; |
| 243 | exception_mask_t masks[EXC_TYPES_COUNT]; |
| 244 | mach_port_t ports[EXC_TYPES_COUNT]; |
| 245 | exception_behavior_t behaviors[EXC_TYPES_COUNT]; |
| 246 | thread_state_flavor_t flavors[EXC_TYPES_COUNT]; |
| 247 | exception_mask_t mask = EXC_MASK_ALL & ~(EXC_MASK_RESOURCE | EXC_MASK_GUARD); |
| 248 | kern_return_t result = task_get_exception_ports(mach_task_self(), mask, masks, &portCount, |
| 249 | ports, behaviors, flavors); |
| 250 | if (result == KERN_SUCCESS) { |
| 251 | for (mach_msg_type_number_t portIndex = 0; portIndex < portCount; ++portIndex) { |
| 252 | if (MACH_PORT_VALID(ports[portIndex])) { |
| 253 | return true; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | return false; |
| 258 | #else |
| 259 | // TODO |
| 260 | return false; |
| 261 | #endif |
| 262 | } |
| 263 | |
| 264 | static bool hasSystemCrashReporter() |
| 265 | { |
| 266 | #if defined(Q_OS_MACOS) |
| 267 | return QTestPrivate::macCrashReporterWillShowDialog(); |
| 268 | #else |
| 269 | return false; |
| 270 | #endif |
| 271 | } |
| 272 | |
| 273 | void maybeDisableCoreDump() |
| 274 | { |
| 275 | #ifdef RLIMIT_CORE |
| 276 | bool ok = false; |
| 277 | const int disableCoreDump = qEnvironmentVariableIntValue(varName: "QTEST_DISABLE_CORE_DUMP" , ok: &ok); |
| 278 | if (ok && disableCoreDump) { |
| 279 | struct rlimit limit; |
| 280 | limit.rlim_cur = 0; |
| 281 | limit.rlim_max = 0; |
| 282 | if (setrlimit(RLIMIT_CORE, rlimits: &limit) != 0) |
| 283 | qWarning(msg: "Failed to disable core dumps: %d" , errno); |
| 284 | } |
| 285 | #endif |
| 286 | } |
| 287 | |
| 288 | static DebuggerProgram debugger = None; |
| 289 | void prepareStackTrace() |
| 290 | { |
| 291 | |
| 292 | bool ok = false; |
| 293 | const int disableStackDump = qEnvironmentVariableIntValue(varName: "QTEST_DISABLE_STACK_DUMP" , ok: &ok); |
| 294 | if (ok && disableStackDump) |
| 295 | return; |
| 296 | |
| 297 | if (hasSystemCrashReporter()) |
| 298 | return; |
| 299 | |
| 300 | #if defined(Q_OS_MACOS) |
| 301 | // Try to handle https://github.com/llvm/llvm-project/issues/53254, |
| 302 | // where LLDB will hang and fail to provide a valid stack trace. |
| 303 | # if defined(Q_PROCESSOR_ARM) |
| 304 | return; |
| 305 | # else |
| 306 | std::optional<uint32_t> sipConfiguration = qt_mac_sipConfiguration(); |
| 307 | if (!sipConfiguration || !(*sipConfiguration & CSR_ALLOW_UNRESTRICTED_FS)) |
| 308 | return; |
| 309 | # endif |
| 310 | #endif |
| 311 | |
| 312 | // like QStandardPaths::findExecutable(), but simpler |
| 313 | auto hasExecutable = [](const char *execname) { |
| 314 | std::string candidate; |
| 315 | std::string path; |
| 316 | if (const char *p = getenv(name: "PATH" ); p && *p) |
| 317 | path = p; |
| 318 | else |
| 319 | path = _PATH_DEFPATH; |
| 320 | for (const char *p = std::strtok(s: &path[0], delim: ":'" ); p; p = std::strtok(s: nullptr, delim: ":" )) { |
| 321 | candidate = p; |
| 322 | candidate += '/'; |
| 323 | candidate += execname; |
| 324 | if (QT_ACCESS(name: candidate.data(), X_OK) == 0) |
| 325 | return true; |
| 326 | } |
| 327 | return false; |
| 328 | }; |
| 329 | |
| 330 | static constexpr DebuggerProgram debuggerSearchOrder[] = { |
| 331 | # if defined(Q_OS_QNX) || (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)) |
| 332 | Gdb, Lldb |
| 333 | # else |
| 334 | Lldb, Gdb |
| 335 | # endif |
| 336 | }; |
| 337 | for (DebuggerProgram candidate : debuggerSearchOrder) { |
| 338 | switch (candidate) { |
| 339 | case None: |
| 340 | Q_UNREACHABLE(); |
| 341 | break; |
| 342 | case Gdb: |
| 343 | if (hasExecutable("gdb" )) { |
| 344 | debugger = Gdb; |
| 345 | return; |
| 346 | } |
| 347 | break; |
| 348 | case Lldb: |
| 349 | if (hasExecutable("lldb" )) { |
| 350 | debugger = Lldb; |
| 351 | return; |
| 352 | } |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void printTestRunTime() |
| 359 | { |
| 360 | const int msecsFunctionTime = qRound(d: QTestLog::msecsFunctionTime()); |
| 361 | const int msecsTotalTime = qRound(d: QTestLog::msecsTotalTime()); |
| 362 | const char *const name = QTest::currentTestFunction(); |
| 363 | writeToStderr(args: "\n " , args: name ? name : "[Non-test]" , |
| 364 | args: " function time: " , args: asyncSafeToString(n: msecsFunctionTime), |
| 365 | args: "ms, total time: " , args: asyncSafeToString(n: msecsTotalTime), args: "ms\n" ); |
| 366 | } |
| 367 | |
| 368 | static quintptr getProgramCounter(void *ucontext) |
| 369 | { |
| 370 | quintptr pc = 0; |
| 371 | if ([[maybe_unused]] auto ctx = static_cast<ucontext_t *>(ucontext)) { |
| 372 | #if 0 // keep the list below alphabetical |
| 373 | |
| 374 | #elif defined(Q_OS_DARWIN) && defined(Q_PROCESSOR_ARM_64) |
| 375 | pc = ctx->uc_mcontext->__ss.__pc; |
| 376 | #elif defined(Q_OS_DARWIN) && defined(Q_PROCESSOR_X86_64) |
| 377 | pc = ctx->uc_mcontext->__ss.__rip; |
| 378 | |
| 379 | #elif defined(Q_OS_FREEBSD) && defined(Q_PROCESSOR_X86_32) |
| 380 | pc = ctx->uc_mcontext.mc_eip; |
| 381 | #elif defined(Q_OS_FREEBSD) && defined(Q_PROCESSOR_X86_64) |
| 382 | pc = ctx->uc_mcontext.mc_rip; |
| 383 | |
| 384 | #elif defined(Q_OS_LINUX) && defined(Q_PROCESSOR_ARM_32) |
| 385 | // pc = ctx->uc_mcontext.arm_pc; // untested |
| 386 | #elif defined(Q_OS_LINUX) && defined(Q_PROCESSOR_ARM_64) |
| 387 | pc = ctx->uc_mcontext.pc; |
| 388 | #elif defined(Q_OS_LINUX) && defined(Q_PROCESSOR_MIPS) |
| 389 | // pc = ctx->uc_mcontext.pc; // untested |
| 390 | #elif defined(Q_OS_LINUX) && defined(Q_PROCESSOR_LOONGARCH) |
| 391 | // pc = ctx->uc_mcontext.__pc; // untested |
| 392 | #elif defined(Q_OS_LINUX) && defined(Q_PROCESSOR_POWER_32) |
| 393 | // pc = ctx->uc_mcontext.uc_regs->gregs[PT_NIP]; // untested |
| 394 | #elif defined(Q_OS_LINUX) && defined(Q_PROCESSOR_POWER_64) |
| 395 | // pc = ctx->uc_mcontext.gregs[PT_NIP]; // untested |
| 396 | #elif defined(Q_OS_LINUX) && defined(Q_PROCESSOR_RISCV) |
| 397 | // pc = ctx->uc_mcontext.__gregs[REG_PC]; // untested |
| 398 | #elif defined(Q_OS_LINUX) && defined(Q_PROCESSOR_X86_32) |
| 399 | pc = ctx->uc_mcontext.gregs[REG_EIP]; |
| 400 | #elif defined(Q_OS_LINUX) && defined(Q_PROCESSOR_X86_64) |
| 401 | pc = ctx->uc_mcontext.gregs[REG_RIP]; |
| 402 | #endif |
| 403 | } |
| 404 | return pc; |
| 405 | } |
| 406 | |
| 407 | void generateStackTrace(quintptr ip) |
| 408 | { |
| 409 | if (debugger == None || alreadyDebugging()) |
| 410 | return; |
| 411 | |
| 412 | # if defined(Q_OS_LINUX) && defined(PR_SET_PTRACER) |
| 413 | // allow ourselves to be debugged |
| 414 | (void) prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY); |
| 415 | # endif |
| 416 | |
| 417 | # if !defined(Q_OS_INTEGRITY) && !defined(Q_OS_VXWORKS) |
| 418 | writeToStderr(args: "\n=== Stack trace ===\n" ); |
| 419 | |
| 420 | // execlp() requires null-termination, so call the default constructor |
| 421 | AsyncSafeIntBuffer pidbuffer; |
| 422 | asyncSafeToString(n: getpid(), result: std::move(pidbuffer)); |
| 423 | |
| 424 | // Note: POSIX.1-2001 still has fork() in the list of async-safe functions, |
| 425 | // but in a future edition, it might be removed. It would be safer to wake |
| 426 | // up a babysitter thread to launch the debugger. |
| 427 | pid_t pid = fork(); |
| 428 | if (pid == 0) { |
| 429 | // child process |
| 430 | (void) dup2(STDERR_FILENO, STDOUT_FILENO); // redirect stdout to stderr |
| 431 | |
| 432 | // disassemble the crashing instruction, if known |
| 433 | // (syntax is the same for gdb and lldb) |
| 434 | char disasmInstr[sizeof("x/i 0x" ) + sizeof(ip) * 2] = {}; // zero-init for terminator |
| 435 | if (ip) { |
| 436 | strcpy(dest: disasmInstr, src: "x/i " ); |
| 437 | asyncSafeToHexString(u: ip, ptr: disasmInstr + strlen(s: disasmInstr)); |
| 438 | } |
| 439 | |
| 440 | struct Args { |
| 441 | std::array<const char *, 16> argv; |
| 442 | int count = 0; |
| 443 | Args &operator<<(const char *arg) |
| 444 | { |
| 445 | Q_ASSERT(count < int(argv.size())); |
| 446 | argv[count++] = arg; |
| 447 | return *this; |
| 448 | } |
| 449 | operator char **() const { return const_cast<char **>(argv.data()); } |
| 450 | } argv; |
| 451 | |
| 452 | switch (debugger) { |
| 453 | case None: |
| 454 | Q_UNREACHABLE(); |
| 455 | break; |
| 456 | case Gdb: |
| 457 | argv << "gdb" << "--nx" << "--batch" ; |
| 458 | if (ip) |
| 459 | argv << "-ex" << disasmInstr; |
| 460 | argv << "-ex" << "thread apply all bt" |
| 461 | << "-ex" << "printf \"\\n\"" |
| 462 | << "-ex" << "info proc mappings" |
| 463 | << "--pid" ; |
| 464 | break; |
| 465 | case Lldb: |
| 466 | argv << "lldb" << "--no-lldbinit" << "--batch" ; |
| 467 | if (ip) |
| 468 | argv << "-o" << disasmInstr; |
| 469 | argv << "-o" << "bt all" |
| 470 | << "--attach-pid" ; |
| 471 | break; |
| 472 | } |
| 473 | if (argv.count) { |
| 474 | argv << pidbuffer.array.data() << nullptr; |
| 475 | execvp(file: argv.argv[0], argv: argv); |
| 476 | } |
| 477 | _exit(status: 1); |
| 478 | } else if (pid < 0) { |
| 479 | writeToStderr(args: "Failed to start debugger.\n" ); |
| 480 | } else { |
| 481 | int ret; |
| 482 | QT_EINTR_LOOP(ret, waitpid(pid, nullptr, 0)); |
| 483 | } |
| 484 | |
| 485 | writeToStderr(args: "=== End of stack trace ===\n" ); |
| 486 | # else |
| 487 | Q_UNUSED(ip); |
| 488 | # endif // !Q_OS_INTEGRITY && !Q_OS_VXWORKS |
| 489 | } |
| 490 | |
| 491 | #ifndef Q_OS_WASM // no signal handling for WASM |
| 492 | void blockUnixSignals() |
| 493 | { |
| 494 | // Block most Unix signals so the WatchDog thread won't be called when |
| 495 | // external signals are delivered, thus avoiding interfering with the test |
| 496 | sigset_t set; |
| 497 | sigfillset(set: &set); |
| 498 | |
| 499 | // we allow the crashing signals, in case we have bugs |
| 500 | for (int signo : fatalSignals) |
| 501 | sigdelset(set: &set, signo: signo); |
| 502 | |
| 503 | pthread_sigmask(SIG_BLOCK, newmask: &set, oldmask: nullptr); |
| 504 | } |
| 505 | |
| 506 | template <typename T> static |
| 507 | std::enable_if_t<sizeof(std::declval<T>().si_pid) + sizeof(std::declval<T>().si_uid) >= 1> |
| 508 | printSentSignalInfo(T *info) |
| 509 | { |
| 510 | writeToStderr(" sent by PID " , asyncSafeToString(info->si_pid), |
| 511 | " UID " , asyncSafeToString(info->si_uid)); |
| 512 | } |
| 513 | [[maybe_unused]] static void printSentSignalInfo(...) {} |
| 514 | |
| 515 | template <typename T> static std::enable_if_t<sizeof(std::declval<T>().si_addr) >= 1> |
| 516 | printCrashingSignalInfo(T *info, quintptr pc) |
| 517 | { |
| 518 | using HexString = std::array<char, sizeof(quintptr) * 2 + 2>; |
| 519 | auto toHexString = [](quintptr u, HexString &&r = {}) { |
| 520 | return asyncSafeToHexString(u, ptr: r.data()); |
| 521 | }; |
| 522 | writeToStderr(", code " , asyncSafeToString(info->si_code)); |
| 523 | if (pc) |
| 524 | writeToStderr(", at instruction address " , toHexString(pc)); |
| 525 | writeToStderr(", accessing address " , toHexString(quintptr(info->si_addr))); |
| 526 | } |
| 527 | [[maybe_unused]] static void printCrashingSignalInfo(...) {} |
| 528 | |
| 529 | [[maybe_unused]] static void regularHandler(int signum) |
| 530 | { |
| 531 | actionHandler(signum, info: nullptr, nullptr); |
| 532 | } |
| 533 | |
| 534 | FatalSignalHandler::FatalSignalHandler() |
| 535 | { |
| 536 | pauseOnCrash = qEnvironmentVariableIsSet(varName: "QTEST_PAUSE_ON_CRASH" ); |
| 537 | struct sigaction act; |
| 538 | memset(s: &act, c: 0, n: sizeof(act)); |
| 539 | act.sa_handler = SIG_DFL; |
| 540 | oldActions.fill(u: act); |
| 541 | |
| 542 | // Remove the handler after it is invoked. |
| 543 | act.sa_flags = SA_RESETHAND | setupAlternateStack(); |
| 544 | |
| 545 | # ifdef SA_SIGINFO |
| 546 | act.sa_flags |= SA_SIGINFO; |
| 547 | act.sa_sigaction = actionHandler; |
| 548 | # else |
| 549 | act.sa_handler = regularHandler; |
| 550 | # endif |
| 551 | |
| 552 | // Block all fatal signals in our signal handler so we don't try to close |
| 553 | // the testlog twice. |
| 554 | sigemptyset(set: &act.sa_mask); |
| 555 | for (int signal : fatalSignals) |
| 556 | sigaddset(set: &act.sa_mask, signo: signal); |
| 557 | |
| 558 | for (size_t i = 0; i < fatalSignals.size(); ++i) |
| 559 | sigaction(sig: fatalSignals[i], act: &act, oact: &oldActions[i]); |
| 560 | } |
| 561 | |
| 562 | FatalSignalHandler::~FatalSignalHandler() |
| 563 | { |
| 564 | // Restore the default signal handlers in place of ours. |
| 565 | // If ours has been replaced, leave the replacement alone. |
| 566 | auto isOurs = [](const struct sigaction &old) { |
| 567 | # ifdef SA_SIGINFO |
| 568 | return (old.sa_flags & SA_SIGINFO) && old.sa_sigaction == actionHandler; |
| 569 | # else |
| 570 | return old.sa_handler == regularHandler; |
| 571 | # endif |
| 572 | }; |
| 573 | struct sigaction action; |
| 574 | |
| 575 | for (size_t i = 0; i < fatalSignals.size(); ++i) { |
| 576 | struct sigaction &act = oldActions[i]; |
| 577 | if (sigaction(sig: fatalSignals[i], act: nullptr, oact: &action)) |
| 578 | continue; // Failed to query present handler |
| 579 | if (action.sa_flags == 0 && action.sa_handler == SIG_DFL) |
| 580 | continue; // Already the default |
| 581 | if (isOurs(action)) |
| 582 | sigaction(sig: fatalSignals[i], act: &act, oact: nullptr); |
| 583 | } |
| 584 | |
| 585 | freeAlternateStack(); |
| 586 | } |
| 587 | |
| 588 | static auto alternateStackSize() noexcept |
| 589 | { |
| 590 | struct R { size_t size, pageSize; }; |
| 591 | static constexpr size_t MinStackSize = 32 * 1024; |
| 592 | size_t pageSize = sysconf(_SC_PAGESIZE); |
| 593 | size_t size = SIGSTKSZ; |
| 594 | if (size < MinStackSize) { |
| 595 | size = MinStackSize; |
| 596 | } else { |
| 597 | // round up to a page |
| 598 | size = (size + pageSize - 1) & -pageSize; |
| 599 | } |
| 600 | |
| 601 | return R{ .size: size + pageSize, .pageSize: pageSize }; |
| 602 | } |
| 603 | |
| 604 | int FatalSignalHandler::setupAlternateStack() |
| 605 | { |
| 606 | // tvOS/watchOS both define SA_ONSTACK (in sys/signal.h) but mark sigaltstack() as |
| 607 | // unavailable (__WATCHOS_PROHIBITED __TVOS_PROHIBITED in signal.h) |
| 608 | # if defined(SA_ONSTACK) && !defined(Q_OS_TVOS) && !defined(Q_OS_WATCHOS) |
| 609 | // Let the signal handlers use an alternate stack |
| 610 | // This is necessary if SIGSEGV is to catch a stack overflow |
| 611 | auto r = alternateStackSize(); |
| 612 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 613 | # ifdef MAP_STACK |
| 614 | flags |= MAP_STACK; |
| 615 | # endif |
| 616 | alternateStackBase = mmap(addr: nullptr, len: r.size, PROT_READ | PROT_WRITE, flags: flags, fd: -1, offset: 0); |
| 617 | if (alternateStackBase == MAP_FAILED) |
| 618 | return 0; |
| 619 | |
| 620 | // mark the bottom page inaccessible, to catch a handler stack overflow |
| 621 | (void) mprotect(addr: alternateStackBase, len: r.pageSize, PROT_NONE); |
| 622 | |
| 623 | stack_t stack; |
| 624 | stack.ss_flags = 0; |
| 625 | stack.ss_size = r.size - r.pageSize; |
| 626 | stack.ss_sp = static_cast<char *>(alternateStackBase) + r.pageSize; |
| 627 | sigaltstack(ss: &stack, oss: nullptr); |
| 628 | return SA_ONSTACK; |
| 629 | # else |
| 630 | return 0; |
| 631 | # endif |
| 632 | } |
| 633 | |
| 634 | void FatalSignalHandler::freeAlternateStack() |
| 635 | { |
| 636 | # if defined(SA_ONSTACK) && !defined(Q_OS_TVOS) && !defined(Q_OS_WATCHOS) |
| 637 | if (alternateStackBase != MAP_FAILED) { |
| 638 | stack_t stack = {}; |
| 639 | stack.ss_flags = SS_DISABLE; |
| 640 | sigaltstack(ss: &stack, oss: nullptr); |
| 641 | munmap(addr: alternateStackBase, len: alternateStackSize().size); |
| 642 | } |
| 643 | # endif |
| 644 | } |
| 645 | |
| 646 | void actionHandler(int signum, siginfo_t *info, void *ucontext) |
| 647 | { |
| 648 | writeToStderr(args: "Received signal " , args: asyncSafeToString(n: signum), |
| 649 | args: " (SIG" , args: signalName(signum), args: ")" ); |
| 650 | |
| 651 | quintptr pc = 0; |
| 652 | bool isCrashingSignal = |
| 653 | std::find(first: crashingSignals.begin(), last: crashingSignals.end(), val: signum) != crashingSignals.end(); |
| 654 | if (isCrashingSignal && (!info || info->si_code <= 0)) |
| 655 | isCrashingSignal = false; // wasn't sent by the kernel, so it's not really a crash |
| 656 | if (isCrashingSignal) |
| 657 | printCrashingSignalInfo(info, pc: (pc = getProgramCounter(ucontext))); |
| 658 | else if (info && (info->si_code == SI_USER || info->si_code == SI_QUEUE)) |
| 659 | printSentSignalInfo(info); |
| 660 | |
| 661 | printTestRunTime(); |
| 662 | if (signum != SIGINT) { |
| 663 | generateStackTrace(ip: pc); |
| 664 | if (pauseOnCrash) { |
| 665 | writeToStderr(args: "Pausing process " , args: asyncSafeToString(n: getpid()), |
| 666 | args: " for debugging\n" ); |
| 667 | raise(SIGSTOP); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // chain back to the previous handler, if any |
| 672 | for (size_t i = 0; i < fatalSignals.size(); ++i) { |
| 673 | struct sigaction &act = oldActions[i]; |
| 674 | if (signum != fatalSignals[i]) |
| 675 | continue; |
| 676 | |
| 677 | // restore the handler (if SA_RESETHAND hasn't done the job for us) |
| 678 | if (SA_RESETHAND == 0 || act.sa_handler != SIG_DFL || act.sa_flags) |
| 679 | (void) sigaction(sig: signum, act: &act, oact: nullptr); |
| 680 | |
| 681 | if (!isCrashingSignal) |
| 682 | raise(sig: signum); |
| 683 | |
| 684 | // signal is blocked, so it'll be delivered when we return |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | // we shouldn't reach here! |
| 689 | std::abort(); |
| 690 | } |
| 691 | #endif // !defined(Q_OS_WASM) |
| 692 | |
| 693 | } // namespace CrashHandler |
| 694 | } // namespace QTest |
| 695 | |
| 696 | QT_END_NAMESPACE |
| 697 | |