1 //= ProgramState.cpp - Path-Sensitive "State" for tracking values --*- C++ -*--=
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements ProgramState and ProgramStateManager.
12 //===----------------------------------------------------------------------===//
14 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
15 #include "clang/Analysis/CFG.h"
16 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h"
20 #include "llvm/Support/raw_ostream.h"
22 using namespace clang;
25 namespace clang { namespace ento {
26 /// Increments the number of times this state is referenced.
28 void ProgramStateRetain(const ProgramState *state) {
29 ++const_cast<ProgramState*>(state)->refCount;
32 /// Decrement the number of times this state is referenced.
33 void ProgramStateRelease(const ProgramState *state) {
34 assert(state->refCount > 0);
35 ProgramState *s = const_cast<ProgramState*>(state);
36 if (--s->refCount == 0) {
37 ProgramStateManager &Mgr = s->getStateManager();
38 Mgr.StateSet.RemoveNode(s);
40 Mgr.freeStates.push_back(s);
45 ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env,
46 StoreRef st, GenericDataMap gdm)
52 stateMgr->getStoreManager().incrementReferenceCount(store);
55 ProgramState::ProgramState(const ProgramState &RHS)
56 : llvm::FoldingSetNode(),
57 stateMgr(RHS.stateMgr),
62 stateMgr->getStoreManager().incrementReferenceCount(store);
65 ProgramState::~ProgramState() {
67 stateMgr->getStoreManager().decrementReferenceCount(store);
70 ProgramStateManager::ProgramStateManager(ASTContext &Ctx,
71 StoreManagerCreator CreateSMgr,
72 ConstraintManagerCreator CreateCMgr,
73 llvm::BumpPtrAllocator &alloc,
75 : Eng(SubEng), EnvMgr(alloc), GDMFactory(alloc),
76 svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
77 CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) {
78 StoreMgr = (*CreateSMgr)(*this);
79 ConstraintMgr = (*CreateCMgr)(*this, SubEng);
83 ProgramStateManager::~ProgramStateManager() {
84 for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
86 I->second.second(I->second.first);
90 ProgramStateManager::removeDeadBindings(ProgramStateRef state,
91 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 ProgramStateRef Result = getPersistentState(NewState);
111 return ConstraintMgr->removeDeadBindings(Result, SymReaper);
114 ProgramStateRef ProgramState::bindLoc(Loc LV, SVal V, bool notifyChanges) const {
115 ProgramStateManager &Mgr = getStateManager();
116 ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
118 const MemRegion *MR = LV.getAsRegion();
119 if (MR && Mgr.getOwningEngine() && notifyChanges)
120 return Mgr.getOwningEngine()->processRegionChange(newState, MR);
125 ProgramStateRef ProgramState::bindDefault(SVal loc, SVal V) const {
126 ProgramStateManager &Mgr = getStateManager();
127 const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
128 const StoreRef &newStore = Mgr.StoreMgr->BindDefault(getStore(), R, V);
129 ProgramStateRef new_state = makeWithStore(newStore);
130 return Mgr.getOwningEngine() ?
131 Mgr.getOwningEngine()->processRegionChange(new_state, R) :
135 typedef ArrayRef<const MemRegion *> RegionList;
136 typedef ArrayRef<SVal> ValueList;
139 ProgramState::invalidateRegions(RegionList Regions,
140 const Expr *E, unsigned Count,
141 const LocationContext *LCtx,
142 bool CausedByPointerEscape,
143 InvalidatedSymbols *IS,
144 const CallEvent *Call,
145 RegionAndSymbolInvalidationTraits *ITraits) const {
146 SmallVector<SVal, 8> Values;
147 for (RegionList::const_iterator I = Regions.begin(),
148 End = Regions.end(); I != End; ++I)
149 Values.push_back(loc::MemRegionVal(*I));
151 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
156 ProgramState::invalidateRegions(ValueList Values,
157 const Expr *E, unsigned Count,
158 const LocationContext *LCtx,
159 bool CausedByPointerEscape,
160 InvalidatedSymbols *IS,
161 const CallEvent *Call,
162 RegionAndSymbolInvalidationTraits *ITraits) const {
164 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
169 ProgramState::invalidateRegionsImpl(ValueList Values,
170 const Expr *E, unsigned Count,
171 const LocationContext *LCtx,
172 bool CausedByPointerEscape,
173 InvalidatedSymbols *IS,
174 RegionAndSymbolInvalidationTraits *ITraits,
175 const CallEvent *Call) const {
176 ProgramStateManager &Mgr = getStateManager();
177 SubEngine* Eng = Mgr.getOwningEngine();
179 InvalidatedSymbols Invalidated;
183 RegionAndSymbolInvalidationTraits ITraitsLocal;
185 ITraits = &ITraitsLocal;
188 StoreManager::InvalidatedRegions TopLevelInvalidated;
189 StoreManager::InvalidatedRegions Invalidated;
190 const StoreRef &newStore
191 = Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
192 *IS, *ITraits, &TopLevelInvalidated,
195 ProgramStateRef newState = makeWithStore(newStore);
197 if (CausedByPointerEscape) {
198 newState = Eng->notifyCheckersOfPointerEscape(newState, IS,
204 return Eng->processRegionChanges(newState, IS, TopLevelInvalidated,
208 const StoreRef &newStore =
209 Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
210 *IS, *ITraits, nullptr, nullptr);
211 return makeWithStore(newStore);
214 ProgramStateRef ProgramState::killBinding(Loc LV) const {
215 assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead.");
217 Store OldStore = getStore();
218 const StoreRef &newStore =
219 getStateManager().StoreMgr->killBinding(OldStore, LV);
221 if (newStore.getStore() == OldStore)
224 return makeWithStore(newStore);
228 ProgramState::enterStackFrame(const CallEvent &Call,
229 const StackFrameContext *CalleeCtx) const {
230 const StoreRef &NewStore =
231 getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
232 return makeWithStore(NewStore);
235 SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
236 // We only want to do fetches from regions that we can actually bind
237 // values. For example, SymbolicRegions of type 'id<...>' cannot
238 // have direct bindings (but their can be bindings on their subregions).
239 if (!R->isBoundable())
242 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
243 QualType T = TR->getValueType();
244 if (Loc::isLocType(T) || T->isIntegralOrEnumerationType())
251 SVal ProgramState::getSVal(Loc location, QualType T) const {
252 SVal V = getRawSVal(cast<Loc>(location), T);
254 // If 'V' is a symbolic value that is *perfectly* constrained to
255 // be a constant value, use that value instead to lessen the burden
256 // on later analysis stages (so we have less symbolic values to reason
259 if (SymbolRef sym = V.getAsSymbol()) {
260 if (const llvm::APSInt *Int = getStateManager()
261 .getConstraintManager()
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.
267 // This shows up in the following:
270 // unsigned x = foo();
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',
277 const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
280 return loc::ConcreteInt(NewV);
282 return nonloc::ConcreteInt(NewV);
290 ProgramStateRef ProgramState::BindExpr(const Stmt *S,
291 const LocationContext *LCtx,
292 SVal V, bool Invalidate) const{
294 getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
299 ProgramState NewSt = *this;
301 return getStateManager().getPersistentState(NewSt);
304 ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
305 DefinedOrUnknownSVal UpperBound,
307 QualType indexTy) const {
308 if (Idx.isUnknown() || UpperBound.isUnknown())
311 // Build an expression for 0 <= Idx < UpperBound.
312 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
313 // FIXME: This should probably be part of SValBuilder.
314 ProgramStateManager &SM = getStateManager();
315 SValBuilder &svalBuilder = SM.getSValBuilder();
316 ASTContext &Ctx = svalBuilder.getContext();
318 // Get the offset: the minimum value of the array index type.
319 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
320 // FIXME: This should be using ValueManager::ArrayindexTy...somehow.
321 if (indexTy.isNull())
323 nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
326 SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
327 Idx.castAs<NonLoc>(), Min, indexTy);
328 if (newIdx.isUnknownOrUndef())
331 // Adjust the upper bound.
333 svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
336 if (newBound.isUnknownOrUndef())
339 // Build the actual comparison.
340 SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(),
341 newBound.castAs<NonLoc>(), Ctx.IntTy);
342 if (inBound.isUnknownOrUndef())
345 // Finally, let the constraint manager take care of it.
346 ConstraintManager &CM = SM.getConstraintManager();
347 return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption);
350 ConditionTruthVal ProgramState::isNull(SVal V) const {
351 if (V.isZeroConstant())
357 SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
359 return ConditionTruthVal();
361 return getStateManager().ConstraintMgr->isNull(this, Sym);
364 ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
365 ProgramState State(this,
366 EnvMgr.getInitialEnvironment(),
367 StoreMgr->getInitialStore(InitLoc),
368 GDMFactory.getEmptyMap());
370 return getPersistentState(State);
373 ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
374 ProgramStateRef FromState,
375 ProgramStateRef GDMState) {
376 ProgramState NewState(*FromState);
377 NewState.GDM = GDMState->GDM;
378 return getPersistentState(NewState);
381 ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
383 llvm::FoldingSetNodeID ID;
387 if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
390 ProgramState *newState = nullptr;
391 if (!freeStates.empty()) {
392 newState = freeStates.back();
393 freeStates.pop_back();
396 newState = (ProgramState*) Alloc.Allocate<ProgramState>();
398 new (newState) ProgramState(State);
399 StateSet.InsertNode(newState, InsertPos);
403 ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
404 ProgramState NewSt(*this);
405 NewSt.setStore(store);
406 return getStateManager().getPersistentState(NewSt);
409 void ProgramState::setStore(const StoreRef &newStore) {
410 Store newStoreStore = newStore.getStore();
412 stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
414 stateMgr->getStoreManager().decrementReferenceCount(store);
415 store = newStoreStore;
418 //===----------------------------------------------------------------------===//
419 // State pretty-printing.
420 //===----------------------------------------------------------------------===//
422 void ProgramState::print(raw_ostream &Out,
423 const char *NL, const char *Sep) const {
425 ProgramStateManager &Mgr = getStateManager();
426 Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
428 // Print out the environment.
429 Env.print(Out, NL, Sep);
431 // Print out the constraints.
432 Mgr.getConstraintManager().print(this, Out, NL, Sep);
434 // Print checker-specific data.
435 Mgr.getOwningEngine()->printState(Out, this, NL, Sep);
438 void ProgramState::printDOT(raw_ostream &Out) const {
439 print(Out, "\\l", "\\|");
442 LLVM_DUMP_METHOD void ProgramState::dump() const {
446 void ProgramState::printTaint(raw_ostream &Out,
447 const char *NL, const char *Sep) const {
448 TaintMapImpl TM = get<TaintMap>();
451 Out <<"Tainted Symbols:" << NL;
453 for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) {
454 Out << I->first << " : " << I->second << NL;
458 void ProgramState::dumpTaint() const {
459 printTaint(llvm::errs());
462 //===----------------------------------------------------------------------===//
464 //===----------------------------------------------------------------------===//
466 void *const* ProgramState::FindGDM(void *K) const {
467 return GDM.lookup(K);
471 ProgramStateManager::FindGDMContext(void *K,
472 void *(*CreateContext)(llvm::BumpPtrAllocator&),
473 void (*DeleteContext)(void*)) {
475 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
477 p.first = CreateContext(Alloc);
478 p.second = DeleteContext;
484 ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){
485 ProgramState::GenericDataMap M1 = St->getGDM();
486 ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
491 ProgramState NewSt = *St;
493 return getPersistentState(NewSt);
496 ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) {
497 ProgramState::GenericDataMap OldM = state->getGDM();
498 ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
503 ProgramState NewState = *state;
505 return getPersistentState(NewState);
508 bool ScanReachableSymbols::scan(nonloc::LazyCompoundVal val) {
509 bool wasVisited = !visited.insert(val.getCVData()).second;
513 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
514 // FIXME: We don't really want to use getBaseRegion() here because pointer
515 // arithmetic doesn't apply, but scanReachableSymbols only accepts base
516 // regions right now.
517 const MemRegion *R = val.getRegion()->getBaseRegion();
518 return StoreMgr.scanReachableSymbols(val.getStore(), R, *this);
521 bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
522 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
529 bool ScanReachableSymbols::scan(const SymExpr *sym) {
530 for (SymExpr::symbol_iterator SI = sym->symbol_begin(),
531 SE = sym->symbol_end();
533 bool wasVisited = !visited.insert(*SI).second;
537 if (!visitor.VisitSymbol(*SI))
544 bool ScanReachableSymbols::scan(SVal val) {
545 if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>())
546 return scan(X->getRegion());
548 if (Optional<nonloc::LazyCompoundVal> X =
549 val.getAs<nonloc::LazyCompoundVal>())
552 if (Optional<nonloc::LocAsInteger> X = val.getAs<nonloc::LocAsInteger>())
553 return scan(X->getLoc());
555 if (SymbolRef Sym = val.getAsSymbol())
558 if (const SymExpr *Sym = val.getAsSymbolicExpression())
561 if (Optional<nonloc::CompoundVal> X = val.getAs<nonloc::CompoundVal>())
567 bool ScanReachableSymbols::scan(const MemRegion *R) {
568 if (isa<MemSpaceRegion>(R))
571 bool wasVisited = !visited.insert(R).second;
575 if (!visitor.VisitMemRegion(R))
578 // If this is a symbolic region, visit the symbol for the region.
579 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
580 if (!visitor.VisitSymbol(SR->getSymbol()))
583 // If this is a subregion, also visit the parent regions.
584 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
585 const MemRegion *Super = SR->getSuperRegion();
589 // When we reach the topmost region, scan all symbols in it.
590 if (isa<MemSpaceRegion>(Super)) {
591 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
592 if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
597 // Regions captured by a block are also implicitly reachable.
598 if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
599 BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
600 E = BDR->referenced_vars_end();
601 for ( ; I != E; ++I) {
602 if (!scan(I.getCapturedRegion()))
610 bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
611 ScanReachableSymbols S(this, visitor);
615 bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
616 SymbolVisitor &visitor) const {
617 ScanReachableSymbols S(this, visitor);
618 for ( ; I != E; ++I) {
625 bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
626 const MemRegion * const *E,
627 SymbolVisitor &visitor) const {
628 ScanReachableSymbols S(this, visitor);
629 for ( ; I != E; ++I) {
636 ProgramStateRef ProgramState::addTaint(const Stmt *S,
637 const LocationContext *LCtx,
638 TaintTagType Kind) const {
639 if (const Expr *E = dyn_cast_or_null<Expr>(S))
640 S = E->IgnoreParens();
642 SymbolRef Sym = getSVal(S, LCtx).getAsSymbol();
644 return addTaint(Sym, Kind);
646 const MemRegion *R = getSVal(S, LCtx).getAsRegion();
649 // Cannot add taint, so just return the state.
653 ProgramStateRef ProgramState::addTaint(const MemRegion *R,
654 TaintTagType Kind) const {
655 if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R))
656 return addTaint(SR->getSymbol(), Kind);
660 ProgramStateRef ProgramState::addTaint(SymbolRef Sym,
661 TaintTagType Kind) const {
662 // If this is a symbol cast, remove the cast before adding the taint. Taint
664 while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym))
665 Sym = SC->getOperand();
667 ProgramStateRef NewState = set<TaintMap>(Sym, Kind);
672 bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx,
673 TaintTagType Kind) const {
674 if (const Expr *E = dyn_cast_or_null<Expr>(S))
675 S = E->IgnoreParens();
677 SVal val = getSVal(S, LCtx);
678 return isTainted(val, Kind);
681 bool ProgramState::isTainted(SVal V, TaintTagType Kind) const {
682 if (const SymExpr *Sym = V.getAsSymExpr())
683 return isTainted(Sym, Kind);
684 if (const MemRegion *Reg = V.getAsRegion())
685 return isTainted(Reg, Kind);
689 bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const {
693 // Element region (array element) is tainted if either the base or the offset
695 if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg))
696 return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K);
698 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg))
699 return isTainted(SR->getSymbol(), K);
701 if (const SubRegion *ER = dyn_cast<SubRegion>(Reg))
702 return isTainted(ER->getSuperRegion(), K);
707 bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const {
711 // Traverse all the symbols this symbol depends on to see if any are tainted.
712 bool Tainted = false;
713 for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
715 if (!isa<SymbolData>(*SI))
718 const TaintTagType *Tag = get<TaintMap>(*SI);
719 Tainted = (Tag && *Tag == Kind);
721 // If this is a SymbolDerived with a tainted parent, it's also tainted.
722 if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI))
723 Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind);
725 // If memory region is tainted, data is also tainted.
726 if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI))
727 Tainted = Tainted || isTainted(SRV->getRegion(), Kind);
729 // If If this is a SymbolCast from a tainted value, it's also tainted.
730 if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI))
731 Tainted = Tainted || isTainted(SC->getOperand(), Kind);