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