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