Skip to content
File

Blob: src/workerd/util/immediate-crash.h

cpp105 lines
1// Copyright (c) 2023 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4//
5// When testing the REPRL interface it's important to see whether Fuzzilli can
6// observe a crash.
7// The IMMEDIATE_CRASH macro is being used to test crashes in fuzzilli function
8// which can be called from within JavaScript as fuzzilli("FUZZILLI_CRASH",0).
9//
10 
11#pragma once
12 
13#if defined(__GNUC__) || defined(__clang__)
14 
15#if defined(__x86_64__) || defined(__i386__)
16 
17#define TRAP_SEQUENCE1_() asm volatile("int3")
18#define TRAP_SEQUENCE2_() asm volatile("ud2")
19 
20#elifdef __arm__
21 
22#define TRAP_SEQUENCE1_() asm volatile("bkpt #0")
23#define TRAP_SEQUENCE2_() asm volatile("udf #0")
24 
25#elifdef __aarch64__
26 
27#define TRAP_SEQUENCE1_() asm volatile("brk #0")
28#define TRAP_SEQUENCE2_() asm volatile("hlt #0")
29 
30#elifdef __powerpc64__
31 
32#define TRAP_SEQUENCE1_() asm volatile(".4byte 0x7D821008")
33#define TRAP_SEQUENCE2_() asm volatile("")
34 
35#elifdef __s390x__
36 
37#define TRAP_SEQUENCE1_() asm volatile(".2byte 0x0001")
38#define TRAP_SEQUENCE2_() asm volatile("")
39 
40#else
41 
42#define TRAP_SEQUENCE1_() __builtin_trap()
43#define TRAP_SEQUENCE2_() asm volatile("")
44 
45#endif // Architecture check
46 
47#elifdef _MSC_VER
48 
49#if defined(_M_X64) || defined(_M_IX86)
50 
51#define TRAP_SEQUENCE1_() __debugbreak()
52#define TRAP_SEQUENCE2_()
53 
54#elifdef _M_ARM64
55 
56#define TRAP_SEQUENCE1_() __debugbreak()
57#define TRAP_SEQUENCE2_()
58 
59#else
60 
61#error No supported trap sequence for this MSVC architecture!
62 
63#endif // _MSC_VER architecture check
64 
65#else
66 
67#error No supported compiler!
68 
69#endif // Compiler check
70 
71#define TRAP_SEQUENCE_() \
72 do { \
73 TRAP_SEQUENCE1_(); \
74 TRAP_SEQUENCE2_(); \
75 } while (false)
76 
77// Wrapping the trap sequence to allow its use inside constexpr functions.
78#if defined(__GNUC__) || defined(__clang__)
79 
80#define WRAPPED_TRAP_SEQUENCE_() \
81 do { \
82 [] { TRAP_SEQUENCE_(); }(); \
83 } while (false)
84 
85#else
86 
87#define WRAPPED_TRAP_SEQUENCE_() TRAP_SEQUENCE_()
88 
89#endif // Compiler check for wrapping
90 
91#if defined(__clang__) || defined(__GNUC__)
92 
93// __builtin_unreachable() hints to the compiler that this code path is not reachable.
94#define IMMEDIATE_CRASH() \
95 ({ \
96 WRAPPED_TRAP_SEQUENCE_(); \
97 __builtin_unreachable(); \
98 })
99 
100#else
101 
102#define IMMEDIATE_CRASH() WRAPPED_TRAP_SEQUENCE_()
103 
104#endif // __clang__ or __GNUC__