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