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/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
16 #include "clang/Analysis/CFG.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 using namespace clang;
25 using namespace ento;
26 
27 namespace clang { namespace  ento {
28 /// Increments the number of times this state is referenced.
29 
30 void ProgramStateRetain(const ProgramState *state) {
31   ++const_cast<ProgramState*>(state)->refCount;
32 }
33 
34 /// Decrement the number of times this state is referenced.
35 void ProgramStateRelease(const ProgramState *state) {
36   assert(state->refCount > 0);
37   ProgramState *s = const_cast<ProgramState*>(state);
38   if (--s->refCount == 0) {
39     ProgramStateManager &Mgr = s->getStateManager();
40     Mgr.StateSet.RemoveNode(s);
41     s->~ProgramState();
42     Mgr.freeStates.push_back(s);
43   }
44 }
45 }}
46 
47 ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env,
48                  StoreRef st, GenericDataMap gdm)
49   : stateMgr(mgr),
50     Env(env),
51     store(st.getStore()),
52     GDM(gdm),
53     refCount(0) {
54   stateMgr->getStoreManager().incrementReferenceCount(store);
55 }
56 
57 ProgramState::ProgramState(const ProgramState &RHS)
58     : llvm::FoldingSetNode(),
59       stateMgr(RHS.stateMgr),
60       Env(RHS.Env),
61       store(RHS.store),
62       GDM(RHS.GDM),
63       refCount(0) {
64   stateMgr->getStoreManager().incrementReferenceCount(store);
65 }
66 
67 ProgramState::~ProgramState() {
68   if (store)
69     stateMgr->getStoreManager().decrementReferenceCount(store);
70 }
71 
72 int64_t ProgramState::getID() const {
73   Optional<int64_t> Out = getStateManager().Alloc.identifyObject(this);
74   assert(Out && "Wrong allocator used");
75   assert(*Out % alignof(ProgramState) == 0 && "Wrong alignment information");
76   return *Out / alignof(ProgramState);
77 }
78 
79 ProgramStateManager::ProgramStateManager(ASTContext &Ctx,
80                                          StoreManagerCreator CreateSMgr,
81                                          ConstraintManagerCreator CreateCMgr,
82                                          llvm::BumpPtrAllocator &alloc,
83                                          SubEngine *SubEng)
84   : Eng(SubEng), EnvMgr(alloc), GDMFactory(alloc),
85     svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
86     CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) {
87   StoreMgr = (*CreateSMgr)(*this);
88   ConstraintMgr = (*CreateCMgr)(*this, SubEng);
89 }
90 
91 
92 ProgramStateManager::~ProgramStateManager() {
93   for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
94        I!=E; ++I)
95     I->second.second(I->second.first);
96 }
97 
98 ProgramStateRef
99 ProgramStateManager::removeDeadBindings(ProgramStateRef state,
100                                    const StackFrameContext *LCtx,
101                                    SymbolReaper& SymReaper) {
102 
103   // This code essentially performs a "mark-and-sweep" of the VariableBindings.
104   // The roots are any Block-level exprs and Decls that our liveness algorithm
105   // tells us are live.  We then see what Decls they may reference, and keep
106   // those around.  This code more than likely can be made faster, and the
107   // frequency of which this method is called should be experimented with
108   // for optimum performance.
109   ProgramState NewState = *state;
110 
111   NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state);
112 
113   // Clean up the store.
114   StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx,
115                                                    SymReaper);
116   NewState.setStore(newStore);
117   SymReaper.setReapedStore(newStore);
118 
119   ProgramStateRef Result = getPersistentState(NewState);
120   return ConstraintMgr->removeDeadBindings(Result, SymReaper);
121 }
122 
123 ProgramStateRef ProgramState::bindLoc(Loc LV,
124                                       SVal V,
125                                       const LocationContext *LCtx,
126                                       bool notifyChanges) const {
127   ProgramStateManager &Mgr = getStateManager();
128   ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
129                                                              LV, V));
130   const MemRegion *MR = LV.getAsRegion();
131   if (MR && Mgr.getOwningEngine() && notifyChanges)
132     return Mgr.getOwningEngine()->processRegionChange(newState, MR, LCtx);
133 
134   return newState;
135 }
136 
137 ProgramStateRef
138 ProgramState::bindDefaultInitial(SVal loc, SVal V,
139                                  const LocationContext *LCtx) const {
140   ProgramStateManager &Mgr = getStateManager();
141   const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
142   const StoreRef &newStore = Mgr.StoreMgr->BindDefaultInitial(getStore(), R, V);
143   ProgramStateRef new_state = makeWithStore(newStore);
144   return Mgr.getOwningEngine()
145              ? Mgr.getOwningEngine()->processRegionChange(new_state, R, LCtx)
146              : new_state;
147 }
148 
149 ProgramStateRef
150 ProgramState::bindDefaultZero(SVal loc, const LocationContext *LCtx) const {
151   ProgramStateManager &Mgr = getStateManager();
152   const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
153   const StoreRef &newStore = Mgr.StoreMgr->BindDefaultZero(getStore(), R);
154   ProgramStateRef new_state = makeWithStore(newStore);
155   return Mgr.getOwningEngine()
156              ? Mgr.getOwningEngine()->processRegionChange(new_state, R, LCtx)
157              : new_state;
158 }
159 
160 typedef ArrayRef<const MemRegion *> RegionList;
161 typedef ArrayRef<SVal> ValueList;
162 
163 ProgramStateRef
164 ProgramState::invalidateRegions(RegionList Regions,
165                              const Expr *E, unsigned Count,
166                              const LocationContext *LCtx,
167                              bool CausedByPointerEscape,
168                              InvalidatedSymbols *IS,
169                              const CallEvent *Call,
170                              RegionAndSymbolInvalidationTraits *ITraits) const {
171   SmallVector<SVal, 8> Values;
172   for (RegionList::const_iterator I = Regions.begin(),
173                                   End = Regions.end(); I != End; ++I)
174     Values.push_back(loc::MemRegionVal(*I));
175 
176   return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
177                                IS, ITraits, Call);
178 }
179 
180 ProgramStateRef
181 ProgramState::invalidateRegions(ValueList Values,
182                              const Expr *E, unsigned Count,
183                              const LocationContext *LCtx,
184                              bool CausedByPointerEscape,
185                              InvalidatedSymbols *IS,
186                              const CallEvent *Call,
187                              RegionAndSymbolInvalidationTraits *ITraits) const {
188 
189   return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
190                                IS, ITraits, Call);
191 }
192 
193 ProgramStateRef
194 ProgramState::invalidateRegionsImpl(ValueList Values,
195                                     const Expr *E, unsigned Count,
196                                     const LocationContext *LCtx,
197                                     bool CausedByPointerEscape,
198                                     InvalidatedSymbols *IS,
199                                     RegionAndSymbolInvalidationTraits *ITraits,
200                                     const CallEvent *Call) const {
201   ProgramStateManager &Mgr = getStateManager();
202   SubEngine* Eng = Mgr.getOwningEngine();
203 
204   InvalidatedSymbols Invalidated;
205   if (!IS)
206     IS = &Invalidated;
207 
208   RegionAndSymbolInvalidationTraits ITraitsLocal;
209   if (!ITraits)
210     ITraits = &ITraitsLocal;
211 
212   if (Eng) {
213     StoreManager::InvalidatedRegions TopLevelInvalidated;
214     StoreManager::InvalidatedRegions Invalidated;
215     const StoreRef &newStore
216     = Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
217                                       *IS, *ITraits, &TopLevelInvalidated,
218                                       &Invalidated);
219 
220     ProgramStateRef newState = makeWithStore(newStore);
221 
222     if (CausedByPointerEscape) {
223       newState = Eng->notifyCheckersOfPointerEscape(newState, IS,
224                                                     TopLevelInvalidated,
225                                                     Invalidated, Call,
226                                                     *ITraits);
227     }
228 
229     return Eng->processRegionChanges(newState, IS, TopLevelInvalidated,
230                                      Invalidated, LCtx, Call);
231   }
232 
233   const StoreRef &newStore =
234   Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
235                                   *IS, *ITraits, nullptr, nullptr);
236   return makeWithStore(newStore);
237 }
238 
239 ProgramStateRef ProgramState::killBinding(Loc LV) const {
240   assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead.");
241 
242   Store OldStore = getStore();
243   const StoreRef &newStore =
244     getStateManager().StoreMgr->killBinding(OldStore, LV);
245 
246   if (newStore.getStore() == OldStore)
247     return this;
248 
249   return makeWithStore(newStore);
250 }
251 
252 ProgramStateRef
253 ProgramState::enterStackFrame(const CallEvent &Call,
254                               const StackFrameContext *CalleeCtx) const {
255   const StoreRef &NewStore =
256     getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
257   return makeWithStore(NewStore);
258 }
259 
260 SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
261   // We only want to do fetches from regions that we can actually bind
262   // values.  For example, SymbolicRegions of type 'id<...>' cannot
263   // have direct bindings (but their can be bindings on their subregions).
264   if (!R->isBoundable())
265     return UnknownVal();
266 
267   if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
268     QualType T = TR->getValueType();
269     if (Loc::isLocType(T) || T->isIntegralOrEnumerationType())
270       return getSVal(R);
271   }
272 
273   return UnknownVal();
274 }
275 
276 SVal ProgramState::getSVal(Loc location, QualType T) const {
277   SVal V = getRawSVal(location, T);
278 
279   // If 'V' is a symbolic value that is *perfectly* constrained to
280   // be a constant value, use that value instead to lessen the burden
281   // on later analysis stages (so we have less symbolic values to reason
282   // about).
283   // We only go into this branch if we can convert the APSInt value we have
284   // to the type of T, which is not always the case (e.g. for void).
285   if (!T.isNull() && (T->isIntegralOrEnumerationType() || Loc::isLocType(T))) {
286     if (SymbolRef sym = V.getAsSymbol()) {
287       if (const llvm::APSInt *Int = getStateManager()
288                                     .getConstraintManager()
289                                     .getSymVal(this, sym)) {
290         // FIXME: Because we don't correctly model (yet) sign-extension
291         // and truncation of symbolic values, we need to convert
292         // the integer value to the correct signedness and bitwidth.
293         //
294         // This shows up in the following:
295         //
296         //   char foo();
297         //   unsigned x = foo();
298         //   if (x == 54)
299         //     ...
300         //
301         //  The symbolic value stored to 'x' is actually the conjured
302         //  symbol for the call to foo(); the type of that symbol is 'char',
303         //  not unsigned.
304         const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
305 
306         if (V.getAs<Loc>())
307           return loc::ConcreteInt(NewV);
308         else
309           return nonloc::ConcreteInt(NewV);
310       }
311     }
312   }
313 
314   return V;
315 }
316 
317 ProgramStateRef ProgramState::BindExpr(const Stmt *S,
318                                            const LocationContext *LCtx,
319                                            SVal V, bool Invalidate) const{
320   Environment NewEnv =
321     getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
322                                       Invalidate);
323   if (NewEnv == Env)
324     return this;
325 
326   ProgramState NewSt = *this;
327   NewSt.Env = NewEnv;
328   return getStateManager().getPersistentState(NewSt);
329 }
330 
331 ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
332                                       DefinedOrUnknownSVal UpperBound,
333                                       bool Assumption,
334                                       QualType indexTy) const {
335   if (Idx.isUnknown() || UpperBound.isUnknown())
336     return this;
337 
338   // Build an expression for 0 <= Idx < UpperBound.
339   // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
340   // FIXME: This should probably be part of SValBuilder.
341   ProgramStateManager &SM = getStateManager();
342   SValBuilder &svalBuilder = SM.getSValBuilder();
343   ASTContext &Ctx = svalBuilder.getContext();
344 
345   // Get the offset: the minimum value of the array index type.
346   BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
347   if (indexTy.isNull())
348     indexTy = svalBuilder.getArrayIndexType();
349   nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
350 
351   // Adjust the index.
352   SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
353                                         Idx.castAs<NonLoc>(), Min, indexTy);
354   if (newIdx.isUnknownOrUndef())
355     return this;
356 
357   // Adjust the upper bound.
358   SVal newBound =
359     svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
360                             Min, indexTy);
361 
362   if (newBound.isUnknownOrUndef())
363     return this;
364 
365   // Build the actual comparison.
366   SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(),
367                                          newBound.castAs<NonLoc>(), Ctx.IntTy);
368   if (inBound.isUnknownOrUndef())
369     return this;
370 
371   // Finally, let the constraint manager take care of it.
372   ConstraintManager &CM = SM.getConstraintManager();
373   return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption);
374 }
375 
376 ConditionTruthVal ProgramState::isNonNull(SVal V) const {
377   ConditionTruthVal IsNull = isNull(V);
378   if (IsNull.isUnderconstrained())
379     return IsNull;
380   return ConditionTruthVal(!IsNull.getValue());
381 }
382 
383 ConditionTruthVal ProgramState::areEqual(SVal Lhs, SVal Rhs) const {
384   return stateMgr->getSValBuilder().areEqual(this, Lhs, Rhs);
385 }
386 
387 ConditionTruthVal ProgramState::isNull(SVal V) const {
388   if (V.isZeroConstant())
389     return true;
390 
391   if (V.isConstant())
392     return false;
393 
394   SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
395   if (!Sym)
396     return ConditionTruthVal();
397 
398   return getStateManager().ConstraintMgr->isNull(this, Sym);
399 }
400 
401 ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
402   ProgramState State(this,
403                 EnvMgr.getInitialEnvironment(),
404                 StoreMgr->getInitialStore(InitLoc),
405                 GDMFactory.getEmptyMap());
406 
407   return getPersistentState(State);
408 }
409 
410 ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
411                                                      ProgramStateRef FromState,
412                                                      ProgramStateRef GDMState) {
413   ProgramState NewState(*FromState);
414   NewState.GDM = GDMState->GDM;
415   return getPersistentState(NewState);
416 }
417 
418 ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
419 
420   llvm::FoldingSetNodeID ID;
421   State.Profile(ID);
422   void *InsertPos;
423 
424   if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
425     return I;
426 
427   ProgramState *newState = nullptr;
428   if (!freeStates.empty()) {
429     newState = freeStates.back();
430     freeStates.pop_back();
431   }
432   else {
433     newState = (ProgramState*) Alloc.Allocate<ProgramState>();
434   }
435   new (newState) ProgramState(State);
436   StateSet.InsertNode(newState, InsertPos);
437   return newState;
438 }
439 
440 ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
441   ProgramState NewSt(*this);
442   NewSt.setStore(store);
443   return getStateManager().getPersistentState(NewSt);
444 }
445 
446 void ProgramState::setStore(const StoreRef &newStore) {
447   Store newStoreStore = newStore.getStore();
448   if (newStoreStore)
449     stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
450   if (store)
451     stateMgr->getStoreManager().decrementReferenceCount(store);
452   store = newStoreStore;
453 }
454 
455 //===----------------------------------------------------------------------===//
456 //  State pretty-printing.
457 //===----------------------------------------------------------------------===//
458 
459 void ProgramState::print(raw_ostream &Out,
460                          const char *NL, const char *Sep,
461                          const LocationContext *LC) const {
462   // Print the store.
463   ProgramStateManager &Mgr = getStateManager();
464   const ASTContext &Context = getStateManager().getContext();
465   Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
466 
467   // Print out the environment.
468   Env.print(Out, NL, Sep, Context, LC);
469 
470   // Print out the constraints.
471   Mgr.getConstraintManager().print(this, Out, NL, Sep);
472 
473   // Print out the tracked dynamic types.
474   printDynamicTypeInfo(this, Out, NL, Sep);
475 
476   // Print out tainted symbols.
477   printTaint(Out, NL, Sep);
478 
479   // Print checker-specific data.
480   Mgr.getOwningEngine()->printState(Out, this, NL, Sep, LC);
481 }
482 
483 void ProgramState::printDOT(raw_ostream &Out,
484                             const LocationContext *LC) const {
485   print(Out, "\\l", "\\|", LC);
486 }
487 
488 LLVM_DUMP_METHOD void ProgramState::dump() const {
489   print(llvm::errs());
490 }
491 
492 void ProgramState::printTaint(raw_ostream &Out,
493                               const char *NL, const char *Sep) const {
494   TaintMapImpl TM = get<TaintMap>();
495 
496   if (!TM.isEmpty())
497     Out <<"Tainted symbols:" << NL;
498 
499   for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) {
500     Out << I->first << " : " << I->second << NL;
501   }
502 }
503 
504 void ProgramState::dumpTaint() const {
505   printTaint(llvm::errs());
506 }
507 
508 AnalysisManager& ProgramState::getAnalysisManager() const {
509   return stateMgr->getOwningEngine()->getAnalysisManager();
510 }
511 
512 //===----------------------------------------------------------------------===//
513 // Generic Data Map.
514 //===----------------------------------------------------------------------===//
515 
516 void *const* ProgramState::FindGDM(void *K) const {
517   return GDM.lookup(K);
518 }
519 
520 void*
521 ProgramStateManager::FindGDMContext(void *K,
522                                void *(*CreateContext)(llvm::BumpPtrAllocator&),
523                                void (*DeleteContext)(void*)) {
524 
525   std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
526   if (!p.first) {
527     p.first = CreateContext(Alloc);
528     p.second = DeleteContext;
529   }
530 
531   return p.first;
532 }
533 
534 ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){
535   ProgramState::GenericDataMap M1 = St->getGDM();
536   ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
537 
538   if (M1 == M2)
539     return St;
540 
541   ProgramState NewSt = *St;
542   NewSt.GDM = M2;
543   return getPersistentState(NewSt);
544 }
545 
546 ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) {
547   ProgramState::GenericDataMap OldM = state->getGDM();
548   ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
549 
550   if (NewM == OldM)
551     return state;
552 
553   ProgramState NewState = *state;
554   NewState.GDM = NewM;
555   return getPersistentState(NewState);
556 }
557 
558 bool ScanReachableSymbols::scan(nonloc::LazyCompoundVal val) {
559   bool wasVisited = !visited.insert(val.getCVData()).second;
560   if (wasVisited)
561     return true;
562 
563   StoreManager &StoreMgr = state->getStateManager().getStoreManager();
564   // FIXME: We don't really want to use getBaseRegion() here because pointer
565   // arithmetic doesn't apply, but scanReachableSymbols only accepts base
566   // regions right now.
567   const MemRegion *R = val.getRegion()->getBaseRegion();
568   return StoreMgr.scanReachableSymbols(val.getStore(), R, *this);
569 }
570 
571 bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
572   for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
573     if (!scan(*I))
574       return false;
575 
576   return true;
577 }
578 
579 bool ScanReachableSymbols::scan(const SymExpr *sym) {
580   for (SymExpr::symbol_iterator SI = sym->symbol_begin(),
581                                 SE = sym->symbol_end();
582        SI != SE; ++SI) {
583     bool wasVisited = !visited.insert(*SI).second;
584     if (wasVisited)
585       continue;
586 
587     if (!visitor.VisitSymbol(*SI))
588       return false;
589   }
590 
591   return true;
592 }
593 
594 bool ScanReachableSymbols::scan(SVal val) {
595   if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>())
596     return scan(X->getRegion());
597 
598   if (Optional<nonloc::LazyCompoundVal> X =
599           val.getAs<nonloc::LazyCompoundVal>())
600     return scan(*X);
601 
602   if (Optional<nonloc::LocAsInteger> X = val.getAs<nonloc::LocAsInteger>())
603     return scan(X->getLoc());
604 
605   if (SymbolRef Sym = val.getAsSymbol())
606     return scan(Sym);
607 
608   if (const SymExpr *Sym = val.getAsSymbolicExpression())
609     return scan(Sym);
610 
611   if (Optional<nonloc::CompoundVal> X = val.getAs<nonloc::CompoundVal>())
612     return scan(*X);
613 
614   return true;
615 }
616 
617 bool ScanReachableSymbols::scan(const MemRegion *R) {
618   if (isa<MemSpaceRegion>(R))
619     return true;
620 
621   bool wasVisited = !visited.insert(R).second;
622   if (wasVisited)
623     return true;
624 
625   if (!visitor.VisitMemRegion(R))
626     return false;
627 
628   // If this is a symbolic region, visit the symbol for the region.
629   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
630     if (!visitor.VisitSymbol(SR->getSymbol()))
631       return false;
632 
633   // If this is a subregion, also visit the parent regions.
634   if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
635     const MemRegion *Super = SR->getSuperRegion();
636     if (!scan(Super))
637       return false;
638 
639     // When we reach the topmost region, scan all symbols in it.
640     if (isa<MemSpaceRegion>(Super)) {
641       StoreManager &StoreMgr = state->getStateManager().getStoreManager();
642       if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
643         return false;
644     }
645   }
646 
647   // Regions captured by a block are also implicitly reachable.
648   if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
649     BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
650                                               E = BDR->referenced_vars_end();
651     for ( ; I != E; ++I) {
652       if (!scan(I.getCapturedRegion()))
653         return false;
654     }
655   }
656 
657   return true;
658 }
659 
660 bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
661   ScanReachableSymbols S(this, visitor);
662   return S.scan(val);
663 }
664 
665 bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
666                                    SymbolVisitor &visitor) const {
667   ScanReachableSymbols S(this, visitor);
668   for ( ; I != E; ++I) {
669     if (!S.scan(*I))
670       return false;
671   }
672   return true;
673 }
674 
675 bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
676                                    const MemRegion * const *E,
677                                    SymbolVisitor &visitor) const {
678   ScanReachableSymbols S(this, visitor);
679   for ( ; I != E; ++I) {
680     if (!S.scan(*I))
681       return false;
682   }
683   return true;
684 }
685 
686 ProgramStateRef ProgramState::addTaint(const Stmt *S,
687                                            const LocationContext *LCtx,
688                                            TaintTagType Kind) const {
689   if (const Expr *E = dyn_cast_or_null<Expr>(S))
690     S = E->IgnoreParens();
691 
692   return addTaint(getSVal(S, LCtx), Kind);
693 }
694 
695 ProgramStateRef ProgramState::addTaint(SVal V,
696                                        TaintTagType Kind) const {
697   SymbolRef Sym = V.getAsSymbol();
698   if (Sym)
699     return addTaint(Sym, Kind);
700 
701   // If the SVal represents a structure, try to mass-taint all values within the
702   // structure. For now it only works efficiently on lazy compound values that
703   // were conjured during a conservative evaluation of a function - either as
704   // return values of functions that return structures or arrays by value, or as
705   // values of structures or arrays passed into the function by reference,
706   // directly or through pointer aliasing. Such lazy compound values are
707   // characterized by having exactly one binding in their captured store within
708   // their parent region, which is a conjured symbol default-bound to the base
709   // region of the parent region.
710   if (auto LCV = V.getAs<nonloc::LazyCompoundVal>()) {
711     if (Optional<SVal> binding = getStateManager().StoreMgr->getDefaultBinding(*LCV)) {
712       if (SymbolRef Sym = binding->getAsSymbol())
713         return addPartialTaint(Sym, LCV->getRegion(), Kind);
714     }
715   }
716 
717   const MemRegion *R = V.getAsRegion();
718   return addTaint(R, Kind);
719 }
720 
721 ProgramStateRef ProgramState::addTaint(const MemRegion *R,
722                                            TaintTagType Kind) const {
723   if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R))
724     return addTaint(SR->getSymbol(), Kind);
725   return this;
726 }
727 
728 ProgramStateRef ProgramState::addTaint(SymbolRef Sym,
729                                            TaintTagType Kind) const {
730   // If this is a symbol cast, remove the cast before adding the taint. Taint
731   // is cast agnostic.
732   while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym))
733     Sym = SC->getOperand();
734 
735   ProgramStateRef NewState = set<TaintMap>(Sym, Kind);
736   assert(NewState);
737   return NewState;
738 }
739 
740 ProgramStateRef ProgramState::addPartialTaint(SymbolRef ParentSym,
741                                               const SubRegion *SubRegion,
742                                               TaintTagType Kind) const {
743   // Ignore partial taint if the entire parent symbol is already tainted.
744   if (contains<TaintMap>(ParentSym) && *get<TaintMap>(ParentSym) == Kind)
745     return this;
746 
747   // Partial taint applies if only a portion of the symbol is tainted.
748   if (SubRegion == SubRegion->getBaseRegion())
749     return addTaint(ParentSym, Kind);
750 
751   const TaintedSubRegions *SavedRegs = get<DerivedSymTaint>(ParentSym);
752   TaintedSubRegions Regs =
753       SavedRegs ? *SavedRegs : stateMgr->TSRFactory.getEmptyMap();
754 
755   Regs = stateMgr->TSRFactory.add(Regs, SubRegion, Kind);
756   ProgramStateRef NewState = set<DerivedSymTaint>(ParentSym, Regs);
757   assert(NewState);
758   return NewState;
759 }
760 
761 bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx,
762                              TaintTagType Kind) const {
763   if (const Expr *E = dyn_cast_or_null<Expr>(S))
764     S = E->IgnoreParens();
765 
766   SVal val = getSVal(S, LCtx);
767   return isTainted(val, Kind);
768 }
769 
770 bool ProgramState::isTainted(SVal V, TaintTagType Kind) const {
771   if (const SymExpr *Sym = V.getAsSymExpr())
772     return isTainted(Sym, Kind);
773   if (const MemRegion *Reg = V.getAsRegion())
774     return isTainted(Reg, Kind);
775   return false;
776 }
777 
778 bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const {
779   if (!Reg)
780     return false;
781 
782   // Element region (array element) is tainted if either the base or the offset
783   // are tainted.
784   if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg))
785     return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K);
786 
787   if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg))
788     return isTainted(SR->getSymbol(), K);
789 
790   if (const SubRegion *ER = dyn_cast<SubRegion>(Reg))
791     return isTainted(ER->getSuperRegion(), K);
792 
793   return false;
794 }
795 
796 bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const {
797   if (!Sym)
798     return false;
799 
800   // Traverse all the symbols this symbol depends on to see if any are tainted.
801   for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
802        SI != SE; ++SI) {
803     if (!isa<SymbolData>(*SI))
804       continue;
805 
806     if (const TaintTagType *Tag = get<TaintMap>(*SI)) {
807       if (*Tag == Kind)
808         return true;
809     }
810 
811     if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI)) {
812       // If this is a SymbolDerived with a tainted parent, it's also tainted.
813       if (isTainted(SD->getParentSymbol(), Kind))
814         return true;
815 
816       // If this is a SymbolDerived with the same parent symbol as another
817       // tainted SymbolDerived and a region that's a sub-region of that tainted
818       // symbol, it's also tainted.
819       if (const TaintedSubRegions *Regs =
820               get<DerivedSymTaint>(SD->getParentSymbol())) {
821         const TypedValueRegion *R = SD->getRegion();
822         for (auto I : *Regs) {
823           // FIXME: The logic to identify tainted regions could be more
824           // complete. For example, this would not currently identify
825           // overlapping fields in a union as tainted. To identify this we can
826           // check for overlapping/nested byte offsets.
827           if (Kind == I.second && R->isSubRegionOf(I.first))
828             return true;
829         }
830       }
831     }
832 
833     // If memory region is tainted, data is also tainted.
834     if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI)) {
835       if (isTainted(SRV->getRegion(), Kind))
836         return true;
837     }
838 
839     // If this is a SymbolCast from a tainted value, it's also tainted.
840     if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI)) {
841       if (isTainted(SC->getOperand(), Kind))
842         return true;
843     }
844   }
845 
846   return false;
847 }
848 
849