1 //= ProgramState.cpp - Path-Sensitive "State" for tracking values --*- C++ -*--=
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
7 //===----------------------------------------------------------------------===//
9 // This file implements ProgramState and ProgramStateManager.
11 //===----------------------------------------------------------------------===//
13 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
14 #include "clang/Analysis/CFG.h"
15 #include "clang/Basic/JsonSupport.h"
16 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
21 #include "llvm/Support/raw_ostream.h"
23 using namespace clang;
26 namespace clang { namespace ento {
27 /// Increments the number of times this state is referenced.
29 void ProgramStateRetain(const ProgramState *state) {
30 ++const_cast<ProgramState*>(state)->refCount;
33 /// Decrement the number of times this state is referenced.
34 void ProgramStateRelease(const ProgramState *state) {
35 assert(state->refCount > 0);
36 ProgramState *s = const_cast<ProgramState*>(state);
37 if (--s->refCount == 0) {
38 ProgramStateManager &Mgr = s->getStateManager();
39 Mgr.StateSet.RemoveNode(s);
41 Mgr.freeStates.push_back(s);
46 ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env,
47 StoreRef st, GenericDataMap gdm)
53 stateMgr->getStoreManager().incrementReferenceCount(store);
56 ProgramState::ProgramState(const ProgramState &RHS)
57 : stateMgr(RHS.stateMgr), Env(RHS.Env), store(RHS.store), GDM(RHS.GDM),
59 stateMgr->getStoreManager().incrementReferenceCount(store);
62 ProgramState::~ProgramState() {
64 stateMgr->getStoreManager().decrementReferenceCount(store);
67 int64_t ProgramState::getID() const {
68 return getStateManager().Alloc.identifyKnownAlignedObject<ProgramState>(this);
71 ProgramStateManager::ProgramStateManager(ASTContext &Ctx,
72 StoreManagerCreator CreateSMgr,
73 ConstraintManagerCreator CreateCMgr,
74 llvm::BumpPtrAllocator &alloc,
76 : Eng(ExprEng), EnvMgr(alloc), GDMFactory(alloc),
77 svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
78 CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) {
79 StoreMgr = (*CreateSMgr)(*this);
80 ConstraintMgr = (*CreateCMgr)(*this, ExprEng);
84 ProgramStateManager::~ProgramStateManager() {
85 for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
87 I->second.second(I->second.first);
90 ProgramStateRef ProgramStateManager::removeDeadBindingsFromEnvironmentAndStore(
91 ProgramStateRef state, const StackFrameContext *LCtx,
92 SymbolReaper &SymReaper) {
94 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
95 // The roots are any Block-level exprs and Decls that our liveness algorithm
96 // tells us are live. We then see what Decls they may reference, and keep
97 // those around. This code more than likely can be made faster, and the
98 // frequency of which this method is called should be experimented with
99 // for optimum performance.
100 ProgramState NewState = *state;
102 NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state);
104 // Clean up the store.
105 StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx,
107 NewState.setStore(newStore);
108 SymReaper.setReapedStore(newStore);
110 return getPersistentState(NewState);
113 ProgramStateRef ProgramState::bindLoc(Loc LV,
115 const LocationContext *LCtx,
116 bool notifyChanges) const {
117 ProgramStateManager &Mgr = getStateManager();
118 ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
120 const MemRegion *MR = LV.getAsRegion();
121 if (MR && notifyChanges)
122 return Mgr.getOwningEngine().processRegionChange(newState, MR, LCtx);
128 ProgramState::bindDefaultInitial(SVal loc, SVal V,
129 const LocationContext *LCtx) const {
130 ProgramStateManager &Mgr = getStateManager();
131 const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
132 const StoreRef &newStore = Mgr.StoreMgr->BindDefaultInitial(getStore(), R, V);
133 ProgramStateRef new_state = makeWithStore(newStore);
134 return Mgr.getOwningEngine().processRegionChange(new_state, R, LCtx);
138 ProgramState::bindDefaultZero(SVal loc, const LocationContext *LCtx) const {
139 ProgramStateManager &Mgr = getStateManager();
140 const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
141 const StoreRef &newStore = Mgr.StoreMgr->BindDefaultZero(getStore(), R);
142 ProgramStateRef new_state = makeWithStore(newStore);
143 return Mgr.getOwningEngine().processRegionChange(new_state, R, LCtx);
146 typedef ArrayRef<const MemRegion *> RegionList;
147 typedef ArrayRef<SVal> ValueList;
150 ProgramState::invalidateRegions(RegionList Regions,
151 const Expr *E, unsigned Count,
152 const LocationContext *LCtx,
153 bool CausedByPointerEscape,
154 InvalidatedSymbols *IS,
155 const CallEvent *Call,
156 RegionAndSymbolInvalidationTraits *ITraits) const {
157 SmallVector<SVal, 8> Values;
158 for (RegionList::const_iterator I = Regions.begin(),
159 End = Regions.end(); I != End; ++I)
160 Values.push_back(loc::MemRegionVal(*I));
162 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
167 ProgramState::invalidateRegions(ValueList Values,
168 const Expr *E, unsigned Count,
169 const LocationContext *LCtx,
170 bool CausedByPointerEscape,
171 InvalidatedSymbols *IS,
172 const CallEvent *Call,
173 RegionAndSymbolInvalidationTraits *ITraits) const {
175 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
180 ProgramState::invalidateRegionsImpl(ValueList Values,
181 const Expr *E, unsigned Count,
182 const LocationContext *LCtx,
183 bool CausedByPointerEscape,
184 InvalidatedSymbols *IS,
185 RegionAndSymbolInvalidationTraits *ITraits,
186 const CallEvent *Call) const {
187 ProgramStateManager &Mgr = getStateManager();
188 ExprEngine &Eng = Mgr.getOwningEngine();
190 InvalidatedSymbols InvalidatedSyms;
192 IS = &InvalidatedSyms;
194 RegionAndSymbolInvalidationTraits ITraitsLocal;
196 ITraits = &ITraitsLocal;
198 StoreManager::InvalidatedRegions TopLevelInvalidated;
199 StoreManager::InvalidatedRegions Invalidated;
200 const StoreRef &newStore
201 = Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
202 *IS, *ITraits, &TopLevelInvalidated,
205 ProgramStateRef newState = makeWithStore(newStore);
207 if (CausedByPointerEscape) {
208 newState = Eng.notifyCheckersOfPointerEscape(newState, IS,
214 return Eng.processRegionChanges(newState, IS, TopLevelInvalidated,
215 Invalidated, LCtx, Call);
218 ProgramStateRef ProgramState::killBinding(Loc LV) const {
219 assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead.");
221 Store OldStore = getStore();
222 const StoreRef &newStore =
223 getStateManager().StoreMgr->killBinding(OldStore, LV);
225 if (newStore.getStore() == OldStore)
228 return makeWithStore(newStore);
232 ProgramState::enterStackFrame(const CallEvent &Call,
233 const StackFrameContext *CalleeCtx) const {
234 const StoreRef &NewStore =
235 getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
236 return makeWithStore(NewStore);
239 SVal ProgramState::getSelfSVal(const LocationContext *LCtx) const {
240 const ImplicitParamDecl *SelfDecl = LCtx->getSelfDecl();
243 return getSVal(getRegion(SelfDecl, LCtx));
246 SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
247 // We only want to do fetches from regions that we can actually bind
248 // values. For example, SymbolicRegions of type 'id<...>' cannot
249 // have direct bindings (but their can be bindings on their subregions).
250 if (!R->isBoundable())
253 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
254 QualType T = TR->getValueType();
255 if (Loc::isLocType(T) || T->isIntegralOrEnumerationType())
262 SVal ProgramState::getSVal(Loc location, QualType T) const {
263 SVal V = getRawSVal(location, T);
265 // If 'V' is a symbolic value that is *perfectly* constrained to
266 // be a constant value, use that value instead to lessen the burden
267 // on later analysis stages (so we have less symbolic values to reason
269 // We only go into this branch if we can convert the APSInt value we have
270 // to the type of T, which is not always the case (e.g. for void).
271 if (!T.isNull() && (T->isIntegralOrEnumerationType() || Loc::isLocType(T))) {
272 if (SymbolRef sym = V.getAsSymbol()) {
273 if (const llvm::APSInt *Int = getStateManager()
274 .getConstraintManager()
275 .getSymVal(this, sym)) {
276 // FIXME: Because we don't correctly model (yet) sign-extension
277 // and truncation of symbolic values, we need to convert
278 // the integer value to the correct signedness and bitwidth.
280 // This shows up in the following:
283 // unsigned x = foo();
287 // The symbolic value stored to 'x' is actually the conjured
288 // symbol for the call to foo(); the type of that symbol is 'char',
290 const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
293 return loc::ConcreteInt(NewV);
295 return nonloc::ConcreteInt(NewV);
303 ProgramStateRef ProgramState::BindExpr(const Stmt *S,
304 const LocationContext *LCtx,
305 SVal V, bool Invalidate) const{
307 getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
312 ProgramState NewSt = *this;
314 return getStateManager().getPersistentState(NewSt);
317 ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
318 DefinedOrUnknownSVal UpperBound,
320 QualType indexTy) const {
321 if (Idx.isUnknown() || UpperBound.isUnknown())
324 // Build an expression for 0 <= Idx < UpperBound.
325 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
326 // FIXME: This should probably be part of SValBuilder.
327 ProgramStateManager &SM = getStateManager();
328 SValBuilder &svalBuilder = SM.getSValBuilder();
329 ASTContext &Ctx = svalBuilder.getContext();
331 // Get the offset: the minimum value of the array index type.
332 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
333 if (indexTy.isNull())
334 indexTy = svalBuilder.getArrayIndexType();
335 nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
338 SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
339 Idx.castAs<NonLoc>(), Min, indexTy);
340 if (newIdx.isUnknownOrUndef())
343 // Adjust the upper bound.
345 svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
348 if (newBound.isUnknownOrUndef())
351 // Build the actual comparison.
352 SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(),
353 newBound.castAs<NonLoc>(), Ctx.IntTy);
354 if (inBound.isUnknownOrUndef())
357 // Finally, let the constraint manager take care of it.
358 ConstraintManager &CM = SM.getConstraintManager();
359 return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption);
362 ConditionTruthVal ProgramState::isNonNull(SVal V) const {
363 ConditionTruthVal IsNull = isNull(V);
364 if (IsNull.isUnderconstrained())
366 return ConditionTruthVal(!IsNull.getValue());
369 ConditionTruthVal ProgramState::areEqual(SVal Lhs, SVal Rhs) const {
370 return stateMgr->getSValBuilder().areEqual(this, Lhs, Rhs);
373 ConditionTruthVal ProgramState::isNull(SVal V) const {
374 if (V.isZeroConstant())
380 SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
382 return ConditionTruthVal();
384 return getStateManager().ConstraintMgr->isNull(this, Sym);
387 ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
388 ProgramState State(this,
389 EnvMgr.getInitialEnvironment(),
390 StoreMgr->getInitialStore(InitLoc),
391 GDMFactory.getEmptyMap());
393 return getPersistentState(State);
396 ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
397 ProgramStateRef FromState,
398 ProgramStateRef GDMState) {
399 ProgramState NewState(*FromState);
400 NewState.GDM = GDMState->GDM;
401 return getPersistentState(NewState);
404 ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
406 llvm::FoldingSetNodeID ID;
410 if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
413 ProgramState *newState = nullptr;
414 if (!freeStates.empty()) {
415 newState = freeStates.back();
416 freeStates.pop_back();
419 newState = (ProgramState*) Alloc.Allocate<ProgramState>();
421 new (newState) ProgramState(State);
422 StateSet.InsertNode(newState, InsertPos);
426 ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
427 ProgramState NewSt(*this);
428 NewSt.setStore(store);
429 return getStateManager().getPersistentState(NewSt);
432 void ProgramState::setStore(const StoreRef &newStore) {
433 Store newStoreStore = newStore.getStore();
435 stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
437 stateMgr->getStoreManager().decrementReferenceCount(store);
438 store = newStoreStore;
441 //===----------------------------------------------------------------------===//
442 // State pretty-printing.
443 //===----------------------------------------------------------------------===//
445 void ProgramState::printJson(raw_ostream &Out, const LocationContext *LCtx,
446 const char *NL, unsigned int Space,
448 Indent(Out, Space, IsDot) << "\"program_state\": {" << NL;
451 ProgramStateManager &Mgr = getStateManager();
454 Mgr.getStoreManager().printJson(Out, getStore(), NL, Space, IsDot);
456 // Print out the environment.
457 Env.printJson(Out, Mgr.getContext(), LCtx, NL, Space, IsDot);
459 // Print out the constraints.
460 Mgr.getConstraintManager().printJson(Out, this, NL, Space, IsDot);
462 // Print out the tracked dynamic types.
463 printDynamicTypeInfoJson(Out, this, NL, Space, IsDot);
465 // Print checker-specific data.
466 Mgr.getOwningEngine().printJson(Out, this, LCtx, NL, Space, IsDot);
469 Indent(Out, Space, IsDot) << '}';
472 void ProgramState::printDOT(raw_ostream &Out, const LocationContext *LCtx,
473 unsigned int Space) const {
474 printJson(Out, LCtx, /*NL=*/"\\l", Space, /*IsDot=*/true);
477 LLVM_DUMP_METHOD void ProgramState::dump() const {
478 printJson(llvm::errs());
481 AnalysisManager& ProgramState::getAnalysisManager() const {
482 return stateMgr->getOwningEngine().getAnalysisManager();
485 //===----------------------------------------------------------------------===//
487 //===----------------------------------------------------------------------===//
489 void *const* ProgramState::FindGDM(void *K) const {
490 return GDM.lookup(K);
494 ProgramStateManager::FindGDMContext(void *K,
495 void *(*CreateContext)(llvm::BumpPtrAllocator&),
496 void (*DeleteContext)(void*)) {
498 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
500 p.first = CreateContext(Alloc);
501 p.second = DeleteContext;
507 ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){
508 ProgramState::GenericDataMap M1 = St->getGDM();
509 ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
514 ProgramState NewSt = *St;
516 return getPersistentState(NewSt);
519 ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) {
520 ProgramState::GenericDataMap OldM = state->getGDM();
521 ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
526 ProgramState NewState = *state;
528 return getPersistentState(NewState);
531 bool ScanReachableSymbols::scan(nonloc::LazyCompoundVal val) {
532 bool wasVisited = !visited.insert(val.getCVData()).second;
536 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
537 // FIXME: We don't really want to use getBaseRegion() here because pointer
538 // arithmetic doesn't apply, but scanReachableSymbols only accepts base
539 // regions right now.
540 const MemRegion *R = val.getRegion()->getBaseRegion();
541 return StoreMgr.scanReachableSymbols(val.getStore(), R, *this);
544 bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
545 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
552 bool ScanReachableSymbols::scan(const SymExpr *sym) {
553 for (SymExpr::symbol_iterator SI = sym->symbol_begin(),
554 SE = sym->symbol_end();
556 bool wasVisited = !visited.insert(*SI).second;
560 if (!visitor.VisitSymbol(*SI))
567 bool ScanReachableSymbols::scan(SVal val) {
568 if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>())
569 return scan(X->getRegion());
571 if (Optional<nonloc::LazyCompoundVal> X =
572 val.getAs<nonloc::LazyCompoundVal>())
575 if (Optional<nonloc::LocAsInteger> X = val.getAs<nonloc::LocAsInteger>())
576 return scan(X->getLoc());
578 if (SymbolRef Sym = val.getAsSymbol())
581 if (Optional<nonloc::CompoundVal> X = val.getAs<nonloc::CompoundVal>())
587 bool ScanReachableSymbols::scan(const MemRegion *R) {
588 if (isa<MemSpaceRegion>(R))
591 bool wasVisited = !visited.insert(R).second;
595 if (!visitor.VisitMemRegion(R))
598 // If this is a symbolic region, visit the symbol for the region.
599 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
600 if (!visitor.VisitSymbol(SR->getSymbol()))
603 // If this is a subregion, also visit the parent regions.
604 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
605 const MemRegion *Super = SR->getSuperRegion();
609 // When we reach the topmost region, scan all symbols in it.
610 if (isa<MemSpaceRegion>(Super)) {
611 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
612 if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
617 // Regions captured by a block are also implicitly reachable.
618 if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
619 BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
620 E = BDR->referenced_vars_end();
621 for ( ; I != E; ++I) {
622 if (!scan(I.getCapturedRegion()))
630 bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
631 ScanReachableSymbols S(this, visitor);
635 bool ProgramState::scanReachableSymbols(
636 llvm::iterator_range<region_iterator> Reachable,
637 SymbolVisitor &visitor) const {
638 ScanReachableSymbols S(this, visitor);
639 for (const MemRegion *R : Reachable) {