Store.h revision 8bef8238181a30e52dea380789a7e2d760eac532
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 CallOrObjCMessage; 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] state 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] state 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 bindings as 65 /// \c state with the addition of having the value specified by \c val bound 66 /// 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 virtual Loc getLValueString(const StringLiteral* S) { 98 return svalBuilder.makeLoc(MRMgr.getStringRegion(S)); 99 } 100 101 Loc getLValueCompoundLiteral(const CompoundLiteralExpr *CL, 102 const LocationContext *LC) { 103 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)); 104 } 105 106 virtual SVal getLValueIvar(const ObjCIvarDecl *decl, SVal base) { 107 return getLValueFieldOrIvar(decl, base); 108 } 109 110 virtual SVal getLValueField(const FieldDecl *D, SVal Base) { 111 return getLValueFieldOrIvar(D, Base); 112 } 113 114 virtual SVal getLValueElement(QualType elementType, NonLoc offset, SVal Base); 115 116 // FIXME: This should soon be eliminated altogether; clients should deal with 117 // region extents directly. 118 virtual DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state, 119 const MemRegion *region, 120 QualType EleTy) { 121 return UnknownVal(); 122 } 123 124 /// ArrayToPointer - Used by ExprEngine::VistCast to handle implicit 125 /// conversions between arrays and pointers. 126 virtual SVal ArrayToPointer(Loc Array) = 0; 127 128 /// Evaluates DerivedToBase casts. 129 virtual SVal evalDerivedToBase(SVal derived, QualType basePtrType) { 130 return UnknownVal(); 131 } 132 133 class CastResult { 134 ProgramStateRef state; 135 const MemRegion *region; 136 public: 137 ProgramStateRef getState() const { return state; } 138 const MemRegion* getRegion() const { return region; } 139 CastResult(ProgramStateRef s, const MemRegion* r = 0) : state(s), region(r){} 140 }; 141 142 const ElementRegion *GetElementZeroRegion(const MemRegion *R, QualType T); 143 144 /// castRegion - Used by ExprEngine::VisitCast to handle casts from 145 /// a MemRegion* to a specific location type. 'R' is the region being 146 /// casted and 'CastToTy' the result type of the cast. 147 const MemRegion *castRegion(const MemRegion *region, QualType CastToTy); 148 149 virtual StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx, 150 SymbolReaper& SymReaper) = 0; 151 152 virtual StoreRef BindDecl(Store store, const VarRegion *VR, SVal initVal) = 0; 153 154 virtual StoreRef BindDeclWithNoInit(Store store, const VarRegion *VR) = 0; 155 156 virtual bool includedInBindings(Store store, 157 const MemRegion *region) const = 0; 158 159 /// If the StoreManager supports it, increment the reference count of 160 /// the specified Store object. 161 virtual void incrementReferenceCount(Store store) {} 162 163 /// If the StoreManager supports it, decrement the reference count of 164 /// the specified Store object. If the reference count hits 0, the memory 165 /// associated with the object is recycled. 166 virtual void decrementReferenceCount(Store store) {} 167 168 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols; 169 typedef SmallVector<const MemRegion *, 8> InvalidatedRegions; 170 171 /// invalidateRegions - Clears out the specified regions from the store, 172 /// marking their values as unknown. Depending on the store, this may also 173 /// invalidate additional regions that may have changed based on accessing 174 /// the given regions. Optionally, invalidates non-static globals as well. 175 /// \param[in] store The initial store 176 /// \param[in] Begin A pointer to the first region to invalidate. 177 /// \param[in] End A pointer just past the last region to invalidate. 178 /// \param[in] E The current statement being evaluated. Used to conjure 179 /// symbols to mark the values of invalidated regions. 180 /// \param[in] Count The current block count. Used to conjure 181 /// symbols to mark the values of invalidated regions. 182 /// \param[in,out] IS A set to fill with any symbols that are no longer 183 /// accessible. Pass \c NULL if this information will not be used. 184 /// \param[in] Call The call expression which will be used to determine which 185 /// globals should get invalidated. 186 /// \param[in,out] Regions A vector to fill with any regions being 187 /// invalidated. This should include any regions explicitly invalidated 188 /// even if they do not currently have bindings. Pass \c NULL if this 189 /// information will not be used. 190 virtual StoreRef invalidateRegions(Store store, 191 ArrayRef<const MemRegion *> Regions, 192 const Expr *E, unsigned Count, 193 InvalidatedSymbols &IS, 194 const CallOrObjCMessage *Call, 195 InvalidatedRegions *Invalidated) = 0; 196 197 /// enterStackFrame - Let the StoreManager to do something when execution 198 /// engine is about to execute into a callee. 199 virtual StoreRef enterStackFrame(ProgramStateRef state, 200 const LocationContext *callerCtx, 201 const StackFrameContext *calleeCtx); 202 203 virtual void print(Store store, raw_ostream &Out, 204 const char* nl, const char *sep) = 0; 205 206 class BindingsHandler { 207 public: 208 virtual ~BindingsHandler(); 209 virtual bool HandleBinding(StoreManager& SMgr, Store store, 210 const MemRegion *region, SVal val) = 0; 211 }; 212 213 /// iterBindings - Iterate over the bindings in the Store. 214 virtual void iterBindings(Store store, BindingsHandler& f) = 0; 215 216protected: 217 const MemRegion *MakeElementRegion(const MemRegion *baseRegion, 218 QualType pointeeTy, uint64_t index = 0); 219 220 /// CastRetrievedVal - Used by subclasses of StoreManager to implement 221 /// implicit casts that arise from loads from regions that are reinterpreted 222 /// as another region. 223 SVal CastRetrievedVal(SVal val, const TypedValueRegion *region, 224 QualType castTy, bool performTestOnly = true); 225 226private: 227 SVal getLValueFieldOrIvar(const Decl *decl, SVal base); 228}; 229 230 231inline StoreRef::StoreRef(Store store, StoreManager & smgr) 232 : store(store), mgr(smgr) { 233 if (store) 234 mgr.incrementReferenceCount(store); 235} 236 237inline StoreRef::StoreRef(const StoreRef &sr) 238 : store(sr.store), mgr(sr.mgr) 239{ 240 if (store) 241 mgr.incrementReferenceCount(store); 242} 243 244inline StoreRef::~StoreRef() { 245 if (store) 246 mgr.decrementReferenceCount(store); 247} 248 249inline StoreRef &StoreRef::operator=(StoreRef const &newStore) { 250 assert(&newStore.mgr == &mgr); 251 if (store != newStore.store) { 252 mgr.incrementReferenceCount(newStore.store); 253 mgr.decrementReferenceCount(store); 254 store = newStore.getStore(); 255 } 256 return *this; 257} 258 259// FIXME: Do we still need this? 260/// SubRegionMap - An abstract interface that represents a queryable map 261/// between MemRegion objects and their subregions. 262class SubRegionMap { 263 virtual void anchor(); 264public: 265 virtual ~SubRegionMap() {} 266 267 class Visitor { 268 virtual void anchor(); 269 public: 270 virtual ~Visitor() {} 271 virtual bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) = 0; 272 }; 273 274 virtual bool iterSubRegions(const MemRegion *region, Visitor& V) const = 0; 275}; 276 277// FIXME: Do we need to pass ProgramStateManager anymore? 278StoreManager *CreateRegionStoreManager(ProgramStateManager& StMgr); 279StoreManager *CreateFieldsOnlyRegionStoreManager(ProgramStateManager& StMgr); 280 281} // end GR namespace 282 283} // end clang namespace 284 285#endif 286