clang 24.0.0git
ProgramState.cpp
Go to the documentation of this file.
1//= ProgramState.cpp - 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 implements ProgramState and ProgramStateManager.
10//
11//===----------------------------------------------------------------------===//
12
14#include "clang/Analysis/CFG.h"
20#include "llvm/Support/raw_ostream.h"
21#include <optional>
22
23using namespace clang;
24using namespace ento;
25
26void ProgramStateManager::freeState(ProgramState *State) {
27 StateSet.erase(State);
28 State->~ProgramState();
29 freeStates.push_back(State);
30}
31
34 : stateMgr(mgr),
35 Env(env),
36 store(st.getStore()),
37 GDM(gdm),
38 refCount(0) {
39 stateMgr->getStoreManager().incrementReferenceCount(store);
40}
41
43 : stateMgr(RHS.stateMgr), Env(RHS.Env), store(RHS.store), GDM(RHS.GDM),
44 PosteriorlyOverconstrained(RHS.PosteriorlyOverconstrained), refCount(0) {
45 stateMgr->getStoreManager().incrementReferenceCount(store);
46}
47
49 : stateMgr(RHS.stateMgr), Env(std::move(RHS.Env)), store(RHS.store),
50 GDM(std::move(RHS.GDM)),
51 PosteriorlyOverconstrained(RHS.PosteriorlyOverconstrained), refCount(0) {
52 RHS.store = nullptr;
53}
54
56 if (store)
57 stateMgr->getStoreManager().decrementReferenceCount(store);
58}
59
60int64_t ProgramState::getID() const {
61 return getStateManager().Alloc.identifyKnownAlignedObject<ProgramState>(this);
62}
63
65 StoreManagerCreator CreateSMgr,
66 ConstraintManagerCreator CreateCMgr,
67 llvm::BumpPtrAllocator &alloc,
68 ExprEngine *ExprEng)
69 : Eng(ExprEng), EnvMgr(alloc), GDMFactory(alloc),
70 svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
71 CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) {
72 StoreMgr = (*CreateSMgr)(*this);
73 ConstraintMgr = (*CreateCMgr)(*this, ExprEng);
74}
75
76
78 for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
79 I!=E; ++I)
80 I->second.second(I->second.first);
81}
82
84 ProgramStateRef state, const StackFrame *SF, SymbolReaper &SymReaper) {
85
86 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
87 // The roots are any Block-level exprs and Decls that our liveness algorithm
88 // tells us are live. We then see what Decls they may reference, and keep
89 // those around. This code more than likely can be made faster, and the
90 // frequency of which this method is called should be experimented with
91 // for optimum performance.
92 ProgramState NewState = *state;
93
94 NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state);
95
96 // Clean up the store.
97 StoreRef newStore =
98 StoreMgr->removeDeadBindings(NewState.getStore(), SF, SymReaper);
99 NewState.setStore(newStore);
100 SymReaper.setReapedStore(std::move(newStore));
101
102 return getPersistentState(std::move(NewState));
103}
104
106 bool notifyChanges) const {
108 ExprEngine &Eng = Mgr.getOwningEngine();
109 ProgramStateRef State = makeWithStore(Mgr.StoreMgr->Bind(getStore(), LV, V));
110 const MemRegion *MR = LV.getAsRegion();
111
112 if (MR && notifyChanges)
113 return Eng.processRegionChange(State, MR, SF);
114
115 return State;
116}
117
119 const StackFrame *SF) const {
121 const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
122 BindResult BindRes = Mgr.StoreMgr->BindDefaultInitial(getStore(), R, V);
123 ProgramStateRef State = makeWithStore(BindRes);
124 return Mgr.getOwningEngine().processRegionChange(State, R, SF);
125}
126
128 const StackFrame *SF) const {
130 const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
131 BindResult BindRes = Mgr.StoreMgr->BindDefaultZero(getStore(), R);
132 ProgramStateRef State = makeWithStore(BindRes);
133 return Mgr.getOwningEngine().processRegionChange(State, R, SF);
134}
135
138
140 RegionList Regions, ConstCFGElementRef Elem, unsigned Count,
141 const StackFrame *SF, bool CausedByPointerEscape, InvalidatedSymbols *IS,
142 const CallEvent *Call, RegionAndSymbolInvalidationTraits *ITraits) const {
144 for (const MemRegion *Reg : Regions)
145 Values.push_back(loc::MemRegionVal(Reg));
146
147 return invalidateRegions(Values, Elem, Count, SF, CausedByPointerEscape, IS,
148 Call, ITraits);
149}
150
152 ValueList Values, ConstCFGElementRef Elem, unsigned Count,
153 const StackFrame *SF, bool CausedByPointerEscape, InvalidatedSymbols *IS,
154 const CallEvent *Call, RegionAndSymbolInvalidationTraits *ITraits) const {
155
157 ExprEngine &Eng = Mgr.getOwningEngine();
158
159 InvalidatedSymbols InvalidatedSyms;
160 if (!IS)
161 IS = &InvalidatedSyms;
162
163 RegionAndSymbolInvalidationTraits ITraitsLocal;
164 if (!ITraits)
165 ITraits = &ITraitsLocal;
166
167 StoreManager::InvalidatedRegions TopLevelInvalidated;
169 const StoreRef &NewStore = Mgr.StoreMgr->invalidateRegions(
170 getStore(), Values, Elem, Count, SF, Call, *IS, *ITraits,
171 &TopLevelInvalidated, &Invalidated);
172
173 ProgramStateRef NewState = makeWithStore(NewStore);
174
175 if (CausedByPointerEscape) {
176 NewState = Eng.notifyCheckersOfPointerEscape(
177 NewState, IS, TopLevelInvalidated, Call, *ITraits);
178 }
179
180 return Eng.processRegionChanges(NewState, IS, TopLevelInvalidated,
181 Invalidated, SF, Call);
182}
183
185 Store OldStore = getStore();
186 const StoreRef &newStore =
187 getStateManager().StoreMgr->killBinding(OldStore, LV);
188
189 if (newStore.getStore() == OldStore)
190 return this;
191
192 return makeWithStore(newStore);
193}
194
195/// We should never form a MemRegion that would wrap a TypedValueRegion of a
196/// reference type. What we actually wanted was to create a MemRegion refering
197/// to the pointee of that reference.
198SVal ProgramState::desugarReference(SVal Val) const {
199 const auto *TyReg = dyn_cast_or_null<TypedValueRegion>(Val.getAsRegion());
200 if (!TyReg || !TyReg->getValueType()->isReferenceType())
201 return Val;
202 return getSVal(TyReg);
203}
204
205/// SymbolicRegions are expected to be wrapped by an ElementRegion as a
206/// canonical representation. As a canonical representation, SymbolicRegions
207/// should be wrapped by ElementRegions before getting a FieldRegion.
208/// See f8643a9b31c4029942f67d4534c9139b45173504 why.
209SVal ProgramState::wrapSymbolicRegion(SVal Val) const {
210 const auto *BaseReg = dyn_cast_or_null<SymbolicRegion>(Val.getAsRegion());
211 if (!BaseReg)
212 return Val;
213
215 QualType ElemTy = BaseReg->getPointeeStaticType();
216 return loc::MemRegionVal{SM.GetElementZeroRegion(BaseReg, ElemTy)};
217}
218
221 const StackFrame *CalleeCtx) const {
222 return makeWithStore(
223 getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx));
224}
225
227 const ImplicitParamDecl *SelfDecl = SF->getSelfDecl();
228 if (!SelfDecl)
229 return SVal();
230 return getSVal(getRegion(SelfDecl, SF));
231}
232
234 // We only want to do fetches from regions that we can actually bind
235 // values. For example, SymbolicRegions of type 'id<...>' cannot
236 // have direct bindings (but their can be bindings on their subregions).
237 if (!R->isBoundable())
238 return UnknownVal();
239
240 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
241 QualType T = TR->getValueType();
242 if (Loc::isLocType(T) || T->isIntegralOrEnumerationType())
243 return getSVal(R);
244 }
245
246 return UnknownVal();
247}
248
250 SVal V = getRawSVal(location, T);
251
252 // If 'V' is a symbolic value that is *perfectly* constrained to
253 // be a constant value, use that value instead to lessen the burden
254 // on later analysis stages (so we have less symbolic values to reason
255 // about).
256 // We only go into this branch if we can convert the APSInt value we have
257 // to the type of T, which is not always the case (e.g. for void).
258 if (!T.isNull() && (T->isIntegralOrEnumerationType() || Loc::isLocType(T))) {
259 if (SymbolRef sym = V.getAsSymbol()) {
260 if (const llvm::APSInt *Int = getStateManager()
262 .getSymVal(this, sym)) {
263 // FIXME: Because we don't correctly model (yet) sign-extension
264 // and truncation of symbolic values, we need to convert
265 // the integer value to the correct signedness and bitwidth.
266 //
267 // This shows up in the following:
268 //
269 // char foo();
270 // unsigned x = foo();
271 // if (x == 54)
272 // ...
273 //
274 // The symbolic value stored to 'x' is actually the conjured
275 // symbol for the call to foo(); the type of that symbol is 'char',
276 // not unsigned.
277 APSIntPtr NewV = getBasicVals().Convert(T, *Int);
278 if (V.getAs<Loc>())
279 return loc::ConcreteInt(NewV);
280 return nonloc::ConcreteInt(NewV);
281 }
282 }
283 }
284
285 return V;
286}
287
289 SVal V, bool Invalidate) const {
290 Environment NewEnv = getStateManager().EnvMgr.bindExpr(
291 Env, EnvironmentEntry(E, SF), V, Invalidate);
292 if (NewEnv == Env)
293 return this;
294
295 ProgramState NewSt = *this;
296 NewSt.Env = std::move(NewEnv);
297 return getStateManager().getPersistentState(std::move(NewSt));
298}
299
300[[nodiscard]] std::pair<ProgramStateRef, ProgramStateRef>
302 DefinedOrUnknownSVal UpperBound,
303 QualType indexTy) const {
304 if (Idx.isUnknown() || UpperBound.isUnknown())
305 return {this, this};
306
307 // Build an expression for 0 <= Idx < UpperBound.
308 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
309 // FIXME: This should probably be part of SValBuilder.
311 SValBuilder &svalBuilder = SM.getSValBuilder();
312 ASTContext &Ctx = svalBuilder.getContext();
313
314 // Get the offset: the minimum value of the array index type.
315 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
316 if (indexTy.isNull())
317 indexTy = svalBuilder.getArrayIndexType();
318 nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
319
320 // Adjust the index.
321 SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
322 Idx.castAs<NonLoc>(), Min, indexTy);
323 if (newIdx.isUnknownOrUndef())
324 return {this, this};
325
326 // Adjust the upper bound.
327 SVal newBound =
328 svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
329 Min, indexTy);
330
331 if (newBound.isUnknownOrUndef())
332 return {this, this};
333
334 // Build the actual comparison.
335 SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(),
336 newBound.castAs<NonLoc>(), Ctx.IntTy);
337 if (inBound.isUnknownOrUndef())
338 return {this, this};
339
340 // Finally, let the constraint manager take care of it.
342 return CM.assumeDual(this, inBound.castAs<DefinedSVal>());
343}
344
346 DefinedOrUnknownSVal UpperBound,
347 bool Assumption,
348 QualType indexTy) const {
349 std::pair<ProgramStateRef, ProgramStateRef> R =
350 assumeInBoundDual(Idx, UpperBound, indexTy);
351 return Assumption ? R.first : R.second;
352}
353
356 if (IsNull.isUnderconstrained())
357 return IsNull;
358 return ConditionTruthVal(!IsNull.getValue());
359}
360
362 return stateMgr->getSValBuilder().areEqual(this, Lhs, Rhs);
363}
364
366 if (V.isZeroConstant())
367 return true;
368
369 if (V.isConstant())
370 return false;
371
372 SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
373 if (!Sym)
374 return ConditionTruthVal();
375
376 return getStateManager().ConstraintMgr->isNull(this, Sym);
377}
378
380 ProgramState State(this, EnvMgr.getInitialEnvironment(),
381 StoreMgr->getInitialStore(InitSF),
382 GDMFactory.getEmptyMap());
383
384 return getPersistentState(std::move(State));
385}
386
388 ProgramStateRef FromState,
389 ProgramStateRef GDMState) {
390 ProgramState NewState(*FromState);
391 NewState.GDM = GDMState->GDM;
392 return getPersistentState(std::move(NewState));
393}
394
396
397 llvm::FoldingSetNodeID ID;
398 State.Profile(ID);
399 llvm::FoldingSetInsertToken InsertToken;
400
401 if (ProgramState *I = StateSet.lookup(ID, InsertToken))
402 return I;
403
404 ProgramState *newState = nullptr;
405 if (!freeStates.empty()) {
406 newState = freeStates.back();
407 freeStates.pop_back();
408 }
409 else {
410 newState = Alloc.Allocate<ProgramState>();
411 }
412 new (newState) ProgramState(std::move(State));
413 StateSet.insert(newState, InsertToken);
414 return newState;
415}
416
417ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
418 ProgramState NewSt(*this);
419 NewSt.setStore(store);
420 return getStateManager().getPersistentState(std::move(NewSt));
421}
422
423ProgramStateRef ProgramState::makeWithStore(const BindResult &BindRes) const {
425 ProgramStateRef State = makeWithStore(BindRes.ResultingStore);
426
427 // We must always notify the checkers for failing binds because otherwise they
428 // may keep stale traits for these symbols.
429 // Eg., Malloc checker may report leaks if we failed to bind that symbol.
430 if (BindRes.FailedToBindValues.empty())
431 return State;
432 return Eng.escapeValues(State, BindRes.FailedToBindValues, PSK_EscapeOnBind);
433}
434
435ProgramStateRef ProgramState::cloneAsPosteriorlyOverconstrained() const {
436 ProgramState NewSt(*this);
437 NewSt.PosteriorlyOverconstrained = true;
438 return getStateManager().getPersistentState(std::move(NewSt));
439}
440
441void ProgramState::setStore(const StoreRef &newStore) {
442 Store newStoreStore = newStore.getStore();
443 if (newStoreStore)
444 stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
445 if (store)
446 stateMgr->getStoreManager().decrementReferenceCount(store);
447 store = newStoreStore;
448}
449
451 Base = desugarReference(Base);
452 Base = wrapSymbolicRegion(Base);
453 return getStateManager().StoreMgr->getLValueField(D, Base);
454}
455
457 StoreManager &SM = *getStateManager().StoreMgr;
458 Base = desugarReference(Base);
459 Base = wrapSymbolicRegion(Base);
460
461 // FIXME: This should work with `SM.getLValueField(D->getAnonField(), Base)`,
462 // but that would break some tests. There is probably a bug somewhere that it
463 // would expose.
464 for (const auto *I : D->chain()) {
466 }
467 return Base;
468}
469
470//===----------------------------------------------------------------------===//
471// State pretty-printing.
472//===----------------------------------------------------------------------===//
473
474void ProgramState::printJson(raw_ostream &Out, const StackFrame *SF,
475 const char *NL, unsigned int Space,
476 bool IsDot) const {
477 Indent(Out, Space, IsDot) << "\"program_state\": {" << NL;
478 ++Space;
479
481
482 // Print the store.
483 Mgr.getStoreManager().printJson(Out, getStore(), NL, Space, IsDot);
484
485 // Print out the environment.
486 Env.printJson(Out, Mgr.getContext(), SF, NL, Space, IsDot);
487
488 // Print out the constraints.
489 Mgr.getConstraintManager().printJson(Out, this, NL, Space, IsDot);
490
491 // Print out the tracked dynamic types.
492 printDynamicTypeInfoJson(Out, this, NL, Space, IsDot);
493
494 // Print checker-specific data.
495 Mgr.getOwningEngine().printJson(Out, this, SF, NL, Space, IsDot);
496
497 --Space;
498 Indent(Out, Space, IsDot) << '}';
499}
500
501void ProgramState::printDOT(raw_ostream &Out, const StackFrame *SF,
502 unsigned int Space) const {
503 printJson(Out, SF, /*NL=*/"\\l", Space, /*IsDot=*/true);
504}
505
506LLVM_DUMP_METHOD void ProgramState::dump() const {
507 printJson(llvm::errs());
508}
509
511 return stateMgr->getOwningEngine().getAnalysisManager();
512}
513
514//===----------------------------------------------------------------------===//
515// Generic Data Map.
516//===----------------------------------------------------------------------===//
517
518void *const *ProgramState::FindGDM(const void *K) const {
519 return GDM.lookup(K);
520}
521
523 const void *K, void *(*CreateContext)(llvm::BumpPtrAllocator &),
524 void (*DeleteContext)(void *)) {
525
526 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
527 if (!p.first) {
528 p.first = CreateContext(Alloc);
529 p.second = DeleteContext;
530 }
531
532 return p.first;
533}
534
536 void *Data) {
537 // A null value is indistinguishable from an absent entry for every trait, so
538 // remove the key to keep a single canonical representation of the state.
539 if (!Data)
540 return removeGDM(St, Key);
541
542 ProgramState::GenericDataMap M1 = St->getGDM();
543 ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
544
545 if (M1 == M2)
546 return St;
547
548 ProgramState NewSt = *St;
549 NewSt.GDM = std::move(M2);
550 return getPersistentState(std::move(NewSt));
551}
552
554 const void *Key) {
555 ProgramState::GenericDataMap OldM = state->getGDM();
556 ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
557
558 if (NewM == OldM)
559 return state;
560
561 ProgramState NewState = *state;
562 NewState.GDM = std::move(NewM);
563 return getPersistentState(std::move(NewState));
564}
565
567 bool wasVisited = !visited.insert(val.getCVData()).second;
568 if (wasVisited)
569 return true;
570
571 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
572 // FIXME: We don't really want to use getBaseRegion() here because pointer
573 // arithmetic doesn't apply, but scanReachableSymbols only accepts base
574 // regions right now.
575 const MemRegion *R = val.getRegion()->getBaseRegion();
576 return StoreMgr.scanReachableSymbols(val.getStore(), R, *this);
577}
578
580 for (SVal V : val)
581 if (!scan(V))
582 return false;
583
584 return true;
585}
586
588 for (SymbolRef SubSym : sym->symbols()) {
589 bool wasVisited = !visited.insert(SubSym).second;
590 if (wasVisited)
591 continue;
592
593 if (!visitor.VisitSymbol(SubSym))
594 return false;
595 }
596
597 return true;
598}
599
601 if (std::optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>())
602 return scan(X->getRegion());
603
604 if (std::optional<nonloc::LazyCompoundVal> X =
606 return scan(*X);
607
608 if (std::optional<nonloc::LocAsInteger> X = val.getAs<nonloc::LocAsInteger>())
609 return scan(X->getLoc());
610
611 if (SymbolRef Sym = val.getAsSymbol())
612 return scan(Sym);
613
614 if (std::optional<nonloc::CompoundVal> X = val.getAs<nonloc::CompoundVal>())
615 return scan(*X);
616
617 return true;
618}
619
621 if (isa<MemSpaceRegion>(R))
622 return true;
623
624 bool wasVisited = !visited.insert(R).second;
625 if (wasVisited)
626 return true;
627
628 if (!visitor.VisitMemRegion(R))
629 return false;
630
631 // If this is a symbolic region, visit the symbol for the region.
632 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
633 if (!visitor.VisitSymbol(SR->getSymbol()))
634 return false;
635
636 // If this is a subregion, also visit the parent regions.
637 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
638 const MemRegion *Super = SR->getSuperRegion();
639 if (!scan(Super))
640 return false;
641
642 // When we reach the topmost region, scan all symbols in it.
643 if (isa<MemSpaceRegion>(Super)) {
644 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
645 if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
646 return false;
647 }
648 }
649
650 // Regions captured by a block are also implicitly reachable.
651 if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
652 for (auto Var : BDR->referenced_vars()) {
653 if (!scan(Var.getCapturedRegion()))
654 return false;
655 }
656 }
657
658 return true;
659}
660
662 ScanReachableSymbols S(this, visitor);
663 return S.scan(val);
664}
665
667 llvm::iterator_range<region_iterator> Reachable,
668 SymbolVisitor &visitor) const {
669 ScanReachableSymbols S(this, visitor);
670 for (const MemRegion *R : Reachable) {
671 if (!S.scan(R))
672 return false;
673 }
674 return true;
675}
#define V(N, I)
#define X(type, name)
Definition Value.h:97
ArrayRef< const MemRegion * > RegionList
ArrayRef< SVal > ValueList
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
CanQualType IntTy
This represents one expression.
Definition Expr.h:114
Represents a member of a struct/union/class.
Definition Decl.h:3341
Represents a field injected from an anonymous union/struct into the parent scope.
Definition Decl.h:3648
ArrayRef< NamedDecl * > chain() const
Definition Decl.h:3669
A (possibly-)qualified type.
Definition TypeBase.h:938
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1005
It represents a stack frame of the call stack.
const ImplicitParamDecl * getSelfDecl() const
A safe wrapper around APSInt objects allocated and owned by BasicValueFactory.
Definition APSIntPtr.h:19
APSIntPtr getMinValue(const llvm::APSInt &v)
APSIntPtr Convert(const llvm::APSInt &To, const llvm::APSInt &From)
Convert - Create a new persistent APSInt with the same value as 'From' but with the bitwidth and sign...
BlockDataRegion - A region that represents a block instance.
Definition MemRegion.h:713
Manages the lifetime of CallEvent objects.
Definition CallEvent.h:1354
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:152
ProgramStatePair assumeDual(ProgramStateRef State, DefinedSVal Cond)
Returns a pair of states (StTrue, StFalse) where the given condition is assumed to be true or false,...
virtual void printJson(raw_ostream &Out, ProgramStateRef State, const char *NL, unsigned int Space, bool IsDot) const =0
An entry in the environment consists of a Stmt and an StackFrame.
Definition Environment.h:35
Environment bindExpr(Environment Env, const EnvironmentEntry &E, SVal V, bool Invalidate)
Bind a symbolic value to the given environment entry.
An immutable map from EnvironmentEntries to SVals.
Definition Environment.h:55
ProgramStateRef processRegionChanges(ProgramStateRef state, const InvalidatedSymbols *invalidated, ArrayRef< const MemRegion * > ExplicitRegions, ArrayRef< const MemRegion * > Regions, const StackFrame *SF, const CallEvent *Call)
processRegionChanges - Called by ProgramStateManager whenever a change is made to the store.
void printJson(raw_ostream &Out, ProgramStateRef State, const StackFrame *SF, const char *NL, unsigned int Space, bool IsDot) const
printJson - Called by ProgramStateManager to print checker-specific data.
ProgramStateRef processRegionChange(ProgramStateRef state, const MemRegion *MR, const StackFrame *SF)
Definition ExprEngine.h:441
ProgramStateRef notifyCheckersOfPointerEscape(ProgramStateRef State, const InvalidatedSymbols *Invalidated, ArrayRef< const MemRegion * > ExplicitRegions, const CallEvent *Call, RegionAndSymbolInvalidationTraits &ITraits)
Call PointerEscape callback when a value escapes as a result of region invalidation.
ProgramStateRef escapeValues(ProgramStateRef State, ArrayRef< SVal > Vs, PointerEscapeKind K, const CallEvent *Call=nullptr) const
A simple wrapper when you only need to notify checkers of pointer-escape of some values.
static bool isLocType(QualType T)
Definition SVals.h:265
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:97
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getBaseRegion() const
ProgramStateRef getInitialState(const StackFrame *InitSF)
void * FindGDMContext(const void *index, void *(*CreateContext)(llvm::BumpPtrAllocator &), void(*DeleteContext)(void *))
ProgramStateRef removeDeadBindingsFromEnvironmentAndStore(ProgramStateRef St, const StackFrame *SF, SymbolReaper &SymReaper)
ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState, ProgramStateRef GDMState)
ProgramStateRef removeGDM(ProgramStateRef state, const void *Key)
ProgramStateRef addGDM(ProgramStateRef St, const void *Key, void *Data)
ProgramStateRef getPersistentState(ProgramState &&Impl)
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.
Loc getLValue(const CXXBaseSpecifier &BaseSpec, const SubRegion *Super) const
Get the lvalue for a base class object reference.
friend class ProgramStateManager
llvm::ImmutableMap< const void *, void * > GenericDataMap
ProgramStateRef bindDefaultInitial(SVal loc, SVal V, const StackFrame *SF) const
Initializes the region of memory represented by loc with an initial value.
ConstraintManager & getConstraintManager() const
Return the ConstraintManager.
LLVM_ATTRIBUTE_RETURNS_NONNULL const VarRegion * getRegion(const VarDecl *D, const StackFrame *SF) const
Utility method for getting regions.
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.
ProgramStateRef bindLoc(Loc location, SVal V, const StackFrame *SF, bool notifyChanges=true) const
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.
ProgramStateRef BindExpr(const Expr *E, const StackFrame *SF, 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 bindDefaultZero(SVal loc, const StackFrame *SF) const
Performs C++ zero-initialization procedure on the region of memory represented by loc.
SVal getSVal(const Expr *E, const StackFrame *SF) const
Returns the SVal bound to the expression E in the state's environment.
Store getStore() const
Return the store associated with this state.
ConditionTruthVal areEqual(SVal Lhs, SVal Rhs) const
ConditionTruthVal isNonNull(SVal V) const
Check if the given SVal is not constrained to zero and is not a zero constant.
ProgramStateRef assumeInBound(DefinedOrUnknownSVal idx, DefinedOrUnknownSVal upperBound, bool assumption, QualType IndexType=QualType()) const
SVal getSValAsScalarOrLoc(const Expr *E, const StackFrame *SF) const
ProgramStateRef invalidateRegions(ArrayRef< const MemRegion * > Regions, ConstCFGElementRef Elem, unsigned BlockCount, const StackFrame *SF, 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.
void printDOT(raw_ostream &Out, const StackFrame *SF=nullptr, unsigned int Space=0) const
SVal getSelfSVal(const StackFrame *SF) const
Return the value of 'self' if available in the given context.
void printJson(raw_ostream &Out, const StackFrame *SF=nullptr, const char *NL="\n", unsigned int Space=0, bool IsDot=false) const
BasicValueFactory & getBasicVals() const
std::pair< ProgramStateRef, ProgramStateRef > assumeInBoundDual(DefinedOrUnknownSVal idx, DefinedOrUnknownSVal upperBound, QualType IndexType=QualType()) const
void *const * FindGDM(const void *K) const
AnalysisManager & getAnalysisManager() 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.
Information about invalidation for a particular region/symbol.
Definition MemRegion.h:1664
BasicValueFactory & getBasicValueFactory()
ASTContext & getContext()
QualType getArrayIndexType() const
virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op, NonLoc lhs, NonLoc rhs, QualType resultTy)=0
Create a new value which represents a binary expression with two non- location operands.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:57
bool isUnknownOrUndef() const
Definition SVals.h:115
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
Definition SVals.cpp:103
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:88
const MemRegion * getAsRegion() const
Definition SVals.cpp:119
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition SVals.h:84
bool isUnknown() const
Definition SVals.h:111
A utility class that visits the reachable symbols using a custom SymbolVisitor.
bool scan(nonloc::LazyCompoundVal val)
virtual bool scanReachableSymbols(Store S, const MemRegion *R, ScanReachableSymbols &Visitor)=0
Finds the transitive closure of symbols within the given region.
SmallVector< const MemRegion *, 8 > InvalidatedRegions
Definition Store.h:210
const ElementRegion * GetElementZeroRegion(const SubRegion *R, QualType T)
Definition Store.cpp:69
SVal getLValueField(const FieldDecl *D, SVal Base)
Definition Store.h:153
virtual void printJson(raw_ostream &Out, Store S, const char *NL, unsigned int Space, bool IsDot) const =0
Store getStore() const
Definition StoreRef.h:50
SubRegion - A region that subsets another larger region.
Definition MemRegion.h:481
Symbolic value.
Definition SymExpr.h:33
symbol_range symbols() const
Definition SymExpr.h:142
A class responsible for cleaning up unused symbols.
void setReapedStore(StoreRef st)
Set to the value of the symbolic store after StoreManager::removeDeadBindings has been called.
SymbolicRegion - A special, "non-concrete" region.
Definition MemRegion.h:814
TypedValueRegion - An abstract class representing regions having a typed value.
Definition MemRegion.h:570
The simplest example of a concrete compound value is nonloc::CompoundVal, which represents a concrete...
Definition SVals.h:342
Value representing integer constant.
Definition SVals.h:303
While nonloc::CompoundVal covers a few simple use cases, nonloc::LazyCompoundVal is a more performant...
Definition SVals.h:392
LLVM_ATTRIBUTE_RETURNS_NONNULL const LazyCompoundValData * getCVData() const
Definition SVals.h:402
LLVM_ATTRIBUTE_RETURNS_NONNULL const TypedValueRegion * getRegion() const
This function itself is immaterial.
Definition SVals.cpp:193
const void * getStore() const
It might return null.
Definition SVals.cpp:189
@ PSK_EscapeOnBind
A pointer escapes due to binding its value to a location that the analyzer cannot track.
SValBuilder * createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, ProgramStateManager &stateMgr)
llvm::DenseSet< SymbolRef > InvalidatedSymbols
Definition Store.h:50
void printDynamicTypeInfoJson(raw_ostream &Out, ProgramStateRef State, const char *NL="\n", unsigned int Space=0, bool IsDot=false)
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Definition SymExpr.h:166
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
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
CFGBlock::ConstCFGElementRef ConstCFGElementRef
Definition CFG.h:1271
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
Definition JsonSupport.h:21
const FunctionProtoType * T
U cast(CodeGen::Address addr)
Definition Address.h:327
llvm::SmallVector< SVal, 0 > FailedToBindValues
Definition Store.h:57
StoreRef ResultingStore
Definition Store.h:53