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 
131  const LocationContext *LCtx) const {
132  ProgramStateManager &Mgr = getStateManager();
133  const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
134  const StoreRef &newStore = Mgr.StoreMgr->BindDefaultInitial(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  ProgramStateManager &Mgr = getStateManager();
144  const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
145  const StoreRef &newStore = Mgr.StoreMgr->BindDefaultZero(getStore(), R);
146  ProgramStateRef new_state = makeWithStore(newStore);
147  return Mgr.getOwningEngine()
148  ? Mgr.getOwningEngine()->processRegionChange(new_state, R, LCtx)
149  : new_state;
150 }
151 
154 
157  const Expr *E, unsigned Count,
158  const LocationContext *LCtx,
159  bool CausedByPointerEscape,
160  InvalidatedSymbols *IS,
161  const CallEvent *Call,
162  RegionAndSymbolInvalidationTraits *ITraits) const {
163  SmallVector<SVal, 8> Values;
164  for (RegionList::const_iterator I = Regions.begin(),
165  End = Regions.end(); I != End; ++I)
166  Values.push_back(loc::MemRegionVal(*I));
167 
168  return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
169  IS, ITraits, Call);
170 }
171 
174  const Expr *E, unsigned Count,
175  const LocationContext *LCtx,
176  bool CausedByPointerEscape,
177  InvalidatedSymbols *IS,
178  const CallEvent *Call,
179  RegionAndSymbolInvalidationTraits *ITraits) const {
180 
181  return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
182  IS, ITraits, Call);
183 }
184 
186 ProgramState::invalidateRegionsImpl(ValueList Values,
187  const Expr *E, unsigned Count,
188  const LocationContext *LCtx,
189  bool CausedByPointerEscape,
190  InvalidatedSymbols *IS,
192  const CallEvent *Call) const {
193  ProgramStateManager &Mgr = getStateManager();
194  SubEngine* Eng = Mgr.getOwningEngine();
195 
196  InvalidatedSymbols Invalidated;
197  if (!IS)
198  IS = &Invalidated;
199 
201  if (!ITraits)
202  ITraits = &ITraitsLocal;
203 
204  if (Eng) {
205  StoreManager::InvalidatedRegions TopLevelInvalidated;
207  const StoreRef &newStore
208  = Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
209  *IS, *ITraits, &TopLevelInvalidated,
210  &Invalidated);
211 
212  ProgramStateRef newState = makeWithStore(newStore);
213 
214  if (CausedByPointerEscape) {
215  newState = Eng->notifyCheckersOfPointerEscape(newState, IS,
216  TopLevelInvalidated,
217  Invalidated, Call,
218  *ITraits);
219  }
220 
221  return Eng->processRegionChanges(newState, IS, TopLevelInvalidated,
222  Invalidated, LCtx, Call);
223  }
224 
225  const StoreRef &newStore =
226  Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
227  *IS, *ITraits, nullptr, nullptr);
228  return makeWithStore(newStore);
229 }
230 
232  assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead.");
233 
234  Store OldStore = getStore();
235  const StoreRef &newStore =
236  getStateManager().StoreMgr->killBinding(OldStore, LV);
237 
238  if (newStore.getStore() == OldStore)
239  return this;
240 
241  return makeWithStore(newStore);
242 }
243 
246  const StackFrameContext *CalleeCtx) const {
247  const StoreRef &NewStore =
248  getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
249  return makeWithStore(NewStore);
250 }
251 
253  // We only want to do fetches from regions that we can actually bind
254  // values. For example, SymbolicRegions of type 'id<...>' cannot
255  // have direct bindings (but their can be bindings on their subregions).
256  if (!R->isBoundable())
257  return UnknownVal();
258 
259  if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
260  QualType T = TR->getValueType();
262  return getSVal(R);
263  }
264 
265  return UnknownVal();
266 }
267 
269  SVal V = getRawSVal(location, T);
270 
271  // If 'V' is a symbolic value that is *perfectly* constrained to
272  // be a constant value, use that value instead to lessen the burden
273  // on later analysis stages (so we have less symbolic values to reason
274  // about).
275  // We only go into this branch if we can convert the APSInt value we have
276  // to the type of T, which is not always the case (e.g. for void).
277  if (!T.isNull() && (T->isIntegralOrEnumerationType() || Loc::isLocType(T))) {
278  if (SymbolRef sym = V.getAsSymbol()) {
279  if (const llvm::APSInt *Int = getStateManager()
280  .getConstraintManager()
281  .getSymVal(this, sym)) {
282  // FIXME: Because we don't correctly model (yet) sign-extension
283  // and truncation of symbolic values, we need to convert
284  // the integer value to the correct signedness and bitwidth.
285  //
286  // This shows up in the following:
287  //
288  // char foo();
289  // unsigned x = foo();
290  // if (x == 54)
291  // ...
292  //
293  // The symbolic value stored to 'x' is actually the conjured
294  // symbol for the call to foo(); the type of that symbol is 'char',
295  // not unsigned.
296  const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
297 
298  if (V.getAs<Loc>())
299  return loc::ConcreteInt(NewV);
300  else
301  return nonloc::ConcreteInt(NewV);
302  }
303  }
304  }
305 
306  return V;
307 }
308 
310  const LocationContext *LCtx,
311  SVal V, bool Invalidate) const{
312  Environment NewEnv =
313  getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
314  Invalidate);
315  if (NewEnv == Env)
316  return this;
317 
318  ProgramState NewSt = *this;
319  NewSt.Env = NewEnv;
320  return getStateManager().getPersistentState(NewSt);
321 }
322 
324  DefinedOrUnknownSVal UpperBound,
325  bool Assumption,
326  QualType indexTy) const {
327  if (Idx.isUnknown() || UpperBound.isUnknown())
328  return this;
329 
330  // Build an expression for 0 <= Idx < UpperBound.
331  // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
332  // FIXME: This should probably be part of SValBuilder.
333  ProgramStateManager &SM = getStateManager();
334  SValBuilder &svalBuilder = SM.getSValBuilder();
335  ASTContext &Ctx = svalBuilder.getContext();
336 
337  // Get the offset: the minimum value of the array index type.
338  BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
339  if (indexTy.isNull())
340  indexTy = svalBuilder.getArrayIndexType();
341  nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
342 
343  // Adjust the index.
344  SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
345  Idx.castAs<NonLoc>(), Min, indexTy);
346  if (newIdx.isUnknownOrUndef())
347  return this;
348 
349  // Adjust the upper bound.
350  SVal newBound =
351  svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
352  Min, indexTy);
353 
354  if (newBound.isUnknownOrUndef())
355  return this;
356 
357  // Build the actual comparison.
358  SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(),
359  newBound.castAs<NonLoc>(), Ctx.IntTy);
360  if (inBound.isUnknownOrUndef())
361  return this;
362 
363  // Finally, let the constraint manager take care of it.
365  return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption);
366 }
367 
369  ConditionTruthVal IsNull = isNull(V);
370  if (IsNull.isUnderconstrained())
371  return IsNull;
372  return ConditionTruthVal(!IsNull.getValue());
373 }
374 
376  return stateMgr->getSValBuilder().areEqual(this, Lhs, Rhs);
377 }
378 
380  if (V.isZeroConstant())
381  return true;
382 
383  if (V.isConstant())
384  return false;
385 
386  SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
387  if (!Sym)
388  return ConditionTruthVal();
389 
390  return getStateManager().ConstraintMgr->isNull(this, Sym);
391 }
392 
394  ProgramState State(this,
395  EnvMgr.getInitialEnvironment(),
396  StoreMgr->getInitialStore(InitLoc),
397  GDMFactory.getEmptyMap());
398 
399  return getPersistentState(State);
400 }
401 
403  ProgramStateRef FromState,
404  ProgramStateRef GDMState) {
405  ProgramState NewState(*FromState);
406  NewState.GDM = GDMState->GDM;
407  return getPersistentState(NewState);
408 }
409 
411 
412  llvm::FoldingSetNodeID ID;
413  State.Profile(ID);
414  void *InsertPos;
415 
416  if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
417  return I;
418 
419  ProgramState *newState = nullptr;
420  if (!freeStates.empty()) {
421  newState = freeStates.back();
422  freeStates.pop_back();
423  }
424  else {
425  newState = (ProgramState*) Alloc.Allocate<ProgramState>();
426  }
427  new (newState) ProgramState(State);
428  StateSet.InsertNode(newState, InsertPos);
429  return newState;
430 }
431 
432 ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
433  ProgramState NewSt(*this);
434  NewSt.setStore(store);
435  return getStateManager().getPersistentState(NewSt);
436 }
437 
438 void ProgramState::setStore(const StoreRef &newStore) {
439  Store newStoreStore = newStore.getStore();
440  if (newStoreStore)
441  stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
442  if (store)
443  stateMgr->getStoreManager().decrementReferenceCount(store);
444  store = newStoreStore;
445 }
446 
447 //===----------------------------------------------------------------------===//
448 // State pretty-printing.
449 //===----------------------------------------------------------------------===//
450 
451 void ProgramState::print(raw_ostream &Out, const char *NL, const char *Sep,
452  const LocationContext *LC) const {
453  // Print the store.
454  ProgramStateManager &Mgr = getStateManager();
455  Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
456 
457  // Print out the environment.
458  Env.print(Out, NL, Sep, LC);
459 
460  // Print out the constraints.
461  Mgr.getConstraintManager().print(this, Out, NL, Sep);
462 
463  // Print out the tracked dynamic types.
464  printDynamicTypeInfo(this, Out, NL, Sep);
465 
466  // Print out tainted symbols.
467  printTaint(Out, NL, Sep);
468 
469  // Print checker-specific data.
470  Mgr.getOwningEngine()->printState(Out, this, NL, Sep, LC);
471 }
472 
473 void ProgramState::printDOT(raw_ostream &Out, const LocationContext *LC) const {
474  print(Out, "\\l", "\\|", LC);
475 }
476 
477 LLVM_DUMP_METHOD void ProgramState::dump() const {
478  print(llvm::errs());
479 }
480 
481 void ProgramState::printTaint(raw_ostream &Out,
482  const char *NL, const char *Sep) const {
483  TaintMapImpl TM = get<TaintMap>();
484 
485  if (!TM.isEmpty())
486  Out <<"Tainted symbols:" << NL;
487 
488  for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) {
489  Out << I->first << " : " << I->second << NL;
490  }
491 }
492 
494  printTaint(llvm::errs());
495 }
496 
497 //===----------------------------------------------------------------------===//
498 // Generic Data Map.
499 //===----------------------------------------------------------------------===//
500 
501 void *const* ProgramState::FindGDM(void *K) const {
502  return GDM.lookup(K);
503 }
504 
505 void*
507  void *(*CreateContext)(llvm::BumpPtrAllocator&),
508  void (*DeleteContext)(void*)) {
509 
510  std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
511  if (!p.first) {
512  p.first = CreateContext(Alloc);
513  p.second = DeleteContext;
514  }
515 
516  return p.first;
517 }
518 
520  ProgramState::GenericDataMap M1 = St->getGDM();
521  ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
522 
523  if (M1 == M2)
524  return St;
525 
526  ProgramState NewSt = *St;
527  NewSt.GDM = M2;
528  return getPersistentState(NewSt);
529 }
530 
532  ProgramState::GenericDataMap OldM = state->getGDM();
533  ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
534 
535  if (NewM == OldM)
536  return state;
537 
538  ProgramState NewState = *state;
539  NewState.GDM = NewM;
540  return getPersistentState(NewState);
541 }
542 
544  bool wasVisited = !visited.insert(val.getCVData()).second;
545  if (wasVisited)
546  return true;
547 
548  StoreManager &StoreMgr = state->getStateManager().getStoreManager();
549  // FIXME: We don't really want to use getBaseRegion() here because pointer
550  // arithmetic doesn't apply, but scanReachableSymbols only accepts base
551  // regions right now.
552  const MemRegion *R = val.getRegion()->getBaseRegion();
553  return StoreMgr.scanReachableSymbols(val.getStore(), R, *this);
554 }
555 
557  for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
558  if (!scan(*I))
559  return false;
560 
561  return true;
562 }
563 
565  for (SymExpr::symbol_iterator SI = sym->symbol_begin(),
566  SE = sym->symbol_end();
567  SI != SE; ++SI) {
568  bool wasVisited = !visited.insert(*SI).second;
569  if (wasVisited)
570  continue;
571 
572  if (!visitor.VisitSymbol(*SI))
573  return false;
574  }
575 
576  return true;
577 }
578 
581  return scan(X->getRegion());
582 
585  return scan(*X);
586 
588  return scan(X->getLoc());
589 
590  if (SymbolRef Sym = val.getAsSymbol())
591  return scan(Sym);
592 
593  if (const SymExpr *Sym = val.getAsSymbolicExpression())
594  return scan(Sym);
595 
597  return scan(*X);
598 
599  return true;
600 }
601 
603  if (isa<MemSpaceRegion>(R))
604  return true;
605 
606  bool wasVisited = !visited.insert(R).second;
607  if (wasVisited)
608  return true;
609 
610  if (!visitor.VisitMemRegion(R))
611  return false;
612 
613  // If this is a symbolic region, visit the symbol for the region.
614  if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
615  if (!visitor.VisitSymbol(SR->getSymbol()))
616  return false;
617 
618  // If this is a subregion, also visit the parent regions.
619  if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
620  const MemRegion *Super = SR->getSuperRegion();
621  if (!scan(Super))
622  return false;
623 
624  // When we reach the topmost region, scan all symbols in it.
625  if (isa<MemSpaceRegion>(Super)) {
626  StoreManager &StoreMgr = state->getStateManager().getStoreManager();
627  if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
628  return false;
629  }
630  }
631 
632  // Regions captured by a block are also implicitly reachable.
633  if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
634  BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
635  E = BDR->referenced_vars_end();
636  for ( ; I != E; ++I) {
637  if (!scan(I.getCapturedRegion()))
638  return false;
639  }
640  }
641 
642  return true;
643 }
644 
646  ScanReachableSymbols S(this, visitor);
647  return S.scan(val);
648 }
649 
651  SymbolVisitor &visitor) const {
652  ScanReachableSymbols S(this, visitor);
653  for ( ; I != E; ++I) {
654  if (!S.scan(*I))
655  return false;
656  }
657  return true;
658 }
659 
661  const MemRegion * const *E,
662  SymbolVisitor &visitor) const {
663  ScanReachableSymbols S(this, visitor);
664  for ( ; I != E; ++I) {
665  if (!S.scan(*I))
666  return false;
667  }
668  return true;
669 }
670 
672  const LocationContext *LCtx,
673  TaintTagType Kind) const {
674  if (const Expr *E = dyn_cast_or_null<Expr>(S))
675  S = E->IgnoreParens();
676 
677  return addTaint(getSVal(S, LCtx), Kind);
678 }
679 
681  TaintTagType Kind) const {
682  SymbolRef Sym = V.getAsSymbol();
683  if (Sym)
684  return addTaint(Sym, Kind);
685 
686  // If the SVal represents a structure, try to mass-taint all values within the
687  // structure. For now it only works efficiently on lazy compound values that
688  // were conjured during a conservative evaluation of a function - either as
689  // return values of functions that return structures or arrays by value, or as
690  // values of structures or arrays passed into the function by reference,
691  // directly or through pointer aliasing. Such lazy compound values are
692  // characterized by having exactly one binding in their captured store within
693  // their parent region, which is a conjured symbol default-bound to the base
694  // region of the parent region.
695  if (auto LCV = V.getAs<nonloc::LazyCompoundVal>()) {
696  if (Optional<SVal> binding = getStateManager().StoreMgr->getDefaultBinding(*LCV)) {
697  if (SymbolRef Sym = binding->getAsSymbol())
698  return addPartialTaint(Sym, LCV->getRegion(), Kind);
699  }
700  }
701 
702  const MemRegion *R = V.getAsRegion();
703  return addTaint(R, Kind);
704 }
705 
707  TaintTagType Kind) const {
708  if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R))
709  return addTaint(SR->getSymbol(), Kind);
710  return this;
711 }
712 
714  TaintTagType Kind) const {
715  // If this is a symbol cast, remove the cast before adding the taint. Taint
716  // is cast agnostic.
717  while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym))
718  Sym = SC->getOperand();
719 
720  ProgramStateRef NewState = set<TaintMap>(Sym, Kind);
721  assert(NewState);
722  return NewState;
723 }
724 
726  const SubRegion *SubRegion,
727  TaintTagType Kind) const {
728  // Ignore partial taint if the entire parent symbol is already tainted.
729  if (contains<TaintMap>(ParentSym) && *get<TaintMap>(ParentSym) == Kind)
730  return this;
731 
732  // Partial taint applies if only a portion of the symbol is tainted.
733  if (SubRegion == SubRegion->getBaseRegion())
734  return addTaint(ParentSym, Kind);
735 
736  const TaintedSubRegions *SavedRegs = get<DerivedSymTaint>(ParentSym);
737  TaintedSubRegions Regs =
738  SavedRegs ? *SavedRegs : stateMgr->TSRFactory.getEmptyMap();
739 
740  Regs = stateMgr->TSRFactory.add(Regs, SubRegion, Kind);
741  ProgramStateRef NewState = set<DerivedSymTaint>(ParentSym, Regs);
742  assert(NewState);
743  return NewState;
744 }
745 
746 bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx,
747  TaintTagType Kind) const {
748  if (const Expr *E = dyn_cast_or_null<Expr>(S))
749  S = E->IgnoreParens();
750 
751  SVal val = getSVal(S, LCtx);
752  return isTainted(val, Kind);
753 }
754 
756  if (const SymExpr *Sym = V.getAsSymExpr())
757  return isTainted(Sym, Kind);
758  if (const MemRegion *Reg = V.getAsRegion())
759  return isTainted(Reg, Kind);
760  return false;
761 }
762 
763 bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const {
764  if (!Reg)
765  return false;
766 
767  // Element region (array element) is tainted if either the base or the offset
768  // are tainted.
769  if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg))
770  return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K);
771 
772  if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg))
773  return isTainted(SR->getSymbol(), K);
774 
775  if (const SubRegion *ER = dyn_cast<SubRegion>(Reg))
776  return isTainted(ER->getSuperRegion(), K);
777 
778  return false;
779 }
780 
782  if (!Sym)
783  return false;
784 
785  // Traverse all the symbols this symbol depends on to see if any are tainted.
786  for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
787  SI != SE; ++SI) {
788  if (!isa<SymbolData>(*SI))
789  continue;
790 
791  if (const TaintTagType *Tag = get<TaintMap>(*SI)) {
792  if (*Tag == Kind)
793  return true;
794  }
795 
796  if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI)) {
797  // If this is a SymbolDerived with a tainted parent, it's also tainted.
798  if (isTainted(SD->getParentSymbol(), Kind))
799  return true;
800 
801  // If this is a SymbolDerived with the same parent symbol as another
802  // tainted SymbolDerived and a region that's a sub-region of that tainted
803  // symbol, it's also tainted.
804  if (const TaintedSubRegions *Regs =
805  get<DerivedSymTaint>(SD->getParentSymbol())) {
806  const TypedValueRegion *R = SD->getRegion();
807  for (auto I : *Regs) {
808  // FIXME: The logic to identify tainted regions could be more
809  // complete. For example, this would not currently identify
810  // overlapping fields in a union as tainted. To identify this we can
811  // check for overlapping/nested byte offsets.
812  if (Kind == I.second && R->isSubRegionOf(I.first))
813  return true;
814  }
815  }
816  }
817 
818  // If memory region is tainted, data is also tainted.
819  if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI)) {
820  if (isTainted(SRV->getRegion(), Kind))
821  return true;
822  }
823 
824  // If this is a SymbolCast from a tainted value, it's also tainted.
825  if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI)) {
826  if (isTainted(SC->getOperand(), Kind))
827  return true;
828  }
829  }
830 
831  return false;
832 }
833 
virtual ProgramStateRef assume(ProgramStateRef state, DefinedSVal Cond, bool Assumption)=0
LLVM_NODISCARD 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()))
QualType getArrayIndexType() const
Definition: SValBuilder.h:165
A (possibly-)qualified type.
Definition: Type.h:655
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
LLVM_NODISCARD 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:542
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:377
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:900
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
LLVM_NODISCARD ProgramStateRef bindDefaultInitial(SVal loc, SVal V, const LocationContext *LCtx) const
Initializes the region of memory represented by loc with an initial value.
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:150
LineState State
llvm::ImmutableList< SVal >::iterator iterator
Definition: SVals.h:465
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:6433
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:206
const LazyCompoundValData * getCVData() const
Definition: SVals.h:491
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:793
LLVM_NODISCARD 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
LLVM_NODISCARD 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
Represents a cast expression.
LLVM_NODISCARD ProgramStateRef bindDefaultZero(SVal loc, const LocationContext *LCtx) const
Performs C++ zero-initialization procedure on the region of memory represented by loc...
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:720
const SourceManager & SM
Definition: Format.cpp:1474
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)
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:133
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)
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
LLVM_NODISCARD 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.
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
LLVM_NODISCARD 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:569
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
LLVM_NODISCARD 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:13530
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:1126
virtual void incrementReferenceCount(Store store)
If the StoreManager supports it, increment the reference count of the specified Store object...
Definition: Store.h:201
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:1007
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
LLVM_NODISCARD 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:800
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:2486
Iterator over symbols that the current symbol depends on.
Definition: SymExpr.h:69