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