Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 1 | // 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 Cheng | f355d63 | 2019-05-14 17:25:30 | [diff] [blame] | 9 | #include "base/base_paths.h" |
Elly Fong-Jones | 664e25f | 2020-10-13 21:35:35 | [diff] [blame] | 10 | #include "base/clang_profiling_buildflags.h" |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 12 | #include "base/containers/span.h" |
| 13 | #include "base/files/file_path.h" |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 14 | #include "base/optional.h" |
Daniel Cheng | f355d63 | 2019-05-14 17:25:30 | [diff] [blame] | 15 | #include "base/path_service.h" |
Anton Bikineev | a61fb57 | 2020-10-18 08:54:44 | [diff] [blame^] | 16 | #include "base/ranges/algorithm.h" |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 17 | #include "base/scoped_native_library.h" |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 18 | #include "base/strings/string_number_conversions.h" |
| 19 | #include "build/build_config.h" |
Elly Fong-Jones | 664e25f | 2020-10-13 21:35:35 | [diff] [blame] | 20 | #include "build/buildflag.h" |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
| 23 | namespace base { |
| 24 | |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 25 | namespace { |
| 26 | |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 27 | // Compile test. |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 28 | int ALLOW_UNUSED_TYPE TestImmediateCrashTreatedAsNoReturn() { |
Daniel Cheng | 69359e9 | 2019-06-20 23:43:02 | [diff] [blame] | 29 | IMMEDIATE_CRASH(); |
| 30 | } |
| 31 | |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 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; |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 37 | |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 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 { |
| 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 | |
| 77 | constexpr Instruction kRequiredBody[] = {kBrkF000, kBrk1}; |
| 78 | constexpr Instruction kOptionalFooter[] = {}; |
| 79 | |
| 80 | #elif defined(OS_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. |
| 99 | void GetTestFunctionInstructions(std::vector<Instruction>* body) { |
Daniel Cheng | f355d63 | 2019-05-14 17:25:30 | [diff] [blame] | 100 | FilePath helper_library_path; |
| 101 | #if !defined(OS_ANDROID) && !defined(OS_FUCHSIA) |
Reid Kleckner | c55cd14 | 2019-07-23 00:38:17 | [diff] [blame] | 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. |
Daniel Cheng | f355d63 | 2019-05-14 17:25:30 | [diff] [blame] | 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")); |
Daniel Cheng | 79bb3cf2 | 2019-05-16 19:15:29 | [diff] [blame] | 109 | #if defined(OS_ANDROID) && defined(COMPONENT_BUILD) |
| 110 | helper_library_path = helper_library_path.ReplaceExtension(".cr.so"); |
| 111 | #endif |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 112 | ScopedNativeLibrary helper_library(helper_library_path); |
| 113 | ASSERT_TRUE(helper_library.is_valid()) |
| 114 | << "shared library load failed: " |
| 115 | << helper_library.GetError()->ToString(); |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 116 | |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 117 | void* a = helper_library.GetFunctionPointer("TestFunction1"); |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 118 | ASSERT_TRUE(a); |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 119 | void* b = helper_library.GetFunctionPointer("TestFunction2"); |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 120 | ASSERT_TRUE(b); |
| 121 | |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 122 | #if defined(ARCH_CPU_ARMEL) |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 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}); |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 133 | #endif |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 134 | |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 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)); |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 140 | |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 141 | for (const Instruction& instruction : make_span(start, end)) |
| 142 | body->push_back(instruction); |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 143 | } |
| 144 | |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 145 | base::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 | |
| 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 | |
Elly Fong-Jones | 664e25f | 2020-10-13 21:35:35 | [diff] [blame] | 169 | #if BUILDFLAG(USE_CLANG_COVERAGE) |
| 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 | |
| 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 |
| 187 | |
| 188 | std::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-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 202 | } // namespace |
| 203 | |
| 204 | // Checks that the IMMEDIATE_CRASH() macro produces specific instructions; see |
| 205 | // comments in immediate_crash.h for the requirements. |
| 206 | TEST(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 Bikineev | a61fb57 | 2020-10-18 08:54:44 | [diff] [blame^] | 211 | auto it = ranges::find(body, kRet); |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 212 | ASSERT_NE(body.end(), it) << "Failed to find return opcode"; |
| 213 | it++; |
| 214 | |
| 215 | body = std::vector<Instruction>(it, body.end()); |
Elly Fong-Jones | 664e25f | 2020-10-13 21:35:35 | [diff] [blame] | 216 | base::Optional<std::vector<Instruction>> result = MaybeSkipCoverageHook(body); |
| 217 | result = ExpectImmediateCrashInvocation(result.value()); |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 218 | result = MaybeSkipOptionalFooter(result.value()); |
Elly Fong-Jones | 664e25f | 2020-10-13 21:35:35 | [diff] [blame] | 219 | result = MaybeSkipCoverageHook(result.value()); |
Elly Fong-Jones | e3203220 | 2020-09-25 02:08:15 | [diff] [blame] | 220 | result = ExpectImmediateCrashInvocation(result.value()); |
| 221 | ASSERT_TRUE(result); |
| 222 | } |
Daniel Cheng | ed0471b | 2019-05-10 11:43:36 | [diff] [blame] | 223 | |
| 224 | } // namespace base |