GraphBuilder.h 5.5 KB

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