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