1 // Copyright 2019 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.
5 #ifndef BASE_IMMEDIATE_CRASH_H_
6 #define BASE_IMMEDIATE_CRASH_H_
8 #include "build/build_config.h"
10 // Crashes in the fastest possible way with no attempt at logging.
11 // There are several constraints; see http://crbug.com/664209 for more context.
13 // - TRAP_SEQUENCE_() must be fatal. It should not be possible to ignore the
14 // resulting exception or simply hit 'continue' to skip over it in a debugger.
15 // - Different instances of TRAP_SEQUENCE_() must not be folded together, to
16 // ensure crash reports are debuggable. Unlike __builtin_trap(), asm volatile
17 // blocks will not be folded together.
18 // Note: TRAP_SEQUENCE_() previously required an instruction with a unique
19 // nonce since unlike clang, GCC folds together identical asm volatile
21 // - TRAP_SEQUENCE_() must produce a signal that is distinct from an invalid
23 // - TRAP_SEQUENCE_() must be treated as a set of noreturn instructions.
24 // __builtin_unreachable() is used to provide that hint here. clang also uses
25 // this as a heuristic to pack the instructions in the function epilogue to
26 // improve code density.
27 // - base::ImmediateCrash() is used in allocation hooks. To prevent recursions,
28 // TRAP_SEQUENCE_() must not allocate.
30 // Additional properties that are nice to have:
31 // - TRAP_SEQUENCE_() should be as compact as possible.
32 // - The first instruction of TRAP_SEQUENCE_() should not change, to avoid
33 // shifting crash reporting clusters. As a consequence of this, explicit
34 // assembly is preferred over intrinsics.
35 // Note: this last bullet point may no longer be true, and may be removed in
38 // Note: TRAP_SEQUENCE Is currently split into two macro helpers due to the fact
39 // that clang emits an actual instruction for __builtin_unreachable() on certain
40 // platforms (see https://crbug.com/958675). In addition, the int3/bkpt/brk will
41 // be removed in followups, so splitting it up like this now makes it easy to
42 // land the followups.
44 #if defined(COMPILER_GCC)
46 #if BUILDFLAG(IS_NACL)
48 // Crash report accuracy is not guaranteed on NaCl.
49 #define TRAP_SEQUENCE1_() __builtin_trap()
50 #define TRAP_SEQUENCE2_() asm volatile("")
52 #elif defined(ARCH_CPU_X86_FAMILY)
54 // TODO(https://crbug.com/958675): In theory, it should be possible to use just
55 // int3. However, there are a number of crashes with SIGILL as the exception
56 // code, so it seems likely that there's a signal handler that allows execution
57 // to continue after SIGTRAP.
58 #define TRAP_SEQUENCE1_() asm volatile("int3")
60 #if BUILDFLAG(IS_APPLE)
61 // Intentionally empty: __builtin_unreachable() is always part of the sequence
62 // (see IMMEDIATE_CRASH below) and already emits a ud2 on Mac.
63 #define TRAP_SEQUENCE2_() asm volatile("")
65 #define TRAP_SEQUENCE2_() asm volatile("ud2")
66 #endif // BUILDFLAG(IS_APPLE)
68 #elif defined(ARCH_CPU_ARMEL)
70 // bkpt will generate a SIGBUS when running on armv7 and a SIGTRAP when running
71 // as a 32 bit userspace app on arm64. There doesn't seem to be any way to
72 // cause a SIGTRAP from userspace without using a syscall (which would be a
73 // problem for sandboxing).
74 // TODO(https://crbug.com/958675): Remove bkpt from this sequence.
75 #define TRAP_SEQUENCE1_() asm volatile("bkpt #0")
76 #define TRAP_SEQUENCE2_() asm volatile("udf #0")
78 #elif defined(ARCH_CPU_ARM64)
80 // This will always generate a SIGTRAP on arm64.
81 // TODO(https://crbug.com/958675): Remove brk from this sequence.
82 #define TRAP_SEQUENCE1_() asm volatile("brk #0")
83 #define TRAP_SEQUENCE2_() asm volatile("hlt #0")
87 // Crash report accuracy will not be guaranteed on other architectures, but at
88 // least this will crash as expected.
89 #define TRAP_SEQUENCE1_() __builtin_trap()
90 #define TRAP_SEQUENCE2_() asm volatile("")
94 #elif defined(COMPILER_MSVC)
96 #if !defined(__clang__)
98 // MSVC x64 doesn't support inline asm, so use the MSVC intrinsic.
99 #define TRAP_SEQUENCE1_() __debugbreak()
100 #define TRAP_SEQUENCE2_()
102 #elif defined(ARCH_CPU_ARM64)
104 // Windows ARM64 uses "BRK #F000" as its breakpoint instruction, and
105 // __debugbreak() generates that in both VC++ and clang.
106 #define TRAP_SEQUENCE1_() __debugbreak()
107 // Intentionally empty: __builtin_unreachable() is always part of the sequence
108 // (see IMMEDIATE_CRASH below) and already emits a ud2 on Win64,
109 // https://crbug.com/958373
110 #define TRAP_SEQUENCE2_() __asm volatile("")
114 #define TRAP_SEQUENCE1_() asm volatile("int3")
115 #define TRAP_SEQUENCE2_() asm volatile("ud2")
121 #error No supported trap sequence!
123 #endif // COMPILER_GCC
125 #define TRAP_SEQUENCE_() \
131 // This version of ALWAYS_INLINE inlines even in is_debug=true.
132 // TODO(pbos): See if NDEBUG can be dropped from ALWAYS_INLINE as well, and if
133 // so merge. Otherwise document why it cannot inline in debug in
134 // base/compiler_specific.h.
135 #if defined(COMPILER_GCC)
136 #define IMMEDIATE_CRASH_ALWAYS_INLINE inline __attribute__((__always_inline__))
137 #elif defined(COMPILER_MSVC)
138 #define IMMEDIATE_CRASH_ALWAYS_INLINE __forceinline
140 #define IMMEDIATE_CRASH_ALWAYS_INLINE inline
145 [[noreturn]] IMMEDIATE_CRASH_ALWAYS_INLINE void ImmediateCrash() {
147 #if defined(__clang__) || defined(COMPILER_GCC)
148 __builtin_unreachable();
149 #endif // defined(__clang__) || defined(COMPILER_GCC)
154 #endif // BASE_IMMEDIATE_CRASH_H_