xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/SafeStack.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1 //===- SafeStack.cpp - Safe Stack Insertion -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass splits the stack into the safe stack (kept as-is for LLVM backend)
10 // and the unsafe stack (explicitly allocated and managed through the runtime
11 // support library).
12 //
13 // http://clang.llvm.org/docs/SafeStack.html
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/CodeGen/SafeStack.h"
18 #include "SafeStackLayout.h"
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Analysis/AssumptionCache.h"
25 #include "llvm/Analysis/BranchProbabilityInfo.h"
26 #include "llvm/Analysis/DomTreeUpdater.h"
27 #include "llvm/Analysis/InlineCost.h"
28 #include "llvm/Analysis/LoopInfo.h"
29 #include "llvm/Analysis/ScalarEvolution.h"
30 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
31 #include "llvm/Analysis/StackLifetime.h"
32 #include "llvm/Analysis/TargetLibraryInfo.h"
33 #include "llvm/CodeGen/TargetLowering.h"
34 #include "llvm/CodeGen/TargetPassConfig.h"
35 #include "llvm/CodeGen/TargetSubtargetInfo.h"
36 #include "llvm/IR/Argument.h"
37 #include "llvm/IR/Attributes.h"
38 #include "llvm/IR/ConstantRange.h"
39 #include "llvm/IR/Constants.h"
40 #include "llvm/IR/DIBuilder.h"
41 #include "llvm/IR/DataLayout.h"
42 #include "llvm/IR/DerivedTypes.h"
43 #include "llvm/IR/Dominators.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/IRBuilder.h"
46 #include "llvm/IR/InstIterator.h"
47 #include "llvm/IR/Instruction.h"
48 #include "llvm/IR/Instructions.h"
49 #include "llvm/IR/IntrinsicInst.h"
50 #include "llvm/IR/Intrinsics.h"
51 #include "llvm/IR/MDBuilder.h"
52 #include "llvm/IR/Metadata.h"
53 #include "llvm/IR/Module.h"
54 #include "llvm/IR/Type.h"
55 #include "llvm/IR/Use.h"
56 #include "llvm/IR/Value.h"
57 #include "llvm/InitializePasses.h"
58 #include "llvm/Pass.h"
59 #include "llvm/Support/Casting.h"
60 #include "llvm/Support/Debug.h"
61 #include "llvm/Support/ErrorHandling.h"
62 #include "llvm/Support/MathExtras.h"
63 #include "llvm/Support/raw_ostream.h"
64 #include "llvm/Target/TargetMachine.h"
65 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
66 #include "llvm/Transforms/Utils/Cloning.h"
67 #include "llvm/Transforms/Utils/Local.h"
68 #include <algorithm>
69 #include <cassert>
70 #include <cstdint>
71 #include <optional>
72 #include <string>
73 #include <utility>
74 
75 using namespace llvm;
76 using namespace llvm::safestack;
77 
78 #define DEBUG_TYPE "safe-stack"
79 
80 namespace llvm {
81 
82 STATISTIC(NumFunctions, "Total number of functions");
83 STATISTIC(NumUnsafeStackFunctions, "Number of functions with unsafe stack");
84 STATISTIC(NumUnsafeStackRestorePointsFunctions,
85           "Number of functions that use setjmp or exceptions");
86 
87 STATISTIC(NumAllocas, "Total number of allocas");
88 STATISTIC(NumUnsafeStaticAllocas, "Number of unsafe static allocas");
89 STATISTIC(NumUnsafeDynamicAllocas, "Number of unsafe dynamic allocas");
90 STATISTIC(NumUnsafeByValArguments, "Number of unsafe byval arguments");
91 STATISTIC(NumUnsafeStackRestorePoints, "Number of setjmps and landingpads");
92 
93 } // namespace llvm
94 
95 /// Use __safestack_pointer_address even if the platform has a faster way of
96 /// access safe stack pointer.
97 static cl::opt<bool>
98     SafeStackUsePointerAddress("safestack-use-pointer-address",
99                                   cl::init(false), cl::Hidden);
100 
101 static cl::opt<bool> ClColoring("safe-stack-coloring",
102                                 cl::desc("enable safe stack coloring"),
103                                 cl::Hidden, cl::init(true));
104 
105 namespace {
106 
107 /// The SafeStack pass splits the stack of each function into the safe
108 /// stack, which is only accessed through memory safe dereferences (as
109 /// determined statically), and the unsafe stack, which contains all
110 /// local variables that are accessed in ways that we can't prove to
111 /// be safe.
112 class SafeStack {
113   Function &F;
114   const TargetLoweringBase &TL;
115   const DataLayout &DL;
116   DomTreeUpdater *DTU;
117   ScalarEvolution &SE;
118 
119   Type *StackPtrTy;
120   Type *IntPtrTy;
121   Type *Int32Ty;
122   Type *Int8Ty;
123 
124   Value *UnsafeStackPtr = nullptr;
125 
126   /// Unsafe stack alignment. Each stack frame must ensure that the stack is
127   /// aligned to this value. We need to re-align the unsafe stack if the
128   /// alignment of any object on the stack exceeds this value.
129   ///
130   /// 16 seems like a reasonable upper bound on the alignment of objects that we
131   /// might expect to appear on the stack on most common targets.
132   static constexpr Align StackAlignment = Align::Constant<16>();
133 
134   /// Return the value of the stack canary.
135   Value *getStackGuard(IRBuilder<> &IRB, Function &F);
136 
137   /// Load stack guard from the frame and check if it has changed.
138   void checkStackGuard(IRBuilder<> &IRB, Function &F, Instruction &RI,
139                        AllocaInst *StackGuardSlot, Value *StackGuard);
140 
141   /// Find all static allocas, dynamic allocas, return instructions and
142   /// stack restore points (exception unwind blocks and setjmp calls) in the
143   /// given function and append them to the respective vectors.
144   void findInsts(Function &F, SmallVectorImpl<AllocaInst *> &StaticAllocas,
145                  SmallVectorImpl<AllocaInst *> &DynamicAllocas,
146                  SmallVectorImpl<Argument *> &ByValArguments,
147                  SmallVectorImpl<Instruction *> &Returns,
148                  SmallVectorImpl<Instruction *> &StackRestorePoints);
149 
150   /// Calculate the allocation size of a given alloca. Returns 0 if the
151   /// size can not be statically determined.
152   uint64_t getStaticAllocaAllocationSize(const AllocaInst* AI);
153 
154   /// Allocate space for all static allocas in \p StaticAllocas,
155   /// replace allocas with pointers into the unsafe stack.
156   ///
157   /// \returns A pointer to the top of the unsafe stack after all unsafe static
158   /// allocas are allocated.
159   Value *moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F,
160                                         ArrayRef<AllocaInst *> StaticAllocas,
161                                         ArrayRef<Argument *> ByValArguments,
162                                         Instruction *BasePointer,
163                                         AllocaInst *StackGuardSlot);
164 
165   /// Generate code to restore the stack after all stack restore points
166   /// in \p StackRestorePoints.
167   ///
168   /// \returns A local variable in which to maintain the dynamic top of the
169   /// unsafe stack if needed.
170   AllocaInst *
171   createStackRestorePoints(IRBuilder<> &IRB, Function &F,
172                            ArrayRef<Instruction *> StackRestorePoints,
173                            Value *StaticTop, bool NeedDynamicTop);
174 
175   /// Replace all allocas in \p DynamicAllocas with code to allocate
176   /// space dynamically on the unsafe stack and store the dynamic unsafe stack
177   /// top to \p DynamicTop if non-null.
178   void moveDynamicAllocasToUnsafeStack(Function &F, Value *UnsafeStackPtr,
179                                        AllocaInst *DynamicTop,
180                                        ArrayRef<AllocaInst *> DynamicAllocas);
181 
182   bool IsSafeStackAlloca(const Value *AllocaPtr, uint64_t AllocaSize);
183 
184   bool IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U,
185                           const Value *AllocaPtr, uint64_t AllocaSize);
186   bool IsAccessSafe(Value *Addr, uint64_t Size, const Value *AllocaPtr,
187                     uint64_t AllocaSize);
188 
189   bool ShouldInlinePointerAddress(CallInst &CI);
190   void TryInlinePointerAddress();
191 
192 public:
193   SafeStack(Function &F, const TargetLoweringBase &TL, const DataLayout &DL,
194             DomTreeUpdater *DTU, ScalarEvolution &SE)
195       : F(F), TL(TL), DL(DL), DTU(DTU), SE(SE),
196         StackPtrTy(PointerType::getUnqual(F.getContext())),
197         IntPtrTy(DL.getIntPtrType(F.getContext())),
198         Int32Ty(Type::getInt32Ty(F.getContext())),
199         Int8Ty(Type::getInt8Ty(F.getContext())) {}
200 
201   // Run the transformation on the associated function.
202   // Returns whether the function was changed.
203   bool run();
204 };
205 
206 constexpr Align SafeStack::StackAlignment;
207 
208 uint64_t SafeStack::getStaticAllocaAllocationSize(const AllocaInst* AI) {
209   uint64_t Size = DL.getTypeAllocSize(AI->getAllocatedType());
210   if (AI->isArrayAllocation()) {
211     auto C = dyn_cast<ConstantInt>(AI->getArraySize());
212     if (!C)
213       return 0;
214     Size *= C->getZExtValue();
215   }
216   return Size;
217 }
218 
219 bool SafeStack::IsAccessSafe(Value *Addr, uint64_t AccessSize,
220                              const Value *AllocaPtr, uint64_t AllocaSize) {
221   const SCEV *AddrExpr = SE.getSCEV(Addr);
222   const auto *Base = dyn_cast<SCEVUnknown>(SE.getPointerBase(AddrExpr));
223   if (!Base || Base->getValue() != AllocaPtr) {
224     LLVM_DEBUG(
225         dbgs() << "[SafeStack] "
226                << (isa<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
227                << *AllocaPtr << "\n"
228                << "SCEV " << *AddrExpr << " not directly based on alloca\n");
229     return false;
230   }
231 
232   const SCEV *Expr = SE.removePointerBase(AddrExpr);
233   uint64_t BitWidth = SE.getTypeSizeInBits(Expr->getType());
234   ConstantRange AccessStartRange = SE.getUnsignedRange(Expr);
235   ConstantRange SizeRange =
236       ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, AccessSize));
237   ConstantRange AccessRange = AccessStartRange.add(SizeRange);
238   ConstantRange AllocaRange =
239       ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, AllocaSize));
240   bool Safe = AllocaRange.contains(AccessRange);
241 
242   LLVM_DEBUG(
243       dbgs() << "[SafeStack] "
244              << (isa<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
245              << *AllocaPtr << "\n"
246              << "            Access " << *Addr << "\n"
247              << "            SCEV " << *Expr
248              << " U: " << SE.getUnsignedRange(Expr)
249              << ", S: " << SE.getSignedRange(Expr) << "\n"
250              << "            Range " << AccessRange << "\n"
251              << "            AllocaRange " << AllocaRange << "\n"
252              << "            " << (Safe ? "safe" : "unsafe") << "\n");
253 
254   return Safe;
255 }
256 
257 bool SafeStack::IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U,
258                                    const Value *AllocaPtr,
259                                    uint64_t AllocaSize) {
260   if (auto MTI = dyn_cast<MemTransferInst>(MI)) {
261     if (MTI->getRawSource() != U && MTI->getRawDest() != U)
262       return true;
263   } else {
264     if (MI->getRawDest() != U)
265       return true;
266   }
267 
268   const auto *Len = dyn_cast<ConstantInt>(MI->getLength());
269   // Non-constant size => unsafe. FIXME: try SCEV getRange.
270   if (!Len) return false;
271   return IsAccessSafe(U, Len->getZExtValue(), AllocaPtr, AllocaSize);
272 }
273 
274 /// Check whether a given allocation must be put on the safe
275 /// stack or not. The function analyzes all uses of AI and checks whether it is
276 /// only accessed in a memory safe way (as decided statically).
277 bool SafeStack::IsSafeStackAlloca(const Value *AllocaPtr, uint64_t AllocaSize) {
278   // Go through all uses of this alloca and check whether all accesses to the
279   // allocated object are statically known to be memory safe and, hence, the
280   // object can be placed on the safe stack.
281   SmallPtrSet<const Value *, 16> Visited;
282   SmallVector<const Value *, 8> WorkList;
283   WorkList.push_back(AllocaPtr);
284 
285   // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc.
286   while (!WorkList.empty()) {
287     const Value *V = WorkList.pop_back_val();
288     for (const Use &UI : V->uses()) {
289       auto I = cast<const Instruction>(UI.getUser());
290       assert(V == UI.get());
291 
292       switch (I->getOpcode()) {
293       case Instruction::Load:
294         if (!IsAccessSafe(UI, DL.getTypeStoreSize(I->getType()), AllocaPtr,
295                           AllocaSize))
296           return false;
297         break;
298 
299       case Instruction::VAArg:
300         // "va-arg" from a pointer is safe.
301         break;
302       case Instruction::Store:
303         if (V == I->getOperand(0)) {
304           // Stored the pointer - conservatively assume it may be unsafe.
305           LLVM_DEBUG(dbgs()
306                      << "[SafeStack] Unsafe alloca: " << *AllocaPtr
307                      << "\n            store of address: " << *I << "\n");
308           return false;
309         }
310 
311         if (!IsAccessSafe(UI, DL.getTypeStoreSize(I->getOperand(0)->getType()),
312                           AllocaPtr, AllocaSize))
313           return false;
314         break;
315 
316       case Instruction::Ret:
317         // Information leak.
318         return false;
319 
320       case Instruction::Call:
321       case Instruction::Invoke: {
322         const CallBase &CS = *cast<CallBase>(I);
323 
324         if (I->isLifetimeStartOrEnd())
325           continue;
326 
327         if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
328           if (!IsMemIntrinsicSafe(MI, UI, AllocaPtr, AllocaSize)) {
329             LLVM_DEBUG(dbgs()
330                        << "[SafeStack] Unsafe alloca: " << *AllocaPtr
331                        << "\n            unsafe memintrinsic: " << *I << "\n");
332             return false;
333           }
334           continue;
335         }
336 
337         // LLVM 'nocapture' attribute is only set for arguments whose address
338         // is not stored, passed around, or used in any other non-trivial way.
339         // We assume that passing a pointer to an object as a 'nocapture
340         // readnone' argument is safe.
341         // FIXME: a more precise solution would require an interprocedural
342         // analysis here, which would look at all uses of an argument inside
343         // the function being called.
344         auto B = CS.arg_begin(), E = CS.arg_end();
345         for (const auto *A = B; A != E; ++A)
346           if (A->get() == V)
347             if (!(CS.doesNotCapture(A - B) && (CS.doesNotAccessMemory(A - B) ||
348                                                CS.doesNotAccessMemory()))) {
349               LLVM_DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AllocaPtr
350                                 << "\n            unsafe call: " << *I << "\n");
351               return false;
352             }
353         continue;
354       }
355 
356       default:
357         if (Visited.insert(I).second)
358           WorkList.push_back(cast<const Instruction>(I));
359       }
360     }
361   }
362 
363   // All uses of the alloca are safe, we can place it on the safe stack.
364   return true;
365 }
366 
367 Value *SafeStack::getStackGuard(IRBuilder<> &IRB, Function &F) {
368   Value *StackGuardVar = TL.getIRStackGuard(IRB);
369   Module *M = F.getParent();
370 
371   if (!StackGuardVar) {
372     TL.insertSSPDeclarations(*M);
373     return IRB.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackguard));
374   }
375 
376   return IRB.CreateLoad(StackPtrTy, StackGuardVar, "StackGuard");
377 }
378 
379 void SafeStack::findInsts(Function &F,
380                           SmallVectorImpl<AllocaInst *> &StaticAllocas,
381                           SmallVectorImpl<AllocaInst *> &DynamicAllocas,
382                           SmallVectorImpl<Argument *> &ByValArguments,
383                           SmallVectorImpl<Instruction *> &Returns,
384                           SmallVectorImpl<Instruction *> &StackRestorePoints) {
385   for (Instruction &I : instructions(&F)) {
386     if (auto AI = dyn_cast<AllocaInst>(&I)) {
387       ++NumAllocas;
388 
389       uint64_t Size = getStaticAllocaAllocationSize(AI);
390       if (IsSafeStackAlloca(AI, Size))
391         continue;
392 
393       if (AI->isStaticAlloca()) {
394         ++NumUnsafeStaticAllocas;
395         StaticAllocas.push_back(AI);
396       } else {
397         ++NumUnsafeDynamicAllocas;
398         DynamicAllocas.push_back(AI);
399       }
400     } else if (auto RI = dyn_cast<ReturnInst>(&I)) {
401       if (CallInst *CI = I.getParent()->getTerminatingMustTailCall())
402         Returns.push_back(CI);
403       else
404         Returns.push_back(RI);
405     } else if (auto CI = dyn_cast<CallInst>(&I)) {
406       // setjmps require stack restore.
407       if (CI->getCalledFunction() && CI->canReturnTwice())
408         StackRestorePoints.push_back(CI);
409     } else if (auto LP = dyn_cast<LandingPadInst>(&I)) {
410       // Exception landing pads require stack restore.
411       StackRestorePoints.push_back(LP);
412     } else if (auto II = dyn_cast<IntrinsicInst>(&I)) {
413       if (II->getIntrinsicID() == Intrinsic::gcroot)
414         report_fatal_error(
415             "gcroot intrinsic not compatible with safestack attribute");
416     }
417   }
418   for (Argument &Arg : F.args()) {
419     if (!Arg.hasByValAttr())
420       continue;
421     uint64_t Size = DL.getTypeStoreSize(Arg.getParamByValType());
422     if (IsSafeStackAlloca(&Arg, Size))
423       continue;
424 
425     ++NumUnsafeByValArguments;
426     ByValArguments.push_back(&Arg);
427   }
428 }
429 
430 AllocaInst *
431 SafeStack::createStackRestorePoints(IRBuilder<> &IRB, Function &F,
432                                     ArrayRef<Instruction *> StackRestorePoints,
433                                     Value *StaticTop, bool NeedDynamicTop) {
434   assert(StaticTop && "The stack top isn't set.");
435 
436   if (StackRestorePoints.empty())
437     return nullptr;
438 
439   // We need the current value of the shadow stack pointer to restore
440   // after longjmp or exception catching.
441 
442   // FIXME: On some platforms this could be handled by the longjmp/exception
443   // runtime itself.
444 
445   AllocaInst *DynamicTop = nullptr;
446   if (NeedDynamicTop) {
447     // If we also have dynamic alloca's, the stack pointer value changes
448     // throughout the function. For now we store it in an alloca.
449     DynamicTop = IRB.CreateAlloca(StackPtrTy, /*ArraySize=*/nullptr,
450                                   "unsafe_stack_dynamic_ptr");
451     IRB.CreateStore(StaticTop, DynamicTop);
452   }
453 
454   // Restore current stack pointer after longjmp/exception catch.
455   for (Instruction *I : StackRestorePoints) {
456     ++NumUnsafeStackRestorePoints;
457 
458     IRB.SetInsertPoint(I->getNextNode());
459     Value *CurrentTop =
460         DynamicTop ? IRB.CreateLoad(StackPtrTy, DynamicTop) : StaticTop;
461     IRB.CreateStore(CurrentTop, UnsafeStackPtr);
462   }
463 
464   return DynamicTop;
465 }
466 
467 void SafeStack::checkStackGuard(IRBuilder<> &IRB, Function &F, Instruction &RI,
468                                 AllocaInst *StackGuardSlot, Value *StackGuard) {
469   Value *V = IRB.CreateLoad(StackPtrTy, StackGuardSlot);
470   Value *Cmp = IRB.CreateICmpNE(StackGuard, V);
471 
472   auto SuccessProb = BranchProbabilityInfo::getBranchProbStackProtector(true);
473   auto FailureProb = BranchProbabilityInfo::getBranchProbStackProtector(false);
474   MDNode *Weights = MDBuilder(F.getContext())
475                         .createBranchWeights(SuccessProb.getNumerator(),
476                                              FailureProb.getNumerator());
477   Instruction *CheckTerm =
478       SplitBlockAndInsertIfThen(Cmp, &RI, /* Unreachable */ true, Weights, DTU);
479   IRBuilder<> IRBFail(CheckTerm);
480   // FIXME: respect -fsanitize-trap / -ftrap-function here?
481   FunctionCallee StackChkFail =
482       F.getParent()->getOrInsertFunction("__stack_chk_fail", IRB.getVoidTy());
483   IRBFail.CreateCall(StackChkFail, {});
484 }
485 
486 /// We explicitly compute and set the unsafe stack layout for all unsafe
487 /// static alloca instructions. We save the unsafe "base pointer" in the
488 /// prologue into a local variable and restore it in the epilogue.
489 Value *SafeStack::moveStaticAllocasToUnsafeStack(
490     IRBuilder<> &IRB, Function &F, ArrayRef<AllocaInst *> StaticAllocas,
491     ArrayRef<Argument *> ByValArguments, Instruction *BasePointer,
492     AllocaInst *StackGuardSlot) {
493   if (StaticAllocas.empty() && ByValArguments.empty())
494     return BasePointer;
495 
496   DIBuilder DIB(*F.getParent());
497 
498   StackLifetime SSC(F, StaticAllocas, StackLifetime::LivenessType::May);
499   static const StackLifetime::LiveRange NoColoringRange(1, true);
500   if (ClColoring)
501     SSC.run();
502 
503   for (const auto *I : SSC.getMarkers()) {
504     auto *Op = dyn_cast<Instruction>(I->getOperand(1));
505     const_cast<IntrinsicInst *>(I)->eraseFromParent();
506     // Remove the operand bitcast, too, if it has no more uses left.
507     if (Op && Op->use_empty())
508       Op->eraseFromParent();
509   }
510 
511   // Unsafe stack always grows down.
512   StackLayout SSL(StackAlignment);
513   if (StackGuardSlot) {
514     Type *Ty = StackGuardSlot->getAllocatedType();
515     Align Align = std::max(DL.getPrefTypeAlign(Ty), StackGuardSlot->getAlign());
516     SSL.addObject(StackGuardSlot, getStaticAllocaAllocationSize(StackGuardSlot),
517                   Align, SSC.getFullLiveRange());
518   }
519 
520   for (Argument *Arg : ByValArguments) {
521     Type *Ty = Arg->getParamByValType();
522     uint64_t Size = DL.getTypeStoreSize(Ty);
523     if (Size == 0)
524       Size = 1; // Don't create zero-sized stack objects.
525 
526     // Ensure the object is properly aligned.
527     Align Align = DL.getPrefTypeAlign(Ty);
528     if (auto A = Arg->getParamAlign())
529       Align = std::max(Align, *A);
530     SSL.addObject(Arg, Size, Align, SSC.getFullLiveRange());
531   }
532 
533   for (AllocaInst *AI : StaticAllocas) {
534     Type *Ty = AI->getAllocatedType();
535     uint64_t Size = getStaticAllocaAllocationSize(AI);
536     if (Size == 0)
537       Size = 1; // Don't create zero-sized stack objects.
538 
539     // Ensure the object is properly aligned.
540     Align Align = std::max(DL.getPrefTypeAlign(Ty), AI->getAlign());
541 
542     SSL.addObject(AI, Size, Align,
543                   ClColoring ? SSC.getLiveRange(AI) : NoColoringRange);
544   }
545 
546   SSL.computeLayout();
547   Align FrameAlignment = SSL.getFrameAlignment();
548 
549   // FIXME: tell SSL that we start at a less-then-MaxAlignment aligned location
550   // (AlignmentSkew).
551   if (FrameAlignment > StackAlignment) {
552     // Re-align the base pointer according to the max requested alignment.
553     IRB.SetInsertPoint(BasePointer->getNextNode());
554     BasePointer = cast<Instruction>(IRB.CreateIntToPtr(
555         IRB.CreateAnd(
556             IRB.CreatePtrToInt(BasePointer, IntPtrTy),
557             ConstantInt::get(IntPtrTy, ~(FrameAlignment.value() - 1))),
558         StackPtrTy));
559   }
560 
561   IRB.SetInsertPoint(BasePointer->getNextNode());
562 
563   if (StackGuardSlot) {
564     unsigned Offset = SSL.getObjectOffset(StackGuardSlot);
565     Value *Off = IRB.CreateGEP(Int8Ty, BasePointer, // BasePointer is i8*
566                                ConstantInt::get(Int32Ty, -Offset));
567     Value *NewAI =
568         IRB.CreateBitCast(Off, StackGuardSlot->getType(), "StackGuardSlot");
569 
570     // Replace alloc with the new location.
571     StackGuardSlot->replaceAllUsesWith(NewAI);
572     StackGuardSlot->eraseFromParent();
573   }
574 
575   for (Argument *Arg : ByValArguments) {
576     unsigned Offset = SSL.getObjectOffset(Arg);
577     MaybeAlign Align(SSL.getObjectAlignment(Arg));
578     Type *Ty = Arg->getParamByValType();
579 
580     uint64_t Size = DL.getTypeStoreSize(Ty);
581     if (Size == 0)
582       Size = 1; // Don't create zero-sized stack objects.
583 
584     Value *Off = IRB.CreateGEP(Int8Ty, BasePointer, // BasePointer is i8*
585                                ConstantInt::get(Int32Ty, -Offset));
586     Value *NewArg = IRB.CreateBitCast(Off, Arg->getType(),
587                                      Arg->getName() + ".unsafe-byval");
588 
589     // Replace alloc with the new location.
590     replaceDbgDeclare(Arg, BasePointer, DIB, DIExpression::ApplyOffset,
591                       -Offset);
592     Arg->replaceAllUsesWith(NewArg);
593     IRB.SetInsertPoint(cast<Instruction>(NewArg)->getNextNode());
594     IRB.CreateMemCpy(Off, Align, Arg, Arg->getParamAlign(), Size);
595   }
596 
597   // Allocate space for every unsafe static AllocaInst on the unsafe stack.
598   for (AllocaInst *AI : StaticAllocas) {
599     IRB.SetInsertPoint(AI);
600     unsigned Offset = SSL.getObjectOffset(AI);
601 
602     replaceDbgDeclare(AI, BasePointer, DIB, DIExpression::ApplyOffset, -Offset);
603     replaceDbgValueForAlloca(AI, BasePointer, DIB, -Offset);
604 
605     // Replace uses of the alloca with the new location.
606     // Insert address calculation close to each use to work around PR27844.
607     std::string Name = std::string(AI->getName()) + ".unsafe";
608     while (!AI->use_empty()) {
609       Use &U = *AI->use_begin();
610       Instruction *User = cast<Instruction>(U.getUser());
611 
612       Instruction *InsertBefore;
613       if (auto *PHI = dyn_cast<PHINode>(User))
614         InsertBefore = PHI->getIncomingBlock(U)->getTerminator();
615       else
616         InsertBefore = User;
617 
618       IRBuilder<> IRBUser(InsertBefore);
619       Value *Off = IRBUser.CreateGEP(Int8Ty, BasePointer, // BasePointer is i8*
620                                      ConstantInt::get(Int32Ty, -Offset));
621       Value *Replacement = IRBUser.CreateBitCast(Off, AI->getType(), Name);
622 
623       if (auto *PHI = dyn_cast<PHINode>(User))
624         // PHI nodes may have multiple incoming edges from the same BB (why??),
625         // all must be updated at once with the same incoming value.
626         PHI->setIncomingValueForBlock(PHI->getIncomingBlock(U), Replacement);
627       else
628         U.set(Replacement);
629     }
630 
631     AI->eraseFromParent();
632   }
633 
634   // Re-align BasePointer so that our callees would see it aligned as
635   // expected.
636   // FIXME: no need to update BasePointer in leaf functions.
637   unsigned FrameSize = alignTo(SSL.getFrameSize(), StackAlignment);
638 
639   MDBuilder MDB(F.getContext());
640   SmallVector<Metadata *, 2> Data;
641   Data.push_back(MDB.createString("unsafe-stack-size"));
642   Data.push_back(MDB.createConstant(ConstantInt::get(Int32Ty, FrameSize)));
643   MDNode *MD = MDTuple::get(F.getContext(), Data);
644   F.setMetadata(LLVMContext::MD_annotation, MD);
645 
646   // Update shadow stack pointer in the function epilogue.
647   IRB.SetInsertPoint(BasePointer->getNextNode());
648 
649   Value *StaticTop =
650       IRB.CreateGEP(Int8Ty, BasePointer, ConstantInt::get(Int32Ty, -FrameSize),
651                     "unsafe_stack_static_top");
652   IRB.CreateStore(StaticTop, UnsafeStackPtr);
653   return StaticTop;
654 }
655 
656 void SafeStack::moveDynamicAllocasToUnsafeStack(
657     Function &F, Value *UnsafeStackPtr, AllocaInst *DynamicTop,
658     ArrayRef<AllocaInst *> DynamicAllocas) {
659   DIBuilder DIB(*F.getParent());
660 
661   for (AllocaInst *AI : DynamicAllocas) {
662     IRBuilder<> IRB(AI);
663 
664     // Compute the new SP value (after AI).
665     Value *ArraySize = AI->getArraySize();
666     if (ArraySize->getType() != IntPtrTy)
667       ArraySize = IRB.CreateIntCast(ArraySize, IntPtrTy, false);
668 
669     Type *Ty = AI->getAllocatedType();
670     uint64_t TySize = DL.getTypeAllocSize(Ty);
671     Value *Size = IRB.CreateMul(ArraySize, ConstantInt::get(IntPtrTy, TySize));
672 
673     Value *SP = IRB.CreatePtrToInt(IRB.CreateLoad(StackPtrTy, UnsafeStackPtr),
674                                    IntPtrTy);
675     SP = IRB.CreateSub(SP, Size);
676 
677     // Align the SP value to satisfy the AllocaInst, type and stack alignments.
678     auto Align = std::max(std::max(DL.getPrefTypeAlign(Ty), AI->getAlign()),
679                           StackAlignment);
680 
681     Value *NewTop = IRB.CreateIntToPtr(
682         IRB.CreateAnd(SP,
683                       ConstantInt::get(IntPtrTy, ~uint64_t(Align.value() - 1))),
684         StackPtrTy);
685 
686     // Save the stack pointer.
687     IRB.CreateStore(NewTop, UnsafeStackPtr);
688     if (DynamicTop)
689       IRB.CreateStore(NewTop, DynamicTop);
690 
691     Value *NewAI = IRB.CreatePointerCast(NewTop, AI->getType());
692     if (AI->hasName() && isa<Instruction>(NewAI))
693       NewAI->takeName(AI);
694 
695     replaceDbgDeclare(AI, NewAI, DIB, DIExpression::ApplyOffset, 0);
696     AI->replaceAllUsesWith(NewAI);
697     AI->eraseFromParent();
698   }
699 
700   if (!DynamicAllocas.empty()) {
701     // Now go through the instructions again, replacing stacksave/stackrestore.
702     for (Instruction &I : llvm::make_early_inc_range(instructions(&F))) {
703       auto *II = dyn_cast<IntrinsicInst>(&I);
704       if (!II)
705         continue;
706 
707       if (II->getIntrinsicID() == Intrinsic::stacksave) {
708         IRBuilder<> IRB(II);
709         Instruction *LI = IRB.CreateLoad(StackPtrTy, UnsafeStackPtr);
710         LI->takeName(II);
711         II->replaceAllUsesWith(LI);
712         II->eraseFromParent();
713       } else if (II->getIntrinsicID() == Intrinsic::stackrestore) {
714         IRBuilder<> IRB(II);
715         Instruction *SI = IRB.CreateStore(II->getArgOperand(0), UnsafeStackPtr);
716         SI->takeName(II);
717         assert(II->use_empty());
718         II->eraseFromParent();
719       }
720     }
721   }
722 }
723 
724 bool SafeStack::ShouldInlinePointerAddress(CallInst &CI) {
725   Function *Callee = CI.getCalledFunction();
726   if (CI.hasFnAttr(Attribute::AlwaysInline) &&
727       isInlineViable(*Callee).isSuccess())
728     return true;
729   if (Callee->isInterposable() || Callee->hasFnAttribute(Attribute::NoInline) ||
730       CI.isNoInline())
731     return false;
732   return true;
733 }
734 
735 void SafeStack::TryInlinePointerAddress() {
736   auto *CI = dyn_cast<CallInst>(UnsafeStackPtr);
737   if (!CI)
738     return;
739 
740   if(F.hasOptNone())
741     return;
742 
743   Function *Callee = CI->getCalledFunction();
744   if (!Callee || Callee->isDeclaration())
745     return;
746 
747   if (!ShouldInlinePointerAddress(*CI))
748     return;
749 
750   InlineFunctionInfo IFI;
751   InlineFunction(*CI, IFI);
752 }
753 
754 bool SafeStack::run() {
755   assert(F.hasFnAttribute(Attribute::SafeStack) &&
756          "Can't run SafeStack on a function without the attribute");
757   assert(!F.isDeclaration() && "Can't run SafeStack on a function declaration");
758 
759   ++NumFunctions;
760 
761   SmallVector<AllocaInst *, 16> StaticAllocas;
762   SmallVector<AllocaInst *, 4> DynamicAllocas;
763   SmallVector<Argument *, 4> ByValArguments;
764   SmallVector<Instruction *, 4> Returns;
765 
766   // Collect all points where stack gets unwound and needs to be restored
767   // This is only necessary because the runtime (setjmp and unwind code) is
768   // not aware of the unsafe stack and won't unwind/restore it properly.
769   // To work around this problem without changing the runtime, we insert
770   // instrumentation to restore the unsafe stack pointer when necessary.
771   SmallVector<Instruction *, 4> StackRestorePoints;
772 
773   // Find all static and dynamic alloca instructions that must be moved to the
774   // unsafe stack, all return instructions and stack restore points.
775   findInsts(F, StaticAllocas, DynamicAllocas, ByValArguments, Returns,
776             StackRestorePoints);
777 
778   if (StaticAllocas.empty() && DynamicAllocas.empty() &&
779       ByValArguments.empty() && StackRestorePoints.empty())
780     return false; // Nothing to do in this function.
781 
782   if (!StaticAllocas.empty() || !DynamicAllocas.empty() ||
783       !ByValArguments.empty())
784     ++NumUnsafeStackFunctions; // This function has the unsafe stack.
785 
786   if (!StackRestorePoints.empty())
787     ++NumUnsafeStackRestorePointsFunctions;
788 
789   IRBuilder<> IRB(&F.front(), F.begin()->getFirstInsertionPt());
790   // Calls must always have a debug location, or else inlining breaks. So
791   // we explicitly set a artificial debug location here.
792   if (DISubprogram *SP = F.getSubprogram())
793     IRB.SetCurrentDebugLocation(
794         DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP));
795   if (SafeStackUsePointerAddress) {
796     FunctionCallee Fn = F.getParent()->getOrInsertFunction(
797         "__safestack_pointer_address", IRB.getPtrTy(0));
798     UnsafeStackPtr = IRB.CreateCall(Fn);
799   } else {
800     UnsafeStackPtr = TL.getSafeStackPointerLocation(IRB);
801   }
802 
803   // Load the current stack pointer (we'll also use it as a base pointer).
804   // FIXME: use a dedicated register for it ?
805   Instruction *BasePointer =
806       IRB.CreateLoad(StackPtrTy, UnsafeStackPtr, false, "unsafe_stack_ptr");
807   assert(BasePointer->getType() == StackPtrTy);
808 
809   AllocaInst *StackGuardSlot = nullptr;
810   // FIXME: implement weaker forms of stack protector.
811   if (F.hasFnAttribute(Attribute::StackProtect) ||
812       F.hasFnAttribute(Attribute::StackProtectStrong) ||
813       F.hasFnAttribute(Attribute::StackProtectReq)) {
814     Value *StackGuard = getStackGuard(IRB, F);
815     StackGuardSlot = IRB.CreateAlloca(StackPtrTy, nullptr);
816     IRB.CreateStore(StackGuard, StackGuardSlot);
817 
818     for (Instruction *RI : Returns) {
819       IRBuilder<> IRBRet(RI);
820       checkStackGuard(IRBRet, F, *RI, StackGuardSlot, StackGuard);
821     }
822   }
823 
824   // The top of the unsafe stack after all unsafe static allocas are
825   // allocated.
826   Value *StaticTop = moveStaticAllocasToUnsafeStack(
827       IRB, F, StaticAllocas, ByValArguments, BasePointer, StackGuardSlot);
828 
829   // Safe stack object that stores the current unsafe stack top. It is updated
830   // as unsafe dynamic (non-constant-sized) allocas are allocated and freed.
831   // This is only needed if we need to restore stack pointer after longjmp
832   // or exceptions, and we have dynamic allocations.
833   // FIXME: a better alternative might be to store the unsafe stack pointer
834   // before setjmp / invoke instructions.
835   AllocaInst *DynamicTop = createStackRestorePoints(
836       IRB, F, StackRestorePoints, StaticTop, !DynamicAllocas.empty());
837 
838   // Handle dynamic allocas.
839   moveDynamicAllocasToUnsafeStack(F, UnsafeStackPtr, DynamicTop,
840                                   DynamicAllocas);
841 
842   // Restore the unsafe stack pointer before each return.
843   for (Instruction *RI : Returns) {
844     IRB.SetInsertPoint(RI);
845     IRB.CreateStore(BasePointer, UnsafeStackPtr);
846   }
847 
848   TryInlinePointerAddress();
849 
850   LLVM_DEBUG(dbgs() << "[SafeStack]     safestack applied\n");
851   return true;
852 }
853 
854 class SafeStackLegacyPass : public FunctionPass {
855   const TargetMachine *TM = nullptr;
856 
857 public:
858   static char ID; // Pass identification, replacement for typeid..
859 
860   SafeStackLegacyPass() : FunctionPass(ID) {
861     initializeSafeStackLegacyPassPass(*PassRegistry::getPassRegistry());
862   }
863 
864   void getAnalysisUsage(AnalysisUsage &AU) const override {
865     AU.addRequired<TargetPassConfig>();
866     AU.addRequired<TargetLibraryInfoWrapperPass>();
867     AU.addRequired<AssumptionCacheTracker>();
868     AU.addPreserved<DominatorTreeWrapperPass>();
869   }
870 
871   bool runOnFunction(Function &F) override {
872     LLVM_DEBUG(dbgs() << "[SafeStack] Function: " << F.getName() << "\n");
873 
874     if (!F.hasFnAttribute(Attribute::SafeStack)) {
875       LLVM_DEBUG(dbgs() << "[SafeStack]     safestack is not requested"
876                            " for this function\n");
877       return false;
878     }
879 
880     if (F.isDeclaration()) {
881       LLVM_DEBUG(dbgs() << "[SafeStack]     function definition"
882                            " is not available\n");
883       return false;
884     }
885 
886     TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
887     auto *TL = TM->getSubtargetImpl(F)->getTargetLowering();
888     if (!TL)
889       report_fatal_error("TargetLowering instance is required");
890 
891     auto *DL = &F.getParent()->getDataLayout();
892     auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
893     auto &ACT = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
894 
895     // Compute DT and LI only for functions that have the attribute.
896     // This is only useful because the legacy pass manager doesn't let us
897     // compute analyzes lazily.
898 
899     DominatorTree *DT;
900     bool ShouldPreserveDominatorTree;
901     std::optional<DominatorTree> LazilyComputedDomTree;
902 
903     // Do we already have a DominatorTree avaliable from the previous pass?
904     // Note that we should *NOT* require it, to avoid the case where we end up
905     // not needing it, but the legacy PM would have computed it for us anyways.
906     if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>()) {
907       DT = &DTWP->getDomTree();
908       ShouldPreserveDominatorTree = true;
909     } else {
910       // Otherwise, we need to compute it.
911       LazilyComputedDomTree.emplace(F);
912       DT = &*LazilyComputedDomTree;
913       ShouldPreserveDominatorTree = false;
914     }
915 
916     // Likewise, lazily compute loop info.
917     LoopInfo LI(*DT);
918 
919     DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
920 
921     ScalarEvolution SE(F, TLI, ACT, *DT, LI);
922 
923     return SafeStack(F, *TL, *DL, ShouldPreserveDominatorTree ? &DTU : nullptr,
924                      SE)
925         .run();
926   }
927 };
928 
929 } // end anonymous namespace
930 
931 PreservedAnalyses SafeStackPass::run(Function &F,
932                                      FunctionAnalysisManager &FAM) {
933   LLVM_DEBUG(dbgs() << "[SafeStack] Function: " << F.getName() << "\n");
934 
935   if (!F.hasFnAttribute(Attribute::SafeStack)) {
936     LLVM_DEBUG(dbgs() << "[SafeStack]     safestack is not requested"
937                          " for this function\n");
938     return PreservedAnalyses::all();
939   }
940 
941   if (F.isDeclaration()) {
942     LLVM_DEBUG(dbgs() << "[SafeStack]     function definition"
943                          " is not available\n");
944     return PreservedAnalyses::all();
945   }
946 
947   auto *TL = TM->getSubtargetImpl(F)->getTargetLowering();
948   if (!TL)
949     report_fatal_error("TargetLowering instance is required");
950 
951   auto &DL = F.getParent()->getDataLayout();
952 
953   // preserve DominatorTree
954   auto &DT = FAM.getResult<DominatorTreeAnalysis>(F);
955   auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F);
956   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
957 
958   bool Changed = SafeStack(F, *TL, DL, &DTU, SE).run();
959 
960   if (!Changed)
961     return PreservedAnalyses::all();
962   PreservedAnalyses PA;
963   PA.preserve<DominatorTreeAnalysis>();
964   return PA;
965 }
966 
967 char SafeStackLegacyPass::ID = 0;
968 
969 INITIALIZE_PASS_BEGIN(SafeStackLegacyPass, DEBUG_TYPE,
970                       "Safe Stack instrumentation pass", false, false)
971 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
972 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
973 INITIALIZE_PASS_END(SafeStackLegacyPass, DEBUG_TYPE,
974                     "Safe Stack instrumentation pass", false, false)
975 
976 FunctionPass *llvm::createSafeStackPass() { return new SafeStackLegacyPass(); }
977