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