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