clang  7.0.0svn
ProgramState.cpp
Go to the documentation of this file.
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 
15 #include "clang/Analysis/CFG.h"
20 #include "llvm/Support/raw_ostream.h"
21 
22 using namespace clang;
23 using namespace ento;
24 
25 namespace clang { namespace ento {
26 /// Increments the number of times this state is referenced.
27 
29  ++const_cast<ProgramState*>(state)->refCount;
30 }
31 
32 /// Decrement the number of times this state is referenced.
34  assert(state->refCount > 0);
35  ProgramState *s = const_cast<ProgramState*>(state);
36  if (--s->refCount == 0) {
38  Mgr.StateSet.RemoveNode(s);
39  s->~ProgramState();
40  Mgr.freeStates.push_back(s);
41  }
42 }
43 }}
44 
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 
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 
66  if (store)
67  stateMgr->getStoreManager().decrementReferenceCount(store);
68 }
69 
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 = (*CreateSMgr)(*this);
79  ConstraintMgr = (*CreateCMgr)(*this, SubEng);
80 }
81 
82 
84  for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
85  I!=E; ++I)
86  I->second.second(I->second.first);
87 }
88 
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 
115  SVal V,
116  const LocationContext *LCtx,
117  bool notifyChanges) const {
118  ProgramStateManager &Mgr = getStateManager();
119  ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
120  LV, V));
121  const MemRegion *MR = LV.getAsRegion();
122  if (MR && Mgr.getOwningEngine() && notifyChanges)
123  return Mgr.getOwningEngine()->processRegionChange(newState, MR, LCtx);
124 
125  return newState;
126 }
127 
129  SVal V,
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) :
137  new_state;
138 }
139 
142 
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));
155 
156  return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
157  IS, ITraits, Call);
158 }
159 
162  const Expr *E, unsigned Count,
163  const LocationContext *LCtx,
164  bool CausedByPointerEscape,
165  InvalidatedSymbols *IS,
166  const CallEvent *Call,
167  RegionAndSymbolInvalidationTraits *ITraits) const {
168 
169  return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
170  IS, ITraits, Call);
171 }
172 
174 ProgramState::invalidateRegionsImpl(ValueList Values,
175  const Expr *E, unsigned Count,
176  const LocationContext *LCtx,
177  bool CausedByPointerEscape,
178  InvalidatedSymbols *IS,
180  const CallEvent *Call) const {
181  ProgramStateManager &Mgr = getStateManager();
182  SubEngine* Eng = Mgr.getOwningEngine();
183 
184  InvalidatedSymbols Invalidated;
185  if (!IS)
186  IS = &Invalidated;
187 
189  if (!ITraits)
190  ITraits = &ITraitsLocal;
191 
192  if (Eng) {
193  StoreManager::InvalidatedRegions TopLevelInvalidated;
195  const StoreRef &newStore
196  = Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
197  *IS, *ITraits, &TopLevelInvalidated,
198  &Invalidated);
199 
200  ProgramStateRef newState = makeWithStore(newStore);
201 
202  if (CausedByPointerEscape) {
203  newState = Eng->notifyCheckersOfPointerEscape(newState, IS,
204  TopLevelInvalidated,
205  Invalidated, Call,
206  *ITraits);
207  }
208 
209  return Eng->processRegionChanges(newState, IS, TopLevelInvalidated,
210  Invalidated, LCtx, Call);
211  }
212 
213  const StoreRef &newStore =
214  Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
215  *IS, *ITraits, nullptr, nullptr);
216  return makeWithStore(newStore);
217 }
218 
220  assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead.");
221 
222  Store OldStore = getStore();
223  const StoreRef &newStore =
224  getStateManager().StoreMgr->killBinding(OldStore, LV);
225 
226  if (newStore.getStore() == OldStore)
227  return this;
228 
229  return makeWithStore(newStore);
230 }
231 
234  const StackFrameContext *CalleeCtx) const {
235  const StoreRef &NewStore =
236  getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
237  return makeWithStore(NewStore);
238 }
239 
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())
245  return UnknownVal();
246 
247  if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
248  QualType T = TR->getValueType();
250  return getSVal(R);
251  }
252 
253  return UnknownVal();
254 }
255 
257  SVal V = getRawSVal(cast<Loc>(location), T);
258 
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
262  // about).
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.
273  //
274  // This shows up in the following:
275  //
276  // char foo();
277  // unsigned x = foo();
278  // if (x == 54)
279  // ...
280  //
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',
283  // not unsigned.
284  const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
285 
286  if (V.getAs<Loc>())
287  return loc::ConcreteInt(NewV);
288  else
289  return nonloc::ConcreteInt(NewV);
290  }
291  }
292  }
293 
294  return V;
295 }
296 
298  const LocationContext *LCtx,
299  SVal V, bool Invalidate) const{
300  Environment NewEnv =
301  getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
302  Invalidate);
303  if (NewEnv == Env)
304  return this;
305 
306  ProgramState NewSt = *this;
307  NewSt.Env = NewEnv;
308  return getStateManager().getPersistentState(NewSt);
309 }
310 
312  DefinedOrUnknownSVal UpperBound,
313  bool Assumption,
314  QualType indexTy) const {
315  if (Idx.isUnknown() || UpperBound.isUnknown())
316  return this;
317 
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();
324 
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())
329  indexTy = Ctx.IntTy;
330  nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
331 
332  // Adjust the index.
333  SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
334  Idx.castAs<NonLoc>(), Min, indexTy);
335  if (newIdx.isUnknownOrUndef())
336  return this;
337 
338  // Adjust the upper bound.
339  SVal newBound =
340  svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
341  Min, indexTy);
342 
343  if (newBound.isUnknownOrUndef())
344  return this;
345 
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())
350  return this;
351 
352  // Finally, let the constraint manager take care of it.
354  return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption);
355 }
356 
358  ConditionTruthVal IsNull = isNull(V);
359  if (IsNull.isUnderconstrained())
360  return IsNull;
361  return ConditionTruthVal(!IsNull.getValue());
362 }
363 
365  return stateMgr->getSValBuilder().areEqual(this, Lhs, Rhs);
366 }
367 
369  if (V.isZeroConstant())
370  return true;
371 
372  if (V.isConstant())
373  return false;
374 
375  SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
376  if (!Sym)
377  return ConditionTruthVal();
378 
379  return getStateManager().ConstraintMgr->isNull(this, Sym);
380 }
381 
383  ProgramState State(this,
384  EnvMgr.getInitialEnvironment(),
385  StoreMgr->getInitialStore(InitLoc),
386  GDMFactory.getEmptyMap());
387 
388  return getPersistentState(State);
389 }
390 
392  ProgramStateRef FromState,
393  ProgramStateRef GDMState) {
394  ProgramState NewState(*FromState);
395  NewState.GDM = GDMState->GDM;
396  return getPersistentState(NewState);
397 }
398 
400 
401  llvm::FoldingSetNodeID ID;
402  State.Profile(ID);
403  void *InsertPos;
404 
405  if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
406  return I;
407 
408  ProgramState *newState = nullptr;
409  if (!freeStates.empty()) {
410  newState = freeStates.back();
411  freeStates.pop_back();
412  }
413  else {
414  newState = (ProgramState*) Alloc.Allocate<ProgramState>();
415  }
416  new (newState) ProgramState(State);
417  StateSet.InsertNode(newState, InsertPos);
418  return newState;
419 }
420 
421 ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
422  ProgramState NewSt(*this);
423  NewSt.setStore(store);
424  return getStateManager().getPersistentState(NewSt);
425 }
426 
427 void ProgramState::setStore(const StoreRef &newStore) {
428  Store newStoreStore = newStore.getStore();
429  if (newStoreStore)
430  stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
431  if (store)
432  stateMgr->getStoreManager().decrementReferenceCount(store);
433  store = newStoreStore;
434 }
435 
436 //===----------------------------------------------------------------------===//
437 // State pretty-printing.
438 //===----------------------------------------------------------------------===//
439 
440 void ProgramState::print(raw_ostream &Out,
441  const char *NL, const char *Sep) const {
442  // Print the store.
443  ProgramStateManager &Mgr = getStateManager();
444  Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
445 
446  // Print out the environment.
447  Env.print(Out, NL, Sep);
448 
449  // Print out the constraints.
450  Mgr.getConstraintManager().print(this, Out, NL, Sep);
451 
452  // Print checker-specific data.
453  Mgr.getOwningEngine()->printState(Out, this, NL, Sep);
454 }
455 
456 void ProgramState::printDOT(raw_ostream &Out) const {
457  print(Out, "\\l", "\\|");
458 }
459 
460 LLVM_DUMP_METHOD void ProgramState::dump() const {
461  print(llvm::errs());
462 }
463 
464 void ProgramState::printTaint(raw_ostream &Out,
465  const char *NL, const char *Sep) const {
466  TaintMapImpl TM = get<TaintMap>();
467 
468  if (!TM.isEmpty())
469  Out <<"Tainted Symbols:" << NL;
470 
471  for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) {
472  Out << I->first << " : " << I->second << NL;
473  }
474 }
475 
477  printTaint(llvm::errs());
478 }
479 
480 //===----------------------------------------------------------------------===//
481 // Generic Data Map.
482 //===----------------------------------------------------------------------===//
483 
484 void *const* ProgramState::FindGDM(void *K) const {
485  return GDM.lookup(K);
486 }
487 
488 void*
490  void *(*CreateContext)(llvm::BumpPtrAllocator&),
491  void (*DeleteContext)(void*)) {
492 
493  std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
494  if (!p.first) {
495  p.first = CreateContext(Alloc);
496  p.second = DeleteContext;
497  }
498 
499  return p.first;
500 }
501 
503  ProgramState::GenericDataMap M1 = St->getGDM();
504  ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
505 
506  if (M1 == M2)
507  return St;
508 
509  ProgramState NewSt = *St;
510  NewSt.GDM = M2;
511  return getPersistentState(NewSt);
512 }
513 
515  ProgramState::GenericDataMap OldM = state->getGDM();
516  ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
517 
518  if (NewM == OldM)
519  return state;
520 
521  ProgramState NewState = *state;
522  NewState.GDM = NewM;
523  return getPersistentState(NewState);
524 }
525 
527  bool wasVisited = !visited.insert(val.getCVData()).second;
528  if (wasVisited)
529  return true;
530 
531  StoreManager &StoreMgr = state->getStateManager().getStoreManager();
532  // FIXME: We don't really want to use getBaseRegion() here because pointer
533  // arithmetic doesn't apply, but scanReachableSymbols only accepts base
534  // regions right now.
535  const MemRegion *R = val.getRegion()->getBaseRegion();
536  return StoreMgr.scanReachableSymbols(val.getStore(), R, *this);
537 }
538 
540  for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
541  if (!scan(*I))
542  return false;
543 
544  return true;
545 }
546 
548  for (SymExpr::symbol_iterator SI = sym->symbol_begin(),
549  SE = sym->symbol_end();
550  SI != SE; ++SI) {
551  bool wasVisited = !visited.insert(*SI).second;
552  if (wasVisited)
553  continue;
554 
555  if (!visitor.VisitSymbol(*SI))
556  return false;
557  }
558 
559  return true;
560 }
561 
564  return scan(X->getRegion());
565 
568  return scan(*X);
569 
571  return scan(X->getLoc());
572 
573  if (SymbolRef Sym = val.getAsSymbol())
574  return scan(Sym);
575 
576  if (const SymExpr *Sym = val.getAsSymbolicExpression())
577  return scan(Sym);
578 
580  return scan(*X);
581 
582  return true;
583 }
584 
586  if (isa<MemSpaceRegion>(R))
587  return true;
588 
589  bool wasVisited = !visited.insert(R).second;
590  if (wasVisited)
591  return true;
592 
593  if (!visitor.VisitMemRegion(R))
594  return false;
595 
596  // If this is a symbolic region, visit the symbol for the region.
597  if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
598  if (!visitor.VisitSymbol(SR->getSymbol()))
599  return false;
600 
601  // If this is a subregion, also visit the parent regions.
602  if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
603  const MemRegion *Super = SR->getSuperRegion();
604  if (!scan(Super))
605  return false;
606 
607  // When we reach the topmost region, scan all symbols in it.
608  if (isa<MemSpaceRegion>(Super)) {
609  StoreManager &StoreMgr = state->getStateManager().getStoreManager();
610  if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
611  return false;
612  }
613  }
614 
615  // Regions captured by a block are also implicitly reachable.
616  if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
617  BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
618  E = BDR->referenced_vars_end();
619  for ( ; I != E; ++I) {
620  if (!scan(I.getCapturedRegion()))
621  return false;
622  }
623  }
624 
625  return true;
626 }
627 
629  ScanReachableSymbols S(this, visitor);
630  return S.scan(val);
631 }
632 
634  SymbolVisitor &visitor) const {
635  ScanReachableSymbols S(this, visitor);
636  for ( ; I != E; ++I) {
637  if (!S.scan(*I))
638  return false;
639  }
640  return true;
641 }
642 
644  const MemRegion * const *E,
645  SymbolVisitor &visitor) const {
646  ScanReachableSymbols S(this, visitor);
647  for ( ; I != E; ++I) {
648  if (!S.scan(*I))
649  return false;
650  }
651  return true;
652 }
653 
655  const LocationContext *LCtx,
656  TaintTagType Kind) const {
657  if (const Expr *E = dyn_cast_or_null<Expr>(S))
658  S = E->IgnoreParens();
659 
660  return addTaint(getSVal(S, LCtx), Kind);
661 }
662 
664  TaintTagType Kind) const {
665  SymbolRef Sym = V.getAsSymbol();
666  if (Sym)
667  return addTaint(Sym, Kind);
668 
669  // If the SVal represents a structure, try to mass-taint all values within the
670  // structure. For now it only works efficiently on lazy compound values that
671  // were conjured during a conservative evaluation of a function - either as
672  // return values of functions that return structures or arrays by value, or as
673  // values of structures or arrays passed into the function by reference,
674  // directly or through pointer aliasing. Such lazy compound values are
675  // characterized by having exactly one binding in their captured store within
676  // their parent region, which is a conjured symbol default-bound to the base
677  // region of the parent region.
678  if (auto LCV = V.getAs<nonloc::LazyCompoundVal>()) {
679  if (Optional<SVal> binding = getStateManager().StoreMgr->getDefaultBinding(*LCV)) {
680  if (SymbolRef Sym = binding->getAsSymbol())
681  return addPartialTaint(Sym, LCV->getRegion(), Kind);
682  }
683  }
684 
685  const MemRegion *R = V.getAsRegion();
686  return addTaint(R, Kind);
687 }
688 
690  TaintTagType Kind) const {
691  if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R))
692  return addTaint(SR->getSymbol(), Kind);
693  return this;
694 }
695 
697  TaintTagType Kind) const {
698  // If this is a symbol cast, remove the cast before adding the taint. Taint
699  // is cast agnostic.
700  while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym))
701  Sym = SC->getOperand();
702 
703  ProgramStateRef NewState = set<TaintMap>(Sym, Kind);
704  assert(NewState);
705  return NewState;
706 }
707 
709  const SubRegion *SubRegion,
710  TaintTagType Kind) const {
711  // Ignore partial taint if the entire parent symbol is already tainted.
712  if (contains<TaintMap>(ParentSym) && *get<TaintMap>(ParentSym) == Kind)
713  return this;
714 
715  // Partial taint applies if only a portion of the symbol is tainted.
716  if (SubRegion == SubRegion->getBaseRegion())
717  return addTaint(ParentSym, Kind);
718 
719  const TaintedSubRegions *SavedRegs = get<DerivedSymTaint>(ParentSym);
720  TaintedSubRegions Regs =
721  SavedRegs ? *SavedRegs : stateMgr->TSRFactory.getEmptyMap();
722 
723  Regs = stateMgr->TSRFactory.add(Regs, SubRegion, Kind);
724  ProgramStateRef NewState = set<DerivedSymTaint>(ParentSym, Regs);
725  assert(NewState);
726  return NewState;
727 }
728 
729 bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx,
730  TaintTagType Kind) const {
731  if (const Expr *E = dyn_cast_or_null<Expr>(S))
732  S = E->IgnoreParens();
733 
734  SVal val = getSVal(S, LCtx);
735  return isTainted(val, Kind);
736 }
737 
739  if (const SymExpr *Sym = V.getAsSymExpr())
740  return isTainted(Sym, Kind);
741  if (const MemRegion *Reg = V.getAsRegion())
742  return isTainted(Reg, Kind);
743  return false;
744 }
745 
746 bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const {
747  if (!Reg)
748  return false;
749 
750  // Element region (array element) is tainted if either the base or the offset
751  // are tainted.
752  if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg))
753  return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K);
754 
755  if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg))
756  return isTainted(SR->getSymbol(), K);
757 
758  if (const SubRegion *ER = dyn_cast<SubRegion>(Reg))
759  return isTainted(ER->getSuperRegion(), K);
760 
761  return false;
762 }
763 
765  if (!Sym)
766  return false;
767 
768  // Traverse all the symbols this symbol depends on to see if any are tainted.
769  for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
770  SI != SE; ++SI) {
771  if (!isa<SymbolData>(*SI))
772  continue;
773 
774  if (const TaintTagType *Tag = get<TaintMap>(*SI)) {
775  if (*Tag == Kind)
776  return true;
777  }
778 
779  if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI)) {
780  // If this is a SymbolDerived with a tainted parent, it's also tainted.
781  if (isTainted(SD->getParentSymbol(), Kind))
782  return true;
783 
784  // If this is a SymbolDerived with the same parent symbol as another
785  // tainted SymbolDerived and a region that's a sub-region of that tainted
786  // symbol, it's also tainted.
787  if (const TaintedSubRegions *Regs =
788  get<DerivedSymTaint>(SD->getParentSymbol())) {
789  const TypedValueRegion *R = SD->getRegion();
790  for (auto I : *Regs) {
791  // FIXME: The logic to identify tainted regions could be more
792  // complete. For example, this would not currently identify
793  // overlapping fields in a union as tainted. To identify this we can
794  // check for overlapping/nested byte offsets.
795  if (Kind == I.second && R->isSubRegionOf(I.first))
796  return true;
797  }
798  }
799  }
800 
801  // If memory region is tainted, data is also tainted.
802  if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI)) {
803  if (isTainted(SRV->getRegion(), Kind))
804  return true;
805  }
806 
807  // If this is a SymbolCast from a tainted value, it's also tainted.
808  if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI)) {
809  if (isTainted(SC->getOperand(), Kind))
810  return true;
811  }
812  }
813 
814  return false;
815 }
816 
virtual ProgramStateRef assume(ProgramStateRef state, DefinedSVal Cond, bool Assumption)=0
ProgramStateRef addPartialTaint(SymbolRef ParentSym, const SubRegion *SubRegion, TaintTagType Kind=TaintTagGeneric) const
Create a new state in a which a sub-region of a given symbol is tainted.
ProgramStateRef addGDM(ProgramStateRef St, void *Key, void *Data)
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:512
ProgramStateRef processRegionChange(ProgramStateRef state, const MemRegion *MR, const LocationContext *LCtx)
Definition: SubEngine.h:139
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
A (possibly-)qualified type.
Definition: Type.h:653
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:79
bool isUnderconstrained() const
Return true if the constrained is underconstrained and we do not know if the constraint is true of va...
Store getStore() const
Definition: StoreRef.h:46
ProgramStateRef enterStackFrame(const CallEvent &Call, const StackFrameContext *CalleeCtx) const
enterStackFrame - Returns the state for entry to the given stack frame, preserving the current state...
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
SValBuilder * createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, ProgramStateManager &stateMgr)
Stmt - This represents one statement.
Definition: Stmt.h:66
Information about invalidation for a particular region/symbol.
Definition: MemRegion.h:1389
BasicValueFactory & getBasicVals()
Definition: ProgramState.h:514
Manages the lifetime of CallEvent objects.
Definition: CallEvent.h:982
const void * Store
Store - This opaque type encapsulates an immutable mapping from locations to values.
Definition: StoreRef.h:26
bool isTainted(const Stmt *S, const LocationContext *LCtx, TaintTagType Kind=TaintTagGeneric) const
Check if the statement is tainted in the current state.
Value representing integer constant.
Definition: SVals.h:353
ConditionTruthVal isNonNull(SVal V) const
Check if the given SVal is not constrained to zero and is not a zero constant.
A utility class that visits the reachable symbols using a custom SymbolVisitor.
Definition: ProgramState.h:856
void print(raw_ostream &Out, const char *nl="\, const char *sep="") const
ProgramStateRef removeDeadBindings(ProgramStateRef St, const StackFrameContext *LCtx, SymbolReaper &SymReaper)
Store getStore() const
Return the store associated with this state.
Definition: ProgramState.h:123
Symbolic value.
Definition: SymExpr.h:29
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
LineState State
std::unique_ptr< StoreManager >(* StoreManagerCreator)(ProgramStateManager &)
Definition: ProgramState.h:44
symbol_iterator symbol_begin() const
Definition: SymExpr.h:83
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
const SymExpr * getAsSymbolicExpression() const
getAsSymbolicExpression - If this Sval wraps a symbolic expression then return that expression...
Definition: SVals.cpp:126
void setReapedStore(StoreRef st)
Set to the value of the symbolic store after StoreManager::removeDeadBindings has been called...
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:6219
const llvm::APSInt & Convert(const llvm::APSInt &To, const llvm::APSInt &From)
Convert - Create a new persistent APSInt with the same value as &#39;From&#39; but with the bitwidth and sign...
static bool isLocType(QualType T)
Definition: SVals.h:308
ProgramStateManager & getStateManager() const
Return the ProgramStateManager associated with this state.
Definition: ProgramState.h:110
BlockDataRegion - A region that represents a block instance.
Definition: MemRegion.h:657
bool scanReachableSymbols(SVal val, SymbolVisitor &visitor) const
Visits the symbols reachable from the given SVal using the provided SymbolVisitor.
bool isUnknown() const
Definition: SVals.h:125
virtual void decrementReferenceCount(Store store)
If the StoreManager supports it, decrement the reference count of the specified Store object...
Definition: Store.h:183
const LazyCompoundValData * getCVData() const
Definition: SVals.h:460
A symbol representing the value of a MemRegion whose parent region has symbolic value.
bool isConstant() const
Definition: SVals.cpp:207
SVal getSVal(const Stmt *S, const LocationContext *LCtx) const
Returns the SVal bound to the statement &#39;S&#39; in the state&#39;s environment.
Definition: ProgramState.h:749
ProgramStateRef bindLoc(Loc location, SVal V, const LocationContext *LCtx, bool notifyChanges=true) const
virtual void print(ProgramStateRef state, raw_ostream &Out, const char *nl, const char *sep)=0
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
Definition: SVals.cpp:116
void printDOT(raw_ostream &Out) const
llvm::ImmutableList< SVal >::iterator iterator
Definition: SVals.h:438
std::unique_ptr< ConstraintManager >(* ConstraintManagerCreator)(ProgramStateManager &, SubEngine *)
Definition: ProgramState.h:42
ProgramStateRef BindExpr(const Stmt *S, const LocationContext *LCtx, SVal V, bool Invalidate=true) const
Create a new state by binding the value &#39;V&#39; to the statement &#39;S&#39; in the state&#39;s environment.
SymbolicRegion - A special, "non-concrete" region.
Definition: MemRegion.h:743
unsigned TaintTagType
The type of taint, which helps to differentiate between different types of taint. ...
Definition: TaintTag.h:22
ProgramState - This class encapsulates:
Definition: ProgramState.h:74
Expr - This represents one expression.
Definition: Expr.h:106
SourceLocation End
const FunctionProtoType * T
Represents a cast expression.
ConditionTruthVal isNull(SVal V) const
Check if the given SVal is constrained to zero or is a zero constant.
bool isNull() const
Return true if this QualType doesn&#39;t point to a type yet.
Definition: Type.h:719
const SourceManager & SM
Definition: Format.cpp:1399
void *const * FindGDM(void *K) const
Optional< T > getAs() const
Convert to the specified SVal type, returning None if this SVal is not of the desired type...
Definition: SVals.h:100
virtual bool isBoundable() const
Definition: MemRegion.h:153
ProgramStateRef removeGDM(ProgramStateRef state, void *Key)
bool scan(nonloc::LazyCompoundVal val)
Kind
ProgramStateRef getInitialState(const LocationContext *InitLoc)
ProgramStateRef invalidateRegions(ArrayRef< const MemRegion *> Regions, const Expr *E, unsigned BlockCount, const LocationContext *LCtx, bool CausesPointerEscape, InvalidatedSymbols *IS=nullptr, const CallEvent *Call=nullptr, RegionAndSymbolInvalidationTraits *ITraits=nullptr) const
Returns the state with bindings for the given regions cleared from the store.
const MemRegion * getAsRegion() const
Definition: SVals.cpp:140
bool isSubRegionOf(const MemRegion *R) const override
Check if the region is a subregion of the given region.
Definition: MemRegion.cpp:105
An entry in the environment consists of a Stmt and an LocationContext.
Definition: Environment.h:35
ASTContext & getContext()
Definition: SValBuilder.h:138
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:63
virtual ProgramStateRef processRegionChanges(ProgramStateRef state, const InvalidatedSymbols *invalidated, ArrayRef< const MemRegion *> ExplicitRegions, ArrayRef< const MemRegion *> Regions, const LocationContext *LCtx, const CallEvent *Call)=0
processRegionChanges - Called by ProgramStateManager whenever a change is made to the store...
A class responsible for cleaning up unused symbols.
ConditionTruthVal areEqual(SVal Lhs, SVal Rhs) const
ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState, ProgramStateRef GDMState)
ProgramStateRef bindDefault(SVal loc, SVal V, const LocationContext *LCtx) const
virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op, NonLoc lhs, NonLoc rhs, QualType resultTy)=0
Create a new value which represents a binary expression with two non- location operands.
An immutable map from EnvironemntEntries to SVals.
Definition: Environment.h:56
A symbol representing the value stored at a MemRegion.
Definition: SymbolManager.h:42
Dataflow Directional Tag Classes.
void ProgramStateRelease(const ProgramState *state)
Decrement the number of times this state is referenced.
ArrayRef< const MemRegion * > RegionList
bool isZeroConstant() const
Definition: SVals.cpp:219
static symbol_iterator symbol_end()
Definition: SymExpr.h:84
ProgramStateRef killBinding(Loc LV) const
const void * getStore() const
Definition: SVals.cpp:155
void printTaint(raw_ostream &Out, const char *nl="\, const char *sep="") const
virtual void print(Store store, raw_ostream &Out, const char *nl, const char *sep)=0
Represents an abstract call to a function or method along a particular path.
Definition: CallEvent.h:140
virtual bool scanReachableSymbols(Store S, const MemRegion *R, ScanReachableSymbols &Visitor)=0
Finds the transitive closure of symbols within the given region.
const llvm::APSInt & getMinValue(const llvm::APSInt &v)
ConstraintManager & getConstraintManager()
Definition: ProgramState.h:541
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition: SVals.h:92
BasicValueFactory & getBasicValueFactory()
Definition: SValBuilder.h:151
SubRegion - A region that subsets another larger region.
Definition: MemRegion.h:420
ProgramState(ProgramStateManager *mgr, const Environment &env, StoreRef st, GenericDataMap gdm)
This ctor is used when creating the first ProgramState object.
void ProgramStateRetain(const ProgramState *state)
Increments the number of times this state is referenced.
const SymExpr * getAsSymExpr() const
Definition: SVals.cpp:133
ProgramStateRef addTaint(const Stmt *S, const LocationContext *LCtx, TaintTagType Kind=TaintTagGeneric) const
Create a new state in which the statement is marked as tainted.
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:13413
ProgramStateManager(ASTContext &Ctx, StoreManagerCreator CreateStoreManager, ConstraintManagerCreator CreateConstraintManager, llvm::BumpPtrAllocator &alloc, SubEngine *subeng)
const TypedValueRegion * getRegion() const
Definition: SVals.cpp:159
Environment removeDeadBindings(Environment Env, SymbolReaper &SymReaper, ProgramStateRef state)
void * FindGDMContext(void *index, void *(*CreateContext)(llvm::BumpPtrAllocator &), void(*DeleteContext)(void *))
const MemRegion * getBaseRegion() const
Definition: MemRegion.cpp:1093
virtual void incrementReferenceCount(Store store)
If the StoreManager supports it, increment the reference count of the specified Store object...
Definition: Store.h:178
virtual ProgramStateRef notifyCheckersOfPointerEscape(ProgramStateRef State, const InvalidatedSymbols *Invalidated, ArrayRef< const MemRegion *> ExplicitRegions, ArrayRef< const MemRegion *> Regions, const CallEvent *Call, RegionAndSymbolInvalidationTraits &HTraits)=0
ElementRegin is used to represent both array elements and casts.
Definition: MemRegion.h:1070
CanQualType IntTy
Definition: ASTContext.h:1004
virtual void printState(raw_ostream &Out, ProgramStateRef State, const char *NL, const char *Sep)=0
printState - Called by ProgramStateManager to print checker-specific data.
static void Profile(llvm::FoldingSetNodeID &ID, const ProgramState *V)
Profile - Profile the contents of a ProgramState object for use in a FoldingSet.
Definition: ProgramState.h:134
ProgramStateRef assumeInBound(DefinedOrUnknownSVal idx, DefinedOrUnknownSVal upperBound, bool assumption, QualType IndexType=QualType()) const
ProgramStateRef getPersistentState(ProgramState &Impl)
SVal getSValAsScalarOrLoc(const Stmt *Ex, const LocationContext *LCtx) const
Definition: ProgramState.h:756
bool isUnknownOrUndef() const
Definition: SVals.h:133
ArrayRef< SVal > ValueList
Expr * IgnoreParens() LLVM_READONLY
IgnoreParens - Ignore parentheses.
Definition: Expr.cpp:2434
Iterator over symbols that the current symbol depends on.
Definition: SymExpr.h:68