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