xref: /llvm-project/llvm/lib/Analysis/StackSafetyAnalysis.cpp (revision 5b5d774f5d3840a1e242f0ef5873d735ccf817a4)
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.getReturnedArgOperand() == V) {
410           if (Visited.insert(I).second)
411             WorkList.push_back(cast<const Instruction>(I));
412         }
413 
414         if (!CB.isArgOperand(&UI)) {
415           US.addRange(I, UnknownRange);
416           break;
417         }
418 
419         unsigned ArgNo = CB.getArgOperandNo(&UI);
420         if (CB.isByValArgument(ArgNo)) {
421           US.addRange(I, getAccessRange(
422                              UI, Ptr,
423                              DL.getTypeStoreSize(CB.getParamByValType(ArgNo))));
424           break;
425         }
426 
427         // FIXME: consult devirt?
428         // Do not follow aliases, otherwise we could inadvertently follow
429         // dso_preemptable aliases or aliases with interposable linkage.
430         const GlobalValue *Callee =
431             dyn_cast<GlobalValue>(CB.getCalledOperand()->stripPointerCasts());
432         if (!Callee) {
433           US.addRange(I, UnknownRange);
434           break;
435         }
436 
437         assert(isa<Function>(Callee) || isa<GlobalAlias>(Callee));
438         ConstantRange Offsets = offsetFrom(UI, Ptr);
439         auto Insert =
440             US.Calls.emplace(CallInfo<GlobalValue>(Callee, ArgNo), Offsets);
441         if (!Insert.second)
442           Insert.first->second = Insert.first->second.unionWith(Offsets);
443         break;
444       }
445 
446       default:
447         if (Visited.insert(I).second)
448           WorkList.push_back(cast<const Instruction>(I));
449       }
450     }
451   }
452 }
453 
454 FunctionInfo<GlobalValue> StackSafetyLocalAnalysis::run() {
455   FunctionInfo<GlobalValue> Info;
456   assert(!F.isDeclaration() &&
457          "Can't run StackSafety on a function declaration");
458 
459   LLVM_DEBUG(dbgs() << "[StackSafety] " << F.getName() << "\n");
460 
461   SmallVector<AllocaInst *, 64> Allocas;
462   for (auto &I : instructions(F))
463     if (auto *AI = dyn_cast<AllocaInst>(&I))
464       Allocas.push_back(AI);
465   StackLifetime SL(F, Allocas, StackLifetime::LivenessType::Must);
466   SL.run();
467 
468   for (auto *AI : Allocas) {
469     auto &UI = Info.Allocas.emplace(AI, PointerSize).first->second;
470     analyzeAllUses(AI, UI, SL);
471   }
472 
473   for (Argument &A : F.args()) {
474     // Non pointers and bypass arguments are not going to be used in any global
475     // processing.
476     if (A.getType()->isPointerTy() && !A.hasByValAttr()) {
477       auto &UI = Info.Params.emplace(A.getArgNo(), PointerSize).first->second;
478       analyzeAllUses(&A, UI, SL);
479     }
480   }
481 
482   LLVM_DEBUG(Info.print(dbgs(), F.getName(), &F));
483   LLVM_DEBUG(dbgs() << "\n[StackSafety] done\n");
484   return Info;
485 }
486 
487 template <typename CalleeTy> class StackSafetyDataFlowAnalysis {
488   using FunctionMap = std::map<const CalleeTy *, FunctionInfo<CalleeTy>>;
489 
490   FunctionMap Functions;
491   const ConstantRange UnknownRange;
492 
493   // Callee-to-Caller multimap.
494   DenseMap<const CalleeTy *, SmallVector<const CalleeTy *, 4>> Callers;
495   SetVector<const CalleeTy *> WorkList;
496 
497   bool updateOneUse(UseInfo<CalleeTy> &US, bool UpdateToFullSet);
498   void updateOneNode(const CalleeTy *Callee, FunctionInfo<CalleeTy> &FS);
499   void updateOneNode(const CalleeTy *Callee) {
500     updateOneNode(Callee, Functions.find(Callee)->second);
501   }
502   void updateAllNodes() {
503     for (auto &F : Functions)
504       updateOneNode(F.first, F.second);
505   }
506   void runDataFlow();
507 #ifndef NDEBUG
508   void verifyFixedPoint();
509 #endif
510 
511 public:
512   StackSafetyDataFlowAnalysis(uint32_t PointerBitWidth, FunctionMap Functions)
513       : Functions(std::move(Functions)),
514         UnknownRange(ConstantRange::getFull(PointerBitWidth)) {}
515 
516   const FunctionMap &run();
517 
518   ConstantRange getArgumentAccessRange(const CalleeTy *Callee, unsigned ParamNo,
519                                        const ConstantRange &Offsets) const;
520 };
521 
522 template <typename CalleeTy>
523 ConstantRange StackSafetyDataFlowAnalysis<CalleeTy>::getArgumentAccessRange(
524     const CalleeTy *Callee, unsigned ParamNo,
525     const ConstantRange &Offsets) const {
526   auto FnIt = Functions.find(Callee);
527   // Unknown callee (outside of LTO domain or an indirect call).
528   if (FnIt == Functions.end())
529     return UnknownRange;
530   auto &FS = FnIt->second;
531   auto ParamIt = FS.Params.find(ParamNo);
532   if (ParamIt == FS.Params.end())
533     return UnknownRange;
534   auto &Access = ParamIt->second.Range;
535   if (Access.isEmptySet())
536     return Access;
537   if (Access.isFullSet())
538     return UnknownRange;
539   return addOverflowNever(Access, Offsets);
540 }
541 
542 template <typename CalleeTy>
543 bool StackSafetyDataFlowAnalysis<CalleeTy>::updateOneUse(UseInfo<CalleeTy> &US,
544                                                          bool UpdateToFullSet) {
545   bool Changed = false;
546   for (auto &KV : US.Calls) {
547     assert(!KV.second.isEmptySet() &&
548            "Param range can't be empty-set, invalid offset range");
549 
550     ConstantRange CalleeRange =
551         getArgumentAccessRange(KV.first.Callee, KV.first.ParamNo, KV.second);
552     if (!US.Range.contains(CalleeRange)) {
553       Changed = true;
554       if (UpdateToFullSet)
555         US.Range = UnknownRange;
556       else
557         US.updateRange(CalleeRange);
558     }
559   }
560   return Changed;
561 }
562 
563 template <typename CalleeTy>
564 void StackSafetyDataFlowAnalysis<CalleeTy>::updateOneNode(
565     const CalleeTy *Callee, FunctionInfo<CalleeTy> &FS) {
566   bool UpdateToFullSet = FS.UpdateCount > StackSafetyMaxIterations;
567   bool Changed = false;
568   for (auto &KV : FS.Params)
569     Changed |= updateOneUse(KV.second, UpdateToFullSet);
570 
571   if (Changed) {
572     LLVM_DEBUG(dbgs() << "=== update [" << FS.UpdateCount
573                       << (UpdateToFullSet ? ", full-set" : "") << "] " << &FS
574                       << "\n");
575     // Callers of this function may need updating.
576     for (auto &CallerID : Callers[Callee])
577       WorkList.insert(CallerID);
578 
579     ++FS.UpdateCount;
580   }
581 }
582 
583 template <typename CalleeTy>
584 void StackSafetyDataFlowAnalysis<CalleeTy>::runDataFlow() {
585   SmallVector<const CalleeTy *, 16> Callees;
586   for (auto &F : Functions) {
587     Callees.clear();
588     auto &FS = F.second;
589     for (auto &KV : FS.Params)
590       for (auto &CS : KV.second.Calls)
591         Callees.push_back(CS.first.Callee);
592 
593     llvm::sort(Callees);
594     Callees.erase(std::unique(Callees.begin(), Callees.end()), Callees.end());
595 
596     for (auto &Callee : Callees)
597       Callers[Callee].push_back(F.first);
598   }
599 
600   updateAllNodes();
601 
602   while (!WorkList.empty()) {
603     const CalleeTy *Callee = WorkList.back();
604     WorkList.pop_back();
605     updateOneNode(Callee);
606   }
607 }
608 
609 #ifndef NDEBUG
610 template <typename CalleeTy>
611 void StackSafetyDataFlowAnalysis<CalleeTy>::verifyFixedPoint() {
612   WorkList.clear();
613   updateAllNodes();
614   assert(WorkList.empty());
615 }
616 #endif
617 
618 template <typename CalleeTy>
619 const typename StackSafetyDataFlowAnalysis<CalleeTy>::FunctionMap &
620 StackSafetyDataFlowAnalysis<CalleeTy>::run() {
621   runDataFlow();
622   LLVM_DEBUG(verifyFixedPoint());
623   return Functions;
624 }
625 
626 FunctionSummary *findCalleeFunctionSummary(ValueInfo VI, StringRef ModuleId) {
627   if (!VI)
628     return nullptr;
629   auto SummaryList = VI.getSummaryList();
630   GlobalValueSummary* S = nullptr;
631   for (const auto& GVS : SummaryList) {
632     if (!GVS->isLive())
633       continue;
634     if (const AliasSummary *AS = dyn_cast<AliasSummary>(GVS.get()))
635       if (!AS->hasAliasee())
636         continue;
637     if (!isa<FunctionSummary>(GVS->getBaseObject()))
638       continue;
639     if (GlobalValue::isLocalLinkage(GVS->linkage())) {
640       if (GVS->modulePath() == ModuleId) {
641         S = GVS.get();
642         break;
643       }
644     } else if (GlobalValue::isExternalLinkage(GVS->linkage())) {
645       if (S) {
646         ++NumIndexCalleeMultipleExternal;
647         return nullptr;
648       }
649       S = GVS.get();
650     } else if (GlobalValue::isWeakLinkage(GVS->linkage())) {
651       if (S) {
652         ++NumIndexCalleeMultipleWeak;
653         return nullptr;
654       }
655       S = GVS.get();
656     } else if (GlobalValue::isAvailableExternallyLinkage(GVS->linkage()) ||
657                GlobalValue::isLinkOnceLinkage(GVS->linkage())) {
658       if (SummaryList.size() == 1)
659         S = GVS.get();
660       // According thinLTOResolvePrevailingGUID these are unlikely prevailing.
661     } else {
662       ++NumIndexCalleeUnhandled;
663     }
664   };
665   while (S) {
666     if (!S->isLive() || !S->isDSOLocal())
667       return nullptr;
668     if (FunctionSummary *FS = dyn_cast<FunctionSummary>(S))
669       return FS;
670     AliasSummary *AS = dyn_cast<AliasSummary>(S);
671     if (!AS || !AS->hasAliasee())
672       return nullptr;
673     S = AS->getBaseObject();
674     if (S == AS)
675       return nullptr;
676   }
677   return nullptr;
678 }
679 
680 const Function *findCalleeInModule(const GlobalValue *GV) {
681   while (GV) {
682     if (GV->isDeclaration() || GV->isInterposable() || !GV->isDSOLocal())
683       return nullptr;
684     if (const Function *F = dyn_cast<Function>(GV))
685       return F;
686     const GlobalAlias *A = dyn_cast<GlobalAlias>(GV);
687     if (!A)
688       return nullptr;
689     GV = A->getBaseObject();
690     if (GV == A)
691       return nullptr;
692   }
693   return nullptr;
694 }
695 
696 const ConstantRange *findParamAccess(const FunctionSummary &FS,
697                                      uint32_t ParamNo) {
698   assert(FS.isLive());
699   assert(FS.isDSOLocal());
700   for (auto &PS : FS.paramAccesses())
701     if (ParamNo == PS.ParamNo)
702       return &PS.Use;
703   return nullptr;
704 }
705 
706 void resolveAllCalls(UseInfo<GlobalValue> &Use,
707                      const ModuleSummaryIndex *Index) {
708   ConstantRange FullSet(Use.Range.getBitWidth(), true);
709   // Move Use.Calls to a temp storage and repopulate - don't use std::move as it
710   // leaves Use.Calls in an undefined state.
711   UseInfo<GlobalValue>::CallsTy TmpCalls;
712   std::swap(TmpCalls, Use.Calls);
713   for (const auto &C : TmpCalls) {
714     const Function *F = findCalleeInModule(C.first.Callee);
715     if (F) {
716       Use.Calls.emplace(CallInfo<GlobalValue>(F, C.first.ParamNo), C.second);
717       continue;
718     }
719 
720     if (!Index)
721       return Use.updateRange(FullSet);
722     FunctionSummary *FS =
723         findCalleeFunctionSummary(Index->getValueInfo(C.first.Callee->getGUID()),
724                                   C.first.Callee->getParent()->getModuleIdentifier());
725     ++NumModuleCalleeLookupTotal;
726     if (!FS) {
727       ++NumModuleCalleeLookupFailed;
728       return Use.updateRange(FullSet);
729     }
730     const ConstantRange *Found = findParamAccess(*FS, C.first.ParamNo);
731     if (!Found || Found->isFullSet())
732       return Use.updateRange(FullSet);
733     ConstantRange Access = Found->sextOrTrunc(Use.Range.getBitWidth());
734     if (!Access.isEmptySet())
735       Use.updateRange(addOverflowNever(Access, C.second));
736   }
737 }
738 
739 GVToSSI createGlobalStackSafetyInfo(
740     std::map<const GlobalValue *, FunctionInfo<GlobalValue>> Functions,
741     const ModuleSummaryIndex *Index) {
742   GVToSSI SSI;
743   if (Functions.empty())
744     return SSI;
745 
746   // FIXME: Simplify printing and remove copying here.
747   auto Copy = Functions;
748 
749   for (auto &FnKV : Copy)
750     for (auto &KV : FnKV.second.Params) {
751       resolveAllCalls(KV.second, Index);
752       if (KV.second.Range.isFullSet())
753         KV.second.Calls.clear();
754     }
755 
756   uint32_t PointerSize = Copy.begin()
757                              ->first->getParent()
758                              ->getDataLayout()
759                              .getMaxPointerSizeInBits();
760   StackSafetyDataFlowAnalysis<GlobalValue> SSDFA(PointerSize, std::move(Copy));
761 
762   for (auto &F : SSDFA.run()) {
763     auto FI = F.second;
764     auto &SrcF = Functions[F.first];
765     for (auto &KV : FI.Allocas) {
766       auto &A = KV.second;
767       resolveAllCalls(A, Index);
768       for (auto &C : A.Calls) {
769         A.updateRange(SSDFA.getArgumentAccessRange(C.first.Callee,
770                                                    C.first.ParamNo, C.second));
771       }
772       // FIXME: This is needed only to preserve calls in print() results.
773       A.Calls = SrcF.Allocas.find(KV.first)->second.Calls;
774     }
775     for (auto &KV : FI.Params) {
776       auto &P = KV.second;
777       P.Calls = SrcF.Params.find(KV.first)->second.Calls;
778     }
779     SSI[F.first] = std::move(FI);
780   }
781 
782   return SSI;
783 }
784 
785 } // end anonymous namespace
786 
787 StackSafetyInfo::StackSafetyInfo() = default;
788 
789 StackSafetyInfo::StackSafetyInfo(Function *F,
790                                  std::function<ScalarEvolution &()> GetSE)
791     : F(F), GetSE(GetSE) {}
792 
793 StackSafetyInfo::StackSafetyInfo(StackSafetyInfo &&) = default;
794 
795 StackSafetyInfo &StackSafetyInfo::operator=(StackSafetyInfo &&) = default;
796 
797 StackSafetyInfo::~StackSafetyInfo() = default;
798 
799 const StackSafetyInfo::InfoTy &StackSafetyInfo::getInfo() const {
800   if (!Info) {
801     StackSafetyLocalAnalysis SSLA(*F, GetSE());
802     Info.reset(new InfoTy{SSLA.run()});
803   }
804   return *Info;
805 }
806 
807 void StackSafetyInfo::print(raw_ostream &O) const {
808   getInfo().Info.print(O, F->getName(), dyn_cast<Function>(F));
809   O << "\n";
810 }
811 
812 const StackSafetyGlobalInfo::InfoTy &StackSafetyGlobalInfo::getInfo() const {
813   if (!Info) {
814     std::map<const GlobalValue *, FunctionInfo<GlobalValue>> Functions;
815     for (auto &F : M->functions()) {
816       if (!F.isDeclaration()) {
817         auto FI = GetSSI(F).getInfo().Info;
818         Functions.emplace(&F, std::move(FI));
819       }
820     }
821     Info.reset(new InfoTy{
822         createGlobalStackSafetyInfo(std::move(Functions), Index), {}, {}});
823 
824     std::map<const Instruction *, bool> AccessIsUnsafe;
825     for (auto &FnKV : Info->Info) {
826       for (auto &KV : FnKV.second.Allocas) {
827         ++NumAllocaTotal;
828         const AllocaInst *AI = KV.first;
829         auto AIRange = getStaticAllocaSizeRange(*AI);
830         if (AIRange.contains(KV.second.Range)) {
831           Info->SafeAllocas.insert(AI);
832           ++NumAllocaStackSafe;
833         }
834         for (const auto &A : KV.second.Accesses)
835           AccessIsUnsafe[A.first] |= !AIRange.contains(A.second);
836       }
837     }
838 
839     for (const auto &KV : AccessIsUnsafe)
840       if (!KV.second)
841         Info->SafeAccesses.insert(KV.first);
842 
843     if (StackSafetyPrint)
844       print(errs());
845   }
846   return *Info;
847 }
848 
849 std::vector<FunctionSummary::ParamAccess>
850 StackSafetyInfo::getParamAccesses(ModuleSummaryIndex &Index) const {
851   // Implementation transforms internal representation of parameter information
852   // into FunctionSummary format.
853   std::vector<FunctionSummary::ParamAccess> ParamAccesses;
854   for (const auto &KV : getInfo().Info.Params) {
855     auto &PS = KV.second;
856     // Parameter accessed by any or unknown offset, represented as FullSet by
857     // StackSafety, is handled as the parameter for which we have no
858     // StackSafety info at all. So drop it to reduce summary size.
859     if (PS.Range.isFullSet())
860       continue;
861 
862     ParamAccesses.emplace_back(KV.first, PS.Range);
863     FunctionSummary::ParamAccess &Param = ParamAccesses.back();
864 
865     Param.Calls.reserve(PS.Calls.size());
866     for (auto &C : PS.Calls) {
867       // Parameter forwarded into another function by any or unknown offset
868       // will make ParamAccess::Range as FullSet anyway. So we can drop the
869       // entire parameter like we did above.
870       // TODO(vitalybuka): Return already filtered parameters from getInfo().
871       if (C.second.isFullSet()) {
872         ParamAccesses.pop_back();
873         break;
874       }
875       Param.Calls.emplace_back(C.first.ParamNo,
876                                Index.getOrInsertValueInfo(C.first.Callee),
877                                C.second);
878     }
879   }
880   for (FunctionSummary::ParamAccess &Param : ParamAccesses) {
881     sort(Param.Calls, [](const FunctionSummary::ParamAccess::Call &L,
882                          const FunctionSummary::ParamAccess::Call &R) {
883       return std::tie(L.ParamNo, L.Callee) < std::tie(R.ParamNo, R.Callee);
884     });
885   }
886   return ParamAccesses;
887 }
888 
889 StackSafetyGlobalInfo::StackSafetyGlobalInfo() = default;
890 
891 StackSafetyGlobalInfo::StackSafetyGlobalInfo(
892     Module *M, std::function<const StackSafetyInfo &(Function &F)> GetSSI,
893     const ModuleSummaryIndex *Index)
894     : M(M), GetSSI(GetSSI), Index(Index) {
895   if (StackSafetyRun)
896     getInfo();
897 }
898 
899 StackSafetyGlobalInfo::StackSafetyGlobalInfo(StackSafetyGlobalInfo &&) =
900     default;
901 
902 StackSafetyGlobalInfo &
903 StackSafetyGlobalInfo::operator=(StackSafetyGlobalInfo &&) = default;
904 
905 StackSafetyGlobalInfo::~StackSafetyGlobalInfo() = default;
906 
907 bool StackSafetyGlobalInfo::isSafe(const AllocaInst &AI) const {
908   const auto &Info = getInfo();
909   return Info.SafeAllocas.count(&AI);
910 }
911 
912 bool StackSafetyGlobalInfo::accessIsSafe(const Instruction &I) const {
913   const auto &Info = getInfo();
914   return Info.SafeAccesses.count(&I);
915 }
916 
917 void StackSafetyGlobalInfo::print(raw_ostream &O) const {
918   auto &SSI = getInfo().Info;
919   if (SSI.empty())
920     return;
921   const Module &M = *SSI.begin()->first->getParent();
922   for (auto &F : M.functions()) {
923     if (!F.isDeclaration()) {
924       SSI.find(&F)->second.print(O, F.getName(), &F);
925       O << "    safe accesses:"
926         << "\n";
927       for (const auto &I : instructions(F)) {
928         if (accessIsSafe(I)) {
929           O << "     " << I << "\n";
930         }
931       }
932       O << "\n";
933     }
934   }
935 }
936 
937 LLVM_DUMP_METHOD void StackSafetyGlobalInfo::dump() const { print(dbgs()); }
938 
939 AnalysisKey StackSafetyAnalysis::Key;
940 
941 StackSafetyInfo StackSafetyAnalysis::run(Function &F,
942                                          FunctionAnalysisManager &AM) {
943   return StackSafetyInfo(&F, [&AM, &F]() -> ScalarEvolution & {
944     return AM.getResult<ScalarEvolutionAnalysis>(F);
945   });
946 }
947 
948 PreservedAnalyses StackSafetyPrinterPass::run(Function &F,
949                                               FunctionAnalysisManager &AM) {
950   OS << "'Stack Safety Local Analysis' for function '" << F.getName() << "'\n";
951   AM.getResult<StackSafetyAnalysis>(F).print(OS);
952   return PreservedAnalyses::all();
953 }
954 
955 char StackSafetyInfoWrapperPass::ID = 0;
956 
957 StackSafetyInfoWrapperPass::StackSafetyInfoWrapperPass() : FunctionPass(ID) {
958   initializeStackSafetyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
959 }
960 
961 void StackSafetyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
962   AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
963   AU.setPreservesAll();
964 }
965 
966 void StackSafetyInfoWrapperPass::print(raw_ostream &O, const Module *M) const {
967   SSI.print(O);
968 }
969 
970 bool StackSafetyInfoWrapperPass::runOnFunction(Function &F) {
971   auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
972   SSI = {&F, [SE]() -> ScalarEvolution & { return *SE; }};
973   return false;
974 }
975 
976 AnalysisKey StackSafetyGlobalAnalysis::Key;
977 
978 StackSafetyGlobalInfo
979 StackSafetyGlobalAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
980   // FIXME: Lookup Module Summary.
981   FunctionAnalysisManager &FAM =
982       AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
983   return {&M,
984           [&FAM](Function &F) -> const StackSafetyInfo & {
985             return FAM.getResult<StackSafetyAnalysis>(F);
986           },
987           nullptr};
988 }
989 
990 PreservedAnalyses StackSafetyGlobalPrinterPass::run(Module &M,
991                                                     ModuleAnalysisManager &AM) {
992   OS << "'Stack Safety Analysis' for module '" << M.getName() << "'\n";
993   AM.getResult<StackSafetyGlobalAnalysis>(M).print(OS);
994   return PreservedAnalyses::all();
995 }
996 
997 char StackSafetyGlobalInfoWrapperPass::ID = 0;
998 
999 StackSafetyGlobalInfoWrapperPass::StackSafetyGlobalInfoWrapperPass()
1000     : ModulePass(ID) {
1001   initializeStackSafetyGlobalInfoWrapperPassPass(
1002       *PassRegistry::getPassRegistry());
1003 }
1004 
1005 StackSafetyGlobalInfoWrapperPass::~StackSafetyGlobalInfoWrapperPass() = default;
1006 
1007 void StackSafetyGlobalInfoWrapperPass::print(raw_ostream &O,
1008                                              const Module *M) const {
1009   SSGI.print(O);
1010 }
1011 
1012 void StackSafetyGlobalInfoWrapperPass::getAnalysisUsage(
1013     AnalysisUsage &AU) const {
1014   AU.setPreservesAll();
1015   AU.addRequired<StackSafetyInfoWrapperPass>();
1016 }
1017 
1018 bool StackSafetyGlobalInfoWrapperPass::runOnModule(Module &M) {
1019   const ModuleSummaryIndex *ImportSummary = nullptr;
1020   if (auto *IndexWrapperPass =
1021           getAnalysisIfAvailable<ImmutableModuleSummaryIndexWrapperPass>())
1022     ImportSummary = IndexWrapperPass->getIndex();
1023 
1024   SSGI = {&M,
1025           [this](Function &F) -> const StackSafetyInfo & {
1026             return getAnalysis<StackSafetyInfoWrapperPass>(F).getResult();
1027           },
1028           ImportSummary};
1029   return false;
1030 }
1031 
1032 bool llvm::needsParamAccessSummary(const Module &M) {
1033   if (StackSafetyRun)
1034     return true;
1035   for (auto &F : M.functions())
1036     if (F.hasFnAttribute(Attribute::SanitizeMemTag))
1037       return true;
1038   return false;
1039 }
1040 
1041 void llvm::generateParamAccessSummary(ModuleSummaryIndex &Index) {
1042   if (!Index.hasParamAccess())
1043     return;
1044   const ConstantRange FullSet(FunctionSummary::ParamAccess::RangeWidth, true);
1045 
1046   auto CountParamAccesses = [&](auto &Stat) {
1047     if (!AreStatisticsEnabled())
1048       return;
1049     for (auto &GVS : Index)
1050       for (auto &GV : GVS.second.SummaryList)
1051         if (FunctionSummary *FS = dyn_cast<FunctionSummary>(GV.get()))
1052           Stat += FS->paramAccesses().size();
1053   };
1054 
1055   CountParamAccesses(NumCombinedParamAccessesBefore);
1056 
1057   std::map<const FunctionSummary *, FunctionInfo<FunctionSummary>> Functions;
1058 
1059   // Convert the ModuleSummaryIndex to a FunctionMap
1060   for (auto &GVS : Index) {
1061     for (auto &GV : GVS.second.SummaryList) {
1062       FunctionSummary *FS = dyn_cast<FunctionSummary>(GV.get());
1063       if (!FS || FS->paramAccesses().empty())
1064         continue;
1065       if (FS->isLive() && FS->isDSOLocal()) {
1066         FunctionInfo<FunctionSummary> FI;
1067         for (auto &PS : FS->paramAccesses()) {
1068           auto &US =
1069               FI.Params
1070                   .emplace(PS.ParamNo, FunctionSummary::ParamAccess::RangeWidth)
1071                   .first->second;
1072           US.Range = PS.Use;
1073           for (auto &Call : PS.Calls) {
1074             assert(!Call.Offsets.isFullSet());
1075             FunctionSummary *S =
1076                 findCalleeFunctionSummary(Call.Callee, FS->modulePath());
1077             ++NumCombinedCalleeLookupTotal;
1078             if (!S) {
1079               ++NumCombinedCalleeLookupFailed;
1080               US.Range = FullSet;
1081               US.Calls.clear();
1082               break;
1083             }
1084             US.Calls.emplace(CallInfo<FunctionSummary>(S, Call.ParamNo),
1085                              Call.Offsets);
1086           }
1087         }
1088         Functions.emplace(FS, std::move(FI));
1089       }
1090       // Reset data for all summaries. Alive and DSO local will be set back from
1091       // of data flow results below. Anything else will not be accessed
1092       // by ThinLTO backend, so we can save on bitcode size.
1093       FS->setParamAccesses({});
1094     }
1095   }
1096   NumCombinedDataFlowNodes += Functions.size();
1097   StackSafetyDataFlowAnalysis<FunctionSummary> SSDFA(
1098       FunctionSummary::ParamAccess::RangeWidth, std::move(Functions));
1099   for (auto &KV : SSDFA.run()) {
1100     std::vector<FunctionSummary::ParamAccess> NewParams;
1101     NewParams.reserve(KV.second.Params.size());
1102     for (auto &Param : KV.second.Params) {
1103       // It's not needed as FullSet is processed the same as a missing value.
1104       if (Param.second.Range.isFullSet())
1105         continue;
1106       NewParams.emplace_back();
1107       FunctionSummary::ParamAccess &New = NewParams.back();
1108       New.ParamNo = Param.first;
1109       New.Use = Param.second.Range; // Only range is needed.
1110     }
1111     const_cast<FunctionSummary *>(KV.first)->setParamAccesses(
1112         std::move(NewParams));
1113   }
1114 
1115   CountParamAccesses(NumCombinedParamAccessesAfter);
1116 }
1117 
1118 static const char LocalPassArg[] = "stack-safety-local";
1119 static const char LocalPassName[] = "Stack Safety Local Analysis";
1120 INITIALIZE_PASS_BEGIN(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName,
1121                       false, true)
1122 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
1123 INITIALIZE_PASS_END(StackSafetyInfoWrapperPass, LocalPassArg, LocalPassName,
1124                     false, true)
1125 
1126 static const char GlobalPassName[] = "Stack Safety Analysis";
1127 INITIALIZE_PASS_BEGIN(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE,
1128                       GlobalPassName, false, true)
1129 INITIALIZE_PASS_DEPENDENCY(StackSafetyInfoWrapperPass)
1130 INITIALIZE_PASS_DEPENDENCY(ImmutableModuleSummaryIndexWrapperPass)
1131 INITIALIZE_PASS_END(StackSafetyGlobalInfoWrapperPass, DEBUG_TYPE,
1132                     GlobalPassName, false, true)
1133