xref: /aosp_15_r20/external/cronet/base/mac/os_crash_dumps.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #include "base/mac/os_crash_dumps.h"
6 
7 #include <signal.h>
8 #include <stddef.h>
9 #include <unistd.h>
10 
11 #include "base/logging.h"
12 
13 namespace base::mac {
14 
15 namespace {
16 
ExitSignalHandler(int sig)17 void ExitSignalHandler(int sig) {
18   // A call to exit() can call atexit() handlers.  If we SIGSEGV due
19   // to a corrupt heap, and if we have an atexit handler that
20   // allocates or frees memory, we are in trouble if we do not _exit.
21   _exit(128 + sig);
22 }
23 
24 }  // namespace
25 
DisableOSCrashDumps()26 void DisableOSCrashDumps() {
27   // These are the POSIX signals corresponding to the Mach exceptions that
28   // Apple Crash Reporter handles.  See ux_exception() in xnu's
29   // bsd/uxkern/ux_exception.c and machine_exception() in xnu's
30   // bsd/dev/*/unix_signal.c.
31   const int signals_to_intercept[] = {
32     // Hardware faults
33     SIGILL,   // EXC_BAD_INSTRUCTION
34     SIGTRAP,  // EXC_BREAKPOINT
35     SIGFPE,   // EXC_ARITHMETIC
36     SIGBUS,   // EXC_BAD_ACCESS
37     SIGSEGV,  // EXC_BAD_ACCESS
38     // Not a hardware fault
39     SIGABRT
40   };
41 
42   // For all these signals, just wire things up so we exit immediately.
43   for (size_t i = 0; i < std::size(signals_to_intercept); ++i) {
44     struct sigaction act = {};
45     act.sa_handler = ExitSignalHandler;
46 
47     // It is better to allow the signal handler to run on the stack
48     // registered with sigaltstack(), if one is present.
49     act.sa_flags = SA_ONSTACK;
50 
51     if (sigemptyset(&act.sa_mask) != 0)
52       DPLOG(FATAL) << "sigemptyset() failed";
53     if (sigaction(signals_to_intercept[i], &act, NULL) != 0)
54       DPLOG(FATAL) << "sigaction() failed";
55   }
56 }
57 
58 }  // namespace base::mac
59