1 //===-- SafeStack.cpp - Safe Stack Insertion ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
12 // support library).
13 //
14 // http://clang.llvm.org/docs/SafeStack.html
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "SafeStackColoring.h"
19 #include "SafeStackLayout.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Analysis/BranchProbabilityInfo.h"
23 #include "llvm/Analysis/ScalarEvolution.h"
24 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DIBuilder.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/IRBuilder.h"
32 #include "llvm/IR/InstIterator.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/IntrinsicInst.h"
35 #include "llvm/IR/Intrinsics.h"
36 #include "llvm/IR/MDBuilder.h"
37 #include "llvm/IR/Module.h"
38 #include "llvm/Pass.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/Format.h"
42 #include "llvm/Support/MathExtras.h"
43 #include "llvm/Support/raw_os_ostream.h"
44 #include "llvm/Target/TargetLowering.h"
45 #include "llvm/Target/TargetSubtargetInfo.h"
46 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
47 #include "llvm/Transforms/Utils/Local.h"
48 #include "llvm/Transforms/Utils/ModuleUtils.h"
49 
50 using namespace llvm;
51 using namespace llvm::safestack;
52 
53 #define DEBUG_TYPE "safestack"
54 
55 enum UnsafeStackPtrStorageVal { ThreadLocalUSP, SingleThreadUSP };
56 
57 static cl::opt<UnsafeStackPtrStorageVal> USPStorage("safe-stack-usp-storage",
58     cl::Hidden, cl::init(ThreadLocalUSP),
59     cl::desc("Type of storage for the unsafe stack pointer"),
60     cl::values(clEnumValN(ThreadLocalUSP, "thread-local",
61                           "Thread-local storage"),
62                clEnumValN(SingleThreadUSP, "single-thread",
63                           "Non-thread-local storage")));
64 
65 namespace llvm {
66 
67 STATISTIC(NumFunctions, "Total number of functions");
68 STATISTIC(NumUnsafeStackFunctions, "Number of functions with unsafe stack");
69 STATISTIC(NumUnsafeStackRestorePointsFunctions,
70           "Number of functions that use setjmp or exceptions");
71 
72 STATISTIC(NumAllocas, "Total number of allocas");
73 STATISTIC(NumUnsafeStaticAllocas, "Number of unsafe static allocas");
74 STATISTIC(NumUnsafeDynamicAllocas, "Number of unsafe dynamic allocas");
75 STATISTIC(NumUnsafeByValArguments, "Number of unsafe byval arguments");
76 STATISTIC(NumUnsafeStackRestorePoints, "Number of setjmps and landingpads");
77 
78 } // namespace llvm
79 
80 namespace {
81 
82 /// Rewrite an SCEV expression for a memory access address to an expression that
83 /// represents offset from the given alloca.
84 ///
85 /// The implementation simply replaces all mentions of the alloca with zero.
86 class AllocaOffsetRewriter : public SCEVRewriteVisitor<AllocaOffsetRewriter> {
87   const Value *AllocaPtr;
88 
89 public:
90   AllocaOffsetRewriter(ScalarEvolution &SE, const Value *AllocaPtr)
91       : SCEVRewriteVisitor(SE), AllocaPtr(AllocaPtr) {}
92 
93   const SCEV *visitUnknown(const SCEVUnknown *Expr) {
94     if (Expr->getValue() == AllocaPtr)
95       return SE.getZero(Expr->getType());
96     return Expr;
97   }
98 };
99 
100 /// The SafeStack pass splits the stack of each function into the safe
101 /// stack, which is only accessed through memory safe dereferences (as
102 /// determined statically), and the unsafe stack, which contains all
103 /// local variables that are accessed in ways that we can't prove to
104 /// be safe.
105 class SafeStack : public FunctionPass {
106   const TargetMachine *TM;
107   const TargetLoweringBase *TL;
108   const DataLayout *DL;
109   ScalarEvolution *SE;
110 
111   Type *StackPtrTy;
112   Type *IntPtrTy;
113   Type *Int32Ty;
114   Type *Int8Ty;
115 
116   Value *UnsafeStackPtr = nullptr;
117 
118   /// Unsafe stack alignment. Each stack frame must ensure that the stack is
119   /// aligned to this value. We need to re-align the unsafe stack if the
120   /// alignment of any object on the stack exceeds this value.
121   ///
122   /// 16 seems like a reasonable upper bound on the alignment of objects that we
123   /// might expect to appear on the stack on most common targets.
124   enum { StackAlignment = 16 };
125 
126   /// \brief Build a value representing a pointer to the unsafe stack pointer.
127   Value *getOrCreateUnsafeStackPtr(IRBuilder<> &IRB, Function &F);
128 
129   /// \brief Return the value of the stack canary.
130   Value *getStackGuard(IRBuilder<> &IRB, Function &F);
131 
132   /// \brief Load stack guard from the frame and check if it has changed.
133   void checkStackGuard(IRBuilder<> &IRB, Function &F, ReturnInst &RI,
134                        AllocaInst *StackGuardSlot, Value *StackGuard);
135 
136   /// \brief Find all static allocas, dynamic allocas, return instructions and
137   /// stack restore points (exception unwind blocks and setjmp calls) in the
138   /// given function and append them to the respective vectors.
139   void findInsts(Function &F, SmallVectorImpl<AllocaInst *> &StaticAllocas,
140                  SmallVectorImpl<AllocaInst *> &DynamicAllocas,
141                  SmallVectorImpl<Argument *> &ByValArguments,
142                  SmallVectorImpl<ReturnInst *> &Returns,
143                  SmallVectorImpl<Instruction *> &StackRestorePoints);
144 
145   /// \brief Calculate the allocation size of a given alloca. Returns 0 if the
146   /// size can not be statically determined.
147   uint64_t getStaticAllocaAllocationSize(const AllocaInst* AI);
148 
149   /// \brief Allocate space for all static allocas in \p StaticAllocas,
150   /// replace allocas with pointers into the unsafe stack and generate code to
151   /// restore the stack pointer before all return instructions in \p Returns.
152   ///
153   /// \returns A pointer to the top of the unsafe stack after all unsafe static
154   /// allocas are allocated.
155   Value *moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F,
156                                         ArrayRef<AllocaInst *> StaticAllocas,
157                                         ArrayRef<Argument *> ByValArguments,
158                                         ArrayRef<ReturnInst *> Returns,
159                                         Instruction *BasePointer,
160                                         AllocaInst *StackGuardSlot);
161 
162   /// \brief Generate code to restore the stack after all stack restore points
163   /// in \p StackRestorePoints.
164   ///
165   /// \returns A local variable in which to maintain the dynamic top of the
166   /// unsafe stack if needed.
167   AllocaInst *
168   createStackRestorePoints(IRBuilder<> &IRB, Function &F,
169                            ArrayRef<Instruction *> StackRestorePoints,
170                            Value *StaticTop, bool NeedDynamicTop);
171 
172   /// \brief Replace all allocas in \p DynamicAllocas with code to allocate
173   /// space dynamically on the unsafe stack and store the dynamic unsafe stack
174   /// top to \p DynamicTop if non-null.
175   void moveDynamicAllocasToUnsafeStack(Function &F, Value *UnsafeStackPtr,
176                                        AllocaInst *DynamicTop,
177                                        ArrayRef<AllocaInst *> DynamicAllocas);
178 
179   bool IsSafeStackAlloca(const Value *AllocaPtr, uint64_t AllocaSize);
180 
181   bool IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U,
182                           const Value *AllocaPtr, uint64_t AllocaSize);
183   bool IsAccessSafe(Value *Addr, uint64_t Size, const Value *AllocaPtr,
184                     uint64_t AllocaSize);
185 
186 public:
187   static char ID; // Pass identification, replacement for typeid.
188   SafeStack(const TargetMachine *TM)
189       : FunctionPass(ID), TM(TM), TL(nullptr), DL(nullptr) {
190     initializeSafeStackPass(*PassRegistry::getPassRegistry());
191   }
192   SafeStack() : SafeStack(nullptr) {}
193 
194   void getAnalysisUsage(AnalysisUsage &AU) const override {
195     AU.addRequired<ScalarEvolutionWrapperPass>();
196   }
197 
198   bool doInitialization(Module &M) override {
199     DL = &M.getDataLayout();
200 
201     StackPtrTy = Type::getInt8PtrTy(M.getContext());
202     IntPtrTy = DL->getIntPtrType(M.getContext());
203     Int32Ty = Type::getInt32Ty(M.getContext());
204     Int8Ty = Type::getInt8Ty(M.getContext());
205 
206     return false;
207   }
208 
209   bool runOnFunction(Function &F) override;
210 }; // class SafeStack
211 
212 uint64_t SafeStack::getStaticAllocaAllocationSize(const AllocaInst* AI) {
213   uint64_t Size = DL->getTypeAllocSize(AI->getAllocatedType());
214   if (AI->isArrayAllocation()) {
215     auto C = dyn_cast<ConstantInt>(AI->getArraySize());
216     if (!C)
217       return 0;
218     Size *= C->getZExtValue();
219   }
220   return Size;
221 }
222 
223 bool SafeStack::IsAccessSafe(Value *Addr, uint64_t AccessSize,
224                              const Value *AllocaPtr, uint64_t AllocaSize) {
225   AllocaOffsetRewriter Rewriter(*SE, AllocaPtr);
226   const SCEV *Expr = Rewriter.visit(SE->getSCEV(Addr));
227 
228   uint64_t BitWidth = SE->getTypeSizeInBits(Expr->getType());
229   ConstantRange AccessStartRange = SE->getUnsignedRange(Expr);
230   ConstantRange SizeRange =
231       ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, AccessSize));
232   ConstantRange AccessRange = AccessStartRange.add(SizeRange);
233   ConstantRange AllocaRange =
234       ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, AllocaSize));
235   bool Safe = AllocaRange.contains(AccessRange);
236 
237   DEBUG(dbgs() << "[SafeStack] "
238                << (isa<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
239                << *AllocaPtr << "\n"
240                << "            Access " << *Addr << "\n"
241                << "            SCEV " << *Expr
242                << " U: " << SE->getUnsignedRange(Expr)
243                << ", S: " << SE->getSignedRange(Expr) << "\n"
244                << "            Range " << AccessRange << "\n"
245                << "            AllocaRange " << AllocaRange << "\n"
246                << "            " << (Safe ? "safe" : "unsafe") << "\n");
247 
248   return Safe;
249 }
250 
251 bool SafeStack::IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U,
252                                    const Value *AllocaPtr,
253                                    uint64_t AllocaSize) {
254   // All MemIntrinsics have destination address in Arg0 and size in Arg2.
255   if (MI->getRawDest() != U) return true;
256   const auto *Len = dyn_cast<ConstantInt>(MI->getLength());
257   // Non-constant size => unsafe. FIXME: try SCEV getRange.
258   if (!Len) return false;
259   return IsAccessSafe(U, Len->getZExtValue(), AllocaPtr, AllocaSize);
260 }
261 
262 /// Check whether a given allocation must be put on the safe
263 /// stack or not. The function analyzes all uses of AI and checks whether it is
264 /// only accessed in a memory safe way (as decided statically).
265 bool SafeStack::IsSafeStackAlloca(const Value *AllocaPtr, uint64_t AllocaSize) {
266   // Go through all uses of this alloca and check whether all accesses to the
267   // allocated object are statically known to be memory safe and, hence, the
268   // object can be placed on the safe stack.
269   SmallPtrSet<const Value *, 16> Visited;
270   SmallVector<const Value *, 8> WorkList;
271   WorkList.push_back(AllocaPtr);
272 
273   // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc.
274   while (!WorkList.empty()) {
275     const Value *V = WorkList.pop_back_val();
276     for (const Use &UI : V->uses()) {
277       auto I = cast<const Instruction>(UI.getUser());
278       assert(V == UI.get());
279 
280       switch (I->getOpcode()) {
281       case Instruction::Load: {
282         if (!IsAccessSafe(UI, DL->getTypeStoreSize(I->getType()), AllocaPtr,
283                           AllocaSize))
284           return false;
285         break;
286       }
287       case Instruction::VAArg:
288         // "va-arg" from a pointer is safe.
289         break;
290       case Instruction::Store: {
291         if (V == I->getOperand(0)) {
292           // Stored the pointer - conservatively assume it may be unsafe.
293           DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AllocaPtr
294                        << "\n            store of address: " << *I << "\n");
295           return false;
296         }
297 
298         if (!IsAccessSafe(UI, DL->getTypeStoreSize(I->getOperand(0)->getType()),
299                           AllocaPtr, AllocaSize))
300           return false;
301         break;
302       }
303       case Instruction::Ret: {
304         // Information leak.
305         return false;
306       }
307 
308       case Instruction::Call:
309       case Instruction::Invoke: {
310         ImmutableCallSite CS(I);
311 
312         if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
313           if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
314               II->getIntrinsicID() == Intrinsic::lifetime_end)
315             continue;
316         }
317 
318         if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
319           if (!IsMemIntrinsicSafe(MI, UI, AllocaPtr, AllocaSize)) {
320             DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AllocaPtr
321                          << "\n            unsafe memintrinsic: " << *I
322                          << "\n");
323             return false;
324           }
325           continue;
326         }
327 
328         // LLVM 'nocapture' attribute is only set for arguments whose address
329         // is not stored, passed around, or used in any other non-trivial way.
330         // We assume that passing a pointer to an object as a 'nocapture
331         // readnone' argument is safe.
332         // FIXME: a more precise solution would require an interprocedural
333         // analysis here, which would look at all uses of an argument inside
334         // the function being called.
335         ImmutableCallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
336         for (ImmutableCallSite::arg_iterator A = B; A != E; ++A)
337           if (A->get() == V)
338             if (!(CS.doesNotCapture(A - B) && (CS.doesNotAccessMemory(A - B) ||
339                                                CS.doesNotAccessMemory()))) {
340               DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AllocaPtr
341                            << "\n            unsafe call: " << *I << "\n");
342               return false;
343             }
344         continue;
345       }
346 
347       default:
348         if (Visited.insert(I).second)
349           WorkList.push_back(cast<const Instruction>(I));
350       }
351     }
352   }
353 
354   // All uses of the alloca are safe, we can place it on the safe stack.
355   return true;
356 }
357 
358 Value *SafeStack::getOrCreateUnsafeStackPtr(IRBuilder<> &IRB, Function &F) {
359   // Check if there is a target-specific location for the unsafe stack pointer.
360   if (Value *V = TL->getSafeStackPointerLocation(IRB))
361     return V;
362 
363   // Otherwise, assume the target links with compiler-rt, which provides a
364   // thread-local variable with a magic name.
365   Module &M = *F.getParent();
366   const char *UnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
367   auto UnsafeStackPtr =
368       dyn_cast_or_null<GlobalVariable>(M.getNamedValue(UnsafeStackPtrVar));
369 
370   bool UseTLS = USPStorage == ThreadLocalUSP;
371 
372   if (!UnsafeStackPtr) {
373     auto TLSModel = UseTLS ?
374         GlobalValue::InitialExecTLSModel :
375         GlobalValue::NotThreadLocal;
376     // The global variable is not defined yet, define it ourselves.
377     // We use the initial-exec TLS model because we do not support the
378     // variable living anywhere other than in the main executable.
379     UnsafeStackPtr = new GlobalVariable(
380         M, StackPtrTy, false, GlobalValue::ExternalLinkage, nullptr,
381         UnsafeStackPtrVar, nullptr, TLSModel);
382   } else {
383     // The variable exists, check its type and attributes.
384     if (UnsafeStackPtr->getValueType() != StackPtrTy)
385       report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type");
386     if (UseTLS != UnsafeStackPtr->isThreadLocal())
387       report_fatal_error(Twine(UnsafeStackPtrVar) + " must " +
388                          (UseTLS ? "" : "not ") + "be thread-local");
389   }
390   return UnsafeStackPtr;
391 }
392 
393 Value *SafeStack::getStackGuard(IRBuilder<> &IRB, Function &F) {
394   Value *StackGuardVar = TL->getIRStackGuard(IRB);
395   if (!StackGuardVar)
396     StackGuardVar =
397         F.getParent()->getOrInsertGlobal("__stack_chk_guard", StackPtrTy);
398   return IRB.CreateLoad(StackGuardVar, "StackGuard");
399 }
400 
401 void SafeStack::findInsts(Function &F,
402                           SmallVectorImpl<AllocaInst *> &StaticAllocas,
403                           SmallVectorImpl<AllocaInst *> &DynamicAllocas,
404                           SmallVectorImpl<Argument *> &ByValArguments,
405                           SmallVectorImpl<ReturnInst *> &Returns,
406                           SmallVectorImpl<Instruction *> &StackRestorePoints) {
407   for (Instruction &I : instructions(&F)) {
408     if (auto AI = dyn_cast<AllocaInst>(&I)) {
409       ++NumAllocas;
410 
411       uint64_t Size = getStaticAllocaAllocationSize(AI);
412       if (IsSafeStackAlloca(AI, Size))
413         continue;
414 
415       if (AI->isStaticAlloca()) {
416         ++NumUnsafeStaticAllocas;
417         StaticAllocas.push_back(AI);
418       } else {
419         ++NumUnsafeDynamicAllocas;
420         DynamicAllocas.push_back(AI);
421       }
422     } else if (auto RI = dyn_cast<ReturnInst>(&I)) {
423       Returns.push_back(RI);
424     } else if (auto CI = dyn_cast<CallInst>(&I)) {
425       // setjmps require stack restore.
426       if (CI->getCalledFunction() && CI->canReturnTwice())
427         StackRestorePoints.push_back(CI);
428     } else if (auto LP = dyn_cast<LandingPadInst>(&I)) {
429       // Exception landing pads require stack restore.
430       StackRestorePoints.push_back(LP);
431     } else if (auto II = dyn_cast<IntrinsicInst>(&I)) {
432       if (II->getIntrinsicID() == Intrinsic::gcroot)
433         llvm::report_fatal_error(
434             "gcroot intrinsic not compatible with safestack attribute");
435     }
436   }
437   for (Argument &Arg : F.args()) {
438     if (!Arg.hasByValAttr())
439       continue;
440     uint64_t Size =
441         DL->getTypeStoreSize(Arg.getType()->getPointerElementType());
442     if (IsSafeStackAlloca(&Arg, Size))
443       continue;
444 
445     ++NumUnsafeByValArguments;
446     ByValArguments.push_back(&Arg);
447   }
448 }
449 
450 AllocaInst *
451 SafeStack::createStackRestorePoints(IRBuilder<> &IRB, Function &F,
452                                     ArrayRef<Instruction *> StackRestorePoints,
453                                     Value *StaticTop, bool NeedDynamicTop) {
454   assert(StaticTop && "The stack top isn't set.");
455 
456   if (StackRestorePoints.empty())
457     return nullptr;
458 
459   // We need the current value of the shadow stack pointer to restore
460   // after longjmp or exception catching.
461 
462   // FIXME: On some platforms this could be handled by the longjmp/exception
463   // runtime itself.
464 
465   AllocaInst *DynamicTop = nullptr;
466   if (NeedDynamicTop) {
467     // If we also have dynamic alloca's, the stack pointer value changes
468     // throughout the function. For now we store it in an alloca.
469     DynamicTop = IRB.CreateAlloca(StackPtrTy, /*ArraySize=*/nullptr,
470                                   "unsafe_stack_dynamic_ptr");
471     IRB.CreateStore(StaticTop, DynamicTop);
472   }
473 
474   // Restore current stack pointer after longjmp/exception catch.
475   for (Instruction *I : StackRestorePoints) {
476     ++NumUnsafeStackRestorePoints;
477 
478     IRB.SetInsertPoint(I->getNextNode());
479     Value *CurrentTop = DynamicTop ? IRB.CreateLoad(DynamicTop) : StaticTop;
480     IRB.CreateStore(CurrentTop, UnsafeStackPtr);
481   }
482 
483   return DynamicTop;
484 }
485 
486 void SafeStack::checkStackGuard(IRBuilder<> &IRB, Function &F, ReturnInst &RI,
487                                 AllocaInst *StackGuardSlot, Value *StackGuard) {
488   Value *V = IRB.CreateLoad(StackGuardSlot);
489   Value *Cmp = IRB.CreateICmpNE(StackGuard, V);
490 
491   auto SuccessProb = BranchProbabilityInfo::getBranchProbStackProtector(true);
492   auto FailureProb = BranchProbabilityInfo::getBranchProbStackProtector(false);
493   MDNode *Weights = MDBuilder(F.getContext())
494                         .createBranchWeights(SuccessProb.getNumerator(),
495                                              FailureProb.getNumerator());
496   Instruction *CheckTerm =
497       SplitBlockAndInsertIfThen(Cmp, &RI,
498                                 /* Unreachable */ true, Weights);
499   IRBuilder<> IRBFail(CheckTerm);
500   // FIXME: respect -fsanitize-trap / -ftrap-function here?
501   Constant *StackChkFail = F.getParent()->getOrInsertFunction(
502       "__stack_chk_fail", IRB.getVoidTy(), nullptr);
503   IRBFail.CreateCall(StackChkFail, {});
504 }
505 
506 /// We explicitly compute and set the unsafe stack layout for all unsafe
507 /// static alloca instructions. We save the unsafe "base pointer" in the
508 /// prologue into a local variable and restore it in the epilogue.
509 Value *SafeStack::moveStaticAllocasToUnsafeStack(
510     IRBuilder<> &IRB, Function &F, ArrayRef<AllocaInst *> StaticAllocas,
511     ArrayRef<Argument *> ByValArguments, ArrayRef<ReturnInst *> Returns,
512     Instruction *BasePointer, AllocaInst *StackGuardSlot) {
513   if (StaticAllocas.empty() && ByValArguments.empty())
514     return BasePointer;
515 
516   DIBuilder DIB(*F.getParent());
517 
518   StackColoring SSC(F, StaticAllocas);
519   SSC.run();
520   SSC.removeAllMarkers();
521 
522   // Unsafe stack always grows down.
523   StackLayout SSL(StackAlignment);
524   if (StackGuardSlot) {
525     Type *Ty = StackGuardSlot->getAllocatedType();
526     unsigned Align =
527         std::max(DL->getPrefTypeAlignment(Ty), StackGuardSlot->getAlignment());
528     SSL.addObject(StackGuardSlot, getStaticAllocaAllocationSize(StackGuardSlot),
529                   Align, SSC.getFullLiveRange());
530   }
531 
532   for (Argument *Arg : ByValArguments) {
533     Type *Ty = Arg->getType()->getPointerElementType();
534     uint64_t Size = DL->getTypeStoreSize(Ty);
535     if (Size == 0)
536       Size = 1; // Don't create zero-sized stack objects.
537 
538     // Ensure the object is properly aligned.
539     unsigned Align = std::max((unsigned)DL->getPrefTypeAlignment(Ty),
540                               Arg->getParamAlignment());
541     SSL.addObject(Arg, Size, Align, SSC.getFullLiveRange());
542   }
543 
544   for (AllocaInst *AI : StaticAllocas) {
545     Type *Ty = AI->getAllocatedType();
546     uint64_t Size = getStaticAllocaAllocationSize(AI);
547     if (Size == 0)
548       Size = 1; // Don't create zero-sized stack objects.
549 
550     // Ensure the object is properly aligned.
551     unsigned Align =
552         std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment());
553 
554     SSL.addObject(AI, Size, Align, SSC.getLiveRange(AI));
555   }
556 
557   SSL.computeLayout();
558   unsigned FrameAlignment = SSL.getFrameAlignment();
559 
560   // FIXME: tell SSL that we start at a less-then-MaxAlignment aligned location
561   // (AlignmentSkew).
562   if (FrameAlignment > StackAlignment) {
563     // Re-align the base pointer according to the max requested alignment.
564     assert(isPowerOf2_32(FrameAlignment));
565     IRB.SetInsertPoint(BasePointer->getNextNode());
566     BasePointer = cast<Instruction>(IRB.CreateIntToPtr(
567         IRB.CreateAnd(IRB.CreatePtrToInt(BasePointer, IntPtrTy),
568                       ConstantInt::get(IntPtrTy, ~uint64_t(FrameAlignment - 1))),
569         StackPtrTy));
570   }
571 
572   IRB.SetInsertPoint(BasePointer->getNextNode());
573 
574   if (StackGuardSlot) {
575     unsigned Offset = SSL.getObjectOffset(StackGuardSlot);
576     Value *Off = IRB.CreateGEP(BasePointer, // BasePointer is i8*
577                                ConstantInt::get(Int32Ty, -Offset));
578     Value *NewAI =
579         IRB.CreateBitCast(Off, StackGuardSlot->getType(), "StackGuardSlot");
580 
581     // Replace alloc with the new location.
582     StackGuardSlot->replaceAllUsesWith(NewAI);
583     StackGuardSlot->eraseFromParent();
584   }
585 
586   for (Argument *Arg : ByValArguments) {
587     unsigned Offset = SSL.getObjectOffset(Arg);
588     Type *Ty = Arg->getType()->getPointerElementType();
589 
590     uint64_t Size = DL->getTypeStoreSize(Ty);
591     if (Size == 0)
592       Size = 1; // Don't create zero-sized stack objects.
593 
594     Value *Off = IRB.CreateGEP(BasePointer, // BasePointer is i8*
595                                ConstantInt::get(Int32Ty, -Offset));
596     Value *NewArg = IRB.CreateBitCast(Off, Arg->getType(),
597                                      Arg->getName() + ".unsafe-byval");
598 
599     // Replace alloc with the new location.
600     replaceDbgDeclare(Arg, BasePointer, BasePointer->getNextNode(), DIB,
601                       /*Deref=*/true, -Offset);
602     Arg->replaceAllUsesWith(NewArg);
603     IRB.SetInsertPoint(cast<Instruction>(NewArg)->getNextNode());
604     IRB.CreateMemCpy(Off, Arg, Size, Arg->getParamAlignment());
605   }
606 
607   // Allocate space for every unsafe static AllocaInst on the unsafe stack.
608   for (AllocaInst *AI : StaticAllocas) {
609     IRB.SetInsertPoint(AI);
610     unsigned Offset = SSL.getObjectOffset(AI);
611 
612     uint64_t Size = getStaticAllocaAllocationSize(AI);
613     if (Size == 0)
614       Size = 1; // Don't create zero-sized stack objects.
615 
616     replaceDbgDeclareForAlloca(AI, BasePointer, DIB, /*Deref=*/true, -Offset);
617     replaceDbgValueForAlloca(AI, BasePointer, DIB, -Offset);
618 
619     // Replace uses of the alloca with the new location.
620     // Insert address calculation close to each use to work around PR27844.
621     std::string Name = std::string(AI->getName()) + ".unsafe";
622     while (!AI->use_empty()) {
623       Use &U = *AI->use_begin();
624       Instruction *User = cast<Instruction>(U.getUser());
625 
626       Instruction *InsertBefore;
627       if (auto *PHI = dyn_cast<PHINode>(User))
628         InsertBefore = PHI->getIncomingBlock(U)->getTerminator();
629       else
630         InsertBefore = User;
631 
632       IRBuilder<> IRBUser(InsertBefore);
633       Value *Off = IRBUser.CreateGEP(BasePointer, // BasePointer is i8*
634                                      ConstantInt::get(Int32Ty, -Offset));
635       Value *Replacement = IRBUser.CreateBitCast(Off, AI->getType(), Name);
636 
637       if (auto *PHI = dyn_cast<PHINode>(User)) {
638         // PHI nodes may have multiple incoming edges from the same BB (why??),
639         // all must be updated at once with the same incoming value.
640         auto *BB = PHI->getIncomingBlock(U);
641         for (unsigned I = 0; I < PHI->getNumIncomingValues(); ++I)
642           if (PHI->getIncomingBlock(I) == BB)
643             PHI->setIncomingValue(I, Replacement);
644       } else {
645         U.set(Replacement);
646       }
647     }
648 
649     AI->eraseFromParent();
650   }
651 
652   // Re-align BasePointer so that our callees would see it aligned as
653   // expected.
654   // FIXME: no need to update BasePointer in leaf functions.
655   unsigned FrameSize = alignTo(SSL.getFrameSize(), StackAlignment);
656 
657   // Update shadow stack pointer in the function epilogue.
658   IRB.SetInsertPoint(BasePointer->getNextNode());
659 
660   Value *StaticTop =
661       IRB.CreateGEP(BasePointer, ConstantInt::get(Int32Ty, -FrameSize),
662                     "unsafe_stack_static_top");
663   IRB.CreateStore(StaticTop, UnsafeStackPtr);
664   return StaticTop;
665 }
666 
667 void SafeStack::moveDynamicAllocasToUnsafeStack(
668     Function &F, Value *UnsafeStackPtr, AllocaInst *DynamicTop,
669     ArrayRef<AllocaInst *> DynamicAllocas) {
670   DIBuilder DIB(*F.getParent());
671 
672   for (AllocaInst *AI : DynamicAllocas) {
673     IRBuilder<> IRB(AI);
674 
675     // Compute the new SP value (after AI).
676     Value *ArraySize = AI->getArraySize();
677     if (ArraySize->getType() != IntPtrTy)
678       ArraySize = IRB.CreateIntCast(ArraySize, IntPtrTy, false);
679 
680     Type *Ty = AI->getAllocatedType();
681     uint64_t TySize = DL->getTypeAllocSize(Ty);
682     Value *Size = IRB.CreateMul(ArraySize, ConstantInt::get(IntPtrTy, TySize));
683 
684     Value *SP = IRB.CreatePtrToInt(IRB.CreateLoad(UnsafeStackPtr), IntPtrTy);
685     SP = IRB.CreateSub(SP, Size);
686 
687     // Align the SP value to satisfy the AllocaInst, type and stack alignments.
688     unsigned Align = std::max(
689         std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment()),
690         (unsigned)StackAlignment);
691 
692     assert(isPowerOf2_32(Align));
693     Value *NewTop = IRB.CreateIntToPtr(
694         IRB.CreateAnd(SP, ConstantInt::get(IntPtrTy, ~uint64_t(Align - 1))),
695         StackPtrTy);
696 
697     // Save the stack pointer.
698     IRB.CreateStore(NewTop, UnsafeStackPtr);
699     if (DynamicTop)
700       IRB.CreateStore(NewTop, DynamicTop);
701 
702     Value *NewAI = IRB.CreatePointerCast(NewTop, AI->getType());
703     if (AI->hasName() && isa<Instruction>(NewAI))
704       NewAI->takeName(AI);
705 
706     replaceDbgDeclareForAlloca(AI, NewAI, DIB, /*Deref=*/true);
707     AI->replaceAllUsesWith(NewAI);
708     AI->eraseFromParent();
709   }
710 
711   if (!DynamicAllocas.empty()) {
712     // Now go through the instructions again, replacing stacksave/stackrestore.
713     for (inst_iterator It = inst_begin(&F), Ie = inst_end(&F); It != Ie;) {
714       Instruction *I = &*(It++);
715       auto II = dyn_cast<IntrinsicInst>(I);
716       if (!II)
717         continue;
718 
719       if (II->getIntrinsicID() == Intrinsic::stacksave) {
720         IRBuilder<> IRB(II);
721         Instruction *LI = IRB.CreateLoad(UnsafeStackPtr);
722         LI->takeName(II);
723         II->replaceAllUsesWith(LI);
724         II->eraseFromParent();
725       } else if (II->getIntrinsicID() == Intrinsic::stackrestore) {
726         IRBuilder<> IRB(II);
727         Instruction *SI = IRB.CreateStore(II->getArgOperand(0), UnsafeStackPtr);
728         SI->takeName(II);
729         assert(II->use_empty());
730         II->eraseFromParent();
731       }
732     }
733   }
734 }
735 
736 bool SafeStack::runOnFunction(Function &F) {
737   DEBUG(dbgs() << "[SafeStack] Function: " << F.getName() << "\n");
738 
739   if (!F.hasFnAttribute(Attribute::SafeStack)) {
740     DEBUG(dbgs() << "[SafeStack]     safestack is not requested"
741                     " for this function\n");
742     return false;
743   }
744 
745   if (F.isDeclaration()) {
746     DEBUG(dbgs() << "[SafeStack]     function definition"
747                     " is not available\n");
748     return false;
749   }
750 
751   if (!TM)
752     report_fatal_error("Target machine is required");
753   TL = TM->getSubtargetImpl(F)->getTargetLowering();
754   SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
755 
756   ++NumFunctions;
757 
758   SmallVector<AllocaInst *, 16> StaticAllocas;
759   SmallVector<AllocaInst *, 4> DynamicAllocas;
760   SmallVector<Argument *, 4> ByValArguments;
761   SmallVector<ReturnInst *, 4> Returns;
762 
763   // Collect all points where stack gets unwound and needs to be restored
764   // This is only necessary because the runtime (setjmp and unwind code) is
765   // not aware of the unsafe stack and won't unwind/restore it prorerly.
766   // To work around this problem without changing the runtime, we insert
767   // instrumentation to restore the unsafe stack pointer when necessary.
768   SmallVector<Instruction *, 4> StackRestorePoints;
769 
770   // Find all static and dynamic alloca instructions that must be moved to the
771   // unsafe stack, all return instructions and stack restore points.
772   findInsts(F, StaticAllocas, DynamicAllocas, ByValArguments, Returns,
773             StackRestorePoints);
774 
775   if (StaticAllocas.empty() && DynamicAllocas.empty() &&
776       ByValArguments.empty() && StackRestorePoints.empty())
777     return false; // Nothing to do in this function.
778 
779   if (!StaticAllocas.empty() || !DynamicAllocas.empty() ||
780       !ByValArguments.empty())
781     ++NumUnsafeStackFunctions; // This function has the unsafe stack.
782 
783   if (!StackRestorePoints.empty())
784     ++NumUnsafeStackRestorePointsFunctions;
785 
786   IRBuilder<> IRB(&F.front(), F.begin()->getFirstInsertionPt());
787   UnsafeStackPtr = getOrCreateUnsafeStackPtr(IRB, F);
788 
789   // Load the current stack pointer (we'll also use it as a base pointer).
790   // FIXME: use a dedicated register for it ?
791   Instruction *BasePointer =
792       IRB.CreateLoad(UnsafeStackPtr, false, "unsafe_stack_ptr");
793   assert(BasePointer->getType() == StackPtrTy);
794 
795   AllocaInst *StackGuardSlot = nullptr;
796   // FIXME: implement weaker forms of stack protector.
797   if (F.hasFnAttribute(Attribute::StackProtect) ||
798       F.hasFnAttribute(Attribute::StackProtectStrong) ||
799       F.hasFnAttribute(Attribute::StackProtectReq)) {
800     Value *StackGuard = getStackGuard(IRB, F);
801     StackGuardSlot = IRB.CreateAlloca(StackPtrTy, nullptr);
802     IRB.CreateStore(StackGuard, StackGuardSlot);
803 
804     for (ReturnInst *RI : Returns) {
805       IRBuilder<> IRBRet(RI);
806       checkStackGuard(IRBRet, F, *RI, StackGuardSlot, StackGuard);
807     }
808   }
809 
810   // The top of the unsafe stack after all unsafe static allocas are
811   // allocated.
812   Value *StaticTop =
813       moveStaticAllocasToUnsafeStack(IRB, F, StaticAllocas, ByValArguments,
814                                      Returns, BasePointer, StackGuardSlot);
815 
816   // Safe stack object that stores the current unsafe stack top. It is updated
817   // as unsafe dynamic (non-constant-sized) allocas are allocated and freed.
818   // This is only needed if we need to restore stack pointer after longjmp
819   // or exceptions, and we have dynamic allocations.
820   // FIXME: a better alternative might be to store the unsafe stack pointer
821   // before setjmp / invoke instructions.
822   AllocaInst *DynamicTop = createStackRestorePoints(
823       IRB, F, StackRestorePoints, StaticTop, !DynamicAllocas.empty());
824 
825   // Handle dynamic allocas.
826   moveDynamicAllocasToUnsafeStack(F, UnsafeStackPtr, DynamicTop,
827                                   DynamicAllocas);
828 
829   // Restore the unsafe stack pointer before each return.
830   for (ReturnInst *RI : Returns) {
831     IRB.SetInsertPoint(RI);
832     IRB.CreateStore(BasePointer, UnsafeStackPtr);
833   }
834 
835   DEBUG(dbgs() << "[SafeStack]     safestack applied\n");
836   return true;
837 }
838 
839 } // anonymous namespace
840 
841 char SafeStack::ID = 0;
842 INITIALIZE_TM_PASS_BEGIN(SafeStack, "safe-stack",
843                          "Safe Stack instrumentation pass", false, false)
844 INITIALIZE_TM_PASS_END(SafeStack, "safe-stack",
845                        "Safe Stack instrumentation pass", false, false)
846 
847 FunctionPass *llvm::createSafeStackPass(const llvm::TargetMachine *TM) {
848   return new SafeStack(TM);
849 }
850