Lines Matching +full:fixed +full:- +full:layout

1 //===- StackProtector.cpp - Stack Protector Insertion ---------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
56 #define DEBUG_TYPE "stack-protector"
62 static cl::opt<bool> EnableSelectionDAGSP("enable-selectiondag-sp",
64 static cl::opt<bool> DisableCheckNoReturn("disable-check-noreturn-call",
67 /// InsertStackProtectors - Insert code into the prologue and epilogue of the
70 /// - The prologue code loads and stores the stack guard onto the stack.
71 /// - The epilogue checks the value stored in the prologue against the original
77 /// CreateFailBB - Create a basic block to jump to when the stack protector
86 if (Layout.empty())
97 SSPLayoutMap::const_iterator LI = Layout.find(AI);
98 if (LI == Layout.end())
101 MFI.setObjectSSPLayout(I, LI->second);
110 SSPLayoutAnalysis::requiresStackProtector(&F, &Info.Layout);
112 "stack-protector-buffer-size", SSPLayoutInfo::DefaultSSPBufferSize);
128 // Do nothing if this is funclet-based personality.
139 assert((!DT || DT->verify(DominatorTree::VerificationLevel::Full)) &&
173 M = F->getParent();
175 DTU.emplace(DTWP->getDomTree(), DomTreeUpdater::UpdateStrategy::Lazy);
181 "stack-protector-buffer-size", SSPLayoutInfo::DefaultSSPBufferSize);
182 if (!requiresStackProtector(F, &LayoutInfo.Layout))
186 // Do nothing if this is funclet-based personality.
199 DTU->getDomTree().verify(DominatorTree::VerificationLevel::Full)) &&
207 /// it is "large" ( >= ssp-buffer-size). In the case of a structure with
215 if (!AT->getElementType()->isIntegerTy(8)) {
216 // If we're on a non-Darwin platform or we're inside of a structure, don't
220 if (!Strong && (InStruct || !Triple(M->getTargetTriple()).isOSDarwin()))
226 if (SSPBufferSize <= M->getDataLayout().getTypeAllocSize(AT)) {
241 for (Type *ET : ST->elements())
258 const DataLayout &DL = M->getDataLayout();
259 for (const User *U : AI->users()) {
264 if (MemLoc && MemLoc->Size.hasValue() &&
265 !TypeSize::isKnownGE(AllocSize, MemLoc->Size.getValue()))
267 switch (I->getOpcode()) {
269 if (AI == cast<StoreInst>(I)->getValueOperand())
275 if (AI == cast<AtomicCmpXchgInst>(I)->getNewValOperand())
279 if (AI == cast<PtrToIntInst>(I)->getOperand(0))
284 // TODO: Narrow this to intrinsics that have store-like effects.
286 if (!CI->isDebugOrPseudoInst() && !CI->isLifetimeStartOrEnd())
293 // If the GEP offset is out-of-bounds, or is non-constant and so has to be
294 // assumed to be potentially out-of-bounds, then any memory access that
295 // would use it could also be out-of-bounds meaning stack protection is
298 unsigned IndexSize = DL.getIndexTypeSizeInBits(I->getType());
300 if (!GEP->accumulateConstantOffset(DL, Offset))
306 // We can't subtract a fixed size from a scalable one, so in that case
309 TypeSize::getFixed(AllocSize.getKnownMinValue()) - OffsetSize;
332 // These instructions take an address operand, but have load-like or
353 if (II->getIntrinsicID() == Intrinsic::stackprotector)
369 /// size, and functions that contain stack-based variables that have had their
372 SSPLayoutMap *Layout) {
373 Module *M = F->getParent();
382 unsigned SSPBufferSize = F->getFnAttributeAsParsedInteger(
383 "stack-protector-buffer-size", SSPLayoutInfo::DefaultSSPBufferSize);
385 if (F->hasFnAttribute(Attribute::SafeStack))
393 if (F->hasFnAttribute(Attribute::StackProtectReq)) {
394 if (!Layout)
400 << " due to a function attribute or command-line switch";
404 } else if (F->hasFnAttribute(Attribute::StackProtectStrong))
406 else if (!F->hasFnAttribute(Attribute::StackProtect))
412 if (AI->isArrayAllocation()) {
421 if (const auto *CI = dyn_cast<ConstantInt>(AI->getArraySize())) {
422 if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) {
425 if (!Layout)
427 Layout->insert(
433 if (!Layout)
435 Layout->insert(
442 if (!Layout)
444 Layout->insert(
453 if (ContainsProtectableArray(AI->getAllocatedType(), M, SSPBufferSize,
455 if (!Layout)
457 Layout->insert(std::make_pair(
473 AI, M->getDataLayout().getTypeAllocSize(AI->getAllocatedType()),
476 if (!Layout)
478 Layout->insert(std::make_pair(AI, MachineFrameInfo::SSPLK_AddrOf));
503 Value *Guard = TLI->getIRStackGuard(B);
504 StringRef GuardMode = M->getStackProtectorGuard();
512 // defined as !TLI->getIRStackGuard(B), where getIRStackGuard is also
521 TLI->insertSSPDeclarations(*M);
538 IRBuilder<> B(&F->getEntryBlock().front());
539 PointerType *PtrTy = PointerType::getUnqual(CheckLoc->getContext());
551 auto *M = F->getParent();
552 auto *TLI = TM->getSubtargetImpl(*F)->getTargetLowering();
558 TLI->useStackGuardXorFP() ||
559 (EnableSelectionDAGSP && !TM->Options.EnableFastISel);
573 if (CB->doesNotReturn() && !CB->doesNotThrow()) {
597 AI = cast<AllocaInst>(SPCall->getArgOperand(1));
610 Instruction *Prev = CheckLoc->getPrevNonDebugInstruction();
611 if (Prev && isa<CallInst>(Prev) && cast<CallInst>(Prev)->isTailCall())
614 Prev = Prev->getPrevNonDebugInstruction();
615 if (Prev && isa<CallInst>(Prev) && cast<CallInst>(Prev)->isTailCall())
620 // function-based or inlined depending on which mechanism the target is
622 if (Function *GuardCheck = TLI->getSSPStackGuardCheck(*M)) {
623 // Generate the function-based epilogue instrumentation.
628 Call->setAttributes(GuardCheck->getAttributes());
629 Call->setCallingConv(GuardCheck->getCallingConv());
661 FailBB = CreateFailBB(F, TM->getTargetTriple());
671 MDNode *Weights = MDBuilder(F->getContext())
679 auto *BI = cast<BranchInst>(Cmp->getParent()->getTerminator());
680 BasicBlock *NewBB = BI->getSuccessor(1);
681 NewBB->setName("SP_return");
682 NewBB->moveAfter(&BB);
684 Cmp->setPredicate(Cmp->getInversePredicate());
685 BI->swapSuccessors();
695 auto *M = F->getParent();
696 LLVMContext &Context = F->getContext();
699 if (F->getSubprogram())
701 DILocation::get(Context, 0, 0, F->getSubprogram()));
705 StackChkFail = M->getOrInsertFunction("__stack_smash_handler",
708 Args.push_back(B.CreateGlobalStringPtr(F->getName(), "SSH"));
711 M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
713 cast<Function>(StackChkFail.getCallee())->addFnAttr(Attribute::NoReturn);