v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
immediate-crash.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
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 V8_BASE_IMMEDIATE_CRASH_H_
6#define V8_BASE_IMMEDIATE_CRASH_H_
7
8#include "include/v8config.h"
10
11// Crashes in the fastest possible way with no attempt at logging.
12// There are several constraints; see http://crbug.com/664209 for more context.
13//
14// - TRAP_SEQUENCE_() must be fatal. It should not be possible to ignore the
15// resulting exception or simply hit 'continue' to skip over it in a debugger.
16// - Different instances of TRAP_SEQUENCE_() must not be folded together, to
17// ensure crash reports are debuggable. Unlike __builtin_trap(), asm volatile
18// blocks will not be folded together.
19// Note: TRAP_SEQUENCE_() previously required an instruction with a unique
20// nonce since unlike clang, GCC folds together identical asm volatile
21// blocks.
22// - TRAP_SEQUENCE_() must produce a signal that is distinct from an invalid
23// memory access.
24// - TRAP_SEQUENCE_() must be treated as a set of noreturn instructions.
25// __builtin_unreachable() is used to provide that hint here. clang also uses
26// this as a heuristic to pack the instructions in the function epilogue to
27// improve code density.
28//
29// Additional properties that are nice to have:
30// - TRAP_SEQUENCE_() should be as compact as possible.
31// - The first instruction of TRAP_SEQUENCE_() should not change, to avoid
32// shifting crash reporting clusters. As a consequence of this, explicit
33// assembly is preferred over intrinsics.
34// Note: this last bullet point may no longer be true, and may be removed in
35// the future.
36
37// Note: TRAP_SEQUENCE Is currently split into two macro helpers due to the fact
38// that clang emits an actual instruction for __builtin_unreachable() on certain
39// platforms (see https://crbug.com/958675). In addition, the int3/bkpt/brk will
40// be removed in followups, so splitting it up like this now makes it easy to
41// land the followups.
42
43#if V8_CC_GNU
44
45#if V8_HOST_ARCH_X64 || V8_HOST_ARCH_IA32
46
47// TODO(https://crbug.com/958675): In theory, it should be possible to use just
48// int3. However, there are a number of crashes with SIGILL as the exception
49// code, so it seems likely that there's a signal handler that allows execution
50// to continue after SIGTRAP.
51#define TRAP_SEQUENCE1_() asm volatile("int3")
52
53#if V8_OS_DARWIN
54// Intentionally empty: __builtin_unreachable() is always part of the sequence
55// (see IMMEDIATE_CRASH below) and already emits a ud2 on Mac.
56#define TRAP_SEQUENCE2_() asm volatile("")
57#else
58#define TRAP_SEQUENCE2_() asm volatile("ud2")
59#endif // V8_OS_DARWIN
60
61#elif V8_HOST_ARCH_ARM
62
63// bkpt will generate a SIGBUS when running on armv7 and a SIGTRAP when running
64// as a 32 bit userspace app on arm64. There doesn't seem to be any way to
65// cause a SIGTRAP from userspace without using a syscall (which would be a
66// problem for sandboxing).
67// TODO(https://crbug.com/958675): Remove bkpt from this sequence.
68#define TRAP_SEQUENCE1_() asm volatile("bkpt #0")
69#define TRAP_SEQUENCE2_() asm volatile("udf #0")
70
71#elif V8_HOST_ARCH_ARM64
72
73// This will always generate a SIGTRAP on arm64.
74// TODO(https://crbug.com/958675): Remove brk from this sequence.
75#define TRAP_SEQUENCE1_() asm volatile("brk #0")
76#define TRAP_SEQUENCE2_() asm volatile("hlt #0")
77
78#elif V8_HOST_ARCH_PPC64
79
80// GDB software breakpoint instruction.
81// Same as `bkpt` under the assembler.
82#if V8_OS_AIX
83#define TRAP_SEQUENCE1_() asm volatile(".vbyte 4,0x7D821008");
84#else
85#define TRAP_SEQUENCE1_() asm volatile(".4byte 0x7D821008");
86#endif
87#define TRAP_SEQUENCE2_() asm volatile("")
88
89#elif V8_OS_ZOS
90
91#define TRAP_SEQUENCE1_() __builtin_trap()
92#define TRAP_SEQUENCE2_() asm volatile("")
93
94#elif V8_HOST_ARCH_S390X
95
96// GDB software breakpoint instruction.
97// Same as `bkpt` under the assembler.
98#define TRAP_SEQUENCE1_() asm volatile(".2byte 0x0001");
99#define TRAP_SEQUENCE2_() asm volatile("")
100
101#else
102
103// Crash report accuracy will not be guaranteed on other architectures, but at
104// least this will crash as expected.
105#define TRAP_SEQUENCE1_() __builtin_trap()
106#define TRAP_SEQUENCE2_() asm volatile("")
107
108#endif // V8_HOST_ARCH_*
109
110#elif V8_CC_MSVC
111
112#if !defined(__clang__)
113
114// MSVC x64 doesn't support inline asm, so use the MSVC intrinsic.
115#define TRAP_SEQUENCE1_() __debugbreak()
116#define TRAP_SEQUENCE2_()
117
118#elif V8_HOST_ARCH_ARM64
119
120// Windows ARM64 uses "BRK #F000" as its breakpoint instruction, and
121// __debugbreak() generates that in both VC++ and clang.
122#define TRAP_SEQUENCE1_() __debugbreak()
123// Intentionally empty: __builtin_unreachable() is always part of the sequence
124// (see IMMEDIATE_CRASH below) and already emits a ud2 on Win64,
125// https://crbug.com/958373
126#define TRAP_SEQUENCE2_() __asm volatile("")
127
128#else
129
130#define TRAP_SEQUENCE1_() asm volatile("int3")
131#define TRAP_SEQUENCE2_() asm volatile("ud2")
132
133#endif // __clang__
134
135#else
136
137#error No supported trap sequence!
138
139#endif // V8_CC_GNU
140
141#define TRAP_SEQUENCE_() \
142 do { \
143 TRAP_SEQUENCE1_(); \
144 TRAP_SEQUENCE2_(); \
145 } while (false)
146
147// CHECK() and the trap sequence can be invoked from a constexpr function.
148// This could make compilation fail on GCC, as it forbids directly using inline
149// asm inside a constexpr function. However, it allows calling a lambda
150// expression including the same asm.
151// The side effect is that the top of the stacktrace will not point to the
152// calling function, but to this anonymous lambda. This is still useful as the
153// full name of the lambda will typically include the name of the function that
154// calls CHECK() and the debugger will still break at the right line of code.
155#if !V8_CC_GNU
156
157#define WRAPPED_TRAP_SEQUENCE_() TRAP_SEQUENCE_()
158
159#else
160
161#define WRAPPED_TRAP_SEQUENCE_() \
162 do { \
163 [] { TRAP_SEQUENCE_(); }(); \
164 } while (false)
165
166#endif // !V8_CC_GNU
167
168#if defined(__clang__) || V8_CC_GNU
169
170// __builtin_unreachable() hints to the compiler that this is noreturn and can
171// be packed in the function epilogue.
172#define IMMEDIATE_CRASH() \
173 ({ \
174 WRAPPED_TRAP_SEQUENCE_(); \
175 __builtin_unreachable(); \
176 })
177
178#else
179
180// This is supporting build with MSVC where there is no __builtin_unreachable().
181#define IMMEDIATE_CRASH() WRAPPED_TRAP_SEQUENCE_()
182
183#endif // defined(__clang__) || defined(COMPILER_GCC)
184
185#endif // V8_BASE_IMMEDIATE_CRASH_H_