Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 1 | //===- FileAnalysis.h -------------------------------------------*- C++ -*-===// |
| 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 | #ifndef LLVM_CFI_VERIFY_FILE_ANALYSIS_H |
| 11 | #define LLVM_CFI_VERIFY_FILE_ANALYSIS_H |
| 12 | |
Mitch Phillips | 99fa140 | 2017-10-23 20:25:19 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseMap.h" |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 14 | #include "llvm/BinaryFormat/ELF.h" |
Mitch Phillips | 7db6f7a | 2017-10-31 23:20:05 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCAsmInfo.h" |
| 17 | #include "llvm/MC/MCContext.h" |
| 18 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 19 | #include "llvm/MC/MCInst.h" |
| 20 | #include "llvm/MC/MCInstPrinter.h" |
| 21 | #include "llvm/MC/MCInstrAnalysis.h" |
| 22 | #include "llvm/MC/MCInstrDesc.h" |
| 23 | #include "llvm/MC/MCInstrInfo.h" |
| 24 | #include "llvm/MC/MCObjectFileInfo.h" |
| 25 | #include "llvm/MC/MCRegisterInfo.h" |
| 26 | #include "llvm/MC/MCSubtargetInfo.h" |
| 27 | #include "llvm/Object/Binary.h" |
| 28 | #include "llvm/Object/COFF.h" |
| 29 | #include "llvm/Object/ELFObjectFile.h" |
| 30 | #include "llvm/Object/ObjectFile.h" |
| 31 | #include "llvm/Support/Casting.h" |
| 32 | #include "llvm/Support/CommandLine.h" |
| 33 | #include "llvm/Support/Error.h" |
| 34 | #include "llvm/Support/MemoryBuffer.h" |
| 35 | #include "llvm/Support/TargetRegistry.h" |
| 36 | #include "llvm/Support/TargetSelect.h" |
| 37 | #include "llvm/Support/raw_ostream.h" |
| 38 | |
| 39 | #include <functional> |
| 40 | #include <set> |
| 41 | #include <string> |
| 42 | #include <unordered_map> |
| 43 | |
| 44 | namespace llvm { |
| 45 | namespace cfi_verify { |
| 46 | |
| 47 | // Disassembler and analysis tool for machine code files. Keeps track of non- |
| 48 | // sequential control flows, including indirect control flow instructions. |
| 49 | class FileAnalysis { |
| 50 | public: |
| 51 | // A metadata struct for an instruction. |
| 52 | struct Instr { |
| 53 | uint64_t VMAddress; // Virtual memory address of this instruction. |
| 54 | MCInst Instruction; // Instruction. |
| 55 | uint64_t InstructionSize; // Size of this instruction. |
| 56 | bool Valid; // Is this a valid instruction? If false, Instr::Instruction is |
| 57 | // undefined. |
| 58 | }; |
| 59 | |
| 60 | // Construct a FileAnalysis from a file path. |
| 61 | static Expected<FileAnalysis> Create(StringRef Filename); |
| 62 | |
| 63 | // Construct and take ownership of the supplied object. Do not use this |
| 64 | // constructor, prefer to use FileAnalysis::Create instead. |
| 65 | FileAnalysis(object::OwningBinary<object::Binary> Binary); |
| 66 | FileAnalysis() = delete; |
| 67 | FileAnalysis(const FileAnalysis &) = delete; |
| 68 | FileAnalysis(FileAnalysis &&Other) = default; |
| 69 | |
Mitch Phillips | 5ff01cd | 2017-10-25 21:21:16 +0000 | [diff] [blame] | 70 | // Check whether the provided instruction is CFI protected in this file. |
| 71 | // Returns false if this instruction doesn't exist in this file, if it's not |
| 72 | // an indirect control flow instruction, or isn't CFI protected. Returns true |
| 73 | // otherwise. |
| 74 | bool isIndirectInstructionCFIProtected(uint64_t Address) const; |
| 75 | |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 76 | // Returns the instruction at the provided address. Returns nullptr if there |
| 77 | // is no instruction at the provided address. |
| 78 | const Instr *getInstruction(uint64_t Address) const; |
| 79 | |
| 80 | // Returns the instruction at the provided adress, dying if the instruction is |
| 81 | // not found. |
| 82 | const Instr &getInstructionOrDie(uint64_t Address) const; |
| 83 | |
| 84 | // Returns a pointer to the previous/next instruction in sequence, |
| 85 | // respectively. Returns nullptr if the next/prev instruction doesn't exist, |
| 86 | // or if the provided instruction doesn't exist. |
| 87 | const Instr *getPrevInstructionSequential(const Instr &InstrMeta) const; |
| 88 | const Instr *getNextInstructionSequential(const Instr &InstrMeta) const; |
| 89 | |
Vlad Tsyrklevich | 0ee2632 | 2017-10-11 23:17:29 +0000 | [diff] [blame] | 90 | // Returns whether this instruction is used by CFI to trap the program. |
| 91 | bool isCFITrap(const Instr &InstrMeta) const; |
| 92 | |
| 93 | // Returns whether this function can fall through to the next instruction. |
| 94 | // Undefined (and bad) instructions cannot fall through, and instruction that |
| 95 | // modify the control flow can only fall through if they are conditional |
| 96 | // branches or calls. |
| 97 | bool canFallThrough(const Instr &InstrMeta) const; |
| 98 | |
| 99 | // Returns the definitive next instruction. This is different from the next |
| 100 | // instruction sequentially as it will follow unconditional branches (assuming |
| 101 | // they can be resolved at compile time, i.e. not indirect). This method |
| 102 | // returns nullptr if the provided instruction does not transfer control flow |
| 103 | // to exactly one instruction that is known deterministically at compile time. |
| 104 | // Also returns nullptr if the deterministic target does not exist in this |
| 105 | // file. |
| 106 | const Instr *getDefiniteNextInstruction(const Instr &InstrMeta) const; |
| 107 | |
| 108 | // Get a list of deterministic control flows that lead to the provided |
| 109 | // instruction. This list includes all static control flow cross-references as |
| 110 | // well as the previous instruction if it can fall through. |
| 111 | std::set<const Instr *> |
| 112 | getDirectControlFlowXRefs(const Instr &InstrMeta) const; |
| 113 | |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 114 | // Returns whether this instruction uses a register operand. |
| 115 | bool usesRegisterOperand(const Instr &InstrMeta) const; |
| 116 | |
| 117 | // Returns the list of indirect instructions. |
| 118 | const std::set<uint64_t> &getIndirectInstructions() const; |
| 119 | |
| 120 | const MCRegisterInfo *getRegisterInfo() const; |
| 121 | const MCInstrInfo *getMCInstrInfo() const; |
| 122 | const MCInstrAnalysis *getMCInstrAnalysis() const; |
| 123 | |
Mitch Phillips | 7db6f7a | 2017-10-31 23:20:05 +0000 | [diff] [blame] | 124 | // Returns true if this class is using DWARF line tables for elimination. |
| 125 | bool hasLineTableInfo() const; |
| 126 | |
| 127 | // Returns the line table information for the range {Address +- |
| 128 | // DWARFSearchRange}. Returns an empty table if the address has no valid line |
| 129 | // table information, or this analysis object has DWARF handling disabled. |
| 130 | DILineInfoTable getLineInfoForAddressRange(uint64_t Address); |
| 131 | |
| 132 | // Returns whether the provided address has valid line information for |
| 133 | // instructions in the range of Address +- DWARFSearchRange. |
| 134 | bool hasValidLineInfoForAddressRange(uint64_t Address); |
| 135 | |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 136 | protected: |
| 137 | // Construct a blank object with the provided triple and features. Used in |
| 138 | // testing, where a sub class will dependency inject protected methods to |
| 139 | // allow analysis of raw binary, without requiring a fully valid ELF file. |
| 140 | FileAnalysis(const Triple &ObjectTriple, const SubtargetFeatures &Features); |
| 141 | |
| 142 | // Add an instruction to this object. |
| 143 | void addInstruction(const Instr &Instruction); |
| 144 | |
| 145 | // Disassemble and parse the provided bytes into this object. Instruction |
| 146 | // address calculation is done relative to the provided SectionAddress. |
| 147 | void parseSectionContents(ArrayRef<uint8_t> SectionBytes, |
| 148 | uint64_t SectionAddress); |
| 149 | |
| 150 | // Constructs and initialises members required for disassembly. |
| 151 | Error initialiseDisassemblyMembers(); |
| 152 | |
| 153 | // Parses code sections from the internal object file. Saves them into the |
| 154 | // internal members. Should only be called once by Create(). |
| 155 | Error parseCodeSections(); |
| 156 | |
| 157 | private: |
| 158 | // Members that describe the input file. |
| 159 | object::OwningBinary<object::Binary> Binary; |
| 160 | const object::ObjectFile *Object = nullptr; |
| 161 | Triple ObjectTriple; |
| 162 | std::string ArchName; |
| 163 | std::string MCPU; |
| 164 | const Target *ObjectTarget = nullptr; |
| 165 | SubtargetFeatures Features; |
| 166 | |
| 167 | // Members required for disassembly. |
| 168 | std::unique_ptr<const MCRegisterInfo> RegisterInfo; |
| 169 | std::unique_ptr<const MCAsmInfo> AsmInfo; |
| 170 | std::unique_ptr<MCSubtargetInfo> SubtargetInfo; |
| 171 | std::unique_ptr<const MCInstrInfo> MII; |
| 172 | MCObjectFileInfo MOFI; |
| 173 | std::unique_ptr<MCContext> Context; |
| 174 | std::unique_ptr<const MCDisassembler> Disassembler; |
| 175 | std::unique_ptr<const MCInstrAnalysis> MIA; |
| 176 | std::unique_ptr<MCInstPrinter> Printer; |
| 177 | |
Mitch Phillips | 7db6f7a | 2017-10-31 23:20:05 +0000 | [diff] [blame] | 178 | // DWARF debug information. |
| 179 | std::unique_ptr<DWARFContext> DWARF; |
| 180 | |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 181 | // A mapping between the virtual memory address to the instruction metadata |
Mitch Phillips | 7db6f7a | 2017-10-31 23:20:05 +0000 | [diff] [blame] | 182 | // struct. TODO(hctim): Reimplement this as a sorted vector to avoid per- |
| 183 | // insertion allocation. |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 184 | std::map<uint64_t, Instr> Instructions; |
| 185 | |
| 186 | // Contains a mapping between a specific address, and a list of instructions |
| 187 | // that use this address as a branch target (including call instructions). |
Mitch Phillips | 99fa140 | 2017-10-23 20:25:19 +0000 | [diff] [blame] | 188 | DenseMap<uint64_t, std::vector<uint64_t>> StaticBranchTargetings; |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 189 | |
| 190 | // A list of addresses of indirect control flow instructions. |
| 191 | std::set<uint64_t> IndirectInstructions; |
| 192 | }; |
| 193 | |
| 194 | class UnsupportedDisassembly : public ErrorInfo<UnsupportedDisassembly> { |
| 195 | public: |
| 196 | static char ID; |
Mitch Phillips | d9af383 | 2017-10-23 20:54:01 +0000 | [diff] [blame] | 197 | std::string Text; |
| 198 | |
| 199 | UnsupportedDisassembly(StringRef Text); |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame] | 200 | |
| 201 | void log(raw_ostream &OS) const override; |
| 202 | std::error_code convertToErrorCode() const override; |
| 203 | }; |
| 204 | |
| 205 | } // namespace cfi_verify |
| 206 | } // namespace llvm |
| 207 | |
| 208 | #endif // LLVM_CFI_VERIFY_FILE_ANALYSIS_H |