10b57cec5SDimitry Andric //===- StackProtector.cpp - Stack Protector Insertion ---------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This pass inserts stack protectors into functions which need them. A variable 100b57cec5SDimitry Andric // with a random value in it is stored onto the stack before the local variables 110b57cec5SDimitry Andric // are allocated. Upon exiting the block, the stored value is checked. If it's 120b57cec5SDimitry Andric // changed, then there was some sort of violation and the program aborts. 130b57cec5SDimitry Andric // 140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "llvm/CodeGen/StackProtector.h" 170b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 18*fe013be4SDimitry Andric #include "llvm/ADT/SmallVector.h" 190b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 200b57cec5SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h" 215ffd83dbSDimitry Andric #include "llvm/Analysis/MemoryLocation.h" 220b57cec5SDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 270b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 280b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 290b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 300b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 310b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 320b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 33*fe013be4SDimitry Andric #include "llvm/IR/EHPersonalities.h" 340b57cec5SDimitry Andric #include "llvm/IR/Function.h" 350b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h" 360b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 370b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 380b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 390b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 400b57cec5SDimitry Andric #include "llvm/IR/MDBuilder.h" 410b57cec5SDimitry Andric #include "llvm/IR/Module.h" 420b57cec5SDimitry Andric #include "llvm/IR/Type.h" 430b57cec5SDimitry Andric #include "llvm/IR/User.h" 44480093f4SDimitry Andric #include "llvm/InitializePasses.h" 450b57cec5SDimitry Andric #include "llvm/Pass.h" 460b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 470b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 480b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 490b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 50bdd1243dSDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h" 51bdd1243dSDimitry Andric #include <optional> 520b57cec5SDimitry Andric #include <utility> 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric using namespace llvm; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric #define DEBUG_TYPE "stack-protector" 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric STATISTIC(NumFunProtected, "Number of functions protected"); 590b57cec5SDimitry Andric STATISTIC(NumAddrTaken, "Number of local variables that have their address" 600b57cec5SDimitry Andric " taken."); 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric static cl::opt<bool> EnableSelectionDAGSP("enable-selectiondag-sp", 630b57cec5SDimitry Andric cl::init(true), cl::Hidden); 64bdd1243dSDimitry Andric static cl::opt<bool> DisableCheckNoReturn("disable-check-noreturn-call", 65bdd1243dSDimitry Andric cl::init(false), cl::Hidden); 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric char StackProtector::ID = 0; 680b57cec5SDimitry Andric 69bdd1243dSDimitry Andric StackProtector::StackProtector() : FunctionPass(ID) { 70480093f4SDimitry Andric initializeStackProtectorPass(*PassRegistry::getPassRegistry()); 71480093f4SDimitry Andric } 72480093f4SDimitry Andric 730b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(StackProtector, DEBUG_TYPE, 740b57cec5SDimitry Andric "Insert stack protectors", false, true) 750b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) 76fe6060f1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 770b57cec5SDimitry Andric INITIALIZE_PASS_END(StackProtector, DEBUG_TYPE, 780b57cec5SDimitry Andric "Insert stack protectors", false, true) 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric FunctionPass *llvm::createStackProtectorPass() { return new StackProtector(); } 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric void StackProtector::getAnalysisUsage(AnalysisUsage &AU) const { 830b57cec5SDimitry Andric AU.addRequired<TargetPassConfig>(); 840b57cec5SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>(); 850b57cec5SDimitry Andric } 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric bool StackProtector::runOnFunction(Function &Fn) { 880b57cec5SDimitry Andric F = &Fn; 890b57cec5SDimitry Andric M = F->getParent(); 90bdd1243dSDimitry Andric if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>()) 91bdd1243dSDimitry Andric DTU.emplace(DTWP->getDomTree(), DomTreeUpdater::UpdateStrategy::Lazy); 920b57cec5SDimitry Andric TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>(); 930b57cec5SDimitry Andric Trip = TM->getTargetTriple(); 940b57cec5SDimitry Andric TLI = TM->getSubtargetImpl(Fn)->getTargetLowering(); 950b57cec5SDimitry Andric HasPrologue = false; 960b57cec5SDimitry Andric HasIRCheck = false; 970b57cec5SDimitry Andric 98bdd1243dSDimitry Andric SSPBufferSize = Fn.getFnAttributeAsParsedInteger( 99bdd1243dSDimitry Andric "stack-protector-buffer-size", DefaultSSPBufferSize); 100*fe013be4SDimitry Andric if (!requiresStackProtector(F, &Layout)) 1010b57cec5SDimitry Andric return false; 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric // TODO(etienneb): Functions with funclets are not correctly supported now. 1040b57cec5SDimitry Andric // Do nothing if this is funclet-based personality. 1050b57cec5SDimitry Andric if (Fn.hasPersonalityFn()) { 1060b57cec5SDimitry Andric EHPersonality Personality = classifyEHPersonality(Fn.getPersonalityFn()); 1070b57cec5SDimitry Andric if (isFuncletEHPersonality(Personality)) 1080b57cec5SDimitry Andric return false; 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric ++NumFunProtected; 112bdd1243dSDimitry Andric bool Changed = InsertStackProtectors(); 113bdd1243dSDimitry Andric #ifdef EXPENSIVE_CHECKS 114bdd1243dSDimitry Andric assert((!DTU || 115bdd1243dSDimitry Andric DTU->getDomTree().verify(DominatorTree::VerificationLevel::Full)) && 116bdd1243dSDimitry Andric "Failed to maintain validity of domtree!"); 117bdd1243dSDimitry Andric #endif 118bdd1243dSDimitry Andric DTU.reset(); 119bdd1243dSDimitry Andric return Changed; 1200b57cec5SDimitry Andric } 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric /// \param [out] IsLarge is set to true if a protectable array is found and 1230b57cec5SDimitry Andric /// it is "large" ( >= ssp-buffer-size). In the case of a structure with 1240b57cec5SDimitry Andric /// multiple arrays, this gets set if any of them is large. 125*fe013be4SDimitry Andric static bool ContainsProtectableArray(Type *Ty, Module *M, unsigned SSPBufferSize, 126*fe013be4SDimitry Andric bool &IsLarge, bool Strong, 127*fe013be4SDimitry Andric bool InStruct) { 1280b57cec5SDimitry Andric if (!Ty) 1290b57cec5SDimitry Andric return false; 1300b57cec5SDimitry Andric if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) { 1310b57cec5SDimitry Andric if (!AT->getElementType()->isIntegerTy(8)) { 1320b57cec5SDimitry Andric // If we're on a non-Darwin platform or we're inside of a structure, don't 1330b57cec5SDimitry Andric // add stack protectors unless the array is a character array. 1340b57cec5SDimitry Andric // However, in strong mode any array, regardless of type and size, 1350b57cec5SDimitry Andric // triggers a protector. 136*fe013be4SDimitry Andric if (!Strong && (InStruct || !Triple(M->getTargetTriple()).isOSDarwin())) 1370b57cec5SDimitry Andric return false; 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric // If an array has more than SSPBufferSize bytes of allocated space, then we 1410b57cec5SDimitry Andric // emit stack protectors. 1420b57cec5SDimitry Andric if (SSPBufferSize <= M->getDataLayout().getTypeAllocSize(AT)) { 1430b57cec5SDimitry Andric IsLarge = true; 1440b57cec5SDimitry Andric return true; 1450b57cec5SDimitry Andric } 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric if (Strong) 1480b57cec5SDimitry Andric // Require a protector for all arrays in strong mode 1490b57cec5SDimitry Andric return true; 1500b57cec5SDimitry Andric } 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric const StructType *ST = dyn_cast<StructType>(Ty); 1530b57cec5SDimitry Andric if (!ST) 1540b57cec5SDimitry Andric return false; 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric bool NeedsProtector = false; 157349cc55cSDimitry Andric for (Type *ET : ST->elements()) 158*fe013be4SDimitry Andric if (ContainsProtectableArray(ET, M, SSPBufferSize, IsLarge, Strong, true)) { 1590b57cec5SDimitry Andric // If the element is a protectable array and is large (>= SSPBufferSize) 1600b57cec5SDimitry Andric // then we are done. If the protectable array is not large, then 1610b57cec5SDimitry Andric // keep looking in case a subsequent element is a large array. 1620b57cec5SDimitry Andric if (IsLarge) 1630b57cec5SDimitry Andric return true; 1640b57cec5SDimitry Andric NeedsProtector = true; 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric return NeedsProtector; 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric 170*fe013be4SDimitry Andric /// Check whether a stack allocation has its address taken. 171*fe013be4SDimitry Andric static bool HasAddressTaken(const Instruction *AI, TypeSize AllocSize, 172*fe013be4SDimitry Andric Module *M, 173*fe013be4SDimitry Andric SmallPtrSet<const PHINode *, 16> &VisitedPHIs) { 1745ffd83dbSDimitry Andric const DataLayout &DL = M->getDataLayout(); 175c14a5a88SDimitry Andric for (const User *U : AI->users()) { 176c14a5a88SDimitry Andric const auto *I = cast<Instruction>(U); 1775ffd83dbSDimitry Andric // If this instruction accesses memory make sure it doesn't access beyond 1785ffd83dbSDimitry Andric // the bounds of the allocated object. 179bdd1243dSDimitry Andric std::optional<MemoryLocation> MemLoc = MemoryLocation::getOrNone(I); 18081ad6265SDimitry Andric if (MemLoc && MemLoc->Size.hasValue() && 1810eae32dcSDimitry Andric !TypeSize::isKnownGE(AllocSize, 1820eae32dcSDimitry Andric TypeSize::getFixed(MemLoc->Size.getValue()))) 1835ffd83dbSDimitry Andric return true; 184c14a5a88SDimitry Andric switch (I->getOpcode()) { 185c14a5a88SDimitry Andric case Instruction::Store: 186c14a5a88SDimitry Andric if (AI == cast<StoreInst>(I)->getValueOperand()) 187c14a5a88SDimitry Andric return true; 188c14a5a88SDimitry Andric break; 189c14a5a88SDimitry Andric case Instruction::AtomicCmpXchg: 190c14a5a88SDimitry Andric // cmpxchg conceptually includes both a load and store from the same 191c14a5a88SDimitry Andric // location. So, like store, the value being stored is what matters. 192c14a5a88SDimitry Andric if (AI == cast<AtomicCmpXchgInst>(I)->getNewValOperand()) 193c14a5a88SDimitry Andric return true; 194c14a5a88SDimitry Andric break; 195c14a5a88SDimitry Andric case Instruction::PtrToInt: 196c14a5a88SDimitry Andric if (AI == cast<PtrToIntInst>(I)->getOperand(0)) 197c14a5a88SDimitry Andric return true; 198c14a5a88SDimitry Andric break; 199c14a5a88SDimitry Andric case Instruction::Call: { 200c14a5a88SDimitry Andric // Ignore intrinsics that do not become real instructions. 201c14a5a88SDimitry Andric // TODO: Narrow this to intrinsics that have store-like effects. 202c14a5a88SDimitry Andric const auto *CI = cast<CallInst>(I); 203d409305fSDimitry Andric if (!CI->isDebugOrPseudoInst() && !CI->isLifetimeStartOrEnd()) 204c14a5a88SDimitry Andric return true; 205c14a5a88SDimitry Andric break; 206c14a5a88SDimitry Andric } 207c14a5a88SDimitry Andric case Instruction::Invoke: 208c14a5a88SDimitry Andric return true; 2095ffd83dbSDimitry Andric case Instruction::GetElementPtr: { 2105ffd83dbSDimitry Andric // If the GEP offset is out-of-bounds, or is non-constant and so has to be 2115ffd83dbSDimitry Andric // assumed to be potentially out-of-bounds, then any memory access that 2125ffd83dbSDimitry Andric // would use it could also be out-of-bounds meaning stack protection is 2135ffd83dbSDimitry Andric // required. 2145ffd83dbSDimitry Andric const GetElementPtrInst *GEP = cast<GetElementPtrInst>(I); 2150eae32dcSDimitry Andric unsigned IndexSize = DL.getIndexTypeSizeInBits(I->getType()); 2160eae32dcSDimitry Andric APInt Offset(IndexSize, 0); 2170eae32dcSDimitry Andric if (!GEP->accumulateConstantOffset(DL, Offset)) 2180eae32dcSDimitry Andric return true; 2190eae32dcSDimitry Andric TypeSize OffsetSize = TypeSize::Fixed(Offset.getLimitedValue()); 2200eae32dcSDimitry Andric if (!TypeSize::isKnownGT(AllocSize, OffsetSize)) 2215ffd83dbSDimitry Andric return true; 2225ffd83dbSDimitry Andric // Adjust AllocSize to be the space remaining after this offset. 2230eae32dcSDimitry Andric // We can't subtract a fixed size from a scalable one, so in that case 2240eae32dcSDimitry Andric // assume the scalable value is of minimum size. 2250eae32dcSDimitry Andric TypeSize NewAllocSize = 2260eae32dcSDimitry Andric TypeSize::Fixed(AllocSize.getKnownMinValue()) - OffsetSize; 227*fe013be4SDimitry Andric if (HasAddressTaken(I, NewAllocSize, M, VisitedPHIs)) 2285ffd83dbSDimitry Andric return true; 2295ffd83dbSDimitry Andric break; 2305ffd83dbSDimitry Andric } 231c14a5a88SDimitry Andric case Instruction::BitCast: 232c14a5a88SDimitry Andric case Instruction::Select: 233c14a5a88SDimitry Andric case Instruction::AddrSpaceCast: 234*fe013be4SDimitry Andric if (HasAddressTaken(I, AllocSize, M, VisitedPHIs)) 235c14a5a88SDimitry Andric return true; 236c14a5a88SDimitry Andric break; 237c14a5a88SDimitry Andric case Instruction::PHI: { 238c14a5a88SDimitry Andric // Keep track of what PHI nodes we have already visited to ensure 239c14a5a88SDimitry Andric // they are only visited once. 240c14a5a88SDimitry Andric const auto *PN = cast<PHINode>(I); 241c14a5a88SDimitry Andric if (VisitedPHIs.insert(PN).second) 242*fe013be4SDimitry Andric if (HasAddressTaken(PN, AllocSize, M, VisitedPHIs)) 243c14a5a88SDimitry Andric return true; 244c14a5a88SDimitry Andric break; 245c14a5a88SDimitry Andric } 246c14a5a88SDimitry Andric case Instruction::Load: 247c14a5a88SDimitry Andric case Instruction::AtomicRMW: 248c14a5a88SDimitry Andric case Instruction::Ret: 249c14a5a88SDimitry Andric // These instructions take an address operand, but have load-like or 250c14a5a88SDimitry Andric // other innocuous behavior that should not trigger a stack protector. 251c14a5a88SDimitry Andric // atomicrmw conceptually has both load and store semantics, but the 252c14a5a88SDimitry Andric // value being stored must be integer; so if a pointer is being stored, 253c14a5a88SDimitry Andric // we'll catch it in the PtrToInt case above. 254c14a5a88SDimitry Andric break; 255c14a5a88SDimitry Andric default: 256c14a5a88SDimitry Andric // Conservatively return true for any instruction that takes an address 257c14a5a88SDimitry Andric // operand, but is not handled above. 258c14a5a88SDimitry Andric return true; 259c14a5a88SDimitry Andric } 260c14a5a88SDimitry Andric } 261c14a5a88SDimitry Andric return false; 262c14a5a88SDimitry Andric } 263c14a5a88SDimitry Andric 2640b57cec5SDimitry Andric /// Search for the first call to the llvm.stackprotector intrinsic and return it 2650b57cec5SDimitry Andric /// if present. 2660b57cec5SDimitry Andric static const CallInst *findStackProtectorIntrinsic(Function &F) { 2670b57cec5SDimitry Andric for (const BasicBlock &BB : F) 2680b57cec5SDimitry Andric for (const Instruction &I : BB) 269e8d8bef9SDimitry Andric if (const auto *II = dyn_cast<IntrinsicInst>(&I)) 270e8d8bef9SDimitry Andric if (II->getIntrinsicID() == Intrinsic::stackprotector) 271e8d8bef9SDimitry Andric return II; 2720b57cec5SDimitry Andric return nullptr; 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric /// Check whether or not this function needs a stack protector based 2760b57cec5SDimitry Andric /// upon the stack protector level. 2770b57cec5SDimitry Andric /// 2780b57cec5SDimitry Andric /// We use two heuristics: a standard (ssp) and strong (sspstrong). 2790b57cec5SDimitry Andric /// The standard heuristic which will add a guard variable to functions that 2800b57cec5SDimitry Andric /// call alloca with a either a variable size or a size >= SSPBufferSize, 2810b57cec5SDimitry Andric /// functions with character buffers larger than SSPBufferSize, and functions 2820b57cec5SDimitry Andric /// with aggregates containing character buffers larger than SSPBufferSize. The 2830b57cec5SDimitry Andric /// strong heuristic will add a guard variables to functions that call alloca 2840b57cec5SDimitry Andric /// regardless of size, functions with any buffer regardless of type and size, 2850b57cec5SDimitry Andric /// functions with aggregates that contain any buffer regardless of type and 2860b57cec5SDimitry Andric /// size, and functions that contain stack-based variables that have had their 2870b57cec5SDimitry Andric /// address taken. 288*fe013be4SDimitry Andric bool StackProtector::requiresStackProtector(Function *F, SSPLayoutMap *Layout) { 289*fe013be4SDimitry Andric Module *M = F->getParent(); 2900b57cec5SDimitry Andric bool Strong = false; 2910b57cec5SDimitry Andric bool NeedsProtector = false; 2920b57cec5SDimitry Andric 293*fe013be4SDimitry Andric // The set of PHI nodes visited when determining if a variable's reference has 294*fe013be4SDimitry Andric // been taken. This set is maintained to ensure we don't visit the same PHI 295*fe013be4SDimitry Andric // node multiple times. 296*fe013be4SDimitry Andric SmallPtrSet<const PHINode *, 16> VisitedPHIs; 297*fe013be4SDimitry Andric 298*fe013be4SDimitry Andric unsigned SSPBufferSize = F->getFnAttributeAsParsedInteger( 299*fe013be4SDimitry Andric "stack-protector-buffer-size", DefaultSSPBufferSize); 300*fe013be4SDimitry Andric 3010b57cec5SDimitry Andric if (F->hasFnAttribute(Attribute::SafeStack)) 3020b57cec5SDimitry Andric return false; 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric // We are constructing the OptimizationRemarkEmitter on the fly rather than 3050b57cec5SDimitry Andric // using the analysis pass to avoid building DominatorTree and LoopInfo which 3060b57cec5SDimitry Andric // are not available this late in the IR pipeline. 3070b57cec5SDimitry Andric OptimizationRemarkEmitter ORE(F); 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric if (F->hasFnAttribute(Attribute::StackProtectReq)) { 310*fe013be4SDimitry Andric if (!Layout) 311*fe013be4SDimitry Andric return true; 3120b57cec5SDimitry Andric ORE.emit([&]() { 3130b57cec5SDimitry Andric return OptimizationRemark(DEBUG_TYPE, "StackProtectorRequested", F) 3140b57cec5SDimitry Andric << "Stack protection applied to function " 3150b57cec5SDimitry Andric << ore::NV("Function", F) 3160b57cec5SDimitry Andric << " due to a function attribute or command-line switch"; 3170b57cec5SDimitry Andric }); 3180b57cec5SDimitry Andric NeedsProtector = true; 3190b57cec5SDimitry Andric Strong = true; // Use the same heuristic as strong to determine SSPLayout 3200b57cec5SDimitry Andric } else if (F->hasFnAttribute(Attribute::StackProtectStrong)) 3210b57cec5SDimitry Andric Strong = true; 3220b57cec5SDimitry Andric else if (!F->hasFnAttribute(Attribute::StackProtect)) 3230b57cec5SDimitry Andric return false; 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric for (const BasicBlock &BB : *F) { 3260b57cec5SDimitry Andric for (const Instruction &I : BB) { 3270b57cec5SDimitry Andric if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { 3280b57cec5SDimitry Andric if (AI->isArrayAllocation()) { 3290b57cec5SDimitry Andric auto RemarkBuilder = [&]() { 3300b57cec5SDimitry Andric return OptimizationRemark(DEBUG_TYPE, "StackProtectorAllocaOrArray", 3310b57cec5SDimitry Andric &I) 3320b57cec5SDimitry Andric << "Stack protection applied to function " 3330b57cec5SDimitry Andric << ore::NV("Function", F) 3340b57cec5SDimitry Andric << " due to a call to alloca or use of a variable length " 3350b57cec5SDimitry Andric "array"; 3360b57cec5SDimitry Andric }; 3370b57cec5SDimitry Andric if (const auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) { 3380b57cec5SDimitry Andric if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) { 3390b57cec5SDimitry Andric // A call to alloca with size >= SSPBufferSize requires 3400b57cec5SDimitry Andric // stack protectors. 341*fe013be4SDimitry Andric if (!Layout) 342*fe013be4SDimitry Andric return true; 343*fe013be4SDimitry Andric Layout->insert( 344*fe013be4SDimitry Andric std::make_pair(AI, MachineFrameInfo::SSPLK_LargeArray)); 3450b57cec5SDimitry Andric ORE.emit(RemarkBuilder); 3460b57cec5SDimitry Andric NeedsProtector = true; 3470b57cec5SDimitry Andric } else if (Strong) { 3480b57cec5SDimitry Andric // Require protectors for all alloca calls in strong mode. 349*fe013be4SDimitry Andric if (!Layout) 350*fe013be4SDimitry Andric return true; 351*fe013be4SDimitry Andric Layout->insert( 352*fe013be4SDimitry Andric std::make_pair(AI, MachineFrameInfo::SSPLK_SmallArray)); 3530b57cec5SDimitry Andric ORE.emit(RemarkBuilder); 3540b57cec5SDimitry Andric NeedsProtector = true; 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric } else { 3570b57cec5SDimitry Andric // A call to alloca with a variable size requires protectors. 358*fe013be4SDimitry Andric if (!Layout) 359*fe013be4SDimitry Andric return true; 360*fe013be4SDimitry Andric Layout->insert( 361*fe013be4SDimitry Andric std::make_pair(AI, MachineFrameInfo::SSPLK_LargeArray)); 3620b57cec5SDimitry Andric ORE.emit(RemarkBuilder); 3630b57cec5SDimitry Andric NeedsProtector = true; 3640b57cec5SDimitry Andric } 3650b57cec5SDimitry Andric continue; 3660b57cec5SDimitry Andric } 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric bool IsLarge = false; 369*fe013be4SDimitry Andric if (ContainsProtectableArray(AI->getAllocatedType(), M, SSPBufferSize, 370*fe013be4SDimitry Andric IsLarge, Strong, false)) { 371*fe013be4SDimitry Andric if (!Layout) 372*fe013be4SDimitry Andric return true; 373*fe013be4SDimitry Andric Layout->insert(std::make_pair( 374*fe013be4SDimitry Andric AI, IsLarge ? MachineFrameInfo::SSPLK_LargeArray 3750b57cec5SDimitry Andric : MachineFrameInfo::SSPLK_SmallArray)); 3760b57cec5SDimitry Andric ORE.emit([&]() { 3770b57cec5SDimitry Andric return OptimizationRemark(DEBUG_TYPE, "StackProtectorBuffer", &I) 3780b57cec5SDimitry Andric << "Stack protection applied to function " 3790b57cec5SDimitry Andric << ore::NV("Function", F) 3800b57cec5SDimitry Andric << " due to a stack allocated buffer or struct containing a " 3810b57cec5SDimitry Andric "buffer"; 3820b57cec5SDimitry Andric }); 3830b57cec5SDimitry Andric NeedsProtector = true; 3840b57cec5SDimitry Andric continue; 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric 387*fe013be4SDimitry Andric if (Strong && 388*fe013be4SDimitry Andric HasAddressTaken( 389*fe013be4SDimitry Andric AI, M->getDataLayout().getTypeAllocSize(AI->getAllocatedType()), 390*fe013be4SDimitry Andric M, VisitedPHIs)) { 3910b57cec5SDimitry Andric ++NumAddrTaken; 392*fe013be4SDimitry Andric if (!Layout) 393*fe013be4SDimitry Andric return true; 394*fe013be4SDimitry Andric Layout->insert(std::make_pair(AI, MachineFrameInfo::SSPLK_AddrOf)); 3950b57cec5SDimitry Andric ORE.emit([&]() { 3960b57cec5SDimitry Andric return OptimizationRemark(DEBUG_TYPE, "StackProtectorAddressTaken", 3970b57cec5SDimitry Andric &I) 3980b57cec5SDimitry Andric << "Stack protection applied to function " 3990b57cec5SDimitry Andric << ore::NV("Function", F) 4000b57cec5SDimitry Andric << " due to the address of a local variable being taken"; 4010b57cec5SDimitry Andric }); 4020b57cec5SDimitry Andric NeedsProtector = true; 4030b57cec5SDimitry Andric } 4045ffd83dbSDimitry Andric // Clear any PHIs that we visited, to make sure we examine all uses of 4055ffd83dbSDimitry Andric // any subsequent allocas that we look at. 4065ffd83dbSDimitry Andric VisitedPHIs.clear(); 4070b57cec5SDimitry Andric } 4080b57cec5SDimitry Andric } 4090b57cec5SDimitry Andric } 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric return NeedsProtector; 4120b57cec5SDimitry Andric } 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric /// Create a stack guard loading and populate whether SelectionDAG SSP is 4150b57cec5SDimitry Andric /// supported. 4160b57cec5SDimitry Andric static Value *getStackGuard(const TargetLoweringBase *TLI, Module *M, 4170b57cec5SDimitry Andric IRBuilder<> &B, 4180b57cec5SDimitry Andric bool *SupportsSelectionDAGSP = nullptr) { 419e8d8bef9SDimitry Andric Value *Guard = TLI->getIRStackGuard(B); 420fe6060f1SDimitry Andric StringRef GuardMode = M->getStackProtectorGuard(); 421fe6060f1SDimitry Andric if ((GuardMode == "tls" || GuardMode.empty()) && Guard) 4220b57cec5SDimitry Andric return B.CreateLoad(B.getInt8PtrTy(), Guard, true, "StackGuard"); 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric // Use SelectionDAG SSP handling, since there isn't an IR guard. 4250b57cec5SDimitry Andric // 4260b57cec5SDimitry Andric // This is more or less weird, since we optionally output whether we 4270b57cec5SDimitry Andric // should perform a SelectionDAG SP here. The reason is that it's strictly 4280b57cec5SDimitry Andric // defined as !TLI->getIRStackGuard(B), where getIRStackGuard is also 4290b57cec5SDimitry Andric // mutating. There is no way to get this bit without mutating the IR, so 4300b57cec5SDimitry Andric // getting this bit has to happen in this right time. 4310b57cec5SDimitry Andric // 4320b57cec5SDimitry Andric // We could have define a new function TLI::supportsSelectionDAGSP(), but that 4330b57cec5SDimitry Andric // will put more burden on the backends' overriding work, especially when it 4340b57cec5SDimitry Andric // actually conveys the same information getIRStackGuard() already gives. 4350b57cec5SDimitry Andric if (SupportsSelectionDAGSP) 4360b57cec5SDimitry Andric *SupportsSelectionDAGSP = true; 4370b57cec5SDimitry Andric TLI->insertSSPDeclarations(*M); 4380b57cec5SDimitry Andric return B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackguard)); 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric /// Insert code into the entry block that stores the stack guard 4420b57cec5SDimitry Andric /// variable onto the stack: 4430b57cec5SDimitry Andric /// 4440b57cec5SDimitry Andric /// entry: 4450b57cec5SDimitry Andric /// StackGuardSlot = alloca i8* 4460b57cec5SDimitry Andric /// StackGuard = <stack guard> 4470b57cec5SDimitry Andric /// call void @llvm.stackprotector(StackGuard, StackGuardSlot) 4480b57cec5SDimitry Andric /// 4490b57cec5SDimitry Andric /// Returns true if the platform/triple supports the stackprotectorcreate pseudo 4500b57cec5SDimitry Andric /// node. 451bdd1243dSDimitry Andric static bool CreatePrologue(Function *F, Module *M, Instruction *CheckLoc, 4520b57cec5SDimitry Andric const TargetLoweringBase *TLI, AllocaInst *&AI) { 4530b57cec5SDimitry Andric bool SupportsSelectionDAGSP = false; 4540b57cec5SDimitry Andric IRBuilder<> B(&F->getEntryBlock().front()); 455bdd1243dSDimitry Andric PointerType *PtrTy = Type::getInt8PtrTy(CheckLoc->getContext()); 4560b57cec5SDimitry Andric AI = B.CreateAlloca(PtrTy, nullptr, "StackGuardSlot"); 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric Value *GuardSlot = getStackGuard(TLI, M, B, &SupportsSelectionDAGSP); 4590b57cec5SDimitry Andric B.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), 4600b57cec5SDimitry Andric {GuardSlot, AI}); 4610b57cec5SDimitry Andric return SupportsSelectionDAGSP; 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric /// InsertStackProtectors - Insert code into the prologue and epilogue of the 4650b57cec5SDimitry Andric /// function. 4660b57cec5SDimitry Andric /// 4670b57cec5SDimitry Andric /// - The prologue code loads and stores the stack guard onto the stack. 4680b57cec5SDimitry Andric /// - The epilogue checks the value stored in the prologue against the original 4690b57cec5SDimitry Andric /// value. It calls __stack_chk_fail if they differ. 4700b57cec5SDimitry Andric bool StackProtector::InsertStackProtectors() { 4710b57cec5SDimitry Andric // If the target wants to XOR the frame pointer into the guard value, it's 4720b57cec5SDimitry Andric // impossible to emit the check in IR, so the target *must* support stack 4730b57cec5SDimitry Andric // protection in SDAG. 4740b57cec5SDimitry Andric bool SupportsSelectionDAGSP = 4750b57cec5SDimitry Andric TLI->useStackGuardXorFP() || 476349cc55cSDimitry Andric (EnableSelectionDAGSP && !TM->Options.EnableFastISel); 4770b57cec5SDimitry Andric AllocaInst *AI = nullptr; // Place on stack that stores the stack guard. 478bdd1243dSDimitry Andric BasicBlock *FailBB = nullptr; 4790b57cec5SDimitry Andric 480349cc55cSDimitry Andric for (BasicBlock &BB : llvm::make_early_inc_range(*F)) { 481bdd1243dSDimitry Andric // This is stack protector auto generated check BB, skip it. 482bdd1243dSDimitry Andric if (&BB == FailBB) 483bdd1243dSDimitry Andric continue; 484bdd1243dSDimitry Andric Instruction *CheckLoc = dyn_cast<ReturnInst>(BB.getTerminator()); 4859e7101a8SDimitry Andric if (!CheckLoc && !DisableCheckNoReturn) 4869e7101a8SDimitry Andric for (auto &Inst : BB) 4879e7101a8SDimitry Andric if (auto *CB = dyn_cast<CallBase>(&Inst)) 4889e7101a8SDimitry Andric // Do stack check before noreturn calls that aren't nounwind (e.g: 4899e7101a8SDimitry Andric // __cxa_throw). 4909e7101a8SDimitry Andric if (CB->doesNotReturn() && !CB->doesNotThrow()) { 491bdd1243dSDimitry Andric CheckLoc = CB; 492bdd1243dSDimitry Andric break; 493bdd1243dSDimitry Andric } 494bdd1243dSDimitry Andric 495bdd1243dSDimitry Andric if (!CheckLoc) 4960b57cec5SDimitry Andric continue; 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric // Generate prologue instrumentation if not already generated. 4990b57cec5SDimitry Andric if (!HasPrologue) { 5000b57cec5SDimitry Andric HasPrologue = true; 501bdd1243dSDimitry Andric SupportsSelectionDAGSP &= CreatePrologue(F, M, CheckLoc, TLI, AI); 5020b57cec5SDimitry Andric } 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric // SelectionDAG based code generation. Nothing else needs to be done here. 5050b57cec5SDimitry Andric // The epilogue instrumentation is postponed to SelectionDAG. 5060b57cec5SDimitry Andric if (SupportsSelectionDAGSP) 5070b57cec5SDimitry Andric break; 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric // Find the stack guard slot if the prologue was not created by this pass 5100b57cec5SDimitry Andric // itself via a previous call to CreatePrologue(). 5110b57cec5SDimitry Andric if (!AI) { 5120b57cec5SDimitry Andric const CallInst *SPCall = findStackProtectorIntrinsic(*F); 5130b57cec5SDimitry Andric assert(SPCall && "Call to llvm.stackprotector is missing"); 5140b57cec5SDimitry Andric AI = cast<AllocaInst>(SPCall->getArgOperand(1)); 5150b57cec5SDimitry Andric } 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric // Set HasIRCheck to true, so that SelectionDAG will not generate its own 5180b57cec5SDimitry Andric // version. SelectionDAG called 'shouldEmitSDCheck' to check whether 5190b57cec5SDimitry Andric // instrumentation has already been generated. 5200b57cec5SDimitry Andric HasIRCheck = true; 5210b57cec5SDimitry Andric 522bdd1243dSDimitry Andric // If we're instrumenting a block with a tail call, the check has to be 52323408297SDimitry Andric // inserted before the call rather than between it and the return. The 524bdd1243dSDimitry Andric // verifier guarantees that a tail call is either directly before the 52523408297SDimitry Andric // return or with a single correct bitcast of the return value in between so 52623408297SDimitry Andric // we don't need to worry about many situations here. 527bdd1243dSDimitry Andric Instruction *Prev = CheckLoc->getPrevNonDebugInstruction(); 528bdd1243dSDimitry Andric if (Prev && isa<CallInst>(Prev) && cast<CallInst>(Prev)->isTailCall()) 52923408297SDimitry Andric CheckLoc = Prev; 53023408297SDimitry Andric else if (Prev) { 53123408297SDimitry Andric Prev = Prev->getPrevNonDebugInstruction(); 532bdd1243dSDimitry Andric if (Prev && isa<CallInst>(Prev) && cast<CallInst>(Prev)->isTailCall()) 53323408297SDimitry Andric CheckLoc = Prev; 53423408297SDimitry Andric } 53523408297SDimitry Andric 5360b57cec5SDimitry Andric // Generate epilogue instrumentation. The epilogue intrumentation can be 5370b57cec5SDimitry Andric // function-based or inlined depending on which mechanism the target is 5380b57cec5SDimitry Andric // providing. 5390b57cec5SDimitry Andric if (Function *GuardCheck = TLI->getSSPStackGuardCheck(*M)) { 5400b57cec5SDimitry Andric // Generate the function-based epilogue instrumentation. 5410b57cec5SDimitry Andric // The target provides a guard check function, generate a call to it. 54223408297SDimitry Andric IRBuilder<> B(CheckLoc); 5430b57cec5SDimitry Andric LoadInst *Guard = B.CreateLoad(B.getInt8PtrTy(), AI, true, "Guard"); 5440b57cec5SDimitry Andric CallInst *Call = B.CreateCall(GuardCheck, {Guard}); 5450b57cec5SDimitry Andric Call->setAttributes(GuardCheck->getAttributes()); 5460b57cec5SDimitry Andric Call->setCallingConv(GuardCheck->getCallingConv()); 5470b57cec5SDimitry Andric } else { 5480b57cec5SDimitry Andric // Generate the epilogue with inline instrumentation. 54923408297SDimitry Andric // If we do not support SelectionDAG based calls, generate IR level 55023408297SDimitry Andric // calls. 5510b57cec5SDimitry Andric // 5520b57cec5SDimitry Andric // For each block with a return instruction, convert this: 5530b57cec5SDimitry Andric // 5540b57cec5SDimitry Andric // return: 5550b57cec5SDimitry Andric // ... 5560b57cec5SDimitry Andric // ret ... 5570b57cec5SDimitry Andric // 5580b57cec5SDimitry Andric // into this: 5590b57cec5SDimitry Andric // 5600b57cec5SDimitry Andric // return: 5610b57cec5SDimitry Andric // ... 5620b57cec5SDimitry Andric // %1 = <stack guard> 5630b57cec5SDimitry Andric // %2 = load StackGuardSlot 564bdd1243dSDimitry Andric // %3 = icmp ne i1 %1, %2 565bdd1243dSDimitry Andric // br i1 %3, label %CallStackCheckFailBlk, label %SP_return 5660b57cec5SDimitry Andric // 5670b57cec5SDimitry Andric // SP_return: 5680b57cec5SDimitry Andric // ret ... 5690b57cec5SDimitry Andric // 5700b57cec5SDimitry Andric // CallStackCheckFailBlk: 5710b57cec5SDimitry Andric // call void @__stack_chk_fail() 5720b57cec5SDimitry Andric // unreachable 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andric // Create the FailBB. We duplicate the BB every time since the MI tail 5750b57cec5SDimitry Andric // merge pass will merge together all of the various BB into one including 5760b57cec5SDimitry Andric // fail BB generated by the stack protector pseudo instruction. 577bdd1243dSDimitry Andric if (!FailBB) 578bdd1243dSDimitry Andric FailBB = CreateFailBB(); 5790b57cec5SDimitry Andric 580bdd1243dSDimitry Andric IRBuilder<> B(CheckLoc); 5810b57cec5SDimitry Andric Value *Guard = getStackGuard(TLI, M, B); 5820b57cec5SDimitry Andric LoadInst *LI2 = B.CreateLoad(B.getInt8PtrTy(), AI, true); 583bdd1243dSDimitry Andric auto *Cmp = cast<ICmpInst>(B.CreateICmpNE(Guard, LI2)); 5840b57cec5SDimitry Andric auto SuccessProb = 5850b57cec5SDimitry Andric BranchProbabilityInfo::getBranchProbStackProtector(true); 5860b57cec5SDimitry Andric auto FailureProb = 5870b57cec5SDimitry Andric BranchProbabilityInfo::getBranchProbStackProtector(false); 5880b57cec5SDimitry Andric MDNode *Weights = MDBuilder(F->getContext()) 589bdd1243dSDimitry Andric .createBranchWeights(FailureProb.getNumerator(), 590bdd1243dSDimitry Andric SuccessProb.getNumerator()); 591bdd1243dSDimitry Andric 592bdd1243dSDimitry Andric SplitBlockAndInsertIfThen(Cmp, CheckLoc, 593bdd1243dSDimitry Andric /*Unreachable=*/false, Weights, 594bdd1243dSDimitry Andric DTU ? &*DTU : nullptr, 595bdd1243dSDimitry Andric /*LI=*/nullptr, /*ThenBlock=*/FailBB); 596bdd1243dSDimitry Andric 597bdd1243dSDimitry Andric auto *BI = cast<BranchInst>(Cmp->getParent()->getTerminator()); 598bdd1243dSDimitry Andric BasicBlock *NewBB = BI->getSuccessor(1); 599bdd1243dSDimitry Andric NewBB->setName("SP_return"); 600bdd1243dSDimitry Andric NewBB->moveAfter(&BB); 601bdd1243dSDimitry Andric 602bdd1243dSDimitry Andric Cmp->setPredicate(Cmp->getInversePredicate()); 603bdd1243dSDimitry Andric BI->swapSuccessors(); 6040b57cec5SDimitry Andric } 6050b57cec5SDimitry Andric } 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric // Return if we didn't modify any basic blocks. i.e., there are no return 6080b57cec5SDimitry Andric // statements in the function. 6090b57cec5SDimitry Andric return HasPrologue; 6100b57cec5SDimitry Andric } 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric /// CreateFailBB - Create a basic block to jump to when the stack protector 6130b57cec5SDimitry Andric /// check fails. 6140b57cec5SDimitry Andric BasicBlock *StackProtector::CreateFailBB() { 6150b57cec5SDimitry Andric LLVMContext &Context = F->getContext(); 6160b57cec5SDimitry Andric BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F); 6170b57cec5SDimitry Andric IRBuilder<> B(FailBB); 618e8d8bef9SDimitry Andric if (F->getSubprogram()) 619e8d8bef9SDimitry Andric B.SetCurrentDebugLocation( 620e8d8bef9SDimitry Andric DILocation::get(Context, 0, 0, F->getSubprogram())); 621*fe013be4SDimitry Andric FunctionCallee StackChkFail; 622*fe013be4SDimitry Andric SmallVector<Value *, 1> Args; 6230b57cec5SDimitry Andric if (Trip.isOSOpenBSD()) { 624*fe013be4SDimitry Andric StackChkFail = M->getOrInsertFunction("__stack_smash_handler", 625*fe013be4SDimitry Andric Type::getVoidTy(Context), 6260b57cec5SDimitry Andric Type::getInt8PtrTy(Context)); 627*fe013be4SDimitry Andric Args.push_back(B.CreateGlobalStringPtr(F->getName(), "SSH")); 6280b57cec5SDimitry Andric } else { 629*fe013be4SDimitry Andric StackChkFail = 6300b57cec5SDimitry Andric M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context)); 6310b57cec5SDimitry Andric } 632*fe013be4SDimitry Andric cast<Function>(StackChkFail.getCallee())->addFnAttr(Attribute::NoReturn); 633*fe013be4SDimitry Andric B.CreateCall(StackChkFail, Args); 6340b57cec5SDimitry Andric B.CreateUnreachable(); 6350b57cec5SDimitry Andric return FailBB; 6360b57cec5SDimitry Andric } 6370b57cec5SDimitry Andric 6380b57cec5SDimitry Andric bool StackProtector::shouldEmitSDCheck(const BasicBlock &BB) const { 6390b57cec5SDimitry Andric return HasPrologue && !HasIRCheck && isa<ReturnInst>(BB.getTerminator()); 6400b57cec5SDimitry Andric } 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andric void StackProtector::copyToMachineFrameInfo(MachineFrameInfo &MFI) const { 6430b57cec5SDimitry Andric if (Layout.empty()) 6440b57cec5SDimitry Andric return; 6450b57cec5SDimitry Andric 6460b57cec5SDimitry Andric for (int I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I) { 6470b57cec5SDimitry Andric if (MFI.isDeadObjectIndex(I)) 6480b57cec5SDimitry Andric continue; 6490b57cec5SDimitry Andric 6500b57cec5SDimitry Andric const AllocaInst *AI = MFI.getObjectAllocation(I); 6510b57cec5SDimitry Andric if (!AI) 6520b57cec5SDimitry Andric continue; 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric SSPLayoutMap::const_iterator LI = Layout.find(AI); 6550b57cec5SDimitry Andric if (LI == Layout.end()) 6560b57cec5SDimitry Andric continue; 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric MFI.setObjectSSPLayout(I, LI->second); 6590b57cec5SDimitry Andric } 6600b57cec5SDimitry Andric } 661