1 //===- StackSafetyAnalysis.cpp - Stack memory safety analysis -------------===// 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 //===----------------------------------------------------------------------===// 10 11 #include "llvm/Analysis/StackSafetyAnalysis.h" 12 #include "llvm/ADT/APInt.h" 13 #include "llvm/ADT/SmallPtrSet.h" 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/ADT/Statistic.h" 16 #include "llvm/Analysis/ModuleSummaryAnalysis.h" 17 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 18 #include "llvm/Analysis/StackLifetime.h" 19 #include "llvm/IR/ConstantRange.h" 20 #include "llvm/IR/DerivedTypes.h" 21 #include "llvm/IR/GlobalValue.h" 22 #include "llvm/IR/InstIterator.h" 23 #include "llvm/IR/Instructions.h" 24 #include "llvm/IR/IntrinsicInst.h" 25 #include "llvm/IR/ModuleSummaryIndex.h" 26 #include "llvm/InitializePasses.h" 27 #include "llvm/Support/Casting.h" 28 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Support/FormatVariadic.h" 30 #include "llvm/Support/raw_ostream.h" 31 #include <algorithm> 32 #include <memory> 33 #include <tuple> 34 35 using namespace llvm; 36 37 #define DEBUG_TYPE "stack-safety" 38 39 STATISTIC(NumAllocaStackSafe, "Number of safe allocas"); 40 STATISTIC(NumAllocaTotal, "Number of total allocas"); 41 42 STATISTIC(NumCombinedCalleeLookupTotal, 43 "Number of total callee lookups on combined index."); 44 STATISTIC(NumCombinedCalleeLookupFailed, 45 "Number of failed callee lookups on combined index."); 46 STATISTIC(NumModuleCalleeLookupTotal, 47 "Number of total callee lookups on module index."); 48 STATISTIC(NumModuleCalleeLookupFailed, 49 "Number of failed callee lookups on module index."); 50 STATISTIC(NumCombinedParamAccessesBefore, 51 "Number of total param accesses before generateParamAccessSummary."); 52 STATISTIC(NumCombinedParamAccessesAfter, 53 "Number of total param accesses after generateParamAccessSummary."); 54 STATISTIC(NumCombinedDataFlowNodes, 55 "Number of total nodes in combined index for dataflow processing."); 56 STATISTIC(NumIndexCalleeUnhandled, "Number of index callee which are unhandled."); 57 STATISTIC(NumIndexCalleeMultipleWeak, "Number of index callee non-unique weak."); 58 STATISTIC(NumIndexCalleeMultipleExternal, "Number of index callee non-unique external."); 59 60 61 static cl::opt<int> StackSafetyMaxIterations("stack-safety-max-iterations", 62 cl::init(20), cl::Hidden); 63 64 static cl::opt<bool> StackSafetyPrint("stack-safety-print", cl::init(false), 65 cl::Hidden); 66 67 static cl::opt<bool> StackSafetyRun("stack-safety-run", cl::init(false), 68 cl::Hidden); 69 70 namespace { 71 72 // Check if we should bailout for such ranges. 73 bool isUnsafe(const ConstantRange &R) { 74 return R.isEmptySet() || R.isFullSet() || R.isUpperSignWrapped(); 75 } 76 77 ConstantRange addOverflowNever(const ConstantRange &L, const ConstantRange &R) { 78 assert(!L.isSignWrappedSet()); 79 assert(!R.isSignWrappedSet()); 80 if (L.signedAddMayOverflow(R) != 81 ConstantRange::OverflowResult::NeverOverflows) 82 return ConstantRange::getFull(L.getBitWidth()); 83 ConstantRange Result = L.add(R); 84 assert(!Result.isSignWrappedSet()); 85 return Result; 86 } 87 88 ConstantRange unionNoWrap(const ConstantRange &L, const ConstantRange &R) { 89 assert(!L.isSignWrappedSet()); 90 assert(!R.isSignWrappedSet()); 91 auto Result = L.unionWith(R); 92 // Two non-wrapped sets can produce wrapped. 93 if (Result.isSignWrappedSet()) 94 Result = ConstantRange::getFull(Result.getBitWidth()); 95 return Result; 96 } 97 98 /// Describes use of address in as a function call argument. 99 template <typename CalleeTy> struct CallInfo { 100 /// Function being called. 101 const CalleeTy *Callee = nullptr; 102 /// Index of argument which pass address. 103 size_t ParamNo = 0; 104 105 CallInfo(const CalleeTy *Callee, size_t ParamNo) 106 : Callee(Callee), ParamNo(ParamNo) {} 107 108 struct Less { 109 bool operator()(const CallInfo &L, const CallInfo &R) const { 110 return std::tie(L.ParamNo, L.Callee) < std::tie(R.ParamNo, R.Callee); 111 } 112 }; 113 }; 114 115 /// Describe uses of address (alloca or parameter) inside of the function. 116 template <typename CalleeTy> struct UseInfo { 117 // Access range if the address (alloca or parameters). 118 // It is allowed to be empty-set when there are no known accesses. 119 ConstantRange Range; 120 std::map<const Instruction *, ConstantRange> Accesses; 121 122 // List of calls which pass address as an argument. 123 // Value is offset range of address from base address (alloca or calling 124 // function argument). Range should never set to empty-set, that is an invalid 125 // access range that can cause empty-set to be propagated with 126 // ConstantRange::add 127 using CallsTy = std::map<CallInfo<CalleeTy>, ConstantRange, 128 typename CallInfo<CalleeTy>::Less>; 129 CallsTy Calls; 130 131 UseInfo(unsigned PointerSize) : Range{PointerSize, false} {} 132 133 void updateRange(const ConstantRange &R) { Range = unionNoWrap(Range, R); } 134 void addRange(const Instruction *I, const ConstantRange &R) { 135 auto Ins = Accesses.emplace(I, R); 136 if (!Ins.second) 137 Ins.first->second = unionNoWrap(Ins.first->second, R); 138 updateRange(R); 139 } 140 }; 141 142 template <typename CalleeTy> 143 raw_ostream &operator<<(raw_ostream &OS, const UseInfo<CalleeTy> &U) { 144 OS << U.Range; 145 for (auto &Call : U.Calls) 146 OS << ", " 147 << "@" << Call.first.Callee->getName() << "(arg" << Call.first.ParamNo 148 << ", " << Call.second << ")"; 149 return OS; 150 } 151 152 /// Calculate the allocation size of a given alloca. Returns empty range 153 // in case of confution. 154 ConstantRange getStaticAllocaSizeRange(const AllocaInst &AI) { 155 const DataLayout &DL = AI.getModule()->getDataLayout(); 156 TypeSize TS = DL.getTypeAllocSize(AI.getAllocatedType()); 157 unsigned PointerSize = DL.getMaxPointerSizeInBits(); 158 // Fallback to empty range for alloca size. 159 ConstantRange R = ConstantRange::getEmpty(PointerSize); 160 if (TS.isScalable()) 161 return R; 162 APInt APSize(PointerSize, TS.getFixedSize(), true); 163 if (APSize.isNonPositive()) 164 return R; 165 if (AI.isArrayAllocation()) { 166 const auto *C = dyn_cast<ConstantInt>(AI.getArraySize()); 167 if (!C) 168 return R; 169 bool Overflow = false; 170 APInt Mul = C->getValue(); 171 if (Mul.isNonPositive()) 172 return R; 173 Mul = Mul.sextOrTrunc(PointerSize); 174 APSize = APSize.smul_ov(Mul, Overflow); 175 if (Overflow) 176 return R; 177 } 178 R = ConstantRange(APInt::getZero(PointerSize), APSize); 179 assert(!isUnsafe(R)); 180 return R; 181 } 182 183 template <typename CalleeTy> struct FunctionInfo { 184 std::map<const AllocaInst *, UseInfo<CalleeTy>> Allocas; 185 std::map<uint32_t, UseInfo<CalleeTy>> Params; 186 // TODO: describe return value as depending on one or more of its arguments. 187 188 // StackSafetyDataFlowAnalysis counter stored here for faster access. 189 int UpdateCount = 0; 190 191 void print(raw_ostream &O, StringRef Name, const Function *F) const { 192 // TODO: Consider different printout format after 193 // StackSafetyDataFlowAnalysis. Calls and parameters are irrelevant then. 194 O << " @" << Name << ((F && F->isDSOLocal()) ? "" : " dso_preemptable") 195 << ((F && F->isInterposable()) ? " interposable" : "") << "\n"; 196 197 O << " args uses:\n"; 198 for (auto &KV : Params) { 199 O << " "; 200 if (F) 201 O << F->getArg(KV.first)->getName(); 202 else 203 O << formatv("arg{0}", KV.first); 204 O << "[]: " << KV.second << "\n"; 205 } 206 207 O << " allocas uses:\n"; 208 if (F) { 209 for (auto &I : instructions(F)) { 210 if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { 211 auto &AS = Allocas.find(AI)->second; 212 O << " " << AI->getName() << "[" 213 << getStaticAllocaSizeRange(*AI).getUpper() << "]: " << AS << "\n"; 214 } 215 } 216 } else { 217 assert(Allocas.empty()); 218 } 219 } 220 }; 221 222 using GVToSSI = std::map<const GlobalValue *, FunctionInfo<GlobalValue>>; 223 224 } // namespace 225 226 struct StackSafetyInfo::InfoTy { 227 FunctionInfo<GlobalValue> Info; 228 }; 229 230 struct StackSafetyGlobalInfo::InfoTy { 231 GVToSSI Info; 232 SmallPtrSet<const AllocaInst *, 8> SafeAllocas; 233 SmallPtrSet<const Instruction *, 8> SafeAccesses; 234 }; 235 236 namespace { 237 238 class StackSafetyLocalAnalysis { 239 Function &F; 240 const DataLayout &DL; 241 ScalarEvolution &SE; 242 unsigned PointerSize = 0; 243 244 const ConstantRange UnknownRange; 245 246 ConstantRange offsetFrom(Value *Addr, Value *Base); 247 ConstantRange getAccessRange(Value *Addr, Value *Base, 248 const ConstantRange &SizeRange); 249 ConstantRange getAccessRange(Value *Addr, Value *Base, TypeSize Size); 250 ConstantRange getMemIntrinsicAccessRange(const MemIntrinsic *MI, const Use &U, 251 Value *Base); 252 253 void analyzeAllUses(Value *Ptr, UseInfo<GlobalValue> &AS, 254 const StackLifetime &SL); 255 256 public: 257 StackSafetyLocalAnalysis(Function &F, ScalarEvolution &SE) 258 : F(F), DL(F.getParent()->getDataLayout()), SE(SE), 259 PointerSize(DL.getPointerSizeInBits()), 260 UnknownRange(PointerSize, true) {} 261 262 // Run the transformation on the associated function. 263 FunctionInfo<GlobalValue> run(); 264 }; 265 266 ConstantRange StackSafetyLocalAnalysis::offsetFrom(Value *Addr, Value *Base) { 267 if (!SE.isSCEVable(Addr->getType()) || !SE.isSCEVable(Base->getType())) 268 return UnknownRange; 269 270 auto *PtrTy = IntegerType::getInt8PtrTy(SE.getContext()); 271 const SCEV *AddrExp = SE.getTruncateOrZeroExtend(SE.getSCEV(Addr), PtrTy); 272 const SCEV *BaseExp = SE.getTruncateOrZeroExtend(SE.getSCEV(Base), PtrTy); 273 const SCEV *Diff = SE.getMinusSCEV(AddrExp, BaseExp); 274 if (isa<SCEVCouldNotCompute>(Diff)) 275 return UnknownRange; 276 277 ConstantRange Offset = SE.getSignedRange(Diff); 278 if (isUnsafe(Offset)) 279 return UnknownRange; 280 return Offset.sextOrTrunc(PointerSize); 281 } 282 283 ConstantRange 284 StackSafetyLocalAnalysis::getAccessRange(Value *Addr, Value *Base, 285 const ConstantRange &SizeRange) { 286 // Zero-size loads and stores do not access memory. 287 if (SizeRange.isEmptySet()) 288 return ConstantRange::getEmpty(PointerSize); 289 assert(!isUnsafe(SizeRange)); 290 291 ConstantRange Offsets = offsetFrom(Addr, Base); 292 if (isUnsafe(Offsets)) 293 return UnknownRange; 294 295 Offsets = addOverflowNever(Offsets, SizeRange); 296 if (isUnsafe(Offsets)) 297 return UnknownRange; 298 return Offsets; 299 } 300 301 ConstantRange StackSafetyLocalAnalysis::getAccessRange(Value *Addr, Value *Base, 302 TypeSize Size) { 303 if (Size.isScalable()) 304 return UnknownRange; 305 APInt APSize(PointerSize, Size.getFixedSize(), true); 306 if (APSize.isNegative()) 307 return UnknownRange; 308 return getAccessRange(Addr, Base, 309 ConstantRange(APInt::getZero(PointerSize), APSize)); 310 } 311 312 ConstantRange StackSafetyLocalAnalysis::getMemIntrinsicAccessRange( 313 const MemIntrinsic *MI, const Use &U, Value *Base) { 314 if (const auto *MTI = dyn_cast<MemTransferInst>(MI)) { 315 if (MTI->getRawSource() != U && MTI->getRawDest() != U) 316 return ConstantRange::getEmpty(PointerSize); 317 } else { 318 if (MI->getRawDest() != U) 319 return ConstantRange::getEmpty(PointerSize); 320 } 321 322 auto *CalculationTy = IntegerType::getIntNTy(SE.getContext(), PointerSize); 323 if (!SE.isSCEVable(MI->getLength()->getType())) 324 return UnknownRange; 325 326 const SCEV *Expr = 327 SE.getTruncateOrZeroExtend(SE.getSCEV(MI->getLength()), CalculationTy); 328 ConstantRange Sizes = SE.getSignedRange(Expr); 329 if (Sizes.getUpper().isNegative() || isUnsafe(Sizes)) 330 return UnknownRange; 331 Sizes = Sizes.sextOrTrunc(PointerSize); 332 ConstantRange SizeRange(APInt::getZero(PointerSize), Sizes.getUpper() - 1); 333 return getAccessRange(U, Base, SizeRange); 334 } 335 336 /// The function analyzes all local uses of Ptr (alloca or argument) and 337 /// calculates local access range and all function calls where it was used. 338 void StackSafetyLocalAnalysis::analyzeAllUses(Value *Ptr, 339 UseInfo<GlobalValue> &US, 340 const StackLifetime &SL) { 341 SmallPtrSet<const Value *, 16> Visited; 342 SmallVector<const Value *, 8> WorkList; 343 WorkList.push_back(Ptr); 344 const AllocaInst *AI = dyn_cast<AllocaInst>(Ptr); 345 346 // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc. 347 while (!WorkList.empty()) { 348 const Value *V = WorkList.pop_back_val(); 349 for (const Use &UI : V->uses()) { 350 const auto *I = cast<Instruction>(UI.getUser()); 351 if (!SL.isReachable(I)) 352 continue; 353 354 assert(V == UI.get()); 355 356 switch (I->getOpcode()) { 357 case Instruction::Load: { 358 if (AI && !SL.isAliveAfter(AI, I)) { 359 US.addRange(I, UnknownRange); 360 break; 361 } 362 US.addRange(I, 363 getAccessRange(UI, Ptr, DL.getTypeStoreSize(I->getType()))); 364 break; 365 } 366 367 case Instruction::VAArg: 368 // "va-arg" from a pointer is safe. 369 break; 370 case Instruction::Store: { 371 if (V == I->getOperand(0)) { 372 // Stored the pointer - conservatively assume it may be unsafe. 373 US.addRange(I, UnknownRange); 374 break; 375 } 376 if (AI && !SL.isAliveAfter(AI, I)) { 377 US.addRange(I, UnknownRange); 378 break; 379 } 380 US.addRange( 381 I, getAccessRange( 382 UI, Ptr, DL.getTypeStoreSize(I->getOperand(0)->getType()))); 383 break; 384 } 385 386 case Instruction::Ret: 387 // Information leak. 388 // FIXME: Process parameters correctly. This is a leak only if we return 389 // alloca. 390 US.addRange(I, UnknownRange); 391 break; 392 393 case Instruction::Call: 394 case Instruction::Invoke: { 395 if (I->isLifetimeStartOrEnd()) 396 break; 397 398 if (AI && !SL.isAliveAfter(AI, I)) { 399 US.addRange(I, UnknownRange); 400 break; 401 } 402 403 if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) { 404 US.addRange(I, getMemIntrinsicAccessRange(MI, UI, Ptr)); 405 break; 406 } 407 408 const auto &CB = cast<CallBase>(*I); 409 if (!CB.isArgOperand(&UI)) { 410 US.addRange(I, UnknownRange); 411 break; 412 } 413 414 unsigned ArgNo = CB.getArgOperandNo(&UI); 415 if (CB.isByValArgument(ArgNo)) { 416 US.addRange(I, getAccessRange( 417 UI, Ptr, 418 DL.getTypeStoreSize(CB.getParamByValType(ArgNo)))); 419 break; 420 } 421 422 // FIXME: consult devirt? 423 // Do not follow aliases, otherwise we could inadvertently follow 424 // dso_preemptable aliases or aliases with interposable linkage. 425 const GlobalValue *Callee = 426 dyn_cast<GlobalValue>(CB.getCalledOperand()->stripPointerCasts()); 427 if (!Callee) { 428 US.addRange(I, UnknownRange); 429 break; 430 } 431 432 assert(isa<Function>(Callee) || isa<GlobalAlias>(Callee)); 433 ConstantRange Offsets = offsetFrom(UI, Ptr); 434 auto Insert = 435 US.Calls.emplace(CallInfo<GlobalValue>(Callee, ArgNo), Offsets); 436 if (!Insert.second) 437 Insert.first->second = Insert.first->second.unionWith(Offsets); 438 break; 439 } 440 441 default: 442 if (Visited.insert(I).second) 443 WorkList.push_back(cast<const Instruction>(I)); 444 } 445 } 446 } 447 } 448 449 FunctionInfo<GlobalValue> StackSafetyLocalAnalysis::run() { 450 FunctionInfo<GlobalValue> Info; 451 assert(!F.isDeclaration() && 452 "Can't run StackSafety on a function declaration"); 453 454 LLVM_DEBUG(dbgs() << "[StackSafety] " << F.getName() << "\n"); 455 456 SmallVector<AllocaInst *, 64> Allocas; 457 for (auto &I : instructions(F)) 458 if (auto *AI = dyn_cast<AllocaInst>(&I)) 459 Allocas.push_back(AI); 460 StackLifetime SL(F, Allocas, StackLifetime::LivenessType::Must); 461 SL.run(); 462 463 for (auto *AI : Allocas) { 464 auto &UI = Info.Allocas.emplace(AI, PointerSize).first->second; 465 analyzeAllUses(AI, UI, SL); 466 } 467 468 for (Argument &A : F.args()) { 469 // Non pointers and bypass arguments are not going to be used in any global 470 // processing. 471 if (A.getType()->isPointerTy() && !A.hasByValAttr()) { 472 auto &UI = Info.Params.emplace(A.getArgNo(), PointerSize).first->second; 473 analyzeAllUses(&A, UI, SL); 474 } 475 } 476 477 LLVM_DEBUG(Info.print(dbgs(), F.getName(), &F)); 478 LLVM_DEBUG(dbgs() << "\n[StackSafety] done\n"); 479 return Info; 480 } 481 482 template <typename CalleeTy> class StackSafetyDataFlowAnalysis { 483 using FunctionMap = std::map<const CalleeTy *, FunctionInfo<CalleeTy>>; 484 485 FunctionMap Functions; 486 const ConstantRange UnknownRange; 487 488 // Callee-to-Caller multimap. 489 DenseMap<const CalleeTy *, SmallVector<const CalleeTy *, 4>> Callers; 490 SetVector<const CalleeTy *> WorkList; 491 492 bool updateOneUse(UseInfo<CalleeTy> &US, bool UpdateToFullSet); 493 void updateOneNode(const CalleeTy *Callee, FunctionInfo<CalleeTy> &FS); 494 void updateOneNode(const CalleeTy *Callee) { 495 updateOneNode(Callee, Functions.find(Callee)->second); 496 } 497 void updateAllNodes() { 498 for (auto &F : Functions) 499 updateOneNode(F.first, F.second); 500 } 501 void runDataFlow(); 502 #ifndef NDEBUG 503 void verifyFixedPoint(); 504 #endif 505 506 public: 507 StackSafetyDataFlowAnalysis(uint32_t PointerBitWidth, FunctionMap Functions) 508 : Functions(std::move(Functions)), 509 UnknownRange(ConstantRange::getFull(PointerBitWidth)) {} 510 511 const FunctionMap &run(); 512 513 ConstantRange getArgumentAccessRange(const CalleeTy *Callee, unsigned ParamNo, 514 const ConstantRange &Offsets) const; 515 }; 516 517 template <typename CalleeTy> 518 ConstantRange StackSafetyDataFlowAnalysis<CalleeTy>::getArgumentAccessRange( 519 const CalleeTy *Callee, unsigned ParamNo, 520 const ConstantRange &Offsets) const { 521 auto FnIt = Functions.find(Callee); 522 // Unknown callee (outside of LTO domain or an indirect call). 523 if (FnIt == Functions.end()) 524 return UnknownRange; 525 auto &FS = FnIt->second; 526 auto ParamIt = FS.Params.find(ParamNo); 527 if (ParamIt == FS.Params.end()) 528 return UnknownRange; 529 auto &Access = ParamIt->second.Range; 530 if (Access.isEmptySet()) 531 return Access; 532 if (Access.isFullSet()) 533 return UnknownRange; 534 return addOverflowNever(Access, Offsets); 535 } 536 537 template <typename CalleeTy> 538 bool StackSafetyDataFlowAnalysis<CalleeTy>::updateOneUse(UseInfo<CalleeTy> &US, 539 bool UpdateToFullSet) { 540 bool Changed = false; 541 for (auto &KV : US.Calls) { 542 assert(!KV.second.isEmptySet() && 543 "Param range can't be empty-set, invalid offset range"); 544 545 ConstantRange CalleeRange = 546 getArgumentAccessRange(KV.first.Callee, KV.first.ParamNo, KV.second); 547 if (!US.Range.contains(CalleeRange)) { 548 Changed = true; 549 if (UpdateToFullSet) 550 US.Range = UnknownRange; 551 else 552 US.updateRange(CalleeRange); 553 } 554 } 555 return Changed; 556 } 557 558 template <typename CalleeTy> 559 void StackSafetyDataFlowAnalysis<CalleeTy>::updateOneNode( 560 const CalleeTy *Callee, FunctionInfo<CalleeTy> &FS) { 561 bool UpdateToFullSet = FS.UpdateCount > StackSafetyMaxIterations; 562 bool Changed = false; 563 for (auto &KV : FS.Params) 564 Changed |= updateOneUse(KV.second, UpdateToFullSet); 565 566 if (Changed) { 567 LLVM_DEBUG(dbgs() << "=== update [" << FS.UpdateCount 568 << (UpdateToFullSet ? ", full-set" : "") << "] " << &FS 569 << "\n"); 570 // Callers of this function may need updating. 571 for (auto &CallerID : Callers[Callee]) 572 WorkList.insert(CallerID); 573 574 ++FS.UpdateCount; 575 } 576 } 577 578 template <typename CalleeTy> 579 void StackSafetyDataFlowAnalysis<CalleeTy>::runDataFlow() { 580 SmallVector<const CalleeTy *, 16> Callees; 581 for (auto &F : Functions) { 582 Callees.clear(); 583 auto &FS = F.second; 584 for (auto &KV : FS.Params) 585 for (auto &CS : KV.second.Calls) 586 Callees.push_back(CS.first.Callee); 587 588 llvm::sort(Callees); 589 Callees.erase(std::unique(Callees.begin(), Callees.end()), Callees.end()); 590 591 for (auto &Callee : Callees) 592 Callers[Callee].push_back(F.first); 593 } 594 595 updateAllNodes(); 596 597 while (!WorkList.empty()) { 598 const CalleeTy *Callee = WorkList.back(); 599 WorkList.pop_back(); 600 updateOneNode(Callee); 601 } 602 } 603 604 #ifndef NDEBUG 605 template <typename CalleeTy> 606 void StackSafetyDataFlowAnalysis<CalleeTy>::verifyFixedPoint() { 607 WorkList.clear(); 608 updateAllNodes(); 609 assert(WorkList.empty()); 610 } 611 #endif 612 613 template <typename CalleeTy> 614 const typename StackSafetyDataFlowAnalysis<CalleeTy>::FunctionMap & 615 StackSafetyDataFlowAnalysis<CalleeTy>::run() { 616 runDataFlow(); 617 LLVM_DEBUG(verifyFixedPoint()); 618 return Functions; 619 } 620 621 FunctionSummary *findCalleeFunctionSummary(ValueInfo VI, StringRef ModuleId) { 622 if (!VI) 623 return nullptr; 624 auto SummaryList = VI.getSummaryList(); 625 GlobalValueSummary* S = nullptr; 626 for (const auto& GVS : SummaryList) { 627 if (!GVS->isLive()) 628 continue; 629 if (const AliasSummary *AS = dyn_cast<AliasSummary>(GVS.get())) 630 if (!AS->hasAliasee()) 631 continue; 632 if (!isa<FunctionSummary>(GVS->getBaseObject())) 633 continue; 634 if (GlobalValue::isLocalLinkage(GVS->linkage())) { 635 if (GVS->modulePath() == ModuleId) { 636 S = GVS.get(); 637 break; 638 } 639 } else if (GlobalValue::isExternalLinkage(GVS->linkage())) { 640 if (S) { 641 ++NumIndexCalleeMultipleExternal; 642 return nullptr; 643 } 644 S = GVS.get(); 645 } else if (GlobalValue::isWeakLinkage(GVS->linkage())) { 646 if (S) { 647 ++NumIndexCalleeMultipleWeak; 648 return nullptr; 649 } 650 S = GVS.get(); 651 } else if (GlobalValue::isAvailableExternallyLinkage(GVS->linkage()) || 652 GlobalValue::isLinkOnceLinkage(GVS->linkage())) { 653 if (SummaryList.size() == 1) 654 S = GVS.get(); 655 // According thinLTOResolvePrevailingGUID these are unlikely prevailing. 656 } else { 657 ++NumIndexCalleeUnhandled; 658 } 659 }; 660 while (S) { 661 if (!S->isLive() || !S->isDSOLocal()) 662 return nullptr; 663 if (FunctionSummary *FS = dyn_cast<FunctionSummary>(S)) 664 return FS; 665 AliasSummary *AS = dyn_cast<AliasSummary>(S); 666 if (!AS || !AS->hasAliasee()) 667 return nullptr; 668 S = AS->getBaseObject(); 669 if (S == AS) 670 return nullptr; 671 } 672 return nullptr; 673 } 674 675 const Function *findCalleeInModule(const GlobalValue *GV) { 676 while (GV) { 677 if (GV->isDeclaration() || GV->isInterposable() || !GV->isDSOLocal()) 678 return nullptr; 679 if (const Function *F = dyn_cast<Function>(GV)) 680 return F; 681 const GlobalAlias *A = dyn_cast<GlobalAlias>(GV); 682 if (!A) 683 return nullptr; 684 GV = A->getBaseObject(); 685 if (GV == A) 686 return nullptr; 687 } 688 return nullptr; 689 } 690 691 const ConstantRange *findParamAccess(const FunctionSummary &FS, 692 uint32_t ParamNo) { 693 assert(FS.isLive()); 694 assert(FS.isDSOLocal()); 695 for (auto &PS : FS.paramAccesses()) 696 if (ParamNo == PS.ParamNo) 697 return &PS.Use; 698 return nullptr; 699 } 700 701 void resolveAllCalls(UseInfo<GlobalValue> &Use, 702 const ModuleSummaryIndex *Index) { 703 ConstantRange FullSet(Use.Range.getBitWidth(), true); 704 // Move Use.Calls to a temp storage and repopulate - don't use std::move as it 705 // leaves Use.Calls in an undefined state. 706 UseInfo<GlobalValue>::CallsTy TmpCalls; 707 std::swap(TmpCalls, Use.Calls); 708 for (const auto &C : TmpCalls) { 709 const Function *F = findCalleeInModule(C.first.Callee); 710 if (F) { 711 Use.Calls.emplace(CallInfo<GlobalValue>(F, C.first.ParamNo), C.second); 712 continue; 713 } 714 715 if (!Index) 716 return Use.updateRange(FullSet); 717 FunctionSummary *FS = 718 findCalleeFunctionSummary(Index->getValueInfo(C.first.Callee->getGUID()), 719 C.first.Callee->getParent()->getModuleIdentifier()); 720 ++NumModuleCalleeLookupTotal; 721 if (!FS) { 722 ++NumModuleCalleeLookupFailed; 723 return Use.updateRange(FullSet); 724 } 725 const ConstantRange *Found = findParamAccess(*FS, C.first.ParamNo); 726 if (!Found || Found->isFullSet()) 727 return Use.updateRange(FullSet); 728 ConstantRange Access = Found->sextOrTrunc(Use.Range.getBitWidth()); 729 if (!Access.isEmptySet()) 730 Use.updateRange(addOverflowNever(Access, C.second)); 731 } 732 } 733 734 GVToSSI createGlobalStackSafetyInfo( 735 std::map<const GlobalValue *, FunctionInfo<GlobalValue>> Functions, 736 const ModuleSummaryIndex *Index) { 737 GVToSSI SSI; 738 if (Functions.empty()) 739 return SSI; 740 741 // FIXME: Simplify printing and remove copying here. 742 auto Copy = Functions; 743 744 for (auto &FnKV : Copy) 745 for (auto &KV : FnKV.second.Params) { 746 resolveAllCalls(KV.second, Index); 747 if (KV.second.Range.isFullSet()) 748 KV.second.Calls.clear(); 749 } 750 751 uint32_t PointerSize = Copy.begin() 752 ->first->getParent() 753 ->getDataLayout() 754 .getMaxPointerSizeInBits(); 755 StackSafetyDataFlowAnalysis<GlobalValue> SSDFA(PointerSize, std::move(Copy)); 756 757 for (auto &F : SSDFA.run()) { 758 auto FI = F.second; 759 auto &SrcF = Functions[F.first]; 760 for (auto &KV : FI.Allocas) { 761 auto &A = KV.second; 762 resolveAllCalls(A, Index); 763 for (auto &C : A.Calls) { 764 A.updateRange(SSDFA.getArgumentAccessRange(C.first.Callee, 765 C.first.ParamNo, C.second)); 766 } 767 // FIXME: This is needed only to preserve calls in print() results. 768 A.Calls = SrcF.Allocas.find(KV.first)->second.Calls; 769 } 770 for (auto &KV : FI.Params) { 771 auto &P = KV.second; 772 P.Calls = SrcF.Params.find(KV.first)->second.Calls; 773 } 774 SSI[F.first] = std::move(FI); 775 } 776 777 return SSI; 778 } 779 780 } // end anonymous namespace 781 782 StackSafetyInfo::StackSafetyInfo() = default; 783 784 StackSafetyInfo::StackSafetyInfo(Function *F, 785 std::function<ScalarEvolution &()> GetSE) 786 : F(F), GetSE(GetSE) {} 787 788 StackSafetyInfo::StackSafetyInfo(StackSafetyInfo &&) = default; 789 790 StackSafetyInfo &StackSafetyInfo::operator=(StackSafetyInfo &&) = default; 791 792 StackSafetyInfo::~StackSafetyInfo() = default; 793 794 const StackSafetyInfo::InfoTy &StackSafetyInfo::getInfo() const { 795 if (!Info) { 796 StackSafetyLocalAnalysis SSLA(*F, GetSE()); 797 Info.reset(new InfoTy{SSLA.run()}); 798 } 799 return *Info; 800 } 801 802 void StackSafetyInfo::print(raw_ostream &O) const { 803 getInfo().Info.print(O, F->getName(), dyn_cast<Function>(F)); 804 O << "\n"; 805 } 806 807 const StackSafetyGlobalInfo::InfoTy &StackSafetyGlobalInfo::getInfo() const { 808 if (!Info) { 809 std::map<const GlobalValue *, FunctionInfo<GlobalValue>> Functions; 810 for (auto &F : M->functions()) { 811 if (!F.isDeclaration()) { 812 auto FI = GetSSI(F).getInfo().Info; 813 Functions.emplace(&F, std::move(FI)); 814 } 815 } 816 Info.reset(new InfoTy{ 817 createGlobalStackSafetyInfo(std::move(Functions), Index), {}, {}}); 818 819 std::map<const Instruction *, bool> AccessIsUnsafe; 820 for (auto &FnKV : Info->Info) { 821 for (auto &KV : FnKV.second.Allocas) { 822 ++NumAllocaTotal; 823 const AllocaInst *AI = KV.first; 824 auto AIRange = getStaticAllocaSizeRange(*AI); 825 if (AIRange.contains(KV.second.Range)) { 826 Info->SafeAllocas.insert(AI); 827 ++NumAllocaStackSafe; 828 } 829 for (const auto &A : KV.second.Accesses) 830 AccessIsUnsafe[A.first] |= !AIRange.contains(A.second); 831 } 832 } 833 834 for (const auto &KV : AccessIsUnsafe) 835 if (!KV.second) 836 Info->SafeAccesses.insert(KV.first); 837 838 if (StackSafetyPrint) 839 print(errs()); 840 } 841 return *Info; 842 } 843 844 std::vector<FunctionSummary::ParamAccess> 845 StackSafetyInfo::getParamAccesses(ModuleSummaryIndex &Index) const { 846 // Implementation transforms internal representation of parameter information 847 // into FunctionSummary format. 848 std::vector<FunctionSummary::ParamAccess> ParamAccesses; 849 for (const auto &KV : getInfo().Info.Params) { 850 auto &PS = KV.second; 851 // Parameter accessed by any or unknown offset, represented as FullSet by 852 // StackSafety, is handled as the parameter for which we have no 853 // StackSafety info at all. So drop it to reduce summary size. 854 if (PS.Range.isFullSet()) 855 continue; 856 857 ParamAccesses.emplace_back(KV.first, PS.Range); 858 FunctionSummary::ParamAccess &Param = ParamAccesses.back(); 859 860 Param.Calls.reserve(PS.Calls.size()); 861 for (auto &C : PS.Calls) { 862 // Parameter forwarded into another function by any or unknown offset 863 // will make ParamAccess::Range as FullSet anyway. So we can drop the 864 // entire parameter like we did above. 865 // TODO(vitalybuka): Return already filtered parameters from getInfo(). 866 if (C.second.isFullSet()) { 867 ParamAccesses.pop_back(); 868 break; 869 } 870 Param.Calls.emplace_back(C.first.ParamNo, 871 Index.getOrInsertValueInfo(C.first.Callee), 872 C.second); 873 } 874 } 875 for (FunctionSummary::ParamAccess &Param : ParamAccesses) { 876 sort(Param.Calls, [](const FunctionSummary::ParamAccess::Call &L, 877 const FunctionSummary::ParamAccess::Call &R) { 878 return std::tie(L.ParamNo, L.Callee) < std::tie(R.ParamNo, R.Callee); 879 }); 880 } 881 return ParamAccesses; 882 } 883 884 StackSafetyGlobalInfo::StackSafetyGlobalInfo() = default; 885 886 StackSafetyGlobalInfo::StackSafetyGlobalInfo( 887 Module *M, std::function<const StackSafetyInfo &(Function &F)> GetSSI, 888 const ModuleSummaryIndex *Index) 889 : M(M), GetSSI(GetSSI), Index(Index) { 890 if (StackSafetyRun) 891 getInfo(); 892 } 893 894 StackSafetyGlobalInfo::StackSafetyGlobalInfo(StackSafetyGlobalInfo &&) = 895 default; 896 897 StackSafetyGlobalInfo & 898 StackSafetyGlobalInfo::operator=(StackSafetyGlobalInfo &&) = default; 899 900 StackSafetyGlobalInfo::~StackSafetyGlobalInfo() = default; 901 902 bool StackSafetyGlobalInfo::isSafe(const AllocaInst &AI) const { 903 const auto &Info = getInfo(); 904 return Info.SafeAllocas.count(&AI); 905 } 906 907 bool StackSafetyGlobalInfo::accessIsSafe(const Instruction &I) const { 908 const auto &Info = getInfo(); 909 return Info.SafeAccesses.count(&I); 910 } 911 912 void StackSafetyGlobalInfo::print(raw_ostream &O) const { 913 auto &SSI = getInfo().Info; 914 if (SSI.empty()) 915 return; 916 const Module &M = *SSI.begin()->first->getParent(); 917 for (auto &F : M.functions()) { 918 if (!F.isDeclaration()) { 919 SSI.find(&F)->second.print(O, F.getName(), &F); 920 O << " safe accesses:" 921 << "\n"; 922 for (const auto &I : instructions(F)) { 923 if (accessIsSafe(I)) { 924 O << " " << I << "\n"; 925 } 926 } 927 O << "\n"; 928 } 929 } 930 } 931 932 LLVM_DUMP_METHOD void StackSafetyGlobalInfo::dump() const { print(dbgs()); } 933 934 AnalysisKey StackSafetyAnalysis::Key; 935 936 StackSafetyInfo StackSafetyAnalysis::run(Function &F, 937 FunctionAnalysisManager &AM) { 938 return StackSafetyInfo(&F, [&AM, &F]() -> ScalarEvolution & { 939 return AM.getResult<ScalarEvolutionAnalysis>(F); 940 }); 941 } 942 943 PreservedAnalyses StackSafetyPrinterPass::run(Function &F, 944 FunctionAnalysisManager &AM) { 945 OS << "'Stack Safety Local Analysis' for function '" << F.getName() << "'\n"; 946 AM.getResult<StackSafetyAnalysis>(F).print(OS); 947 return PreservedAnalyses::all(); 948 } 949 950 char StackSafetyInfoWrapperPass::ID = 0; 951 952 StackSafetyInfoWrapperPass::StackSafetyInfoWrapperPass() : FunctionPass(ID) { 953 initializeStackSafetyInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 954 } 955 956 void StackSafetyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 957 AU.addRequiredTransitive<ScalarEvolutionWrapperPass>(); 958 AU.setPreservesAll(); 959 } 960 961 void StackSafetyInfoWrapperPass::print(raw_ostream &O, const Module *M) const { 962 SSI.print(O); 963 } 964 965 bool StackSafetyInfoWrapperPass::runOnFunction(Function &F) { 966 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); 967 SSI = {&F, [SE]() -> ScalarEvolution & { return *SE; }}; 968 return false; 969 } 970 971 AnalysisKey StackSafetyGlobalAnalysis::Key; 972 973 StackSafetyGlobalInfo 974 StackSafetyGlobalAnalysis::run(Module &M, ModuleAnalysisManager &AM) { 975 // FIXME: Lookup Module Summary. 976 FunctionAnalysisManager &FAM = 977 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 978 return {&M, 979 [&FAM](Function &F) -> const StackSafetyInfo & { 980 return FAM.getResult<StackSafetyAnalysis>(F); 981 }, 982 nullptr}; 983 } 984 985 PreservedAnalyses StackSafetyGlobalPrinterPass::run(Module &M, 986 ModuleAnalysisManager &AM) { 987 OS << "'Stack Safety Analysis' for module '" << M.getName() << "'\n"; 988 AM.getResult<StackSafetyGlobalAnalysis>(M).print(OS); 989 return PreservedAnalyses::all(); 990 } 991 992 char StackSafetyGlobalInfoWrapperPass::ID = 0; 993 994 StackSafetyGlobalInfoWrapperPass::StackSafetyGlobalInfoWrapperPass() 995 : ModulePass(ID) { 996 initializeStackSafetyGlobalInfoWrapperPassPass( 997 *PassRegistry::getPassRegistry()); 998 } 999 1000 StackSafetyGlobalInfoWrapperPass::~StackSafetyGlobalInfoWrapperPass() = default; 1001 1002 void StackSafetyGlobalInfoWrapperPass::print(raw_ostream &O, 1003 const Module *M) const { 1004 SSGI.print(O); 1005 } 1006 1007 void StackSafetyGlobalInfoWrapperPass::getAnalysisUsage( 1008 AnalysisUsage &AU) const { 1009 AU.setPreservesAll(); 1010 AU.addRequired<StackSafetyInfoWrapperPass>(); 1011 } 1012 1013 bool StackSafetyGlobalInfoWrapperPass::runOnModule(Module &M) { 1014 const ModuleSummaryIndex *ImportSummary = nullptr; 1015 if (auto *IndexWrapperPass = 1016 getAnalysisIfAvailable<ImmutableModuleSummaryIndexWrapperPass>()) 1017 ImportSummary = IndexWrapperPass->getIndex(); 1018 1019 SSGI = {&M, 1020 [this](Function &F) -> const StackSafetyInfo & { 1021 return getAnalysis<StackSafetyInfoWrapperPass>(F).getResult(); 1022 }, 1023 ImportSummary}; 1024 return false; 1025 } 1026 1027 bool llvm::needsParamAccessSummary(const Module &M) { 1028 if (StackSafetyRun) 1029 return true; 1030 for (auto &F : M.functions()) 1031 if (F.hasFnAttribute(Attribute::SanitizeMemTag)) 1032 return true; 1033 return false; 1034 } 1035 1036 void llvm::generateParamAccessSummary(ModuleSummaryIndex &Index) { 1037 if (!Index.hasParamAccess()) 1038 return; 1039 const ConstantRange FullSet(FunctionSummary::ParamAccess::RangeWidth, true); 1040 1041 auto CountParamAccesses = [&](auto &Stat) { 1042 if (!AreStatisticsEnabled()) 1043 return; 1044 for (auto &GVS : Index) 1045 for (auto &GV : GVS.second.SummaryList) 1046 if (FunctionSummary *FS = dyn_cast<FunctionSummary>(GV.get())) 1047 Stat += FS->paramAccesses().size(); 1048 }; 1049 1050 CountParamAccesses(NumCombinedParamAccessesBefore); 1051 1052 std::map<const FunctionSummary *, FunctionInfo<FunctionSummary>> Functions; 1053 1054 // Convert the ModuleSummaryIndex to a FunctionMap 1055 for (auto &GVS : Index) { 1056 for (auto &GV : GVS.second.SummaryList) { 1057 FunctionSummary *FS = dyn_cast<FunctionSummary>(GV.get()); 1058 if (!FS || FS->paramAccesses().empty()) 1059 continue; 1060 if (FS->isLive() && FS->isDSOLocal()) { 1061 FunctionInfo<FunctionSummary> FI; 1062 for (auto &PS : FS->paramAccesses()) { 1063 auto &US = 1064 FI.Params 1065 .emplace(PS.ParamNo, FunctionSummary::ParamAccess::RangeWidth) 1066 .first->second; 1067 US.Range = PS.Use; 1068 for (auto &Call : PS.Calls) { 1069 assert(!Call.Offsets.isFullSet()); 1070 FunctionSummary *S = 1071 findCalleeFunctionSummary(Call.Callee, FS->modulePath()); 1072 ++NumCombinedCalleeLookupTotal; 1073 if (!S) { 1074 ++NumCombinedCalleeLookupFailed; 1075 US.Range = FullSet; 1076 US.Calls.clear(); 1077 break; 1078 } 1079 US.Calls.emplace(CallInfo<FunctionSummary>(S, Call.ParamNo), 1080 Call.Offsets); 1081 } 1082 } 1083 Functions.emplace(FS, std::move(FI)); 1084 } 1085 // Reset data for all summaries. Alive and DSO local will be set back from 1086 // of data flow results below. Anything else will not be accessed 1087 // by ThinLTO backend, so we can save on bitcode size. 1088 FS->setParamAccesses({}); 1089 } 1090 } 1091 NumCombinedDataFlowNodes += Functions.size(); 1092 StackSafetyDataFlowAnalysis<FunctionSummary> SSDFA( 1093 FunctionSummary::ParamAccess::RangeWidth, std::move(Functions)); 1094 for (auto &KV : SSDFA.run()) { 1095 std::vector<FunctionSummary::ParamAccess> NewParams; 1096 NewParams.reserve(KV.second.Params.size()); 1097 for (auto &Param : KV.second.Params) { 1098 // It's not needed as FullSet is processed the same as a missing value. 1099 if (Param.second.Range.isFullSet()) 1100 continue; 1101 NewParams.emplace_back(); 1102 FunctionSummary::ParamAccess &New = NewParams.back(); 1103 New.ParamNo = Param.first; 1104 New.Use = Param.second.Range; // Only range is needed. 1105 } 1106 const_cast<FunctionSummary *>(KV.first)->setParamAccesses( 1107 std::move(NewParams)); 1108 } 1109 1110 CountParamAccesses(NumCombinedParamAccessesAfter); 1111 } 1112 1113 static const char LocalPassArg[] = "stack-safety-local"; 1114 static const char LocalPassName[] = "Stack Safety Local Analysis"; 1115 INITIALIZE_PASS_BEGIN(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName, 1116 false, true) 1117 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) 1118 INITIALIZE_PASS_END(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName, 1119 false, true) 1120 1121 static const char GlobalPassName[] = "Stack Safety Analysis"; 1122 INITIALIZE_PASS_BEGIN(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE, 1123 GlobalPassName, false, true) 1124 INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass) 1125 INITIALIZE_PASS_DEPENDENCY(ImmutableModuleSummaryIndexWrapperPass) 1126 INITIALIZE_PASS_END(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE, 1127 GlobalPassName, false, true) 1128