ProgramState.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. #include "clang/Analysis/CFG.h"
  14. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace clang;
  20. using namespace ento;
  21. // Give the vtable for ConstraintManager somewhere to live.
  22. // FIXME: Move this elsewhere.
  23. ConstraintManager::~ConstraintManager() {}
  24. ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env,
  25. StoreRef st, GenericDataMap gdm)
  26. : stateMgr(mgr),
  27. Env(env),
  28. store(st.getStore()),
  29. GDM(gdm),
  30. refCount(0) {
  31. stateMgr->getStoreManager().incrementReferenceCount(store);
  32. }
  33. ProgramState::ProgramState(const ProgramState &RHS)
  34. : llvm::FoldingSetNode(),
  35. stateMgr(RHS.stateMgr),
  36. Env(RHS.Env),
  37. store(RHS.store),
  38. GDM(RHS.GDM),
  39. refCount(0) {
  40. stateMgr->getStoreManager().incrementReferenceCount(store);
  41. }
  42. ProgramState::~ProgramState() {
  43. if (store)
  44. stateMgr->getStoreManager().decrementReferenceCount(store);
  45. }
  46. ProgramStateManager::~ProgramStateManager() {
  47. for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
  48. I!=E; ++I)
  49. I->second.second(I->second.first);
  50. }
  51. const ProgramState*
  52. ProgramStateManager::removeDeadBindings(const ProgramState *state,
  53. const StackFrameContext *LCtx,
  54. SymbolReaper& SymReaper) {
  55. // This code essentially performs a "mark-and-sweep" of the VariableBindings.
  56. // The roots are any Block-level exprs and Decls that our liveness algorithm
  57. // tells us are live. We then see what Decls they may reference, and keep
  58. // those around. This code more than likely can be made faster, and the
  59. // frequency of which this method is called should be experimented with
  60. // for optimum performance.
  61. ProgramState NewState = *state;
  62. NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state);
  63. // Clean up the store.
  64. StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx,
  65. SymReaper);
  66. NewState.setStore(newStore);
  67. SymReaper.setReapedStore(newStore);
  68. return getPersistentState(NewState);
  69. }
  70. const ProgramState *ProgramStateManager::MarshalState(const ProgramState *state,
  71. const StackFrameContext *InitLoc) {
  72. // make up an empty state for now.
  73. ProgramState State(this,
  74. EnvMgr.getInitialEnvironment(),
  75. StoreMgr->getInitialStore(InitLoc),
  76. GDMFactory.getEmptyMap());
  77. return getPersistentState(State);
  78. }
  79. const ProgramState *ProgramState::bindCompoundLiteral(const CompoundLiteralExpr *CL,
  80. const LocationContext *LC,
  81. SVal V) const {
  82. const StoreRef &newStore =
  83. getStateManager().StoreMgr->BindCompoundLiteral(getStore(), CL, LC, V);
  84. return makeWithStore(newStore);
  85. }
  86. const ProgramState *ProgramState::bindDecl(const VarRegion* VR, SVal IVal) const {
  87. const StoreRef &newStore =
  88. getStateManager().StoreMgr->BindDecl(getStore(), VR, IVal);
  89. return makeWithStore(newStore);
  90. }
  91. const ProgramState *ProgramState::bindDeclWithNoInit(const VarRegion* VR) const {
  92. const StoreRef &newStore =
  93. getStateManager().StoreMgr->BindDeclWithNoInit(getStore(), VR);
  94. return makeWithStore(newStore);
  95. }
  96. const ProgramState *ProgramState::bindLoc(Loc LV, SVal V) const {
  97. ProgramStateManager &Mgr = getStateManager();
  98. const ProgramState *newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
  99. LV, V));
  100. const MemRegion *MR = LV.getAsRegion();
  101. if (MR && Mgr.getOwningEngine())
  102. return Mgr.getOwningEngine()->processRegionChange(newState, MR);
  103. return newState;
  104. }
  105. const ProgramState *ProgramState::bindDefault(SVal loc, SVal V) const {
  106. ProgramStateManager &Mgr = getStateManager();
  107. const MemRegion *R = cast<loc::MemRegionVal>(loc).getRegion();
  108. const StoreRef &newStore = Mgr.StoreMgr->BindDefault(getStore(), R, V);
  109. const ProgramState *new_state = makeWithStore(newStore);
  110. return Mgr.getOwningEngine() ?
  111. Mgr.getOwningEngine()->processRegionChange(new_state, R) :
  112. new_state;
  113. }
  114. const ProgramState *
  115. ProgramState::invalidateRegions(ArrayRef<const MemRegion *> Regions,
  116. const Expr *E, unsigned Count,
  117. StoreManager::InvalidatedSymbols *IS,
  118. bool invalidateGlobals) const {
  119. if (!IS) {
  120. StoreManager::InvalidatedSymbols invalidated;
  121. return invalidateRegionsImpl(Regions, E, Count,
  122. invalidated, invalidateGlobals);
  123. }
  124. return invalidateRegionsImpl(Regions, E, Count, *IS, invalidateGlobals);
  125. }
  126. const ProgramState *
  127. ProgramState::invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions,
  128. const Expr *E, unsigned Count,
  129. StoreManager::InvalidatedSymbols &IS,
  130. bool invalidateGlobals) const {
  131. ProgramStateManager &Mgr = getStateManager();
  132. SubEngine* Eng = Mgr.getOwningEngine();
  133. if (Eng && Eng->wantsRegionChangeUpdate(this)) {
  134. StoreManager::InvalidatedRegions Invalidated;
  135. const StoreRef &newStore
  136. = Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, IS,
  137. invalidateGlobals, &Invalidated);
  138. const ProgramState *newState = makeWithStore(newStore);
  139. return Eng->processRegionChanges(newState, &IS, Regions, Invalidated);
  140. }
  141. const StoreRef &newStore =
  142. Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, IS,
  143. invalidateGlobals, NULL);
  144. return makeWithStore(newStore);
  145. }
  146. const ProgramState *ProgramState::unbindLoc(Loc LV) const {
  147. assert(!isa<loc::MemRegionVal>(LV) && "Use invalidateRegion instead.");
  148. Store OldStore = getStore();
  149. const StoreRef &newStore = getStateManager().StoreMgr->Remove(OldStore, LV);
  150. if (newStore.getStore() == OldStore)
  151. return this;
  152. return makeWithStore(newStore);
  153. }
  154. const ProgramState *ProgramState::enterStackFrame(const StackFrameContext *frame) const {
  155. const StoreRef &new_store =
  156. getStateManager().StoreMgr->enterStackFrame(this, frame);
  157. return makeWithStore(new_store);
  158. }
  159. SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
  160. // We only want to do fetches from regions that we can actually bind
  161. // values. For example, SymbolicRegions of type 'id<...>' cannot
  162. // have direct bindings (but their can be bindings on their subregions).
  163. if (!R->isBoundable())
  164. return UnknownVal();
  165. if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
  166. QualType T = TR->getValueType();
  167. if (Loc::isLocType(T) || T->isIntegerType())
  168. return getSVal(R);
  169. }
  170. return UnknownVal();
  171. }
  172. SVal ProgramState::getSVal(Loc location, QualType T) const {
  173. SVal V = getRawSVal(cast<Loc>(location), T);
  174. // If 'V' is a symbolic value that is *perfectly* constrained to
  175. // be a constant value, use that value instead to lessen the burden
  176. // on later analysis stages (so we have less symbolic values to reason
  177. // about).
  178. if (!T.isNull()) {
  179. if (SymbolRef sym = V.getAsSymbol()) {
  180. if (const llvm::APSInt *Int = getSymVal(sym)) {
  181. // FIXME: Because we don't correctly model (yet) sign-extension
  182. // and truncation of symbolic values, we need to convert
  183. // the integer value to the correct signedness and bitwidth.
  184. //
  185. // This shows up in the following:
  186. //
  187. // char foo();
  188. // unsigned x = foo();
  189. // if (x == 54)
  190. // ...
  191. //
  192. // The symbolic value stored to 'x' is actually the conjured
  193. // symbol for the call to foo(); the type of that symbol is 'char',
  194. // not unsigned.
  195. const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
  196. if (isa<Loc>(V))
  197. return loc::ConcreteInt(NewV);
  198. else
  199. return nonloc::ConcreteInt(NewV);
  200. }
  201. }
  202. }
  203. return V;
  204. }
  205. const ProgramState *ProgramState::BindExpr(const Stmt *S, SVal V, bool Invalidate) const{
  206. Environment NewEnv = getStateManager().EnvMgr.bindExpr(Env, S, V,
  207. Invalidate);
  208. if (NewEnv == Env)
  209. return this;
  210. ProgramState NewSt = *this;
  211. NewSt.Env = NewEnv;
  212. return getStateManager().getPersistentState(NewSt);
  213. }
  214. const ProgramState *ProgramState::bindExprAndLocation(const Stmt *S, SVal location,
  215. SVal V) const {
  216. Environment NewEnv =
  217. getStateManager().EnvMgr.bindExprAndLocation(Env, S, location, V);
  218. if (NewEnv == Env)
  219. return this;
  220. ProgramState NewSt = *this;
  221. NewSt.Env = NewEnv;
  222. return getStateManager().getPersistentState(NewSt);
  223. }
  224. const ProgramState *ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
  225. DefinedOrUnknownSVal UpperBound,
  226. bool Assumption) const {
  227. if (Idx.isUnknown() || UpperBound.isUnknown())
  228. return this;
  229. // Build an expression for 0 <= Idx < UpperBound.
  230. // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
  231. // FIXME: This should probably be part of SValBuilder.
  232. ProgramStateManager &SM = getStateManager();
  233. SValBuilder &svalBuilder = SM.getSValBuilder();
  234. ASTContext &Ctx = svalBuilder.getContext();
  235. // Get the offset: the minimum value of the array index type.
  236. BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
  237. // FIXME: This should be using ValueManager::ArrayindexTy...somehow.
  238. QualType indexTy = Ctx.IntTy;
  239. nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
  240. // Adjust the index.
  241. SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
  242. cast<NonLoc>(Idx), Min, indexTy);
  243. if (newIdx.isUnknownOrUndef())
  244. return this;
  245. // Adjust the upper bound.
  246. SVal newBound =
  247. svalBuilder.evalBinOpNN(this, BO_Add, cast<NonLoc>(UpperBound),
  248. Min, indexTy);
  249. if (newBound.isUnknownOrUndef())
  250. return this;
  251. // Build the actual comparison.
  252. SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT,
  253. cast<NonLoc>(newIdx), cast<NonLoc>(newBound),
  254. Ctx.IntTy);
  255. if (inBound.isUnknownOrUndef())
  256. return this;
  257. // Finally, let the constraint manager take care of it.
  258. ConstraintManager &CM = SM.getConstraintManager();
  259. return CM.assume(this, cast<DefinedSVal>(inBound), Assumption);
  260. }
  261. const ProgramState *ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
  262. ProgramState State(this,
  263. EnvMgr.getInitialEnvironment(),
  264. StoreMgr->getInitialStore(InitLoc),
  265. GDMFactory.getEmptyMap());
  266. return getPersistentState(State);
  267. }
  268. void ProgramStateManager::recycleUnusedStates() {
  269. for (std::vector<ProgramState*>::iterator i = recentlyAllocatedStates.begin(),
  270. e = recentlyAllocatedStates.end(); i != e; ++i) {
  271. ProgramState *state = *i;
  272. if (state->referencedByExplodedNode())
  273. continue;
  274. StateSet.RemoveNode(state);
  275. freeStates.push_back(state);
  276. state->~ProgramState();
  277. }
  278. recentlyAllocatedStates.clear();
  279. }
  280. const ProgramState *ProgramStateManager::getPersistentStateWithGDM(
  281. const ProgramState *FromState,
  282. const ProgramState *GDMState) {
  283. ProgramState NewState = *FromState;
  284. NewState.GDM = GDMState->GDM;
  285. return getPersistentState(NewState);
  286. }
  287. const ProgramState *ProgramStateManager::getPersistentState(ProgramState &State) {
  288. llvm::FoldingSetNodeID ID;
  289. State.Profile(ID);
  290. void *InsertPos;
  291. if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
  292. return I;
  293. ProgramState *newState = 0;
  294. if (!freeStates.empty()) {
  295. newState = freeStates.back();
  296. freeStates.pop_back();
  297. }
  298. else {
  299. newState = (ProgramState*) Alloc.Allocate<ProgramState>();
  300. }
  301. new (newState) ProgramState(State);
  302. StateSet.InsertNode(newState, InsertPos);
  303. recentlyAllocatedStates.push_back(newState);
  304. return newState;
  305. }
  306. const ProgramState *ProgramState::makeWithStore(const StoreRef &store) const {
  307. ProgramState NewSt = *this;
  308. NewSt.setStore(store);
  309. return getStateManager().getPersistentState(NewSt);
  310. }
  311. void ProgramState::setStore(const StoreRef &newStore) {
  312. Store newStoreStore = newStore.getStore();
  313. if (newStoreStore)
  314. stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
  315. if (store)
  316. stateMgr->getStoreManager().decrementReferenceCount(store);
  317. store = newStoreStore;
  318. }
  319. //===----------------------------------------------------------------------===//
  320. // State pretty-printing.
  321. //===----------------------------------------------------------------------===//
  322. static bool IsEnvLoc(const Stmt *S) {
  323. // FIXME: This is a layering violation. Should be in environment.
  324. return (bool) (((uintptr_t) S) & 0x1);
  325. }
  326. void ProgramState::print(raw_ostream &Out, CFG &C,
  327. const char *NL, const char *Sep) const {
  328. // Print the store.
  329. ProgramStateManager &Mgr = getStateManager();
  330. Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
  331. // Print Subexpression bindings.
  332. bool isFirst = true;
  333. // FIXME: All environment printing should be moved inside Environment.
  334. for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
  335. if (C.isBlkExpr(I.getKey()) || IsEnvLoc(I.getKey()))
  336. continue;
  337. if (isFirst) {
  338. Out << NL << NL << "Sub-Expressions:" << NL;
  339. isFirst = false;
  340. } else {
  341. Out << NL;
  342. }
  343. Out << " (" << (void*) I.getKey() << ") ";
  344. LangOptions LO; // FIXME.
  345. I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
  346. Out << " : " << I.getData();
  347. }
  348. // Print block-expression bindings.
  349. isFirst = true;
  350. for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
  351. if (!C.isBlkExpr(I.getKey()))
  352. continue;
  353. if (isFirst) {
  354. Out << NL << NL << "Block-level Expressions:" << NL;
  355. isFirst = false;
  356. } else {
  357. Out << NL;
  358. }
  359. Out << " (" << (void*) I.getKey() << ") ";
  360. LangOptions LO; // FIXME.
  361. I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
  362. Out << " : " << I.getData();
  363. }
  364. // Print locations.
  365. isFirst = true;
  366. for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
  367. if (!IsEnvLoc(I.getKey()))
  368. continue;
  369. if (isFirst) {
  370. Out << NL << NL << "Load/store locations:" << NL;
  371. isFirst = false;
  372. } else {
  373. Out << NL;
  374. }
  375. const Stmt *S = (Stmt*) (((uintptr_t) I.getKey()) & ((uintptr_t) ~0x1));
  376. Out << " (" << (void*) S << ") ";
  377. LangOptions LO; // FIXME.
  378. S->printPretty(Out, 0, PrintingPolicy(LO));
  379. Out << " : " << I.getData();
  380. }
  381. Mgr.getConstraintManager().print(this, Out, NL, Sep);
  382. // Print checker-specific data.
  383. Mgr.getOwningEngine()->printState(Out, this, NL, Sep);
  384. }
  385. void ProgramState::printDOT(raw_ostream &Out, CFG &C) const {
  386. print(Out, C, "\\l", "\\|");
  387. }
  388. void ProgramState::printStdErr(CFG &C) const {
  389. print(llvm::errs(), C);
  390. }
  391. //===----------------------------------------------------------------------===//
  392. // Generic Data Map.
  393. //===----------------------------------------------------------------------===//
  394. void *const* ProgramState::FindGDM(void *K) const {
  395. return GDM.lookup(K);
  396. }
  397. void*
  398. ProgramStateManager::FindGDMContext(void *K,
  399. void *(*CreateContext)(llvm::BumpPtrAllocator&),
  400. void (*DeleteContext)(void*)) {
  401. std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
  402. if (!p.first) {
  403. p.first = CreateContext(Alloc);
  404. p.second = DeleteContext;
  405. }
  406. return p.first;
  407. }
  408. const ProgramState *ProgramStateManager::addGDM(const ProgramState *St, void *Key, void *Data){
  409. ProgramState::GenericDataMap M1 = St->getGDM();
  410. ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
  411. if (M1 == M2)
  412. return St;
  413. ProgramState NewSt = *St;
  414. NewSt.GDM = M2;
  415. return getPersistentState(NewSt);
  416. }
  417. const ProgramState *ProgramStateManager::removeGDM(const ProgramState *state, void *Key) {
  418. ProgramState::GenericDataMap OldM = state->getGDM();
  419. ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
  420. if (NewM == OldM)
  421. return state;
  422. ProgramState NewState = *state;
  423. NewState.GDM = NewM;
  424. return getPersistentState(NewState);
  425. }
  426. bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
  427. for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
  428. if (!scan(*I))
  429. return false;
  430. return true;
  431. }
  432. bool ScanReachableSymbols::scan(const SymExpr *sym) {
  433. unsigned &isVisited = visited[sym];
  434. if (isVisited)
  435. return true;
  436. isVisited = 1;
  437. if (const SymbolData *sData = dyn_cast<SymbolData>(sym))
  438. if (!visitor.VisitSymbol(sData))
  439. return false;
  440. switch (sym->getKind()) {
  441. case SymExpr::RegionValueKind:
  442. case SymExpr::ConjuredKind:
  443. case SymExpr::DerivedKind:
  444. case SymExpr::ExtentKind:
  445. case SymExpr::MetadataKind:
  446. break;
  447. case SymExpr::SymIntKind:
  448. return scan(cast<SymIntExpr>(sym)->getLHS());
  449. case SymExpr::SymSymKind: {
  450. const SymSymExpr *x = cast<SymSymExpr>(sym);
  451. return scan(x->getLHS()) && scan(x->getRHS());
  452. }
  453. }
  454. return true;
  455. }
  456. bool ScanReachableSymbols::scan(SVal val) {
  457. if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
  458. return scan(X->getRegion());
  459. if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val))
  460. return scan(X->getLoc());
  461. if (SymbolRef Sym = val.getAsSymbol())
  462. return scan(Sym);
  463. if (const SymExpr *Sym = val.getAsSymbolicExpression())
  464. return scan(Sym);
  465. if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
  466. return scan(*X);
  467. return true;
  468. }
  469. bool ScanReachableSymbols::scan(const MemRegion *R) {
  470. if (isa<MemSpaceRegion>(R))
  471. return true;
  472. unsigned &isVisited = visited[R];
  473. if (isVisited)
  474. return true;
  475. isVisited = 1;
  476. // If this is a symbolic region, visit the symbol for the region.
  477. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
  478. if (!visitor.VisitSymbol(SR->getSymbol()))
  479. return false;
  480. // If this is a subregion, also visit the parent regions.
  481. if (const SubRegion *SR = dyn_cast<SubRegion>(R))
  482. if (!scan(SR->getSuperRegion()))
  483. return false;
  484. // Now look at the binding to this region (if any).
  485. if (!scan(state->getSValAsScalarOrLoc(R)))
  486. return false;
  487. // Now look at the subregions.
  488. if (!SRM.get())
  489. SRM.reset(state->getStateManager().getStoreManager().
  490. getSubRegionMap(state->getStore()));
  491. return SRM->iterSubRegions(R, *this);
  492. }
  493. bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
  494. ScanReachableSymbols S(this, visitor);
  495. return S.scan(val);
  496. }
  497. bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
  498. SymbolVisitor &visitor) const {
  499. ScanReachableSymbols S(this, visitor);
  500. for ( ; I != E; ++I) {
  501. if (!S.scan(*I))
  502. return false;
  503. }
  504. return true;
  505. }
  506. bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
  507. const MemRegion * const *E,
  508. SymbolVisitor &visitor) const {
  509. ScanReachableSymbols S(this, visitor);
  510. for ( ; I != E; ++I) {
  511. if (!S.scan(*I))
  512. return false;
  513. }
  514. return true;
  515. }
  516. const ProgramState* ProgramState::addTaint(const Stmt *S,
  517. TaintTagType Kind) const {
  518. SymbolRef Sym = getSVal(S).getAsSymbol();
  519. assert(Sym && "Cannot add taint to statements whose value is not a symbol");
  520. return addTaint(Sym, Kind);
  521. }
  522. const ProgramState* ProgramState::addTaint(SymbolRef Sym,
  523. TaintTagType Kind) const {
  524. const ProgramState *NewState = set<TaintMap>(Sym, Kind);
  525. assert(NewState);
  526. return NewState;
  527. }
  528. bool ProgramState::isTainted(const Stmt *S, TaintTagType Kind) const {
  529. return isTainted(getSVal(S), Kind);
  530. }
  531. bool ProgramState::isTainted(SVal V, TaintTagType Kind) const {
  532. return isTainted(V.getAsSymExpr(), Kind);
  533. }
  534. bool ProgramState::isTainted(const SymExpr* Sym, TaintTagType Kind) const {
  535. if (!Sym)
  536. return false;
  537. // Check taint on derived symbols.
  538. if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(Sym))
  539. return isTainted(SD->getParentSymbol(), Kind);
  540. if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(Sym))
  541. return isTainted(SIE->getLHS(), Kind);
  542. if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(Sym))
  543. return (isTainted(SSE->getLHS(), Kind) || isTainted(SSE->getRHS(), Kind));
  544. // Check taint on the current symbol.
  545. if (const SymbolData *SymR = dyn_cast<SymbolData>(Sym)) {
  546. const TaintTagType *Tag = get<TaintMap>(SymR);
  547. return (Tag && *Tag == Kind);
  548. }
  549. // TODO: Remove llvm unreachable.
  550. llvm_unreachable("We do not know show to check taint on this symbol.");
  551. return false;
  552. }