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                                 const CallOrObjCMessage *Call) const {
140   if (!IS) {
141     StoreManager::InvalidatedSymbols invalidated;
142     return invalidateRegionsImpl(Regions, E, Count,
143                                  invalidated, Call);
144   }
145   return invalidateRegionsImpl(Regions, E, Count, *IS, Call);
146 }
147 
148 const ProgramState *
149 ProgramState::invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions,
150                                     const Expr *E, unsigned Count,
151                                     StoreManager::InvalidatedSymbols &IS,
152                                     const CallOrObjCMessage *Call) 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                                         Call, &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                                     Call, 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,
243                                            const LocationContext *LCtx,
244                                            SVal V, bool Invalidate) const{
245   Environment NewEnv =
246     getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
247                                       Invalidate);
248   if (NewEnv == Env)
249     return this;
250 
251   ProgramState NewSt = *this;
252   NewSt.Env = NewEnv;
253   return getStateManager().getPersistentState(NewSt);
254 }
255 
256 const ProgramState *
257 ProgramState::bindExprAndLocation(const Stmt *S, const LocationContext *LCtx,
258                                   SVal location,
259                                   SVal V) const {
260   Environment NewEnv =
261     getStateManager().EnvMgr.bindExprAndLocation(Env,
262                                                  EnvironmentEntry(S, LCtx),
263                                                  location, V);
264 
265   if (NewEnv == Env)
266     return this;
267 
268   ProgramState NewSt = *this;
269   NewSt.Env = NewEnv;
270   return getStateManager().getPersistentState(NewSt);
271 }
272 
273 const ProgramState *ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
274                                       DefinedOrUnknownSVal UpperBound,
275                                       bool Assumption) const {
276   if (Idx.isUnknown() || UpperBound.isUnknown())
277     return this;
278 
279   // Build an expression for 0 <= Idx < UpperBound.
280   // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
281   // FIXME: This should probably be part of SValBuilder.
282   ProgramStateManager &SM = getStateManager();
283   SValBuilder &svalBuilder = SM.getSValBuilder();
284   ASTContext &Ctx = svalBuilder.getContext();
285 
286   // Get the offset: the minimum value of the array index type.
287   BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
288   // FIXME: This should be using ValueManager::ArrayindexTy...somehow.
289   QualType indexTy = Ctx.IntTy;
290   nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
291 
292   // Adjust the index.
293   SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
294                                         cast<NonLoc>(Idx), Min, indexTy);
295   if (newIdx.isUnknownOrUndef())
296     return this;
297 
298   // Adjust the upper bound.
299   SVal newBound =
300     svalBuilder.evalBinOpNN(this, BO_Add, cast<NonLoc>(UpperBound),
301                             Min, indexTy);
302 
303   if (newBound.isUnknownOrUndef())
304     return this;
305 
306   // Build the actual comparison.
307   SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT,
308                                 cast<NonLoc>(newIdx), cast<NonLoc>(newBound),
309                                 Ctx.IntTy);
310   if (inBound.isUnknownOrUndef())
311     return this;
312 
313   // Finally, let the constraint manager take care of it.
314   ConstraintManager &CM = SM.getConstraintManager();
315   return CM.assume(this, cast<DefinedSVal>(inBound), Assumption);
316 }
317 
318 const ProgramState *ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
319   ProgramState State(this,
320                 EnvMgr.getInitialEnvironment(),
321                 StoreMgr->getInitialStore(InitLoc),
322                 GDMFactory.getEmptyMap());
323 
324   return getPersistentState(State);
325 }
326 
327 void ProgramStateManager::recycleUnusedStates() {
328   for (std::vector<ProgramState*>::iterator i = recentlyAllocatedStates.begin(),
329        e = recentlyAllocatedStates.end(); i != e; ++i) {
330     ProgramState *state = *i;
331     if (state->referencedByExplodedNode())
332       continue;
333     StateSet.RemoveNode(state);
334     freeStates.push_back(state);
335     state->~ProgramState();
336   }
337   recentlyAllocatedStates.clear();
338 }
339 
340 const ProgramState *ProgramStateManager::getPersistentStateWithGDM(
341                                                      const ProgramState *FromState,
342                                                      const ProgramState *GDMState) {
343   ProgramState NewState = *FromState;
344   NewState.GDM = GDMState->GDM;
345   return getPersistentState(NewState);
346 }
347 
348 const ProgramState *ProgramStateManager::getPersistentState(ProgramState &State) {
349 
350   llvm::FoldingSetNodeID ID;
351   State.Profile(ID);
352   void *InsertPos;
353 
354   if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
355     return I;
356 
357   ProgramState *newState = 0;
358   if (!freeStates.empty()) {
359     newState = freeStates.back();
360     freeStates.pop_back();
361   }
362   else {
363     newState = (ProgramState*) Alloc.Allocate<ProgramState>();
364   }
365   new (newState) ProgramState(State);
366   StateSet.InsertNode(newState, InsertPos);
367   recentlyAllocatedStates.push_back(newState);
368   return newState;
369 }
370 
371 const ProgramState *ProgramState::makeWithStore(const StoreRef &store) const {
372   ProgramState NewSt = *this;
373   NewSt.setStore(store);
374   return getStateManager().getPersistentState(NewSt);
375 }
376 
377 void ProgramState::setStore(const StoreRef &newStore) {
378   Store newStoreStore = newStore.getStore();
379   if (newStoreStore)
380     stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
381   if (store)
382     stateMgr->getStoreManager().decrementReferenceCount(store);
383   store = newStoreStore;
384 }
385 
386 //===----------------------------------------------------------------------===//
387 //  State pretty-printing.
388 //===----------------------------------------------------------------------===//
389 
390 void ProgramState::print(raw_ostream &Out,
391                          const char *NL, const char *Sep) const {
392   // Print the store.
393   ProgramStateManager &Mgr = getStateManager();
394   Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
395 
396   // Print out the environment.
397   Env.print(Out, NL, Sep);
398 
399   // Print out the constraints.
400   Mgr.getConstraintManager().print(this, Out, NL, Sep);
401 
402   // Print checker-specific data.
403   Mgr.getOwningEngine()->printState(Out, this, NL, Sep);
404 }
405 
406 void ProgramState::printDOT(raw_ostream &Out) const {
407   print(Out, "\\l", "\\|");
408 }
409 
410 void ProgramState::dump() const {
411   print(llvm::errs());
412 }
413 
414 //===----------------------------------------------------------------------===//
415 // Generic Data Map.
416 //===----------------------------------------------------------------------===//
417 
418 void *const* ProgramState::FindGDM(void *K) const {
419   return GDM.lookup(K);
420 }
421 
422 void*
423 ProgramStateManager::FindGDMContext(void *K,
424                                void *(*CreateContext)(llvm::BumpPtrAllocator&),
425                                void (*DeleteContext)(void*)) {
426 
427   std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
428   if (!p.first) {
429     p.first = CreateContext(Alloc);
430     p.second = DeleteContext;
431   }
432 
433   return p.first;
434 }
435 
436 const ProgramState *ProgramStateManager::addGDM(const ProgramState *St, void *Key, void *Data){
437   ProgramState::GenericDataMap M1 = St->getGDM();
438   ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
439 
440   if (M1 == M2)
441     return St;
442 
443   ProgramState NewSt = *St;
444   NewSt.GDM = M2;
445   return getPersistentState(NewSt);
446 }
447 
448 const ProgramState *ProgramStateManager::removeGDM(const ProgramState *state, void *Key) {
449   ProgramState::GenericDataMap OldM = state->getGDM();
450   ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
451 
452   if (NewM == OldM)
453     return state;
454 
455   ProgramState NewState = *state;
456   NewState.GDM = NewM;
457   return getPersistentState(NewState);
458 }
459 
460 void ScanReachableSymbols::anchor() { }
461 
462 bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
463   for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
464     if (!scan(*I))
465       return false;
466 
467   return true;
468 }
469 
470 bool ScanReachableSymbols::scan(const SymExpr *sym) {
471   unsigned &isVisited = visited[sym];
472   if (isVisited)
473     return true;
474   isVisited = 1;
475 
476   if (!visitor.VisitSymbol(sym))
477     return false;
478 
479   // TODO: should be rewritten using SymExpr::symbol_iterator.
480   switch (sym->getKind()) {
481     case SymExpr::RegionValueKind:
482     case SymExpr::ConjuredKind:
483     case SymExpr::DerivedKind:
484     case SymExpr::ExtentKind:
485     case SymExpr::MetadataKind:
486       break;
487     case SymExpr::CastSymbolKind:
488       return scan(cast<SymbolCast>(sym)->getOperand());
489     case SymExpr::SymIntKind:
490       return scan(cast<SymIntExpr>(sym)->getLHS());
491     case SymExpr::IntSymKind:
492       return scan(cast<IntSymExpr>(sym)->getRHS());
493     case SymExpr::SymSymKind: {
494       const SymSymExpr *x = cast<SymSymExpr>(sym);
495       return scan(x->getLHS()) && scan(x->getRHS());
496     }
497   }
498   return true;
499 }
500 
501 bool ScanReachableSymbols::scan(SVal val) {
502   if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
503     return scan(X->getRegion());
504 
505   if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val))
506     return scan(X->getLoc());
507 
508   if (SymbolRef Sym = val.getAsSymbol())
509     return scan(Sym);
510 
511   if (const SymExpr *Sym = val.getAsSymbolicExpression())
512     return scan(Sym);
513 
514   if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
515     return scan(*X);
516 
517   return true;
518 }
519 
520 bool ScanReachableSymbols::scan(const MemRegion *R) {
521   if (isa<MemSpaceRegion>(R))
522     return true;
523 
524   unsigned &isVisited = visited[R];
525   if (isVisited)
526     return true;
527   isVisited = 1;
528 
529   // If this is a symbolic region, visit the symbol for the region.
530   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
531     if (!visitor.VisitSymbol(SR->getSymbol()))
532       return false;
533 
534   // If this is a subregion, also visit the parent regions.
535   if (const SubRegion *SR = dyn_cast<SubRegion>(R))
536     if (!scan(SR->getSuperRegion()))
537       return false;
538 
539   // Now look at the binding to this region (if any).
540   if (!scan(state->getSValAsScalarOrLoc(R)))
541     return false;
542 
543   // Now look at the subregions.
544   if (!SRM.get())
545     SRM.reset(state->getStateManager().getStoreManager().
546                                            getSubRegionMap(state->getStore()));
547 
548   return SRM->iterSubRegions(R, *this);
549 }
550 
551 bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
552   ScanReachableSymbols S(this, visitor);
553   return S.scan(val);
554 }
555 
556 bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
557                                    SymbolVisitor &visitor) const {
558   ScanReachableSymbols S(this, visitor);
559   for ( ; I != E; ++I) {
560     if (!S.scan(*I))
561       return false;
562   }
563   return true;
564 }
565 
566 bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
567                                    const MemRegion * const *E,
568                                    SymbolVisitor &visitor) const {
569   ScanReachableSymbols S(this, visitor);
570   for ( ; I != E; ++I) {
571     if (!S.scan(*I))
572       return false;
573   }
574   return true;
575 }
576 
577 const ProgramState* ProgramState::addTaint(const Stmt *S,
578                                            const LocationContext *LCtx,
579                                            TaintTagType Kind) const {
580   if (const Expr *E = dyn_cast_or_null<Expr>(S))
581     S = E->IgnoreParens();
582 
583   SymbolRef Sym = getSVal(S, LCtx).getAsSymbol();
584   if (Sym)
585     return addTaint(Sym, Kind);
586 
587   const MemRegion *R = getSVal(S, LCtx).getAsRegion();
588   addTaint(R, Kind);
589 
590   // Cannot add taint, so just return the state.
591   return this;
592 }
593 
594 const ProgramState* ProgramState::addTaint(const MemRegion *R,
595                                            TaintTagType Kind) const {
596   if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R))
597     return addTaint(SR->getSymbol(), Kind);
598   return this;
599 }
600 
601 const ProgramState* ProgramState::addTaint(SymbolRef Sym,
602                                            TaintTagType Kind) const {
603   const ProgramState *NewState = set<TaintMap>(Sym, Kind);
604   assert(NewState);
605   return NewState;
606 }
607 
608 bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx,
609                              TaintTagType Kind) const {
610   if (const Expr *E = dyn_cast_or_null<Expr>(S))
611     S = E->IgnoreParens();
612 
613   SVal val = getSVal(S, LCtx);
614   return isTainted(val, Kind);
615 }
616 
617 bool ProgramState::isTainted(SVal V, TaintTagType Kind) const {
618   if (const SymExpr *Sym = V.getAsSymExpr())
619     return isTainted(Sym, Kind);
620   if (const MemRegion *Reg = V.getAsRegion())
621     return isTainted(Reg, Kind);
622   return false;
623 }
624 
625 bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const {
626   if (!Reg)
627     return false;
628 
629   // Element region (array element) is tainted if either the base or the offset
630   // are tainted.
631   if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg))
632     return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K);
633 
634   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg))
635     return isTainted(SR->getSymbol(), K);
636 
637   if (const SubRegion *ER = dyn_cast<SubRegion>(Reg))
638     return isTainted(ER->getSuperRegion(), K);
639 
640   return false;
641 }
642 
643 bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const {
644   if (!Sym)
645     return false;
646 
647   // Traverse all the symbols this symbol depends on to see if any are tainted.
648   bool Tainted = false;
649   for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
650        SI != SE; ++SI) {
651     assert(isa<SymbolData>(*SI));
652     const TaintTagType *Tag = get<TaintMap>(*SI);
653     Tainted = (Tag && *Tag == Kind);
654 
655     // If this is a SymbolDerived with a tainted parent, it's also tainted.
656     if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI))
657       Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind);
658 
659     // If memory region is tainted, data is also tainted.
660     if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI))
661       Tainted = Tainted || isTainted(SRV->getRegion(), Kind);
662 
663     if (Tainted)
664       return true;
665   }
666 
667   return Tainted;
668 }
669