clang API Documentation

Store.h
Go to the documentation of this file.
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_STATICANALYZER_CORE_PATHSENSITIVE_STORE_H
00015 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_STORE_H
00016 
00017 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
00018 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
00019 #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.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 CXXBasePath;
00029 class StackFrameContext;
00030 
00031 namespace ento {
00032 
00033 class CallEvent;
00034 class ProgramState;
00035 class ProgramStateManager;
00036 class ScanReachableSymbols;
00037 
00038 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols;
00039 
00040 class StoreManager {
00041 protected:
00042   SValBuilder &svalBuilder;
00043   ProgramStateManager &StateMgr;
00044 
00045   /// MRMgr - Manages region objects associated with this StoreManager.
00046   MemRegionManager &MRMgr;
00047   ASTContext &Ctx;
00048 
00049   StoreManager(ProgramStateManager &stateMgr);
00050 
00051 public:
00052   virtual ~StoreManager() {}
00053 
00054   /// Return the value bound to specified location in a given state.
00055   /// \param[in] store The analysis state.
00056   /// \param[in] loc The symbolic memory location.
00057   /// \param[in] T An optional type that provides a hint indicating the
00058   ///   expected type of the returned value.  This is used if the value is
00059   ///   lazily computed.
00060   /// \return The value bound to the location \c loc.
00061   virtual SVal getBinding(Store store, Loc loc, QualType T = QualType()) = 0;
00062 
00063   /// Return a state with the specified value bound to the given location.
00064   /// \param[in] store The analysis state.
00065   /// \param[in] loc The symbolic memory location.
00066   /// \param[in] val The value to bind to location \c loc.
00067   /// \return A pointer to a ProgramState object that contains the same
00068   ///   bindings as \c state with the addition of having the value specified
00069   ///   by \c val bound to the location given for \c loc.
00070   virtual StoreRef Bind(Store store, Loc loc, SVal val) = 0;
00071 
00072   virtual StoreRef BindDefault(Store store, const MemRegion *R, SVal V);
00073 
00074   /// \brief Create a new store with the specified binding removed.
00075   /// \param ST the original store, that is the basis for the new store.
00076   /// \param L the location whose binding should be removed.
00077   virtual StoreRef killBinding(Store ST, Loc L) = 0;
00078 
00079   /// getInitialStore - Returns the initial "empty" store representing the
00080   ///  value bindings upon entry to an analyzed function.
00081   virtual StoreRef getInitialStore(const LocationContext *InitLoc) = 0;
00082 
00083   /// getRegionManager - Returns the internal RegionManager object that is
00084   ///  used to query and manipulate MemRegion objects.
00085   MemRegionManager& getRegionManager() { return MRMgr; }
00086 
00087   virtual Loc getLValueVar(const VarDecl *VD, const LocationContext *LC) {
00088     return svalBuilder.makeLoc(MRMgr.getVarRegion(VD, LC));
00089   }
00090 
00091   Loc getLValueCompoundLiteral(const CompoundLiteralExpr *CL,
00092                                const LocationContext *LC) {
00093     return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC));
00094   }
00095 
00096   virtual SVal getLValueIvar(const ObjCIvarDecl *decl, SVal base);
00097 
00098   virtual SVal getLValueField(const FieldDecl *D, SVal Base) {
00099     return getLValueFieldOrIvar(D, Base);
00100   }
00101 
00102   virtual SVal getLValueElement(QualType elementType, NonLoc offset, SVal Base);
00103 
00104   // FIXME: This should soon be eliminated altogether; clients should deal with
00105   // region extents directly.
00106   virtual DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state, 
00107                                                  const MemRegion *region,
00108                                                  QualType EleTy) {
00109     return UnknownVal();
00110   }
00111 
00112   /// ArrayToPointer - Used by ExprEngine::VistCast to handle implicit
00113   ///  conversions between arrays and pointers.
00114   virtual SVal ArrayToPointer(Loc Array, QualType ElementTy) = 0;
00115 
00116   /// Evaluates a chain of derived-to-base casts through the path specified in
00117   /// \p Cast.
00118   SVal evalDerivedToBase(SVal Derived, const CastExpr *Cast);
00119 
00120   /// Evaluates a chain of derived-to-base casts through the specified path.
00121   SVal evalDerivedToBase(SVal Derived, const CXXBasePath &CastPath);
00122 
00123   /// Evaluates a derived-to-base cast through a single level of derivation.
00124   SVal evalDerivedToBase(SVal Derived, QualType DerivedPtrType,
00125                          bool IsVirtual);
00126 
00127   /// \brief Evaluates C++ dynamic_cast cast.
00128   /// The callback may result in the following 3 scenarios:
00129   ///  - Successful cast (ex: derived is subclass of base).
00130   ///  - Failed cast (ex: derived is definitely not a subclass of base).
00131   ///  - We don't know (base is a symbolic region and we don't have 
00132   ///    enough info to determine if the cast will succeed at run time).
00133   /// The function returns an SVal representing the derived class; it's
00134   /// valid only if Failed flag is set to false.
00135   SVal evalDynamicCast(SVal Base, QualType DerivedPtrType, bool &Failed);
00136 
00137   const ElementRegion *GetElementZeroRegion(const MemRegion *R, QualType T);
00138 
00139   /// castRegion - Used by ExprEngine::VisitCast to handle casts from
00140   ///  a MemRegion* to a specific location type.  'R' is the region being
00141   ///  casted and 'CastToTy' the result type of the cast.
00142   const MemRegion *castRegion(const MemRegion *region, QualType CastToTy);
00143 
00144   virtual StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx,
00145                                       SymbolReaper& SymReaper) = 0;
00146 
00147   virtual bool includedInBindings(Store store,
00148                                   const MemRegion *region) const = 0;
00149   
00150   /// If the StoreManager supports it, increment the reference count of
00151   /// the specified Store object.
00152   virtual void incrementReferenceCount(Store store) {}
00153 
00154   /// If the StoreManager supports it, decrement the reference count of
00155   /// the specified Store object.  If the reference count hits 0, the memory
00156   /// associated with the object is recycled.
00157   virtual void decrementReferenceCount(Store store) {}
00158 
00159   typedef SmallVector<const MemRegion *, 8> InvalidatedRegions;
00160 
00161   /// invalidateRegions - Clears out the specified regions from the store,
00162   ///  marking their values as unknown. Depending on the store, this may also
00163   ///  invalidate additional regions that may have changed based on accessing
00164   ///  the given regions. Optionally, invalidates non-static globals as well.
00165   /// \param[in] store The initial store
00166   /// \param[in] Values The values to invalidate.
00167   /// \param[in] E The current statement being evaluated. Used to conjure
00168   ///   symbols to mark the values of invalidated regions.
00169   /// \param[in] Count The current block count. Used to conjure
00170   ///   symbols to mark the values of invalidated regions.
00171   /// \param[in] Call The call expression which will be used to determine which
00172   ///   globals should get invalidated.
00173   /// \param[in,out] IS A set to fill with any symbols that are no longer
00174   ///   accessible. Pass \c NULL if this information will not be used.
00175   /// \param[in] ITraits Information about invalidation for a particular 
00176   ///   region/symbol.
00177   /// \param[in,out] InvalidatedTopLevel A vector to fill with regions
00178   ////  explicitly being invalidated. Pass \c NULL if this
00179   ///   information will not be used.
00180   /// \param[in,out] Invalidated A vector to fill with any regions being
00181   ///   invalidated. This should include any regions explicitly invalidated
00182   ///   even if they do not currently have bindings. Pass \c NULL if this
00183   ///   information will not be used.
00184   virtual StoreRef invalidateRegions(Store store,
00185                                   ArrayRef<SVal> Values,
00186                                   const Expr *E, unsigned Count,
00187                                   const LocationContext *LCtx,
00188                                   const CallEvent *Call,
00189                                   InvalidatedSymbols &IS,
00190                                   RegionAndSymbolInvalidationTraits &ITraits,
00191                                   InvalidatedRegions *InvalidatedTopLevel,
00192                                   InvalidatedRegions *Invalidated) = 0;
00193 
00194   /// enterStackFrame - Let the StoreManager to do something when execution
00195   /// engine is about to execute into a callee.
00196   StoreRef enterStackFrame(Store store,
00197                            const CallEvent &Call,
00198                            const StackFrameContext *CalleeCtx);
00199 
00200   /// Finds the transitive closure of symbols within the given region.
00201   ///
00202   /// Returns false if the visitor aborted the scan.
00203   virtual bool scanReachableSymbols(Store S, const MemRegion *R,
00204                                     ScanReachableSymbols &Visitor) = 0;
00205 
00206   virtual void print(Store store, raw_ostream &Out,
00207                      const char* nl, const char *sep) = 0;
00208 
00209   class BindingsHandler {
00210   public:
00211     virtual ~BindingsHandler();
00212     virtual bool HandleBinding(StoreManager& SMgr, Store store,
00213                                const MemRegion *region, SVal val) = 0;
00214   };
00215 
00216   class FindUniqueBinding :
00217   public BindingsHandler {
00218     SymbolRef Sym;
00219     const MemRegion* Binding;
00220     bool First;
00221 
00222   public:
00223     FindUniqueBinding(SymbolRef sym)
00224       : Sym(sym), Binding(nullptr), First(true) {}
00225 
00226     bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
00227                        SVal val) override;
00228     LLVM_EXPLICIT 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 need to pass ProgramStateManager anymore?
00279 std::unique_ptr<StoreManager>
00280 CreateRegionStoreManager(ProgramStateManager &StMgr);
00281 std::unique_ptr<StoreManager>
00282 CreateFieldsOnlyRegionStoreManager(ProgramStateManager &StMgr);
00283 
00284 } // end GR namespace
00285 
00286 } // end clang namespace
00287 
00288 #endif