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