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