1 //===-- SafeStack.cpp - Safe Stack Insertion ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass splits the stack into the safe stack (kept as-is for LLVM backend)
11 // and the unsafe stack (explicitly allocated and managed through the runtime
14 // http://clang.llvm.org/docs/SafeStack.html
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Transforms/Instrumentation.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Analysis/ScalarEvolution.h"
22 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/DIBuilder.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/InstIterator.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/IR/Intrinsics.h"
33 #include "llvm/IR/IRBuilder.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/Pass.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/Format.h"
39 #include "llvm/Support/MathExtras.h"
40 #include "llvm/Support/raw_os_ostream.h"
41 #include "llvm/Target/TargetLowering.h"
42 #include "llvm/Target/TargetSubtargetInfo.h"
43 #include "llvm/Transforms/Utils/Local.h"
44 #include "llvm/Transforms/Utils/ModuleUtils.h"
48 #define DEBUG_TYPE "safestack"
52 STATISTIC(NumFunctions, "Total number of functions");
53 STATISTIC(NumUnsafeStackFunctions, "Number of functions with unsafe stack");
54 STATISTIC(NumUnsafeStackRestorePointsFunctions,
55 "Number of functions that use setjmp or exceptions");
57 STATISTIC(NumAllocas, "Total number of allocas");
58 STATISTIC(NumUnsafeStaticAllocas, "Number of unsafe static allocas");
59 STATISTIC(NumUnsafeDynamicAllocas, "Number of unsafe dynamic allocas");
60 STATISTIC(NumUnsafeStackRestorePoints, "Number of setjmps and landingpads");
66 /// Rewrite an SCEV expression for a memory access address to an expression that
67 /// represents offset from the given alloca.
69 /// The implementation simply replaces all mentions of the alloca with zero.
70 class AllocaOffsetRewriter : public SCEVRewriteVisitor<AllocaOffsetRewriter> {
74 AllocaOffsetRewriter(ScalarEvolution &SE, const AllocaInst *AI)
75 : SCEVRewriteVisitor(SE), AI(AI) {}
77 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
78 if (Expr->getValue() == AI)
79 return SE.getZero(Expr->getType());
84 /// The SafeStack pass splits the stack of each function into the safe
85 /// stack, which is only accessed through memory safe dereferences (as
86 /// determined statically), and the unsafe stack, which contains all
87 /// local variables that are accessed in ways that we can't prove to
89 class SafeStack : public FunctionPass {
90 const TargetMachine *TM;
91 const TargetLoweringBase *TL;
100 Value *UnsafeStackPtr = nullptr;
102 /// Unsafe stack alignment. Each stack frame must ensure that the stack is
103 /// aligned to this value. We need to re-align the unsafe stack if the
104 /// alignment of any object on the stack exceeds this value.
106 /// 16 seems like a reasonable upper bound on the alignment of objects that we
107 /// might expect to appear on the stack on most common targets.
108 enum { StackAlignment = 16 };
110 /// \brief Build a value representing a pointer to the unsafe stack pointer.
111 Value *getOrCreateUnsafeStackPtr(IRBuilder<> &IRB, Function &F);
113 /// \brief Find all static allocas, dynamic allocas, return instructions and
114 /// stack restore points (exception unwind blocks and setjmp calls) in the
115 /// given function and append them to the respective vectors.
116 void findInsts(Function &F, SmallVectorImpl<AllocaInst *> &StaticAllocas,
117 SmallVectorImpl<AllocaInst *> &DynamicAllocas,
118 SmallVectorImpl<ReturnInst *> &Returns,
119 SmallVectorImpl<Instruction *> &StackRestorePoints);
121 /// \brief Allocate space for all static allocas in \p StaticAllocas,
122 /// replace allocas with pointers into the unsafe stack and generate code to
123 /// restore the stack pointer before all return instructions in \p Returns.
125 /// \returns A pointer to the top of the unsafe stack after all unsafe static
126 /// allocas are allocated.
127 Value *moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F,
128 ArrayRef<AllocaInst *> StaticAllocas,
129 ArrayRef<ReturnInst *> Returns);
131 /// \brief Generate code to restore the stack after all stack restore points
132 /// in \p StackRestorePoints.
134 /// \returns A local variable in which to maintain the dynamic top of the
135 /// unsafe stack if needed.
137 createStackRestorePoints(IRBuilder<> &IRB, Function &F,
138 ArrayRef<Instruction *> StackRestorePoints,
139 Value *StaticTop, bool NeedDynamicTop);
141 /// \brief Replace all allocas in \p DynamicAllocas with code to allocate
142 /// space dynamically on the unsafe stack and store the dynamic unsafe stack
143 /// top to \p DynamicTop if non-null.
144 void moveDynamicAllocasToUnsafeStack(Function &F, Value *UnsafeStackPtr,
145 AllocaInst *DynamicTop,
146 ArrayRef<AllocaInst *> DynamicAllocas);
148 bool IsSafeStackAlloca(const AllocaInst *AI);
150 bool IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U,
151 const AllocaInst *AI);
152 bool IsAccessSafe(Value *Addr, uint64_t Size, const AllocaInst *AI);
155 static char ID; // Pass identification, replacement for typeid.
156 SafeStack(const TargetMachine *TM)
157 : FunctionPass(ID), TM(TM), TL(nullptr), DL(nullptr) {
158 initializeSafeStackPass(*PassRegistry::getPassRegistry());
160 SafeStack() : SafeStack(nullptr) {}
162 void getAnalysisUsage(AnalysisUsage &AU) const override {
163 AU.addRequired<ScalarEvolutionWrapperPass>();
166 bool doInitialization(Module &M) override {
167 DL = &M.getDataLayout();
169 StackPtrTy = Type::getInt8PtrTy(M.getContext());
170 IntPtrTy = DL->getIntPtrType(M.getContext());
171 Int32Ty = Type::getInt32Ty(M.getContext());
172 Int8Ty = Type::getInt8Ty(M.getContext());
177 bool runOnFunction(Function &F) override;
178 }; // class SafeStack
180 bool SafeStack::IsAccessSafe(Value *Addr, uint64_t Size, const AllocaInst *AI) {
181 AllocaOffsetRewriter Rewriter(*SE, AI);
182 const SCEV *Expr = Rewriter.visit(SE->getSCEV(Addr));
184 uint64_t BitWidth = SE->getTypeSizeInBits(Expr->getType());
185 ConstantRange AccessStartRange = SE->getUnsignedRange(Expr);
186 ConstantRange SizeRange =
187 ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, Size));
188 ConstantRange AccessRange = AccessStartRange.add(SizeRange);
189 ConstantRange AllocaRange = ConstantRange(
191 APInt(BitWidth, DL->getTypeStoreSize(AI->getAllocatedType())));
192 bool Safe = AllocaRange.contains(AccessRange);
194 DEBUG(dbgs() << "[SafeStack] Alloca " << *AI << "\n"
195 << " Access " << *Addr << "\n"
197 << " U: " << SE->getUnsignedRange(Expr)
198 << ", S: " << SE->getSignedRange(Expr) << "\n"
199 << " Range " << AccessRange << "\n"
200 << " AllocaRange " << AllocaRange << "\n"
201 << " " << (Safe ? "safe" : "unsafe") << "\n");
206 bool SafeStack::IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U,
207 const AllocaInst *AI) {
208 // All MemIntrinsics have destination address in Arg0 and size in Arg2.
209 if (MI->getRawDest() != U) return true;
210 const auto *Len = dyn_cast<ConstantInt>(MI->getLength());
211 // Non-constant size => unsafe. FIXME: try SCEV getRange.
212 if (!Len) return false;
213 return IsAccessSafe(U, Len->getZExtValue(), AI);
216 /// Check whether a given alloca instruction (AI) should be put on the safe
217 /// stack or not. The function analyzes all uses of AI and checks whether it is
218 /// only accessed in a memory safe way (as decided statically).
219 bool SafeStack::IsSafeStackAlloca(const AllocaInst *AI) {
220 // Go through all uses of this alloca and check whether all accesses to the
221 // allocated object are statically known to be memory safe and, hence, the
222 // object can be placed on the safe stack.
223 SmallPtrSet<const Value *, 16> Visited;
224 SmallVector<const Instruction *, 8> WorkList;
225 WorkList.push_back(AI);
227 // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc.
228 while (!WorkList.empty()) {
229 const Instruction *V = WorkList.pop_back_val();
230 for (const Use &UI : V->uses()) {
231 auto I = cast<const Instruction>(UI.getUser());
232 assert(V == UI.get());
234 switch (I->getOpcode()) {
235 case Instruction::Load: {
236 if (!IsAccessSafe(UI, DL->getTypeStoreSize(I->getType()), AI))
240 case Instruction::VAArg:
241 // "va-arg" from a pointer is safe.
243 case Instruction::Store: {
244 if (V == I->getOperand(0)) {
245 // Stored the pointer - conservatively assume it may be unsafe.
246 DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AI
247 << "\n store of address: " << *I << "\n");
252 UI, DL->getTypeStoreSize(I->getOperand(0)->getType()), AI))
256 case Instruction::Ret: {
261 case Instruction::Call:
262 case Instruction::Invoke: {
263 ImmutableCallSite CS(I);
265 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
266 if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
267 II->getIntrinsicID() == Intrinsic::lifetime_end)
271 if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
272 if (!IsMemIntrinsicSafe(MI, UI, AI)) {
273 DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AI
274 << "\n unsafe memintrinsic: " << *I
281 // LLVM 'nocapture' attribute is only set for arguments whose address
282 // is not stored, passed around, or used in any other non-trivial way.
283 // We assume that passing a pointer to an object as a 'nocapture
284 // readnone' argument is safe.
285 // FIXME: a more precise solution would require an interprocedural
286 // analysis here, which would look at all uses of an argument inside
287 // the function being called.
288 ImmutableCallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
289 for (ImmutableCallSite::arg_iterator A = B; A != E; ++A)
291 if (!(CS.doesNotCapture(A - B) &&
292 (CS.doesNotAccessMemory(A - B) || CS.doesNotAccessMemory()))) {
293 DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AI
294 << "\n unsafe call: " << *I << "\n");
301 if (Visited.insert(I).second)
302 WorkList.push_back(cast<const Instruction>(I));
307 // All uses of the alloca are safe, we can place it on the safe stack.
311 Value *SafeStack::getOrCreateUnsafeStackPtr(IRBuilder<> &IRB, Function &F) {
312 // Check if there is a target-specific location for the unsafe stack pointer.
314 if (Value *V = TL->getSafeStackPointerLocation(IRB))
317 // Otherwise, assume the target links with compiler-rt, which provides a
318 // thread-local variable with a magic name.
319 Module &M = *F.getParent();
320 const char *UnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
321 auto UnsafeStackPtr =
322 dyn_cast_or_null<GlobalVariable>(M.getNamedValue(UnsafeStackPtrVar));
324 if (!UnsafeStackPtr) {
325 // The global variable is not defined yet, define it ourselves.
326 // We use the initial-exec TLS model because we do not support the
327 // variable living anywhere other than in the main executable.
328 UnsafeStackPtr = new GlobalVariable(
329 M, StackPtrTy, false, GlobalValue::ExternalLinkage, nullptr,
330 UnsafeStackPtrVar, nullptr, GlobalValue::InitialExecTLSModel);
332 // The variable exists, check its type and attributes.
333 if (UnsafeStackPtr->getValueType() != StackPtrTy)
334 report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type");
335 if (!UnsafeStackPtr->isThreadLocal())
336 report_fatal_error(Twine(UnsafeStackPtrVar) + " must be thread-local");
338 return UnsafeStackPtr;
341 void SafeStack::findInsts(Function &F,
342 SmallVectorImpl<AllocaInst *> &StaticAllocas,
343 SmallVectorImpl<AllocaInst *> &DynamicAllocas,
344 SmallVectorImpl<ReturnInst *> &Returns,
345 SmallVectorImpl<Instruction *> &StackRestorePoints) {
346 for (Instruction &I : instructions(&F)) {
347 if (auto AI = dyn_cast<AllocaInst>(&I)) {
350 if (IsSafeStackAlloca(AI))
353 if (AI->isStaticAlloca()) {
354 ++NumUnsafeStaticAllocas;
355 StaticAllocas.push_back(AI);
357 ++NumUnsafeDynamicAllocas;
358 DynamicAllocas.push_back(AI);
360 } else if (auto RI = dyn_cast<ReturnInst>(&I)) {
361 Returns.push_back(RI);
362 } else if (auto CI = dyn_cast<CallInst>(&I)) {
363 // setjmps require stack restore.
364 if (CI->getCalledFunction() && CI->canReturnTwice())
365 StackRestorePoints.push_back(CI);
366 } else if (auto LP = dyn_cast<LandingPadInst>(&I)) {
367 // Exception landing pads require stack restore.
368 StackRestorePoints.push_back(LP);
369 } else if (auto II = dyn_cast<IntrinsicInst>(&I)) {
370 if (II->getIntrinsicID() == Intrinsic::gcroot)
371 llvm::report_fatal_error(
372 "gcroot intrinsic not compatible with safestack attribute");
378 SafeStack::createStackRestorePoints(IRBuilder<> &IRB, Function &F,
379 ArrayRef<Instruction *> StackRestorePoints,
380 Value *StaticTop, bool NeedDynamicTop) {
381 if (StackRestorePoints.empty())
384 // We need the current value of the shadow stack pointer to restore
385 // after longjmp or exception catching.
387 // FIXME: On some platforms this could be handled by the longjmp/exception
390 AllocaInst *DynamicTop = nullptr;
392 // If we also have dynamic alloca's, the stack pointer value changes
393 // throughout the function. For now we store it in an alloca.
394 DynamicTop = IRB.CreateAlloca(StackPtrTy, /*ArraySize=*/nullptr,
395 "unsafe_stack_dynamic_ptr");
398 // We need the original unsafe stack pointer value, even if there are
399 // no unsafe static allocas.
400 StaticTop = IRB.CreateLoad(UnsafeStackPtr, false, "unsafe_stack_ptr");
403 IRB.CreateStore(StaticTop, DynamicTop);
405 // Restore current stack pointer after longjmp/exception catch.
406 for (Instruction *I : StackRestorePoints) {
407 ++NumUnsafeStackRestorePoints;
409 IRB.SetInsertPoint(cast<Instruction>(I->getNextNode()));
410 Value *CurrentTop = DynamicTop ? IRB.CreateLoad(DynamicTop) : StaticTop;
411 IRB.CreateStore(CurrentTop, UnsafeStackPtr);
418 SafeStack::moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F,
419 ArrayRef<AllocaInst *> StaticAllocas,
420 ArrayRef<ReturnInst *> Returns) {
421 if (StaticAllocas.empty())
424 DIBuilder DIB(*F.getParent());
426 // We explicitly compute and set the unsafe stack layout for all unsafe
427 // static alloca instructions. We save the unsafe "base pointer" in the
428 // prologue into a local variable and restore it in the epilogue.
430 // Load the current stack pointer (we'll also use it as a base pointer).
431 // FIXME: use a dedicated register for it ?
432 Instruction *BasePointer =
433 IRB.CreateLoad(UnsafeStackPtr, false, "unsafe_stack_ptr");
434 assert(BasePointer->getType() == StackPtrTy);
436 for (ReturnInst *RI : Returns) {
437 IRB.SetInsertPoint(RI);
438 IRB.CreateStore(BasePointer, UnsafeStackPtr);
441 // Compute maximum alignment among static objects on the unsafe stack.
442 unsigned MaxAlignment = 0;
443 for (AllocaInst *AI : StaticAllocas) {
444 Type *Ty = AI->getAllocatedType();
446 std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment());
447 if (Align > MaxAlignment)
448 MaxAlignment = Align;
451 if (MaxAlignment > StackAlignment) {
452 // Re-align the base pointer according to the max requested alignment.
453 assert(isPowerOf2_32(MaxAlignment));
454 IRB.SetInsertPoint(cast<Instruction>(BasePointer->getNextNode()));
455 BasePointer = cast<Instruction>(IRB.CreateIntToPtr(
456 IRB.CreateAnd(IRB.CreatePtrToInt(BasePointer, IntPtrTy),
457 ConstantInt::get(IntPtrTy, ~uint64_t(MaxAlignment - 1))),
461 // Allocate space for every unsafe static AllocaInst on the unsafe stack.
462 int64_t StaticOffset = 0; // Current stack top.
463 for (AllocaInst *AI : StaticAllocas) {
464 IRB.SetInsertPoint(AI);
466 auto CArraySize = cast<ConstantInt>(AI->getArraySize());
467 Type *Ty = AI->getAllocatedType();
469 uint64_t Size = DL->getTypeAllocSize(Ty) * CArraySize->getZExtValue();
471 Size = 1; // Don't create zero-sized stack objects.
473 // Ensure the object is properly aligned.
475 std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment());
478 // NOTE: we ensure that BasePointer itself is aligned to >= Align.
479 StaticOffset += Size;
480 StaticOffset = RoundUpToAlignment(StaticOffset, Align);
482 Value *Off = IRB.CreateGEP(BasePointer, // BasePointer is i8*
483 ConstantInt::get(Int32Ty, -StaticOffset));
484 Value *NewAI = IRB.CreateBitCast(Off, AI->getType(), AI->getName());
485 if (AI->hasName() && isa<Instruction>(NewAI))
486 cast<Instruction>(NewAI)->takeName(AI);
488 // Replace alloc with the new location.
489 replaceDbgDeclareForAlloca(AI, BasePointer, DIB, /*Deref=*/true, -StaticOffset);
490 AI->replaceAllUsesWith(NewAI);
491 AI->eraseFromParent();
494 // Re-align BasePointer so that our callees would see it aligned as
496 // FIXME: no need to update BasePointer in leaf functions.
497 StaticOffset = RoundUpToAlignment(StaticOffset, StackAlignment);
499 // Update shadow stack pointer in the function epilogue.
500 IRB.SetInsertPoint(cast<Instruction>(BasePointer->getNextNode()));
503 IRB.CreateGEP(BasePointer, ConstantInt::get(Int32Ty, -StaticOffset),
504 "unsafe_stack_static_top");
505 IRB.CreateStore(StaticTop, UnsafeStackPtr);
509 void SafeStack::moveDynamicAllocasToUnsafeStack(
510 Function &F, Value *UnsafeStackPtr, AllocaInst *DynamicTop,
511 ArrayRef<AllocaInst *> DynamicAllocas) {
512 DIBuilder DIB(*F.getParent());
514 for (AllocaInst *AI : DynamicAllocas) {
517 // Compute the new SP value (after AI).
518 Value *ArraySize = AI->getArraySize();
519 if (ArraySize->getType() != IntPtrTy)
520 ArraySize = IRB.CreateIntCast(ArraySize, IntPtrTy, false);
522 Type *Ty = AI->getAllocatedType();
523 uint64_t TySize = DL->getTypeAllocSize(Ty);
524 Value *Size = IRB.CreateMul(ArraySize, ConstantInt::get(IntPtrTy, TySize));
526 Value *SP = IRB.CreatePtrToInt(IRB.CreateLoad(UnsafeStackPtr), IntPtrTy);
527 SP = IRB.CreateSub(SP, Size);
529 // Align the SP value to satisfy the AllocaInst, type and stack alignments.
530 unsigned Align = std::max(
531 std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment()),
532 (unsigned)StackAlignment);
534 assert(isPowerOf2_32(Align));
535 Value *NewTop = IRB.CreateIntToPtr(
536 IRB.CreateAnd(SP, ConstantInt::get(IntPtrTy, ~uint64_t(Align - 1))),
539 // Save the stack pointer.
540 IRB.CreateStore(NewTop, UnsafeStackPtr);
542 IRB.CreateStore(NewTop, DynamicTop);
544 Value *NewAI = IRB.CreateIntToPtr(SP, AI->getType());
545 if (AI->hasName() && isa<Instruction>(NewAI))
548 replaceDbgDeclareForAlloca(AI, NewAI, DIB, /*Deref=*/true);
549 AI->replaceAllUsesWith(NewAI);
550 AI->eraseFromParent();
553 if (!DynamicAllocas.empty()) {
554 // Now go through the instructions again, replacing stacksave/stackrestore.
555 for (inst_iterator It = inst_begin(&F), Ie = inst_end(&F); It != Ie;) {
556 Instruction *I = &*(It++);
557 auto II = dyn_cast<IntrinsicInst>(I);
561 if (II->getIntrinsicID() == Intrinsic::stacksave) {
563 Instruction *LI = IRB.CreateLoad(UnsafeStackPtr);
565 II->replaceAllUsesWith(LI);
566 II->eraseFromParent();
567 } else if (II->getIntrinsicID() == Intrinsic::stackrestore) {
569 Instruction *SI = IRB.CreateStore(II->getArgOperand(0), UnsafeStackPtr);
571 assert(II->use_empty());
572 II->eraseFromParent();
578 bool SafeStack::runOnFunction(Function &F) {
579 DEBUG(dbgs() << "[SafeStack] Function: " << F.getName() << "\n");
581 if (!F.hasFnAttribute(Attribute::SafeStack)) {
582 DEBUG(dbgs() << "[SafeStack] safestack is not requested"
583 " for this function\n");
587 if (F.isDeclaration()) {
588 DEBUG(dbgs() << "[SafeStack] function definition"
589 " is not available\n");
593 TL = TM ? TM->getSubtargetImpl(F)->getTargetLowering() : nullptr;
594 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
597 // Make sure the regular stack protector won't run on this function
598 // (safestack attribute takes precedence).
600 B.addAttribute(Attribute::StackProtect)
601 .addAttribute(Attribute::StackProtectReq)
602 .addAttribute(Attribute::StackProtectStrong);
604 AttributeSet::FunctionIndex,
605 AttributeSet::get(F.getContext(), AttributeSet::FunctionIndex, B));
610 SmallVector<AllocaInst *, 16> StaticAllocas;
611 SmallVector<AllocaInst *, 4> DynamicAllocas;
612 SmallVector<ReturnInst *, 4> Returns;
614 // Collect all points where stack gets unwound and needs to be restored
615 // This is only necessary because the runtime (setjmp and unwind code) is
616 // not aware of the unsafe stack and won't unwind/restore it prorerly.
617 // To work around this problem without changing the runtime, we insert
618 // instrumentation to restore the unsafe stack pointer when necessary.
619 SmallVector<Instruction *, 4> StackRestorePoints;
621 // Find all static and dynamic alloca instructions that must be moved to the
622 // unsafe stack, all return instructions and stack restore points.
623 findInsts(F, StaticAllocas, DynamicAllocas, Returns, StackRestorePoints);
625 if (StaticAllocas.empty() && DynamicAllocas.empty() &&
626 StackRestorePoints.empty())
627 return false; // Nothing to do in this function.
629 if (!StaticAllocas.empty() || !DynamicAllocas.empty())
630 ++NumUnsafeStackFunctions; // This function has the unsafe stack.
632 if (!StackRestorePoints.empty())
633 ++NumUnsafeStackRestorePointsFunctions;
635 IRBuilder<> IRB(&F.front(), F.begin()->getFirstInsertionPt());
636 UnsafeStackPtr = getOrCreateUnsafeStackPtr(IRB, F);
638 // The top of the unsafe stack after all unsafe static allocas are allocated.
639 Value *StaticTop = moveStaticAllocasToUnsafeStack(IRB, F, StaticAllocas, Returns);
641 // Safe stack object that stores the current unsafe stack top. It is updated
642 // as unsafe dynamic (non-constant-sized) allocas are allocated and freed.
643 // This is only needed if we need to restore stack pointer after longjmp
644 // or exceptions, and we have dynamic allocations.
645 // FIXME: a better alternative might be to store the unsafe stack pointer
646 // before setjmp / invoke instructions.
647 AllocaInst *DynamicTop = createStackRestorePoints(
648 IRB, F, StackRestorePoints, StaticTop, !DynamicAllocas.empty());
650 // Handle dynamic allocas.
651 moveDynamicAllocasToUnsafeStack(F, UnsafeStackPtr, DynamicTop,
654 DEBUG(dbgs() << "[SafeStack] safestack applied\n");
658 } // anonymous namespace
660 char SafeStack::ID = 0;
661 INITIALIZE_TM_PASS_BEGIN(SafeStack, "safe-stack",
662 "Safe Stack instrumentation pass", false, false)
663 INITIALIZE_TM_PASS_END(SafeStack, "safe-stack",
664 "Safe Stack instrumentation pass", false, false)
666 FunctionPass *llvm::createSafeStackPass(const llvm::TargetMachine *TM) {
667 return new SafeStack(TM);