1 //===- FileAnalysis.h -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CFI_VERIFY_FILE_ANALYSIS_H
11 #define LLVM_CFI_VERIFY_FILE_ANALYSIS_H
13 #include "llvm/ADT/DenseMap.h"
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"
41 #include <unordered_map>
44 namespace cfi_verify {
46 // Disassembler and analysis tool for machine code files. Keeps track of non-
47 // sequential control flows, including indirect control flow instructions.
50 // A metadata struct for an instruction.
52 uint64_t VMAddress; // Virtual memory address of this instruction.
53 MCInst Instruction; // Instruction.
54 uint64_t InstructionSize; // Size of this instruction.
55 bool Valid; // Is this a valid instruction? If false, Instr::Instruction is
59 // Construct a FileAnalysis from a file path.
60 static Expected<FileAnalysis> Create(StringRef Filename);
62 // Construct and take ownership of the supplied object. Do not use this
63 // constructor, prefer to use FileAnalysis::Create instead.
64 FileAnalysis(object::OwningBinary<object::Binary> Binary);
65 FileAnalysis() = delete;
66 FileAnalysis(const FileAnalysis &) = delete;
67 FileAnalysis(FileAnalysis &&Other) = default;
69 // Returns the instruction at the provided address. Returns nullptr if there
70 // is no instruction at the provided address.
71 const Instr *getInstruction(uint64_t Address) const;
73 // Returns the instruction at the provided adress, dying if the instruction is
75 const Instr &getInstructionOrDie(uint64_t Address) const;
77 // Returns a pointer to the previous/next instruction in sequence,
78 // respectively. Returns nullptr if the next/prev instruction doesn't exist,
79 // or if the provided instruction doesn't exist.
80 const Instr *getPrevInstructionSequential(const Instr &InstrMeta) const;
81 const Instr *getNextInstructionSequential(const Instr &InstrMeta) const;
83 // Returns whether this instruction is used by CFI to trap the program.
84 bool isCFITrap(const Instr &InstrMeta) const;
86 // Returns whether this function can fall through to the next instruction.
87 // Undefined (and bad) instructions cannot fall through, and instruction that
88 // modify the control flow can only fall through if they are conditional
90 bool canFallThrough(const Instr &InstrMeta) const;
92 // Returns the definitive next instruction. This is different from the next
93 // instruction sequentially as it will follow unconditional branches (assuming
94 // they can be resolved at compile time, i.e. not indirect). This method
95 // returns nullptr if the provided instruction does not transfer control flow
96 // to exactly one instruction that is known deterministically at compile time.
97 // Also returns nullptr if the deterministic target does not exist in this
99 const Instr *getDefiniteNextInstruction(const Instr &InstrMeta) const;
101 // Get a list of deterministic control flows that lead to the provided
102 // instruction. This list includes all static control flow cross-references as
103 // well as the previous instruction if it can fall through.
104 std::set<const Instr *>
105 getDirectControlFlowXRefs(const Instr &InstrMeta) const;
107 // Returns whether this instruction uses a register operand.
108 bool usesRegisterOperand(const Instr &InstrMeta) const;
110 // Returns the list of indirect instructions.
111 const std::set<uint64_t> &getIndirectInstructions() const;
113 const MCRegisterInfo *getRegisterInfo() const;
114 const MCInstrInfo *getMCInstrInfo() const;
115 const MCInstrAnalysis *getMCInstrAnalysis() const;
118 // Construct a blank object with the provided triple and features. Used in
119 // testing, where a sub class will dependency inject protected methods to
120 // allow analysis of raw binary, without requiring a fully valid ELF file.
121 FileAnalysis(const Triple &ObjectTriple, const SubtargetFeatures &Features);
123 // Add an instruction to this object.
124 void addInstruction(const Instr &Instruction);
126 // Disassemble and parse the provided bytes into this object. Instruction
127 // address calculation is done relative to the provided SectionAddress.
128 void parseSectionContents(ArrayRef<uint8_t> SectionBytes,
129 uint64_t SectionAddress);
131 // Constructs and initialises members required for disassembly.
132 Error initialiseDisassemblyMembers();
134 // Parses code sections from the internal object file. Saves them into the
135 // internal members. Should only be called once by Create().
136 Error parseCodeSections();
139 // Members that describe the input file.
140 object::OwningBinary<object::Binary> Binary;
141 const object::ObjectFile *Object = nullptr;
143 std::string ArchName;
145 const Target *ObjectTarget = nullptr;
146 SubtargetFeatures Features;
148 // Members required for disassembly.
149 std::unique_ptr<const MCRegisterInfo> RegisterInfo;
150 std::unique_ptr<const MCAsmInfo> AsmInfo;
151 std::unique_ptr<MCSubtargetInfo> SubtargetInfo;
152 std::unique_ptr<const MCInstrInfo> MII;
153 MCObjectFileInfo MOFI;
154 std::unique_ptr<MCContext> Context;
155 std::unique_ptr<const MCDisassembler> Disassembler;
156 std::unique_ptr<const MCInstrAnalysis> MIA;
157 std::unique_ptr<MCInstPrinter> Printer;
159 // A mapping between the virtual memory address to the instruction metadata
161 std::map<uint64_t, Instr> Instructions;
163 // Contains a mapping between a specific address, and a list of instructions
164 // that use this address as a branch target (including call instructions).
165 DenseMap<uint64_t, std::vector<uint64_t>> StaticBranchTargetings;
167 // A list of addresses of indirect control flow instructions.
168 std::set<uint64_t> IndirectInstructions;
171 class UnsupportedDisassembly : public ErrorInfo<UnsupportedDisassembly> {
176 UnsupportedDisassembly(StringRef Text);
178 void log(raw_ostream &OS) const override;
179 std::error_code convertToErrorCode() const override;
182 } // namespace cfi_verify
185 #endif // LLVM_CFI_VERIFY_FILE_ANALYSIS_H