clang API Documentation
00001 //== Store.h - Interface for maps from Locations to Values ------*- C++ -*--==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defined the types Store and StoreManager. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_GR_STORE_H 00015 #define LLVM_CLANG_GR_STORE_H 00016 00017 #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h" 00018 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 00019 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 00020 #include "llvm/ADT/DenseSet.h" 00021 #include "llvm/ADT/Optional.h" 00022 00023 namespace clang { 00024 00025 class Stmt; 00026 class Expr; 00027 class ObjCIvarDecl; 00028 class StackFrameContext; 00029 00030 namespace ento { 00031 00032 class CallOrObjCMessage; 00033 class ProgramState; 00034 class ProgramStateManager; 00035 class SubRegionMap; 00036 00037 class StoreManager { 00038 protected: 00039 SValBuilder &svalBuilder; 00040 ProgramStateManager &StateMgr; 00041 00042 /// MRMgr - Manages region objects associated with this StoreManager. 00043 MemRegionManager &MRMgr; 00044 ASTContext &Ctx; 00045 00046 StoreManager(ProgramStateManager &stateMgr); 00047 00048 public: 00049 virtual ~StoreManager() {} 00050 00051 /// Return the value bound to specified location in a given state. 00052 /// \param[in] state The analysis state. 00053 /// \param[in] loc The symbolic memory location. 00054 /// \param[in] T An optional type that provides a hint indicating the 00055 /// expected type of the returned value. This is used if the value is 00056 /// lazily computed. 00057 /// \return The value bound to the location \c loc. 00058 virtual SVal getBinding(Store store, Loc loc, QualType T = QualType()) = 0; 00059 00060 /// Return a state with the specified value bound to the given location. 00061 /// \param[in] state The analysis state. 00062 /// \param[in] loc The symbolic memory location. 00063 /// \param[in] val The value to bind to location \c loc. 00064 /// \return A pointer to a ProgramState object that contains the same bindings as 00065 /// \c state with the addition of having the value specified by \c val bound 00066 /// to the location given for \c loc. 00067 virtual StoreRef Bind(Store store, Loc loc, SVal val) = 0; 00068 00069 virtual StoreRef BindDefault(Store store, const MemRegion *R, SVal V); 00070 virtual StoreRef Remove(Store St, Loc L) = 0; 00071 00072 /// BindCompoundLiteral - Return the store that has the bindings currently 00073 /// in 'store' plus the bindings for the CompoundLiteral. 'R' is the region 00074 /// for the compound literal and 'BegInit' and 'EndInit' represent an 00075 /// array of initializer values. 00076 virtual StoreRef BindCompoundLiteral(Store store, 00077 const CompoundLiteralExpr *cl, 00078 const LocationContext *LC, SVal v) = 0; 00079 00080 /// getInitialStore - Returns the initial "empty" store representing the 00081 /// value bindings upon entry to an analyzed function. 00082 virtual StoreRef getInitialStore(const LocationContext *InitLoc) = 0; 00083 00084 /// getRegionManager - Returns the internal RegionManager object that is 00085 /// used to query and manipulate MemRegion objects. 00086 MemRegionManager& getRegionManager() { return MRMgr; } 00087 00088 /// getSubRegionMap - Returns an opaque map object that clients can query 00089 /// to get the subregions of a given MemRegion object. It is the 00090 // caller's responsibility to 'delete' the returned map. 00091 virtual SubRegionMap *getSubRegionMap(Store store) = 0; 00092 00093 virtual Loc getLValueVar(const VarDecl *VD, const LocationContext *LC) { 00094 return svalBuilder.makeLoc(MRMgr.getVarRegion(VD, LC)); 00095 } 00096 00097 Loc getLValueCompoundLiteral(const CompoundLiteralExpr *CL, 00098 const LocationContext *LC) { 00099 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)); 00100 } 00101 00102 virtual SVal getLValueIvar(const ObjCIvarDecl *decl, SVal base); 00103 00104 virtual SVal getLValueField(const FieldDecl *D, SVal Base) { 00105 return getLValueFieldOrIvar(D, Base); 00106 } 00107 00108 virtual SVal getLValueElement(QualType elementType, NonLoc offset, SVal Base); 00109 00110 // FIXME: This should soon be eliminated altogether; clients should deal with 00111 // region extents directly. 00112 virtual DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state, 00113 const MemRegion *region, 00114 QualType EleTy) { 00115 return UnknownVal(); 00116 } 00117 00118 /// ArrayToPointer - Used by ExprEngine::VistCast to handle implicit 00119 /// conversions between arrays and pointers. 00120 virtual SVal ArrayToPointer(Loc Array) = 0; 00121 00122 /// Evaluates DerivedToBase casts. 00123 virtual SVal evalDerivedToBase(SVal derived, QualType basePtrType) = 0; 00124 00125 /// \brief Evaluates C++ dynamic_cast cast. 00126 /// The callback may result in the following 3 scenarios: 00127 /// - Successful cast (ex: derived is subclass of base). 00128 /// - Failed cast (ex: derived is definitely not a subclass of base). 00129 /// - We don't know (base is a symbolic region and we don't have 00130 /// enough info to determine if the cast will succeed at run time). 00131 /// The function returns an SVal representing the derived class; it's 00132 /// valid only if Failed flag is set to false. 00133 virtual SVal evalDynamicCast(SVal base, QualType derivedPtrType, 00134 bool &Failed) = 0; 00135 00136 class CastResult { 00137 ProgramStateRef state; 00138 const MemRegion *region; 00139 public: 00140 ProgramStateRef getState() const { return state; } 00141 const MemRegion* getRegion() const { return region; } 00142 CastResult(ProgramStateRef s, const MemRegion* r = 0) : state(s), region(r){} 00143 }; 00144 00145 const ElementRegion *GetElementZeroRegion(const MemRegion *R, QualType T); 00146 00147 /// castRegion - Used by ExprEngine::VisitCast to handle casts from 00148 /// a MemRegion* to a specific location type. 'R' is the region being 00149 /// casted and 'CastToTy' the result type of the cast. 00150 const MemRegion *castRegion(const MemRegion *region, QualType CastToTy); 00151 00152 virtual StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx, 00153 SymbolReaper& SymReaper) = 0; 00154 00155 virtual StoreRef BindDecl(Store store, const VarRegion *VR, SVal initVal) = 0; 00156 00157 virtual StoreRef BindDeclWithNoInit(Store store, const VarRegion *VR) = 0; 00158 00159 virtual bool includedInBindings(Store store, 00160 const MemRegion *region) const = 0; 00161 00162 /// If the StoreManager supports it, increment the reference count of 00163 /// the specified Store object. 00164 virtual void incrementReferenceCount(Store store) {} 00165 00166 /// If the StoreManager supports it, decrement the reference count of 00167 /// the specified Store object. If the reference count hits 0, the memory 00168 /// associated with the object is recycled. 00169 virtual void decrementReferenceCount(Store store) {} 00170 00171 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols; 00172 typedef SmallVector<const MemRegion *, 8> InvalidatedRegions; 00173 00174 /// invalidateRegions - Clears out the specified regions from the store, 00175 /// marking their values as unknown. Depending on the store, this may also 00176 /// invalidate additional regions that may have changed based on accessing 00177 /// the given regions. Optionally, invalidates non-static globals as well. 00178 /// \param[in] store The initial store 00179 /// \param[in] Begin A pointer to the first region to invalidate. 00180 /// \param[in] End A pointer just past the last region to invalidate. 00181 /// \param[in] E The current statement being evaluated. Used to conjure 00182 /// symbols to mark the values of invalidated regions. 00183 /// \param[in] Count The current block count. Used to conjure 00184 /// symbols to mark the values of invalidated regions. 00185 /// \param[in,out] IS A set to fill with any symbols that are no longer 00186 /// accessible. Pass \c NULL if this information will not be used. 00187 /// \param[in] Call The call expression which will be used to determine which 00188 /// globals should get invalidated. 00189 /// \param[in,out] Regions A vector to fill with any regions being 00190 /// invalidated. This should include any regions explicitly invalidated 00191 /// even if they do not currently have bindings. Pass \c NULL if this 00192 /// information will not be used. 00193 virtual StoreRef invalidateRegions(Store store, 00194 ArrayRef<const MemRegion *> Regions, 00195 const Expr *E, unsigned Count, 00196 const LocationContext *LCtx, 00197 InvalidatedSymbols &IS, 00198 const CallOrObjCMessage *Call, 00199 InvalidatedRegions *Invalidated) = 0; 00200 00201 /// enterStackFrame - Let the StoreManager to do something when execution 00202 /// engine is about to execute into a callee. 00203 virtual StoreRef enterStackFrame(ProgramStateRef state, 00204 const LocationContext *callerCtx, 00205 const StackFrameContext *calleeCtx); 00206 00207 virtual void print(Store store, raw_ostream &Out, 00208 const char* nl, const char *sep) = 0; 00209 00210 class BindingsHandler { 00211 public: 00212 virtual ~BindingsHandler(); 00213 virtual bool HandleBinding(StoreManager& SMgr, Store store, 00214 const MemRegion *region, SVal val) = 0; 00215 }; 00216 00217 class FindUniqueBinding : 00218 public BindingsHandler { 00219 SymbolRef Sym; 00220 const MemRegion* Binding; 00221 bool First; 00222 00223 public: 00224 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {} 00225 00226 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R, 00227 SVal val); 00228 operator bool() { return First && Binding; } 00229 const MemRegion *getRegion() { return Binding; } 00230 }; 00231 00232 /// iterBindings - Iterate over the bindings in the Store. 00233 virtual void iterBindings(Store store, BindingsHandler& f) = 0; 00234 00235 protected: 00236 const MemRegion *MakeElementRegion(const MemRegion *baseRegion, 00237 QualType pointeeTy, uint64_t index = 0); 00238 00239 /// CastRetrievedVal - Used by subclasses of StoreManager to implement 00240 /// implicit casts that arise from loads from regions that are reinterpreted 00241 /// as another region. 00242 SVal CastRetrievedVal(SVal val, const TypedValueRegion *region, 00243 QualType castTy, bool performTestOnly = true); 00244 00245 private: 00246 SVal getLValueFieldOrIvar(const Decl *decl, SVal base); 00247 }; 00248 00249 00250 inline StoreRef::StoreRef(Store store, StoreManager & smgr) 00251 : store(store), mgr(smgr) { 00252 if (store) 00253 mgr.incrementReferenceCount(store); 00254 } 00255 00256 inline StoreRef::StoreRef(const StoreRef &sr) 00257 : store(sr.store), mgr(sr.mgr) 00258 { 00259 if (store) 00260 mgr.incrementReferenceCount(store); 00261 } 00262 00263 inline StoreRef::~StoreRef() { 00264 if (store) 00265 mgr.decrementReferenceCount(store); 00266 } 00267 00268 inline StoreRef &StoreRef::operator=(StoreRef const &newStore) { 00269 assert(&newStore.mgr == &mgr); 00270 if (store != newStore.store) { 00271 mgr.incrementReferenceCount(newStore.store); 00272 mgr.decrementReferenceCount(store); 00273 store = newStore.getStore(); 00274 } 00275 return *this; 00276 } 00277 00278 // FIXME: Do we still need this? 00279 /// SubRegionMap - An abstract interface that represents a queryable map 00280 /// between MemRegion objects and their subregions. 00281 class SubRegionMap { 00282 virtual void anchor(); 00283 public: 00284 virtual ~SubRegionMap() {} 00285 00286 class Visitor { 00287 virtual void anchor(); 00288 public: 00289 virtual ~Visitor() {} 00290 virtual bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) = 0; 00291 }; 00292 00293 virtual bool iterSubRegions(const MemRegion *region, Visitor& V) const = 0; 00294 }; 00295 00296 // FIXME: Do we need to pass ProgramStateManager anymore? 00297 StoreManager *CreateRegionStoreManager(ProgramStateManager& StMgr); 00298 StoreManager *CreateFieldsOnlyRegionStoreManager(ProgramStateManager& StMgr); 00299 00300 } // end GR namespace 00301 00302 } // end clang namespace 00303 00304 #endif