1 //= ProgramState.cpp - Path-Sensitive "State" for tracking values --*- C++ -*--=
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements ProgramState and ProgramStateManager.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Analysis/CFG.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
16 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 using namespace clang;
22 using namespace ento;
23 
24 // Give the vtable for ConstraintManager somewhere to live.
25 // FIXME: Move this elsewhere.
26 ConstraintManager::~ConstraintManager() {}
27 
28 ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env,
29                  StoreRef st, GenericDataMap gdm)
30   : stateMgr(mgr),
31     Env(env),
32     store(st.getStore()),
33     GDM(gdm),
34     refCount(0) {
35   stateMgr->getStoreManager().incrementReferenceCount(store);
36 }
37 
38 ProgramState::ProgramState(const ProgramState &RHS)
39     : llvm::FoldingSetNode(),
40       stateMgr(RHS.stateMgr),
41       Env(RHS.Env),
42       store(RHS.store),
43       GDM(RHS.GDM),
44       refCount(0) {
45   stateMgr->getStoreManager().incrementReferenceCount(store);
46 }
47 
48 ProgramState::~ProgramState() {
49   if (store)
50     stateMgr->getStoreManager().decrementReferenceCount(store);
51 }
52 
53 ProgramStateManager::~ProgramStateManager() {
54   for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
55        I!=E; ++I)
56     I->second.second(I->second.first);
57 }
58 
59 const ProgramState*
60 ProgramStateManager::removeDeadBindings(const ProgramState *state,
61                                    const StackFrameContext *LCtx,
62                                    SymbolReaper& SymReaper) {
63 
64   // This code essentially performs a "mark-and-sweep" of the VariableBindings.
65   // The roots are any Block-level exprs and Decls that our liveness algorithm
66   // tells us are live.  We then see what Decls they may reference, and keep
67   // those around.  This code more than likely can be made faster, and the
68   // frequency of which this method is called should be experimented with
69   // for optimum performance.
70   ProgramState NewState = *state;
71 
72   NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state);
73 
74   // Clean up the store.
75   StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx,
76                                                    SymReaper);
77   NewState.setStore(newStore);
78   SymReaper.setReapedStore(newStore);
79 
80   return getPersistentState(NewState);
81 }
82 
83 const ProgramState *ProgramStateManager::MarshalState(const ProgramState *state,
84                                             const StackFrameContext *InitLoc) {
85   // make up an empty state for now.
86   ProgramState State(this,
87                 EnvMgr.getInitialEnvironment(),
88                 StoreMgr->getInitialStore(InitLoc),
89                 GDMFactory.getEmptyMap());
90 
91   return getPersistentState(State);
92 }
93 
94 const ProgramState *ProgramState::bindCompoundLiteral(const CompoundLiteralExpr *CL,
95                                             const LocationContext *LC,
96                                             SVal V) const {
97   const StoreRef &newStore =
98     getStateManager().StoreMgr->BindCompoundLiteral(getStore(), CL, LC, V);
99   return makeWithStore(newStore);
100 }
101 
102 const ProgramState *ProgramState::bindDecl(const VarRegion* VR, SVal IVal) const {
103   const StoreRef &newStore =
104     getStateManager().StoreMgr->BindDecl(getStore(), VR, IVal);
105   return makeWithStore(newStore);
106 }
107 
108 const ProgramState *ProgramState::bindDeclWithNoInit(const VarRegion* VR) const {
109   const StoreRef &newStore =
110     getStateManager().StoreMgr->BindDeclWithNoInit(getStore(), VR);
111   return makeWithStore(newStore);
112 }
113 
114 const ProgramState *ProgramState::bindLoc(Loc LV, SVal V) const {
115   ProgramStateManager &Mgr = getStateManager();
116   const ProgramState *newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
117                                                              LV, V));
118   const MemRegion *MR = LV.getAsRegion();
119   if (MR && Mgr.getOwningEngine())
120     return Mgr.getOwningEngine()->processRegionChange(newState, MR);
121 
122   return newState;
123 }
124 
125 const ProgramState *ProgramState::bindDefault(SVal loc, SVal V) const {
126   ProgramStateManager &Mgr = getStateManager();
127   const MemRegion *R = cast<loc::MemRegionVal>(loc).getRegion();
128   const StoreRef &newStore = Mgr.StoreMgr->BindDefault(getStore(), R, V);
129   const ProgramState *new_state = makeWithStore(newStore);
130   return Mgr.getOwningEngine() ?
131            Mgr.getOwningEngine()->processRegionChange(new_state, R) :
132            new_state;
133 }
134 
135 const ProgramState *
136 ProgramState::invalidateRegions(ArrayRef<const MemRegion *> Regions,
137                                 const Expr *E, unsigned Count,
138                                 StoreManager::InvalidatedSymbols *IS,
139                                 bool invalidateGlobals) const {
140   if (!IS) {
141     StoreManager::InvalidatedSymbols invalidated;
142     return invalidateRegionsImpl(Regions, E, Count,
143                                  invalidated, invalidateGlobals);
144   }
145   return invalidateRegionsImpl(Regions, E, Count, *IS, invalidateGlobals);
146 }
147 
148 const ProgramState *
149 ProgramState::invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions,
150                                     const Expr *E, unsigned Count,
151                                     StoreManager::InvalidatedSymbols &IS,
152                                     bool invalidateGlobals) const {
153   ProgramStateManager &Mgr = getStateManager();
154   SubEngine* Eng = Mgr.getOwningEngine();
155 
156   if (Eng && Eng->wantsRegionChangeUpdate(this)) {
157     StoreManager::InvalidatedRegions Invalidated;
158     const StoreRef &newStore
159       = Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, IS,
160                                         invalidateGlobals, &Invalidated);
161     const ProgramState *newState = makeWithStore(newStore);
162     return Eng->processRegionChanges(newState, &IS, Regions, Invalidated);
163   }
164 
165   const StoreRef &newStore =
166     Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, IS,
167                                     invalidateGlobals, NULL);
168   return makeWithStore(newStore);
169 }
170 
171 const ProgramState *ProgramState::unbindLoc(Loc LV) const {
172   assert(!isa<loc::MemRegionVal>(LV) && "Use invalidateRegion instead.");
173 
174   Store OldStore = getStore();
175   const StoreRef &newStore = getStateManager().StoreMgr->Remove(OldStore, LV);
176 
177   if (newStore.getStore() == OldStore)
178     return this;
179 
180   return makeWithStore(newStore);
181 }
182 
183 const ProgramState *ProgramState::enterStackFrame(const StackFrameContext *frame) const {
184   const StoreRef &new_store =
185     getStateManager().StoreMgr->enterStackFrame(this, frame);
186   return makeWithStore(new_store);
187 }
188 
189 SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
190   // We only want to do fetches from regions that we can actually bind
191   // values.  For example, SymbolicRegions of type 'id<...>' cannot
192   // have direct bindings (but their can be bindings on their subregions).
193   if (!R->isBoundable())
194     return UnknownVal();
195 
196   if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
197     QualType T = TR->getValueType();
198     if (Loc::isLocType(T) || T->isIntegerType())
199       return getSVal(R);
200   }
201 
202   return UnknownVal();
203 }
204 
205 SVal ProgramState::getSVal(Loc location, QualType T) const {
206   SVal V = getRawSVal(cast<Loc>(location), T);
207 
208   // If 'V' is a symbolic value that is *perfectly* constrained to
209   // be a constant value, use that value instead to lessen the burden
210   // on later analysis stages (so we have less symbolic values to reason
211   // about).
212   if (!T.isNull()) {
213     if (SymbolRef sym = V.getAsSymbol()) {
214       if (const llvm::APSInt *Int = getSymVal(sym)) {
215         // FIXME: Because we don't correctly model (yet) sign-extension
216         // and truncation of symbolic values, we need to convert
217         // the integer value to the correct signedness and bitwidth.
218         //
219         // This shows up in the following:
220         //
221         //   char foo();
222         //   unsigned x = foo();
223         //   if (x == 54)
224         //     ...
225         //
226         //  The symbolic value stored to 'x' is actually the conjured
227         //  symbol for the call to foo(); the type of that symbol is 'char',
228         //  not unsigned.
229         const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
230 
231         if (isa<Loc>(V))
232           return loc::ConcreteInt(NewV);
233         else
234           return nonloc::ConcreteInt(NewV);
235       }
236     }
237   }
238 
239   return V;
240 }
241 
242 const ProgramState *ProgramState::BindExpr(const Stmt *S, SVal V, bool Invalidate) const{
243   Environment NewEnv = getStateManager().EnvMgr.bindExpr(Env, S, V,
244                                                          Invalidate);
245   if (NewEnv == Env)
246     return this;
247 
248   ProgramState NewSt = *this;
249   NewSt.Env = NewEnv;
250   return getStateManager().getPersistentState(NewSt);
251 }
252 
253 const ProgramState *ProgramState::bindExprAndLocation(const Stmt *S, SVal location,
254                                             SVal V) const {
255   Environment NewEnv =
256     getStateManager().EnvMgr.bindExprAndLocation(Env, S, location, V);
257 
258   if (NewEnv == Env)
259     return this;
260 
261   ProgramState NewSt = *this;
262   NewSt.Env = NewEnv;
263   return getStateManager().getPersistentState(NewSt);
264 }
265 
266 const ProgramState *ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
267                                       DefinedOrUnknownSVal UpperBound,
268                                       bool Assumption) const {
269   if (Idx.isUnknown() || UpperBound.isUnknown())
270     return this;
271 
272   // Build an expression for 0 <= Idx < UpperBound.
273   // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
274   // FIXME: This should probably be part of SValBuilder.
275   ProgramStateManager &SM = getStateManager();
276   SValBuilder &svalBuilder = SM.getSValBuilder();
277   ASTContext &Ctx = svalBuilder.getContext();
278 
279   // Get the offset: the minimum value of the array index type.
280   BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
281   // FIXME: This should be using ValueManager::ArrayindexTy...somehow.
282   QualType indexTy = Ctx.IntTy;
283   nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
284 
285   // Adjust the index.
286   SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
287                                         cast<NonLoc>(Idx), Min, indexTy);
288   if (newIdx.isUnknownOrUndef())
289     return this;
290 
291   // Adjust the upper bound.
292   SVal newBound =
293     svalBuilder.evalBinOpNN(this, BO_Add, cast<NonLoc>(UpperBound),
294                             Min, indexTy);
295 
296   if (newBound.isUnknownOrUndef())
297     return this;
298 
299   // Build the actual comparison.
300   SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT,
301                                 cast<NonLoc>(newIdx), cast<NonLoc>(newBound),
302                                 Ctx.IntTy);
303   if (inBound.isUnknownOrUndef())
304     return this;
305 
306   // Finally, let the constraint manager take care of it.
307   ConstraintManager &CM = SM.getConstraintManager();
308   return CM.assume(this, cast<DefinedSVal>(inBound), Assumption);
309 }
310 
311 const ProgramState *ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
312   ProgramState State(this,
313                 EnvMgr.getInitialEnvironment(),
314                 StoreMgr->getInitialStore(InitLoc),
315                 GDMFactory.getEmptyMap());
316 
317   return getPersistentState(State);
318 }
319 
320 void ProgramStateManager::recycleUnusedStates() {
321   for (std::vector<ProgramState*>::iterator i = recentlyAllocatedStates.begin(),
322        e = recentlyAllocatedStates.end(); i != e; ++i) {
323     ProgramState *state = *i;
324     if (state->referencedByExplodedNode())
325       continue;
326     StateSet.RemoveNode(state);
327     freeStates.push_back(state);
328     state->~ProgramState();
329   }
330   recentlyAllocatedStates.clear();
331 }
332 
333 const ProgramState *ProgramStateManager::getPersistentStateWithGDM(
334                                                      const ProgramState *FromState,
335                                                      const ProgramState *GDMState) {
336   ProgramState NewState = *FromState;
337   NewState.GDM = GDMState->GDM;
338   return getPersistentState(NewState);
339 }
340 
341 const ProgramState *ProgramStateManager::getPersistentState(ProgramState &State) {
342 
343   llvm::FoldingSetNodeID ID;
344   State.Profile(ID);
345   void *InsertPos;
346 
347   if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
348     return I;
349 
350   ProgramState *newState = 0;
351   if (!freeStates.empty()) {
352     newState = freeStates.back();
353     freeStates.pop_back();
354   }
355   else {
356     newState = (ProgramState*) Alloc.Allocate<ProgramState>();
357   }
358   new (newState) ProgramState(State);
359   StateSet.InsertNode(newState, InsertPos);
360   recentlyAllocatedStates.push_back(newState);
361   return newState;
362 }
363 
364 const ProgramState *ProgramState::makeWithStore(const StoreRef &store) const {
365   ProgramState NewSt = *this;
366   NewSt.setStore(store);
367   return getStateManager().getPersistentState(NewSt);
368 }
369 
370 void ProgramState::setStore(const StoreRef &newStore) {
371   Store newStoreStore = newStore.getStore();
372   if (newStoreStore)
373     stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
374   if (store)
375     stateMgr->getStoreManager().decrementReferenceCount(store);
376   store = newStoreStore;
377 }
378 
379 //===----------------------------------------------------------------------===//
380 //  State pretty-printing.
381 //===----------------------------------------------------------------------===//
382 
383 static bool IsEnvLoc(const Stmt *S) {
384   // FIXME: This is a layering violation.  Should be in environment.
385   return (bool) (((uintptr_t) S) & 0x1);
386 }
387 
388 void ProgramState::print(raw_ostream &Out, CFG *C,
389                          const char *NL, const char *Sep) const {
390   // Print the store.
391   ProgramStateManager &Mgr = getStateManager();
392   Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
393   bool isFirst = true;
394 
395   // FIXME: All environment printing should be moved inside Environment.
396   if (C) {
397     // Print Subexpression bindings.
398     for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
399       if (C->isBlkExpr(I.getKey()) || IsEnvLoc(I.getKey()))
400         continue;
401 
402       if (isFirst) {
403         Out << NL << NL << "Sub-Expressions:" << NL;
404         isFirst = false;
405       } else {
406         Out << NL;
407       }
408 
409       Out << " (" << (void*) I.getKey() << ") ";
410       LangOptions LO; // FIXME.
411       I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
412       Out << " : " << I.getData();
413     }
414 
415     // Print block-expression bindings.
416     isFirst = true;
417     for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
418       if (!C->isBlkExpr(I.getKey()))
419         continue;
420 
421       if (isFirst) {
422         Out << NL << NL << "Block-level Expressions:" << NL;
423         isFirst = false;
424       } else {
425         Out << NL;
426       }
427 
428       Out << " (" << (void*) I.getKey() << ") ";
429       LangOptions LO; // FIXME.
430       I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
431       Out << " : " << I.getData();
432     }
433   } else {
434     // Print All bindings - no info to differentiate block from subexpressions.
435     for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
436       if (IsEnvLoc(I.getKey()))
437         continue;
438 
439       if (isFirst) {
440         Out << NL << NL << "Expressions:" << NL;
441         isFirst = false;
442       } else {
443         Out << NL;
444       }
445 
446       Out << " (" << (void*) I.getKey() << ") ";
447       LangOptions LO; // FIXME.
448       I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
449       Out << " : " << I.getData();
450     }
451   }
452 
453   // Print locations.
454   isFirst = true;
455 
456   for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
457     if (!IsEnvLoc(I.getKey()))
458       continue;
459 
460     if (isFirst) {
461       Out << NL << NL << "Load/store locations:" << NL;
462       isFirst = false;
463     } else {
464       Out << NL;
465     }
466 
467     const Stmt *S = (Stmt*) (((uintptr_t) I.getKey()) & ((uintptr_t) ~0x1));
468 
469     Out << " (" << (void*) S << ") ";
470     LangOptions LO; // FIXME.
471     S->printPretty(Out, 0, PrintingPolicy(LO));
472     Out << " : " << I.getData();
473   }
474 
475   Mgr.getConstraintManager().print(this, Out, NL, Sep);
476 
477   // Print checker-specific data.
478   Mgr.getOwningEngine()->printState(Out, this, NL, Sep);
479 }
480 
481 void ProgramState::printDOT(raw_ostream &Out, CFG &C) const {
482   print(Out, &C, "\\l", "\\|");
483 }
484 
485 void ProgramState::dump(CFG &C) const {
486   print(llvm::errs(), &C);
487 }
488 
489 void ProgramState::dump() const {
490   print(llvm::errs(), 0);
491 }
492 
493 //===----------------------------------------------------------------------===//
494 // Generic Data Map.
495 //===----------------------------------------------------------------------===//
496 
497 void *const* ProgramState::FindGDM(void *K) const {
498   return GDM.lookup(K);
499 }
500 
501 void*
502 ProgramStateManager::FindGDMContext(void *K,
503                                void *(*CreateContext)(llvm::BumpPtrAllocator&),
504                                void (*DeleteContext)(void*)) {
505 
506   std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
507   if (!p.first) {
508     p.first = CreateContext(Alloc);
509     p.second = DeleteContext;
510   }
511 
512   return p.first;
513 }
514 
515 const ProgramState *ProgramStateManager::addGDM(const ProgramState *St, void *Key, void *Data){
516   ProgramState::GenericDataMap M1 = St->getGDM();
517   ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
518 
519   if (M1 == M2)
520     return St;
521 
522   ProgramState NewSt = *St;
523   NewSt.GDM = M2;
524   return getPersistentState(NewSt);
525 }
526 
527 const ProgramState *ProgramStateManager::removeGDM(const ProgramState *state, void *Key) {
528   ProgramState::GenericDataMap OldM = state->getGDM();
529   ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
530 
531   if (NewM == OldM)
532     return state;
533 
534   ProgramState NewState = *state;
535   NewState.GDM = NewM;
536   return getPersistentState(NewState);
537 }
538 
539 void ScanReachableSymbols::anchor() { }
540 
541 bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
542   for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
543     if (!scan(*I))
544       return false;
545 
546   return true;
547 }
548 
549 bool ScanReachableSymbols::scan(const SymExpr *sym) {
550   unsigned &isVisited = visited[sym];
551   if (isVisited)
552     return true;
553   isVisited = 1;
554 
555   if (!visitor.VisitSymbol(sym))
556     return false;
557 
558   // TODO: should be rewritten using SymExpr::symbol_iterator.
559   switch (sym->getKind()) {
560     case SymExpr::RegionValueKind:
561     case SymExpr::ConjuredKind:
562     case SymExpr::DerivedKind:
563     case SymExpr::ExtentKind:
564     case SymExpr::MetadataKind:
565       break;
566     case SymExpr::CastSymbolKind:
567       return scan(cast<SymbolCast>(sym)->getOperand());
568     case SymExpr::SymIntKind:
569       return scan(cast<SymIntExpr>(sym)->getLHS());
570     case SymExpr::IntSymKind:
571       return scan(cast<IntSymExpr>(sym)->getRHS());
572     case SymExpr::SymSymKind: {
573       const SymSymExpr *x = cast<SymSymExpr>(sym);
574       return scan(x->getLHS()) && scan(x->getRHS());
575     }
576   }
577   return true;
578 }
579 
580 bool ScanReachableSymbols::scan(SVal val) {
581   if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
582     return scan(X->getRegion());
583 
584   if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val))
585     return scan(X->getLoc());
586 
587   if (SymbolRef Sym = val.getAsSymbol())
588     return scan(Sym);
589 
590   if (const SymExpr *Sym = val.getAsSymbolicExpression())
591     return scan(Sym);
592 
593   if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
594     return scan(*X);
595 
596   return true;
597 }
598 
599 bool ScanReachableSymbols::scan(const MemRegion *R) {
600   if (isa<MemSpaceRegion>(R))
601     return true;
602 
603   unsigned &isVisited = visited[R];
604   if (isVisited)
605     return true;
606   isVisited = 1;
607 
608   // If this is a symbolic region, visit the symbol for the region.
609   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
610     if (!visitor.VisitSymbol(SR->getSymbol()))
611       return false;
612 
613   // If this is a subregion, also visit the parent regions.
614   if (const SubRegion *SR = dyn_cast<SubRegion>(R))
615     if (!scan(SR->getSuperRegion()))
616       return false;
617 
618   // Now look at the binding to this region (if any).
619   if (!scan(state->getSValAsScalarOrLoc(R)))
620     return false;
621 
622   // Now look at the subregions.
623   if (!SRM.get())
624     SRM.reset(state->getStateManager().getStoreManager().
625                                            getSubRegionMap(state->getStore()));
626 
627   return SRM->iterSubRegions(R, *this);
628 }
629 
630 bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
631   ScanReachableSymbols S(this, visitor);
632   return S.scan(val);
633 }
634 
635 bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
636                                    SymbolVisitor &visitor) const {
637   ScanReachableSymbols S(this, visitor);
638   for ( ; I != E; ++I) {
639     if (!S.scan(*I))
640       return false;
641   }
642   return true;
643 }
644 
645 bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
646                                    const MemRegion * const *E,
647                                    SymbolVisitor &visitor) const {
648   ScanReachableSymbols S(this, visitor);
649   for ( ; I != E; ++I) {
650     if (!S.scan(*I))
651       return false;
652   }
653   return true;
654 }
655 
656 const ProgramState* ProgramState::addTaint(const Stmt *S,
657                                            TaintTagType Kind) const {
658   if (const Expr *E = dyn_cast_or_null<Expr>(S))
659     S = E->IgnoreParens();
660 
661   SymbolRef Sym = getSVal(S).getAsSymbol();
662   if (Sym)
663     return addTaint(Sym, Kind);
664 
665   const MemRegion *R = getSVal(S).getAsRegion();
666   addTaint(R, Kind);
667 
668   // Cannot add taint, so just return the state.
669   return this;
670 }
671 
672 const ProgramState* ProgramState::addTaint(const MemRegion *R,
673                                            TaintTagType Kind) const {
674   if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R))
675     return addTaint(SR->getSymbol(), Kind);
676   return this;
677 }
678 
679 const ProgramState* ProgramState::addTaint(SymbolRef Sym,
680                                            TaintTagType Kind) const {
681   const ProgramState *NewState = set<TaintMap>(Sym, Kind);
682   assert(NewState);
683   return NewState;
684 }
685 
686 bool ProgramState::isTainted(const Stmt *S, TaintTagType Kind) const {
687   if (const Expr *E = dyn_cast_or_null<Expr>(S))
688     S = E->IgnoreParens();
689 
690   SVal val = getSVal(S);
691   return isTainted(val, Kind);
692 }
693 
694 bool ProgramState::isTainted(SVal V, TaintTagType Kind) const {
695   if (const SymExpr *Sym = V.getAsSymExpr())
696     return isTainted(Sym, Kind);
697   if (const MemRegion *Reg = V.getAsRegion())
698     return isTainted(Reg, Kind);
699   return false;
700 }
701 
702 bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const {
703   if (!Reg)
704     return false;
705 
706   // Element region (array element) is tainted if either the base or the offset
707   // are tainted.
708   if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg))
709     return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K);
710 
711   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg))
712     return isTainted(SR->getSymbol(), K);
713 
714   if (const SubRegion *ER = dyn_cast<SubRegion>(Reg))
715     return isTainted(ER->getSuperRegion(), K);
716 
717   return false;
718 }
719 
720 bool ProgramState::isTainted(const SymExpr* Sym, TaintTagType Kind) const {
721   if (!Sym)
722     return false;
723 
724   // Traverse all the symbols this symbol depends on to see if any are tainted.
725   bool Tainted = false;
726   for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
727        SI != SE; ++SI) {
728     assert(isa<SymbolData>(*SI));
729     const TaintTagType *Tag = get<TaintMap>(*SI);
730     Tainted = (Tag && *Tag == Kind);
731 
732     // If this is a SymbolDerived with a tainted parent, it's also tainted.
733     if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI))
734       Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind);
735 
736     // If memory region is tainted, data is also tainted.
737     if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI))
738       Tainted = Tainted || isTainted(SRV->getRegion(), Kind);
739 
740     if (Tainted)
741       return true;
742   }
743 
744   return Tainted;
745 }
746