Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_IMMEDIATE_CRASH_H_ |
| 6 | #define BASE_IMMEDIATE_CRASH_H_ |
| 7 | |
| 8 | #include "build/build_config.h" |
| 9 | |
| 10 | // Crashes in the fastest possible way with no attempt at logging. |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 11 | // There are several constraints; see http://crbug.com/664209 for more context. |
| 12 | // |
| 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 |
| 20 | // blocks. |
| 21 | // - TRAP_SEQUENCE_() must produce a signal that is distinct from an invalid |
| 22 | // memory access. |
| 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 | // |
| 28 | // Additional properties that are nice to have: |
| 29 | // - TRAP_SEQUENCE_() should be as compact as possible. |
| 30 | // - The first instruction of TRAP_SEQUENCE_() should not change, to avoid |
| 31 | // shifting crash reporting clusters. As a consequence of this, explicit |
| 32 | // assembly is preferred over intrinsics. |
| 33 | // Note: this last bullet point may no longer be true, and may be removed in |
| 34 | // the future. |
| 35 | |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 36 | // Note: TRAP_SEQUENCE Is currently split into two macro helpers due to the fact |
| 37 | // that clang emits an actual instruction for __builtin_unreachable() on certain |
| 38 | // platforms (see https://crbug.com/958675). In addition, the int3/bkpt/brk will |
| 39 | // be removed in followups, so splitting it up like this now makes it easy to |
| 40 | // land the followups. |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 41 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 42 | #if defined(COMPILER_GCC) |
| 43 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 44 | #if BUILDFLAG(IS_NACL) |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 45 | |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 46 | // Crash report accuracy is not guaranteed on NaCl. |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 47 | #define TRAP_SEQUENCE1_() __builtin_trap() |
| 48 | #define TRAP_SEQUENCE2_() asm volatile("") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 49 | |
| 50 | #elif defined(ARCH_CPU_X86_FAMILY) |
| 51 | |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 52 | // TODO(https://crbug.com/958675): In theory, it should be possible to use just |
| 53 | // int3. However, there are a number of crashes with SIGILL as the exception |
| 54 | // code, so it seems likely that there's a signal handler that allows execution |
| 55 | // to continue after SIGTRAP. |
| 56 | #define TRAP_SEQUENCE1_() asm volatile("int3") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 57 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 58 | #if BUILDFLAG(IS_APPLE) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 59 | // Intentionally empty: __builtin_unreachable() is always part of the sequence |
| 60 | // (see IMMEDIATE_CRASH below) and already emits a ud2 on Mac. |
| 61 | #define TRAP_SEQUENCE2_() asm volatile("") |
| 62 | #else |
| 63 | #define TRAP_SEQUENCE2_() asm volatile("ud2") |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 64 | #endif // BUILDFLAG(IS_APPLE) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 65 | |
| 66 | #elif defined(ARCH_CPU_ARMEL) |
| 67 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 68 | // bkpt will generate a SIGBUS when running on armv7 and a SIGTRAP when running |
| 69 | // as a 32 bit userspace app on arm64. There doesn't seem to be any way to |
| 70 | // cause a SIGTRAP from userspace without using a syscall (which would be a |
| 71 | // problem for sandboxing). |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 72 | // TODO(https://crbug.com/958675): Remove bkpt from this sequence. |
| 73 | #define TRAP_SEQUENCE1_() asm volatile("bkpt #0") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 74 | #define TRAP_SEQUENCE2_() asm volatile("udf #0") |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 75 | |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 76 | #elif defined(ARCH_CPU_ARM64) |
| 77 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 78 | // This will always generate a SIGTRAP on arm64. |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 79 | // TODO(https://crbug.com/958675): Remove brk from this sequence. |
| 80 | #define TRAP_SEQUENCE1_() asm volatile("brk #0") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 81 | #define TRAP_SEQUENCE2_() asm volatile("hlt #0") |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 82 | |
| 83 | #else |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 84 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 85 | // Crash report accuracy will not be guaranteed on other architectures, but at |
| 86 | // least this will crash as expected. |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 87 | #define TRAP_SEQUENCE1_() __builtin_trap() |
| 88 | #define TRAP_SEQUENCE2_() asm volatile("") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 89 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 90 | #endif // ARCH_CPU_* |
| 91 | |
| 92 | #elif defined(COMPILER_MSVC) |
| 93 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 94 | #if !defined(__clang__) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 95 | |
| 96 | // MSVC x64 doesn't support inline asm, so use the MSVC intrinsic. |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 97 | #define TRAP_SEQUENCE1_() __debugbreak() |
| 98 | #define TRAP_SEQUENCE2_() |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 99 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 100 | #elif defined(ARCH_CPU_ARM64) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 101 | |
Tom Tan | 9fc93d8 | 2019-10-30 09:01:25 | [diff] [blame] | 102 | // Windows ARM64 uses "BRK #F000" as its breakpoint instruction, and |
| 103 | // __debugbreak() generates that in both VC++ and clang. |
| 104 | #define TRAP_SEQUENCE1_() __debugbreak() |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 105 | // Intentionally empty: __builtin_unreachable() is always part of the sequence |
Nico Weber | 8b833cd | 2019-09-16 11:47:40 | [diff] [blame] | 106 | // (see IMMEDIATE_CRASH below) and already emits a ud2 on Win64, |
| 107 | // https://crbug.com/958373 |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 108 | #define TRAP_SEQUENCE2_() __asm volatile("") |
| 109 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 110 | #else |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 111 | |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 112 | #define TRAP_SEQUENCE1_() asm volatile("int3") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 113 | #define TRAP_SEQUENCE2_() asm volatile("ud2") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 114 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 115 | #endif // __clang__ |
| 116 | |
| 117 | #else |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 118 | |
| 119 | #error No supported trap sequence! |
| 120 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 121 | #endif // COMPILER_GCC |
| 122 | |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 123 | #define TRAP_SEQUENCE_() \ |
| 124 | do { \ |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 125 | TRAP_SEQUENCE1_(); \ |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 126 | TRAP_SEQUENCE2_(); \ |
| 127 | } while (false) |
| 128 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 129 | // CHECK() and the trap sequence can be invoked from a constexpr function. |
| 130 | // This could make compilation fail on GCC, as it forbids directly using inline |
| 131 | // asm inside a constexpr function. However, it allows calling a lambda |
| 132 | // expression including the same asm. |
| 133 | // The side effect is that the top of the stacktrace will not point to the |
| 134 | // calling function, but to this anonymous lambda. This is still useful as the |
| 135 | // full name of the lambda will typically include the name of the function that |
| 136 | // calls CHECK() and the debugger will still break at the right line of code. |
Nico Weber | df757b1 | 2022-01-06 23:06:13 | [diff] [blame] | 137 | #if !defined(COMPILER_GCC) || defined(__clang__) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 138 | |
| 139 | #define WRAPPED_TRAP_SEQUENCE_() TRAP_SEQUENCE_() |
| 140 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 141 | #else |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 142 | |
| 143 | #define WRAPPED_TRAP_SEQUENCE_() \ |
| 144 | do { \ |
| 145 | [] { TRAP_SEQUENCE_(); }(); \ |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 146 | } while (false) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 147 | |
Nico Weber | df757b1 | 2022-01-06 23:06:13 | [diff] [blame] | 148 | #endif // !defined(COMPILER_GCC) || defined(__clang__) |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 149 | |
| 150 | #if defined(__clang__) || defined(COMPILER_GCC) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 151 | |
| 152 | // __builtin_unreachable() hints to the compiler that this is noreturn and can |
| 153 | // be packed in the function epilogue. |
| 154 | #define IMMEDIATE_CRASH() \ |
| 155 | ({ \ |
| 156 | WRAPPED_TRAP_SEQUENCE_(); \ |
| 157 | __builtin_unreachable(); \ |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 158 | }) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 159 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 160 | #else |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 161 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 162 | // This is supporting non-chromium user of logging.h to build with MSVC, like |
| 163 | // pdfium. On MSVC there is no __builtin_unreachable(). |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 164 | #define IMMEDIATE_CRASH() WRAPPED_TRAP_SEQUENCE_() |
| 165 | |
| 166 | #endif // defined(__clang__) || defined(COMPILER_GCC) |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 167 | |
| 168 | #endif // BASE_IMMEDIATE_CRASH_H_ |