xref: /aosp_15_r20/external/pdfium/third_party/base/immediate_crash.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1*3ac0a46fSAndroid Build Coastguard Worker // Copyright 2019 The Chromium Authors
2*3ac0a46fSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*3ac0a46fSAndroid Build Coastguard Worker // found in the LICENSE file.
4*3ac0a46fSAndroid Build Coastguard Worker 
5*3ac0a46fSAndroid Build Coastguard Worker #ifndef THIRD_PARTY_BASE_IMMEDIATE_CRASH_H_
6*3ac0a46fSAndroid Build Coastguard Worker #define THIRD_PARTY_BASE_IMMEDIATE_CRASH_H_
7*3ac0a46fSAndroid Build Coastguard Worker 
8*3ac0a46fSAndroid Build Coastguard Worker #include "build/build_config.h"
9*3ac0a46fSAndroid Build Coastguard Worker 
10*3ac0a46fSAndroid Build Coastguard Worker // Crashes in the fastest possible way with no attempt at logging.
11*3ac0a46fSAndroid Build Coastguard Worker // There are several constraints; see http://crbug.com/664209 for more context.
12*3ac0a46fSAndroid Build Coastguard Worker //
13*3ac0a46fSAndroid Build Coastguard Worker // - TRAP_SEQUENCE_() must be fatal. It should not be possible to ignore the
14*3ac0a46fSAndroid Build Coastguard Worker //   resulting exception or simply hit 'continue' to skip over it in a debugger.
15*3ac0a46fSAndroid Build Coastguard Worker // - Different instances of TRAP_SEQUENCE_() must not be folded together, to
16*3ac0a46fSAndroid Build Coastguard Worker //   ensure crash reports are debuggable. Unlike __builtin_trap(), asm volatile
17*3ac0a46fSAndroid Build Coastguard Worker //   blocks will not be folded together.
18*3ac0a46fSAndroid Build Coastguard Worker //   Note: TRAP_SEQUENCE_() previously required an instruction with a unique
19*3ac0a46fSAndroid Build Coastguard Worker //   nonce since unlike clang, GCC folds together identical asm volatile
20*3ac0a46fSAndroid Build Coastguard Worker //   blocks.
21*3ac0a46fSAndroid Build Coastguard Worker // - TRAP_SEQUENCE_() must produce a signal that is distinct from an invalid
22*3ac0a46fSAndroid Build Coastguard Worker //   memory access.
23*3ac0a46fSAndroid Build Coastguard Worker // - TRAP_SEQUENCE_() must be treated as a set of noreturn instructions.
24*3ac0a46fSAndroid Build Coastguard Worker //   __builtin_unreachable() is used to provide that hint here. clang also uses
25*3ac0a46fSAndroid Build Coastguard Worker //   this as a heuristic to pack the instructions in the function epilogue to
26*3ac0a46fSAndroid Build Coastguard Worker //   improve code density.
27*3ac0a46fSAndroid Build Coastguard Worker //
28*3ac0a46fSAndroid Build Coastguard Worker // Additional properties that are nice to have:
29*3ac0a46fSAndroid Build Coastguard Worker // - TRAP_SEQUENCE_() should be as compact as possible.
30*3ac0a46fSAndroid Build Coastguard Worker // - The first instruction of TRAP_SEQUENCE_() should not change, to avoid
31*3ac0a46fSAndroid Build Coastguard Worker //   shifting crash reporting clusters. As a consequence of this, explicit
32*3ac0a46fSAndroid Build Coastguard Worker //   assembly is preferred over intrinsics.
33*3ac0a46fSAndroid Build Coastguard Worker //   Note: this last bullet point may no longer be true, and may be removed in
34*3ac0a46fSAndroid Build Coastguard Worker //   the future.
35*3ac0a46fSAndroid Build Coastguard Worker 
36*3ac0a46fSAndroid Build Coastguard Worker // Note: TRAP_SEQUENCE Is currently split into two macro helpers due to the fact
37*3ac0a46fSAndroid Build Coastguard Worker // that clang emits an actual instruction for __builtin_unreachable() on certain
38*3ac0a46fSAndroid Build Coastguard Worker // platforms (see https://crbug.com/958675). In addition, the int3/bkpt/brk will
39*3ac0a46fSAndroid Build Coastguard Worker // be removed in followups, so splitting it up like this now makes it easy to
40*3ac0a46fSAndroid Build Coastguard Worker // land the followups.
41*3ac0a46fSAndroid Build Coastguard Worker 
42*3ac0a46fSAndroid Build Coastguard Worker #if defined(COMPILER_GCC)
43*3ac0a46fSAndroid Build Coastguard Worker 
44*3ac0a46fSAndroid Build Coastguard Worker #if BUILDFLAG(IS_NACL)
45*3ac0a46fSAndroid Build Coastguard Worker 
46*3ac0a46fSAndroid Build Coastguard Worker // Crash report accuracy is not guaranteed on NaCl.
47*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE1_() __builtin_trap()
48*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE2_() asm volatile("")
49*3ac0a46fSAndroid Build Coastguard Worker 
50*3ac0a46fSAndroid Build Coastguard Worker #elif defined(ARCH_CPU_X86_FAMILY)
51*3ac0a46fSAndroid Build Coastguard Worker 
52*3ac0a46fSAndroid Build Coastguard Worker // TODO(https://crbug.com/958675): In theory, it should be possible to use just
53*3ac0a46fSAndroid Build Coastguard Worker // int3. However, there are a number of crashes with SIGILL as the exception
54*3ac0a46fSAndroid Build Coastguard Worker // code, so it seems likely that there's a signal handler that allows execution
55*3ac0a46fSAndroid Build Coastguard Worker // to continue after SIGTRAP.
56*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE1_() asm volatile("int3")
57*3ac0a46fSAndroid Build Coastguard Worker 
58*3ac0a46fSAndroid Build Coastguard Worker #if BUILDFLAG(IS_APPLE)
59*3ac0a46fSAndroid Build Coastguard Worker // Intentionally empty: __builtin_unreachable() is always part of the sequence
60*3ac0a46fSAndroid Build Coastguard Worker // (see IMMEDIATE_CRASH below) and already emits a ud2 on Mac.
61*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE2_() asm volatile("")
62*3ac0a46fSAndroid Build Coastguard Worker #else
63*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE2_() asm volatile("ud2")
64*3ac0a46fSAndroid Build Coastguard Worker #endif  // BUILDFLAG(IS_APPLE)
65*3ac0a46fSAndroid Build Coastguard Worker 
66*3ac0a46fSAndroid Build Coastguard Worker #elif defined(ARCH_CPU_ARMEL)
67*3ac0a46fSAndroid Build Coastguard Worker 
68*3ac0a46fSAndroid Build Coastguard Worker // bkpt will generate a SIGBUS when running on armv7 and a SIGTRAP when running
69*3ac0a46fSAndroid Build Coastguard Worker // as a 32 bit userspace app on arm64. There doesn't seem to be any way to
70*3ac0a46fSAndroid Build Coastguard Worker // cause a SIGTRAP from userspace without using a syscall (which would be a
71*3ac0a46fSAndroid Build Coastguard Worker // problem for sandboxing).
72*3ac0a46fSAndroid Build Coastguard Worker // TODO(https://crbug.com/958675): Remove bkpt from this sequence.
73*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE1_() asm volatile("bkpt #0")
74*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE2_() asm volatile("udf #0")
75*3ac0a46fSAndroid Build Coastguard Worker 
76*3ac0a46fSAndroid Build Coastguard Worker #elif defined(ARCH_CPU_ARM64)
77*3ac0a46fSAndroid Build Coastguard Worker 
78*3ac0a46fSAndroid Build Coastguard Worker // This will always generate a SIGTRAP on arm64.
79*3ac0a46fSAndroid Build Coastguard Worker // TODO(https://crbug.com/958675): Remove brk from this sequence.
80*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE1_() asm volatile("brk #0")
81*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE2_() asm volatile("hlt #0")
82*3ac0a46fSAndroid Build Coastguard Worker 
83*3ac0a46fSAndroid Build Coastguard Worker #else
84*3ac0a46fSAndroid Build Coastguard Worker 
85*3ac0a46fSAndroid Build Coastguard Worker // Crash report accuracy will not be guaranteed on other architectures, but at
86*3ac0a46fSAndroid Build Coastguard Worker // least this will crash as expected.
87*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE1_() __builtin_trap()
88*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE2_() asm volatile("")
89*3ac0a46fSAndroid Build Coastguard Worker 
90*3ac0a46fSAndroid Build Coastguard Worker #endif  // ARCH_CPU_*
91*3ac0a46fSAndroid Build Coastguard Worker 
92*3ac0a46fSAndroid Build Coastguard Worker #elif defined(COMPILER_MSVC)
93*3ac0a46fSAndroid Build Coastguard Worker 
94*3ac0a46fSAndroid Build Coastguard Worker #if !defined(__clang__)
95*3ac0a46fSAndroid Build Coastguard Worker 
96*3ac0a46fSAndroid Build Coastguard Worker // MSVC x64 doesn't support inline asm, so use the MSVC intrinsic.
97*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE1_() __debugbreak()
98*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE2_()
99*3ac0a46fSAndroid Build Coastguard Worker 
100*3ac0a46fSAndroid Build Coastguard Worker #elif defined(ARCH_CPU_ARM64)
101*3ac0a46fSAndroid Build Coastguard Worker 
102*3ac0a46fSAndroid Build Coastguard Worker // Windows ARM64 uses "BRK #F000" as its breakpoint instruction, and
103*3ac0a46fSAndroid Build Coastguard Worker // __debugbreak() generates that in both VC++ and clang.
104*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE1_() __debugbreak()
105*3ac0a46fSAndroid Build Coastguard Worker // Intentionally empty: __builtin_unreachable() is always part of the sequence
106*3ac0a46fSAndroid Build Coastguard Worker // (see IMMEDIATE_CRASH below) and already emits a ud2 on Win64,
107*3ac0a46fSAndroid Build Coastguard Worker // https://crbug.com/958373
108*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE2_() __asm volatile("")
109*3ac0a46fSAndroid Build Coastguard Worker 
110*3ac0a46fSAndroid Build Coastguard Worker #else
111*3ac0a46fSAndroid Build Coastguard Worker 
112*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE1_() asm volatile("int3")
113*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE2_() asm volatile("ud2")
114*3ac0a46fSAndroid Build Coastguard Worker 
115*3ac0a46fSAndroid Build Coastguard Worker #endif  // __clang__
116*3ac0a46fSAndroid Build Coastguard Worker 
117*3ac0a46fSAndroid Build Coastguard Worker #else
118*3ac0a46fSAndroid Build Coastguard Worker 
119*3ac0a46fSAndroid Build Coastguard Worker #error No supported trap sequence!
120*3ac0a46fSAndroid Build Coastguard Worker 
121*3ac0a46fSAndroid Build Coastguard Worker #endif  // COMPILER_GCC
122*3ac0a46fSAndroid Build Coastguard Worker 
123*3ac0a46fSAndroid Build Coastguard Worker #define TRAP_SEQUENCE_() \
124*3ac0a46fSAndroid Build Coastguard Worker   do {                   \
125*3ac0a46fSAndroid Build Coastguard Worker     TRAP_SEQUENCE1_();   \
126*3ac0a46fSAndroid Build Coastguard Worker     TRAP_SEQUENCE2_();   \
127*3ac0a46fSAndroid Build Coastguard Worker   } while (false)
128*3ac0a46fSAndroid Build Coastguard Worker 
129*3ac0a46fSAndroid Build Coastguard Worker // This version of ALWAYS_INLINE inlines even in is_debug=true.
130*3ac0a46fSAndroid Build Coastguard Worker // TODO(pbos): See if NDEBUG can be dropped from ALWAYS_INLINE as well, and if
131*3ac0a46fSAndroid Build Coastguard Worker // so merge. Otherwise document why it cannot inline in debug in
132*3ac0a46fSAndroid Build Coastguard Worker // base/compiler_specific.h.
133*3ac0a46fSAndroid Build Coastguard Worker #if defined(COMPILER_GCC)
134*3ac0a46fSAndroid Build Coastguard Worker #define IMMEDIATE_CRASH_ALWAYS_INLINE inline __attribute__((__always_inline__))
135*3ac0a46fSAndroid Build Coastguard Worker #elif defined(COMPILER_MSVC)
136*3ac0a46fSAndroid Build Coastguard Worker #define IMMEDIATE_CRASH_ALWAYS_INLINE __forceinline
137*3ac0a46fSAndroid Build Coastguard Worker #else
138*3ac0a46fSAndroid Build Coastguard Worker #define IMMEDIATE_CRASH_ALWAYS_INLINE inline
139*3ac0a46fSAndroid Build Coastguard Worker #endif
140*3ac0a46fSAndroid Build Coastguard Worker 
141*3ac0a46fSAndroid Build Coastguard Worker namespace pdfium {
142*3ac0a46fSAndroid Build Coastguard Worker namespace base {
143*3ac0a46fSAndroid Build Coastguard Worker 
ImmediateCrash()144*3ac0a46fSAndroid Build Coastguard Worker [[noreturn]] IMMEDIATE_CRASH_ALWAYS_INLINE void ImmediateCrash() {
145*3ac0a46fSAndroid Build Coastguard Worker   TRAP_SEQUENCE_();
146*3ac0a46fSAndroid Build Coastguard Worker #if defined(__clang__) || defined(COMPILER_GCC)
147*3ac0a46fSAndroid Build Coastguard Worker   __builtin_unreachable();
148*3ac0a46fSAndroid Build Coastguard Worker #endif  // defined(__clang__) || defined(COMPILER_GCC)
149*3ac0a46fSAndroid Build Coastguard Worker }
150*3ac0a46fSAndroid Build Coastguard Worker 
151*3ac0a46fSAndroid Build Coastguard Worker }  // namespace base
152*3ac0a46fSAndroid Build Coastguard Worker }  // namespace pdfium
153*3ac0a46fSAndroid Build Coastguard Worker 
154*3ac0a46fSAndroid Build Coastguard Worker #endif  // THIRD_PARTY_BASE_IMMEDIATE_CRASH_H_
155