199fa1405SMitch Phillips //===- GraphBuilder.h -------------------------------------------*- C++ -*-===// 299fa1405SMitch Phillips // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 699fa1405SMitch Phillips // 799fa1405SMitch Phillips //===----------------------------------------------------------------------===// 899fa1405SMitch Phillips 999fa1405SMitch Phillips #ifndef LLVM_CFI_VERIFY_GRAPH_BUILDER_H 1099fa1405SMitch Phillips #define LLVM_CFI_VERIFY_GRAPH_BUILDER_H 1199fa1405SMitch Phillips 1299fa1405SMitch Phillips #include "FileAnalysis.h" 1399fa1405SMitch Phillips 1499fa1405SMitch Phillips #include "llvm/ADT/DenseMap.h" 1599fa1405SMitch Phillips #include "llvm/BinaryFormat/ELF.h" 1699fa1405SMitch Phillips #include "llvm/MC/MCAsmInfo.h" 1799fa1405SMitch Phillips #include "llvm/MC/MCContext.h" 1899fa1405SMitch Phillips #include "llvm/MC/MCDisassembler/MCDisassembler.h" 1999fa1405SMitch Phillips #include "llvm/MC/MCInst.h" 2099fa1405SMitch Phillips #include "llvm/MC/MCInstPrinter.h" 2199fa1405SMitch Phillips #include "llvm/MC/MCInstrAnalysis.h" 2299fa1405SMitch Phillips #include "llvm/MC/MCInstrDesc.h" 2399fa1405SMitch Phillips #include "llvm/MC/MCInstrInfo.h" 2499fa1405SMitch Phillips #include "llvm/MC/MCObjectFileInfo.h" 2599fa1405SMitch Phillips #include "llvm/MC/MCRegisterInfo.h" 2699fa1405SMitch Phillips #include "llvm/MC/MCSubtargetInfo.h" 27*89b57061SReid Kleckner #include "llvm/MC/TargetRegistry.h" 2899fa1405SMitch Phillips #include "llvm/Object/Binary.h" 2999fa1405SMitch Phillips #include "llvm/Object/COFF.h" 3099fa1405SMitch Phillips #include "llvm/Object/ELFObjectFile.h" 3199fa1405SMitch Phillips #include "llvm/Object/ObjectFile.h" 3299fa1405SMitch Phillips #include "llvm/Support/Casting.h" 3399fa1405SMitch Phillips #include "llvm/Support/CommandLine.h" 3499fa1405SMitch Phillips #include "llvm/Support/Error.h" 3599fa1405SMitch Phillips #include "llvm/Support/MemoryBuffer.h" 3699fa1405SMitch Phillips #include "llvm/Support/TargetSelect.h" 3799fa1405SMitch Phillips #include "llvm/Support/raw_ostream.h" 3899fa1405SMitch Phillips 3999fa1405SMitch Phillips #include <functional> 4099fa1405SMitch Phillips #include <set> 4199fa1405SMitch Phillips 4299fa1405SMitch Phillips using Instr = llvm::cfi_verify::FileAnalysis::Instr; 4399fa1405SMitch Phillips 4499fa1405SMitch Phillips namespace llvm { 4599fa1405SMitch Phillips namespace cfi_verify { 4699fa1405SMitch Phillips 47b5f39845SFangrui Song extern uint64_t SearchLengthForUndef; 48b5f39845SFangrui Song extern uint64_t SearchLengthForConditionalBranch; 4999fa1405SMitch Phillips 5099fa1405SMitch Phillips struct ConditionalBranchNode { 5199fa1405SMitch Phillips uint64_t Address; 5299fa1405SMitch Phillips uint64_t Target; 5399fa1405SMitch Phillips uint64_t Fallthrough; 5499fa1405SMitch Phillips // Does this conditional branch look like it's used for CFI protection? i.e. 5599fa1405SMitch Phillips // - The exit point of a basic block whos entry point is {target|fallthrough} 5699fa1405SMitch Phillips // is a CFI trap, and... 5799fa1405SMitch Phillips // - The exit point of the other basic block is an undirect CF instruction. 5899fa1405SMitch Phillips bool CFIProtection; 592e7be2a6SMitch Phillips bool IndirectCFIsOnTargetPath; 6099fa1405SMitch Phillips }; 6199fa1405SMitch Phillips 6299fa1405SMitch Phillips // The canonical graph result structure returned by GraphBuilder. The members 6399fa1405SMitch Phillips // in this structure encapsulate all possible code paths to the instruction 6499fa1405SMitch Phillips // located at `BaseAddress`. 6599fa1405SMitch Phillips struct GraphResult { 6699fa1405SMitch Phillips uint64_t BaseAddress; 6799fa1405SMitch Phillips 6899fa1405SMitch Phillips // Map between an instruction address, and the address of the next instruction 6999fa1405SMitch Phillips // that will be executed. This map will contain all keys in the range: 7099fa1405SMitch Phillips // - [orphaned node, base address) 7199fa1405SMitch Phillips // - [conditional branch node {target|fallthrough}, base address) 7299fa1405SMitch Phillips DenseMap<uint64_t, uint64_t> IntermediateNodes; 7399fa1405SMitch Phillips 7499fa1405SMitch Phillips // A list of orphaned nodes. A node is an 'orphan' if it meets any of the 7599fa1405SMitch Phillips // following criteria: 7699fa1405SMitch Phillips // - The length of the path from the base to this node has exceeded 7799fa1405SMitch Phillips // `SearchLengthForConditionalBranch`. 7899fa1405SMitch Phillips // - The node has no cross references to it. 7999fa1405SMitch Phillips // - The path from the base to this node is cyclic. 8099fa1405SMitch Phillips std::vector<uint64_t> OrphanedNodes; 8199fa1405SMitch Phillips 8299fa1405SMitch Phillips // A list of top-level conditional branches that exist at the top of any 8399fa1405SMitch Phillips // non-orphan paths from the base. 8499fa1405SMitch Phillips std::vector<ConditionalBranchNode> ConditionalBranchNodes; 8599fa1405SMitch Phillips 8699fa1405SMitch Phillips // Returns an in-order list of the path between the address provided and the 8799fa1405SMitch Phillips // base. The provided address must be part of this graph, and must not be a 8899fa1405SMitch Phillips // conditional branch. 8999fa1405SMitch Phillips std::vector<uint64_t> flattenAddress(uint64_t Address) const; 9002993892SMitch Phillips 9102993892SMitch Phillips // Print the DOT representation of this result. 9202993892SMitch Phillips void printToDOT(const FileAnalysis &Analysis, raw_ostream &OS) const; 9399fa1405SMitch Phillips }; 9499fa1405SMitch Phillips 9599fa1405SMitch Phillips class GraphBuilder { 9699fa1405SMitch Phillips public: 9799fa1405SMitch Phillips // Build the control flow graph for a provided control flow node. This method 9899fa1405SMitch Phillips // will enumerate all branch nodes that can lead to this node, and place them 9999fa1405SMitch Phillips // into GraphResult::ConditionalBranchNodes. It will also provide any orphaned 10099fa1405SMitch Phillips // (i.e. the upwards traversal did not make it to a branch node) flows to the 10199fa1405SMitch Phillips // provided node in GraphResult::OrphanedNodes. 10299fa1405SMitch Phillips static GraphResult buildFlowGraph(const FileAnalysis &Analysis, 10377fc1f60SAlexey Lapshin object::SectionedAddress Address); 10499fa1405SMitch Phillips 10599fa1405SMitch Phillips private: 10699fa1405SMitch Phillips // Implementation function that actually builds the flow graph. Retrieves a 10799fa1405SMitch Phillips // list of cross references to instruction referenced in `Address`. If any of 10899fa1405SMitch Phillips // these XRefs are conditional branches, it will build the other potential 10999fa1405SMitch Phillips // path (fallthrough or target) using `buildFlowsToUndefined`. Otherwise, this 11099fa1405SMitch Phillips // function will recursively call itself where `Address` in the recursive call 11199fa1405SMitch Phillips // is now the XRef. If any XRef is an orphan, it is added to 11299fa1405SMitch Phillips // `Result.OrphanedNodes`. `OpenedNodes` keeps track of the list of nodes 11399fa1405SMitch Phillips // in the current path and is used for cycle-checking. If the path is found 11499fa1405SMitch Phillips // to be cyclic, it will be added to `Result.OrphanedNodes`. 11599fa1405SMitch Phillips static void buildFlowGraphImpl(const FileAnalysis &Analysis, 11699fa1405SMitch Phillips DenseSet<uint64_t> &OpenedNodes, 11799fa1405SMitch Phillips GraphResult &Result, uint64_t Address, 11899fa1405SMitch Phillips uint64_t Depth); 11999fa1405SMitch Phillips 12099fa1405SMitch Phillips // Utilised by buildFlowGraphImpl to build the tree out from the provided 12199fa1405SMitch Phillips // conditional branch node to an undefined instruction. The provided 12299fa1405SMitch Phillips // conditional branch node must have exactly one of its subtrees set, and will 12399fa1405SMitch Phillips // update the node's CFIProtection field if a deterministic flow can be found 12499fa1405SMitch Phillips // to an undefined instruction. 12599fa1405SMitch Phillips static void buildFlowsToUndefined(const FileAnalysis &Analysis, 12699fa1405SMitch Phillips GraphResult &Result, 12799fa1405SMitch Phillips ConditionalBranchNode &BranchNode, 12899fa1405SMitch Phillips const Instr &BranchInstrMeta); 12999fa1405SMitch Phillips }; 13099fa1405SMitch Phillips 13199fa1405SMitch Phillips } // end namespace cfi_verify 13299fa1405SMitch Phillips } // end namespace llvm 13399fa1405SMitch Phillips 13499fa1405SMitch Phillips #endif // LLVM_CFI_VERIFY_GRAPH_BUILDER_H 135