RegionStore.cpp revision 697a68590a75f5cd2326c8f686a6c666b51688b6
1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==// 2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// The LLVM Compiler Infrastructure 4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// License. See LICENSE.TXT for details. 7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 109ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// This file defines a basic region store model. In this model, we do have field 115e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)// sensitivity. But we assume nothing about the heap shape. So recursive data 125e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)// structures are largely ignored. Basically we do 1-limiting analysis. 13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Parameter pointers are assumed with no aliasing. Pointee objects of 14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// parameters are created lazily. 15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===----------------------------------------------------------------------===// 17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "clang/AST/Attr.h" 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "clang/AST/CharUnits.h" 19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "clang/Analysis/Analyses/LiveVariables.h" 20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "clang/Analysis/AnalysisContext.h" 21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "clang/Basic/TargetInfo.h" 22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" 26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "llvm/ADT/ImmutableList.h" 27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "llvm/ADT/ImmutableMap.h" 28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "llvm/ADT/Optional.h" 29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "llvm/Support/raw_ostream.h" 30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using namespace clang; 32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using namespace ento; 33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using llvm::Optional; 34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Representation of binding keys. 37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace { 40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class BindingKey { 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)public: 42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) enum Kind { Default = 0x0, Direct = 0x1 }; 43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)private: 44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) enum { Symbolic = 0x2 }; 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) llvm::PointerIntPair<const MemRegion *, 2> P; 47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) uint64_t Data; 48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) explicit BindingKey(const MemRegion *r, const MemRegion *Base, Kind k) 50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : P(r, k | Symbolic), Data(reinterpret_cast<uintptr_t>(Base)) { 51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(r && Base && "Must have known regions."); 52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(getConcreteOffsetRegion() == Base && "Failed to store base region"); 53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k) 55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : P(r, k), Data(offset) { 56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(r && "Must have known regions."); 57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(getOffset() == offset && "Failed to store offset"); 58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert((r == r->getBaseRegion() || isa<ObjCIvarRegion>(r)) && "Not a base"); 59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)public: 61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool isDirect() const { return P.getInt() & Direct; } 63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool hasSymbolicOffset() const { return P.getInt() & Symbolic; } 64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *getRegion() const { return P.getPointer(); } 66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) uint64_t getOffset() const { 67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(!hasSymbolicOffset()); 68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return Data; 69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *getConcreteOffsetRegion() const { 72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(hasSymbolicOffset()); 73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return reinterpret_cast<const MemRegion *>(static_cast<uintptr_t>(Data)); 74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *getBaseRegion() const { 77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (hasSymbolicOffset()) 78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return getConcreteOffsetRegion()->getBaseRegion(); 79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return getRegion()->getBaseRegion(); 80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void Profile(llvm::FoldingSetNodeID& ID) const { 83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ID.AddPointer(P.getOpaqueValue()); 84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ID.AddInteger(Data); 85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static BindingKey Make(const MemRegion *R, Kind k); 88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool operator<(const BindingKey &X) const { 90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (P.getOpaqueValue() < X.P.getOpaqueValue()) 91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return true; 92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (P.getOpaqueValue() > X.P.getOpaqueValue()) 93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return false; 94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return Data < X.Data; 95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool operator==(const BindingKey &X) const { 98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return P.getOpaqueValue() == X.P.getOpaqueValue() && 99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Data == X.Data; 100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) LLVM_ATTRIBUTE_USED void dump() const; 103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} // end anonymous namespace 105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)BindingKey BindingKey::Make(const MemRegion *R, Kind k) { 107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const RegionOffset &RO = R->getAsOffset(); 108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (RO.hasSymbolicOffset()) 109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return BindingKey(R, RO.getRegion(), k); 110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return BindingKey(RO.getRegion(), RO.getOffset(), k); 112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace llvm { 115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static inline 116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) raw_ostream &operator<<(raw_ostream &os, BindingKey K) { 117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) os << '(' << K.getRegion(); 118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!K.hasSymbolicOffset()) 119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) os << ',' << K.getOffset(); 120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) os << ',' << (K.isDirect() ? "direct" : "default") 121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) << ')'; 122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return os; 123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} // end llvm namespace 125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void BindingKey::dump() const { 127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) llvm::errs() << *this; 128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Actual Store type. 132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef llvm::ImmutableMap<BindingKey, SVal> ClusterBindings; 135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef llvm::ImmutableMapRef<BindingKey, SVal> ClusterBindingsRef; 136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef llvm::ImmutableMap<const MemRegion *, ClusterBindings> 138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindings; 139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace { 141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class RegionBindingsRef : public llvm::ImmutableMapRef<const MemRegion *, 142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ClusterBindings> { 143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ClusterBindings::Factory &CBFactory; 144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)public: 145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef llvm::ImmutableMapRef<const MemRegion *, ClusterBindings> 146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ParentTy; 147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef(ClusterBindings::Factory &CBFactory, 149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const RegionBindings::TreeTy *T, 150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindings::TreeTy::Factory *F) 151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(T, F), 152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CBFactory(CBFactory) {} 153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef(const ParentTy &P, ClusterBindings::Factory &CBFactory) 155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(P), 156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CBFactory(CBFactory) {} 157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef add(key_type_ref K, data_type_ref D) const { 159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return RegionBindingsRef(static_cast<const ParentTy*>(this)->add(K, D), 160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CBFactory); 161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef remove(key_type_ref K) const { 164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return RegionBindingsRef(static_cast<const ParentTy*>(this)->remove(K), 165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CBFactory); 166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef addBinding(BindingKey K, SVal V) const; 169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef addBinding(const MemRegion *R, 171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) BindingKey::Kind k, SVal V) const; 172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef &operator=(const RegionBindingsRef &X) { 174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) *static_cast<ParentTy*>(this) = X; 175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return *this; 176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 177868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const SVal *lookup(BindingKey K) const; 179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const SVal *lookup(const MemRegion *R, BindingKey::Kind k) const; 180868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ClusterBindings *lookup(const MemRegion *R) const { 181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return static_cast<const ParentTy*>(this)->lookup(R); 182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef removeBinding(BindingKey K); 185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 186868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef removeBinding(const MemRegion *R, 187868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) BindingKey::Kind k); 188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 189868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef removeBinding(const MemRegion *R) { 190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return removeBinding(R, BindingKey::Direct). 191868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) removeBinding(R, BindingKey::Default); 192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 193868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Optional<SVal> getDirectBinding(const MemRegion *R) const; 195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 196868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// getDefaultBinding - Returns an SVal* representing an optional default 197868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// binding associated with a region and its subregions. 198868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Optional<SVal> getDefaultBinding(const MemRegion *R) const; 199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// Return the internal tree as a Store. 201868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Store asStore() const { 202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return asImmutableMap().getRootWithoutRetain(); 203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void dump(raw_ostream &OS, const char *nl) const { 206868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (iterator I = begin(), E = end(); I != E; ++I) { 207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ClusterBindings &Cluster = I.getData(); 208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 209868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CI != CE; ++CI) { 210868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) OS << ' ' << CI.getKey() << " : " << CI.getData() << nl; 211868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 212868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) OS << nl; 213868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 214868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 215868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 216868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) LLVM_ATTRIBUTE_USED void dump() const { 217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) dump(llvm::errs(), "\n"); 218868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 219868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 220868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} // end anonymous namespace 221868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 222868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef const RegionBindingsRef& RegionBindingsConstRef; 223868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 224868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)Optional<SVal> RegionBindingsRef::getDirectBinding(const MemRegion *R) const { 225868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (const SVal *V = lookup(R, BindingKey::Direct)) 226868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return *V; 227868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return Optional<SVal>(); 228868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 229868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 230868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)Optional<SVal> RegionBindingsRef::getDefaultBinding(const MemRegion *R) const { 231868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (R->isBoundable()) 232868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) 233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (TR->getValueType()->isUnionType()) 234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return UnknownVal(); 235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (const SVal *V = lookup(R, BindingKey::Default)) 236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return *V; 237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return Optional<SVal>(); 238868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 240868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)RegionBindingsRef RegionBindingsRef::addBinding(BindingKey K, SVal V) const { 241868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *Base = K.getBaseRegion(); 242868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 243868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ClusterBindings *ExistingCluster = lookup(Base); 244868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ClusterBindings Cluster = (ExistingCluster ? *ExistingCluster 245868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : CBFactory.getEmptyMap()); 246868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 247868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ClusterBindings NewCluster = CBFactory.add(Cluster, K, V); 248868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return add(Base, NewCluster); 249868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 250868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)RegionBindingsRef RegionBindingsRef::addBinding(const MemRegion *R, 253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) BindingKey::Kind k, 254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal V) const { 255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return addBinding(BindingKey::Make(R, k), V); 256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 257a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 258868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const SVal *RegionBindingsRef::lookup(BindingKey K) const { 259868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ClusterBindings *Cluster = lookup(K.getBaseRegion()); 260868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!Cluster) 261868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return 0; 262868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return Cluster->lookup(K); 263868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 264868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 265868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const SVal *RegionBindingsRef::lookup(const MemRegion *R, 266868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) BindingKey::Kind k) const { 267868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return lookup(BindingKey::Make(R, k)); 268868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 269868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 270868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)RegionBindingsRef RegionBindingsRef::removeBinding(BindingKey K) { 271868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *Base = K.getBaseRegion(); 272868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ClusterBindings *Cluster = lookup(Base); 273868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!Cluster) 274868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return *this; 275868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 276868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ClusterBindings NewCluster = CBFactory.remove(*Cluster, K); 277868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (NewCluster.isEmpty()) 278868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return remove(Base); 279868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return add(Base, NewCluster); 280868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 281868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 282868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)RegionBindingsRef RegionBindingsRef::removeBinding(const MemRegion *R, 283868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) BindingKey::Kind k){ 284868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return removeBinding(BindingKey::Make(R, k)); 285868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 286868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 287868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 288868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Fine-grained control of RegionStoreManager. 289868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 290868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 291868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace { 292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)struct minimal_features_tag {}; 293868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)struct maximal_features_tag {}; 294868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 295868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class RegionStoreFeatures { 296868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool SupportsFields; 297868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)public: 298868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionStoreFeatures(minimal_features_tag) : 299868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SupportsFields(false) {} 300868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 301868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionStoreFeatures(maximal_features_tag) : 302868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SupportsFields(true) {} 303868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 304868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void enableFields(bool t) { SupportsFields = t; } 305868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 306868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool supportsFields() const { return SupportsFields; } 307868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 308868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 309868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 310868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 311868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Main RegionStore logic. 312868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 313868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 314868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace { 315868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 316868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class RegionStoreManager : public StoreManager { 317868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)public: 318868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const RegionStoreFeatures Features; 319868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindings::Factory RBFactory; 320868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) mutable ClusterBindings::Factory CBFactory; 321868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 322868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionStoreManager(ProgramStateManager& mgr, const RegionStoreFeatures &f) 323868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : StoreManager(mgr), Features(f), 324868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RBFactory(mgr.getAllocator()), CBFactory(mgr.getAllocator()) {} 325868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 326868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 327868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// setImplicitDefaultValue - Set the default binding for the provided 328868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// MemRegion to the value implicitly defined for compound literals when 329868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// the value is not specified. 330868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef setImplicitDefaultValue(RegionBindingsConstRef B, 331868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *R, QualType T); 332868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 333868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// ArrayToPointer - Emulates the "decay" of an array to a pointer 334868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// type. 'Array' represents the lvalue of the array being decayed 335868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// to a pointer, and the returned SVal represents the decayed 336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// version of that lvalue (i.e., a pointer to the first element of 337868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// the array). This is called by ExprEngine when evaluating 338868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// casts from arrays to pointers. 339868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal ArrayToPointer(Loc Array); 340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StoreRef getInitialStore(const LocationContext *InitLoc) { 342868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return StoreRef(RBFactory.getEmptyMap().getRootWithoutRetain(), *this); 343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 344868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 345868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) //===-------------------------------------------------------------------===// 346868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Binding values to regions. 347868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) //===-------------------------------------------------------------------===// 348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef invalidateGlobalRegion(MemRegion::Kind K, 349868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const Expr *Ex, 350868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) unsigned Count, 351868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const LocationContext *LCtx, 352868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef B, 353868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) InvalidatedRegions *Invalidated); 354868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 355868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StoreRef invalidateRegions(Store store, ArrayRef<const MemRegion *> Regions, 356868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const Expr *E, unsigned Count, 357868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const LocationContext *LCtx, 358868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) InvalidatedSymbols &IS, 359868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const CallEvent *Call, 360868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) InvalidatedRegions *Invalidated); 361868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 362868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool scanReachableSymbols(Store S, const MemRegion *R, 363868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ScanReachableSymbols &Callbacks); 364868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 365868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef removeSubRegionBindings(RegionBindingsConstRef B, 366868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const SubRegion *R); 367868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 368868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)public: // Part of public interface to class. 369868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 370868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) virtual StoreRef Bind(Store store, Loc LV, SVal V) { 371868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return StoreRef(bind(getRegionBindings(store), LV, V).asStore(), *this); 372868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 373868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 374868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef bind(RegionBindingsConstRef B, Loc LV, SVal V); 375868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 376868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // BindDefault is only used to initialize a region with a default value. 377868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StoreRef BindDefault(Store store, const MemRegion *R, SVal V) { 378868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef B = getRegionBindings(store); 379868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(!B.lookup(R, BindingKey::Default)); 380868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(!B.lookup(R, BindingKey::Direct)); 381868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return StoreRef(B.addBinding(R, BindingKey::Default, V) 382868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) .asImmutableMap() 383868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) .getRootWithoutRetain(), *this); 384868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 385868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 386868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// \brief Create a new store that binds a value to a compound literal. 387868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// 388868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// \param ST The original store whose bindings are the basis for the new 389868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// store. 390868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// 391868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// \param CL The compound literal to bind (the binding key). 392868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// 393868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// \param LC The LocationContext for the binding. 394868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// 395868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// \param V The value to bind to the compound literal. 396868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StoreRef bindCompoundLiteral(Store ST, 397868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const CompoundLiteralExpr *CL, 398868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const LocationContext *LC, SVal V); 399868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 400868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// BindStruct - Bind a compound value to a structure. 401868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef bindStruct(RegionBindingsConstRef B, 402868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const TypedValueRegion* R, SVal V); 403868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 404868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// BindVector - Bind a compound value to a vector. 405868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef bindVector(RegionBindingsConstRef B, 406868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const TypedValueRegion* R, SVal V); 407868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 408868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef bindArray(RegionBindingsConstRef B, 409868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const TypedValueRegion* R, 410868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal V); 411868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 412868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// Clears out all bindings in the given region and assigns a new value 413868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// as a Default binding. 414868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef bindAggregate(RegionBindingsConstRef B, 415868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const TypedRegion *R, 416868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal DefaultVal); 417868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 418868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// \brief Create a new store with the specified binding removed. 419868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// \param ST the original store, that is the basis for the new store. 420868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// \param L the location whose binding should be removed. 421868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) virtual StoreRef killBinding(Store ST, Loc L); 422868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 423868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void incrementReferenceCount(Store store) { 424868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) getRegionBindings(store).manualRetain(); 425868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 426868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 427868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// If the StoreManager supports it, decrement the reference count of 428868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// the specified Store object. If the reference count hits 0, the memory 429868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// associated with the object is recycled. 430868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void decrementReferenceCount(Store store) { 431868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) getRegionBindings(store).manualRelease(); 432868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 433868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 434868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool includedInBindings(Store store, const MemRegion *region) const; 435868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 436868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// \brief Return the value bound to specified location in a given state. 437868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// 438868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// The high level logic for this method is this: 439868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// getBinding (L) 440868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// if L has binding 441868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// return L's binding 442868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// else if L is in killset 443868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// return unknown 444868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// else 445868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// if L is on stack or heap 446868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// return undefined 447868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// else 448868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// return symbolic 449868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) virtual SVal getBinding(Store S, Loc L, QualType T) { 450868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return getBinding(getRegionBindings(S), L, T); 451868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 452868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 453868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal getBinding(RegionBindingsConstRef B, Loc L, QualType T = QualType()); 454868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 455868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal getBindingForElement(RegionBindingsConstRef B, const ElementRegion *R); 456868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 457868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal getBindingForField(RegionBindingsConstRef B, const FieldRegion *R); 458868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 459868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal getBindingForObjCIvar(RegionBindingsConstRef B, const ObjCIvarRegion *R); 460868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 461868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal getBindingForVar(RegionBindingsConstRef B, const VarRegion *R); 462868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 463868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal getBindingForLazySymbol(const TypedValueRegion *R); 464868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 465868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal getBindingForFieldOrElementCommon(RegionBindingsConstRef B, 466868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const TypedValueRegion *R, 467868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) QualType Ty, 468868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *superR); 469868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 470868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal getLazyBinding(const MemRegion *LazyBindingRegion, 471868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef LazyBinding); 472868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 473868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// Get bindings for the values in a struct and return a CompoundVal, used 474868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// when doing struct copy: 475868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// struct s x, y; 476868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// x = y; 477868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// y's value is retrieved by this method. 478868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal getBindingForStruct(RegionBindingsConstRef B, const TypedValueRegion *R); 479868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal getBindingForArray(RegionBindingsConstRef B, const TypedValueRegion *R); 480868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) NonLoc createLazyBinding(RegionBindingsConstRef B, const TypedValueRegion *R); 481868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 482868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// Used to lazily generate derived symbols for bindings that are defined 483868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// implicitly by default bindings in a super region. 484868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Optional<SVal> getBindingForDerivedDefaultValue(RegionBindingsConstRef B, 485868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *superR, 486868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const TypedValueRegion *R, 487868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) QualType Ty); 488868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 489868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// Get the state and region whose binding this region R corresponds to. 490868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::pair<Store, const MemRegion*> 491868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) getLazyBinding(RegionBindingsConstRef B, const MemRegion *R, 492868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *originalRegion); 493868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 494868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) //===------------------------------------------------------------------===// 495868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // State pruning. 496868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) //===------------------------------------------------------------------===// 497868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 498868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// removeDeadBindings - Scans the RegionStore of 'state' for dead values. 499868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// It returns a new Store with these values removed. 500868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx, 501868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SymbolReaper& SymReaper); 502868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 503868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) //===------------------------------------------------------------------===// 504868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Region "extents". 505868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) //===------------------------------------------------------------------===// 506868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 507868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // FIXME: This method will soon be eliminated; see the note in Store.h. 508868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state, 509868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion* R, QualType EleTy); 510868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 511868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) //===------------------------------------------------------------------===// 512868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Utility methods. 513868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) //===------------------------------------------------------------------===// 514868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 515868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef getRegionBindings(Store store) const { 516868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return RegionBindingsRef(CBFactory, 517868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static_cast<const RegionBindings::TreeTy*>(store), 518868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RBFactory.getTreeFactory()); 519868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 520868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 521868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void print(Store store, raw_ostream &Out, const char* nl, 522868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const char *sep); 523868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 524868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void iterBindings(Store store, BindingsHandler& f) { 525868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef B = getRegionBindings(store); 526868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) { 527868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ClusterBindings &Cluster = I.getData(); 528868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 529868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CI != CE; ++CI) { 530868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const BindingKey &K = CI.getKey(); 531868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!K.isDirect()) 532868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) continue; 533868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (const SubRegion *R = dyn_cast<SubRegion>(K.getRegion())) { 534868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // FIXME: Possibly incorporate the offset? 535868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!f.HandleBinding(*this, store, R, CI.getData())) 536868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return; 537868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 538868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 539868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 540868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 541868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 542868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 543868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} // end anonymous namespace 544868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 545868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 546868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// RegionStore creation. 547868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 548868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 549868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)StoreManager *ento::CreateRegionStoreManager(ProgramStateManager& StMgr) { 550868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionStoreFeatures F = maximal_features_tag(); 551868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return new RegionStoreManager(StMgr, F); 552868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 553868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 554868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)StoreManager * 555868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)ento::CreateFieldsOnlyRegionStoreManager(ProgramStateManager &StMgr) { 556868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionStoreFeatures F = minimal_features_tag(); 557868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) F.enableFields(true); 558868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return new RegionStoreManager(StMgr, F); 559868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 560868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 561868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 562868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 563868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Region Cluster analysis. 564868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 565868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 566a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace { 567868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)template <typename DERIVED> 568868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class ClusterAnalysis { 569868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)protected: 570868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef llvm::DenseMap<const MemRegion *, const ClusterBindings *> ClusterMap; 571868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) typedef SmallVector<const MemRegion *, 10> WorkList; 572868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 573868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) llvm::SmallPtrSet<const ClusterBindings *, 16> Visited; 574868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 575868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) WorkList WL; 576a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 577868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionStoreManager &RM; 578868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASTContext &Ctx; 579868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SValBuilder &svalBuilder; 580868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 581868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef B; 582868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 583868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const bool includeGlobals; 584868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 585868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ClusterBindings *getCluster(const MemRegion *R) { 586868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return B.lookup(R); 587868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 588868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 589868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)public: 590868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ClusterAnalysis(RegionStoreManager &rm, ProgramStateManager &StateMgr, 591868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef b, const bool includeGlobals) 592868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : RM(rm), Ctx(StateMgr.getContext()), 593868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) svalBuilder(StateMgr.getSValBuilder()), 594868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) B(b), includeGlobals(includeGlobals) {} 595868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 596868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef getRegionBindings() const { return B; } 597868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 598868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool isVisited(const MemRegion *R) { 599868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return Visited.count(getCluster(R)); 600868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 601868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 602868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void GenerateClusters() { 603868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Scan the entire set of bindings and record the region clusters. 604868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); 605868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RI != RE; ++RI){ 606868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *Base = RI.getKey(); 607868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 608868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ClusterBindings &Cluster = RI.getData(); 609868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(!Cluster.isEmpty() && "Empty clusters should be removed"); 610868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static_cast<DERIVED*>(this)->VisitAddedToCluster(Base, Cluster); 611868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 612868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (includeGlobals) 613868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (isa<NonStaticGlobalSpaceRegion>(Base->getMemorySpace())) 614868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) AddToWorkList(Base, &Cluster); 615868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 616868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 617868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 618868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool AddToWorkList(const MemRegion *R, const ClusterBindings *C) { 619868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (C && !Visited.insert(C)) 620868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return false; 621868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) WL.push_back(R); 622868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return true; 623868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 624868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 625868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool AddToWorkList(const MemRegion *R) { 626868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *baseR = R->getBaseRegion(); 627868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return AddToWorkList(baseR, getCluster(baseR)); 628868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 629868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 630868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void RunWorkList() { 631868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) while (!WL.empty()) { 632868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *baseR = WL.pop_back_val(); 633868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 634868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // First visit the cluster. 635868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (const ClusterBindings *Cluster = getCluster(baseR)) 636868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static_cast<DERIVED*>(this)->VisitCluster(baseR, *Cluster); 637868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 638868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Next, visit the base region. 639868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static_cast<DERIVED*>(this)->VisitBaseRegion(baseR); 640868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 641868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 642868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 643868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)public: 644868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C) {} 645868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void VisitCluster(const MemRegion *baseR, const ClusterBindings &C) {} 646868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void VisitBaseRegion(const MemRegion *baseR) {} 647868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 648868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 649868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 650868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 651868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Binding invalidation. 652868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 653868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 654868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool RegionStoreManager::scanReachableSymbols(Store S, const MemRegion *R, 655868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ScanReachableSymbols &Callbacks) { 656868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(R == R->getBaseRegion() && "Should only be called for base regions"); 657868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef B = getRegionBindings(S); 658868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ClusterBindings *Cluster = B.lookup(R); 659868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 660868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!Cluster) 661868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return true; 662868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 663868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (ClusterBindings::iterator RI = Cluster->begin(), RE = Cluster->end(); 664868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RI != RE; ++RI) { 665868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!Callbacks.scan(RI.getData())) 666868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return false; 667868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 668868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 669868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return true; 670868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 671868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 672868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)static inline bool isUnionField(const FieldRegion *FR) { 673868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return FR->getDecl()->getParent()->isUnion(); 674868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 675868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 676868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)typedef SmallVector<const FieldDecl *, 8> FieldVector; 677868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 678868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void getSymbolicOffsetFields(BindingKey K, FieldVector &Fields) { 679868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys"); 680868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 681868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *Base = K.getConcreteOffsetRegion(); 682868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *R = K.getRegion(); 683868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 684868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) while (R != Base) { 685868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) 686868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!isUnionField(FR)) 687868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Fields.push_back(FR->getDecl()); 688868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 689868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) R = cast<SubRegion>(R)->getSuperRegion(); 690868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 691868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 692868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 693868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)static bool isCompatibleWithFields(BindingKey K, const FieldVector &Fields) { 694868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys"); 695868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 696868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (Fields.empty()) 697868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return true; 698868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 699868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) FieldVector FieldsInBindingKey; 700868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) getSymbolicOffsetFields(K, FieldsInBindingKey); 701868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 702868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ptrdiff_t Delta = FieldsInBindingKey.size() - Fields.size(); 703868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (Delta >= 0) 704868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return std::equal(FieldsInBindingKey.begin() + Delta, 705868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) FieldsInBindingKey.end(), 706868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Fields.begin()); 707868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) else 708868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return std::equal(FieldsInBindingKey.begin(), FieldsInBindingKey.end(), 709868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Fields.begin() - Delta); 710868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 711868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 712868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)RegionBindingsRef 713868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)RegionStoreManager::removeSubRegionBindings(RegionBindingsConstRef B, 714868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const SubRegion *R) { 715868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) BindingKey SRKey = BindingKey::Make(R, BindingKey::Default); 716868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *ClusterHead = SRKey.getBaseRegion(); 717868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (R == ClusterHead) { 718868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // We can remove an entire cluster's bindings all in one go. 719868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return B.remove(R); 720868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 721868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 722868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) FieldVector FieldsInSymbolicSubregions; 723868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool HasSymbolicOffset = SRKey.hasSymbolicOffset(); 724868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (HasSymbolicOffset) { 725868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) getSymbolicOffsetFields(SRKey, FieldsInSymbolicSubregions); 726868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) R = cast<SubRegion>(SRKey.getConcreteOffsetRegion()); 727868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SRKey = BindingKey::Make(R, BindingKey::Default); 728868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 729868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 730868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // This assumes the region being invalidated is char-aligned. This isn't 731868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // true for bitfields, but since bitfields have no subregions they shouldn't 732868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // be using this function anyway. 733868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) uint64_t Length = UINT64_MAX; 734868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 735868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SVal Extent = R->getExtent(svalBuilder); 736868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (nonloc::ConcreteInt *ExtentCI = dyn_cast<nonloc::ConcreteInt>(&Extent)) { 737868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const llvm::APSInt &ExtentInt = ExtentCI->getValue(); 738868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) assert(ExtentInt.isNonNegative() || ExtentInt.isUnsigned()); 739868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Extents are in bytes but region offsets are in bits. Be careful! 740868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Length = ExtentInt.getLimitedValue() * Ctx.getCharWidth(); 741868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 742868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 743868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ClusterBindings *Cluster = B.lookup(ClusterHead); 744868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!Cluster) 745868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return B; 746868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 747868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ClusterBindingsRef Result(*Cluster, CBFactory); 748868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 749868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // It is safe to iterate over the bindings as they are being changed 750868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // because they are in an ImmutableMap. 751868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (ClusterBindings::iterator I = Cluster->begin(), E = Cluster->end(); 752868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) I != E; ++I) { 753868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) BindingKey NextKey = I.getKey(); 754868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (NextKey.getRegion() == SRKey.getRegion()) { 755868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // FIXME: This doesn't catch the case where we're really invalidating a 756868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // region with a symbolic offset. Example: 757868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // R: points[i].y 758868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Next: points[0].x 759868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 760868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (NextKey.getOffset() > SRKey.getOffset() && 761868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) NextKey.getOffset() - SRKey.getOffset() < Length) { 762868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Case 1: The next binding is inside the region we're invalidating. 763868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Remove it. 764868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Result = Result.remove(NextKey); 765868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 766868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } else if (NextKey.getOffset() == SRKey.getOffset()) { 767868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Case 2: The next binding is at the same offset as the region we're 768868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // invalidating. In this case, we need to leave default bindings alone, 769868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // since they may be providing a default value for a regions beyond what 770868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // we're invalidating. 771868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // FIXME: This is probably incorrect; consider invalidating an outer 772868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // struct whose first field is bound to a LazyCompoundVal. 773868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (NextKey.isDirect()) 774868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Result = Result.remove(NextKey); 775868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 776868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 777868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } else if (NextKey.hasSymbolicOffset()) { 778868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const MemRegion *Base = NextKey.getConcreteOffsetRegion(); 779868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (R->isSubRegionOf(Base)) { 780868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Case 3: The next key is symbolic and we just changed something within 781868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // its concrete region. We don't know if the binding is still valid, so 782868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // we'll be conservative and remove it. 783868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (NextKey.isDirect()) 784868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions)) 785868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Result = Result.remove(NextKey); 786868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } else if (const SubRegion *BaseSR = dyn_cast<SubRegion>(Base)) { 787868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Case 4: The next key is symbolic, but we changed a known 788868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // super-region. In this case the binding is certainly no longer valid. 789868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (R == Base || BaseSR->isSubRegionOf(R)) 790868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions)) 791868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Result = Result.remove(NextKey); 792868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 793868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 794868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 795868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 796868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // If we're invalidating a region with a symbolic offset, we need to make sure 797868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // we don't treat the base region as uninitialized anymore. 798868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // FIXME: This isn't very precise; see the example in the loop. 799868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (HasSymbolicOffset) 800868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Result = Result.add(SRKey, UnknownVal()); 801868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 802868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (Result.isEmpty()) 803868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return B.remove(ClusterHead); 804868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return B.add(ClusterHead, Result.asImmutableMap()); 805868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 806868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 807868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace { 808868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class invalidateRegionsWorker : public ClusterAnalysis<invalidateRegionsWorker> 809868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles){ 810868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const Expr *Ex; 811868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) unsigned Count; 812868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const LocationContext *LCtx; 813868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) InvalidatedSymbols &IS; 814868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StoreManager::InvalidatedRegions *Regions; 815868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)public: 816868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) invalidateRegionsWorker(RegionStoreManager &rm, 817868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ProgramStateManager &stateMgr, 818868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef b, 819868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const Expr *ex, unsigned count, 820868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const LocationContext *lctx, 821868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) InvalidatedSymbols &is, 822868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StoreManager::InvalidatedRegions *r, 823868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool includeGlobals) 824868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : ClusterAnalysis<invalidateRegionsWorker>(rm, stateMgr, b, includeGlobals), 825868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Ex(ex), Count(count), LCtx(lctx), IS(is), Regions(r) {} 826868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 827868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void VisitCluster(const MemRegion *baseR, const ClusterBindings &C); 828868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void VisitBaseRegion(const MemRegion *baseR); 829868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 830868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)private: 831868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) void VisitBinding(SVal V); 832868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}; 833868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 834868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 835868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void invalidateRegionsWorker::VisitBinding(SVal V) { 836868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // A symbol? Mark it touched by the invalidation. 837868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (SymbolRef Sym = V.getAsSymbol()) 838868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) IS.insert(Sym); 839868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 840868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (const MemRegion *R = V.getAsRegion()) { 841868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) AddToWorkList(R); 842868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return; 843868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 844868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 845868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Is it a LazyCompoundVal? All references get invalidated as well. 846868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (const nonloc::LazyCompoundVal *LCS = 847868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) dyn_cast<nonloc::LazyCompoundVal>(&V)) { 848868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 849a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const MemRegion *LazyR = LCS->getRegion(); 850868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) RegionBindingsRef B = RM.getRegionBindings(LCS->getStore()); 851868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 852868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // FIXME: This should not have to walk all bindings in the old store. 853868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){ 854868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ClusterBindings &Cluster = RI.getData(); 855868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 856868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CI != CE; ++CI) { 857868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) BindingKey K = CI.getKey(); 858868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (const SubRegion *BaseR = dyn_cast<SubRegion>(K.getRegion())) { 859868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (BaseR == LazyR) 860868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) VisitBinding(CI.getData()); 861868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) else if (K.hasSymbolicOffset() && BaseR->isSubRegionOf(LazyR)) 862868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) VisitBinding(CI.getData()); 863868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 864868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 865868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 866868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 867868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return; 868868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 869} 870 871void invalidateRegionsWorker::VisitCluster(const MemRegion *BaseR, 872 const ClusterBindings &C) { 873 for (ClusterBindings::iterator I = C.begin(), E = C.end(); I != E; ++I) 874 VisitBinding(I.getData()); 875 876 B = B.remove(BaseR); 877} 878 879void invalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) { 880 // Symbolic region? Mark that symbol touched by the invalidation. 881 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) 882 IS.insert(SR->getSymbol()); 883 884 // BlockDataRegion? If so, invalidate captured variables that are passed 885 // by reference. 886 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) { 887 for (BlockDataRegion::referenced_vars_iterator 888 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ; 889 BI != BE; ++BI) { 890 const VarRegion *VR = BI.getCapturedRegion(); 891 const VarDecl *VD = VR->getDecl(); 892 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage()) { 893 AddToWorkList(VR); 894 } 895 else if (Loc::isLocType(VR->getValueType())) { 896 // Map the current bindings to a Store to retrieve the value 897 // of the binding. If that binding itself is a region, we should 898 // invalidate that region. This is because a block may capture 899 // a pointer value, but the thing pointed by that pointer may 900 // get invalidated. 901 SVal V = RM.getBinding(B, loc::MemRegionVal(VR)); 902 if (const Loc *L = dyn_cast<Loc>(&V)) { 903 if (const MemRegion *LR = L->getAsRegion()) 904 AddToWorkList(LR); 905 } 906 } 907 } 908 return; 909 } 910 911 // Otherwise, we have a normal data region. Record that we touched the region. 912 if (Regions) 913 Regions->push_back(baseR); 914 915 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) { 916 // Invalidate the region by setting its default value to 917 // conjured symbol. The type of the symbol is irrelavant. 918 DefinedOrUnknownSVal V = 919 svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, Ctx.IntTy, Count); 920 B = B.addBinding(baseR, BindingKey::Default, V); 921 return; 922 } 923 924 if (!baseR->isBoundable()) 925 return; 926 927 const TypedValueRegion *TR = cast<TypedValueRegion>(baseR); 928 QualType T = TR->getValueType(); 929 930 // Invalidate the binding. 931 if (T->isStructureOrClassType()) { 932 // Invalidate the region by setting its default value to 933 // conjured symbol. The type of the symbol is irrelavant. 934 DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, 935 Ctx.IntTy, Count); 936 B = B.addBinding(baseR, BindingKey::Default, V); 937 return; 938 } 939 940 if (const ArrayType *AT = Ctx.getAsArrayType(T)) { 941 // Set the default value of the array to conjured symbol. 942 DefinedOrUnknownSVal V = 943 svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, 944 AT->getElementType(), Count); 945 B = B.addBinding(baseR, BindingKey::Default, V); 946 return; 947 } 948 949 if (includeGlobals && 950 isa<NonStaticGlobalSpaceRegion>(baseR->getMemorySpace())) { 951 // If the region is a global and we are invalidating all globals, 952 // just erase the entry. This causes all globals to be lazily 953 // symbolicated from the same base symbol. 954 B = B.removeBinding(baseR); 955 return; 956 } 957 958 959 DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, 960 T,Count); 961 assert(SymbolManager::canSymbolicate(T) || V.isUnknown()); 962 B = B.addBinding(baseR, BindingKey::Direct, V); 963} 964 965RegionBindingsRef 966RegionStoreManager::invalidateGlobalRegion(MemRegion::Kind K, 967 const Expr *Ex, 968 unsigned Count, 969 const LocationContext *LCtx, 970 RegionBindingsRef B, 971 InvalidatedRegions *Invalidated) { 972 // Bind the globals memory space to a new symbol that we will use to derive 973 // the bindings for all globals. 974 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion(K); 975 SVal V = svalBuilder.conjureSymbolVal(/* SymbolTag = */ (const void*) GS, Ex, LCtx, 976 /* type does not matter */ Ctx.IntTy, 977 Count); 978 979 B = B.removeBinding(GS) 980 .addBinding(BindingKey::Make(GS, BindingKey::Default), V); 981 982 // Even if there are no bindings in the global scope, we still need to 983 // record that we touched it. 984 if (Invalidated) 985 Invalidated->push_back(GS); 986 987 return B; 988} 989 990StoreRef 991RegionStoreManager::invalidateRegions(Store store, 992 ArrayRef<const MemRegion *> Regions, 993 const Expr *Ex, unsigned Count, 994 const LocationContext *LCtx, 995 InvalidatedSymbols &IS, 996 const CallEvent *Call, 997 InvalidatedRegions *Invalidated) { 998 invalidateRegionsWorker W(*this, StateMgr, 999 RegionStoreManager::getRegionBindings(store), 1000 Ex, Count, LCtx, IS, Invalidated, false); 1001 1002 // Scan the bindings and generate the clusters. 1003 W.GenerateClusters(); 1004 1005 // Add the regions to the worklist. 1006 for (ArrayRef<const MemRegion *>::iterator 1007 I = Regions.begin(), E = Regions.end(); I != E; ++I) 1008 W.AddToWorkList(*I); 1009 1010 W.RunWorkList(); 1011 1012 // Return the new bindings. 1013 RegionBindingsRef B = W.getRegionBindings(); 1014 1015 // For all globals which are not static nor immutable: determine which global 1016 // regions should be invalidated and invalidate them. 1017 // TODO: This could possibly be more precise with modules. 1018 // 1019 // System calls invalidate only system globals. 1020 if (Call && Call->isInSystemHeader()) { 1021 B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind, 1022 Ex, Count, LCtx, B, Invalidated); 1023 // Internal calls might invalidate both system and internal globals. 1024 } else { 1025 B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind, 1026 Ex, Count, LCtx, B, Invalidated); 1027 B = invalidateGlobalRegion(MemRegion::GlobalInternalSpaceRegionKind, 1028 Ex, Count, LCtx, B, Invalidated); 1029 } 1030 1031 return StoreRef(B.asStore(), *this); 1032} 1033 1034//===----------------------------------------------------------------------===// 1035// Extents for regions. 1036//===----------------------------------------------------------------------===// 1037 1038DefinedOrUnknownSVal 1039RegionStoreManager::getSizeInElements(ProgramStateRef state, 1040 const MemRegion *R, 1041 QualType EleTy) { 1042 SVal Size = cast<SubRegion>(R)->getExtent(svalBuilder); 1043 const llvm::APSInt *SizeInt = svalBuilder.getKnownValue(state, Size); 1044 if (!SizeInt) 1045 return UnknownVal(); 1046 1047 CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue()); 1048 1049 if (Ctx.getAsVariableArrayType(EleTy)) { 1050 // FIXME: We need to track extra state to properly record the size 1051 // of VLAs. Returning UnknownVal here, however, is a stop-gap so that 1052 // we don't have a divide-by-zero below. 1053 return UnknownVal(); 1054 } 1055 1056 CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy); 1057 1058 // If a variable is reinterpreted as a type that doesn't fit into a larger 1059 // type evenly, round it down. 1060 // This is a signed value, since it's used in arithmetic with signed indices. 1061 return svalBuilder.makeIntVal(RegionSize / EleSize, false); 1062} 1063 1064//===----------------------------------------------------------------------===// 1065// Location and region casting. 1066//===----------------------------------------------------------------------===// 1067 1068/// ArrayToPointer - Emulates the "decay" of an array to a pointer 1069/// type. 'Array' represents the lvalue of the array being decayed 1070/// to a pointer, and the returned SVal represents the decayed 1071/// version of that lvalue (i.e., a pointer to the first element of 1072/// the array). This is called by ExprEngine when evaluating casts 1073/// from arrays to pointers. 1074SVal RegionStoreManager::ArrayToPointer(Loc Array) { 1075 if (!isa<loc::MemRegionVal>(Array)) 1076 return UnknownVal(); 1077 1078 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion(); 1079 const TypedValueRegion* ArrayR = dyn_cast<TypedValueRegion>(R); 1080 1081 if (!ArrayR) 1082 return UnknownVal(); 1083 1084 // Strip off typedefs from the ArrayRegion's ValueType. 1085 QualType T = ArrayR->getValueType().getDesugaredType(Ctx); 1086 const ArrayType *AT = cast<ArrayType>(T); 1087 T = AT->getElementType(); 1088 1089 NonLoc ZeroIdx = svalBuilder.makeZeroArrayIndex(); 1090 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx)); 1091} 1092 1093//===----------------------------------------------------------------------===// 1094// Loading values from regions. 1095//===----------------------------------------------------------------------===// 1096 1097SVal RegionStoreManager::getBinding(RegionBindingsConstRef B, Loc L, QualType T) { 1098 assert(!isa<UnknownVal>(L) && "location unknown"); 1099 assert(!isa<UndefinedVal>(L) && "location undefined"); 1100 1101 // For access to concrete addresses, return UnknownVal. Checks 1102 // for null dereferences (and similar errors) are done by checkers, not 1103 // the Store. 1104 // FIXME: We can consider lazily symbolicating such memory, but we really 1105 // should defer this when we can reason easily about symbolicating arrays 1106 // of bytes. 1107 if (isa<loc::ConcreteInt>(L)) { 1108 return UnknownVal(); 1109 } 1110 if (!isa<loc::MemRegionVal>(L)) { 1111 return UnknownVal(); 1112 } 1113 1114 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion(); 1115 1116 if (isa<AllocaRegion>(MR) || 1117 isa<SymbolicRegion>(MR) || 1118 isa<CodeTextRegion>(MR)) { 1119 if (T.isNull()) { 1120 if (const TypedRegion *TR = dyn_cast<TypedRegion>(MR)) 1121 T = TR->getLocationType(); 1122 else { 1123 const SymbolicRegion *SR = cast<SymbolicRegion>(MR); 1124 T = SR->getSymbol()->getType(); 1125 } 1126 } 1127 MR = GetElementZeroRegion(MR, T); 1128 } 1129 1130 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument 1131 // instead of 'Loc', and have the other Loc cases handled at a higher level. 1132 const TypedValueRegion *R = cast<TypedValueRegion>(MR); 1133 QualType RTy = R->getValueType(); 1134 1135 // FIXME: we do not yet model the parts of a complex type, so treat the 1136 // whole thing as "unknown". 1137 if (RTy->isAnyComplexType()) 1138 return UnknownVal(); 1139 1140 // FIXME: We should eventually handle funny addressing. e.g.: 1141 // 1142 // int x = ...; 1143 // int *p = &x; 1144 // char *q = (char*) p; 1145 // char c = *q; // returns the first byte of 'x'. 1146 // 1147 // Such funny addressing will occur due to layering of regions. 1148 if (RTy->isStructureOrClassType()) 1149 return getBindingForStruct(B, R); 1150 1151 // FIXME: Handle unions. 1152 if (RTy->isUnionType()) 1153 return UnknownVal(); 1154 1155 if (RTy->isArrayType()) { 1156 if (RTy->isConstantArrayType()) 1157 return getBindingForArray(B, R); 1158 else 1159 return UnknownVal(); 1160 } 1161 1162 // FIXME: handle Vector types. 1163 if (RTy->isVectorType()) 1164 return UnknownVal(); 1165 1166 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) 1167 return CastRetrievedVal(getBindingForField(B, FR), FR, T, false); 1168 1169 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) { 1170 // FIXME: Here we actually perform an implicit conversion from the loaded 1171 // value to the element type. Eventually we want to compose these values 1172 // more intelligently. For example, an 'element' can encompass multiple 1173 // bound regions (e.g., several bound bytes), or could be a subset of 1174 // a larger value. 1175 return CastRetrievedVal(getBindingForElement(B, ER), ER, T, false); 1176 } 1177 1178 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) { 1179 // FIXME: Here we actually perform an implicit conversion from the loaded 1180 // value to the ivar type. What we should model is stores to ivars 1181 // that blow past the extent of the ivar. If the address of the ivar is 1182 // reinterpretted, it is possible we stored a different value that could 1183 // fit within the ivar. Either we need to cast these when storing them 1184 // or reinterpret them lazily (as we do here). 1185 return CastRetrievedVal(getBindingForObjCIvar(B, IVR), IVR, T, false); 1186 } 1187 1188 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { 1189 // FIXME: Here we actually perform an implicit conversion from the loaded 1190 // value to the variable type. What we should model is stores to variables 1191 // that blow past the extent of the variable. If the address of the 1192 // variable is reinterpretted, it is possible we stored a different value 1193 // that could fit within the variable. Either we need to cast these when 1194 // storing them or reinterpret them lazily (as we do here). 1195 return CastRetrievedVal(getBindingForVar(B, VR), VR, T, false); 1196 } 1197 1198 const SVal *V = B.lookup(R, BindingKey::Direct); 1199 1200 // Check if the region has a binding. 1201 if (V) 1202 return *V; 1203 1204 // The location does not have a bound value. This means that it has 1205 // the value it had upon its creation and/or entry to the analyzed 1206 // function/method. These are either symbolic values or 'undefined'. 1207 if (R->hasStackNonParametersStorage()) { 1208 // All stack variables are considered to have undefined values 1209 // upon creation. All heap allocated blocks are considered to 1210 // have undefined values as well unless they are explicitly bound 1211 // to specific values. 1212 return UndefinedVal(); 1213 } 1214 1215 // All other values are symbolic. 1216 return svalBuilder.getRegionValueSymbolVal(R); 1217} 1218 1219std::pair<Store, const MemRegion *> 1220RegionStoreManager::getLazyBinding(RegionBindingsConstRef B, 1221 const MemRegion *R, 1222 const MemRegion *originalRegion) { 1223 if (originalRegion != R) { 1224 if (Optional<SVal> OV = B.getDefaultBinding(R)) { 1225 if (const nonloc::LazyCompoundVal *V = 1226 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer())) 1227 return std::make_pair(V->getStore(), V->getRegion()); 1228 } 1229 } 1230 1231 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { 1232 const std::pair<Store, const MemRegion *> &X = 1233 getLazyBinding(B, ER->getSuperRegion(), originalRegion); 1234 1235 if (X.second) 1236 return std::make_pair(X.first, 1237 MRMgr.getElementRegionWithSuper(ER, X.second)); 1238 } 1239 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) { 1240 const std::pair<Store, const MemRegion *> &X = 1241 getLazyBinding(B, FR->getSuperRegion(), originalRegion); 1242 1243 if (X.second) { 1244 return std::make_pair(X.first, 1245 MRMgr.getFieldRegionWithSuper(FR, X.second)); 1246 } 1247 1248 } 1249 // C++ base object region is another kind of region that we should blast 1250 // through to look for lazy compound value. It is like a field region. 1251 else if (const CXXBaseObjectRegion *baseReg = 1252 dyn_cast<CXXBaseObjectRegion>(R)) { 1253 const std::pair<Store, const MemRegion *> &X = 1254 getLazyBinding(B, baseReg->getSuperRegion(), originalRegion); 1255 1256 if (X.second) { 1257 return std::make_pair(X.first, 1258 MRMgr.getCXXBaseObjectRegionWithSuper(baseReg, 1259 X.second)); 1260 } 1261 } 1262 1263 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is 1264 // possible for a valid lazy binding. 1265 return std::make_pair((Store) 0, (const MemRegion *) 0); 1266} 1267 1268SVal RegionStoreManager::getBindingForElement(RegionBindingsConstRef B, 1269 const ElementRegion* R) { 1270 // We do not currently model bindings of the CompoundLiteralregion. 1271 if (isa<CompoundLiteralRegion>(R->getBaseRegion())) 1272 return UnknownVal(); 1273 1274 // Check if the region has a binding. 1275 if (const Optional<SVal> &V = B.getDirectBinding(R)) 1276 return *V; 1277 1278 const MemRegion* superR = R->getSuperRegion(); 1279 1280 // Check if the region is an element region of a string literal. 1281 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) { 1282 // FIXME: Handle loads from strings where the literal is treated as 1283 // an integer, e.g., *((unsigned int*)"hello") 1284 QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType(); 1285 if (T != Ctx.getCanonicalType(R->getElementType())) 1286 return UnknownVal(); 1287 1288 const StringLiteral *Str = StrR->getStringLiteral(); 1289 SVal Idx = R->getIndex(); 1290 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) { 1291 int64_t i = CI->getValue().getSExtValue(); 1292 // Abort on string underrun. This can be possible by arbitrary 1293 // clients of getBindingForElement(). 1294 if (i < 0) 1295 return UndefinedVal(); 1296 int64_t length = Str->getLength(); 1297 // Technically, only i == length is guaranteed to be null. 1298 // However, such overflows should be caught before reaching this point; 1299 // the only time such an access would be made is if a string literal was 1300 // used to initialize a larger array. 1301 char c = (i >= length) ? '\0' : Str->getCodeUnit(i); 1302 return svalBuilder.makeIntVal(c, T); 1303 } 1304 } 1305 1306 // Check for loads from a code text region. For such loads, just give up. 1307 if (isa<CodeTextRegion>(superR)) 1308 return UnknownVal(); 1309 1310 // Handle the case where we are indexing into a larger scalar object. 1311 // For example, this handles: 1312 // int x = ... 1313 // char *y = &x; 1314 // return *y; 1315 // FIXME: This is a hack, and doesn't do anything really intelligent yet. 1316 const RegionRawOffset &O = R->getAsArrayOffset(); 1317 1318 // If we cannot reason about the offset, return an unknown value. 1319 if (!O.getRegion()) 1320 return UnknownVal(); 1321 1322 if (const TypedValueRegion *baseR = 1323 dyn_cast_or_null<TypedValueRegion>(O.getRegion())) { 1324 QualType baseT = baseR->getValueType(); 1325 if (baseT->isScalarType()) { 1326 QualType elemT = R->getElementType(); 1327 if (elemT->isScalarType()) { 1328 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) { 1329 if (const Optional<SVal> &V = B.getDirectBinding(superR)) { 1330 if (SymbolRef parentSym = V->getAsSymbol()) 1331 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R); 1332 1333 if (V->isUnknownOrUndef()) 1334 return *V; 1335 // Other cases: give up. We are indexing into a larger object 1336 // that has some value, but we don't know how to handle that yet. 1337 return UnknownVal(); 1338 } 1339 } 1340 } 1341 } 1342 } 1343 return getBindingForFieldOrElementCommon(B, R, R->getElementType(), 1344 superR); 1345} 1346 1347SVal RegionStoreManager::getBindingForField(RegionBindingsConstRef B, 1348 const FieldRegion* R) { 1349 1350 // Check if the region has a binding. 1351 if (const Optional<SVal> &V = B.getDirectBinding(R)) 1352 return *V; 1353 1354 QualType Ty = R->getValueType(); 1355 return getBindingForFieldOrElementCommon(B, R, Ty, R->getSuperRegion()); 1356} 1357 1358Optional<SVal> 1359RegionStoreManager::getBindingForDerivedDefaultValue(RegionBindingsConstRef B, 1360 const MemRegion *superR, 1361 const TypedValueRegion *R, 1362 QualType Ty) { 1363 1364 if (const Optional<SVal> &D = B.getDefaultBinding(superR)) { 1365 const SVal &val = D.getValue(); 1366 if (SymbolRef parentSym = val.getAsSymbol()) 1367 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R); 1368 1369 if (val.isZeroConstant()) 1370 return svalBuilder.makeZeroVal(Ty); 1371 1372 if (val.isUnknownOrUndef()) 1373 return val; 1374 1375 // Lazy bindings are handled later. 1376 if (isa<nonloc::LazyCompoundVal>(val)) 1377 return Optional<SVal>(); 1378 1379 llvm_unreachable("Unknown default value"); 1380 } 1381 1382 return Optional<SVal>(); 1383} 1384 1385SVal RegionStoreManager::getLazyBinding(const MemRegion *LazyBindingRegion, 1386 RegionBindingsRef LazyBinding) { 1387 SVal Result; 1388 if (const ElementRegion *ER = dyn_cast<ElementRegion>(LazyBindingRegion)) 1389 Result = getBindingForElement(LazyBinding, ER); 1390 else 1391 Result = getBindingForField(LazyBinding, 1392 cast<FieldRegion>(LazyBindingRegion)); 1393 1394 // This is a hack to deal with RegionStore's inability to distinguish a 1395 // default value for /part/ of an aggregate from a default value for the 1396 // /entire/ aggregate. The most common case of this is when struct Outer 1397 // has as its first member a struct Inner, which is copied in from a stack 1398 // variable. In this case, even if the Outer's default value is symbolic, 0, 1399 // or unknown, it gets overridden by the Inner's default value of undefined. 1400 // 1401 // This is a general problem -- if the Inner is zero-initialized, the Outer 1402 // will now look zero-initialized. The proper way to solve this is with a 1403 // new version of RegionStore that tracks the extent of a binding as well 1404 // as the offset. 1405 // 1406 // This hack only takes care of the undefined case because that can very 1407 // quickly result in a warning. 1408 if (Result.isUndef()) 1409 Result = UnknownVal(); 1410 1411 return Result; 1412} 1413 1414SVal 1415RegionStoreManager::getBindingForFieldOrElementCommon(RegionBindingsConstRef B, 1416 const TypedValueRegion *R, 1417 QualType Ty, 1418 const MemRegion *superR) { 1419 1420 // At this point we have already checked in either getBindingForElement or 1421 // getBindingForField if 'R' has a direct binding. 1422 1423 // Lazy binding? 1424 Store lazyBindingStore = NULL; 1425 const MemRegion *lazyBindingRegion = NULL; 1426 llvm::tie(lazyBindingStore, lazyBindingRegion) = getLazyBinding(B, R, R); 1427 if (lazyBindingRegion) 1428 return getLazyBinding(lazyBindingRegion, 1429 getRegionBindings(lazyBindingStore)); 1430 1431 // Record whether or not we see a symbolic index. That can completely 1432 // be out of scope of our lookup. 1433 bool hasSymbolicIndex = false; 1434 1435 while (superR) { 1436 if (const Optional<SVal> &D = 1437 getBindingForDerivedDefaultValue(B, superR, R, Ty)) 1438 return *D; 1439 1440 if (const ElementRegion *ER = dyn_cast<ElementRegion>(superR)) { 1441 NonLoc index = ER->getIndex(); 1442 if (!index.isConstant()) 1443 hasSymbolicIndex = true; 1444 } 1445 1446 // If our super region is a field or element itself, walk up the region 1447 // hierarchy to see if there is a default value installed in an ancestor. 1448 if (const SubRegion *SR = dyn_cast<SubRegion>(superR)) { 1449 superR = SR->getSuperRegion(); 1450 continue; 1451 } 1452 break; 1453 } 1454 1455 if (R->hasStackNonParametersStorage()) { 1456 if (isa<ElementRegion>(R)) { 1457 // Currently we don't reason specially about Clang-style vectors. Check 1458 // if superR is a vector and if so return Unknown. 1459 if (const TypedValueRegion *typedSuperR = 1460 dyn_cast<TypedValueRegion>(superR)) { 1461 if (typedSuperR->getValueType()->isVectorType()) 1462 return UnknownVal(); 1463 } 1464 } 1465 1466 // FIXME: We also need to take ElementRegions with symbolic indexes into 1467 // account. This case handles both directly accessing an ElementRegion 1468 // with a symbolic offset, but also fields within an element with 1469 // a symbolic offset. 1470 if (hasSymbolicIndex) 1471 return UnknownVal(); 1472 1473 return UndefinedVal(); 1474 } 1475 1476 // All other values are symbolic. 1477 return svalBuilder.getRegionValueSymbolVal(R); 1478} 1479 1480SVal RegionStoreManager::getBindingForObjCIvar(RegionBindingsConstRef B, 1481 const ObjCIvarRegion* R) { 1482 // Check if the region has a binding. 1483 if (const Optional<SVal> &V = B.getDirectBinding(R)) 1484 return *V; 1485 1486 const MemRegion *superR = R->getSuperRegion(); 1487 1488 // Check if the super region has a default binding. 1489 if (const Optional<SVal> &V = B.getDefaultBinding(superR)) { 1490 if (SymbolRef parentSym = V->getAsSymbol()) 1491 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R); 1492 1493 // Other cases: give up. 1494 return UnknownVal(); 1495 } 1496 1497 return getBindingForLazySymbol(R); 1498} 1499 1500static Optional<SVal> getConstValue(SValBuilder &SVB, const VarDecl *VD) { 1501 ASTContext &Ctx = SVB.getContext(); 1502 if (!VD->getType().isConstQualified()) 1503 return Optional<SVal>(); 1504 1505 const Expr *Init = VD->getInit(); 1506 if (!Init) 1507 return Optional<SVal>(); 1508 1509 llvm::APSInt Result; 1510 if (Init->EvaluateAsInt(Result, Ctx)) 1511 return SVB.makeIntVal(Result); 1512 1513 if (Init->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)) 1514 return SVB.makeNull(); 1515 1516 // FIXME: Handle other possible constant expressions. 1517 return Optional<SVal>(); 1518} 1519 1520SVal RegionStoreManager::getBindingForVar(RegionBindingsConstRef B, 1521 const VarRegion *R) { 1522 1523 // Check if the region has a binding. 1524 if (const Optional<SVal> &V = B.getDirectBinding(R)) 1525 return *V; 1526 1527 // Lazily derive a value for the VarRegion. 1528 const VarDecl *VD = R->getDecl(); 1529 const MemSpaceRegion *MS = R->getMemorySpace(); 1530 1531 // Arguments are always symbolic. 1532 if (isa<StackArgumentsSpaceRegion>(MS)) 1533 return svalBuilder.getRegionValueSymbolVal(R); 1534 1535 // Is 'VD' declared constant? If so, retrieve the constant value. 1536 if (Optional<SVal> V = getConstValue(svalBuilder, VD)) 1537 return *V; 1538 1539 // This must come after the check for constants because closure-captured 1540 // constant variables may appear in UnknownSpaceRegion. 1541 if (isa<UnknownSpaceRegion>(MS)) 1542 return svalBuilder.getRegionValueSymbolVal(R); 1543 1544 if (isa<GlobalsSpaceRegion>(MS)) { 1545 QualType T = VD->getType(); 1546 1547 // Function-scoped static variables are default-initialized to 0; if they 1548 // have an initializer, it would have been processed by now. 1549 if (isa<StaticGlobalSpaceRegion>(MS)) 1550 return svalBuilder.makeZeroVal(T); 1551 1552 if (Optional<SVal> V = getBindingForDerivedDefaultValue(B, MS, R, T)) 1553 return V.getValue(); 1554 1555 return svalBuilder.getRegionValueSymbolVal(R); 1556 } 1557 1558 return UndefinedVal(); 1559} 1560 1561SVal RegionStoreManager::getBindingForLazySymbol(const TypedValueRegion *R) { 1562 // All other values are symbolic. 1563 return svalBuilder.getRegionValueSymbolVal(R); 1564} 1565 1566NonLoc RegionStoreManager::createLazyBinding(RegionBindingsConstRef B, 1567 const TypedValueRegion *R) { 1568 // If we already have a lazy binding, and it's for the whole structure, 1569 // don't create a new lazy binding. 1570 if (Optional<SVal> V = B.getDefaultBinding(R)) { 1571 const nonloc::LazyCompoundVal *LCV = 1572 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer()); 1573 if (LCV) { 1574 QualType RegionTy = R->getValueType(); 1575 QualType SourceRegionTy = LCV->getRegion()->getValueType(); 1576 if (RegionTy.getCanonicalType() == SourceRegionTy.getCanonicalType()) 1577 return *LCV; 1578 } 1579 } 1580 1581 return svalBuilder.makeLazyCompoundVal(StoreRef(B.asStore(), *this), R); 1582} 1583 1584SVal RegionStoreManager::getBindingForStruct(RegionBindingsConstRef B, 1585 const TypedValueRegion *R) { 1586 const RecordDecl *RD = R->getValueType()->castAs<RecordType>()->getDecl(); 1587 if (RD->field_empty()) 1588 return UnknownVal(); 1589 1590 return createLazyBinding(B, R); 1591} 1592 1593SVal RegionStoreManager::getBindingForArray(RegionBindingsConstRef B, 1594 const TypedValueRegion *R) { 1595 assert(Ctx.getAsConstantArrayType(R->getValueType()) && 1596 "Only constant array types can have compound bindings."); 1597 1598 return createLazyBinding(B, R); 1599} 1600 1601bool RegionStoreManager::includedInBindings(Store store, 1602 const MemRegion *region) const { 1603 RegionBindingsRef B = getRegionBindings(store); 1604 region = region->getBaseRegion(); 1605 1606 // Quick path: if the base is the head of a cluster, the region is live. 1607 if (B.lookup(region)) 1608 return true; 1609 1610 // Slow path: if the region is the VALUE of any binding, it is live. 1611 for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) { 1612 const ClusterBindings &Cluster = RI.getData(); 1613 for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 1614 CI != CE; ++CI) { 1615 const SVal &D = CI.getData(); 1616 if (const MemRegion *R = D.getAsRegion()) 1617 if (R->getBaseRegion() == region) 1618 return true; 1619 } 1620 } 1621 1622 return false; 1623} 1624 1625//===----------------------------------------------------------------------===// 1626// Binding values to regions. 1627//===----------------------------------------------------------------------===// 1628 1629StoreRef RegionStoreManager::killBinding(Store ST, Loc L) { 1630 if (isa<loc::MemRegionVal>(L)) 1631 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion()) 1632 return StoreRef(getRegionBindings(ST).removeBinding(R) 1633 .asImmutableMap() 1634 .getRootWithoutRetain(), 1635 *this); 1636 1637 return StoreRef(ST, *this); 1638} 1639 1640RegionBindingsRef 1641RegionStoreManager::bind(RegionBindingsConstRef B, Loc L, SVal V) { 1642 if (isa<loc::ConcreteInt>(L)) 1643 return B; 1644 1645 // If we get here, the location should be a region. 1646 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion(); 1647 1648 // Check if the region is a struct region. 1649 if (const TypedValueRegion* TR = dyn_cast<TypedValueRegion>(R)) { 1650 QualType Ty = TR->getValueType(); 1651 if (Ty->isArrayType()) 1652 return bindArray(B, TR, V); 1653 if (Ty->isStructureOrClassType()) 1654 return bindStruct(B, TR, V); 1655 if (Ty->isVectorType()) 1656 return bindVector(B, TR, V); 1657 } 1658 1659 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) { 1660 // Binding directly to a symbolic region should be treated as binding 1661 // to element 0. 1662 QualType T = SR->getSymbol()->getType(); 1663 if (T->isAnyPointerType() || T->isReferenceType()) 1664 T = T->getPointeeType(); 1665 1666 R = GetElementZeroRegion(SR, T); 1667 } 1668 1669 // Clear out bindings that may overlap with this binding. 1670 RegionBindingsRef NewB = removeSubRegionBindings(B, cast<SubRegion>(R)); 1671 return NewB.addBinding(BindingKey::Make(R, BindingKey::Direct), V); 1672} 1673 1674// FIXME: this method should be merged into Bind(). 1675StoreRef RegionStoreManager::bindCompoundLiteral(Store ST, 1676 const CompoundLiteralExpr *CL, 1677 const LocationContext *LC, 1678 SVal V) { 1679 return Bind(ST, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)), V); 1680} 1681 1682RegionBindingsRef 1683RegionStoreManager::setImplicitDefaultValue(RegionBindingsConstRef B, 1684 const MemRegion *R, 1685 QualType T) { 1686 SVal V; 1687 1688 if (Loc::isLocType(T)) 1689 V = svalBuilder.makeNull(); 1690 else if (T->isIntegerType()) 1691 V = svalBuilder.makeZeroVal(T); 1692 else if (T->isStructureOrClassType() || T->isArrayType()) { 1693 // Set the default value to a zero constant when it is a structure 1694 // or array. The type doesn't really matter. 1695 V = svalBuilder.makeZeroVal(Ctx.IntTy); 1696 } 1697 else { 1698 // We can't represent values of this type, but we still need to set a value 1699 // to record that the region has been initialized. 1700 // If this assertion ever fires, a new case should be added above -- we 1701 // should know how to default-initialize any value we can symbolicate. 1702 assert(!SymbolManager::canSymbolicate(T) && "This type is representable"); 1703 V = UnknownVal(); 1704 } 1705 1706 return B.addBinding(R, BindingKey::Default, V); 1707} 1708 1709RegionBindingsRef 1710RegionStoreManager::bindArray(RegionBindingsConstRef B, 1711 const TypedValueRegion* R, 1712 SVal Init) { 1713 1714 const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType())); 1715 QualType ElementTy = AT->getElementType(); 1716 Optional<uint64_t> Size; 1717 1718 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT)) 1719 Size = CAT->getSize().getZExtValue(); 1720 1721 // Check if the init expr is a string literal. 1722 if (loc::MemRegionVal *MRV = dyn_cast<loc::MemRegionVal>(&Init)) { 1723 const StringRegion *S = cast<StringRegion>(MRV->getRegion()); 1724 1725 // Treat the string as a lazy compound value. 1726 StoreRef store(B.asStore(), *this); 1727 nonloc::LazyCompoundVal LCV = 1728 cast<nonloc::LazyCompoundVal>(svalBuilder.makeLazyCompoundVal(store, S)); 1729 return bindAggregate(B, R, LCV); 1730 } 1731 1732 // Handle lazy compound values. 1733 if (isa<nonloc::LazyCompoundVal>(Init)) 1734 return bindAggregate(B, R, Init); 1735 1736 // Remaining case: explicit compound values. 1737 1738 if (Init.isUnknown()) 1739 return setImplicitDefaultValue(B, R, ElementTy); 1740 1741 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init); 1742 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); 1743 uint64_t i = 0; 1744 1745 RegionBindingsRef NewB(B); 1746 1747 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) { 1748 // The init list might be shorter than the array length. 1749 if (VI == VE) 1750 break; 1751 1752 const NonLoc &Idx = svalBuilder.makeArrayIndex(i); 1753 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx); 1754 1755 if (ElementTy->isStructureOrClassType()) 1756 NewB = bindStruct(NewB, ER, *VI); 1757 else if (ElementTy->isArrayType()) 1758 NewB = bindArray(NewB, ER, *VI); 1759 else 1760 NewB = bind(NewB, svalBuilder.makeLoc(ER), *VI); 1761 } 1762 1763 // If the init list is shorter than the array length, set the 1764 // array default value. 1765 if (Size.hasValue() && i < Size.getValue()) 1766 NewB = setImplicitDefaultValue(NewB, R, ElementTy); 1767 1768 return NewB; 1769} 1770 1771RegionBindingsRef RegionStoreManager::bindVector(RegionBindingsConstRef B, 1772 const TypedValueRegion* R, 1773 SVal V) { 1774 QualType T = R->getValueType(); 1775 assert(T->isVectorType()); 1776 const VectorType *VT = T->getAs<VectorType>(); // Use getAs for typedefs. 1777 1778 // Handle lazy compound values and symbolic values. 1779 if (isa<nonloc::LazyCompoundVal>(V) || isa<nonloc::SymbolVal>(V)) 1780 return bindAggregate(B, R, V); 1781 1782 // We may get non-CompoundVal accidentally due to imprecise cast logic or 1783 // that we are binding symbolic struct value. Kill the field values, and if 1784 // the value is symbolic go and bind it as a "default" binding. 1785 if (!isa<nonloc::CompoundVal>(V)) { 1786 return bindAggregate(B, R, UnknownVal()); 1787 } 1788 1789 QualType ElemType = VT->getElementType(); 1790 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); 1791 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); 1792 unsigned index = 0, numElements = VT->getNumElements(); 1793 RegionBindingsRef NewB(B); 1794 1795 for ( ; index != numElements ; ++index) { 1796 if (VI == VE) 1797 break; 1798 1799 NonLoc Idx = svalBuilder.makeArrayIndex(index); 1800 const ElementRegion *ER = MRMgr.getElementRegion(ElemType, Idx, R, Ctx); 1801 1802 if (ElemType->isArrayType()) 1803 NewB = bindArray(NewB, ER, *VI); 1804 else if (ElemType->isStructureOrClassType()) 1805 NewB = bindStruct(NewB, ER, *VI); 1806 else 1807 NewB = bind(NewB, svalBuilder.makeLoc(ER), *VI); 1808 } 1809 return NewB; 1810} 1811 1812RegionBindingsRef RegionStoreManager::bindStruct(RegionBindingsConstRef B, 1813 const TypedValueRegion* R, 1814 SVal V) { 1815 if (!Features.supportsFields()) 1816 return B; 1817 1818 QualType T = R->getValueType(); 1819 assert(T->isStructureOrClassType()); 1820 1821 const RecordType* RT = T->getAs<RecordType>(); 1822 RecordDecl *RD = RT->getDecl(); 1823 1824 if (!RD->isCompleteDefinition()) 1825 return B; 1826 1827 // Handle lazy compound values and symbolic values. 1828 if (isa<nonloc::LazyCompoundVal>(V) || isa<nonloc::SymbolVal>(V)) 1829 return bindAggregate(B, R, V); 1830 1831 // We may get non-CompoundVal accidentally due to imprecise cast logic or 1832 // that we are binding symbolic struct value. Kill the field values, and if 1833 // the value is symbolic go and bind it as a "default" binding. 1834 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V)) 1835 return bindAggregate(B, R, UnknownVal()); 1836 1837 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V); 1838 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); 1839 1840 RecordDecl::field_iterator FI, FE; 1841 RegionBindingsRef NewB(B); 1842 1843 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI) { 1844 1845 if (VI == VE) 1846 break; 1847 1848 // Skip any unnamed bitfields to stay in sync with the initializers. 1849 if (FI->isUnnamedBitfield()) 1850 continue; 1851 1852 QualType FTy = FI->getType(); 1853 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); 1854 1855 if (FTy->isArrayType()) 1856 NewB = bindArray(NewB, FR, *VI); 1857 else if (FTy->isStructureOrClassType()) 1858 NewB = bindStruct(NewB, FR, *VI); 1859 else 1860 NewB = bind(NewB, svalBuilder.makeLoc(FR), *VI); 1861 ++VI; 1862 } 1863 1864 // There may be fewer values in the initialize list than the fields of struct. 1865 if (FI != FE) { 1866 NewB = NewB.addBinding(R, BindingKey::Default, 1867 svalBuilder.makeIntVal(0, false)); 1868 } 1869 1870 return NewB; 1871} 1872 1873RegionBindingsRef 1874RegionStoreManager::bindAggregate(RegionBindingsConstRef B, 1875 const TypedRegion *R, 1876 SVal Val) { 1877 // Remove the old bindings, using 'R' as the root of all regions 1878 // we will invalidate. Then add the new binding. 1879 return removeSubRegionBindings(B, R).addBinding(R, BindingKey::Default, Val); 1880} 1881 1882//===----------------------------------------------------------------------===// 1883// State pruning. 1884//===----------------------------------------------------------------------===// 1885 1886namespace { 1887class removeDeadBindingsWorker : 1888 public ClusterAnalysis<removeDeadBindingsWorker> { 1889 SmallVector<const SymbolicRegion*, 12> Postponed; 1890 SymbolReaper &SymReaper; 1891 const StackFrameContext *CurrentLCtx; 1892 1893public: 1894 removeDeadBindingsWorker(RegionStoreManager &rm, 1895 ProgramStateManager &stateMgr, 1896 RegionBindingsRef b, SymbolReaper &symReaper, 1897 const StackFrameContext *LCtx) 1898 : ClusterAnalysis<removeDeadBindingsWorker>(rm, stateMgr, b, 1899 /* includeGlobals = */ false), 1900 SymReaper(symReaper), CurrentLCtx(LCtx) {} 1901 1902 // Called by ClusterAnalysis. 1903 void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C); 1904 void VisitCluster(const MemRegion *baseR, const ClusterBindings &C); 1905 1906 bool UpdatePostponed(); 1907 void VisitBinding(SVal V); 1908}; 1909} 1910 1911void removeDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR, 1912 const ClusterBindings &C) { 1913 1914 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) { 1915 if (SymReaper.isLive(VR)) 1916 AddToWorkList(baseR, &C); 1917 1918 return; 1919 } 1920 1921 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) { 1922 if (SymReaper.isLive(SR->getSymbol())) 1923 AddToWorkList(SR, &C); 1924 else 1925 Postponed.push_back(SR); 1926 1927 return; 1928 } 1929 1930 if (isa<NonStaticGlobalSpaceRegion>(baseR)) { 1931 AddToWorkList(baseR, &C); 1932 return; 1933 } 1934 1935 // CXXThisRegion in the current or parent location context is live. 1936 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) { 1937 const StackArgumentsSpaceRegion *StackReg = 1938 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion()); 1939 const StackFrameContext *RegCtx = StackReg->getStackFrame(); 1940 if (CurrentLCtx && 1941 (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))) 1942 AddToWorkList(TR, &C); 1943 } 1944} 1945 1946void removeDeadBindingsWorker::VisitCluster(const MemRegion *baseR, 1947 const ClusterBindings &C) { 1948 // Mark the symbol for any SymbolicRegion with live bindings as live itself. 1949 // This means we should continue to track that symbol. 1950 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(baseR)) 1951 SymReaper.markLive(SymR->getSymbol()); 1952 1953 for (ClusterBindings::iterator I = C.begin(), E = C.end(); I != E; ++I) 1954 VisitBinding(I.getData()); 1955} 1956 1957void removeDeadBindingsWorker::VisitBinding(SVal V) { 1958 // Is it a LazyCompoundVal? All referenced regions are live as well. 1959 if (const nonloc::LazyCompoundVal *LCS = 1960 dyn_cast<nonloc::LazyCompoundVal>(&V)) { 1961 1962 const MemRegion *LazyR = LCS->getRegion(); 1963 RegionBindingsRef B = RM.getRegionBindings(LCS->getStore()); 1964 1965 // FIXME: This should not have to walk all bindings in the old store. 1966 for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); 1967 RI != RE; ++RI){ 1968 const ClusterBindings &Cluster = RI.getData(); 1969 for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 1970 CI != CE; ++CI) { 1971 BindingKey K = CI.getKey(); 1972 if (const SubRegion *BaseR = dyn_cast<SubRegion>(K.getRegion())) { 1973 if (BaseR == LazyR) 1974 VisitBinding(CI.getData()); 1975 else if (K.hasSymbolicOffset() && BaseR->isSubRegionOf(LazyR)) 1976 VisitBinding(CI.getData()); 1977 } 1978 } 1979 } 1980 1981 return; 1982 } 1983 1984 // If V is a region, then add it to the worklist. 1985 if (const MemRegion *R = V.getAsRegion()) { 1986 AddToWorkList(R); 1987 1988 // All regions captured by a block are also live. 1989 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) { 1990 BlockDataRegion::referenced_vars_iterator I = BR->referenced_vars_begin(), 1991 E = BR->referenced_vars_end(); 1992 for ( ; I != E; ++I) 1993 AddToWorkList(I.getCapturedRegion()); 1994 } 1995 } 1996 1997 1998 // Update the set of live symbols. 1999 for (SymExpr::symbol_iterator SI = V.symbol_begin(), SE = V.symbol_end(); 2000 SI!=SE; ++SI) 2001 SymReaper.markLive(*SI); 2002} 2003 2004bool removeDeadBindingsWorker::UpdatePostponed() { 2005 // See if any postponed SymbolicRegions are actually live now, after 2006 // having done a scan. 2007 bool changed = false; 2008 2009 for (SmallVectorImpl<const SymbolicRegion*>::iterator 2010 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) { 2011 if (const SymbolicRegion *SR = *I) { 2012 if (SymReaper.isLive(SR->getSymbol())) { 2013 changed |= AddToWorkList(SR); 2014 *I = NULL; 2015 } 2016 } 2017 } 2018 2019 return changed; 2020} 2021 2022StoreRef RegionStoreManager::removeDeadBindings(Store store, 2023 const StackFrameContext *LCtx, 2024 SymbolReaper& SymReaper) { 2025 RegionBindingsRef B = getRegionBindings(store); 2026 removeDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx); 2027 W.GenerateClusters(); 2028 2029 // Enqueue the region roots onto the worklist. 2030 for (SymbolReaper::region_iterator I = SymReaper.region_begin(), 2031 E = SymReaper.region_end(); I != E; ++I) { 2032 W.AddToWorkList(*I); 2033 } 2034 2035 do W.RunWorkList(); while (W.UpdatePostponed()); 2036 2037 // We have now scanned the store, marking reachable regions and symbols 2038 // as live. We now remove all the regions that are dead from the store 2039 // as well as update DSymbols with the set symbols that are now dead. 2040 for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) { 2041 const MemRegion *Base = I.getKey(); 2042 2043 // If the cluster has been visited, we know the region has been marked. 2044 if (W.isVisited(Base)) 2045 continue; 2046 2047 // Remove the dead entry. 2048 B = B.remove(Base); 2049 2050 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(Base)) 2051 SymReaper.maybeDead(SymR->getSymbol()); 2052 2053 // Mark all non-live symbols that this binding references as dead. 2054 const ClusterBindings &Cluster = I.getData(); 2055 for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 2056 CI != CE; ++CI) { 2057 SVal X = CI.getData(); 2058 SymExpr::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); 2059 for (; SI != SE; ++SI) 2060 SymReaper.maybeDead(*SI); 2061 } 2062 } 2063 2064 return StoreRef(B.asStore(), *this); 2065} 2066 2067//===----------------------------------------------------------------------===// 2068// Utility methods. 2069//===----------------------------------------------------------------------===// 2070 2071void RegionStoreManager::print(Store store, raw_ostream &OS, 2072 const char* nl, const char *sep) { 2073 RegionBindingsRef B = getRegionBindings(store); 2074 OS << "Store (direct and default bindings), " 2075 << B.asStore() 2076 << " :" << nl; 2077 B.dump(OS, nl); 2078} 2079