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