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