xref: /llvm-project/llvm/tools/llvm-cfi-verify/lib/GraphBuilder.cpp (revision db29f4374daba375fbc6534988782f5f7ce0ddbc)
199fa1405SMitch Phillips //===- GraphBuilder.cpp -----------------------------------------*- 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 #include "GraphBuilder.h"
1099fa1405SMitch Phillips 
1199fa1405SMitch Phillips #include "llvm/BinaryFormat/ELF.h"
12*db29f437Sserge-sans-paille #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
1399fa1405SMitch Phillips #include "llvm/MC/MCAsmInfo.h"
1499fa1405SMitch Phillips #include "llvm/MC/MCContext.h"
1599fa1405SMitch Phillips #include "llvm/MC/MCDisassembler/MCDisassembler.h"
1699fa1405SMitch Phillips #include "llvm/MC/MCInst.h"
1799fa1405SMitch Phillips #include "llvm/MC/MCInstPrinter.h"
1899fa1405SMitch Phillips #include "llvm/MC/MCInstrAnalysis.h"
1999fa1405SMitch Phillips #include "llvm/MC/MCInstrDesc.h"
2099fa1405SMitch Phillips #include "llvm/MC/MCInstrInfo.h"
2199fa1405SMitch Phillips #include "llvm/MC/MCObjectFileInfo.h"
2299fa1405SMitch Phillips #include "llvm/MC/MCRegisterInfo.h"
2399fa1405SMitch Phillips #include "llvm/MC/MCSubtargetInfo.h"
2489b57061SReid Kleckner #include "llvm/MC/TargetRegistry.h"
2599fa1405SMitch Phillips #include "llvm/Object/Binary.h"
2699fa1405SMitch Phillips #include "llvm/Object/COFF.h"
2799fa1405SMitch Phillips #include "llvm/Object/ELFObjectFile.h"
2899fa1405SMitch Phillips #include "llvm/Object/ObjectFile.h"
2999fa1405SMitch Phillips #include "llvm/Support/Casting.h"
3099fa1405SMitch Phillips #include "llvm/Support/CommandLine.h"
3199fa1405SMitch Phillips #include "llvm/Support/Error.h"
3299fa1405SMitch Phillips #include "llvm/Support/MemoryBuffer.h"
3399fa1405SMitch Phillips #include "llvm/Support/TargetSelect.h"
3499fa1405SMitch Phillips #include "llvm/Support/raw_ostream.h"
3599fa1405SMitch Phillips 
3699fa1405SMitch Phillips using Instr = llvm::cfi_verify::FileAnalysis::Instr;
3799fa1405SMitch Phillips 
3899fa1405SMitch Phillips namespace llvm {
3999fa1405SMitch Phillips namespace cfi_verify {
4099fa1405SMitch Phillips 
41b5f39845SFangrui Song uint64_t SearchLengthForUndef;
42b5f39845SFangrui Song uint64_t SearchLengthForConditionalBranch;
4399fa1405SMitch Phillips 
44b5f39845SFangrui Song static cl::opt<uint64_t, true> SearchLengthForUndefArg(
4599fa1405SMitch Phillips     "search-length-undef",
4699fa1405SMitch Phillips     cl::desc("Specify the maximum amount of instructions "
4799fa1405SMitch Phillips              "to inspect when searching for an undefined "
4899fa1405SMitch Phillips              "instruction from a conditional branch."),
4999fa1405SMitch Phillips     cl::location(SearchLengthForUndef), cl::init(2));
5099fa1405SMitch Phillips 
51b5f39845SFangrui Song static cl::opt<uint64_t, true> SearchLengthForConditionalBranchArg(
5299fa1405SMitch Phillips     "search-length-cb",
5399fa1405SMitch Phillips     cl::desc("Specify the maximum amount of instructions "
5499fa1405SMitch Phillips              "to inspect when searching for a conditional "
5599fa1405SMitch Phillips              "branch from an indirect control flow."),
5699fa1405SMitch Phillips     cl::location(SearchLengthForConditionalBranch), cl::init(20));
5799fa1405SMitch Phillips 
flattenAddress(uint64_t Address) const5899fa1405SMitch Phillips std::vector<uint64_t> GraphResult::flattenAddress(uint64_t Address) const {
5999fa1405SMitch Phillips   std::vector<uint64_t> Addresses;
6099fa1405SMitch Phillips 
6199fa1405SMitch Phillips   auto It = IntermediateNodes.find(Address);
6299fa1405SMitch Phillips   Addresses.push_back(Address);
6399fa1405SMitch Phillips 
6499fa1405SMitch Phillips   while (It != IntermediateNodes.end()) {
6599fa1405SMitch Phillips     Addresses.push_back(It->second);
6699fa1405SMitch Phillips     It = IntermediateNodes.find(It->second);
6799fa1405SMitch Phillips   }
6899fa1405SMitch Phillips   return Addresses;
6999fa1405SMitch Phillips }
7099fa1405SMitch Phillips 
printPairToDOT(const FileAnalysis & Analysis,raw_ostream & OS,uint64_t From,uint64_t To)7102993892SMitch Phillips void printPairToDOT(const FileAnalysis &Analysis, raw_ostream &OS,
7202993892SMitch Phillips                           uint64_t From, uint64_t To) {
7302993892SMitch Phillips   OS << "  \"" << format_hex(From, 2) << ": ";
7402993892SMitch Phillips   Analysis.printInstruction(Analysis.getInstructionOrDie(From), OS);
7502993892SMitch Phillips   OS << "\" -> \"" << format_hex(To, 2) << ": ";
7602993892SMitch Phillips   Analysis.printInstruction(Analysis.getInstructionOrDie(To), OS);
7702993892SMitch Phillips   OS << "\"\n";
7802993892SMitch Phillips }
7902993892SMitch Phillips 
printToDOT(const FileAnalysis & Analysis,raw_ostream & OS) const8002993892SMitch Phillips void GraphResult::printToDOT(const FileAnalysis &Analysis,
8102993892SMitch Phillips                              raw_ostream &OS) const {
8202993892SMitch Phillips   std::map<uint64_t, uint64_t> SortedIntermediateNodes(
8302993892SMitch Phillips       IntermediateNodes.begin(), IntermediateNodes.end());
8402993892SMitch Phillips   OS << "digraph graph_" << format_hex(BaseAddress, 2) << " {\n";
8502993892SMitch Phillips   for (const auto &KV : SortedIntermediateNodes)
8602993892SMitch Phillips     printPairToDOT(Analysis, OS, KV.first, KV.second);
8702993892SMitch Phillips 
8802993892SMitch Phillips   for (auto &BranchNode : ConditionalBranchNodes) {
8902993892SMitch Phillips     for (auto &V : {BranchNode.Target, BranchNode.Fallthrough})
9002993892SMitch Phillips       printPairToDOT(Analysis, OS, BranchNode.Address, V);
9102993892SMitch Phillips   }
9202993892SMitch Phillips   OS << "}\n";
9302993892SMitch Phillips }
9402993892SMitch Phillips 
buildFlowGraph(const FileAnalysis & Analysis,object::SectionedAddress Address)9599fa1405SMitch Phillips GraphResult GraphBuilder::buildFlowGraph(const FileAnalysis &Analysis,
9677fc1f60SAlexey Lapshin                                          object::SectionedAddress Address) {
9799fa1405SMitch Phillips   GraphResult Result;
9877fc1f60SAlexey Lapshin   Result.BaseAddress = Address.Address;
9999fa1405SMitch Phillips   DenseSet<uint64_t> OpenedNodes;
10099fa1405SMitch Phillips 
10199fa1405SMitch Phillips   const auto &IndirectInstructions = Analysis.getIndirectInstructions();
10299fa1405SMitch Phillips 
10377fc1f60SAlexey Lapshin   // check that IndirectInstructions contains specified Address
10477fc1f60SAlexey Lapshin   if (IndirectInstructions.find(Address) == IndirectInstructions.end()) {
10599fa1405SMitch Phillips     return Result;
10677fc1f60SAlexey Lapshin   }
10799fa1405SMitch Phillips 
10877fc1f60SAlexey Lapshin   buildFlowGraphImpl(Analysis, OpenedNodes, Result, Address.Address, 0);
10999fa1405SMitch Phillips   return Result;
11099fa1405SMitch Phillips }
11199fa1405SMitch Phillips 
buildFlowsToUndefined(const FileAnalysis & Analysis,GraphResult & Result,ConditionalBranchNode & BranchNode,const Instr & BranchInstrMeta)11299fa1405SMitch Phillips void GraphBuilder::buildFlowsToUndefined(const FileAnalysis &Analysis,
11399fa1405SMitch Phillips                                          GraphResult &Result,
11499fa1405SMitch Phillips                                          ConditionalBranchNode &BranchNode,
11599fa1405SMitch Phillips                                          const Instr &BranchInstrMeta) {
11699fa1405SMitch Phillips   assert(SearchLengthForUndef > 0 &&
11799fa1405SMitch Phillips          "Search length for undefined flow must be greater than zero.");
11899fa1405SMitch Phillips 
11999fa1405SMitch Phillips   // Start setting up the next node in the block.
12099fa1405SMitch Phillips   uint64_t NextAddress = 0;
12199fa1405SMitch Phillips   const Instr *NextMetaPtr;
12299fa1405SMitch Phillips 
12399fa1405SMitch Phillips   // Find out the next instruction in the block and add it to the new
12499fa1405SMitch Phillips   // node.
12599fa1405SMitch Phillips   if (BranchNode.Target && !BranchNode.Fallthrough) {
12699fa1405SMitch Phillips     // We know the target of the branch, find the fallthrough.
12799fa1405SMitch Phillips     NextMetaPtr = Analysis.getNextInstructionSequential(BranchInstrMeta);
12899fa1405SMitch Phillips     if (!NextMetaPtr) {
12999fa1405SMitch Phillips       errs() << "Failed to get next instruction from "
13099fa1405SMitch Phillips              << format_hex(BranchNode.Address, 2) << ".\n";
13199fa1405SMitch Phillips       return;
13299fa1405SMitch Phillips     }
13399fa1405SMitch Phillips 
13499fa1405SMitch Phillips     NextAddress = NextMetaPtr->VMAddress;
13599fa1405SMitch Phillips     BranchNode.Fallthrough =
13699fa1405SMitch Phillips         NextMetaPtr->VMAddress; // Add the new node to the branch head.
13799fa1405SMitch Phillips   } else if (BranchNode.Fallthrough && !BranchNode.Target) {
13899fa1405SMitch Phillips     // We already know the fallthrough, evaluate the target.
13999fa1405SMitch Phillips     uint64_t Target;
14099fa1405SMitch Phillips     if (!Analysis.getMCInstrAnalysis()->evaluateBranch(
14199fa1405SMitch Phillips             BranchInstrMeta.Instruction, BranchInstrMeta.VMAddress,
14299fa1405SMitch Phillips             BranchInstrMeta.InstructionSize, Target)) {
14399fa1405SMitch Phillips       errs() << "Failed to get branch target for conditional branch at address "
14499fa1405SMitch Phillips              << format_hex(BranchInstrMeta.VMAddress, 2) << ".\n";
14599fa1405SMitch Phillips       return;
14699fa1405SMitch Phillips     }
14799fa1405SMitch Phillips 
14899fa1405SMitch Phillips     // Resolve the meta pointer for the target of this branch.
14999fa1405SMitch Phillips     NextMetaPtr = Analysis.getInstruction(Target);
15099fa1405SMitch Phillips     if (!NextMetaPtr) {
15199fa1405SMitch Phillips       errs() << "Failed to find instruction at address "
15299fa1405SMitch Phillips              << format_hex(Target, 2) << ".\n";
15399fa1405SMitch Phillips       return;
15499fa1405SMitch Phillips     }
15599fa1405SMitch Phillips 
15699fa1405SMitch Phillips     NextAddress = Target;
15799fa1405SMitch Phillips     BranchNode.Target =
15899fa1405SMitch Phillips         NextMetaPtr->VMAddress; // Add the new node to the branch head.
15999fa1405SMitch Phillips   } else {
16099fa1405SMitch Phillips     errs() << "ControlBranchNode supplied to buildFlowsToUndefined should "
16199fa1405SMitch Phillips               "provide Target xor Fallthrough.\n";
16299fa1405SMitch Phillips     return;
16399fa1405SMitch Phillips   }
16499fa1405SMitch Phillips 
16599fa1405SMitch Phillips   uint64_t CurrentAddress = NextAddress;
16699fa1405SMitch Phillips   const Instr *CurrentMetaPtr = NextMetaPtr;
16799fa1405SMitch Phillips 
16899fa1405SMitch Phillips   // Now the branch head has been set properly, complete the rest of the block.
16999fa1405SMitch Phillips   for (uint64_t i = 1; i < SearchLengthForUndef; ++i) {
17099fa1405SMitch Phillips     // Check to see whether the block should die.
17199fa1405SMitch Phillips     if (Analysis.isCFITrap(*CurrentMetaPtr)) {
17299fa1405SMitch Phillips       BranchNode.CFIProtection = true;
17399fa1405SMitch Phillips       return;
17499fa1405SMitch Phillips     }
17599fa1405SMitch Phillips 
17699fa1405SMitch Phillips     // Find the metadata of the next instruction.
17799fa1405SMitch Phillips     NextMetaPtr = Analysis.getDefiniteNextInstruction(*CurrentMetaPtr);
17899fa1405SMitch Phillips     if (!NextMetaPtr)
17999fa1405SMitch Phillips       return;
18099fa1405SMitch Phillips 
18199fa1405SMitch Phillips     // Setup the next node.
18299fa1405SMitch Phillips     NextAddress = NextMetaPtr->VMAddress;
18399fa1405SMitch Phillips 
18499fa1405SMitch Phillips     // Add this as an intermediate.
18599fa1405SMitch Phillips     Result.IntermediateNodes[CurrentAddress] = NextAddress;
18699fa1405SMitch Phillips 
18799fa1405SMitch Phillips     // Move the 'current' pointers to the new tail of the block.
18899fa1405SMitch Phillips     CurrentMetaPtr = NextMetaPtr;
18999fa1405SMitch Phillips     CurrentAddress = NextAddress;
19099fa1405SMitch Phillips   }
19199fa1405SMitch Phillips 
19299fa1405SMitch Phillips   // Final check of the last thing we added to the block.
19399fa1405SMitch Phillips   if (Analysis.isCFITrap(*CurrentMetaPtr))
19499fa1405SMitch Phillips     BranchNode.CFIProtection = true;
19599fa1405SMitch Phillips }
19699fa1405SMitch Phillips 
buildFlowGraphImpl(const FileAnalysis & Analysis,DenseSet<uint64_t> & OpenedNodes,GraphResult & Result,uint64_t Address,uint64_t Depth)19799fa1405SMitch Phillips void GraphBuilder::buildFlowGraphImpl(const FileAnalysis &Analysis,
19899fa1405SMitch Phillips                                       DenseSet<uint64_t> &OpenedNodes,
19999fa1405SMitch Phillips                                       GraphResult &Result, uint64_t Address,
20099fa1405SMitch Phillips                                       uint64_t Depth) {
20199fa1405SMitch Phillips   // If we've exceeded the flow length, terminate.
20299fa1405SMitch Phillips   if (Depth >= SearchLengthForConditionalBranch) {
20399fa1405SMitch Phillips     Result.OrphanedNodes.push_back(Address);
20499fa1405SMitch Phillips     return;
20599fa1405SMitch Phillips   }
20699fa1405SMitch Phillips 
20799fa1405SMitch Phillips   // Ensure this flow is acyclic.
20899fa1405SMitch Phillips   if (OpenedNodes.count(Address))
20999fa1405SMitch Phillips     Result.OrphanedNodes.push_back(Address);
21099fa1405SMitch Phillips 
21199fa1405SMitch Phillips   // If this flow is already explored, stop here.
21299fa1405SMitch Phillips   if (Result.IntermediateNodes.count(Address))
21399fa1405SMitch Phillips     return;
21499fa1405SMitch Phillips 
21599fa1405SMitch Phillips   // Get the metadata for the node instruction.
21699fa1405SMitch Phillips   const auto &InstrMetaPtr = Analysis.getInstruction(Address);
21799fa1405SMitch Phillips   if (!InstrMetaPtr) {
21899fa1405SMitch Phillips     errs() << "Failed to build flow graph for instruction at address "
21999fa1405SMitch Phillips            << format_hex(Address, 2) << ".\n";
22099fa1405SMitch Phillips     Result.OrphanedNodes.push_back(Address);
22199fa1405SMitch Phillips     return;
22299fa1405SMitch Phillips   }
22399fa1405SMitch Phillips   const auto &ChildMeta = *InstrMetaPtr;
22499fa1405SMitch Phillips 
22599fa1405SMitch Phillips   OpenedNodes.insert(Address);
22699fa1405SMitch Phillips   std::set<const Instr *> CFCrossRefs =
22799fa1405SMitch Phillips       Analysis.getDirectControlFlowXRefs(ChildMeta);
22899fa1405SMitch Phillips 
22999fa1405SMitch Phillips   bool HasValidCrossRef = false;
23099fa1405SMitch Phillips 
23199fa1405SMitch Phillips   for (const auto *ParentMetaPtr : CFCrossRefs) {
23299fa1405SMitch Phillips     assert(ParentMetaPtr && "CFCrossRefs returned nullptr.");
23399fa1405SMitch Phillips     const auto &ParentMeta = *ParentMetaPtr;
23499fa1405SMitch Phillips     const auto &ParentDesc =
23599fa1405SMitch Phillips         Analysis.getMCInstrInfo()->get(ParentMeta.Instruction.getOpcode());
23699fa1405SMitch Phillips 
23799fa1405SMitch Phillips     if (!ParentDesc.mayAffectControlFlow(ParentMeta.Instruction,
23899fa1405SMitch Phillips                                          *Analysis.getRegisterInfo())) {
23999fa1405SMitch Phillips       // If this cross reference doesn't affect CF, continue the graph.
24099fa1405SMitch Phillips       buildFlowGraphImpl(Analysis, OpenedNodes, Result, ParentMeta.VMAddress,
24199fa1405SMitch Phillips                          Depth + 1);
24299fa1405SMitch Phillips       Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
24399fa1405SMitch Phillips       HasValidCrossRef = true;
24499fa1405SMitch Phillips       continue;
24599fa1405SMitch Phillips     }
24699fa1405SMitch Phillips 
2477db6f7a3SMitch Phillips     // Call instructions are not valid in the upwards traversal.
2487db6f7a3SMitch Phillips     if (ParentDesc.isCall()) {
2497db6f7a3SMitch Phillips       Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
2507db6f7a3SMitch Phillips       Result.OrphanedNodes.push_back(ParentMeta.VMAddress);
2517db6f7a3SMitch Phillips       continue;
2527db6f7a3SMitch Phillips     }
2537db6f7a3SMitch Phillips 
25499fa1405SMitch Phillips     // Evaluate the branch target to ascertain whether this XRef is the result
25599fa1405SMitch Phillips     // of a fallthrough or the target of a branch.
25699fa1405SMitch Phillips     uint64_t BranchTarget;
25799fa1405SMitch Phillips     if (!Analysis.getMCInstrAnalysis()->evaluateBranch(
25899fa1405SMitch Phillips             ParentMeta.Instruction, ParentMeta.VMAddress,
25999fa1405SMitch Phillips             ParentMeta.InstructionSize, BranchTarget)) {
26099fa1405SMitch Phillips       errs() << "Failed to evaluate branch target for instruction at address "
26199fa1405SMitch Phillips              << format_hex(ParentMeta.VMAddress, 2) << ".\n";
26299fa1405SMitch Phillips       Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
26399fa1405SMitch Phillips       Result.OrphanedNodes.push_back(ParentMeta.VMAddress);
26499fa1405SMitch Phillips       continue;
26599fa1405SMitch Phillips     }
26699fa1405SMitch Phillips 
26799fa1405SMitch Phillips     // Allow unconditional branches to be part of the upwards traversal.
26899fa1405SMitch Phillips     if (ParentDesc.isUnconditionalBranch()) {
26999fa1405SMitch Phillips       // Ensures that the unconditional branch is actually an XRef to the child.
27099fa1405SMitch Phillips       if (BranchTarget != Address) {
27199fa1405SMitch Phillips         errs() << "Control flow to " << format_hex(Address, 2)
27299fa1405SMitch Phillips                << ", but target resolution of "
27399fa1405SMitch Phillips                << format_hex(ParentMeta.VMAddress, 2)
27499fa1405SMitch Phillips                << " is not this address?\n";
27599fa1405SMitch Phillips         Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
27699fa1405SMitch Phillips         Result.OrphanedNodes.push_back(ParentMeta.VMAddress);
27799fa1405SMitch Phillips         continue;
27899fa1405SMitch Phillips       }
27999fa1405SMitch Phillips 
28099fa1405SMitch Phillips       buildFlowGraphImpl(Analysis, OpenedNodes, Result, ParentMeta.VMAddress,
28199fa1405SMitch Phillips                          Depth + 1);
28299fa1405SMitch Phillips       Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
28399fa1405SMitch Phillips       HasValidCrossRef = true;
28499fa1405SMitch Phillips       continue;
28599fa1405SMitch Phillips     }
28699fa1405SMitch Phillips 
28799fa1405SMitch Phillips     // Ensure that any unknown CFs are caught.
28899fa1405SMitch Phillips     if (!ParentDesc.isConditionalBranch()) {
28999fa1405SMitch Phillips       errs() << "Unknown control flow encountered when building graph at "
29099fa1405SMitch Phillips              << format_hex(Address, 2) << "\n.";
29199fa1405SMitch Phillips       Result.IntermediateNodes[ParentMeta.VMAddress] = Address;
29299fa1405SMitch Phillips       Result.OrphanedNodes.push_back(ParentMeta.VMAddress);
29399fa1405SMitch Phillips       continue;
29499fa1405SMitch Phillips     }
29599fa1405SMitch Phillips 
29699fa1405SMitch Phillips     // Only direct conditional branches should be present at this point. Setup
29799fa1405SMitch Phillips     // a conditional branch node and build flows to the ud2.
29899fa1405SMitch Phillips     ConditionalBranchNode BranchNode;
29999fa1405SMitch Phillips     BranchNode.Address = ParentMeta.VMAddress;
30099fa1405SMitch Phillips     BranchNode.Target = 0;
30199fa1405SMitch Phillips     BranchNode.Fallthrough = 0;
30299fa1405SMitch Phillips     BranchNode.CFIProtection = false;
3032e7be2a6SMitch Phillips     BranchNode.IndirectCFIsOnTargetPath = (BranchTarget == Address);
30499fa1405SMitch Phillips 
30599fa1405SMitch Phillips     if (BranchTarget == Address)
30699fa1405SMitch Phillips       BranchNode.Target = Address;
30799fa1405SMitch Phillips     else
30899fa1405SMitch Phillips       BranchNode.Fallthrough = Address;
30999fa1405SMitch Phillips 
31099fa1405SMitch Phillips     HasValidCrossRef = true;
31199fa1405SMitch Phillips     buildFlowsToUndefined(Analysis, Result, BranchNode, ParentMeta);
31299fa1405SMitch Phillips     Result.ConditionalBranchNodes.push_back(BranchNode);
31399fa1405SMitch Phillips   }
31499fa1405SMitch Phillips 
3156cc0e63eSJoel Galenson   // When using cross-DSO, some indirect calls are not guarded by a branch to a
3166cc0e63eSJoel Galenson   // trap but instead follow a call to __cfi_slowpath.  For example:
3176cc0e63eSJoel Galenson   // if (!InlinedFastCheck(f))
3186cc0e63eSJoel Galenson   //    call *f
3196cc0e63eSJoel Galenson   //  else {
3206cc0e63eSJoel Galenson   //    __cfi_slowpath(CallSiteTypeId, f);
3216cc0e63eSJoel Galenson   //    call *f
3226cc0e63eSJoel Galenson   //  }
3236cc0e63eSJoel Galenson   // To mark the second call as protected, we recognize indirect calls that
3246cc0e63eSJoel Galenson   // directly follow calls to functions that will trap on CFI violations.
3256cc0e63eSJoel Galenson   if (CFCrossRefs.empty()) {
3266cc0e63eSJoel Galenson     const Instr *PrevInstr = Analysis.getPrevInstructionSequential(ChildMeta);
3276cc0e63eSJoel Galenson     if (PrevInstr && Analysis.willTrapOnCFIViolation(*PrevInstr)) {
3286cc0e63eSJoel Galenson       Result.IntermediateNodes[PrevInstr->VMAddress] = Address;
3296cc0e63eSJoel Galenson       HasValidCrossRef = true;
3306cc0e63eSJoel Galenson     }
3316cc0e63eSJoel Galenson   }
3326cc0e63eSJoel Galenson 
33399fa1405SMitch Phillips   if (!HasValidCrossRef)
33499fa1405SMitch Phillips     Result.OrphanedNodes.push_back(Address);
33599fa1405SMitch Phillips 
33699fa1405SMitch Phillips   OpenedNodes.erase(Address);
33799fa1405SMitch Phillips }
33899fa1405SMitch Phillips 
33999fa1405SMitch Phillips } // namespace cfi_verify
34099fa1405SMitch Phillips } // namespace llvm
341