OSDN Git Service

Made llvm-cfi-verify not execute unit tests on non-x86 builds.
[android-x86/external-llvm.git] / tools / llvm-cfi-verify / lib / FileAnalysis.h
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
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"
37
38 #include <functional>
39 #include <set>
40 #include <string>
41 #include <unordered_map>
42
43 namespace llvm {
44 namespace cfi_verify {
45
46 // Disassembler and analysis tool for machine code files. Keeps track of non-
47 // sequential control flows, including indirect control flow instructions.
48 class FileAnalysis {
49 public:
50   // A metadata struct for an instruction.
51   struct Instr {
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
56                 // undefined.
57   };
58
59   // Construct a FileAnalysis from a file path.
60   static Expected<FileAnalysis> Create(StringRef Filename);
61
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;
68
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;
72
73   // Returns the instruction at the provided adress, dying if the instruction is
74   // not found.
75   const Instr &getInstructionOrDie(uint64_t Address) const;
76
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;
82
83   // Returns whether this instruction is used by CFI to trap the program.
84   bool isCFITrap(const Instr &InstrMeta) const;
85
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
89   // branches or calls.
90   bool canFallThrough(const Instr &InstrMeta) const;
91
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
98   // file.
99   const Instr *getDefiniteNextInstruction(const Instr &InstrMeta) const;
100
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;
106
107   // Returns whether this instruction uses a register operand.
108   bool usesRegisterOperand(const Instr &InstrMeta) const;
109
110   // Returns the list of indirect instructions.
111   const std::set<uint64_t> &getIndirectInstructions() const;
112
113   const MCRegisterInfo *getRegisterInfo() const;
114   const MCInstrInfo *getMCInstrInfo() const;
115   const MCInstrAnalysis *getMCInstrAnalysis() const;
116
117 protected:
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);
122
123   // Add an instruction to this object.
124   void addInstruction(const Instr &Instruction);
125
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);
130
131   // Constructs and initialises members required for disassembly.
132   Error initialiseDisassemblyMembers();
133
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();
137
138 private:
139   // Members that describe the input file.
140   object::OwningBinary<object::Binary> Binary;
141   const object::ObjectFile *Object = nullptr;
142   Triple ObjectTriple;
143   std::string ArchName;
144   std::string MCPU;
145   const Target *ObjectTarget = nullptr;
146   SubtargetFeatures Features;
147
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;
158
159   // A mapping between the virtual memory address to the instruction metadata
160   // struct.
161   std::map<uint64_t, Instr> Instructions;
162
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;
166
167   // A list of addresses of indirect control flow instructions.
168   std::set<uint64_t> IndirectInstructions;
169 };
170
171 class UnsupportedDisassembly : public ErrorInfo<UnsupportedDisassembly> {
172 public:
173   static char ID;
174   std::string Text;
175
176   UnsupportedDisassembly(StringRef Text);
177
178   void log(raw_ostream &OS) const override;
179   std::error_code convertToErrorCode() const override;
180 };
181
182 } // namespace cfi_verify
183 } // namespace llvm
184
185 #endif // LLVM_CFI_VERIFY_FILE_ANALYSIS_H