Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 1 | //===-- SafeStack.cpp - Safe Stack Insertion ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass splits the stack into the safe stack (kept as-is for LLVM backend) |
| 11 | // and the unsafe stack (explicitly allocated and managed through the runtime |
| 12 | // support library). |
| 13 | // |
| 14 | // http://clang.llvm.org/docs/SafeStack.html |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "llvm/Transforms/Instrumentation.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
| 20 | #include "llvm/ADT/Triple.h" |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 21 | #include "llvm/Analysis/ScalarEvolution.h" |
| 22 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/Passes.h" |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/DataLayout.h" |
| 26 | #include "llvm/IR/DerivedTypes.h" |
| 27 | #include "llvm/IR/DIBuilder.h" |
| 28 | #include "llvm/IR/Function.h" |
| 29 | #include "llvm/IR/InstIterator.h" |
| 30 | #include "llvm/IR/Instructions.h" |
| 31 | #include "llvm/IR/IntrinsicInst.h" |
| 32 | #include "llvm/IR/Intrinsics.h" |
| 33 | #include "llvm/IR/IRBuilder.h" |
| 34 | #include "llvm/IR/Module.h" |
| 35 | #include "llvm/Pass.h" |
| 36 | #include "llvm/Support/CommandLine.h" |
| 37 | #include "llvm/Support/Debug.h" |
| 38 | #include "llvm/Support/Format.h" |
| 39 | #include "llvm/Support/MathExtras.h" |
| 40 | #include "llvm/Support/raw_os_ostream.h" |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetLowering.h" |
| 42 | #include "llvm/Target/TargetSubtargetInfo.h" |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 43 | #include "llvm/Transforms/Utils/Local.h" |
| 44 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
| 45 | |
| 46 | using namespace llvm; |
| 47 | |
| 48 | #define DEBUG_TYPE "safestack" |
| 49 | |
| 50 | namespace llvm { |
| 51 | |
| 52 | STATISTIC(NumFunctions, "Total number of functions"); |
| 53 | STATISTIC(NumUnsafeStackFunctions, "Number of functions with unsafe stack"); |
| 54 | STATISTIC(NumUnsafeStackRestorePointsFunctions, |
| 55 | "Number of functions that use setjmp or exceptions"); |
| 56 | |
| 57 | STATISTIC(NumAllocas, "Total number of allocas"); |
| 58 | STATISTIC(NumUnsafeStaticAllocas, "Number of unsafe static allocas"); |
| 59 | STATISTIC(NumUnsafeDynamicAllocas, "Number of unsafe dynamic allocas"); |
| 60 | STATISTIC(NumUnsafeStackRestorePoints, "Number of setjmps and landingpads"); |
| 61 | |
| 62 | } // namespace llvm |
| 63 | |
| 64 | namespace { |
| 65 | |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 66 | /// Rewrite an SCEV expression for a memory access address to an expression that |
| 67 | /// represents offset from the given alloca. |
| 68 | /// |
| 69 | /// The implementation simply replaces all mentions of the alloca with zero. |
| 70 | class AllocaOffsetRewriter : public SCEVRewriteVisitor<AllocaOffsetRewriter> { |
| 71 | const AllocaInst *AI; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 72 | |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 73 | public: |
| 74 | AllocaOffsetRewriter(ScalarEvolution &SE, const AllocaInst *AI) |
| 75 | : SCEVRewriteVisitor(SE), AI(AI) {} |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 76 | |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 77 | const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
| 78 | if (Expr->getValue() == AI) |
| 79 | return SE.getZero(Expr->getType()); |
| 80 | return Expr; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 81 | } |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 82 | }; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 83 | |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 84 | /// The SafeStack pass splits the stack of each function into the safe |
| 85 | /// stack, which is only accessed through memory safe dereferences (as |
| 86 | /// determined statically), and the unsafe stack, which contains all |
| 87 | /// local variables that are accessed in ways that we can't prove to |
| 88 | /// be safe. |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 89 | class SafeStack : public FunctionPass { |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 90 | const TargetMachine *TM; |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 91 | const TargetLoweringBase *TL; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 92 | const DataLayout *DL; |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 93 | ScalarEvolution *SE; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 94 | |
| 95 | Type *StackPtrTy; |
| 96 | Type *IntPtrTy; |
| 97 | Type *Int32Ty; |
| 98 | Type *Int8Ty; |
| 99 | |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 100 | Value *UnsafeStackPtr = nullptr; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 101 | |
| 102 | /// Unsafe stack alignment. Each stack frame must ensure that the stack is |
| 103 | /// aligned to this value. We need to re-align the unsafe stack if the |
| 104 | /// alignment of any object on the stack exceeds this value. |
| 105 | /// |
| 106 | /// 16 seems like a reasonable upper bound on the alignment of objects that we |
| 107 | /// might expect to appear on the stack on most common targets. |
| 108 | enum { StackAlignment = 16 }; |
| 109 | |
Evgeniy Stepanov | d1aad26 | 2015-10-26 18:28:25 +0000 | [diff] [blame] | 110 | /// \brief Build a value representing a pointer to the unsafe stack pointer. |
Evgeniy Stepanov | 9addbc9 | 2015-10-15 21:26:49 +0000 | [diff] [blame] | 111 | Value *getOrCreateUnsafeStackPtr(IRBuilder<> &IRB, Function &F); |
| 112 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 113 | /// \brief Find all static allocas, dynamic allocas, return instructions and |
| 114 | /// stack restore points (exception unwind blocks and setjmp calls) in the |
| 115 | /// given function and append them to the respective vectors. |
| 116 | void findInsts(Function &F, SmallVectorImpl<AllocaInst *> &StaticAllocas, |
| 117 | SmallVectorImpl<AllocaInst *> &DynamicAllocas, |
| 118 | SmallVectorImpl<ReturnInst *> &Returns, |
| 119 | SmallVectorImpl<Instruction *> &StackRestorePoints); |
| 120 | |
| 121 | /// \brief Allocate space for all static allocas in \p StaticAllocas, |
| 122 | /// replace allocas with pointers into the unsafe stack and generate code to |
| 123 | /// restore the stack pointer before all return instructions in \p Returns. |
| 124 | /// |
| 125 | /// \returns A pointer to the top of the unsafe stack after all unsafe static |
| 126 | /// allocas are allocated. |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 127 | Value *moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F, |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 128 | ArrayRef<AllocaInst *> StaticAllocas, |
| 129 | ArrayRef<ReturnInst *> Returns); |
| 130 | |
| 131 | /// \brief Generate code to restore the stack after all stack restore points |
| 132 | /// in \p StackRestorePoints. |
| 133 | /// |
| 134 | /// \returns A local variable in which to maintain the dynamic top of the |
| 135 | /// unsafe stack if needed. |
| 136 | AllocaInst * |
Evgeniy Stepanov | 8685daf | 2015-09-24 01:23:51 +0000 | [diff] [blame] | 137 | createStackRestorePoints(IRBuilder<> &IRB, Function &F, |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 138 | ArrayRef<Instruction *> StackRestorePoints, |
| 139 | Value *StaticTop, bool NeedDynamicTop); |
| 140 | |
| 141 | /// \brief Replace all allocas in \p DynamicAllocas with code to allocate |
| 142 | /// space dynamically on the unsafe stack and store the dynamic unsafe stack |
| 143 | /// top to \p DynamicTop if non-null. |
| 144 | void moveDynamicAllocasToUnsafeStack(Function &F, Value *UnsafeStackPtr, |
| 145 | AllocaInst *DynamicTop, |
| 146 | ArrayRef<AllocaInst *> DynamicAllocas); |
| 147 | |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 148 | bool IsSafeStackAlloca(const AllocaInst *AI); |
| 149 | |
| 150 | bool IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U, |
| 151 | const AllocaInst *AI); |
| 152 | bool IsAccessSafe(Value *Addr, uint64_t Size, const AllocaInst *AI); |
| 153 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 154 | public: |
| 155 | static char ID; // Pass identification, replacement for typeid. |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 156 | SafeStack(const TargetMachine *TM) |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 157 | : FunctionPass(ID), TM(TM), TL(nullptr), DL(nullptr) { |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 158 | initializeSafeStackPass(*PassRegistry::getPassRegistry()); |
| 159 | } |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 160 | SafeStack() : SafeStack(nullptr) {} |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 161 | |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 162 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 163 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 166 | bool doInitialization(Module &M) override { |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 167 | DL = &M.getDataLayout(); |
| 168 | |
| 169 | StackPtrTy = Type::getInt8PtrTy(M.getContext()); |
| 170 | IntPtrTy = DL->getIntPtrType(M.getContext()); |
| 171 | Int32Ty = Type::getInt32Ty(M.getContext()); |
| 172 | Int8Ty = Type::getInt8Ty(M.getContext()); |
| 173 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 174 | return false; |
| 175 | } |
| 176 | |
Hans Wennborg | aa15bff | 2015-09-10 16:49:58 +0000 | [diff] [blame] | 177 | bool runOnFunction(Function &F) override; |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 178 | }; // class SafeStack |
| 179 | |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 180 | bool SafeStack::IsAccessSafe(Value *Addr, uint64_t Size, const AllocaInst *AI) { |
| 181 | AllocaOffsetRewriter Rewriter(*SE, AI); |
| 182 | const SCEV *Expr = Rewriter.visit(SE->getSCEV(Addr)); |
| 183 | |
| 184 | uint64_t BitWidth = SE->getTypeSizeInBits(Expr->getType()); |
| 185 | ConstantRange AccessStartRange = SE->getUnsignedRange(Expr); |
| 186 | ConstantRange SizeRange = |
| 187 | ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, Size)); |
| 188 | ConstantRange AccessRange = AccessStartRange.add(SizeRange); |
| 189 | ConstantRange AllocaRange = ConstantRange( |
| 190 | APInt(BitWidth, 0), |
| 191 | APInt(BitWidth, DL->getTypeStoreSize(AI->getAllocatedType()))); |
| 192 | bool Safe = AllocaRange.contains(AccessRange); |
| 193 | |
| 194 | DEBUG(dbgs() << "[SafeStack] Alloca " << *AI << "\n" |
| 195 | << " Access " << *Addr << "\n" |
| 196 | << " SCEV " << *Expr |
| 197 | << " U: " << SE->getUnsignedRange(Expr) |
| 198 | << ", S: " << SE->getSignedRange(Expr) << "\n" |
| 199 | << " Range " << AccessRange << "\n" |
| 200 | << " AllocaRange " << AllocaRange << "\n" |
| 201 | << " " << (Safe ? "safe" : "unsafe") << "\n"); |
| 202 | |
| 203 | return Safe; |
| 204 | } |
| 205 | |
| 206 | bool SafeStack::IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U, |
| 207 | const AllocaInst *AI) { |
| 208 | // All MemIntrinsics have destination address in Arg0 and size in Arg2. |
| 209 | if (MI->getRawDest() != U) return true; |
| 210 | const auto *Len = dyn_cast<ConstantInt>(MI->getLength()); |
| 211 | // Non-constant size => unsafe. FIXME: try SCEV getRange. |
| 212 | if (!Len) return false; |
| 213 | return IsAccessSafe(U, Len->getZExtValue(), AI); |
| 214 | } |
| 215 | |
| 216 | /// Check whether a given alloca instruction (AI) should be put on the safe |
| 217 | /// stack or not. The function analyzes all uses of AI and checks whether it is |
| 218 | /// only accessed in a memory safe way (as decided statically). |
| 219 | bool SafeStack::IsSafeStackAlloca(const AllocaInst *AI) { |
| 220 | // Go through all uses of this alloca and check whether all accesses to the |
| 221 | // allocated object are statically known to be memory safe and, hence, the |
| 222 | // object can be placed on the safe stack. |
| 223 | SmallPtrSet<const Value *, 16> Visited; |
| 224 | SmallVector<const Instruction *, 8> WorkList; |
| 225 | WorkList.push_back(AI); |
| 226 | |
| 227 | // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc. |
| 228 | while (!WorkList.empty()) { |
| 229 | const Instruction *V = WorkList.pop_back_val(); |
| 230 | for (const Use &UI : V->uses()) { |
| 231 | auto I = cast<const Instruction>(UI.getUser()); |
| 232 | assert(V == UI.get()); |
| 233 | |
| 234 | switch (I->getOpcode()) { |
| 235 | case Instruction::Load: { |
| 236 | if (!IsAccessSafe(UI, DL->getTypeStoreSize(I->getType()), AI)) |
| 237 | return false; |
| 238 | break; |
| 239 | } |
| 240 | case Instruction::VAArg: |
| 241 | // "va-arg" from a pointer is safe. |
| 242 | break; |
| 243 | case Instruction::Store: { |
| 244 | if (V == I->getOperand(0)) { |
| 245 | // Stored the pointer - conservatively assume it may be unsafe. |
| 246 | DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AI |
| 247 | << "\n store of address: " << *I << "\n"); |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | if (!IsAccessSafe( |
| 252 | UI, DL->getTypeStoreSize(I->getOperand(0)->getType()), AI)) |
| 253 | return false; |
| 254 | break; |
| 255 | } |
| 256 | case Instruction::Ret: { |
| 257 | // Information leak. |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | case Instruction::Call: |
| 262 | case Instruction::Invoke: { |
| 263 | ImmutableCallSite CS(I); |
| 264 | |
| 265 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 266 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 267 | II->getIntrinsicID() == Intrinsic::lifetime_end) |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) { |
| 272 | if (!IsMemIntrinsicSafe(MI, UI, AI)) { |
| 273 | DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AI |
| 274 | << "\n unsafe memintrinsic: " << *I |
| 275 | << "\n"); |
| 276 | return false; |
| 277 | } |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | // LLVM 'nocapture' attribute is only set for arguments whose address |
| 282 | // is not stored, passed around, or used in any other non-trivial way. |
| 283 | // We assume that passing a pointer to an object as a 'nocapture |
| 284 | // readnone' argument is safe. |
| 285 | // FIXME: a more precise solution would require an interprocedural |
| 286 | // analysis here, which would look at all uses of an argument inside |
| 287 | // the function being called. |
| 288 | ImmutableCallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); |
| 289 | for (ImmutableCallSite::arg_iterator A = B; A != E; ++A) |
| 290 | if (A->get() == V) |
| 291 | if (!(CS.doesNotCapture(A - B) && |
| 292 | (CS.doesNotAccessMemory(A - B) || CS.doesNotAccessMemory()))) { |
| 293 | DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AI |
| 294 | << "\n unsafe call: " << *I << "\n"); |
| 295 | return false; |
| 296 | } |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | default: |
| 301 | if (Visited.insert(I).second) |
| 302 | WorkList.push_back(cast<const Instruction>(I)); |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // All uses of the alloca are safe, we can place it on the safe stack. |
| 308 | return true; |
| 309 | } |
| 310 | |
Evgeniy Stepanov | 9addbc9 | 2015-10-15 21:26:49 +0000 | [diff] [blame] | 311 | Value *SafeStack::getOrCreateUnsafeStackPtr(IRBuilder<> &IRB, Function &F) { |
Evgeniy Stepanov | d1aad26 | 2015-10-26 18:28:25 +0000 | [diff] [blame] | 312 | // Check if there is a target-specific location for the unsafe stack pointer. |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 313 | if (TL) |
| 314 | if (Value *V = TL->getSafeStackPointerLocation(IRB)) |
Evgeniy Stepanov | d1aad26 | 2015-10-26 18:28:25 +0000 | [diff] [blame] | 315 | return V; |
| 316 | |
| 317 | // Otherwise, assume the target links with compiler-rt, which provides a |
| 318 | // thread-local variable with a magic name. |
Evgeniy Stepanov | 9addbc9 | 2015-10-15 21:26:49 +0000 | [diff] [blame] | 319 | Module &M = *F.getParent(); |
Evgeniy Stepanov | d1aad26 | 2015-10-26 18:28:25 +0000 | [diff] [blame] | 320 | const char *UnsafeStackPtrVar = "__safestack_unsafe_stack_ptr"; |
| 321 | auto UnsafeStackPtr = |
| 322 | dyn_cast_or_null<GlobalVariable>(M.getNamedValue(UnsafeStackPtrVar)); |
Evgeniy Stepanov | 9addbc9 | 2015-10-15 21:26:49 +0000 | [diff] [blame] | 323 | |
Evgeniy Stepanov | d1aad26 | 2015-10-26 18:28:25 +0000 | [diff] [blame] | 324 | if (!UnsafeStackPtr) { |
| 325 | // The global variable is not defined yet, define it ourselves. |
| 326 | // We use the initial-exec TLS model because we do not support the |
| 327 | // variable living anywhere other than in the main executable. |
| 328 | UnsafeStackPtr = new GlobalVariable( |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 329 | M, StackPtrTy, false, GlobalValue::ExternalLinkage, nullptr, |
Evgeniy Stepanov | d1aad26 | 2015-10-26 18:28:25 +0000 | [diff] [blame] | 330 | UnsafeStackPtrVar, nullptr, GlobalValue::InitialExecTLSModel); |
Evgeniy Stepanov | 9addbc9 | 2015-10-15 21:26:49 +0000 | [diff] [blame] | 331 | } else { |
Evgeniy Stepanov | d1aad26 | 2015-10-26 18:28:25 +0000 | [diff] [blame] | 332 | // The variable exists, check its type and attributes. |
| 333 | if (UnsafeStackPtr->getValueType() != StackPtrTy) |
| 334 | report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type"); |
| 335 | if (!UnsafeStackPtr->isThreadLocal()) |
| 336 | report_fatal_error(Twine(UnsafeStackPtrVar) + " must be thread-local"); |
Evgeniy Stepanov | 9addbc9 | 2015-10-15 21:26:49 +0000 | [diff] [blame] | 337 | } |
Evgeniy Stepanov | d1aad26 | 2015-10-26 18:28:25 +0000 | [diff] [blame] | 338 | return UnsafeStackPtr; |
Evgeniy Stepanov | 9addbc9 | 2015-10-15 21:26:49 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 341 | void SafeStack::findInsts(Function &F, |
| 342 | SmallVectorImpl<AllocaInst *> &StaticAllocas, |
| 343 | SmallVectorImpl<AllocaInst *> &DynamicAllocas, |
| 344 | SmallVectorImpl<ReturnInst *> &Returns, |
| 345 | SmallVectorImpl<Instruction *> &StackRestorePoints) { |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 346 | for (Instruction &I : instructions(&F)) { |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 347 | if (auto AI = dyn_cast<AllocaInst>(&I)) { |
| 348 | ++NumAllocas; |
| 349 | |
| 350 | if (IsSafeStackAlloca(AI)) |
| 351 | continue; |
| 352 | |
| 353 | if (AI->isStaticAlloca()) { |
| 354 | ++NumUnsafeStaticAllocas; |
| 355 | StaticAllocas.push_back(AI); |
| 356 | } else { |
| 357 | ++NumUnsafeDynamicAllocas; |
| 358 | DynamicAllocas.push_back(AI); |
| 359 | } |
| 360 | } else if (auto RI = dyn_cast<ReturnInst>(&I)) { |
| 361 | Returns.push_back(RI); |
| 362 | } else if (auto CI = dyn_cast<CallInst>(&I)) { |
| 363 | // setjmps require stack restore. |
| 364 | if (CI->getCalledFunction() && CI->canReturnTwice()) |
| 365 | StackRestorePoints.push_back(CI); |
| 366 | } else if (auto LP = dyn_cast<LandingPadInst>(&I)) { |
| 367 | // Exception landing pads require stack restore. |
| 368 | StackRestorePoints.push_back(LP); |
| 369 | } else if (auto II = dyn_cast<IntrinsicInst>(&I)) { |
| 370 | if (II->getIntrinsicID() == Intrinsic::gcroot) |
| 371 | llvm::report_fatal_error( |
| 372 | "gcroot intrinsic not compatible with safestack attribute"); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | AllocaInst * |
Evgeniy Stepanov | 8685daf | 2015-09-24 01:23:51 +0000 | [diff] [blame] | 378 | SafeStack::createStackRestorePoints(IRBuilder<> &IRB, Function &F, |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 379 | ArrayRef<Instruction *> StackRestorePoints, |
| 380 | Value *StaticTop, bool NeedDynamicTop) { |
| 381 | if (StackRestorePoints.empty()) |
| 382 | return nullptr; |
| 383 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 384 | // We need the current value of the shadow stack pointer to restore |
| 385 | // after longjmp or exception catching. |
| 386 | |
| 387 | // FIXME: On some platforms this could be handled by the longjmp/exception |
| 388 | // runtime itself. |
| 389 | |
| 390 | AllocaInst *DynamicTop = nullptr; |
| 391 | if (NeedDynamicTop) |
| 392 | // If we also have dynamic alloca's, the stack pointer value changes |
| 393 | // throughout the function. For now we store it in an alloca. |
| 394 | DynamicTop = IRB.CreateAlloca(StackPtrTy, /*ArraySize=*/nullptr, |
| 395 | "unsafe_stack_dynamic_ptr"); |
| 396 | |
| 397 | if (!StaticTop) |
| 398 | // We need the original unsafe stack pointer value, even if there are |
| 399 | // no unsafe static allocas. |
| 400 | StaticTop = IRB.CreateLoad(UnsafeStackPtr, false, "unsafe_stack_ptr"); |
| 401 | |
| 402 | if (NeedDynamicTop) |
| 403 | IRB.CreateStore(StaticTop, DynamicTop); |
| 404 | |
| 405 | // Restore current stack pointer after longjmp/exception catch. |
| 406 | for (Instruction *I : StackRestorePoints) { |
| 407 | ++NumUnsafeStackRestorePoints; |
| 408 | |
| 409 | IRB.SetInsertPoint(cast<Instruction>(I->getNextNode())); |
| 410 | Value *CurrentTop = DynamicTop ? IRB.CreateLoad(DynamicTop) : StaticTop; |
| 411 | IRB.CreateStore(CurrentTop, UnsafeStackPtr); |
| 412 | } |
| 413 | |
| 414 | return DynamicTop; |
| 415 | } |
| 416 | |
| 417 | Value * |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 418 | SafeStack::moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F, |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 419 | ArrayRef<AllocaInst *> StaticAllocas, |
| 420 | ArrayRef<ReturnInst *> Returns) { |
| 421 | if (StaticAllocas.empty()) |
| 422 | return nullptr; |
| 423 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 424 | DIBuilder DIB(*F.getParent()); |
| 425 | |
| 426 | // We explicitly compute and set the unsafe stack layout for all unsafe |
| 427 | // static alloca instructions. We save the unsafe "base pointer" in the |
| 428 | // prologue into a local variable and restore it in the epilogue. |
| 429 | |
| 430 | // Load the current stack pointer (we'll also use it as a base pointer). |
| 431 | // FIXME: use a dedicated register for it ? |
| 432 | Instruction *BasePointer = |
| 433 | IRB.CreateLoad(UnsafeStackPtr, false, "unsafe_stack_ptr"); |
| 434 | assert(BasePointer->getType() == StackPtrTy); |
| 435 | |
| 436 | for (ReturnInst *RI : Returns) { |
| 437 | IRB.SetInsertPoint(RI); |
| 438 | IRB.CreateStore(BasePointer, UnsafeStackPtr); |
| 439 | } |
| 440 | |
| 441 | // Compute maximum alignment among static objects on the unsafe stack. |
| 442 | unsigned MaxAlignment = 0; |
| 443 | for (AllocaInst *AI : StaticAllocas) { |
| 444 | Type *Ty = AI->getAllocatedType(); |
| 445 | unsigned Align = |
| 446 | std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment()); |
| 447 | if (Align > MaxAlignment) |
| 448 | MaxAlignment = Align; |
| 449 | } |
| 450 | |
| 451 | if (MaxAlignment > StackAlignment) { |
| 452 | // Re-align the base pointer according to the max requested alignment. |
| 453 | assert(isPowerOf2_32(MaxAlignment)); |
| 454 | IRB.SetInsertPoint(cast<Instruction>(BasePointer->getNextNode())); |
| 455 | BasePointer = cast<Instruction>(IRB.CreateIntToPtr( |
| 456 | IRB.CreateAnd(IRB.CreatePtrToInt(BasePointer, IntPtrTy), |
| 457 | ConstantInt::get(IntPtrTy, ~uint64_t(MaxAlignment - 1))), |
| 458 | StackPtrTy)); |
| 459 | } |
| 460 | |
| 461 | // Allocate space for every unsafe static AllocaInst on the unsafe stack. |
| 462 | int64_t StaticOffset = 0; // Current stack top. |
| 463 | for (AllocaInst *AI : StaticAllocas) { |
| 464 | IRB.SetInsertPoint(AI); |
| 465 | |
| 466 | auto CArraySize = cast<ConstantInt>(AI->getArraySize()); |
| 467 | Type *Ty = AI->getAllocatedType(); |
| 468 | |
| 469 | uint64_t Size = DL->getTypeAllocSize(Ty) * CArraySize->getZExtValue(); |
| 470 | if (Size == 0) |
| 471 | Size = 1; // Don't create zero-sized stack objects. |
| 472 | |
| 473 | // Ensure the object is properly aligned. |
| 474 | unsigned Align = |
| 475 | std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment()); |
| 476 | |
| 477 | // Add alignment. |
| 478 | // NOTE: we ensure that BasePointer itself is aligned to >= Align. |
| 479 | StaticOffset += Size; |
| 480 | StaticOffset = RoundUpToAlignment(StaticOffset, Align); |
| 481 | |
| 482 | Value *Off = IRB.CreateGEP(BasePointer, // BasePointer is i8* |
| 483 | ConstantInt::get(Int32Ty, -StaticOffset)); |
| 484 | Value *NewAI = IRB.CreateBitCast(Off, AI->getType(), AI->getName()); |
| 485 | if (AI->hasName() && isa<Instruction>(NewAI)) |
| 486 | cast<Instruction>(NewAI)->takeName(AI); |
| 487 | |
| 488 | // Replace alloc with the new location. |
Evgeniy Stepanov | f608111 | 2015-09-30 19:55:43 +0000 | [diff] [blame] | 489 | replaceDbgDeclareForAlloca(AI, BasePointer, DIB, /*Deref=*/true, -StaticOffset); |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 490 | AI->replaceAllUsesWith(NewAI); |
| 491 | AI->eraseFromParent(); |
| 492 | } |
| 493 | |
| 494 | // Re-align BasePointer so that our callees would see it aligned as |
| 495 | // expected. |
| 496 | // FIXME: no need to update BasePointer in leaf functions. |
| 497 | StaticOffset = RoundUpToAlignment(StaticOffset, StackAlignment); |
| 498 | |
| 499 | // Update shadow stack pointer in the function epilogue. |
| 500 | IRB.SetInsertPoint(cast<Instruction>(BasePointer->getNextNode())); |
| 501 | |
| 502 | Value *StaticTop = |
| 503 | IRB.CreateGEP(BasePointer, ConstantInt::get(Int32Ty, -StaticOffset), |
| 504 | "unsafe_stack_static_top"); |
| 505 | IRB.CreateStore(StaticTop, UnsafeStackPtr); |
| 506 | return StaticTop; |
| 507 | } |
| 508 | |
| 509 | void SafeStack::moveDynamicAllocasToUnsafeStack( |
| 510 | Function &F, Value *UnsafeStackPtr, AllocaInst *DynamicTop, |
| 511 | ArrayRef<AllocaInst *> DynamicAllocas) { |
| 512 | DIBuilder DIB(*F.getParent()); |
| 513 | |
| 514 | for (AllocaInst *AI : DynamicAllocas) { |
| 515 | IRBuilder<> IRB(AI); |
| 516 | |
| 517 | // Compute the new SP value (after AI). |
| 518 | Value *ArraySize = AI->getArraySize(); |
| 519 | if (ArraySize->getType() != IntPtrTy) |
| 520 | ArraySize = IRB.CreateIntCast(ArraySize, IntPtrTy, false); |
| 521 | |
| 522 | Type *Ty = AI->getAllocatedType(); |
| 523 | uint64_t TySize = DL->getTypeAllocSize(Ty); |
| 524 | Value *Size = IRB.CreateMul(ArraySize, ConstantInt::get(IntPtrTy, TySize)); |
| 525 | |
| 526 | Value *SP = IRB.CreatePtrToInt(IRB.CreateLoad(UnsafeStackPtr), IntPtrTy); |
| 527 | SP = IRB.CreateSub(SP, Size); |
| 528 | |
| 529 | // Align the SP value to satisfy the AllocaInst, type and stack alignments. |
| 530 | unsigned Align = std::max( |
| 531 | std::max((unsigned)DL->getPrefTypeAlignment(Ty), AI->getAlignment()), |
| 532 | (unsigned)StackAlignment); |
| 533 | |
| 534 | assert(isPowerOf2_32(Align)); |
| 535 | Value *NewTop = IRB.CreateIntToPtr( |
| 536 | IRB.CreateAnd(SP, ConstantInt::get(IntPtrTy, ~uint64_t(Align - 1))), |
| 537 | StackPtrTy); |
| 538 | |
| 539 | // Save the stack pointer. |
| 540 | IRB.CreateStore(NewTop, UnsafeStackPtr); |
| 541 | if (DynamicTop) |
| 542 | IRB.CreateStore(NewTop, DynamicTop); |
| 543 | |
| 544 | Value *NewAI = IRB.CreateIntToPtr(SP, AI->getType()); |
| 545 | if (AI->hasName() && isa<Instruction>(NewAI)) |
| 546 | NewAI->takeName(AI); |
| 547 | |
| 548 | replaceDbgDeclareForAlloca(AI, NewAI, DIB, /*Deref=*/true); |
| 549 | AI->replaceAllUsesWith(NewAI); |
| 550 | AI->eraseFromParent(); |
| 551 | } |
| 552 | |
| 553 | if (!DynamicAllocas.empty()) { |
| 554 | // Now go through the instructions again, replacing stacksave/stackrestore. |
| 555 | for (inst_iterator It = inst_begin(&F), Ie = inst_end(&F); It != Ie;) { |
| 556 | Instruction *I = &*(It++); |
| 557 | auto II = dyn_cast<IntrinsicInst>(I); |
| 558 | if (!II) |
| 559 | continue; |
| 560 | |
| 561 | if (II->getIntrinsicID() == Intrinsic::stacksave) { |
| 562 | IRBuilder<> IRB(II); |
| 563 | Instruction *LI = IRB.CreateLoad(UnsafeStackPtr); |
| 564 | LI->takeName(II); |
| 565 | II->replaceAllUsesWith(LI); |
| 566 | II->eraseFromParent(); |
| 567 | } else if (II->getIntrinsicID() == Intrinsic::stackrestore) { |
| 568 | IRBuilder<> IRB(II); |
| 569 | Instruction *SI = IRB.CreateStore(II->getArgOperand(0), UnsafeStackPtr); |
| 570 | SI->takeName(II); |
| 571 | assert(II->use_empty()); |
| 572 | II->eraseFromParent(); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | bool SafeStack::runOnFunction(Function &F) { |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 579 | DEBUG(dbgs() << "[SafeStack] Function: " << F.getName() << "\n"); |
| 580 | |
| 581 | if (!F.hasFnAttribute(Attribute::SafeStack)) { |
| 582 | DEBUG(dbgs() << "[SafeStack] safestack is not requested" |
| 583 | " for this function\n"); |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | if (F.isDeclaration()) { |
| 588 | DEBUG(dbgs() << "[SafeStack] function definition" |
| 589 | " is not available\n"); |
| 590 | return false; |
| 591 | } |
| 592 | |
Evgeniy Stepanov | 447bbdb | 2015-11-13 21:21:42 +0000 | [diff] [blame^] | 593 | TL = TM ? TM->getSubtargetImpl(F)->getTargetLowering() : nullptr; |
| 594 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 595 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 596 | { |
| 597 | // Make sure the regular stack protector won't run on this function |
| 598 | // (safestack attribute takes precedence). |
| 599 | AttrBuilder B; |
| 600 | B.addAttribute(Attribute::StackProtect) |
| 601 | .addAttribute(Attribute::StackProtectReq) |
| 602 | .addAttribute(Attribute::StackProtectStrong); |
| 603 | F.removeAttributes( |
| 604 | AttributeSet::FunctionIndex, |
| 605 | AttributeSet::get(F.getContext(), AttributeSet::FunctionIndex, B)); |
| 606 | } |
| 607 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 608 | ++NumFunctions; |
| 609 | |
| 610 | SmallVector<AllocaInst *, 16> StaticAllocas; |
| 611 | SmallVector<AllocaInst *, 4> DynamicAllocas; |
| 612 | SmallVector<ReturnInst *, 4> Returns; |
| 613 | |
| 614 | // Collect all points where stack gets unwound and needs to be restored |
| 615 | // This is only necessary because the runtime (setjmp and unwind code) is |
| 616 | // not aware of the unsafe stack and won't unwind/restore it prorerly. |
| 617 | // To work around this problem without changing the runtime, we insert |
| 618 | // instrumentation to restore the unsafe stack pointer when necessary. |
| 619 | SmallVector<Instruction *, 4> StackRestorePoints; |
| 620 | |
| 621 | // Find all static and dynamic alloca instructions that must be moved to the |
| 622 | // unsafe stack, all return instructions and stack restore points. |
| 623 | findInsts(F, StaticAllocas, DynamicAllocas, Returns, StackRestorePoints); |
| 624 | |
| 625 | if (StaticAllocas.empty() && DynamicAllocas.empty() && |
| 626 | StackRestorePoints.empty()) |
| 627 | return false; // Nothing to do in this function. |
| 628 | |
| 629 | if (!StaticAllocas.empty() || !DynamicAllocas.empty()) |
| 630 | ++NumUnsafeStackFunctions; // This function has the unsafe stack. |
| 631 | |
| 632 | if (!StackRestorePoints.empty()) |
| 633 | ++NumUnsafeStackRestorePointsFunctions; |
| 634 | |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 635 | IRBuilder<> IRB(&F.front(), F.begin()->getFirstInsertionPt()); |
Evgeniy Stepanov | 9addbc9 | 2015-10-15 21:26:49 +0000 | [diff] [blame] | 636 | UnsafeStackPtr = getOrCreateUnsafeStackPtr(IRB, F); |
Peter Collingbourne | de26a91 | 2015-06-22 20:26:54 +0000 | [diff] [blame] | 637 | |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 638 | // The top of the unsafe stack after all unsafe static allocas are allocated. |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 639 | Value *StaticTop = moveStaticAllocasToUnsafeStack(IRB, F, StaticAllocas, Returns); |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 640 | |
| 641 | // Safe stack object that stores the current unsafe stack top. It is updated |
| 642 | // as unsafe dynamic (non-constant-sized) allocas are allocated and freed. |
| 643 | // This is only needed if we need to restore stack pointer after longjmp |
| 644 | // or exceptions, and we have dynamic allocations. |
| 645 | // FIXME: a better alternative might be to store the unsafe stack pointer |
| 646 | // before setjmp / invoke instructions. |
| 647 | AllocaInst *DynamicTop = createStackRestorePoints( |
Evgeniy Stepanov | 8685daf | 2015-09-24 01:23:51 +0000 | [diff] [blame] | 648 | IRB, F, StackRestorePoints, StaticTop, !DynamicAllocas.empty()); |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 649 | |
| 650 | // Handle dynamic allocas. |
| 651 | moveDynamicAllocasToUnsafeStack(F, UnsafeStackPtr, DynamicTop, |
| 652 | DynamicAllocas); |
| 653 | |
| 654 | DEBUG(dbgs() << "[SafeStack] safestack applied\n"); |
| 655 | return true; |
| 656 | } |
| 657 | |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 658 | } // anonymous namespace |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 659 | |
| 660 | char SafeStack::ID = 0; |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 661 | INITIALIZE_TM_PASS_BEGIN(SafeStack, "safe-stack", |
| 662 | "Safe Stack instrumentation pass", false, false) |
| 663 | INITIALIZE_TM_PASS_END(SafeStack, "safe-stack", |
| 664 | "Safe Stack instrumentation pass", false, false) |
Peter Collingbourne | 82437bf | 2015-06-15 21:07:11 +0000 | [diff] [blame] | 665 | |
Evgeniy Stepanov | a2002b0 | 2015-09-23 18:07:56 +0000 | [diff] [blame] | 666 | FunctionPass *llvm::createSafeStackPass(const llvm::TargetMachine *TM) { |
| 667 | return new SafeStack(TM); |
| 668 | } |