blob: 2bf07b53bf6d6dc25acc4597d48367f5f7a4ada8 [file] [log] [blame]
Mitch Phillips99fa1402017-10-23 20:25:19 +00001//===- GraphBuilder.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_GRAPH_BUILDER_H
11#define LLVM_CFI_VERIFY_GRAPH_BUILDER_H
12
13#include "FileAnalysis.h"
14
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/BinaryFormat/ELF.h"
17#include "llvm/MC/MCAsmInfo.h"
18#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCDisassembler/MCDisassembler.h"
20#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstPrinter.h"
22#include "llvm/MC/MCInstrAnalysis.h"
23#include "llvm/MC/MCInstrDesc.h"
24#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCObjectFileInfo.h"
26#include "llvm/MC/MCRegisterInfo.h"
27#include "llvm/MC/MCSubtargetInfo.h"
28#include "llvm/Object/Binary.h"
29#include "llvm/Object/COFF.h"
30#include "llvm/Object/ELFObjectFile.h"
31#include "llvm/Object/ObjectFile.h"
32#include "llvm/Support/Casting.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Error.h"
35#include "llvm/Support/MemoryBuffer.h"
36#include "llvm/Support/TargetRegistry.h"
37#include "llvm/Support/TargetSelect.h"
38#include "llvm/Support/raw_ostream.h"
39
40#include <functional>
41#include <set>
42#include <string>
43#include <unordered_map>
44
45using Instr = llvm::cfi_verify::FileAnalysis::Instr;
46
47namespace llvm {
48namespace cfi_verify {
49
Mitch Phillips4ab6fc02017-11-01 23:39:41 +000050extern unsigned long long SearchLengthForUndef;
51extern unsigned long long SearchLengthForConditionalBranch;
Mitch Phillips99fa1402017-10-23 20:25:19 +000052
53struct ConditionalBranchNode {
54 uint64_t Address;
55 uint64_t Target;
56 uint64_t Fallthrough;
57 // Does this conditional branch look like it's used for CFI protection? i.e.
58 // - The exit point of a basic block whos entry point is {target|fallthrough}
59 // is a CFI trap, and...
60 // - The exit point of the other basic block is an undirect CF instruction.
61 bool CFIProtection;
Mitch Phillips2e7be2a2017-11-15 00:35:26 +000062 bool IndirectCFIsOnTargetPath;
Mitch Phillips99fa1402017-10-23 20:25:19 +000063};
64
65// The canonical graph result structure returned by GraphBuilder. The members
66// in this structure encapsulate all possible code paths to the instruction
67// located at `BaseAddress`.
68struct GraphResult {
69 uint64_t BaseAddress;
70
71 // Map between an instruction address, and the address of the next instruction
72 // that will be executed. This map will contain all keys in the range:
73 // - [orphaned node, base address)
74 // - [conditional branch node {target|fallthrough}, base address)
75 DenseMap<uint64_t, uint64_t> IntermediateNodes;
76
77 // A list of orphaned nodes. A node is an 'orphan' if it meets any of the
78 // following criteria:
79 // - The length of the path from the base to this node has exceeded
80 // `SearchLengthForConditionalBranch`.
81 // - The node has no cross references to it.
82 // - The path from the base to this node is cyclic.
83 std::vector<uint64_t> OrphanedNodes;
84
85 // A list of top-level conditional branches that exist at the top of any
86 // non-orphan paths from the base.
87 std::vector<ConditionalBranchNode> ConditionalBranchNodes;
88
89 // Returns an in-order list of the path between the address provided and the
90 // base. The provided address must be part of this graph, and must not be a
91 // conditional branch.
92 std::vector<uint64_t> flattenAddress(uint64_t Address) const;
Mitch Phillips02993892017-11-14 22:43:13 +000093
94 // Print the DOT representation of this result.
95 void printToDOT(const FileAnalysis &Analysis, raw_ostream &OS) const;
Mitch Phillips99fa1402017-10-23 20:25:19 +000096};
97
98class GraphBuilder {
99public:
100 // Build the control flow graph for a provided control flow node. This method
101 // will enumerate all branch nodes that can lead to this node, and place them
102 // into GraphResult::ConditionalBranchNodes. It will also provide any orphaned
103 // (i.e. the upwards traversal did not make it to a branch node) flows to the
104 // provided node in GraphResult::OrphanedNodes.
105 static GraphResult buildFlowGraph(const FileAnalysis &Analysis,
106 uint64_t Address);
107
108private:
109 // Implementation function that actually builds the flow graph. Retrieves a
110 // list of cross references to instruction referenced in `Address`. If any of
111 // these XRefs are conditional branches, it will build the other potential
112 // path (fallthrough or target) using `buildFlowsToUndefined`. Otherwise, this
113 // function will recursively call itself where `Address` in the recursive call
114 // is now the XRef. If any XRef is an orphan, it is added to
115 // `Result.OrphanedNodes`. `OpenedNodes` keeps track of the list of nodes
116 // in the current path and is used for cycle-checking. If the path is found
117 // to be cyclic, it will be added to `Result.OrphanedNodes`.
118 static void buildFlowGraphImpl(const FileAnalysis &Analysis,
119 DenseSet<uint64_t> &OpenedNodes,
120 GraphResult &Result, uint64_t Address,
121 uint64_t Depth);
122
123 // Utilised by buildFlowGraphImpl to build the tree out from the provided
124 // conditional branch node to an undefined instruction. The provided
125 // conditional branch node must have exactly one of its subtrees set, and will
126 // update the node's CFIProtection field if a deterministic flow can be found
127 // to an undefined instruction.
128 static void buildFlowsToUndefined(const FileAnalysis &Analysis,
129 GraphResult &Result,
130 ConditionalBranchNode &BranchNode,
131 const Instr &BranchInstrMeta);
132};
133
134} // end namespace cfi_verify
135} // end namespace llvm
136
137#endif // LLVM_CFI_VERIFY_GRAPH_BUILDER_H