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/AliasAnalysis.h"
22 #include "llvm/Analysis/TargetTransformInfo.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/DIBuilder.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/InstIterator.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/IntrinsicInst.h"
31 #include "llvm/IR/Intrinsics.h"
32 #include "llvm/IR/IRBuilder.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/Pass.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Format.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Support/raw_os_ostream.h"
40 #include "llvm/Transforms/Utils/Local.h"
41 #include "llvm/Transforms/Utils/ModuleUtils.h"
45 #define DEBUG_TYPE "safestack"
49 STATISTIC(NumFunctions, "Total number of functions");
50 STATISTIC(NumUnsafeStackFunctions, "Number of functions with unsafe stack");
51 STATISTIC(NumUnsafeStackRestorePointsFunctions,
52 "Number of functions that use setjmp or exceptions");
54 STATISTIC(NumAllocas, "Total number of allocas");
55 STATISTIC(NumUnsafeStaticAllocas, "Number of unsafe static allocas");
56 STATISTIC(NumUnsafeDynamicAllocas, "Number of unsafe dynamic allocas");
57 STATISTIC(NumUnsafeStackRestorePoints, "Number of setjmps and landingpads");
63 /// Check whether a given alloca instruction (AI) should be put on the safe
64 /// stack or not. The function analyzes all uses of AI and checks whether it is
65 /// only accessed in a memory safe way (as decided statically).
66 bool IsSafeStackAlloca(const AllocaInst *AI) {
67 // Go through all uses of this alloca and check whether all accesses to the
68 // allocated object are statically known to be memory safe and, hence, the
69 // object can be placed on the safe stack.
71 SmallPtrSet<const Value *, 16> Visited;
72 SmallVector<const Instruction *, 8> WorkList;
73 WorkList.push_back(AI);
75 // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc.
76 while (!WorkList.empty()) {
77 const Instruction *V = WorkList.pop_back_val();
78 for (const Use &UI : V->uses()) {
79 auto I = cast<const Instruction>(UI.getUser());
80 assert(V == UI.get());
82 switch (I->getOpcode()) {
83 case Instruction::Load:
84 // Loading from a pointer is safe.
86 case Instruction::VAArg:
87 // "va-arg" from a pointer is safe.
89 case Instruction::Store:
90 if (V == I->getOperand(0))
91 // Stored the pointer - conservatively assume it may be unsafe.
93 // Storing to the pointee is safe.
96 case Instruction::GetElementPtr:
97 if (!cast<const GetElementPtrInst>(I)->hasAllConstantIndices())
98 // GEP with non-constant indices can lead to memory errors.
99 // This also applies to inbounds GEPs, as the inbounds attribute
100 // represents an assumption that the address is in bounds, rather than
101 // an assertion that it is.
104 // We assume that GEP on static alloca with constant indices is safe,
105 // otherwise a compiler would detect it and warn during compilation.
107 if (!isa<const ConstantInt>(AI->getArraySize()))
108 // However, if the array size itself is not constant, the access
109 // might still be unsafe at runtime.
114 case Instruction::BitCast:
115 case Instruction::IntToPtr:
116 case Instruction::PHI:
117 case Instruction::PtrToInt:
118 case Instruction::Select:
119 // The object can be safe or not, depending on how the result of the
120 // instruction is used.
121 if (Visited.insert(I).second)
122 WorkList.push_back(cast<const Instruction>(I));
125 case Instruction::Call:
126 case Instruction::Invoke: {
127 // FIXME: add support for memset and memcpy intrinsics.
128 ImmutableCallSite CS(I);
130 // LLVM 'nocapture' attribute is only set for arguments whose address
131 // is not stored, passed around, or used in any other non-trivial way.
132 // We assume that passing a pointer to an object as a 'nocapture'
134 // FIXME: a more precise solution would require an interprocedural
135 // analysis here, which would look at all uses of an argument inside
136 // the function being called.
137 ImmutableCallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
138 for (ImmutableCallSite::arg_iterator A = B; A != E; ++A)
139 if (A->get() == V && !CS.doesNotCapture(A - B))
140 // The parameter is not marked 'nocapture' - unsafe.
146 // The object is unsafe if it is used in any other way.
152 // All uses of the alloca are safe, we can place it on the safe stack.
156 /// The SafeStack pass splits the stack of each function into the
157 /// safe stack, which is only accessed through memory safe dereferences
158 /// (as determined statically), and the unsafe stack, which contains all
159 /// local variables that are accessed in unsafe ways.
160 class SafeStack : public FunctionPass {
161 const DataLayout *DL;
168 Constant *UnsafeStackPtr;
170 /// Unsafe stack alignment. Each stack frame must ensure that the stack is
171 /// aligned to this value. We need to re-align the unsafe stack if the
172 /// alignment of any object on the stack exceeds this value.
174 /// 16 seems like a reasonable upper bound on the alignment of objects that we
175 /// might expect to appear on the stack on most common targets.
176 enum { StackAlignment = 16 };
178 /// \brief Build a constant representing a pointer to the unsafe stack
180 Constant *getOrCreateUnsafeStackPtr(Module &M);
182 /// \brief Find all static allocas, dynamic allocas, return instructions and
183 /// stack restore points (exception unwind blocks and setjmp calls) in the
184 /// given function and append them to the respective vectors.
185 void findInsts(Function &F, SmallVectorImpl<AllocaInst *> &StaticAllocas,
186 SmallVectorImpl<AllocaInst *> &DynamicAllocas,
187 SmallVectorImpl<ReturnInst *> &Returns,
188 SmallVectorImpl<Instruction *> &StackRestorePoints);
190 /// \brief Allocate space for all static allocas in \p StaticAllocas,
191 /// replace allocas with pointers into the unsafe stack and generate code to
192 /// restore the stack pointer before all return instructions in \p Returns.
194 /// \returns A pointer to the top of the unsafe stack after all unsafe static
195 /// allocas are allocated.
196 Value *moveStaticAllocasToUnsafeStack(Function &F,
197 ArrayRef<AllocaInst *> StaticAllocas,
198 ArrayRef<ReturnInst *> Returns);
200 /// \brief Generate code to restore the stack after all stack restore points
201 /// in \p StackRestorePoints.
203 /// \returns A local variable in which to maintain the dynamic top of the
204 /// unsafe stack if needed.
206 createStackRestorePoints(Function &F,
207 ArrayRef<Instruction *> StackRestorePoints,
208 Value *StaticTop, bool NeedDynamicTop);
210 /// \brief Replace all allocas in \p DynamicAllocas with code to allocate
211 /// space dynamically on the unsafe stack and store the dynamic unsafe stack
212 /// top to \p DynamicTop if non-null.
213 void moveDynamicAllocasToUnsafeStack(Function &F, Value *UnsafeStackPtr,
214 AllocaInst *DynamicTop,
215 ArrayRef<AllocaInst *> DynamicAllocas);
218 static char ID; // Pass identification, replacement for typeid.
219 SafeStack() : FunctionPass(ID), DL(nullptr) {
220 initializeSafeStackPass(*PassRegistry::getPassRegistry());
223 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
224 AU.addRequired<AliasAnalysis>();
227 virtual bool doInitialization(Module &M) {
228 DL = &M.getDataLayout();
230 StackPtrTy = Type::getInt8PtrTy(M.getContext());
231 IntPtrTy = DL->getIntPtrType(M.getContext());
232 Int32Ty = Type::getInt32Ty(M.getContext());
233 Int8Ty = Type::getInt8Ty(M.getContext());
235 UnsafeStackPtr = getOrCreateUnsafeStackPtr(M);
240 bool runOnFunction(Function &F);
242 }; // class SafeStack
244 Constant *SafeStack::getOrCreateUnsafeStackPtr(Module &M) {
245 // The unsafe stack pointer is stored in a global variable with a magic name.
246 const char *kUnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
248 auto UnsafeStackPtr =
249 dyn_cast_or_null<GlobalVariable>(M.getNamedValue(kUnsafeStackPtrVar));
251 if (!UnsafeStackPtr) {
252 // The global variable is not defined yet, define it ourselves.
253 // We use the initial-exec TLS model because we do not support the variable
254 // living anywhere other than in the main executable.
255 UnsafeStackPtr = new GlobalVariable(
256 /*Module=*/M, /*Type=*/StackPtrTy,
257 /*isConstant=*/false, /*Linkage=*/GlobalValue::ExternalLinkage,
258 /*Initializer=*/0, /*Name=*/kUnsafeStackPtrVar,
259 /*InsertBefore=*/nullptr,
260 /*ThreadLocalMode=*/GlobalValue::InitialExecTLSModel);
262 // The variable exists, check its type and attributes.
263 if (UnsafeStackPtr->getValueType() != StackPtrTy) {
264 report_fatal_error(Twine(kUnsafeStackPtrVar) + " must have void* type");
267 if (!UnsafeStackPtr->isThreadLocal()) {
268 report_fatal_error(Twine(kUnsafeStackPtrVar) + " must be thread-local");
272 return UnsafeStackPtr;
275 void SafeStack::findInsts(Function &F,
276 SmallVectorImpl<AllocaInst *> &StaticAllocas,
277 SmallVectorImpl<AllocaInst *> &DynamicAllocas,
278 SmallVectorImpl<ReturnInst *> &Returns,
279 SmallVectorImpl<Instruction *> &StackRestorePoints) {
280 for (Instruction &I : inst_range(&F)) {
281 if (auto AI = dyn_cast<AllocaInst>(&I)) {
284 if (IsSafeStackAlloca(AI))
287 if (AI->isStaticAlloca()) {
288 ++NumUnsafeStaticAllocas;
289 StaticAllocas.push_back(AI);
291 ++NumUnsafeDynamicAllocas;
292 DynamicAllocas.push_back(AI);
294 } else if (auto RI = dyn_cast<ReturnInst>(&I)) {
295 Returns.push_back(RI);
296 } else if (auto CI = dyn_cast<CallInst>(&I)) {
297 // setjmps require stack restore.
298 if (CI->getCalledFunction() && CI->canReturnTwice())
299 StackRestorePoints.push_back(CI);
300 } else if (auto LP = dyn_cast<LandingPadInst>(&I)) {
301 // Exception landing pads require stack restore.
302 StackRestorePoints.push_back(LP);
303 } else if (auto II = dyn_cast<IntrinsicInst>(&I)) {
304 if (II->getIntrinsicID() == Intrinsic::gcroot)
305 llvm::report_fatal_error(
306 "gcroot intrinsic not compatible with safestack attribute");
312 SafeStack::createStackRestorePoints(Function &F,
313 ArrayRef<Instruction *> StackRestorePoints,
314 Value *StaticTop, bool NeedDynamicTop) {
315 if (StackRestorePoints.empty())
318 IRBuilder<> IRB(StaticTop
319 ? cast<Instruction>(StaticTop)->getNextNode()
320 : (Instruction *)F.getEntryBlock().getFirstInsertionPt());
322 // We need the current value of the shadow stack pointer to restore
323 // after longjmp or exception catching.
325 // FIXME: On some platforms this could be handled by the longjmp/exception
328 AllocaInst *DynamicTop = nullptr;
330 // If we also have dynamic alloca's, the stack pointer value changes
331 // throughout the function. For now we store it in an alloca.
332 DynamicTop = IRB.CreateAlloca(StackPtrTy, /*ArraySize=*/nullptr,
333 "unsafe_stack_dynamic_ptr");
336 // We need the original unsafe stack pointer value, even if there are
337 // no unsafe static allocas.
338 StaticTop = IRB.CreateLoad(UnsafeStackPtr, false, "unsafe_stack_ptr");
341 IRB.CreateStore(StaticTop, DynamicTop);
343 // Restore current stack pointer after longjmp/exception catch.
344 for (Instruction *I : StackRestorePoints) {
345 ++NumUnsafeStackRestorePoints;
347 IRB.SetInsertPoint(cast<Instruction>(I->getNextNode()));
348 Value *CurrentTop = DynamicTop ? IRB.CreateLoad(DynamicTop) : StaticTop;
349 IRB.CreateStore(CurrentTop, UnsafeStackPtr);
356 SafeStack::moveStaticAllocasToUnsafeStack(Function &F,
357 ArrayRef<AllocaInst *> StaticAllocas,
358 ArrayRef<ReturnInst *> Returns) {
359 if (StaticAllocas.empty())
362 IRBuilder<> IRB(F.getEntryBlock().getFirstInsertionPt());
363 DIBuilder DIB(*F.getParent());
365 // We explicitly compute and set the unsafe stack layout for all unsafe
366 // static alloca instructions. We save the unsafe "base pointer" in the
367 // prologue into a local variable and restore it in the epilogue.
369 // Load the current stack pointer (we'll also use it as a base pointer).
370 // FIXME: use a dedicated register for it ?
371 Instruction *BasePointer =
372 IRB.CreateLoad(UnsafeStackPtr, false, "unsafe_stack_ptr");
373 assert(BasePointer->getType() == StackPtrTy);
375 for (ReturnInst *RI : Returns) {
376 IRB.SetInsertPoint(RI);
377 IRB.CreateStore(BasePointer, UnsafeStackPtr);
380 // Compute maximum alignment among static objects on the unsafe stack.
381 unsigned MaxAlignment = 0;
382 for (AllocaInst *AI : StaticAllocas) {
383 Type *Ty = AI->getAllocatedType();
385 std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment());
386 if (Align > MaxAlignment)
387 MaxAlignment = Align;
390 if (MaxAlignment > StackAlignment) {
391 // Re-align the base pointer according to the max requested alignment.
392 assert(isPowerOf2_32(MaxAlignment));
393 IRB.SetInsertPoint(cast<Instruction>(BasePointer->getNextNode()));
394 BasePointer = cast<Instruction>(IRB.CreateIntToPtr(
395 IRB.CreateAnd(IRB.CreatePtrToInt(BasePointer, IntPtrTy),
396 ConstantInt::get(IntPtrTy, ~uint64_t(MaxAlignment - 1))),
400 // Allocate space for every unsafe static AllocaInst on the unsafe stack.
401 int64_t StaticOffset = 0; // Current stack top.
402 for (AllocaInst *AI : StaticAllocas) {
403 IRB.SetInsertPoint(AI);
405 auto CArraySize = cast<ConstantInt>(AI->getArraySize());
406 Type *Ty = AI->getAllocatedType();
408 uint64_t Size = DL->getTypeAllocSize(Ty) * CArraySize->getZExtValue();
410 Size = 1; // Don't create zero-sized stack objects.
412 // Ensure the object is properly aligned.
414 std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment());
417 // NOTE: we ensure that BasePointer itself is aligned to >= Align.
418 StaticOffset += Size;
419 StaticOffset = RoundUpToAlignment(StaticOffset, Align);
421 Value *Off = IRB.CreateGEP(BasePointer, // BasePointer is i8*
422 ConstantInt::get(Int32Ty, -StaticOffset));
423 Value *NewAI = IRB.CreateBitCast(Off, AI->getType(), AI->getName());
424 if (AI->hasName() && isa<Instruction>(NewAI))
425 cast<Instruction>(NewAI)->takeName(AI);
427 // Replace alloc with the new location.
428 replaceDbgDeclareForAlloca(AI, NewAI, DIB, /*Deref=*/true);
429 AI->replaceAllUsesWith(NewAI);
430 AI->eraseFromParent();
433 // Re-align BasePointer so that our callees would see it aligned as
435 // FIXME: no need to update BasePointer in leaf functions.
436 StaticOffset = RoundUpToAlignment(StaticOffset, StackAlignment);
438 // Update shadow stack pointer in the function epilogue.
439 IRB.SetInsertPoint(cast<Instruction>(BasePointer->getNextNode()));
442 IRB.CreateGEP(BasePointer, ConstantInt::get(Int32Ty, -StaticOffset),
443 "unsafe_stack_static_top");
444 IRB.CreateStore(StaticTop, UnsafeStackPtr);
448 void SafeStack::moveDynamicAllocasToUnsafeStack(
449 Function &F, Value *UnsafeStackPtr, AllocaInst *DynamicTop,
450 ArrayRef<AllocaInst *> DynamicAllocas) {
451 DIBuilder DIB(*F.getParent());
453 for (AllocaInst *AI : DynamicAllocas) {
456 // Compute the new SP value (after AI).
457 Value *ArraySize = AI->getArraySize();
458 if (ArraySize->getType() != IntPtrTy)
459 ArraySize = IRB.CreateIntCast(ArraySize, IntPtrTy, false);
461 Type *Ty = AI->getAllocatedType();
462 uint64_t TySize = DL->getTypeAllocSize(Ty);
463 Value *Size = IRB.CreateMul(ArraySize, ConstantInt::get(IntPtrTy, TySize));
465 Value *SP = IRB.CreatePtrToInt(IRB.CreateLoad(UnsafeStackPtr), IntPtrTy);
466 SP = IRB.CreateSub(SP, Size);
468 // Align the SP value to satisfy the AllocaInst, type and stack alignments.
469 unsigned Align = std::max(
470 std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment()),
471 (unsigned)StackAlignment);
473 assert(isPowerOf2_32(Align));
474 Value *NewTop = IRB.CreateIntToPtr(
475 IRB.CreateAnd(SP, ConstantInt::get(IntPtrTy, ~uint64_t(Align - 1))),
478 // Save the stack pointer.
479 IRB.CreateStore(NewTop, UnsafeStackPtr);
481 IRB.CreateStore(NewTop, DynamicTop);
483 Value *NewAI = IRB.CreateIntToPtr(SP, AI->getType());
484 if (AI->hasName() && isa<Instruction>(NewAI))
487 replaceDbgDeclareForAlloca(AI, NewAI, DIB, /*Deref=*/true);
488 AI->replaceAllUsesWith(NewAI);
489 AI->eraseFromParent();
492 if (!DynamicAllocas.empty()) {
493 // Now go through the instructions again, replacing stacksave/stackrestore.
494 for (inst_iterator It = inst_begin(&F), Ie = inst_end(&F); It != Ie;) {
495 Instruction *I = &*(It++);
496 auto II = dyn_cast<IntrinsicInst>(I);
500 if (II->getIntrinsicID() == Intrinsic::stacksave) {
502 Instruction *LI = IRB.CreateLoad(UnsafeStackPtr);
504 II->replaceAllUsesWith(LI);
505 II->eraseFromParent();
506 } else if (II->getIntrinsicID() == Intrinsic::stackrestore) {
508 Instruction *SI = IRB.CreateStore(II->getArgOperand(0), UnsafeStackPtr);
510 assert(II->use_empty());
511 II->eraseFromParent();
517 bool SafeStack::runOnFunction(Function &F) {
518 auto AA = &getAnalysis<AliasAnalysis>();
520 DEBUG(dbgs() << "[SafeStack] Function: " << F.getName() << "\n");
522 if (!F.hasFnAttribute(Attribute::SafeStack)) {
523 DEBUG(dbgs() << "[SafeStack] safestack is not requested"
524 " for this function\n");
528 if (F.isDeclaration()) {
529 DEBUG(dbgs() << "[SafeStack] function definition"
530 " is not available\n");
535 // Make sure the regular stack protector won't run on this function
536 // (safestack attribute takes precedence).
538 B.addAttribute(Attribute::StackProtect)
539 .addAttribute(Attribute::StackProtectReq)
540 .addAttribute(Attribute::StackProtectStrong);
542 AttributeSet::FunctionIndex,
543 AttributeSet::get(F.getContext(), AttributeSet::FunctionIndex, B));
546 if (AA->onlyReadsMemory(&F)) {
547 // XXX: we don't protect against information leak attacks for now.
548 DEBUG(dbgs() << "[SafeStack] function only reads memory\n");
554 SmallVector<AllocaInst *, 16> StaticAllocas;
555 SmallVector<AllocaInst *, 4> DynamicAllocas;
556 SmallVector<ReturnInst *, 4> Returns;
558 // Collect all points where stack gets unwound and needs to be restored
559 // This is only necessary because the runtime (setjmp and unwind code) is
560 // not aware of the unsafe stack and won't unwind/restore it prorerly.
561 // To work around this problem without changing the runtime, we insert
562 // instrumentation to restore the unsafe stack pointer when necessary.
563 SmallVector<Instruction *, 4> StackRestorePoints;
565 // Find all static and dynamic alloca instructions that must be moved to the
566 // unsafe stack, all return instructions and stack restore points.
567 findInsts(F, StaticAllocas, DynamicAllocas, Returns, StackRestorePoints);
569 if (StaticAllocas.empty() && DynamicAllocas.empty() &&
570 StackRestorePoints.empty())
571 return false; // Nothing to do in this function.
573 if (!StaticAllocas.empty() || !DynamicAllocas.empty())
574 ++NumUnsafeStackFunctions; // This function has the unsafe stack.
576 if (!StackRestorePoints.empty())
577 ++NumUnsafeStackRestorePointsFunctions;
579 // The top of the unsafe stack after all unsafe static allocas are allocated.
580 Value *StaticTop = moveStaticAllocasToUnsafeStack(F, StaticAllocas, Returns);
582 // Safe stack object that stores the current unsafe stack top. It is updated
583 // as unsafe dynamic (non-constant-sized) allocas are allocated and freed.
584 // This is only needed if we need to restore stack pointer after longjmp
585 // or exceptions, and we have dynamic allocations.
586 // FIXME: a better alternative might be to store the unsafe stack pointer
587 // before setjmp / invoke instructions.
588 AllocaInst *DynamicTop = createStackRestorePoints(
589 F, StackRestorePoints, StaticTop, !DynamicAllocas.empty());
591 // Handle dynamic allocas.
592 moveDynamicAllocasToUnsafeStack(F, UnsafeStackPtr, DynamicTop,
595 DEBUG(dbgs() << "[SafeStack] safestack applied\n");
599 } // end anonymous namespace
601 char SafeStack::ID = 0;
602 INITIALIZE_PASS_BEGIN(SafeStack, "safe-stack",
603 "Safe Stack instrumentation pass", false, false)
604 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
605 INITIALIZE_PASS_END(SafeStack, "safe-stack", "Safe Stack instrumentation pass",
608 FunctionPass *llvm::createSafeStackPass() { return new SafeStack(); }