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 | |
Adrian Taylor | ab8f357 | 2023-12-01 20:57:18 | [diff] [blame] | 8 | #include "base/fuzzing_buildflags.h" |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 9 | #include "build/build_config.h" |
| 10 | |
Brendon Tiszka | 254cb73 | 2024-09-27 15:17:08 | [diff] [blame] | 11 | #if !(defined(OFFICIAL_BUILD) || BUILDFLAG(IS_WIN)) |
Adrian Taylor | ab8f357 | 2023-12-01 20:57:18 | [diff] [blame] | 12 | #include <stdlib.h> |
Brendon Tiszka | 254cb73 | 2024-09-27 15:17:08 | [diff] [blame] | 13 | #endif |
Adrian Taylor | c54d197 | 2023-12-05 22:10:02 | [diff] [blame] | 14 | |
Brendon Tiszka | 254cb73 | 2024-09-27 15:17:08 | [diff] [blame] | 15 | #if BUILDFLAG(USE_FUZZING_ENGINE) && BUILDFLAG(IS_LINUX) |
Adrian Taylor | c54d197 | 2023-12-05 22:10:02 | [diff] [blame] | 16 | // The fuzzing coverage display wants to record coverage even |
| 17 | // for failure cases. It's Linux-only. So on Linux, dump coverage |
| 18 | // before we immediately exit. We provide a weak symbol so that |
| 19 | // this causes no link problems on configurations that don't involve |
Brendon Tiszka | 254cb73 | 2024-09-27 15:17:08 | [diff] [blame] | 20 | // coverage. (This wouldn't work on Windows due to limitations of |
| 21 | // weak symbol linkage.) |
Paul Semel | 5ad5f164 | 2023-12-12 11:15:58 | [diff] [blame] | 22 | extern "C" int __attribute__((weak)) __llvm_profile_write_file(void); |
Brendon Tiszka | 254cb73 | 2024-09-27 15:17:08 | [diff] [blame] | 23 | #endif // BUILDFLAG(USE_FUZZING_ENGINE) && BUILDFLAG(IS_LINUX) |
Adrian Taylor | ab8f357 | 2023-12-01 20:57:18 | [diff] [blame] | 24 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 25 | // Crashes in the fastest possible way with no attempt at logging. |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 26 | // There are several constraints; see http://crbug.com/664209 for more context. |
| 27 | // |
| 28 | // - TRAP_SEQUENCE_() must be fatal. It should not be possible to ignore the |
| 29 | // resulting exception or simply hit 'continue' to skip over it in a debugger. |
| 30 | // - Different instances of TRAP_SEQUENCE_() must not be folded together, to |
| 31 | // ensure crash reports are debuggable. Unlike __builtin_trap(), asm volatile |
| 32 | // blocks will not be folded together. |
| 33 | // Note: TRAP_SEQUENCE_() previously required an instruction with a unique |
| 34 | // nonce since unlike clang, GCC folds together identical asm volatile |
| 35 | // blocks. |
| 36 | // - TRAP_SEQUENCE_() must produce a signal that is distinct from an invalid |
| 37 | // memory access. |
| 38 | // - TRAP_SEQUENCE_() must be treated as a set of noreturn instructions. |
| 39 | // __builtin_unreachable() is used to provide that hint here. clang also uses |
| 40 | // this as a heuristic to pack the instructions in the function epilogue to |
| 41 | // improve code density. |
André Kempe | 3566cc5 | 2023-03-30 14:10:17 | [diff] [blame] | 42 | // - base::ImmediateCrash() is used in allocation hooks. To prevent recursions, |
| 43 | // TRAP_SEQUENCE_() must not allocate. |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 44 | // |
| 45 | // Additional properties that are nice to have: |
| 46 | // - TRAP_SEQUENCE_() should be as compact as possible. |
| 47 | // - The first instruction of TRAP_SEQUENCE_() should not change, to avoid |
| 48 | // shifting crash reporting clusters. As a consequence of this, explicit |
| 49 | // assembly is preferred over intrinsics. |
| 50 | // Note: this last bullet point may no longer be true, and may be removed in |
| 51 | // the future. |
| 52 | |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 53 | // Note: TRAP_SEQUENCE Is currently split into two macro helpers due to the fact |
| 54 | // that clang emits an actual instruction for __builtin_unreachable() on certain |
| 55 | // platforms (see https://crbug.com/958675). In addition, the int3/bkpt/brk will |
| 56 | // be removed in followups, so splitting it up like this now makes it easy to |
| 57 | // land the followups. |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 58 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 59 | #if defined(COMPILER_GCC) |
| 60 | |
Daniel Cheng | 110e9da | 2025-06-24 05:17:07 | [diff] [blame] | 61 | #if defined(ARCH_CPU_X86_FAMILY) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 62 | |
Alison Gale | d965ba0 | 2024-04-26 21:50:54 | [diff] [blame] | 63 | // TODO(crbug.com/40625592): In theory, it should be possible to use just |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 64 | // int3. However, there are a number of crashes with SIGILL as the exception |
| 65 | // code, so it seems likely that there's a signal handler that allows execution |
| 66 | // to continue after SIGTRAP. |
| 67 | #define TRAP_SEQUENCE1_() asm volatile("int3") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 68 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 69 | #if BUILDFLAG(IS_APPLE) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 70 | // Intentionally empty: __builtin_unreachable() is always part of the sequence |
| 71 | // (see IMMEDIATE_CRASH below) and already emits a ud2 on Mac. |
| 72 | #define TRAP_SEQUENCE2_() asm volatile("") |
| 73 | #else |
| 74 | #define TRAP_SEQUENCE2_() asm volatile("ud2") |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 75 | #endif // BUILDFLAG(IS_APPLE) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 76 | |
| 77 | #elif defined(ARCH_CPU_ARMEL) |
| 78 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 79 | // bkpt will generate a SIGBUS when running on armv7 and a SIGTRAP when running |
| 80 | // as a 32 bit userspace app on arm64. There doesn't seem to be any way to |
| 81 | // cause a SIGTRAP from userspace without using a syscall (which would be a |
| 82 | // problem for sandboxing). |
Alison Gale | d965ba0 | 2024-04-26 21:50:54 | [diff] [blame] | 83 | // TODO(crbug.com/40625592): Remove bkpt from this sequence. |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 84 | #define TRAP_SEQUENCE1_() asm volatile("bkpt #0") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 85 | #define TRAP_SEQUENCE2_() asm volatile("udf #0") |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 86 | |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 87 | #elif defined(ARCH_CPU_ARM64) |
| 88 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 89 | // This will always generate a SIGTRAP on arm64. |
Alison Gale | d965ba0 | 2024-04-26 21:50:54 | [diff] [blame] | 90 | // TODO(crbug.com/40625592): Remove brk from this sequence. |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 91 | #define TRAP_SEQUENCE1_() asm volatile("brk #0") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 92 | #define TRAP_SEQUENCE2_() asm volatile("hlt #0") |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 93 | |
| 94 | #else |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 95 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 96 | // Crash report accuracy will not be guaranteed on other architectures, but at |
| 97 | // least this will crash as expected. |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 98 | #define TRAP_SEQUENCE1_() __builtin_trap() |
| 99 | #define TRAP_SEQUENCE2_() asm volatile("") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 100 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 101 | #endif // ARCH_CPU_* |
| 102 | |
| 103 | #elif defined(COMPILER_MSVC) |
| 104 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 105 | #if !defined(__clang__) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 106 | |
| 107 | // MSVC x64 doesn't support inline asm, so use the MSVC intrinsic. |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 108 | #define TRAP_SEQUENCE1_() __debugbreak() |
| 109 | #define TRAP_SEQUENCE2_() |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 110 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 111 | #elif defined(ARCH_CPU_ARM64) |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 112 | |
Tom Tan | 9fc93d8 | 2019-10-30 09:01:25 | [diff] [blame] | 113 | // Windows ARM64 uses "BRK #F000" as its breakpoint instruction, and |
| 114 | // __debugbreak() generates that in both VC++ and clang. |
| 115 | #define TRAP_SEQUENCE1_() __debugbreak() |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 116 | // Intentionally empty: __builtin_unreachable() is always part of the sequence |
Nico Weber | 8b833cd | 2019-09-16 11:47:40 | [diff] [blame] | 117 | // (see IMMEDIATE_CRASH below) and already emits a ud2 on Win64, |
| 118 | // https://crbug.com/958373 |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 119 | #define TRAP_SEQUENCE2_() __asm volatile("") |
| 120 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 121 | #else |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 122 | |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 123 | #define TRAP_SEQUENCE1_() asm volatile("int3") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 124 | #define TRAP_SEQUENCE2_() asm volatile("ud2") |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 125 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 126 | #endif // __clang__ |
| 127 | |
| 128 | #else |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 129 | |
| 130 | #error No supported trap sequence! |
| 131 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 132 | #endif // COMPILER_GCC |
| 133 | |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 134 | #define TRAP_SEQUENCE_() \ |
| 135 | do { \ |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 136 | TRAP_SEQUENCE1_(); \ |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 137 | TRAP_SEQUENCE2_(); \ |
| 138 | } while (false) |
| 139 | |
Peter Boström | b30544d | 2022-10-21 00:17:58 | [diff] [blame] | 140 | // This version of ALWAYS_INLINE inlines even in is_debug=true. |
| 141 | // TODO(pbos): See if NDEBUG can be dropped from ALWAYS_INLINE as well, and if |
| 142 | // so merge. Otherwise document why it cannot inline in debug in |
| 143 | // base/compiler_specific.h. |
| 144 | #if defined(COMPILER_GCC) |
| 145 | #define IMMEDIATE_CRASH_ALWAYS_INLINE inline __attribute__((__always_inline__)) |
| 146 | #elif defined(COMPILER_MSVC) |
| 147 | #define IMMEDIATE_CRASH_ALWAYS_INLINE __forceinline |
Hiroki Nakagawa | cd01d12 | 2022-10-20 02:44:20 | [diff] [blame] | 148 | #else |
Peter Boström | b30544d | 2022-10-21 00:17:58 | [diff] [blame] | 149 | #define IMMEDIATE_CRASH_ALWAYS_INLINE inline |
| 150 | #endif |
Hiroki Nakagawa | cd01d12 | 2022-10-20 02:44:20 | [diff] [blame] | 151 | |
Peter Boström | b30544d | 2022-10-21 00:17:58 | [diff] [blame] | 152 | namespace base { |
Hiroki Nakagawa | cd01d12 | 2022-10-20 02:44:20 | [diff] [blame] | 153 | |
Peter Boström | b30544d | 2022-10-21 00:17:58 | [diff] [blame] | 154 | [[noreturn]] IMMEDIATE_CRASH_ALWAYS_INLINE void ImmediateCrash() { |
Brendon Tiszka | 254cb73 | 2024-09-27 15:17:08 | [diff] [blame] | 155 | #if BUILDFLAG(USE_FUZZING_ENGINE) && BUILDFLAG(IS_LINUX) |
| 156 | // A fuzzer run will often handle many successful cases then |
| 157 | // find one which crashes and dies. It's important that the |
| 158 | // coverage of those successful cases is represented when we're |
| 159 | // considering fuzzing coverage. At the moment fuzzing coverage |
| 160 | // is only measured on Linux, which is why this is Linux- |
| 161 | // specific. |
| 162 | // exit() arranges to write out coverage information because |
| 163 | // an atexit handler is registered to do so, but there is no |
| 164 | // such action in the std::abort case. Instead, manually write |
| 165 | // out such coverage. |
| 166 | // We could extend this step to all coverage builds, but |
| 167 | // at present failing tests don't get coverage reported, |
| 168 | // so we're retaining that behavior. |
| 169 | // TODO(crbug.com/40948553): consider doing this for all coverage builds |
Paul Semel | df26c85 | 2023-12-18 20:36:58 | [diff] [blame] | 170 | if (__llvm_profile_write_file) { |
| 171 | __llvm_profile_write_file(); |
| 172 | } |
Brendon Tiszka | 254cb73 | 2024-09-27 15:17:08 | [diff] [blame] | 173 | #endif // BUILDFLAG(USE_FUZZING_ENGINE) && BUILDFLAG(IS_LINUX) |
| 174 | |
| 175 | #if defined(OFFICIAL_BUILD) || BUILDFLAG(IS_WIN) |
| 176 | // We can't use abort() on Windows because it results in the |
| 177 | // abort/retry/ignore dialog which disrupts automated tests. |
| 178 | // TODO(crbug.com/40948553): investigate if such dialogs can |
| 179 | // be suppressed |
Peter Boström | b30544d | 2022-10-21 00:17:58 | [diff] [blame] | 180 | TRAP_SEQUENCE_(); |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 181 | #if defined(__clang__) || defined(COMPILER_GCC) |
Peter Boström | b30544d | 2022-10-21 00:17:58 | [diff] [blame] | 182 | __builtin_unreachable(); |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 183 | #endif // defined(__clang__) || defined(COMPILER_GCC) |
Brendon Tiszka | 254cb73 | 2024-09-27 15:17:08 | [diff] [blame] | 184 | #else // defined(OFFICIAL_BUILD) || BUILDFLAG(IS_WIN) |
| 185 | abort(); |
| 186 | #endif // defined(OFFICIAL_BUILD) |
Peter Boström | b30544d | 2022-10-21 00:17:58 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | } // namespace base |
| 190 | |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 191 | #endif // BASE_IMMEDIATE_CRASH_H_ |