1 //== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines a basic region store model. In this model, we do have field
10 // sensitivity. But we assume nothing about the heap shape. So recursive data
11 // structures are largely ignored. Basically we do 1-limiting analysis.
12 // Parameter pointers are assumed with no aliasing. Pointee objects of
13 // parameters are created lazily.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/CharUnits.h"
19 #include "clang/ASTMatchers/ASTMatchFinder.h"
20 #include "clang/Analysis/Analyses/LiveVariables.h"
21 #include "clang/Analysis/AnalysisDeclContext.h"
22 #include "clang/Basic/JsonSupport.h"
23 #include "clang/Basic/TargetInfo.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
28 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
29 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
30 #include "llvm/ADT/ImmutableMap.h"
31 #include "llvm/ADT/Optional.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include <utility>
34 
35 using namespace clang;
36 using namespace ento;
37 
38 //===----------------------------------------------------------------------===//
39 // Representation of binding keys.
40 //===----------------------------------------------------------------------===//
41 
42 namespace {
43 class BindingKey {
44 public:
45   enum Kind { Default = 0x0, Direct = 0x1 };
46 private:
47   enum { Symbolic = 0x2 };
48 
49   llvm::PointerIntPair<const MemRegion *, 2> P;
50   uint64_t Data;
51 
52   /// Create a key for a binding to region \p r, which has a symbolic offset
53   /// from region \p Base.
54   explicit BindingKey(const SubRegion *r, const SubRegion *Base, Kind k)
55     : P(r, k | Symbolic), Data(reinterpret_cast<uintptr_t>(Base)) {
56     assert(r && Base && "Must have known regions.");
57     assert(getConcreteOffsetRegion() == Base && "Failed to store base region");
58   }
59 
60   /// Create a key for a binding at \p offset from base region \p r.
61   explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
62     : P(r, k), Data(offset) {
63     assert(r && "Must have known regions.");
64     assert(getOffset() == offset && "Failed to store offset");
65     assert((r == r->getBaseRegion() || isa<ObjCIvarRegion>(r) ||
66             isa <CXXDerivedObjectRegion>(r)) &&
67            "Not a base");
68   }
69 public:
70 
71   bool isDirect() const { return P.getInt() & Direct; }
72   bool hasSymbolicOffset() const { return P.getInt() & Symbolic; }
73 
74   const MemRegion *getRegion() const { return P.getPointer(); }
75   uint64_t getOffset() const {
76     assert(!hasSymbolicOffset());
77     return Data;
78   }
79 
80   const SubRegion *getConcreteOffsetRegion() const {
81     assert(hasSymbolicOffset());
82     return reinterpret_cast<const SubRegion *>(static_cast<uintptr_t>(Data));
83   }
84 
85   const MemRegion *getBaseRegion() const {
86     if (hasSymbolicOffset())
87       return getConcreteOffsetRegion()->getBaseRegion();
88     return getRegion()->getBaseRegion();
89   }
90 
91   void Profile(llvm::FoldingSetNodeID& ID) const {
92     ID.AddPointer(P.getOpaqueValue());
93     ID.AddInteger(Data);
94   }
95 
96   static BindingKey Make(const MemRegion *R, Kind k);
97 
98   bool operator<(const BindingKey &X) const {
99     if (P.getOpaqueValue() < X.P.getOpaqueValue())
100       return true;
101     if (P.getOpaqueValue() > X.P.getOpaqueValue())
102       return false;
103     return Data < X.Data;
104   }
105 
106   bool operator==(const BindingKey &X) const {
107     return P.getOpaqueValue() == X.P.getOpaqueValue() &&
108            Data == X.Data;
109   }
110 
111   LLVM_DUMP_METHOD void dump() const;
112 };
113 } // end anonymous namespace
114 
115 BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
116   const RegionOffset &RO = R->getAsOffset();
117   if (RO.hasSymbolicOffset())
118     return BindingKey(cast<SubRegion>(R), cast<SubRegion>(RO.getRegion()), k);
119 
120   return BindingKey(RO.getRegion(), RO.getOffset(), k);
121 }
122 
123 namespace llvm {
124 static inline raw_ostream &operator<<(raw_ostream &Out, BindingKey K) {
125   Out << "\"kind\": \"" << (K.isDirect() ? "Direct" : "Default")
126       << "\", \"offset\": ";
127 
128   if (!K.hasSymbolicOffset())
129     Out << K.getOffset();
130   else
131     Out << "null";
132 
133   return Out;
134 }
135 
136 } // namespace llvm
137 
138 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
139 void BindingKey::dump() const { llvm::errs() << *this; }
140 #endif
141 
142 //===----------------------------------------------------------------------===//
143 // Actual Store type.
144 //===----------------------------------------------------------------------===//
145 
146 typedef llvm::ImmutableMap<BindingKey, SVal>    ClusterBindings;
147 typedef llvm::ImmutableMapRef<BindingKey, SVal> ClusterBindingsRef;
148 typedef std::pair<BindingKey, SVal> BindingPair;
149 
150 typedef llvm::ImmutableMap<const MemRegion *, ClusterBindings>
151         RegionBindings;
152 
153 namespace {
154 class RegionBindingsRef : public llvm::ImmutableMapRef<const MemRegion *,
155                                  ClusterBindings> {
156   ClusterBindings::Factory *CBFactory;
157 
158   // This flag indicates whether the current bindings are within the analysis
159   // that has started from main(). It affects how we perform loads from
160   // global variables that have initializers: if we have observed the
161   // program execution from the start and we know that these variables
162   // have not been overwritten yet, we can be sure that their initializers
163   // are still relevant. This flag never gets changed when the bindings are
164   // updated, so it could potentially be moved into RegionStoreManager
165   // (as if it's the same bindings but a different loading procedure)
166   // however that would have made the manager needlessly stateful.
167   bool IsMainAnalysis;
168 
169 public:
170   typedef llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>
171           ParentTy;
172 
173   RegionBindingsRef(ClusterBindings::Factory &CBFactory,
174                     const RegionBindings::TreeTy *T,
175                     RegionBindings::TreeTy::Factory *F,
176                     bool IsMainAnalysis)
177       : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(T, F),
178         CBFactory(&CBFactory), IsMainAnalysis(IsMainAnalysis) {}
179 
180   RegionBindingsRef(const ParentTy &P,
181                     ClusterBindings::Factory &CBFactory,
182                     bool IsMainAnalysis)
183       : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(P),
184         CBFactory(&CBFactory), IsMainAnalysis(IsMainAnalysis) {}
185 
186   RegionBindingsRef add(key_type_ref K, data_type_ref D) const {
187     return RegionBindingsRef(static_cast<const ParentTy *>(this)->add(K, D),
188                              *CBFactory, IsMainAnalysis);
189   }
190 
191   RegionBindingsRef remove(key_type_ref K) const {
192     return RegionBindingsRef(static_cast<const ParentTy *>(this)->remove(K),
193                              *CBFactory, IsMainAnalysis);
194   }
195 
196   RegionBindingsRef addBinding(BindingKey K, SVal V) const;
197 
198   RegionBindingsRef addBinding(const MemRegion *R,
199                                BindingKey::Kind k, SVal V) const;
200 
201   const SVal *lookup(BindingKey K) const;
202   const SVal *lookup(const MemRegion *R, BindingKey::Kind k) const;
203   using llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>::lookup;
204 
205   RegionBindingsRef removeBinding(BindingKey K);
206 
207   RegionBindingsRef removeBinding(const MemRegion *R,
208                                   BindingKey::Kind k);
209 
210   RegionBindingsRef removeBinding(const MemRegion *R) {
211     return removeBinding(R, BindingKey::Direct).
212            removeBinding(R, BindingKey::Default);
213   }
214 
215   Optional<SVal> getDirectBinding(const MemRegion *R) const;
216 
217   /// getDefaultBinding - Returns an SVal* representing an optional default
218   ///  binding associated with a region and its subregions.
219   Optional<SVal> getDefaultBinding(const MemRegion *R) const;
220 
221   /// Return the internal tree as a Store.
222   Store asStore() const {
223     llvm::PointerIntPair<Store, 1, bool> Ptr = {
224         asImmutableMap().getRootWithoutRetain(), IsMainAnalysis};
225     return reinterpret_cast<Store>(Ptr.getOpaqueValue());
226   }
227 
228   bool isMainAnalysis() const {
229     return IsMainAnalysis;
230   }
231 
232   void printJson(raw_ostream &Out, const char *NL = "\n",
233                  unsigned int Space = 0, bool IsDot = false) const {
234     for (iterator I = begin(); I != end(); ++I) {
235       // TODO: We might need a .printJson for I.getKey() as well.
236       Indent(Out, Space, IsDot)
237           << "{ \"cluster\": \"" << I.getKey() << "\", \"pointer\": \""
238           << (const void *)I.getKey() << "\", \"items\": [" << NL;
239 
240       ++Space;
241       const ClusterBindings &CB = I.getData();
242       for (ClusterBindings::iterator CI = CB.begin(); CI != CB.end(); ++CI) {
243         Indent(Out, Space, IsDot) << "{ " << CI.getKey() << ", \"value\": ";
244         CI.getData().printJson(Out, /*AddQuotes=*/true);
245         Out << " }";
246         if (std::next(CI) != CB.end())
247           Out << ',';
248         Out << NL;
249       }
250 
251       --Space;
252       Indent(Out, Space, IsDot) << "]}";
253       if (std::next(I) != end())
254         Out << ',';
255       Out << NL;
256     }
257   }
258 
259   LLVM_DUMP_METHOD void dump() const { printJson(llvm::errs()); }
260 };
261 } // end anonymous namespace
262 
263 typedef const RegionBindingsRef& RegionBindingsConstRef;
264 
265 Optional<SVal> RegionBindingsRef::getDirectBinding(const MemRegion *R) const {
266   return Optional<SVal>::create(lookup(R, BindingKey::Direct));
267 }
268 
269 Optional<SVal> RegionBindingsRef::getDefaultBinding(const MemRegion *R) const {
270   return Optional<SVal>::create(lookup(R, BindingKey::Default));
271 }
272 
273 RegionBindingsRef RegionBindingsRef::addBinding(BindingKey K, SVal V) const {
274   const MemRegion *Base = K.getBaseRegion();
275 
276   const ClusterBindings *ExistingCluster = lookup(Base);
277   ClusterBindings Cluster =
278       (ExistingCluster ? *ExistingCluster : CBFactory->getEmptyMap());
279 
280   ClusterBindings NewCluster = CBFactory->add(Cluster, K, V);
281   return add(Base, NewCluster);
282 }
283 
284 
285 RegionBindingsRef RegionBindingsRef::addBinding(const MemRegion *R,
286                                                 BindingKey::Kind k,
287                                                 SVal V) const {
288   return addBinding(BindingKey::Make(R, k), V);
289 }
290 
291 const SVal *RegionBindingsRef::lookup(BindingKey K) const {
292   const ClusterBindings *Cluster = lookup(K.getBaseRegion());
293   if (!Cluster)
294     return nullptr;
295   return Cluster->lookup(K);
296 }
297 
298 const SVal *RegionBindingsRef::lookup(const MemRegion *R,
299                                       BindingKey::Kind k) const {
300   return lookup(BindingKey::Make(R, k));
301 }
302 
303 RegionBindingsRef RegionBindingsRef::removeBinding(BindingKey K) {
304   const MemRegion *Base = K.getBaseRegion();
305   const ClusterBindings *Cluster = lookup(Base);
306   if (!Cluster)
307     return *this;
308 
309   ClusterBindings NewCluster = CBFactory->remove(*Cluster, K);
310   if (NewCluster.isEmpty())
311     return remove(Base);
312   return add(Base, NewCluster);
313 }
314 
315 RegionBindingsRef RegionBindingsRef::removeBinding(const MemRegion *R,
316                                                 BindingKey::Kind k){
317   return removeBinding(BindingKey::Make(R, k));
318 }
319 
320 //===----------------------------------------------------------------------===//
321 // Fine-grained control of RegionStoreManager.
322 //===----------------------------------------------------------------------===//
323 
324 namespace {
325 struct minimal_features_tag {};
326 struct maximal_features_tag {};
327 
328 class RegionStoreFeatures {
329   bool SupportsFields;
330 public:
331   RegionStoreFeatures(minimal_features_tag) :
332     SupportsFields(false) {}
333 
334   RegionStoreFeatures(maximal_features_tag) :
335     SupportsFields(true) {}
336 
337   void enableFields(bool t) { SupportsFields = t; }
338 
339   bool supportsFields() const { return SupportsFields; }
340 };
341 }
342 
343 //===----------------------------------------------------------------------===//
344 // Main RegionStore logic.
345 //===----------------------------------------------------------------------===//
346 
347 namespace {
348 class InvalidateRegionsWorker;
349 
350 class RegionStoreManager : public StoreManager {
351 public:
352   const RegionStoreFeatures Features;
353 
354   RegionBindings::Factory RBFactory;
355   mutable ClusterBindings::Factory CBFactory;
356 
357   typedef std::vector<SVal> SValListTy;
358 private:
359   typedef llvm::DenseMap<const LazyCompoundValData *,
360                          SValListTy> LazyBindingsMapTy;
361   LazyBindingsMapTy LazyBindingsMap;
362 
363   /// The largest number of fields a struct can have and still be
364   /// considered "small".
365   ///
366   /// This is currently used to decide whether or not it is worth "forcing" a
367   /// LazyCompoundVal on bind.
368   ///
369   /// This is controlled by 'region-store-small-struct-limit' option.
370   /// To disable all small-struct-dependent behavior, set the option to "0".
371   unsigned SmallStructLimit;
372 
373   /// A helper used to populate the work list with the given set of
374   /// regions.
375   void populateWorkList(InvalidateRegionsWorker &W,
376                         ArrayRef<SVal> Values,
377                         InvalidatedRegions *TopLevelRegions);
378 
379 public:
380   RegionStoreManager(ProgramStateManager& mgr, const RegionStoreFeatures &f)
381     : StoreManager(mgr), Features(f),
382       RBFactory(mgr.getAllocator()), CBFactory(mgr.getAllocator()),
383       SmallStructLimit(0) {
384     SubEngine &Eng = StateMgr.getOwningEngine();
385     AnalyzerOptions &Options = Eng.getAnalysisManager().options;
386     SmallStructLimit = Options.RegionStoreSmallStructLimit;
387   }
388 
389 
390   /// setImplicitDefaultValue - Set the default binding for the provided
391   ///  MemRegion to the value implicitly defined for compound literals when
392   ///  the value is not specified.
393   RegionBindingsRef setImplicitDefaultValue(RegionBindingsConstRef B,
394                                             const MemRegion *R, QualType T);
395 
396   /// ArrayToPointer - Emulates the "decay" of an array to a pointer
397   ///  type.  'Array' represents the lvalue of the array being decayed
398   ///  to a pointer, and the returned SVal represents the decayed
399   ///  version of that lvalue (i.e., a pointer to the first element of
400   ///  the array).  This is called by ExprEngine when evaluating
401   ///  casts from arrays to pointers.
402   SVal ArrayToPointer(Loc Array, QualType ElementTy) override;
403 
404   /// Creates the Store that correctly represents memory contents before
405   /// the beginning of the analysis of the given top-level stack frame.
406   StoreRef getInitialStore(const LocationContext *InitLoc) override {
407     bool IsMainAnalysis = false;
408     if (const auto *FD = dyn_cast<FunctionDecl>(InitLoc->getDecl()))
409       IsMainAnalysis = FD->isMain() && !Ctx.getLangOpts().CPlusPlus;
410     return StoreRef(RegionBindingsRef(
411         RegionBindingsRef::ParentTy(RBFactory.getEmptyMap(), RBFactory),
412         CBFactory, IsMainAnalysis).asStore(), *this);
413   }
414 
415   //===-------------------------------------------------------------------===//
416   // Binding values to regions.
417   //===-------------------------------------------------------------------===//
418   RegionBindingsRef invalidateGlobalRegion(MemRegion::Kind K,
419                                            const Expr *Ex,
420                                            unsigned Count,
421                                            const LocationContext *LCtx,
422                                            RegionBindingsRef B,
423                                            InvalidatedRegions *Invalidated);
424 
425   StoreRef invalidateRegions(Store store,
426                              ArrayRef<SVal> Values,
427                              const Expr *E, unsigned Count,
428                              const LocationContext *LCtx,
429                              const CallEvent *Call,
430                              InvalidatedSymbols &IS,
431                              RegionAndSymbolInvalidationTraits &ITraits,
432                              InvalidatedRegions *Invalidated,
433                              InvalidatedRegions *InvalidatedTopLevel) override;
434 
435   bool scanReachableSymbols(Store S, const MemRegion *R,
436                             ScanReachableSymbols &Callbacks) override;
437 
438   RegionBindingsRef removeSubRegionBindings(RegionBindingsConstRef B,
439                                             const SubRegion *R);
440 
441 public: // Part of public interface to class.
442 
443   StoreRef Bind(Store store, Loc LV, SVal V) override {
444     return StoreRef(bind(getRegionBindings(store), LV, V).asStore(), *this);
445   }
446 
447   RegionBindingsRef bind(RegionBindingsConstRef B, Loc LV, SVal V);
448 
449   // BindDefaultInitial is only used to initialize a region with
450   // a default value.
451   StoreRef BindDefaultInitial(Store store, const MemRegion *R,
452                               SVal V) override {
453     RegionBindingsRef B = getRegionBindings(store);
454     // Use other APIs when you have to wipe the region that was initialized
455     // earlier.
456     assert(!(B.getDefaultBinding(R) || B.getDirectBinding(R)) &&
457            "Double initialization!");
458     B = B.addBinding(BindingKey::Make(R, BindingKey::Default), V);
459     return StoreRef(B.asImmutableMap().getRootWithoutRetain(), *this);
460   }
461 
462   // BindDefaultZero is used for zeroing constructors that may accidentally
463   // overwrite existing bindings.
464   StoreRef BindDefaultZero(Store store, const MemRegion *R) override {
465     // FIXME: The offsets of empty bases can be tricky because of
466     // of the so called "empty base class optimization".
467     // If a base class has been optimized out
468     // we should not try to create a binding, otherwise we should.
469     // Unfortunately, at the moment ASTRecordLayout doesn't expose
470     // the actual sizes of the empty bases
471     // and trying to infer them from offsets/alignments
472     // seems to be error-prone and non-trivial because of the trailing padding.
473     // As a temporary mitigation we don't create bindings for empty bases.
474     if (const auto *BR = dyn_cast<CXXBaseObjectRegion>(R))
475       if (BR->getDecl()->isEmpty())
476         return StoreRef(store, *this);
477 
478     RegionBindingsRef B = getRegionBindings(store);
479     SVal V = svalBuilder.makeZeroVal(Ctx.CharTy);
480     B = removeSubRegionBindings(B, cast<SubRegion>(R));
481     B = B.addBinding(BindingKey::Make(R, BindingKey::Default), V);
482     return StoreRef(B.asImmutableMap().getRootWithoutRetain(), *this);
483   }
484 
485   /// Attempt to extract the fields of \p LCV and bind them to the struct region
486   /// \p R.
487   ///
488   /// This path is used when it seems advantageous to "force" loading the values
489   /// within a LazyCompoundVal to bind memberwise to the struct region, rather
490   /// than using a Default binding at the base of the entire region. This is a
491   /// heuristic attempting to avoid building long chains of LazyCompoundVals.
492   ///
493   /// \returns The updated store bindings, or \c None if binding non-lazily
494   ///          would be too expensive.
495   Optional<RegionBindingsRef> tryBindSmallStruct(RegionBindingsConstRef B,
496                                                  const TypedValueRegion *R,
497                                                  const RecordDecl *RD,
498                                                  nonloc::LazyCompoundVal LCV);
499 
500   /// BindStruct - Bind a compound value to a structure.
501   RegionBindingsRef bindStruct(RegionBindingsConstRef B,
502                                const TypedValueRegion* R, SVal V);
503 
504   /// BindVector - Bind a compound value to a vector.
505   RegionBindingsRef bindVector(RegionBindingsConstRef B,
506                                const TypedValueRegion* R, SVal V);
507 
508   RegionBindingsRef bindArray(RegionBindingsConstRef B,
509                               const TypedValueRegion* R,
510                               SVal V);
511 
512   /// Clears out all bindings in the given region and assigns a new value
513   /// as a Default binding.
514   RegionBindingsRef bindAggregate(RegionBindingsConstRef B,
515                                   const TypedRegion *R,
516                                   SVal DefaultVal);
517 
518   /// Create a new store with the specified binding removed.
519   /// \param ST the original store, that is the basis for the new store.
520   /// \param L the location whose binding should be removed.
521   StoreRef killBinding(Store ST, Loc L) override;
522 
523   void incrementReferenceCount(Store store) override {
524     getRegionBindings(store).manualRetain();
525   }
526 
527   /// If the StoreManager supports it, decrement the reference count of
528   /// the specified Store object.  If the reference count hits 0, the memory
529   /// associated with the object is recycled.
530   void decrementReferenceCount(Store store) override {
531     getRegionBindings(store).manualRelease();
532   }
533 
534   bool includedInBindings(Store store, const MemRegion *region) const override;
535 
536   /// Return the value bound to specified location in a given state.
537   ///
538   /// The high level logic for this method is this:
539   /// getBinding (L)
540   ///   if L has binding
541   ///     return L's binding
542   ///   else if L is in killset
543   ///     return unknown
544   ///   else
545   ///     if L is on stack or heap
546   ///       return undefined
547   ///     else
548   ///       return symbolic
549   SVal getBinding(Store S, Loc L, QualType T) override {
550     return getBinding(getRegionBindings(S), L, T);
551   }
552 
553   Optional<SVal> getDefaultBinding(Store S, const MemRegion *R) override {
554     RegionBindingsRef B = getRegionBindings(S);
555     // Default bindings are always applied over a base region so look up the
556     // base region's default binding, otherwise the lookup will fail when R
557     // is at an offset from R->getBaseRegion().
558     return B.getDefaultBinding(R->getBaseRegion());
559   }
560 
561   SVal getBinding(RegionBindingsConstRef B, Loc L, QualType T = QualType());
562 
563   SVal getBindingForElement(RegionBindingsConstRef B, const ElementRegion *R);
564 
565   SVal getBindingForField(RegionBindingsConstRef B, const FieldRegion *R);
566 
567   SVal getBindingForObjCIvar(RegionBindingsConstRef B, const ObjCIvarRegion *R);
568 
569   SVal getBindingForVar(RegionBindingsConstRef B, const VarRegion *R);
570 
571   SVal getBindingForLazySymbol(const TypedValueRegion *R);
572 
573   SVal getBindingForFieldOrElementCommon(RegionBindingsConstRef B,
574                                          const TypedValueRegion *R,
575                                          QualType Ty);
576 
577   SVal getLazyBinding(const SubRegion *LazyBindingRegion,
578                       RegionBindingsRef LazyBinding);
579 
580   /// Get bindings for the values in a struct and return a CompoundVal, used
581   /// when doing struct copy:
582   /// struct s x, y;
583   /// x = y;
584   /// y's value is retrieved by this method.
585   SVal getBindingForStruct(RegionBindingsConstRef B, const TypedValueRegion *R);
586   SVal getBindingForArray(RegionBindingsConstRef B, const TypedValueRegion *R);
587   NonLoc createLazyBinding(RegionBindingsConstRef B, const TypedValueRegion *R);
588 
589   /// Used to lazily generate derived symbols for bindings that are defined
590   /// implicitly by default bindings in a super region.
591   ///
592   /// Note that callers may need to specially handle LazyCompoundVals, which
593   /// are returned as is in case the caller needs to treat them differently.
594   Optional<SVal> getBindingForDerivedDefaultValue(RegionBindingsConstRef B,
595                                                   const MemRegion *superR,
596                                                   const TypedValueRegion *R,
597                                                   QualType Ty);
598 
599   /// Get the state and region whose binding this region \p R corresponds to.
600   ///
601   /// If there is no lazy binding for \p R, the returned value will have a null
602   /// \c second. Note that a null pointer can represents a valid Store.
603   std::pair<Store, const SubRegion *>
604   findLazyBinding(RegionBindingsConstRef B, const SubRegion *R,
605                   const SubRegion *originalRegion);
606 
607   /// Returns the cached set of interesting SVals contained within a lazy
608   /// binding.
609   ///
610   /// The precise value of "interesting" is determined for the purposes of
611   /// RegionStore's internal analysis. It must always contain all regions and
612   /// symbols, but may omit constants and other kinds of SVal.
613   const SValListTy &getInterestingValues(nonloc::LazyCompoundVal LCV);
614 
615   //===------------------------------------------------------------------===//
616   // State pruning.
617   //===------------------------------------------------------------------===//
618 
619   /// removeDeadBindings - Scans the RegionStore of 'state' for dead values.
620   ///  It returns a new Store with these values removed.
621   StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx,
622                               SymbolReaper& SymReaper) override;
623 
624   //===------------------------------------------------------------------===//
625   // Region "extents".
626   //===------------------------------------------------------------------===//
627 
628   // FIXME: This method will soon be eliminated; see the note in Store.h.
629   DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state,
630                                          const MemRegion* R,
631                                          QualType EleTy) override;
632 
633   //===------------------------------------------------------------------===//
634   // Utility methods.
635   //===------------------------------------------------------------------===//
636 
637   RegionBindingsRef getRegionBindings(Store store) const {
638     llvm::PointerIntPair<Store, 1, bool> Ptr;
639     Ptr.setFromOpaqueValue(const_cast<void *>(store));
640     return RegionBindingsRef(
641         CBFactory,
642         static_cast<const RegionBindings::TreeTy *>(Ptr.getPointer()),
643         RBFactory.getTreeFactory(),
644         Ptr.getInt());
645   }
646 
647   void printJson(raw_ostream &Out, Store S, const char *NL = "\n",
648                  unsigned int Space = 0, bool IsDot = false) const override;
649 
650   void iterBindings(Store store, BindingsHandler& f) override {
651     RegionBindingsRef B = getRegionBindings(store);
652     for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) {
653       const ClusterBindings &Cluster = I.getData();
654       for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
655            CI != CE; ++CI) {
656         const BindingKey &K = CI.getKey();
657         if (!K.isDirect())
658           continue;
659         if (const SubRegion *R = dyn_cast<SubRegion>(K.getRegion())) {
660           // FIXME: Possibly incorporate the offset?
661           if (!f.HandleBinding(*this, store, R, CI.getData()))
662             return;
663         }
664       }
665     }
666   }
667 };
668 
669 } // end anonymous namespace
670 
671 //===----------------------------------------------------------------------===//
672 // RegionStore creation.
673 //===----------------------------------------------------------------------===//
674 
675 std::unique_ptr<StoreManager>
676 ento::CreateRegionStoreManager(ProgramStateManager &StMgr) {
677   RegionStoreFeatures F = maximal_features_tag();
678   return std::make_unique<RegionStoreManager>(StMgr, F);
679 }
680 
681 std::unique_ptr<StoreManager>
682 ento::CreateFieldsOnlyRegionStoreManager(ProgramStateManager &StMgr) {
683   RegionStoreFeatures F = minimal_features_tag();
684   F.enableFields(true);
685   return std::make_unique<RegionStoreManager>(StMgr, F);
686 }
687 
688 
689 //===----------------------------------------------------------------------===//
690 // Region Cluster analysis.
691 //===----------------------------------------------------------------------===//
692 
693 namespace {
694 /// Used to determine which global regions are automatically included in the
695 /// initial worklist of a ClusterAnalysis.
696 enum GlobalsFilterKind {
697   /// Don't include any global regions.
698   GFK_None,
699   /// Only include system globals.
700   GFK_SystemOnly,
701   /// Include all global regions.
702   GFK_All
703 };
704 
705 template <typename DERIVED>
706 class ClusterAnalysis  {
707 protected:
708   typedef llvm::DenseMap<const MemRegion *, const ClusterBindings *> ClusterMap;
709   typedef const MemRegion * WorkListElement;
710   typedef SmallVector<WorkListElement, 10> WorkList;
711 
712   llvm::SmallPtrSet<const ClusterBindings *, 16> Visited;
713 
714   WorkList WL;
715 
716   RegionStoreManager &RM;
717   ASTContext &Ctx;
718   SValBuilder &svalBuilder;
719 
720   RegionBindingsRef B;
721 
722 
723 protected:
724   const ClusterBindings *getCluster(const MemRegion *R) {
725     return B.lookup(R);
726   }
727 
728   /// Returns true if all clusters in the given memspace should be initially
729   /// included in the cluster analysis. Subclasses may provide their
730   /// own implementation.
731   bool includeEntireMemorySpace(const MemRegion *Base) {
732     return false;
733   }
734 
735 public:
736   ClusterAnalysis(RegionStoreManager &rm, ProgramStateManager &StateMgr,
737                   RegionBindingsRef b)
738       : RM(rm), Ctx(StateMgr.getContext()),
739         svalBuilder(StateMgr.getSValBuilder()), B(std::move(b)) {}
740 
741   RegionBindingsRef getRegionBindings() const { return B; }
742 
743   bool isVisited(const MemRegion *R) {
744     return Visited.count(getCluster(R));
745   }
746 
747   void GenerateClusters() {
748     // Scan the entire set of bindings and record the region clusters.
749     for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end();
750          RI != RE; ++RI){
751       const MemRegion *Base = RI.getKey();
752 
753       const ClusterBindings &Cluster = RI.getData();
754       assert(!Cluster.isEmpty() && "Empty clusters should be removed");
755       static_cast<DERIVED*>(this)->VisitAddedToCluster(Base, Cluster);
756 
757       // If the base's memspace should be entirely invalidated, add the cluster
758       // to the workspace up front.
759       if (static_cast<DERIVED*>(this)->includeEntireMemorySpace(Base))
760         AddToWorkList(WorkListElement(Base), &Cluster);
761     }
762   }
763 
764   bool AddToWorkList(WorkListElement E, const ClusterBindings *C) {
765     if (C && !Visited.insert(C).second)
766       return false;
767     WL.push_back(E);
768     return true;
769   }
770 
771   bool AddToWorkList(const MemRegion *R) {
772     return static_cast<DERIVED*>(this)->AddToWorkList(R);
773   }
774 
775   void RunWorkList() {
776     while (!WL.empty()) {
777       WorkListElement E = WL.pop_back_val();
778       const MemRegion *BaseR = E;
779 
780       static_cast<DERIVED*>(this)->VisitCluster(BaseR, getCluster(BaseR));
781     }
782   }
783 
784   void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C) {}
785   void VisitCluster(const MemRegion *baseR, const ClusterBindings *C) {}
786 
787   void VisitCluster(const MemRegion *BaseR, const ClusterBindings *C,
788                     bool Flag) {
789     static_cast<DERIVED*>(this)->VisitCluster(BaseR, C);
790   }
791 };
792 }
793 
794 //===----------------------------------------------------------------------===//
795 // Binding invalidation.
796 //===----------------------------------------------------------------------===//
797 
798 bool RegionStoreManager::scanReachableSymbols(Store S, const MemRegion *R,
799                                               ScanReachableSymbols &Callbacks) {
800   assert(R == R->getBaseRegion() && "Should only be called for base regions");
801   RegionBindingsRef B = getRegionBindings(S);
802   const ClusterBindings *Cluster = B.lookup(R);
803 
804   if (!Cluster)
805     return true;
806 
807   for (ClusterBindings::iterator RI = Cluster->begin(), RE = Cluster->end();
808        RI != RE; ++RI) {
809     if (!Callbacks.scan(RI.getData()))
810       return false;
811   }
812 
813   return true;
814 }
815 
816 static inline bool isUnionField(const FieldRegion *FR) {
817   return FR->getDecl()->getParent()->isUnion();
818 }
819 
820 typedef SmallVector<const FieldDecl *, 8> FieldVector;
821 
822 static void getSymbolicOffsetFields(BindingKey K, FieldVector &Fields) {
823   assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys");
824 
825   const MemRegion *Base = K.getConcreteOffsetRegion();
826   const MemRegion *R = K.getRegion();
827 
828   while (R != Base) {
829     if (const FieldRegion *FR = dyn_cast<FieldRegion>(R))
830       if (!isUnionField(FR))
831         Fields.push_back(FR->getDecl());
832 
833     R = cast<SubRegion>(R)->getSuperRegion();
834   }
835 }
836 
837 static bool isCompatibleWithFields(BindingKey K, const FieldVector &Fields) {
838   assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys");
839 
840   if (Fields.empty())
841     return true;
842 
843   FieldVector FieldsInBindingKey;
844   getSymbolicOffsetFields(K, FieldsInBindingKey);
845 
846   ptrdiff_t Delta = FieldsInBindingKey.size() - Fields.size();
847   if (Delta >= 0)
848     return std::equal(FieldsInBindingKey.begin() + Delta,
849                       FieldsInBindingKey.end(),
850                       Fields.begin());
851   else
852     return std::equal(FieldsInBindingKey.begin(), FieldsInBindingKey.end(),
853                       Fields.begin() - Delta);
854 }
855 
856 /// Collects all bindings in \p Cluster that may refer to bindings within
857 /// \p Top.
858 ///
859 /// Each binding is a pair whose \c first is the key (a BindingKey) and whose
860 /// \c second is the value (an SVal).
861 ///
862 /// The \p IncludeAllDefaultBindings parameter specifies whether to include
863 /// default bindings that may extend beyond \p Top itself, e.g. if \p Top is
864 /// an aggregate within a larger aggregate with a default binding.
865 static void
866 collectSubRegionBindings(SmallVectorImpl<BindingPair> &Bindings,
867                          SValBuilder &SVB, const ClusterBindings &Cluster,
868                          const SubRegion *Top, BindingKey TopKey,
869                          bool IncludeAllDefaultBindings) {
870   FieldVector FieldsInSymbolicSubregions;
871   if (TopKey.hasSymbolicOffset()) {
872     getSymbolicOffsetFields(TopKey, FieldsInSymbolicSubregions);
873     Top = TopKey.getConcreteOffsetRegion();
874     TopKey = BindingKey::Make(Top, BindingKey::Default);
875   }
876 
877   // Find the length (in bits) of the region being invalidated.
878   uint64_t Length = UINT64_MAX;
879   SVal Extent = Top->getExtent(SVB);
880   if (Optional<nonloc::ConcreteInt> ExtentCI =
881           Extent.getAs<nonloc::ConcreteInt>()) {
882     const llvm::APSInt &ExtentInt = ExtentCI->getValue();
883     assert(ExtentInt.isNonNegative() || ExtentInt.isUnsigned());
884     // Extents are in bytes but region offsets are in bits. Be careful!
885     Length = ExtentInt.getLimitedValue() * SVB.getContext().getCharWidth();
886   } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(Top)) {
887     if (FR->getDecl()->isBitField())
888       Length = FR->getDecl()->getBitWidthValue(SVB.getContext());
889   }
890 
891   for (ClusterBindings::iterator I = Cluster.begin(), E = Cluster.end();
892        I != E; ++I) {
893     BindingKey NextKey = I.getKey();
894     if (NextKey.getRegion() == TopKey.getRegion()) {
895       // FIXME: This doesn't catch the case where we're really invalidating a
896       // region with a symbolic offset. Example:
897       //      R: points[i].y
898       //   Next: points[0].x
899 
900       if (NextKey.getOffset() > TopKey.getOffset() &&
901           NextKey.getOffset() - TopKey.getOffset() < Length) {
902         // Case 1: The next binding is inside the region we're invalidating.
903         // Include it.
904         Bindings.push_back(*I);
905 
906       } else if (NextKey.getOffset() == TopKey.getOffset()) {
907         // Case 2: The next binding is at the same offset as the region we're
908         // invalidating. In this case, we need to leave default bindings alone,
909         // since they may be providing a default value for a regions beyond what
910         // we're invalidating.
911         // FIXME: This is probably incorrect; consider invalidating an outer
912         // struct whose first field is bound to a LazyCompoundVal.
913         if (IncludeAllDefaultBindings || NextKey.isDirect())
914           Bindings.push_back(*I);
915       }
916 
917     } else if (NextKey.hasSymbolicOffset()) {
918       const MemRegion *Base = NextKey.getConcreteOffsetRegion();
919       if (Top->isSubRegionOf(Base) && Top != Base) {
920         // Case 3: The next key is symbolic and we just changed something within
921         // its concrete region. We don't know if the binding is still valid, so
922         // we'll be conservative and include it.
923         if (IncludeAllDefaultBindings || NextKey.isDirect())
924           if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions))
925             Bindings.push_back(*I);
926       } else if (const SubRegion *BaseSR = dyn_cast<SubRegion>(Base)) {
927         // Case 4: The next key is symbolic, but we changed a known
928         // super-region. In this case the binding is certainly included.
929         if (BaseSR->isSubRegionOf(Top))
930           if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions))
931             Bindings.push_back(*I);
932       }
933     }
934   }
935 }
936 
937 static void
938 collectSubRegionBindings(SmallVectorImpl<BindingPair> &Bindings,
939                          SValBuilder &SVB, const ClusterBindings &Cluster,
940                          const SubRegion *Top, bool IncludeAllDefaultBindings) {
941   collectSubRegionBindings(Bindings, SVB, Cluster, Top,
942                            BindingKey::Make(Top, BindingKey::Default),
943                            IncludeAllDefaultBindings);
944 }
945 
946 RegionBindingsRef
947 RegionStoreManager::removeSubRegionBindings(RegionBindingsConstRef B,
948                                             const SubRegion *Top) {
949   BindingKey TopKey = BindingKey::Make(Top, BindingKey::Default);
950   const MemRegion *ClusterHead = TopKey.getBaseRegion();
951 
952   if (Top == ClusterHead) {
953     // We can remove an entire cluster's bindings all in one go.
954     return B.remove(Top);
955   }
956 
957   const ClusterBindings *Cluster = B.lookup(ClusterHead);
958   if (!Cluster) {
959     // If we're invalidating a region with a symbolic offset, we need to make
960     // sure we don't treat the base region as uninitialized anymore.
961     if (TopKey.hasSymbolicOffset()) {
962       const SubRegion *Concrete = TopKey.getConcreteOffsetRegion();
963       return B.addBinding(Concrete, BindingKey::Default, UnknownVal());
964     }
965     return B;
966   }
967 
968   SmallVector<BindingPair, 32> Bindings;
969   collectSubRegionBindings(Bindings, svalBuilder, *Cluster, Top, TopKey,
970                            /*IncludeAllDefaultBindings=*/false);
971 
972   ClusterBindingsRef Result(*Cluster, CBFactory);
973   for (SmallVectorImpl<BindingPair>::const_iterator I = Bindings.begin(),
974                                                     E = Bindings.end();
975        I != E; ++I)
976     Result = Result.remove(I->first);
977 
978   // If we're invalidating a region with a symbolic offset, we need to make sure
979   // we don't treat the base region as uninitialized anymore.
980   // FIXME: This isn't very precise; see the example in
981   // collectSubRegionBindings.
982   if (TopKey.hasSymbolicOffset()) {
983     const SubRegion *Concrete = TopKey.getConcreteOffsetRegion();
984     Result = Result.add(BindingKey::Make(Concrete, BindingKey::Default),
985                         UnknownVal());
986   }
987 
988   if (Result.isEmpty())
989     return B.remove(ClusterHead);
990   return B.add(ClusterHead, Result.asImmutableMap());
991 }
992 
993 namespace {
994 class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
995 {
996   const Expr *Ex;
997   unsigned Count;
998   const LocationContext *LCtx;
999   InvalidatedSymbols &IS;
1000   RegionAndSymbolInvalidationTraits &ITraits;
1001   StoreManager::InvalidatedRegions *Regions;
1002   GlobalsFilterKind GlobalsFilter;
1003 public:
1004   InvalidateRegionsWorker(RegionStoreManager &rm,
1005                           ProgramStateManager &stateMgr,
1006                           RegionBindingsRef b,
1007                           const Expr *ex, unsigned count,
1008                           const LocationContext *lctx,
1009                           InvalidatedSymbols &is,
1010                           RegionAndSymbolInvalidationTraits &ITraitsIn,
1011                           StoreManager::InvalidatedRegions *r,
1012                           GlobalsFilterKind GFK)
1013      : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
1014        Ex(ex), Count(count), LCtx(lctx), IS(is), ITraits(ITraitsIn), Regions(r),
1015        GlobalsFilter(GFK) {}
1016 
1017   void VisitCluster(const MemRegion *baseR, const ClusterBindings *C);
1018   void VisitBinding(SVal V);
1019 
1020   using ClusterAnalysis::AddToWorkList;
1021 
1022   bool AddToWorkList(const MemRegion *R);
1023 
1024   /// Returns true if all clusters in the memory space for \p Base should be
1025   /// be invalidated.
1026   bool includeEntireMemorySpace(const MemRegion *Base);
1027 
1028   /// Returns true if the memory space of the given region is one of the global
1029   /// regions specially included at the start of invalidation.
1030   bool isInitiallyIncludedGlobalRegion(const MemRegion *R);
1031 };
1032 }
1033 
1034 bool InvalidateRegionsWorker::AddToWorkList(const MemRegion *R) {
1035   bool doNotInvalidateSuperRegion = ITraits.hasTrait(
1036       R, RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
1037   const MemRegion *BaseR = doNotInvalidateSuperRegion ? R : R->getBaseRegion();
1038   return AddToWorkList(WorkListElement(BaseR), getCluster(BaseR));
1039 }
1040 
1041 void InvalidateRegionsWorker::VisitBinding(SVal V) {
1042   // A symbol?  Mark it touched by the invalidation.
1043   if (SymbolRef Sym = V.getAsSymbol())
1044     IS.insert(Sym);
1045 
1046   if (const MemRegion *R = V.getAsRegion()) {
1047     AddToWorkList(R);
1048     return;
1049   }
1050 
1051   // Is it a LazyCompoundVal?  All references get invalidated as well.
1052   if (Optional<nonloc::LazyCompoundVal> LCS =
1053           V.getAs<nonloc::LazyCompoundVal>()) {
1054 
1055     const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS);
1056 
1057     for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(),
1058                                                         E = Vals.end();
1059          I != E; ++I)
1060       VisitBinding(*I);
1061 
1062     return;
1063   }
1064 }
1065 
1066 void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
1067                                            const ClusterBindings *C) {
1068 
1069   bool PreserveRegionsContents =
1070       ITraits.hasTrait(baseR,
1071                        RegionAndSymbolInvalidationTraits::TK_PreserveContents);
1072 
1073   if (C) {
1074     for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E; ++I)
1075       VisitBinding(I.getData());
1076 
1077     // Invalidate regions contents.
1078     if (!PreserveRegionsContents)
1079       B = B.remove(baseR);
1080   }
1081 
1082   if (const auto *TO = dyn_cast<TypedValueRegion>(baseR)) {
1083     if (const auto *RD = TO->getValueType()->getAsCXXRecordDecl()) {
1084 
1085       // Lambdas can affect all static local variables without explicitly
1086       // capturing those.
1087       // We invalidate all static locals referenced inside the lambda body.
1088       if (RD->isLambda() && RD->getLambdaCallOperator()->getBody()) {
1089         using namespace ast_matchers;
1090 
1091         const char *DeclBind = "DeclBind";
1092         StatementMatcher RefToStatic = stmt(hasDescendant(declRefExpr(
1093               to(varDecl(hasStaticStorageDuration()).bind(DeclBind)))));
1094         auto Matches =
1095             match(RefToStatic, *RD->getLambdaCallOperator()->getBody(),
1096                   RD->getASTContext());
1097 
1098         for (BoundNodes &Match : Matches) {
1099           auto *VD = Match.getNodeAs<VarDecl>(DeclBind);
1100           const VarRegion *ToInvalidate =
1101               RM.getRegionManager().getVarRegion(VD, LCtx);
1102           AddToWorkList(ToInvalidate);
1103         }
1104       }
1105     }
1106   }
1107 
1108   // BlockDataRegion?  If so, invalidate captured variables that are passed
1109   // by reference.
1110   if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
1111     for (BlockDataRegion::referenced_vars_iterator
1112          BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
1113          BI != BE; ++BI) {
1114       const VarRegion *VR = BI.getCapturedRegion();
1115       const VarDecl *VD = VR->getDecl();
1116       if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage()) {
1117         AddToWorkList(VR);
1118       }
1119       else if (Loc::isLocType(VR->getValueType())) {
1120         // Map the current bindings to a Store to retrieve the value
1121         // of the binding.  If that binding itself is a region, we should
1122         // invalidate that region.  This is because a block may capture
1123         // a pointer value, but the thing pointed by that pointer may
1124         // get invalidated.
1125         SVal V = RM.getBinding(B, loc::MemRegionVal(VR));
1126         if (Optional<Loc> L = V.getAs<Loc>()) {
1127           if (const MemRegion *LR = L->getAsRegion())
1128             AddToWorkList(LR);
1129         }
1130       }
1131     }
1132     return;
1133   }
1134 
1135   // Symbolic region?
1136   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
1137     IS.insert(SR->getSymbol());
1138 
1139   // Nothing else should be done in the case when we preserve regions context.
1140   if (PreserveRegionsContents)
1141     return;
1142 
1143   // Otherwise, we have a normal data region. Record that we touched the region.
1144   if (Regions)
1145     Regions->push_back(baseR);
1146 
1147   if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
1148     // Invalidate the region by setting its default value to
1149     // conjured symbol. The type of the symbol is irrelevant.
1150     DefinedOrUnknownSVal V =
1151       svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, Ctx.IntTy, Count);
1152     B = B.addBinding(baseR, BindingKey::Default, V);
1153     return;
1154   }
1155 
1156   if (!baseR->isBoundable())
1157     return;
1158 
1159   const TypedValueRegion *TR = cast<TypedValueRegion>(baseR);
1160   QualType T = TR->getValueType();
1161 
1162   if (isInitiallyIncludedGlobalRegion(baseR)) {
1163     // If the region is a global and we are invalidating all globals,
1164     // erasing the entry is good enough.  This causes all globals to be lazily
1165     // symbolicated from the same base symbol.
1166     return;
1167   }
1168 
1169   if (T->isRecordType()) {
1170     // Invalidate the region by setting its default value to
1171     // conjured symbol. The type of the symbol is irrelevant.
1172     DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
1173                                                           Ctx.IntTy, Count);
1174     B = B.addBinding(baseR, BindingKey::Default, V);
1175     return;
1176   }
1177 
1178   if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
1179     bool doNotInvalidateSuperRegion = ITraits.hasTrait(
1180         baseR,
1181         RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
1182 
1183     if (doNotInvalidateSuperRegion) {
1184       // We are not doing blank invalidation of the whole array region so we
1185       // have to manually invalidate each elements.
1186       Optional<uint64_t> NumElements;
1187 
1188       // Compute lower and upper offsets for region within array.
1189       if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1190         NumElements = CAT->getSize().getZExtValue();
1191       if (!NumElements) // We are not dealing with a constant size array
1192         goto conjure_default;
1193       QualType ElementTy = AT->getElementType();
1194       uint64_t ElemSize = Ctx.getTypeSize(ElementTy);
1195       const RegionOffset &RO = baseR->getAsOffset();
1196       const MemRegion *SuperR = baseR->getBaseRegion();
1197       if (RO.hasSymbolicOffset()) {
1198         // If base region has a symbolic offset,
1199         // we revert to invalidating the super region.
1200         if (SuperR)
1201           AddToWorkList(SuperR);
1202         goto conjure_default;
1203       }
1204 
1205       uint64_t LowerOffset = RO.getOffset();
1206       uint64_t UpperOffset = LowerOffset + *NumElements * ElemSize;
1207       bool UpperOverflow = UpperOffset < LowerOffset;
1208 
1209       // Invalidate regions which are within array boundaries,
1210       // or have a symbolic offset.
1211       if (!SuperR)
1212         goto conjure_default;
1213 
1214       const ClusterBindings *C = B.lookup(SuperR);
1215       if (!C)
1216         goto conjure_default;
1217 
1218       for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E;
1219            ++I) {
1220         const BindingKey &BK = I.getKey();
1221         Optional<uint64_t> ROffset =
1222             BK.hasSymbolicOffset() ? Optional<uint64_t>() : BK.getOffset();
1223 
1224         // Check offset is not symbolic and within array's boundaries.
1225         // Handles arrays of 0 elements and of 0-sized elements as well.
1226         if (!ROffset ||
1227             ((*ROffset >= LowerOffset && *ROffset < UpperOffset) ||
1228              (UpperOverflow &&
1229               (*ROffset >= LowerOffset || *ROffset < UpperOffset)) ||
1230              (LowerOffset == UpperOffset && *ROffset == LowerOffset))) {
1231           B = B.removeBinding(I.getKey());
1232           // Bound symbolic regions need to be invalidated for dead symbol
1233           // detection.
1234           SVal V = I.getData();
1235           const MemRegion *R = V.getAsRegion();
1236           if (R && isa<SymbolicRegion>(R))
1237             VisitBinding(V);
1238         }
1239       }
1240     }
1241   conjure_default:
1242       // Set the default value of the array to conjured symbol.
1243     DefinedOrUnknownSVal V =
1244     svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
1245                                      AT->getElementType(), Count);
1246     B = B.addBinding(baseR, BindingKey::Default, V);
1247     return;
1248   }
1249 
1250   DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
1251                                                         T,Count);
1252   assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
1253   B = B.addBinding(baseR, BindingKey::Direct, V);
1254 }
1255 
1256 bool InvalidateRegionsWorker::isInitiallyIncludedGlobalRegion(
1257     const MemRegion *R) {
1258   switch (GlobalsFilter) {
1259   case GFK_None:
1260     return false;
1261   case GFK_SystemOnly:
1262     return isa<GlobalSystemSpaceRegion>(R->getMemorySpace());
1263   case GFK_All:
1264     return isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace());
1265   }
1266 
1267   llvm_unreachable("unknown globals filter");
1268 }
1269 
1270 bool InvalidateRegionsWorker::includeEntireMemorySpace(const MemRegion *Base) {
1271   if (isInitiallyIncludedGlobalRegion(Base))
1272     return true;
1273 
1274   const MemSpaceRegion *MemSpace = Base->getMemorySpace();
1275   return ITraits.hasTrait(MemSpace,
1276                           RegionAndSymbolInvalidationTraits::TK_EntireMemSpace);
1277 }
1278 
1279 RegionBindingsRef
1280 RegionStoreManager::invalidateGlobalRegion(MemRegion::Kind K,
1281                                            const Expr *Ex,
1282                                            unsigned Count,
1283                                            const LocationContext *LCtx,
1284                                            RegionBindingsRef B,
1285                                            InvalidatedRegions *Invalidated) {
1286   // Bind the globals memory space to a new symbol that we will use to derive
1287   // the bindings for all globals.
1288   const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion(K);
1289   SVal V = svalBuilder.conjureSymbolVal(/* symbolTag = */ (const void*) GS, Ex, LCtx,
1290                                         /* type does not matter */ Ctx.IntTy,
1291                                         Count);
1292 
1293   B = B.removeBinding(GS)
1294        .addBinding(BindingKey::Make(GS, BindingKey::Default), V);
1295 
1296   // Even if there are no bindings in the global scope, we still need to
1297   // record that we touched it.
1298   if (Invalidated)
1299     Invalidated->push_back(GS);
1300 
1301   return B;
1302 }
1303 
1304 void RegionStoreManager::populateWorkList(InvalidateRegionsWorker &W,
1305                                           ArrayRef<SVal> Values,
1306                                           InvalidatedRegions *TopLevelRegions) {
1307   for (ArrayRef<SVal>::iterator I = Values.begin(),
1308                                 E = Values.end(); I != E; ++I) {
1309     SVal V = *I;
1310     if (Optional<nonloc::LazyCompoundVal> LCS =
1311         V.getAs<nonloc::LazyCompoundVal>()) {
1312 
1313       const SValListTy &Vals = getInterestingValues(*LCS);
1314 
1315       for (SValListTy::const_iterator I = Vals.begin(),
1316                                       E = Vals.end(); I != E; ++I) {
1317         // Note: the last argument is false here because these are
1318         // non-top-level regions.
1319         if (const MemRegion *R = (*I).getAsRegion())
1320           W.AddToWorkList(R);
1321       }
1322       continue;
1323     }
1324 
1325     if (const MemRegion *R = V.getAsRegion()) {
1326       if (TopLevelRegions)
1327         TopLevelRegions->push_back(R);
1328       W.AddToWorkList(R);
1329       continue;
1330     }
1331   }
1332 }
1333 
1334 StoreRef
1335 RegionStoreManager::invalidateRegions(Store store,
1336                                      ArrayRef<SVal> Values,
1337                                      const Expr *Ex, unsigned Count,
1338                                      const LocationContext *LCtx,
1339                                      const CallEvent *Call,
1340                                      InvalidatedSymbols &IS,
1341                                      RegionAndSymbolInvalidationTraits &ITraits,
1342                                      InvalidatedRegions *TopLevelRegions,
1343                                      InvalidatedRegions *Invalidated) {
1344   GlobalsFilterKind GlobalsFilter;
1345   if (Call) {
1346     if (Call->isInSystemHeader())
1347       GlobalsFilter = GFK_SystemOnly;
1348     else
1349       GlobalsFilter = GFK_All;
1350   } else {
1351     GlobalsFilter = GFK_None;
1352   }
1353 
1354   RegionBindingsRef B = getRegionBindings(store);
1355   InvalidateRegionsWorker W(*this, StateMgr, B, Ex, Count, LCtx, IS, ITraits,
1356                             Invalidated, GlobalsFilter);
1357 
1358   // Scan the bindings and generate the clusters.
1359   W.GenerateClusters();
1360 
1361   // Add the regions to the worklist.
1362   populateWorkList(W, Values, TopLevelRegions);
1363 
1364   W.RunWorkList();
1365 
1366   // Return the new bindings.
1367   B = W.getRegionBindings();
1368 
1369   // For calls, determine which global regions should be invalidated and
1370   // invalidate them. (Note that function-static and immutable globals are never
1371   // invalidated by this.)
1372   // TODO: This could possibly be more precise with modules.
1373   switch (GlobalsFilter) {
1374   case GFK_All:
1375     B = invalidateGlobalRegion(MemRegion::GlobalInternalSpaceRegionKind,
1376                                Ex, Count, LCtx, B, Invalidated);
1377     LLVM_FALLTHROUGH;
1378   case GFK_SystemOnly:
1379     B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind,
1380                                Ex, Count, LCtx, B, Invalidated);
1381     LLVM_FALLTHROUGH;
1382   case GFK_None:
1383     break;
1384   }
1385 
1386   return StoreRef(B.asStore(), *this);
1387 }
1388 
1389 //===----------------------------------------------------------------------===//
1390 // Extents for regions.
1391 //===----------------------------------------------------------------------===//
1392 
1393 DefinedOrUnknownSVal
1394 RegionStoreManager::getSizeInElements(ProgramStateRef state,
1395                                       const MemRegion *R,
1396                                       QualType EleTy) {
1397   SVal Size = cast<SubRegion>(R)->getExtent(svalBuilder);
1398   const llvm::APSInt *SizeInt = svalBuilder.getKnownValue(state, Size);
1399   if (!SizeInt)
1400     return UnknownVal();
1401 
1402   CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue());
1403 
1404   if (Ctx.getAsVariableArrayType(EleTy)) {
1405     // FIXME: We need to track extra state to properly record the size
1406     // of VLAs.  Returning UnknownVal here, however, is a stop-gap so that
1407     // we don't have a divide-by-zero below.
1408     return UnknownVal();
1409   }
1410 
1411   CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
1412 
1413   // If a variable is reinterpreted as a type that doesn't fit into a larger
1414   // type evenly, round it down.
1415   // This is a signed value, since it's used in arithmetic with signed indices.
1416   return svalBuilder.makeIntVal(RegionSize / EleSize,
1417                                 svalBuilder.getArrayIndexType());
1418 }
1419 
1420 //===----------------------------------------------------------------------===//
1421 // Location and region casting.
1422 //===----------------------------------------------------------------------===//
1423 
1424 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
1425 ///  type.  'Array' represents the lvalue of the array being decayed
1426 ///  to a pointer, and the returned SVal represents the decayed
1427 ///  version of that lvalue (i.e., a pointer to the first element of
1428 ///  the array).  This is called by ExprEngine when evaluating casts
1429 ///  from arrays to pointers.
1430 SVal RegionStoreManager::ArrayToPointer(Loc Array, QualType T) {
1431   if (Array.getAs<loc::ConcreteInt>())
1432     return Array;
1433 
1434   if (!Array.getAs<loc::MemRegionVal>())
1435     return UnknownVal();
1436 
1437   const SubRegion *R =
1438       cast<SubRegion>(Array.castAs<loc::MemRegionVal>().getRegion());
1439   NonLoc ZeroIdx = svalBuilder.makeZeroArrayIndex();
1440   return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, R, Ctx));
1441 }
1442 
1443 //===----------------------------------------------------------------------===//
1444 // Loading values from regions.
1445 //===----------------------------------------------------------------------===//
1446 
1447 SVal RegionStoreManager::getBinding(RegionBindingsConstRef B, Loc L, QualType T) {
1448   assert(!L.getAs<UnknownVal>() && "location unknown");
1449   assert(!L.getAs<UndefinedVal>() && "location undefined");
1450 
1451   // For access to concrete addresses, return UnknownVal.  Checks
1452   // for null dereferences (and similar errors) are done by checkers, not
1453   // the Store.
1454   // FIXME: We can consider lazily symbolicating such memory, but we really
1455   // should defer this when we can reason easily about symbolicating arrays
1456   // of bytes.
1457   if (L.getAs<loc::ConcreteInt>()) {
1458     return UnknownVal();
1459   }
1460   if (!L.getAs<loc::MemRegionVal>()) {
1461     return UnknownVal();
1462   }
1463 
1464   const MemRegion *MR = L.castAs<loc::MemRegionVal>().getRegion();
1465 
1466   if (isa<BlockDataRegion>(MR)) {
1467     return UnknownVal();
1468   }
1469 
1470   if (!isa<TypedValueRegion>(MR)) {
1471     if (T.isNull()) {
1472       if (const TypedRegion *TR = dyn_cast<TypedRegion>(MR))
1473         T = TR->getLocationType()->getPointeeType();
1474       else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1475         T = SR->getSymbol()->getType()->getPointeeType();
1476     }
1477     assert(!T.isNull() && "Unable to auto-detect binding type!");
1478     assert(!T->isVoidType() && "Attempting to dereference a void pointer!");
1479     MR = GetElementZeroRegion(cast<SubRegion>(MR), T);
1480   } else {
1481     T = cast<TypedValueRegion>(MR)->getValueType();
1482   }
1483 
1484   // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1485   //  instead of 'Loc', and have the other Loc cases handled at a higher level.
1486   const TypedValueRegion *R = cast<TypedValueRegion>(MR);
1487   QualType RTy = R->getValueType();
1488 
1489   // FIXME: we do not yet model the parts of a complex type, so treat the
1490   // whole thing as "unknown".
1491   if (RTy->isAnyComplexType())
1492     return UnknownVal();
1493 
1494   // FIXME: We should eventually handle funny addressing.  e.g.:
1495   //
1496   //   int x = ...;
1497   //   int *p = &x;
1498   //   char *q = (char*) p;
1499   //   char c = *q;  // returns the first byte of 'x'.
1500   //
1501   // Such funny addressing will occur due to layering of regions.
1502   if (RTy->isStructureOrClassType())
1503     return getBindingForStruct(B, R);
1504 
1505   // FIXME: Handle unions.
1506   if (RTy->isUnionType())
1507     return createLazyBinding(B, R);
1508 
1509   if (RTy->isArrayType()) {
1510     if (RTy->isConstantArrayType())
1511       return getBindingForArray(B, R);
1512     else
1513       return UnknownVal();
1514   }
1515 
1516   // FIXME: handle Vector types.
1517   if (RTy->isVectorType())
1518     return UnknownVal();
1519 
1520   if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
1521     return CastRetrievedVal(getBindingForField(B, FR), FR, T);
1522 
1523   if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1524     // FIXME: Here we actually perform an implicit conversion from the loaded
1525     // value to the element type.  Eventually we want to compose these values
1526     // more intelligently.  For example, an 'element' can encompass multiple
1527     // bound regions (e.g., several bound bytes), or could be a subset of
1528     // a larger value.
1529     return CastRetrievedVal(getBindingForElement(B, ER), ER, T);
1530   }
1531 
1532   if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1533     // FIXME: Here we actually perform an implicit conversion from the loaded
1534     // value to the ivar type.  What we should model is stores to ivars
1535     // that blow past the extent of the ivar.  If the address of the ivar is
1536     // reinterpretted, it is possible we stored a different value that could
1537     // fit within the ivar.  Either we need to cast these when storing them
1538     // or reinterpret them lazily (as we do here).
1539     return CastRetrievedVal(getBindingForObjCIvar(B, IVR), IVR, T);
1540   }
1541 
1542   if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1543     // FIXME: Here we actually perform an implicit conversion from the loaded
1544     // value to the variable type.  What we should model is stores to variables
1545     // that blow past the extent of the variable.  If the address of the
1546     // variable is reinterpretted, it is possible we stored a different value
1547     // that could fit within the variable.  Either we need to cast these when
1548     // storing them or reinterpret them lazily (as we do here).
1549     return CastRetrievedVal(getBindingForVar(B, VR), VR, T);
1550   }
1551 
1552   const SVal *V = B.lookup(R, BindingKey::Direct);
1553 
1554   // Check if the region has a binding.
1555   if (V)
1556     return *V;
1557 
1558   // The location does not have a bound value.  This means that it has
1559   // the value it had upon its creation and/or entry to the analyzed
1560   // function/method.  These are either symbolic values or 'undefined'.
1561   if (R->hasStackNonParametersStorage()) {
1562     // All stack variables are considered to have undefined values
1563     // upon creation.  All heap allocated blocks are considered to
1564     // have undefined values as well unless they are explicitly bound
1565     // to specific values.
1566     return UndefinedVal();
1567   }
1568 
1569   // All other values are symbolic.
1570   return svalBuilder.getRegionValueSymbolVal(R);
1571 }
1572 
1573 static QualType getUnderlyingType(const SubRegion *R) {
1574   QualType RegionTy;
1575   if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(R))
1576     RegionTy = TVR->getValueType();
1577 
1578   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1579     RegionTy = SR->getSymbol()->getType();
1580 
1581   return RegionTy;
1582 }
1583 
1584 /// Checks to see if store \p B has a lazy binding for region \p R.
1585 ///
1586 /// If \p AllowSubregionBindings is \c false, a lazy binding will be rejected
1587 /// if there are additional bindings within \p R.
1588 ///
1589 /// Note that unlike RegionStoreManager::findLazyBinding, this will not search
1590 /// for lazy bindings for super-regions of \p R.
1591 static Optional<nonloc::LazyCompoundVal>
1592 getExistingLazyBinding(SValBuilder &SVB, RegionBindingsConstRef B,
1593                        const SubRegion *R, bool AllowSubregionBindings) {
1594   Optional<SVal> V = B.getDefaultBinding(R);
1595   if (!V)
1596     return None;
1597 
1598   Optional<nonloc::LazyCompoundVal> LCV = V->getAs<nonloc::LazyCompoundVal>();
1599   if (!LCV)
1600     return None;
1601 
1602   // If the LCV is for a subregion, the types might not match, and we shouldn't
1603   // reuse the binding.
1604   QualType RegionTy = getUnderlyingType(R);
1605   if (!RegionTy.isNull() &&
1606       !RegionTy->isVoidPointerType()) {
1607     QualType SourceRegionTy = LCV->getRegion()->getValueType();
1608     if (!SVB.getContext().hasSameUnqualifiedType(RegionTy, SourceRegionTy))
1609       return None;
1610   }
1611 
1612   if (!AllowSubregionBindings) {
1613     // If there are any other bindings within this region, we shouldn't reuse
1614     // the top-level binding.
1615     SmallVector<BindingPair, 16> Bindings;
1616     collectSubRegionBindings(Bindings, SVB, *B.lookup(R->getBaseRegion()), R,
1617                              /*IncludeAllDefaultBindings=*/true);
1618     if (Bindings.size() > 1)
1619       return None;
1620   }
1621 
1622   return *LCV;
1623 }
1624 
1625 
1626 std::pair<Store, const SubRegion *>
1627 RegionStoreManager::findLazyBinding(RegionBindingsConstRef B,
1628                                    const SubRegion *R,
1629                                    const SubRegion *originalRegion) {
1630   if (originalRegion != R) {
1631     if (Optional<nonloc::LazyCompoundVal> V =
1632           getExistingLazyBinding(svalBuilder, B, R, true))
1633       return std::make_pair(V->getStore(), V->getRegion());
1634   }
1635 
1636   typedef std::pair<Store, const SubRegion *> StoreRegionPair;
1637   StoreRegionPair Result = StoreRegionPair();
1638 
1639   if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1640     Result = findLazyBinding(B, cast<SubRegion>(ER->getSuperRegion()),
1641                              originalRegion);
1642 
1643     if (Result.second)
1644       Result.second = MRMgr.getElementRegionWithSuper(ER, Result.second);
1645 
1646   } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1647     Result = findLazyBinding(B, cast<SubRegion>(FR->getSuperRegion()),
1648                                        originalRegion);
1649 
1650     if (Result.second)
1651       Result.second = MRMgr.getFieldRegionWithSuper(FR, Result.second);
1652 
1653   } else if (const CXXBaseObjectRegion *BaseReg =
1654                dyn_cast<CXXBaseObjectRegion>(R)) {
1655     // C++ base object region is another kind of region that we should blast
1656     // through to look for lazy compound value. It is like a field region.
1657     Result = findLazyBinding(B, cast<SubRegion>(BaseReg->getSuperRegion()),
1658                              originalRegion);
1659 
1660     if (Result.second)
1661       Result.second = MRMgr.getCXXBaseObjectRegionWithSuper(BaseReg,
1662                                                             Result.second);
1663   }
1664 
1665   return Result;
1666 }
1667 
1668 SVal RegionStoreManager::getBindingForElement(RegionBindingsConstRef B,
1669                                               const ElementRegion* R) {
1670   // We do not currently model bindings of the CompoundLiteralregion.
1671   if (isa<CompoundLiteralRegion>(R->getBaseRegion()))
1672     return UnknownVal();
1673 
1674   // Check if the region has a binding.
1675   if (const Optional<SVal> &V = B.getDirectBinding(R))
1676     return *V;
1677 
1678   const MemRegion* superR = R->getSuperRegion();
1679 
1680   // Check if the region is an element region of a string literal.
1681   if (const StringRegion *StrR = dyn_cast<StringRegion>(superR)) {
1682     // FIXME: Handle loads from strings where the literal is treated as
1683     // an integer, e.g., *((unsigned int*)"hello")
1684     QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
1685     if (!Ctx.hasSameUnqualifiedType(T, R->getElementType()))
1686       return UnknownVal();
1687 
1688     const StringLiteral *Str = StrR->getStringLiteral();
1689     SVal Idx = R->getIndex();
1690     if (Optional<nonloc::ConcreteInt> CI = Idx.getAs<nonloc::ConcreteInt>()) {
1691       int64_t i = CI->getValue().getSExtValue();
1692       // Abort on string underrun.  This can be possible by arbitrary
1693       // clients of getBindingForElement().
1694       if (i < 0)
1695         return UndefinedVal();
1696       int64_t length = Str->getLength();
1697       // Technically, only i == length is guaranteed to be null.
1698       // However, such overflows should be caught before reaching this point;
1699       // the only time such an access would be made is if a string literal was
1700       // used to initialize a larger array.
1701       char c = (i >= length) ? '\0' : Str->getCodeUnit(i);
1702       return svalBuilder.makeIntVal(c, T);
1703     }
1704   } else if (const VarRegion *VR = dyn_cast<VarRegion>(superR)) {
1705     // Check if the containing array has an initialized value that we can trust.
1706     // We can trust a const value or a value of a global initializer in main().
1707     const VarDecl *VD = VR->getDecl();
1708     if (VD->getType().isConstQualified() ||
1709         R->getElementType().isConstQualified() ||
1710         (B.isMainAnalysis() && VD->hasGlobalStorage())) {
1711       if (const Expr *Init = VD->getAnyInitializer()) {
1712         if (const auto *InitList = dyn_cast<InitListExpr>(Init)) {
1713           // The array index has to be known.
1714           if (auto CI = R->getIndex().getAs<nonloc::ConcreteInt>()) {
1715             int64_t i = CI->getValue().getSExtValue();
1716             // If it is known that the index is out of bounds, we can return
1717             // an undefined value.
1718             if (i < 0)
1719               return UndefinedVal();
1720 
1721             if (auto CAT = Ctx.getAsConstantArrayType(VD->getType()))
1722               if (CAT->getSize().sle(i))
1723                 return UndefinedVal();
1724 
1725             // If there is a list, but no init, it must be zero.
1726             if (i >= InitList->getNumInits())
1727               return svalBuilder.makeZeroVal(R->getElementType());
1728 
1729             if (const Expr *ElemInit = InitList->getInit(i))
1730               if (Optional<SVal> V = svalBuilder.getConstantVal(ElemInit))
1731                 return *V;
1732           }
1733         }
1734       }
1735     }
1736   }
1737 
1738   // Check for loads from a code text region.  For such loads, just give up.
1739   if (isa<CodeTextRegion>(superR))
1740     return UnknownVal();
1741 
1742   // Handle the case where we are indexing into a larger scalar object.
1743   // For example, this handles:
1744   //   int x = ...
1745   //   char *y = &x;
1746   //   return *y;
1747   // FIXME: This is a hack, and doesn't do anything really intelligent yet.
1748   const RegionRawOffset &O = R->getAsArrayOffset();
1749 
1750   // If we cannot reason about the offset, return an unknown value.
1751   if (!O.getRegion())
1752     return UnknownVal();
1753 
1754   if (const TypedValueRegion *baseR =
1755         dyn_cast_or_null<TypedValueRegion>(O.getRegion())) {
1756     QualType baseT = baseR->getValueType();
1757     if (baseT->isScalarType()) {
1758       QualType elemT = R->getElementType();
1759       if (elemT->isScalarType()) {
1760         if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1761           if (const Optional<SVal> &V = B.getDirectBinding(superR)) {
1762             if (SymbolRef parentSym = V->getAsSymbol())
1763               return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1764 
1765             if (V->isUnknownOrUndef())
1766               return *V;
1767             // Other cases: give up.  We are indexing into a larger object
1768             // that has some value, but we don't know how to handle that yet.
1769             return UnknownVal();
1770           }
1771         }
1772       }
1773     }
1774   }
1775   return getBindingForFieldOrElementCommon(B, R, R->getElementType());
1776 }
1777 
1778 SVal RegionStoreManager::getBindingForField(RegionBindingsConstRef B,
1779                                             const FieldRegion* R) {
1780 
1781   // Check if the region has a binding.
1782   if (const Optional<SVal> &V = B.getDirectBinding(R))
1783     return *V;
1784 
1785   // Is the field declared constant and has an in-class initializer?
1786   const FieldDecl *FD = R->getDecl();
1787   QualType Ty = FD->getType();
1788   if (Ty.isConstQualified())
1789     if (const Expr *Init = FD->getInClassInitializer())
1790       if (Optional<SVal> V = svalBuilder.getConstantVal(Init))
1791         return *V;
1792 
1793   // If the containing record was initialized, try to get its constant value.
1794   const MemRegion* superR = R->getSuperRegion();
1795   if (const auto *VR = dyn_cast<VarRegion>(superR)) {
1796     const VarDecl *VD = VR->getDecl();
1797     QualType RecordVarTy = VD->getType();
1798     unsigned Index = FD->getFieldIndex();
1799     // Either the record variable or the field has an initializer that we can
1800     // trust. We trust initializers of constants and, additionally, respect
1801     // initializers of globals when analyzing main().
1802     if (RecordVarTy.isConstQualified() || Ty.isConstQualified() ||
1803         (B.isMainAnalysis() && VD->hasGlobalStorage()))
1804       if (const Expr *Init = VD->getAnyInitializer())
1805         if (const auto *InitList = dyn_cast<InitListExpr>(Init)) {
1806           if (Index < InitList->getNumInits()) {
1807             if (const Expr *FieldInit = InitList->getInit(Index))
1808               if (Optional<SVal> V = svalBuilder.getConstantVal(FieldInit))
1809                 return *V;
1810           } else {
1811             return svalBuilder.makeZeroVal(Ty);
1812           }
1813         }
1814   }
1815 
1816   return getBindingForFieldOrElementCommon(B, R, Ty);
1817 }
1818 
1819 Optional<SVal>
1820 RegionStoreManager::getBindingForDerivedDefaultValue(RegionBindingsConstRef B,
1821                                                      const MemRegion *superR,
1822                                                      const TypedValueRegion *R,
1823                                                      QualType Ty) {
1824 
1825   if (const Optional<SVal> &D = B.getDefaultBinding(superR)) {
1826     const SVal &val = D.getValue();
1827     if (SymbolRef parentSym = val.getAsSymbol())
1828       return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1829 
1830     if (val.isZeroConstant())
1831       return svalBuilder.makeZeroVal(Ty);
1832 
1833     if (val.isUnknownOrUndef())
1834       return val;
1835 
1836     // Lazy bindings are usually handled through getExistingLazyBinding().
1837     // We should unify these two code paths at some point.
1838     if (val.getAs<nonloc::LazyCompoundVal>() ||
1839         val.getAs<nonloc::CompoundVal>())
1840       return val;
1841 
1842     llvm_unreachable("Unknown default value");
1843   }
1844 
1845   return None;
1846 }
1847 
1848 SVal RegionStoreManager::getLazyBinding(const SubRegion *LazyBindingRegion,
1849                                         RegionBindingsRef LazyBinding) {
1850   SVal Result;
1851   if (const ElementRegion *ER = dyn_cast<ElementRegion>(LazyBindingRegion))
1852     Result = getBindingForElement(LazyBinding, ER);
1853   else
1854     Result = getBindingForField(LazyBinding,
1855                                 cast<FieldRegion>(LazyBindingRegion));
1856 
1857   // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
1858   // default value for /part/ of an aggregate from a default value for the
1859   // /entire/ aggregate. The most common case of this is when struct Outer
1860   // has as its first member a struct Inner, which is copied in from a stack
1861   // variable. In this case, even if the Outer's default value is symbolic, 0,
1862   // or unknown, it gets overridden by the Inner's default value of undefined.
1863   //
1864   // This is a general problem -- if the Inner is zero-initialized, the Outer
1865   // will now look zero-initialized. The proper way to solve this is with a
1866   // new version of RegionStore that tracks the extent of a binding as well
1867   // as the offset.
1868   //
1869   // This hack only takes care of the undefined case because that can very
1870   // quickly result in a warning.
1871   if (Result.isUndef())
1872     Result = UnknownVal();
1873 
1874   return Result;
1875 }
1876 
1877 SVal
1878 RegionStoreManager::getBindingForFieldOrElementCommon(RegionBindingsConstRef B,
1879                                                       const TypedValueRegion *R,
1880                                                       QualType Ty) {
1881 
1882   // At this point we have already checked in either getBindingForElement or
1883   // getBindingForField if 'R' has a direct binding.
1884 
1885   // Lazy binding?
1886   Store lazyBindingStore = nullptr;
1887   const SubRegion *lazyBindingRegion = nullptr;
1888   std::tie(lazyBindingStore, lazyBindingRegion) = findLazyBinding(B, R, R);
1889   if (lazyBindingRegion)
1890     return getLazyBinding(lazyBindingRegion,
1891                           getRegionBindings(lazyBindingStore));
1892 
1893   // Record whether or not we see a symbolic index.  That can completely
1894   // be out of scope of our lookup.
1895   bool hasSymbolicIndex = false;
1896 
1897   // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
1898   // default value for /part/ of an aggregate from a default value for the
1899   // /entire/ aggregate. The most common case of this is when struct Outer
1900   // has as its first member a struct Inner, which is copied in from a stack
1901   // variable. In this case, even if the Outer's default value is symbolic, 0,
1902   // or unknown, it gets overridden by the Inner's default value of undefined.
1903   //
1904   // This is a general problem -- if the Inner is zero-initialized, the Outer
1905   // will now look zero-initialized. The proper way to solve this is with a
1906   // new version of RegionStore that tracks the extent of a binding as well
1907   // as the offset.
1908   //
1909   // This hack only takes care of the undefined case because that can very
1910   // quickly result in a warning.
1911   bool hasPartialLazyBinding = false;
1912 
1913   const SubRegion *SR = R;
1914   while (SR) {
1915     const MemRegion *Base = SR->getSuperRegion();
1916     if (Optional<SVal> D = getBindingForDerivedDefaultValue(B, Base, R, Ty)) {
1917       if (D->getAs<nonloc::LazyCompoundVal>()) {
1918         hasPartialLazyBinding = true;
1919         break;
1920       }
1921 
1922       return *D;
1923     }
1924 
1925     if (const ElementRegion *ER = dyn_cast<ElementRegion>(Base)) {
1926       NonLoc index = ER->getIndex();
1927       if (!index.isConstant())
1928         hasSymbolicIndex = true;
1929     }
1930 
1931     // If our super region is a field or element itself, walk up the region
1932     // hierarchy to see if there is a default value installed in an ancestor.
1933     SR = dyn_cast<SubRegion>(Base);
1934   }
1935 
1936   if (R->hasStackNonParametersStorage()) {
1937     if (isa<ElementRegion>(R)) {
1938       // Currently we don't reason specially about Clang-style vectors.  Check
1939       // if superR is a vector and if so return Unknown.
1940       if (const TypedValueRegion *typedSuperR =
1941             dyn_cast<TypedValueRegion>(R->getSuperRegion())) {
1942         if (typedSuperR->getValueType()->isVectorType())
1943           return UnknownVal();
1944       }
1945     }
1946 
1947     // FIXME: We also need to take ElementRegions with symbolic indexes into
1948     // account.  This case handles both directly accessing an ElementRegion
1949     // with a symbolic offset, but also fields within an element with
1950     // a symbolic offset.
1951     if (hasSymbolicIndex)
1952       return UnknownVal();
1953 
1954     // Additionally allow introspection of a block's internal layout.
1955     if (!hasPartialLazyBinding && !isa<BlockDataRegion>(R->getBaseRegion()))
1956       return UndefinedVal();
1957   }
1958 
1959   // All other values are symbolic.
1960   return svalBuilder.getRegionValueSymbolVal(R);
1961 }
1962 
1963 SVal RegionStoreManager::getBindingForObjCIvar(RegionBindingsConstRef B,
1964                                                const ObjCIvarRegion* R) {
1965   // Check if the region has a binding.
1966   if (const Optional<SVal> &V = B.getDirectBinding(R))
1967     return *V;
1968 
1969   const MemRegion *superR = R->getSuperRegion();
1970 
1971   // Check if the super region has a default binding.
1972   if (const Optional<SVal> &V = B.getDefaultBinding(superR)) {
1973     if (SymbolRef parentSym = V->getAsSymbol())
1974       return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1975 
1976     // Other cases: give up.
1977     return UnknownVal();
1978   }
1979 
1980   return getBindingForLazySymbol(R);
1981 }
1982 
1983 SVal RegionStoreManager::getBindingForVar(RegionBindingsConstRef B,
1984                                           const VarRegion *R) {
1985 
1986   // Check if the region has a binding.
1987   if (Optional<SVal> V = B.getDirectBinding(R))
1988     return *V;
1989 
1990   if (Optional<SVal> V = B.getDefaultBinding(R))
1991     return *V;
1992 
1993   // Lazily derive a value for the VarRegion.
1994   const VarDecl *VD = R->getDecl();
1995   const MemSpaceRegion *MS = R->getMemorySpace();
1996 
1997   // Arguments are always symbolic.
1998   if (isa<StackArgumentsSpaceRegion>(MS))
1999     return svalBuilder.getRegionValueSymbolVal(R);
2000 
2001   // Is 'VD' declared constant?  If so, retrieve the constant value.
2002   if (VD->getType().isConstQualified()) {
2003     if (const Expr *Init = VD->getAnyInitializer()) {
2004       if (Optional<SVal> V = svalBuilder.getConstantVal(Init))
2005         return *V;
2006 
2007       // If the variable is const qualified and has an initializer but
2008       // we couldn't evaluate initializer to a value, treat the value as
2009       // unknown.
2010       return UnknownVal();
2011     }
2012   }
2013 
2014   // This must come after the check for constants because closure-captured
2015   // constant variables may appear in UnknownSpaceRegion.
2016   if (isa<UnknownSpaceRegion>(MS))
2017     return svalBuilder.getRegionValueSymbolVal(R);
2018 
2019   if (isa<GlobalsSpaceRegion>(MS)) {
2020     QualType T = VD->getType();
2021 
2022     // If we're in main(), then global initializers have not become stale yet.
2023     if (B.isMainAnalysis())
2024       if (const Expr *Init = VD->getAnyInitializer())
2025         if (Optional<SVal> V = svalBuilder.getConstantVal(Init))
2026           return *V;
2027 
2028     // Function-scoped static variables are default-initialized to 0; if they
2029     // have an initializer, it would have been processed by now.
2030     // FIXME: This is only true when we're starting analysis from main().
2031     // We're losing a lot of coverage here.
2032     if (isa<StaticGlobalSpaceRegion>(MS))
2033       return svalBuilder.makeZeroVal(T);
2034 
2035     if (Optional<SVal> V = getBindingForDerivedDefaultValue(B, MS, R, T)) {
2036       assert(!V->getAs<nonloc::LazyCompoundVal>());
2037       return V.getValue();
2038     }
2039 
2040     return svalBuilder.getRegionValueSymbolVal(R);
2041   }
2042 
2043   return UndefinedVal();
2044 }
2045 
2046 SVal RegionStoreManager::getBindingForLazySymbol(const TypedValueRegion *R) {
2047   // All other values are symbolic.
2048   return svalBuilder.getRegionValueSymbolVal(R);
2049 }
2050 
2051 const RegionStoreManager::SValListTy &
2052 RegionStoreManager::getInterestingValues(nonloc::LazyCompoundVal LCV) {
2053   // First, check the cache.
2054   LazyBindingsMapTy::iterator I = LazyBindingsMap.find(LCV.getCVData());
2055   if (I != LazyBindingsMap.end())
2056     return I->second;
2057 
2058   // If we don't have a list of values cached, start constructing it.
2059   SValListTy List;
2060 
2061   const SubRegion *LazyR = LCV.getRegion();
2062   RegionBindingsRef B = getRegionBindings(LCV.getStore());
2063 
2064   // If this region had /no/ bindings at the time, there are no interesting
2065   // values to return.
2066   const ClusterBindings *Cluster = B.lookup(LazyR->getBaseRegion());
2067   if (!Cluster)
2068     return (LazyBindingsMap[LCV.getCVData()] = std::move(List));
2069 
2070   SmallVector<BindingPair, 32> Bindings;
2071   collectSubRegionBindings(Bindings, svalBuilder, *Cluster, LazyR,
2072                            /*IncludeAllDefaultBindings=*/true);
2073   for (SmallVectorImpl<BindingPair>::const_iterator I = Bindings.begin(),
2074                                                     E = Bindings.end();
2075        I != E; ++I) {
2076     SVal V = I->second;
2077     if (V.isUnknownOrUndef() || V.isConstant())
2078       continue;
2079 
2080     if (Optional<nonloc::LazyCompoundVal> InnerLCV =
2081             V.getAs<nonloc::LazyCompoundVal>()) {
2082       const SValListTy &InnerList = getInterestingValues(*InnerLCV);
2083       List.insert(List.end(), InnerList.begin(), InnerList.end());
2084       continue;
2085     }
2086 
2087     List.push_back(V);
2088   }
2089 
2090   return (LazyBindingsMap[LCV.getCVData()] = std::move(List));
2091 }
2092 
2093 NonLoc RegionStoreManager::createLazyBinding(RegionBindingsConstRef B,
2094                                              const TypedValueRegion *R) {
2095   if (Optional<nonloc::LazyCompoundVal> V =
2096         getExistingLazyBinding(svalBuilder, B, R, false))
2097     return *V;
2098 
2099   return svalBuilder.makeLazyCompoundVal(StoreRef(B.asStore(), *this), R);
2100 }
2101 
2102 static bool isRecordEmpty(const RecordDecl *RD) {
2103   if (!RD->field_empty())
2104     return false;
2105   if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD))
2106     return CRD->getNumBases() == 0;
2107   return true;
2108 }
2109 
2110 SVal RegionStoreManager::getBindingForStruct(RegionBindingsConstRef B,
2111                                              const TypedValueRegion *R) {
2112   const RecordDecl *RD = R->getValueType()->castAs<RecordType>()->getDecl();
2113   if (!RD->getDefinition() || isRecordEmpty(RD))
2114     return UnknownVal();
2115 
2116   return createLazyBinding(B, R);
2117 }
2118 
2119 SVal RegionStoreManager::getBindingForArray(RegionBindingsConstRef B,
2120                                             const TypedValueRegion *R) {
2121   assert(Ctx.getAsConstantArrayType(R->getValueType()) &&
2122          "Only constant array types can have compound bindings.");
2123 
2124   return createLazyBinding(B, R);
2125 }
2126 
2127 bool RegionStoreManager::includedInBindings(Store store,
2128                                             const MemRegion *region) const {
2129   RegionBindingsRef B = getRegionBindings(store);
2130   region = region->getBaseRegion();
2131 
2132   // Quick path: if the base is the head of a cluster, the region is live.
2133   if (B.lookup(region))
2134     return true;
2135 
2136   // Slow path: if the region is the VALUE of any binding, it is live.
2137   for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
2138     const ClusterBindings &Cluster = RI.getData();
2139     for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
2140          CI != CE; ++CI) {
2141       const SVal &D = CI.getData();
2142       if (const MemRegion *R = D.getAsRegion())
2143         if (R->getBaseRegion() == region)
2144           return true;
2145     }
2146   }
2147 
2148   return false;
2149 }
2150 
2151 //===----------------------------------------------------------------------===//
2152 // Binding values to regions.
2153 //===----------------------------------------------------------------------===//
2154 
2155 StoreRef RegionStoreManager::killBinding(Store ST, Loc L) {
2156   if (Optional<loc::MemRegionVal> LV = L.getAs<loc::MemRegionVal>())
2157     if (const MemRegion* R = LV->getRegion())
2158       return StoreRef(getRegionBindings(ST).removeBinding(R)
2159                                            .asImmutableMap()
2160                                            .getRootWithoutRetain(),
2161                       *this);
2162 
2163   return StoreRef(ST, *this);
2164 }
2165 
2166 RegionBindingsRef
2167 RegionStoreManager::bind(RegionBindingsConstRef B, Loc L, SVal V) {
2168   if (L.getAs<loc::ConcreteInt>())
2169     return B;
2170 
2171   // If we get here, the location should be a region.
2172   const MemRegion *R = L.castAs<loc::MemRegionVal>().getRegion();
2173 
2174   // Check if the region is a struct region.
2175   if (const TypedValueRegion* TR = dyn_cast<TypedValueRegion>(R)) {
2176     QualType Ty = TR->getValueType();
2177     if (Ty->isArrayType())
2178       return bindArray(B, TR, V);
2179     if (Ty->isStructureOrClassType())
2180       return bindStruct(B, TR, V);
2181     if (Ty->isVectorType())
2182       return bindVector(B, TR, V);
2183     if (Ty->isUnionType())
2184       return bindAggregate(B, TR, V);
2185   }
2186 
2187   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
2188     // Binding directly to a symbolic region should be treated as binding
2189     // to element 0.
2190     QualType T = SR->getSymbol()->getType();
2191     if (T->isAnyPointerType() || T->isReferenceType())
2192       T = T->getPointeeType();
2193 
2194     R = GetElementZeroRegion(SR, T);
2195   }
2196 
2197   assert((!isa<CXXThisRegion>(R) || !B.lookup(R)) &&
2198          "'this' pointer is not an l-value and is not assignable");
2199 
2200   // Clear out bindings that may overlap with this binding.
2201   RegionBindingsRef NewB = removeSubRegionBindings(B, cast<SubRegion>(R));
2202   return NewB.addBinding(BindingKey::Make(R, BindingKey::Direct), V);
2203 }
2204 
2205 RegionBindingsRef
2206 RegionStoreManager::setImplicitDefaultValue(RegionBindingsConstRef B,
2207                                             const MemRegion *R,
2208                                             QualType T) {
2209   SVal V;
2210 
2211   if (Loc::isLocType(T))
2212     V = svalBuilder.makeNull();
2213   else if (T->isIntegralOrEnumerationType())
2214     V = svalBuilder.makeZeroVal(T);
2215   else if (T->isStructureOrClassType() || T->isArrayType()) {
2216     // Set the default value to a zero constant when it is a structure
2217     // or array.  The type doesn't really matter.
2218     V = svalBuilder.makeZeroVal(Ctx.IntTy);
2219   }
2220   else {
2221     // We can't represent values of this type, but we still need to set a value
2222     // to record that the region has been initialized.
2223     // If this assertion ever fires, a new case should be added above -- we
2224     // should know how to default-initialize any value we can symbolicate.
2225     assert(!SymbolManager::canSymbolicate(T) && "This type is representable");
2226     V = UnknownVal();
2227   }
2228 
2229   return B.addBinding(R, BindingKey::Default, V);
2230 }
2231 
2232 RegionBindingsRef
2233 RegionStoreManager::bindArray(RegionBindingsConstRef B,
2234                               const TypedValueRegion* R,
2235                               SVal Init) {
2236 
2237   const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
2238   QualType ElementTy = AT->getElementType();
2239   Optional<uint64_t> Size;
2240 
2241   if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
2242     Size = CAT->getSize().getZExtValue();
2243 
2244   // Check if the init expr is a literal. If so, bind the rvalue instead.
2245   // FIXME: It's not responsibility of the Store to transform this lvalue
2246   // to rvalue. ExprEngine or maybe even CFG should do this before binding.
2247   if (Optional<loc::MemRegionVal> MRV = Init.getAs<loc::MemRegionVal>()) {
2248     SVal V = getBinding(B.asStore(), *MRV, R->getValueType());
2249     return bindAggregate(B, R, V);
2250   }
2251 
2252   // Handle lazy compound values.
2253   if (Init.getAs<nonloc::LazyCompoundVal>())
2254     return bindAggregate(B, R, Init);
2255 
2256   if (Init.isUnknown())
2257     return bindAggregate(B, R, UnknownVal());
2258 
2259   // Remaining case: explicit compound values.
2260   const nonloc::CompoundVal& CV = Init.castAs<nonloc::CompoundVal>();
2261   nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
2262   uint64_t i = 0;
2263 
2264   RegionBindingsRef NewB(B);
2265 
2266   for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
2267     // The init list might be shorter than the array length.
2268     if (VI == VE)
2269       break;
2270 
2271     const NonLoc &Idx = svalBuilder.makeArrayIndex(i);
2272     const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
2273 
2274     if (ElementTy->isStructureOrClassType())
2275       NewB = bindStruct(NewB, ER, *VI);
2276     else if (ElementTy->isArrayType())
2277       NewB = bindArray(NewB, ER, *VI);
2278     else
2279       NewB = bind(NewB, loc::MemRegionVal(ER), *VI);
2280   }
2281 
2282   // If the init list is shorter than the array length (or the array has
2283   // variable length), set the array default value. Values that are already set
2284   // are not overwritten.
2285   if (!Size.hasValue() || i < Size.getValue())
2286     NewB = setImplicitDefaultValue(NewB, R, ElementTy);
2287 
2288   return NewB;
2289 }
2290 
2291 RegionBindingsRef RegionStoreManager::bindVector(RegionBindingsConstRef B,
2292                                                  const TypedValueRegion* R,
2293                                                  SVal V) {
2294   QualType T = R->getValueType();
2295   const VectorType *VT = T->castAs<VectorType>(); // Use castAs for typedefs.
2296 
2297   // Handle lazy compound values and symbolic values.
2298   if (V.getAs<nonloc::LazyCompoundVal>() || V.getAs<nonloc::SymbolVal>())
2299     return bindAggregate(B, R, V);
2300 
2301   // We may get non-CompoundVal accidentally due to imprecise cast logic or
2302   // that we are binding symbolic struct value. Kill the field values, and if
2303   // the value is symbolic go and bind it as a "default" binding.
2304   if (!V.getAs<nonloc::CompoundVal>()) {
2305     return bindAggregate(B, R, UnknownVal());
2306   }
2307 
2308   QualType ElemType = VT->getElementType();
2309   nonloc::CompoundVal CV = V.castAs<nonloc::CompoundVal>();
2310   nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
2311   unsigned index = 0, numElements = VT->getNumElements();
2312   RegionBindingsRef NewB(B);
2313 
2314   for ( ; index != numElements ; ++index) {
2315     if (VI == VE)
2316       break;
2317 
2318     NonLoc Idx = svalBuilder.makeArrayIndex(index);
2319     const ElementRegion *ER = MRMgr.getElementRegion(ElemType, Idx, R, Ctx);
2320 
2321     if (ElemType->isArrayType())
2322       NewB = bindArray(NewB, ER, *VI);
2323     else if (ElemType->isStructureOrClassType())
2324       NewB = bindStruct(NewB, ER, *VI);
2325     else
2326       NewB = bind(NewB, loc::MemRegionVal(ER), *VI);
2327   }
2328   return NewB;
2329 }
2330 
2331 Optional<RegionBindingsRef>
2332 RegionStoreManager::tryBindSmallStruct(RegionBindingsConstRef B,
2333                                        const TypedValueRegion *R,
2334                                        const RecordDecl *RD,
2335                                        nonloc::LazyCompoundVal LCV) {
2336   FieldVector Fields;
2337 
2338   if (const CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(RD))
2339     if (Class->getNumBases() != 0 || Class->getNumVBases() != 0)
2340       return None;
2341 
2342   for (const auto *FD : RD->fields()) {
2343     if (FD->isUnnamedBitfield())
2344       continue;
2345 
2346     // If there are too many fields, or if any of the fields are aggregates,
2347     // just use the LCV as a default binding.
2348     if (Fields.size() == SmallStructLimit)
2349       return None;
2350 
2351     QualType Ty = FD->getType();
2352     if (!(Ty->isScalarType() || Ty->isReferenceType()))
2353       return None;
2354 
2355     Fields.push_back(FD);
2356   }
2357 
2358   RegionBindingsRef NewB = B;
2359 
2360   for (FieldVector::iterator I = Fields.begin(), E = Fields.end(); I != E; ++I){
2361     const FieldRegion *SourceFR = MRMgr.getFieldRegion(*I, LCV.getRegion());
2362     SVal V = getBindingForField(getRegionBindings(LCV.getStore()), SourceFR);
2363 
2364     const FieldRegion *DestFR = MRMgr.getFieldRegion(*I, R);
2365     NewB = bind(NewB, loc::MemRegionVal(DestFR), V);
2366   }
2367 
2368   return NewB;
2369 }
2370 
2371 RegionBindingsRef RegionStoreManager::bindStruct(RegionBindingsConstRef B,
2372                                                  const TypedValueRegion* R,
2373                                                  SVal V) {
2374   if (!Features.supportsFields())
2375     return B;
2376 
2377   QualType T = R->getValueType();
2378   assert(T->isStructureOrClassType());
2379 
2380   const RecordType* RT = T->castAs<RecordType>();
2381   const RecordDecl *RD = RT->getDecl();
2382 
2383   if (!RD->isCompleteDefinition())
2384     return B;
2385 
2386   // Handle lazy compound values and symbolic values.
2387   if (Optional<nonloc::LazyCompoundVal> LCV =
2388         V.getAs<nonloc::LazyCompoundVal>()) {
2389     if (Optional<RegionBindingsRef> NewB = tryBindSmallStruct(B, R, RD, *LCV))
2390       return *NewB;
2391     return bindAggregate(B, R, V);
2392   }
2393   if (V.getAs<nonloc::SymbolVal>())
2394     return bindAggregate(B, R, V);
2395 
2396   // We may get non-CompoundVal accidentally due to imprecise cast logic or
2397   // that we are binding symbolic struct value. Kill the field values, and if
2398   // the value is symbolic go and bind it as a "default" binding.
2399   if (V.isUnknown() || !V.getAs<nonloc::CompoundVal>())
2400     return bindAggregate(B, R, UnknownVal());
2401 
2402   // The raw CompoundVal is essentially a symbolic InitListExpr: an (immutable)
2403   // list of other values. It appears pretty much only when there's an actual
2404   // initializer list expression in the program, and the analyzer tries to
2405   // unwrap it as soon as possible.
2406   // This code is where such unwrap happens: when the compound value is put into
2407   // the object that it was supposed to initialize (it's an *initializer* list,
2408   // after all), instead of binding the whole value to the whole object, we bind
2409   // sub-values to sub-objects. Sub-values may themselves be compound values,
2410   // and in this case the procedure becomes recursive.
2411   // FIXME: The annoying part about compound values is that they don't carry
2412   // any sort of information about which value corresponds to which sub-object.
2413   // It's simply a list of values in the middle of nowhere; we expect to match
2414   // them to sub-objects, essentially, "by index": first value binds to
2415   // the first field, second value binds to the second field, etc.
2416   // It would have been much safer to organize non-lazy compound values as
2417   // a mapping from fields/bases to values.
2418   const nonloc::CompoundVal& CV = V.castAs<nonloc::CompoundVal>();
2419   nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
2420 
2421   RegionBindingsRef NewB(B);
2422 
2423   // In C++17 aggregates may have base classes, handle those as well.
2424   // They appear before fields in the initializer list / compound value.
2425   if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
2426     // If the object was constructed with a constructor, its value is a
2427     // LazyCompoundVal. If it's a raw CompoundVal, it means that we're
2428     // performing aggregate initialization. The only exception from this
2429     // rule is sending an Objective-C++ message that returns a C++ object
2430     // to a nil receiver; in this case the semantics is to return a
2431     // zero-initialized object even if it's a C++ object that doesn't have
2432     // this sort of constructor; the CompoundVal is empty in this case.
2433     assert((CRD->isAggregate() || (Ctx.getLangOpts().ObjC && VI == VE)) &&
2434            "Non-aggregates are constructed with a constructor!");
2435 
2436     for (const auto &B : CRD->bases()) {
2437       // (Multiple inheritance is fine though.)
2438       assert(!B.isVirtual() && "Aggregates cannot have virtual base classes!");
2439 
2440       if (VI == VE)
2441         break;
2442 
2443       QualType BTy = B.getType();
2444       assert(BTy->isStructureOrClassType() && "Base classes must be classes!");
2445 
2446       const CXXRecordDecl *BRD = BTy->getAsCXXRecordDecl();
2447       assert(BRD && "Base classes must be C++ classes!");
2448 
2449       const CXXBaseObjectRegion *BR =
2450           MRMgr.getCXXBaseObjectRegion(BRD, R, /*IsVirtual=*/false);
2451 
2452       NewB = bindStruct(NewB, BR, *VI);
2453 
2454       ++VI;
2455     }
2456   }
2457 
2458   RecordDecl::field_iterator FI, FE;
2459 
2460   for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI) {
2461 
2462     if (VI == VE)
2463       break;
2464 
2465     // Skip any unnamed bitfields to stay in sync with the initializers.
2466     if (FI->isUnnamedBitfield())
2467       continue;
2468 
2469     QualType FTy = FI->getType();
2470     const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
2471 
2472     if (FTy->isArrayType())
2473       NewB = bindArray(NewB, FR, *VI);
2474     else if (FTy->isStructureOrClassType())
2475       NewB = bindStruct(NewB, FR, *VI);
2476     else
2477       NewB = bind(NewB, loc::MemRegionVal(FR), *VI);
2478     ++VI;
2479   }
2480 
2481   // There may be fewer values in the initialize list than the fields of struct.
2482   if (FI != FE) {
2483     NewB = NewB.addBinding(R, BindingKey::Default,
2484                            svalBuilder.makeIntVal(0, false));
2485   }
2486 
2487   return NewB;
2488 }
2489 
2490 RegionBindingsRef
2491 RegionStoreManager::bindAggregate(RegionBindingsConstRef B,
2492                                   const TypedRegion *R,
2493                                   SVal Val) {
2494   // Remove the old bindings, using 'R' as the root of all regions
2495   // we will invalidate. Then add the new binding.
2496   return removeSubRegionBindings(B, R).addBinding(R, BindingKey::Default, Val);
2497 }
2498 
2499 //===----------------------------------------------------------------------===//
2500 // State pruning.
2501 //===----------------------------------------------------------------------===//
2502 
2503 namespace {
2504 class RemoveDeadBindingsWorker
2505     : public ClusterAnalysis<RemoveDeadBindingsWorker> {
2506   SmallVector<const SymbolicRegion *, 12> Postponed;
2507   SymbolReaper &SymReaper;
2508   const StackFrameContext *CurrentLCtx;
2509 
2510 public:
2511   RemoveDeadBindingsWorker(RegionStoreManager &rm,
2512                            ProgramStateManager &stateMgr,
2513                            RegionBindingsRef b, SymbolReaper &symReaper,
2514                            const StackFrameContext *LCtx)
2515     : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
2516       SymReaper(symReaper), CurrentLCtx(LCtx) {}
2517 
2518   // Called by ClusterAnalysis.
2519   void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C);
2520   void VisitCluster(const MemRegion *baseR, const ClusterBindings *C);
2521   using ClusterAnalysis<RemoveDeadBindingsWorker>::VisitCluster;
2522 
2523   using ClusterAnalysis::AddToWorkList;
2524 
2525   bool AddToWorkList(const MemRegion *R);
2526 
2527   bool UpdatePostponed();
2528   void VisitBinding(SVal V);
2529 };
2530 }
2531 
2532 bool RemoveDeadBindingsWorker::AddToWorkList(const MemRegion *R) {
2533   const MemRegion *BaseR = R->getBaseRegion();
2534   return AddToWorkList(WorkListElement(BaseR), getCluster(BaseR));
2535 }
2536 
2537 void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
2538                                                    const ClusterBindings &C) {
2539 
2540   if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
2541     if (SymReaper.isLive(VR))
2542       AddToWorkList(baseR, &C);
2543 
2544     return;
2545   }
2546 
2547   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
2548     if (SymReaper.isLive(SR->getSymbol()))
2549       AddToWorkList(SR, &C);
2550     else
2551       Postponed.push_back(SR);
2552 
2553     return;
2554   }
2555 
2556   if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
2557     AddToWorkList(baseR, &C);
2558     return;
2559   }
2560 
2561   // CXXThisRegion in the current or parent location context is live.
2562   if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
2563     const auto *StackReg =
2564         cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
2565     const StackFrameContext *RegCtx = StackReg->getStackFrame();
2566     if (CurrentLCtx &&
2567         (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx)))
2568       AddToWorkList(TR, &C);
2569   }
2570 }
2571 
2572 void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
2573                                             const ClusterBindings *C) {
2574   if (!C)
2575     return;
2576 
2577   // Mark the symbol for any SymbolicRegion with live bindings as live itself.
2578   // This means we should continue to track that symbol.
2579   if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(baseR))
2580     SymReaper.markLive(SymR->getSymbol());
2581 
2582   for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E; ++I) {
2583     // Element index of a binding key is live.
2584     SymReaper.markElementIndicesLive(I.getKey().getRegion());
2585 
2586     VisitBinding(I.getData());
2587   }
2588 }
2589 
2590 void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
2591   // Is it a LazyCompoundVal?  All referenced regions are live as well.
2592   if (Optional<nonloc::LazyCompoundVal> LCS =
2593           V.getAs<nonloc::LazyCompoundVal>()) {
2594 
2595     const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS);
2596 
2597     for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(),
2598                                                         E = Vals.end();
2599          I != E; ++I)
2600       VisitBinding(*I);
2601 
2602     return;
2603   }
2604 
2605   // If V is a region, then add it to the worklist.
2606   if (const MemRegion *R = V.getAsRegion()) {
2607     AddToWorkList(R);
2608     SymReaper.markLive(R);
2609 
2610     // All regions captured by a block are also live.
2611     if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
2612       BlockDataRegion::referenced_vars_iterator I = BR->referenced_vars_begin(),
2613                                                 E = BR->referenced_vars_end();
2614       for ( ; I != E; ++I)
2615         AddToWorkList(I.getCapturedRegion());
2616     }
2617   }
2618 
2619 
2620   // Update the set of live symbols.
2621   for (auto SI = V.symbol_begin(), SE = V.symbol_end(); SI!=SE; ++SI)
2622     SymReaper.markLive(*SI);
2623 }
2624 
2625 bool RemoveDeadBindingsWorker::UpdatePostponed() {
2626   // See if any postponed SymbolicRegions are actually live now, after
2627   // having done a scan.
2628   bool Changed = false;
2629 
2630   for (auto I = Postponed.begin(), E = Postponed.end(); I != E; ++I) {
2631     if (const SymbolicRegion *SR = *I) {
2632       if (SymReaper.isLive(SR->getSymbol())) {
2633         Changed |= AddToWorkList(SR);
2634         *I = nullptr;
2635       }
2636     }
2637   }
2638 
2639   return Changed;
2640 }
2641 
2642 StoreRef RegionStoreManager::removeDeadBindings(Store store,
2643                                                 const StackFrameContext *LCtx,
2644                                                 SymbolReaper& SymReaper) {
2645   RegionBindingsRef B = getRegionBindings(store);
2646   RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
2647   W.GenerateClusters();
2648 
2649   // Enqueue the region roots onto the worklist.
2650   for (SymbolReaper::region_iterator I = SymReaper.region_begin(),
2651        E = SymReaper.region_end(); I != E; ++I) {
2652     W.AddToWorkList(*I);
2653   }
2654 
2655   do W.RunWorkList(); while (W.UpdatePostponed());
2656 
2657   // We have now scanned the store, marking reachable regions and symbols
2658   // as live.  We now remove all the regions that are dead from the store
2659   // as well as update DSymbols with the set symbols that are now dead.
2660   for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) {
2661     const MemRegion *Base = I.getKey();
2662 
2663     // If the cluster has been visited, we know the region has been marked.
2664     // Otherwise, remove the dead entry.
2665     if (!W.isVisited(Base))
2666       B = B.remove(Base);
2667   }
2668 
2669   return StoreRef(B.asStore(), *this);
2670 }
2671 
2672 //===----------------------------------------------------------------------===//
2673 // Utility methods.
2674 //===----------------------------------------------------------------------===//
2675 
2676 void RegionStoreManager::printJson(raw_ostream &Out, Store S, const char *NL,
2677                                    unsigned int Space, bool IsDot) const {
2678   RegionBindingsRef Bindings = getRegionBindings(S);
2679 
2680   Indent(Out, Space, IsDot) << "\"store\": ";
2681 
2682   if (Bindings.isEmpty()) {
2683     Out << "null," << NL;
2684     return;
2685   }
2686 
2687   Out << "{ \"pointer\": \"" << Bindings.asStore() << "\", \"items\": [" << NL;
2688   Bindings.printJson(Out, NL, Space + 1, IsDot);
2689   Indent(Out, Space, IsDot) << "]}," << NL;
2690 }
2691