blob: b27c78ec9a7e3fe21fad33601c3f4eb9ce843802 [file] [log] [blame]
Daniel Chenged0471b2019-05-10 11:43:361// Copyright 2019 The Chromium 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#include "base/immediate_crash.h"
6
7#include <stdint.h>
8
Daniel Chengf355d632019-05-14 17:25:309#include "base/base_paths.h"
Elly Fong-Jones664e25f2020-10-13 21:35:3510#include "base/clang_profiling_buildflags.h"
Elly Fong-Jonese32032202020-09-25 02:08:1511#include "base/compiler_specific.h"
Daniel Chenged0471b2019-05-10 11:43:3612#include "base/containers/span.h"
13#include "base/files/file_path.h"
Daniel Chenged0471b2019-05-10 11:43:3614#include "base/optional.h"
Daniel Chengf355d632019-05-14 17:25:3015#include "base/path_service.h"
Anton Bikineeva61fb572020-10-18 08:54:4416#include "base/ranges/algorithm.h"
Elly Fong-Jonese32032202020-09-25 02:08:1517#include "base/scoped_native_library.h"
Daniel Chenged0471b2019-05-10 11:43:3618#include "base/strings/string_number_conversions.h"
19#include "build/build_config.h"
Elly Fong-Jones664e25f2020-10-13 21:35:3520#include "build/buildflag.h"
Daniel Chenged0471b2019-05-10 11:43:3621#include "testing/gtest/include/gtest/gtest.h"
22
23namespace base {
24
Elly Fong-Jonese32032202020-09-25 02:08:1525namespace {
26
Daniel Cheng69359e92019-06-20 23:43:0227// Compile test.
Elly Fong-Jonese32032202020-09-25 02:08:1528int ALLOW_UNUSED_TYPE TestImmediateCrashTreatedAsNoReturn() {
Daniel Cheng69359e92019-06-20 23:43:0229 IMMEDIATE_CRASH();
30}
31
Elly Fong-Jonese32032202020-09-25 02:08:1532#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.
36using Instruction = uint8_t;
Daniel Chenged0471b2019-05-10 11:43:3637
Elly Fong-Jonese32032202020-09-25 02:08:1538// 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â„¢.
43constexpr Instruction kRet = 0xc3;
44// INT3 ; UD2
45constexpr Instruction kRequiredBody[] = {0xcc, 0x0f, 0x0b};
46constexpr Instruction kOptionalFooter[] = {};
47
48#elif defined(ARCH_CPU_ARMEL)
49using Instruction = uint16_t;
50
51// T32 opcode reference: https://developer.arm.com/docs/ddi0487/latest
52// Actually BX LR, canonical encoding:
53constexpr Instruction kRet = 0x4770;
54// BKPT #0; UDF #0
55constexpr Instruction kRequiredBody[] = {0xbe00, 0xde00};
56constexpr Instruction kOptionalFooter[] = {};
57
58#elif defined(ARCH_CPU_ARM64)
59using 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.
65enum {
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 defined(OS_WIN)
76
77constexpr Instruction kRequiredBody[] = {kBrkF000, kBrk1};
78constexpr Instruction kOptionalFooter[] = {};
79
80#elif defined(OS_MAC)
81
82constexpr 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.
85constexpr Instruction kOptionalFooter[] = {kBrk1};
86
87#else
88
89constexpr Instruction kRequiredBody[] = {kBrk0, kHlt0};
90constexpr 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.
99void GetTestFunctionInstructions(std::vector<Instruction>* body) {
Daniel Chengf355d632019-05-14 17:25:30100 FilePath helper_library_path;
101#if !defined(OS_ANDROID) && !defined(OS_FUCHSIA)
Reid Klecknerc55cd142019-07-23 00:38:17102 // 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.
Daniel Chengf355d632019-05-14 17:25:30105 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"));
Daniel Cheng79bb3cf22019-05-16 19:15:29109#if defined(OS_ANDROID) && defined(COMPONENT_BUILD)
110 helper_library_path = helper_library_path.ReplaceExtension(".cr.so");
111#endif
Elly Fong-Jonese32032202020-09-25 02:08:15112 ScopedNativeLibrary helper_library(helper_library_path);
113 ASSERT_TRUE(helper_library.is_valid())
114 << "shared library load failed: "
115 << helper_library.GetError()->ToString();
Daniel Chenged0471b2019-05-10 11:43:36116
Elly Fong-Jonese32032202020-09-25 02:08:15117 void* a = helper_library.GetFunctionPointer("TestFunction1");
Daniel Chenged0471b2019-05-10 11:43:36118 ASSERT_TRUE(a);
Elly Fong-Jonese32032202020-09-25 02:08:15119 void* b = helper_library.GetFunctionPointer("TestFunction2");
Daniel Chenged0471b2019-05-10 11:43:36120 ASSERT_TRUE(b);
121
Elly Fong-Jonese32032202020-09-25 02:08:15122#if defined(ARCH_CPU_ARMEL)
Daniel Chenged0471b2019-05-10 11:43:36123 // 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});
Elly Fong-Jonese32032202020-09-25 02:08:15133#endif
Daniel Chenged0471b2019-05-10 11:43:36134
Elly Fong-Jonese32032202020-09-25 02:08:15135 // 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));
Daniel Chenged0471b2019-05-10 11:43:36140
Elly Fong-Jonese32032202020-09-25 02:08:15141 for (const Instruction& instruction : make_span(start, end))
142 body->push_back(instruction);
Daniel Chenged0471b2019-05-10 11:43:36143}
144
Elly Fong-Jonese32032202020-09-25 02:08:15145base::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 base::nullopt;
151 EXPECT_EQ(inst, *iter);
152 iter++;
153 }
154 return base::make_optional(
155 std::vector<Instruction>(iter, instructions.end()));
156}
157
158std::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
Elly Fong-Jones664e25f2020-10-13 21:35:35169#if BUILDFLAG(USE_CLANG_COVERAGE)
170bool 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
179std::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
187
188std::vector<Instruction> MaybeSkipCoverageHook(
189 std::vector<Instruction> instructions) {
190#if BUILDFLAG(USE_CLANG_COVERAGE)
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
200}
201
Elly Fong-Jonese32032202020-09-25 02:08:15202} // namespace
203
204// Checks that the IMMEDIATE_CRASH() macro produces specific instructions; see
205// comments in immediate_crash.h for the requirements.
206TEST(ImmediateCrashTest, ExpectedOpcodeSequence) {
207 std::vector<Instruction> body;
208 ASSERT_NO_FATAL_FAILURE(GetTestFunctionInstructions(&body));
209 SCOPED_TRACE(HexEncode(body.data(), body.size() * sizeof(Instruction)));
210
Anton Bikineeva61fb572020-10-18 08:54:44211 auto it = ranges::find(body, kRet);
Elly Fong-Jonese32032202020-09-25 02:08:15212 ASSERT_NE(body.end(), it) << "Failed to find return opcode";
213 it++;
214
215 body = std::vector<Instruction>(it, body.end());
Elly Fong-Jones664e25f2020-10-13 21:35:35216 base::Optional<std::vector<Instruction>> result = MaybeSkipCoverageHook(body);
217 result = ExpectImmediateCrashInvocation(result.value());
Elly Fong-Jonese32032202020-09-25 02:08:15218 result = MaybeSkipOptionalFooter(result.value());
Elly Fong-Jones664e25f2020-10-13 21:35:35219 result = MaybeSkipCoverageHook(result.value());
Elly Fong-Jonese32032202020-09-25 02:08:15220 result = ExpectImmediateCrashInvocation(result.value());
221 ASSERT_TRUE(result);
222}
Daniel Chenged0471b2019-05-10 11:43:36223
224} // namespace base