1 //===- StackSafetyAnalysis.cpp - Stack memory safety analysis -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 //===----------------------------------------------------------------------===// 11 12 #include "llvm/Analysis/StackSafetyAnalysis.h" 13 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 14 #include "llvm/IR/CallSite.h" 15 #include "llvm/IR/InstIterator.h" 16 #include "llvm/IR/IntrinsicInst.h" 17 #include "llvm/Support/raw_ostream.h" 18 19 using namespace llvm; 20 21 #define DEBUG_TYPE "stack-safety" 22 23 static cl::opt<int> StackSafetyMaxIterations("stack-safety-max-iterations", 24 cl::init(20), cl::Hidden); 25 26 namespace { 27 28 /// Rewrite an SCEV expression for a memory access address to an expression that 29 /// represents offset from the given alloca. 30 class AllocaOffsetRewriter : public SCEVRewriteVisitor<AllocaOffsetRewriter> { 31 const Value *AllocaPtr; 32 33 public: 34 AllocaOffsetRewriter(ScalarEvolution &SE, const Value *AllocaPtr) 35 : SCEVRewriteVisitor(SE), AllocaPtr(AllocaPtr) {} 36 37 const SCEV *visit(const SCEV *Expr) { 38 // Only re-write the expression if the alloca is used in an addition 39 // expression (it can be used in other types of expressions if it's cast to 40 // an int and passed as an argument.) 41 if (!isa<SCEVAddRecExpr>(Expr) && !isa<SCEVAddExpr>(Expr) && 42 !isa<SCEVUnknown>(Expr)) 43 return Expr; 44 return SCEVRewriteVisitor<AllocaOffsetRewriter>::visit(Expr); 45 } 46 47 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 48 // FIXME: look through one or several levels of definitions? 49 // This can be inttoptr(AllocaPtr) and SCEV would not unwrap 50 // it for us. 51 if (Expr->getValue() == AllocaPtr) 52 return SE.getZero(Expr->getType()); 53 return Expr; 54 } 55 }; 56 57 /// Describes use of address in as a function call argument. 58 struct PassAsArgInfo { 59 /// Function being called. 60 const GlobalValue *Callee = nullptr; 61 /// Index of argument which pass address. 62 size_t ParamNo = 0; 63 // Offset range of address from base address (alloca or calling function 64 // argument). 65 // Range should never set to empty-set, that is an invalid access range 66 // that can cause empty-set to be propagated with ConstantRange::add 67 ConstantRange Offset; 68 PassAsArgInfo(const GlobalValue *Callee, size_t ParamNo, ConstantRange Offset) 69 : Callee(Callee), ParamNo(ParamNo), Offset(Offset) {} 70 71 StringRef getName() const { return Callee->getName(); } 72 }; 73 74 raw_ostream &operator<<(raw_ostream &OS, const PassAsArgInfo &P) { 75 return OS << "@" << P.getName() << "(arg" << P.ParamNo << ", " << P.Offset 76 << ")"; 77 } 78 79 /// Describe uses of address (alloca or parameter) inside of the function. 80 struct UseInfo { 81 // Access range if the address (alloca or parameters). 82 // It is allowed to be empty-set when there are no known accesses. 83 ConstantRange Range; 84 85 // List of calls which pass address as an argument. 86 SmallVector<PassAsArgInfo, 4> Calls; 87 88 explicit UseInfo(unsigned PointerSize) : Range{PointerSize, false} {} 89 90 void updateRange(ConstantRange R) { Range = Range.unionWith(R); } 91 }; 92 93 raw_ostream &operator<<(raw_ostream &OS, const UseInfo &U) { 94 OS << U.Range; 95 for (auto &Call : U.Calls) 96 OS << ", " << Call; 97 return OS; 98 } 99 100 struct AllocaInfo { 101 const AllocaInst *AI = nullptr; 102 uint64_t Size = 0; 103 UseInfo Use; 104 105 AllocaInfo(unsigned PointerSize, const AllocaInst *AI, uint64_t Size) 106 : AI(AI), Size(Size), Use(PointerSize) {} 107 108 StringRef getName() const { return AI->getName(); } 109 }; 110 111 raw_ostream &operator<<(raw_ostream &OS, const AllocaInfo &A) { 112 return OS << A.getName() << "[" << A.Size << "]: " << A.Use; 113 } 114 115 struct ParamInfo { 116 const Argument *Arg = nullptr; 117 UseInfo Use; 118 119 explicit ParamInfo(unsigned PointerSize, const Argument *Arg) 120 : Arg(Arg), Use(PointerSize) {} 121 122 StringRef getName() const { return Arg ? Arg->getName() : "<N/A>"; } 123 }; 124 125 raw_ostream &operator<<(raw_ostream &OS, const ParamInfo &P) { 126 return OS << P.getName() << "[]: " << P.Use; 127 } 128 129 /// Calculate the allocation size of a given alloca. Returns 0 if the 130 /// size can not be statically determined. 131 uint64_t getStaticAllocaAllocationSize(const AllocaInst *AI) { 132 const DataLayout &DL = AI->getModule()->getDataLayout(); 133 uint64_t Size = DL.getTypeAllocSize(AI->getAllocatedType()); 134 if (AI->isArrayAllocation()) { 135 auto C = dyn_cast<ConstantInt>(AI->getArraySize()); 136 if (!C) 137 return 0; 138 Size *= C->getZExtValue(); 139 } 140 return Size; 141 } 142 143 } // end anonymous namespace 144 145 /// Describes uses of allocas and parameters inside of a single function. 146 struct StackSafetyInfo::FunctionInfo { 147 // May be a Function or a GlobalAlias 148 const GlobalValue *GV = nullptr; 149 // Informations about allocas uses. 150 SmallVector<AllocaInfo, 4> Allocas; 151 // Informations about parameters uses. 152 SmallVector<ParamInfo, 4> Params; 153 // TODO: describe return value as depending on one or more of its arguments. 154 155 // StackSafetyDataFlowAnalysis counter stored here for faster access. 156 int UpdateCount = 0; 157 158 FunctionInfo(const StackSafetyInfo &SSI) : FunctionInfo(*SSI.Info) {} 159 160 explicit FunctionInfo(const Function *F) : GV(F){}; 161 // Creates FunctionInfo that forwards all the parameters to the aliasee. 162 explicit FunctionInfo(const GlobalAlias *A); 163 164 FunctionInfo(FunctionInfo &&) = default; 165 166 bool IsDSOLocal() const { return GV->isDSOLocal(); }; 167 168 bool IsInterposable() const { return GV->isInterposable(); }; 169 170 StringRef getName() const { return GV->getName(); } 171 172 void print(raw_ostream &O) const { 173 // TODO: Consider different printout format after 174 // StackSafetyDataFlowAnalysis. Calls and parameters are irrelevant then. 175 O << " @" << getName() << (IsDSOLocal() ? "" : " dso_preemptable") 176 << (IsInterposable() ? " interposable" : "") << "\n"; 177 O << " args uses:\n"; 178 for (auto &P : Params) 179 O << " " << P << "\n"; 180 O << " allocas uses:\n"; 181 for (auto &AS : Allocas) 182 O << " " << AS << "\n"; 183 } 184 185 private: 186 FunctionInfo(const FunctionInfo &) = default; 187 }; 188 189 StackSafetyInfo::FunctionInfo::FunctionInfo(const GlobalAlias *A) : GV(A) { 190 unsigned PointerSize = A->getParent()->getDataLayout().getPointerSizeInBits(); 191 const GlobalObject *Aliasee = A->getBaseObject(); 192 const FunctionType *Type = cast<FunctionType>(Aliasee->getValueType()); 193 // 'Forward' all parameters to this alias to the aliasee 194 for (unsigned ArgNo = 0; ArgNo < Type->getNumParams(); ArgNo++) { 195 Params.emplace_back(PointerSize, nullptr); 196 UseInfo &US = Params.back().Use; 197 US.Calls.emplace_back(Aliasee, ArgNo, ConstantRange(APInt(PointerSize, 0))); 198 } 199 } 200 201 namespace { 202 203 class StackSafetyLocalAnalysis { 204 const Function &F; 205 const DataLayout &DL; 206 ScalarEvolution &SE; 207 unsigned PointerSize = 0; 208 209 const ConstantRange UnknownRange; 210 211 ConstantRange offsetFromAlloca(Value *Addr, const Value *AllocaPtr); 212 ConstantRange getAccessRange(Value *Addr, const Value *AllocaPtr, 213 uint64_t AccessSize); 214 ConstantRange getMemIntrinsicAccessRange(const MemIntrinsic *MI, const Use &U, 215 const Value *AllocaPtr); 216 217 bool analyzeAllUses(const Value *Ptr, UseInfo &AS); 218 219 ConstantRange getRange(uint64_t Lower, uint64_t Upper) const { 220 return ConstantRange(APInt(PointerSize, Lower), APInt(PointerSize, Upper)); 221 } 222 223 public: 224 StackSafetyLocalAnalysis(const Function &F, ScalarEvolution &SE) 225 : F(F), DL(F.getParent()->getDataLayout()), SE(SE), 226 PointerSize(DL.getPointerSizeInBits()), 227 UnknownRange(PointerSize, true) {} 228 229 // Run the transformation on the associated function. 230 StackSafetyInfo run(); 231 }; 232 233 ConstantRange 234 StackSafetyLocalAnalysis::offsetFromAlloca(Value *Addr, 235 const Value *AllocaPtr) { 236 if (!SE.isSCEVable(Addr->getType())) 237 return UnknownRange; 238 239 AllocaOffsetRewriter Rewriter(SE, AllocaPtr); 240 const SCEV *Expr = Rewriter.visit(SE.getSCEV(Addr)); 241 ConstantRange Offset = SE.getUnsignedRange(Expr).zextOrTrunc(PointerSize); 242 assert(!Offset.isEmptySet()); 243 return Offset; 244 } 245 246 ConstantRange StackSafetyLocalAnalysis::getAccessRange(Value *Addr, 247 const Value *AllocaPtr, 248 uint64_t AccessSize) { 249 if (!SE.isSCEVable(Addr->getType())) 250 return UnknownRange; 251 252 AllocaOffsetRewriter Rewriter(SE, AllocaPtr); 253 const SCEV *Expr = Rewriter.visit(SE.getSCEV(Addr)); 254 255 ConstantRange AccessStartRange = 256 SE.getUnsignedRange(Expr).zextOrTrunc(PointerSize); 257 ConstantRange SizeRange = getRange(0, AccessSize); 258 ConstantRange AccessRange = AccessStartRange.add(SizeRange); 259 assert(!AccessRange.isEmptySet()); 260 return AccessRange; 261 } 262 263 ConstantRange StackSafetyLocalAnalysis::getMemIntrinsicAccessRange( 264 const MemIntrinsic *MI, const Use &U, const Value *AllocaPtr) { 265 if (auto MTI = dyn_cast<MemTransferInst>(MI)) { 266 if (MTI->getRawSource() != U && MTI->getRawDest() != U) 267 return getRange(0, 1); 268 } else { 269 if (MI->getRawDest() != U) 270 return getRange(0, 1); 271 } 272 const auto *Len = dyn_cast<ConstantInt>(MI->getLength()); 273 // Non-constant size => unsafe. FIXME: try SCEV getRange. 274 if (!Len) 275 return UnknownRange; 276 ConstantRange AccessRange = getAccessRange(U, AllocaPtr, Len->getZExtValue()); 277 return AccessRange; 278 } 279 280 /// The function analyzes all local uses of Ptr (alloca or argument) and 281 /// calculates local access range and all function calls where it was used. 282 bool StackSafetyLocalAnalysis::analyzeAllUses(const Value *Ptr, UseInfo &US) { 283 SmallPtrSet<const Value *, 16> Visited; 284 SmallVector<const Value *, 8> WorkList; 285 WorkList.push_back(Ptr); 286 287 // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc. 288 while (!WorkList.empty()) { 289 const Value *V = WorkList.pop_back_val(); 290 for (const Use &UI : V->uses()) { 291 auto I = cast<const Instruction>(UI.getUser()); 292 assert(V == UI.get()); 293 294 switch (I->getOpcode()) { 295 case Instruction::Load: { 296 US.updateRange( 297 getAccessRange(UI, Ptr, DL.getTypeStoreSize(I->getType()))); 298 break; 299 } 300 301 case Instruction::VAArg: 302 // "va-arg" from a pointer is safe. 303 break; 304 case Instruction::Store: { 305 if (V == I->getOperand(0)) { 306 // Stored the pointer - conservatively assume it may be unsafe. 307 US.updateRange(UnknownRange); 308 return false; 309 } 310 US.updateRange(getAccessRange( 311 UI, Ptr, DL.getTypeStoreSize(I->getOperand(0)->getType()))); 312 break; 313 } 314 315 case Instruction::Ret: 316 // Information leak. 317 // FIXME: Process parameters correctly. This is a leak only if we return 318 // alloca. 319 US.updateRange(UnknownRange); 320 return false; 321 322 case Instruction::Call: 323 case Instruction::Invoke: { 324 ImmutableCallSite CS(I); 325 326 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 327 if (II->getIntrinsicID() == Intrinsic::lifetime_start || 328 II->getIntrinsicID() == Intrinsic::lifetime_end) 329 break; 330 } 331 332 if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) { 333 US.updateRange(getMemIntrinsicAccessRange(MI, UI, Ptr)); 334 break; 335 } 336 337 // FIXME: consult devirt? 338 // Do not follow aliases, otherwise we could inadvertently follow 339 // dso_preemptable aliases or aliases with interposable linkage. 340 const GlobalValue *Callee = dyn_cast<GlobalValue>( 341 CS.getCalledValue()->stripPointerCastsNoFollowAliases()); 342 if (!Callee) { 343 US.updateRange(UnknownRange); 344 return false; 345 } 346 347 assert(isa<Function>(Callee) || isa<GlobalAlias>(Callee)); 348 349 ImmutableCallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); 350 for (ImmutableCallSite::arg_iterator A = B; A != E; ++A) { 351 if (A->get() == V) { 352 ConstantRange Offset = offsetFromAlloca(UI, Ptr); 353 US.Calls.emplace_back(Callee, A - B, Offset); 354 } 355 } 356 357 break; 358 } 359 360 default: 361 if (Visited.insert(I).second) 362 WorkList.push_back(cast<const Instruction>(I)); 363 } 364 } 365 } 366 367 return true; 368 } 369 370 StackSafetyInfo StackSafetyLocalAnalysis::run() { 371 StackSafetyInfo::FunctionInfo Info(&F); 372 assert(!F.isDeclaration() && 373 "Can't run StackSafety on a function declaration"); 374 375 LLVM_DEBUG(dbgs() << "[StackSafety] " << F.getName() << "\n"); 376 377 for (auto &I : instructions(F)) { 378 if (auto AI = dyn_cast<AllocaInst>(&I)) { 379 Info.Allocas.emplace_back(PointerSize, AI, 380 getStaticAllocaAllocationSize(AI)); 381 AllocaInfo &AS = Info.Allocas.back(); 382 analyzeAllUses(AI, AS.Use); 383 } 384 } 385 386 for (const Argument &A : make_range(F.arg_begin(), F.arg_end())) { 387 Info.Params.emplace_back(PointerSize, &A); 388 ParamInfo &PS = Info.Params.back(); 389 analyzeAllUses(&A, PS.Use); 390 } 391 392 LLVM_DEBUG(dbgs() << "[StackSafety] done\n"); 393 LLVM_DEBUG(Info.print(dbgs())); 394 return StackSafetyInfo(std::move(Info)); 395 } 396 397 class StackSafetyDataFlowAnalysis { 398 using FunctionMap = 399 std::map<const GlobalValue *, StackSafetyInfo::FunctionInfo>; 400 401 FunctionMap Functions; 402 // Callee-to-Caller multimap. 403 DenseMap<const GlobalValue *, SmallVector<const GlobalValue *, 4>> Callers; 404 SetVector<const GlobalValue *> WorkList; 405 406 unsigned PointerSize = 0; 407 const ConstantRange UnknownRange; 408 409 ConstantRange getArgumentAccessRange(const GlobalValue *Callee, 410 unsigned ParamNo) const; 411 bool updateOneUse(UseInfo &US, bool UpdateToFullSet); 412 void updateOneNode(const GlobalValue *Callee, 413 StackSafetyInfo::FunctionInfo &FS); 414 void updateOneNode(const GlobalValue *Callee) { 415 updateOneNode(Callee, Functions.find(Callee)->second); 416 } 417 void updateAllNodes() { 418 for (auto &F : Functions) 419 updateOneNode(F.first, F.second); 420 } 421 void runDataFlow(); 422 void verifyFixedPoint(); 423 424 public: 425 StackSafetyDataFlowAnalysis( 426 Module &M, std::function<const StackSafetyInfo &(Function &)> FI); 427 StackSafetyGlobalInfo run(); 428 }; 429 430 StackSafetyDataFlowAnalysis::StackSafetyDataFlowAnalysis( 431 Module &M, std::function<const StackSafetyInfo &(Function &)> FI) 432 : PointerSize(M.getDataLayout().getPointerSizeInBits()), 433 UnknownRange(PointerSize, true) { 434 // Without ThinLTO, run the local analysis for every function in the TU and 435 // then run the DFA. 436 for (auto &F : M.functions()) 437 if (!F.isDeclaration()) 438 Functions.emplace(&F, FI(F)); 439 for (auto &A : M.aliases()) 440 if (isa<Function>(A.getBaseObject())) 441 Functions.emplace(&A, StackSafetyInfo::FunctionInfo(&A)); 442 } 443 444 ConstantRange 445 StackSafetyDataFlowAnalysis::getArgumentAccessRange(const GlobalValue *Callee, 446 unsigned ParamNo) const { 447 auto IT = Functions.find(Callee); 448 // Unknown callee (outside of LTO domain or an indirect call). 449 if (IT == Functions.end()) 450 return UnknownRange; 451 const StackSafetyInfo::FunctionInfo &FS = IT->second; 452 // The definition of this symbol may not be the definition in this linkage 453 // unit. 454 if (!FS.IsDSOLocal() || FS.IsInterposable()) 455 return UnknownRange; 456 if (ParamNo >= FS.Params.size()) // possibly vararg 457 return UnknownRange; 458 return FS.Params[ParamNo].Use.Range; 459 } 460 461 bool StackSafetyDataFlowAnalysis::updateOneUse(UseInfo &US, 462 bool UpdateToFullSet) { 463 bool Changed = false; 464 for (auto &CS : US.Calls) { 465 assert(!CS.Offset.isEmptySet() && 466 "Param range can't be empty-set, invalid offset range"); 467 468 ConstantRange CalleeRange = getArgumentAccessRange(CS.Callee, CS.ParamNo); 469 CalleeRange = CalleeRange.add(CS.Offset); 470 if (!US.Range.contains(CalleeRange)) { 471 Changed = true; 472 if (UpdateToFullSet) 473 US.Range = UnknownRange; 474 else 475 US.Range = US.Range.unionWith(CalleeRange); 476 } 477 } 478 return Changed; 479 } 480 481 void StackSafetyDataFlowAnalysis::updateOneNode( 482 const GlobalValue *Callee, StackSafetyInfo::FunctionInfo &FS) { 483 bool UpdateToFullSet = FS.UpdateCount > StackSafetyMaxIterations; 484 bool Changed = false; 485 for (auto &AS : FS.Allocas) 486 Changed |= updateOneUse(AS.Use, UpdateToFullSet); 487 for (auto &PS : FS.Params) 488 Changed |= updateOneUse(PS.Use, UpdateToFullSet); 489 490 if (Changed) { 491 LLVM_DEBUG(dbgs() << "=== update [" << FS.UpdateCount 492 << (UpdateToFullSet ? ", full-set" : "") << "] " 493 << FS.getName() << "\n"); 494 // Callers of this function may need updating. 495 for (auto &CallerID : Callers[Callee]) 496 WorkList.insert(CallerID); 497 498 ++FS.UpdateCount; 499 } 500 } 501 502 void StackSafetyDataFlowAnalysis::runDataFlow() { 503 Callers.clear(); 504 WorkList.clear(); 505 506 SmallVector<const GlobalValue *, 16> Callees; 507 for (auto &F : Functions) { 508 Callees.clear(); 509 StackSafetyInfo::FunctionInfo &FS = F.second; 510 for (auto &AS : FS.Allocas) 511 for (auto &CS : AS.Use.Calls) 512 Callees.push_back(CS.Callee); 513 for (auto &PS : FS.Params) 514 for (auto &CS : PS.Use.Calls) 515 Callees.push_back(CS.Callee); 516 517 llvm::sort(Callees); 518 Callees.erase(std::unique(Callees.begin(), Callees.end()), Callees.end()); 519 520 for (auto &Callee : Callees) 521 Callers[Callee].push_back(F.first); 522 } 523 524 updateAllNodes(); 525 526 while (!WorkList.empty()) { 527 const GlobalValue *Callee = WorkList.back(); 528 WorkList.pop_back(); 529 updateOneNode(Callee); 530 } 531 } 532 533 void StackSafetyDataFlowAnalysis::verifyFixedPoint() { 534 WorkList.clear(); 535 updateAllNodes(); 536 assert(WorkList.empty()); 537 } 538 539 StackSafetyGlobalInfo StackSafetyDataFlowAnalysis::run() { 540 runDataFlow(); 541 LLVM_DEBUG(verifyFixedPoint()); 542 543 StackSafetyGlobalInfo SSI; 544 for (auto &F : Functions) 545 SSI.emplace(F.first, std::move(F.second)); 546 return SSI; 547 } 548 549 void print(const StackSafetyGlobalInfo &SSI, raw_ostream &O, const Module &M) { 550 size_t Count = 0; 551 for (auto &F : M.functions()) 552 if (!F.isDeclaration()) { 553 SSI.find(&F)->second.print(O); 554 O << "\n"; 555 ++Count; 556 } 557 for (auto &A : M.aliases()) { 558 SSI.find(&A)->second.print(O); 559 O << "\n"; 560 ++Count; 561 } 562 assert(Count == SSI.size() && "Unexpected functions in the result"); 563 } 564 565 } // end anonymous namespace 566 567 StackSafetyInfo::StackSafetyInfo() = default; 568 StackSafetyInfo::StackSafetyInfo(StackSafetyInfo &&) = default; 569 StackSafetyInfo &StackSafetyInfo::operator=(StackSafetyInfo &&) = default; 570 571 StackSafetyInfo::StackSafetyInfo(FunctionInfo &&Info) 572 : Info(new FunctionInfo(std::move(Info))) {} 573 574 StackSafetyInfo::~StackSafetyInfo() = default; 575 576 void StackSafetyInfo::print(raw_ostream &O) const { Info->print(O); } 577 578 AnalysisKey StackSafetyAnalysis::Key; 579 580 StackSafetyInfo StackSafetyAnalysis::run(Function &F, 581 FunctionAnalysisManager &AM) { 582 StackSafetyLocalAnalysis SSLA(F, AM.getResult<ScalarEvolutionAnalysis>(F)); 583 return SSLA.run(); 584 } 585 586 PreservedAnalyses StackSafetyPrinterPass::run(Function &F, 587 FunctionAnalysisManager &AM) { 588 OS << "'Stack Safety Local Analysis' for function '" << F.getName() << "'\n"; 589 AM.getResult<StackSafetyAnalysis>(F).print(OS); 590 return PreservedAnalyses::all(); 591 } 592 593 char StackSafetyInfoWrapperPass::ID = 0; 594 595 StackSafetyInfoWrapperPass::StackSafetyInfoWrapperPass() : FunctionPass(ID) { 596 initializeStackSafetyInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 597 } 598 599 void StackSafetyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 600 AU.addRequired<ScalarEvolutionWrapperPass>(); 601 AU.setPreservesAll(); 602 } 603 604 void StackSafetyInfoWrapperPass::print(raw_ostream &O, const Module *M) const { 605 SSI.print(O); 606 } 607 608 bool StackSafetyInfoWrapperPass::runOnFunction(Function &F) { 609 StackSafetyLocalAnalysis SSLA( 610 F, getAnalysis<ScalarEvolutionWrapperPass>().getSE()); 611 SSI = StackSafetyInfo(SSLA.run()); 612 return false; 613 } 614 615 AnalysisKey StackSafetyGlobalAnalysis::Key; 616 617 StackSafetyGlobalInfo 618 StackSafetyGlobalAnalysis::run(Module &M, ModuleAnalysisManager &AM) { 619 FunctionAnalysisManager &FAM = 620 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 621 622 StackSafetyDataFlowAnalysis SSDFA( 623 M, [&FAM](Function &F) -> const StackSafetyInfo & { 624 return FAM.getResult<StackSafetyAnalysis>(F); 625 }); 626 return SSDFA.run(); 627 } 628 629 PreservedAnalyses StackSafetyGlobalPrinterPass::run(Module &M, 630 ModuleAnalysisManager &AM) { 631 OS << "'Stack Safety Analysis' for module '" << M.getName() << "'\n"; 632 print(AM.getResult<StackSafetyGlobalAnalysis>(M), OS, M); 633 return PreservedAnalyses::all(); 634 } 635 636 char StackSafetyGlobalInfoWrapperPass::ID = 0; 637 638 StackSafetyGlobalInfoWrapperPass::StackSafetyGlobalInfoWrapperPass() 639 : ModulePass(ID) { 640 initializeStackSafetyGlobalInfoWrapperPassPass( 641 *PassRegistry::getPassRegistry()); 642 } 643 644 void StackSafetyGlobalInfoWrapperPass::print(raw_ostream &O, 645 const Module *M) const { 646 ::print(SSI, O, *M); 647 } 648 649 void StackSafetyGlobalInfoWrapperPass::getAnalysisUsage( 650 AnalysisUsage &AU) const { 651 AU.addRequired<StackSafetyInfoWrapperPass>(); 652 } 653 654 bool StackSafetyGlobalInfoWrapperPass::runOnModule(Module &M) { 655 StackSafetyDataFlowAnalysis SSDFA( 656 M, [this](Function &F) -> const StackSafetyInfo & { 657 return getAnalysis<StackSafetyInfoWrapperPass>(F).getResult(); 658 }); 659 SSI = SSDFA.run(); 660 return false; 661 } 662 663 static const char LocalPassArg[] = "stack-safety-local"; 664 static const char LocalPassName[] = "Stack Safety Local Analysis"; 665 INITIALIZE_PASS_BEGIN(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName, 666 false, true) 667 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) 668 INITIALIZE_PASS_END(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName, 669 false, true) 670 671 static const char GlobalPassName[] = "Stack Safety Analysis"; 672 INITIALIZE_PASS_BEGIN(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE, 673 GlobalPassName, false, false) 674 INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass) 675 INITIALIZE_PASS_END(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE, 676 GlobalPassName, false, false) 677