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 ScanReachableSymbols; 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 71 /// \brief Create a new store with the specified binding removed. 72 /// \param ST the original store, that is the basis for the new store. 73 /// \param L the location whose binding should be removed. 74 virtual StoreRef killBinding(Store ST, Loc L) = 0; 75 76 /// \brief Create a new store that binds a value to a compound literal. 77 /// 78 /// \param ST The original store whose bindings are the basis for the new 79 /// store. 80 /// 81 /// \param CL The compound literal to bind (the binding key). 82 /// 83 /// \param LC The LocationContext for the binding. 84 /// 85 /// \param V The value to bind to the compound literal. 86 virtual StoreRef bindCompoundLiteral(Store ST, 87 const CompoundLiteralExpr *CL, 88 const LocationContext *LC, 89 SVal V) = 0; 90 91 /// getInitialStore - Returns the initial "empty" store representing the 92 /// value bindings upon entry to an analyzed function. 93 virtual StoreRef getInitialStore(const LocationContext *InitLoc) = 0; 94 95 /// getRegionManager - Returns the internal RegionManager object that is 96 /// used to query and manipulate MemRegion objects. 97 MemRegionManager& getRegionManager() { return MRMgr; } 98 99 virtual Loc getLValueVar(const VarDecl *VD, const LocationContext *LC) { 100 return svalBuilder.makeLoc(MRMgr.getVarRegion(VD, LC)); 101 } 102 103 Loc getLValueCompoundLiteral(const CompoundLiteralExpr *CL, 104 const LocationContext *LC) { 105 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)); 106 } 107 108 virtual SVal getLValueIvar(const ObjCIvarDecl *decl, SVal base); 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 SVal evalDerivedToBase(SVal derived, const CastExpr *Cast); 130 131 /// Evaluates a derived-to-base cast through a single level of derivation. 132 virtual SVal evalDerivedToBase(SVal derived, QualType derivedPtrType) = 0; 133 134 /// \brief Evaluates C++ dynamic_cast cast. 135 /// The callback may result in the following 3 scenarios: 136 /// - Successful cast (ex: derived is subclass of base). 137 /// - Failed cast (ex: derived is definitely not a subclass of base). 138 /// - We don't know (base is a symbolic region and we don't have 139 /// enough info to determine if the cast will succeed at run time). 140 /// The function returns an SVal representing the derived class; it's 141 /// valid only if Failed flag is set to false. 142 virtual SVal evalDynamicCast(SVal base, QualType derivedPtrType, 143 bool &Failed) = 0; 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 bool includedInBindings(Store store, 156 const MemRegion *region) const = 0; 157 158 /// If the StoreManager supports it, increment the reference count of 159 /// the specified Store object. 160 virtual void incrementReferenceCount(Store store) {} 161 162 /// If the StoreManager supports it, decrement the reference count of 163 /// the specified Store object. If the reference count hits 0, the memory 164 /// associated with the object is recycled. 165 virtual void decrementReferenceCount(Store store) {} 166 167 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols; 168 typedef SmallVector<const MemRegion *, 8> InvalidatedRegions; 169 170 /// invalidateRegions - Clears out the specified regions from the store, 171 /// marking their values as unknown. Depending on the store, this may also 172 /// invalidate additional regions that may have changed based on accessing 173 /// the given regions. Optionally, invalidates non-static globals as well. 174 /// \param[in] store The initial store 175 /// \param[in] Regions The regions to invalidate. 176 /// \param[in] E The current statement being evaluated. Used to conjure 177 /// symbols to mark the values of invalidated regions. 178 /// \param[in] Count The current block count. Used to conjure 179 /// symbols to mark the values of invalidated regions. 180 /// \param[in,out] IS A set to fill with any symbols that are no longer 181 /// accessible. Pass \c NULL if this information will not be used. 182 /// \param[in] Call The call expression which will be used to determine which 183 /// globals should get invalidated. 184 /// \param[in,out] Invalidated A vector to fill with any regions being 185 /// invalidated. This should include any regions explicitly invalidated 186 /// even if they do not currently have bindings. Pass \c NULL if this 187 /// information will not be used. 188 virtual StoreRef invalidateRegions(Store store, 189 ArrayRef<const MemRegion *> Regions, 190 const Expr *E, unsigned Count, 191 const LocationContext *LCtx, 192 InvalidatedSymbols &IS, 193 const CallEvent *Call, 194 InvalidatedRegions *Invalidated) = 0; 195 196 /// enterStackFrame - Let the StoreManager to do something when execution 197 /// engine is about to execute into a callee. 198 StoreRef enterStackFrame(Store store, 199 const CallEvent &Call, 200 const StackFrameContext *CalleeCtx); 201 202 /// Finds the transitive closure of symbols within the given region. 203 /// 204 /// Returns false if the visitor aborted the scan. 205 virtual bool scanReachableSymbols(Store S, const MemRegion *R, 206 ScanReachableSymbols &Visitor) = 0; 207 208 virtual void print(Store store, raw_ostream &Out, 209 const char* nl, const char *sep) = 0; 210 211 class BindingsHandler { 212 public: 213 virtual ~BindingsHandler(); 214 virtual bool HandleBinding(StoreManager& SMgr, Store store, 215 const MemRegion *region, SVal val) = 0; 216 }; 217 218 class FindUniqueBinding : 219 public BindingsHandler { 220 SymbolRef Sym; 221 const MemRegion* Binding; 222 bool First; 223 224 public: 225 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {} 226 227 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R, 228 SVal val); 229 operator bool() { return First && Binding; } 230 const MemRegion *getRegion() { return Binding; } 231 }; 232 233 /// iterBindings - Iterate over the bindings in the Store. 234 virtual void iterBindings(Store store, BindingsHandler& f) = 0; 235 236protected: 237 const MemRegion *MakeElementRegion(const MemRegion *baseRegion, 238 QualType pointeeTy, uint64_t index = 0); 239 240 /// CastRetrievedVal - Used by subclasses of StoreManager to implement 241 /// implicit casts that arise from loads from regions that are reinterpreted 242 /// as another region. 243 SVal CastRetrievedVal(SVal val, const TypedValueRegion *region, 244 QualType castTy, bool performTestOnly = true); 245 246private: 247 SVal getLValueFieldOrIvar(const Decl *decl, SVal base); 248}; 249 250 251inline StoreRef::StoreRef(Store store, StoreManager & smgr) 252 : store(store), mgr(smgr) { 253 if (store) 254 mgr.incrementReferenceCount(store); 255} 256 257inline StoreRef::StoreRef(const StoreRef &sr) 258 : store(sr.store), mgr(sr.mgr) 259{ 260 if (store) 261 mgr.incrementReferenceCount(store); 262} 263 264inline StoreRef::~StoreRef() { 265 if (store) 266 mgr.decrementReferenceCount(store); 267} 268 269inline StoreRef &StoreRef::operator=(StoreRef const &newStore) { 270 assert(&newStore.mgr == &mgr); 271 if (store != newStore.store) { 272 mgr.incrementReferenceCount(newStore.store); 273 mgr.decrementReferenceCount(store); 274 store = newStore.getStore(); 275 } 276 return *this; 277} 278 279// FIXME: Do we need to pass ProgramStateManager anymore? 280StoreManager *CreateRegionStoreManager(ProgramStateManager& StMgr); 281StoreManager *CreateFieldsOnlyRegionStoreManager(ProgramStateManager& StMgr); 282 283} // end GR namespace 284 285} // end clang namespace 286 287#endif 288