ProgramState.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. bool isFirst = true;
  332. // FIXME: All environment printing should be moved inside Environment.
  333. if (C) {
  334. // Print Subexpression bindings.
  335. for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
  336. if (C->isBlkExpr(I.getKey()) || IsEnvLoc(I.getKey()))
  337. continue;
  338. if (isFirst) {
  339. Out << NL << NL << "Sub-Expressions:" << NL;
  340. isFirst = false;
  341. } else {
  342. Out << NL;
  343. }
  344. Out << " (" << (void*) I.getKey() << ") ";
  345. LangOptions LO; // FIXME.
  346. I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
  347. Out << " : " << I.getData();
  348. }
  349. // Print block-expression bindings.
  350. isFirst = true;
  351. for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
  352. if (!C->isBlkExpr(I.getKey()))
  353. continue;
  354. if (isFirst) {
  355. Out << NL << NL << "Block-level Expressions:" << NL;
  356. isFirst = false;
  357. } else {
  358. Out << NL;
  359. }
  360. Out << " (" << (void*) I.getKey() << ") ";
  361. LangOptions LO; // FIXME.
  362. I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
  363. Out << " : " << I.getData();
  364. }
  365. } else {
  366. // Print All bindings - no info to differentiate block from subexpressions.
  367. for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
  368. if (IsEnvLoc(I.getKey()))
  369. continue;
  370. if (isFirst) {
  371. Out << NL << NL << "Expressions:" << NL;
  372. isFirst = false;
  373. } else {
  374. Out << NL;
  375. }
  376. Out << " (" << (void*) I.getKey() << ") ";
  377. LangOptions LO; // FIXME.
  378. I.getKey()->printPretty(Out, 0, PrintingPolicy(LO));
  379. Out << " : " << I.getData();
  380. }
  381. }
  382. // Print locations.
  383. isFirst = true;
  384. for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) {
  385. if (!IsEnvLoc(I.getKey()))
  386. continue;
  387. if (isFirst) {
  388. Out << NL << NL << "Load/store locations:" << NL;
  389. isFirst = false;
  390. } else {
  391. Out << NL;
  392. }
  393. const Stmt *S = (Stmt*) (((uintptr_t) I.getKey()) & ((uintptr_t) ~0x1));
  394. Out << " (" << (void*) S << ") ";
  395. LangOptions LO; // FIXME.
  396. S->printPretty(Out, 0, PrintingPolicy(LO));
  397. Out << " : " << I.getData();
  398. }
  399. Mgr.getConstraintManager().print(this, Out, NL, Sep);
  400. // Print checker-specific data.
  401. Mgr.getOwningEngine()->printState(Out, this, NL, Sep);
  402. }
  403. void ProgramState::printDOT(raw_ostream &Out, CFG &C) const {
  404. print(Out, &C, "\\l", "\\|");
  405. }
  406. void ProgramState::dump(CFG &C) const {
  407. print(llvm::errs(), &C);
  408. }
  409. void ProgramState::dump() const {
  410. print(llvm::errs(), 0);
  411. }
  412. //===----------------------------------------------------------------------===//
  413. // Generic Data Map.
  414. //===----------------------------------------------------------------------===//
  415. void *const* ProgramState::FindGDM(void *K) const {
  416. return GDM.lookup(K);
  417. }
  418. void*
  419. ProgramStateManager::FindGDMContext(void *K,
  420. void *(*CreateContext)(llvm::BumpPtrAllocator&),
  421. void (*DeleteContext)(void*)) {
  422. std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
  423. if (!p.first) {
  424. p.first = CreateContext(Alloc);
  425. p.second = DeleteContext;
  426. }
  427. return p.first;
  428. }
  429. const ProgramState *ProgramStateManager::addGDM(const ProgramState *St, void *Key, void *Data){
  430. ProgramState::GenericDataMap M1 = St->getGDM();
  431. ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
  432. if (M1 == M2)
  433. return St;
  434. ProgramState NewSt = *St;
  435. NewSt.GDM = M2;
  436. return getPersistentState(NewSt);
  437. }
  438. const ProgramState *ProgramStateManager::removeGDM(const ProgramState *state, void *Key) {
  439. ProgramState::GenericDataMap OldM = state->getGDM();
  440. ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
  441. if (NewM == OldM)
  442. return state;
  443. ProgramState NewState = *state;
  444. NewState.GDM = NewM;
  445. return getPersistentState(NewState);
  446. }
  447. bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
  448. for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
  449. if (!scan(*I))
  450. return false;
  451. return true;
  452. }
  453. bool ScanReachableSymbols::scan(const SymExpr *sym) {
  454. unsigned &isVisited = visited[sym];
  455. if (isVisited)
  456. return true;
  457. isVisited = 1;
  458. if (!visitor.VisitSymbol(sym))
  459. return false;
  460. switch (sym->getKind()) {
  461. case SymExpr::RegionValueKind:
  462. case SymExpr::ConjuredKind:
  463. case SymExpr::DerivedKind:
  464. case SymExpr::ExtentKind:
  465. case SymExpr::MetadataKind:
  466. break;
  467. case SymExpr::CastSymbolKind:
  468. return scan(cast<SymbolCast>(sym)->getOperand());
  469. case SymExpr::SymIntKind:
  470. return scan(cast<SymIntExpr>(sym)->getLHS());
  471. case SymExpr::SymSymKind: {
  472. const SymSymExpr *x = cast<SymSymExpr>(sym);
  473. return scan(x->getLHS()) && scan(x->getRHS());
  474. }
  475. }
  476. return true;
  477. }
  478. bool ScanReachableSymbols::scan(SVal val) {
  479. if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val))
  480. return scan(X->getRegion());
  481. if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val))
  482. return scan(X->getLoc());
  483. if (SymbolRef Sym = val.getAsSymbol())
  484. return scan(Sym);
  485. if (const SymExpr *Sym = val.getAsSymbolicExpression())
  486. return scan(Sym);
  487. if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val))
  488. return scan(*X);
  489. return true;
  490. }
  491. bool ScanReachableSymbols::scan(const MemRegion *R) {
  492. if (isa<MemSpaceRegion>(R))
  493. return true;
  494. unsigned &isVisited = visited[R];
  495. if (isVisited)
  496. return true;
  497. isVisited = 1;
  498. // If this is a symbolic region, visit the symbol for the region.
  499. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
  500. if (!visitor.VisitSymbol(SR->getSymbol()))
  501. return false;
  502. // If this is a subregion, also visit the parent regions.
  503. if (const SubRegion *SR = dyn_cast<SubRegion>(R))
  504. if (!scan(SR->getSuperRegion()))
  505. return false;
  506. // Now look at the binding to this region (if any).
  507. if (!scan(state->getSValAsScalarOrLoc(R)))
  508. return false;
  509. // Now look at the subregions.
  510. if (!SRM.get())
  511. SRM.reset(state->getStateManager().getStoreManager().
  512. getSubRegionMap(state->getStore()));
  513. return SRM->iterSubRegions(R, *this);
  514. }
  515. bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
  516. ScanReachableSymbols S(this, visitor);
  517. return S.scan(val);
  518. }
  519. bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
  520. SymbolVisitor &visitor) const {
  521. ScanReachableSymbols S(this, visitor);
  522. for ( ; I != E; ++I) {
  523. if (!S.scan(*I))
  524. return false;
  525. }
  526. return true;
  527. }
  528. bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
  529. const MemRegion * const *E,
  530. SymbolVisitor &visitor) const {
  531. ScanReachableSymbols S(this, visitor);
  532. for ( ; I != E; ++I) {
  533. if (!S.scan(*I))
  534. return false;
  535. }
  536. return true;
  537. }
  538. const ProgramState* ProgramState::addTaint(const Stmt *S,
  539. TaintTagType Kind) const {
  540. SymbolRef Sym = getSVal(S).getAsSymbol();
  541. assert(Sym && "Cannot add taint to statements whose value is not a symbol");
  542. return addTaint(Sym, Kind);
  543. }
  544. const ProgramState* ProgramState::addTaint(SymbolRef Sym,
  545. TaintTagType Kind) const {
  546. const ProgramState *NewState = set<TaintMap>(Sym, Kind);
  547. assert(NewState);
  548. return NewState;
  549. }
  550. bool ProgramState::isTainted(const Stmt *S, TaintTagType Kind) const {
  551. return isTainted(getSVal(S), Kind);
  552. }
  553. bool ProgramState::isTainted(SVal V, TaintTagType Kind) const {
  554. return isTainted(V.getAsSymExpr(), Kind);
  555. }
  556. bool ProgramState::isTainted(const SymExpr* Sym, TaintTagType Kind) const {
  557. if (!Sym)
  558. return false;
  559. // Travese all the symbols this symbol depends on to see if any are tainted.
  560. bool Tainted = false;
  561. for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
  562. SI != SE; ++SI) {
  563. assert(isa<SymbolData>(*SI));
  564. const TaintTagType *Tag = get<TaintMap>(*SI);
  565. Tainted = (Tag && *Tag == Kind);
  566. // If this is a SymbolDerived with a tainted parent, it's also tainted.
  567. if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI))
  568. Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind);
  569. if (Tainted)
  570. return true;
  571. }
  572. return Tainted;
  573. }