Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 1 | //===- llvm/unittests/tools/llvm-cfi-verify/FileAnalysis.cpp --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "../tools/llvm-cfi-verify/lib/FileAnalysis.h" |
| 11 | #include "gmock/gmock.h" |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | #include "llvm/BinaryFormat/ELF.h" |
| 15 | #include "llvm/MC/MCAsmInfo.h" |
| 16 | #include "llvm/MC/MCContext.h" |
| 17 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 18 | #include "llvm/MC/MCInst.h" |
| 19 | #include "llvm/MC/MCInstPrinter.h" |
| 20 | #include "llvm/MC/MCInstrAnalysis.h" |
| 21 | #include "llvm/MC/MCInstrDesc.h" |
| 22 | #include "llvm/MC/MCInstrInfo.h" |
| 23 | #include "llvm/MC/MCObjectFileInfo.h" |
| 24 | #include "llvm/MC/MCRegisterInfo.h" |
| 25 | #include "llvm/MC/MCSubtargetInfo.h" |
| 26 | #include "llvm/Object/Binary.h" |
| 27 | #include "llvm/Object/COFF.h" |
| 28 | #include "llvm/Object/ELFObjectFile.h" |
| 29 | #include "llvm/Object/ObjectFile.h" |
| 30 | #include "llvm/Support/Casting.h" |
| 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Error.h" |
| 33 | #include "llvm/Support/MemoryBuffer.h" |
| 34 | #include "llvm/Support/TargetRegistry.h" |
| 35 | #include "llvm/Support/TargetSelect.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
| 37 | |
| 38 | #include <cstdlib> |
| 39 | |
| 40 | using Instr = ::llvm::cfi_verify::FileAnalysis::Instr; |
| 41 | using ::testing::Eq; |
Vlad Tsyrklevich | 0ee2632 | 2017-10-11 23:17:29 +0000 | [diff] [blame] | 42 | using ::testing::Field; |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 43 | |
| 44 | namespace llvm { |
| 45 | namespace cfi_verify { |
| 46 | namespace { |
| 47 | class ELFx86TestFileAnalysis : public FileAnalysis { |
| 48 | public: |
| 49 | ELFx86TestFileAnalysis() |
| 50 | : FileAnalysis(Triple("x86_64--"), SubtargetFeatures()) {} |
| 51 | |
| 52 | // Expose this method publicly for testing. |
| 53 | void parseSectionContents(ArrayRef<uint8_t> SectionBytes, |
| 54 | uint64_t SectionAddress) { |
| 55 | FileAnalysis::parseSectionContents(SectionBytes, SectionAddress); |
| 56 | } |
| 57 | |
| 58 | Error initialiseDisassemblyMembers() { |
| 59 | return FileAnalysis::initialiseDisassemblyMembers(); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | class BasicFileAnalysisTest : public ::testing::Test { |
| 64 | protected: |
| 65 | virtual void SetUp() { |
Mitch Phillips | d9af383 | 2017-10-23 20:54:01 +0000 | [diff] [blame] | 66 | SuccessfullyInitialised = true; |
| 67 | if (auto Err = Analysis.initialiseDisassemblyMembers()) { |
| 68 | handleAllErrors(std::move(Err), [&](const UnsupportedDisassembly &E) { |
| 69 | SuccessfullyInitialised = false; |
| 70 | outs() |
| 71 | << "Note: CFIVerifyTests are disabled due to lack of x86 support " |
| 72 | "on this build.\n"; |
| 73 | }); |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Mitch Phillips | d9af383 | 2017-10-23 20:54:01 +0000 | [diff] [blame] | 77 | bool SuccessfullyInitialised; |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 78 | ELFx86TestFileAnalysis Analysis; |
| 79 | }; |
| 80 | |
| 81 | TEST_F(BasicFileAnalysisTest, BasicDisassemblyTraversalTest) { |
Mitch Phillips | d9af383 | 2017-10-23 20:54:01 +0000 | [diff] [blame] | 82 | if (!SuccessfullyInitialised) |
| 83 | return; |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 84 | Analysis.parseSectionContents( |
| 85 | { |
| 86 | 0x90, // 0: nop |
| 87 | 0xb0, 0x00, // 1: mov $0x0, %al |
| 88 | 0x48, 0x89, 0xe5, // 3: mov %rsp, %rbp |
| 89 | 0x48, 0x83, 0xec, 0x18, // 6: sub $0x18, %rsp |
| 90 | 0x48, 0xbe, 0xc4, 0x07, 0x40, |
| 91 | 0x00, 0x00, 0x00, 0x00, 0x00, // 10: movabs $0x4007c4, %rsi |
| 92 | 0x2f, // 20: (bad) |
| 93 | 0x41, 0x0e, // 21: rex.B (bad) |
| 94 | 0x62, 0x72, 0x65, 0x61, 0x6b, // 23: (bad) {%k1} |
| 95 | }, |
| 96 | 0xDEADBEEF); |
| 97 | |
| 98 | EXPECT_EQ(nullptr, Analysis.getInstruction(0x0)); |
| 99 | EXPECT_EQ(nullptr, Analysis.getInstruction(0x1000)); |
| 100 | |
| 101 | // 0xDEADBEEF: nop |
| 102 | const auto *InstrMeta = Analysis.getInstruction(0xDEADBEEF); |
| 103 | EXPECT_NE(nullptr, InstrMeta); |
| 104 | EXPECT_EQ(0xDEADBEEF, InstrMeta->VMAddress); |
| 105 | EXPECT_EQ(1u, InstrMeta->InstructionSize); |
| 106 | EXPECT_TRUE(InstrMeta->Valid); |
| 107 | |
| 108 | const auto *NextInstrMeta = Analysis.getNextInstructionSequential(*InstrMeta); |
| 109 | EXPECT_EQ(nullptr, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 110 | const auto *PrevInstrMeta = InstrMeta; |
| 111 | |
| 112 | // 0xDEADBEEF + 1: mov $0x0, %al |
| 113 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 1); |
| 114 | EXPECT_NE(nullptr, InstrMeta); |
| 115 | EXPECT_EQ(NextInstrMeta, InstrMeta); |
| 116 | EXPECT_EQ(0xDEADBEEF + 1, InstrMeta->VMAddress); |
| 117 | EXPECT_EQ(2u, InstrMeta->InstructionSize); |
| 118 | EXPECT_TRUE(InstrMeta->Valid); |
| 119 | |
| 120 | NextInstrMeta = Analysis.getNextInstructionSequential(*InstrMeta); |
| 121 | EXPECT_EQ(PrevInstrMeta, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 122 | PrevInstrMeta = InstrMeta; |
| 123 | |
| 124 | // 0xDEADBEEF + 3: mov %rsp, %rbp |
| 125 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 3); |
| 126 | EXPECT_NE(nullptr, InstrMeta); |
| 127 | EXPECT_EQ(NextInstrMeta, InstrMeta); |
| 128 | EXPECT_EQ(0xDEADBEEF + 3, InstrMeta->VMAddress); |
| 129 | EXPECT_EQ(3u, InstrMeta->InstructionSize); |
| 130 | EXPECT_TRUE(InstrMeta->Valid); |
| 131 | |
| 132 | NextInstrMeta = Analysis.getNextInstructionSequential(*InstrMeta); |
| 133 | EXPECT_EQ(PrevInstrMeta, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 134 | PrevInstrMeta = InstrMeta; |
| 135 | |
| 136 | // 0xDEADBEEF + 6: sub $0x18, %rsp |
| 137 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 6); |
| 138 | EXPECT_NE(nullptr, InstrMeta); |
| 139 | EXPECT_EQ(NextInstrMeta, InstrMeta); |
| 140 | EXPECT_EQ(0xDEADBEEF + 6, InstrMeta->VMAddress); |
| 141 | EXPECT_EQ(4u, InstrMeta->InstructionSize); |
| 142 | EXPECT_TRUE(InstrMeta->Valid); |
| 143 | |
| 144 | NextInstrMeta = Analysis.getNextInstructionSequential(*InstrMeta); |
| 145 | EXPECT_EQ(PrevInstrMeta, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 146 | PrevInstrMeta = InstrMeta; |
| 147 | |
| 148 | // 0xDEADBEEF + 10: movabs $0x4007c4, %rsi |
| 149 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 10); |
| 150 | EXPECT_NE(nullptr, InstrMeta); |
| 151 | EXPECT_EQ(NextInstrMeta, InstrMeta); |
| 152 | EXPECT_EQ(0xDEADBEEF + 10, InstrMeta->VMAddress); |
| 153 | EXPECT_EQ(10u, InstrMeta->InstructionSize); |
| 154 | EXPECT_TRUE(InstrMeta->Valid); |
| 155 | |
| 156 | EXPECT_EQ(nullptr, Analysis.getNextInstructionSequential(*InstrMeta)); |
| 157 | EXPECT_EQ(PrevInstrMeta, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 158 | PrevInstrMeta = InstrMeta; |
| 159 | |
| 160 | // 0xDEADBEEF + 20: (bad) |
| 161 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 20); |
| 162 | EXPECT_NE(nullptr, InstrMeta); |
| 163 | EXPECT_EQ(0xDEADBEEF + 20, InstrMeta->VMAddress); |
| 164 | EXPECT_EQ(1u, InstrMeta->InstructionSize); |
| 165 | EXPECT_FALSE(InstrMeta->Valid); |
| 166 | |
| 167 | EXPECT_EQ(nullptr, Analysis.getNextInstructionSequential(*InstrMeta)); |
| 168 | EXPECT_EQ(PrevInstrMeta, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 169 | |
| 170 | // 0xDEADBEEF + 21: rex.B (bad) |
| 171 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 21); |
| 172 | EXPECT_NE(nullptr, InstrMeta); |
| 173 | EXPECT_EQ(0xDEADBEEF + 21, InstrMeta->VMAddress); |
| 174 | EXPECT_EQ(2u, InstrMeta->InstructionSize); |
| 175 | EXPECT_FALSE(InstrMeta->Valid); |
| 176 | |
| 177 | EXPECT_EQ(nullptr, Analysis.getNextInstructionSequential(*InstrMeta)); |
| 178 | EXPECT_EQ(nullptr, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 179 | |
| 180 | // 0xDEADBEEF + 6: (bad) {%k1} |
| 181 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 23); |
| 182 | EXPECT_NE(nullptr, InstrMeta); |
| 183 | EXPECT_EQ(0xDEADBEEF + 23, InstrMeta->VMAddress); |
| 184 | EXPECT_EQ(5u, InstrMeta->InstructionSize); |
| 185 | EXPECT_FALSE(InstrMeta->Valid); |
| 186 | |
| 187 | EXPECT_EQ(nullptr, Analysis.getNextInstructionSequential(*InstrMeta)); |
| 188 | EXPECT_EQ(nullptr, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 189 | } |
| 190 | |
| 191 | TEST_F(BasicFileAnalysisTest, PrevAndNextFromBadInst) { |
Mitch Phillips | d9af383 | 2017-10-23 20:54:01 +0000 | [diff] [blame] | 192 | if (!SuccessfullyInitialised) |
| 193 | return; |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 194 | Analysis.parseSectionContents( |
| 195 | { |
| 196 | 0x90, // 0: nop |
| 197 | 0x2f, // 1: (bad) |
| 198 | 0x90 // 2: nop |
| 199 | }, |
| 200 | 0xDEADBEEF); |
| 201 | const auto &BadInstrMeta = Analysis.getInstructionOrDie(0xDEADBEEF + 1); |
| 202 | const auto *GoodInstrMeta = |
| 203 | Analysis.getPrevInstructionSequential(BadInstrMeta); |
| 204 | EXPECT_NE(nullptr, GoodInstrMeta); |
| 205 | EXPECT_EQ(0xDEADBEEF, GoodInstrMeta->VMAddress); |
| 206 | EXPECT_EQ(1u, GoodInstrMeta->InstructionSize); |
| 207 | |
| 208 | GoodInstrMeta = Analysis.getNextInstructionSequential(BadInstrMeta); |
| 209 | EXPECT_NE(nullptr, GoodInstrMeta); |
| 210 | EXPECT_EQ(0xDEADBEEF + 2, GoodInstrMeta->VMAddress); |
| 211 | EXPECT_EQ(1u, GoodInstrMeta->InstructionSize); |
| 212 | } |
| 213 | |
Vlad Tsyrklevich | 0ee2632 | 2017-10-11 23:17:29 +0000 | [diff] [blame] | 214 | TEST_F(BasicFileAnalysisTest, CFITrapTest) { |
Mitch Phillips | d9af383 | 2017-10-23 20:54:01 +0000 | [diff] [blame] | 215 | if (!SuccessfullyInitialised) |
| 216 | return; |
Vlad Tsyrklevich | 0ee2632 | 2017-10-11 23:17:29 +0000 | [diff] [blame] | 217 | Analysis.parseSectionContents( |
| 218 | { |
| 219 | 0x90, // 0: nop |
| 220 | 0xb0, 0x00, // 1: mov $0x0, %al |
| 221 | 0x48, 0x89, 0xe5, // 3: mov %rsp, %rbp |
| 222 | 0x48, 0x83, 0xec, 0x18, // 6: sub $0x18, %rsp |
| 223 | 0x48, 0xbe, 0xc4, 0x07, 0x40, |
| 224 | 0x00, 0x00, 0x00, 0x00, 0x00, // 10: movabs $0x4007c4, %rsi |
| 225 | 0x2f, // 20: (bad) |
| 226 | 0x41, 0x0e, // 21: rex.B (bad) |
| 227 | 0x62, 0x72, 0x65, 0x61, 0x6b, // 23: (bad) {%k1} |
| 228 | 0x0f, 0x0b // 28: ud2 |
| 229 | }, |
| 230 | 0xDEADBEEF); |
| 231 | |
| 232 | EXPECT_FALSE(Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF))); |
| 233 | EXPECT_FALSE( |
| 234 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 3))); |
| 235 | EXPECT_FALSE( |
| 236 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 6))); |
| 237 | EXPECT_FALSE( |
| 238 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 10))); |
| 239 | EXPECT_FALSE( |
| 240 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 20))); |
| 241 | EXPECT_FALSE( |
| 242 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 21))); |
| 243 | EXPECT_FALSE( |
| 244 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 23))); |
| 245 | EXPECT_TRUE( |
| 246 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 28))); |
| 247 | } |
| 248 | |
| 249 | TEST_F(BasicFileAnalysisTest, FallThroughTest) { |
Mitch Phillips | d9af383 | 2017-10-23 20:54:01 +0000 | [diff] [blame] | 250 | if (!SuccessfullyInitialised) |
| 251 | return; |
Vlad Tsyrklevich | 0ee2632 | 2017-10-11 23:17:29 +0000 | [diff] [blame] | 252 | Analysis.parseSectionContents( |
| 253 | { |
| 254 | 0x90, // 0: nop |
| 255 | 0xb0, 0x00, // 1: mov $0x0, %al |
| 256 | 0x2f, // 3: (bad) |
| 257 | 0x0f, 0x0b, // 4: ud2 |
| 258 | 0xff, 0x20, // 6: jmpq *(%rax) |
| 259 | 0xeb, 0x00, // 8: jmp +0 |
| 260 | 0xe8, 0x45, 0xfe, 0xff, 0xff, // 10: callq [some loc] |
| 261 | 0xff, 0x10, // 15: callq *(rax) |
| 262 | 0x75, 0x00, // 17: jne +0 |
| 263 | 0xc3, // 19: retq |
| 264 | }, |
| 265 | 0xDEADBEEF); |
| 266 | |
| 267 | EXPECT_TRUE( |
| 268 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF))); |
| 269 | EXPECT_TRUE( |
| 270 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 1))); |
| 271 | EXPECT_FALSE( |
| 272 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 3))); |
| 273 | EXPECT_FALSE( |
| 274 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 4))); |
| 275 | EXPECT_FALSE( |
| 276 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 6))); |
| 277 | EXPECT_FALSE( |
| 278 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 8))); |
| 279 | EXPECT_FALSE( |
| 280 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 10))); |
| 281 | EXPECT_FALSE( |
| 282 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 15))); |
| 283 | EXPECT_TRUE( |
| 284 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 17))); |
| 285 | EXPECT_FALSE( |
| 286 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 19))); |
| 287 | } |
| 288 | |
| 289 | TEST_F(BasicFileAnalysisTest, DefiniteNextInstructionTest) { |
Mitch Phillips | d9af383 | 2017-10-23 20:54:01 +0000 | [diff] [blame] | 290 | if (!SuccessfullyInitialised) |
| 291 | return; |
Vlad Tsyrklevich | 0ee2632 | 2017-10-11 23:17:29 +0000 | [diff] [blame] | 292 | Analysis.parseSectionContents( |
| 293 | { |
| 294 | 0x90, // 0: nop |
| 295 | 0xb0, 0x00, // 1: mov $0x0, %al |
| 296 | 0x2f, // 3: (bad) |
| 297 | 0x0f, 0x0b, // 4: ud2 |
| 298 | 0xff, 0x20, // 6: jmpq *(%rax) |
| 299 | 0xeb, 0x00, // 8: jmp 10 [+0] |
| 300 | 0xeb, 0x05, // 10: jmp 17 [+5] |
| 301 | 0xe8, 0x00, 0x00, 0x00, 0x00, // 12: callq 17 [+0] |
| 302 | 0xe8, 0x78, 0x56, 0x34, 0x12, // 17: callq 0x1234569f [+0x12345678] |
| 303 | 0xe8, 0x04, 0x00, 0x00, 0x00, // 22: callq 31 [+4] |
| 304 | 0xff, 0x10, // 27: callq *(rax) |
| 305 | 0x75, 0x00, // 29: jne 31 [+0] |
| 306 | 0x75, 0xe0, // 31: jne 1 [-32] |
| 307 | 0xc3, // 33: retq |
| 308 | 0xeb, 0xdd, // 34: jmp 1 [-35] |
| 309 | 0xeb, 0xdd, // 36: jmp 3 [-35] |
| 310 | 0xeb, 0xdc, // 38: jmp 4 [-36] |
| 311 | }, |
| 312 | 0xDEADBEEF); |
| 313 | |
| 314 | const auto *Current = Analysis.getInstruction(0xDEADBEEF); |
| 315 | const auto *Next = Analysis.getDefiniteNextInstruction(*Current); |
| 316 | EXPECT_NE(nullptr, Next); |
| 317 | EXPECT_EQ(0xDEADBEEF + 1, Next->VMAddress); |
| 318 | |
| 319 | Current = Analysis.getInstruction(0xDEADBEEF + 1); |
| 320 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 321 | |
| 322 | Current = Analysis.getInstruction(0xDEADBEEF + 3); |
| 323 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 324 | |
| 325 | Current = Analysis.getInstruction(0xDEADBEEF + 4); |
| 326 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 327 | |
| 328 | Current = Analysis.getInstruction(0xDEADBEEF + 6); |
| 329 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 330 | |
| 331 | Current = Analysis.getInstruction(0xDEADBEEF + 8); |
| 332 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 333 | EXPECT_NE(nullptr, Next); |
| 334 | EXPECT_EQ(0xDEADBEEF + 10, Next->VMAddress); |
| 335 | |
| 336 | Current = Analysis.getInstruction(0xDEADBEEF + 10); |
| 337 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 338 | EXPECT_NE(nullptr, Next); |
| 339 | EXPECT_EQ(0xDEADBEEF + 17, Next->VMAddress); |
| 340 | |
| 341 | Current = Analysis.getInstruction(0xDEADBEEF + 12); |
| 342 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 343 | EXPECT_NE(nullptr, Next); |
| 344 | EXPECT_EQ(0xDEADBEEF + 17, Next->VMAddress); |
| 345 | |
| 346 | Current = Analysis.getInstruction(0xDEADBEEF + 17); |
| 347 | // Note, definite next instruction address is out of range and should fail. |
| 348 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 349 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 350 | |
| 351 | Current = Analysis.getInstruction(0xDEADBEEF + 22); |
| 352 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 353 | EXPECT_NE(nullptr, Next); |
| 354 | EXPECT_EQ(0xDEADBEEF + 31, Next->VMAddress); |
| 355 | |
| 356 | Current = Analysis.getInstruction(0xDEADBEEF + 27); |
| 357 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 358 | Current = Analysis.getInstruction(0xDEADBEEF + 29); |
| 359 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 360 | Current = Analysis.getInstruction(0xDEADBEEF + 31); |
| 361 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 362 | Current = Analysis.getInstruction(0xDEADBEEF + 33); |
| 363 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 364 | |
| 365 | Current = Analysis.getInstruction(0xDEADBEEF + 34); |
| 366 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 367 | EXPECT_NE(nullptr, Next); |
| 368 | EXPECT_EQ(0xDEADBEEF + 1, Next->VMAddress); |
| 369 | |
| 370 | Current = Analysis.getInstruction(0xDEADBEEF + 36); |
| 371 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 372 | |
| 373 | Current = Analysis.getInstruction(0xDEADBEEF + 38); |
| 374 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 375 | EXPECT_NE(nullptr, Next); |
| 376 | EXPECT_EQ(0xDEADBEEF + 4, Next->VMAddress); |
| 377 | } |
| 378 | |
| 379 | TEST_F(BasicFileAnalysisTest, ControlFlowXRefsTest) { |
Mitch Phillips | d9af383 | 2017-10-23 20:54:01 +0000 | [diff] [blame] | 380 | if (!SuccessfullyInitialised) |
| 381 | return; |
Vlad Tsyrklevich | 0ee2632 | 2017-10-11 23:17:29 +0000 | [diff] [blame] | 382 | Analysis.parseSectionContents( |
| 383 | { |
| 384 | 0x90, // 0: nop |
| 385 | 0xb0, 0x00, // 1: mov $0x0, %al |
| 386 | 0x2f, // 3: (bad) |
| 387 | 0x0f, 0x0b, // 4: ud2 |
| 388 | 0xff, 0x20, // 6: jmpq *(%rax) |
| 389 | 0xeb, 0x00, // 8: jmp 10 [+0] |
| 390 | 0xeb, 0x05, // 10: jmp 17 [+5] |
| 391 | 0xe8, 0x00, 0x00, 0x00, 0x00, // 12: callq 17 [+0] |
| 392 | 0xe8, 0x78, 0x56, 0x34, 0x12, // 17: callq 0x1234569f [+0x12345678] |
| 393 | 0xe8, 0x04, 0x00, 0x00, 0x00, // 22: callq 31 [+4] |
| 394 | 0xff, 0x10, // 27: callq *(rax) |
| 395 | 0x75, 0x00, // 29: jne 31 [+0] |
| 396 | 0x75, 0xe0, // 31: jne 1 [-32] |
| 397 | 0xc3, // 33: retq |
| 398 | 0xeb, 0xdd, // 34: jmp 1 [-35] |
| 399 | 0xeb, 0xdd, // 36: jmp 3 [-35] |
| 400 | 0xeb, 0xdc, // 38: jmp 4 [-36] |
| 401 | }, |
| 402 | 0xDEADBEEF); |
| 403 | const auto *InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF); |
| 404 | std::set<const Instr *> XRefs = |
| 405 | Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 406 | EXPECT_TRUE(XRefs.empty()); |
| 407 | |
| 408 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 1); |
| 409 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 410 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 411 | Field(&Instr::VMAddress, Eq(0xDEADBEEF)), |
| 412 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 31)), |
| 413 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 34)))); |
| 414 | |
| 415 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 3); |
| 416 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 417 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 418 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 1)), |
| 419 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 36)))); |
| 420 | |
| 421 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 4); |
| 422 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 423 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 424 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 38)))); |
| 425 | |
| 426 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 6); |
| 427 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 428 | |
| 429 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 8); |
| 430 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 431 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 432 | |
| 433 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 10); |
| 434 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 435 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 436 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 8)))); |
| 437 | |
| 438 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 12); |
| 439 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 440 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 441 | |
| 442 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 17); |
| 443 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 444 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 445 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 10)), |
| 446 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 12)))); |
| 447 | |
| 448 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 22); |
| 449 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 450 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 451 | |
| 452 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 27); |
| 453 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 454 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 455 | |
| 456 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 29); |
| 457 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 458 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 459 | |
| 460 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 31); |
| 461 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 462 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 463 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 22)), |
| 464 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 29)))); |
| 465 | |
| 466 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 33); |
| 467 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 468 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 469 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 31)))); |
| 470 | |
| 471 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 34); |
| 472 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 473 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 474 | |
| 475 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 36); |
| 476 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 477 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 478 | |
| 479 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 38); |
| 480 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 481 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 482 | } |
| 483 | |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 484 | } // anonymous namespace |
| 485 | } // end namespace cfi_verify |
| 486 | } // end namespace llvm |
| 487 | |
| 488 | int main(int argc, char **argv) { |
| 489 | ::testing::InitGoogleTest(&argc, argv); |
| 490 | llvm::cl::ParseCommandLineOptions(argc, argv); |
| 491 | |
| 492 | llvm::InitializeAllTargetInfos(); |
| 493 | llvm::InitializeAllTargetMCs(); |
| 494 | llvm::InitializeAllAsmParsers(); |
| 495 | llvm::InitializeAllDisassemblers(); |
| 496 | |
| 497 | return RUN_ALL_TESTS(); |
| 498 | } |