clang  6.0.0svn
Store.h
Go to the documentation of this file.
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_STATICANALYZER_CORE_PATHSENSITIVE_STORE_H
15 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_STORE_H
16 
20 #include "llvm/ADT/DenseSet.h"
21 
22 namespace clang {
23 
24 class Stmt;
25 class Expr;
26 class ObjCIvarDecl;
27 class CXXBasePath;
28 class StackFrameContext;
29 
30 namespace ento {
31 
32 class CallEvent;
33 class ProgramState;
34 class ProgramStateManager;
36 
38 
39 class StoreManager {
40 protected:
43 
44  /// MRMgr - Manages region objects associated with this StoreManager.
47 
49 
50 public:
51  virtual ~StoreManager() {}
52 
53  /// Return the value bound to specified location in a given state.
54  /// \param[in] store The store in which to make the lookup.
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
58  /// lazily computed.
59  /// \return The value bound to the location \c loc.
60  virtual SVal getBinding(Store store, Loc loc, QualType T = QualType()) = 0;
61 
62  /// Return the default value bound to a region in a given store. The default
63  /// binding is the value of sub-regions that were not initialized separately
64  /// from their base region. For example, if the structure is zero-initialized
65  /// upon construction, this method retrieves the concrete zero value, even if
66  /// some or all fields were later overwritten manually. Default binding may be
67  /// an unknown, undefined, concrete, or symbolic value.
68  /// \param[in] store The store in which to make the lookup.
69  /// \param[in] R The region to find the default binding for.
70  /// \return The default value bound to the region in the store, if a default
71  /// binding exists.
72  virtual Optional<SVal> getDefaultBinding(Store store, const MemRegion *R) = 0;
73 
74  /// Return the default value bound to a LazyCompoundVal. The default binding
75  /// is used to represent the value of any fields or elements within the
76  /// structure represented by the LazyCompoundVal which were not initialized
77  /// explicitly separately from the whole structure. Default binding may be an
78  /// unknown, undefined, concrete, or symbolic value.
79  /// \param[in] lcv The lazy compound value.
80  /// \return The default value bound to the LazyCompoundVal \c lcv, if a
81  /// default binding exists.
83  return getDefaultBinding(lcv.getStore(), lcv.getRegion());
84  }
85 
86  /// Return a store with the specified value bound to the given location.
87  /// \param[in] store The store in which to make the binding.
88  /// \param[in] loc The symbolic memory location.
89  /// \param[in] val The value to bind to location \c loc.
90  /// \return A StoreRef object that contains the same
91  /// bindings as \c store with the addition of having the value specified
92  /// by \c val bound to the location given for \c loc.
93  virtual StoreRef Bind(Store store, Loc loc, SVal val) = 0;
94 
95  virtual StoreRef BindDefault(Store store, const MemRegion *R, SVal V);
96 
97  /// \brief Create a new store with the specified binding removed.
98  /// \param ST the original store, that is the basis for the new store.
99  /// \param L the location whose binding should be removed.
100  virtual StoreRef killBinding(Store ST, Loc L) = 0;
101 
102  /// getInitialStore - Returns the initial "empty" store representing the
103  /// value bindings upon entry to an analyzed function.
104  virtual StoreRef getInitialStore(const LocationContext *InitLoc) = 0;
105 
106  /// getRegionManager - Returns the internal RegionManager object that is
107  /// used to query and manipulate MemRegion objects.
109 
110  virtual Loc getLValueVar(const VarDecl *VD, const LocationContext *LC) {
111  return svalBuilder.makeLoc(MRMgr.getVarRegion(VD, LC));
112  }
113 
115  const LocationContext *LC) {
116  return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC));
117  }
118 
119  virtual SVal getLValueIvar(const ObjCIvarDecl *decl, SVal base);
120 
121  virtual SVal getLValueField(const FieldDecl *D, SVal Base) {
122  return getLValueFieldOrIvar(D, Base);
123  }
124 
125  virtual SVal getLValueElement(QualType elementType, NonLoc offset, SVal Base);
126 
127  // FIXME: This should soon be eliminated altogether; clients should deal with
128  // region extents directly.
130  const MemRegion *region,
131  QualType EleTy) {
132  return UnknownVal();
133  }
134 
135  /// ArrayToPointer - Used by ExprEngine::VistCast to handle implicit
136  /// conversions between arrays and pointers.
137  virtual SVal ArrayToPointer(Loc Array, QualType ElementTy) = 0;
138 
139  /// Evaluates a chain of derived-to-base casts through the path specified in
140  /// \p Cast.
141  SVal evalDerivedToBase(SVal Derived, const CastExpr *Cast);
142 
143  /// Evaluates a chain of derived-to-base casts through the specified path.
144  SVal evalDerivedToBase(SVal Derived, const CXXBasePath &CastPath);
145 
146  /// Evaluates a derived-to-base cast through a single level of derivation.
147  SVal evalDerivedToBase(SVal Derived, QualType DerivedPtrType,
148  bool IsVirtual);
149 
150  /// \brief Attempts to do a down cast. Used to model BaseToDerived and C++
151  /// dynamic_cast.
152  /// The callback may result in the following 3 scenarios:
153  /// - Successful cast (ex: derived is subclass of base).
154  /// - Failed cast (ex: derived is definitely not a subclass of base).
155  /// The distinction of this case from the next one is necessary to model
156  /// dynamic_cast.
157  /// - We don't know (base is a symbolic region and we don't have
158  /// enough info to determine if the cast will succeed at run time).
159  /// The function returns an SVal representing the derived class; it's
160  /// valid only if Failed flag is set to false.
161  SVal attemptDownCast(SVal Base, QualType DerivedPtrType, bool &Failed);
162 
164 
165  /// castRegion - Used by ExprEngine::VisitCast to handle casts from
166  /// a MemRegion* to a specific location type. 'R' is the region being
167  /// casted and 'CastToTy' the result type of the cast.
168  const MemRegion *castRegion(const MemRegion *region, QualType CastToTy);
169 
170  virtual StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx,
171  SymbolReaper& SymReaper) = 0;
172 
173  virtual bool includedInBindings(Store store,
174  const MemRegion *region) const = 0;
175 
176  /// If the StoreManager supports it, increment the reference count of
177  /// the specified Store object.
178  virtual void incrementReferenceCount(Store store) {}
179 
180  /// If the StoreManager supports it, decrement the reference count of
181  /// the specified Store object. If the reference count hits 0, the memory
182  /// associated with the object is recycled.
183  virtual void decrementReferenceCount(Store store) {}
184 
186 
187  /// invalidateRegions - Clears out the specified regions from the store,
188  /// marking their values as unknown. Depending on the store, this may also
189  /// invalidate additional regions that may have changed based on accessing
190  /// the given regions. Optionally, invalidates non-static globals as well.
191  /// \param[in] store The initial store
192  /// \param[in] Values The values to invalidate.
193  /// \param[in] E The current statement being evaluated. Used to conjure
194  /// symbols to mark the values of invalidated regions.
195  /// \param[in] Count The current block count. Used to conjure
196  /// symbols to mark the values of invalidated regions.
197  /// \param[in] Call The call expression which will be used to determine which
198  /// globals should get invalidated.
199  /// \param[in,out] IS A set to fill with any symbols that are no longer
200  /// accessible. Pass \c NULL if this information will not be used.
201  /// \param[in] ITraits Information about invalidation for a particular
202  /// region/symbol.
203  /// \param[in,out] InvalidatedTopLevel A vector to fill with regions
204  //// explicitly being invalidated. Pass \c NULL if this
205  /// information will not be used.
206  /// \param[in,out] Invalidated A vector to fill with any regions being
207  /// invalidated. This should include any regions explicitly invalidated
208  /// even if they do not currently have bindings. Pass \c NULL if this
209  /// information will not be used.
210  virtual StoreRef invalidateRegions(Store store,
211  ArrayRef<SVal> Values,
212  const Expr *E, unsigned Count,
213  const LocationContext *LCtx,
214  const CallEvent *Call,
215  InvalidatedSymbols &IS,
217  InvalidatedRegions *InvalidatedTopLevel,
218  InvalidatedRegions *Invalidated) = 0;
219 
220  /// enterStackFrame - Let the StoreManager to do something when execution
221  /// engine is about to execute into a callee.
223  const CallEvent &Call,
224  const StackFrameContext *CalleeCtx);
225 
226  /// Finds the transitive closure of symbols within the given region.
227  ///
228  /// Returns false if the visitor aborted the scan.
229  virtual bool scanReachableSymbols(Store S, const MemRegion *R,
230  ScanReachableSymbols &Visitor) = 0;
231 
232  virtual void print(Store store, raw_ostream &Out,
233  const char* nl, const char *sep) = 0;
234 
236  public:
237  virtual ~BindingsHandler();
238  virtual bool HandleBinding(StoreManager& SMgr, Store store,
239  const MemRegion *region, SVal val) = 0;
240  };
241 
243  public BindingsHandler {
244  SymbolRef Sym;
245  const MemRegion* Binding;
246  bool First;
247 
248  public:
250  : Sym(sym), Binding(nullptr), First(true) {}
251 
252  bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
253  SVal val) override;
254  explicit operator bool() { return First && Binding; }
255  const MemRegion *getRegion() { return Binding; }
256  };
257 
258  /// iterBindings - Iterate over the bindings in the Store.
259  virtual void iterBindings(Store store, BindingsHandler& f) = 0;
260 
261 protected:
262  const ElementRegion *MakeElementRegion(const SubRegion *baseRegion,
263  QualType pointeeTy,
264  uint64_t index = 0);
265 
266  /// CastRetrievedVal - Used by subclasses of StoreManager to implement
267  /// implicit casts that arise from loads from regions that are reinterpreted
268  /// as another region.
269  SVal CastRetrievedVal(SVal val, const TypedValueRegion *region,
270  QualType castTy, bool performTestOnly = true);
271 
272 private:
273  SVal getLValueFieldOrIvar(const Decl *decl, SVal base);
274 };
275 
276 
277 inline StoreRef::StoreRef(Store store, StoreManager & smgr)
278  : store(store), mgr(smgr) {
279  if (store)
280  mgr.incrementReferenceCount(store);
281 }
282 
283 inline StoreRef::StoreRef(const StoreRef &sr)
284  : store(sr.store), mgr(sr.mgr)
285 {
286  if (store)
287  mgr.incrementReferenceCount(store);
288 }
289 
291  if (store)
292  mgr.decrementReferenceCount(store);
293 }
294 
295 inline StoreRef &StoreRef::operator=(StoreRef const &newStore) {
296  assert(&newStore.mgr == &mgr);
297  if (store != newStore.store) {
298  mgr.incrementReferenceCount(newStore.store);
299  mgr.decrementReferenceCount(store);
300  store = newStore.getStore();
301  }
302  return *this;
303 }
304 
305 // FIXME: Do we need to pass ProgramStateManager anymore?
306 std::unique_ptr<StoreManager>
308 std::unique_ptr<StoreManager>
310 
311 } // end GR namespace
312 
313 } // end clang namespace
314 
315 #endif
SVal attemptDownCast(SVal Base, QualType DerivedPtrType, bool &Failed)
Attempts to do a down cast.
Definition: Store.cpp:296
virtual DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state, const MemRegion *region, QualType EleTy)
Definition: Store.h:129
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:511
SVal evalDerivedToBase(SVal Derived, const CastExpr *Cast)
Evaluates a chain of derived-to-base casts through the path specified in Cast.
Definition: Store.cpp:237
A (possibly-)qualified type.
Definition: Type.h:653
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:79
Store getStore() const
Definition: StoreRef.h:46
Information about invalidation for a particular region/symbol.
Definition: MemRegion.h:1383
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
const void * Store
Store - This opaque type encapsulates an immutable mapping from locations to values.
Definition: StoreRef.h:26
virtual StoreRef invalidateRegions(Store store, ArrayRef< SVal > Values, const Expr *E, unsigned Count, const LocationContext *LCtx, const CallEvent *Call, InvalidatedSymbols &IS, RegionAndSymbolInvalidationTraits &ITraits, InvalidatedRegions *InvalidatedTopLevel, InvalidatedRegions *Invalidated)=0
invalidateRegions - Clears out the specified regions from the store, marking their values as unknown...
Represents a path from a specific derived class (which is not represented as part of the path) to a p...
A utility class that visits the reachable symbols using a custom SymbolVisitor.
Definition: ProgramState.h:844
virtual StoreRef getInitialStore(const LocationContext *InitLoc)=0
getInitialStore - Returns the initial "empty" store representing the value bindings upon entry to an ...
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:807
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:2637
const ElementRegion * GetElementZeroRegion(const SubRegion *R, QualType T)
Definition: Store.cpp:56
virtual SVal getBinding(Store store, Loc loc, QualType T=QualType())=0
Return the value bound to specified location in a given state.
std::unique_ptr< StoreManager > CreateFieldsOnlyRegionStoreManager(ProgramStateManager &StMgr)
const MemRegion * castRegion(const MemRegion *region, QualType CastToTy)
castRegion - Used by ExprEngine::VisitCast to handle casts from a MemRegion* to a specific location t...
Definition: Store.cpp:63
ASTContext & Ctx
Definition: Store.h:46
Symbolic value.
Definition: SymExpr.h:29
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
FieldDecl - An instance of this class is created by Sema::ActOnField to represent a member of a struc...
Definition: Decl.h:2461
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
Optional< SVal > getDefaultBinding(nonloc::LazyCompoundVal lcv)
Return the default value bound to a LazyCompoundVal.
Definition: Store.h:82
virtual ~StoreManager()
Definition: Store.h:51
virtual void decrementReferenceCount(Store store)
If the StoreManager supports it, decrement the reference count of the specified Store object...
Definition: Store.h:183
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:2710
virtual SVal getLValueField(const FieldDecl *D, SVal Base)
Definition: Store.h:121
StoreRef enterStackFrame(Store store, const CallEvent &Call, const StackFrameContext *CalleeCtx)
enterStackFrame - Let the StoreManager to do something when execution engine is about to execute into...
Definition: Store.cpp:28
virtual SVal getLValueIvar(const ObjCIvarDecl *decl, SVal base)
Definition: Store.cpp:430
std::unique_ptr< StoreManager > CreateRegionStoreManager(ProgramStateManager &StMgr)
Loc makeLoc(SymbolRef sym)
Definition: SValBuilder.h:329
Expr - This represents one expression.
Definition: Expr.h:106
const FunctionProtoType * T
SValBuilder & svalBuilder
Definition: Store.h:41
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
virtual Loc getLValueVar(const VarDecl *VD, const LocationContext *LC)
Definition: Store.h:110
#define bool
Definition: stdbool.h:31
virtual StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx, SymbolReaper &SymReaper)=0
StoreRef & operator=(StoreRef const &)
Definition: Store.h:295
ProgramStateManager & StateMgr
Definition: Store.h:42
virtual StoreRef Bind(Store store, Loc loc, SVal val)=0
Return a store with the specified value bound to the given location.
virtual Optional< SVal > getDefaultBinding(Store store, const MemRegion *R)=0
Return the default value bound to a region in a given store.
virtual StoreRef killBinding(Store ST, Loc L)=0
Create a new store with the specified binding removed.
SVal CastRetrievedVal(SVal val, const TypedValueRegion *region, QualType castTy, bool performTestOnly=true)
CastRetrievedVal - Used by subclasses of StoreManager to implement implicit casts that arise from loa...
Definition: Store.cpp:368
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:63
A class responsible for cleaning up unused symbols.
const CompoundLiteralRegion * getCompoundLiteralRegion(const CompoundLiteralExpr *CL, const LocationContext *LC)
getCompoundLiteralRegion - Retrieve the region associated with a given CompoundLiteral.
Definition: MemRegion.cpp:909
llvm::DenseSet< SymbolRef > InvalidatedSymbols
Definition: Store.h:35
Dataflow Directional Tag Classes.
virtual void iterBindings(Store store, BindingsHandler &f)=0
iterBindings - Iterate over the bindings in the Store.
const VarRegion * getVarRegion(const VarDecl *D, const LocationContext *LC)
getVarRegion - Retrieve or create the memory region associated with a specified VarDecl and LocationC...
Definition: MemRegion.cpp:783
const void * getStore() const
Definition: SVals.cpp:155
virtual bool includedInBindings(Store store, const MemRegion *region) const =0
virtual void print(Store store, raw_ostream &Out, const char *nl, const char *sep)=0
Represents an abstract call to a function or method along a particular path.
Definition: CallEvent.h:140
virtual bool scanReachableSymbols(Store S, const MemRegion *R, ScanReachableSymbols &Visitor)=0
Finds the transitive closure of symbols within the given region.
MemRegionManager & getRegionManager()
getRegionManager - Returns the internal RegionManager object that is used to query and manipulate Mem...
Definition: Store.h:108
StoreRef(Store, StoreManager &)
Definition: Store.h:277
SubRegion - A region that subsets another larger region.
Definition: MemRegion.h:419
virtual SVal ArrayToPointer(Loc Array, QualType ElementTy)=0
ArrayToPointer - Used by ExprEngine::VistCast to handle implicit conversions between arrays and point...
const ElementRegion * MakeElementRegion(const SubRegion *baseRegion, QualType pointeeTy, uint64_t index=0)
Definition: Store.cpp:45
MemRegionManager & MRMgr
MRMgr - Manages region objects associated with this StoreManager.
Definition: Store.h:45
virtual SVal getLValueElement(QualType elementType, NonLoc offset, SVal Base)
Definition: Store.cpp:434
const TypedValueRegion * getRegion() const
Definition: SVals.cpp:159
virtual bool HandleBinding(StoreManager &SMgr, Store store, const MemRegion *region, SVal val)=0
ObjCIvarDecl - Represents an ObjC instance variable.
Definition: DeclObjC.h:1964
virtual void incrementReferenceCount(Store store)
If the StoreManager supports it, increment the reference count of the specified Store object...
Definition: Store.h:178
virtual StoreRef BindDefault(Store store, const MemRegion *R, SVal V)
Definition: Store.cpp:52
ElementRegin is used to represent both array elements and casts.
Definition: MemRegion.h:1066
#define true
Definition: stdbool.h:32
StoreManager(ProgramStateManager &stateMgr)
Definition: Store.cpp:24
Loc getLValueCompoundLiteral(const CompoundLiteralExpr *CL, const LocationContext *LC)
Definition: Store.h:114
SmallVector< const MemRegion *, 8 > InvalidatedRegions
Definition: Store.h:185