ProgramState.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  14. #include "clang/Analysis/CFG.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace clang;
  21. using namespace ento;
  22. namespace clang { namespace ento {
  23. /// Increments the number of times this state is referenced.
  24. void ProgramStateRetain(const ProgramState *state) {
  25. ++const_cast<ProgramState*>(state)->refCount;
  26. }
  27. /// Decrement the number of times this state is referenced.
  28. void ProgramStateRelease(const ProgramState *state) {
  29. assert(state->refCount > 0);
  30. ProgramState *s = const_cast<ProgramState*>(state);
  31. if (--s->refCount == 0) {
  32. ProgramStateManager &Mgr = s->getStateManager();
  33. Mgr.StateSet.RemoveNode(s);
  34. s->~ProgramState();
  35. Mgr.freeStates.push_back(s);
  36. }
  37. }
  38. }}
  39. ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env,
  40. StoreRef st, GenericDataMap gdm)
  41. : stateMgr(mgr),
  42. Env(env),
  43. store(st.getStore()),
  44. GDM(gdm),
  45. refCount(0) {
  46. stateMgr->getStoreManager().incrementReferenceCount(store);
  47. }
  48. ProgramState::ProgramState(const ProgramState &RHS)
  49. : llvm::FoldingSetNode(),
  50. stateMgr(RHS.stateMgr),
  51. Env(RHS.Env),
  52. store(RHS.store),
  53. GDM(RHS.GDM),
  54. refCount(0) {
  55. stateMgr->getStoreManager().incrementReferenceCount(store);
  56. }
  57. ProgramState::~ProgramState() {
  58. if (store)
  59. stateMgr->getStoreManager().decrementReferenceCount(store);
  60. }
  61. ProgramStateManager::ProgramStateManager(ASTContext &Ctx,
  62. StoreManagerCreator CreateSMgr,
  63. ConstraintManagerCreator CreateCMgr,
  64. llvm::BumpPtrAllocator &alloc,
  65. SubEngine *SubEng)
  66. : Eng(SubEng), EnvMgr(alloc), GDMFactory(alloc),
  67. svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
  68. CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) {
  69. StoreMgr.reset((*CreateSMgr)(*this));
  70. ConstraintMgr.reset((*CreateCMgr)(*this, SubEng));
  71. }
  72. ProgramStateManager::~ProgramStateManager() {
  73. for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
  74. I!=E; ++I)
  75. I->second.second(I->second.first);
  76. }
  77. ProgramStateRef
  78. ProgramStateManager::removeDeadBindings(ProgramStateRef state,
  79. const StackFrameContext *LCtx,
  80. SymbolReaper& SymReaper) {
  81. // This code essentially performs a "mark-and-sweep" of the VariableBindings.
  82. // The roots are any Block-level exprs and Decls that our liveness algorithm
  83. // tells us are live. We then see what Decls they may reference, and keep
  84. // those around. This code more than likely can be made faster, and the
  85. // frequency of which this method is called should be experimented with
  86. // for optimum performance.
  87. ProgramState NewState = *state;
  88. NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state);
  89. // Clean up the store.
  90. StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx,
  91. SymReaper);
  92. NewState.setStore(newStore);
  93. SymReaper.setReapedStore(newStore);
  94. ProgramStateRef Result = getPersistentState(NewState);
  95. return ConstraintMgr->removeDeadBindings(Result, SymReaper);
  96. }
  97. ProgramStateRef ProgramState::bindCompoundLiteral(const CompoundLiteralExpr *CL,
  98. const LocationContext *LC,
  99. SVal V) const {
  100. const StoreRef &newStore =
  101. getStateManager().StoreMgr->bindCompoundLiteral(getStore(), CL, LC, V);
  102. return makeWithStore(newStore);
  103. }
  104. ProgramStateRef ProgramState::bindLoc(Loc LV, SVal V, bool notifyChanges) const {
  105. ProgramStateManager &Mgr = getStateManager();
  106. ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
  107. LV, V));
  108. const MemRegion *MR = LV.getAsRegion();
  109. if (MR && Mgr.getOwningEngine() && notifyChanges)
  110. return Mgr.getOwningEngine()->processRegionChange(newState, MR);
  111. return newState;
  112. }
  113. ProgramStateRef ProgramState::bindDefault(SVal loc, SVal V) const {
  114. ProgramStateManager &Mgr = getStateManager();
  115. const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
  116. const StoreRef &newStore = Mgr.StoreMgr->BindDefault(getStore(), R, V);
  117. ProgramStateRef new_state = makeWithStore(newStore);
  118. return Mgr.getOwningEngine() ?
  119. Mgr.getOwningEngine()->processRegionChange(new_state, R) :
  120. new_state;
  121. }
  122. typedef ArrayRef<const MemRegion *> RegionList;
  123. typedef ArrayRef<SVal> ValueList;
  124. ProgramStateRef
  125. ProgramState::invalidateRegions(RegionList Regions,
  126. const Expr *E, unsigned Count,
  127. const LocationContext *LCtx,
  128. bool CausedByPointerEscape,
  129. InvalidatedSymbols *IS,
  130. const CallEvent *Call,
  131. RegionList ConstRegions) const {
  132. SmallVector<SVal, 8> Values;
  133. for (RegionList::const_iterator I = Regions.begin(),
  134. End = Regions.end(); I != End; ++I)
  135. Values.push_back(loc::MemRegionVal(*I));
  136. SmallVector<SVal, 8> ConstValues;
  137. for (RegionList::const_iterator I = ConstRegions.begin(),
  138. End = ConstRegions.end(); I != End; ++I)
  139. ConstValues.push_back(loc::MemRegionVal(*I));
  140. if (!IS) {
  141. InvalidatedSymbols invalidated;
  142. return invalidateRegionsImpl(Values, E, Count, LCtx,
  143. CausedByPointerEscape,
  144. invalidated, Call, ConstValues);
  145. }
  146. return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
  147. *IS, Call, ConstValues);
  148. }
  149. ProgramStateRef
  150. ProgramState::invalidateRegions(ValueList Values,
  151. const Expr *E, unsigned Count,
  152. const LocationContext *LCtx,
  153. bool CausedByPointerEscape,
  154. InvalidatedSymbols *IS,
  155. const CallEvent *Call,
  156. ValueList ConstValues) const {
  157. if (!IS) {
  158. InvalidatedSymbols invalidated;
  159. return invalidateRegionsImpl(Values, E, Count, LCtx,
  160. CausedByPointerEscape,
  161. invalidated, Call, ConstValues);
  162. }
  163. return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
  164. *IS, Call, ConstValues);
  165. }
  166. ProgramStateRef
  167. ProgramState::invalidateRegionsImpl(ValueList Values,
  168. const Expr *E, unsigned Count,
  169. const LocationContext *LCtx,
  170. bool CausedByPointerEscape,
  171. InvalidatedSymbols &IS,
  172. const CallEvent *Call,
  173. ValueList ConstValues) const {
  174. ProgramStateManager &Mgr = getStateManager();
  175. SubEngine* Eng = Mgr.getOwningEngine();
  176. InvalidatedSymbols ConstIS;
  177. if (Eng) {
  178. StoreManager::InvalidatedRegions TopLevelInvalidated;
  179. StoreManager::InvalidatedRegions TopLevelConstInvalidated;
  180. StoreManager::InvalidatedRegions Invalidated;
  181. const StoreRef &newStore
  182. = Mgr.StoreMgr->invalidateRegions(getStore(), Values, ConstValues,
  183. E, Count, LCtx, Call,
  184. IS, ConstIS,
  185. &TopLevelInvalidated,
  186. &TopLevelConstInvalidated,
  187. &Invalidated);
  188. ProgramStateRef newState = makeWithStore(newStore);
  189. if (CausedByPointerEscape) {
  190. newState = Eng->notifyCheckersOfPointerEscape(newState, &IS,
  191. TopLevelInvalidated,
  192. Invalidated, Call);
  193. if (!ConstValues.empty()) {
  194. StoreManager::InvalidatedRegions Empty;
  195. newState = Eng->notifyCheckersOfPointerEscape(newState, &ConstIS,
  196. TopLevelConstInvalidated,
  197. Empty, Call,
  198. true);
  199. }
  200. }
  201. return Eng->processRegionChanges(newState, &IS,
  202. TopLevelInvalidated, Invalidated,
  203. Call);
  204. }
  205. const StoreRef &newStore =
  206. Mgr.StoreMgr->invalidateRegions(getStore(), Values, ConstValues,
  207. E, Count, LCtx, Call,
  208. IS, ConstIS, NULL, NULL, NULL);
  209. return makeWithStore(newStore);
  210. }
  211. ProgramStateRef ProgramState::killBinding(Loc LV) const {
  212. assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead.");
  213. Store OldStore = getStore();
  214. const StoreRef &newStore =
  215. getStateManager().StoreMgr->killBinding(OldStore, LV);
  216. if (newStore.getStore() == OldStore)
  217. return this;
  218. return makeWithStore(newStore);
  219. }
  220. ProgramStateRef
  221. ProgramState::enterStackFrame(const CallEvent &Call,
  222. const StackFrameContext *CalleeCtx) const {
  223. const StoreRef &NewStore =
  224. getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
  225. return makeWithStore(NewStore);
  226. }
  227. SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
  228. // We only want to do fetches from regions that we can actually bind
  229. // values. For example, SymbolicRegions of type 'id<...>' cannot
  230. // have direct bindings (but their can be bindings on their subregions).
  231. if (!R->isBoundable())
  232. return UnknownVal();
  233. if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
  234. QualType T = TR->getValueType();
  235. if (Loc::isLocType(T) || T->isIntegralOrEnumerationType())
  236. return getSVal(R);
  237. }
  238. return UnknownVal();
  239. }
  240. SVal ProgramState::getSVal(Loc location, QualType T) const {
  241. SVal V = getRawSVal(cast<Loc>(location), T);
  242. // If 'V' is a symbolic value that is *perfectly* constrained to
  243. // be a constant value, use that value instead to lessen the burden
  244. // on later analysis stages (so we have less symbolic values to reason
  245. // about).
  246. if (!T.isNull()) {
  247. if (SymbolRef sym = V.getAsSymbol()) {
  248. if (const llvm::APSInt *Int = getStateManager()
  249. .getConstraintManager()
  250. .getSymVal(this, sym)) {
  251. // FIXME: Because we don't correctly model (yet) sign-extension
  252. // and truncation of symbolic values, we need to convert
  253. // the integer value to the correct signedness and bitwidth.
  254. //
  255. // This shows up in the following:
  256. //
  257. // char foo();
  258. // unsigned x = foo();
  259. // if (x == 54)
  260. // ...
  261. //
  262. // The symbolic value stored to 'x' is actually the conjured
  263. // symbol for the call to foo(); the type of that symbol is 'char',
  264. // not unsigned.
  265. const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
  266. if (V.getAs<Loc>())
  267. return loc::ConcreteInt(NewV);
  268. else
  269. return nonloc::ConcreteInt(NewV);
  270. }
  271. }
  272. }
  273. return V;
  274. }
  275. ProgramStateRef ProgramState::BindExpr(const Stmt *S,
  276. const LocationContext *LCtx,
  277. SVal V, bool Invalidate) const{
  278. Environment NewEnv =
  279. getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
  280. Invalidate);
  281. if (NewEnv == Env)
  282. return this;
  283. ProgramState NewSt = *this;
  284. NewSt.Env = NewEnv;
  285. return getStateManager().getPersistentState(NewSt);
  286. }
  287. ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
  288. DefinedOrUnknownSVal UpperBound,
  289. bool Assumption,
  290. QualType indexTy) const {
  291. if (Idx.isUnknown() || UpperBound.isUnknown())
  292. return this;
  293. // Build an expression for 0 <= Idx < UpperBound.
  294. // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
  295. // FIXME: This should probably be part of SValBuilder.
  296. ProgramStateManager &SM = getStateManager();
  297. SValBuilder &svalBuilder = SM.getSValBuilder();
  298. ASTContext &Ctx = svalBuilder.getContext();
  299. // Get the offset: the minimum value of the array index type.
  300. BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
  301. // FIXME: This should be using ValueManager::ArrayindexTy...somehow.
  302. if (indexTy.isNull())
  303. indexTy = Ctx.IntTy;
  304. nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
  305. // Adjust the index.
  306. SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
  307. Idx.castAs<NonLoc>(), Min, indexTy);
  308. if (newIdx.isUnknownOrUndef())
  309. return this;
  310. // Adjust the upper bound.
  311. SVal newBound =
  312. svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
  313. Min, indexTy);
  314. if (newBound.isUnknownOrUndef())
  315. return this;
  316. // Build the actual comparison.
  317. SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(),
  318. newBound.castAs<NonLoc>(), Ctx.IntTy);
  319. if (inBound.isUnknownOrUndef())
  320. return this;
  321. // Finally, let the constraint manager take care of it.
  322. ConstraintManager &CM = SM.getConstraintManager();
  323. return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption);
  324. }
  325. ConditionTruthVal ProgramState::isNull(SVal V) const {
  326. if (V.isZeroConstant())
  327. return true;
  328. if (V.isConstant())
  329. return false;
  330. SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
  331. if (!Sym)
  332. return ConditionTruthVal();
  333. return getStateManager().ConstraintMgr->isNull(this, Sym);
  334. }
  335. ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
  336. ProgramState State(this,
  337. EnvMgr.getInitialEnvironment(),
  338. StoreMgr->getInitialStore(InitLoc),
  339. GDMFactory.getEmptyMap());
  340. return getPersistentState(State);
  341. }
  342. ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
  343. ProgramStateRef FromState,
  344. ProgramStateRef GDMState) {
  345. ProgramState NewState(*FromState);
  346. NewState.GDM = GDMState->GDM;
  347. return getPersistentState(NewState);
  348. }
  349. ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
  350. llvm::FoldingSetNodeID ID;
  351. State.Profile(ID);
  352. void *InsertPos;
  353. if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
  354. return I;
  355. ProgramState *newState = 0;
  356. if (!freeStates.empty()) {
  357. newState = freeStates.back();
  358. freeStates.pop_back();
  359. }
  360. else {
  361. newState = (ProgramState*) Alloc.Allocate<ProgramState>();
  362. }
  363. new (newState) ProgramState(State);
  364. StateSet.InsertNode(newState, InsertPos);
  365. return newState;
  366. }
  367. ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
  368. ProgramState NewSt(*this);
  369. NewSt.setStore(store);
  370. return getStateManager().getPersistentState(NewSt);
  371. }
  372. void ProgramState::setStore(const StoreRef &newStore) {
  373. Store newStoreStore = newStore.getStore();
  374. if (newStoreStore)
  375. stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
  376. if (store)
  377. stateMgr->getStoreManager().decrementReferenceCount(store);
  378. store = newStoreStore;
  379. }
  380. //===----------------------------------------------------------------------===//
  381. // State pretty-printing.
  382. //===----------------------------------------------------------------------===//
  383. void ProgramState::print(raw_ostream &Out,
  384. const char *NL, const char *Sep) const {
  385. // Print the store.
  386. ProgramStateManager &Mgr = getStateManager();
  387. Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
  388. // Print out the environment.
  389. Env.print(Out, NL, Sep);
  390. // Print out the constraints.
  391. Mgr.getConstraintManager().print(this, Out, NL, Sep);
  392. // Print checker-specific data.
  393. Mgr.getOwningEngine()->printState(Out, this, NL, Sep);
  394. }
  395. void ProgramState::printDOT(raw_ostream &Out) const {
  396. print(Out, "\\l", "\\|");
  397. }
  398. void ProgramState::dump() const {
  399. print(llvm::errs());
  400. }
  401. void ProgramState::printTaint(raw_ostream &Out,
  402. const char *NL, const char *Sep) const {
  403. TaintMapImpl TM = get<TaintMap>();
  404. if (!TM.isEmpty())
  405. Out <<"Tainted Symbols:" << NL;
  406. for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) {
  407. Out << I->first << " : " << I->second << NL;
  408. }
  409. }
  410. void ProgramState::dumpTaint() const {
  411. printTaint(llvm::errs());
  412. }
  413. //===----------------------------------------------------------------------===//
  414. // Generic Data Map.
  415. //===----------------------------------------------------------------------===//
  416. void *const* ProgramState::FindGDM(void *K) const {
  417. return GDM.lookup(K);
  418. }
  419. void*
  420. ProgramStateManager::FindGDMContext(void *K,
  421. void *(*CreateContext)(llvm::BumpPtrAllocator&),
  422. void (*DeleteContext)(void*)) {
  423. std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
  424. if (!p.first) {
  425. p.first = CreateContext(Alloc);
  426. p.second = DeleteContext;
  427. }
  428. return p.first;
  429. }
  430. ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){
  431. ProgramState::GenericDataMap M1 = St->getGDM();
  432. ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
  433. if (M1 == M2)
  434. return St;
  435. ProgramState NewSt = *St;
  436. NewSt.GDM = M2;
  437. return getPersistentState(NewSt);
  438. }
  439. ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) {
  440. ProgramState::GenericDataMap OldM = state->getGDM();
  441. ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
  442. if (NewM == OldM)
  443. return state;
  444. ProgramState NewState = *state;
  445. NewState.GDM = NewM;
  446. return getPersistentState(NewState);
  447. }
  448. bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
  449. for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
  450. if (!scan(*I))
  451. return false;
  452. return true;
  453. }
  454. bool ScanReachableSymbols::scan(const SymExpr *sym) {
  455. unsigned &isVisited = visited[sym];
  456. if (isVisited)
  457. return true;
  458. isVisited = 1;
  459. if (!visitor.VisitSymbol(sym))
  460. return false;
  461. // TODO: should be rewritten using SymExpr::symbol_iterator.
  462. switch (sym->getKind()) {
  463. case SymExpr::RegionValueKind:
  464. case SymExpr::ConjuredKind:
  465. case SymExpr::DerivedKind:
  466. case SymExpr::ExtentKind:
  467. case SymExpr::MetadataKind:
  468. break;
  469. case SymExpr::CastSymbolKind:
  470. return scan(cast<SymbolCast>(sym)->getOperand());
  471. case SymExpr::SymIntKind:
  472. return scan(cast<SymIntExpr>(sym)->getLHS());
  473. case SymExpr::IntSymKind:
  474. return scan(cast<IntSymExpr>(sym)->getRHS());
  475. case SymExpr::SymSymKind: {
  476. const SymSymExpr *x = cast<SymSymExpr>(sym);
  477. return scan(x->getLHS()) && scan(x->getRHS());
  478. }
  479. }
  480. return true;
  481. }
  482. bool ScanReachableSymbols::scan(SVal val) {
  483. if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>())
  484. return scan(X->getRegion());
  485. if (Optional<nonloc::LazyCompoundVal> X =
  486. val.getAs<nonloc::LazyCompoundVal>()) {
  487. StoreManager &StoreMgr = state->getStateManager().getStoreManager();
  488. // FIXME: We don't really want to use getBaseRegion() here because pointer
  489. // arithmetic doesn't apply, but scanReachableSymbols only accepts base
  490. // regions right now.
  491. if (!StoreMgr.scanReachableSymbols(X->getStore(),
  492. X->getRegion()->getBaseRegion(),
  493. *this))
  494. return false;
  495. }
  496. if (Optional<nonloc::LocAsInteger> X = val.getAs<nonloc::LocAsInteger>())
  497. return scan(X->getLoc());
  498. if (SymbolRef Sym = val.getAsSymbol())
  499. return scan(Sym);
  500. if (const SymExpr *Sym = val.getAsSymbolicExpression())
  501. return scan(Sym);
  502. if (Optional<nonloc::CompoundVal> X = val.getAs<nonloc::CompoundVal>())
  503. return scan(*X);
  504. return true;
  505. }
  506. bool ScanReachableSymbols::scan(const MemRegion *R) {
  507. if (isa<MemSpaceRegion>(R))
  508. return true;
  509. unsigned &isVisited = visited[R];
  510. if (isVisited)
  511. return true;
  512. isVisited = 1;
  513. if (!visitor.VisitMemRegion(R))
  514. return false;
  515. // If this is a symbolic region, visit the symbol for the region.
  516. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
  517. if (!visitor.VisitSymbol(SR->getSymbol()))
  518. return false;
  519. // If this is a subregion, also visit the parent regions.
  520. if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
  521. const MemRegion *Super = SR->getSuperRegion();
  522. if (!scan(Super))
  523. return false;
  524. // When we reach the topmost region, scan all symbols in it.
  525. if (isa<MemSpaceRegion>(Super)) {
  526. StoreManager &StoreMgr = state->getStateManager().getStoreManager();
  527. if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
  528. return false;
  529. }
  530. }
  531. // Regions captured by a block are also implicitly reachable.
  532. if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
  533. BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
  534. E = BDR->referenced_vars_end();
  535. for ( ; I != E; ++I) {
  536. if (!scan(I.getCapturedRegion()))
  537. return false;
  538. }
  539. }
  540. return true;
  541. }
  542. bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
  543. ScanReachableSymbols S(this, visitor);
  544. return S.scan(val);
  545. }
  546. bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
  547. SymbolVisitor &visitor) const {
  548. ScanReachableSymbols S(this, visitor);
  549. for ( ; I != E; ++I) {
  550. if (!S.scan(*I))
  551. return false;
  552. }
  553. return true;
  554. }
  555. bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
  556. const MemRegion * const *E,
  557. SymbolVisitor &visitor) const {
  558. ScanReachableSymbols S(this, visitor);
  559. for ( ; I != E; ++I) {
  560. if (!S.scan(*I))
  561. return false;
  562. }
  563. return true;
  564. }
  565. ProgramStateRef ProgramState::addTaint(const Stmt *S,
  566. const LocationContext *LCtx,
  567. TaintTagType Kind) const {
  568. if (const Expr *E = dyn_cast_or_null<Expr>(S))
  569. S = E->IgnoreParens();
  570. SymbolRef Sym = getSVal(S, LCtx).getAsSymbol();
  571. if (Sym)
  572. return addTaint(Sym, Kind);
  573. const MemRegion *R = getSVal(S, LCtx).getAsRegion();
  574. addTaint(R, Kind);
  575. // Cannot add taint, so just return the state.
  576. return this;
  577. }
  578. ProgramStateRef ProgramState::addTaint(const MemRegion *R,
  579. TaintTagType Kind) const {
  580. if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R))
  581. return addTaint(SR->getSymbol(), Kind);
  582. return this;
  583. }
  584. ProgramStateRef ProgramState::addTaint(SymbolRef Sym,
  585. TaintTagType Kind) const {
  586. // If this is a symbol cast, remove the cast before adding the taint. Taint
  587. // is cast agnostic.
  588. while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym))
  589. Sym = SC->getOperand();
  590. ProgramStateRef NewState = set<TaintMap>(Sym, Kind);
  591. assert(NewState);
  592. return NewState;
  593. }
  594. bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx,
  595. TaintTagType Kind) const {
  596. if (const Expr *E = dyn_cast_or_null<Expr>(S))
  597. S = E->IgnoreParens();
  598. SVal val = getSVal(S, LCtx);
  599. return isTainted(val, Kind);
  600. }
  601. bool ProgramState::isTainted(SVal V, TaintTagType Kind) const {
  602. if (const SymExpr *Sym = V.getAsSymExpr())
  603. return isTainted(Sym, Kind);
  604. if (const MemRegion *Reg = V.getAsRegion())
  605. return isTainted(Reg, Kind);
  606. return false;
  607. }
  608. bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const {
  609. if (!Reg)
  610. return false;
  611. // Element region (array element) is tainted if either the base or the offset
  612. // are tainted.
  613. if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg))
  614. return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K);
  615. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg))
  616. return isTainted(SR->getSymbol(), K);
  617. if (const SubRegion *ER = dyn_cast<SubRegion>(Reg))
  618. return isTainted(ER->getSuperRegion(), K);
  619. return false;
  620. }
  621. bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const {
  622. if (!Sym)
  623. return false;
  624. // Traverse all the symbols this symbol depends on to see if any are tainted.
  625. bool Tainted = false;
  626. for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
  627. SI != SE; ++SI) {
  628. if (!isa<SymbolData>(*SI))
  629. continue;
  630. const TaintTagType *Tag = get<TaintMap>(*SI);
  631. Tainted = (Tag && *Tag == Kind);
  632. // If this is a SymbolDerived with a tainted parent, it's also tainted.
  633. if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI))
  634. Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind);
  635. // If memory region is tainted, data is also tainted.
  636. if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI))
  637. Tainted = Tainted || isTainted(SRV->getRegion(), Kind);
  638. // If If this is a SymbolCast from a tainted value, it's also tainted.
  639. if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI))
  640. Tainted = Tainted || isTainted(SC->getOperand(), Kind);
  641. if (Tainted)
  642. return true;
  643. }
  644. return Tainted;
  645. }
  646. /// The GDM component containing the dynamic type info. This is a map from a
  647. /// symbol to its most likely type.
  648. REGISTER_TRAIT_WITH_PROGRAMSTATE(DynamicTypeMap,
  649. CLANG_ENTO_PROGRAMSTATE_MAP(const MemRegion *,
  650. DynamicTypeInfo))
  651. DynamicTypeInfo ProgramState::getDynamicTypeInfo(const MemRegion *Reg) const {
  652. Reg = Reg->StripCasts();
  653. // Look up the dynamic type in the GDM.
  654. const DynamicTypeInfo *GDMType = get<DynamicTypeMap>(Reg);
  655. if (GDMType)
  656. return *GDMType;
  657. // Otherwise, fall back to what we know about the region.
  658. if (const TypedRegion *TR = dyn_cast<TypedRegion>(Reg))
  659. return DynamicTypeInfo(TR->getLocationType(), /*CanBeSubclass=*/false);
  660. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) {
  661. SymbolRef Sym = SR->getSymbol();
  662. return DynamicTypeInfo(Sym->getType());
  663. }
  664. return DynamicTypeInfo();
  665. }
  666. ProgramStateRef ProgramState::setDynamicTypeInfo(const MemRegion *Reg,
  667. DynamicTypeInfo NewTy) const {
  668. Reg = Reg->StripCasts();
  669. ProgramStateRef NewState = set<DynamicTypeMap>(Reg, NewTy);
  670. assert(NewState);
  671. return NewState;
  672. }