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