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