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