GraphBuilder.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. //===- GraphBuilder.cpp -----------------------------------------*- 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. #include "GraphBuilder.h"
  9. #include "llvm/BinaryFormat/ELF.h"
  10. #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
  11. #include "llvm/MC/MCAsmInfo.h"
  12. #include "llvm/MC/MCContext.h"
  13. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  14. #include "llvm/MC/MCInst.h"
  15. #include "llvm/MC/MCInstPrinter.h"
  16. #include "llvm/MC/MCInstrAnalysis.h"
  17. #include "llvm/MC/MCInstrDesc.h"
  18. #include "llvm/MC/MCInstrInfo.h"
  19. #include "llvm/MC/MCObjectFileInfo.h"
  20. #include "llvm/MC/MCRegisterInfo.h"
  21. #include "llvm/MC/MCSubtargetInfo.h"
  22. #include "llvm/MC/TargetRegistry.h"
  23. #include "llvm/Object/Binary.h"
  24. #include "llvm/Object/COFF.h"
  25. #include "llvm/Object/ELFObjectFile.h"
  26. #include "llvm/Object/ObjectFile.h"
  27. #include "llvm/Support/Casting.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/Error.h"
  30. #include "llvm/Support/MemoryBuffer.h"
  31. #include "llvm/Support/TargetSelect.h"
  32. #include "llvm/Support/raw_ostream.h"
  33. using Instr = llvm::cfi_verify::FileAnalysis::Instr;
  34. namespace llvm {
  35. namespace cfi_verify {
  36. uint64_t SearchLengthForUndef;
  37. uint64_t SearchLengthForConditionalBranch;
  38. static cl::opt<uint64_t, true> SearchLengthForUndefArg(
  39. "search-length-undef",
  40. cl::desc("Specify the maximum amount of instructions "
  41. "to inspect when searching for an undefined "
  42. "instruction from a conditional branch."),
  43. cl::location(SearchLengthForUndef), cl::init(2));
  44. static cl::opt<uint64_t, true> SearchLengthForConditionalBranchArg(
  45. "search-length-cb",
  46. cl::desc("Specify the maximum amount of instructions "
  47. "to inspect when searching for a conditional "
  48. "branch from an indirect control flow."),
  49. cl::location(SearchLengthForConditionalBranch), cl::init(20));
  50. std::vector<uint64_t> GraphResult::flattenAddress(uint64_t Address) const {
  51. std::vector<uint64_t> Addresses;
  52. auto It = IntermediateNodes.find(Address);
  53. Addresses.push_back(Address);
  54. while (It != IntermediateNodes.end()) {
  55. Addresses.push_back(It->second);
  56. It = IntermediateNodes.find(It->second);
  57. }
  58. return Addresses;
  59. }
  60. void printPairToDOT(const FileAnalysis &Analysis, raw_ostream &OS,
  61. uint64_t From, uint64_t To) {
  62. OS << " \"" << format_hex(From, 2) << ": ";
  63. Analysis.printInstruction(Analysis.getInstructionOrDie(From), OS);
  64. OS << "\" -> \"" << format_hex(To, 2) << ": ";
  65. Analysis.printInstruction(Analysis.getInstructionOrDie(To), OS);
  66. OS << "\"\n";
  67. }
  68. void GraphResult::printToDOT(const FileAnalysis &Analysis,
  69. raw_ostream &OS) const {
  70. std::map<uint64_t, uint64_t> SortedIntermediateNodes(
  71. IntermediateNodes.begin(), IntermediateNodes.end());
  72. OS << "digraph graph_" << format_hex(BaseAddress, 2) << " {\n";
  73. for (const auto &KV : SortedIntermediateNodes)
  74. printPairToDOT(Analysis, OS, KV.first, KV.second);
  75. for (auto &BranchNode : ConditionalBranchNodes) {
  76. for (auto &V : {BranchNode.Target, BranchNode.Fallthrough})
  77. printPairToDOT(Analysis, OS, BranchNode.Address, V);
  78. }
  79. OS << "}\n";
  80. }
  81. GraphResult GraphBuilder::buildFlowGraph(const FileAnalysis &Analysis,
  82. object::SectionedAddress Address) {
  83. GraphResult Result;
  84. Result.BaseAddress = Address.Address;
  85. DenseSet<uint64_t> OpenedNodes;
  86. const auto &IndirectInstructions = Analysis.getIndirectInstructions();
  87. // check that IndirectInstructions contains specified Address
  88. if (IndirectInstructions.find(Address) == IndirectInstructions.end()) {
  89. return Result;
  90. }
  91. buildFlowGraphImpl(Analysis, OpenedNodes, Result, Address.Address, 0);
  92. return Result;
  93. }
  94. void GraphBuilder::buildFlowsToUndefined(const FileAnalysis &Analysis,
  95. GraphResult &Result,
  96. ConditionalBranchNode &BranchNode,
  97. const Instr &BranchInstrMeta) {
  98. assert(SearchLengthForUndef > 0 &&
  99. "Search length for undefined flow must be greater than zero.");
  100. // Start setting up the next node in the block.
  101. uint64_t NextAddress = 0;
  102. const Instr *NextMetaPtr;
  103. // Find out the next instruction in the block and add it to the new
  104. // node.
  105. if (BranchNode.Target && !BranchNode.Fallthrough) {
  106. // We know the target of the branch, find the fallthrough.
  107. NextMetaPtr = Analysis.getNextInstructionSequential(BranchInstrMeta);
  108. if (!NextMetaPtr) {
  109. errs() << "Failed to get next instruction from "
  110. << format_hex(BranchNode.Address, 2) << ".\n";
  111. return;
  112. }
  113. NextAddress = NextMetaPtr->VMAddress;
  114. BranchNode.Fallthrough =
  115. NextMetaPtr->VMAddress; // Add the new node to the branch head.
  116. } else if (BranchNode.Fallthrough && !BranchNode.Target) {
  117. // We already know the fallthrough, evaluate the target.
  118. uint64_t Target;
  119. if (!Analysis.getMCInstrAnalysis()->evaluateBranch(
  120. BranchInstrMeta.Instruction, BranchInstrMeta.VMAddress,
  121. BranchInstrMeta.InstructionSize, Target)) {
  122. errs() << "Failed to get branch target for conditional branch at address "
  123. << format_hex(BranchInstrMeta.VMAddress, 2) << ".\n";
  124. return;
  125. }
  126. // Resolve the meta pointer for the target of this branch.
  127. NextMetaPtr = Analysis.getInstruction(Target);
  128. if (!NextMetaPtr) {
  129. errs() << "Failed to find instruction at address "
  130. << format_hex(Target, 2) << ".\n";
  131. return;
  132. }
  133. NextAddress = Target;
  134. BranchNode.Target =
  135. NextMetaPtr->VMAddress; // Add the new node to the branch head.
  136. } else {
  137. errs() << "ControlBranchNode supplied to buildFlowsToUndefined should "
  138. "provide Target xor Fallthrough.\n";
  139. return;
  140. }
  141. uint64_t CurrentAddress = NextAddress;
  142. const Instr *CurrentMetaPtr = NextMetaPtr;
  143. // Now the branch head has been set properly, complete the rest of the block.
  144. for (uint64_t i = 1; i < SearchLengthForUndef; ++i) {
  145. // Check to see whether the block should die.
  146. if (Analysis.isCFITrap(*CurrentMetaPtr)) {
  147. BranchNode.CFIProtection = true;
  148. return;
  149. }
  150. // Find the metadata of the next instruction.
  151. NextMetaPtr = Analysis.getDefiniteNextInstruction(*CurrentMetaPtr);
  152. if (!NextMetaPtr)
  153. return;
  154. // Setup the next node.
  155. NextAddress = NextMetaPtr->VMAddress;
  156. // Add this as an intermediate.
  157. Result.IntermediateNodes[CurrentAddress] = NextAddress;
  158. // Move the 'current' pointers to the new tail of the block.
  159. CurrentMetaPtr = NextMetaPtr;
  160. CurrentAddress = NextAddress;
  161. }
  162. // Final check of the last thing we added to the block.
  163. if (Analysis.isCFITrap(*CurrentMetaPtr))
  164. BranchNode.CFIProtection = true;
  165. }
  166. void GraphBuilder::buildFlowGraphImpl(const FileAnalysis &Analysis,
  167. DenseSet<uint64_t> &OpenedNodes,
  168. GraphResult &Result, uint64_t Address,
  169. uint64_t Depth) {
  170. // If we've exceeded the flow length, terminate.
  171. if (Depth >= SearchLengthForConditionalBranch) {
  172. Result.OrphanedNodes.push_back(Address);
  173. return;
  174. }
  175. // Ensure this flow is acyclic.
  176. if (OpenedNodes.count(Address))
  177. Result.OrphanedNodes.push_back(Address);
  178. // If this flow is already explored, stop here.
  179. if (Result.IntermediateNodes.count(Address))
  180. return;
  181. // Get the metadata for the node instruction.
  182. const auto &InstrMetaPtr = Analysis.getInstruction(Address);
  183. if (!InstrMetaPtr) {
  184. errs() << "Failed to build flow graph for instruction at address "
  185. << format_hex(Address, 2) << ".\n";
  186. Result.OrphanedNodes.push_back(Address);
  187. return;
  188. }
  189. const auto &ChildMeta = *InstrMetaPtr;
  190. OpenedNodes.insert(Address);
  191. std::set<const Instr *> CFCrossRefs =
  192. Analysis.getDirectControlFlowXRefs(ChildMeta);
  193. bool HasValidCrossRef = false;
  194. for (const auto *ParentMetaPtr : CFCrossRefs) {
  195. assert(ParentMetaPtr && "CFCrossRefs returned nullptr.");
  196. const auto &ParentMeta = *ParentMetaPtr;
  197. const auto &ParentDesc =
  198. Analysis.getMCInstrInfo()->get(ParentMeta.Instruction.getOpcode());
  199. if (!ParentDesc.mayAffectControlFlow(ParentMeta.Instruction,
  200. *Analysis.getRegisterInfo())) {
  201. // If this cross reference doesn't affect CF, continue the graph.
  202. buildFlowGraphImpl(Analysis, OpenedNodes, Result, ParentMeta.VMAddress,
  203. Depth + 1);
  204. Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
  205. HasValidCrossRef = true;
  206. continue;
  207. }
  208. // Call instructions are not valid in the upwards traversal.
  209. if (ParentDesc.isCall()) {
  210. Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
  211. Result.OrphanedNodes.push_back(ParentMeta.VMAddress);
  212. continue;
  213. }
  214. // Evaluate the branch target to ascertain whether this XRef is the result
  215. // of a fallthrough or the target of a branch.
  216. uint64_t BranchTarget;
  217. if (!Analysis.getMCInstrAnalysis()->evaluateBranch(
  218. ParentMeta.Instruction, ParentMeta.VMAddress,
  219. ParentMeta.InstructionSize, BranchTarget)) {
  220. errs() << "Failed to evaluate branch target for instruction at address "
  221. << format_hex(ParentMeta.VMAddress, 2) << ".\n";
  222. Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
  223. Result.OrphanedNodes.push_back(ParentMeta.VMAddress);
  224. continue;
  225. }
  226. // Allow unconditional branches to be part of the upwards traversal.
  227. if (ParentDesc.isUnconditionalBranch()) {
  228. // Ensures that the unconditional branch is actually an XRef to the child.
  229. if (BranchTarget != Address) {
  230. errs() << "Control flow to " << format_hex(Address, 2)
  231. << ", but target resolution of "
  232. << format_hex(ParentMeta.VMAddress, 2)
  233. << " is not this address?\n";
  234. Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
  235. Result.OrphanedNodes.push_back(ParentMeta.VMAddress);
  236. continue;
  237. }
  238. buildFlowGraphImpl(Analysis, OpenedNodes, Result, ParentMeta.VMAddress,
  239. Depth + 1);
  240. Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
  241. HasValidCrossRef = true;
  242. continue;
  243. }
  244. // Ensure that any unknown CFs are caught.
  245. if (!ParentDesc.isConditionalBranch()) {
  246. errs() << "Unknown control flow encountered when building graph at "
  247. << format_hex(Address, 2) << "\n.";
  248. Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
  249. Result.OrphanedNodes.push_back(ParentMeta.VMAddress);
  250. continue;
  251. }
  252. // Only direct conditional branches should be present at this point. Setup
  253. // a conditional branch node and build flows to the ud2.
  254. ConditionalBranchNode BranchNode;
  255. BranchNode.Address = ParentMeta.VMAddress;
  256. BranchNode.Target = 0;
  257. BranchNode.Fallthrough = 0;
  258. BranchNode.CFIProtection = false;
  259. BranchNode.IndirectCFIsOnTargetPath = (BranchTarget == Address);
  260. if (BranchTarget == Address)
  261. BranchNode.Target = Address;
  262. else
  263. BranchNode.Fallthrough = Address;
  264. HasValidCrossRef = true;
  265. buildFlowsToUndefined(Analysis, Result, BranchNode, ParentMeta);
  266. Result.ConditionalBranchNodes.push_back(BranchNode);
  267. }
  268. // When using cross-DSO, some indirect calls are not guarded by a branch to a
  269. // trap but instead follow a call to __cfi_slowpath. For example:
  270. // if (!InlinedFastCheck(f))
  271. // call *f
  272. // else {
  273. // __cfi_slowpath(CallSiteTypeId, f);
  274. // call *f
  275. // }
  276. // To mark the second call as protected, we recognize indirect calls that
  277. // directly follow calls to functions that will trap on CFI violations.
  278. if (CFCrossRefs.empty()) {
  279. const Instr *PrevInstr = Analysis.getPrevInstructionSequential(ChildMeta);
  280. if (PrevInstr && Analysis.willTrapOnCFIViolation(*PrevInstr)) {
  281. Result.IntermediateNodes[PrevInstr->VMAddress] = Address;
  282. HasValidCrossRef = true;
  283. }
  284. }
  285. if (!HasValidCrossRef)
  286. Result.OrphanedNodes.push_back(Address);
  287. OpenedNodes.erase(Address);
  288. }
  289. } // namespace cfi_verify
  290. } // namespace llvm