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,
116 const LocationContext *LCtx,
117 bool notifyChanges) const {
118 ProgramStateManager &Mgr = getStateManager();
119 ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
121 const MemRegion *MR = LV.getAsRegion();
122 if (MR && Mgr.getOwningEngine() && notifyChanges)
123 return Mgr.getOwningEngine()->processRegionChange(newState, MR, LCtx);
128 ProgramStateRef ProgramState::bindDefault(SVal loc,
130 const LocationContext *LCtx) const {
131 ProgramStateManager &Mgr = getStateManager();
132 const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
133 const StoreRef &newStore = Mgr.StoreMgr->BindDefault(getStore(), R, V);
134 ProgramStateRef new_state = makeWithStore(newStore);
135 return Mgr.getOwningEngine() ?
136 Mgr.getOwningEngine()->processRegionChange(new_state, R, LCtx) :
140 typedef ArrayRef<const MemRegion *> RegionList;
141 typedef ArrayRef<SVal> ValueList;
144 ProgramState::invalidateRegions(RegionList Regions,
145 const Expr *E, unsigned Count,
146 const LocationContext *LCtx,
147 bool CausedByPointerEscape,
148 InvalidatedSymbols *IS,
149 const CallEvent *Call,
150 RegionAndSymbolInvalidationTraits *ITraits) const {
151 SmallVector<SVal, 8> Values;
152 for (RegionList::const_iterator I = Regions.begin(),
153 End = Regions.end(); I != End; ++I)
154 Values.push_back(loc::MemRegionVal(*I));
156 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
161 ProgramState::invalidateRegions(ValueList Values,
162 const Expr *E, unsigned Count,
163 const LocationContext *LCtx,
164 bool CausedByPointerEscape,
165 InvalidatedSymbols *IS,
166 const CallEvent *Call,
167 RegionAndSymbolInvalidationTraits *ITraits) const {
169 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
174 ProgramState::invalidateRegionsImpl(ValueList Values,
175 const Expr *E, unsigned Count,
176 const LocationContext *LCtx,
177 bool CausedByPointerEscape,
178 InvalidatedSymbols *IS,
179 RegionAndSymbolInvalidationTraits *ITraits,
180 const CallEvent *Call) const {
181 ProgramStateManager &Mgr = getStateManager();
182 SubEngine* Eng = Mgr.getOwningEngine();
184 InvalidatedSymbols Invalidated;
188 RegionAndSymbolInvalidationTraits ITraitsLocal;
190 ITraits = &ITraitsLocal;
193 StoreManager::InvalidatedRegions TopLevelInvalidated;
194 StoreManager::InvalidatedRegions Invalidated;
195 const StoreRef &newStore
196 = Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
197 *IS, *ITraits, &TopLevelInvalidated,
200 ProgramStateRef newState = makeWithStore(newStore);
202 if (CausedByPointerEscape) {
203 newState = Eng->notifyCheckersOfPointerEscape(newState, IS,
209 return Eng->processRegionChanges(newState, IS, TopLevelInvalidated,
210 Invalidated, LCtx, Call);
213 const StoreRef &newStore =
214 Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
215 *IS, *ITraits, nullptr, nullptr);
216 return makeWithStore(newStore);
219 ProgramStateRef ProgramState::killBinding(Loc LV) const {
220 assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead.");
222 Store OldStore = getStore();
223 const StoreRef &newStore =
224 getStateManager().StoreMgr->killBinding(OldStore, LV);
226 if (newStore.getStore() == OldStore)
229 return makeWithStore(newStore);
233 ProgramState::enterStackFrame(const CallEvent &Call,
234 const StackFrameContext *CalleeCtx) const {
235 const StoreRef &NewStore =
236 getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
237 return makeWithStore(NewStore);
240 SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
241 // We only want to do fetches from regions that we can actually bind
242 // values. For example, SymbolicRegions of type 'id<...>' cannot
243 // have direct bindings (but their can be bindings on their subregions).
244 if (!R->isBoundable())
247 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
248 QualType T = TR->getValueType();
249 if (Loc::isLocType(T) || T->isIntegralOrEnumerationType())
256 SVal ProgramState::getSVal(Loc location, QualType T) const {
257 SVal V = getRawSVal(cast<Loc>(location), T);
259 // If 'V' is a symbolic value that is *perfectly* constrained to
260 // be a constant value, use that value instead to lessen the burden
261 // on later analysis stages (so we have less symbolic values to reason
263 // We only go into this branch if we can convert the APSInt value we have
264 // to the type of T, which is not always the case (e.g. for void).
265 if (!T.isNull() && (T->isIntegralOrEnumerationType() || Loc::isLocType(T))) {
266 if (SymbolRef sym = V.getAsSymbol()) {
267 if (const llvm::APSInt *Int = getStateManager()
268 .getConstraintManager()
269 .getSymVal(this, sym)) {
270 // FIXME: Because we don't correctly model (yet) sign-extension
271 // and truncation of symbolic values, we need to convert
272 // the integer value to the correct signedness and bitwidth.
274 // This shows up in the following:
277 // unsigned x = foo();
281 // The symbolic value stored to 'x' is actually the conjured
282 // symbol for the call to foo(); the type of that symbol is 'char',
284 const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
287 return loc::ConcreteInt(NewV);
289 return nonloc::ConcreteInt(NewV);
297 ProgramStateRef ProgramState::BindExpr(const Stmt *S,
298 const LocationContext *LCtx,
299 SVal V, bool Invalidate) const{
301 getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
306 ProgramState NewSt = *this;
308 return getStateManager().getPersistentState(NewSt);
311 ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
312 DefinedOrUnknownSVal UpperBound,
314 QualType indexTy) const {
315 if (Idx.isUnknown() || UpperBound.isUnknown())
318 // Build an expression for 0 <= Idx < UpperBound.
319 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
320 // FIXME: This should probably be part of SValBuilder.
321 ProgramStateManager &SM = getStateManager();
322 SValBuilder &svalBuilder = SM.getSValBuilder();
323 ASTContext &Ctx = svalBuilder.getContext();
325 // Get the offset: the minimum value of the array index type.
326 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
327 // FIXME: This should be using ValueManager::ArrayindexTy...somehow.
328 if (indexTy.isNull())
330 nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
333 SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
334 Idx.castAs<NonLoc>(), Min, indexTy);
335 if (newIdx.isUnknownOrUndef())
338 // Adjust the upper bound.
340 svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
343 if (newBound.isUnknownOrUndef())
346 // Build the actual comparison.
347 SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(),
348 newBound.castAs<NonLoc>(), Ctx.IntTy);
349 if (inBound.isUnknownOrUndef())
352 // Finally, let the constraint manager take care of it.
353 ConstraintManager &CM = SM.getConstraintManager();
354 return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption);
357 ConditionTruthVal ProgramState::isNull(SVal V) const {
358 if (V.isZeroConstant())
364 SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
366 return ConditionTruthVal();
368 return getStateManager().ConstraintMgr->isNull(this, Sym);
371 ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
372 ProgramState State(this,
373 EnvMgr.getInitialEnvironment(),
374 StoreMgr->getInitialStore(InitLoc),
375 GDMFactory.getEmptyMap());
377 return getPersistentState(State);
380 ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
381 ProgramStateRef FromState,
382 ProgramStateRef GDMState) {
383 ProgramState NewState(*FromState);
384 NewState.GDM = GDMState->GDM;
385 return getPersistentState(NewState);
388 ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
390 llvm::FoldingSetNodeID ID;
394 if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
397 ProgramState *newState = nullptr;
398 if (!freeStates.empty()) {
399 newState = freeStates.back();
400 freeStates.pop_back();
403 newState = (ProgramState*) Alloc.Allocate<ProgramState>();
405 new (newState) ProgramState(State);
406 StateSet.InsertNode(newState, InsertPos);
410 ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
411 ProgramState NewSt(*this);
412 NewSt.setStore(store);
413 return getStateManager().getPersistentState(NewSt);
416 void ProgramState::setStore(const StoreRef &newStore) {
417 Store newStoreStore = newStore.getStore();
419 stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
421 stateMgr->getStoreManager().decrementReferenceCount(store);
422 store = newStoreStore;
425 //===----------------------------------------------------------------------===//
426 // State pretty-printing.
427 //===----------------------------------------------------------------------===//
429 void ProgramState::print(raw_ostream &Out,
430 const char *NL, const char *Sep) const {
432 ProgramStateManager &Mgr = getStateManager();
433 Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
435 // Print out the environment.
436 Env.print(Out, NL, Sep);
438 // Print out the constraints.
439 Mgr.getConstraintManager().print(this, Out, NL, Sep);
441 // Print checker-specific data.
442 Mgr.getOwningEngine()->printState(Out, this, NL, Sep);
445 void ProgramState::printDOT(raw_ostream &Out) const {
446 print(Out, "\\l", "\\|");
449 LLVM_DUMP_METHOD void ProgramState::dump() const {
453 void ProgramState::printTaint(raw_ostream &Out,
454 const char *NL, const char *Sep) const {
455 TaintMapImpl TM = get<TaintMap>();
458 Out <<"Tainted Symbols:" << NL;
460 for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) {
461 Out << I->first << " : " << I->second << NL;
465 void ProgramState::dumpTaint() const {
466 printTaint(llvm::errs());
469 //===----------------------------------------------------------------------===//
471 //===----------------------------------------------------------------------===//
473 void *const* ProgramState::FindGDM(void *K) const {
474 return GDM.lookup(K);
478 ProgramStateManager::FindGDMContext(void *K,
479 void *(*CreateContext)(llvm::BumpPtrAllocator&),
480 void (*DeleteContext)(void*)) {
482 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
484 p.first = CreateContext(Alloc);
485 p.second = DeleteContext;
491 ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){
492 ProgramState::GenericDataMap M1 = St->getGDM();
493 ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
498 ProgramState NewSt = *St;
500 return getPersistentState(NewSt);
503 ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) {
504 ProgramState::GenericDataMap OldM = state->getGDM();
505 ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
510 ProgramState NewState = *state;
512 return getPersistentState(NewState);
515 bool ScanReachableSymbols::scan(nonloc::LazyCompoundVal val) {
516 bool wasVisited = !visited.insert(val.getCVData()).second;
520 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
521 // FIXME: We don't really want to use getBaseRegion() here because pointer
522 // arithmetic doesn't apply, but scanReachableSymbols only accepts base
523 // regions right now.
524 const MemRegion *R = val.getRegion()->getBaseRegion();
525 return StoreMgr.scanReachableSymbols(val.getStore(), R, *this);
528 bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
529 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
536 bool ScanReachableSymbols::scan(const SymExpr *sym) {
537 for (SymExpr::symbol_iterator SI = sym->symbol_begin(),
538 SE = sym->symbol_end();
540 bool wasVisited = !visited.insert(*SI).second;
544 if (!visitor.VisitSymbol(*SI))
551 bool ScanReachableSymbols::scan(SVal val) {
552 if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>())
553 return scan(X->getRegion());
555 if (Optional<nonloc::LazyCompoundVal> X =
556 val.getAs<nonloc::LazyCompoundVal>())
559 if (Optional<nonloc::LocAsInteger> X = val.getAs<nonloc::LocAsInteger>())
560 return scan(X->getLoc());
562 if (SymbolRef Sym = val.getAsSymbol())
565 if (const SymExpr *Sym = val.getAsSymbolicExpression())
568 if (Optional<nonloc::CompoundVal> X = val.getAs<nonloc::CompoundVal>())
574 bool ScanReachableSymbols::scan(const MemRegion *R) {
575 if (isa<MemSpaceRegion>(R))
578 bool wasVisited = !visited.insert(R).second;
582 if (!visitor.VisitMemRegion(R))
585 // If this is a symbolic region, visit the symbol for the region.
586 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
587 if (!visitor.VisitSymbol(SR->getSymbol()))
590 // If this is a subregion, also visit the parent regions.
591 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
592 const MemRegion *Super = SR->getSuperRegion();
596 // When we reach the topmost region, scan all symbols in it.
597 if (isa<MemSpaceRegion>(Super)) {
598 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
599 if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
604 // Regions captured by a block are also implicitly reachable.
605 if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
606 BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
607 E = BDR->referenced_vars_end();
608 for ( ; I != E; ++I) {
609 if (!scan(I.getCapturedRegion()))
617 bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
618 ScanReachableSymbols S(this, visitor);
622 bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
623 SymbolVisitor &visitor) const {
624 ScanReachableSymbols S(this, visitor);
625 for ( ; I != E; ++I) {
632 bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
633 const MemRegion * const *E,
634 SymbolVisitor &visitor) const {
635 ScanReachableSymbols S(this, visitor);
636 for ( ; I != E; ++I) {
643 ProgramStateRef ProgramState::addTaint(const Stmt *S,
644 const LocationContext *LCtx,
645 TaintTagType Kind) const {
646 if (const Expr *E = dyn_cast_or_null<Expr>(S))
647 S = E->IgnoreParens();
649 return addTaint(getSVal(S, LCtx), Kind);
652 ProgramStateRef ProgramState::addTaint(SVal V,
653 TaintTagType Kind) const {
654 SymbolRef Sym = V.getAsSymbol();
656 return addTaint(Sym, Kind);
658 // If the SVal represents a structure, try to mass-taint all values within the
659 // structure. For now it only works efficiently on lazy compound values that
660 // were conjured during a conservative evaluation of a function - either as
661 // return values of functions that return structures or arrays by value, or as
662 // values of structures or arrays passed into the function by reference,
663 // directly or through pointer aliasing. Such lazy compound values are
664 // characterized by having exactly one binding in their captured store within
665 // their parent region, which is a conjured symbol default-bound to the base
666 // region of the parent region.
667 if (auto LCV = V.getAs<nonloc::LazyCompoundVal>()) {
668 if (Optional<SVal> binding = getStateManager().StoreMgr->getDefaultBinding(*LCV)) {
669 if (SymbolRef Sym = binding->getAsSymbol())
670 return addPartialTaint(Sym, LCV->getRegion(), Kind);
674 const MemRegion *R = V.getAsRegion();
675 return addTaint(R, Kind);
678 ProgramStateRef ProgramState::addTaint(const MemRegion *R,
679 TaintTagType Kind) const {
680 if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R))
681 return addTaint(SR->getSymbol(), Kind);
685 ProgramStateRef ProgramState::addTaint(SymbolRef Sym,
686 TaintTagType Kind) const {
687 // If this is a symbol cast, remove the cast before adding the taint. Taint
689 while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym))
690 Sym = SC->getOperand();
692 ProgramStateRef NewState = set<TaintMap>(Sym, Kind);
697 ProgramStateRef ProgramState::addPartialTaint(SymbolRef ParentSym,
698 const SubRegion *SubRegion,
699 TaintTagType Kind) const {
700 // Ignore partial taint if the entire parent symbol is already tainted.
701 if (contains<TaintMap>(ParentSym) && *get<TaintMap>(ParentSym) == Kind)
704 // Partial taint applies if only a portion of the symbol is tainted.
705 if (SubRegion == SubRegion->getBaseRegion())
706 return addTaint(ParentSym, Kind);
708 const TaintedSubRegions *SavedRegs = get<DerivedSymTaint>(ParentSym);
709 TaintedSubRegions Regs =
710 SavedRegs ? *SavedRegs : stateMgr->TSRFactory.getEmptyMap();
712 Regs = stateMgr->TSRFactory.add(Regs, SubRegion, Kind);
713 ProgramStateRef NewState = set<DerivedSymTaint>(ParentSym, Regs);
718 bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx,
719 TaintTagType Kind) const {
720 if (const Expr *E = dyn_cast_or_null<Expr>(S))
721 S = E->IgnoreParens();
723 SVal val = getSVal(S, LCtx);
724 return isTainted(val, Kind);
727 bool ProgramState::isTainted(SVal V, TaintTagType Kind) const {
728 if (const SymExpr *Sym = V.getAsSymExpr())
729 return isTainted(Sym, Kind);
730 if (const MemRegion *Reg = V.getAsRegion())
731 return isTainted(Reg, Kind);
735 bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const {
739 // Element region (array element) is tainted if either the base or the offset
741 if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg))
742 return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K);
744 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg))
745 return isTainted(SR->getSymbol(), K);
747 if (const SubRegion *ER = dyn_cast<SubRegion>(Reg))
748 return isTainted(ER->getSuperRegion(), K);
753 bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const {
757 // Traverse all the symbols this symbol depends on to see if any are tainted.
758 for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
760 if (!isa<SymbolData>(*SI))
763 if (const TaintTagType *Tag = get<TaintMap>(*SI)) {
768 if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI)) {
769 // If this is a SymbolDerived with a tainted parent, it's also tainted.
770 if (isTainted(SD->getParentSymbol(), Kind))
773 // If this is a SymbolDerived with the same parent symbol as another
774 // tainted SymbolDerived and a region that's a sub-region of that tainted
775 // symbol, it's also tainted.
776 if (const TaintedSubRegions *Regs =
777 get<DerivedSymTaint>(SD->getParentSymbol())) {
778 const TypedValueRegion *R = SD->getRegion();
779 for (auto I : *Regs) {
780 // FIXME: The logic to identify tainted regions could be more
781 // complete. For example, this would not currently identify
782 // overlapping fields in a union as tainted. To identify this we can
783 // check for overlapping/nested byte offsets.
784 if (Kind == I.second &&
785 (R == I.first || R->isSubRegionOf(I.first)))
791 // If memory region is tainted, data is also tainted.
792 if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI)) {
793 if (isTainted(SRV->getRegion(), Kind))
797 // If this is a SymbolCast from a tainted value, it's also tainted.
798 if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI)) {
799 if (isTainted(SC->getOperand(), Kind))