17a7e6055SDimitry Andric //===- StackProtector.cpp - Stack Protector Insertion ---------------------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky //                     The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // This pass inserts stack protectors into functions which need them. A variable
11f22ef01cSRoman Divacky // with a random value in it is stored onto the stack before the local variables
12f22ef01cSRoman Divacky // are allocated. Upon exiting the block, the stored value is checked. If it's
13f22ef01cSRoman Divacky // changed, then there was some sort of violation and the program aborts.
14f22ef01cSRoman Divacky //
15f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
16f22ef01cSRoman Divacky 
172cab237bSDimitry Andric #include "llvm/CodeGen/StackProtector.h"
18139f7f9bSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
19139f7f9bSDimitry Andric #include "llvm/ADT/Statistic.h"
2039d628a0SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h"
213ca95b02SDimitry Andric #include "llvm/Analysis/EHPersonalities.h"
222cab237bSDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h"
2391bc56edSDimitry Andric #include "llvm/CodeGen/Passes.h"
242cab237bSDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
25db17bf38SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
262cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
27139f7f9bSDimitry Andric #include "llvm/IR/Attributes.h"
287a7e6055SDimitry Andric #include "llvm/IR/BasicBlock.h"
29139f7f9bSDimitry Andric #include "llvm/IR/Constants.h"
30139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h"
313ca95b02SDimitry Andric #include "llvm/IR/DebugInfo.h"
327a7e6055SDimitry Andric #include "llvm/IR/DebugLoc.h"
33139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h"
34db17bf38SDimitry Andric #include "llvm/IR/Dominators.h"
35139f7f9bSDimitry Andric #include "llvm/IR/Function.h"
36f785676fSDimitry Andric #include "llvm/IR/IRBuilder.h"
377a7e6055SDimitry Andric #include "llvm/IR/Instruction.h"
38139f7f9bSDimitry Andric #include "llvm/IR/Instructions.h"
394ba319b5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
40139f7f9bSDimitry Andric #include "llvm/IR/Intrinsics.h"
4139d628a0SDimitry Andric #include "llvm/IR/MDBuilder.h"
42139f7f9bSDimitry Andric #include "llvm/IR/Module.h"
437a7e6055SDimitry Andric #include "llvm/IR/Type.h"
447a7e6055SDimitry Andric #include "llvm/IR/User.h"
457a7e6055SDimitry Andric #include "llvm/Pass.h"
467a7e6055SDimitry Andric #include "llvm/Support/Casting.h"
47f22ef01cSRoman Divacky #include "llvm/Support/CommandLine.h"
487a7e6055SDimitry Andric #include "llvm/Target/TargetMachine.h"
497a7e6055SDimitry Andric #include "llvm/Target/TargetOptions.h"
507a7e6055SDimitry Andric #include <utility>
517a7e6055SDimitry Andric 
52f22ef01cSRoman Divacky using namespace llvm;
53f22ef01cSRoman Divacky 
5491bc56edSDimitry Andric #define DEBUG_TYPE "stack-protector"
5591bc56edSDimitry Andric 
56139f7f9bSDimitry Andric STATISTIC(NumFunProtected, "Number of functions protected");
57139f7f9bSDimitry Andric STATISTIC(NumAddrTaken, "Number of local variables that have their address"
58139f7f9bSDimitry Andric                         " taken.");
59139f7f9bSDimitry Andric 
60f785676fSDimitry Andric static cl::opt<bool> EnableSelectionDAGSP("enable-selectiondag-sp",
61f785676fSDimitry Andric                                           cl::init(true), cl::Hidden);
62f22ef01cSRoman Divacky 
63f22ef01cSRoman Divacky char StackProtector::ID = 0;
64db17bf38SDimitry Andric 
65302affcbSDimitry Andric INITIALIZE_PASS_BEGIN(StackProtector, DEBUG_TYPE,
66d8866befSDimitry Andric                       "Insert stack protectors", false, true)
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)67d8866befSDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
68302affcbSDimitry Andric INITIALIZE_PASS_END(StackProtector, DEBUG_TYPE,
69d8866befSDimitry Andric                     "Insert stack protectors", false, true)
70f22ef01cSRoman Divacky 
71d8866befSDimitry Andric FunctionPass *llvm::createStackProtectorPass() { return new StackProtector(); }
72f785676fSDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const73db17bf38SDimitry Andric void StackProtector::getAnalysisUsage(AnalysisUsage &AU) const {
74db17bf38SDimitry Andric   AU.addRequired<TargetPassConfig>();
75db17bf38SDimitry Andric   AU.addPreserved<DominatorTreeWrapperPass>();
76db17bf38SDimitry Andric }
77db17bf38SDimitry Andric 
runOnFunction(Function & Fn)78f22ef01cSRoman Divacky bool StackProtector::runOnFunction(Function &Fn) {
79f22ef01cSRoman Divacky   F = &Fn;
80f22ef01cSRoman Divacky   M = F->getParent();
8191bc56edSDimitry Andric   DominatorTreeWrapperPass *DTWP =
8291bc56edSDimitry Andric       getAnalysisIfAvailable<DominatorTreeWrapperPass>();
8391bc56edSDimitry Andric   DT = DTWP ? &DTWP->getDomTree() : nullptr;
84d8866befSDimitry Andric   TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
85d8866befSDimitry Andric   Trip = TM->getTargetTriple();
86ff0cc061SDimitry Andric   TLI = TM->getSubtargetImpl(Fn)->getTargetLowering();
873ca95b02SDimitry Andric   HasPrologue = false;
883ca95b02SDimitry Andric   HasIRCheck = false;
89f22ef01cSRoman Divacky 
90ff0cc061SDimitry Andric   Attribute Attr = Fn.getFnAttribute("stack-protector-buffer-size");
9191bc56edSDimitry Andric   if (Attr.isStringAttribute() &&
9291bc56edSDimitry Andric       Attr.getValueAsString().getAsInteger(10, SSPBufferSize))
9391bc56edSDimitry Andric     return false; // Invalid integer string
9491bc56edSDimitry Andric 
9591bc56edSDimitry Andric   if (!RequiresStackProtector())
9691bc56edSDimitry Andric     return false;
97f22ef01cSRoman Divacky 
983ca95b02SDimitry Andric   // TODO(etienneb): Functions with funclets are not correctly supported now.
993ca95b02SDimitry Andric   // Do nothing if this is funclet-based personality.
1003ca95b02SDimitry Andric   if (Fn.hasPersonalityFn()) {
1013ca95b02SDimitry Andric     EHPersonality Personality = classifyEHPersonality(Fn.getPersonalityFn());
1023ca95b02SDimitry Andric     if (isFuncletEHPersonality(Personality))
1033ca95b02SDimitry Andric       return false;
1043ca95b02SDimitry Andric   }
1053ca95b02SDimitry Andric 
106139f7f9bSDimitry Andric   ++NumFunProtected;
107f22ef01cSRoman Divacky   return InsertStackProtectors();
108f22ef01cSRoman Divacky }
109f22ef01cSRoman Divacky 
110f785676fSDimitry Andric /// \param [out] IsLarge is set to true if a protectable array is found and
111f785676fSDimitry Andric /// it is "large" ( >= ssp-buffer-size).  In the case of a structure with
112f785676fSDimitry Andric /// multiple arrays, this gets set if any of them is large.
ContainsProtectableArray(Type * Ty,bool & IsLarge,bool Strong,bool InStruct) const113f785676fSDimitry Andric bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge,
114f785676fSDimitry Andric                                               bool Strong,
115139f7f9bSDimitry Andric                                               bool InStruct) const {
116f785676fSDimitry Andric   if (!Ty)
117f785676fSDimitry Andric     return false;
1183861d79fSDimitry Andric   if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
1193861d79fSDimitry Andric     if (!AT->getElementType()->isIntegerTy(8)) {
1203861d79fSDimitry Andric       // If we're on a non-Darwin platform or we're inside of a structure, don't
1213861d79fSDimitry Andric       // add stack protectors unless the array is a character array.
122f785676fSDimitry Andric       // However, in strong mode any array, regardless of type and size,
123f785676fSDimitry Andric       // triggers a protector.
124f785676fSDimitry Andric       if (!Strong && (InStruct || !Trip.isOSDarwin()))
1253861d79fSDimitry Andric         return false;
1263861d79fSDimitry Andric     }
1273861d79fSDimitry Andric 
1283861d79fSDimitry Andric     // If an array has more than SSPBufferSize bytes of allocated space, then we
1293861d79fSDimitry Andric     // emit stack protectors.
130875ed548SDimitry Andric     if (SSPBufferSize <= M->getDataLayout().getTypeAllocSize(AT)) {
131f785676fSDimitry Andric       IsLarge = true;
132f785676fSDimitry Andric       return true;
133f785676fSDimitry Andric     }
134f785676fSDimitry Andric 
135f785676fSDimitry Andric     if (Strong)
136f785676fSDimitry Andric       // Require a protector for all arrays in strong mode
1373861d79fSDimitry Andric       return true;
1383861d79fSDimitry Andric   }
1393861d79fSDimitry Andric 
1403861d79fSDimitry Andric   const StructType *ST = dyn_cast<StructType>(Ty);
141f785676fSDimitry Andric   if (!ST)
1423861d79fSDimitry Andric     return false;
143f785676fSDimitry Andric 
144f785676fSDimitry Andric   bool NeedsProtector = false;
145f785676fSDimitry Andric   for (StructType::element_iterator I = ST->element_begin(),
146f785676fSDimitry Andric                                     E = ST->element_end();
147f785676fSDimitry Andric        I != E; ++I)
148f785676fSDimitry Andric     if (ContainsProtectableArray(*I, IsLarge, Strong, true)) {
149f785676fSDimitry Andric       // If the element is a protectable array and is large (>= SSPBufferSize)
150f785676fSDimitry Andric       // then we are done.  If the protectable array is not large, then
151f785676fSDimitry Andric       // keep looking in case a subsequent element is a large array.
152f785676fSDimitry Andric       if (IsLarge)
153f785676fSDimitry Andric         return true;
154f785676fSDimitry Andric       NeedsProtector = true;
155f785676fSDimitry Andric     }
156f785676fSDimitry Andric 
157f785676fSDimitry Andric   return NeedsProtector;
1583861d79fSDimitry Andric }
1593861d79fSDimitry Andric 
HasAddressTaken(const Instruction * AI)160139f7f9bSDimitry Andric bool StackProtector::HasAddressTaken(const Instruction *AI) {
16191bc56edSDimitry Andric   for (const User *U : AI->users()) {
162139f7f9bSDimitry Andric     if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
163139f7f9bSDimitry Andric       if (AI == SI->getValueOperand())
164f22ef01cSRoman Divacky         return true;
165139f7f9bSDimitry Andric     } else if (const PtrToIntInst *SI = dyn_cast<PtrToIntInst>(U)) {
166139f7f9bSDimitry Andric       if (AI == SI->getOperand(0))
167139f7f9bSDimitry Andric         return true;
1684ba319b5SDimitry Andric     } else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
1694ba319b5SDimitry Andric       // Ignore intrinsics that are not calls. TODO: Use isLoweredToCall().
170*b5893f02SDimitry Andric       if (!isa<DbgInfoIntrinsic>(CI) && !CI->isLifetimeStartOrEnd())
171139f7f9bSDimitry Andric         return true;
172139f7f9bSDimitry Andric     } else if (isa<InvokeInst>(U)) {
173139f7f9bSDimitry Andric       return true;
174139f7f9bSDimitry Andric     } else if (const SelectInst *SI = dyn_cast<SelectInst>(U)) {
175139f7f9bSDimitry Andric       if (HasAddressTaken(SI))
176139f7f9bSDimitry Andric         return true;
177139f7f9bSDimitry Andric     } else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
178139f7f9bSDimitry Andric       // Keep track of what PHI nodes we have already visited to ensure
179139f7f9bSDimitry Andric       // they are only visited once.
18039d628a0SDimitry Andric       if (VisitedPHIs.insert(PN).second)
181139f7f9bSDimitry Andric         if (HasAddressTaken(PN))
182139f7f9bSDimitry Andric           return true;
183139f7f9bSDimitry Andric     } else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
184139f7f9bSDimitry Andric       if (HasAddressTaken(GEP))
185139f7f9bSDimitry Andric         return true;
186139f7f9bSDimitry Andric     } else if (const BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
187139f7f9bSDimitry Andric       if (HasAddressTaken(BI))
188139f7f9bSDimitry Andric         return true;
189139f7f9bSDimitry Andric     }
190139f7f9bSDimitry Andric   }
191139f7f9bSDimitry Andric   return false;
192139f7f9bSDimitry Andric }
193f22ef01cSRoman Divacky 
194*b5893f02SDimitry Andric /// Search for the first call to the llvm.stackprotector intrinsic and return it
195*b5893f02SDimitry Andric /// if present.
findStackProtectorIntrinsic(Function & F)196*b5893f02SDimitry Andric static const CallInst *findStackProtectorIntrinsic(Function &F) {
197*b5893f02SDimitry Andric   for (const BasicBlock &BB : F)
198*b5893f02SDimitry Andric     for (const Instruction &I : BB)
199*b5893f02SDimitry Andric       if (const CallInst *CI = dyn_cast<CallInst>(&I))
200*b5893f02SDimitry Andric         if (CI->getCalledFunction() ==
201*b5893f02SDimitry Andric             Intrinsic::getDeclaration(F.getParent(), Intrinsic::stackprotector))
202*b5893f02SDimitry Andric           return CI;
203*b5893f02SDimitry Andric   return nullptr;
204*b5893f02SDimitry Andric }
205*b5893f02SDimitry Andric 
2064ba319b5SDimitry Andric /// Check whether or not this function needs a stack protector based
207139f7f9bSDimitry Andric /// upon the stack protector level.
208139f7f9bSDimitry Andric ///
209139f7f9bSDimitry Andric /// We use two heuristics: a standard (ssp) and strong (sspstrong).
210139f7f9bSDimitry Andric /// The standard heuristic which will add a guard variable to functions that
211139f7f9bSDimitry Andric /// call alloca with a either a variable size or a size >= SSPBufferSize,
212139f7f9bSDimitry Andric /// functions with character buffers larger than SSPBufferSize, and functions
213139f7f9bSDimitry Andric /// with aggregates containing character buffers larger than SSPBufferSize. The
214139f7f9bSDimitry Andric /// strong heuristic will add a guard variables to functions that call alloca
215139f7f9bSDimitry Andric /// regardless of size, functions with any buffer regardless of type and size,
216139f7f9bSDimitry Andric /// functions with aggregates that contain any buffer regardless of type and
217139f7f9bSDimitry Andric /// size, and functions that contain stack-based variables that have had their
218139f7f9bSDimitry Andric /// address taken.
RequiresStackProtector()219139f7f9bSDimitry Andric bool StackProtector::RequiresStackProtector() {
220139f7f9bSDimitry Andric   bool Strong = false;
221f785676fSDimitry Andric   bool NeedsProtector = false;
222*b5893f02SDimitry Andric   HasPrologue = findStackProtectorIntrinsic(*F);
2233ca95b02SDimitry Andric 
2243ca95b02SDimitry Andric   if (F->hasFnAttribute(Attribute::SafeStack))
2253ca95b02SDimitry Andric     return false;
2263ca95b02SDimitry Andric 
2277a7e6055SDimitry Andric   // We are constructing the OptimizationRemarkEmitter on the fly rather than
2287a7e6055SDimitry Andric   // using the analysis pass to avoid building DominatorTree and LoopInfo which
2297a7e6055SDimitry Andric   // are not available this late in the IR pipeline.
2307a7e6055SDimitry Andric   OptimizationRemarkEmitter ORE(F);
2317a7e6055SDimitry Andric 
232ff0cc061SDimitry Andric   if (F->hasFnAttribute(Attribute::StackProtectReq)) {
2332cab237bSDimitry Andric     ORE.emit([&]() {
2342cab237bSDimitry Andric       return OptimizationRemark(DEBUG_TYPE, "StackProtectorRequested", F)
2357a7e6055SDimitry Andric              << "Stack protection applied to function "
2367a7e6055SDimitry Andric              << ore::NV("Function", F)
2372cab237bSDimitry Andric              << " due to a function attribute or command-line switch";
2382cab237bSDimitry Andric     });
239f785676fSDimitry Andric     NeedsProtector = true;
240f785676fSDimitry Andric     Strong = true; // Use the same heuristic as strong to determine SSPLayout
241ff0cc061SDimitry Andric   } else if (F->hasFnAttribute(Attribute::StackProtectStrong))
242139f7f9bSDimitry Andric     Strong = true;
2433ca95b02SDimitry Andric   else if (HasPrologue)
2443ca95b02SDimitry Andric     NeedsProtector = true;
245ff0cc061SDimitry Andric   else if (!F->hasFnAttribute(Attribute::StackProtect))
246f22ef01cSRoman Divacky     return false;
247f22ef01cSRoman Divacky 
24839d628a0SDimitry Andric   for (const BasicBlock &BB : *F) {
24939d628a0SDimitry Andric     for (const Instruction &I : BB) {
25039d628a0SDimitry Andric       if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
251139f7f9bSDimitry Andric         if (AI->isArrayAllocation()) {
2522cab237bSDimitry Andric           auto RemarkBuilder = [&]() {
2532cab237bSDimitry Andric             return OptimizationRemark(DEBUG_TYPE, "StackProtectorAllocaOrArray",
2542cab237bSDimitry Andric                                       &I)
2557a7e6055SDimitry Andric                    << "Stack protection applied to function "
2567a7e6055SDimitry Andric                    << ore::NV("Function", F)
2572cab237bSDimitry Andric                    << " due to a call to alloca or use of a variable length "
2582cab237bSDimitry Andric                       "array";
2592cab237bSDimitry Andric           };
26039d628a0SDimitry Andric           if (const auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) {
261f785676fSDimitry Andric             if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) {
262139f7f9bSDimitry Andric               // A call to alloca with size >= SSPBufferSize requires
263139f7f9bSDimitry Andric               // stack protectors.
2644ba319b5SDimitry Andric               Layout.insert(std::make_pair(AI,
2654ba319b5SDimitry Andric                                            MachineFrameInfo::SSPLK_LargeArray));
2662cab237bSDimitry Andric               ORE.emit(RemarkBuilder);
267f785676fSDimitry Andric               NeedsProtector = true;
268f785676fSDimitry Andric             } else if (Strong) {
269f785676fSDimitry Andric               // Require protectors for all alloca calls in strong mode.
2704ba319b5SDimitry Andric               Layout.insert(std::make_pair(AI,
2714ba319b5SDimitry Andric                                            MachineFrameInfo::SSPLK_SmallArray));
2722cab237bSDimitry Andric               ORE.emit(RemarkBuilder);
273f785676fSDimitry Andric               NeedsProtector = true;
274f785676fSDimitry Andric             }
275f785676fSDimitry Andric           } else {
276f785676fSDimitry Andric             // A call to alloca with a variable size requires protectors.
2774ba319b5SDimitry Andric             Layout.insert(std::make_pair(AI,
2784ba319b5SDimitry Andric                                          MachineFrameInfo::SSPLK_LargeArray));
2792cab237bSDimitry Andric             ORE.emit(RemarkBuilder);
280f785676fSDimitry Andric             NeedsProtector = true;
281f785676fSDimitry Andric           }
282f785676fSDimitry Andric           continue;
283139f7f9bSDimitry Andric         }
284139f7f9bSDimitry Andric 
285f785676fSDimitry Andric         bool IsLarge = false;
286f785676fSDimitry Andric         if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) {
2874ba319b5SDimitry Andric           Layout.insert(std::make_pair(AI, IsLarge
2884ba319b5SDimitry Andric                                        ? MachineFrameInfo::SSPLK_LargeArray
2894ba319b5SDimitry Andric                                        : MachineFrameInfo::SSPLK_SmallArray));
2902cab237bSDimitry Andric           ORE.emit([&]() {
2912cab237bSDimitry Andric             return OptimizationRemark(DEBUG_TYPE, "StackProtectorBuffer", &I)
2927a7e6055SDimitry Andric                    << "Stack protection applied to function "
2937a7e6055SDimitry Andric                    << ore::NV("Function", F)
2947a7e6055SDimitry Andric                    << " due to a stack allocated buffer or struct containing a "
2952cab237bSDimitry Andric                       "buffer";
2962cab237bSDimitry Andric           });
297f785676fSDimitry Andric           NeedsProtector = true;
298f785676fSDimitry Andric           continue;
299f785676fSDimitry Andric         }
300139f7f9bSDimitry Andric 
301139f7f9bSDimitry Andric         if (Strong && HasAddressTaken(AI)) {
302139f7f9bSDimitry Andric           ++NumAddrTaken;
3034ba319b5SDimitry Andric           Layout.insert(std::make_pair(AI, MachineFrameInfo::SSPLK_AddrOf));
3042cab237bSDimitry Andric           ORE.emit([&]() {
3052cab237bSDimitry Andric             return OptimizationRemark(DEBUG_TYPE, "StackProtectorAddressTaken",
3062cab237bSDimitry Andric                                       &I)
3077a7e6055SDimitry Andric                    << "Stack protection applied to function "
3087a7e6055SDimitry Andric                    << ore::NV("Function", F)
3092cab237bSDimitry Andric                    << " due to the address of a local variable being taken";
3102cab237bSDimitry Andric           });
311f785676fSDimitry Andric           NeedsProtector = true;
312139f7f9bSDimitry Andric         }
313139f7f9bSDimitry Andric       }
314f22ef01cSRoman Divacky     }
315f22ef01cSRoman Divacky   }
316f22ef01cSRoman Divacky 
317f785676fSDimitry Andric   return NeedsProtector;
318f785676fSDimitry Andric }
319f785676fSDimitry Andric 
3203ca95b02SDimitry Andric /// Create a stack guard loading and populate whether SelectionDAG SSP is
3213ca95b02SDimitry Andric /// supported.
getStackGuard(const TargetLoweringBase * TLI,Module * M,IRBuilder<> & B,bool * SupportsSelectionDAGSP=nullptr)3223ca95b02SDimitry Andric static Value *getStackGuard(const TargetLoweringBase *TLI, Module *M,
3233ca95b02SDimitry Andric                             IRBuilder<> &B,
3243ca95b02SDimitry Andric                             bool *SupportsSelectionDAGSP = nullptr) {
3253ca95b02SDimitry Andric   if (Value *Guard = TLI->getIRStackGuard(B))
3263ca95b02SDimitry Andric     return B.CreateLoad(Guard, true, "StackGuard");
327f785676fSDimitry Andric 
3283ca95b02SDimitry Andric   // Use SelectionDAG SSP handling, since there isn't an IR guard.
329f785676fSDimitry Andric   //
3303ca95b02SDimitry Andric   // This is more or less weird, since we optionally output whether we
3313ca95b02SDimitry Andric   // should perform a SelectionDAG SP here. The reason is that it's strictly
3323ca95b02SDimitry Andric   // defined as !TLI->getIRStackGuard(B), where getIRStackGuard is also
3333ca95b02SDimitry Andric   // mutating. There is no way to get this bit without mutating the IR, so
3343ca95b02SDimitry Andric   // getting this bit has to happen in this right time.
335f785676fSDimitry Andric   //
3363ca95b02SDimitry Andric   // We could have define a new function TLI::supportsSelectionDAGSP(), but that
3373ca95b02SDimitry Andric   // will put more burden on the backends' overriding work, especially when it
3383ca95b02SDimitry Andric   // actually conveys the same information getIRStackGuard() already gives.
3393ca95b02SDimitry Andric   if (SupportsSelectionDAGSP)
3403ca95b02SDimitry Andric     *SupportsSelectionDAGSP = true;
3413ca95b02SDimitry Andric   TLI->insertSSPDeclarations(*M);
3423ca95b02SDimitry Andric   return B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackguard));
343f785676fSDimitry Andric }
344f785676fSDimitry Andric 
3453ca95b02SDimitry Andric /// Insert code into the entry block that stores the stack guard
346f785676fSDimitry Andric /// variable onto the stack:
347f785676fSDimitry Andric ///
348f785676fSDimitry Andric ///   entry:
349f785676fSDimitry Andric ///     StackGuardSlot = alloca i8*
3503ca95b02SDimitry Andric ///     StackGuard = <stack guard>
3513ca95b02SDimitry Andric ///     call void @llvm.stackprotector(StackGuard, StackGuardSlot)
352f785676fSDimitry Andric ///
353f785676fSDimitry Andric /// Returns true if the platform/triple supports the stackprotectorcreate pseudo
354f785676fSDimitry Andric /// node.
CreatePrologue(Function * F,Module * M,ReturnInst * RI,const TargetLoweringBase * TLI,AllocaInst * & AI)355f785676fSDimitry Andric static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI,
3563ca95b02SDimitry Andric                            const TargetLoweringBase *TLI, AllocaInst *&AI) {
357f785676fSDimitry Andric   bool SupportsSelectionDAGSP = false;
358f785676fSDimitry Andric   IRBuilder<> B(&F->getEntryBlock().front());
3593ca95b02SDimitry Andric   PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext());
36091bc56edSDimitry Andric   AI = B.CreateAlloca(PtrTy, nullptr, "StackGuardSlot");
361f785676fSDimitry Andric 
3623ca95b02SDimitry Andric   Value *GuardSlot = getStackGuard(TLI, M, B, &SupportsSelectionDAGSP);
3633ca95b02SDimitry Andric   B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackprotector),
3643ca95b02SDimitry Andric                {GuardSlot, AI});
365f785676fSDimitry Andric   return SupportsSelectionDAGSP;
366f22ef01cSRoman Divacky }
367f22ef01cSRoman Divacky 
368f22ef01cSRoman Divacky /// InsertStackProtectors - Insert code into the prologue and epilogue of the
369f22ef01cSRoman Divacky /// function.
370f22ef01cSRoman Divacky ///
371f22ef01cSRoman Divacky ///  - The prologue code loads and stores the stack guard onto the stack.
372f22ef01cSRoman Divacky ///  - The epilogue checks the value stored in the prologue against the original
373f22ef01cSRoman Divacky ///    value. It calls __stack_chk_fail if they differ.
InsertStackProtectors()374f22ef01cSRoman Divacky bool StackProtector::InsertStackProtectors() {
3752cab237bSDimitry Andric   // If the target wants to XOR the frame pointer into the guard value, it's
3762cab237bSDimitry Andric   // impossible to emit the check in IR, so the target *must* support stack
3772cab237bSDimitry Andric   // protection in SDAG.
378f785676fSDimitry Andric   bool SupportsSelectionDAGSP =
3792cab237bSDimitry Andric       TLI->useStackGuardXorFP() ||
380*b5893f02SDimitry Andric       (EnableSelectionDAGSP && !TM->Options.EnableFastISel &&
381*b5893f02SDimitry Andric        !TM->Options.EnableGlobalISel);
38291bc56edSDimitry Andric   AllocaInst *AI = nullptr;       // Place on stack that stores the stack guard.
383f22ef01cSRoman Divacky 
384f22ef01cSRoman Divacky   for (Function::iterator I = F->begin(), E = F->end(); I != E;) {
3857d523365SDimitry Andric     BasicBlock *BB = &*I++;
386f22ef01cSRoman Divacky     ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
387f785676fSDimitry Andric     if (!RI)
388f785676fSDimitry Andric       continue;
389f22ef01cSRoman Divacky 
3903ca95b02SDimitry Andric     // Generate prologue instrumentation if not already generated.
391f785676fSDimitry Andric     if (!HasPrologue) {
392f785676fSDimitry Andric       HasPrologue = true;
3933ca95b02SDimitry Andric       SupportsSelectionDAGSP &= CreatePrologue(F, M, RI, TLI, AI);
394f785676fSDimitry Andric     }
395ffd1746dSEd Schouten 
3963ca95b02SDimitry Andric     // SelectionDAG based code generation. Nothing else needs to be done here.
3973ca95b02SDimitry Andric     // The epilogue instrumentation is postponed to SelectionDAG.
3983ca95b02SDimitry Andric     if (SupportsSelectionDAGSP)
3993ca95b02SDimitry Andric       break;
400f22ef01cSRoman Divacky 
401*b5893f02SDimitry Andric     // Find the stack guard slot if the prologue was not created by this pass
402*b5893f02SDimitry Andric     // itself via a previous call to CreatePrologue().
403*b5893f02SDimitry Andric     if (!AI) {
404*b5893f02SDimitry Andric       const CallInst *SPCall = findStackProtectorIntrinsic(*F);
405*b5893f02SDimitry Andric       assert(SPCall && "Call to llvm.stackprotector is missing");
406*b5893f02SDimitry Andric       AI = cast<AllocaInst>(SPCall->getArgOperand(1));
407*b5893f02SDimitry Andric     }
408*b5893f02SDimitry Andric 
4093ca95b02SDimitry Andric     // Set HasIRCheck to true, so that SelectionDAG will not generate its own
4103ca95b02SDimitry Andric     // version. SelectionDAG called 'shouldEmitSDCheck' to check whether
4113ca95b02SDimitry Andric     // instrumentation has already been generated.
4123ca95b02SDimitry Andric     HasIRCheck = true;
4133ca95b02SDimitry Andric 
4143ca95b02SDimitry Andric     // Generate epilogue instrumentation. The epilogue intrumentation can be
4153ca95b02SDimitry Andric     // function-based or inlined depending on which mechanism the target is
4163ca95b02SDimitry Andric     // providing.
4173ca95b02SDimitry Andric     if (Value* GuardCheck = TLI->getSSPStackGuardCheck(*M)) {
4183ca95b02SDimitry Andric       // Generate the function-based epilogue instrumentation.
4193ca95b02SDimitry Andric       // The target provides a guard check function, generate a call to it.
4203ca95b02SDimitry Andric       IRBuilder<> B(RI);
4213ca95b02SDimitry Andric       LoadInst *Guard = B.CreateLoad(AI, true, "Guard");
4223ca95b02SDimitry Andric       CallInst *Call = B.CreateCall(GuardCheck, {Guard});
4233ca95b02SDimitry Andric       llvm::Function *Function = cast<llvm::Function>(GuardCheck);
4243ca95b02SDimitry Andric       Call->setAttributes(Function->getAttributes());
4253ca95b02SDimitry Andric       Call->setCallingConv(Function->getCallingConv());
426f785676fSDimitry Andric     } else {
4273ca95b02SDimitry Andric       // Generate the epilogue with inline instrumentation.
428f785676fSDimitry Andric       // If we do not support SelectionDAG based tail calls, generate IR level
429f785676fSDimitry Andric       // tail calls.
430f785676fSDimitry Andric       //
431f22ef01cSRoman Divacky       // For each block with a return instruction, convert this:
432f22ef01cSRoman Divacky       //
433f22ef01cSRoman Divacky       //   return:
434f22ef01cSRoman Divacky       //     ...
435f22ef01cSRoman Divacky       //     ret ...
436f22ef01cSRoman Divacky       //
437f22ef01cSRoman Divacky       // into this:
438f22ef01cSRoman Divacky       //
439f22ef01cSRoman Divacky       //   return:
440f22ef01cSRoman Divacky       //     ...
4413ca95b02SDimitry Andric       //     %1 = <stack guard>
442f22ef01cSRoman Divacky       //     %2 = load StackGuardSlot
443f22ef01cSRoman Divacky       //     %3 = cmp i1 %1, %2
444f22ef01cSRoman Divacky       //     br i1 %3, label %SP_return, label %CallStackCheckFailBlk
445f22ef01cSRoman Divacky       //
446f22ef01cSRoman Divacky       //   SP_return:
447f22ef01cSRoman Divacky       //     ret ...
448f22ef01cSRoman Divacky       //
449f22ef01cSRoman Divacky       //   CallStackCheckFailBlk:
450f22ef01cSRoman Divacky       //     call void @__stack_chk_fail()
451f22ef01cSRoman Divacky       //     unreachable
452f22ef01cSRoman Divacky 
453f785676fSDimitry Andric       // Create the FailBB. We duplicate the BB every time since the MI tail
454f785676fSDimitry Andric       // merge pass will merge together all of the various BB into one including
455f785676fSDimitry Andric       // fail BB generated by the stack protector pseudo instruction.
456f785676fSDimitry Andric       BasicBlock *FailBB = CreateFailBB();
457f785676fSDimitry Andric 
458f22ef01cSRoman Divacky       // Split the basic block before the return instruction.
4597d523365SDimitry Andric       BasicBlock *NewBB = BB->splitBasicBlock(RI->getIterator(), "SP_return");
4603b0f4066SDimitry Andric 
461f785676fSDimitry Andric       // Update the dominator tree if we need to.
4623b0f4066SDimitry Andric       if (DT && DT->isReachableFromEntry(BB)) {
4633b0f4066SDimitry Andric         DT->addNewBlock(NewBB, BB);
464f785676fSDimitry Andric         DT->addNewBlock(FailBB, BB);
4652754fe60SDimitry Andric       }
466f22ef01cSRoman Divacky 
467f22ef01cSRoman Divacky       // Remove default branch instruction to the new BB.
468f22ef01cSRoman Divacky       BB->getTerminator()->eraseFromParent();
469f22ef01cSRoman Divacky 
470f785676fSDimitry Andric       // Move the newly created basic block to the point right after the old
471f785676fSDimitry Andric       // basic block so that it's in the "fall through" position.
472f22ef01cSRoman Divacky       NewBB->moveAfter(BB);
473f22ef01cSRoman Divacky 
474f22ef01cSRoman Divacky       // Generate the stack protector instructions in the old basic block.
475f785676fSDimitry Andric       IRBuilder<> B(BB);
4763ca95b02SDimitry Andric       Value *Guard = getStackGuard(TLI, M, B);
4773ca95b02SDimitry Andric       LoadInst *LI2 = B.CreateLoad(AI, true);
4783ca95b02SDimitry Andric       Value *Cmp = B.CreateICmpEQ(Guard, LI2);
4797d523365SDimitry Andric       auto SuccessProb =
4807d523365SDimitry Andric           BranchProbabilityInfo::getBranchProbStackProtector(true);
4817d523365SDimitry Andric       auto FailureProb =
4827d523365SDimitry Andric           BranchProbabilityInfo::getBranchProbStackProtector(false);
48339d628a0SDimitry Andric       MDNode *Weights = MDBuilder(F->getContext())
4847d523365SDimitry Andric                             .createBranchWeights(SuccessProb.getNumerator(),
4857d523365SDimitry Andric                                                  FailureProb.getNumerator());
48639d628a0SDimitry Andric       B.CreateCondBr(Cmp, NewBB, FailBB, Weights);
487f785676fSDimitry Andric     }
488f22ef01cSRoman Divacky   }
489f22ef01cSRoman Divacky 
49039d628a0SDimitry Andric   // Return if we didn't modify any basic blocks. i.e., there are no return
491f22ef01cSRoman Divacky   // statements in the function.
4927d523365SDimitry Andric   return HasPrologue;
493f22ef01cSRoman Divacky }
494f22ef01cSRoman Divacky 
495f22ef01cSRoman Divacky /// CreateFailBB - Create a basic block to jump to when the stack protector
496f22ef01cSRoman Divacky /// check fails.
CreateFailBB()497f22ef01cSRoman Divacky BasicBlock *StackProtector::CreateFailBB() {
498f785676fSDimitry Andric   LLVMContext &Context = F->getContext();
499f785676fSDimitry Andric   BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F);
500f785676fSDimitry Andric   IRBuilder<> B(FailBB);
5013ca95b02SDimitry Andric   B.SetCurrentDebugLocation(DebugLoc::get(0, 0, F->getSubprogram()));
50239d628a0SDimitry Andric   if (Trip.isOSOpenBSD()) {
50339d628a0SDimitry Andric     Constant *StackChkFail =
50439d628a0SDimitry Andric         M->getOrInsertFunction("__stack_smash_handler",
50539d628a0SDimitry Andric                                Type::getVoidTy(Context),
5067a7e6055SDimitry Andric                                Type::getInt8PtrTy(Context));
507f785676fSDimitry Andric 
508f785676fSDimitry Andric     B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
509f785676fSDimitry Andric   } else {
51039d628a0SDimitry Andric     Constant *StackChkFail =
5117a7e6055SDimitry Andric         M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
5127a7e6055SDimitry Andric 
513ff0cc061SDimitry Andric     B.CreateCall(StackChkFail, {});
514f785676fSDimitry Andric   }
515f785676fSDimitry Andric   B.CreateUnreachable();
516f22ef01cSRoman Divacky   return FailBB;
517f22ef01cSRoman Divacky }
5183ca95b02SDimitry Andric 
shouldEmitSDCheck(const BasicBlock & BB) const5193ca95b02SDimitry Andric bool StackProtector::shouldEmitSDCheck(const BasicBlock &BB) const {
5203ca95b02SDimitry Andric   return HasPrologue && !HasIRCheck && dyn_cast<ReturnInst>(BB.getTerminator());
5213ca95b02SDimitry Andric }
5224ba319b5SDimitry Andric 
copyToMachineFrameInfo(MachineFrameInfo & MFI) const5234ba319b5SDimitry Andric void StackProtector::copyToMachineFrameInfo(MachineFrameInfo &MFI) const {
5244ba319b5SDimitry Andric   if (Layout.empty())
5254ba319b5SDimitry Andric     return;
5264ba319b5SDimitry Andric 
5274ba319b5SDimitry Andric   for (int I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I) {
5284ba319b5SDimitry Andric     if (MFI.isDeadObjectIndex(I))
5294ba319b5SDimitry Andric       continue;
5304ba319b5SDimitry Andric 
5314ba319b5SDimitry Andric     const AllocaInst *AI = MFI.getObjectAllocation(I);
5324ba319b5SDimitry Andric     if (!AI)
5334ba319b5SDimitry Andric       continue;
5344ba319b5SDimitry Andric 
5354ba319b5SDimitry Andric     SSPLayoutMap::const_iterator LI = Layout.find(AI);
5364ba319b5SDimitry Andric     if (LI == Layout.end())
5374ba319b5SDimitry Andric       continue;
5384ba319b5SDimitry Andric 
5394ba319b5SDimitry Andric     MFI.setObjectSSPLayout(I, LI->second);
5404ba319b5SDimitry Andric   }
5414ba319b5SDimitry Andric }
542