1*0946e70aSDimitry Andric //==-- X86LoadValueInjectionLoadHardening.cpp - LVI load hardening for x86 --=// 2*0946e70aSDimitry Andric // 3*0946e70aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0946e70aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0946e70aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0946e70aSDimitry Andric // 7*0946e70aSDimitry Andric //===----------------------------------------------------------------------===// 8*0946e70aSDimitry Andric /// 9*0946e70aSDimitry Andric /// Description: This pass finds Load Value Injection (LVI) gadgets consisting 10*0946e70aSDimitry Andric /// of a load from memory (i.e., SOURCE), and any operation that may transmit 11*0946e70aSDimitry Andric /// the value loaded from memory over a covert channel, or use the value loaded 12*0946e70aSDimitry Andric /// from memory to determine a branch/call target (i.e., SINK). After finding 13*0946e70aSDimitry Andric /// all such gadgets in a given function, the pass minimally inserts LFENCE 14*0946e70aSDimitry Andric /// instructions in such a manner that the following property is satisfied: for 15*0946e70aSDimitry Andric /// all SOURCE+SINK pairs, all paths in the CFG from SOURCE to SINK contain at 16*0946e70aSDimitry Andric /// least one LFENCE instruction. The algorithm that implements this minimal 17*0946e70aSDimitry Andric /// insertion is influenced by an academic paper that minimally inserts memory 18*0946e70aSDimitry Andric /// fences for high-performance concurrent programs: 19*0946e70aSDimitry Andric /// http://www.cs.ucr.edu/~lesani/companion/oopsla15/OOPSLA15.pdf 20*0946e70aSDimitry Andric /// The algorithm implemented in this pass is as follows: 21*0946e70aSDimitry Andric /// 1. Build a condensed CFG (i.e., a GadgetGraph) consisting only of the 22*0946e70aSDimitry Andric /// following components: 23*0946e70aSDimitry Andric /// - SOURCE instructions (also includes function arguments) 24*0946e70aSDimitry Andric /// - SINK instructions 25*0946e70aSDimitry Andric /// - Basic block entry points 26*0946e70aSDimitry Andric /// - Basic block terminators 27*0946e70aSDimitry Andric /// - LFENCE instructions 28*0946e70aSDimitry Andric /// 2. Analyze the GadgetGraph to determine which SOURCE+SINK pairs (i.e., 29*0946e70aSDimitry Andric /// gadgets) are already mitigated by existing LFENCEs. If all gadgets have been 30*0946e70aSDimitry Andric /// mitigated, go to step 6. 31*0946e70aSDimitry Andric /// 3. Use a heuristic or plugin to approximate minimal LFENCE insertion. 32*0946e70aSDimitry Andric /// 4. Insert one LFENCE along each CFG edge that was cut in step 3. 33*0946e70aSDimitry Andric /// 5. Go to step 2. 34*0946e70aSDimitry Andric /// 6. If any LFENCEs were inserted, return `true` from runOnMachineFunction() 35*0946e70aSDimitry Andric /// to tell LLVM that the function was modified. 36*0946e70aSDimitry Andric /// 37*0946e70aSDimitry Andric //===----------------------------------------------------------------------===// 38*0946e70aSDimitry Andric 39*0946e70aSDimitry Andric #include "ImmutableGraph.h" 40*0946e70aSDimitry Andric #include "X86.h" 41*0946e70aSDimitry Andric #include "X86Subtarget.h" 42*0946e70aSDimitry Andric #include "X86TargetMachine.h" 43*0946e70aSDimitry Andric #include "llvm/ADT/DenseMap.h" 44*0946e70aSDimitry Andric #include "llvm/ADT/DenseSet.h" 45*0946e70aSDimitry Andric #include "llvm/ADT/SmallSet.h" 46*0946e70aSDimitry Andric #include "llvm/ADT/Statistic.h" 47*0946e70aSDimitry Andric #include "llvm/ADT/StringRef.h" 48*0946e70aSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 49*0946e70aSDimitry Andric #include "llvm/CodeGen/MachineDominanceFrontier.h" 50*0946e70aSDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 51*0946e70aSDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 52*0946e70aSDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 53*0946e70aSDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 54*0946e70aSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 55*0946e70aSDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h" 56*0946e70aSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 57*0946e70aSDimitry Andric #include "llvm/CodeGen/RDFGraph.h" 58*0946e70aSDimitry Andric #include "llvm/CodeGen/RDFLiveness.h" 59*0946e70aSDimitry Andric #include "llvm/InitializePasses.h" 60*0946e70aSDimitry Andric #include "llvm/Support/CommandLine.h" 61*0946e70aSDimitry Andric #include "llvm/Support/DOTGraphTraits.h" 62*0946e70aSDimitry Andric #include "llvm/Support/Debug.h" 63*0946e70aSDimitry Andric #include "llvm/Support/DynamicLibrary.h" 64*0946e70aSDimitry Andric #include "llvm/Support/GraphWriter.h" 65*0946e70aSDimitry Andric #include "llvm/Support/raw_ostream.h" 66*0946e70aSDimitry Andric 67*0946e70aSDimitry Andric using namespace llvm; 68*0946e70aSDimitry Andric 69*0946e70aSDimitry Andric #define PASS_KEY "x86-lvi-load" 70*0946e70aSDimitry Andric #define DEBUG_TYPE PASS_KEY 71*0946e70aSDimitry Andric 72*0946e70aSDimitry Andric STATISTIC(NumFences, "Number of LFENCEs inserted for LVI mitigation"); 73*0946e70aSDimitry Andric STATISTIC(NumFunctionsConsidered, "Number of functions analyzed"); 74*0946e70aSDimitry Andric STATISTIC(NumFunctionsMitigated, "Number of functions for which mitigations " 75*0946e70aSDimitry Andric "were deployed"); 76*0946e70aSDimitry Andric STATISTIC(NumGadgets, "Number of LVI gadgets detected during analysis"); 77*0946e70aSDimitry Andric 78*0946e70aSDimitry Andric static cl::opt<std::string> OptimizePluginPath( 79*0946e70aSDimitry Andric PASS_KEY "-opt-plugin", 80*0946e70aSDimitry Andric cl::desc("Specify a plugin to optimize LFENCE insertion"), cl::Hidden); 81*0946e70aSDimitry Andric 82*0946e70aSDimitry Andric static cl::opt<bool> NoConditionalBranches( 83*0946e70aSDimitry Andric PASS_KEY "-no-cbranch", 84*0946e70aSDimitry Andric cl::desc("Don't treat conditional branches as disclosure gadgets. This " 85*0946e70aSDimitry Andric "may improve performance, at the cost of security."), 86*0946e70aSDimitry Andric cl::init(false), cl::Hidden); 87*0946e70aSDimitry Andric 88*0946e70aSDimitry Andric static cl::opt<bool> EmitDot( 89*0946e70aSDimitry Andric PASS_KEY "-dot", 90*0946e70aSDimitry Andric cl::desc( 91*0946e70aSDimitry Andric "For each function, emit a dot graph depicting potential LVI gadgets"), 92*0946e70aSDimitry Andric cl::init(false), cl::Hidden); 93*0946e70aSDimitry Andric 94*0946e70aSDimitry Andric static cl::opt<bool> EmitDotOnly( 95*0946e70aSDimitry Andric PASS_KEY "-dot-only", 96*0946e70aSDimitry Andric cl::desc("For each function, emit a dot graph depicting potential LVI " 97*0946e70aSDimitry Andric "gadgets, and do not insert any fences"), 98*0946e70aSDimitry Andric cl::init(false), cl::Hidden); 99*0946e70aSDimitry Andric 100*0946e70aSDimitry Andric static cl::opt<bool> EmitDotVerify( 101*0946e70aSDimitry Andric PASS_KEY "-dot-verify", 102*0946e70aSDimitry Andric cl::desc("For each function, emit a dot graph to stdout depicting " 103*0946e70aSDimitry Andric "potential LVI gadgets, used for testing purposes only"), 104*0946e70aSDimitry Andric cl::init(false), cl::Hidden); 105*0946e70aSDimitry Andric 106*0946e70aSDimitry Andric static llvm::sys::DynamicLibrary OptimizeDL; 107*0946e70aSDimitry Andric typedef int (*OptimizeCutT)(unsigned int *nodes, unsigned int nodes_size, 108*0946e70aSDimitry Andric unsigned int *edges, int *edge_values, 109*0946e70aSDimitry Andric int *cut_edges /* out */, unsigned int edges_size); 110*0946e70aSDimitry Andric static OptimizeCutT OptimizeCut = nullptr; 111*0946e70aSDimitry Andric 112*0946e70aSDimitry Andric namespace { 113*0946e70aSDimitry Andric 114*0946e70aSDimitry Andric struct MachineGadgetGraph : ImmutableGraph<MachineInstr *, int> { 115*0946e70aSDimitry Andric static constexpr int GadgetEdgeSentinel = -1; 116*0946e70aSDimitry Andric static constexpr MachineInstr *const ArgNodeSentinel = nullptr; 117*0946e70aSDimitry Andric 118*0946e70aSDimitry Andric using GraphT = ImmutableGraph<MachineInstr *, int>; 119*0946e70aSDimitry Andric using Node = typename GraphT::Node; 120*0946e70aSDimitry Andric using Edge = typename GraphT::Edge; 121*0946e70aSDimitry Andric using size_type = typename GraphT::size_type; 122*0946e70aSDimitry Andric MachineGadgetGraph(std::unique_ptr<Node[]> Nodes, 123*0946e70aSDimitry Andric std::unique_ptr<Edge[]> Edges, size_type NodesSize, 124*0946e70aSDimitry Andric size_type EdgesSize, int NumFences = 0, int NumGadgets = 0) 125*0946e70aSDimitry Andric : GraphT(std::move(Nodes), std::move(Edges), NodesSize, EdgesSize), 126*0946e70aSDimitry Andric NumFences(NumFences), NumGadgets(NumGadgets) {} 127*0946e70aSDimitry Andric static inline bool isCFGEdge(const Edge &E) { 128*0946e70aSDimitry Andric return E.getValue() != GadgetEdgeSentinel; 129*0946e70aSDimitry Andric } 130*0946e70aSDimitry Andric static inline bool isGadgetEdge(const Edge &E) { 131*0946e70aSDimitry Andric return E.getValue() == GadgetEdgeSentinel; 132*0946e70aSDimitry Andric } 133*0946e70aSDimitry Andric int NumFences; 134*0946e70aSDimitry Andric int NumGadgets; 135*0946e70aSDimitry Andric }; 136*0946e70aSDimitry Andric 137*0946e70aSDimitry Andric class X86LoadValueInjectionLoadHardeningPass : public MachineFunctionPass { 138*0946e70aSDimitry Andric public: 139*0946e70aSDimitry Andric X86LoadValueInjectionLoadHardeningPass() : MachineFunctionPass(ID) {} 140*0946e70aSDimitry Andric 141*0946e70aSDimitry Andric StringRef getPassName() const override { 142*0946e70aSDimitry Andric return "X86 Load Value Injection (LVI) Load Hardening"; 143*0946e70aSDimitry Andric } 144*0946e70aSDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override; 145*0946e70aSDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 146*0946e70aSDimitry Andric 147*0946e70aSDimitry Andric static char ID; 148*0946e70aSDimitry Andric 149*0946e70aSDimitry Andric private: 150*0946e70aSDimitry Andric using GraphBuilder = ImmutableGraphBuilder<MachineGadgetGraph>; 151*0946e70aSDimitry Andric using EdgeSet = MachineGadgetGraph::EdgeSet; 152*0946e70aSDimitry Andric using NodeSet = MachineGadgetGraph::NodeSet; 153*0946e70aSDimitry Andric using Gadget = std::pair<MachineInstr *, MachineInstr *>; 154*0946e70aSDimitry Andric 155*0946e70aSDimitry Andric const X86Subtarget *STI; 156*0946e70aSDimitry Andric const TargetInstrInfo *TII; 157*0946e70aSDimitry Andric const TargetRegisterInfo *TRI; 158*0946e70aSDimitry Andric 159*0946e70aSDimitry Andric std::unique_ptr<MachineGadgetGraph> 160*0946e70aSDimitry Andric getGadgetGraph(MachineFunction &MF, const MachineLoopInfo &MLI, 161*0946e70aSDimitry Andric const MachineDominatorTree &MDT, 162*0946e70aSDimitry Andric const MachineDominanceFrontier &MDF) const; 163*0946e70aSDimitry Andric int hardenLoadsWithPlugin(MachineFunction &MF, 164*0946e70aSDimitry Andric std::unique_ptr<MachineGadgetGraph> Graph) const; 165*0946e70aSDimitry Andric int hardenLoadsWithGreedyHeuristic( 166*0946e70aSDimitry Andric MachineFunction &MF, std::unique_ptr<MachineGadgetGraph> Graph) const; 167*0946e70aSDimitry Andric int elimMitigatedEdgesAndNodes(MachineGadgetGraph &G, 168*0946e70aSDimitry Andric EdgeSet &ElimEdges /* in, out */, 169*0946e70aSDimitry Andric NodeSet &ElimNodes /* in, out */) const; 170*0946e70aSDimitry Andric std::unique_ptr<MachineGadgetGraph> 171*0946e70aSDimitry Andric trimMitigatedEdges(std::unique_ptr<MachineGadgetGraph> Graph) const; 172*0946e70aSDimitry Andric void findAndCutEdges(MachineGadgetGraph &G, 173*0946e70aSDimitry Andric EdgeSet &CutEdges /* out */) const; 174*0946e70aSDimitry Andric int insertFences(MachineFunction &MF, MachineGadgetGraph &G, 175*0946e70aSDimitry Andric EdgeSet &CutEdges /* in, out */) const; 176*0946e70aSDimitry Andric bool instrUsesRegToAccessMemory(const MachineInstr &I, unsigned Reg) const; 177*0946e70aSDimitry Andric bool instrUsesRegToBranch(const MachineInstr &I, unsigned Reg) const; 178*0946e70aSDimitry Andric inline bool isFence(const MachineInstr *MI) const { 179*0946e70aSDimitry Andric return MI && (MI->getOpcode() == X86::LFENCE || 180*0946e70aSDimitry Andric (STI->useLVIControlFlowIntegrity() && MI->isCall())); 181*0946e70aSDimitry Andric } 182*0946e70aSDimitry Andric }; 183*0946e70aSDimitry Andric 184*0946e70aSDimitry Andric } // end anonymous namespace 185*0946e70aSDimitry Andric 186*0946e70aSDimitry Andric namespace llvm { 187*0946e70aSDimitry Andric 188*0946e70aSDimitry Andric template <> 189*0946e70aSDimitry Andric struct GraphTraits<MachineGadgetGraph *> 190*0946e70aSDimitry Andric : GraphTraits<ImmutableGraph<MachineInstr *, int> *> {}; 191*0946e70aSDimitry Andric 192*0946e70aSDimitry Andric template <> 193*0946e70aSDimitry Andric struct DOTGraphTraits<MachineGadgetGraph *> : DefaultDOTGraphTraits { 194*0946e70aSDimitry Andric using GraphType = MachineGadgetGraph; 195*0946e70aSDimitry Andric using Traits = llvm::GraphTraits<GraphType *>; 196*0946e70aSDimitry Andric using NodeRef = typename Traits::NodeRef; 197*0946e70aSDimitry Andric using EdgeRef = typename Traits::EdgeRef; 198*0946e70aSDimitry Andric using ChildIteratorType = typename Traits::ChildIteratorType; 199*0946e70aSDimitry Andric using ChildEdgeIteratorType = typename Traits::ChildEdgeIteratorType; 200*0946e70aSDimitry Andric 201*0946e70aSDimitry Andric DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} 202*0946e70aSDimitry Andric 203*0946e70aSDimitry Andric std::string getNodeLabel(NodeRef Node, GraphType *) { 204*0946e70aSDimitry Andric if (Node->getValue() == MachineGadgetGraph::ArgNodeSentinel) 205*0946e70aSDimitry Andric return "ARGS"; 206*0946e70aSDimitry Andric 207*0946e70aSDimitry Andric std::string Str; 208*0946e70aSDimitry Andric raw_string_ostream OS(Str); 209*0946e70aSDimitry Andric OS << *Node->getValue(); 210*0946e70aSDimitry Andric return OS.str(); 211*0946e70aSDimitry Andric } 212*0946e70aSDimitry Andric 213*0946e70aSDimitry Andric static std::string getNodeAttributes(NodeRef Node, GraphType *) { 214*0946e70aSDimitry Andric MachineInstr *MI = Node->getValue(); 215*0946e70aSDimitry Andric if (MI == MachineGadgetGraph::ArgNodeSentinel) 216*0946e70aSDimitry Andric return "color = blue"; 217*0946e70aSDimitry Andric if (MI->getOpcode() == X86::LFENCE) 218*0946e70aSDimitry Andric return "color = green"; 219*0946e70aSDimitry Andric return ""; 220*0946e70aSDimitry Andric } 221*0946e70aSDimitry Andric 222*0946e70aSDimitry Andric static std::string getEdgeAttributes(NodeRef, ChildIteratorType E, 223*0946e70aSDimitry Andric GraphType *) { 224*0946e70aSDimitry Andric int EdgeVal = (*E.getCurrent()).getValue(); 225*0946e70aSDimitry Andric return EdgeVal >= 0 ? "label = " + std::to_string(EdgeVal) 226*0946e70aSDimitry Andric : "color = red, style = \"dashed\""; 227*0946e70aSDimitry Andric } 228*0946e70aSDimitry Andric }; 229*0946e70aSDimitry Andric 230*0946e70aSDimitry Andric } // end namespace llvm 231*0946e70aSDimitry Andric 232*0946e70aSDimitry Andric constexpr MachineInstr *MachineGadgetGraph::ArgNodeSentinel; 233*0946e70aSDimitry Andric constexpr int MachineGadgetGraph::GadgetEdgeSentinel; 234*0946e70aSDimitry Andric 235*0946e70aSDimitry Andric char X86LoadValueInjectionLoadHardeningPass::ID = 0; 236*0946e70aSDimitry Andric 237*0946e70aSDimitry Andric void X86LoadValueInjectionLoadHardeningPass::getAnalysisUsage( 238*0946e70aSDimitry Andric AnalysisUsage &AU) const { 239*0946e70aSDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 240*0946e70aSDimitry Andric AU.addRequired<MachineLoopInfo>(); 241*0946e70aSDimitry Andric AU.addRequired<MachineDominatorTree>(); 242*0946e70aSDimitry Andric AU.addRequired<MachineDominanceFrontier>(); 243*0946e70aSDimitry Andric AU.setPreservesCFG(); 244*0946e70aSDimitry Andric } 245*0946e70aSDimitry Andric 246*0946e70aSDimitry Andric static void WriteGadgetGraph(raw_ostream &OS, MachineFunction &MF, 247*0946e70aSDimitry Andric MachineGadgetGraph *G) { 248*0946e70aSDimitry Andric WriteGraph(OS, G, /*ShortNames*/ false, 249*0946e70aSDimitry Andric "Speculative gadgets for \"" + MF.getName() + "\" function"); 250*0946e70aSDimitry Andric } 251*0946e70aSDimitry Andric 252*0946e70aSDimitry Andric bool X86LoadValueInjectionLoadHardeningPass::runOnMachineFunction( 253*0946e70aSDimitry Andric MachineFunction &MF) { 254*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "***** " << getPassName() << " : " << MF.getName() 255*0946e70aSDimitry Andric << " *****\n"); 256*0946e70aSDimitry Andric STI = &MF.getSubtarget<X86Subtarget>(); 257*0946e70aSDimitry Andric if (!STI->useLVILoadHardening()) 258*0946e70aSDimitry Andric return false; 259*0946e70aSDimitry Andric 260*0946e70aSDimitry Andric // FIXME: support 32-bit 261*0946e70aSDimitry Andric if (!STI->is64Bit()) 262*0946e70aSDimitry Andric report_fatal_error("LVI load hardening is only supported on 64-bit", false); 263*0946e70aSDimitry Andric 264*0946e70aSDimitry Andric // Don't skip functions with the "optnone" attr but participate in opt-bisect. 265*0946e70aSDimitry Andric const Function &F = MF.getFunction(); 266*0946e70aSDimitry Andric if (!F.hasOptNone() && skipFunction(F)) 267*0946e70aSDimitry Andric return false; 268*0946e70aSDimitry Andric 269*0946e70aSDimitry Andric ++NumFunctionsConsidered; 270*0946e70aSDimitry Andric TII = STI->getInstrInfo(); 271*0946e70aSDimitry Andric TRI = STI->getRegisterInfo(); 272*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Building gadget graph...\n"); 273*0946e70aSDimitry Andric const auto &MLI = getAnalysis<MachineLoopInfo>(); 274*0946e70aSDimitry Andric const auto &MDT = getAnalysis<MachineDominatorTree>(); 275*0946e70aSDimitry Andric const auto &MDF = getAnalysis<MachineDominanceFrontier>(); 276*0946e70aSDimitry Andric std::unique_ptr<MachineGadgetGraph> Graph = getGadgetGraph(MF, MLI, MDT, MDF); 277*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Building gadget graph... Done\n"); 278*0946e70aSDimitry Andric if (Graph == nullptr) 279*0946e70aSDimitry Andric return false; // didn't find any gadgets 280*0946e70aSDimitry Andric 281*0946e70aSDimitry Andric if (EmitDotVerify) { 282*0946e70aSDimitry Andric WriteGadgetGraph(outs(), MF, Graph.get()); 283*0946e70aSDimitry Andric return false; 284*0946e70aSDimitry Andric } 285*0946e70aSDimitry Andric 286*0946e70aSDimitry Andric if (EmitDot || EmitDotOnly) { 287*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Emitting gadget graph...\n"); 288*0946e70aSDimitry Andric std::error_code FileError; 289*0946e70aSDimitry Andric std::string FileName = "lvi."; 290*0946e70aSDimitry Andric FileName += MF.getName(); 291*0946e70aSDimitry Andric FileName += ".dot"; 292*0946e70aSDimitry Andric raw_fd_ostream FileOut(FileName, FileError); 293*0946e70aSDimitry Andric if (FileError) 294*0946e70aSDimitry Andric errs() << FileError.message(); 295*0946e70aSDimitry Andric WriteGadgetGraph(FileOut, MF, Graph.get()); 296*0946e70aSDimitry Andric FileOut.close(); 297*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Emitting gadget graph... Done\n"); 298*0946e70aSDimitry Andric if (EmitDotOnly) 299*0946e70aSDimitry Andric return false; 300*0946e70aSDimitry Andric } 301*0946e70aSDimitry Andric 302*0946e70aSDimitry Andric int FencesInserted; 303*0946e70aSDimitry Andric if (!OptimizePluginPath.empty()) { 304*0946e70aSDimitry Andric if (!OptimizeDL.isValid()) { 305*0946e70aSDimitry Andric std::string ErrorMsg; 306*0946e70aSDimitry Andric OptimizeDL = llvm::sys::DynamicLibrary::getPermanentLibrary( 307*0946e70aSDimitry Andric OptimizePluginPath.c_str(), &ErrorMsg); 308*0946e70aSDimitry Andric if (!ErrorMsg.empty()) 309*0946e70aSDimitry Andric report_fatal_error("Failed to load opt plugin: \"" + ErrorMsg + '\"'); 310*0946e70aSDimitry Andric OptimizeCut = (OptimizeCutT)OptimizeDL.getAddressOfSymbol("optimize_cut"); 311*0946e70aSDimitry Andric if (!OptimizeCut) 312*0946e70aSDimitry Andric report_fatal_error("Invalid optimization plugin"); 313*0946e70aSDimitry Andric } 314*0946e70aSDimitry Andric FencesInserted = hardenLoadsWithPlugin(MF, std::move(Graph)); 315*0946e70aSDimitry Andric } else { // Use the default greedy heuristic 316*0946e70aSDimitry Andric FencesInserted = hardenLoadsWithGreedyHeuristic(MF, std::move(Graph)); 317*0946e70aSDimitry Andric } 318*0946e70aSDimitry Andric 319*0946e70aSDimitry Andric if (FencesInserted > 0) 320*0946e70aSDimitry Andric ++NumFunctionsMitigated; 321*0946e70aSDimitry Andric NumFences += FencesInserted; 322*0946e70aSDimitry Andric return (FencesInserted > 0); 323*0946e70aSDimitry Andric } 324*0946e70aSDimitry Andric 325*0946e70aSDimitry Andric std::unique_ptr<MachineGadgetGraph> 326*0946e70aSDimitry Andric X86LoadValueInjectionLoadHardeningPass::getGadgetGraph( 327*0946e70aSDimitry Andric MachineFunction &MF, const MachineLoopInfo &MLI, 328*0946e70aSDimitry Andric const MachineDominatorTree &MDT, 329*0946e70aSDimitry Andric const MachineDominanceFrontier &MDF) const { 330*0946e70aSDimitry Andric using namespace rdf; 331*0946e70aSDimitry Andric 332*0946e70aSDimitry Andric // Build the Register Dataflow Graph using the RDF framework 333*0946e70aSDimitry Andric TargetOperandInfo TOI{*TII}; 334*0946e70aSDimitry Andric DataFlowGraph DFG{MF, *TII, *TRI, MDT, MDF, TOI}; 335*0946e70aSDimitry Andric DFG.build(); 336*0946e70aSDimitry Andric Liveness L{MF.getRegInfo(), DFG}; 337*0946e70aSDimitry Andric L.computePhiInfo(); 338*0946e70aSDimitry Andric 339*0946e70aSDimitry Andric GraphBuilder Builder; 340*0946e70aSDimitry Andric using GraphIter = typename GraphBuilder::BuilderNodeRef; 341*0946e70aSDimitry Andric DenseMap<MachineInstr *, GraphIter> NodeMap; 342*0946e70aSDimitry Andric int FenceCount = 0, GadgetCount = 0; 343*0946e70aSDimitry Andric auto MaybeAddNode = [&NodeMap, &Builder](MachineInstr *MI) { 344*0946e70aSDimitry Andric auto Ref = NodeMap.find(MI); 345*0946e70aSDimitry Andric if (Ref == NodeMap.end()) { 346*0946e70aSDimitry Andric auto I = Builder.addVertex(MI); 347*0946e70aSDimitry Andric NodeMap[MI] = I; 348*0946e70aSDimitry Andric return std::pair<GraphIter, bool>{I, true}; 349*0946e70aSDimitry Andric } 350*0946e70aSDimitry Andric return std::pair<GraphIter, bool>{Ref->getSecond(), false}; 351*0946e70aSDimitry Andric }; 352*0946e70aSDimitry Andric 353*0946e70aSDimitry Andric // The `Transmitters` map memoizes transmitters found for each def. If a def 354*0946e70aSDimitry Andric // has not yet been analyzed, then it will not appear in the map. If a def 355*0946e70aSDimitry Andric // has been analyzed and was determined not to have any transmitters, then 356*0946e70aSDimitry Andric // its list of transmitters will be empty. 357*0946e70aSDimitry Andric DenseMap<NodeId, std::vector<NodeId>> Transmitters; 358*0946e70aSDimitry Andric 359*0946e70aSDimitry Andric // Analyze all machine instructions to find gadgets and LFENCEs, adding 360*0946e70aSDimitry Andric // each interesting value to `Nodes` 361*0946e70aSDimitry Andric auto AnalyzeDef = [&](NodeAddr<DefNode *> SourceDef) { 362*0946e70aSDimitry Andric SmallSet<NodeId, 8> UsesVisited, DefsVisited; 363*0946e70aSDimitry Andric std::function<void(NodeAddr<DefNode *>)> AnalyzeDefUseChain = 364*0946e70aSDimitry Andric [&](NodeAddr<DefNode *> Def) { 365*0946e70aSDimitry Andric if (Transmitters.find(Def.Id) != Transmitters.end()) 366*0946e70aSDimitry Andric return; // Already analyzed `Def` 367*0946e70aSDimitry Andric 368*0946e70aSDimitry Andric // Use RDF to find all the uses of `Def` 369*0946e70aSDimitry Andric rdf::NodeSet Uses; 370*0946e70aSDimitry Andric RegisterRef DefReg = DFG.getPRI().normalize(Def.Addr->getRegRef(DFG)); 371*0946e70aSDimitry Andric for (auto UseID : L.getAllReachedUses(DefReg, Def)) { 372*0946e70aSDimitry Andric auto Use = DFG.addr<UseNode *>(UseID); 373*0946e70aSDimitry Andric if (Use.Addr->getFlags() & NodeAttrs::PhiRef) { // phi node 374*0946e70aSDimitry Andric NodeAddr<PhiNode *> Phi = Use.Addr->getOwner(DFG); 375*0946e70aSDimitry Andric for (auto I : L.getRealUses(Phi.Id)) { 376*0946e70aSDimitry Andric if (DFG.getPRI().alias(RegisterRef(I.first), DefReg)) { 377*0946e70aSDimitry Andric for (auto UA : I.second) 378*0946e70aSDimitry Andric Uses.emplace(UA.first); 379*0946e70aSDimitry Andric } 380*0946e70aSDimitry Andric } 381*0946e70aSDimitry Andric } else { // not a phi node 382*0946e70aSDimitry Andric Uses.emplace(UseID); 383*0946e70aSDimitry Andric } 384*0946e70aSDimitry Andric } 385*0946e70aSDimitry Andric 386*0946e70aSDimitry Andric // For each use of `Def`, we want to know whether: 387*0946e70aSDimitry Andric // (1) The use can leak the Def'ed value, 388*0946e70aSDimitry Andric // (2) The use can further propagate the Def'ed value to more defs 389*0946e70aSDimitry Andric for (auto UseID : Uses) { 390*0946e70aSDimitry Andric if (!UsesVisited.insert(UseID).second) 391*0946e70aSDimitry Andric continue; // Already visited this use of `Def` 392*0946e70aSDimitry Andric 393*0946e70aSDimitry Andric auto Use = DFG.addr<UseNode *>(UseID); 394*0946e70aSDimitry Andric assert(!(Use.Addr->getFlags() & NodeAttrs::PhiRef)); 395*0946e70aSDimitry Andric MachineOperand &UseMO = Use.Addr->getOp(); 396*0946e70aSDimitry Andric MachineInstr &UseMI = *UseMO.getParent(); 397*0946e70aSDimitry Andric assert(UseMO.isReg()); 398*0946e70aSDimitry Andric 399*0946e70aSDimitry Andric // We naively assume that an instruction propagates any loaded 400*0946e70aSDimitry Andric // uses to all defs unless the instruction is a call, in which 401*0946e70aSDimitry Andric // case all arguments will be treated as gadget sources during 402*0946e70aSDimitry Andric // analysis of the callee function. 403*0946e70aSDimitry Andric if (UseMI.isCall()) 404*0946e70aSDimitry Andric continue; 405*0946e70aSDimitry Andric 406*0946e70aSDimitry Andric // Check whether this use can transmit (leak) its value. 407*0946e70aSDimitry Andric if (instrUsesRegToAccessMemory(UseMI, UseMO.getReg()) || 408*0946e70aSDimitry Andric (!NoConditionalBranches && 409*0946e70aSDimitry Andric instrUsesRegToBranch(UseMI, UseMO.getReg()))) { 410*0946e70aSDimitry Andric Transmitters[Def.Id].push_back(Use.Addr->getOwner(DFG).Id); 411*0946e70aSDimitry Andric if (UseMI.mayLoad()) 412*0946e70aSDimitry Andric continue; // Found a transmitting load -- no need to continue 413*0946e70aSDimitry Andric // traversing its defs (i.e., this load will become 414*0946e70aSDimitry Andric // a new gadget source anyways). 415*0946e70aSDimitry Andric } 416*0946e70aSDimitry Andric 417*0946e70aSDimitry Andric // Check whether the use propagates to more defs. 418*0946e70aSDimitry Andric NodeAddr<InstrNode *> Owner{Use.Addr->getOwner(DFG)}; 419*0946e70aSDimitry Andric rdf::NodeList AnalyzedChildDefs; 420*0946e70aSDimitry Andric for (auto &ChildDef : 421*0946e70aSDimitry Andric Owner.Addr->members_if(DataFlowGraph::IsDef, DFG)) { 422*0946e70aSDimitry Andric if (!DefsVisited.insert(ChildDef.Id).second) 423*0946e70aSDimitry Andric continue; // Already visited this def 424*0946e70aSDimitry Andric if (Def.Addr->getAttrs() & NodeAttrs::Dead) 425*0946e70aSDimitry Andric continue; 426*0946e70aSDimitry Andric if (Def.Id == ChildDef.Id) 427*0946e70aSDimitry Andric continue; // `Def` uses itself (e.g., increment loop counter) 428*0946e70aSDimitry Andric 429*0946e70aSDimitry Andric AnalyzeDefUseChain(ChildDef); 430*0946e70aSDimitry Andric 431*0946e70aSDimitry Andric // `Def` inherits all of its child defs' transmitters. 432*0946e70aSDimitry Andric for (auto TransmitterId : Transmitters[ChildDef.Id]) 433*0946e70aSDimitry Andric Transmitters[Def.Id].push_back(TransmitterId); 434*0946e70aSDimitry Andric } 435*0946e70aSDimitry Andric } 436*0946e70aSDimitry Andric 437*0946e70aSDimitry Andric // Note that this statement adds `Def.Id` to the map if no 438*0946e70aSDimitry Andric // transmitters were found for `Def`. 439*0946e70aSDimitry Andric auto &DefTransmitters = Transmitters[Def.Id]; 440*0946e70aSDimitry Andric 441*0946e70aSDimitry Andric // Remove duplicate transmitters 442*0946e70aSDimitry Andric llvm::sort(DefTransmitters); 443*0946e70aSDimitry Andric DefTransmitters.erase( 444*0946e70aSDimitry Andric std::unique(DefTransmitters.begin(), DefTransmitters.end()), 445*0946e70aSDimitry Andric DefTransmitters.end()); 446*0946e70aSDimitry Andric }; 447*0946e70aSDimitry Andric 448*0946e70aSDimitry Andric // Find all of the transmitters 449*0946e70aSDimitry Andric AnalyzeDefUseChain(SourceDef); 450*0946e70aSDimitry Andric auto &SourceDefTransmitters = Transmitters[SourceDef.Id]; 451*0946e70aSDimitry Andric if (SourceDefTransmitters.empty()) 452*0946e70aSDimitry Andric return; // No transmitters for `SourceDef` 453*0946e70aSDimitry Andric 454*0946e70aSDimitry Andric MachineInstr *Source = SourceDef.Addr->getFlags() & NodeAttrs::PhiRef 455*0946e70aSDimitry Andric ? MachineGadgetGraph::ArgNodeSentinel 456*0946e70aSDimitry Andric : SourceDef.Addr->getOp().getParent(); 457*0946e70aSDimitry Andric auto GadgetSource = MaybeAddNode(Source); 458*0946e70aSDimitry Andric // Each transmitter is a sink for `SourceDef`. 459*0946e70aSDimitry Andric for (auto TransmitterId : SourceDefTransmitters) { 460*0946e70aSDimitry Andric MachineInstr *Sink = DFG.addr<StmtNode *>(TransmitterId).Addr->getCode(); 461*0946e70aSDimitry Andric auto GadgetSink = MaybeAddNode(Sink); 462*0946e70aSDimitry Andric // Add the gadget edge to the graph. 463*0946e70aSDimitry Andric Builder.addEdge(MachineGadgetGraph::GadgetEdgeSentinel, 464*0946e70aSDimitry Andric GadgetSource.first, GadgetSink.first); 465*0946e70aSDimitry Andric ++GadgetCount; 466*0946e70aSDimitry Andric } 467*0946e70aSDimitry Andric }; 468*0946e70aSDimitry Andric 469*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Analyzing def-use chains to find gadgets\n"); 470*0946e70aSDimitry Andric // Analyze function arguments 471*0946e70aSDimitry Andric NodeAddr<BlockNode *> EntryBlock = DFG.getFunc().Addr->getEntryBlock(DFG); 472*0946e70aSDimitry Andric for (NodeAddr<PhiNode *> ArgPhi : 473*0946e70aSDimitry Andric EntryBlock.Addr->members_if(DataFlowGraph::IsPhi, DFG)) { 474*0946e70aSDimitry Andric NodeList Defs = ArgPhi.Addr->members_if(DataFlowGraph::IsDef, DFG); 475*0946e70aSDimitry Andric llvm::for_each(Defs, AnalyzeDef); 476*0946e70aSDimitry Andric } 477*0946e70aSDimitry Andric // Analyze every instruction in MF 478*0946e70aSDimitry Andric for (NodeAddr<BlockNode *> BA : DFG.getFunc().Addr->members(DFG)) { 479*0946e70aSDimitry Andric for (NodeAddr<StmtNode *> SA : 480*0946e70aSDimitry Andric BA.Addr->members_if(DataFlowGraph::IsCode<NodeAttrs::Stmt>, DFG)) { 481*0946e70aSDimitry Andric MachineInstr *MI = SA.Addr->getCode(); 482*0946e70aSDimitry Andric if (isFence(MI)) { 483*0946e70aSDimitry Andric MaybeAddNode(MI); 484*0946e70aSDimitry Andric ++FenceCount; 485*0946e70aSDimitry Andric } else if (MI->mayLoad()) { 486*0946e70aSDimitry Andric NodeList Defs = SA.Addr->members_if(DataFlowGraph::IsDef, DFG); 487*0946e70aSDimitry Andric llvm::for_each(Defs, AnalyzeDef); 488*0946e70aSDimitry Andric } 489*0946e70aSDimitry Andric } 490*0946e70aSDimitry Andric } 491*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Found " << FenceCount << " fences\n"); 492*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Found " << GadgetCount << " gadgets\n"); 493*0946e70aSDimitry Andric if (GadgetCount == 0) 494*0946e70aSDimitry Andric return nullptr; 495*0946e70aSDimitry Andric NumGadgets += GadgetCount; 496*0946e70aSDimitry Andric 497*0946e70aSDimitry Andric // Traverse CFG to build the rest of the graph 498*0946e70aSDimitry Andric SmallSet<MachineBasicBlock *, 8> BlocksVisited; 499*0946e70aSDimitry Andric std::function<void(MachineBasicBlock *, GraphIter, unsigned)> TraverseCFG = 500*0946e70aSDimitry Andric [&](MachineBasicBlock *MBB, GraphIter GI, unsigned ParentDepth) { 501*0946e70aSDimitry Andric unsigned LoopDepth = MLI.getLoopDepth(MBB); 502*0946e70aSDimitry Andric if (!MBB->empty()) { 503*0946e70aSDimitry Andric // Always add the first instruction in each block 504*0946e70aSDimitry Andric auto NI = MBB->begin(); 505*0946e70aSDimitry Andric auto BeginBB = MaybeAddNode(&*NI); 506*0946e70aSDimitry Andric Builder.addEdge(ParentDepth, GI, BeginBB.first); 507*0946e70aSDimitry Andric if (!BlocksVisited.insert(MBB).second) 508*0946e70aSDimitry Andric return; 509*0946e70aSDimitry Andric 510*0946e70aSDimitry Andric // Add any instructions within the block that are gadget components 511*0946e70aSDimitry Andric GI = BeginBB.first; 512*0946e70aSDimitry Andric while (++NI != MBB->end()) { 513*0946e70aSDimitry Andric auto Ref = NodeMap.find(&*NI); 514*0946e70aSDimitry Andric if (Ref != NodeMap.end()) { 515*0946e70aSDimitry Andric Builder.addEdge(LoopDepth, GI, Ref->getSecond()); 516*0946e70aSDimitry Andric GI = Ref->getSecond(); 517*0946e70aSDimitry Andric } 518*0946e70aSDimitry Andric } 519*0946e70aSDimitry Andric 520*0946e70aSDimitry Andric // Always add the terminator instruction, if one exists 521*0946e70aSDimitry Andric auto T = MBB->getFirstTerminator(); 522*0946e70aSDimitry Andric if (T != MBB->end()) { 523*0946e70aSDimitry Andric auto EndBB = MaybeAddNode(&*T); 524*0946e70aSDimitry Andric if (EndBB.second) 525*0946e70aSDimitry Andric Builder.addEdge(LoopDepth, GI, EndBB.first); 526*0946e70aSDimitry Andric GI = EndBB.first; 527*0946e70aSDimitry Andric } 528*0946e70aSDimitry Andric } 529*0946e70aSDimitry Andric for (MachineBasicBlock *Succ : MBB->successors()) 530*0946e70aSDimitry Andric TraverseCFG(Succ, GI, LoopDepth); 531*0946e70aSDimitry Andric }; 532*0946e70aSDimitry Andric // ArgNodeSentinel is a pseudo-instruction that represents MF args in the 533*0946e70aSDimitry Andric // GadgetGraph 534*0946e70aSDimitry Andric GraphIter ArgNode = MaybeAddNode(MachineGadgetGraph::ArgNodeSentinel).first; 535*0946e70aSDimitry Andric TraverseCFG(&MF.front(), ArgNode, 0); 536*0946e70aSDimitry Andric std::unique_ptr<MachineGadgetGraph> G{Builder.get(FenceCount, GadgetCount)}; 537*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Found " << G->nodes_size() << " nodes\n"); 538*0946e70aSDimitry Andric return G; 539*0946e70aSDimitry Andric } 540*0946e70aSDimitry Andric 541*0946e70aSDimitry Andric // Returns the number of remaining gadget edges that could not be eliminated 542*0946e70aSDimitry Andric int X86LoadValueInjectionLoadHardeningPass::elimMitigatedEdgesAndNodes( 543*0946e70aSDimitry Andric MachineGadgetGraph &G, MachineGadgetGraph::EdgeSet &ElimEdges /* in, out */, 544*0946e70aSDimitry Andric MachineGadgetGraph::NodeSet &ElimNodes /* in, out */) const { 545*0946e70aSDimitry Andric if (G.NumFences > 0) { 546*0946e70aSDimitry Andric // Eliminate fences and CFG edges that ingress and egress the fence, as 547*0946e70aSDimitry Andric // they are trivially mitigated. 548*0946e70aSDimitry Andric for (const auto &E : G.edges()) { 549*0946e70aSDimitry Andric const MachineGadgetGraph::Node *Dest = E.getDest(); 550*0946e70aSDimitry Andric if (isFence(Dest->getValue())) { 551*0946e70aSDimitry Andric ElimNodes.insert(*Dest); 552*0946e70aSDimitry Andric ElimEdges.insert(E); 553*0946e70aSDimitry Andric for (const auto &DE : Dest->edges()) 554*0946e70aSDimitry Andric ElimEdges.insert(DE); 555*0946e70aSDimitry Andric } 556*0946e70aSDimitry Andric } 557*0946e70aSDimitry Andric } 558*0946e70aSDimitry Andric 559*0946e70aSDimitry Andric // Find and eliminate gadget edges that have been mitigated. 560*0946e70aSDimitry Andric int MitigatedGadgets = 0, RemainingGadgets = 0; 561*0946e70aSDimitry Andric MachineGadgetGraph::NodeSet ReachableNodes{G}; 562*0946e70aSDimitry Andric for (const auto &RootN : G.nodes()) { 563*0946e70aSDimitry Andric if (llvm::none_of(RootN.edges(), MachineGadgetGraph::isGadgetEdge)) 564*0946e70aSDimitry Andric continue; // skip this node if it isn't a gadget source 565*0946e70aSDimitry Andric 566*0946e70aSDimitry Andric // Find all of the nodes that are CFG-reachable from RootN using DFS 567*0946e70aSDimitry Andric ReachableNodes.clear(); 568*0946e70aSDimitry Andric std::function<void(const MachineGadgetGraph::Node *, bool)> 569*0946e70aSDimitry Andric FindReachableNodes = 570*0946e70aSDimitry Andric [&](const MachineGadgetGraph::Node *N, bool FirstNode) { 571*0946e70aSDimitry Andric if (!FirstNode) 572*0946e70aSDimitry Andric ReachableNodes.insert(*N); 573*0946e70aSDimitry Andric for (const auto &E : N->edges()) { 574*0946e70aSDimitry Andric const MachineGadgetGraph::Node *Dest = E.getDest(); 575*0946e70aSDimitry Andric if (MachineGadgetGraph::isCFGEdge(E) && 576*0946e70aSDimitry Andric !ElimEdges.contains(E) && !ReachableNodes.contains(*Dest)) 577*0946e70aSDimitry Andric FindReachableNodes(Dest, false); 578*0946e70aSDimitry Andric } 579*0946e70aSDimitry Andric }; 580*0946e70aSDimitry Andric FindReachableNodes(&RootN, true); 581*0946e70aSDimitry Andric 582*0946e70aSDimitry Andric // Any gadget whose sink is unreachable has been mitigated 583*0946e70aSDimitry Andric for (const auto &E : RootN.edges()) { 584*0946e70aSDimitry Andric if (MachineGadgetGraph::isGadgetEdge(E)) { 585*0946e70aSDimitry Andric if (ReachableNodes.contains(*E.getDest())) { 586*0946e70aSDimitry Andric // This gadget's sink is reachable 587*0946e70aSDimitry Andric ++RemainingGadgets; 588*0946e70aSDimitry Andric } else { // This gadget's sink is unreachable, and therefore mitigated 589*0946e70aSDimitry Andric ++MitigatedGadgets; 590*0946e70aSDimitry Andric ElimEdges.insert(E); 591*0946e70aSDimitry Andric } 592*0946e70aSDimitry Andric } 593*0946e70aSDimitry Andric } 594*0946e70aSDimitry Andric } 595*0946e70aSDimitry Andric return RemainingGadgets; 596*0946e70aSDimitry Andric } 597*0946e70aSDimitry Andric 598*0946e70aSDimitry Andric std::unique_ptr<MachineGadgetGraph> 599*0946e70aSDimitry Andric X86LoadValueInjectionLoadHardeningPass::trimMitigatedEdges( 600*0946e70aSDimitry Andric std::unique_ptr<MachineGadgetGraph> Graph) const { 601*0946e70aSDimitry Andric MachineGadgetGraph::NodeSet ElimNodes{*Graph}; 602*0946e70aSDimitry Andric MachineGadgetGraph::EdgeSet ElimEdges{*Graph}; 603*0946e70aSDimitry Andric int RemainingGadgets = 604*0946e70aSDimitry Andric elimMitigatedEdgesAndNodes(*Graph, ElimEdges, ElimNodes); 605*0946e70aSDimitry Andric if (ElimEdges.empty() && ElimNodes.empty()) { 606*0946e70aSDimitry Andric Graph->NumFences = 0; 607*0946e70aSDimitry Andric Graph->NumGadgets = RemainingGadgets; 608*0946e70aSDimitry Andric } else { 609*0946e70aSDimitry Andric Graph = GraphBuilder::trim(*Graph, ElimNodes, ElimEdges, 0 /* NumFences */, 610*0946e70aSDimitry Andric RemainingGadgets); 611*0946e70aSDimitry Andric } 612*0946e70aSDimitry Andric return Graph; 613*0946e70aSDimitry Andric } 614*0946e70aSDimitry Andric 615*0946e70aSDimitry Andric int X86LoadValueInjectionLoadHardeningPass::hardenLoadsWithPlugin( 616*0946e70aSDimitry Andric MachineFunction &MF, std::unique_ptr<MachineGadgetGraph> Graph) const { 617*0946e70aSDimitry Andric int FencesInserted = 0; 618*0946e70aSDimitry Andric 619*0946e70aSDimitry Andric do { 620*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Eliminating mitigated paths...\n"); 621*0946e70aSDimitry Andric Graph = trimMitigatedEdges(std::move(Graph)); 622*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Eliminating mitigated paths... Done\n"); 623*0946e70aSDimitry Andric if (Graph->NumGadgets == 0) 624*0946e70aSDimitry Andric break; 625*0946e70aSDimitry Andric 626*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Cutting edges...\n"); 627*0946e70aSDimitry Andric EdgeSet CutEdges{*Graph}; 628*0946e70aSDimitry Andric auto Nodes = std::make_unique<unsigned int[]>(Graph->nodes_size() + 629*0946e70aSDimitry Andric 1 /* terminator node */); 630*0946e70aSDimitry Andric auto Edges = std::make_unique<unsigned int[]>(Graph->edges_size()); 631*0946e70aSDimitry Andric auto EdgeCuts = std::make_unique<int[]>(Graph->edges_size()); 632*0946e70aSDimitry Andric auto EdgeValues = std::make_unique<int[]>(Graph->edges_size()); 633*0946e70aSDimitry Andric for (const auto &N : Graph->nodes()) { 634*0946e70aSDimitry Andric Nodes[Graph->getNodeIndex(N)] = Graph->getEdgeIndex(*N.edges_begin()); 635*0946e70aSDimitry Andric } 636*0946e70aSDimitry Andric Nodes[Graph->nodes_size()] = Graph->edges_size(); // terminator node 637*0946e70aSDimitry Andric for (const auto &E : Graph->edges()) { 638*0946e70aSDimitry Andric Edges[Graph->getEdgeIndex(E)] = Graph->getNodeIndex(*E.getDest()); 639*0946e70aSDimitry Andric EdgeValues[Graph->getEdgeIndex(E)] = E.getValue(); 640*0946e70aSDimitry Andric } 641*0946e70aSDimitry Andric OptimizeCut(Nodes.get(), Graph->nodes_size(), Edges.get(), EdgeValues.get(), 642*0946e70aSDimitry Andric EdgeCuts.get(), Graph->edges_size()); 643*0946e70aSDimitry Andric for (int I = 0; I < Graph->edges_size(); ++I) 644*0946e70aSDimitry Andric if (EdgeCuts[I]) 645*0946e70aSDimitry Andric CutEdges.set(I); 646*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Cutting edges... Done\n"); 647*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Cut " << CutEdges.count() << " edges\n"); 648*0946e70aSDimitry Andric 649*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Inserting LFENCEs...\n"); 650*0946e70aSDimitry Andric FencesInserted += insertFences(MF, *Graph, CutEdges); 651*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Inserting LFENCEs... Done\n"); 652*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Inserted " << FencesInserted << " fences\n"); 653*0946e70aSDimitry Andric 654*0946e70aSDimitry Andric Graph = GraphBuilder::trim(*Graph, MachineGadgetGraph::NodeSet{*Graph}, 655*0946e70aSDimitry Andric CutEdges); 656*0946e70aSDimitry Andric } while (true); 657*0946e70aSDimitry Andric 658*0946e70aSDimitry Andric return FencesInserted; 659*0946e70aSDimitry Andric } 660*0946e70aSDimitry Andric 661*0946e70aSDimitry Andric int X86LoadValueInjectionLoadHardeningPass::hardenLoadsWithGreedyHeuristic( 662*0946e70aSDimitry Andric MachineFunction &MF, std::unique_ptr<MachineGadgetGraph> Graph) const { 663*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Eliminating mitigated paths...\n"); 664*0946e70aSDimitry Andric Graph = trimMitigatedEdges(std::move(Graph)); 665*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Eliminating mitigated paths... Done\n"); 666*0946e70aSDimitry Andric if (Graph->NumGadgets == 0) 667*0946e70aSDimitry Andric return 0; 668*0946e70aSDimitry Andric 669*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Cutting edges...\n"); 670*0946e70aSDimitry Andric MachineGadgetGraph::NodeSet ElimNodes{*Graph}, GadgetSinks{*Graph}; 671*0946e70aSDimitry Andric MachineGadgetGraph::EdgeSet ElimEdges{*Graph}, CutEdges{*Graph}; 672*0946e70aSDimitry Andric auto IsCFGEdge = [&ElimEdges, &CutEdges](const MachineGadgetGraph::Edge &E) { 673*0946e70aSDimitry Andric return !ElimEdges.contains(E) && !CutEdges.contains(E) && 674*0946e70aSDimitry Andric MachineGadgetGraph::isCFGEdge(E); 675*0946e70aSDimitry Andric }; 676*0946e70aSDimitry Andric auto IsGadgetEdge = [&ElimEdges, 677*0946e70aSDimitry Andric &CutEdges](const MachineGadgetGraph::Edge &E) { 678*0946e70aSDimitry Andric return !ElimEdges.contains(E) && !CutEdges.contains(E) && 679*0946e70aSDimitry Andric MachineGadgetGraph::isGadgetEdge(E); 680*0946e70aSDimitry Andric }; 681*0946e70aSDimitry Andric 682*0946e70aSDimitry Andric // FIXME: this is O(E^2), we could probably do better. 683*0946e70aSDimitry Andric do { 684*0946e70aSDimitry Andric // Find the cheapest CFG edge that will eliminate a gadget (by being 685*0946e70aSDimitry Andric // egress from a SOURCE node or ingress to a SINK node), and cut it. 686*0946e70aSDimitry Andric const MachineGadgetGraph::Edge *CheapestSoFar = nullptr; 687*0946e70aSDimitry Andric 688*0946e70aSDimitry Andric // First, collect all gadget source and sink nodes. 689*0946e70aSDimitry Andric MachineGadgetGraph::NodeSet GadgetSources{*Graph}, GadgetSinks{*Graph}; 690*0946e70aSDimitry Andric for (const auto &N : Graph->nodes()) { 691*0946e70aSDimitry Andric if (ElimNodes.contains(N)) 692*0946e70aSDimitry Andric continue; 693*0946e70aSDimitry Andric for (const auto &E : N.edges()) { 694*0946e70aSDimitry Andric if (IsGadgetEdge(E)) { 695*0946e70aSDimitry Andric GadgetSources.insert(N); 696*0946e70aSDimitry Andric GadgetSinks.insert(*E.getDest()); 697*0946e70aSDimitry Andric } 698*0946e70aSDimitry Andric } 699*0946e70aSDimitry Andric } 700*0946e70aSDimitry Andric 701*0946e70aSDimitry Andric // Next, look for the cheapest CFG edge which, when cut, is guaranteed to 702*0946e70aSDimitry Andric // mitigate at least one gadget by either: 703*0946e70aSDimitry Andric // (a) being egress from a gadget source, or 704*0946e70aSDimitry Andric // (b) being ingress to a gadget sink. 705*0946e70aSDimitry Andric for (const auto &N : Graph->nodes()) { 706*0946e70aSDimitry Andric if (ElimNodes.contains(N)) 707*0946e70aSDimitry Andric continue; 708*0946e70aSDimitry Andric for (const auto &E : N.edges()) { 709*0946e70aSDimitry Andric if (IsCFGEdge(E)) { 710*0946e70aSDimitry Andric if (GadgetSources.contains(N) || GadgetSinks.contains(*E.getDest())) { 711*0946e70aSDimitry Andric if (!CheapestSoFar || E.getValue() < CheapestSoFar->getValue()) 712*0946e70aSDimitry Andric CheapestSoFar = &E; 713*0946e70aSDimitry Andric } 714*0946e70aSDimitry Andric } 715*0946e70aSDimitry Andric } 716*0946e70aSDimitry Andric } 717*0946e70aSDimitry Andric 718*0946e70aSDimitry Andric assert(CheapestSoFar && "Failed to cut an edge"); 719*0946e70aSDimitry Andric CutEdges.insert(*CheapestSoFar); 720*0946e70aSDimitry Andric ElimEdges.insert(*CheapestSoFar); 721*0946e70aSDimitry Andric } while (elimMitigatedEdgesAndNodes(*Graph, ElimEdges, ElimNodes)); 722*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Cutting edges... Done\n"); 723*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Cut " << CutEdges.count() << " edges\n"); 724*0946e70aSDimitry Andric 725*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Inserting LFENCEs...\n"); 726*0946e70aSDimitry Andric int FencesInserted = insertFences(MF, *Graph, CutEdges); 727*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Inserting LFENCEs... Done\n"); 728*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Inserted " << FencesInserted << " fences\n"); 729*0946e70aSDimitry Andric 730*0946e70aSDimitry Andric return FencesInserted; 731*0946e70aSDimitry Andric } 732*0946e70aSDimitry Andric 733*0946e70aSDimitry Andric int X86LoadValueInjectionLoadHardeningPass::insertFences( 734*0946e70aSDimitry Andric MachineFunction &MF, MachineGadgetGraph &G, 735*0946e70aSDimitry Andric EdgeSet &CutEdges /* in, out */) const { 736*0946e70aSDimitry Andric int FencesInserted = 0; 737*0946e70aSDimitry Andric for (const auto &N : G.nodes()) { 738*0946e70aSDimitry Andric for (const auto &E : N.edges()) { 739*0946e70aSDimitry Andric if (CutEdges.contains(E)) { 740*0946e70aSDimitry Andric MachineInstr *MI = N.getValue(), *Prev; 741*0946e70aSDimitry Andric MachineBasicBlock *MBB; // Insert an LFENCE in this MBB 742*0946e70aSDimitry Andric MachineBasicBlock::iterator InsertionPt; // ...at this point 743*0946e70aSDimitry Andric if (MI == MachineGadgetGraph::ArgNodeSentinel) { 744*0946e70aSDimitry Andric // insert LFENCE at beginning of entry block 745*0946e70aSDimitry Andric MBB = &MF.front(); 746*0946e70aSDimitry Andric InsertionPt = MBB->begin(); 747*0946e70aSDimitry Andric Prev = nullptr; 748*0946e70aSDimitry Andric } else if (MI->isBranch()) { // insert the LFENCE before the branch 749*0946e70aSDimitry Andric MBB = MI->getParent(); 750*0946e70aSDimitry Andric InsertionPt = MI; 751*0946e70aSDimitry Andric Prev = MI->getPrevNode(); 752*0946e70aSDimitry Andric // Remove all egress CFG edges from this branch because the inserted 753*0946e70aSDimitry Andric // LFENCE prevents gadgets from crossing the branch. 754*0946e70aSDimitry Andric for (const auto &E : N.edges()) { 755*0946e70aSDimitry Andric if (MachineGadgetGraph::isCFGEdge(E)) 756*0946e70aSDimitry Andric CutEdges.insert(E); 757*0946e70aSDimitry Andric } 758*0946e70aSDimitry Andric } else { // insert the LFENCE after the instruction 759*0946e70aSDimitry Andric MBB = MI->getParent(); 760*0946e70aSDimitry Andric InsertionPt = MI->getNextNode() ? MI->getNextNode() : MBB->end(); 761*0946e70aSDimitry Andric Prev = InsertionPt == MBB->end() 762*0946e70aSDimitry Andric ? (MBB->empty() ? nullptr : &MBB->back()) 763*0946e70aSDimitry Andric : InsertionPt->getPrevNode(); 764*0946e70aSDimitry Andric } 765*0946e70aSDimitry Andric // Ensure this insertion is not redundant (two LFENCEs in sequence). 766*0946e70aSDimitry Andric if ((InsertionPt == MBB->end() || !isFence(&*InsertionPt)) && 767*0946e70aSDimitry Andric (!Prev || !isFence(Prev))) { 768*0946e70aSDimitry Andric BuildMI(*MBB, InsertionPt, DebugLoc(), TII->get(X86::LFENCE)); 769*0946e70aSDimitry Andric ++FencesInserted; 770*0946e70aSDimitry Andric } 771*0946e70aSDimitry Andric } 772*0946e70aSDimitry Andric } 773*0946e70aSDimitry Andric } 774*0946e70aSDimitry Andric return FencesInserted; 775*0946e70aSDimitry Andric } 776*0946e70aSDimitry Andric 777*0946e70aSDimitry Andric bool X86LoadValueInjectionLoadHardeningPass::instrUsesRegToAccessMemory( 778*0946e70aSDimitry Andric const MachineInstr &MI, unsigned Reg) const { 779*0946e70aSDimitry Andric if (!MI.mayLoadOrStore() || MI.getOpcode() == X86::MFENCE || 780*0946e70aSDimitry Andric MI.getOpcode() == X86::SFENCE || MI.getOpcode() == X86::LFENCE) 781*0946e70aSDimitry Andric return false; 782*0946e70aSDimitry Andric 783*0946e70aSDimitry Andric // FIXME: This does not handle pseudo loading instruction like TCRETURN* 784*0946e70aSDimitry Andric const MCInstrDesc &Desc = MI.getDesc(); 785*0946e70aSDimitry Andric int MemRefBeginIdx = X86II::getMemoryOperandNo(Desc.TSFlags); 786*0946e70aSDimitry Andric if (MemRefBeginIdx < 0) { 787*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "Warning: unable to obtain memory operand for loading " 788*0946e70aSDimitry Andric "instruction:\n"; 789*0946e70aSDimitry Andric MI.print(dbgs()); dbgs() << '\n';); 790*0946e70aSDimitry Andric return false; 791*0946e70aSDimitry Andric } 792*0946e70aSDimitry Andric MemRefBeginIdx += X86II::getOperandBias(Desc); 793*0946e70aSDimitry Andric 794*0946e70aSDimitry Andric const MachineOperand &BaseMO = 795*0946e70aSDimitry Andric MI.getOperand(MemRefBeginIdx + X86::AddrBaseReg); 796*0946e70aSDimitry Andric const MachineOperand &IndexMO = 797*0946e70aSDimitry Andric MI.getOperand(MemRefBeginIdx + X86::AddrIndexReg); 798*0946e70aSDimitry Andric return (BaseMO.isReg() && BaseMO.getReg() != X86::NoRegister && 799*0946e70aSDimitry Andric TRI->regsOverlap(BaseMO.getReg(), Reg)) || 800*0946e70aSDimitry Andric (IndexMO.isReg() && IndexMO.getReg() != X86::NoRegister && 801*0946e70aSDimitry Andric TRI->regsOverlap(IndexMO.getReg(), Reg)); 802*0946e70aSDimitry Andric } 803*0946e70aSDimitry Andric 804*0946e70aSDimitry Andric bool X86LoadValueInjectionLoadHardeningPass::instrUsesRegToBranch( 805*0946e70aSDimitry Andric const MachineInstr &MI, unsigned Reg) const { 806*0946e70aSDimitry Andric if (!MI.isConditionalBranch()) 807*0946e70aSDimitry Andric return false; 808*0946e70aSDimitry Andric for (const MachineOperand &Use : MI.uses()) 809*0946e70aSDimitry Andric if (Use.isReg() && Use.getReg() == Reg) 810*0946e70aSDimitry Andric return true; 811*0946e70aSDimitry Andric return false; 812*0946e70aSDimitry Andric } 813*0946e70aSDimitry Andric 814*0946e70aSDimitry Andric INITIALIZE_PASS_BEGIN(X86LoadValueInjectionLoadHardeningPass, PASS_KEY, 815*0946e70aSDimitry Andric "X86 LVI load hardening", false, false) 816*0946e70aSDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 817*0946e70aSDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 818*0946e70aSDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominanceFrontier) 819*0946e70aSDimitry Andric INITIALIZE_PASS_END(X86LoadValueInjectionLoadHardeningPass, PASS_KEY, 820*0946e70aSDimitry Andric "X86 LVI load hardening", false, false) 821*0946e70aSDimitry Andric 822*0946e70aSDimitry Andric FunctionPass *llvm::createX86LoadValueInjectionLoadHardeningPass() { 823*0946e70aSDimitry Andric return new X86LoadValueInjectionLoadHardeningPass(); 824*0946e70aSDimitry Andric } 825*0946e70aSDimitry Andric 826*0946e70aSDimitry Andric namespace { 827*0946e70aSDimitry Andric 828*0946e70aSDimitry Andric /// The `X86LoadValueInjectionLoadHardeningPass` above depends on expensive 829*0946e70aSDimitry Andric /// analysis passes that add complexity to the pipeline. This complexity 830*0946e70aSDimitry Andric /// can cause noticable overhead when no optimizations are enabled, i.e., -O0. 831*0946e70aSDimitry Andric /// The purpose of `X86LoadValueInjectionLoadHardeningUnoptimizedPass` is to 832*0946e70aSDimitry Andric /// provide the same security as the optimized pass, but without adding 833*0946e70aSDimitry Andric /// unnecessary complexity to the LLVM pipeline. 834*0946e70aSDimitry Andric /// 835*0946e70aSDimitry Andric /// The behavior of this pass is simply to insert an LFENCE after every load 836*0946e70aSDimitry Andric /// instruction. 837*0946e70aSDimitry Andric class X86LoadValueInjectionLoadHardeningUnoptimizedPass 838*0946e70aSDimitry Andric : public MachineFunctionPass { 839*0946e70aSDimitry Andric public: 840*0946e70aSDimitry Andric X86LoadValueInjectionLoadHardeningUnoptimizedPass() 841*0946e70aSDimitry Andric : MachineFunctionPass(ID) {} 842*0946e70aSDimitry Andric 843*0946e70aSDimitry Andric StringRef getPassName() const override { 844*0946e70aSDimitry Andric return "X86 Load Value Injection (LVI) Load Hardening (Unoptimized)"; 845*0946e70aSDimitry Andric } 846*0946e70aSDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 847*0946e70aSDimitry Andric static char ID; 848*0946e70aSDimitry Andric }; 849*0946e70aSDimitry Andric 850*0946e70aSDimitry Andric } // end anonymous namespace 851*0946e70aSDimitry Andric 852*0946e70aSDimitry Andric char X86LoadValueInjectionLoadHardeningUnoptimizedPass::ID = 0; 853*0946e70aSDimitry Andric 854*0946e70aSDimitry Andric bool X86LoadValueInjectionLoadHardeningUnoptimizedPass::runOnMachineFunction( 855*0946e70aSDimitry Andric MachineFunction &MF) { 856*0946e70aSDimitry Andric LLVM_DEBUG(dbgs() << "***** " << getPassName() << " : " << MF.getName() 857*0946e70aSDimitry Andric << " *****\n"); 858*0946e70aSDimitry Andric const X86Subtarget *STI = &MF.getSubtarget<X86Subtarget>(); 859*0946e70aSDimitry Andric if (!STI->useLVILoadHardening()) 860*0946e70aSDimitry Andric return false; 861*0946e70aSDimitry Andric 862*0946e70aSDimitry Andric // FIXME: support 32-bit 863*0946e70aSDimitry Andric if (!STI->is64Bit()) 864*0946e70aSDimitry Andric report_fatal_error("LVI load hardening is only supported on 64-bit", false); 865*0946e70aSDimitry Andric 866*0946e70aSDimitry Andric // Don't skip functions with the "optnone" attr but participate in opt-bisect. 867*0946e70aSDimitry Andric const Function &F = MF.getFunction(); 868*0946e70aSDimitry Andric if (!F.hasOptNone() && skipFunction(F)) 869*0946e70aSDimitry Andric return false; 870*0946e70aSDimitry Andric 871*0946e70aSDimitry Andric bool Modified = false; 872*0946e70aSDimitry Andric ++NumFunctionsConsidered; 873*0946e70aSDimitry Andric 874*0946e70aSDimitry Andric const TargetInstrInfo *TII = STI->getInstrInfo(); 875*0946e70aSDimitry Andric for (auto &MBB : MF) { 876*0946e70aSDimitry Andric for (auto &MI : MBB) { 877*0946e70aSDimitry Andric if (!MI.mayLoad() || MI.getOpcode() == X86::LFENCE || 878*0946e70aSDimitry Andric MI.getOpcode() == X86::MFENCE) 879*0946e70aSDimitry Andric continue; 880*0946e70aSDimitry Andric 881*0946e70aSDimitry Andric MachineBasicBlock::iterator InsertionPt = 882*0946e70aSDimitry Andric MI.getNextNode() ? MI.getNextNode() : MBB.end(); 883*0946e70aSDimitry Andric BuildMI(MBB, InsertionPt, DebugLoc(), TII->get(X86::LFENCE)); 884*0946e70aSDimitry Andric ++NumFences; 885*0946e70aSDimitry Andric Modified = true; 886*0946e70aSDimitry Andric } 887*0946e70aSDimitry Andric } 888*0946e70aSDimitry Andric 889*0946e70aSDimitry Andric if (Modified) 890*0946e70aSDimitry Andric ++NumFunctionsMitigated; 891*0946e70aSDimitry Andric 892*0946e70aSDimitry Andric return Modified; 893*0946e70aSDimitry Andric } 894*0946e70aSDimitry Andric 895*0946e70aSDimitry Andric INITIALIZE_PASS(X86LoadValueInjectionLoadHardeningUnoptimizedPass, PASS_KEY, 896*0946e70aSDimitry Andric "X86 LVI load hardening", false, false) 897*0946e70aSDimitry Andric 898*0946e70aSDimitry Andric FunctionPass *llvm::createX86LoadValueInjectionLoadHardeningUnoptimizedPass() { 899*0946e70aSDimitry Andric return new X86LoadValueInjectionLoadHardeningUnoptimizedPass(); 900*0946e70aSDimitry Andric } 901