RegionStore.cpp revision 3056439bb175db8c46b89fb4385de8b3a8e42d0d
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);
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) {
1254  if (!Array.getAs<loc::MemRegionVal>())
1255    return UnknownVal();
1256
1257  const MemRegion* R = Array.castAs<loc::MemRegionVal>().getRegion();
1258  const TypedValueRegion* ArrayR = dyn_cast<TypedValueRegion>(R);
1259
1260  if (!ArrayR)
1261    return UnknownVal();
1262
1263  // Strip off typedefs from the ArrayRegion's ValueType.
1264  QualType T = ArrayR->getValueType().getDesugaredType(Ctx);
1265  const ArrayType *AT = dyn_cast<ArrayType>(T);
1266  if (!AT)
1267    return UnknownVal();
1268
1269  T = AT->getElementType();
1270
1271  NonLoc ZeroIdx = svalBuilder.makeZeroArrayIndex();
1272  return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx));
1273}
1274
1275//===----------------------------------------------------------------------===//
1276// Loading values from regions.
1277//===----------------------------------------------------------------------===//
1278
1279SVal RegionStoreManager::getBinding(RegionBindingsConstRef B, Loc L, QualType T) {
1280  assert(!L.getAs<UnknownVal>() && "location unknown");
1281  assert(!L.getAs<UndefinedVal>() && "location undefined");
1282
1283  // For access to concrete addresses, return UnknownVal.  Checks
1284  // for null dereferences (and similar errors) are done by checkers, not
1285  // the Store.
1286  // FIXME: We can consider lazily symbolicating such memory, but we really
1287  // should defer this when we can reason easily about symbolicating arrays
1288  // of bytes.
1289  if (L.getAs<loc::ConcreteInt>()) {
1290    return UnknownVal();
1291  }
1292  if (!L.getAs<loc::MemRegionVal>()) {
1293    return UnknownVal();
1294  }
1295
1296  const MemRegion *MR = L.castAs<loc::MemRegionVal>().getRegion();
1297
1298  if (isa<AllocaRegion>(MR) ||
1299      isa<SymbolicRegion>(MR) ||
1300      isa<CodeTextRegion>(MR)) {
1301    if (T.isNull()) {
1302      if (const TypedRegion *TR = dyn_cast<TypedRegion>(MR))
1303        T = TR->getLocationType();
1304      else {
1305        const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
1306        T = SR->getSymbol()->getType();
1307      }
1308    }
1309    MR = GetElementZeroRegion(MR, T);
1310  }
1311
1312  // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1313  //  instead of 'Loc', and have the other Loc cases handled at a higher level.
1314  const TypedValueRegion *R = cast<TypedValueRegion>(MR);
1315  QualType RTy = R->getValueType();
1316
1317  // FIXME: we do not yet model the parts of a complex type, so treat the
1318  // whole thing as "unknown".
1319  if (RTy->isAnyComplexType())
1320    return UnknownVal();
1321
1322  // FIXME: We should eventually handle funny addressing.  e.g.:
1323  //
1324  //   int x = ...;
1325  //   int *p = &x;
1326  //   char *q = (char*) p;
1327  //   char c = *q;  // returns the first byte of 'x'.
1328  //
1329  // Such funny addressing will occur due to layering of regions.
1330  if (RTy->isStructureOrClassType())
1331    return getBindingForStruct(B, R);
1332
1333  // FIXME: Handle unions.
1334  if (RTy->isUnionType())
1335    return UnknownVal();
1336
1337  if (RTy->isArrayType()) {
1338    if (RTy->isConstantArrayType())
1339      return getBindingForArray(B, R);
1340    else
1341      return UnknownVal();
1342  }
1343
1344  // FIXME: handle Vector types.
1345  if (RTy->isVectorType())
1346    return UnknownVal();
1347
1348  if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
1349    return CastRetrievedVal(getBindingForField(B, FR), FR, T, false);
1350
1351  if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1352    // FIXME: Here we actually perform an implicit conversion from the loaded
1353    // value to the element type.  Eventually we want to compose these values
1354    // more intelligently.  For example, an 'element' can encompass multiple
1355    // bound regions (e.g., several bound bytes), or could be a subset of
1356    // a larger value.
1357    return CastRetrievedVal(getBindingForElement(B, ER), ER, T, false);
1358  }
1359
1360  if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1361    // FIXME: Here we actually perform an implicit conversion from the loaded
1362    // value to the ivar type.  What we should model is stores to ivars
1363    // that blow past the extent of the ivar.  If the address of the ivar is
1364    // reinterpretted, it is possible we stored a different value that could
1365    // fit within the ivar.  Either we need to cast these when storing them
1366    // or reinterpret them lazily (as we do here).
1367    return CastRetrievedVal(getBindingForObjCIvar(B, IVR), IVR, T, false);
1368  }
1369
1370  if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1371    // FIXME: Here we actually perform an implicit conversion from the loaded
1372    // value to the variable type.  What we should model is stores to variables
1373    // that blow past the extent of the variable.  If the address of the
1374    // variable is reinterpretted, it is possible we stored a different value
1375    // that could fit within the variable.  Either we need to cast these when
1376    // storing them or reinterpret them lazily (as we do here).
1377    return CastRetrievedVal(getBindingForVar(B, VR), VR, T, false);
1378  }
1379
1380  const SVal *V = B.lookup(R, BindingKey::Direct);
1381
1382  // Check if the region has a binding.
1383  if (V)
1384    return *V;
1385
1386  // The location does not have a bound value.  This means that it has
1387  // the value it had upon its creation and/or entry to the analyzed
1388  // function/method.  These are either symbolic values or 'undefined'.
1389  if (R->hasStackNonParametersStorage()) {
1390    // All stack variables are considered to have undefined values
1391    // upon creation.  All heap allocated blocks are considered to
1392    // have undefined values as well unless they are explicitly bound
1393    // to specific values.
1394    return UndefinedVal();
1395  }
1396
1397  // All other values are symbolic.
1398  return svalBuilder.getRegionValueSymbolVal(R);
1399}
1400
1401static QualType getUnderlyingType(const SubRegion *R) {
1402  QualType RegionTy;
1403  if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(R))
1404    RegionTy = TVR->getValueType();
1405
1406  if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1407    RegionTy = SR->getSymbol()->getType();
1408
1409  return RegionTy;
1410}
1411
1412/// Checks to see if store \p B has a lazy binding for region \p R.
1413///
1414/// If \p AllowSubregionBindings is \c false, a lazy binding will be rejected
1415/// if there are additional bindings within \p R.
1416///
1417/// Note that unlike RegionStoreManager::findLazyBinding, this will not search
1418/// for lazy bindings for super-regions of \p R.
1419static Optional<nonloc::LazyCompoundVal>
1420getExistingLazyBinding(SValBuilder &SVB, RegionBindingsConstRef B,
1421                       const SubRegion *R, bool AllowSubregionBindings) {
1422  Optional<SVal> V = B.getDefaultBinding(R);
1423  if (!V)
1424    return None;
1425
1426  Optional<nonloc::LazyCompoundVal> LCV = V->getAs<nonloc::LazyCompoundVal>();
1427  if (!LCV)
1428    return None;
1429
1430  // If the LCV is for a subregion, the types might not match, and we shouldn't
1431  // reuse the binding.
1432  QualType RegionTy = getUnderlyingType(R);
1433  if (!RegionTy.isNull() &&
1434      !RegionTy->isVoidPointerType()) {
1435    QualType SourceRegionTy = LCV->getRegion()->getValueType();
1436    if (!SVB.getContext().hasSameUnqualifiedType(RegionTy, SourceRegionTy))
1437      return None;
1438  }
1439
1440  if (!AllowSubregionBindings) {
1441    // If there are any other bindings within this region, we shouldn't reuse
1442    // the top-level binding.
1443    SmallVector<BindingPair, 16> Bindings;
1444    collectSubRegionBindings(Bindings, SVB, *B.lookup(R->getBaseRegion()), R,
1445                             /*IncludeAllDefaultBindings=*/true);
1446    if (Bindings.size() > 1)
1447      return None;
1448  }
1449
1450  return *LCV;
1451}
1452
1453
1454std::pair<Store, const SubRegion *>
1455RegionStoreManager::findLazyBinding(RegionBindingsConstRef B,
1456                                   const SubRegion *R,
1457                                   const SubRegion *originalRegion) {
1458  if (originalRegion != R) {
1459    if (Optional<nonloc::LazyCompoundVal> V =
1460          getExistingLazyBinding(svalBuilder, B, R, true))
1461      return std::make_pair(V->getStore(), V->getRegion());
1462  }
1463
1464  typedef std::pair<Store, const SubRegion *> StoreRegionPair;
1465  StoreRegionPair Result = StoreRegionPair();
1466
1467  if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1468    Result = findLazyBinding(B, cast<SubRegion>(ER->getSuperRegion()),
1469                             originalRegion);
1470
1471    if (Result.second)
1472      Result.second = MRMgr.getElementRegionWithSuper(ER, Result.second);
1473
1474  } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1475    Result = findLazyBinding(B, cast<SubRegion>(FR->getSuperRegion()),
1476                                       originalRegion);
1477
1478    if (Result.second)
1479      Result.second = MRMgr.getFieldRegionWithSuper(FR, Result.second);
1480
1481  } else if (const CXXBaseObjectRegion *BaseReg =
1482               dyn_cast<CXXBaseObjectRegion>(R)) {
1483    // C++ base object region is another kind of region that we should blast
1484    // through to look for lazy compound value. It is like a field region.
1485    Result = findLazyBinding(B, cast<SubRegion>(BaseReg->getSuperRegion()),
1486                             originalRegion);
1487
1488    if (Result.second)
1489      Result.second = MRMgr.getCXXBaseObjectRegionWithSuper(BaseReg,
1490                                                            Result.second);
1491  }
1492
1493  return Result;
1494}
1495
1496SVal RegionStoreManager::getBindingForElement(RegionBindingsConstRef B,
1497                                              const ElementRegion* R) {
1498  // We do not currently model bindings of the CompoundLiteralregion.
1499  if (isa<CompoundLiteralRegion>(R->getBaseRegion()))
1500    return UnknownVal();
1501
1502  // Check if the region has a binding.
1503  if (const Optional<SVal> &V = B.getDirectBinding(R))
1504    return *V;
1505
1506  const MemRegion* superR = R->getSuperRegion();
1507
1508  // Check if the region is an element region of a string literal.
1509  if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
1510    // FIXME: Handle loads from strings where the literal is treated as
1511    // an integer, e.g., *((unsigned int*)"hello")
1512    QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
1513    if (T != Ctx.getCanonicalType(R->getElementType()))
1514      return UnknownVal();
1515
1516    const StringLiteral *Str = StrR->getStringLiteral();
1517    SVal Idx = R->getIndex();
1518    if (Optional<nonloc::ConcreteInt> CI = Idx.getAs<nonloc::ConcreteInt>()) {
1519      int64_t i = CI->getValue().getSExtValue();
1520      // Abort on string underrun.  This can be possible by arbitrary
1521      // clients of getBindingForElement().
1522      if (i < 0)
1523        return UndefinedVal();
1524      int64_t length = Str->getLength();
1525      // Technically, only i == length is guaranteed to be null.
1526      // However, such overflows should be caught before reaching this point;
1527      // the only time such an access would be made is if a string literal was
1528      // used to initialize a larger array.
1529      char c = (i >= length) ? '\0' : Str->getCodeUnit(i);
1530      return svalBuilder.makeIntVal(c, T);
1531    }
1532  }
1533
1534  // Check for loads from a code text region.  For such loads, just give up.
1535  if (isa<CodeTextRegion>(superR))
1536    return UnknownVal();
1537
1538  // Handle the case where we are indexing into a larger scalar object.
1539  // For example, this handles:
1540  //   int x = ...
1541  //   char *y = &x;
1542  //   return *y;
1543  // FIXME: This is a hack, and doesn't do anything really intelligent yet.
1544  const RegionRawOffset &O = R->getAsArrayOffset();
1545
1546  // If we cannot reason about the offset, return an unknown value.
1547  if (!O.getRegion())
1548    return UnknownVal();
1549
1550  if (const TypedValueRegion *baseR =
1551        dyn_cast_or_null<TypedValueRegion>(O.getRegion())) {
1552    QualType baseT = baseR->getValueType();
1553    if (baseT->isScalarType()) {
1554      QualType elemT = R->getElementType();
1555      if (elemT->isScalarType()) {
1556        if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1557          if (const Optional<SVal> &V = B.getDirectBinding(superR)) {
1558            if (SymbolRef parentSym = V->getAsSymbol())
1559              return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1560
1561            if (V->isUnknownOrUndef())
1562              return *V;
1563            // Other cases: give up.  We are indexing into a larger object
1564            // that has some value, but we don't know how to handle that yet.
1565            return UnknownVal();
1566          }
1567        }
1568      }
1569    }
1570  }
1571  return getBindingForFieldOrElementCommon(B, R, R->getElementType());
1572}
1573
1574SVal RegionStoreManager::getBindingForField(RegionBindingsConstRef B,
1575                                            const FieldRegion* R) {
1576
1577  // Check if the region has a binding.
1578  if (const Optional<SVal> &V = B.getDirectBinding(R))
1579    return *V;
1580
1581  QualType Ty = R->getValueType();
1582  return getBindingForFieldOrElementCommon(B, R, Ty);
1583}
1584
1585Optional<SVal>
1586RegionStoreManager::getBindingForDerivedDefaultValue(RegionBindingsConstRef B,
1587                                                     const MemRegion *superR,
1588                                                     const TypedValueRegion *R,
1589                                                     QualType Ty) {
1590
1591  if (const Optional<SVal> &D = B.getDefaultBinding(superR)) {
1592    const SVal &val = D.getValue();
1593    if (SymbolRef parentSym = val.getAsSymbol())
1594      return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1595
1596    if (val.isZeroConstant())
1597      return svalBuilder.makeZeroVal(Ty);
1598
1599    if (val.isUnknownOrUndef())
1600      return val;
1601
1602    // Lazy bindings are usually handled through getExistingLazyBinding().
1603    // We should unify these two code paths at some point.
1604    if (val.getAs<nonloc::LazyCompoundVal>())
1605      return val;
1606
1607    llvm_unreachable("Unknown default value");
1608  }
1609
1610  return None;
1611}
1612
1613SVal RegionStoreManager::getLazyBinding(const SubRegion *LazyBindingRegion,
1614                                        RegionBindingsRef LazyBinding) {
1615  SVal Result;
1616  if (const ElementRegion *ER = dyn_cast<ElementRegion>(LazyBindingRegion))
1617    Result = getBindingForElement(LazyBinding, ER);
1618  else
1619    Result = getBindingForField(LazyBinding,
1620                                cast<FieldRegion>(LazyBindingRegion));
1621
1622  // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
1623  // default value for /part/ of an aggregate from a default value for the
1624  // /entire/ aggregate. The most common case of this is when struct Outer
1625  // has as its first member a struct Inner, which is copied in from a stack
1626  // variable. In this case, even if the Outer's default value is symbolic, 0,
1627  // or unknown, it gets overridden by the Inner's default value of undefined.
1628  //
1629  // This is a general problem -- if the Inner is zero-initialized, the Outer
1630  // will now look zero-initialized. The proper way to solve this is with a
1631  // new version of RegionStore that tracks the extent of a binding as well
1632  // as the offset.
1633  //
1634  // This hack only takes care of the undefined case because that can very
1635  // quickly result in a warning.
1636  if (Result.isUndef())
1637    Result = UnknownVal();
1638
1639  return Result;
1640}
1641
1642SVal
1643RegionStoreManager::getBindingForFieldOrElementCommon(RegionBindingsConstRef B,
1644                                                      const TypedValueRegion *R,
1645                                                      QualType Ty) {
1646
1647  // At this point we have already checked in either getBindingForElement or
1648  // getBindingForField if 'R' has a direct binding.
1649
1650  // Lazy binding?
1651  Store lazyBindingStore = NULL;
1652  const SubRegion *lazyBindingRegion = NULL;
1653  llvm::tie(lazyBindingStore, lazyBindingRegion) = findLazyBinding(B, R, R);
1654  if (lazyBindingRegion)
1655    return getLazyBinding(lazyBindingRegion,
1656                          getRegionBindings(lazyBindingStore));
1657
1658  // Record whether or not we see a symbolic index.  That can completely
1659  // be out of scope of our lookup.
1660  bool hasSymbolicIndex = false;
1661
1662  // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
1663  // default value for /part/ of an aggregate from a default value for the
1664  // /entire/ aggregate. The most common case of this is when struct Outer
1665  // has as its first member a struct Inner, which is copied in from a stack
1666  // variable. In this case, even if the Outer's default value is symbolic, 0,
1667  // or unknown, it gets overridden by the Inner's default value of undefined.
1668  //
1669  // This is a general problem -- if the Inner is zero-initialized, the Outer
1670  // will now look zero-initialized. The proper way to solve this is with a
1671  // new version of RegionStore that tracks the extent of a binding as well
1672  // as the offset.
1673  //
1674  // This hack only takes care of the undefined case because that can very
1675  // quickly result in a warning.
1676  bool hasPartialLazyBinding = false;
1677
1678  const SubRegion *SR = dyn_cast<SubRegion>(R);
1679  while (SR) {
1680    const MemRegion *Base = SR->getSuperRegion();
1681    if (Optional<SVal> D = getBindingForDerivedDefaultValue(B, Base, R, Ty)) {
1682      if (D->getAs<nonloc::LazyCompoundVal>()) {
1683        hasPartialLazyBinding = true;
1684        break;
1685      }
1686
1687      return *D;
1688    }
1689
1690    if (const ElementRegion *ER = dyn_cast<ElementRegion>(Base)) {
1691      NonLoc index = ER->getIndex();
1692      if (!index.isConstant())
1693        hasSymbolicIndex = true;
1694    }
1695
1696    // If our super region is a field or element itself, walk up the region
1697    // hierarchy to see if there is a default value installed in an ancestor.
1698    SR = dyn_cast<SubRegion>(Base);
1699  }
1700
1701  if (R->hasStackNonParametersStorage()) {
1702    if (isa<ElementRegion>(R)) {
1703      // Currently we don't reason specially about Clang-style vectors.  Check
1704      // if superR is a vector and if so return Unknown.
1705      if (const TypedValueRegion *typedSuperR =
1706            dyn_cast<TypedValueRegion>(R->getSuperRegion())) {
1707        if (typedSuperR->getValueType()->isVectorType())
1708          return UnknownVal();
1709      }
1710    }
1711
1712    // FIXME: We also need to take ElementRegions with symbolic indexes into
1713    // account.  This case handles both directly accessing an ElementRegion
1714    // with a symbolic offset, but also fields within an element with
1715    // a symbolic offset.
1716    if (hasSymbolicIndex)
1717      return UnknownVal();
1718
1719    if (!hasPartialLazyBinding)
1720      return UndefinedVal();
1721  }
1722
1723  // All other values are symbolic.
1724  return svalBuilder.getRegionValueSymbolVal(R);
1725}
1726
1727SVal RegionStoreManager::getBindingForObjCIvar(RegionBindingsConstRef B,
1728                                               const ObjCIvarRegion* R) {
1729  // Check if the region has a binding.
1730  if (const Optional<SVal> &V = B.getDirectBinding(R))
1731    return *V;
1732
1733  const MemRegion *superR = R->getSuperRegion();
1734
1735  // Check if the super region has a default binding.
1736  if (const Optional<SVal> &V = B.getDefaultBinding(superR)) {
1737    if (SymbolRef parentSym = V->getAsSymbol())
1738      return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1739
1740    // Other cases: give up.
1741    return UnknownVal();
1742  }
1743
1744  return getBindingForLazySymbol(R);
1745}
1746
1747SVal RegionStoreManager::getBindingForVar(RegionBindingsConstRef B,
1748                                          const VarRegion *R) {
1749
1750  // Check if the region has a binding.
1751  if (const Optional<SVal> &V = B.getDirectBinding(R))
1752    return *V;
1753
1754  // Lazily derive a value for the VarRegion.
1755  const VarDecl *VD = R->getDecl();
1756  const MemSpaceRegion *MS = R->getMemorySpace();
1757
1758  // Arguments are always symbolic.
1759  if (isa<StackArgumentsSpaceRegion>(MS))
1760    return svalBuilder.getRegionValueSymbolVal(R);
1761
1762  // Is 'VD' declared constant?  If so, retrieve the constant value.
1763  if (VD->getType().isConstQualified())
1764    if (const Expr *Init = VD->getInit())
1765      if (Optional<SVal> V = svalBuilder.getConstantVal(Init))
1766        return *V;
1767
1768  // This must come after the check for constants because closure-captured
1769  // constant variables may appear in UnknownSpaceRegion.
1770  if (isa<UnknownSpaceRegion>(MS))
1771    return svalBuilder.getRegionValueSymbolVal(R);
1772
1773  if (isa<GlobalsSpaceRegion>(MS)) {
1774    QualType T = VD->getType();
1775
1776    // Function-scoped static variables are default-initialized to 0; if they
1777    // have an initializer, it would have been processed by now.
1778    if (isa<StaticGlobalSpaceRegion>(MS))
1779      return svalBuilder.makeZeroVal(T);
1780
1781    if (Optional<SVal> V = getBindingForDerivedDefaultValue(B, MS, R, T)) {
1782      assert(!V->getAs<nonloc::LazyCompoundVal>());
1783      return V.getValue();
1784    }
1785
1786    return svalBuilder.getRegionValueSymbolVal(R);
1787  }
1788
1789  return UndefinedVal();
1790}
1791
1792SVal RegionStoreManager::getBindingForLazySymbol(const TypedValueRegion *R) {
1793  // All other values are symbolic.
1794  return svalBuilder.getRegionValueSymbolVal(R);
1795}
1796
1797const RegionStoreManager::SValListTy &
1798RegionStoreManager::getInterestingValues(nonloc::LazyCompoundVal LCV) {
1799  // First, check the cache.
1800  LazyBindingsMapTy::iterator I = LazyBindingsMap.find(LCV.getCVData());
1801  if (I != LazyBindingsMap.end())
1802    return I->second;
1803
1804  // If we don't have a list of values cached, start constructing it.
1805  SValListTy List;
1806
1807  const SubRegion *LazyR = LCV.getRegion();
1808  RegionBindingsRef B = getRegionBindings(LCV.getStore());
1809
1810  // If this region had /no/ bindings at the time, there are no interesting
1811  // values to return.
1812  const ClusterBindings *Cluster = B.lookup(LazyR->getBaseRegion());
1813  if (!Cluster)
1814    return (LazyBindingsMap[LCV.getCVData()] = llvm_move(List));
1815
1816  SmallVector<BindingPair, 32> Bindings;
1817  collectSubRegionBindings(Bindings, svalBuilder, *Cluster, LazyR,
1818                           /*IncludeAllDefaultBindings=*/true);
1819  for (SmallVectorImpl<BindingPair>::const_iterator I = Bindings.begin(),
1820                                                    E = Bindings.end();
1821       I != E; ++I) {
1822    SVal V = I->second;
1823    if (V.isUnknownOrUndef() || V.isConstant())
1824      continue;
1825
1826    if (Optional<nonloc::LazyCompoundVal> InnerLCV =
1827            V.getAs<nonloc::LazyCompoundVal>()) {
1828      const SValListTy &InnerList = getInterestingValues(*InnerLCV);
1829      List.insert(List.end(), InnerList.begin(), InnerList.end());
1830      continue;
1831    }
1832
1833    List.push_back(V);
1834  }
1835
1836  return (LazyBindingsMap[LCV.getCVData()] = llvm_move(List));
1837}
1838
1839NonLoc RegionStoreManager::createLazyBinding(RegionBindingsConstRef B,
1840                                             const TypedValueRegion *R) {
1841  if (Optional<nonloc::LazyCompoundVal> V =
1842        getExistingLazyBinding(svalBuilder, B, R, false))
1843    return *V;
1844
1845  return svalBuilder.makeLazyCompoundVal(StoreRef(B.asStore(), *this), R);
1846}
1847
1848SVal RegionStoreManager::getBindingForStruct(RegionBindingsConstRef B,
1849                                             const TypedValueRegion *R) {
1850  const RecordDecl *RD = R->getValueType()->castAs<RecordType>()->getDecl();
1851  if (RD->field_empty())
1852    return UnknownVal();
1853
1854  return createLazyBinding(B, R);
1855}
1856
1857SVal RegionStoreManager::getBindingForArray(RegionBindingsConstRef B,
1858                                            const TypedValueRegion *R) {
1859  assert(Ctx.getAsConstantArrayType(R->getValueType()) &&
1860         "Only constant array types can have compound bindings.");
1861
1862  return createLazyBinding(B, R);
1863}
1864
1865bool RegionStoreManager::includedInBindings(Store store,
1866                                            const MemRegion *region) const {
1867  RegionBindingsRef B = getRegionBindings(store);
1868  region = region->getBaseRegion();
1869
1870  // Quick path: if the base is the head of a cluster, the region is live.
1871  if (B.lookup(region))
1872    return true;
1873
1874  // Slow path: if the region is the VALUE of any binding, it is live.
1875  for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
1876    const ClusterBindings &Cluster = RI.getData();
1877    for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
1878         CI != CE; ++CI) {
1879      const SVal &D = CI.getData();
1880      if (const MemRegion *R = D.getAsRegion())
1881        if (R->getBaseRegion() == region)
1882          return true;
1883    }
1884  }
1885
1886  return false;
1887}
1888
1889//===----------------------------------------------------------------------===//
1890// Binding values to regions.
1891//===----------------------------------------------------------------------===//
1892
1893StoreRef RegionStoreManager::killBinding(Store ST, Loc L) {
1894  if (Optional<loc::MemRegionVal> LV = L.getAs<loc::MemRegionVal>())
1895    if (const MemRegion* R = LV->getRegion())
1896      return StoreRef(getRegionBindings(ST).removeBinding(R)
1897                                           .asImmutableMap()
1898                                           .getRootWithoutRetain(),
1899                      *this);
1900
1901  return StoreRef(ST, *this);
1902}
1903
1904RegionBindingsRef
1905RegionStoreManager::bind(RegionBindingsConstRef B, Loc L, SVal V) {
1906  if (L.getAs<loc::ConcreteInt>())
1907    return B;
1908
1909  // If we get here, the location should be a region.
1910  const MemRegion *R = L.castAs<loc::MemRegionVal>().getRegion();
1911
1912  // Check if the region is a struct region.
1913  if (const TypedValueRegion* TR = dyn_cast<TypedValueRegion>(R)) {
1914    QualType Ty = TR->getValueType();
1915    if (Ty->isArrayType())
1916      return bindArray(B, TR, V);
1917    if (Ty->isStructureOrClassType())
1918      return bindStruct(B, TR, V);
1919    if (Ty->isVectorType())
1920      return bindVector(B, TR, V);
1921  }
1922
1923  if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1924    // Binding directly to a symbolic region should be treated as binding
1925    // to element 0.
1926    QualType T = SR->getSymbol()->getType();
1927    if (T->isAnyPointerType() || T->isReferenceType())
1928      T = T->getPointeeType();
1929
1930    R = GetElementZeroRegion(SR, T);
1931  }
1932
1933  // Clear out bindings that may overlap with this binding.
1934  RegionBindingsRef NewB = removeSubRegionBindings(B, cast<SubRegion>(R));
1935  return NewB.addBinding(BindingKey::Make(R, BindingKey::Direct), V);
1936}
1937
1938RegionBindingsRef
1939RegionStoreManager::setImplicitDefaultValue(RegionBindingsConstRef B,
1940                                            const MemRegion *R,
1941                                            QualType T) {
1942  SVal V;
1943
1944  if (Loc::isLocType(T))
1945    V = svalBuilder.makeNull();
1946  else if (T->isIntegralOrEnumerationType())
1947    V = svalBuilder.makeZeroVal(T);
1948  else if (T->isStructureOrClassType() || T->isArrayType()) {
1949    // Set the default value to a zero constant when it is a structure
1950    // or array.  The type doesn't really matter.
1951    V = svalBuilder.makeZeroVal(Ctx.IntTy);
1952  }
1953  else {
1954    // We can't represent values of this type, but we still need to set a value
1955    // to record that the region has been initialized.
1956    // If this assertion ever fires, a new case should be added above -- we
1957    // should know how to default-initialize any value we can symbolicate.
1958    assert(!SymbolManager::canSymbolicate(T) && "This type is representable");
1959    V = UnknownVal();
1960  }
1961
1962  return B.addBinding(R, BindingKey::Default, V);
1963}
1964
1965RegionBindingsRef
1966RegionStoreManager::bindArray(RegionBindingsConstRef B,
1967                              const TypedValueRegion* R,
1968                              SVal Init) {
1969
1970  const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
1971  QualType ElementTy = AT->getElementType();
1972  Optional<uint64_t> Size;
1973
1974  if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1975    Size = CAT->getSize().getZExtValue();
1976
1977  // Check if the init expr is a string literal.
1978  if (Optional<loc::MemRegionVal> MRV = Init.getAs<loc::MemRegionVal>()) {
1979    const StringRegion *S = cast<StringRegion>(MRV->getRegion());
1980
1981    // Treat the string as a lazy compound value.
1982    StoreRef store(B.asStore(), *this);
1983    nonloc::LazyCompoundVal LCV = svalBuilder.makeLazyCompoundVal(store, S)
1984        .castAs<nonloc::LazyCompoundVal>();
1985    return bindAggregate(B, R, LCV);
1986  }
1987
1988  // Handle lazy compound values.
1989  if (Init.getAs<nonloc::LazyCompoundVal>())
1990    return bindAggregate(B, R, Init);
1991
1992  // Remaining case: explicit compound values.
1993
1994  if (Init.isUnknown())
1995    return setImplicitDefaultValue(B, R, ElementTy);
1996
1997  const nonloc::CompoundVal& CV = Init.castAs<nonloc::CompoundVal>();
1998  nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1999  uint64_t i = 0;
2000
2001  RegionBindingsRef NewB(B);
2002
2003  for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
2004    // The init list might be shorter than the array length.
2005    if (VI == VE)
2006      break;
2007
2008    const NonLoc &Idx = svalBuilder.makeArrayIndex(i);
2009    const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
2010
2011    if (ElementTy->isStructureOrClassType())
2012      NewB = bindStruct(NewB, ER, *VI);
2013    else if (ElementTy->isArrayType())
2014      NewB = bindArray(NewB, ER, *VI);
2015    else
2016      NewB = bind(NewB, loc::MemRegionVal(ER), *VI);
2017  }
2018
2019  // If the init list is shorter than the array length, set the
2020  // array default value.
2021  if (Size.hasValue() && i < Size.getValue())
2022    NewB = setImplicitDefaultValue(NewB, R, ElementTy);
2023
2024  return NewB;
2025}
2026
2027RegionBindingsRef RegionStoreManager::bindVector(RegionBindingsConstRef B,
2028                                                 const TypedValueRegion* R,
2029                                                 SVal V) {
2030  QualType T = R->getValueType();
2031  assert(T->isVectorType());
2032  const VectorType *VT = T->getAs<VectorType>(); // Use getAs for typedefs.
2033
2034  // Handle lazy compound values and symbolic values.
2035  if (V.getAs<nonloc::LazyCompoundVal>() || V.getAs<nonloc::SymbolVal>())
2036    return bindAggregate(B, R, V);
2037
2038  // We may get non-CompoundVal accidentally due to imprecise cast logic or
2039  // that we are binding symbolic struct value. Kill the field values, and if
2040  // the value is symbolic go and bind it as a "default" binding.
2041  if (!V.getAs<nonloc::CompoundVal>()) {
2042    return bindAggregate(B, R, UnknownVal());
2043  }
2044
2045  QualType ElemType = VT->getElementType();
2046  nonloc::CompoundVal CV = V.castAs<nonloc::CompoundVal>();
2047  nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
2048  unsigned index = 0, numElements = VT->getNumElements();
2049  RegionBindingsRef NewB(B);
2050
2051  for ( ; index != numElements ; ++index) {
2052    if (VI == VE)
2053      break;
2054
2055    NonLoc Idx = svalBuilder.makeArrayIndex(index);
2056    const ElementRegion *ER = MRMgr.getElementRegion(ElemType, Idx, R, Ctx);
2057
2058    if (ElemType->isArrayType())
2059      NewB = bindArray(NewB, ER, *VI);
2060    else if (ElemType->isStructureOrClassType())
2061      NewB = bindStruct(NewB, ER, *VI);
2062    else
2063      NewB = bind(NewB, loc::MemRegionVal(ER), *VI);
2064  }
2065  return NewB;
2066}
2067
2068Optional<RegionBindingsRef>
2069RegionStoreManager::tryBindSmallStruct(RegionBindingsConstRef B,
2070                                       const TypedValueRegion *R,
2071                                       const RecordDecl *RD,
2072                                       nonloc::LazyCompoundVal LCV) {
2073  FieldVector Fields;
2074
2075  if (const CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(RD))
2076    if (Class->getNumBases() != 0 || Class->getNumVBases() != 0)
2077      return None;
2078
2079  for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2080       I != E; ++I) {
2081    const FieldDecl *FD = *I;
2082    if (FD->isUnnamedBitfield())
2083      continue;
2084
2085    // If there are too many fields, or if any of the fields are aggregates,
2086    // just use the LCV as a default binding.
2087    if (Fields.size() == SmallStructLimit)
2088      return None;
2089
2090    QualType Ty = FD->getType();
2091    if (!(Ty->isScalarType() || Ty->isReferenceType()))
2092      return None;
2093
2094    Fields.push_back(*I);
2095  }
2096
2097  RegionBindingsRef NewB = B;
2098
2099  for (FieldVector::iterator I = Fields.begin(), E = Fields.end(); I != E; ++I){
2100    const FieldRegion *SourceFR = MRMgr.getFieldRegion(*I, LCV.getRegion());
2101    SVal V = getBindingForField(getRegionBindings(LCV.getStore()), SourceFR);
2102
2103    const FieldRegion *DestFR = MRMgr.getFieldRegion(*I, R);
2104    NewB = bind(NewB, loc::MemRegionVal(DestFR), V);
2105  }
2106
2107  return NewB;
2108}
2109
2110RegionBindingsRef RegionStoreManager::bindStruct(RegionBindingsConstRef B,
2111                                                 const TypedValueRegion* R,
2112                                                 SVal V) {
2113  if (!Features.supportsFields())
2114    return B;
2115
2116  QualType T = R->getValueType();
2117  assert(T->isStructureOrClassType());
2118
2119  const RecordType* RT = T->getAs<RecordType>();
2120  const RecordDecl *RD = RT->getDecl();
2121
2122  if (!RD->isCompleteDefinition())
2123    return B;
2124
2125  // Handle lazy compound values and symbolic values.
2126  if (Optional<nonloc::LazyCompoundVal> LCV =
2127        V.getAs<nonloc::LazyCompoundVal>()) {
2128    if (Optional<RegionBindingsRef> NewB = tryBindSmallStruct(B, R, RD, *LCV))
2129      return *NewB;
2130    return bindAggregate(B, R, V);
2131  }
2132  if (V.getAs<nonloc::SymbolVal>())
2133    return bindAggregate(B, R, V);
2134
2135  // We may get non-CompoundVal accidentally due to imprecise cast logic or
2136  // that we are binding symbolic struct value. Kill the field values, and if
2137  // the value is symbolic go and bind it as a "default" binding.
2138  if (V.isUnknown() || !V.getAs<nonloc::CompoundVal>())
2139    return bindAggregate(B, R, UnknownVal());
2140
2141  const nonloc::CompoundVal& CV = V.castAs<nonloc::CompoundVal>();
2142  nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
2143
2144  RecordDecl::field_iterator FI, FE;
2145  RegionBindingsRef NewB(B);
2146
2147  for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI) {
2148
2149    if (VI == VE)
2150      break;
2151
2152    // Skip any unnamed bitfields to stay in sync with the initializers.
2153    if (FI->isUnnamedBitfield())
2154      continue;
2155
2156    QualType FTy = FI->getType();
2157    const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
2158
2159    if (FTy->isArrayType())
2160      NewB = bindArray(NewB, FR, *VI);
2161    else if (FTy->isStructureOrClassType())
2162      NewB = bindStruct(NewB, FR, *VI);
2163    else
2164      NewB = bind(NewB, loc::MemRegionVal(FR), *VI);
2165    ++VI;
2166  }
2167
2168  // There may be fewer values in the initialize list than the fields of struct.
2169  if (FI != FE) {
2170    NewB = NewB.addBinding(R, BindingKey::Default,
2171                           svalBuilder.makeIntVal(0, false));
2172  }
2173
2174  return NewB;
2175}
2176
2177RegionBindingsRef
2178RegionStoreManager::bindAggregate(RegionBindingsConstRef B,
2179                                  const TypedRegion *R,
2180                                  SVal Val) {
2181  // Remove the old bindings, using 'R' as the root of all regions
2182  // we will invalidate. Then add the new binding.
2183  return removeSubRegionBindings(B, R).addBinding(R, BindingKey::Default, Val);
2184}
2185
2186//===----------------------------------------------------------------------===//
2187// State pruning.
2188//===----------------------------------------------------------------------===//
2189
2190namespace {
2191class removeDeadBindingsWorker :
2192  public ClusterAnalysis<removeDeadBindingsWorker> {
2193  SmallVector<const SymbolicRegion*, 12> Postponed;
2194  SymbolReaper &SymReaper;
2195  const StackFrameContext *CurrentLCtx;
2196
2197public:
2198  removeDeadBindingsWorker(RegionStoreManager &rm,
2199                           ProgramStateManager &stateMgr,
2200                           RegionBindingsRef b, SymbolReaper &symReaper,
2201                           const StackFrameContext *LCtx)
2202    : ClusterAnalysis<removeDeadBindingsWorker>(rm, stateMgr, b, GFK_None),
2203      SymReaper(symReaper), CurrentLCtx(LCtx) {}
2204
2205  // Called by ClusterAnalysis.
2206  void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C);
2207  void VisitCluster(const MemRegion *baseR, const ClusterBindings *C);
2208  using ClusterAnalysis<removeDeadBindingsWorker>::VisitCluster;
2209
2210  bool UpdatePostponed();
2211  void VisitBinding(SVal V);
2212};
2213}
2214
2215void removeDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
2216                                                   const ClusterBindings &C) {
2217
2218  if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
2219    if (SymReaper.isLive(VR))
2220      AddToWorkList(baseR, &C);
2221
2222    return;
2223  }
2224
2225  if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
2226    if (SymReaper.isLive(SR->getSymbol()))
2227      AddToWorkList(SR, &C);
2228    else
2229      Postponed.push_back(SR);
2230
2231    return;
2232  }
2233
2234  if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
2235    AddToWorkList(baseR, &C);
2236    return;
2237  }
2238
2239  // CXXThisRegion in the current or parent location context is live.
2240  if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
2241    const StackArgumentsSpaceRegion *StackReg =
2242      cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
2243    const StackFrameContext *RegCtx = StackReg->getStackFrame();
2244    if (CurrentLCtx &&
2245        (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx)))
2246      AddToWorkList(TR, &C);
2247  }
2248}
2249
2250void removeDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
2251                                            const ClusterBindings *C) {
2252  if (!C)
2253    return;
2254
2255  // Mark the symbol for any SymbolicRegion with live bindings as live itself.
2256  // This means we should continue to track that symbol.
2257  if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(baseR))
2258    SymReaper.markLive(SymR->getSymbol());
2259
2260  for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E; ++I)
2261    VisitBinding(I.getData());
2262}
2263
2264void removeDeadBindingsWorker::VisitBinding(SVal V) {
2265  // Is it a LazyCompoundVal?  All referenced regions are live as well.
2266  if (Optional<nonloc::LazyCompoundVal> LCS =
2267          V.getAs<nonloc::LazyCompoundVal>()) {
2268
2269    const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS);
2270
2271    for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(),
2272                                                        E = Vals.end();
2273         I != E; ++I)
2274      VisitBinding(*I);
2275
2276    return;
2277  }
2278
2279  // If V is a region, then add it to the worklist.
2280  if (const MemRegion *R = V.getAsRegion()) {
2281    AddToWorkList(R);
2282
2283    // All regions captured by a block are also live.
2284    if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
2285      BlockDataRegion::referenced_vars_iterator I = BR->referenced_vars_begin(),
2286                                                E = BR->referenced_vars_end();
2287      for ( ; I != E; ++I)
2288        AddToWorkList(I.getCapturedRegion());
2289    }
2290  }
2291
2292
2293  // Update the set of live symbols.
2294  for (SymExpr::symbol_iterator SI = V.symbol_begin(), SE = V.symbol_end();
2295       SI!=SE; ++SI)
2296    SymReaper.markLive(*SI);
2297}
2298
2299bool removeDeadBindingsWorker::UpdatePostponed() {
2300  // See if any postponed SymbolicRegions are actually live now, after
2301  // having done a scan.
2302  bool changed = false;
2303
2304  for (SmallVectorImpl<const SymbolicRegion*>::iterator
2305        I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
2306    if (const SymbolicRegion *SR = *I) {
2307      if (SymReaper.isLive(SR->getSymbol())) {
2308        changed |= AddToWorkList(SR);
2309        *I = NULL;
2310      }
2311    }
2312  }
2313
2314  return changed;
2315}
2316
2317StoreRef RegionStoreManager::removeDeadBindings(Store store,
2318                                                const StackFrameContext *LCtx,
2319                                                SymbolReaper& SymReaper) {
2320  RegionBindingsRef B = getRegionBindings(store);
2321  removeDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
2322  W.GenerateClusters();
2323
2324  // Enqueue the region roots onto the worklist.
2325  for (SymbolReaper::region_iterator I = SymReaper.region_begin(),
2326       E = SymReaper.region_end(); I != E; ++I) {
2327    W.AddToWorkList(*I);
2328  }
2329
2330  do W.RunWorkList(); while (W.UpdatePostponed());
2331
2332  // We have now scanned the store, marking reachable regions and symbols
2333  // as live.  We now remove all the regions that are dead from the store
2334  // as well as update DSymbols with the set symbols that are now dead.
2335  for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) {
2336    const MemRegion *Base = I.getKey();
2337
2338    // If the cluster has been visited, we know the region has been marked.
2339    if (W.isVisited(Base))
2340      continue;
2341
2342    // Remove the dead entry.
2343    B = B.remove(Base);
2344
2345    if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(Base))
2346      SymReaper.maybeDead(SymR->getSymbol());
2347
2348    // Mark all non-live symbols that this binding references as dead.
2349    const ClusterBindings &Cluster = I.getData();
2350    for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
2351         CI != CE; ++CI) {
2352      SVal X = CI.getData();
2353      SymExpr::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
2354      for (; SI != SE; ++SI)
2355        SymReaper.maybeDead(*SI);
2356    }
2357  }
2358
2359  return StoreRef(B.asStore(), *this);
2360}
2361
2362//===----------------------------------------------------------------------===//
2363// Utility methods.
2364//===----------------------------------------------------------------------===//
2365
2366void RegionStoreManager::print(Store store, raw_ostream &OS,
2367                               const char* nl, const char *sep) {
2368  RegionBindingsRef B = getRegionBindings(store);
2369  OS << "Store (direct and default bindings), "
2370     << B.asStore()
2371     << " :" << nl;
2372  B.dump(OS, nl);
2373}
2374