Lines Matching +full:enable +full:- +full:ssc

1 //===- SafeStack.cpp - Safe Stack Insertion -------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass splits the stack into the safe stack (kept as-is for LLVM backend)
15 //===----------------------------------------------------------------------===//
78 #define DEBUG_TYPE "safe-stack"
98 SafeStackUsePointerAddress("safestack-use-pointer-address",
101 static cl::opt<bool> ClColoring("safe-stack-coloring",
102 cl::desc("enable safe stack coloring"),
126 /// aligned to this value. We need to re-align the unsafe stack if the
176 /// top to \p DynamicTop if non-null.
207 uint64_t Size = DL.getTypeAllocSize(AI->getAllocatedType());
208 if (AI->isArrayAllocation()) {
209 auto C = dyn_cast<ConstantInt>(AI->getArraySize());
212 Size *= C->getZExtValue();
221 if (!Base || Base->getValue() != AllocaPtr) {
231 uint64_t BitWidth = SE.getTypeSizeInBits(Expr->getType());
259 if (MTI->getRawSource() != U && MTI->getRawDest() != U)
262 if (MI->getRawDest() != U)
266 const auto *Len = dyn_cast<ConstantInt>(MI->getLength());
267 // Non-constant size => unsafe. FIXME: try SCEV getRange.
269 return IsAccessSafe(U, Len->getZExtValue(), AllocaPtr, AllocaSize);
286 for (const Use &UI : V->uses()) {
290 switch (I->getOpcode()) {
292 if (!IsAccessSafe(UI, DL.getTypeStoreSize(I->getType()), AllocaPtr,
298 // "va-arg" from a pointer is safe.
301 if (V == I->getOperand(0)) {
302 // Stored the pointer - conservatively assume it may be unsafe.
309 if (!IsAccessSafe(UI, DL.getTypeStoreSize(I->getOperand(0)->getType()),
322 if (I->isLifetimeStartOrEnd())
336 // is not stored, passed around, or used in any other non-trivial way.
344 if (A->get() == V)
345 if (!(CS.doesNotCapture(A - B) && (CS.doesNotAccessMemory(A - B) ||
391 if (AI->isStaticAlloca()) {
399 if (CallInst *CI = I.getParent()->getTerminatingMustTailCall())
405 if (CI->getCalledFunction() && CI->canReturnTwice())
411 if (II->getIntrinsicID() == Intrinsic::gcroot)
456 IRB.SetInsertPoint(I->getNextNode());
478 // FIXME: respect -fsanitize-trap / -ftrap-function here?
480 F.getParent()->getOrInsertFunction("__stack_chk_fail", IRB.getVoidTy());
496 StackLifetime SSC(F, StaticAllocas, StackLifetime::LivenessType::May);
499 SSC.run();
501 for (const auto *I : SSC.getMarkers()) {
502 auto *Op = dyn_cast<Instruction>(I->getOperand(1));
503 const_cast<IntrinsicInst *>(I)->eraseFromParent();
505 if (Op && Op->use_empty())
506 Op->eraseFromParent();
512 Type *Ty = StackGuardSlot->getAllocatedType();
513 Align Align = std::max(DL.getPrefTypeAlign(Ty), StackGuardSlot->getAlign());
515 Align, SSC.getFullLiveRange());
519 Type *Ty = Arg->getParamByValType();
522 Size = 1; // Don't create zero-sized stack objects.
526 if (auto A = Arg->getParamAlign())
528 SSL.addObject(Arg, Size, Align, SSC.getFullLiveRange());
532 Type *Ty = AI->getAllocatedType();
535 Size = 1; // Don't create zero-sized stack objects.
538 Align Align = std::max(DL.getPrefTypeAlign(Ty), AI->getAlign());
541 ClColoring ? SSC.getLiveRange(AI) : NoColoringRange);
547 // FIXME: tell SSL that we start at a less-then-MaxAlignment aligned location
550 // Re-align the base pointer according to the max requested alignment.
551 IRB.SetInsertPoint(BasePointer->getNextNode());
555 ConstantInt::get(IntPtrTy, ~(FrameAlignment.value() - 1))),
559 IRB.SetInsertPoint(BasePointer->getNextNode());
564 IRB.CreatePtrAdd(BasePointer, ConstantInt::get(Int32Ty, -Offset));
566 IRB.CreateBitCast(Off, StackGuardSlot->getType(), "StackGuardSlot");
569 StackGuardSlot->replaceAllUsesWith(NewAI);
570 StackGuardSlot->eraseFromParent();
576 Type *Ty = Arg->getParamByValType();
580 Size = 1; // Don't create zero-sized stack objects.
583 IRB.CreatePtrAdd(BasePointer, ConstantInt::get(Int32Ty, -Offset));
584 Value *NewArg = IRB.CreateBitCast(Off, Arg->getType(),
585 Arg->getName() + ".unsafe-byval");
589 -Offset);
590 Arg->replaceAllUsesWith(NewArg);
591 IRB.SetInsertPoint(cast<Instruction>(NewArg)->getNextNode());
592 IRB.CreateMemCpy(Off, Align, Arg, Arg->getParamAlign(), Size);
600 replaceDbgDeclare(AI, BasePointer, DIB, DIExpression::ApplyOffset, -Offset);
601 replaceDbgValueForAlloca(AI, BasePointer, DIB, -Offset);
605 std::string Name = std::string(AI->getName()) + ".unsafe";
606 while (!AI->use_empty()) {
607 Use &U = *AI->use_begin();
612 InsertBefore = PHI->getIncomingBlock(U)->getTerminator();
618 IRBUser.CreatePtrAdd(BasePointer, ConstantInt::get(Int32Ty, -Offset));
619 Value *Replacement = IRBUser.CreateBitCast(Off, AI->getType(), Name);
624 PHI->setIncomingValueForBlock(PHI->getIncomingBlock(U), Replacement);
629 AI->eraseFromParent();
632 // Re-align BasePointer so that our callees would see it aligned as
639 Data.push_back(MDB.createString("unsafe-stack-size"));
645 IRB.SetInsertPoint(BasePointer->getNextNode());
648 IRB.CreatePtrAdd(BasePointer, ConstantInt::get(Int32Ty, -FrameSize),
663 Value *ArraySize = AI->getArraySize();
664 if (ArraySize->getType() != IntPtrTy)
667 Type *Ty = AI->getAllocatedType();
676 auto Align = std::max(std::max(DL.getPrefTypeAlign(Ty), AI->getAlign()),
681 ConstantInt::get(IntPtrTy, ~uint64_t(Align.value() - 1))),
689 Value *NewAI = IRB.CreatePointerCast(NewTop, AI->getType());
690 if (AI->hasName() && isa<Instruction>(NewAI))
691 NewAI->takeName(AI);
694 AI->replaceAllUsesWith(NewAI);
695 AI->eraseFromParent();
705 if (II->getIntrinsicID() == Intrinsic::stacksave) {
708 LI->takeName(II);
709 II->replaceAllUsesWith(LI);
710 II->eraseFromParent();
711 } else if (II->getIntrinsicID() == Intrinsic::stackrestore) {
713 Instruction *SI = IRB.CreateStore(II->getArgOperand(0), UnsafeStackPtr);
714 SI->takeName(II);
715 assert(II->use_empty());
716 II->eraseFromParent();
727 if (Callee->isInterposable() || Callee->hasFnAttribute(Attribute::NoInline) ||
741 Function *Callee = CI->getCalledFunction();
742 if (!Callee || Callee->isDeclaration())
787 IRBuilder<> IRB(&F.front(), F.begin()->getFirstInsertionPt());
792 DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP));
794 FunctionCallee Fn = F.getParent()->getOrInsertFunction(
805 assert(BasePointer->getType() == StackPtrTy);
828 // as unsafe dynamic (non-constant-sized) allocas are allocated and freed.
885 auto *TL = TM->getSubtargetImpl(F)->getTargetLowering();
905 DT = &DTWP->getDomTree();
945 auto *TL = TM->getSubtargetImpl(F)->getTargetLowering();