1 // Copyright 2010 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "base/mac/os_crash_dumps.h"
11
12 #include <signal.h>
13 #include <stddef.h>
14 #include <unistd.h>
15
16 #include "base/logging.h"
17
18 namespace base::mac {
19
20 namespace {
21
ExitSignalHandler(int sig)22 void ExitSignalHandler(int sig) {
23 // A call to exit() can call atexit() handlers. If we SIGSEGV due
24 // to a corrupt heap, and if we have an atexit handler that
25 // allocates or frees memory, we are in trouble if we do not _exit.
26 _exit(128 + sig);
27 }
28
29 } // namespace
30
DisableOSCrashDumps()31 void DisableOSCrashDumps() {
32 // These are the POSIX signals corresponding to the Mach exceptions that
33 // Apple Crash Reporter handles. See ux_exception() in xnu's
34 // bsd/uxkern/ux_exception.c and machine_exception() in xnu's
35 // bsd/dev/*/unix_signal.c.
36 const int signals_to_intercept[] = {
37 // Hardware faults
38 SIGILL, // EXC_BAD_INSTRUCTION
39 SIGTRAP, // EXC_BREAKPOINT
40 SIGFPE, // EXC_ARITHMETIC
41 SIGBUS, // EXC_BAD_ACCESS
42 SIGSEGV, // EXC_BAD_ACCESS
43 // Not a hardware fault
44 SIGABRT
45 };
46
47 // For all these signals, just wire things up so we exit immediately.
48 for (size_t i = 0; i < std::size(signals_to_intercept); ++i) {
49 struct sigaction act = {};
50 act.sa_handler = ExitSignalHandler;
51
52 // It is better to allow the signal handler to run on the stack
53 // registered with sigaltstack(), if one is present.
54 act.sa_flags = SA_ONSTACK;
55
56 if (sigemptyset(&act.sa_mask) != 0)
57 DPLOG(FATAL) << "sigemptyset() failed";
58 if (sigaction(signals_to_intercept[i], &act, NULL) != 0)
59 DPLOG(FATAL) << "sigaction() failed";
60 }
61 }
62
63 } // namespace base::mac
64