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 : llvm::FoldingSetNode(),
58 stateMgr(RHS.stateMgr),
63 stateMgr->getStoreManager().incrementReferenceCount(store);
66 ProgramState::~ProgramState() {
68 stateMgr->getStoreManager().decrementReferenceCount(store);
71 int64_t ProgramState::getID() const {
72 return getStateManager().Alloc.identifyKnownAlignedObject<ProgramState>(this);
75 ProgramStateManager::ProgramStateManager(ASTContext &Ctx,
76 StoreManagerCreator CreateSMgr,
77 ConstraintManagerCreator CreateCMgr,
78 llvm::BumpPtrAllocator &alloc,
80 : Eng(ExprEng), EnvMgr(alloc), GDMFactory(alloc),
81 svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
82 CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) {
83 StoreMgr = (*CreateSMgr)(*this);
84 ConstraintMgr = (*CreateCMgr)(*this, ExprEng);
88 ProgramStateManager::~ProgramStateManager() {
89 for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
91 I->second.second(I->second.first);
94 ProgramStateRef ProgramStateManager::removeDeadBindingsFromEnvironmentAndStore(
95 ProgramStateRef state, const StackFrameContext *LCtx,
96 SymbolReaper &SymReaper) {
98 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
99 // The roots are any Block-level exprs and Decls that our liveness algorithm
100 // tells us are live. We then see what Decls they may reference, and keep
101 // those around. This code more than likely can be made faster, and the
102 // frequency of which this method is called should be experimented with
103 // for optimum performance.
104 ProgramState NewState = *state;
106 NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state);
108 // Clean up the store.
109 StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx,
111 NewState.setStore(newStore);
112 SymReaper.setReapedStore(newStore);
114 return getPersistentState(NewState);
117 ProgramStateRef ProgramState::bindLoc(Loc LV,
119 const LocationContext *LCtx,
120 bool notifyChanges) const {
121 ProgramStateManager &Mgr = getStateManager();
122 ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
124 const MemRegion *MR = LV.getAsRegion();
125 if (MR && notifyChanges)
126 return Mgr.getOwningEngine().processRegionChange(newState, MR, LCtx);
132 ProgramState::bindDefaultInitial(SVal loc, SVal V,
133 const LocationContext *LCtx) const {
134 ProgramStateManager &Mgr = getStateManager();
135 const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
136 const StoreRef &newStore = Mgr.StoreMgr->BindDefaultInitial(getStore(), R, V);
137 ProgramStateRef new_state = makeWithStore(newStore);
138 return Mgr.getOwningEngine().processRegionChange(new_state, R, LCtx);
142 ProgramState::bindDefaultZero(SVal loc, const LocationContext *LCtx) const {
143 ProgramStateManager &Mgr = getStateManager();
144 const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
145 const StoreRef &newStore = Mgr.StoreMgr->BindDefaultZero(getStore(), R);
146 ProgramStateRef new_state = makeWithStore(newStore);
147 return Mgr.getOwningEngine().processRegionChange(new_state, R, LCtx);
150 typedef ArrayRef<const MemRegion *> RegionList;
151 typedef ArrayRef<SVal> ValueList;
154 ProgramState::invalidateRegions(RegionList Regions,
155 const Expr *E, unsigned Count,
156 const LocationContext *LCtx,
157 bool CausedByPointerEscape,
158 InvalidatedSymbols *IS,
159 const CallEvent *Call,
160 RegionAndSymbolInvalidationTraits *ITraits) const {
161 SmallVector<SVal, 8> Values;
162 for (RegionList::const_iterator I = Regions.begin(),
163 End = Regions.end(); I != End; ++I)
164 Values.push_back(loc::MemRegionVal(*I));
166 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
171 ProgramState::invalidateRegions(ValueList Values,
172 const Expr *E, unsigned Count,
173 const LocationContext *LCtx,
174 bool CausedByPointerEscape,
175 InvalidatedSymbols *IS,
176 const CallEvent *Call,
177 RegionAndSymbolInvalidationTraits *ITraits) const {
179 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
184 ProgramState::invalidateRegionsImpl(ValueList Values,
185 const Expr *E, unsigned Count,
186 const LocationContext *LCtx,
187 bool CausedByPointerEscape,
188 InvalidatedSymbols *IS,
189 RegionAndSymbolInvalidationTraits *ITraits,
190 const CallEvent *Call) const {
191 ProgramStateManager &Mgr = getStateManager();
192 ExprEngine &Eng = Mgr.getOwningEngine();
194 InvalidatedSymbols InvalidatedSyms;
196 IS = &InvalidatedSyms;
198 RegionAndSymbolInvalidationTraits ITraitsLocal;
200 ITraits = &ITraitsLocal;
202 StoreManager::InvalidatedRegions TopLevelInvalidated;
203 StoreManager::InvalidatedRegions Invalidated;
204 const StoreRef &newStore
205 = Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
206 *IS, *ITraits, &TopLevelInvalidated,
209 ProgramStateRef newState = makeWithStore(newStore);
211 if (CausedByPointerEscape) {
212 newState = Eng.notifyCheckersOfPointerEscape(newState, IS,
218 return Eng.processRegionChanges(newState, IS, TopLevelInvalidated,
219 Invalidated, LCtx, Call);
222 ProgramStateRef ProgramState::killBinding(Loc LV) const {
223 assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead.");
225 Store OldStore = getStore();
226 const StoreRef &newStore =
227 getStateManager().StoreMgr->killBinding(OldStore, LV);
229 if (newStore.getStore() == OldStore)
232 return makeWithStore(newStore);
236 ProgramState::enterStackFrame(const CallEvent &Call,
237 const StackFrameContext *CalleeCtx) const {
238 const StoreRef &NewStore =
239 getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
240 return makeWithStore(NewStore);
243 SVal ProgramState::getSelfSVal(const LocationContext *LCtx) const {
244 const ImplicitParamDecl *SelfDecl = LCtx->getSelfDecl();
247 return getSVal(getRegion(SelfDecl, LCtx));
250 SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
251 // We only want to do fetches from regions that we can actually bind
252 // values. For example, SymbolicRegions of type 'id<...>' cannot
253 // have direct bindings (but their can be bindings on their subregions).
254 if (!R->isBoundable())
257 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
258 QualType T = TR->getValueType();
259 if (Loc::isLocType(T) || T->isIntegralOrEnumerationType())
266 SVal ProgramState::getSVal(Loc location, QualType T) const {
267 SVal V = getRawSVal(location, T);
269 // If 'V' is a symbolic value that is *perfectly* constrained to
270 // be a constant value, use that value instead to lessen the burden
271 // on later analysis stages (so we have less symbolic values to reason
273 // We only go into this branch if we can convert the APSInt value we have
274 // to the type of T, which is not always the case (e.g. for void).
275 if (!T.isNull() && (T->isIntegralOrEnumerationType() || Loc::isLocType(T))) {
276 if (SymbolRef sym = V.getAsSymbol()) {
277 if (const llvm::APSInt *Int = getStateManager()
278 .getConstraintManager()
279 .getSymVal(this, sym)) {
280 // FIXME: Because we don't correctly model (yet) sign-extension
281 // and truncation of symbolic values, we need to convert
282 // the integer value to the correct signedness and bitwidth.
284 // This shows up in the following:
287 // unsigned x = foo();
291 // The symbolic value stored to 'x' is actually the conjured
292 // symbol for the call to foo(); the type of that symbol is 'char',
294 const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
297 return loc::ConcreteInt(NewV);
299 return nonloc::ConcreteInt(NewV);
307 ProgramStateRef ProgramState::BindExpr(const Stmt *S,
308 const LocationContext *LCtx,
309 SVal V, bool Invalidate) const{
311 getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
316 ProgramState NewSt = *this;
318 return getStateManager().getPersistentState(NewSt);
321 ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
322 DefinedOrUnknownSVal UpperBound,
324 QualType indexTy) const {
325 if (Idx.isUnknown() || UpperBound.isUnknown())
328 // Build an expression for 0 <= Idx < UpperBound.
329 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
330 // FIXME: This should probably be part of SValBuilder.
331 ProgramStateManager &SM = getStateManager();
332 SValBuilder &svalBuilder = SM.getSValBuilder();
333 ASTContext &Ctx = svalBuilder.getContext();
335 // Get the offset: the minimum value of the array index type.
336 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
337 if (indexTy.isNull())
338 indexTy = svalBuilder.getArrayIndexType();
339 nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
342 SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
343 Idx.castAs<NonLoc>(), Min, indexTy);
344 if (newIdx.isUnknownOrUndef())
347 // Adjust the upper bound.
349 svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
352 if (newBound.isUnknownOrUndef())
355 // Build the actual comparison.
356 SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(),
357 newBound.castAs<NonLoc>(), Ctx.IntTy);
358 if (inBound.isUnknownOrUndef())
361 // Finally, let the constraint manager take care of it.
362 ConstraintManager &CM = SM.getConstraintManager();
363 return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption);
366 ConditionTruthVal ProgramState::isNonNull(SVal V) const {
367 ConditionTruthVal IsNull = isNull(V);
368 if (IsNull.isUnderconstrained())
370 return ConditionTruthVal(!IsNull.getValue());
373 ConditionTruthVal ProgramState::areEqual(SVal Lhs, SVal Rhs) const {
374 return stateMgr->getSValBuilder().areEqual(this, Lhs, Rhs);
377 ConditionTruthVal ProgramState::isNull(SVal V) const {
378 if (V.isZeroConstant())
384 SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
386 return ConditionTruthVal();
388 return getStateManager().ConstraintMgr->isNull(this, Sym);
391 ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
392 ProgramState State(this,
393 EnvMgr.getInitialEnvironment(),
394 StoreMgr->getInitialStore(InitLoc),
395 GDMFactory.getEmptyMap());
397 return getPersistentState(State);
400 ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
401 ProgramStateRef FromState,
402 ProgramStateRef GDMState) {
403 ProgramState NewState(*FromState);
404 NewState.GDM = GDMState->GDM;
405 return getPersistentState(NewState);
408 ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
410 llvm::FoldingSetNodeID ID;
414 if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
417 ProgramState *newState = nullptr;
418 if (!freeStates.empty()) {
419 newState = freeStates.back();
420 freeStates.pop_back();
423 newState = (ProgramState*) Alloc.Allocate<ProgramState>();
425 new (newState) ProgramState(State);
426 StateSet.InsertNode(newState, InsertPos);
430 ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
431 ProgramState NewSt(*this);
432 NewSt.setStore(store);
433 return getStateManager().getPersistentState(NewSt);
436 void ProgramState::setStore(const StoreRef &newStore) {
437 Store newStoreStore = newStore.getStore();
439 stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
441 stateMgr->getStoreManager().decrementReferenceCount(store);
442 store = newStoreStore;
445 //===----------------------------------------------------------------------===//
446 // State pretty-printing.
447 //===----------------------------------------------------------------------===//
449 void ProgramState::printJson(raw_ostream &Out, const LocationContext *LCtx,
450 const char *NL, unsigned int Space,
452 Indent(Out, Space, IsDot) << "\"program_state\": {" << NL;
455 ProgramStateManager &Mgr = getStateManager();
458 Mgr.getStoreManager().printJson(Out, getStore(), NL, Space, IsDot);
460 // Print out the environment.
461 Env.printJson(Out, Mgr.getContext(), LCtx, NL, Space, IsDot);
463 // Print out the constraints.
464 Mgr.getConstraintManager().printJson(Out, this, NL, Space, IsDot);
466 // Print out the tracked dynamic types.
467 printDynamicTypeInfoJson(Out, this, NL, Space, IsDot);
469 // Print checker-specific data.
470 Mgr.getOwningEngine().printJson(Out, this, LCtx, NL, Space, IsDot);
473 Indent(Out, Space, IsDot) << '}';
476 void ProgramState::printDOT(raw_ostream &Out, const LocationContext *LCtx,
477 unsigned int Space) const {
478 printJson(Out, LCtx, /*NL=*/"\\l", Space, /*IsDot=*/true);
481 LLVM_DUMP_METHOD void ProgramState::dump() const {
482 printJson(llvm::errs());
485 AnalysisManager& ProgramState::getAnalysisManager() const {
486 return stateMgr->getOwningEngine().getAnalysisManager();
489 //===----------------------------------------------------------------------===//
491 //===----------------------------------------------------------------------===//
493 void *const* ProgramState::FindGDM(void *K) const {
494 return GDM.lookup(K);
498 ProgramStateManager::FindGDMContext(void *K,
499 void *(*CreateContext)(llvm::BumpPtrAllocator&),
500 void (*DeleteContext)(void*)) {
502 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
504 p.first = CreateContext(Alloc);
505 p.second = DeleteContext;
511 ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){
512 ProgramState::GenericDataMap M1 = St->getGDM();
513 ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
518 ProgramState NewSt = *St;
520 return getPersistentState(NewSt);
523 ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) {
524 ProgramState::GenericDataMap OldM = state->getGDM();
525 ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
530 ProgramState NewState = *state;
532 return getPersistentState(NewState);
535 bool ScanReachableSymbols::scan(nonloc::LazyCompoundVal val) {
536 bool wasVisited = !visited.insert(val.getCVData()).second;
540 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
541 // FIXME: We don't really want to use getBaseRegion() here because pointer
542 // arithmetic doesn't apply, but scanReachableSymbols only accepts base
543 // regions right now.
544 const MemRegion *R = val.getRegion()->getBaseRegion();
545 return StoreMgr.scanReachableSymbols(val.getStore(), R, *this);
548 bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
549 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
556 bool ScanReachableSymbols::scan(const SymExpr *sym) {
557 for (SymExpr::symbol_iterator SI = sym->symbol_begin(),
558 SE = sym->symbol_end();
560 bool wasVisited = !visited.insert(*SI).second;
564 if (!visitor.VisitSymbol(*SI))
571 bool ScanReachableSymbols::scan(SVal val) {
572 if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>())
573 return scan(X->getRegion());
575 if (Optional<nonloc::LazyCompoundVal> X =
576 val.getAs<nonloc::LazyCompoundVal>())
579 if (Optional<nonloc::LocAsInteger> X = val.getAs<nonloc::LocAsInteger>())
580 return scan(X->getLoc());
582 if (SymbolRef Sym = val.getAsSymbol())
585 if (Optional<nonloc::CompoundVal> X = val.getAs<nonloc::CompoundVal>())
591 bool ScanReachableSymbols::scan(const MemRegion *R) {
592 if (isa<MemSpaceRegion>(R))
595 bool wasVisited = !visited.insert(R).second;
599 if (!visitor.VisitMemRegion(R))
602 // If this is a symbolic region, visit the symbol for the region.
603 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
604 if (!visitor.VisitSymbol(SR->getSymbol()))
607 // If this is a subregion, also visit the parent regions.
608 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
609 const MemRegion *Super = SR->getSuperRegion();
613 // When we reach the topmost region, scan all symbols in it.
614 if (isa<MemSpaceRegion>(Super)) {
615 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
616 if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
621 // Regions captured by a block are also implicitly reachable.
622 if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
623 BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
624 E = BDR->referenced_vars_end();
625 for ( ; I != E; ++I) {
626 if (!scan(I.getCapturedRegion()))
634 bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
635 ScanReachableSymbols S(this, visitor);
639 bool ProgramState::scanReachableSymbols(
640 llvm::iterator_range<region_iterator> Reachable,
641 SymbolVisitor &visitor) const {
642 ScanReachableSymbols S(this, visitor);
643 for (const MemRegion *R : Reachable) {