blob: 12d9c95e3cf619a3ea6d555288815c7cfcf96bc1 [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;
62};
63
64// The canonical graph result structure returned by GraphBuilder. The members
65// in this structure encapsulate all possible code paths to the instruction
66// located at `BaseAddress`.
67struct GraphResult {
68 uint64_t BaseAddress;
69
70 // Map between an instruction address, and the address of the next instruction
71 // that will be executed. This map will contain all keys in the range:
72 // - [orphaned node, base address)
73 // - [conditional branch node {target|fallthrough}, base address)
74 DenseMap<uint64_t, uint64_t> IntermediateNodes;
75
76 // A list of orphaned nodes. A node is an 'orphan' if it meets any of the
77 // following criteria:
78 // - The length of the path from the base to this node has exceeded
79 // `SearchLengthForConditionalBranch`.
80 // - The node has no cross references to it.
81 // - The path from the base to this node is cyclic.
82 std::vector<uint64_t> OrphanedNodes;
83
84 // A list of top-level conditional branches that exist at the top of any
85 // non-orphan paths from the base.
86 std::vector<ConditionalBranchNode> ConditionalBranchNodes;
87
88 // Returns an in-order list of the path between the address provided and the
89 // base. The provided address must be part of this graph, and must not be a
90 // conditional branch.
91 std::vector<uint64_t> flattenAddress(uint64_t Address) const;
92};
93
94class GraphBuilder {
95public:
96 // Build the control flow graph for a provided control flow node. This method
97 // will enumerate all branch nodes that can lead to this node, and place them
98 // into GraphResult::ConditionalBranchNodes. It will also provide any orphaned
99 // (i.e. the upwards traversal did not make it to a branch node) flows to the
100 // provided node in GraphResult::OrphanedNodes.
101 static GraphResult buildFlowGraph(const FileAnalysis &Analysis,
102 uint64_t Address);
103
104private:
105 // Implementation function that actually builds the flow graph. Retrieves a
106 // list of cross references to instruction referenced in `Address`. If any of
107 // these XRefs are conditional branches, it will build the other potential
108 // path (fallthrough or target) using `buildFlowsToUndefined`. Otherwise, this
109 // function will recursively call itself where `Address` in the recursive call
110 // is now the XRef. If any XRef is an orphan, it is added to
111 // `Result.OrphanedNodes`. `OpenedNodes` keeps track of the list of nodes
112 // in the current path and is used for cycle-checking. If the path is found
113 // to be cyclic, it will be added to `Result.OrphanedNodes`.
114 static void buildFlowGraphImpl(const FileAnalysis &Analysis,
115 DenseSet<uint64_t> &OpenedNodes,
116 GraphResult &Result, uint64_t Address,
117 uint64_t Depth);
118
119 // Utilised by buildFlowGraphImpl to build the tree out from the provided
120 // conditional branch node to an undefined instruction. The provided
121 // conditional branch node must have exactly one of its subtrees set, and will
122 // update the node's CFIProtection field if a deterministic flow can be found
123 // to an undefined instruction.
124 static void buildFlowsToUndefined(const FileAnalysis &Analysis,
125 GraphResult &Result,
126 ConditionalBranchNode &BranchNode,
127 const Instr &BranchInstrMeta);
128};
129
130} // end namespace cfi_verify
131} // end namespace llvm
132
133#endif // LLVM_CFI_VERIFY_GRAPH_BUILDER_H