1 //== Store.h - Interface for maps from Locations to Values ------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defined the types Store and StoreManager.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_STORE_H
15 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_STORE_H
17 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
20 #include "llvm/ADT/DenseSet.h"
28 class StackFrameContext;
34 class ProgramStateManager;
35 class ScanReachableSymbols;
37 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols;
41 SValBuilder &svalBuilder;
42 ProgramStateManager &StateMgr;
44 /// MRMgr - Manages region objects associated with this StoreManager.
45 MemRegionManager &MRMgr;
48 StoreManager(ProgramStateManager &stateMgr);
51 virtual ~StoreManager() {}
53 /// Return the value bound to specified location in a given state.
54 /// \param[in] store The analysis state.
55 /// \param[in] loc The symbolic memory location.
56 /// \param[in] T An optional type that provides a hint indicating the
57 /// expected type of the returned value. This is used if the value is
59 /// \return The value bound to the location \c loc.
60 virtual SVal getBinding(Store store, Loc loc, QualType T = QualType()) = 0;
62 /// Return a state with the specified value bound to the given location.
63 /// \param[in] store The analysis state.
64 /// \param[in] loc The symbolic memory location.
65 /// \param[in] val The value to bind to location \c loc.
66 /// \return A pointer to a ProgramState object that contains the same
67 /// bindings as \c state with the addition of having the value specified
68 /// by \c val bound to the location given for \c loc.
69 virtual StoreRef Bind(Store store, Loc loc, SVal val) = 0;
71 virtual StoreRef BindDefault(Store store, const MemRegion *R, SVal V);
73 /// \brief Create a new store with the specified binding removed.
74 /// \param ST the original store, that is the basis for the new store.
75 /// \param L the location whose binding should be removed.
76 virtual StoreRef killBinding(Store ST, Loc L) = 0;
78 /// getInitialStore - Returns the initial "empty" store representing the
79 /// value bindings upon entry to an analyzed function.
80 virtual StoreRef getInitialStore(const LocationContext *InitLoc) = 0;
82 /// getRegionManager - Returns the internal RegionManager object that is
83 /// used to query and manipulate MemRegion objects.
84 MemRegionManager& getRegionManager() { return MRMgr; }
86 virtual Loc getLValueVar(const VarDecl *VD, const LocationContext *LC) {
87 return svalBuilder.makeLoc(MRMgr.getVarRegion(VD, LC));
90 Loc getLValueCompoundLiteral(const CompoundLiteralExpr *CL,
91 const LocationContext *LC) {
92 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC));
95 virtual SVal getLValueIvar(const ObjCIvarDecl *decl, SVal base);
97 virtual SVal getLValueField(const FieldDecl *D, SVal Base) {
98 return getLValueFieldOrIvar(D, Base);
101 virtual SVal getLValueElement(QualType elementType, NonLoc offset, SVal Base);
103 // FIXME: This should soon be eliminated altogether; clients should deal with
104 // region extents directly.
105 virtual DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state,
106 const MemRegion *region,
111 /// ArrayToPointer - Used by ExprEngine::VistCast to handle implicit
112 /// conversions between arrays and pointers.
113 virtual SVal ArrayToPointer(Loc Array, QualType ElementTy) = 0;
115 /// Evaluates a chain of derived-to-base casts through the path specified in
117 SVal evalDerivedToBase(SVal Derived, const CastExpr *Cast);
119 /// Evaluates a chain of derived-to-base casts through the specified path.
120 SVal evalDerivedToBase(SVal Derived, const CXXBasePath &CastPath);
122 /// Evaluates a derived-to-base cast through a single level of derivation.
123 SVal evalDerivedToBase(SVal Derived, QualType DerivedPtrType,
126 /// \brief Attempts to do a down cast. Used to model BaseToDerived and C++
128 /// The callback may result in the following 3 scenarios:
129 /// - Successful cast (ex: derived is subclass of base).
130 /// - Failed cast (ex: derived is definitely not a subclass of base).
131 /// The distinction of this case from the next one is necessary to model
133 /// - We don't know (base is a symbolic region and we don't have
134 /// enough info to determine if the cast will succeed at run time).
135 /// The function returns an SVal representing the derived class; it's
136 /// valid only if Failed flag is set to false.
137 SVal attemptDownCast(SVal Base, QualType DerivedPtrType, bool &Failed);
139 const ElementRegion *GetElementZeroRegion(const MemRegion *R, QualType T);
141 /// castRegion - Used by ExprEngine::VisitCast to handle casts from
142 /// a MemRegion* to a specific location type. 'R' is the region being
143 /// casted and 'CastToTy' the result type of the cast.
144 const MemRegion *castRegion(const MemRegion *region, QualType CastToTy);
146 virtual StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx,
147 SymbolReaper& SymReaper) = 0;
149 virtual bool includedInBindings(Store store,
150 const MemRegion *region) const = 0;
152 /// If the StoreManager supports it, increment the reference count of
153 /// the specified Store object.
154 virtual void incrementReferenceCount(Store store) {}
156 /// If the StoreManager supports it, decrement the reference count of
157 /// the specified Store object. If the reference count hits 0, the memory
158 /// associated with the object is recycled.
159 virtual void decrementReferenceCount(Store store) {}
161 typedef SmallVector<const MemRegion *, 8> InvalidatedRegions;
163 /// invalidateRegions - Clears out the specified regions from the store,
164 /// marking their values as unknown. Depending on the store, this may also
165 /// invalidate additional regions that may have changed based on accessing
166 /// the given regions. Optionally, invalidates non-static globals as well.
167 /// \param[in] store The initial store
168 /// \param[in] Values The values to invalidate.
169 /// \param[in] E The current statement being evaluated. Used to conjure
170 /// symbols to mark the values of invalidated regions.
171 /// \param[in] Count The current block count. Used to conjure
172 /// symbols to mark the values of invalidated regions.
173 /// \param[in] Call The call expression which will be used to determine which
174 /// globals should get invalidated.
175 /// \param[in,out] IS A set to fill with any symbols that are no longer
176 /// accessible. Pass \c NULL if this information will not be used.
177 /// \param[in] ITraits Information about invalidation for a particular
179 /// \param[in,out] InvalidatedTopLevel A vector to fill with regions
180 //// explicitly being invalidated. Pass \c NULL if this
181 /// information will not be used.
182 /// \param[in,out] Invalidated A vector to fill with any regions being
183 /// invalidated. This should include any regions explicitly invalidated
184 /// even if they do not currently have bindings. Pass \c NULL if this
185 /// information will not be used.
186 virtual StoreRef invalidateRegions(Store store,
187 ArrayRef<SVal> Values,
188 const Expr *E, unsigned Count,
189 const LocationContext *LCtx,
190 const CallEvent *Call,
191 InvalidatedSymbols &IS,
192 RegionAndSymbolInvalidationTraits &ITraits,
193 InvalidatedRegions *InvalidatedTopLevel,
194 InvalidatedRegions *Invalidated) = 0;
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);
202 /// Finds the transitive closure of symbols within the given region.
204 /// Returns false if the visitor aborted the scan.
205 virtual bool scanReachableSymbols(Store S, const MemRegion *R,
206 ScanReachableSymbols &Visitor) = 0;
208 virtual void print(Store store, raw_ostream &Out,
209 const char* nl, const char *sep) = 0;
211 class BindingsHandler {
213 virtual ~BindingsHandler();
214 virtual bool HandleBinding(StoreManager& SMgr, Store store,
215 const MemRegion *region, SVal val) = 0;
218 class FindUniqueBinding :
219 public BindingsHandler {
221 const MemRegion* Binding;
225 FindUniqueBinding(SymbolRef sym)
226 : Sym(sym), Binding(nullptr), First(true) {}
228 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
230 explicit operator bool() { return First && Binding; }
231 const MemRegion *getRegion() { return Binding; }
234 /// iterBindings - Iterate over the bindings in the Store.
235 virtual void iterBindings(Store store, BindingsHandler& f) = 0;
238 const MemRegion *MakeElementRegion(const MemRegion *baseRegion,
239 QualType pointeeTy, uint64_t index = 0);
241 /// CastRetrievedVal - Used by subclasses of StoreManager to implement
242 /// implicit casts that arise from loads from regions that are reinterpreted
243 /// as another region.
244 SVal CastRetrievedVal(SVal val, const TypedValueRegion *region,
245 QualType castTy, bool performTestOnly = true);
248 SVal getLValueFieldOrIvar(const Decl *decl, SVal base);
252 inline StoreRef::StoreRef(Store store, StoreManager & smgr)
253 : store(store), mgr(smgr) {
255 mgr.incrementReferenceCount(store);
258 inline StoreRef::StoreRef(const StoreRef &sr)
259 : store(sr.store), mgr(sr.mgr)
262 mgr.incrementReferenceCount(store);
265 inline StoreRef::~StoreRef() {
267 mgr.decrementReferenceCount(store);
270 inline StoreRef &StoreRef::operator=(StoreRef const &newStore) {
271 assert(&newStore.mgr == &mgr);
272 if (store != newStore.store) {
273 mgr.incrementReferenceCount(newStore.store);
274 mgr.decrementReferenceCount(store);
275 store = newStore.getStore();
280 // FIXME: Do we need to pass ProgramStateManager anymore?
281 std::unique_ptr<StoreManager>
282 CreateRegionStoreManager(ProgramStateManager &StMgr);
283 std::unique_ptr<StoreManager>
284 CreateFieldsOnlyRegionStoreManager(ProgramStateManager &StMgr);
286 } // end GR namespace
288 } // end clang namespace