clang 23.0.0git
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 analysis 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 <optional>
27#include <utility>
28
29namespace llvm {
30class APSInt;
31}
32
33namespace clang {
34class ASTContext;
35
36namespace ento {
37
38class AnalysisManager;
39class CallEvent;
41
42typedef std::unique_ptr<ConstraintManager>(*ConstraintManagerCreator)(
44typedef std::unique_ptr<StoreManager>(*StoreManagerCreator)(
46
47//===----------------------------------------------------------------------===//
48// ProgramStateTrait - Traits used by the Generic Data Map of a ProgramState.
49//===----------------------------------------------------------------------===//
50
51template <typename T> struct ProgramStateTrait {
52 typedef typename T::data_type data_type;
53 static inline void *MakeVoidPtr(data_type D) { return (void*) D; }
54 static inline data_type MakeData(void *const* P) {
55 return P ? (data_type) *P : (data_type) 0;
56 }
57};
58
59/// \class ProgramState
60/// ProgramState - This class encapsulates:
61///
62/// 1. A mapping from expressions to values (Environment)
63/// 2. A mapping from locations to values (Store)
64/// 3. Constraints on symbolic values (GenericDataMap)
65///
66/// Together these represent the "abstract state" of a program.
67///
68/// ProgramState is intended to be used as a functional object; that is,
69/// once it is created and made "persistent" in a FoldingSet, its
70/// values will never change.
71class ProgramState : public llvm::FoldingSetNode {
72public:
73 typedef llvm::ImmutableMap<const void *, void *> GenericDataMap;
74
75private:
76 void operator=(const ProgramState& R) = delete;
77
78 friend class ProgramStateManager;
79 friend class ExplodedGraph;
80 friend class ExplodedNode;
81
82 ProgramStateManager *stateMgr;
83 Environment Env; // Maps a Stmt to its current SVal.
84 Store store; // Maps a location to its current value.
85 GenericDataMap GDM; // Custom data stored by a client of this class.
86
87 // A state is infeasible if there is a contradiction among the constraints.
88 // An infeasible state is represented by a `nullptr`.
89 // In the sense of `assumeDual`, a state can have two children by adding a
90 // new constraint and the negation of that new constraint. A parent state is
91 // over-constrained if both of its children are infeasible. In the
92 // mathematical sense, it means that the parent is infeasible and we should
93 // have realized that at the moment when we have created it. However, we
94 // could not recognize that because of the imperfection of the underlying
95 // constraint solver. We say it is posteriorly over-constrained because we
96 // recognize that a parent is infeasible only *after* a new and more specific
97 // constraint and its negation are evaluated.
98 //
99 // Example:
100 //
101 // x * x = 4 and x is in the range [0, 1]
102 // This is an already infeasible state, but the constraint solver is not
103 // capable of handling sqrt, thus we don't know it yet.
104 //
105 // Then a new constraint `x = 0` is added. At this moment the constraint
106 // solver re-evaluates the existing constraints and realizes the
107 // contradiction `0 * 0 = 4`.
108 // We also evaluate the negated constraint `x != 0`; the constraint solver
109 // deduces `x = 1` and then realizes the contradiction `1 * 1 = 4`.
110 // Both children are infeasible, thus the parent state is marked as
111 // posteriorly over-constrained. These parents are handled with special care:
112 // we do not allow transitions to exploded nodes with such states.
113 bool PosteriorlyOverconstrained = false;
114 // Make internal constraint solver entities friends so they can access the
115 // overconstrained-related functions. We want to keep this API inaccessible
116 // for Checkers.
117 friend class ConstraintManager;
118 // The CoreEngine also needs to be a friend to mark nodes as sinks if they
119 // are generated with a PosteriorlyOverconstrained state.
120 // FIXME: Perform this check in the relevant methods of `ExplodedGraph` and
121 // remove this `friend` declaration.
122 friend class CoreEngine;
123 bool isPosteriorlyOverconstrained() const {
124 return PosteriorlyOverconstrained;
125 }
126 ProgramStateRef cloneAsPosteriorlyOverconstrained() const;
127
128 unsigned refCount;
129
130 /// makeWithStore - Return a ProgramState with the same values as the current
131 /// state with the exception of using the specified Store.
132 ProgramStateRef makeWithStore(const StoreRef &store) const;
133 ProgramStateRef makeWithStore(const BindResult &BindRes) const;
134
135 void setStore(const StoreRef &storeRef);
136
137public:
138 /// This ctor is used when creating the first ProgramState object.
140 StoreRef st, GenericDataMap gdm);
141
142 /// Copy ctor - We must explicitly define this or else the "Next" ptr
143 /// in FoldingSetNode will also get copied.
144 ProgramState(const ProgramState &RHS);
145
147
148 int64_t getID() const;
149
150 /// Return the ProgramStateManager associated with this state.
152 return *stateMgr;
153 }
154
156
157 /// Return the ConstraintManager.
159
160 /// getEnvironment - Return the environment associated with this state.
161 /// The environment is the mapping from expressions to values.
162 const Environment& getEnvironment() const { return Env; }
163
164 /// Return the store associated with this state. The store
165 /// is a mapping from locations to values.
166 Store getStore() const { return store; }
167
168 /// getGDM - Return the generic data map associated with this state.
169 GenericDataMap getGDM() const { return GDM; }
170
171 void setGDM(GenericDataMap gdm) { GDM = gdm; }
172
173 /// Profile - Profile the contents of a ProgramState object for use in a
174 /// FoldingSet. Two ProgramState objects are considered equal if they
175 /// have the same Environment, Store, and GenericDataMap.
176 static void Profile(llvm::FoldingSetNodeID& ID, const ProgramState *V) {
177 V->Env.Profile(ID);
178 ID.AddPointer(V->store);
179 V->GDM.Profile(ID);
180 ID.AddBoolean(V->PosteriorlyOverconstrained);
181 }
182
183 /// Profile - Used to profile the contents of this object for inclusion
184 /// in a FoldingSet.
185 void Profile(llvm::FoldingSetNodeID& ID) const {
186 Profile(ID, this);
187 }
188
191
192 //==---------------------------------------------------------------------==//
193 // Constraints on values.
194 //==---------------------------------------------------------------------==//
195 //
196 // Each ProgramState records constraints on symbolic values. These constraints
197 // are managed using the ConstraintManager associated with a ProgramStateManager.
198 // As constraints gradually accrue on symbolic values, added constraints
199 // may conflict and indicate that a state is infeasible (as no real values
200 // could satisfy all the constraints). This is the principal mechanism
201 // for modeling path-sensitivity in ExprEngine/ProgramState.
202 //
203 // Various "assume" methods form the interface for adding constraints to
204 // symbolic values. A call to 'assume' indicates an assumption being placed
205 // on one or symbolic values. 'assume' methods take the following inputs:
206 //
207 // (1) A ProgramState object representing the current state.
208 //
209 // (2) The assumed constraint (which is specific to a given "assume" method).
210 //
211 // (3) A binary value "Assumption" that indicates whether the constraint is
212 // assumed to be true or false.
213 //
214 // The output of "assume*" is a new ProgramState object with the added constraints.
215 // If no new state is feasible, NULL is returned.
216 //
217
218 /// Assumes that the value of \p cond is zero (if \p assumption is "false")
219 /// or non-zero (if \p assumption is "true").
220 ///
221 /// This returns a new state with the added constraint on \p cond.
222 /// If no new state is feasible, NULL is returned.
224 bool assumption) const;
225
226 /// Assumes both "true" and "false" for \p cond, and returns both
227 /// corresponding states (respectively).
228 ///
229 /// This is more efficient than calling assume() twice. Note that one (but not
230 /// both) of the returned states may be NULL.
231 [[nodiscard]] std::pair<ProgramStateRef, ProgramStateRef>
232 assume(DefinedOrUnknownSVal cond) const;
233
234 [[nodiscard]] std::pair<ProgramStateRef, ProgramStateRef>
236 QualType IndexType = QualType()) const;
237
238 [[nodiscard]] ProgramStateRef
240 bool assumption, QualType IndexType = QualType()) const;
241
242 /// Assumes that the value of \p Val is bounded with [\p From; \p To]
243 /// (if \p assumption is "true") or it is fully out of this range
244 /// (if \p assumption is "false").
245 ///
246 /// This returns a new state with the added constraint on \p cond.
247 /// If no new state is feasible, NULL is returned.
249 const llvm::APSInt &From,
250 const llvm::APSInt &To,
251 bool assumption) const;
252
253 /// Assumes given range both "true" and "false" for \p Val, and returns both
254 /// corresponding states (respectively).
255 ///
256 /// This is more efficient than calling assume() twice. Note that one (but not
257 /// both) of the returned states may be NULL.
258 [[nodiscard]] std::pair<ProgramStateRef, ProgramStateRef>
259 assumeInclusiveRange(DefinedOrUnknownSVal Val, const llvm::APSInt &From,
260 const llvm::APSInt &To) const;
261
262 /// Check if the given SVal is not constrained to zero and is not
263 /// a zero constant.
265
266 /// Check if the given SVal is constrained to zero or is a zero
267 /// constant.
269
270 /// \return Whether values \p Lhs and \p Rhs are equal.
271 ConditionTruthVal areEqual(SVal Lhs, SVal Rhs) const;
272
273 /// Utility method for getting regions.
274 LLVM_ATTRIBUTE_RETURNS_NONNULL
275 const VarRegion* getRegion(const VarDecl *D, const LocationContext *LC) const;
276
277 //==---------------------------------------------------------------------==//
278 // Binding and retrieving values to/from the environment and symbolic store.
279 //==---------------------------------------------------------------------==//
280
281 /// Create a new state by binding the value \p V to the expression \p E in
282 /// the state's environment.
283 [[nodiscard]] ProgramStateRef BindExpr(const Expr *E,
284 const LocationContext *LCtx, SVal V,
285 bool Invalidate = true) const;
286
287 [[nodiscard]] ProgramStateRef bindLoc(Loc location, SVal V,
288 const LocationContext *LCtx,
289 bool notifyChanges = true) const;
290
291 [[nodiscard]] ProgramStateRef bindLoc(SVal location, SVal V,
292 const LocationContext *LCtx) const;
293
294 /// Initializes the region of memory represented by \p loc with an initial
295 /// value. Once initialized, all values loaded from any sub-regions of that
296 /// region will be equal to \p V, unless overwritten later by the program.
297 /// This method should not be used on regions that are already initialized.
298 /// If you need to indicate that memory contents have suddenly become unknown
299 /// within a certain region of memory, consider invalidateRegions().
300 [[nodiscard]] ProgramStateRef
301 bindDefaultInitial(SVal loc, SVal V, const LocationContext *LCtx) const;
302
303 /// Performs C++ zero-initialization procedure on the region of memory
304 /// represented by \p loc.
305 [[nodiscard]] ProgramStateRef
306 bindDefaultZero(SVal loc, const LocationContext *LCtx) const;
307
308 [[nodiscard]] ProgramStateRef killBinding(Loc LV) const;
309
310 /// Returns the state with bindings for the given regions cleared from the
311 /// store. If \p Call is non-null, also invalidates global regions (but if
312 /// \p Call is from a system header, then this is limited to globals declared
313 /// in system headers).
314 ///
315 /// This calls the lower-level method \c StoreManager::invalidateRegions to
316 /// do the actual invalidation, then calls the checker callbacks which should
317 /// be triggered by this event.
318 ///
319 /// \param Regions the set of regions to be invalidated.
320 /// \param Elem The CFG Element that caused the invalidation.
321 /// \param BlockCount The number of times the current basic block has been
322 /// visited.
323 /// \param CausesPointerEscape the flag is set to true when the invalidation
324 /// entails escape of a symbol (representing a pointer). For example,
325 /// due to it being passed as an argument in a call.
326 /// \param IS the set of invalidated symbols.
327 /// \param Call if non-null, the invalidated regions represent parameters to
328 /// the call and should be considered directly invalidated.
329 /// \param ITraits information about special handling for particular regions
330 /// or symbols.
331 [[nodiscard]] ProgramStateRef
333 ConstCFGElementRef Elem, unsigned BlockCount,
334 const LocationContext *LCtx, bool CausesPointerEscape,
335 InvalidatedSymbols *IS = nullptr,
336 const CallEvent *Call = nullptr,
337 RegionAndSymbolInvalidationTraits *ITraits = nullptr) const;
338
339 [[nodiscard]] ProgramStateRef
341 unsigned BlockCount, const LocationContext *LCtx,
342 bool CausesPointerEscape, InvalidatedSymbols *IS = nullptr,
343 const CallEvent *Call = nullptr,
344 RegionAndSymbolInvalidationTraits *ITraits = nullptr) const;
345
346 /// enterStackFrame - Returns the state for entry to the given stack frame,
347 /// preserving the current state.
348 [[nodiscard]] ProgramStateRef
349 enterStackFrame(const CallEvent &Call, const StackFrame *CalleeSF) const;
350
351 /// Return the value of 'self' if available in the given context.
352 SVal getSelfSVal(const LocationContext *LC) const;
353
354 /// Get the lvalue for a base class object reference.
355 Loc getLValue(const CXXBaseSpecifier &BaseSpec, const SubRegion *Super) const;
356
357 /// Get the lvalue for a base class object reference.
358 Loc getLValue(const CXXRecordDecl *BaseClass, const SubRegion *Super,
359 bool IsVirtual) const;
360
361 /// Get the lvalue for a variable reference.
362 Loc getLValue(const VarDecl *D, const LocationContext *LC) const;
363
364 Loc getLValue(const CompoundLiteralExpr *literal,
365 const LocationContext *LC) const;
366
367 /// Get the lvalue for an ivar reference.
368 SVal getLValue(const ObjCIvarDecl *decl, SVal base) const;
369
370 /// Get the lvalue for a field reference.
371 SVal getLValue(const FieldDecl *decl, SVal Base) const;
372
373 /// Get the lvalue for an indirect field reference.
375
376 /// Get the lvalue for an array index.
377 SVal getLValue(QualType ElementType, SVal Idx, SVal Base) const;
378
379 /// Returns the SVal bound to the expression \p E in the state's environment.
380 SVal getSVal(const Expr *E, const LocationContext *LCtx) const;
381
382 SVal getSValAsScalarOrLoc(const Expr *E, const LocationContext *LCtx) const;
383
384 /// Return the value bound to the specified location.
385 /// Returns UnknownVal() if none found.
386 SVal getSVal(Loc LV, QualType T = QualType()) const;
387
388 /// Returns the "raw" SVal bound to LV before any value simplification.
389 SVal getRawSVal(Loc LV, QualType T= QualType()) const;
390
391 /// Return the value bound to the specified location.
392 /// Returns UnknownVal() if none found.
393 SVal getSVal(const MemRegion* R, QualType T = QualType()) const;
394
395 /// Return the value bound to the specified location, assuming
396 /// that the value is a scalar integer or an enumeration or a pointer.
397 /// Returns UnknownVal() if none found or the region is not known to hold
398 /// a value of such type.
399 SVal getSValAsScalarOrLoc(const MemRegion *R) const;
400
401 using region_iterator = const MemRegion **;
402
403 /// Visits the symbols reachable from the given SVal using the provided
404 /// SymbolVisitor.
405 ///
406 /// This is a convenience API. Consider using ScanReachableSymbols class
407 /// directly when making multiple scans on the same state with the same
408 /// visitor to avoid repeated initialization cost.
409 /// \sa ScanReachableSymbols
410 bool scanReachableSymbols(SVal val, SymbolVisitor& visitor) const;
411
412 /// Visits the symbols reachable from the regions in the given
413 /// MemRegions range using the provided SymbolVisitor.
414 bool scanReachableSymbols(llvm::iterator_range<region_iterator> Reachable,
415 SymbolVisitor &visitor) const;
416
417 template <typename CB> CB scanReachableSymbols(SVal val) const;
418 template <typename CB> CB
419 scanReachableSymbols(llvm::iterator_range<region_iterator> Reachable) const;
420
421 //==---------------------------------------------------------------------==//
422 // Accessing the Generic Data Map (GDM).
423 //==---------------------------------------------------------------------==//
424
425 void *const *FindGDM(const void *K) const;
426
427 template <typename T>
428 [[nodiscard]] ProgramStateRef
429 add(typename ProgramStateTrait<T>::key_type K) const;
430
431 template <typename T>
436
437 template<typename T>
443
444 template <typename T>
446
447 template <typename T>
448 [[nodiscard]] ProgramStateRef
449 remove(typename ProgramStateTrait<T>::key_type K) const;
450
451 template <typename T>
452 [[nodiscard]] ProgramStateRef
455
456 template <typename T> [[nodiscard]] ProgramStateRef remove() const;
457
458 template <typename T>
459 [[nodiscard]] ProgramStateRef
460 set(typename ProgramStateTrait<T>::data_type D) const;
461
462 template <typename T>
463 [[nodiscard]] ProgramStateRef
465 typename ProgramStateTrait<T>::value_type E) const;
466
467 template <typename T>
468 [[nodiscard]] ProgramStateRef
472
473 template<typename T>
478
479 // Pretty-printing.
480 void printJson(raw_ostream &Out, const LocationContext *LCtx = nullptr,
481 const char *NL = "\n", unsigned int Space = 0,
482 bool IsDot = false) const;
483
484 void printDOT(raw_ostream &Out, const LocationContext *LCtx = nullptr,
485 unsigned int Space = 0) const;
486
487 void dump() const;
488
489private:
490 friend void ProgramStateRetain(const ProgramState *state);
491 friend void ProgramStateRelease(const ProgramState *state);
492
493 SVal desugarReference(SVal Val) const;
494 SVal wrapSymbolicRegion(SVal Base) const;
495};
496
497//===----------------------------------------------------------------------===//
498// ProgramStateManager - Factory object for ProgramStates.
499//===----------------------------------------------------------------------===//
500
502 friend class ProgramState;
503 friend void ProgramStateRelease(const ProgramState *state);
504private:
505 /// Eng - The ExprEngine that owns this state manager.
506 ExprEngine *Eng; /* Can be null. */
507
508 EnvironmentManager EnvMgr;
509 std::unique_ptr<StoreManager> StoreMgr;
510 std::unique_ptr<ConstraintManager> ConstraintMgr;
511
512 ProgramState::GenericDataMap::Factory GDMFactory;
513
514 typedef llvm::DenseMap<const void *, std::pair<void *, void (*)(void *)>>
515 GDMContextsTy;
516 GDMContextsTy GDMContexts;
517
518 /// StateSet - FoldingSet containing all the states created for analyzing
519 /// a particular function. This is used to unique states.
520 llvm::FoldingSet<ProgramState> StateSet;
521
522 /// Object that manages the data for all created SVals.
523 std::unique_ptr<SValBuilder> svalBuilder;
524
525 /// Manages memory for created CallEvents.
526 std::unique_ptr<CallEventManager> CallEventMgr;
527
528 /// A BumpPtrAllocator to allocate states.
529 llvm::BumpPtrAllocator &Alloc;
530
531 /// A vector of ProgramStates that we can reuse.
532 std::vector<ProgramState *> freeStates;
533
534public:
536 StoreManagerCreator CreateStoreManager,
537 ConstraintManagerCreator CreateConstraintManager,
538 llvm::BumpPtrAllocator& alloc,
539 ExprEngine *expreng);
540
542
544
545 ASTContext &getContext() { return svalBuilder->getContext(); }
546 const ASTContext &getContext() const { return svalBuilder->getContext(); }
547
549 return svalBuilder->getBasicValueFactory();
550 }
551
553 return *svalBuilder;
554 }
555
557 return *svalBuilder;
558 }
559
561 return svalBuilder->getSymbolManager();
562 }
564 return svalBuilder->getSymbolManager();
565 }
566
567 llvm::BumpPtrAllocator& getAllocator() { return Alloc; }
568
570 return svalBuilder->getRegionManager();
571 }
573 return svalBuilder->getRegionManager();
574 }
575
576 CallEventManager &getCallEventManager() { return *CallEventMgr; }
577
578 StoreManager &getStoreManager() { return *StoreMgr; }
579 const StoreManager &getStoreManager() const { return *StoreMgr; }
580 ConstraintManager &getConstraintManager() { return *ConstraintMgr; }
582 return *ConstraintMgr;
583 }
584 ExprEngine &getOwningEngine() { return *Eng; }
585
587 ProgramStateRef St, const StackFrame *SF, SymbolReaper &SymReaper);
588
589public:
590
592 return StoreMgr->ArrayToPointer(Array, ElementTy);
593 }
594
595 // Methods that manipulate the GDM.
596 ProgramStateRef addGDM(ProgramStateRef St, const void *Key, void *Data);
597 ProgramStateRef removeGDM(ProgramStateRef state, const void *Key);
598
599 // Methods that query & manipulate the Store.
600
602 StoreMgr->iterBindings(state->getStore(), F);
603 }
604
607 ProgramStateRef GDMState);
608
610 return ConstraintMgr->haveEqualConstraints(S1, S2);
611 }
612
614 return S1->Env == S2->Env;
615 }
616
618 return S1->store == S2->store;
619 }
620
621 //==---------------------------------------------------------------------==//
622 // Generic Data Map methods.
623 //==---------------------------------------------------------------------==//
624 //
625 // ProgramStateManager and ProgramState support a "generic data map" that allows
626 // different clients of ProgramState objects to embed arbitrary data within a
627 // ProgramState object. The generic data map is essentially an immutable map
628 // from a "tag" (that acts as the "key" for a client) and opaque values.
629 // Tags/keys and values are simply void* values. The typical way that clients
630 // generate unique tags are by taking the address of a static variable.
631 // Clients are responsible for ensuring that data values referred to by a
632 // the data pointer are immutable (and thus are essentially purely functional
633 // data).
634 //
635 // The templated methods below use the ProgramStateTrait<T> class
636 // to resolve keys into the GDM and to return data values to clients.
637 //
638
639 // Trait based GDM dispatch.
640 template <typename T>
645
646 template<typename T>
655
656 template <typename T>
663
664 template <typename T>
672
673 template <typename T>
677
678 void *FindGDMContext(const void *index,
679 void *(*CreateContext)(llvm::BumpPtrAllocator &),
680 void (*DeleteContext)(void *));
681
682 template <typename T>
690};
691
692
693//===----------------------------------------------------------------------===//
694// Out-of-line method definitions for ProgramState.
695//===----------------------------------------------------------------------===//
696
698 return stateMgr->getConstraintManager();
699}
700
702 const LocationContext *LC) const
703{
705}
706
708 bool Assumption) const {
709 if (Cond.isUnknown())
710 return this;
711
712 return getStateManager().ConstraintMgr
713 ->assume(this, Cond.castAs<DefinedSVal>(), Assumption);
714}
715
716inline std::pair<ProgramStateRef , ProgramStateRef >
718 if (Cond.isUnknown())
719 return std::make_pair(this, this);
720
721 return getStateManager().ConstraintMgr
722 ->assumeDual(this, Cond.castAs<DefinedSVal>());
723}
724
726 DefinedOrUnknownSVal Val, const llvm::APSInt &From, const llvm::APSInt &To,
727 bool Assumption) const {
728 if (Val.isUnknown())
729 return this;
730
731 assert(isa<NonLoc>(Val) && "Only NonLocs are supported!");
732
733 return getStateManager().ConstraintMgr->assumeInclusiveRange(
734 this, Val.castAs<NonLoc>(), From, To, Assumption);
735}
736
737inline std::pair<ProgramStateRef, ProgramStateRef>
739 const llvm::APSInt &From,
740 const llvm::APSInt &To) const {
741 if (Val.isUnknown())
742 return std::make_pair(this, this);
743
744 assert(isa<NonLoc>(Val) && "Only NonLocs are supported!");
745
746 return getStateManager().ConstraintMgr->assumeInclusiveRangeDual(
747 this, Val.castAs<NonLoc>(), From, To);
748}
749
751 if (std::optional<Loc> L = LV.getAs<Loc>())
752 return bindLoc(*L, V, LCtx);
753 return this;
754}
755
757 const SubRegion *Super) const {
758 const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
759 return loc::MemRegionVal(
760 getStateManager().getRegionManager().getCXXBaseObjectRegion(
761 Base, Super, BaseSpec.isVirtual()));
762}
763
765 const SubRegion *Super,
766 bool IsVirtual) const {
767 return loc::MemRegionVal(
768 getStateManager().getRegionManager().getCXXBaseObjectRegion(
769 BaseClass, Super, IsVirtual));
770}
771
773 const LocationContext *LC) const {
774 return getStateManager().StoreMgr->getLValueVar(VD, LC);
775}
776
778 const LocationContext *LC) const {
779 return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC);
780}
781
783 return getStateManager().StoreMgr->getLValueIvar(D, Base);
784}
785
786inline SVal ProgramState::getLValue(QualType ElementType, SVal Idx, SVal Base) const{
787 if (std::optional<NonLoc> N = Idx.getAs<NonLoc>())
788 return getStateManager().StoreMgr->getLValueElement(ElementType, *N, Base);
789 return UnknownVal();
790}
791
793 const LocationContext *LCtx) const {
794 return Env.getSVal(EnvironmentEntry(E, LCtx), *getStateManager().svalBuilder);
795}
796
797inline SVal
799 const LocationContext *LCtx) const {
800 QualType T = E->getType();
801 if (E->isGLValue() || Loc::isLocType(T) || T->isIntegralOrEnumerationType())
802 return getSVal(E, LCtx);
803 return UnknownVal();
804}
805
807 return getStateManager().StoreMgr->getBinding(getStore(), LV, T);
808}
809
810inline SVal ProgramState::getSVal(const MemRegion* R, QualType T) const {
811 return getStateManager().StoreMgr->getBinding(getStore(),
813 T);
814}
815
819
823
824template<typename T>
828
829template <typename T>
833
834template<typename T>
838
839template<typename T>
844
845template <typename T>
847 return getStateManager().remove<T>(this);
848}
849
850template<typename T>
854
855template<typename T>
860
861template<typename T>
867
868template <typename CB>
870 CB cb(this);
871 scanReachableSymbols(val, cb);
872 return cb;
873}
874
875template <typename CB>
877 llvm::iterator_range<region_iterator> Reachable) const {
878 CB cb(this);
879 scanReachableSymbols(Reachable, cb);
880 return cb;
881}
882
883/// \class ScanReachableSymbols
884/// A utility class that visits the reachable symbols using a custom
885/// SymbolVisitor. Terminates recursive traversal when the visitor function
886/// returns false.
888 typedef llvm::DenseSet<const void*> VisitedItems;
889
890 VisitedItems visited;
891 ProgramStateRef state;
892 SymbolVisitor &visitor;
893public:
895 : state(std::move(st)), visitor(v) {}
896
898 bool scan(nonloc::CompoundVal val);
899 bool scan(SVal val);
900 bool scan(const MemRegion *R);
901 bool scan(const SymExpr *sym);
902};
903
904} // end ento namespace
905
906} // end clang namespace
907
908#endif
#define V(N, I)
llvm::APSInt APSInt
Definition Compiler.cpp:24
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
llvm::json::Array Array
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:227
Represents a base class of a C++ class.
Definition DeclCXX.h:146
bool isVirtual() const
Determines whether the base class is a virtual base class (or not).
Definition DeclCXX.h:203
QualType getType() const
Retrieves the type of the base class.
Definition DeclCXX.h:249
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CompoundLiteralExpr - [C99 6.5.2.5].
Definition Expr.h:3608
This represents one expression.
Definition Expr.h:112
bool isGLValue() const
Definition Expr.h:287
QualType getType() const
Definition Expr.h:144
Represents a member of a struct/union/class.
Definition Decl.h:3178
Represents a field injected from an anonymous union/struct into the parent scope.
Definition Decl.h:3485
ObjCIvarDecl - Represents an ObjC instance variable.
Definition DeclObjC.h:1952
A (possibly-)qualified type.
Definition TypeBase.h:937
It represents a stack frame of the call stack (based on CallEvent).
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
Represents a variable declaration or definition.
Definition Decl.h:924
Manages the lifetime of CallEvent objects.
Definition CallEvent.h:1374
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:153
An entry in the environment consists of a Stmt and an LocationContext.
Definition Environment.h:35
An immutable map from EnvironmentEntries to SVals.
Definition Environment.h:55
static bool isLocType(QualType T)
Definition SVals.h:262
const VarRegion * getVarRegion(const VarDecl *VD, const LocationContext *LC)
getVarRegion - Retrieve or create the memory region associated with a specified VarDecl and LocationC...
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:98
ProgramStateRef remove(ProgramStateRef st)
const MemRegionManager & getRegionManager() const
bool haveEqualStores(ProgramStateRef S1, ProgramStateRef S2) const
const ASTContext & getContext() const
const StoreManager & getStoreManager() const
void * FindGDMContext(const void *index, void *(*CreateContext)(llvm::BumpPtrAllocator &), void(*DeleteContext)(void *))
CallEventManager & getCallEventManager()
bool haveEqualEnvironments(ProgramStateRef S1, ProgramStateRef S2) const
const SymbolManager & getSymbolManager() const
const SValBuilder & getSValBuilder() const
friend void ProgramStateRelease(const ProgramState *state)
Decrement the number of times this state is referenced.
ProgramStateRef set(ProgramStateRef st, typename ProgramStateTrait< T >::data_type D)
ProgramStateRef removeDeadBindingsFromEnvironmentAndStore(ProgramStateRef St, const StackFrame *SF, SymbolReaper &SymReaper)
ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState, ProgramStateRef GDMState)
ProgramStateRef removeGDM(ProgramStateRef state, const void *Key)
MemRegionManager & getRegionManager()
bool haveEqualConstraints(ProgramStateRef S1, ProgramStateRef S2) const
ProgramStateRef remove(ProgramStateRef st, typename ProgramStateTrait< T >::key_type K, typename ProgramStateTrait< T >::context_type C)
ProgramStateRef addGDM(ProgramStateRef St, const void *Key, void *Data)
ProgramStateRef set(ProgramStateRef st, typename ProgramStateTrait< T >::key_type K, typename ProgramStateTrait< T >::value_type V, typename ProgramStateTrait< T >::context_type C)
ProgramStateRef add(ProgramStateRef st, typename ProgramStateTrait< T >::key_type K, typename ProgramStateTrait< T >::context_type C)
ProgramStateRef getPersistentState(ProgramState &Impl)
void iterBindings(ProgramStateRef state, StoreManager::BindingsHandler &F)
SVal ArrayToPointer(Loc Array, QualType ElementTy)
const ConstraintManager & getConstraintManager() const
ProgramStateRef getInitialState(const LocationContext *InitLoc)
llvm::BumpPtrAllocator & getAllocator()
BasicValueFactory & getBasicVals()
ProgramStateTrait< T >::context_type get_context()
ProgramStateManager(ASTContext &Ctx, StoreManagerCreator CreateStoreManager, ConstraintManagerCreator CreateConstraintManager, llvm::BumpPtrAllocator &alloc, ExprEngine *expreng)
ConstraintManager & getConstraintManager()
ProgramState - This class encapsulates:
bool scanReachableSymbols(SVal val, SymbolVisitor &visitor) const
Visits the symbols reachable from the given SVal using the provided SymbolVisitor.
ProgramStateTrait< T >::data_type get() const
Loc getLValue(const CXXBaseSpecifier &BaseSpec, const SubRegion *Super) const
Get the lvalue for a base class object reference.
friend void ProgramStateRetain(const ProgramState *state)
Increments the number of times this state is referenced.
ProgramStateRef bindDefaultZero(SVal loc, const LocationContext *LCtx) const
Performs C++ zero-initialization procedure on the region of memory represented by loc.
friend class ProgramStateManager
llvm::ImmutableMap< const void *, void * > GenericDataMap
void printJson(raw_ostream &Out, const LocationContext *LCtx=nullptr, const char *NL="\n", unsigned int Space=0, bool IsDot=false) const
ProgramStateRef assumeInclusiveRange(DefinedOrUnknownSVal Val, const llvm::APSInt &From, const llvm::APSInt &To, bool assumption) const
Assumes that the value of Val is bounded with [From; To] (if assumption is "true") or it is fully out...
bool contains(typename ProgramStateTrait< T >::key_type key) const
ProgramStateRef bindDefaultInitial(SVal loc, SVal V, const LocationContext *LCtx) const
Initializes the region of memory represented by loc with an initial value.
ConstraintManager & getConstraintManager() const
Return the ConstraintManager.
ProgramStateRef add(typename ProgramStateTrait< T >::key_type K) const
void Profile(llvm::FoldingSetNodeID &ID) const
Profile - Used to profile the contents of this object for inclusion in a FoldingSet.
SVal getSelfSVal(const LocationContext *LC) const
Return the value of 'self' if available in the given context.
SVal getRawSVal(Loc LV, QualType T=QualType()) const
Returns the "raw" SVal bound to LV before any value simplification.
ConditionTruthVal isNull(SVal V) const
Check if the given SVal is constrained to zero or is a zero constant.
ProgramStateManager & getStateManager() const
Return the ProgramStateManager associated with this state.
ProgramStateRef killBinding(Loc LV) const
ProgramState(ProgramStateManager *mgr, const Environment &env, StoreRef st, GenericDataMap gdm)
This ctor is used when creating the first ProgramState object.
GenericDataMap getGDM() const
getGDM - Return the generic data map associated with this state.
const Environment & getEnvironment() const
getEnvironment - Return the environment associated with this state.
friend void ProgramStateRelease(const ProgramState *state)
Decrement the number of times this state is referenced.
ProgramStateRef BindExpr(const Expr *E, const LocationContext *LCtx, SVal V, bool Invalidate=true) const
Create a new state by binding the value V to the expression E in the state's environment.
ProgramStateRef assume(DefinedOrUnknownSVal cond, bool assumption) const
Assumes that the value of cond is zero (if assumption is "false") or non-zero (if assumption is "true...
Store getStore() const
Return the store associated with this state.
SVal getSVal(const Expr *E, const LocationContext *LCtx) const
Returns the SVal bound to the expression E in the state's environment.
ConditionTruthVal areEqual(SVal Lhs, SVal Rhs) const
void printDOT(raw_ostream &Out, const LocationContext *LCtx=nullptr, unsigned int Space=0) const
ConditionTruthVal isNonNull(SVal V) const
Check if the given SVal is not constrained to zero and is not a zero constant.
ProgramStateRef set(typename ProgramStateTrait< T >::data_type D) const
ProgramStateRef assumeInBound(DefinedOrUnknownSVal idx, DefinedOrUnknownSVal upperBound, bool assumption, QualType IndexType=QualType()) const
LLVM_ATTRIBUTE_RETURNS_NONNULL const VarRegion * getRegion(const VarDecl *D, const LocationContext *LC) const
Utility method for getting regions.
ProgramStateTrait< T >::lookup_type get(typename ProgramStateTrait< T >::key_type key) const
const MemRegion ** region_iterator
ProgramStateTrait< T >::context_type get_context() const
ProgramStateRef invalidateRegions(ArrayRef< const MemRegion * > Regions, ConstCFGElementRef Elem, unsigned BlockCount, const LocationContext *LCtx, bool CausesPointerEscape, InvalidatedSymbols *IS=nullptr, const CallEvent *Call=nullptr, RegionAndSymbolInvalidationTraits *ITraits=nullptr) const
Returns the state with bindings for the given regions cleared from the store.
ProgramStateRef bindLoc(Loc location, SVal V, const LocationContext *LCtx, bool notifyChanges=true) const
static void Profile(llvm::FoldingSetNodeID &ID, const ProgramState *V)
Profile - Profile the contents of a ProgramState object for use in a FoldingSet.
BasicValueFactory & getBasicVals() const
std::pair< ProgramStateRef, ProgramStateRef > assumeInBoundDual(DefinedOrUnknownSVal idx, DefinedOrUnknownSVal upperBound, QualType IndexType=QualType()) const
ProgramStateRef invalidateRegions(ArrayRef< SVal > Values, ConstCFGElementRef Elem, unsigned BlockCount, const LocationContext *LCtx, bool CausesPointerEscape, InvalidatedSymbols *IS=nullptr, const CallEvent *Call=nullptr, RegionAndSymbolInvalidationTraits *ITraits=nullptr) const
void *const * FindGDM(const void *K) const
ProgramStateRef remove() const
void setGDM(GenericDataMap gdm)
AnalysisManager & getAnalysisManager() const
SVal getSValAsScalarOrLoc(const Expr *E, const LocationContext *LCtx) const
ProgramStateRef enterStackFrame(const CallEvent &Call, const StackFrame *CalleeSF) const
enterStackFrame - Returns the state for entry to the given stack frame, preserving the current state.
SymbolManager & getSymbolManager() const
Information about invalidation for a particular region/symbol.
Definition MemRegion.h:1661
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:56
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
Definition SVals.h:87
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition SVals.h:83
bool isUnknown() const
Definition SVals.h:105
ScanReachableSymbols(ProgramStateRef st, SymbolVisitor &v)
bool scan(nonloc::LazyCompoundVal val)
SubRegion - A region that subsets another larger region.
Definition MemRegion.h:474
Symbolic value.
Definition SymExpr.h:32
A class responsible for cleaning up unused symbols.
The simplest example of a concrete compound value is nonloc::CompoundVal, which represents a concrete...
Definition SVals.h:339
While nonloc::CompoundVal covers a few simple use cases, nonloc::LazyCompoundVal is a more performant...
Definition SVals.h:389
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
llvm::DenseSet< SymbolRef > InvalidatedSymbols
Definition Store.h:51
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
std::unique_ptr< ConstraintManager >(* ConstraintManagerCreator)(ProgramStateManager &, ExprEngine *)
std::unique_ptr< StoreManager >(* StoreManagerCreator)(ProgramStateManager &)
const void * Store
Store - This opaque type encapsulates an immutable mapping from locations to values.
Definition StoreRef.h:27
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
CFGBlock::ConstCFGElementRef ConstCFGElementRef
Definition CFG.h:1227
Expr * Cond
};
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
static void * MakeVoidPtr(data_type D)
static data_type MakeData(void *const *P)