ProgramState.cpp 28 KB

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