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