1*0b57cec5SDimitry Andric //===- StackProtector.cpp - Stack Protector Insertion ---------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This pass inserts stack protectors into functions which need them. A variable 10*0b57cec5SDimitry Andric // with a random value in it is stored onto the stack before the local variables 11*0b57cec5SDimitry Andric // are allocated. Upon exiting the block, the stored value is checked. If it's 12*0b57cec5SDimitry Andric // changed, then there was some sort of violation and the program aborts. 13*0b57cec5SDimitry Andric // 14*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric #include "llvm/CodeGen/StackProtector.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 19*0b57cec5SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h" 20*0b57cec5SDimitry Andric #include "llvm/Analysis/EHPersonalities.h" 215ffd83dbSDimitry Andric #include "llvm/Analysis/MemoryLocation.h" 22*0b57cec5SDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 24*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 25*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 26*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 27*0b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 28*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 29*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 30*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 31*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 32*0b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 33*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 34*0b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h" 35*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 36*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 37*0b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 38*0b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 39*0b57cec5SDimitry Andric #include "llvm/IR/MDBuilder.h" 40*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 41*0b57cec5SDimitry Andric #include "llvm/IR/Type.h" 42*0b57cec5SDimitry Andric #include "llvm/IR/User.h" 43480093f4SDimitry Andric #include "llvm/InitializePasses.h" 44*0b57cec5SDimitry Andric #include "llvm/Pass.h" 45*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 46*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 47*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 48*0b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 49bdd1243dSDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h" 50bdd1243dSDimitry Andric #include <optional> 51*0b57cec5SDimitry Andric #include <utility> 52*0b57cec5SDimitry Andric 53*0b57cec5SDimitry Andric using namespace llvm; 54*0b57cec5SDimitry Andric 55*0b57cec5SDimitry Andric #define DEBUG_TYPE "stack-protector" 56*0b57cec5SDimitry Andric 57*0b57cec5SDimitry Andric STATISTIC(NumFunProtected, "Number of functions protected"); 58*0b57cec5SDimitry Andric STATISTIC(NumAddrTaken, "Number of local variables that have their address" 59*0b57cec5SDimitry Andric " taken."); 60*0b57cec5SDimitry Andric 61*0b57cec5SDimitry Andric static cl::opt<bool> EnableSelectionDAGSP("enable-selectiondag-sp", 62*0b57cec5SDimitry Andric cl::init(true), cl::Hidden); 63bdd1243dSDimitry Andric static cl::opt<bool> DisableCheckNoReturn("disable-check-noreturn-call", 64bdd1243dSDimitry Andric cl::init(false), cl::Hidden); 65*0b57cec5SDimitry Andric 66*0b57cec5SDimitry Andric char StackProtector::ID = 0; 67*0b57cec5SDimitry Andric 68bdd1243dSDimitry Andric StackProtector::StackProtector() : FunctionPass(ID) { 69480093f4SDimitry Andric initializeStackProtectorPass(*PassRegistry::getPassRegistry()); 70480093f4SDimitry Andric } 71480093f4SDimitry Andric 72*0b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(StackProtector, DEBUG_TYPE, 73*0b57cec5SDimitry Andric "Insert stack protectors", false, true) 74*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) 75fe6060f1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 76*0b57cec5SDimitry Andric INITIALIZE_PASS_END(StackProtector, DEBUG_TYPE, 77*0b57cec5SDimitry Andric "Insert stack protectors", false, true) 78*0b57cec5SDimitry Andric 79*0b57cec5SDimitry Andric FunctionPass *llvm::createStackProtectorPass() { return new StackProtector(); } 80*0b57cec5SDimitry Andric 81*0b57cec5SDimitry Andric void StackProtector::getAnalysisUsage(AnalysisUsage &AU) const { 82*0b57cec5SDimitry Andric AU.addRequired<TargetPassConfig>(); 83*0b57cec5SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>(); 84*0b57cec5SDimitry Andric } 85*0b57cec5SDimitry Andric 86*0b57cec5SDimitry Andric bool StackProtector::runOnFunction(Function &Fn) { 87*0b57cec5SDimitry Andric F = &Fn; 88*0b57cec5SDimitry Andric M = F->getParent(); 89bdd1243dSDimitry Andric if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>()) 90bdd1243dSDimitry Andric DTU.emplace(DTWP->getDomTree(), DomTreeUpdater::UpdateStrategy::Lazy); 91*0b57cec5SDimitry Andric TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>(); 92*0b57cec5SDimitry Andric Trip = TM->getTargetTriple(); 93*0b57cec5SDimitry Andric TLI = TM->getSubtargetImpl(Fn)->getTargetLowering(); 94*0b57cec5SDimitry Andric HasPrologue = false; 95*0b57cec5SDimitry Andric HasIRCheck = false; 96*0b57cec5SDimitry Andric 97bdd1243dSDimitry Andric SSPBufferSize = Fn.getFnAttributeAsParsedInteger( 98bdd1243dSDimitry Andric "stack-protector-buffer-size", DefaultSSPBufferSize); 99*0b57cec5SDimitry Andric if (!RequiresStackProtector()) 100*0b57cec5SDimitry Andric return false; 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric // TODO(etienneb): Functions with funclets are not correctly supported now. 103*0b57cec5SDimitry Andric // Do nothing if this is funclet-based personality. 104*0b57cec5SDimitry Andric if (Fn.hasPersonalityFn()) { 105*0b57cec5SDimitry Andric EHPersonality Personality = classifyEHPersonality(Fn.getPersonalityFn()); 106*0b57cec5SDimitry Andric if (isFuncletEHPersonality(Personality)) 107*0b57cec5SDimitry Andric return false; 108*0b57cec5SDimitry Andric } 109*0b57cec5SDimitry Andric 110*0b57cec5SDimitry Andric ++NumFunProtected; 111bdd1243dSDimitry Andric bool Changed = InsertStackProtectors(); 112bdd1243dSDimitry Andric #ifdef EXPENSIVE_CHECKS 113bdd1243dSDimitry Andric assert((!DTU || 114bdd1243dSDimitry Andric DTU->getDomTree().verify(DominatorTree::VerificationLevel::Full)) && 115bdd1243dSDimitry Andric "Failed to maintain validity of domtree!"); 116bdd1243dSDimitry Andric #endif 117bdd1243dSDimitry Andric DTU.reset(); 118bdd1243dSDimitry Andric return Changed; 119*0b57cec5SDimitry Andric } 120*0b57cec5SDimitry Andric 121*0b57cec5SDimitry Andric /// \param [out] IsLarge is set to true if a protectable array is found and 122*0b57cec5SDimitry Andric /// it is "large" ( >= ssp-buffer-size). In the case of a structure with 123*0b57cec5SDimitry Andric /// multiple arrays, this gets set if any of them is large. 124*0b57cec5SDimitry Andric bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge, 125*0b57cec5SDimitry Andric bool Strong, 126*0b57cec5SDimitry Andric bool InStruct) const { 127*0b57cec5SDimitry Andric if (!Ty) 128*0b57cec5SDimitry Andric return false; 129*0b57cec5SDimitry Andric if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) { 130*0b57cec5SDimitry Andric if (!AT->getElementType()->isIntegerTy(8)) { 131*0b57cec5SDimitry Andric // If we're on a non-Darwin platform or we're inside of a structure, don't 132*0b57cec5SDimitry Andric // add stack protectors unless the array is a character array. 133*0b57cec5SDimitry Andric // However, in strong mode any array, regardless of type and size, 134*0b57cec5SDimitry Andric // triggers a protector. 135*0b57cec5SDimitry Andric if (!Strong && (InStruct || !Trip.isOSDarwin())) 136*0b57cec5SDimitry Andric return false; 137*0b57cec5SDimitry Andric } 138*0b57cec5SDimitry Andric 139*0b57cec5SDimitry Andric // If an array has more than SSPBufferSize bytes of allocated space, then we 140*0b57cec5SDimitry Andric // emit stack protectors. 141*0b57cec5SDimitry Andric if (SSPBufferSize <= M->getDataLayout().getTypeAllocSize(AT)) { 142*0b57cec5SDimitry Andric IsLarge = true; 143*0b57cec5SDimitry Andric return true; 144*0b57cec5SDimitry Andric } 145*0b57cec5SDimitry Andric 146*0b57cec5SDimitry Andric if (Strong) 147*0b57cec5SDimitry Andric // Require a protector for all arrays in strong mode 148*0b57cec5SDimitry Andric return true; 149*0b57cec5SDimitry Andric } 150*0b57cec5SDimitry Andric 151*0b57cec5SDimitry Andric const StructType *ST = dyn_cast<StructType>(Ty); 152*0b57cec5SDimitry Andric if (!ST) 153*0b57cec5SDimitry Andric return false; 154*0b57cec5SDimitry Andric 155*0b57cec5SDimitry Andric bool NeedsProtector = false; 156349cc55cSDimitry Andric for (Type *ET : ST->elements()) 157349cc55cSDimitry Andric if (ContainsProtectableArray(ET, IsLarge, Strong, true)) { 158*0b57cec5SDimitry Andric // If the element is a protectable array and is large (>= SSPBufferSize) 159*0b57cec5SDimitry Andric // then we are done. If the protectable array is not large, then 160*0b57cec5SDimitry Andric // keep looking in case a subsequent element is a large array. 161*0b57cec5SDimitry Andric if (IsLarge) 162*0b57cec5SDimitry Andric return true; 163*0b57cec5SDimitry Andric NeedsProtector = true; 164*0b57cec5SDimitry Andric } 165*0b57cec5SDimitry Andric 166*0b57cec5SDimitry Andric return NeedsProtector; 167*0b57cec5SDimitry Andric } 168*0b57cec5SDimitry Andric 1695ffd83dbSDimitry Andric bool StackProtector::HasAddressTaken(const Instruction *AI, 1700eae32dcSDimitry Andric TypeSize AllocSize) { 1715ffd83dbSDimitry Andric const DataLayout &DL = M->getDataLayout(); 172c14a5a88SDimitry Andric for (const User *U : AI->users()) { 173c14a5a88SDimitry Andric const auto *I = cast<Instruction>(U); 1745ffd83dbSDimitry Andric // If this instruction accesses memory make sure it doesn't access beyond 1755ffd83dbSDimitry Andric // the bounds of the allocated object. 176bdd1243dSDimitry Andric std::optional<MemoryLocation> MemLoc = MemoryLocation::getOrNone(I); 17781ad6265SDimitry Andric if (MemLoc && MemLoc->Size.hasValue() && 1780eae32dcSDimitry Andric !TypeSize::isKnownGE(AllocSize, 1790eae32dcSDimitry Andric TypeSize::getFixed(MemLoc->Size.getValue()))) 1805ffd83dbSDimitry Andric return true; 181c14a5a88SDimitry Andric switch (I->getOpcode()) { 182c14a5a88SDimitry Andric case Instruction::Store: 183c14a5a88SDimitry Andric if (AI == cast<StoreInst>(I)->getValueOperand()) 184c14a5a88SDimitry Andric return true; 185c14a5a88SDimitry Andric break; 186c14a5a88SDimitry Andric case Instruction::AtomicCmpXchg: 187c14a5a88SDimitry Andric // cmpxchg conceptually includes both a load and store from the same 188c14a5a88SDimitry Andric // location. So, like store, the value being stored is what matters. 189c14a5a88SDimitry Andric if (AI == cast<AtomicCmpXchgInst>(I)->getNewValOperand()) 190c14a5a88SDimitry Andric return true; 191c14a5a88SDimitry Andric break; 192c14a5a88SDimitry Andric case Instruction::PtrToInt: 193c14a5a88SDimitry Andric if (AI == cast<PtrToIntInst>(I)->getOperand(0)) 194c14a5a88SDimitry Andric return true; 195c14a5a88SDimitry Andric break; 196c14a5a88SDimitry Andric case Instruction::Call: { 197c14a5a88SDimitry Andric // Ignore intrinsics that do not become real instructions. 198c14a5a88SDimitry Andric // TODO: Narrow this to intrinsics that have store-like effects. 199c14a5a88SDimitry Andric const auto *CI = cast<CallInst>(I); 200d409305fSDimitry Andric if (!CI->isDebugOrPseudoInst() && !CI->isLifetimeStartOrEnd()) 201c14a5a88SDimitry Andric return true; 202c14a5a88SDimitry Andric break; 203c14a5a88SDimitry Andric } 204c14a5a88SDimitry Andric case Instruction::Invoke: 205c14a5a88SDimitry Andric return true; 2065ffd83dbSDimitry Andric case Instruction::GetElementPtr: { 2075ffd83dbSDimitry Andric // If the GEP offset is out-of-bounds, or is non-constant and so has to be 2085ffd83dbSDimitry Andric // assumed to be potentially out-of-bounds, then any memory access that 2095ffd83dbSDimitry Andric // would use it could also be out-of-bounds meaning stack protection is 2105ffd83dbSDimitry Andric // required. 2115ffd83dbSDimitry Andric const GetElementPtrInst *GEP = cast<GetElementPtrInst>(I); 2120eae32dcSDimitry Andric unsigned IndexSize = DL.getIndexTypeSizeInBits(I->getType()); 2130eae32dcSDimitry Andric APInt Offset(IndexSize, 0); 2140eae32dcSDimitry Andric if (!GEP->accumulateConstantOffset(DL, Offset)) 2150eae32dcSDimitry Andric return true; 2160eae32dcSDimitry Andric TypeSize OffsetSize = TypeSize::Fixed(Offset.getLimitedValue()); 2170eae32dcSDimitry Andric if (!TypeSize::isKnownGT(AllocSize, OffsetSize)) 2185ffd83dbSDimitry Andric return true; 2195ffd83dbSDimitry Andric // Adjust AllocSize to be the space remaining after this offset. 2200eae32dcSDimitry Andric // We can't subtract a fixed size from a scalable one, so in that case 2210eae32dcSDimitry Andric // assume the scalable value is of minimum size. 2220eae32dcSDimitry Andric TypeSize NewAllocSize = 2230eae32dcSDimitry Andric TypeSize::Fixed(AllocSize.getKnownMinValue()) - OffsetSize; 2240eae32dcSDimitry Andric if (HasAddressTaken(I, NewAllocSize)) 2255ffd83dbSDimitry Andric return true; 2265ffd83dbSDimitry Andric break; 2275ffd83dbSDimitry Andric } 228c14a5a88SDimitry Andric case Instruction::BitCast: 229c14a5a88SDimitry Andric case Instruction::Select: 230c14a5a88SDimitry Andric case Instruction::AddrSpaceCast: 2315ffd83dbSDimitry Andric if (HasAddressTaken(I, AllocSize)) 232c14a5a88SDimitry Andric return true; 233c14a5a88SDimitry Andric break; 234c14a5a88SDimitry Andric case Instruction::PHI: { 235c14a5a88SDimitry Andric // Keep track of what PHI nodes we have already visited to ensure 236c14a5a88SDimitry Andric // they are only visited once. 237c14a5a88SDimitry Andric const auto *PN = cast<PHINode>(I); 238c14a5a88SDimitry Andric if (VisitedPHIs.insert(PN).second) 2395ffd83dbSDimitry Andric if (HasAddressTaken(PN, AllocSize)) 240c14a5a88SDimitry Andric return true; 241c14a5a88SDimitry Andric break; 242c14a5a88SDimitry Andric } 243c14a5a88SDimitry Andric case Instruction::Load: 244c14a5a88SDimitry Andric case Instruction::AtomicRMW: 245c14a5a88SDimitry Andric case Instruction::Ret: 246c14a5a88SDimitry Andric // These instructions take an address operand, but have load-like or 247c14a5a88SDimitry Andric // other innocuous behavior that should not trigger a stack protector. 248c14a5a88SDimitry Andric // atomicrmw conceptually has both load and store semantics, but the 249c14a5a88SDimitry Andric // value being stored must be integer; so if a pointer is being stored, 250c14a5a88SDimitry Andric // we'll catch it in the PtrToInt case above. 251c14a5a88SDimitry Andric break; 252c14a5a88SDimitry Andric default: 253c14a5a88SDimitry Andric // Conservatively return true for any instruction that takes an address 254c14a5a88SDimitry Andric // operand, but is not handled above. 255c14a5a88SDimitry Andric return true; 256c14a5a88SDimitry Andric } 257c14a5a88SDimitry Andric } 258c14a5a88SDimitry Andric return false; 259c14a5a88SDimitry Andric } 260c14a5a88SDimitry Andric 261*0b57cec5SDimitry Andric /// Search for the first call to the llvm.stackprotector intrinsic and return it 262*0b57cec5SDimitry Andric /// if present. 263*0b57cec5SDimitry Andric static const CallInst *findStackProtectorIntrinsic(Function &F) { 264*0b57cec5SDimitry Andric for (const BasicBlock &BB : F) 265*0b57cec5SDimitry Andric for (const Instruction &I : BB) 266e8d8bef9SDimitry Andric if (const auto *II = dyn_cast<IntrinsicInst>(&I)) 267e8d8bef9SDimitry Andric if (II->getIntrinsicID() == Intrinsic::stackprotector) 268e8d8bef9SDimitry Andric return II; 269*0b57cec5SDimitry Andric return nullptr; 270*0b57cec5SDimitry Andric } 271*0b57cec5SDimitry Andric 272*0b57cec5SDimitry Andric /// Check whether or not this function needs a stack protector based 273*0b57cec5SDimitry Andric /// upon the stack protector level. 274*0b57cec5SDimitry Andric /// 275*0b57cec5SDimitry Andric /// We use two heuristics: a standard (ssp) and strong (sspstrong). 276*0b57cec5SDimitry Andric /// The standard heuristic which will add a guard variable to functions that 277*0b57cec5SDimitry Andric /// call alloca with a either a variable size or a size >= SSPBufferSize, 278*0b57cec5SDimitry Andric /// functions with character buffers larger than SSPBufferSize, and functions 279*0b57cec5SDimitry Andric /// with aggregates containing character buffers larger than SSPBufferSize. The 280*0b57cec5SDimitry Andric /// strong heuristic will add a guard variables to functions that call alloca 281*0b57cec5SDimitry Andric /// regardless of size, functions with any buffer regardless of type and size, 282*0b57cec5SDimitry Andric /// functions with aggregates that contain any buffer regardless of type and 283*0b57cec5SDimitry Andric /// size, and functions that contain stack-based variables that have had their 284*0b57cec5SDimitry Andric /// address taken. 285*0b57cec5SDimitry Andric bool StackProtector::RequiresStackProtector() { 286*0b57cec5SDimitry Andric bool Strong = false; 287*0b57cec5SDimitry Andric bool NeedsProtector = false; 288*0b57cec5SDimitry Andric 289*0b57cec5SDimitry Andric if (F->hasFnAttribute(Attribute::SafeStack)) 290*0b57cec5SDimitry Andric return false; 291*0b57cec5SDimitry Andric 292*0b57cec5SDimitry Andric // We are constructing the OptimizationRemarkEmitter on the fly rather than 293*0b57cec5SDimitry Andric // using the analysis pass to avoid building DominatorTree and LoopInfo which 294*0b57cec5SDimitry Andric // are not available this late in the IR pipeline. 295*0b57cec5SDimitry Andric OptimizationRemarkEmitter ORE(F); 296*0b57cec5SDimitry Andric 297*0b57cec5SDimitry Andric if (F->hasFnAttribute(Attribute::StackProtectReq)) { 298*0b57cec5SDimitry Andric ORE.emit([&]() { 299*0b57cec5SDimitry Andric return OptimizationRemark(DEBUG_TYPE, "StackProtectorRequested", F) 300*0b57cec5SDimitry Andric << "Stack protection applied to function " 301*0b57cec5SDimitry Andric << ore::NV("Function", F) 302*0b57cec5SDimitry Andric << " due to a function attribute or command-line switch"; 303*0b57cec5SDimitry Andric }); 304*0b57cec5SDimitry Andric NeedsProtector = true; 305*0b57cec5SDimitry Andric Strong = true; // Use the same heuristic as strong to determine SSPLayout 306*0b57cec5SDimitry Andric } else if (F->hasFnAttribute(Attribute::StackProtectStrong)) 307*0b57cec5SDimitry Andric Strong = true; 308*0b57cec5SDimitry Andric else if (!F->hasFnAttribute(Attribute::StackProtect)) 309*0b57cec5SDimitry Andric return false; 310*0b57cec5SDimitry Andric 311*0b57cec5SDimitry Andric for (const BasicBlock &BB : *F) { 312*0b57cec5SDimitry Andric for (const Instruction &I : BB) { 313*0b57cec5SDimitry Andric if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { 314*0b57cec5SDimitry Andric if (AI->isArrayAllocation()) { 315*0b57cec5SDimitry Andric auto RemarkBuilder = [&]() { 316*0b57cec5SDimitry Andric return OptimizationRemark(DEBUG_TYPE, "StackProtectorAllocaOrArray", 317*0b57cec5SDimitry Andric &I) 318*0b57cec5SDimitry Andric << "Stack protection applied to function " 319*0b57cec5SDimitry Andric << ore::NV("Function", F) 320*0b57cec5SDimitry Andric << " due to a call to alloca or use of a variable length " 321*0b57cec5SDimitry Andric "array"; 322*0b57cec5SDimitry Andric }; 323*0b57cec5SDimitry Andric if (const auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) { 324*0b57cec5SDimitry Andric if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) { 325*0b57cec5SDimitry Andric // A call to alloca with size >= SSPBufferSize requires 326*0b57cec5SDimitry Andric // stack protectors. 327*0b57cec5SDimitry Andric Layout.insert(std::make_pair(AI, 328*0b57cec5SDimitry Andric MachineFrameInfo::SSPLK_LargeArray)); 329*0b57cec5SDimitry Andric ORE.emit(RemarkBuilder); 330*0b57cec5SDimitry Andric NeedsProtector = true; 331*0b57cec5SDimitry Andric } else if (Strong) { 332*0b57cec5SDimitry Andric // Require protectors for all alloca calls in strong mode. 333*0b57cec5SDimitry Andric Layout.insert(std::make_pair(AI, 334*0b57cec5SDimitry Andric MachineFrameInfo::SSPLK_SmallArray)); 335*0b57cec5SDimitry Andric ORE.emit(RemarkBuilder); 336*0b57cec5SDimitry Andric NeedsProtector = true; 337*0b57cec5SDimitry Andric } 338*0b57cec5SDimitry Andric } else { 339*0b57cec5SDimitry Andric // A call to alloca with a variable size requires protectors. 340*0b57cec5SDimitry Andric Layout.insert(std::make_pair(AI, 341*0b57cec5SDimitry Andric MachineFrameInfo::SSPLK_LargeArray)); 342*0b57cec5SDimitry Andric ORE.emit(RemarkBuilder); 343*0b57cec5SDimitry Andric NeedsProtector = true; 344*0b57cec5SDimitry Andric } 345*0b57cec5SDimitry Andric continue; 346*0b57cec5SDimitry Andric } 347*0b57cec5SDimitry Andric 348*0b57cec5SDimitry Andric bool IsLarge = false; 349*0b57cec5SDimitry Andric if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) { 350*0b57cec5SDimitry Andric Layout.insert(std::make_pair(AI, IsLarge 351*0b57cec5SDimitry Andric ? MachineFrameInfo::SSPLK_LargeArray 352*0b57cec5SDimitry Andric : MachineFrameInfo::SSPLK_SmallArray)); 353*0b57cec5SDimitry Andric ORE.emit([&]() { 354*0b57cec5SDimitry Andric return OptimizationRemark(DEBUG_TYPE, "StackProtectorBuffer", &I) 355*0b57cec5SDimitry Andric << "Stack protection applied to function " 356*0b57cec5SDimitry Andric << ore::NV("Function", F) 357*0b57cec5SDimitry Andric << " due to a stack allocated buffer or struct containing a " 358*0b57cec5SDimitry Andric "buffer"; 359*0b57cec5SDimitry Andric }); 360*0b57cec5SDimitry Andric NeedsProtector = true; 361*0b57cec5SDimitry Andric continue; 362*0b57cec5SDimitry Andric } 363*0b57cec5SDimitry Andric 3645ffd83dbSDimitry Andric if (Strong && HasAddressTaken(AI, M->getDataLayout().getTypeAllocSize( 3655ffd83dbSDimitry Andric AI->getAllocatedType()))) { 366*0b57cec5SDimitry Andric ++NumAddrTaken; 367*0b57cec5SDimitry Andric Layout.insert(std::make_pair(AI, MachineFrameInfo::SSPLK_AddrOf)); 368*0b57cec5SDimitry Andric ORE.emit([&]() { 369*0b57cec5SDimitry Andric return OptimizationRemark(DEBUG_TYPE, "StackProtectorAddressTaken", 370*0b57cec5SDimitry Andric &I) 371*0b57cec5SDimitry Andric << "Stack protection applied to function " 372*0b57cec5SDimitry Andric << ore::NV("Function", F) 373*0b57cec5SDimitry Andric << " due to the address of a local variable being taken"; 374*0b57cec5SDimitry Andric }); 375*0b57cec5SDimitry Andric NeedsProtector = true; 376*0b57cec5SDimitry Andric } 3775ffd83dbSDimitry Andric // Clear any PHIs that we visited, to make sure we examine all uses of 3785ffd83dbSDimitry Andric // any subsequent allocas that we look at. 3795ffd83dbSDimitry Andric VisitedPHIs.clear(); 380*0b57cec5SDimitry Andric } 381*0b57cec5SDimitry Andric } 382*0b57cec5SDimitry Andric } 383*0b57cec5SDimitry Andric 384*0b57cec5SDimitry Andric return NeedsProtector; 385*0b57cec5SDimitry Andric } 386*0b57cec5SDimitry Andric 387*0b57cec5SDimitry Andric /// Create a stack guard loading and populate whether SelectionDAG SSP is 388*0b57cec5SDimitry Andric /// supported. 389*0b57cec5SDimitry Andric static Value *getStackGuard(const TargetLoweringBase *TLI, Module *M, 390*0b57cec5SDimitry Andric IRBuilder<> &B, 391*0b57cec5SDimitry Andric bool *SupportsSelectionDAGSP = nullptr) { 392e8d8bef9SDimitry Andric Value *Guard = TLI->getIRStackGuard(B); 393fe6060f1SDimitry Andric StringRef GuardMode = M->getStackProtectorGuard(); 394fe6060f1SDimitry Andric if ((GuardMode == "tls" || GuardMode.empty()) && Guard) 395*0b57cec5SDimitry Andric return B.CreateLoad(B.getInt8PtrTy(), Guard, true, "StackGuard"); 396*0b57cec5SDimitry Andric 397*0b57cec5SDimitry Andric // Use SelectionDAG SSP handling, since there isn't an IR guard. 398*0b57cec5SDimitry Andric // 399*0b57cec5SDimitry Andric // This is more or less weird, since we optionally output whether we 400*0b57cec5SDimitry Andric // should perform a SelectionDAG SP here. The reason is that it's strictly 401*0b57cec5SDimitry Andric // defined as !TLI->getIRStackGuard(B), where getIRStackGuard is also 402*0b57cec5SDimitry Andric // mutating. There is no way to get this bit without mutating the IR, so 403*0b57cec5SDimitry Andric // getting this bit has to happen in this right time. 404*0b57cec5SDimitry Andric // 405*0b57cec5SDimitry Andric // We could have define a new function TLI::supportsSelectionDAGSP(), but that 406*0b57cec5SDimitry Andric // will put more burden on the backends' overriding work, especially when it 407*0b57cec5SDimitry Andric // actually conveys the same information getIRStackGuard() already gives. 408*0b57cec5SDimitry Andric if (SupportsSelectionDAGSP) 409*0b57cec5SDimitry Andric *SupportsSelectionDAGSP = true; 410*0b57cec5SDimitry Andric TLI->insertSSPDeclarations(*M); 411*0b57cec5SDimitry Andric return B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackguard)); 412*0b57cec5SDimitry Andric } 413*0b57cec5SDimitry Andric 414*0b57cec5SDimitry Andric /// Insert code into the entry block that stores the stack guard 415*0b57cec5SDimitry Andric /// variable onto the stack: 416*0b57cec5SDimitry Andric /// 417*0b57cec5SDimitry Andric /// entry: 418*0b57cec5SDimitry Andric /// StackGuardSlot = alloca i8* 419*0b57cec5SDimitry Andric /// StackGuard = <stack guard> 420*0b57cec5SDimitry Andric /// call void @llvm.stackprotector(StackGuard, StackGuardSlot) 421*0b57cec5SDimitry Andric /// 422*0b57cec5SDimitry Andric /// Returns true if the platform/triple supports the stackprotectorcreate pseudo 423*0b57cec5SDimitry Andric /// node. 424bdd1243dSDimitry Andric static bool CreatePrologue(Function *F, Module *M, Instruction *CheckLoc, 425*0b57cec5SDimitry Andric const TargetLoweringBase *TLI, AllocaInst *&AI) { 426*0b57cec5SDimitry Andric bool SupportsSelectionDAGSP = false; 427*0b57cec5SDimitry Andric IRBuilder<> B(&F->getEntryBlock().front()); 428bdd1243dSDimitry Andric PointerType *PtrTy = Type::getInt8PtrTy(CheckLoc->getContext()); 429*0b57cec5SDimitry Andric AI = B.CreateAlloca(PtrTy, nullptr, "StackGuardSlot"); 430*0b57cec5SDimitry Andric 431*0b57cec5SDimitry Andric Value *GuardSlot = getStackGuard(TLI, M, B, &SupportsSelectionDAGSP); 432*0b57cec5SDimitry Andric B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), 433*0b57cec5SDimitry Andric {GuardSlot, AI}); 434*0b57cec5SDimitry Andric return SupportsSelectionDAGSP; 435*0b57cec5SDimitry Andric } 436*0b57cec5SDimitry Andric 437*0b57cec5SDimitry Andric /// InsertStackProtectors - Insert code into the prologue and epilogue of the 438*0b57cec5SDimitry Andric /// function. 439*0b57cec5SDimitry Andric /// 440*0b57cec5SDimitry Andric /// - The prologue code loads and stores the stack guard onto the stack. 441*0b57cec5SDimitry Andric /// - The epilogue checks the value stored in the prologue against the original 442*0b57cec5SDimitry Andric /// value. It calls __stack_chk_fail if they differ. 443*0b57cec5SDimitry Andric bool StackProtector::InsertStackProtectors() { 444*0b57cec5SDimitry Andric // If the target wants to XOR the frame pointer into the guard value, it's 445*0b57cec5SDimitry Andric // impossible to emit the check in IR, so the target *must* support stack 446*0b57cec5SDimitry Andric // protection in SDAG. 447*0b57cec5SDimitry Andric bool SupportsSelectionDAGSP = 448*0b57cec5SDimitry Andric TLI->useStackGuardXorFP() || 449349cc55cSDimitry Andric (EnableSelectionDAGSP && !TM->Options.EnableFastISel); 450*0b57cec5SDimitry Andric AllocaInst *AI = nullptr; // Place on stack that stores the stack guard. 451bdd1243dSDimitry Andric BasicBlock *FailBB = nullptr; 452*0b57cec5SDimitry Andric 453349cc55cSDimitry Andric for (BasicBlock &BB : llvm::make_early_inc_range(*F)) { 454bdd1243dSDimitry Andric // This is stack protector auto generated check BB, skip it. 455bdd1243dSDimitry Andric if (&BB == FailBB) 456bdd1243dSDimitry Andric continue; 457bdd1243dSDimitry Andric Instruction *CheckLoc = dyn_cast<ReturnInst>(BB.getTerminator()); 458bdd1243dSDimitry Andric if (!CheckLoc && !DisableCheckNoReturn) { 459bdd1243dSDimitry Andric for (auto &Inst : BB) { 460bdd1243dSDimitry Andric auto *CB = dyn_cast<CallBase>(&Inst); 461bdd1243dSDimitry Andric if (!CB) 462bdd1243dSDimitry Andric continue; 463bdd1243dSDimitry Andric if (!CB->doesNotReturn()) 464bdd1243dSDimitry Andric continue; 465bdd1243dSDimitry Andric // Do stack check before non-return calls (e.g: __cxa_throw) 466bdd1243dSDimitry Andric CheckLoc = CB; 467bdd1243dSDimitry Andric break; 468bdd1243dSDimitry Andric } 469bdd1243dSDimitry Andric } 470bdd1243dSDimitry Andric 471bdd1243dSDimitry Andric if (!CheckLoc) 472*0b57cec5SDimitry Andric continue; 473*0b57cec5SDimitry Andric 474*0b57cec5SDimitry Andric // Generate prologue instrumentation if not already generated. 475*0b57cec5SDimitry Andric if (!HasPrologue) { 476*0b57cec5SDimitry Andric HasPrologue = true; 477bdd1243dSDimitry Andric SupportsSelectionDAGSP &= CreatePrologue(F, M, CheckLoc, TLI, AI); 478*0b57cec5SDimitry Andric } 479*0b57cec5SDimitry Andric 480*0b57cec5SDimitry Andric // SelectionDAG based code generation. Nothing else needs to be done here. 481*0b57cec5SDimitry Andric // The epilogue instrumentation is postponed to SelectionDAG. 482*0b57cec5SDimitry Andric if (SupportsSelectionDAGSP) 483*0b57cec5SDimitry Andric break; 484*0b57cec5SDimitry Andric 485*0b57cec5SDimitry Andric // Find the stack guard slot if the prologue was not created by this pass 486*0b57cec5SDimitry Andric // itself via a previous call to CreatePrologue(). 487*0b57cec5SDimitry Andric if (!AI) { 488*0b57cec5SDimitry Andric const CallInst *SPCall = findStackProtectorIntrinsic(*F); 489*0b57cec5SDimitry Andric assert(SPCall && "Call to llvm.stackprotector is missing"); 490*0b57cec5SDimitry Andric AI = cast<AllocaInst>(SPCall->getArgOperand(1)); 491*0b57cec5SDimitry Andric } 492*0b57cec5SDimitry Andric 493*0b57cec5SDimitry Andric // Set HasIRCheck to true, so that SelectionDAG will not generate its own 494*0b57cec5SDimitry Andric // version. SelectionDAG called 'shouldEmitSDCheck' to check whether 495*0b57cec5SDimitry Andric // instrumentation has already been generated. 496*0b57cec5SDimitry Andric HasIRCheck = true; 497*0b57cec5SDimitry Andric 498bdd1243dSDimitry Andric // If we're instrumenting a block with a tail call, the check has to be 49923408297SDimitry Andric // inserted before the call rather than between it and the return. The 500bdd1243dSDimitry Andric // verifier guarantees that a tail call is either directly before the 50123408297SDimitry Andric // return or with a single correct bitcast of the return value in between so 50223408297SDimitry Andric // we don't need to worry about many situations here. 503bdd1243dSDimitry Andric Instruction *Prev = CheckLoc->getPrevNonDebugInstruction(); 504bdd1243dSDimitry Andric if (Prev && isa<CallInst>(Prev) && cast<CallInst>(Prev)->isTailCall()) 50523408297SDimitry Andric CheckLoc = Prev; 50623408297SDimitry Andric else if (Prev) { 50723408297SDimitry Andric Prev = Prev->getPrevNonDebugInstruction(); 508bdd1243dSDimitry Andric if (Prev && isa<CallInst>(Prev) && cast<CallInst>(Prev)->isTailCall()) 50923408297SDimitry Andric CheckLoc = Prev; 51023408297SDimitry Andric } 51123408297SDimitry Andric 512*0b57cec5SDimitry Andric // Generate epilogue instrumentation. The epilogue intrumentation can be 513*0b57cec5SDimitry Andric // function-based or inlined depending on which mechanism the target is 514*0b57cec5SDimitry Andric // providing. 515*0b57cec5SDimitry Andric if (Function *GuardCheck = TLI->getSSPStackGuardCheck(*M)) { 516*0b57cec5SDimitry Andric // Generate the function-based epilogue instrumentation. 517*0b57cec5SDimitry Andric // The target provides a guard check function, generate a call to it. 51823408297SDimitry Andric IRBuilder<> B(CheckLoc); 519*0b57cec5SDimitry Andric LoadInst *Guard = B.CreateLoad(B.getInt8PtrTy(), AI, true, "Guard"); 520*0b57cec5SDimitry Andric CallInst *Call = B.CreateCall(GuardCheck, {Guard}); 521*0b57cec5SDimitry Andric Call->setAttributes(GuardCheck->getAttributes()); 522*0b57cec5SDimitry Andric Call->setCallingConv(GuardCheck->getCallingConv()); 523*0b57cec5SDimitry Andric } else { 524*0b57cec5SDimitry Andric // Generate the epilogue with inline instrumentation. 52523408297SDimitry Andric // If we do not support SelectionDAG based calls, generate IR level 52623408297SDimitry Andric // calls. 527*0b57cec5SDimitry Andric // 528*0b57cec5SDimitry Andric // For each block with a return instruction, convert this: 529*0b57cec5SDimitry Andric // 530*0b57cec5SDimitry Andric // return: 531*0b57cec5SDimitry Andric // ... 532*0b57cec5SDimitry Andric // ret ... 533*0b57cec5SDimitry Andric // 534*0b57cec5SDimitry Andric // into this: 535*0b57cec5SDimitry Andric // 536*0b57cec5SDimitry Andric // return: 537*0b57cec5SDimitry Andric // ... 538*0b57cec5SDimitry Andric // %1 = <stack guard> 539*0b57cec5SDimitry Andric // %2 = load StackGuardSlot 540bdd1243dSDimitry Andric // %3 = icmp ne i1 %1, %2 541bdd1243dSDimitry Andric // br i1 %3, label %CallStackCheckFailBlk, label %SP_return 542*0b57cec5SDimitry Andric // 543*0b57cec5SDimitry Andric // SP_return: 544*0b57cec5SDimitry Andric // ret ... 545*0b57cec5SDimitry Andric // 546*0b57cec5SDimitry Andric // CallStackCheckFailBlk: 547*0b57cec5SDimitry Andric // call void @__stack_chk_fail() 548*0b57cec5SDimitry Andric // unreachable 549*0b57cec5SDimitry Andric 550*0b57cec5SDimitry Andric // Create the FailBB. We duplicate the BB every time since the MI tail 551*0b57cec5SDimitry Andric // merge pass will merge together all of the various BB into one including 552*0b57cec5SDimitry Andric // fail BB generated by the stack protector pseudo instruction. 553bdd1243dSDimitry Andric if (!FailBB) 554bdd1243dSDimitry Andric FailBB = CreateFailBB(); 555*0b57cec5SDimitry Andric 556bdd1243dSDimitry Andric IRBuilder<> B(CheckLoc); 557*0b57cec5SDimitry Andric Value *Guard = getStackGuard(TLI, M, B); 558*0b57cec5SDimitry Andric LoadInst *LI2 = B.CreateLoad(B.getInt8PtrTy(), AI, true); 559bdd1243dSDimitry Andric auto *Cmp = cast<ICmpInst>(B.CreateICmpNE(Guard, LI2)); 560*0b57cec5SDimitry Andric auto SuccessProb = 561*0b57cec5SDimitry Andric BranchProbabilityInfo::getBranchProbStackProtector(true); 562*0b57cec5SDimitry Andric auto FailureProb = 563*0b57cec5SDimitry Andric BranchProbabilityInfo::getBranchProbStackProtector(false); 564*0b57cec5SDimitry Andric MDNode *Weights = MDBuilder(F->getContext()) 565bdd1243dSDimitry Andric .createBranchWeights(FailureProb.getNumerator(), 566bdd1243dSDimitry Andric SuccessProb.getNumerator()); 567bdd1243dSDimitry Andric 568bdd1243dSDimitry Andric SplitBlockAndInsertIfThen(Cmp, CheckLoc, 569bdd1243dSDimitry Andric /*Unreachable=*/false, Weights, 570bdd1243dSDimitry Andric DTU ? &*DTU : nullptr, 571bdd1243dSDimitry Andric /*LI=*/nullptr, /*ThenBlock=*/FailBB); 572bdd1243dSDimitry Andric 573bdd1243dSDimitry Andric auto *BI = cast<BranchInst>(Cmp->getParent()->getTerminator()); 574bdd1243dSDimitry Andric BasicBlock *NewBB = BI->getSuccessor(1); 575bdd1243dSDimitry Andric NewBB->setName("SP_return"); 576bdd1243dSDimitry Andric NewBB->moveAfter(&BB); 577bdd1243dSDimitry Andric 578bdd1243dSDimitry Andric Cmp->setPredicate(Cmp->getInversePredicate()); 579bdd1243dSDimitry Andric BI->swapSuccessors(); 580*0b57cec5SDimitry Andric } 581*0b57cec5SDimitry Andric } 582*0b57cec5SDimitry Andric 583*0b57cec5SDimitry Andric // Return if we didn't modify any basic blocks. i.e., there are no return 584*0b57cec5SDimitry Andric // statements in the function. 585*0b57cec5SDimitry Andric return HasPrologue; 586*0b57cec5SDimitry Andric } 587*0b57cec5SDimitry Andric 588*0b57cec5SDimitry Andric /// CreateFailBB - Create a basic block to jump to when the stack protector 589*0b57cec5SDimitry Andric /// check fails. 590*0b57cec5SDimitry Andric BasicBlock *StackProtector::CreateFailBB() { 591*0b57cec5SDimitry Andric LLVMContext &Context = F->getContext(); 592*0b57cec5SDimitry Andric BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F); 593*0b57cec5SDimitry Andric IRBuilder<> B(FailBB); 594e8d8bef9SDimitry Andric if (F->getSubprogram()) 595e8d8bef9SDimitry Andric B.SetCurrentDebugLocation( 596e8d8bef9SDimitry Andric DILocation::get(Context, 0, 0, F->getSubprogram())); 597*0b57cec5SDimitry Andric if (Trip.isOSOpenBSD()) { 598*0b57cec5SDimitry Andric FunctionCallee StackChkFail = M->getOrInsertFunction( 599*0b57cec5SDimitry Andric "__stack_smash_handler", Type::getVoidTy(Context), 600*0b57cec5SDimitry Andric Type::getInt8PtrTy(Context)); 601*0b57cec5SDimitry Andric 602*0b57cec5SDimitry Andric B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH")); 603*0b57cec5SDimitry Andric } else { 604*0b57cec5SDimitry Andric FunctionCallee StackChkFail = 605*0b57cec5SDimitry Andric M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context)); 606*0b57cec5SDimitry Andric 607*0b57cec5SDimitry Andric B.CreateCall(StackChkFail, {}); 608*0b57cec5SDimitry Andric } 609*0b57cec5SDimitry Andric B.CreateUnreachable(); 610*0b57cec5SDimitry Andric return FailBB; 611*0b57cec5SDimitry Andric } 612*0b57cec5SDimitry Andric 613*0b57cec5SDimitry Andric bool StackProtector::shouldEmitSDCheck(const BasicBlock &BB) const { 614*0b57cec5SDimitry Andric return HasPrologue && !HasIRCheck && isa<ReturnInst>(BB.getTerminator()); 615*0b57cec5SDimitry Andric } 616*0b57cec5SDimitry Andric 617*0b57cec5SDimitry Andric void StackProtector::copyToMachineFrameInfo(MachineFrameInfo &MFI) const { 618*0b57cec5SDimitry Andric if (Layout.empty()) 619*0b57cec5SDimitry Andric return; 620*0b57cec5SDimitry Andric 621*0b57cec5SDimitry Andric for (int I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I) { 622*0b57cec5SDimitry Andric if (MFI.isDeadObjectIndex(I)) 623*0b57cec5SDimitry Andric continue; 624*0b57cec5SDimitry Andric 625*0b57cec5SDimitry Andric const AllocaInst *AI = MFI.getObjectAllocation(I); 626*0b57cec5SDimitry Andric if (!AI) 627*0b57cec5SDimitry Andric continue; 628*0b57cec5SDimitry Andric 629*0b57cec5SDimitry Andric SSPLayoutMap::const_iterator LI = Layout.find(AI); 630*0b57cec5SDimitry Andric if (LI == Layout.end()) 631*0b57cec5SDimitry Andric continue; 632*0b57cec5SDimitry Andric 633*0b57cec5SDimitry Andric MFI.setObjectSSPLayout(I, LI->second); 634*0b57cec5SDimitry Andric } 635*0b57cec5SDimitry Andric } 636