1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/immediate_crash.h"
6
7 #include <stdint.h>
8
9 #include "base/base_paths.h"
10 #include "base/clang_profiling_buildflags.h"
11 #include "base/containers/span.h"
12 #include "base/files/file_path.h"
13 #include "base/path_service.h"
14 #include "base/ranges/algorithm.h"
15 #include "base/scoped_native_library.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "build/build_config.h"
18 #include "build/buildflag.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/abseil-cpp/absl/types/optional.h"
21
22 namespace base {
23
24 namespace {
25
26 // If ImmediateCrash() is not treated as noreturn by the compiler, the compiler
27 // will complain that not all paths through this function return a value.
TestImmediateCrashTreatedAsNoReturn()28 [[maybe_unused]] int TestImmediateCrashTreatedAsNoReturn() {
29 ImmediateCrash();
30 }
31
32 #if defined(ARCH_CPU_X86_FAMILY)
33 // This is tricksy and false, since x86 instructions are not all one byte long,
34 // but there is no better alternative short of implementing an x86 instruction
35 // decoder.
36 using Instruction = uint8_t;
37
38 // https://software.intel.com/en-us/download/intel-64-and-ia-32-architectures-sdm-combined-volumes-1-2a-2b-2c-2d-3a-3b-3c-3d-and-4
39 // Look for RET opcode (0xc3). Note that 0xC3 is a substring of several
40 // other opcodes (VMRESUME, MOVNTI), and can also be encoded as part of an
41 // argument to another opcode. None of these other cases are expected to be
42 // present, so a simple byte scan should be Good Enough™.
43 constexpr Instruction kRet = 0xc3;
44 // INT3 ; UD2
45 constexpr Instruction kRequiredBody[] = {0xcc, 0x0f, 0x0b};
46 constexpr Instruction kOptionalFooter[] = {};
47
48 #elif defined(ARCH_CPU_ARMEL)
49 using Instruction = uint16_t;
50
51 // T32 opcode reference: https://developer.arm.com/docs/ddi0487/latest
52 // Actually BX LR, canonical encoding:
53 constexpr Instruction kRet = 0x4770;
54 // BKPT #0; UDF #0
55 constexpr Instruction kRequiredBody[] = {0xbe00, 0xde00};
56 constexpr Instruction kOptionalFooter[] = {};
57
58 #elif defined(ARCH_CPU_ARM64)
59 using Instruction = uint32_t;
60
61 // A64 opcode reference: https://developer.arm.com/docs/ddi0487/latest
62 // Use an enum here rather than separate constexpr vars because otherwise some
63 // of the vars will end up unused on each platform, upsetting
64 // -Wunused-const-variable.
65 enum : Instruction {
66 // There are multiple valid encodings of return (which is really a special
67 // form of branch). This is the one clang seems to use:
68 kRet = 0xd65f03c0,
69 kBrk0 = 0xd4200000,
70 kBrk1 = 0xd4200020,
71 kBrkF000 = 0xd43e0000,
72 kHlt0 = 0xd4400000,
73 };
74
75 #if BUILDFLAG(IS_WIN)
76
77 constexpr Instruction kRequiredBody[] = {kBrkF000, kBrk1};
78 constexpr Instruction kOptionalFooter[] = {};
79
80 #elif BUILDFLAG(IS_MAC)
81
82 constexpr Instruction kRequiredBody[] = {kBrk0, kHlt0};
83 // Some clangs emit a BRK #1 for __builtin_unreachable(), but some do not, so
84 // it is allowed but not required to occur.
85 constexpr Instruction kOptionalFooter[] = {kBrk1};
86
87 #else
88
89 constexpr Instruction kRequiredBody[] = {kBrk0, kHlt0};
90 constexpr Instruction kOptionalFooter[] = {};
91
92 #endif
93
94 #endif
95
96 // This function loads a shared library that defines two functions,
97 // TestFunction1 and TestFunction2. It then returns the bytes of the body of
98 // whichever of those functions happens to come first in the library.
GetTestFunctionInstructions(std::vector<Instruction> * body)99 void GetTestFunctionInstructions(std::vector<Instruction>* body) {
100 FilePath helper_library_path;
101 #if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_FUCHSIA)
102 // On Android M, DIR_EXE == /system/bin when running base_unittests.
103 // On Fuchsia, NativeLibrary understands the native convention that libraries
104 // are not colocated with the binary.
105 ASSERT_TRUE(PathService::Get(DIR_EXE, &helper_library_path));
106 #endif
107 helper_library_path = helper_library_path.AppendASCII(
108 GetNativeLibraryName("immediate_crash_test_helper"));
109 #if BUILDFLAG(IS_ANDROID) && defined(COMPONENT_BUILD)
110 helper_library_path = helper_library_path.ReplaceExtension(".cr.so");
111 #endif
112 ScopedNativeLibrary helper_library(helper_library_path);
113 ASSERT_TRUE(helper_library.is_valid())
114 << "shared library load failed: "
115 << helper_library.GetError()->ToString();
116
117 void* a = helper_library.GetFunctionPointer("TestFunction1");
118 ASSERT_TRUE(a);
119 void* b = helper_library.GetFunctionPointer("TestFunction2");
120 ASSERT_TRUE(b);
121
122 #if defined(ARCH_CPU_ARMEL)
123 // Routines loaded from a shared library will have the LSB in the pointer set
124 // if encoded as T32 instructions. The rest of this test assumes T32.
125 ASSERT_TRUE(reinterpret_cast<uintptr_t>(a) & 0x1)
126 << "Expected T32 opcodes but found A32 opcodes instead.";
127 ASSERT_TRUE(reinterpret_cast<uintptr_t>(b) & 0x1)
128 << "Expected T32 opcodes but found A32 opcodes instead.";
129
130 // Mask off the lowest bit.
131 a = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(a) & ~uintptr_t{0x1});
132 b = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(b) & ~uintptr_t{0x1});
133 #endif
134
135 // There are two identical test functions starting at a and b, which may
136 // occur in the library in either order. Grab whichever one comes first,
137 // and use the address of the other to figure out where it ends.
138 const Instruction* const start = static_cast<Instruction*>(std::min(a, b));
139 const Instruction* const end = static_cast<Instruction*>(std::max(a, b));
140
141 for (const Instruction& instruction : make_span(start, end))
142 body->push_back(instruction);
143 }
144
ExpectImmediateCrashInvocation(std::vector<Instruction> instructions)145 absl::optional<std::vector<Instruction>> ExpectImmediateCrashInvocation(
146 std::vector<Instruction> instructions) {
147 auto iter = instructions.begin();
148 for (const auto inst : kRequiredBody) {
149 if (iter == instructions.end())
150 return absl::nullopt;
151 EXPECT_EQ(inst, *iter);
152 iter++;
153 }
154 return absl::make_optional(
155 std::vector<Instruction>(iter, instructions.end()));
156 }
157
MaybeSkipOptionalFooter(std::vector<Instruction> instructions)158 std::vector<Instruction> MaybeSkipOptionalFooter(
159 std::vector<Instruction> instructions) {
160 auto iter = instructions.begin();
161 for (const auto inst : kOptionalFooter) {
162 if (iter == instructions.end() || *iter != inst)
163 break;
164 iter++;
165 }
166 return std::vector<Instruction>(iter, instructions.end());
167 }
168
169 #if BUILDFLAG(USE_CLANG_COVERAGE) || BUILDFLAG(CLANG_PROFILING)
MatchPrefix(const std::vector<Instruction> & haystack,const base::span<const Instruction> & needle)170 bool MatchPrefix(const std::vector<Instruction>& haystack,
171 const base::span<const Instruction>& needle) {
172 for (size_t i = 0; i < needle.size(); i++) {
173 if (i >= haystack.size() || needle[i] != haystack[i])
174 return false;
175 }
176 return true;
177 }
178
DropUntilMatch(std::vector<Instruction> haystack,const base::span<const Instruction> & needle)179 std::vector<Instruction> DropUntilMatch(
180 std::vector<Instruction> haystack,
181 const base::span<const Instruction>& needle) {
182 while (!haystack.empty() && !MatchPrefix(haystack, needle))
183 haystack.erase(haystack.begin());
184 return haystack;
185 }
186 #endif // USE_CLANG_COVERAGE || BUILDFLAG(CLANG_PROFILING)
187
MaybeSkipCoverageHook(std::vector<Instruction> instructions)188 std::vector<Instruction> MaybeSkipCoverageHook(
189 std::vector<Instruction> instructions) {
190 #if BUILDFLAG(USE_CLANG_COVERAGE) || BUILDFLAG(CLANG_PROFILING)
191 // Warning: it is not illegal for the entirety of the expected crash sequence
192 // to appear as a subsequence of the coverage hook code. If that happens, this
193 // code will falsely exit early, having not found the real expected crash
194 // sequence, so this may not adequately ensure that the immediate crash
195 // sequence is present. We do check when not under coverage, at least.
196 return DropUntilMatch(instructions, base::make_span(kRequiredBody));
197 #else
198 return instructions;
199 #endif // USE_CLANG_COVERAGE || BUILDFLAG(CLANG_PROFILING)
200 }
201
202 } // namespace
203
204 // Attempts to verify the actual instructions emitted by ImmediateCrash().
205 // While the test results are highly implementation-specific, this allows macro
206 // changes (e.g. CLs like https://crrev.com/671123) to be verified using the
207 // trybots/waterfall, without having to build and disassemble Chrome on
208 // multiple platforms. This makes it easier to evaluate changes to
209 // ImmediateCrash() against its requirements (e.g. size of emitted sequence,
210 // whether or not multiple ImmediateCrash sequences can be folded together, et
211 // cetera). Please see immediate_crash.h for more details about the
212 // requirements.
213 //
214 // Note that C++ provides no way to get the size of a function. Instead, the
215 // test relies on a shared library which defines only two functions and assumes
216 // the two functions will be laid out contiguously as a heuristic for finding
217 // the size of the function.
TEST(ImmediateCrashTest,ExpectedOpcodeSequence)218 TEST(ImmediateCrashTest, ExpectedOpcodeSequence) {
219 std::vector<Instruction> body;
220 ASSERT_NO_FATAL_FAILURE(GetTestFunctionInstructions(&body));
221 SCOPED_TRACE(HexEncode(body.data(), body.size() * sizeof(Instruction)));
222
223 auto it = ranges::find(body, kRet);
224 ASSERT_NE(body.end(), it) << "Failed to find return opcode";
225 it++;
226
227 body = std::vector<Instruction>(it, body.end());
228 absl::optional<std::vector<Instruction>> result = MaybeSkipCoverageHook(body);
229 result = ExpectImmediateCrashInvocation(result.value());
230 result = MaybeSkipOptionalFooter(result.value());
231 result = MaybeSkipCoverageHook(result.value());
232 result = ExpectImmediateCrashInvocation(result.value());
233 ASSERT_TRUE(result);
234 }
235
236 } // namespace base
237