1 //== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
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
7 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
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"
35 using namespace clang;
38 //===----------------------------------------------------------------------===//
39 // Representation of binding keys.
40 //===----------------------------------------------------------------------===//
45 enum Kind { Default = 0x0, Direct = 0x1 };
47 enum { Symbolic = 0x2 };
49 llvm::PointerIntPair<const MemRegion *, 2> P;
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");
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)) &&
71 bool isDirect() const { return P.getInt() & Direct; }
72 bool hasSymbolicOffset() const { return P.getInt() & Symbolic; }
74 const MemRegion *getRegion() const { return P.getPointer(); }
75 uint64_t getOffset() const {
76 assert(!hasSymbolicOffset());
80 const SubRegion *getConcreteOffsetRegion() const {
81 assert(hasSymbolicOffset());
82 return reinterpret_cast<const SubRegion *>(static_cast<uintptr_t>(Data));
85 const MemRegion *getBaseRegion() const {
86 if (hasSymbolicOffset())
87 return getConcreteOffsetRegion()->getBaseRegion();
88 return getRegion()->getBaseRegion();
91 void Profile(llvm::FoldingSetNodeID& ID) const {
92 ID.AddPointer(P.getOpaqueValue());
96 static BindingKey Make(const MemRegion *R, Kind k);
98 bool operator<(const BindingKey &X) const {
99 if (P.getOpaqueValue() < X.P.getOpaqueValue())
101 if (P.getOpaqueValue() > X.P.getOpaqueValue())
103 return Data < X.Data;
106 bool operator==(const BindingKey &X) const {
107 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
113 } // end anonymous namespace
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);
120 return BindingKey(RO.getRegion(), RO.getOffset(), k);
124 static inline raw_ostream &operator<<(raw_ostream &Out, BindingKey K) {
125 Out << "\"kind\": \"" << (K.isDirect() ? "Direct" : "Default")
126 << "\", \"offset\": ";
128 if (!K.hasSymbolicOffset())
129 Out << K.getOffset();
138 LLVM_DUMP_METHOD void BindingKey::dump() const { llvm::errs() << *this; }
140 //===----------------------------------------------------------------------===//
141 // Actual Store type.
142 //===----------------------------------------------------------------------===//
144 typedef llvm::ImmutableMap<BindingKey, SVal> ClusterBindings;
145 typedef llvm::ImmutableMapRef<BindingKey, SVal> ClusterBindingsRef;
146 typedef std::pair<BindingKey, SVal> BindingPair;
148 typedef llvm::ImmutableMap<const MemRegion *, ClusterBindings>
152 class RegionBindingsRef : public llvm::ImmutableMapRef<const MemRegion *,
154 ClusterBindings::Factory *CBFactory;
157 typedef llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>
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) {}
166 RegionBindingsRef(const ParentTy &P, ClusterBindings::Factory &CBFactory)
167 : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(P),
168 CBFactory(&CBFactory) {}
170 RegionBindingsRef add(key_type_ref K, data_type_ref D) const {
171 return RegionBindingsRef(static_cast<const ParentTy *>(this)->add(K, D),
175 RegionBindingsRef remove(key_type_ref K) const {
176 return RegionBindingsRef(static_cast<const ParentTy *>(this)->remove(K),
180 RegionBindingsRef addBinding(BindingKey K, SVal V) const;
182 RegionBindingsRef addBinding(const MemRegion *R,
183 BindingKey::Kind k, SVal V) const;
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;
189 RegionBindingsRef removeBinding(BindingKey K);
191 RegionBindingsRef removeBinding(const MemRegion *R,
194 RegionBindingsRef removeBinding(const MemRegion *R) {
195 return removeBinding(R, BindingKey::Direct).
196 removeBinding(R, BindingKey::Default);
199 Optional<SVal> getDirectBinding(const MemRegion *R) const;
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;
205 /// Return the internal tree as a Store.
206 Store asStore() const {
207 return asImmutableMap().getRootWithoutRetain();
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 // TODO: We might need a .printJson for I.getKey() as well.
214 Indent(Out, Space, IsDot)
215 << "{ \"cluster\": \"" << I.getKey() << "\", \"pointer\": \""
216 << (const void *)I.getKey() << "\", \"items\": [" << NL;
219 const ClusterBindings &CB = I.getData();
220 for (ClusterBindings::iterator CI = CB.begin(); CI != CB.end(); ++CI) {
221 Indent(Out, Space, IsDot) << "{ " << CI.getKey() << ", \"value\": ";
222 CI.getData().printJson(Out, /*AddQuotes=*/true);
224 if (std::next(CI) != CB.end())
230 Indent(Out, Space, IsDot) << "]}";
231 if (std::next(I) != end())
237 LLVM_DUMP_METHOD void dump() const { printJson(llvm::errs()); }
239 } // end anonymous namespace
241 typedef const RegionBindingsRef& RegionBindingsConstRef;
243 Optional<SVal> RegionBindingsRef::getDirectBinding(const MemRegion *R) const {
244 return Optional<SVal>::create(lookup(R, BindingKey::Direct));
247 Optional<SVal> RegionBindingsRef::getDefaultBinding(const MemRegion *R) const {
248 return Optional<SVal>::create(lookup(R, BindingKey::Default));
251 RegionBindingsRef RegionBindingsRef::addBinding(BindingKey K, SVal V) const {
252 const MemRegion *Base = K.getBaseRegion();
254 const ClusterBindings *ExistingCluster = lookup(Base);
255 ClusterBindings Cluster =
256 (ExistingCluster ? *ExistingCluster : CBFactory->getEmptyMap());
258 ClusterBindings NewCluster = CBFactory->add(Cluster, K, V);
259 return add(Base, NewCluster);
263 RegionBindingsRef RegionBindingsRef::addBinding(const MemRegion *R,
266 return addBinding(BindingKey::Make(R, k), V);
269 const SVal *RegionBindingsRef::lookup(BindingKey K) const {
270 const ClusterBindings *Cluster = lookup(K.getBaseRegion());
273 return Cluster->lookup(K);
276 const SVal *RegionBindingsRef::lookup(const MemRegion *R,
277 BindingKey::Kind k) const {
278 return lookup(BindingKey::Make(R, k));
281 RegionBindingsRef RegionBindingsRef::removeBinding(BindingKey K) {
282 const MemRegion *Base = K.getBaseRegion();
283 const ClusterBindings *Cluster = lookup(Base);
287 ClusterBindings NewCluster = CBFactory->remove(*Cluster, K);
288 if (NewCluster.isEmpty())
290 return add(Base, NewCluster);
293 RegionBindingsRef RegionBindingsRef::removeBinding(const MemRegion *R,
295 return removeBinding(BindingKey::Make(R, k));
298 //===----------------------------------------------------------------------===//
299 // Fine-grained control of RegionStoreManager.
300 //===----------------------------------------------------------------------===//
303 struct minimal_features_tag {};
304 struct maximal_features_tag {};
306 class RegionStoreFeatures {
309 RegionStoreFeatures(minimal_features_tag) :
310 SupportsFields(false) {}
312 RegionStoreFeatures(maximal_features_tag) :
313 SupportsFields(true) {}
315 void enableFields(bool t) { SupportsFields = t; }
317 bool supportsFields() const { return SupportsFields; }
321 //===----------------------------------------------------------------------===//
322 // Main RegionStore logic.
323 //===----------------------------------------------------------------------===//
326 class InvalidateRegionsWorker;
328 class RegionStoreManager : public StoreManager {
330 const RegionStoreFeatures Features;
332 RegionBindings::Factory RBFactory;
333 mutable ClusterBindings::Factory CBFactory;
335 typedef std::vector<SVal> SValListTy;
337 typedef llvm::DenseMap<const LazyCompoundValData *,
338 SValListTy> LazyBindingsMapTy;
339 LazyBindingsMapTy LazyBindingsMap;
341 /// The largest number of fields a struct can have and still be
342 /// considered "small".
344 /// This is currently used to decide whether or not it is worth "forcing" a
345 /// LazyCompoundVal on bind.
347 /// This is controlled by 'region-store-small-struct-limit' option.
348 /// To disable all small-struct-dependent behavior, set the option to "0".
349 unsigned SmallStructLimit;
351 /// A helper used to populate the work list with the given set of
353 void populateWorkList(InvalidateRegionsWorker &W,
354 ArrayRef<SVal> Values,
355 InvalidatedRegions *TopLevelRegions);
358 RegionStoreManager(ProgramStateManager& mgr, const RegionStoreFeatures &f)
359 : StoreManager(mgr), Features(f),
360 RBFactory(mgr.getAllocator()), CBFactory(mgr.getAllocator()),
361 SmallStructLimit(0) {
362 SubEngine &Eng = StateMgr.getOwningEngine();
363 AnalyzerOptions &Options = Eng.getAnalysisManager().options;
364 SmallStructLimit = Options.RegionStoreSmallStructLimit;
368 /// setImplicitDefaultValue - Set the default binding for the provided
369 /// MemRegion to the value implicitly defined for compound literals when
370 /// the value is not specified.
371 RegionBindingsRef setImplicitDefaultValue(RegionBindingsConstRef B,
372 const MemRegion *R, QualType T);
374 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
375 /// type. 'Array' represents the lvalue of the array being decayed
376 /// to a pointer, and the returned SVal represents the decayed
377 /// version of that lvalue (i.e., a pointer to the first element of
378 /// the array). This is called by ExprEngine when evaluating
379 /// casts from arrays to pointers.
380 SVal ArrayToPointer(Loc Array, QualType ElementTy) override;
382 StoreRef getInitialStore(const LocationContext *InitLoc) override {
383 return StoreRef(RBFactory.getEmptyMap().getRootWithoutRetain(), *this);
386 //===-------------------------------------------------------------------===//
387 // Binding values to regions.
388 //===-------------------------------------------------------------------===//
389 RegionBindingsRef invalidateGlobalRegion(MemRegion::Kind K,
392 const LocationContext *LCtx,
394 InvalidatedRegions *Invalidated);
396 StoreRef invalidateRegions(Store store,
397 ArrayRef<SVal> Values,
398 const Expr *E, unsigned Count,
399 const LocationContext *LCtx,
400 const CallEvent *Call,
401 InvalidatedSymbols &IS,
402 RegionAndSymbolInvalidationTraits &ITraits,
403 InvalidatedRegions *Invalidated,
404 InvalidatedRegions *InvalidatedTopLevel) override;
406 bool scanReachableSymbols(Store S, const MemRegion *R,
407 ScanReachableSymbols &Callbacks) override;
409 RegionBindingsRef removeSubRegionBindings(RegionBindingsConstRef B,
412 public: // Part of public interface to class.
414 StoreRef Bind(Store store, Loc LV, SVal V) override {
415 return StoreRef(bind(getRegionBindings(store), LV, V).asStore(), *this);
418 RegionBindingsRef bind(RegionBindingsConstRef B, Loc LV, SVal V);
420 // BindDefaultInitial is only used to initialize a region with
422 StoreRef BindDefaultInitial(Store store, const MemRegion *R,
424 RegionBindingsRef B = getRegionBindings(store);
425 // Use other APIs when you have to wipe the region that was initialized
427 assert(!(B.getDefaultBinding(R) || B.getDirectBinding(R)) &&
428 "Double initialization!");
429 B = B.addBinding(BindingKey::Make(R, BindingKey::Default), V);
430 return StoreRef(B.asImmutableMap().getRootWithoutRetain(), *this);
433 // BindDefaultZero is used for zeroing constructors that may accidentally
434 // overwrite existing bindings.
435 StoreRef BindDefaultZero(Store store, const MemRegion *R) override {
436 // FIXME: The offsets of empty bases can be tricky because of
437 // of the so called "empty base class optimization".
438 // If a base class has been optimized out
439 // we should not try to create a binding, otherwise we should.
440 // Unfortunately, at the moment ASTRecordLayout doesn't expose
441 // the actual sizes of the empty bases
442 // and trying to infer them from offsets/alignments
443 // seems to be error-prone and non-trivial because of the trailing padding.
444 // As a temporary mitigation we don't create bindings for empty bases.
445 if (const auto *BR = dyn_cast<CXXBaseObjectRegion>(R))
446 if (BR->getDecl()->isEmpty())
447 return StoreRef(store, *this);
449 RegionBindingsRef B = getRegionBindings(store);
450 SVal V = svalBuilder.makeZeroVal(Ctx.CharTy);
451 B = removeSubRegionBindings(B, cast<SubRegion>(R));
452 B = B.addBinding(BindingKey::Make(R, BindingKey::Default), V);
453 return StoreRef(B.asImmutableMap().getRootWithoutRetain(), *this);
456 /// Attempt to extract the fields of \p LCV and bind them to the struct region
459 /// This path is used when it seems advantageous to "force" loading the values
460 /// within a LazyCompoundVal to bind memberwise to the struct region, rather
461 /// than using a Default binding at the base of the entire region. This is a
462 /// heuristic attempting to avoid building long chains of LazyCompoundVals.
464 /// \returns The updated store bindings, or \c None if binding non-lazily
465 /// would be too expensive.
466 Optional<RegionBindingsRef> tryBindSmallStruct(RegionBindingsConstRef B,
467 const TypedValueRegion *R,
468 const RecordDecl *RD,
469 nonloc::LazyCompoundVal LCV);
471 /// BindStruct - Bind a compound value to a structure.
472 RegionBindingsRef bindStruct(RegionBindingsConstRef B,
473 const TypedValueRegion* R, SVal V);
475 /// BindVector - Bind a compound value to a vector.
476 RegionBindingsRef bindVector(RegionBindingsConstRef B,
477 const TypedValueRegion* R, SVal V);
479 RegionBindingsRef bindArray(RegionBindingsConstRef B,
480 const TypedValueRegion* R,
483 /// Clears out all bindings in the given region and assigns a new value
484 /// as a Default binding.
485 RegionBindingsRef bindAggregate(RegionBindingsConstRef B,
486 const TypedRegion *R,
489 /// Create a new store with the specified binding removed.
490 /// \param ST the original store, that is the basis for the new store.
491 /// \param L the location whose binding should be removed.
492 StoreRef killBinding(Store ST, Loc L) override;
494 void incrementReferenceCount(Store store) override {
495 getRegionBindings(store).manualRetain();
498 /// If the StoreManager supports it, decrement the reference count of
499 /// the specified Store object. If the reference count hits 0, the memory
500 /// associated with the object is recycled.
501 void decrementReferenceCount(Store store) override {
502 getRegionBindings(store).manualRelease();
505 bool includedInBindings(Store store, const MemRegion *region) const override;
507 /// Return the value bound to specified location in a given state.
509 /// The high level logic for this method is this:
512 /// return L's binding
513 /// else if L is in killset
516 /// if L is on stack or heap
520 SVal getBinding(Store S, Loc L, QualType T) override {
521 return getBinding(getRegionBindings(S), L, T);
524 Optional<SVal> getDefaultBinding(Store S, const MemRegion *R) override {
525 RegionBindingsRef B = getRegionBindings(S);
526 // Default bindings are always applied over a base region so look up the
527 // base region's default binding, otherwise the lookup will fail when R
528 // is at an offset from R->getBaseRegion().
529 return B.getDefaultBinding(R->getBaseRegion());
532 SVal getBinding(RegionBindingsConstRef B, Loc L, QualType T = QualType());
534 SVal getBindingForElement(RegionBindingsConstRef B, const ElementRegion *R);
536 SVal getBindingForField(RegionBindingsConstRef B, const FieldRegion *R);
538 SVal getBindingForObjCIvar(RegionBindingsConstRef B, const ObjCIvarRegion *R);
540 SVal getBindingForVar(RegionBindingsConstRef B, const VarRegion *R);
542 SVal getBindingForLazySymbol(const TypedValueRegion *R);
544 SVal getBindingForFieldOrElementCommon(RegionBindingsConstRef B,
545 const TypedValueRegion *R,
548 SVal getLazyBinding(const SubRegion *LazyBindingRegion,
549 RegionBindingsRef LazyBinding);
551 /// Get bindings for the values in a struct and return a CompoundVal, used
552 /// when doing struct copy:
555 /// y's value is retrieved by this method.
556 SVal getBindingForStruct(RegionBindingsConstRef B, const TypedValueRegion *R);
557 SVal getBindingForArray(RegionBindingsConstRef B, const TypedValueRegion *R);
558 NonLoc createLazyBinding(RegionBindingsConstRef B, const TypedValueRegion *R);
560 /// Used to lazily generate derived symbols for bindings that are defined
561 /// implicitly by default bindings in a super region.
563 /// Note that callers may need to specially handle LazyCompoundVals, which
564 /// are returned as is in case the caller needs to treat them differently.
565 Optional<SVal> getBindingForDerivedDefaultValue(RegionBindingsConstRef B,
566 const MemRegion *superR,
567 const TypedValueRegion *R,
570 /// Get the state and region whose binding this region \p R corresponds to.
572 /// If there is no lazy binding for \p R, the returned value will have a null
573 /// \c second. Note that a null pointer can represents a valid Store.
574 std::pair<Store, const SubRegion *>
575 findLazyBinding(RegionBindingsConstRef B, const SubRegion *R,
576 const SubRegion *originalRegion);
578 /// Returns the cached set of interesting SVals contained within a lazy
581 /// The precise value of "interesting" is determined for the purposes of
582 /// RegionStore's internal analysis. It must always contain all regions and
583 /// symbols, but may omit constants and other kinds of SVal.
584 const SValListTy &getInterestingValues(nonloc::LazyCompoundVal LCV);
586 //===------------------------------------------------------------------===//
588 //===------------------------------------------------------------------===//
590 /// removeDeadBindings - Scans the RegionStore of 'state' for dead values.
591 /// It returns a new Store with these values removed.
592 StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx,
593 SymbolReaper& SymReaper) override;
595 //===------------------------------------------------------------------===//
597 //===------------------------------------------------------------------===//
599 // FIXME: This method will soon be eliminated; see the note in Store.h.
600 DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state,
602 QualType EleTy) override;
604 //===------------------------------------------------------------------===//
606 //===------------------------------------------------------------------===//
608 RegionBindingsRef getRegionBindings(Store store) const {
609 return RegionBindingsRef(CBFactory,
610 static_cast<const RegionBindings::TreeTy*>(store),
611 RBFactory.getTreeFactory());
614 void printJson(raw_ostream &Out, Store S, const char *NL = "\n",
615 unsigned int Space = 0, bool IsDot = false) const override;
617 void iterBindings(Store store, BindingsHandler& f) override {
618 RegionBindingsRef B = getRegionBindings(store);
619 for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) {
620 const ClusterBindings &Cluster = I.getData();
621 for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
623 const BindingKey &K = CI.getKey();
626 if (const SubRegion *R = dyn_cast<SubRegion>(K.getRegion())) {
627 // FIXME: Possibly incorporate the offset?
628 if (!f.HandleBinding(*this, store, R, CI.getData()))
636 } // end anonymous namespace
638 //===----------------------------------------------------------------------===//
639 // RegionStore creation.
640 //===----------------------------------------------------------------------===//
642 std::unique_ptr<StoreManager>
643 ento::CreateRegionStoreManager(ProgramStateManager &StMgr) {
644 RegionStoreFeatures F = maximal_features_tag();
645 return llvm::make_unique<RegionStoreManager>(StMgr, F);
648 std::unique_ptr<StoreManager>
649 ento::CreateFieldsOnlyRegionStoreManager(ProgramStateManager &StMgr) {
650 RegionStoreFeatures F = minimal_features_tag();
651 F.enableFields(true);
652 return llvm::make_unique<RegionStoreManager>(StMgr, F);
656 //===----------------------------------------------------------------------===//
657 // Region Cluster analysis.
658 //===----------------------------------------------------------------------===//
661 /// Used to determine which global regions are automatically included in the
662 /// initial worklist of a ClusterAnalysis.
663 enum GlobalsFilterKind {
664 /// Don't include any global regions.
666 /// Only include system globals.
668 /// Include all global regions.
672 template <typename DERIVED>
673 class ClusterAnalysis {
675 typedef llvm::DenseMap<const MemRegion *, const ClusterBindings *> ClusterMap;
676 typedef const MemRegion * WorkListElement;
677 typedef SmallVector<WorkListElement, 10> WorkList;
679 llvm::SmallPtrSet<const ClusterBindings *, 16> Visited;
683 RegionStoreManager &RM;
685 SValBuilder &svalBuilder;
691 const ClusterBindings *getCluster(const MemRegion *R) {
695 /// Returns true if all clusters in the given memspace should be initially
696 /// included in the cluster analysis. Subclasses may provide their
697 /// own implementation.
698 bool includeEntireMemorySpace(const MemRegion *Base) {
703 ClusterAnalysis(RegionStoreManager &rm, ProgramStateManager &StateMgr,
705 : RM(rm), Ctx(StateMgr.getContext()),
706 svalBuilder(StateMgr.getSValBuilder()), B(std::move(b)) {}
708 RegionBindingsRef getRegionBindings() const { return B; }
710 bool isVisited(const MemRegion *R) {
711 return Visited.count(getCluster(R));
714 void GenerateClusters() {
715 // Scan the entire set of bindings and record the region clusters.
716 for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end();
718 const MemRegion *Base = RI.getKey();
720 const ClusterBindings &Cluster = RI.getData();
721 assert(!Cluster.isEmpty() && "Empty clusters should be removed");
722 static_cast<DERIVED*>(this)->VisitAddedToCluster(Base, Cluster);
724 // If the base's memspace should be entirely invalidated, add the cluster
725 // to the workspace up front.
726 if (static_cast<DERIVED*>(this)->includeEntireMemorySpace(Base))
727 AddToWorkList(WorkListElement(Base), &Cluster);
731 bool AddToWorkList(WorkListElement E, const ClusterBindings *C) {
732 if (C && !Visited.insert(C).second)
738 bool AddToWorkList(const MemRegion *R) {
739 return static_cast<DERIVED*>(this)->AddToWorkList(R);
743 while (!WL.empty()) {
744 WorkListElement E = WL.pop_back_val();
745 const MemRegion *BaseR = E;
747 static_cast<DERIVED*>(this)->VisitCluster(BaseR, getCluster(BaseR));
751 void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C) {}
752 void VisitCluster(const MemRegion *baseR, const ClusterBindings *C) {}
754 void VisitCluster(const MemRegion *BaseR, const ClusterBindings *C,
756 static_cast<DERIVED*>(this)->VisitCluster(BaseR, C);
761 //===----------------------------------------------------------------------===//
762 // Binding invalidation.
763 //===----------------------------------------------------------------------===//
765 bool RegionStoreManager::scanReachableSymbols(Store S, const MemRegion *R,
766 ScanReachableSymbols &Callbacks) {
767 assert(R == R->getBaseRegion() && "Should only be called for base regions");
768 RegionBindingsRef B = getRegionBindings(S);
769 const ClusterBindings *Cluster = B.lookup(R);
774 for (ClusterBindings::iterator RI = Cluster->begin(), RE = Cluster->end();
776 if (!Callbacks.scan(RI.getData()))
783 static inline bool isUnionField(const FieldRegion *FR) {
784 return FR->getDecl()->getParent()->isUnion();
787 typedef SmallVector<const FieldDecl *, 8> FieldVector;
789 static void getSymbolicOffsetFields(BindingKey K, FieldVector &Fields) {
790 assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys");
792 const MemRegion *Base = K.getConcreteOffsetRegion();
793 const MemRegion *R = K.getRegion();
796 if (const FieldRegion *FR = dyn_cast<FieldRegion>(R))
797 if (!isUnionField(FR))
798 Fields.push_back(FR->getDecl());
800 R = cast<SubRegion>(R)->getSuperRegion();
804 static bool isCompatibleWithFields(BindingKey K, const FieldVector &Fields) {
805 assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys");
810 FieldVector FieldsInBindingKey;
811 getSymbolicOffsetFields(K, FieldsInBindingKey);
813 ptrdiff_t Delta = FieldsInBindingKey.size() - Fields.size();
815 return std::equal(FieldsInBindingKey.begin() + Delta,
816 FieldsInBindingKey.end(),
819 return std::equal(FieldsInBindingKey.begin(), FieldsInBindingKey.end(),
820 Fields.begin() - Delta);
823 /// Collects all bindings in \p Cluster that may refer to bindings within
826 /// Each binding is a pair whose \c first is the key (a BindingKey) and whose
827 /// \c second is the value (an SVal).
829 /// The \p IncludeAllDefaultBindings parameter specifies whether to include
830 /// default bindings that may extend beyond \p Top itself, e.g. if \p Top is
831 /// an aggregate within a larger aggregate with a default binding.
833 collectSubRegionBindings(SmallVectorImpl<BindingPair> &Bindings,
834 SValBuilder &SVB, const ClusterBindings &Cluster,
835 const SubRegion *Top, BindingKey TopKey,
836 bool IncludeAllDefaultBindings) {
837 FieldVector FieldsInSymbolicSubregions;
838 if (TopKey.hasSymbolicOffset()) {
839 getSymbolicOffsetFields(TopKey, FieldsInSymbolicSubregions);
840 Top = TopKey.getConcreteOffsetRegion();
841 TopKey = BindingKey::Make(Top, BindingKey::Default);
844 // Find the length (in bits) of the region being invalidated.
845 uint64_t Length = UINT64_MAX;
846 SVal Extent = Top->getExtent(SVB);
847 if (Optional<nonloc::ConcreteInt> ExtentCI =
848 Extent.getAs<nonloc::ConcreteInt>()) {
849 const llvm::APSInt &ExtentInt = ExtentCI->getValue();
850 assert(ExtentInt.isNonNegative() || ExtentInt.isUnsigned());
851 // Extents are in bytes but region offsets are in bits. Be careful!
852 Length = ExtentInt.getLimitedValue() * SVB.getContext().getCharWidth();
853 } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(Top)) {
854 if (FR->getDecl()->isBitField())
855 Length = FR->getDecl()->getBitWidthValue(SVB.getContext());
858 for (ClusterBindings::iterator I = Cluster.begin(), E = Cluster.end();
860 BindingKey NextKey = I.getKey();
861 if (NextKey.getRegion() == TopKey.getRegion()) {
862 // FIXME: This doesn't catch the case where we're really invalidating a
863 // region with a symbolic offset. Example:
867 if (NextKey.getOffset() > TopKey.getOffset() &&
868 NextKey.getOffset() - TopKey.getOffset() < Length) {
869 // Case 1: The next binding is inside the region we're invalidating.
871 Bindings.push_back(*I);
873 } else if (NextKey.getOffset() == TopKey.getOffset()) {
874 // Case 2: The next binding is at the same offset as the region we're
875 // invalidating. In this case, we need to leave default bindings alone,
876 // since they may be providing a default value for a regions beyond what
877 // we're invalidating.
878 // FIXME: This is probably incorrect; consider invalidating an outer
879 // struct whose first field is bound to a LazyCompoundVal.
880 if (IncludeAllDefaultBindings || NextKey.isDirect())
881 Bindings.push_back(*I);
884 } else if (NextKey.hasSymbolicOffset()) {
885 const MemRegion *Base = NextKey.getConcreteOffsetRegion();
886 if (Top->isSubRegionOf(Base) && Top != Base) {
887 // Case 3: The next key is symbolic and we just changed something within
888 // its concrete region. We don't know if the binding is still valid, so
889 // we'll be conservative and include it.
890 if (IncludeAllDefaultBindings || NextKey.isDirect())
891 if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions))
892 Bindings.push_back(*I);
893 } else if (const SubRegion *BaseSR = dyn_cast<SubRegion>(Base)) {
894 // Case 4: The next key is symbolic, but we changed a known
895 // super-region. In this case the binding is certainly included.
896 if (BaseSR->isSubRegionOf(Top))
897 if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions))
898 Bindings.push_back(*I);
905 collectSubRegionBindings(SmallVectorImpl<BindingPair> &Bindings,
906 SValBuilder &SVB, const ClusterBindings &Cluster,
907 const SubRegion *Top, bool IncludeAllDefaultBindings) {
908 collectSubRegionBindings(Bindings, SVB, Cluster, Top,
909 BindingKey::Make(Top, BindingKey::Default),
910 IncludeAllDefaultBindings);
914 RegionStoreManager::removeSubRegionBindings(RegionBindingsConstRef B,
915 const SubRegion *Top) {
916 BindingKey TopKey = BindingKey::Make(Top, BindingKey::Default);
917 const MemRegion *ClusterHead = TopKey.getBaseRegion();
919 if (Top == ClusterHead) {
920 // We can remove an entire cluster's bindings all in one go.
921 return B.remove(Top);
924 const ClusterBindings *Cluster = B.lookup(ClusterHead);
926 // If we're invalidating a region with a symbolic offset, we need to make
927 // sure we don't treat the base region as uninitialized anymore.
928 if (TopKey.hasSymbolicOffset()) {
929 const SubRegion *Concrete = TopKey.getConcreteOffsetRegion();
930 return B.addBinding(Concrete, BindingKey::Default, UnknownVal());
935 SmallVector<BindingPair, 32> Bindings;
936 collectSubRegionBindings(Bindings, svalBuilder, *Cluster, Top, TopKey,
937 /*IncludeAllDefaultBindings=*/false);
939 ClusterBindingsRef Result(*Cluster, CBFactory);
940 for (SmallVectorImpl<BindingPair>::const_iterator I = Bindings.begin(),
943 Result = Result.remove(I->first);
945 // If we're invalidating a region with a symbolic offset, we need to make sure
946 // we don't treat the base region as uninitialized anymore.
947 // FIXME: This isn't very precise; see the example in
948 // collectSubRegionBindings.
949 if (TopKey.hasSymbolicOffset()) {
950 const SubRegion *Concrete = TopKey.getConcreteOffsetRegion();
951 Result = Result.add(BindingKey::Make(Concrete, BindingKey::Default),
955 if (Result.isEmpty())
956 return B.remove(ClusterHead);
957 return B.add(ClusterHead, Result.asImmutableMap());
961 class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
965 const LocationContext *LCtx;
966 InvalidatedSymbols &IS;
967 RegionAndSymbolInvalidationTraits &ITraits;
968 StoreManager::InvalidatedRegions *Regions;
969 GlobalsFilterKind GlobalsFilter;
971 InvalidateRegionsWorker(RegionStoreManager &rm,
972 ProgramStateManager &stateMgr,
974 const Expr *ex, unsigned count,
975 const LocationContext *lctx,
976 InvalidatedSymbols &is,
977 RegionAndSymbolInvalidationTraits &ITraitsIn,
978 StoreManager::InvalidatedRegions *r,
979 GlobalsFilterKind GFK)
980 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
981 Ex(ex), Count(count), LCtx(lctx), IS(is), ITraits(ITraitsIn), Regions(r),
982 GlobalsFilter(GFK) {}
984 void VisitCluster(const MemRegion *baseR, const ClusterBindings *C);
985 void VisitBinding(SVal V);
987 using ClusterAnalysis::AddToWorkList;
989 bool AddToWorkList(const MemRegion *R);
991 /// Returns true if all clusters in the memory space for \p Base should be
993 bool includeEntireMemorySpace(const MemRegion *Base);
995 /// Returns true if the memory space of the given region is one of the global
996 /// regions specially included at the start of invalidation.
997 bool isInitiallyIncludedGlobalRegion(const MemRegion *R);
1001 bool InvalidateRegionsWorker::AddToWorkList(const MemRegion *R) {
1002 bool doNotInvalidateSuperRegion = ITraits.hasTrait(
1003 R, RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
1004 const MemRegion *BaseR = doNotInvalidateSuperRegion ? R : R->getBaseRegion();
1005 return AddToWorkList(WorkListElement(BaseR), getCluster(BaseR));
1008 void InvalidateRegionsWorker::VisitBinding(SVal V) {
1009 // A symbol? Mark it touched by the invalidation.
1010 if (SymbolRef Sym = V.getAsSymbol())
1013 if (const MemRegion *R = V.getAsRegion()) {
1018 // Is it a LazyCompoundVal? All references get invalidated as well.
1019 if (Optional<nonloc::LazyCompoundVal> LCS =
1020 V.getAs<nonloc::LazyCompoundVal>()) {
1022 const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS);
1024 for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(),
1033 void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
1034 const ClusterBindings *C) {
1036 bool PreserveRegionsContents =
1037 ITraits.hasTrait(baseR,
1038 RegionAndSymbolInvalidationTraits::TK_PreserveContents);
1041 for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E; ++I)
1042 VisitBinding(I.getData());
1044 // Invalidate regions contents.
1045 if (!PreserveRegionsContents)
1046 B = B.remove(baseR);
1049 if (const auto *TO = dyn_cast<TypedValueRegion>(baseR)) {
1050 if (const auto *RD = TO->getValueType()->getAsCXXRecordDecl()) {
1052 // Lambdas can affect all static local variables without explicitly
1054 // We invalidate all static locals referenced inside the lambda body.
1055 if (RD->isLambda() && RD->getLambdaCallOperator()->getBody()) {
1056 using namespace ast_matchers;
1058 const char *DeclBind = "DeclBind";
1059 StatementMatcher RefToStatic = stmt(hasDescendant(declRefExpr(
1060 to(varDecl(hasStaticStorageDuration()).bind(DeclBind)))));
1062 match(RefToStatic, *RD->getLambdaCallOperator()->getBody(),
1063 RD->getASTContext());
1065 for (BoundNodes &Match : Matches) {
1066 auto *VD = Match.getNodeAs<VarDecl>(DeclBind);
1067 const VarRegion *ToInvalidate =
1068 RM.getRegionManager().getVarRegion(VD, LCtx);
1069 AddToWorkList(ToInvalidate);
1075 // BlockDataRegion? If so, invalidate captured variables that are passed
1077 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
1078 for (BlockDataRegion::referenced_vars_iterator
1079 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
1081 const VarRegion *VR = BI.getCapturedRegion();
1082 const VarDecl *VD = VR->getDecl();
1083 if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage()) {
1086 else if (Loc::isLocType(VR->getValueType())) {
1087 // Map the current bindings to a Store to retrieve the value
1088 // of the binding. If that binding itself is a region, we should
1089 // invalidate that region. This is because a block may capture
1090 // a pointer value, but the thing pointed by that pointer may
1092 SVal V = RM.getBinding(B, loc::MemRegionVal(VR));
1093 if (Optional<Loc> L = V.getAs<Loc>()) {
1094 if (const MemRegion *LR = L->getAsRegion())
1103 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
1104 IS.insert(SR->getSymbol());
1106 // Nothing else should be done in the case when we preserve regions context.
1107 if (PreserveRegionsContents)
1110 // Otherwise, we have a normal data region. Record that we touched the region.
1112 Regions->push_back(baseR);
1114 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
1115 // Invalidate the region by setting its default value to
1116 // conjured symbol. The type of the symbol is irrelevant.
1117 DefinedOrUnknownSVal V =
1118 svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, Ctx.IntTy, Count);
1119 B = B.addBinding(baseR, BindingKey::Default, V);
1123 if (!baseR->isBoundable())
1126 const TypedValueRegion *TR = cast<TypedValueRegion>(baseR);
1127 QualType T = TR->getValueType();
1129 if (isInitiallyIncludedGlobalRegion(baseR)) {
1130 // If the region is a global and we are invalidating all globals,
1131 // erasing the entry is good enough. This causes all globals to be lazily
1132 // symbolicated from the same base symbol.
1136 if (T->isRecordType()) {
1137 // Invalidate the region by setting its default value to
1138 // conjured symbol. The type of the symbol is irrelevant.
1139 DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
1141 B = B.addBinding(baseR, BindingKey::Default, V);
1145 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
1146 bool doNotInvalidateSuperRegion = ITraits.hasTrait(
1148 RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
1150 if (doNotInvalidateSuperRegion) {
1151 // We are not doing blank invalidation of the whole array region so we
1152 // have to manually invalidate each elements.
1153 Optional<uint64_t> NumElements;
1155 // Compute lower and upper offsets for region within array.
1156 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1157 NumElements = CAT->getSize().getZExtValue();
1158 if (!NumElements) // We are not dealing with a constant size array
1159 goto conjure_default;
1160 QualType ElementTy = AT->getElementType();
1161 uint64_t ElemSize = Ctx.getTypeSize(ElementTy);
1162 const RegionOffset &RO = baseR->getAsOffset();
1163 const MemRegion *SuperR = baseR->getBaseRegion();
1164 if (RO.hasSymbolicOffset()) {
1165 // If base region has a symbolic offset,
1166 // we revert to invalidating the super region.
1168 AddToWorkList(SuperR);
1169 goto conjure_default;
1172 uint64_t LowerOffset = RO.getOffset();
1173 uint64_t UpperOffset = LowerOffset + *NumElements * ElemSize;
1174 bool UpperOverflow = UpperOffset < LowerOffset;
1176 // Invalidate regions which are within array boundaries,
1177 // or have a symbolic offset.
1179 goto conjure_default;
1181 const ClusterBindings *C = B.lookup(SuperR);
1183 goto conjure_default;
1185 for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E;
1187 const BindingKey &BK = I.getKey();
1188 Optional<uint64_t> ROffset =
1189 BK.hasSymbolicOffset() ? Optional<uint64_t>() : BK.getOffset();
1191 // Check offset is not symbolic and within array's boundaries.
1192 // Handles arrays of 0 elements and of 0-sized elements as well.
1194 ((*ROffset >= LowerOffset && *ROffset < UpperOffset) ||
1196 (*ROffset >= LowerOffset || *ROffset < UpperOffset)) ||
1197 (LowerOffset == UpperOffset && *ROffset == LowerOffset))) {
1198 B = B.removeBinding(I.getKey());
1199 // Bound symbolic regions need to be invalidated for dead symbol
1201 SVal V = I.getData();
1202 const MemRegion *R = V.getAsRegion();
1203 if (R && isa<SymbolicRegion>(R))
1209 // Set the default value of the array to conjured symbol.
1210 DefinedOrUnknownSVal V =
1211 svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
1212 AT->getElementType(), Count);
1213 B = B.addBinding(baseR, BindingKey::Default, V);
1217 DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
1219 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
1220 B = B.addBinding(baseR, BindingKey::Direct, V);
1223 bool InvalidateRegionsWorker::isInitiallyIncludedGlobalRegion(
1224 const MemRegion *R) {
1225 switch (GlobalsFilter) {
1228 case GFK_SystemOnly:
1229 return isa<GlobalSystemSpaceRegion>(R->getMemorySpace());
1231 return isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace());
1234 llvm_unreachable("unknown globals filter");
1237 bool InvalidateRegionsWorker::includeEntireMemorySpace(const MemRegion *Base) {
1238 if (isInitiallyIncludedGlobalRegion(Base))
1241 const MemSpaceRegion *MemSpace = Base->getMemorySpace();
1242 return ITraits.hasTrait(MemSpace,
1243 RegionAndSymbolInvalidationTraits::TK_EntireMemSpace);
1247 RegionStoreManager::invalidateGlobalRegion(MemRegion::Kind K,
1250 const LocationContext *LCtx,
1251 RegionBindingsRef B,
1252 InvalidatedRegions *Invalidated) {
1253 // Bind the globals memory space to a new symbol that we will use to derive
1254 // the bindings for all globals.
1255 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion(K);
1256 SVal V = svalBuilder.conjureSymbolVal(/* symbolTag = */ (const void*) GS, Ex, LCtx,
1257 /* type does not matter */ Ctx.IntTy,
1260 B = B.removeBinding(GS)
1261 .addBinding(BindingKey::Make(GS, BindingKey::Default), V);
1263 // Even if there are no bindings in the global scope, we still need to
1264 // record that we touched it.
1266 Invalidated->push_back(GS);
1271 void RegionStoreManager::populateWorkList(InvalidateRegionsWorker &W,
1272 ArrayRef<SVal> Values,
1273 InvalidatedRegions *TopLevelRegions) {
1274 for (ArrayRef<SVal>::iterator I = Values.begin(),
1275 E = Values.end(); I != E; ++I) {
1277 if (Optional<nonloc::LazyCompoundVal> LCS =
1278 V.getAs<nonloc::LazyCompoundVal>()) {
1280 const SValListTy &Vals = getInterestingValues(*LCS);
1282 for (SValListTy::const_iterator I = Vals.begin(),
1283 E = Vals.end(); I != E; ++I) {
1284 // Note: the last argument is false here because these are
1285 // non-top-level regions.
1286 if (const MemRegion *R = (*I).getAsRegion())
1292 if (const MemRegion *R = V.getAsRegion()) {
1293 if (TopLevelRegions)
1294 TopLevelRegions->push_back(R);
1302 RegionStoreManager::invalidateRegions(Store store,
1303 ArrayRef<SVal> Values,
1304 const Expr *Ex, unsigned Count,
1305 const LocationContext *LCtx,
1306 const CallEvent *Call,
1307 InvalidatedSymbols &IS,
1308 RegionAndSymbolInvalidationTraits &ITraits,
1309 InvalidatedRegions *TopLevelRegions,
1310 InvalidatedRegions *Invalidated) {
1311 GlobalsFilterKind GlobalsFilter;
1313 if (Call->isInSystemHeader())
1314 GlobalsFilter = GFK_SystemOnly;
1316 GlobalsFilter = GFK_All;
1318 GlobalsFilter = GFK_None;
1321 RegionBindingsRef B = getRegionBindings(store);
1322 InvalidateRegionsWorker W(*this, StateMgr, B, Ex, Count, LCtx, IS, ITraits,
1323 Invalidated, GlobalsFilter);
1325 // Scan the bindings and generate the clusters.
1326 W.GenerateClusters();
1328 // Add the regions to the worklist.
1329 populateWorkList(W, Values, TopLevelRegions);
1333 // Return the new bindings.
1334 B = W.getRegionBindings();
1336 // For calls, determine which global regions should be invalidated and
1337 // invalidate them. (Note that function-static and immutable globals are never
1338 // invalidated by this.)
1339 // TODO: This could possibly be more precise with modules.
1340 switch (GlobalsFilter) {
1342 B = invalidateGlobalRegion(MemRegion::GlobalInternalSpaceRegionKind,
1343 Ex, Count, LCtx, B, Invalidated);
1345 case GFK_SystemOnly:
1346 B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind,
1347 Ex, Count, LCtx, B, Invalidated);
1353 return StoreRef(B.asStore(), *this);
1356 //===----------------------------------------------------------------------===//
1357 // Extents for regions.
1358 //===----------------------------------------------------------------------===//
1360 DefinedOrUnknownSVal
1361 RegionStoreManager::getSizeInElements(ProgramStateRef state,
1364 SVal Size = cast<SubRegion>(R)->getExtent(svalBuilder);
1365 const llvm::APSInt *SizeInt = svalBuilder.getKnownValue(state, Size);
1367 return UnknownVal();
1369 CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue());
1371 if (Ctx.getAsVariableArrayType(EleTy)) {
1372 // FIXME: We need to track extra state to properly record the size
1373 // of VLAs. Returning UnknownVal here, however, is a stop-gap so that
1374 // we don't have a divide-by-zero below.
1375 return UnknownVal();
1378 CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
1380 // If a variable is reinterpreted as a type that doesn't fit into a larger
1381 // type evenly, round it down.
1382 // This is a signed value, since it's used in arithmetic with signed indices.
1383 return svalBuilder.makeIntVal(RegionSize / EleSize,
1384 svalBuilder.getArrayIndexType());
1387 //===----------------------------------------------------------------------===//
1388 // Location and region casting.
1389 //===----------------------------------------------------------------------===//
1391 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
1392 /// type. 'Array' represents the lvalue of the array being decayed
1393 /// to a pointer, and the returned SVal represents the decayed
1394 /// version of that lvalue (i.e., a pointer to the first element of
1395 /// the array). This is called by ExprEngine when evaluating casts
1396 /// from arrays to pointers.
1397 SVal RegionStoreManager::ArrayToPointer(Loc Array, QualType T) {
1398 if (Array.getAs<loc::ConcreteInt>())
1401 if (!Array.getAs<loc::MemRegionVal>())
1402 return UnknownVal();
1404 const SubRegion *R =
1405 cast<SubRegion>(Array.castAs<loc::MemRegionVal>().getRegion());
1406 NonLoc ZeroIdx = svalBuilder.makeZeroArrayIndex();
1407 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, R, Ctx));
1410 //===----------------------------------------------------------------------===//
1411 // Loading values from regions.
1412 //===----------------------------------------------------------------------===//
1414 SVal RegionStoreManager::getBinding(RegionBindingsConstRef B, Loc L, QualType T) {
1415 assert(!L.getAs<UnknownVal>() && "location unknown");
1416 assert(!L.getAs<UndefinedVal>() && "location undefined");
1418 // For access to concrete addresses, return UnknownVal. Checks
1419 // for null dereferences (and similar errors) are done by checkers, not
1421 // FIXME: We can consider lazily symbolicating such memory, but we really
1422 // should defer this when we can reason easily about symbolicating arrays
1424 if (L.getAs<loc::ConcreteInt>()) {
1425 return UnknownVal();
1427 if (!L.getAs<loc::MemRegionVal>()) {
1428 return UnknownVal();
1431 const MemRegion *MR = L.castAs<loc::MemRegionVal>().getRegion();
1433 if (isa<BlockDataRegion>(MR)) {
1434 return UnknownVal();
1437 if (!isa<TypedValueRegion>(MR)) {
1439 if (const TypedRegion *TR = dyn_cast<TypedRegion>(MR))
1440 T = TR->getLocationType()->getPointeeType();
1441 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1442 T = SR->getSymbol()->getType()->getPointeeType();
1444 assert(!T.isNull() && "Unable to auto-detect binding type!");
1445 assert(!T->isVoidType() && "Attempting to dereference a void pointer!");
1446 MR = GetElementZeroRegion(cast<SubRegion>(MR), T);
1448 T = cast<TypedValueRegion>(MR)->getValueType();
1451 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1452 // instead of 'Loc', and have the other Loc cases handled at a higher level.
1453 const TypedValueRegion *R = cast<TypedValueRegion>(MR);
1454 QualType RTy = R->getValueType();
1456 // FIXME: we do not yet model the parts of a complex type, so treat the
1457 // whole thing as "unknown".
1458 if (RTy->isAnyComplexType())
1459 return UnknownVal();
1461 // FIXME: We should eventually handle funny addressing. e.g.:
1465 // char *q = (char*) p;
1466 // char c = *q; // returns the first byte of 'x'.
1468 // Such funny addressing will occur due to layering of regions.
1469 if (RTy->isStructureOrClassType())
1470 return getBindingForStruct(B, R);
1472 // FIXME: Handle unions.
1473 if (RTy->isUnionType())
1474 return createLazyBinding(B, R);
1476 if (RTy->isArrayType()) {
1477 if (RTy->isConstantArrayType())
1478 return getBindingForArray(B, R);
1480 return UnknownVal();
1483 // FIXME: handle Vector types.
1484 if (RTy->isVectorType())
1485 return UnknownVal();
1487 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
1488 return CastRetrievedVal(getBindingForField(B, FR), FR, T);
1490 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1491 // FIXME: Here we actually perform an implicit conversion from the loaded
1492 // value to the element type. Eventually we want to compose these values
1493 // more intelligently. For example, an 'element' can encompass multiple
1494 // bound regions (e.g., several bound bytes), or could be a subset of
1496 return CastRetrievedVal(getBindingForElement(B, ER), ER, T);
1499 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1500 // FIXME: Here we actually perform an implicit conversion from the loaded
1501 // value to the ivar type. What we should model is stores to ivars
1502 // that blow past the extent of the ivar. If the address of the ivar is
1503 // reinterpretted, it is possible we stored a different value that could
1504 // fit within the ivar. Either we need to cast these when storing them
1505 // or reinterpret them lazily (as we do here).
1506 return CastRetrievedVal(getBindingForObjCIvar(B, IVR), IVR, T);
1509 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1510 // FIXME: Here we actually perform an implicit conversion from the loaded
1511 // value to the variable type. What we should model is stores to variables
1512 // that blow past the extent of the variable. If the address of the
1513 // variable is reinterpretted, it is possible we stored a different value
1514 // that could fit within the variable. Either we need to cast these when
1515 // storing them or reinterpret them lazily (as we do here).
1516 return CastRetrievedVal(getBindingForVar(B, VR), VR, T);
1519 const SVal *V = B.lookup(R, BindingKey::Direct);
1521 // Check if the region has a binding.
1525 // The location does not have a bound value. This means that it has
1526 // the value it had upon its creation and/or entry to the analyzed
1527 // function/method. These are either symbolic values or 'undefined'.
1528 if (R->hasStackNonParametersStorage()) {
1529 // All stack variables are considered to have undefined values
1530 // upon creation. All heap allocated blocks are considered to
1531 // have undefined values as well unless they are explicitly bound
1532 // to specific values.
1533 return UndefinedVal();
1536 // All other values are symbolic.
1537 return svalBuilder.getRegionValueSymbolVal(R);
1540 static QualType getUnderlyingType(const SubRegion *R) {
1542 if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(R))
1543 RegionTy = TVR->getValueType();
1545 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
1546 RegionTy = SR->getSymbol()->getType();
1551 /// Checks to see if store \p B has a lazy binding for region \p R.
1553 /// If \p AllowSubregionBindings is \c false, a lazy binding will be rejected
1554 /// if there are additional bindings within \p R.
1556 /// Note that unlike RegionStoreManager::findLazyBinding, this will not search
1557 /// for lazy bindings for super-regions of \p R.
1558 static Optional<nonloc::LazyCompoundVal>
1559 getExistingLazyBinding(SValBuilder &SVB, RegionBindingsConstRef B,
1560 const SubRegion *R, bool AllowSubregionBindings) {
1561 Optional<SVal> V = B.getDefaultBinding(R);
1565 Optional<nonloc::LazyCompoundVal> LCV = V->getAs<nonloc::LazyCompoundVal>();
1569 // If the LCV is for a subregion, the types might not match, and we shouldn't
1570 // reuse the binding.
1571 QualType RegionTy = getUnderlyingType(R);
1572 if (!RegionTy.isNull() &&
1573 !RegionTy->isVoidPointerType()) {
1574 QualType SourceRegionTy = LCV->getRegion()->getValueType();
1575 if (!SVB.getContext().hasSameUnqualifiedType(RegionTy, SourceRegionTy))
1579 if (!AllowSubregionBindings) {
1580 // If there are any other bindings within this region, we shouldn't reuse
1581 // the top-level binding.
1582 SmallVector<BindingPair, 16> Bindings;
1583 collectSubRegionBindings(Bindings, SVB, *B.lookup(R->getBaseRegion()), R,
1584 /*IncludeAllDefaultBindings=*/true);
1585 if (Bindings.size() > 1)
1593 std::pair<Store, const SubRegion *>
1594 RegionStoreManager::findLazyBinding(RegionBindingsConstRef B,
1596 const SubRegion *originalRegion) {
1597 if (originalRegion != R) {
1598 if (Optional<nonloc::LazyCompoundVal> V =
1599 getExistingLazyBinding(svalBuilder, B, R, true))
1600 return std::make_pair(V->getStore(), V->getRegion());
1603 typedef std::pair<Store, const SubRegion *> StoreRegionPair;
1604 StoreRegionPair Result = StoreRegionPair();
1606 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1607 Result = findLazyBinding(B, cast<SubRegion>(ER->getSuperRegion()),
1611 Result.second = MRMgr.getElementRegionWithSuper(ER, Result.second);
1613 } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1614 Result = findLazyBinding(B, cast<SubRegion>(FR->getSuperRegion()),
1618 Result.second = MRMgr.getFieldRegionWithSuper(FR, Result.second);
1620 } else if (const CXXBaseObjectRegion *BaseReg =
1621 dyn_cast<CXXBaseObjectRegion>(R)) {
1622 // C++ base object region is another kind of region that we should blast
1623 // through to look for lazy compound value. It is like a field region.
1624 Result = findLazyBinding(B, cast<SubRegion>(BaseReg->getSuperRegion()),
1628 Result.second = MRMgr.getCXXBaseObjectRegionWithSuper(BaseReg,
1635 SVal RegionStoreManager::getBindingForElement(RegionBindingsConstRef B,
1636 const ElementRegion* R) {
1637 // We do not currently model bindings of the CompoundLiteralregion.
1638 if (isa<CompoundLiteralRegion>(R->getBaseRegion()))
1639 return UnknownVal();
1641 // Check if the region has a binding.
1642 if (const Optional<SVal> &V = B.getDirectBinding(R))
1645 const MemRegion* superR = R->getSuperRegion();
1647 // Check if the region is an element region of a string literal.
1648 if (const StringRegion *StrR = dyn_cast<StringRegion>(superR)) {
1649 // FIXME: Handle loads from strings where the literal is treated as
1650 // an integer, e.g., *((unsigned int*)"hello")
1651 QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
1652 if (!Ctx.hasSameUnqualifiedType(T, R->getElementType()))
1653 return UnknownVal();
1655 const StringLiteral *Str = StrR->getStringLiteral();
1656 SVal Idx = R->getIndex();
1657 if (Optional<nonloc::ConcreteInt> CI = Idx.getAs<nonloc::ConcreteInt>()) {
1658 int64_t i = CI->getValue().getSExtValue();
1659 // Abort on string underrun. This can be possible by arbitrary
1660 // clients of getBindingForElement().
1662 return UndefinedVal();
1663 int64_t length = Str->getLength();
1664 // Technically, only i == length is guaranteed to be null.
1665 // However, such overflows should be caught before reaching this point;
1666 // the only time such an access would be made is if a string literal was
1667 // used to initialize a larger array.
1668 char c = (i >= length) ? '\0' : Str->getCodeUnit(i);
1669 return svalBuilder.makeIntVal(c, T);
1671 } else if (const VarRegion *VR = dyn_cast<VarRegion>(superR)) {
1672 // Check if the containing array is const and has an initialized value.
1673 const VarDecl *VD = VR->getDecl();
1674 // Either the array or the array element has to be const.
1675 if (VD->getType().isConstQualified() || R->getElementType().isConstQualified()) {
1676 if (const Expr *Init = VD->getAnyInitializer()) {
1677 if (const auto *InitList = dyn_cast<InitListExpr>(Init)) {
1678 // The array index has to be known.
1679 if (auto CI = R->getIndex().getAs<nonloc::ConcreteInt>()) {
1680 int64_t i = CI->getValue().getSExtValue();
1681 // If it is known that the index is out of bounds, we can return
1682 // an undefined value.
1684 return UndefinedVal();
1686 if (auto CAT = Ctx.getAsConstantArrayType(VD->getType()))
1687 if (CAT->getSize().sle(i))
1688 return UndefinedVal();
1690 // If there is a list, but no init, it must be zero.
1691 if (i >= InitList->getNumInits())
1692 return svalBuilder.makeZeroVal(R->getElementType());
1694 if (const Expr *ElemInit = InitList->getInit(i))
1695 if (Optional<SVal> V = svalBuilder.getConstantVal(ElemInit))
1703 // Check for loads from a code text region. For such loads, just give up.
1704 if (isa<CodeTextRegion>(superR))
1705 return UnknownVal();
1707 // Handle the case where we are indexing into a larger scalar object.
1708 // For example, this handles:
1712 // FIXME: This is a hack, and doesn't do anything really intelligent yet.
1713 const RegionRawOffset &O = R->getAsArrayOffset();
1715 // If we cannot reason about the offset, return an unknown value.
1717 return UnknownVal();
1719 if (const TypedValueRegion *baseR =
1720 dyn_cast_or_null<TypedValueRegion>(O.getRegion())) {
1721 QualType baseT = baseR->getValueType();
1722 if (baseT->isScalarType()) {
1723 QualType elemT = R->getElementType();
1724 if (elemT->isScalarType()) {
1725 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1726 if (const Optional<SVal> &V = B.getDirectBinding(superR)) {
1727 if (SymbolRef parentSym = V->getAsSymbol())
1728 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1730 if (V->isUnknownOrUndef())
1732 // Other cases: give up. We are indexing into a larger object
1733 // that has some value, but we don't know how to handle that yet.
1734 return UnknownVal();
1740 return getBindingForFieldOrElementCommon(B, R, R->getElementType());
1743 SVal RegionStoreManager::getBindingForField(RegionBindingsConstRef B,
1744 const FieldRegion* R) {
1746 // Check if the region has a binding.
1747 if (const Optional<SVal> &V = B.getDirectBinding(R))
1750 // Is the field declared constant and has an in-class initializer?
1751 const FieldDecl *FD = R->getDecl();
1752 QualType Ty = FD->getType();
1753 if (Ty.isConstQualified())
1754 if (const Expr *Init = FD->getInClassInitializer())
1755 if (Optional<SVal> V = svalBuilder.getConstantVal(Init))
1758 // If the containing record was initialized, try to get its constant value.
1759 const MemRegion* superR = R->getSuperRegion();
1760 if (const auto *VR = dyn_cast<VarRegion>(superR)) {
1761 const VarDecl *VD = VR->getDecl();
1762 QualType RecordVarTy = VD->getType();
1763 unsigned Index = FD->getFieldIndex();
1764 // Either the record variable or the field has to be const qualified.
1765 if (RecordVarTy.isConstQualified() || Ty.isConstQualified())
1766 if (const Expr *Init = VD->getAnyInitializer())
1767 if (const auto *InitList = dyn_cast<InitListExpr>(Init)) {
1768 if (Index < InitList->getNumInits()) {
1769 if (const Expr *FieldInit = InitList->getInit(Index))
1770 if (Optional<SVal> V = svalBuilder.getConstantVal(FieldInit))
1773 return svalBuilder.makeZeroVal(Ty);
1778 return getBindingForFieldOrElementCommon(B, R, Ty);
1782 RegionStoreManager::getBindingForDerivedDefaultValue(RegionBindingsConstRef B,
1783 const MemRegion *superR,
1784 const TypedValueRegion *R,
1787 if (const Optional<SVal> &D = B.getDefaultBinding(superR)) {
1788 const SVal &val = D.getValue();
1789 if (SymbolRef parentSym = val.getAsSymbol())
1790 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1792 if (val.isZeroConstant())
1793 return svalBuilder.makeZeroVal(Ty);
1795 if (val.isUnknownOrUndef())
1798 // Lazy bindings are usually handled through getExistingLazyBinding().
1799 // We should unify these two code paths at some point.
1800 if (val.getAs<nonloc::LazyCompoundVal>() ||
1801 val.getAs<nonloc::CompoundVal>())
1804 llvm_unreachable("Unknown default value");
1810 SVal RegionStoreManager::getLazyBinding(const SubRegion *LazyBindingRegion,
1811 RegionBindingsRef LazyBinding) {
1813 if (const ElementRegion *ER = dyn_cast<ElementRegion>(LazyBindingRegion))
1814 Result = getBindingForElement(LazyBinding, ER);
1816 Result = getBindingForField(LazyBinding,
1817 cast<FieldRegion>(LazyBindingRegion));
1819 // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
1820 // default value for /part/ of an aggregate from a default value for the
1821 // /entire/ aggregate. The most common case of this is when struct Outer
1822 // has as its first member a struct Inner, which is copied in from a stack
1823 // variable. In this case, even if the Outer's default value is symbolic, 0,
1824 // or unknown, it gets overridden by the Inner's default value of undefined.
1826 // This is a general problem -- if the Inner is zero-initialized, the Outer
1827 // will now look zero-initialized. The proper way to solve this is with a
1828 // new version of RegionStore that tracks the extent of a binding as well
1831 // This hack only takes care of the undefined case because that can very
1832 // quickly result in a warning.
1833 if (Result.isUndef())
1834 Result = UnknownVal();
1840 RegionStoreManager::getBindingForFieldOrElementCommon(RegionBindingsConstRef B,
1841 const TypedValueRegion *R,
1844 // At this point we have already checked in either getBindingForElement or
1845 // getBindingForField if 'R' has a direct binding.
1848 Store lazyBindingStore = nullptr;
1849 const SubRegion *lazyBindingRegion = nullptr;
1850 std::tie(lazyBindingStore, lazyBindingRegion) = findLazyBinding(B, R, R);
1851 if (lazyBindingRegion)
1852 return getLazyBinding(lazyBindingRegion,
1853 getRegionBindings(lazyBindingStore));
1855 // Record whether or not we see a symbolic index. That can completely
1856 // be out of scope of our lookup.
1857 bool hasSymbolicIndex = false;
1859 // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
1860 // default value for /part/ of an aggregate from a default value for the
1861 // /entire/ aggregate. The most common case of this is when struct Outer
1862 // has as its first member a struct Inner, which is copied in from a stack
1863 // variable. In this case, even if the Outer's default value is symbolic, 0,
1864 // or unknown, it gets overridden by the Inner's default value of undefined.
1866 // This is a general problem -- if the Inner is zero-initialized, the Outer
1867 // will now look zero-initialized. The proper way to solve this is with a
1868 // new version of RegionStore that tracks the extent of a binding as well
1871 // This hack only takes care of the undefined case because that can very
1872 // quickly result in a warning.
1873 bool hasPartialLazyBinding = false;
1875 const SubRegion *SR = R;
1877 const MemRegion *Base = SR->getSuperRegion();
1878 if (Optional<SVal> D = getBindingForDerivedDefaultValue(B, Base, R, Ty)) {
1879 if (D->getAs<nonloc::LazyCompoundVal>()) {
1880 hasPartialLazyBinding = true;
1887 if (const ElementRegion *ER = dyn_cast<ElementRegion>(Base)) {
1888 NonLoc index = ER->getIndex();
1889 if (!index.isConstant())
1890 hasSymbolicIndex = true;
1893 // If our super region is a field or element itself, walk up the region
1894 // hierarchy to see if there is a default value installed in an ancestor.
1895 SR = dyn_cast<SubRegion>(Base);
1898 if (R->hasStackNonParametersStorage()) {
1899 if (isa<ElementRegion>(R)) {
1900 // Currently we don't reason specially about Clang-style vectors. Check
1901 // if superR is a vector and if so return Unknown.
1902 if (const TypedValueRegion *typedSuperR =
1903 dyn_cast<TypedValueRegion>(R->getSuperRegion())) {
1904 if (typedSuperR->getValueType()->isVectorType())
1905 return UnknownVal();
1909 // FIXME: We also need to take ElementRegions with symbolic indexes into
1910 // account. This case handles both directly accessing an ElementRegion
1911 // with a symbolic offset, but also fields within an element with
1912 // a symbolic offset.
1913 if (hasSymbolicIndex)
1914 return UnknownVal();
1916 if (!hasPartialLazyBinding)
1917 return UndefinedVal();
1920 // All other values are symbolic.
1921 return svalBuilder.getRegionValueSymbolVal(R);
1924 SVal RegionStoreManager::getBindingForObjCIvar(RegionBindingsConstRef B,
1925 const ObjCIvarRegion* R) {
1926 // Check if the region has a binding.
1927 if (const Optional<SVal> &V = B.getDirectBinding(R))
1930 const MemRegion *superR = R->getSuperRegion();
1932 // Check if the super region has a default binding.
1933 if (const Optional<SVal> &V = B.getDefaultBinding(superR)) {
1934 if (SymbolRef parentSym = V->getAsSymbol())
1935 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
1937 // Other cases: give up.
1938 return UnknownVal();
1941 return getBindingForLazySymbol(R);
1944 SVal RegionStoreManager::getBindingForVar(RegionBindingsConstRef B,
1945 const VarRegion *R) {
1947 // Check if the region has a binding.
1948 if (Optional<SVal> V = B.getDirectBinding(R))
1951 if (Optional<SVal> V = B.getDefaultBinding(R))
1954 // Lazily derive a value for the VarRegion.
1955 const VarDecl *VD = R->getDecl();
1956 const MemSpaceRegion *MS = R->getMemorySpace();
1958 // Arguments are always symbolic.
1959 if (isa<StackArgumentsSpaceRegion>(MS))
1960 return svalBuilder.getRegionValueSymbolVal(R);
1962 // Is 'VD' declared constant? If so, retrieve the constant value.
1963 if (VD->getType().isConstQualified()) {
1964 if (const Expr *Init = VD->getAnyInitializer()) {
1965 if (Optional<SVal> V = svalBuilder.getConstantVal(Init))
1968 // If the variable is const qualified and has an initializer but
1969 // we couldn't evaluate initializer to a value, treat the value as
1971 return UnknownVal();
1975 // This must come after the check for constants because closure-captured
1976 // constant variables may appear in UnknownSpaceRegion.
1977 if (isa<UnknownSpaceRegion>(MS))
1978 return svalBuilder.getRegionValueSymbolVal(R);
1980 if (isa<GlobalsSpaceRegion>(MS)) {
1981 QualType T = VD->getType();
1983 // Function-scoped static variables are default-initialized to 0; if they
1984 // have an initializer, it would have been processed by now.
1985 // FIXME: This is only true when we're starting analysis from main().
1986 // We're losing a lot of coverage here.
1987 if (isa<StaticGlobalSpaceRegion>(MS))
1988 return svalBuilder.makeZeroVal(T);
1990 if (Optional<SVal> V = getBindingForDerivedDefaultValue(B, MS, R, T)) {
1991 assert(!V->getAs<nonloc::LazyCompoundVal>());
1992 return V.getValue();
1995 return svalBuilder.getRegionValueSymbolVal(R);
1998 return UndefinedVal();
2001 SVal RegionStoreManager::getBindingForLazySymbol(const TypedValueRegion *R) {
2002 // All other values are symbolic.
2003 return svalBuilder.getRegionValueSymbolVal(R);
2006 const RegionStoreManager::SValListTy &
2007 RegionStoreManager::getInterestingValues(nonloc::LazyCompoundVal LCV) {
2008 // First, check the cache.
2009 LazyBindingsMapTy::iterator I = LazyBindingsMap.find(LCV.getCVData());
2010 if (I != LazyBindingsMap.end())
2013 // If we don't have a list of values cached, start constructing it.
2016 const SubRegion *LazyR = LCV.getRegion();
2017 RegionBindingsRef B = getRegionBindings(LCV.getStore());
2019 // If this region had /no/ bindings at the time, there are no interesting
2020 // values to return.
2021 const ClusterBindings *Cluster = B.lookup(LazyR->getBaseRegion());
2023 return (LazyBindingsMap[LCV.getCVData()] = std::move(List));
2025 SmallVector<BindingPair, 32> Bindings;
2026 collectSubRegionBindings(Bindings, svalBuilder, *Cluster, LazyR,
2027 /*IncludeAllDefaultBindings=*/true);
2028 for (SmallVectorImpl<BindingPair>::const_iterator I = Bindings.begin(),
2032 if (V.isUnknownOrUndef() || V.isConstant())
2035 if (Optional<nonloc::LazyCompoundVal> InnerLCV =
2036 V.getAs<nonloc::LazyCompoundVal>()) {
2037 const SValListTy &InnerList = getInterestingValues(*InnerLCV);
2038 List.insert(List.end(), InnerList.begin(), InnerList.end());
2045 return (LazyBindingsMap[LCV.getCVData()] = std::move(List));
2048 NonLoc RegionStoreManager::createLazyBinding(RegionBindingsConstRef B,
2049 const TypedValueRegion *R) {
2050 if (Optional<nonloc::LazyCompoundVal> V =
2051 getExistingLazyBinding(svalBuilder, B, R, false))
2054 return svalBuilder.makeLazyCompoundVal(StoreRef(B.asStore(), *this), R);
2057 static bool isRecordEmpty(const RecordDecl *RD) {
2058 if (!RD->field_empty())
2060 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD))
2061 return CRD->getNumBases() == 0;
2065 SVal RegionStoreManager::getBindingForStruct(RegionBindingsConstRef B,
2066 const TypedValueRegion *R) {
2067 const RecordDecl *RD = R->getValueType()->castAs<RecordType>()->getDecl();
2068 if (!RD->getDefinition() || isRecordEmpty(RD))
2069 return UnknownVal();
2071 return createLazyBinding(B, R);
2074 SVal RegionStoreManager::getBindingForArray(RegionBindingsConstRef B,
2075 const TypedValueRegion *R) {
2076 assert(Ctx.getAsConstantArrayType(R->getValueType()) &&
2077 "Only constant array types can have compound bindings.");
2079 return createLazyBinding(B, R);
2082 bool RegionStoreManager::includedInBindings(Store store,
2083 const MemRegion *region) const {
2084 RegionBindingsRef B = getRegionBindings(store);
2085 region = region->getBaseRegion();
2087 // Quick path: if the base is the head of a cluster, the region is live.
2088 if (B.lookup(region))
2091 // Slow path: if the region is the VALUE of any binding, it is live.
2092 for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
2093 const ClusterBindings &Cluster = RI.getData();
2094 for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
2096 const SVal &D = CI.getData();
2097 if (const MemRegion *R = D.getAsRegion())
2098 if (R->getBaseRegion() == region)
2106 //===----------------------------------------------------------------------===//
2107 // Binding values to regions.
2108 //===----------------------------------------------------------------------===//
2110 StoreRef RegionStoreManager::killBinding(Store ST, Loc L) {
2111 if (Optional<loc::MemRegionVal> LV = L.getAs<loc::MemRegionVal>())
2112 if (const MemRegion* R = LV->getRegion())
2113 return StoreRef(getRegionBindings(ST).removeBinding(R)
2115 .getRootWithoutRetain(),
2118 return StoreRef(ST, *this);
2122 RegionStoreManager::bind(RegionBindingsConstRef B, Loc L, SVal V) {
2123 if (L.getAs<loc::ConcreteInt>())
2126 // If we get here, the location should be a region.
2127 const MemRegion *R = L.castAs<loc::MemRegionVal>().getRegion();
2129 // Check if the region is a struct region.
2130 if (const TypedValueRegion* TR = dyn_cast<TypedValueRegion>(R)) {
2131 QualType Ty = TR->getValueType();
2132 if (Ty->isArrayType())
2133 return bindArray(B, TR, V);
2134 if (Ty->isStructureOrClassType())
2135 return bindStruct(B, TR, V);
2136 if (Ty->isVectorType())
2137 return bindVector(B, TR, V);
2138 if (Ty->isUnionType())
2139 return bindAggregate(B, TR, V);
2142 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
2143 // Binding directly to a symbolic region should be treated as binding
2145 QualType T = SR->getSymbol()->getType();
2146 if (T->isAnyPointerType() || T->isReferenceType())
2147 T = T->getPointeeType();
2149 R = GetElementZeroRegion(SR, T);
2152 assert((!isa<CXXThisRegion>(R) || !B.lookup(R)) &&
2153 "'this' pointer is not an l-value and is not assignable");
2155 // Clear out bindings that may overlap with this binding.
2156 RegionBindingsRef NewB = removeSubRegionBindings(B, cast<SubRegion>(R));
2157 return NewB.addBinding(BindingKey::Make(R, BindingKey::Direct), V);
2161 RegionStoreManager::setImplicitDefaultValue(RegionBindingsConstRef B,
2166 if (Loc::isLocType(T))
2167 V = svalBuilder.makeNull();
2168 else if (T->isIntegralOrEnumerationType())
2169 V = svalBuilder.makeZeroVal(T);
2170 else if (T->isStructureOrClassType() || T->isArrayType()) {
2171 // Set the default value to a zero constant when it is a structure
2172 // or array. The type doesn't really matter.
2173 V = svalBuilder.makeZeroVal(Ctx.IntTy);
2176 // We can't represent values of this type, but we still need to set a value
2177 // to record that the region has been initialized.
2178 // If this assertion ever fires, a new case should be added above -- we
2179 // should know how to default-initialize any value we can symbolicate.
2180 assert(!SymbolManager::canSymbolicate(T) && "This type is representable");
2184 return B.addBinding(R, BindingKey::Default, V);
2188 RegionStoreManager::bindArray(RegionBindingsConstRef B,
2189 const TypedValueRegion* R,
2192 const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
2193 QualType ElementTy = AT->getElementType();
2194 Optional<uint64_t> Size;
2196 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
2197 Size = CAT->getSize().getZExtValue();
2199 // Check if the init expr is a literal. If so, bind the rvalue instead.
2200 // FIXME: It's not responsibility of the Store to transform this lvalue
2201 // to rvalue. ExprEngine or maybe even CFG should do this before binding.
2202 if (Optional<loc::MemRegionVal> MRV = Init.getAs<loc::MemRegionVal>()) {
2203 SVal V = getBinding(B.asStore(), *MRV, R->getValueType());
2204 return bindAggregate(B, R, V);
2207 // Handle lazy compound values.
2208 if (Init.getAs<nonloc::LazyCompoundVal>())
2209 return bindAggregate(B, R, Init);
2211 if (Init.isUnknown())
2212 return bindAggregate(B, R, UnknownVal());
2214 // Remaining case: explicit compound values.
2215 const nonloc::CompoundVal& CV = Init.castAs<nonloc::CompoundVal>();
2216 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
2219 RegionBindingsRef NewB(B);
2221 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
2222 // The init list might be shorter than the array length.
2226 const NonLoc &Idx = svalBuilder.makeArrayIndex(i);
2227 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
2229 if (ElementTy->isStructureOrClassType())
2230 NewB = bindStruct(NewB, ER, *VI);
2231 else if (ElementTy->isArrayType())
2232 NewB = bindArray(NewB, ER, *VI);
2234 NewB = bind(NewB, loc::MemRegionVal(ER), *VI);
2237 // If the init list is shorter than the array length (or the array has
2238 // variable length), set the array default value. Values that are already set
2239 // are not overwritten.
2240 if (!Size.hasValue() || i < Size.getValue())
2241 NewB = setImplicitDefaultValue(NewB, R, ElementTy);
2246 RegionBindingsRef RegionStoreManager::bindVector(RegionBindingsConstRef B,
2247 const TypedValueRegion* R,
2249 QualType T = R->getValueType();
2250 assert(T->isVectorType());
2251 const VectorType *VT = T->getAs<VectorType>(); // Use getAs for typedefs.
2253 // Handle lazy compound values and symbolic values.
2254 if (V.getAs<nonloc::LazyCompoundVal>() || V.getAs<nonloc::SymbolVal>())
2255 return bindAggregate(B, R, V);
2257 // We may get non-CompoundVal accidentally due to imprecise cast logic or
2258 // that we are binding symbolic struct value. Kill the field values, and if
2259 // the value is symbolic go and bind it as a "default" binding.
2260 if (!V.getAs<nonloc::CompoundVal>()) {
2261 return bindAggregate(B, R, UnknownVal());
2264 QualType ElemType = VT->getElementType();
2265 nonloc::CompoundVal CV = V.castAs<nonloc::CompoundVal>();
2266 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
2267 unsigned index = 0, numElements = VT->getNumElements();
2268 RegionBindingsRef NewB(B);
2270 for ( ; index != numElements ; ++index) {
2274 NonLoc Idx = svalBuilder.makeArrayIndex(index);
2275 const ElementRegion *ER = MRMgr.getElementRegion(ElemType, Idx, R, Ctx);
2277 if (ElemType->isArrayType())
2278 NewB = bindArray(NewB, ER, *VI);
2279 else if (ElemType->isStructureOrClassType())
2280 NewB = bindStruct(NewB, ER, *VI);
2282 NewB = bind(NewB, loc::MemRegionVal(ER), *VI);
2287 Optional<RegionBindingsRef>
2288 RegionStoreManager::tryBindSmallStruct(RegionBindingsConstRef B,
2289 const TypedValueRegion *R,
2290 const RecordDecl *RD,
2291 nonloc::LazyCompoundVal LCV) {
2294 if (const CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(RD))
2295 if (Class->getNumBases() != 0 || Class->getNumVBases() != 0)
2298 for (const auto *FD : RD->fields()) {
2299 if (FD->isUnnamedBitfield())
2302 // If there are too many fields, or if any of the fields are aggregates,
2303 // just use the LCV as a default binding.
2304 if (Fields.size() == SmallStructLimit)
2307 QualType Ty = FD->getType();
2308 if (!(Ty->isScalarType() || Ty->isReferenceType()))
2311 Fields.push_back(FD);
2314 RegionBindingsRef NewB = B;
2316 for (FieldVector::iterator I = Fields.begin(), E = Fields.end(); I != E; ++I){
2317 const FieldRegion *SourceFR = MRMgr.getFieldRegion(*I, LCV.getRegion());
2318 SVal V = getBindingForField(getRegionBindings(LCV.getStore()), SourceFR);
2320 const FieldRegion *DestFR = MRMgr.getFieldRegion(*I, R);
2321 NewB = bind(NewB, loc::MemRegionVal(DestFR), V);
2327 RegionBindingsRef RegionStoreManager::bindStruct(RegionBindingsConstRef B,
2328 const TypedValueRegion* R,
2330 if (!Features.supportsFields())
2333 QualType T = R->getValueType();
2334 assert(T->isStructureOrClassType());
2336 const RecordType* RT = T->getAs<RecordType>();
2337 const RecordDecl *RD = RT->getDecl();
2339 if (!RD->isCompleteDefinition())
2342 // Handle lazy compound values and symbolic values.
2343 if (Optional<nonloc::LazyCompoundVal> LCV =
2344 V.getAs<nonloc::LazyCompoundVal>()) {
2345 if (Optional<RegionBindingsRef> NewB = tryBindSmallStruct(B, R, RD, *LCV))
2347 return bindAggregate(B, R, V);
2349 if (V.getAs<nonloc::SymbolVal>())
2350 return bindAggregate(B, R, V);
2352 // We may get non-CompoundVal accidentally due to imprecise cast logic or
2353 // that we are binding symbolic struct value. Kill the field values, and if
2354 // the value is symbolic go and bind it as a "default" binding.
2355 if (V.isUnknown() || !V.getAs<nonloc::CompoundVal>())
2356 return bindAggregate(B, R, UnknownVal());
2358 // The raw CompoundVal is essentially a symbolic InitListExpr: an (immutable)
2359 // list of other values. It appears pretty much only when there's an actual
2360 // initializer list expression in the program, and the analyzer tries to
2361 // unwrap it as soon as possible.
2362 // This code is where such unwrap happens: when the compound value is put into
2363 // the object that it was supposed to initialize (it's an *initializer* list,
2364 // after all), instead of binding the whole value to the whole object, we bind
2365 // sub-values to sub-objects. Sub-values may themselves be compound values,
2366 // and in this case the procedure becomes recursive.
2367 // FIXME: The annoying part about compound values is that they don't carry
2368 // any sort of information about which value corresponds to which sub-object.
2369 // It's simply a list of values in the middle of nowhere; we expect to match
2370 // them to sub-objects, essentially, "by index": first value binds to
2371 // the first field, second value binds to the second field, etc.
2372 // It would have been much safer to organize non-lazy compound values as
2373 // a mapping from fields/bases to values.
2374 const nonloc::CompoundVal& CV = V.castAs<nonloc::CompoundVal>();
2375 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
2377 RegionBindingsRef NewB(B);
2379 // In C++17 aggregates may have base classes, handle those as well.
2380 // They appear before fields in the initializer list / compound value.
2381 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
2382 // If the object was constructed with a constructor, its value is a
2383 // LazyCompoundVal. If it's a raw CompoundVal, it means that we're
2384 // performing aggregate initialization. The only exception from this
2385 // rule is sending an Objective-C++ message that returns a C++ object
2386 // to a nil receiver; in this case the semantics is to return a
2387 // zero-initialized object even if it's a C++ object that doesn't have
2388 // this sort of constructor; the CompoundVal is empty in this case.
2389 assert((CRD->isAggregate() || (Ctx.getLangOpts().ObjC && VI == VE)) &&
2390 "Non-aggregates are constructed with a constructor!");
2392 for (const auto &B : CRD->bases()) {
2393 // (Multiple inheritance is fine though.)
2394 assert(!B.isVirtual() && "Aggregates cannot have virtual base classes!");
2399 QualType BTy = B.getType();
2400 assert(BTy->isStructureOrClassType() && "Base classes must be classes!");
2402 const CXXRecordDecl *BRD = BTy->getAsCXXRecordDecl();
2403 assert(BRD && "Base classes must be C++ classes!");
2405 const CXXBaseObjectRegion *BR =
2406 MRMgr.getCXXBaseObjectRegion(BRD, R, /*IsVirtual=*/false);
2408 NewB = bindStruct(NewB, BR, *VI);
2414 RecordDecl::field_iterator FI, FE;
2416 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI) {
2421 // Skip any unnamed bitfields to stay in sync with the initializers.
2422 if (FI->isUnnamedBitfield())
2425 QualType FTy = FI->getType();
2426 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
2428 if (FTy->isArrayType())
2429 NewB = bindArray(NewB, FR, *VI);
2430 else if (FTy->isStructureOrClassType())
2431 NewB = bindStruct(NewB, FR, *VI);
2433 NewB = bind(NewB, loc::MemRegionVal(FR), *VI);
2437 // There may be fewer values in the initialize list than the fields of struct.
2439 NewB = NewB.addBinding(R, BindingKey::Default,
2440 svalBuilder.makeIntVal(0, false));
2447 RegionStoreManager::bindAggregate(RegionBindingsConstRef B,
2448 const TypedRegion *R,
2450 // Remove the old bindings, using 'R' as the root of all regions
2451 // we will invalidate. Then add the new binding.
2452 return removeSubRegionBindings(B, R).addBinding(R, BindingKey::Default, Val);
2455 //===----------------------------------------------------------------------===//
2457 //===----------------------------------------------------------------------===//
2460 class RemoveDeadBindingsWorker
2461 : public ClusterAnalysis<RemoveDeadBindingsWorker> {
2462 SmallVector<const SymbolicRegion *, 12> Postponed;
2463 SymbolReaper &SymReaper;
2464 const StackFrameContext *CurrentLCtx;
2467 RemoveDeadBindingsWorker(RegionStoreManager &rm,
2468 ProgramStateManager &stateMgr,
2469 RegionBindingsRef b, SymbolReaper &symReaper,
2470 const StackFrameContext *LCtx)
2471 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
2472 SymReaper(symReaper), CurrentLCtx(LCtx) {}
2474 // Called by ClusterAnalysis.
2475 void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C);
2476 void VisitCluster(const MemRegion *baseR, const ClusterBindings *C);
2477 using ClusterAnalysis<RemoveDeadBindingsWorker>::VisitCluster;
2479 using ClusterAnalysis::AddToWorkList;
2481 bool AddToWorkList(const MemRegion *R);
2483 bool UpdatePostponed();
2484 void VisitBinding(SVal V);
2488 bool RemoveDeadBindingsWorker::AddToWorkList(const MemRegion *R) {
2489 const MemRegion *BaseR = R->getBaseRegion();
2490 return AddToWorkList(WorkListElement(BaseR), getCluster(BaseR));
2493 void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
2494 const ClusterBindings &C) {
2496 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
2497 if (SymReaper.isLive(VR))
2498 AddToWorkList(baseR, &C);
2503 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
2504 if (SymReaper.isLive(SR->getSymbol()))
2505 AddToWorkList(SR, &C);
2507 Postponed.push_back(SR);
2512 if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
2513 AddToWorkList(baseR, &C);
2517 // CXXThisRegion in the current or parent location context is live.
2518 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
2519 const auto *StackReg =
2520 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
2521 const StackFrameContext *RegCtx = StackReg->getStackFrame();
2523 (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx)))
2524 AddToWorkList(TR, &C);
2528 void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
2529 const ClusterBindings *C) {
2533 // Mark the symbol for any SymbolicRegion with live bindings as live itself.
2534 // This means we should continue to track that symbol.
2535 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(baseR))
2536 SymReaper.markLive(SymR->getSymbol());
2538 for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E; ++I) {
2539 // Element index of a binding key is live.
2540 SymReaper.markElementIndicesLive(I.getKey().getRegion());
2542 VisitBinding(I.getData());
2546 void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
2547 // Is it a LazyCompoundVal? All referenced regions are live as well.
2548 if (Optional<nonloc::LazyCompoundVal> LCS =
2549 V.getAs<nonloc::LazyCompoundVal>()) {
2551 const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS);
2553 for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(),
2561 // If V is a region, then add it to the worklist.
2562 if (const MemRegion *R = V.getAsRegion()) {
2564 SymReaper.markLive(R);
2566 // All regions captured by a block are also live.
2567 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
2568 BlockDataRegion::referenced_vars_iterator I = BR->referenced_vars_begin(),
2569 E = BR->referenced_vars_end();
2570 for ( ; I != E; ++I)
2571 AddToWorkList(I.getCapturedRegion());
2576 // Update the set of live symbols.
2577 for (auto SI = V.symbol_begin(), SE = V.symbol_end(); SI!=SE; ++SI)
2578 SymReaper.markLive(*SI);
2581 bool RemoveDeadBindingsWorker::UpdatePostponed() {
2582 // See if any postponed SymbolicRegions are actually live now, after
2583 // having done a scan.
2584 bool Changed = false;
2586 for (auto I = Postponed.begin(), E = Postponed.end(); I != E; ++I) {
2587 if (const SymbolicRegion *SR = *I) {
2588 if (SymReaper.isLive(SR->getSymbol())) {
2589 Changed |= AddToWorkList(SR);
2598 StoreRef RegionStoreManager::removeDeadBindings(Store store,
2599 const StackFrameContext *LCtx,
2600 SymbolReaper& SymReaper) {
2601 RegionBindingsRef B = getRegionBindings(store);
2602 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
2603 W.GenerateClusters();
2605 // Enqueue the region roots onto the worklist.
2606 for (SymbolReaper::region_iterator I = SymReaper.region_begin(),
2607 E = SymReaper.region_end(); I != E; ++I) {
2608 W.AddToWorkList(*I);
2611 do W.RunWorkList(); while (W.UpdatePostponed());
2613 // We have now scanned the store, marking reachable regions and symbols
2614 // as live. We now remove all the regions that are dead from the store
2615 // as well as update DSymbols with the set symbols that are now dead.
2616 for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) {
2617 const MemRegion *Base = I.getKey();
2619 // If the cluster has been visited, we know the region has been marked.
2620 // Otherwise, remove the dead entry.
2621 if (!W.isVisited(Base))
2625 return StoreRef(B.asStore(), *this);
2628 //===----------------------------------------------------------------------===//
2630 //===----------------------------------------------------------------------===//
2632 void RegionStoreManager::printJson(raw_ostream &Out, Store S, const char *NL,
2633 unsigned int Space, bool IsDot) const {
2634 RegionBindingsRef Bindings = getRegionBindings(S);
2636 Indent(Out, Space, IsDot) << "\"store\": ";
2638 if (Bindings.isEmpty()) {
2639 Out << "null," << NL;
2643 Out << "{ \"pointer\": \"" << Bindings.asStore() << "\", \"items\": [" << NL;
2644 Bindings.printJson(Out, NL, Space + 1, IsDot);
2645 Indent(Out, Space, IsDot) << "]}," << NL;