clang  8.0.0svn
ProgramState.h
Go to the documentation of this file.
1 //== ProgramState.h - Path-sensitive "State" for tracking 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 defines the state of the program along the analysisa path.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_PROGRAMSTATE_H
15 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_PROGRAMSTATE_H
16 
17 #include "clang/Basic/LLVM.h"
25 #include "llvm/ADT/FoldingSet.h"
26 #include "llvm/ADT/ImmutableMap.h"
27 #include "llvm/Support/Allocator.h"
28 #include <utility>
29 
30 namespace llvm {
31 class APSInt;
32 }
33 
34 namespace clang {
35 class ASTContext;
36 
37 namespace ento {
38 
39 class AnalysisManager;
40 class CallEvent;
41 class CallEventManager;
42 
43 typedef std::unique_ptr<ConstraintManager>(*ConstraintManagerCreator)(
45 typedef std::unique_ptr<StoreManager>(*StoreManagerCreator)(
46  ProgramStateManager &);
47 typedef llvm::ImmutableMap<const SubRegion*, TaintTagType> TaintedSubRegions;
48 
49 //===----------------------------------------------------------------------===//
50 // ProgramStateTrait - Traits used by the Generic Data Map of a ProgramState.
51 //===----------------------------------------------------------------------===//
52 
53 template <typename T> struct ProgramStatePartialTrait;
54 
55 template <typename T> struct ProgramStateTrait {
56  typedef typename T::data_type data_type;
57  static inline void *MakeVoidPtr(data_type D) { return (void*) D; }
58  static inline data_type MakeData(void *const* P) {
59  return P ? (data_type) *P : (data_type) 0;
60  }
61 };
62 
63 /// \class ProgramState
64 /// ProgramState - This class encapsulates:
65 ///
66 /// 1. A mapping from expressions to values (Environment)
67 /// 2. A mapping from locations to values (Store)
68 /// 3. Constraints on symbolic values (GenericDataMap)
69 ///
70 /// Together these represent the "abstract state" of a program.
71 ///
72 /// ProgramState is intended to be used as a functional object; that is,
73 /// once it is created and made "persistent" in a FoldingSet, its
74 /// values will never change.
75 class ProgramState : public llvm::FoldingSetNode {
76 public:
77  typedef llvm::ImmutableSet<llvm::APSInt*> IntSetTy;
78  typedef llvm::ImmutableMap<void*, void*> GenericDataMap;
79 
80 private:
81  void operator=(const ProgramState& R) = delete;
82 
83  friend class ProgramStateManager;
84  friend class ExplodedGraph;
85  friend class ExplodedNode;
86 
87  ProgramStateManager *stateMgr;
88  Environment Env; // Maps a Stmt to its current SVal.
89  Store store; // Maps a location to its current value.
90  GenericDataMap GDM; // Custom data stored by a client of this class.
91  unsigned refCount;
92 
93  /// makeWithStore - Return a ProgramState with the same values as the current
94  /// state with the exception of using the specified Store.
95  ProgramStateRef makeWithStore(const StoreRef &store) const;
96 
97  void setStore(const StoreRef &storeRef);
98 
99 public:
100  /// This ctor is used when creating the first ProgramState object.
101  ProgramState(ProgramStateManager *mgr, const Environment& env,
102  StoreRef st, GenericDataMap gdm);
103 
104  /// Copy ctor - We must explicitly define this or else the "Next" ptr
105  /// in FoldingSetNode will also get copied.
106  ProgramState(const ProgramState &RHS);
107 
108  ~ProgramState();
109 
110  int64_t getID() const;
111 
112  /// Return the ProgramStateManager associated with this state.
113  ProgramStateManager &getStateManager() const {
114  return *stateMgr;
115  }
116 
117  AnalysisManager &getAnalysisManager() const;
118 
119  /// Return the ConstraintManager.
120  ConstraintManager &getConstraintManager() const;
121 
122  /// getEnvironment - Return the environment associated with this state.
123  /// The environment is the mapping from expressions to values.
124  const Environment& getEnvironment() const { return Env; }
125 
126  /// Return the store associated with this state. The store
127  /// is a mapping from locations to values.
128  Store getStore() const { return store; }
129 
130 
131  /// getGDM - Return the generic data map associated with this state.
132  GenericDataMap getGDM() const { return GDM; }
133 
134  void setGDM(GenericDataMap gdm) { GDM = gdm; }
135 
136  /// Profile - Profile the contents of a ProgramState object for use in a
137  /// FoldingSet. Two ProgramState objects are considered equal if they
138  /// have the same Environment, Store, and GenericDataMap.
139  static void Profile(llvm::FoldingSetNodeID& ID, const ProgramState *V) {
140  V->Env.Profile(ID);
141  ID.AddPointer(V->store);
142  V->GDM.Profile(ID);
143  }
144 
145  /// Profile - Used to profile the contents of this object for inclusion
146  /// in a FoldingSet.
147  void Profile(llvm::FoldingSetNodeID& ID) const {
148  Profile(ID, this);
149  }
150 
151  BasicValueFactory &getBasicVals() const;
152  SymbolManager &getSymbolManager() const;
153 
154  //==---------------------------------------------------------------------==//
155  // Constraints on values.
156  //==---------------------------------------------------------------------==//
157  //
158  // Each ProgramState records constraints on symbolic values. These constraints
159  // are managed using the ConstraintManager associated with a ProgramStateManager.
160  // As constraints gradually accrue on symbolic values, added constraints
161  // may conflict and indicate that a state is infeasible (as no real values
162  // could satisfy all the constraints). This is the principal mechanism
163  // for modeling path-sensitivity in ExprEngine/ProgramState.
164  //
165  // Various "assume" methods form the interface for adding constraints to
166  // symbolic values. A call to 'assume' indicates an assumption being placed
167  // on one or symbolic values. 'assume' methods take the following inputs:
168  //
169  // (1) A ProgramState object representing the current state.
170  //
171  // (2) The assumed constraint (which is specific to a given "assume" method).
172  //
173  // (3) A binary value "Assumption" that indicates whether the constraint is
174  // assumed to be true or false.
175  //
176  // The output of "assume*" is a new ProgramState object with the added constraints.
177  // If no new state is feasible, NULL is returned.
178  //
179 
180  /// Assumes that the value of \p cond is zero (if \p assumption is "false")
181  /// or non-zero (if \p assumption is "true").
182  ///
183  /// This returns a new state with the added constraint on \p cond.
184  /// If no new state is feasible, NULL is returned.
185  LLVM_NODISCARD ProgramStateRef assume(DefinedOrUnknownSVal cond,
186  bool assumption) const;
187 
188  /// Assumes both "true" and "false" for \p cond, and returns both
189  /// corresponding states (respectively).
190  ///
191  /// This is more efficient than calling assume() twice. Note that one (but not
192  /// both) of the returned states may be NULL.
193  LLVM_NODISCARD std::pair<ProgramStateRef, ProgramStateRef>
194  assume(DefinedOrUnknownSVal cond) const;
195 
196  LLVM_NODISCARD ProgramStateRef
197  assumeInBound(DefinedOrUnknownSVal idx, DefinedOrUnknownSVal upperBound,
198  bool assumption, QualType IndexType = QualType()) const;
199 
200  /// Assumes that the value of \p Val is bounded with [\p From; \p To]
201  /// (if \p assumption is "true") or it is fully out of this range
202  /// (if \p assumption is "false").
203  ///
204  /// This returns a new state with the added constraint on \p cond.
205  /// If no new state is feasible, NULL is returned.
206  LLVM_NODISCARD ProgramStateRef assumeInclusiveRange(DefinedOrUnknownSVal Val,
207  const llvm::APSInt &From,
208  const llvm::APSInt &To,
209  bool assumption) const;
210 
211  /// Assumes given range both "true" and "false" for \p Val, and returns both
212  /// corresponding states (respectively).
213  ///
214  /// This is more efficient than calling assume() twice. Note that one (but not
215  /// both) of the returned states may be NULL.
216  LLVM_NODISCARD std::pair<ProgramStateRef, ProgramStateRef>
217  assumeInclusiveRange(DefinedOrUnknownSVal Val, const llvm::APSInt &From,
218  const llvm::APSInt &To) const;
219 
220  /// Check if the given SVal is not constrained to zero and is not
221  /// a zero constant.
222  ConditionTruthVal isNonNull(SVal V) const;
223 
224  /// Check if the given SVal is constrained to zero or is a zero
225  /// constant.
226  ConditionTruthVal isNull(SVal V) const;
227 
228  /// \return Whether values \p Lhs and \p Rhs are equal.
229  ConditionTruthVal areEqual(SVal Lhs, SVal Rhs) const;
230 
231  /// Utility method for getting regions.
232  const VarRegion* getRegion(const VarDecl *D, const LocationContext *LC) const;
233 
234  //==---------------------------------------------------------------------==//
235  // Binding and retrieving values to/from the environment and symbolic store.
236  //==---------------------------------------------------------------------==//
237 
238  /// Create a new state by binding the value 'V' to the statement 'S' in the
239  /// state's environment.
240  LLVM_NODISCARD ProgramStateRef BindExpr(const Stmt *S,
241  const LocationContext *LCtx, SVal V,
242  bool Invalidate = true) const;
243 
244  LLVM_NODISCARD ProgramStateRef bindLoc(Loc location, SVal V,
245  const LocationContext *LCtx,
246  bool notifyChanges = true) const;
247 
248  LLVM_NODISCARD ProgramStateRef bindLoc(SVal location, SVal V,
249  const LocationContext *LCtx) const;
250 
251  /// Initializes the region of memory represented by \p loc with an initial
252  /// value. Once initialized, all values loaded from any sub-regions of that
253  /// region will be equal to \p V, unless overwritten later by the program.
254  /// This method should not be used on regions that are already initialized.
255  /// If you need to indicate that memory contents have suddenly become unknown
256  /// within a certain region of memory, consider invalidateRegions().
257  LLVM_NODISCARD ProgramStateRef
258  bindDefaultInitial(SVal loc, SVal V, const LocationContext *LCtx) const;
259 
260  /// Performs C++ zero-initialization procedure on the region of memory
261  /// represented by \p loc.
262  LLVM_NODISCARD ProgramStateRef
263  bindDefaultZero(SVal loc, const LocationContext *LCtx) const;
264 
265  LLVM_NODISCARD ProgramStateRef killBinding(Loc LV) const;
266 
267  /// Returns the state with bindings for the given regions
268  /// cleared from the store.
269  ///
270  /// Optionally invalidates global regions as well.
271  ///
272  /// \param Regions the set of regions to be invalidated.
273  /// \param E the expression that caused the invalidation.
274  /// \param BlockCount The number of times the current basic block has been
275  // visited.
276  /// \param CausesPointerEscape the flag is set to true when
277  /// the invalidation entails escape of a symbol (representing a
278  /// pointer). For example, due to it being passed as an argument in a
279  /// call.
280  /// \param IS the set of invalidated symbols.
281  /// \param Call if non-null, the invalidated regions represent parameters to
282  /// the call and should be considered directly invalidated.
283  /// \param ITraits information about special handling for a particular
284  /// region/symbol.
285  LLVM_NODISCARD ProgramStateRef
286  invalidateRegions(ArrayRef<const MemRegion *> Regions, const Expr *E,
287  unsigned BlockCount, const LocationContext *LCtx,
288  bool CausesPointerEscape, InvalidatedSymbols *IS = nullptr,
289  const CallEvent *Call = nullptr,
290  RegionAndSymbolInvalidationTraits *ITraits = nullptr) const;
291 
292  LLVM_NODISCARD ProgramStateRef
293  invalidateRegions(ArrayRef<SVal> Regions, const Expr *E,
294  unsigned BlockCount, const LocationContext *LCtx,
295  bool CausesPointerEscape, InvalidatedSymbols *IS = nullptr,
296  const CallEvent *Call = nullptr,
297  RegionAndSymbolInvalidationTraits *ITraits = nullptr) const;
298 
299  /// enterStackFrame - Returns the state for entry to the given stack frame,
300  /// preserving the current state.
301  LLVM_NODISCARD ProgramStateRef enterStackFrame(
302  const CallEvent &Call, const StackFrameContext *CalleeCtx) const;
303 
304  /// Get the lvalue for a base class object reference.
305  Loc getLValue(const CXXBaseSpecifier &BaseSpec, const SubRegion *Super) const;
306 
307  /// Get the lvalue for a base class object reference.
308  Loc getLValue(const CXXRecordDecl *BaseClass, const SubRegion *Super,
309  bool IsVirtual) const;
310 
311  /// Get the lvalue for a variable reference.
312  Loc getLValue(const VarDecl *D, const LocationContext *LC) const;
313 
314  Loc getLValue(const CompoundLiteralExpr *literal,
315  const LocationContext *LC) const;
316 
317  /// Get the lvalue for an ivar reference.
318  SVal getLValue(const ObjCIvarDecl *decl, SVal base) const;
319 
320  /// Get the lvalue for a field reference.
321  SVal getLValue(const FieldDecl *decl, SVal Base) const;
322 
323  /// Get the lvalue for an indirect field reference.
324  SVal getLValue(const IndirectFieldDecl *decl, SVal Base) const;
325 
326  /// Get the lvalue for an array index.
327  SVal getLValue(QualType ElementType, SVal Idx, SVal Base) const;
328 
329  /// Returns the SVal bound to the statement 'S' in the state's environment.
330  SVal getSVal(const Stmt *S, const LocationContext *LCtx) const;
331 
332  SVal getSValAsScalarOrLoc(const Stmt *Ex, const LocationContext *LCtx) const;
333 
334  /// Return the value bound to the specified location.
335  /// Returns UnknownVal() if none found.
336  SVal getSVal(Loc LV, QualType T = QualType()) const;
337 
338  /// Returns the "raw" SVal bound to LV before any value simplfication.
339  SVal getRawSVal(Loc LV, QualType T= QualType()) const;
340 
341  /// Return the value bound to the specified location.
342  /// Returns UnknownVal() if none found.
343  SVal getSVal(const MemRegion* R, QualType T = QualType()) const;
344 
345  /// Return the value bound to the specified location, assuming
346  /// that the value is a scalar integer or an enumeration or a pointer.
347  /// Returns UnknownVal() if none found or the region is not known to hold
348  /// a value of such type.
349  SVal getSValAsScalarOrLoc(const MemRegion *R) const;
350 
351  /// Visits the symbols reachable from the given SVal using the provided
352  /// SymbolVisitor.
353  ///
354  /// This is a convenience API. Consider using ScanReachableSymbols class
355  /// directly when making multiple scans on the same state with the same
356  /// visitor to avoid repeated initialization cost.
357  /// \sa ScanReachableSymbols
358  bool scanReachableSymbols(SVal val, SymbolVisitor& visitor) const;
359 
360  /// Visits the symbols reachable from the SVals in the given range
361  /// using the provided SymbolVisitor.
362  bool scanReachableSymbols(const SVal *I, const SVal *E,
363  SymbolVisitor &visitor) const;
364 
365  /// Visits the symbols reachable from the regions in the given
366  /// MemRegions range using the provided SymbolVisitor.
367  bool scanReachableSymbols(const MemRegion * const *I,
368  const MemRegion * const *E,
369  SymbolVisitor &visitor) const;
370 
371  template <typename CB> CB scanReachableSymbols(SVal val) const;
372  template <typename CB> CB scanReachableSymbols(const SVal *beg,
373  const SVal *end) const;
374 
375  template <typename CB> CB
376  scanReachableSymbols(const MemRegion * const *beg,
377  const MemRegion * const *end) const;
378 
379  /// Create a new state in which the statement is marked as tainted.
380  LLVM_NODISCARD ProgramStateRef
381  addTaint(const Stmt *S, const LocationContext *LCtx,
383 
384  /// Create a new state in which the value is marked as tainted.
385  LLVM_NODISCARD ProgramStateRef
386  addTaint(SVal V, TaintTagType Kind = TaintTagGeneric) const;
387 
388  /// Create a new state in which the symbol is marked as tainted.
389  LLVM_NODISCARD ProgramStateRef addTaint(SymbolRef S,
391 
392  /// Create a new state in which the region symbol is marked as tainted.
393  LLVM_NODISCARD ProgramStateRef
394  addTaint(const MemRegion *R, TaintTagType Kind = TaintTagGeneric) const;
395 
396  /// Create a new state in a which a sub-region of a given symbol is tainted.
397  /// This might be necessary when referring to regions that can not have an
398  /// individual symbol, e.g. if they are represented by the default binding of
399  /// a LazyCompoundVal.
400  LLVM_NODISCARD ProgramStateRef
401  addPartialTaint(SymbolRef ParentSym, const SubRegion *SubRegion,
403 
404  /// Check if the statement is tainted in the current state.
405  bool isTainted(const Stmt *S, const LocationContext *LCtx,
407  bool isTainted(SVal V, TaintTagType Kind = TaintTagGeneric) const;
408  bool isTainted(SymbolRef Sym, TaintTagType Kind = TaintTagGeneric) const;
409  bool isTainted(const MemRegion *Reg, TaintTagType Kind=TaintTagGeneric) const;
410 
411  //==---------------------------------------------------------------------==//
412  // Accessing the Generic Data Map (GDM).
413  //==---------------------------------------------------------------------==//
414 
415  void *const* FindGDM(void *K) const;
416 
417  template <typename T>
418  LLVM_NODISCARD ProgramStateRef
419  add(typename ProgramStateTrait<T>::key_type K) const;
420 
421  template <typename T>
423  get() const {
425  }
426 
427  template<typename T>
429  get(typename ProgramStateTrait<T>::key_type key) const {
430  void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex());
432  }
433 
434  template <typename T>
435  typename ProgramStateTrait<T>::context_type get_context() const;
436 
437  template <typename T>
438  LLVM_NODISCARD ProgramStateRef
439  remove(typename ProgramStateTrait<T>::key_type K) const;
440 
441  template <typename T>
442  LLVM_NODISCARD ProgramStateRef
443  remove(typename ProgramStateTrait<T>::key_type K,
444  typename ProgramStateTrait<T>::context_type C) const;
445 
446  template <typename T> LLVM_NODISCARD ProgramStateRef remove() const;
447 
448  template <typename T>
449  LLVM_NODISCARD ProgramStateRef
450  set(typename ProgramStateTrait<T>::data_type D) const;
451 
452  template <typename T>
453  LLVM_NODISCARD ProgramStateRef
454  set(typename ProgramStateTrait<T>::key_type K,
455  typename ProgramStateTrait<T>::value_type E) const;
456 
457  template <typename T>
458  LLVM_NODISCARD ProgramStateRef
459  set(typename ProgramStateTrait<T>::key_type K,
461  typename ProgramStateTrait<T>::context_type C) const;
462 
463  template<typename T>
464  bool contains(typename ProgramStateTrait<T>::key_type key) const {
465  void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex());
467  }
468 
469  // Pretty-printing.
470  void print(raw_ostream &Out, const char *nl = "\n", const char *sep = "",
471  const LocationContext *CurrentLC = nullptr) const;
472  void printDOT(raw_ostream &Out,
473  const LocationContext *CurrentLC = nullptr) const;
474  void printTaint(raw_ostream &Out, const char *nl = "\n",
475  const char *sep = "") const;
476 
477  void dump() const;
478  void dumpTaint() const;
479 
480 private:
481  friend void ProgramStateRetain(const ProgramState *state);
482  friend void ProgramStateRelease(const ProgramState *state);
483 
484  /// \sa invalidateValues()
485  /// \sa invalidateRegions()
487  invalidateRegionsImpl(ArrayRef<SVal> Values,
488  const Expr *E, unsigned BlockCount,
489  const LocationContext *LCtx,
490  bool ResultsInSymbolEscape,
491  InvalidatedSymbols *IS,
493  const CallEvent *Call) const;
494 };
495 
496 //===----------------------------------------------------------------------===//
497 // ProgramStateManager - Factory object for ProgramStates.
498 //===----------------------------------------------------------------------===//
499 
500 class ProgramStateManager {
501  friend class ProgramState;
502  friend void ProgramStateRelease(const ProgramState *state);
503 private:
504  /// Eng - The SubEngine that owns this state manager.
505  SubEngine *Eng; /* Can be null. */
506 
507  EnvironmentManager EnvMgr;
508  std::unique_ptr<StoreManager> StoreMgr;
509  std::unique_ptr<ConstraintManager> ConstraintMgr;
510 
511  ProgramState::GenericDataMap::Factory GDMFactory;
512  TaintedSubRegions::Factory TSRFactory;
513 
514  typedef llvm::DenseMap<void*,std::pair<void*,void (*)(void*)> > GDMContextsTy;
515  GDMContextsTy GDMContexts;
516 
517  /// StateSet - FoldingSet containing all the states created for analyzing
518  /// a particular function. This is used to unique states.
519  llvm::FoldingSet<ProgramState> StateSet;
520 
521  /// Object that manages the data for all created SVals.
522  std::unique_ptr<SValBuilder> svalBuilder;
523 
524  /// Manages memory for created CallEvents.
525  std::unique_ptr<CallEventManager> CallEventMgr;
526 
527  /// A BumpPtrAllocator to allocate states.
528  llvm::BumpPtrAllocator &Alloc;
529 
530  /// A vector of ProgramStates that we can reuse.
531  std::vector<ProgramState *> freeStates;
532 
533 public:
534  ProgramStateManager(ASTContext &Ctx,
535  StoreManagerCreator CreateStoreManager,
536  ConstraintManagerCreator CreateConstraintManager,
537  llvm::BumpPtrAllocator& alloc,
538  SubEngine *subeng);
539 
540  ~ProgramStateManager();
541 
542  ProgramStateRef getInitialState(const LocationContext *InitLoc);
543 
544  ASTContext &getContext() { return svalBuilder->getContext(); }
545  const ASTContext &getContext() const { return svalBuilder->getContext(); }
546 
548  return svalBuilder->getBasicValueFactory();
549  }
550 
552  return *svalBuilder;
553  }
554 
556  return svalBuilder->getSymbolManager();
557  }
559  return svalBuilder->getSymbolManager();
560  }
561 
562  llvm::BumpPtrAllocator& getAllocator() { return Alloc; }
563 
565  return svalBuilder->getRegionManager();
566  }
568  return svalBuilder->getRegionManager();
569  }
570 
571  CallEventManager &getCallEventManager() { return *CallEventMgr; }
572 
573  StoreManager& getStoreManager() { return *StoreMgr; }
574  ConstraintManager& getConstraintManager() { return *ConstraintMgr; }
575  SubEngine* getOwningEngine() { return Eng; }
576 
577  ProgramStateRef removeDeadBindings(ProgramStateRef St,
578  const StackFrameContext *LCtx,
579  SymbolReaper& SymReaper);
580 
581 public:
582 
583  SVal ArrayToPointer(Loc Array, QualType ElementTy) {
584  return StoreMgr->ArrayToPointer(Array, ElementTy);
585  }
586 
587  // Methods that manipulate the GDM.
588  ProgramStateRef addGDM(ProgramStateRef St, void *Key, void *Data);
589  ProgramStateRef removeGDM(ProgramStateRef state, void *Key);
590 
591  // Methods that query & manipulate the Store.
592 
594  StoreMgr->iterBindings(state->getStore(), F);
595  }
596 
597  ProgramStateRef getPersistentState(ProgramState &Impl);
598  ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState,
599  ProgramStateRef GDMState);
600 
602  return S1->Env == S2->Env;
603  }
604 
606  return S1->store == S2->store;
607  }
608 
609  //==---------------------------------------------------------------------==//
610  // Generic Data Map methods.
611  //==---------------------------------------------------------------------==//
612  //
613  // ProgramStateManager and ProgramState support a "generic data map" that allows
614  // different clients of ProgramState objects to embed arbitrary data within a
615  // ProgramState object. The generic data map is essentially an immutable map
616  // from a "tag" (that acts as the "key" for a client) and opaque values.
617  // Tags/keys and values are simply void* values. The typical way that clients
618  // generate unique tags are by taking the address of a static variable.
619  // Clients are responsible for ensuring that data values referred to by a
620  // the data pointer are immutable (and thus are essentially purely functional
621  // data).
622  //
623  // The templated methods below use the ProgramStateTrait<T> class
624  // to resolve keys into the GDM and to return data values to clients.
625  //
626 
627  // Trait based GDM dispatch.
628  template <typename T>
630  return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
632  }
633 
634  template<typename T>
639 
640  return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
642  }
643 
644  template <typename T>
648  return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
650  }
651 
652  template <typename T>
656 
657  return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
659  }
660 
661  template <typename T>
663  return removeGDM(st, ProgramStateTrait<T>::GDMIndex());
664  }
665 
666  void *FindGDMContext(void *index,
667  void *(*CreateContext)(llvm::BumpPtrAllocator&),
668  void (*DeleteContext)(void*));
669 
670  template <typename T>
672  void *p = FindGDMContext(ProgramStateTrait<T>::GDMIndex(),
675 
677  }
678 
680  ConstraintMgr->EndPath(St);
681  }
682 };
683 
684 
685 //===----------------------------------------------------------------------===//
686 // Out-of-line method definitions for ProgramState.
687 //===----------------------------------------------------------------------===//
688 
689 inline ConstraintManager &ProgramState::getConstraintManager() const {
690  return stateMgr->getConstraintManager();
691 }
692 
693 inline const VarRegion* ProgramState::getRegion(const VarDecl *D,
694  const LocationContext *LC) const
695 {
696  return getStateManager().getRegionManager().getVarRegion(D, LC);
697 }
698 
699 inline ProgramStateRef ProgramState::assume(DefinedOrUnknownSVal Cond,
700  bool Assumption) const {
701  if (Cond.isUnknown())
702  return this;
703 
704  return getStateManager().ConstraintMgr
705  ->assume(this, Cond.castAs<DefinedSVal>(), Assumption);
706 }
707 
708 inline std::pair<ProgramStateRef , ProgramStateRef >
709 ProgramState::assume(DefinedOrUnknownSVal Cond) const {
710  if (Cond.isUnknown())
711  return std::make_pair(this, this);
712 
713  return getStateManager().ConstraintMgr
714  ->assumeDual(this, Cond.castAs<DefinedSVal>());
715 }
716 
717 inline ProgramStateRef ProgramState::assumeInclusiveRange(
718  DefinedOrUnknownSVal Val, const llvm::APSInt &From, const llvm::APSInt &To,
719  bool Assumption) const {
720  if (Val.isUnknown())
721  return this;
722 
723  assert(Val.getAs<NonLoc>() && "Only NonLocs are supported!");
724 
725  return getStateManager().ConstraintMgr->assumeInclusiveRange(
726  this, Val.castAs<NonLoc>(), From, To, Assumption);
727 }
728 
729 inline std::pair<ProgramStateRef, ProgramStateRef>
730 ProgramState::assumeInclusiveRange(DefinedOrUnknownSVal Val,
731  const llvm::APSInt &From,
732  const llvm::APSInt &To) const {
733  if (Val.isUnknown())
734  return std::make_pair(this, this);
735 
736  assert(Val.getAs<NonLoc>() && "Only NonLocs are supported!");
737 
738  return getStateManager().ConstraintMgr->assumeInclusiveRangeDual(
739  this, Val.castAs<NonLoc>(), From, To);
740 }
741 
742 inline ProgramStateRef ProgramState::bindLoc(SVal LV, SVal V, const LocationContext *LCtx) const {
743  if (Optional<Loc> L = LV.getAs<Loc>())
744  return bindLoc(*L, V, LCtx);
745  return this;
746 }
747 
748 inline Loc ProgramState::getLValue(const CXXBaseSpecifier &BaseSpec,
749  const SubRegion *Super) const {
750  const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
751  return loc::MemRegionVal(
752  getStateManager().getRegionManager().getCXXBaseObjectRegion(
753  Base, Super, BaseSpec.isVirtual()));
754 }
755 
756 inline Loc ProgramState::getLValue(const CXXRecordDecl *BaseClass,
757  const SubRegion *Super,
758  bool IsVirtual) const {
759  return loc::MemRegionVal(
760  getStateManager().getRegionManager().getCXXBaseObjectRegion(
761  BaseClass, Super, IsVirtual));
762 }
763 
764 inline Loc ProgramState::getLValue(const VarDecl *VD,
765  const LocationContext *LC) const {
766  return getStateManager().StoreMgr->getLValueVar(VD, LC);
767 }
768 
769 inline Loc ProgramState::getLValue(const CompoundLiteralExpr *literal,
770  const LocationContext *LC) const {
771  return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC);
772 }
773 
774 inline SVal ProgramState::getLValue(const ObjCIvarDecl *D, SVal Base) const {
775  return getStateManager().StoreMgr->getLValueIvar(D, Base);
776 }
777 
778 inline SVal ProgramState::getLValue(const FieldDecl *D, SVal Base) const {
779  return getStateManager().StoreMgr->getLValueField(D, Base);
780 }
781 
782 inline SVal ProgramState::getLValue(const IndirectFieldDecl *D,
783  SVal Base) const {
784  StoreManager &SM = *getStateManager().StoreMgr;
785  for (const auto *I : D->chain()) {
786  Base = SM.getLValueField(cast<FieldDecl>(I), Base);
787  }
788 
789  return Base;
790 }
791 
792 inline SVal ProgramState::getLValue(QualType ElementType, SVal Idx, SVal Base) const{
793  if (Optional<NonLoc> N = Idx.getAs<NonLoc>())
794  return getStateManager().StoreMgr->getLValueElement(ElementType, *N, Base);
795  return UnknownVal();
796 }
797 
798 inline SVal ProgramState::getSVal(const Stmt *Ex,
799  const LocationContext *LCtx) const{
800  return Env.getSVal(EnvironmentEntry(Ex, LCtx),
801  *getStateManager().svalBuilder);
802 }
803 
804 inline SVal
805 ProgramState::getSValAsScalarOrLoc(const Stmt *S,
806  const LocationContext *LCtx) const {
807  if (const Expr *Ex = dyn_cast<Expr>(S)) {
808  QualType T = Ex->getType();
809  if (Ex->isGLValue() || Loc::isLocType(T) ||
811  return getSVal(S, LCtx);
812  }
813 
814  return UnknownVal();
815 }
816 
817 inline SVal ProgramState::getRawSVal(Loc LV, QualType T) const {
818  return getStateManager().StoreMgr->getBinding(getStore(), LV, T);
819 }
820 
821 inline SVal ProgramState::getSVal(const MemRegion* R, QualType T) const {
822  return getStateManager().StoreMgr->getBinding(getStore(),
824  T);
825 }
826 
827 inline BasicValueFactory &ProgramState::getBasicVals() const {
828  return getStateManager().getBasicVals();
829 }
830 
831 inline SymbolManager &ProgramState::getSymbolManager() const {
832  return getStateManager().getSymbolManager();
833 }
834 
835 template<typename T>
836 ProgramStateRef ProgramState::add(typename ProgramStateTrait<T>::key_type K) const {
837  return getStateManager().add<T>(this, K, get_context<T>());
838 }
839 
840 template <typename T>
841 typename ProgramStateTrait<T>::context_type ProgramState::get_context() const {
842  return getStateManager().get_context<T>();
843 }
844 
845 template<typename T>
846 ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K) const {
847  return getStateManager().remove<T>(this, K, get_context<T>());
848 }
849 
850 template<typename T>
851 ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K,
852  typename ProgramStateTrait<T>::context_type C) const {
853  return getStateManager().remove<T>(this, K, C);
854 }
855 
856 template <typename T>
857 ProgramStateRef ProgramState::remove() const {
858  return getStateManager().remove<T>(this);
859 }
860 
861 template<typename T>
862 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::data_type D) const {
863  return getStateManager().set<T>(this, D);
864 }
865 
866 template<typename T>
867 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K,
868  typename ProgramStateTrait<T>::value_type E) const {
869  return getStateManager().set<T>(this, K, E, get_context<T>());
870 }
871 
872 template<typename T>
873 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K,
875  typename ProgramStateTrait<T>::context_type C) const {
876  return getStateManager().set<T>(this, K, E, C);
877 }
878 
879 template <typename CB>
880 CB ProgramState::scanReachableSymbols(SVal val) const {
881  CB cb(this);
882  scanReachableSymbols(val, cb);
883  return cb;
884 }
885 
886 template <typename CB>
887 CB ProgramState::scanReachableSymbols(const SVal *beg, const SVal *end) const {
888  CB cb(this);
889  scanReachableSymbols(beg, end, cb);
890  return cb;
891 }
892 
893 template <typename CB>
894 CB ProgramState::scanReachableSymbols(const MemRegion * const *beg,
895  const MemRegion * const *end) const {
896  CB cb(this);
897  scanReachableSymbols(beg, end, cb);
898  return cb;
899 }
900 
901 /// \class ScanReachableSymbols
902 /// A utility class that visits the reachable symbols using a custom
903 /// SymbolVisitor. Terminates recursive traversal when the visitor function
904 /// returns false.
907 
908  VisitedItems visited;
910  SymbolVisitor &visitor;
911 public:
913  : state(std::move(st)), visitor(v) {}
914 
915  bool scan(nonloc::LazyCompoundVal val);
916  bool scan(nonloc::CompoundVal val);
917  bool scan(SVal val);
918  bool scan(const MemRegion *R);
919  bool scan(const SymExpr *sym);
920 };
921 
922 } // end ento namespace
923 
924 } // end clang namespace
925 
926 #endif
const Environment & getEnvironment() const
getEnvironment - Return the environment associated with this state.
Definition: ProgramState.h:124
A (possibly-)qualified type.
Definition: Type.h:642
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:94
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
Stmt - This represents one statement.
Definition: Stmt.h:66
Information about invalidation for a particular region/symbol.
Definition: MemRegion.h:1438
BasicValueFactory & getBasicVals()
Definition: ProgramState.h:547
bool haveEqualStores(ProgramStateRef S1, ProgramStateRef S2)
Definition: ProgramState.h:605
bool contains(typename ProgramStateTrait< T >::key_type key) const
Definition: ProgramState.h:464
Manages the lifetime of CallEvent objects.
Definition: CallEvent.h:1083
llvm::ImmutableSet< llvm::APSInt * > IntSetTy
Definition: ProgramState.h:77
bool isVirtual() const
Determines whether the base class is a virtual base class (or not).
Definition: DeclCXX.h:245
StringRef P
A utility class that visits the reachable symbols using a custom SymbolVisitor.
Definition: ProgramState.h:905
bool haveEqualEnvironments(ProgramStateRef S1, ProgramStateRef S2)
Definition: ProgramState.h:601
Represents a variable declaration or definition.
Definition: Decl.h:812
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:2717
const void * Store
Store - This opaque type encapsulates an immutable mapping from locations to values.
Definition: StoreRef.h:28
Store getStore() const
Return the store associated with this state.
Definition: ProgramState.h:128
const SymbolManager & getSymbolManager() const
Definition: ProgramState.h:558
Symbolic value.
Definition: SymExpr.h:30
ProgramStateRef add(ProgramStateRef st, typename ProgramStateTrait< T >::key_type K, typename ProgramStateTrait< T >::context_type C)
Definition: ProgramState.h:645
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
Represents a member of a struct/union/class.
Definition: Decl.h:2556
Definition: Format.h:2031
std::unique_ptr< StoreManager >(* StoreManagerCreator)(ProgramStateManager &)
Definition: ProgramState.h:45
MemRegionManager & getRegionManager()
Definition: ProgramState.h:564
void EndPath(ProgramStateRef St)
Definition: ProgramState.h:679
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
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:6504
ProgramStateManager & getStateManager() const
Return the ProgramStateManager associated with this state.
Definition: ProgramState.h:113
GenericDataMap getGDM() const
getGDM - Return the generic data map associated with this state.
Definition: ProgramState.h:132
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
ArrayRef< NamedDecl * > chain() const
Definition: Decl.h:2824
bool isUnknown() const
Definition: SVals.h:137
virtual SVal getLValueField(const FieldDecl *D, SVal Base)
Definition: Store.h:146
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1605
std::unique_ptr< ConstraintManager >(* ConstraintManagerCreator)(ProgramStateManager &, SubEngine *)
Definition: ProgramState.h:43
llvm::ImmutableMap< void *, void * > GenericDataMap
Definition: ProgramState.h:78
void Profile(llvm::FoldingSetNodeID &ID) const
Profile - Used to profile the contents of this object for inclusion in a FoldingSet.
Definition: ProgramState.h:147
ProgramState - This class encapsulates:
Definition: ProgramState.h:75
This represents one expression.
Definition: Expr.h:105
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
do v
Definition: arm_acle.h:78
const SourceManager & SM
Definition: Format.cpp:1472
Optional< T > getAs() const
Convert to the specified SVal type, returning None if this SVal is not of the desired type...
Definition: SVals.h:112
llvm::BumpPtrAllocator & getAllocator()
Definition: ProgramState.h:562
Kind
static void Profile(llvm::FoldingSetNodeID &ID, const Environment *env)
Profile - Profile the contents of an Environment object for use in a FoldingSet.
Definition: Environment.h:81
CallEventManager & getCallEventManager()
Definition: ProgramState.h:571
An entry in the environment consists of a Stmt and an LocationContext.
Definition: Environment.h:36
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:76
ProgramStateTrait< T >::context_type get_context()
Definition: ProgramState.h:671
static const TaintTagType TaintTagGeneric
Definition: TaintTag.h:25
A class responsible for cleaning up unused symbols.
ScanReachableSymbols(ProgramStateRef st, SymbolVisitor &v)
Definition: ProgramState.h:912
llvm::ImmutableMap< const SubRegion *, TaintTagType > TaintedSubRegions
Definition: ProgramState.h:47
static void * MakeVoidPtr(data_type D)
Definition: ProgramState.h:57
SymbolManager & getSymbolManager()
Definition: ProgramState.h:555
An immutable map from EnvironemntEntries to SVals.
Definition: Environment.h:57
Dataflow Directional Tag Classes.
SVal ArrayToPointer(Loc Array, QualType ElementTy)
Definition: ProgramState.h:583
void ProgramStateRelease(const ProgramState *state)
Decrement the number of times this state is referenced.
static data_type MakeData(void *const *P)
Definition: ProgramState.h:58
Represents a field injected from an anonymous union/struct into the parent scope. ...
Definition: Decl.h:2802
Represents an abstract call to a function or method along a particular path.
Definition: CallEvent.h:182
ConstraintManager & getConstraintManager()
Definition: ProgramState.h:574
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition: SVals.h:104
SubRegion - A region that subsets another larger region.
Definition: MemRegion.h:431
void ProgramStateRetain(const ProgramState *state)
Increments the number of times this state is referenced.
Represents a base class of a C++ class.
Definition: DeclCXX.h:192
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
const MemRegionManager & getRegionManager() const
Definition: ProgramState.h:567
ObjCIvarDecl - Represents an ObjC instance variable.
Definition: DeclObjC.h:1937
unsigned TaintTagType
The type of taint, which helps to differentiate between different types of taint. ...
Definition: TaintTag.h:23
const ASTContext & getContext() const
Definition: ProgramState.h:545
static void Profile(llvm::FoldingSetNodeID &ID, const ProgramState *V)
Profile - Profile the contents of a ProgramState object for use in a FoldingSet.
Definition: ProgramState.h:139
void iterBindings(ProgramStateRef state, StoreManager::BindingsHandler &F)
Definition: ProgramState.h:593
void setGDM(GenericDataMap gdm)
Definition: ProgramState.h:134
QualType getType() const
Retrieves the type of the base class.
Definition: DeclCXX.h:291