• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a basic region store model. In this model, we do have field
11 // sensitivity. But we assume nothing about the heap shape. So recursive data
12 // structures are largely ignored. Basically we do 1-limiting analysis.
13 // Parameter pointers are assumed with no aliasing. Pointee objects of
14 // parameters are created lazily.
15 //
16 //===----------------------------------------------------------------------===//
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/CharUnits.h"
19 #include "clang/Analysis/Analyses/LiveVariables.h"
20 #include "clang/Analysis/AnalysisContext.h"
21 #include "clang/Basic/TargetInfo.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
26 #include "llvm/ADT/ImmutableList.h"
27 #include "llvm/ADT/ImmutableMap.h"
28 #include "llvm/ADT/Optional.h"
29 #include "llvm/Support/raw_ostream.h"
30 
31 using namespace clang;
32 using namespace ento;
33 
34 //===----------------------------------------------------------------------===//
35 // Representation of binding keys.
36 //===----------------------------------------------------------------------===//
37 
38 namespace {
39 class BindingKey {
40 public:
41   enum Kind { Default = 0x0, Direct = 0x1 };
42 private:
43   enum { Symbolic = 0x2 };
44 
45   llvm::PointerIntPair<const MemRegion *, 2> P;
46   uint64_t Data;
47 
48   /// Create a key for a binding to region \p r, which has a symbolic offset
49   /// from region \p Base.
BindingKey(const SubRegion * r,const SubRegion * Base,Kind k)50   explicit BindingKey(const SubRegion *r, const SubRegion *Base, Kind k)
51     : P(r, k | Symbolic), Data(reinterpret_cast<uintptr_t>(Base)) {
52     assert(r && Base && "Must have known regions.");
53     assert(getConcreteOffsetRegion() == Base && "Failed to store base region");
54   }
55 
56   /// Create a key for a binding at \p offset from base region \p r.
BindingKey(const MemRegion * r,uint64_t offset,Kind k)57   explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
58     : P(r, k), Data(offset) {
59     assert(r && "Must have known regions.");
60     assert(getOffset() == offset && "Failed to store offset");
61     assert((r == r->getBaseRegion() || isa<ObjCIvarRegion>(r)) && "Not a base");
62   }
63 public:
64 
isDirect() const65   bool isDirect() const { return P.getInt() & Direct; }
hasSymbolicOffset() const66   bool hasSymbolicOffset() const { return P.getInt() & Symbolic; }
67 
getRegion() const68   const MemRegion *getRegion() const { return P.getPointer(); }
getOffset() const69   uint64_t getOffset() const {
70     assert(!hasSymbolicOffset());
71     return Data;
72   }
73 
getConcreteOffsetRegion() const74   const SubRegion *getConcreteOffsetRegion() const {
75     assert(hasSymbolicOffset());
76     return reinterpret_cast<const SubRegion *>(static_cast<uintptr_t>(Data));
77   }
78 
getBaseRegion() const79   const MemRegion *getBaseRegion() const {
80     if (hasSymbolicOffset())
81       return getConcreteOffsetRegion()->getBaseRegion();
82     return getRegion()->getBaseRegion();
83   }
84 
Profile(llvm::FoldingSetNodeID & ID) const85   void Profile(llvm::FoldingSetNodeID& ID) const {
86     ID.AddPointer(P.getOpaqueValue());
87     ID.AddInteger(Data);
88   }
89 
90   static BindingKey Make(const MemRegion *R, Kind k);
91 
operator <(const BindingKey & X) const92   bool operator<(const BindingKey &X) const {
93     if (P.getOpaqueValue() < X.P.getOpaqueValue())
94       return true;
95     if (P.getOpaqueValue() > X.P.getOpaqueValue())
96       return false;
97     return Data < X.Data;
98   }
99 
operator ==(const BindingKey & X) const100   bool operator==(const BindingKey &X) const {
101     return P.getOpaqueValue() == X.P.getOpaqueValue() &&
102            Data == X.Data;
103   }
104 
105   LLVM_ATTRIBUTE_USED void dump() const;
106 };
107 } // end anonymous namespace
108 
Make(const MemRegion * R,Kind k)109 BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
110   const RegionOffset &RO = R->getAsOffset();
111   if (RO.hasSymbolicOffset())
112     return BindingKey(cast<SubRegion>(R), cast<SubRegion>(RO.getRegion()), k);
113 
114   return BindingKey(RO.getRegion(), RO.getOffset(), k);
115 }
116 
117 namespace llvm {
118   static inline
operator <<(raw_ostream & os,BindingKey K)119   raw_ostream &operator<<(raw_ostream &os, BindingKey K) {
120     os << '(' << K.getRegion();
121     if (!K.hasSymbolicOffset())
122       os << ',' << K.getOffset();
123     os << ',' << (K.isDirect() ? "direct" : "default")
124        << ')';
125     return os;
126   }
127 
128   template <typename T> struct isPodLike;
129   template <> struct isPodLike<BindingKey> {
130     static const bool value = true;
131   };
132 } // end llvm namespace
133 
dump() const134 void BindingKey::dump() const {
135   llvm::errs() << *this;
136 }
137 
138 //===----------------------------------------------------------------------===//
139 // Actual Store type.
140 //===----------------------------------------------------------------------===//
141 
142 typedef llvm::ImmutableMap<BindingKey, SVal>    ClusterBindings;
143 typedef llvm::ImmutableMapRef<BindingKey, SVal> ClusterBindingsRef;
144 typedef std::pair<BindingKey, SVal> BindingPair;
145 
146 typedef llvm::ImmutableMap<const MemRegion *, ClusterBindings>
147         RegionBindings;
148 
149 namespace {
150 class RegionBindingsRef : public llvm::ImmutableMapRef<const MemRegion *,
151                                  ClusterBindings> {
152  ClusterBindings::Factory &CBFactory;
153 public:
154   typedef llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>
155           ParentTy;
156 
RegionBindingsRef(ClusterBindings::Factory & CBFactory,const RegionBindings::TreeTy * T,RegionBindings::TreeTy::Factory * F)157   RegionBindingsRef(ClusterBindings::Factory &CBFactory,
158                     const RegionBindings::TreeTy *T,
159                     RegionBindings::TreeTy::Factory *F)
160     : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(T, F),
161       CBFactory(CBFactory) {}
162 
RegionBindingsRef(const ParentTy & P,ClusterBindings::Factory & CBFactory)163   RegionBindingsRef(const ParentTy &P, ClusterBindings::Factory &CBFactory)
164     : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(P),
165       CBFactory(CBFactory) {}
166 
add(key_type_ref K,data_type_ref D) const167   RegionBindingsRef add(key_type_ref K, data_type_ref D) const {
168     return RegionBindingsRef(static_cast<const ParentTy*>(this)->add(K, D),
169                              CBFactory);
170   }
171 
remove(key_type_ref K) const172   RegionBindingsRef remove(key_type_ref K) const {
173     return RegionBindingsRef(static_cast<const ParentTy*>(this)->remove(K),
174                              CBFactory);
175   }
176 
177   RegionBindingsRef addBinding(BindingKey K, SVal V) const;
178 
179   RegionBindingsRef addBinding(const MemRegion *R,
180                                BindingKey::Kind k, SVal V) const;
181 
operator =(const RegionBindingsRef & X)182   RegionBindingsRef &operator=(const RegionBindingsRef &X) {
183     *static_cast<ParentTy*>(this) = X;
184     return *this;
185   }
186 
187   const SVal *lookup(BindingKey K) const;
188   const SVal *lookup(const MemRegion *R, BindingKey::Kind k) const;
lookup(const MemRegion * R) const189   const ClusterBindings *lookup(const MemRegion *R) const {
190     return static_cast<const ParentTy*>(this)->lookup(R);
191   }
192 
193   RegionBindingsRef removeBinding(BindingKey K);
194 
195   RegionBindingsRef removeBinding(const MemRegion *R,
196                                   BindingKey::Kind k);
197 
removeBinding(const MemRegion * R)198   RegionBindingsRef removeBinding(const MemRegion *R) {
199     return removeBinding(R, BindingKey::Direct).
200            removeBinding(R, BindingKey::Default);
201   }
202 
203   Optional<SVal> getDirectBinding(const MemRegion *R) const;
204 
205   /// getDefaultBinding - Returns an SVal* representing an optional default
206   ///  binding associated with a region and its subregions.
207   Optional<SVal> getDefaultBinding(const MemRegion *R) const;
208 
209   /// Return the internal tree as a Store.
asStore() const210   Store asStore() const {
211     return asImmutableMap().getRootWithoutRetain();
212   }
213 
dump(raw_ostream & OS,const char * nl) const214   void dump(raw_ostream &OS, const char *nl) const {
215    for (iterator I = begin(), E = end(); I != E; ++I) {
216      const ClusterBindings &Cluster = I.getData();
217      for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
218           CI != CE; ++CI) {
219        OS << ' ' << CI.getKey() << " : " << CI.getData() << nl;
220      }
221      OS << nl;
222    }
223   }
224 
dump() const225   LLVM_ATTRIBUTE_USED void dump() const {
226     dump(llvm::errs(), "\n");
227   }
228 };
229 } // end anonymous namespace
230 
231 typedef const RegionBindingsRef& RegionBindingsConstRef;
232 
getDirectBinding(const MemRegion * R) const233 Optional<SVal> RegionBindingsRef::getDirectBinding(const MemRegion *R) const {
234   return Optional<SVal>::create(lookup(R, BindingKey::Direct));
235 }
236 
getDefaultBinding(const MemRegion * R) const237 Optional<SVal> RegionBindingsRef::getDefaultBinding(const MemRegion *R) const {
238   if (R->isBoundable())
239     if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R))
240       if (TR->getValueType()->isUnionType())
241         return UnknownVal();
242 
243   return Optional<SVal>::create(lookup(R, BindingKey::Default));
244 }
245 
addBinding(BindingKey K,SVal V) const246 RegionBindingsRef RegionBindingsRef::addBinding(BindingKey K, SVal V) const {
247   const MemRegion *Base = K.getBaseRegion();
248 
249   const ClusterBindings *ExistingCluster = lookup(Base);
250   ClusterBindings Cluster = (ExistingCluster ? *ExistingCluster
251                              : CBFactory.getEmptyMap());
252 
253   ClusterBindings NewCluster = CBFactory.add(Cluster, K, V);
254   return add(Base, NewCluster);
255 }
256 
257 
addBinding(const MemRegion * R,BindingKey::Kind k,SVal V) const258 RegionBindingsRef RegionBindingsRef::addBinding(const MemRegion *R,
259                                                 BindingKey::Kind k,
260                                                 SVal V) const {
261   return addBinding(BindingKey::Make(R, k), V);
262 }
263 
lookup(BindingKey K) const264 const SVal *RegionBindingsRef::lookup(BindingKey K) const {
265   const ClusterBindings *Cluster = lookup(K.getBaseRegion());
266   if (!Cluster)
267     return 0;
268   return Cluster->lookup(K);
269 }
270 
lookup(const MemRegion * R,BindingKey::Kind k) const271 const SVal *RegionBindingsRef::lookup(const MemRegion *R,
272                                       BindingKey::Kind k) const {
273   return lookup(BindingKey::Make(R, k));
274 }
275 
removeBinding(BindingKey K)276 RegionBindingsRef RegionBindingsRef::removeBinding(BindingKey K) {
277   const MemRegion *Base = K.getBaseRegion();
278   const ClusterBindings *Cluster = lookup(Base);
279   if (!Cluster)
280     return *this;
281 
282   ClusterBindings NewCluster = CBFactory.remove(*Cluster, K);
283   if (NewCluster.isEmpty())
284     return remove(Base);
285   return add(Base, NewCluster);
286 }
287 
removeBinding(const MemRegion * R,BindingKey::Kind k)288 RegionBindingsRef RegionBindingsRef::removeBinding(const MemRegion *R,
289                                                 BindingKey::Kind k){
290   return removeBinding(BindingKey::Make(R, k));
291 }
292 
293 //===----------------------------------------------------------------------===//
294 // Fine-grained control of RegionStoreManager.
295 //===----------------------------------------------------------------------===//
296 
297 namespace {
298 struct minimal_features_tag {};
299 struct maximal_features_tag {};
300 
301 class RegionStoreFeatures {
302   bool SupportsFields;
303 public:
RegionStoreFeatures(minimal_features_tag)304   RegionStoreFeatures(minimal_features_tag) :
305     SupportsFields(false) {}
306 
RegionStoreFeatures(maximal_features_tag)307   RegionStoreFeatures(maximal_features_tag) :
308     SupportsFields(true) {}
309 
enableFields(bool t)310   void enableFields(bool t) { SupportsFields = t; }
311 
supportsFields() const312   bool supportsFields() const { return SupportsFields; }
313 };
314 }
315 
316 //===----------------------------------------------------------------------===//
317 // Main RegionStore logic.
318 //===----------------------------------------------------------------------===//
319 
320 namespace {
321 
322 class RegionStoreManager : public StoreManager {
323 public:
324   const RegionStoreFeatures Features;
325   RegionBindings::Factory RBFactory;
326   mutable ClusterBindings::Factory CBFactory;
327 
328   typedef std::vector<SVal> SValListTy;
329 private:
330   typedef llvm::DenseMap<const LazyCompoundValData *,
331                          SValListTy> LazyBindingsMapTy;
332   LazyBindingsMapTy LazyBindingsMap;
333 
334 public:
RegionStoreManager(ProgramStateManager & mgr,const RegionStoreFeatures & f)335   RegionStoreManager(ProgramStateManager& mgr, const RegionStoreFeatures &f)
336     : StoreManager(mgr), Features(f),
337       RBFactory(mgr.getAllocator()), CBFactory(mgr.getAllocator()) {}
338 
339 
340   /// setImplicitDefaultValue - Set the default binding for the provided
341   ///  MemRegion to the value implicitly defined for compound literals when
342   ///  the value is not specified.
343   RegionBindingsRef setImplicitDefaultValue(RegionBindingsConstRef B,
344                                             const MemRegion *R, QualType T);
345 
346   /// ArrayToPointer - Emulates the "decay" of an array to a pointer
347   ///  type.  'Array' represents the lvalue of the array being decayed
348   ///  to a pointer, and the returned SVal represents the decayed
349   ///  version of that lvalue (i.e., a pointer to the first element of
350   ///  the array).  This is called by ExprEngine when evaluating
351   ///  casts from arrays to pointers.
352   SVal ArrayToPointer(Loc Array);
353 
getInitialStore(const LocationContext * InitLoc)354   StoreRef getInitialStore(const LocationContext *InitLoc) {
355     return StoreRef(RBFactory.getEmptyMap().getRootWithoutRetain(), *this);
356   }
357 
358   //===-------------------------------------------------------------------===//
359   // Binding values to regions.
360   //===-------------------------------------------------------------------===//
361   RegionBindingsRef invalidateGlobalRegion(MemRegion::Kind K,
362                                            const Expr *Ex,
363                                            unsigned Count,
364                                            const LocationContext *LCtx,
365                                            RegionBindingsRef B,
366                                            InvalidatedRegions *Invalidated);
367 
368   StoreRef invalidateRegions(Store store, ArrayRef<const MemRegion *> Regions,
369                              const Expr *E, unsigned Count,
370                              const LocationContext *LCtx,
371                              InvalidatedSymbols &IS,
372                              const CallEvent *Call,
373                              InvalidatedRegions *Invalidated);
374 
375   bool scanReachableSymbols(Store S, const MemRegion *R,
376                             ScanReachableSymbols &Callbacks);
377 
378   RegionBindingsRef removeSubRegionBindings(RegionBindingsConstRef B,
379                                             const SubRegion *R);
380 
381 public: // Part of public interface to class.
382 
Bind(Store store,Loc LV,SVal V)383   virtual StoreRef Bind(Store store, Loc LV, SVal V) {
384     return StoreRef(bind(getRegionBindings(store), LV, V).asStore(), *this);
385   }
386 
387   RegionBindingsRef bind(RegionBindingsConstRef B, Loc LV, SVal V);
388 
389   // BindDefault is only used to initialize a region with a default value.
BindDefault(Store store,const MemRegion * R,SVal V)390   StoreRef BindDefault(Store store, const MemRegion *R, SVal V) {
391     RegionBindingsRef B = getRegionBindings(store);
392     assert(!B.lookup(R, BindingKey::Default));
393     assert(!B.lookup(R, BindingKey::Direct));
394     return StoreRef(B.addBinding(R, BindingKey::Default, V)
395                      .asImmutableMap()
396                      .getRootWithoutRetain(), *this);
397   }
398 
399   /// \brief Create a new store that binds a value to a compound literal.
400   ///
401   /// \param ST The original store whose bindings are the basis for the new
402   ///        store.
403   ///
404   /// \param CL The compound literal to bind (the binding key).
405   ///
406   /// \param LC The LocationContext for the binding.
407   ///
408   /// \param V The value to bind to the compound literal.
409   StoreRef bindCompoundLiteral(Store ST,
410                                const CompoundLiteralExpr *CL,
411                                const LocationContext *LC, SVal V);
412 
413   /// BindStruct - Bind a compound value to a structure.
414   RegionBindingsRef bindStruct(RegionBindingsConstRef B,
415                                const TypedValueRegion* R, SVal V);
416 
417   /// BindVector - Bind a compound value to a vector.
418   RegionBindingsRef bindVector(RegionBindingsConstRef B,
419                                const TypedValueRegion* R, SVal V);
420 
421   RegionBindingsRef bindArray(RegionBindingsConstRef B,
422                               const TypedValueRegion* R,
423                               SVal V);
424 
425   /// Clears out all bindings in the given region and assigns a new value
426   /// as a Default binding.
427   RegionBindingsRef bindAggregate(RegionBindingsConstRef B,
428                                   const TypedRegion *R,
429                                   SVal DefaultVal);
430 
431   /// \brief Create a new store with the specified binding removed.
432   /// \param ST the original store, that is the basis for the new store.
433   /// \param L the location whose binding should be removed.
434   virtual StoreRef killBinding(Store ST, Loc L);
435 
incrementReferenceCount(Store store)436   void incrementReferenceCount(Store store) {
437     getRegionBindings(store).manualRetain();
438   }
439 
440   /// If the StoreManager supports it, decrement the reference count of
441   /// the specified Store object.  If the reference count hits 0, the memory
442   /// associated with the object is recycled.
decrementReferenceCount(Store store)443   void decrementReferenceCount(Store store) {
444     getRegionBindings(store).manualRelease();
445   }
446 
447   bool includedInBindings(Store store, const MemRegion *region) const;
448 
449   /// \brief Return the value bound to specified location in a given state.
450   ///
451   /// The high level logic for this method is this:
452   /// getBinding (L)
453   ///   if L has binding
454   ///     return L's binding
455   ///   else if L is in killset
456   ///     return unknown
457   ///   else
458   ///     if L is on stack or heap
459   ///       return undefined
460   ///     else
461   ///       return symbolic
getBinding(Store S,Loc L,QualType T)462   virtual SVal getBinding(Store S, Loc L, QualType T) {
463     return getBinding(getRegionBindings(S), L, T);
464   }
465 
466   SVal getBinding(RegionBindingsConstRef B, Loc L, QualType T = QualType());
467 
468   SVal getBindingForElement(RegionBindingsConstRef B, const ElementRegion *R);
469 
470   SVal getBindingForField(RegionBindingsConstRef B, const FieldRegion *R);
471 
472   SVal getBindingForObjCIvar(RegionBindingsConstRef B, const ObjCIvarRegion *R);
473 
474   SVal getBindingForVar(RegionBindingsConstRef B, const VarRegion *R);
475 
476   SVal getBindingForLazySymbol(const TypedValueRegion *R);
477 
478   SVal getBindingForFieldOrElementCommon(RegionBindingsConstRef B,
479                                          const TypedValueRegion *R,
480                                          QualType Ty,
481                                          const MemRegion *superR);
482 
483   SVal getLazyBinding(const SubRegion *LazyBindingRegion,
484                       RegionBindingsRef LazyBinding);
485 
486   /// Get bindings for the values in a struct and return a CompoundVal, used
487   /// when doing struct copy:
488   /// struct s x, y;
489   /// x = y;
490   /// y's value is retrieved by this method.
491   SVal getBindingForStruct(RegionBindingsConstRef B, const TypedValueRegion *R);
492   SVal getBindingForArray(RegionBindingsConstRef B, const TypedValueRegion *R);
493   NonLoc createLazyBinding(RegionBindingsConstRef B, const TypedValueRegion *R);
494 
495   /// Used to lazily generate derived symbols for bindings that are defined
496   /// implicitly by default bindings in a super region.
497   ///
498   /// Note that callers may need to specially handle LazyCompoundVals, which
499   /// are returned as is in case the caller needs to treat them differently.
500   Optional<SVal> getBindingForDerivedDefaultValue(RegionBindingsConstRef B,
501                                                   const MemRegion *superR,
502                                                   const TypedValueRegion *R,
503                                                   QualType Ty);
504 
505   /// Get the state and region whose binding this region \p R corresponds to.
506   ///
507   /// If there is no lazy binding for \p R, the returned value will have a null
508   /// \c second. Note that a null pointer can represents a valid Store.
509   std::pair<Store, const SubRegion *>
510   findLazyBinding(RegionBindingsConstRef B, const SubRegion *R,
511                   const SubRegion *originalRegion);
512 
513   /// Returns the cached set of interesting SVals contained within a lazy
514   /// binding.
515   ///
516   /// The precise value of "interesting" is determined for the purposes of
517   /// RegionStore's internal analysis. It must always contain all regions and
518   /// symbols, but may omit constants and other kinds of SVal.
519   const SValListTy &getInterestingValues(nonloc::LazyCompoundVal LCV);
520 
521   //===------------------------------------------------------------------===//
522   // State pruning.
523   //===------------------------------------------------------------------===//
524 
525   /// removeDeadBindings - Scans the RegionStore of 'state' for dead values.
526   ///  It returns a new Store with these values removed.
527   StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx,
528                               SymbolReaper& SymReaper);
529 
530   //===------------------------------------------------------------------===//
531   // Region "extents".
532   //===------------------------------------------------------------------===//
533 
534   // FIXME: This method will soon be eliminated; see the note in Store.h.
535   DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state,
536                                          const MemRegion* R, QualType EleTy);
537 
538   //===------------------------------------------------------------------===//
539   // Utility methods.
540   //===------------------------------------------------------------------===//
541 
getRegionBindings(Store store) const542   RegionBindingsRef getRegionBindings(Store store) const {
543     return RegionBindingsRef(CBFactory,
544                              static_cast<const RegionBindings::TreeTy*>(store),
545                              RBFactory.getTreeFactory());
546   }
547 
548   void print(Store store, raw_ostream &Out, const char* nl,
549              const char *sep);
550 
iterBindings(Store store,BindingsHandler & f)551   void iterBindings(Store store, BindingsHandler& f) {
552     RegionBindingsRef B = getRegionBindings(store);
553     for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) {
554       const ClusterBindings &Cluster = I.getData();
555       for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
556            CI != CE; ++CI) {
557         const BindingKey &K = CI.getKey();
558         if (!K.isDirect())
559           continue;
560         if (const SubRegion *R = dyn_cast<SubRegion>(K.getRegion())) {
561           // FIXME: Possibly incorporate the offset?
562           if (!f.HandleBinding(*this, store, R, CI.getData()))
563             return;
564         }
565       }
566     }
567   }
568 };
569 
570 } // end anonymous namespace
571 
572 //===----------------------------------------------------------------------===//
573 // RegionStore creation.
574 //===----------------------------------------------------------------------===//
575 
CreateRegionStoreManager(ProgramStateManager & StMgr)576 StoreManager *ento::CreateRegionStoreManager(ProgramStateManager& StMgr) {
577   RegionStoreFeatures F = maximal_features_tag();
578   return new RegionStoreManager(StMgr, F);
579 }
580 
581 StoreManager *
CreateFieldsOnlyRegionStoreManager(ProgramStateManager & StMgr)582 ento::CreateFieldsOnlyRegionStoreManager(ProgramStateManager &StMgr) {
583   RegionStoreFeatures F = minimal_features_tag();
584   F.enableFields(true);
585   return new RegionStoreManager(StMgr, F);
586 }
587 
588 
589 //===----------------------------------------------------------------------===//
590 // Region Cluster analysis.
591 //===----------------------------------------------------------------------===//
592 
593 namespace {
594 template <typename DERIVED>
595 class ClusterAnalysis  {
596 protected:
597   typedef llvm::DenseMap<const MemRegion *, const ClusterBindings *> ClusterMap;
598   typedef SmallVector<const MemRegion *, 10> WorkList;
599 
600   llvm::SmallPtrSet<const ClusterBindings *, 16> Visited;
601 
602   WorkList WL;
603 
604   RegionStoreManager &RM;
605   ASTContext &Ctx;
606   SValBuilder &svalBuilder;
607 
608   RegionBindingsRef B;
609 
610   const bool includeGlobals;
611 
getCluster(const MemRegion * R)612   const ClusterBindings *getCluster(const MemRegion *R) {
613     return B.lookup(R);
614   }
615 
616 public:
ClusterAnalysis(RegionStoreManager & rm,ProgramStateManager & StateMgr,RegionBindingsRef b,const bool includeGlobals)617   ClusterAnalysis(RegionStoreManager &rm, ProgramStateManager &StateMgr,
618                   RegionBindingsRef b, const bool includeGlobals)
619     : RM(rm), Ctx(StateMgr.getContext()),
620       svalBuilder(StateMgr.getSValBuilder()),
621       B(b), includeGlobals(includeGlobals) {}
622 
getRegionBindings() const623   RegionBindingsRef getRegionBindings() const { return B; }
624 
isVisited(const MemRegion * R)625   bool isVisited(const MemRegion *R) {
626     return Visited.count(getCluster(R));
627   }
628 
GenerateClusters()629   void GenerateClusters() {
630     // Scan the entire set of bindings and record the region clusters.
631     for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end();
632          RI != RE; ++RI){
633       const MemRegion *Base = RI.getKey();
634 
635       const ClusterBindings &Cluster = RI.getData();
636       assert(!Cluster.isEmpty() && "Empty clusters should be removed");
637       static_cast<DERIVED*>(this)->VisitAddedToCluster(Base, Cluster);
638 
639       if (includeGlobals)
640         if (isa<NonStaticGlobalSpaceRegion>(Base->getMemorySpace()))
641           AddToWorkList(Base, &Cluster);
642     }
643   }
644 
AddToWorkList(const MemRegion * R,const ClusterBindings * C)645   bool AddToWorkList(const MemRegion *R, const ClusterBindings *C) {
646     if (C && !Visited.insert(C))
647       return false;
648     WL.push_back(R);
649     return true;
650   }
651 
AddToWorkList(const MemRegion * R)652   bool AddToWorkList(const MemRegion *R) {
653     const MemRegion *baseR = R->getBaseRegion();
654     return AddToWorkList(baseR, getCluster(baseR));
655   }
656 
RunWorkList()657   void RunWorkList() {
658     while (!WL.empty()) {
659       const MemRegion *baseR = WL.pop_back_val();
660 
661       // First visit the cluster.
662       if (const ClusterBindings *Cluster = getCluster(baseR))
663         static_cast<DERIVED*>(this)->VisitCluster(baseR, *Cluster);
664 
665       // Next, visit the base region.
666       static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
667     }
668   }
669 
670 public:
VisitAddedToCluster(const MemRegion * baseR,const ClusterBindings & C)671   void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C) {}
VisitCluster(const MemRegion * baseR,const ClusterBindings & C)672   void VisitCluster(const MemRegion *baseR, const ClusterBindings &C) {}
VisitBaseRegion(const MemRegion * baseR)673   void VisitBaseRegion(const MemRegion *baseR) {}
674 };
675 }
676 
677 //===----------------------------------------------------------------------===//
678 // Binding invalidation.
679 //===----------------------------------------------------------------------===//
680 
scanReachableSymbols(Store S,const MemRegion * R,ScanReachableSymbols & Callbacks)681 bool RegionStoreManager::scanReachableSymbols(Store S, const MemRegion *R,
682                                               ScanReachableSymbols &Callbacks) {
683   assert(R == R->getBaseRegion() && "Should only be called for base regions");
684   RegionBindingsRef B = getRegionBindings(S);
685   const ClusterBindings *Cluster = B.lookup(R);
686 
687   if (!Cluster)
688     return true;
689 
690   for (ClusterBindings::iterator RI = Cluster->begin(), RE = Cluster->end();
691        RI != RE; ++RI) {
692     if (!Callbacks.scan(RI.getData()))
693       return false;
694   }
695 
696   return true;
697 }
698 
isUnionField(const FieldRegion * FR)699 static inline bool isUnionField(const FieldRegion *FR) {
700   return FR->getDecl()->getParent()->isUnion();
701 }
702 
703 typedef SmallVector<const FieldDecl *, 8> FieldVector;
704 
getSymbolicOffsetFields(BindingKey K,FieldVector & Fields)705 void getSymbolicOffsetFields(BindingKey K, FieldVector &Fields) {
706   assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys");
707 
708   const MemRegion *Base = K.getConcreteOffsetRegion();
709   const MemRegion *R = K.getRegion();
710 
711   while (R != Base) {
712     if (const FieldRegion *FR = dyn_cast<FieldRegion>(R))
713       if (!isUnionField(FR))
714         Fields.push_back(FR->getDecl());
715 
716     R = cast<SubRegion>(R)->getSuperRegion();
717   }
718 }
719 
isCompatibleWithFields(BindingKey K,const FieldVector & Fields)720 static bool isCompatibleWithFields(BindingKey K, const FieldVector &Fields) {
721   assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys");
722 
723   if (Fields.empty())
724     return true;
725 
726   FieldVector FieldsInBindingKey;
727   getSymbolicOffsetFields(K, FieldsInBindingKey);
728 
729   ptrdiff_t Delta = FieldsInBindingKey.size() - Fields.size();
730   if (Delta >= 0)
731     return std::equal(FieldsInBindingKey.begin() + Delta,
732                       FieldsInBindingKey.end(),
733                       Fields.begin());
734   else
735     return std::equal(FieldsInBindingKey.begin(), FieldsInBindingKey.end(),
736                       Fields.begin() - Delta);
737 }
738 
739 /// Collects all bindings in \p Cluster that may refer to bindings within
740 /// \p Top.
741 ///
742 /// Each binding is a pair whose \c first is the key (a BindingKey) and whose
743 /// \c second is the value (an SVal).
744 ///
745 /// The \p IncludeAllDefaultBindings parameter specifies whether to include
746 /// default bindings that may extend beyond \p Top itself, e.g. if \p Top is
747 /// an aggregate within a larger aggregate with a default binding.
748 static void
collectSubRegionBindings(SmallVectorImpl<BindingPair> & Bindings,SValBuilder & SVB,const ClusterBindings & Cluster,const SubRegion * Top,BindingKey TopKey,bool IncludeAllDefaultBindings)749 collectSubRegionBindings(SmallVectorImpl<BindingPair> &Bindings,
750                          SValBuilder &SVB, const ClusterBindings &Cluster,
751                          const SubRegion *Top, BindingKey TopKey,
752                          bool IncludeAllDefaultBindings) {
753   FieldVector FieldsInSymbolicSubregions;
754   if (TopKey.hasSymbolicOffset()) {
755     getSymbolicOffsetFields(TopKey, FieldsInSymbolicSubregions);
756     Top = cast<SubRegion>(TopKey.getConcreteOffsetRegion());
757     TopKey = BindingKey::Make(Top, BindingKey::Default);
758   }
759 
760   // Find the length (in bits) of the region being invalidated.
761   uint64_t Length = UINT64_MAX;
762   SVal Extent = Top->getExtent(SVB);
763   if (Optional<nonloc::ConcreteInt> ExtentCI =
764           Extent.getAs<nonloc::ConcreteInt>()) {
765     const llvm::APSInt &ExtentInt = ExtentCI->getValue();
766     assert(ExtentInt.isNonNegative() || ExtentInt.isUnsigned());
767     // Extents are in bytes but region offsets are in bits. Be careful!
768     Length = ExtentInt.getLimitedValue() * SVB.getContext().getCharWidth();
769   } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(Top)) {
770     if (FR->getDecl()->isBitField())
771       Length = FR->getDecl()->getBitWidthValue(SVB.getContext());
772   }
773 
774   for (ClusterBindings::iterator I = Cluster.begin(), E = Cluster.end();
775        I != E; ++I) {
776     BindingKey NextKey = I.getKey();
777     if (NextKey.getRegion() == TopKey.getRegion()) {
778       // FIXME: This doesn't catch the case where we're really invalidating a
779       // region with a symbolic offset. Example:
780       //      R: points[i].y
781       //   Next: points[0].x
782 
783       if (NextKey.getOffset() > TopKey.getOffset() &&
784           NextKey.getOffset() - TopKey.getOffset() < Length) {
785         // Case 1: The next binding is inside the region we're invalidating.
786         // Include it.
787         Bindings.push_back(*I);
788 
789       } else if (NextKey.getOffset() == TopKey.getOffset()) {
790         // Case 2: The next binding is at the same offset as the region we're
791         // invalidating. In this case, we need to leave default bindings alone,
792         // since they may be providing a default value for a regions beyond what
793         // we're invalidating.
794         // FIXME: This is probably incorrect; consider invalidating an outer
795         // struct whose first field is bound to a LazyCompoundVal.
796         if (IncludeAllDefaultBindings || NextKey.isDirect())
797           Bindings.push_back(*I);
798       }
799 
800     } else if (NextKey.hasSymbolicOffset()) {
801       const MemRegion *Base = NextKey.getConcreteOffsetRegion();
802       if (Top->isSubRegionOf(Base)) {
803         // Case 3: The next key is symbolic and we just changed something within
804         // its concrete region. We don't know if the binding is still valid, so
805         // we'll be conservative and include it.
806         if (IncludeAllDefaultBindings || NextKey.isDirect())
807           if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions))
808             Bindings.push_back(*I);
809       } else if (const SubRegion *BaseSR = dyn_cast<SubRegion>(Base)) {
810         // Case 4: The next key is symbolic, but we changed a known
811         // super-region. In this case the binding is certainly included.
812         if (Top == Base || BaseSR->isSubRegionOf(Top))
813           if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions))
814             Bindings.push_back(*I);
815       }
816     }
817   }
818 }
819 
820 static void
collectSubRegionBindings(SmallVectorImpl<BindingPair> & Bindings,SValBuilder & SVB,const ClusterBindings & Cluster,const SubRegion * Top,bool IncludeAllDefaultBindings)821 collectSubRegionBindings(SmallVectorImpl<BindingPair> &Bindings,
822                          SValBuilder &SVB, const ClusterBindings &Cluster,
823                          const SubRegion *Top, bool IncludeAllDefaultBindings) {
824   collectSubRegionBindings(Bindings, SVB, Cluster, Top,
825                            BindingKey::Make(Top, BindingKey::Default),
826                            IncludeAllDefaultBindings);
827 }
828 
829 RegionBindingsRef
removeSubRegionBindings(RegionBindingsConstRef B,const SubRegion * Top)830 RegionStoreManager::removeSubRegionBindings(RegionBindingsConstRef B,
831                                             const SubRegion *Top) {
832   BindingKey TopKey = BindingKey::Make(Top, BindingKey::Default);
833   const MemRegion *ClusterHead = TopKey.getBaseRegion();
834   if (Top == ClusterHead) {
835     // We can remove an entire cluster's bindings all in one go.
836     return B.remove(Top);
837   }
838 
839   const ClusterBindings *Cluster = B.lookup(ClusterHead);
840   if (!Cluster)
841     return B;
842 
843   SmallVector<BindingPair, 32> Bindings;
844   collectSubRegionBindings(Bindings, svalBuilder, *Cluster, Top, TopKey,
845                            /*IncludeAllDefaultBindings=*/false);
846 
847   ClusterBindingsRef Result(*Cluster, CBFactory);
848   for (SmallVectorImpl<BindingPair>::const_iterator I = Bindings.begin(),
849                                                     E = Bindings.end();
850        I != E; ++I)
851     Result = Result.remove(I->first);
852 
853   // If we're invalidating a region with a symbolic offset, we need to make sure
854   // we don't treat the base region as uninitialized anymore.
855   // FIXME: This isn't very precise; see the example in the loop.
856   if (TopKey.hasSymbolicOffset()) {
857     const SubRegion *Concrete = TopKey.getConcreteOffsetRegion();
858     Result = Result.add(BindingKey::Make(Concrete, BindingKey::Default),
859                         UnknownVal());
860   }
861 
862   if (Result.isEmpty())
863     return B.remove(ClusterHead);
864   return B.add(ClusterHead, Result.asImmutableMap());
865 }
866 
867 namespace {
868 class invalidateRegionsWorker : public ClusterAnalysis<invalidateRegionsWorker>
869 {
870   const Expr *Ex;
871   unsigned Count;
872   const LocationContext *LCtx;
873   InvalidatedSymbols &IS;
874   StoreManager::InvalidatedRegions *Regions;
875 public:
invalidateRegionsWorker(RegionStoreManager & rm,ProgramStateManager & stateMgr,RegionBindingsRef b,const Expr * ex,unsigned count,const LocationContext * lctx,InvalidatedSymbols & is,StoreManager::InvalidatedRegions * r,bool includeGlobals)876   invalidateRegionsWorker(RegionStoreManager &rm,
877                           ProgramStateManager &stateMgr,
878                           RegionBindingsRef b,
879                           const Expr *ex, unsigned count,
880                           const LocationContext *lctx,
881                           InvalidatedSymbols &is,
882                           StoreManager::InvalidatedRegions *r,
883                           bool includeGlobals)
884     : ClusterAnalysis<invalidateRegionsWorker>(rm, stateMgr, b, includeGlobals),
885       Ex(ex), Count(count), LCtx(lctx), IS(is), Regions(r) {}
886 
887   void VisitCluster(const MemRegion *baseR, const ClusterBindings &C);
888   void VisitBaseRegion(const MemRegion *baseR);
889 
890 private:
891   void VisitBinding(SVal V);
892 };
893 }
894 
VisitBinding(SVal V)895 void invalidateRegionsWorker::VisitBinding(SVal V) {
896   // A symbol?  Mark it touched by the invalidation.
897   if (SymbolRef Sym = V.getAsSymbol())
898     IS.insert(Sym);
899 
900   if (const MemRegion *R = V.getAsRegion()) {
901     AddToWorkList(R);
902     return;
903   }
904 
905   // Is it a LazyCompoundVal?  All references get invalidated as well.
906   if (Optional<nonloc::LazyCompoundVal> LCS =
907           V.getAs<nonloc::LazyCompoundVal>()) {
908 
909     const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS);
910 
911     for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(),
912                                                         E = Vals.end();
913          I != E; ++I)
914       VisitBinding(*I);
915 
916     return;
917   }
918 }
919 
VisitCluster(const MemRegion * BaseR,const ClusterBindings & C)920 void invalidateRegionsWorker::VisitCluster(const MemRegion *BaseR,
921                                            const ClusterBindings &C) {
922   for (ClusterBindings::iterator I = C.begin(), E = C.end(); I != E; ++I)
923     VisitBinding(I.getData());
924 
925   B = B.remove(BaseR);
926 }
927 
VisitBaseRegion(const MemRegion * baseR)928 void invalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) {
929   // Symbolic region?  Mark that symbol touched by the invalidation.
930   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
931     IS.insert(SR->getSymbol());
932 
933   // BlockDataRegion?  If so, invalidate captured variables that are passed
934   // by reference.
935   if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
936     for (BlockDataRegion::referenced_vars_iterator
937          BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
938          BI != BE; ++BI) {
939       const VarRegion *VR = BI.getCapturedRegion();
940       const VarDecl *VD = VR->getDecl();
941       if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage()) {
942         AddToWorkList(VR);
943       }
944       else if (Loc::isLocType(VR->getValueType())) {
945         // Map the current bindings to a Store to retrieve the value
946         // of the binding.  If that binding itself is a region, we should
947         // invalidate that region.  This is because a block may capture
948         // a pointer value, but the thing pointed by that pointer may
949         // get invalidated.
950         SVal V = RM.getBinding(B, loc::MemRegionVal(VR));
951         if (Optional<Loc> L = V.getAs<Loc>()) {
952           if (const MemRegion *LR = L->getAsRegion())
953             AddToWorkList(LR);
954         }
955       }
956     }
957     return;
958   }
959 
960   // Otherwise, we have a normal data region. Record that we touched the region.
961   if (Regions)
962     Regions->push_back(baseR);
963 
964   if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
965     // Invalidate the region by setting its default value to
966     // conjured symbol. The type of the symbol is irrelavant.
967     DefinedOrUnknownSVal V =
968       svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, Ctx.IntTy, Count);
969     B = B.addBinding(baseR, BindingKey::Default, V);
970     return;
971   }
972 
973   if (!baseR->isBoundable())
974     return;
975 
976   const TypedValueRegion *TR = cast<TypedValueRegion>(baseR);
977   QualType T = TR->getValueType();
978 
979     // Invalidate the binding.
980   if (T->isStructureOrClassType()) {
981     // Invalidate the region by setting its default value to
982     // conjured symbol. The type of the symbol is irrelavant.
983     DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
984                                                           Ctx.IntTy, Count);
985     B = B.addBinding(baseR, BindingKey::Default, V);
986     return;
987   }
988 
989   if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
990       // Set the default value of the array to conjured symbol.
991     DefinedOrUnknownSVal V =
992     svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
993                                      AT->getElementType(), Count);
994     B = B.addBinding(baseR, BindingKey::Default, V);
995     return;
996   }
997 
998   if (includeGlobals &&
999       isa<NonStaticGlobalSpaceRegion>(baseR->getMemorySpace())) {
1000     // If the region is a global and we are invalidating all globals,
1001     // just erase the entry.  This causes all globals to be lazily
1002     // symbolicated from the same base symbol.
1003     B = B.removeBinding(baseR);
1004     return;
1005   }
1006 
1007 
1008   DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
1009                                                         T,Count);
1010   assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
1011   B = B.addBinding(baseR, BindingKey::Direct, V);
1012 }
1013 
1014 RegionBindingsRef
invalidateGlobalRegion(MemRegion::Kind K,const Expr * Ex,unsigned Count,const LocationContext * LCtx,RegionBindingsRef B,InvalidatedRegions * Invalidated)1015 RegionStoreManager::invalidateGlobalRegion(MemRegion::Kind K,
1016                                            const Expr *Ex,
1017                                            unsigned Count,
1018                                            const LocationContext *LCtx,
1019                                            RegionBindingsRef B,
1020                                            InvalidatedRegions *Invalidated) {
1021   // Bind the globals memory space to a new symbol that we will use to derive
1022   // the bindings for all globals.
1023   const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion(K);
1024   SVal V = svalBuilder.conjureSymbolVal(/* SymbolTag = */ (const void*) GS, Ex, LCtx,
1025                                         /* type does not matter */ Ctx.IntTy,
1026                                         Count);
1027 
1028   B = B.removeBinding(GS)
1029        .addBinding(BindingKey::Make(GS, BindingKey::Default), V);
1030 
1031   // Even if there are no bindings in the global scope, we still need to
1032   // record that we touched it.
1033   if (Invalidated)
1034     Invalidated->push_back(GS);
1035 
1036   return B;
1037 }
1038 
1039 StoreRef
invalidateRegions(Store store,ArrayRef<const MemRegion * > Regions,const Expr * Ex,unsigned Count,const LocationContext * LCtx,InvalidatedSymbols & IS,const CallEvent * Call,InvalidatedRegions * Invalidated)1040 RegionStoreManager::invalidateRegions(Store store,
1041                                       ArrayRef<const MemRegion *> Regions,
1042                                       const Expr *Ex, unsigned Count,
1043                                       const LocationContext *LCtx,
1044                                       InvalidatedSymbols &IS,
1045                                       const CallEvent *Call,
1046                                       InvalidatedRegions *Invalidated) {
1047   invalidateRegionsWorker W(*this, StateMgr,
1048                             RegionStoreManager::getRegionBindings(store),
1049                             Ex, Count, LCtx, IS, Invalidated, false);
1050 
1051   // Scan the bindings and generate the clusters.
1052   W.GenerateClusters();
1053 
1054   // Add the regions to the worklist.
1055   for (ArrayRef<const MemRegion *>::iterator
1056        I = Regions.begin(), E = Regions.end(); I != E; ++I)
1057     W.AddToWorkList(*I);
1058 
1059   W.RunWorkList();
1060 
1061   // Return the new bindings.
1062   RegionBindingsRef B = W.getRegionBindings();
1063 
1064   // For all globals which are not static nor immutable: determine which global
1065   // regions should be invalidated and invalidate them.
1066   // TODO: This could possibly be more precise with modules.
1067   //
1068   // System calls invalidate only system globals.
1069   if (Call && Call->isInSystemHeader()) {
1070     B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind,
1071                                Ex, Count, LCtx, B, Invalidated);
1072   // Internal calls might invalidate both system and internal globals.
1073   } else {
1074     B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind,
1075                                Ex, Count, LCtx, B, Invalidated);
1076     B = invalidateGlobalRegion(MemRegion::GlobalInternalSpaceRegionKind,
1077                                Ex, Count, LCtx, B, Invalidated);
1078   }
1079 
1080   return StoreRef(B.asStore(), *this);
1081 }
1082 
1083 //===----------------------------------------------------------------------===//
1084 // Extents for regions.
1085 //===----------------------------------------------------------------------===//
1086 
1087 DefinedOrUnknownSVal
getSizeInElements(ProgramStateRef state,const MemRegion * R,QualType EleTy)1088 RegionStoreManager::getSizeInElements(ProgramStateRef state,
1089                                       const MemRegion *R,
1090                                       QualType EleTy) {
1091   SVal Size = cast<SubRegion>(R)->getExtent(svalBuilder);
1092   const llvm::APSInt *SizeInt = svalBuilder.getKnownValue(state, Size);
1093   if (!SizeInt)
1094     return UnknownVal();
1095 
1096   CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue());
1097 
1098   if (Ctx.getAsVariableArrayType(EleTy)) {
1099     // FIXME: We need to track extra state to properly record the size
1100     // of VLAs.  Returning UnknownVal here, however, is a stop-gap so that
1101     // we don't have a divide-by-zero below.
1102     return UnknownVal();
1103   }
1104 
1105   CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
1106 
1107   // If a variable is reinterpreted as a type that doesn't fit into a larger
1108   // type evenly, round it down.
1109   // This is a signed value, since it's used in arithmetic with signed indices.
1110   return svalBuilder.makeIntVal(RegionSize / EleSize, false);
1111 }
1112 
1113 //===----------------------------------------------------------------------===//
1114 // Location and region casting.
1115 //===----------------------------------------------------------------------===//
1116 
1117 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
1118 ///  type.  'Array' represents the lvalue of the array being decayed
1119 ///  to a pointer, and the returned SVal represents the decayed
1120 ///  version of that lvalue (i.e., a pointer to the first element of
1121 ///  the array).  This is called by ExprEngine when evaluating casts
1122 ///  from arrays to pointers.
ArrayToPointer(Loc Array)1123 SVal RegionStoreManager::ArrayToPointer(Loc Array) {
1124   if (!Array.getAs<loc::MemRegionVal>())
1125     return UnknownVal();
1126 
1127   const MemRegion* R = Array.castAs<loc::MemRegionVal>().getRegion();
1128   const TypedValueRegion* ArrayR = dyn_cast<TypedValueRegion>(R);
1129 
1130   if (!ArrayR)
1131     return UnknownVal();
1132 
1133   // Strip off typedefs from the ArrayRegion's ValueType.
1134   QualType T = ArrayR->getValueType().getDesugaredType(Ctx);
1135   const ArrayType *AT = cast<ArrayType>(T);
1136   T = AT->getElementType();
1137 
1138   NonLoc ZeroIdx = svalBuilder.makeZeroArrayIndex();
1139   return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx));
1140 }
1141 
1142 //===----------------------------------------------------------------------===//
1143 // Loading values from regions.
1144 //===----------------------------------------------------------------------===//
1145 
getBinding(RegionBindingsConstRef B,Loc L,QualType T)1146 SVal RegionStoreManager::getBinding(RegionBindingsConstRef B, Loc L, QualType T) {
1147   assert(!L.getAs<UnknownVal>() && "location unknown");
1148   assert(!L.getAs<UndefinedVal>() && "location undefined");
1149 
1150   // For access to concrete addresses, return UnknownVal.  Checks
1151   // for null dereferences (and similar errors) are done by checkers, not
1152   // the Store.
1153   // FIXME: We can consider lazily symbolicating such memory, but we really
1154   // should defer this when we can reason easily about symbolicating arrays
1155   // of bytes.
1156   if (L.getAs<loc::ConcreteInt>()) {
1157     return UnknownVal();
1158   }
1159   if (!L.getAs<loc::MemRegionVal>()) {
1160     return UnknownVal();
1161   }
1162 
1163   const MemRegion *MR = L.castAs<loc::MemRegionVal>().getRegion();
1164 
1165   if (isa<AllocaRegion>(MR) ||
1166       isa<SymbolicRegion>(MR) ||
1167       isa<CodeTextRegion>(MR)) {
1168     if (T.isNull()) {
1169       if (const TypedRegion *TR = dyn_cast<TypedRegion>(MR))
1170         T = TR->getLocationType();
1171       else {
1172         const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
1173         T = SR->getSymbol()->getType();
1174       }
1175     }
1176     MR = GetElementZeroRegion(MR, T);
1177   }
1178 
1179   // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1180   //  instead of 'Loc', and have the other Loc cases handled at a higher level.
1181   const TypedValueRegion *R = cast<TypedValueRegion>(MR);
1182   QualType RTy = R->getValueType();
1183 
1184   // FIXME: we do not yet model the parts of a complex type, so treat the
1185   // whole thing as "unknown".
1186   if (RTy->isAnyComplexType())
1187     return UnknownVal();
1188 
1189   // FIXME: We should eventually handle funny addressing.  e.g.:
1190   //
1191   //   int x = ...;
1192   //   int *p = &x;
1193   //   char *q = (char*) p;
1194   //   char c = *q;  // returns the first byte of 'x'.
1195   //
1196   // Such funny addressing will occur due to layering of regions.
1197   if (RTy->isStructureOrClassType())
1198     return getBindingForStruct(B, R);
1199 
1200   // FIXME: Handle unions.
1201   if (RTy->isUnionType())
1202     return UnknownVal();
1203 
1204   if (RTy->isArrayType()) {
1205     if (RTy->isConstantArrayType())
1206       return getBindingForArray(B, R);
1207     else
1208       return UnknownVal();
1209   }
1210 
1211   // FIXME: handle Vector types.
1212   if (RTy->isVectorType())
1213     return UnknownVal();
1214 
1215   if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
1216     return CastRetrievedVal(getBindingForField(B, FR), FR, T, false);
1217 
1218   if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1219     // FIXME: Here we actually perform an implicit conversion from the loaded
1220     // value to the element type.  Eventually we want to compose these values
1221     // more intelligently.  For example, an 'element' can encompass multiple
1222     // bound regions (e.g., several bound bytes), or could be a subset of
1223     // a larger value.
1224     return CastRetrievedVal(getBindingForElement(B, ER), ER, T, false);
1225   }
1226 
1227   if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1228     // FIXME: Here we actually perform an implicit conversion from the loaded
1229     // value to the ivar type.  What we should model is stores to ivars
1230     // that blow past the extent of the ivar.  If the address of the ivar is
1231     // reinterpretted, it is possible we stored a different value that could
1232     // fit within the ivar.  Either we need to cast these when storing them
1233     // or reinterpret them lazily (as we do here).
1234     return CastRetrievedVal(getBindingForObjCIvar(B, IVR), IVR, T, false);
1235   }
1236 
1237   if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1238     // FIXME: Here we actually perform an implicit conversion from the loaded
1239     // value to the variable type.  What we should model is stores to variables
1240     // that blow past the extent of the variable.  If the address of the
1241     // variable is reinterpretted, it is possible we stored a different value
1242     // that could fit within the variable.  Either we need to cast these when
1243     // storing them or reinterpret them lazily (as we do here).
1244     return CastRetrievedVal(getBindingForVar(B, VR), VR, T, false);
1245   }
1246 
1247   const SVal *V = B.lookup(R, BindingKey::Direct);
1248 
1249   // Check if the region has a binding.
1250   if (V)
1251     return *V;
1252 
1253   // The location does not have a bound value.  This means that it has
1254   // the value it had upon its creation and/or entry to the analyzed
1255   // function/method.  These are either symbolic values or 'undefined'.
1256   if (R->hasStackNonParametersStorage()) {
1257     // All stack variables are considered to have undefined values
1258     // upon creation.  All heap allocated blocks are considered to
1259     // have undefined values as well unless they are explicitly bound
1260     // to specific values.
1261     return UndefinedVal();
1262   }
1263 
1264   // All other values are symbolic.
1265   return svalBuilder.getRegionValueSymbolVal(R);
1266 }
1267 
1268 /// Checks to see if store \p B has a lazy binding for region \p R.
1269 ///
1270 /// If \p AllowSubregionBindings is \c false, a lazy binding will be rejected
1271 /// if there are additional bindings within \p R.
1272 ///
1273 /// Note that unlike RegionStoreManager::findLazyBinding, this will not search
1274 /// for lazy bindings for super-regions of \p R.
1275 static Optional<nonloc::LazyCompoundVal>
getExistingLazyBinding(SValBuilder & SVB,RegionBindingsConstRef B,const SubRegion * R,bool AllowSubregionBindings)1276 getExistingLazyBinding(SValBuilder &SVB, RegionBindingsConstRef B,
1277                        const SubRegion *R, bool AllowSubregionBindings) {
1278   Optional<SVal> V = B.getDefaultBinding(R);
1279   if (!V)
1280     return None;
1281 
1282   Optional<nonloc::LazyCompoundVal> LCV = V->getAs<nonloc::LazyCompoundVal>();
1283   if (!LCV)
1284     return None;
1285 
1286   // If the LCV is for a subregion, the types won't match, and we shouldn't
1287   // reuse the binding. Unfortuately we can only check this if the destination
1288   // region is a TypedValueRegion.
1289   if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(R)) {
1290     QualType RegionTy = TVR->getValueType();
1291     QualType SourceRegionTy = LCV->getRegion()->getValueType();
1292     if (!SVB.getContext().hasSameUnqualifiedType(RegionTy, SourceRegionTy))
1293       return None;
1294   }
1295 
1296   if (!AllowSubregionBindings) {
1297     // If there are any other bindings within this region, we shouldn't reuse
1298     // the top-level binding.
1299     SmallVector<BindingPair, 16> Bindings;
1300     collectSubRegionBindings(Bindings, SVB, *B.lookup(R->getBaseRegion()), R,
1301                              /*IncludeAllDefaultBindings=*/true);
1302     if (Bindings.size() > 1)
1303       return None;
1304   }
1305 
1306   return *LCV;
1307 }
1308 
1309 
1310 std::pair<Store, const SubRegion *>
findLazyBinding(RegionBindingsConstRef B,const SubRegion * R,const SubRegion * originalRegion)1311 RegionStoreManager::findLazyBinding(RegionBindingsConstRef B,
1312                                    const SubRegion *R,
1313                                    const SubRegion *originalRegion) {
1314   if (originalRegion != R) {
1315     if (Optional<nonloc::LazyCompoundVal> V =
1316           getExistingLazyBinding(svalBuilder, B, R, true))
1317       return std::make_pair(V->getStore(), V->getRegion());
1318   }
1319 
1320   typedef std::pair<Store, const SubRegion *> StoreRegionPair;
1321   StoreRegionPair Result = StoreRegionPair();
1322 
1323   if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1324     Result = findLazyBinding(B, cast<SubRegion>(ER->getSuperRegion()),
1325                              originalRegion);
1326 
1327     if (Result.second)
1328       Result.second = MRMgr.getElementRegionWithSuper(ER, Result.second);
1329 
1330   } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1331     Result = findLazyBinding(B, cast<SubRegion>(FR->getSuperRegion()),
1332                                        originalRegion);
1333 
1334     if (Result.second)
1335       Result.second = MRMgr.getFieldRegionWithSuper(FR, Result.second);
1336 
1337   } else if (const CXXBaseObjectRegion *BaseReg =
1338                dyn_cast<CXXBaseObjectRegion>(R)) {
1339     // C++ base object region is another kind of region that we should blast
1340     // through to look for lazy compound value. It is like a field region.
1341     Result = findLazyBinding(B, cast<SubRegion>(BaseReg->getSuperRegion()),
1342                              originalRegion);
1343 
1344     if (Result.second)
1345       Result.second = MRMgr.getCXXBaseObjectRegionWithSuper(BaseReg,
1346                                                             Result.second);
1347   }
1348 
1349   return Result;
1350 }
1351 
getBindingForElement(RegionBindingsConstRef B,const ElementRegion * R)1352 SVal RegionStoreManager::getBindingForElement(RegionBindingsConstRef B,
1353                                               const ElementRegion* R) {
1354   // We do not currently model bindings of the CompoundLiteralregion.
1355   if (isa<CompoundLiteralRegion>(R->getBaseRegion()))
1356     return UnknownVal();
1357 
1358   // Check if the region has a binding.
1359   if (const Optional<SVal> &V = B.getDirectBinding(R))
1360     return *V;
1361 
1362   const MemRegion* superR = R->getSuperRegion();
1363 
1364   // Check if the region is an element region of a string literal.
1365   if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
1366     // FIXME: Handle loads from strings where the literal is treated as
1367     // an integer, e.g., *((unsigned int*)"hello")
1368     QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
1369     if (T != Ctx.getCanonicalType(R->getElementType()))
1370       return UnknownVal();
1371 
1372     const StringLiteral *Str = StrR->getStringLiteral();
1373     SVal Idx = R->getIndex();
1374     if (Optional<nonloc::ConcreteInt> CI = Idx.getAs<nonloc::ConcreteInt>()) {
1375       int64_t i = CI->getValue().getSExtValue();
1376       // Abort on string underrun.  This can be possible by arbitrary
1377       // clients of getBindingForElement().
1378       if (i < 0)
1379         return UndefinedVal();
1380       int64_t length = Str->getLength();
1381       // Technically, only i == length is guaranteed to be null.
1382       // However, such overflows should be caught before reaching this point;
1383       // the only time such an access would be made is if a string literal was
1384       // used to initialize a larger array.
1385       char c = (i >= length) ? '\0' : Str->getCodeUnit(i);
1386       return svalBuilder.makeIntVal(c, T);
1387     }
1388   }
1389 
1390   // Check for loads from a code text region.  For such loads, just give up.
1391   if (isa<CodeTextRegion>(superR))
1392     return UnknownVal();
1393 
1394   // Handle the case where we are indexing into a larger scalar object.
1395   // For example, this handles:
1396   //   int x = ...
1397   //   char *y = &x;
1398   //   return *y;
1399   // FIXME: This is a hack, and doesn't do anything really intelligent yet.
1400   const RegionRawOffset &O = R->getAsArrayOffset();
1401 
1402   // If we cannot reason about the offset, return an unknown value.
1403   if (!O.getRegion())
1404     return UnknownVal();
1405 
1406   if (const TypedValueRegion *baseR =
1407         dyn_cast_or_null<TypedValueRegion>(O.getRegion())) {
1408     QualType baseT = baseR->getValueType();
1409     if (baseT->isScalarType()) {
1410       QualType elemT = R->getElementType();
1411       if (elemT->isScalarType()) {
1412         if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1413           if (const Optional<SVal> &V = B.getDirectBinding(superR)) {
1414             if (SymbolRef parentSym = V->getAsSymbol())
1415               return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1416 
1417             if (V->isUnknownOrUndef())
1418               return *V;
1419             // Other cases: give up.  We are indexing into a larger object
1420             // that has some value, but we don't know how to handle that yet.
1421             return UnknownVal();
1422           }
1423         }
1424       }
1425     }
1426   }
1427   return getBindingForFieldOrElementCommon(B, R, R->getElementType(),
1428                                            superR);
1429 }
1430 
getBindingForField(RegionBindingsConstRef B,const FieldRegion * R)1431 SVal RegionStoreManager::getBindingForField(RegionBindingsConstRef B,
1432                                             const FieldRegion* R) {
1433 
1434   // Check if the region has a binding.
1435   if (const Optional<SVal> &V = B.getDirectBinding(R))
1436     return *V;
1437 
1438   QualType Ty = R->getValueType();
1439   return getBindingForFieldOrElementCommon(B, R, Ty, R->getSuperRegion());
1440 }
1441 
1442 Optional<SVal>
getBindingForDerivedDefaultValue(RegionBindingsConstRef B,const MemRegion * superR,const TypedValueRegion * R,QualType Ty)1443 RegionStoreManager::getBindingForDerivedDefaultValue(RegionBindingsConstRef B,
1444                                                      const MemRegion *superR,
1445                                                      const TypedValueRegion *R,
1446                                                      QualType Ty) {
1447 
1448   if (const Optional<SVal> &D = B.getDefaultBinding(superR)) {
1449     const SVal &val = D.getValue();
1450     if (SymbolRef parentSym = val.getAsSymbol())
1451       return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1452 
1453     if (val.isZeroConstant())
1454       return svalBuilder.makeZeroVal(Ty);
1455 
1456     if (val.isUnknownOrUndef())
1457       return val;
1458 
1459     // Lazy bindings are usually handled through getExistingLazyBinding().
1460     // We should unify these two code paths at some point.
1461     if (val.getAs<nonloc::LazyCompoundVal>())
1462       return val;
1463 
1464     llvm_unreachable("Unknown default value");
1465   }
1466 
1467   return None;
1468 }
1469 
getLazyBinding(const SubRegion * LazyBindingRegion,RegionBindingsRef LazyBinding)1470 SVal RegionStoreManager::getLazyBinding(const SubRegion *LazyBindingRegion,
1471                                         RegionBindingsRef LazyBinding) {
1472   SVal Result;
1473   if (const ElementRegion *ER = dyn_cast<ElementRegion>(LazyBindingRegion))
1474     Result = getBindingForElement(LazyBinding, ER);
1475   else
1476     Result = getBindingForField(LazyBinding,
1477                                 cast<FieldRegion>(LazyBindingRegion));
1478 
1479   // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
1480   // default value for /part/ of an aggregate from a default value for the
1481   // /entire/ aggregate. The most common case of this is when struct Outer
1482   // has as its first member a struct Inner, which is copied in from a stack
1483   // variable. In this case, even if the Outer's default value is symbolic, 0,
1484   // or unknown, it gets overridden by the Inner's default value of undefined.
1485   //
1486   // This is a general problem -- if the Inner is zero-initialized, the Outer
1487   // will now look zero-initialized. The proper way to solve this is with a
1488   // new version of RegionStore that tracks the extent of a binding as well
1489   // as the offset.
1490   //
1491   // This hack only takes care of the undefined case because that can very
1492   // quickly result in a warning.
1493   if (Result.isUndef())
1494     Result = UnknownVal();
1495 
1496   return Result;
1497 }
1498 
1499 SVal
getBindingForFieldOrElementCommon(RegionBindingsConstRef B,const TypedValueRegion * R,QualType Ty,const MemRegion * superR)1500 RegionStoreManager::getBindingForFieldOrElementCommon(RegionBindingsConstRef B,
1501                                                       const TypedValueRegion *R,
1502                                                       QualType Ty,
1503                                                       const MemRegion *superR) {
1504 
1505   // At this point we have already checked in either getBindingForElement or
1506   // getBindingForField if 'R' has a direct binding.
1507 
1508   // Lazy binding?
1509   Store lazyBindingStore = NULL;
1510   const SubRegion *lazyBindingRegion = NULL;
1511   llvm::tie(lazyBindingStore, lazyBindingRegion) = findLazyBinding(B, R, R);
1512   if (lazyBindingRegion)
1513     return getLazyBinding(lazyBindingRegion,
1514                           getRegionBindings(lazyBindingStore));
1515 
1516   // Record whether or not we see a symbolic index.  That can completely
1517   // be out of scope of our lookup.
1518   bool hasSymbolicIndex = false;
1519 
1520   // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
1521   // default value for /part/ of an aggregate from a default value for the
1522   // /entire/ aggregate. The most common case of this is when struct Outer
1523   // has as its first member a struct Inner, which is copied in from a stack
1524   // variable. In this case, even if the Outer's default value is symbolic, 0,
1525   // or unknown, it gets overridden by the Inner's default value of undefined.
1526   //
1527   // This is a general problem -- if the Inner is zero-initialized, the Outer
1528   // will now look zero-initialized. The proper way to solve this is with a
1529   // new version of RegionStore that tracks the extent of a binding as well
1530   // as the offset.
1531   //
1532   // This hack only takes care of the undefined case because that can very
1533   // quickly result in a warning.
1534   bool hasPartialLazyBinding = false;
1535 
1536   const SubRegion *Base = dyn_cast<SubRegion>(superR);
1537   while (Base) {
1538     if (Optional<SVal> D = getBindingForDerivedDefaultValue(B, Base, R, Ty)) {
1539       if (D->getAs<nonloc::LazyCompoundVal>()) {
1540         hasPartialLazyBinding = true;
1541         break;
1542       }
1543 
1544       return *D;
1545     }
1546 
1547     if (const ElementRegion *ER = dyn_cast<ElementRegion>(Base)) {
1548       NonLoc index = ER->getIndex();
1549       if (!index.isConstant())
1550         hasSymbolicIndex = true;
1551     }
1552 
1553     // If our super region is a field or element itself, walk up the region
1554     // hierarchy to see if there is a default value installed in an ancestor.
1555     Base = dyn_cast<SubRegion>(Base->getSuperRegion());
1556   }
1557 
1558   if (R->hasStackNonParametersStorage()) {
1559     if (isa<ElementRegion>(R)) {
1560       // Currently we don't reason specially about Clang-style vectors.  Check
1561       // if superR is a vector and if so return Unknown.
1562       if (const TypedValueRegion *typedSuperR =
1563             dyn_cast<TypedValueRegion>(superR)) {
1564         if (typedSuperR->getValueType()->isVectorType())
1565           return UnknownVal();
1566       }
1567     }
1568 
1569     // FIXME: We also need to take ElementRegions with symbolic indexes into
1570     // account.  This case handles both directly accessing an ElementRegion
1571     // with a symbolic offset, but also fields within an element with
1572     // a symbolic offset.
1573     if (hasSymbolicIndex)
1574       return UnknownVal();
1575 
1576     if (!hasPartialLazyBinding)
1577       return UndefinedVal();
1578   }
1579 
1580   // All other values are symbolic.
1581   return svalBuilder.getRegionValueSymbolVal(R);
1582 }
1583 
getBindingForObjCIvar(RegionBindingsConstRef B,const ObjCIvarRegion * R)1584 SVal RegionStoreManager::getBindingForObjCIvar(RegionBindingsConstRef B,
1585                                                const ObjCIvarRegion* R) {
1586   // Check if the region has a binding.
1587   if (const Optional<SVal> &V = B.getDirectBinding(R))
1588     return *V;
1589 
1590   const MemRegion *superR = R->getSuperRegion();
1591 
1592   // Check if the super region has a default binding.
1593   if (const Optional<SVal> &V = B.getDefaultBinding(superR)) {
1594     if (SymbolRef parentSym = V->getAsSymbol())
1595       return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1596 
1597     // Other cases: give up.
1598     return UnknownVal();
1599   }
1600 
1601   return getBindingForLazySymbol(R);
1602 }
1603 
getConstValue(SValBuilder & SVB,const VarDecl * VD)1604 static Optional<SVal> getConstValue(SValBuilder &SVB, const VarDecl *VD) {
1605   ASTContext &Ctx = SVB.getContext();
1606   if (!VD->getType().isConstQualified())
1607     return None;
1608 
1609   const Expr *Init = VD->getInit();
1610   if (!Init)
1611     return None;
1612 
1613   llvm::APSInt Result;
1614   if (!Init->isGLValue() && Init->EvaluateAsInt(Result, Ctx))
1615     return SVB.makeIntVal(Result);
1616 
1617   if (Init->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))
1618     return SVB.makeNull();
1619 
1620   // FIXME: Handle other possible constant expressions.
1621   return None;
1622 }
1623 
getBindingForVar(RegionBindingsConstRef B,const VarRegion * R)1624 SVal RegionStoreManager::getBindingForVar(RegionBindingsConstRef B,
1625                                           const VarRegion *R) {
1626 
1627   // Check if the region has a binding.
1628   if (const Optional<SVal> &V = B.getDirectBinding(R))
1629     return *V;
1630 
1631   // Lazily derive a value for the VarRegion.
1632   const VarDecl *VD = R->getDecl();
1633   const MemSpaceRegion *MS = R->getMemorySpace();
1634 
1635   // Arguments are always symbolic.
1636   if (isa<StackArgumentsSpaceRegion>(MS))
1637     return svalBuilder.getRegionValueSymbolVal(R);
1638 
1639   // Is 'VD' declared constant?  If so, retrieve the constant value.
1640   if (Optional<SVal> V = getConstValue(svalBuilder, VD))
1641     return *V;
1642 
1643   // This must come after the check for constants because closure-captured
1644   // constant variables may appear in UnknownSpaceRegion.
1645   if (isa<UnknownSpaceRegion>(MS))
1646     return svalBuilder.getRegionValueSymbolVal(R);
1647 
1648   if (isa<GlobalsSpaceRegion>(MS)) {
1649     QualType T = VD->getType();
1650 
1651     // Function-scoped static variables are default-initialized to 0; if they
1652     // have an initializer, it would have been processed by now.
1653     if (isa<StaticGlobalSpaceRegion>(MS))
1654       return svalBuilder.makeZeroVal(T);
1655 
1656     if (Optional<SVal> V = getBindingForDerivedDefaultValue(B, MS, R, T)) {
1657       assert(!V->getAs<nonloc::LazyCompoundVal>());
1658       return V.getValue();
1659     }
1660 
1661     return svalBuilder.getRegionValueSymbolVal(R);
1662   }
1663 
1664   return UndefinedVal();
1665 }
1666 
getBindingForLazySymbol(const TypedValueRegion * R)1667 SVal RegionStoreManager::getBindingForLazySymbol(const TypedValueRegion *R) {
1668   // All other values are symbolic.
1669   return svalBuilder.getRegionValueSymbolVal(R);
1670 }
1671 
1672 const RegionStoreManager::SValListTy &
getInterestingValues(nonloc::LazyCompoundVal LCV)1673 RegionStoreManager::getInterestingValues(nonloc::LazyCompoundVal LCV) {
1674   // First, check the cache.
1675   LazyBindingsMapTy::iterator I = LazyBindingsMap.find(LCV.getCVData());
1676   if (I != LazyBindingsMap.end())
1677     return I->second;
1678 
1679   // If we don't have a list of values cached, start constructing it.
1680   SValListTy List;
1681 
1682   const SubRegion *LazyR = LCV.getRegion();
1683   RegionBindingsRef B = getRegionBindings(LCV.getStore());
1684 
1685   // If this region had /no/ bindings at the time, there are no interesting
1686   // values to return.
1687   const ClusterBindings *Cluster = B.lookup(LazyR->getBaseRegion());
1688   if (!Cluster)
1689     return (LazyBindingsMap[LCV.getCVData()] = llvm_move(List));
1690 
1691   SmallVector<BindingPair, 32> Bindings;
1692   collectSubRegionBindings(Bindings, svalBuilder, *Cluster, LazyR,
1693                            /*IncludeAllDefaultBindings=*/true);
1694   for (SmallVectorImpl<BindingPair>::const_iterator I = Bindings.begin(),
1695                                                     E = Bindings.end();
1696        I != E; ++I) {
1697     SVal V = I->second;
1698     if (V.isUnknownOrUndef() || V.isConstant())
1699       continue;
1700 
1701     if (Optional<nonloc::LazyCompoundVal> InnerLCV =
1702             V.getAs<nonloc::LazyCompoundVal>()) {
1703       const SValListTy &InnerList = getInterestingValues(*InnerLCV);
1704       List.insert(List.end(), InnerList.begin(), InnerList.end());
1705       continue;
1706     }
1707 
1708     List.push_back(V);
1709   }
1710 
1711   return (LazyBindingsMap[LCV.getCVData()] = llvm_move(List));
1712 }
1713 
createLazyBinding(RegionBindingsConstRef B,const TypedValueRegion * R)1714 NonLoc RegionStoreManager::createLazyBinding(RegionBindingsConstRef B,
1715                                              const TypedValueRegion *R) {
1716   if (Optional<nonloc::LazyCompoundVal> V =
1717         getExistingLazyBinding(svalBuilder, B, R, false))
1718     return *V;
1719 
1720   return svalBuilder.makeLazyCompoundVal(StoreRef(B.asStore(), *this), R);
1721 }
1722 
getBindingForStruct(RegionBindingsConstRef B,const TypedValueRegion * R)1723 SVal RegionStoreManager::getBindingForStruct(RegionBindingsConstRef B,
1724                                              const TypedValueRegion *R) {
1725   const RecordDecl *RD = R->getValueType()->castAs<RecordType>()->getDecl();
1726   if (RD->field_empty())
1727     return UnknownVal();
1728 
1729   return createLazyBinding(B, R);
1730 }
1731 
getBindingForArray(RegionBindingsConstRef B,const TypedValueRegion * R)1732 SVal RegionStoreManager::getBindingForArray(RegionBindingsConstRef B,
1733                                             const TypedValueRegion *R) {
1734   assert(Ctx.getAsConstantArrayType(R->getValueType()) &&
1735          "Only constant array types can have compound bindings.");
1736 
1737   return createLazyBinding(B, R);
1738 }
1739 
includedInBindings(Store store,const MemRegion * region) const1740 bool RegionStoreManager::includedInBindings(Store store,
1741                                             const MemRegion *region) const {
1742   RegionBindingsRef B = getRegionBindings(store);
1743   region = region->getBaseRegion();
1744 
1745   // Quick path: if the base is the head of a cluster, the region is live.
1746   if (B.lookup(region))
1747     return true;
1748 
1749   // Slow path: if the region is the VALUE of any binding, it is live.
1750   for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
1751     const ClusterBindings &Cluster = RI.getData();
1752     for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
1753          CI != CE; ++CI) {
1754       const SVal &D = CI.getData();
1755       if (const MemRegion *R = D.getAsRegion())
1756         if (R->getBaseRegion() == region)
1757           return true;
1758     }
1759   }
1760 
1761   return false;
1762 }
1763 
1764 //===----------------------------------------------------------------------===//
1765 // Binding values to regions.
1766 //===----------------------------------------------------------------------===//
1767 
killBinding(Store ST,Loc L)1768 StoreRef RegionStoreManager::killBinding(Store ST, Loc L) {
1769   if (Optional<loc::MemRegionVal> LV = L.getAs<loc::MemRegionVal>())
1770     if (const MemRegion* R = LV->getRegion())
1771       return StoreRef(getRegionBindings(ST).removeBinding(R)
1772                                            .asImmutableMap()
1773                                            .getRootWithoutRetain(),
1774                       *this);
1775 
1776   return StoreRef(ST, *this);
1777 }
1778 
1779 RegionBindingsRef
bind(RegionBindingsConstRef B,Loc L,SVal V)1780 RegionStoreManager::bind(RegionBindingsConstRef B, Loc L, SVal V) {
1781   if (L.getAs<loc::ConcreteInt>())
1782     return B;
1783 
1784   // If we get here, the location should be a region.
1785   const MemRegion *R = L.castAs<loc::MemRegionVal>().getRegion();
1786 
1787   // Check if the region is a struct region.
1788   if (const TypedValueRegion* TR = dyn_cast<TypedValueRegion>(R)) {
1789     QualType Ty = TR->getValueType();
1790     if (Ty->isArrayType())
1791       return bindArray(B, TR, V);
1792     if (Ty->isStructureOrClassType())
1793       return bindStruct(B, TR, V);
1794     if (Ty->isVectorType())
1795       return bindVector(B, TR, V);
1796   }
1797 
1798   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1799     // Binding directly to a symbolic region should be treated as binding
1800     // to element 0.
1801     QualType T = SR->getSymbol()->getType();
1802     if (T->isAnyPointerType() || T->isReferenceType())
1803       T = T->getPointeeType();
1804 
1805     R = GetElementZeroRegion(SR, T);
1806   }
1807 
1808   // Clear out bindings that may overlap with this binding.
1809   RegionBindingsRef NewB = removeSubRegionBindings(B, cast<SubRegion>(R));
1810   return NewB.addBinding(BindingKey::Make(R, BindingKey::Direct), V);
1811 }
1812 
1813 // FIXME: this method should be merged into Bind().
bindCompoundLiteral(Store ST,const CompoundLiteralExpr * CL,const LocationContext * LC,SVal V)1814 StoreRef RegionStoreManager::bindCompoundLiteral(Store ST,
1815                                                  const CompoundLiteralExpr *CL,
1816                                                  const LocationContext *LC,
1817                                                  SVal V) {
1818   return Bind(ST, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)), V);
1819 }
1820 
1821 RegionBindingsRef
setImplicitDefaultValue(RegionBindingsConstRef B,const MemRegion * R,QualType T)1822 RegionStoreManager::setImplicitDefaultValue(RegionBindingsConstRef B,
1823                                             const MemRegion *R,
1824                                             QualType T) {
1825   SVal V;
1826 
1827   if (Loc::isLocType(T))
1828     V = svalBuilder.makeNull();
1829   else if (T->isIntegerType())
1830     V = svalBuilder.makeZeroVal(T);
1831   else if (T->isStructureOrClassType() || T->isArrayType()) {
1832     // Set the default value to a zero constant when it is a structure
1833     // or array.  The type doesn't really matter.
1834     V = svalBuilder.makeZeroVal(Ctx.IntTy);
1835   }
1836   else {
1837     // We can't represent values of this type, but we still need to set a value
1838     // to record that the region has been initialized.
1839     // If this assertion ever fires, a new case should be added above -- we
1840     // should know how to default-initialize any value we can symbolicate.
1841     assert(!SymbolManager::canSymbolicate(T) && "This type is representable");
1842     V = UnknownVal();
1843   }
1844 
1845   return B.addBinding(R, BindingKey::Default, V);
1846 }
1847 
1848 RegionBindingsRef
bindArray(RegionBindingsConstRef B,const TypedValueRegion * R,SVal Init)1849 RegionStoreManager::bindArray(RegionBindingsConstRef B,
1850                               const TypedValueRegion* R,
1851                               SVal Init) {
1852 
1853   const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
1854   QualType ElementTy = AT->getElementType();
1855   Optional<uint64_t> Size;
1856 
1857   if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1858     Size = CAT->getSize().getZExtValue();
1859 
1860   // Check if the init expr is a string literal.
1861   if (Optional<loc::MemRegionVal> MRV = Init.getAs<loc::MemRegionVal>()) {
1862     const StringRegion *S = cast<StringRegion>(MRV->getRegion());
1863 
1864     // Treat the string as a lazy compound value.
1865     StoreRef store(B.asStore(), *this);
1866     nonloc::LazyCompoundVal LCV = svalBuilder.makeLazyCompoundVal(store, S)
1867         .castAs<nonloc::LazyCompoundVal>();
1868     return bindAggregate(B, R, LCV);
1869   }
1870 
1871   // Handle lazy compound values.
1872   if (Init.getAs<nonloc::LazyCompoundVal>())
1873     return bindAggregate(B, R, Init);
1874 
1875   // Remaining case: explicit compound values.
1876 
1877   if (Init.isUnknown())
1878     return setImplicitDefaultValue(B, R, ElementTy);
1879 
1880   const nonloc::CompoundVal& CV = Init.castAs<nonloc::CompoundVal>();
1881   nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1882   uint64_t i = 0;
1883 
1884   RegionBindingsRef NewB(B);
1885 
1886   for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
1887     // The init list might be shorter than the array length.
1888     if (VI == VE)
1889       break;
1890 
1891     const NonLoc &Idx = svalBuilder.makeArrayIndex(i);
1892     const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
1893 
1894     if (ElementTy->isStructureOrClassType())
1895       NewB = bindStruct(NewB, ER, *VI);
1896     else if (ElementTy->isArrayType())
1897       NewB = bindArray(NewB, ER, *VI);
1898     else
1899       NewB = bind(NewB, svalBuilder.makeLoc(ER), *VI);
1900   }
1901 
1902   // If the init list is shorter than the array length, set the
1903   // array default value.
1904   if (Size.hasValue() && i < Size.getValue())
1905     NewB = setImplicitDefaultValue(NewB, R, ElementTy);
1906 
1907   return NewB;
1908 }
1909 
bindVector(RegionBindingsConstRef B,const TypedValueRegion * R,SVal V)1910 RegionBindingsRef RegionStoreManager::bindVector(RegionBindingsConstRef B,
1911                                                  const TypedValueRegion* R,
1912                                                  SVal V) {
1913   QualType T = R->getValueType();
1914   assert(T->isVectorType());
1915   const VectorType *VT = T->getAs<VectorType>(); // Use getAs for typedefs.
1916 
1917   // Handle lazy compound values and symbolic values.
1918   if (V.getAs<nonloc::LazyCompoundVal>() || V.getAs<nonloc::SymbolVal>())
1919     return bindAggregate(B, R, V);
1920 
1921   // We may get non-CompoundVal accidentally due to imprecise cast logic or
1922   // that we are binding symbolic struct value. Kill the field values, and if
1923   // the value is symbolic go and bind it as a "default" binding.
1924   if (!V.getAs<nonloc::CompoundVal>()) {
1925     return bindAggregate(B, R, UnknownVal());
1926   }
1927 
1928   QualType ElemType = VT->getElementType();
1929   nonloc::CompoundVal CV = V.castAs<nonloc::CompoundVal>();
1930   nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1931   unsigned index = 0, numElements = VT->getNumElements();
1932   RegionBindingsRef NewB(B);
1933 
1934   for ( ; index != numElements ; ++index) {
1935     if (VI == VE)
1936       break;
1937 
1938     NonLoc Idx = svalBuilder.makeArrayIndex(index);
1939     const ElementRegion *ER = MRMgr.getElementRegion(ElemType, Idx, R, Ctx);
1940 
1941     if (ElemType->isArrayType())
1942       NewB = bindArray(NewB, ER, *VI);
1943     else if (ElemType->isStructureOrClassType())
1944       NewB = bindStruct(NewB, ER, *VI);
1945     else
1946       NewB = bind(NewB, svalBuilder.makeLoc(ER), *VI);
1947   }
1948   return NewB;
1949 }
1950 
bindStruct(RegionBindingsConstRef B,const TypedValueRegion * R,SVal V)1951 RegionBindingsRef RegionStoreManager::bindStruct(RegionBindingsConstRef B,
1952                                                  const TypedValueRegion* R,
1953                                                  SVal V) {
1954   if (!Features.supportsFields())
1955     return B;
1956 
1957   QualType T = R->getValueType();
1958   assert(T->isStructureOrClassType());
1959 
1960   const RecordType* RT = T->getAs<RecordType>();
1961   RecordDecl *RD = RT->getDecl();
1962 
1963   if (!RD->isCompleteDefinition())
1964     return B;
1965 
1966   // Handle lazy compound values and symbolic values.
1967   if (V.getAs<nonloc::LazyCompoundVal>() || V.getAs<nonloc::SymbolVal>())
1968     return bindAggregate(B, R, V);
1969 
1970   // We may get non-CompoundVal accidentally due to imprecise cast logic or
1971   // that we are binding symbolic struct value. Kill the field values, and if
1972   // the value is symbolic go and bind it as a "default" binding.
1973   if (V.isUnknown() || !V.getAs<nonloc::CompoundVal>())
1974     return bindAggregate(B, R, UnknownVal());
1975 
1976   const nonloc::CompoundVal& CV = V.castAs<nonloc::CompoundVal>();
1977   nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1978 
1979   RecordDecl::field_iterator FI, FE;
1980   RegionBindingsRef NewB(B);
1981 
1982   for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI) {
1983 
1984     if (VI == VE)
1985       break;
1986 
1987     // Skip any unnamed bitfields to stay in sync with the initializers.
1988     if (FI->isUnnamedBitfield())
1989       continue;
1990 
1991     QualType FTy = FI->getType();
1992     const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1993 
1994     if (FTy->isArrayType())
1995       NewB = bindArray(NewB, FR, *VI);
1996     else if (FTy->isStructureOrClassType())
1997       NewB = bindStruct(NewB, FR, *VI);
1998     else
1999       NewB = bind(NewB, svalBuilder.makeLoc(FR), *VI);
2000     ++VI;
2001   }
2002 
2003   // There may be fewer values in the initialize list than the fields of struct.
2004   if (FI != FE) {
2005     NewB = NewB.addBinding(R, BindingKey::Default,
2006                            svalBuilder.makeIntVal(0, false));
2007   }
2008 
2009   return NewB;
2010 }
2011 
2012 RegionBindingsRef
bindAggregate(RegionBindingsConstRef B,const TypedRegion * R,SVal Val)2013 RegionStoreManager::bindAggregate(RegionBindingsConstRef B,
2014                                   const TypedRegion *R,
2015                                   SVal Val) {
2016   // Remove the old bindings, using 'R' as the root of all regions
2017   // we will invalidate. Then add the new binding.
2018   return removeSubRegionBindings(B, R).addBinding(R, BindingKey::Default, Val);
2019 }
2020 
2021 //===----------------------------------------------------------------------===//
2022 // State pruning.
2023 //===----------------------------------------------------------------------===//
2024 
2025 namespace {
2026 class removeDeadBindingsWorker :
2027   public ClusterAnalysis<removeDeadBindingsWorker> {
2028   SmallVector<const SymbolicRegion*, 12> Postponed;
2029   SymbolReaper &SymReaper;
2030   const StackFrameContext *CurrentLCtx;
2031 
2032 public:
removeDeadBindingsWorker(RegionStoreManager & rm,ProgramStateManager & stateMgr,RegionBindingsRef b,SymbolReaper & symReaper,const StackFrameContext * LCtx)2033   removeDeadBindingsWorker(RegionStoreManager &rm,
2034                            ProgramStateManager &stateMgr,
2035                            RegionBindingsRef b, SymbolReaper &symReaper,
2036                            const StackFrameContext *LCtx)
2037     : ClusterAnalysis<removeDeadBindingsWorker>(rm, stateMgr, b,
2038                                                 /* includeGlobals = */ false),
2039       SymReaper(symReaper), CurrentLCtx(LCtx) {}
2040 
2041   // Called by ClusterAnalysis.
2042   void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C);
2043   void VisitCluster(const MemRegion *baseR, const ClusterBindings &C);
2044 
2045   bool UpdatePostponed();
2046   void VisitBinding(SVal V);
2047 };
2048 }
2049 
VisitAddedToCluster(const MemRegion * baseR,const ClusterBindings & C)2050 void removeDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
2051                                                    const ClusterBindings &C) {
2052 
2053   if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
2054     if (SymReaper.isLive(VR))
2055       AddToWorkList(baseR, &C);
2056 
2057     return;
2058   }
2059 
2060   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
2061     if (SymReaper.isLive(SR->getSymbol()))
2062       AddToWorkList(SR, &C);
2063     else
2064       Postponed.push_back(SR);
2065 
2066     return;
2067   }
2068 
2069   if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
2070     AddToWorkList(baseR, &C);
2071     return;
2072   }
2073 
2074   // CXXThisRegion in the current or parent location context is live.
2075   if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
2076     const StackArgumentsSpaceRegion *StackReg =
2077       cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
2078     const StackFrameContext *RegCtx = StackReg->getStackFrame();
2079     if (CurrentLCtx &&
2080         (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx)))
2081       AddToWorkList(TR, &C);
2082   }
2083 }
2084 
VisitCluster(const MemRegion * baseR,const ClusterBindings & C)2085 void removeDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
2086                                             const ClusterBindings &C) {
2087   // Mark the symbol for any SymbolicRegion with live bindings as live itself.
2088   // This means we should continue to track that symbol.
2089   if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(baseR))
2090     SymReaper.markLive(SymR->getSymbol());
2091 
2092   for (ClusterBindings::iterator I = C.begin(), E = C.end(); I != E; ++I)
2093     VisitBinding(I.getData());
2094 }
2095 
VisitBinding(SVal V)2096 void removeDeadBindingsWorker::VisitBinding(SVal V) {
2097   // Is it a LazyCompoundVal?  All referenced regions are live as well.
2098   if (Optional<nonloc::LazyCompoundVal> LCS =
2099           V.getAs<nonloc::LazyCompoundVal>()) {
2100 
2101     const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS);
2102 
2103     for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(),
2104                                                         E = Vals.end();
2105          I != E; ++I)
2106       VisitBinding(*I);
2107 
2108     return;
2109   }
2110 
2111   // If V is a region, then add it to the worklist.
2112   if (const MemRegion *R = V.getAsRegion()) {
2113     AddToWorkList(R);
2114 
2115     // All regions captured by a block are also live.
2116     if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
2117       BlockDataRegion::referenced_vars_iterator I = BR->referenced_vars_begin(),
2118                                                 E = BR->referenced_vars_end();
2119       for ( ; I != E; ++I)
2120         AddToWorkList(I.getCapturedRegion());
2121     }
2122   }
2123 
2124 
2125   // Update the set of live symbols.
2126   for (SymExpr::symbol_iterator SI = V.symbol_begin(), SE = V.symbol_end();
2127        SI!=SE; ++SI)
2128     SymReaper.markLive(*SI);
2129 }
2130 
UpdatePostponed()2131 bool removeDeadBindingsWorker::UpdatePostponed() {
2132   // See if any postponed SymbolicRegions are actually live now, after
2133   // having done a scan.
2134   bool changed = false;
2135 
2136   for (SmallVectorImpl<const SymbolicRegion*>::iterator
2137         I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
2138     if (const SymbolicRegion *SR = *I) {
2139       if (SymReaper.isLive(SR->getSymbol())) {
2140         changed |= AddToWorkList(SR);
2141         *I = NULL;
2142       }
2143     }
2144   }
2145 
2146   return changed;
2147 }
2148 
removeDeadBindings(Store store,const StackFrameContext * LCtx,SymbolReaper & SymReaper)2149 StoreRef RegionStoreManager::removeDeadBindings(Store store,
2150                                                 const StackFrameContext *LCtx,
2151                                                 SymbolReaper& SymReaper) {
2152   RegionBindingsRef B = getRegionBindings(store);
2153   removeDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
2154   W.GenerateClusters();
2155 
2156   // Enqueue the region roots onto the worklist.
2157   for (SymbolReaper::region_iterator I = SymReaper.region_begin(),
2158        E = SymReaper.region_end(); I != E; ++I) {
2159     W.AddToWorkList(*I);
2160   }
2161 
2162   do W.RunWorkList(); while (W.UpdatePostponed());
2163 
2164   // We have now scanned the store, marking reachable regions and symbols
2165   // as live.  We now remove all the regions that are dead from the store
2166   // as well as update DSymbols with the set symbols that are now dead.
2167   for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) {
2168     const MemRegion *Base = I.getKey();
2169 
2170     // If the cluster has been visited, we know the region has been marked.
2171     if (W.isVisited(Base))
2172       continue;
2173 
2174     // Remove the dead entry.
2175     B = B.remove(Base);
2176 
2177     if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(Base))
2178       SymReaper.maybeDead(SymR->getSymbol());
2179 
2180     // Mark all non-live symbols that this binding references as dead.
2181     const ClusterBindings &Cluster = I.getData();
2182     for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
2183          CI != CE; ++CI) {
2184       SVal X = CI.getData();
2185       SymExpr::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
2186       for (; SI != SE; ++SI)
2187         SymReaper.maybeDead(*SI);
2188     }
2189   }
2190 
2191   return StoreRef(B.asStore(), *this);
2192 }
2193 
2194 //===----------------------------------------------------------------------===//
2195 // Utility methods.
2196 //===----------------------------------------------------------------------===//
2197 
print(Store store,raw_ostream & OS,const char * nl,const char * sep)2198 void RegionStoreManager::print(Store store, raw_ostream &OS,
2199                                const char* nl, const char *sep) {
2200   RegionBindingsRef B = getRegionBindings(store);
2201   OS << "Store (direct and default bindings), "
2202      << B.asStore()
2203      << " :" << nl;
2204   B.dump(OS, nl);
2205 }
2206