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() { |
| 66 | if (Analysis.initialiseDisassemblyMembers()) { |
| 67 | FAIL() << "Failed to initialise FileAnalysis."; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | ELFx86TestFileAnalysis Analysis; |
| 72 | }; |
| 73 | |
| 74 | TEST_F(BasicFileAnalysisTest, BasicDisassemblyTraversalTest) { |
| 75 | Analysis.parseSectionContents( |
| 76 | { |
| 77 | 0x90, // 0: nop |
| 78 | 0xb0, 0x00, // 1: mov $0x0, %al |
| 79 | 0x48, 0x89, 0xe5, // 3: mov %rsp, %rbp |
| 80 | 0x48, 0x83, 0xec, 0x18, // 6: sub $0x18, %rsp |
| 81 | 0x48, 0xbe, 0xc4, 0x07, 0x40, |
| 82 | 0x00, 0x00, 0x00, 0x00, 0x00, // 10: movabs $0x4007c4, %rsi |
| 83 | 0x2f, // 20: (bad) |
| 84 | 0x41, 0x0e, // 21: rex.B (bad) |
| 85 | 0x62, 0x72, 0x65, 0x61, 0x6b, // 23: (bad) {%k1} |
| 86 | }, |
| 87 | 0xDEADBEEF); |
| 88 | |
| 89 | EXPECT_EQ(nullptr, Analysis.getInstruction(0x0)); |
| 90 | EXPECT_EQ(nullptr, Analysis.getInstruction(0x1000)); |
| 91 | |
| 92 | // 0xDEADBEEF: nop |
| 93 | const auto *InstrMeta = Analysis.getInstruction(0xDEADBEEF); |
| 94 | EXPECT_NE(nullptr, InstrMeta); |
| 95 | EXPECT_EQ(0xDEADBEEF, InstrMeta->VMAddress); |
| 96 | EXPECT_EQ(1u, InstrMeta->InstructionSize); |
| 97 | EXPECT_TRUE(InstrMeta->Valid); |
| 98 | |
| 99 | const auto *NextInstrMeta = Analysis.getNextInstructionSequential(*InstrMeta); |
| 100 | EXPECT_EQ(nullptr, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 101 | const auto *PrevInstrMeta = InstrMeta; |
| 102 | |
| 103 | // 0xDEADBEEF + 1: mov $0x0, %al |
| 104 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 1); |
| 105 | EXPECT_NE(nullptr, InstrMeta); |
| 106 | EXPECT_EQ(NextInstrMeta, InstrMeta); |
| 107 | EXPECT_EQ(0xDEADBEEF + 1, InstrMeta->VMAddress); |
| 108 | EXPECT_EQ(2u, InstrMeta->InstructionSize); |
| 109 | EXPECT_TRUE(InstrMeta->Valid); |
| 110 | |
| 111 | NextInstrMeta = Analysis.getNextInstructionSequential(*InstrMeta); |
| 112 | EXPECT_EQ(PrevInstrMeta, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 113 | PrevInstrMeta = InstrMeta; |
| 114 | |
| 115 | // 0xDEADBEEF + 3: mov %rsp, %rbp |
| 116 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 3); |
| 117 | EXPECT_NE(nullptr, InstrMeta); |
| 118 | EXPECT_EQ(NextInstrMeta, InstrMeta); |
| 119 | EXPECT_EQ(0xDEADBEEF + 3, InstrMeta->VMAddress); |
| 120 | EXPECT_EQ(3u, InstrMeta->InstructionSize); |
| 121 | EXPECT_TRUE(InstrMeta->Valid); |
| 122 | |
| 123 | NextInstrMeta = Analysis.getNextInstructionSequential(*InstrMeta); |
| 124 | EXPECT_EQ(PrevInstrMeta, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 125 | PrevInstrMeta = InstrMeta; |
| 126 | |
| 127 | // 0xDEADBEEF + 6: sub $0x18, %rsp |
| 128 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 6); |
| 129 | EXPECT_NE(nullptr, InstrMeta); |
| 130 | EXPECT_EQ(NextInstrMeta, InstrMeta); |
| 131 | EXPECT_EQ(0xDEADBEEF + 6, InstrMeta->VMAddress); |
| 132 | EXPECT_EQ(4u, InstrMeta->InstructionSize); |
| 133 | EXPECT_TRUE(InstrMeta->Valid); |
| 134 | |
| 135 | NextInstrMeta = Analysis.getNextInstructionSequential(*InstrMeta); |
| 136 | EXPECT_EQ(PrevInstrMeta, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 137 | PrevInstrMeta = InstrMeta; |
| 138 | |
| 139 | // 0xDEADBEEF + 10: movabs $0x4007c4, %rsi |
| 140 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 10); |
| 141 | EXPECT_NE(nullptr, InstrMeta); |
| 142 | EXPECT_EQ(NextInstrMeta, InstrMeta); |
| 143 | EXPECT_EQ(0xDEADBEEF + 10, InstrMeta->VMAddress); |
| 144 | EXPECT_EQ(10u, InstrMeta->InstructionSize); |
| 145 | EXPECT_TRUE(InstrMeta->Valid); |
| 146 | |
| 147 | EXPECT_EQ(nullptr, Analysis.getNextInstructionSequential(*InstrMeta)); |
| 148 | EXPECT_EQ(PrevInstrMeta, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 149 | PrevInstrMeta = InstrMeta; |
| 150 | |
| 151 | // 0xDEADBEEF + 20: (bad) |
| 152 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 20); |
| 153 | EXPECT_NE(nullptr, InstrMeta); |
| 154 | EXPECT_EQ(0xDEADBEEF + 20, InstrMeta->VMAddress); |
| 155 | EXPECT_EQ(1u, InstrMeta->InstructionSize); |
| 156 | EXPECT_FALSE(InstrMeta->Valid); |
| 157 | |
| 158 | EXPECT_EQ(nullptr, Analysis.getNextInstructionSequential(*InstrMeta)); |
| 159 | EXPECT_EQ(PrevInstrMeta, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 160 | |
| 161 | // 0xDEADBEEF + 21: rex.B (bad) |
| 162 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 21); |
| 163 | EXPECT_NE(nullptr, InstrMeta); |
| 164 | EXPECT_EQ(0xDEADBEEF + 21, InstrMeta->VMAddress); |
| 165 | EXPECT_EQ(2u, InstrMeta->InstructionSize); |
| 166 | EXPECT_FALSE(InstrMeta->Valid); |
| 167 | |
| 168 | EXPECT_EQ(nullptr, Analysis.getNextInstructionSequential(*InstrMeta)); |
| 169 | EXPECT_EQ(nullptr, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 170 | |
| 171 | // 0xDEADBEEF + 6: (bad) {%k1} |
| 172 | InstrMeta = Analysis.getInstruction(0xDEADBEEF + 23); |
| 173 | EXPECT_NE(nullptr, InstrMeta); |
| 174 | EXPECT_EQ(0xDEADBEEF + 23, InstrMeta->VMAddress); |
| 175 | EXPECT_EQ(5u, InstrMeta->InstructionSize); |
| 176 | EXPECT_FALSE(InstrMeta->Valid); |
| 177 | |
| 178 | EXPECT_EQ(nullptr, Analysis.getNextInstructionSequential(*InstrMeta)); |
| 179 | EXPECT_EQ(nullptr, Analysis.getPrevInstructionSequential(*InstrMeta)); |
| 180 | } |
| 181 | |
| 182 | TEST_F(BasicFileAnalysisTest, PrevAndNextFromBadInst) { |
| 183 | Analysis.parseSectionContents( |
| 184 | { |
| 185 | 0x90, // 0: nop |
| 186 | 0x2f, // 1: (bad) |
| 187 | 0x90 // 2: nop |
| 188 | }, |
| 189 | 0xDEADBEEF); |
| 190 | const auto &BadInstrMeta = Analysis.getInstructionOrDie(0xDEADBEEF + 1); |
| 191 | const auto *GoodInstrMeta = |
| 192 | Analysis.getPrevInstructionSequential(BadInstrMeta); |
| 193 | EXPECT_NE(nullptr, GoodInstrMeta); |
| 194 | EXPECT_EQ(0xDEADBEEF, GoodInstrMeta->VMAddress); |
| 195 | EXPECT_EQ(1u, GoodInstrMeta->InstructionSize); |
| 196 | |
| 197 | GoodInstrMeta = Analysis.getNextInstructionSequential(BadInstrMeta); |
| 198 | EXPECT_NE(nullptr, GoodInstrMeta); |
| 199 | EXPECT_EQ(0xDEADBEEF + 2, GoodInstrMeta->VMAddress); |
| 200 | EXPECT_EQ(1u, GoodInstrMeta->InstructionSize); |
| 201 | } |
| 202 | |
Vlad Tsyrklevich | 0ee2632 | 2017-10-11 23:17:29 +0000 | [diff] [blame^] | 203 | TEST_F(BasicFileAnalysisTest, CFITrapTest) { |
| 204 | Analysis.parseSectionContents( |
| 205 | { |
| 206 | 0x90, // 0: nop |
| 207 | 0xb0, 0x00, // 1: mov $0x0, %al |
| 208 | 0x48, 0x89, 0xe5, // 3: mov %rsp, %rbp |
| 209 | 0x48, 0x83, 0xec, 0x18, // 6: sub $0x18, %rsp |
| 210 | 0x48, 0xbe, 0xc4, 0x07, 0x40, |
| 211 | 0x00, 0x00, 0x00, 0x00, 0x00, // 10: movabs $0x4007c4, %rsi |
| 212 | 0x2f, // 20: (bad) |
| 213 | 0x41, 0x0e, // 21: rex.B (bad) |
| 214 | 0x62, 0x72, 0x65, 0x61, 0x6b, // 23: (bad) {%k1} |
| 215 | 0x0f, 0x0b // 28: ud2 |
| 216 | }, |
| 217 | 0xDEADBEEF); |
| 218 | |
| 219 | EXPECT_FALSE(Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF))); |
| 220 | EXPECT_FALSE( |
| 221 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 3))); |
| 222 | EXPECT_FALSE( |
| 223 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 6))); |
| 224 | EXPECT_FALSE( |
| 225 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 10))); |
| 226 | EXPECT_FALSE( |
| 227 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 20))); |
| 228 | EXPECT_FALSE( |
| 229 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 21))); |
| 230 | EXPECT_FALSE( |
| 231 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 23))); |
| 232 | EXPECT_TRUE( |
| 233 | Analysis.isCFITrap(Analysis.getInstructionOrDie(0xDEADBEEF + 28))); |
| 234 | } |
| 235 | |
| 236 | TEST_F(BasicFileAnalysisTest, FallThroughTest) { |
| 237 | Analysis.parseSectionContents( |
| 238 | { |
| 239 | 0x90, // 0: nop |
| 240 | 0xb0, 0x00, // 1: mov $0x0, %al |
| 241 | 0x2f, // 3: (bad) |
| 242 | 0x0f, 0x0b, // 4: ud2 |
| 243 | 0xff, 0x20, // 6: jmpq *(%rax) |
| 244 | 0xeb, 0x00, // 8: jmp +0 |
| 245 | 0xe8, 0x45, 0xfe, 0xff, 0xff, // 10: callq [some loc] |
| 246 | 0xff, 0x10, // 15: callq *(rax) |
| 247 | 0x75, 0x00, // 17: jne +0 |
| 248 | 0xc3, // 19: retq |
| 249 | }, |
| 250 | 0xDEADBEEF); |
| 251 | |
| 252 | EXPECT_TRUE( |
| 253 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF))); |
| 254 | EXPECT_TRUE( |
| 255 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 1))); |
| 256 | EXPECT_FALSE( |
| 257 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 3))); |
| 258 | EXPECT_FALSE( |
| 259 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 4))); |
| 260 | EXPECT_FALSE( |
| 261 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 6))); |
| 262 | EXPECT_FALSE( |
| 263 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 8))); |
| 264 | EXPECT_FALSE( |
| 265 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 10))); |
| 266 | EXPECT_FALSE( |
| 267 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 15))); |
| 268 | EXPECT_TRUE( |
| 269 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 17))); |
| 270 | EXPECT_FALSE( |
| 271 | Analysis.canFallThrough(Analysis.getInstructionOrDie(0xDEADBEEF + 19))); |
| 272 | } |
| 273 | |
| 274 | TEST_F(BasicFileAnalysisTest, DefiniteNextInstructionTest) { |
| 275 | Analysis.parseSectionContents( |
| 276 | { |
| 277 | 0x90, // 0: nop |
| 278 | 0xb0, 0x00, // 1: mov $0x0, %al |
| 279 | 0x2f, // 3: (bad) |
| 280 | 0x0f, 0x0b, // 4: ud2 |
| 281 | 0xff, 0x20, // 6: jmpq *(%rax) |
| 282 | 0xeb, 0x00, // 8: jmp 10 [+0] |
| 283 | 0xeb, 0x05, // 10: jmp 17 [+5] |
| 284 | 0xe8, 0x00, 0x00, 0x00, 0x00, // 12: callq 17 [+0] |
| 285 | 0xe8, 0x78, 0x56, 0x34, 0x12, // 17: callq 0x1234569f [+0x12345678] |
| 286 | 0xe8, 0x04, 0x00, 0x00, 0x00, // 22: callq 31 [+4] |
| 287 | 0xff, 0x10, // 27: callq *(rax) |
| 288 | 0x75, 0x00, // 29: jne 31 [+0] |
| 289 | 0x75, 0xe0, // 31: jne 1 [-32] |
| 290 | 0xc3, // 33: retq |
| 291 | 0xeb, 0xdd, // 34: jmp 1 [-35] |
| 292 | 0xeb, 0xdd, // 36: jmp 3 [-35] |
| 293 | 0xeb, 0xdc, // 38: jmp 4 [-36] |
| 294 | }, |
| 295 | 0xDEADBEEF); |
| 296 | |
| 297 | const auto *Current = Analysis.getInstruction(0xDEADBEEF); |
| 298 | const auto *Next = Analysis.getDefiniteNextInstruction(*Current); |
| 299 | EXPECT_NE(nullptr, Next); |
| 300 | EXPECT_EQ(0xDEADBEEF + 1, Next->VMAddress); |
| 301 | |
| 302 | Current = Analysis.getInstruction(0xDEADBEEF + 1); |
| 303 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 304 | |
| 305 | Current = Analysis.getInstruction(0xDEADBEEF + 3); |
| 306 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 307 | |
| 308 | Current = Analysis.getInstruction(0xDEADBEEF + 4); |
| 309 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 310 | |
| 311 | Current = Analysis.getInstruction(0xDEADBEEF + 6); |
| 312 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 313 | |
| 314 | Current = Analysis.getInstruction(0xDEADBEEF + 8); |
| 315 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 316 | EXPECT_NE(nullptr, Next); |
| 317 | EXPECT_EQ(0xDEADBEEF + 10, Next->VMAddress); |
| 318 | |
| 319 | Current = Analysis.getInstruction(0xDEADBEEF + 10); |
| 320 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 321 | EXPECT_NE(nullptr, Next); |
| 322 | EXPECT_EQ(0xDEADBEEF + 17, Next->VMAddress); |
| 323 | |
| 324 | Current = Analysis.getInstruction(0xDEADBEEF + 12); |
| 325 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 326 | EXPECT_NE(nullptr, Next); |
| 327 | EXPECT_EQ(0xDEADBEEF + 17, Next->VMAddress); |
| 328 | |
| 329 | Current = Analysis.getInstruction(0xDEADBEEF + 17); |
| 330 | // Note, definite next instruction address is out of range and should fail. |
| 331 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 332 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 333 | |
| 334 | Current = Analysis.getInstruction(0xDEADBEEF + 22); |
| 335 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 336 | EXPECT_NE(nullptr, Next); |
| 337 | EXPECT_EQ(0xDEADBEEF + 31, Next->VMAddress); |
| 338 | |
| 339 | Current = Analysis.getInstruction(0xDEADBEEF + 27); |
| 340 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 341 | Current = Analysis.getInstruction(0xDEADBEEF + 29); |
| 342 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 343 | Current = Analysis.getInstruction(0xDEADBEEF + 31); |
| 344 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 345 | Current = Analysis.getInstruction(0xDEADBEEF + 33); |
| 346 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 347 | |
| 348 | Current = Analysis.getInstruction(0xDEADBEEF + 34); |
| 349 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 350 | EXPECT_NE(nullptr, Next); |
| 351 | EXPECT_EQ(0xDEADBEEF + 1, Next->VMAddress); |
| 352 | |
| 353 | Current = Analysis.getInstruction(0xDEADBEEF + 36); |
| 354 | EXPECT_EQ(nullptr, Analysis.getDefiniteNextInstruction(*Current)); |
| 355 | |
| 356 | Current = Analysis.getInstruction(0xDEADBEEF + 38); |
| 357 | Next = Analysis.getDefiniteNextInstruction(*Current); |
| 358 | EXPECT_NE(nullptr, Next); |
| 359 | EXPECT_EQ(0xDEADBEEF + 4, Next->VMAddress); |
| 360 | } |
| 361 | |
| 362 | TEST_F(BasicFileAnalysisTest, ControlFlowXRefsTest) { |
| 363 | Analysis.parseSectionContents( |
| 364 | { |
| 365 | 0x90, // 0: nop |
| 366 | 0xb0, 0x00, // 1: mov $0x0, %al |
| 367 | 0x2f, // 3: (bad) |
| 368 | 0x0f, 0x0b, // 4: ud2 |
| 369 | 0xff, 0x20, // 6: jmpq *(%rax) |
| 370 | 0xeb, 0x00, // 8: jmp 10 [+0] |
| 371 | 0xeb, 0x05, // 10: jmp 17 [+5] |
| 372 | 0xe8, 0x00, 0x00, 0x00, 0x00, // 12: callq 17 [+0] |
| 373 | 0xe8, 0x78, 0x56, 0x34, 0x12, // 17: callq 0x1234569f [+0x12345678] |
| 374 | 0xe8, 0x04, 0x00, 0x00, 0x00, // 22: callq 31 [+4] |
| 375 | 0xff, 0x10, // 27: callq *(rax) |
| 376 | 0x75, 0x00, // 29: jne 31 [+0] |
| 377 | 0x75, 0xe0, // 31: jne 1 [-32] |
| 378 | 0xc3, // 33: retq |
| 379 | 0xeb, 0xdd, // 34: jmp 1 [-35] |
| 380 | 0xeb, 0xdd, // 36: jmp 3 [-35] |
| 381 | 0xeb, 0xdc, // 38: jmp 4 [-36] |
| 382 | }, |
| 383 | 0xDEADBEEF); |
| 384 | const auto *InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF); |
| 385 | std::set<const Instr *> XRefs = |
| 386 | Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 387 | EXPECT_TRUE(XRefs.empty()); |
| 388 | |
| 389 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 1); |
| 390 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 391 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 392 | Field(&Instr::VMAddress, Eq(0xDEADBEEF)), |
| 393 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 31)), |
| 394 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 34)))); |
| 395 | |
| 396 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 3); |
| 397 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 398 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 399 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 1)), |
| 400 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 36)))); |
| 401 | |
| 402 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 4); |
| 403 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 404 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 405 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 38)))); |
| 406 | |
| 407 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 6); |
| 408 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 409 | |
| 410 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 8); |
| 411 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 412 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 413 | |
| 414 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 10); |
| 415 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 416 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 417 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 8)))); |
| 418 | |
| 419 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 12); |
| 420 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 421 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 422 | |
| 423 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 17); |
| 424 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 425 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 426 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 10)), |
| 427 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 12)))); |
| 428 | |
| 429 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 22); |
| 430 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 431 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 432 | |
| 433 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 27); |
| 434 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 435 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 436 | |
| 437 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 29); |
| 438 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 439 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 440 | |
| 441 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 31); |
| 442 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 443 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 444 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 22)), |
| 445 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 29)))); |
| 446 | |
| 447 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 33); |
| 448 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 449 | EXPECT_THAT(XRefs, UnorderedElementsAre( |
| 450 | Field(&Instr::VMAddress, Eq(0xDEADBEEF + 31)))); |
| 451 | |
| 452 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 34); |
| 453 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 454 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 455 | |
| 456 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 36); |
| 457 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 458 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 459 | |
| 460 | InstrMetaPtr = &Analysis.getInstructionOrDie(0xDEADBEEF + 38); |
| 461 | XRefs = Analysis.getDirectControlFlowXRefs(*InstrMetaPtr); |
| 462 | EXPECT_TRUE(Analysis.getDirectControlFlowXRefs(*InstrMetaPtr).empty()); |
| 463 | } |
| 464 | |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 465 | } // anonymous namespace |
| 466 | } // end namespace cfi_verify |
| 467 | } // end namespace llvm |
| 468 | |
| 469 | int main(int argc, char **argv) { |
| 470 | ::testing::InitGoogleTest(&argc, argv); |
| 471 | llvm::cl::ParseCommandLineOptions(argc, argv); |
| 472 | |
| 473 | llvm::InitializeAllTargetInfos(); |
| 474 | llvm::InitializeAllTargetMCs(); |
| 475 | llvm::InitializeAllAsmParsers(); |
| 476 | llvm::InitializeAllDisassemblers(); |
| 477 | |
| 478 | return RUN_ALL_TESTS(); |
| 479 | } |