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