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