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