Store.h revision 740d490593e0de8732a697c9f77b90ddd463863b
1//== Store.h - Interface for maps from Locations to Values ------*- C++ -*--==// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defined the types Store and StoreManager. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_GR_STORE_H 15#define LLVM_CLANG_GR_STORE_H 16 17#include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h" 18#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 19#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 20#include "llvm/ADT/DenseSet.h" 21#include "llvm/ADT/Optional.h" 22 23namespace clang { 24 25class Stmt; 26class Expr; 27class ObjCIvarDecl; 28class StackFrameContext; 29 30namespace ento { 31 32class CallEvent; 33class ProgramState; 34class ProgramStateManager; 35class SubRegionMap; 36 37class StoreManager { 38protected: 39 SValBuilder &svalBuilder; 40 ProgramStateManager &StateMgr; 41 42 /// MRMgr - Manages region objects associated with this StoreManager. 43 MemRegionManager &MRMgr; 44 ASTContext &Ctx; 45 46 StoreManager(ProgramStateManager &stateMgr); 47 48public: 49 virtual ~StoreManager() {} 50 51 /// Return the value bound to specified location in a given state. 52 /// \param[in] store The analysis state. 53 /// \param[in] loc The symbolic memory location. 54 /// \param[in] T An optional type that provides a hint indicating the 55 /// expected type of the returned value. This is used if the value is 56 /// lazily computed. 57 /// \return The value bound to the location \c loc. 58 virtual SVal getBinding(Store store, Loc loc, QualType T = QualType()) = 0; 59 60 /// Return a state with the specified value bound to the given location. 61 /// \param[in] store The analysis state. 62 /// \param[in] loc The symbolic memory location. 63 /// \param[in] val The value to bind to location \c loc. 64 /// \return A pointer to a ProgramState object that contains the same 65 /// bindings as \c state with the addition of having the value specified 66 /// by \c val bound to the location given for \c loc. 67 virtual StoreRef Bind(Store store, Loc loc, SVal val) = 0; 68 69 virtual StoreRef BindDefault(Store store, const MemRegion *R, SVal V); 70 virtual StoreRef Remove(Store St, Loc L) = 0; 71 72 /// BindCompoundLiteral - Return the store that has the bindings currently 73 /// in 'store' plus the bindings for the CompoundLiteral. 'R' is the region 74 /// for the compound literal and 'BegInit' and 'EndInit' represent an 75 /// array of initializer values. 76 virtual StoreRef BindCompoundLiteral(Store store, 77 const CompoundLiteralExpr *cl, 78 const LocationContext *LC, SVal v) = 0; 79 80 /// getInitialStore - Returns the initial "empty" store representing the 81 /// value bindings upon entry to an analyzed function. 82 virtual StoreRef getInitialStore(const LocationContext *InitLoc) = 0; 83 84 /// getRegionManager - Returns the internal RegionManager object that is 85 /// used to query and manipulate MemRegion objects. 86 MemRegionManager& getRegionManager() { return MRMgr; } 87 88 /// getSubRegionMap - Returns an opaque map object that clients can query 89 /// to get the subregions of a given MemRegion object. It is the 90 // caller's responsibility to 'delete' the returned map. 91 virtual SubRegionMap *getSubRegionMap(Store store) = 0; 92 93 virtual Loc getLValueVar(const VarDecl *VD, const LocationContext *LC) { 94 return svalBuilder.makeLoc(MRMgr.getVarRegion(VD, LC)); 95 } 96 97 Loc getLValueCompoundLiteral(const CompoundLiteralExpr *CL, 98 const LocationContext *LC) { 99 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)); 100 } 101 102 virtual SVal getLValueIvar(const ObjCIvarDecl *decl, SVal base); 103 104 virtual SVal getLValueField(const FieldDecl *D, SVal Base) { 105 return getLValueFieldOrIvar(D, Base); 106 } 107 108 virtual SVal getLValueElement(QualType elementType, NonLoc offset, SVal Base); 109 110 // FIXME: This should soon be eliminated altogether; clients should deal with 111 // region extents directly. 112 virtual DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state, 113 const MemRegion *region, 114 QualType EleTy) { 115 return UnknownVal(); 116 } 117 118 /// ArrayToPointer - Used by ExprEngine::VistCast to handle implicit 119 /// conversions between arrays and pointers. 120 virtual SVal ArrayToPointer(Loc Array) = 0; 121 122 /// Evaluates DerivedToBase casts. 123 virtual SVal evalDerivedToBase(SVal derived, QualType basePtrType) = 0; 124 125 /// \brief Evaluates C++ dynamic_cast cast. 126 /// The callback may result in the following 3 scenarios: 127 /// - Successful cast (ex: derived is subclass of base). 128 /// - Failed cast (ex: derived is definitely not a subclass of base). 129 /// - We don't know (base is a symbolic region and we don't have 130 /// enough info to determine if the cast will succeed at run time). 131 /// The function returns an SVal representing the derived class; it's 132 /// valid only if Failed flag is set to false. 133 virtual SVal evalDynamicCast(SVal base, QualType derivedPtrType, 134 bool &Failed) = 0; 135 136 class CastResult { 137 ProgramStateRef state; 138 const MemRegion *region; 139 public: 140 ProgramStateRef getState() const { return state; } 141 const MemRegion* getRegion() const { return region; } 142 CastResult(ProgramStateRef s, const MemRegion* r = 0) : state(s), region(r){} 143 }; 144 145 const ElementRegion *GetElementZeroRegion(const MemRegion *R, QualType T); 146 147 /// castRegion - Used by ExprEngine::VisitCast to handle casts from 148 /// a MemRegion* to a specific location type. 'R' is the region being 149 /// casted and 'CastToTy' the result type of the cast. 150 const MemRegion *castRegion(const MemRegion *region, QualType CastToTy); 151 152 virtual StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx, 153 SymbolReaper& SymReaper) = 0; 154 155 virtual StoreRef BindDecl(Store store, const VarRegion *VR, SVal initVal) = 0; 156 157 virtual StoreRef BindDeclWithNoInit(Store store, const VarRegion *VR) = 0; 158 159 virtual bool includedInBindings(Store store, 160 const MemRegion *region) const = 0; 161 162 /// If the StoreManager supports it, increment the reference count of 163 /// the specified Store object. 164 virtual void incrementReferenceCount(Store store) {} 165 166 /// If the StoreManager supports it, decrement the reference count of 167 /// the specified Store object. If the reference count hits 0, the memory 168 /// associated with the object is recycled. 169 virtual void decrementReferenceCount(Store store) {} 170 171 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols; 172 typedef SmallVector<const MemRegion *, 8> InvalidatedRegions; 173 174 /// invalidateRegions - Clears out the specified regions from the store, 175 /// marking their values as unknown. Depending on the store, this may also 176 /// invalidate additional regions that may have changed based on accessing 177 /// the given regions. Optionally, invalidates non-static globals as well. 178 /// \param[in] store The initial store 179 /// \param[in] Regions The regions to invalidate. 180 /// \param[in] E The current statement being evaluated. Used to conjure 181 /// symbols to mark the values of invalidated regions. 182 /// \param[in] Count The current block count. Used to conjure 183 /// symbols to mark the values of invalidated regions. 184 /// \param[in,out] IS A set to fill with any symbols that are no longer 185 /// accessible. Pass \c NULL if this information will not be used. 186 /// \param[in] Call The call expression which will be used to determine which 187 /// globals should get invalidated. 188 /// \param[in,out] Invalidated A vector to fill with any regions being 189 /// invalidated. This should include any regions explicitly invalidated 190 /// even if they do not currently have bindings. Pass \c NULL if this 191 /// information will not be used. 192 virtual StoreRef invalidateRegions(Store store, 193 ArrayRef<const MemRegion *> Regions, 194 const Expr *E, unsigned Count, 195 const LocationContext *LCtx, 196 InvalidatedSymbols &IS, 197 const CallEvent *Call, 198 InvalidatedRegions *Invalidated) = 0; 199 200 /// enterStackFrame - Let the StoreManager to do something when execution 201 /// engine is about to execute into a callee. 202 virtual StoreRef enterStackFrame(ProgramStateRef state, 203 const LocationContext *callerCtx, 204 const StackFrameContext *calleeCtx); 205 206 virtual void print(Store store, raw_ostream &Out, 207 const char* nl, const char *sep) = 0; 208 209 class BindingsHandler { 210 public: 211 virtual ~BindingsHandler(); 212 virtual bool HandleBinding(StoreManager& SMgr, Store store, 213 const MemRegion *region, SVal val) = 0; 214 }; 215 216 class FindUniqueBinding : 217 public BindingsHandler { 218 SymbolRef Sym; 219 const MemRegion* Binding; 220 bool First; 221 222 public: 223 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {} 224 225 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R, 226 SVal val); 227 operator bool() { return First && Binding; } 228 const MemRegion *getRegion() { return Binding; } 229 }; 230 231 /// iterBindings - Iterate over the bindings in the Store. 232 virtual void iterBindings(Store store, BindingsHandler& f) = 0; 233 234protected: 235 const MemRegion *MakeElementRegion(const MemRegion *baseRegion, 236 QualType pointeeTy, uint64_t index = 0); 237 238 /// CastRetrievedVal - Used by subclasses of StoreManager to implement 239 /// implicit casts that arise from loads from regions that are reinterpreted 240 /// as another region. 241 SVal CastRetrievedVal(SVal val, const TypedValueRegion *region, 242 QualType castTy, bool performTestOnly = true); 243 244private: 245 SVal getLValueFieldOrIvar(const Decl *decl, SVal base); 246}; 247 248 249inline StoreRef::StoreRef(Store store, StoreManager & smgr) 250 : store(store), mgr(smgr) { 251 if (store) 252 mgr.incrementReferenceCount(store); 253} 254 255inline StoreRef::StoreRef(const StoreRef &sr) 256 : store(sr.store), mgr(sr.mgr) 257{ 258 if (store) 259 mgr.incrementReferenceCount(store); 260} 261 262inline StoreRef::~StoreRef() { 263 if (store) 264 mgr.decrementReferenceCount(store); 265} 266 267inline StoreRef &StoreRef::operator=(StoreRef const &newStore) { 268 assert(&newStore.mgr == &mgr); 269 if (store != newStore.store) { 270 mgr.incrementReferenceCount(newStore.store); 271 mgr.decrementReferenceCount(store); 272 store = newStore.getStore(); 273 } 274 return *this; 275} 276 277// FIXME: Do we still need this? 278/// SubRegionMap - An abstract interface that represents a queryable map 279/// between MemRegion objects and their subregions. 280class SubRegionMap { 281 virtual void anchor(); 282public: 283 virtual ~SubRegionMap() {} 284 285 class Visitor { 286 virtual void anchor(); 287 public: 288 virtual ~Visitor() {} 289 virtual bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) = 0; 290 }; 291 292 virtual bool iterSubRegions(const MemRegion *region, Visitor& V) const = 0; 293}; 294 295// FIXME: Do we need to pass ProgramStateManager anymore? 296StoreManager *CreateRegionStoreManager(ProgramStateManager& StMgr); 297StoreManager *CreateFieldsOnlyRegionStoreManager(ProgramStateManager& StMgr); 298 299} // end GR namespace 300 301} // end clang namespace 302 303#endif 304