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
bindLoc(Loc LV,SVal V,bool notifyChanges) const114 ProgramStateRef ProgramState::bindLoc(Loc LV, SVal V, bool notifyChanges) const {
115 ProgramStateManager &Mgr = getStateManager();
116 ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
117 LV, V));
118 const MemRegion *MR = LV.getAsRegion();
119 if (MR && Mgr.getOwningEngine() && notifyChanges)
120 return Mgr.getOwningEngine()->processRegionChange(newState, MR);
121
122 return newState;
123 }
124
bindDefault(SVal loc,SVal V) const125 ProgramStateRef ProgramState::bindDefault(SVal loc, SVal V) const {
126 ProgramStateManager &Mgr = getStateManager();
127 const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
128 const StoreRef &newStore = Mgr.StoreMgr->BindDefault(getStore(), R, V);
129 ProgramStateRef new_state = makeWithStore(newStore);
130 return Mgr.getOwningEngine() ?
131 Mgr.getOwningEngine()->processRegionChange(new_state, R) :
132 new_state;
133 }
134
135 typedef ArrayRef<const MemRegion *> RegionList;
136 typedef ArrayRef<SVal> ValueList;
137
138 ProgramStateRef
invalidateRegions(RegionList Regions,const Expr * E,unsigned Count,const LocationContext * LCtx,bool CausedByPointerEscape,InvalidatedSymbols * IS,const CallEvent * Call,RegionAndSymbolInvalidationTraits * ITraits) const139 ProgramState::invalidateRegions(RegionList Regions,
140 const Expr *E, unsigned Count,
141 const LocationContext *LCtx,
142 bool CausedByPointerEscape,
143 InvalidatedSymbols *IS,
144 const CallEvent *Call,
145 RegionAndSymbolInvalidationTraits *ITraits) const {
146 SmallVector<SVal, 8> Values;
147 for (RegionList::const_iterator I = Regions.begin(),
148 End = Regions.end(); I != End; ++I)
149 Values.push_back(loc::MemRegionVal(*I));
150
151 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
152 IS, ITraits, Call);
153 }
154
155 ProgramStateRef
invalidateRegions(ValueList Values,const Expr * E,unsigned Count,const LocationContext * LCtx,bool CausedByPointerEscape,InvalidatedSymbols * IS,const CallEvent * Call,RegionAndSymbolInvalidationTraits * ITraits) const156 ProgramState::invalidateRegions(ValueList Values,
157 const Expr *E, unsigned Count,
158 const LocationContext *LCtx,
159 bool CausedByPointerEscape,
160 InvalidatedSymbols *IS,
161 const CallEvent *Call,
162 RegionAndSymbolInvalidationTraits *ITraits) const {
163
164 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
165 IS, ITraits, Call);
166 }
167
168 ProgramStateRef
invalidateRegionsImpl(ValueList Values,const Expr * E,unsigned Count,const LocationContext * LCtx,bool CausedByPointerEscape,InvalidatedSymbols * IS,RegionAndSymbolInvalidationTraits * ITraits,const CallEvent * Call) const169 ProgramState::invalidateRegionsImpl(ValueList Values,
170 const Expr *E, unsigned Count,
171 const LocationContext *LCtx,
172 bool CausedByPointerEscape,
173 InvalidatedSymbols *IS,
174 RegionAndSymbolInvalidationTraits *ITraits,
175 const CallEvent *Call) const {
176 ProgramStateManager &Mgr = getStateManager();
177 SubEngine* Eng = Mgr.getOwningEngine();
178 InvalidatedSymbols ConstIS;
179
180 InvalidatedSymbols Invalidated;
181 if (!IS)
182 IS = &Invalidated;
183
184 RegionAndSymbolInvalidationTraits ITraitsLocal;
185 if (!ITraits)
186 ITraits = &ITraitsLocal;
187
188 if (Eng) {
189 StoreManager::InvalidatedRegions TopLevelInvalidated;
190 StoreManager::InvalidatedRegions Invalidated;
191 const StoreRef &newStore
192 = Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
193 *IS, *ITraits, &TopLevelInvalidated,
194 &Invalidated);
195
196 ProgramStateRef newState = makeWithStore(newStore);
197
198 if (CausedByPointerEscape) {
199 newState = Eng->notifyCheckersOfPointerEscape(newState, IS,
200 TopLevelInvalidated,
201 Invalidated, Call,
202 *ITraits);
203 }
204
205 return Eng->processRegionChanges(newState, IS, TopLevelInvalidated,
206 Invalidated, Call);
207 }
208
209 const StoreRef &newStore =
210 Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
211 *IS, *ITraits, NULL, NULL);
212 return makeWithStore(newStore);
213 }
214
killBinding(Loc LV) const215 ProgramStateRef ProgramState::killBinding(Loc LV) const {
216 assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead.");
217
218 Store OldStore = getStore();
219 const StoreRef &newStore =
220 getStateManager().StoreMgr->killBinding(OldStore, LV);
221
222 if (newStore.getStore() == OldStore)
223 return this;
224
225 return makeWithStore(newStore);
226 }
227
228 ProgramStateRef
enterStackFrame(const CallEvent & Call,const StackFrameContext * CalleeCtx) const229 ProgramState::enterStackFrame(const CallEvent &Call,
230 const StackFrameContext *CalleeCtx) const {
231 const StoreRef &NewStore =
232 getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
233 return makeWithStore(NewStore);
234 }
235
getSValAsScalarOrLoc(const MemRegion * R) const236 SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
237 // We only want to do fetches from regions that we can actually bind
238 // values. For example, SymbolicRegions of type 'id<...>' cannot
239 // have direct bindings (but their can be bindings on their subregions).
240 if (!R->isBoundable())
241 return UnknownVal();
242
243 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
244 QualType T = TR->getValueType();
245 if (Loc::isLocType(T) || T->isIntegralOrEnumerationType())
246 return getSVal(R);
247 }
248
249 return UnknownVal();
250 }
251
getSVal(Loc location,QualType T) const252 SVal ProgramState::getSVal(Loc location, QualType T) const {
253 SVal V = getRawSVal(cast<Loc>(location), T);
254
255 // If 'V' is a symbolic value that is *perfectly* constrained to
256 // be a constant value, use that value instead to lessen the burden
257 // on later analysis stages (so we have less symbolic values to reason
258 // about).
259 if (!T.isNull()) {
260 if (SymbolRef sym = V.getAsSymbol()) {
261 if (const llvm::APSInt *Int = getStateManager()
262 .getConstraintManager()
263 .getSymVal(this, sym)) {
264 // FIXME: Because we don't correctly model (yet) sign-extension
265 // and truncation of symbolic values, we need to convert
266 // the integer value to the correct signedness and bitwidth.
267 //
268 // This shows up in the following:
269 //
270 // char foo();
271 // unsigned x = foo();
272 // if (x == 54)
273 // ...
274 //
275 // The symbolic value stored to 'x' is actually the conjured
276 // symbol for the call to foo(); the type of that symbol is 'char',
277 // not unsigned.
278 const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
279
280 if (V.getAs<Loc>())
281 return loc::ConcreteInt(NewV);
282 else
283 return nonloc::ConcreteInt(NewV);
284 }
285 }
286 }
287
288 return V;
289 }
290
BindExpr(const Stmt * S,const LocationContext * LCtx,SVal V,bool Invalidate) const291 ProgramStateRef ProgramState::BindExpr(const Stmt *S,
292 const LocationContext *LCtx,
293 SVal V, bool Invalidate) const{
294 Environment NewEnv =
295 getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
296 Invalidate);
297 if (NewEnv == Env)
298 return this;
299
300 ProgramState NewSt = *this;
301 NewSt.Env = NewEnv;
302 return getStateManager().getPersistentState(NewSt);
303 }
304
assumeInBound(DefinedOrUnknownSVal Idx,DefinedOrUnknownSVal UpperBound,bool Assumption,QualType indexTy) const305 ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
306 DefinedOrUnknownSVal UpperBound,
307 bool Assumption,
308 QualType indexTy) const {
309 if (Idx.isUnknown() || UpperBound.isUnknown())
310 return this;
311
312 // Build an expression for 0 <= Idx < UpperBound.
313 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
314 // FIXME: This should probably be part of SValBuilder.
315 ProgramStateManager &SM = getStateManager();
316 SValBuilder &svalBuilder = SM.getSValBuilder();
317 ASTContext &Ctx = svalBuilder.getContext();
318
319 // Get the offset: the minimum value of the array index type.
320 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
321 // FIXME: This should be using ValueManager::ArrayindexTy...somehow.
322 if (indexTy.isNull())
323 indexTy = Ctx.IntTy;
324 nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
325
326 // Adjust the index.
327 SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
328 Idx.castAs<NonLoc>(), Min, indexTy);
329 if (newIdx.isUnknownOrUndef())
330 return this;
331
332 // Adjust the upper bound.
333 SVal newBound =
334 svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
335 Min, indexTy);
336
337 if (newBound.isUnknownOrUndef())
338 return this;
339
340 // Build the actual comparison.
341 SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(),
342 newBound.castAs<NonLoc>(), Ctx.IntTy);
343 if (inBound.isUnknownOrUndef())
344 return this;
345
346 // Finally, let the constraint manager take care of it.
347 ConstraintManager &CM = SM.getConstraintManager();
348 return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption);
349 }
350
isNull(SVal V) const351 ConditionTruthVal ProgramState::isNull(SVal V) const {
352 if (V.isZeroConstant())
353 return true;
354
355 if (V.isConstant())
356 return false;
357
358 SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
359 if (!Sym)
360 return ConditionTruthVal();
361
362 return getStateManager().ConstraintMgr->isNull(this, Sym);
363 }
364
getInitialState(const LocationContext * InitLoc)365 ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
366 ProgramState State(this,
367 EnvMgr.getInitialEnvironment(),
368 StoreMgr->getInitialStore(InitLoc),
369 GDMFactory.getEmptyMap());
370
371 return getPersistentState(State);
372 }
373
getPersistentStateWithGDM(ProgramStateRef FromState,ProgramStateRef GDMState)374 ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
375 ProgramStateRef FromState,
376 ProgramStateRef GDMState) {
377 ProgramState NewState(*FromState);
378 NewState.GDM = GDMState->GDM;
379 return getPersistentState(NewState);
380 }
381
getPersistentState(ProgramState & State)382 ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
383
384 llvm::FoldingSetNodeID ID;
385 State.Profile(ID);
386 void *InsertPos;
387
388 if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
389 return I;
390
391 ProgramState *newState = 0;
392 if (!freeStates.empty()) {
393 newState = freeStates.back();
394 freeStates.pop_back();
395 }
396 else {
397 newState = (ProgramState*) Alloc.Allocate<ProgramState>();
398 }
399 new (newState) ProgramState(State);
400 StateSet.InsertNode(newState, InsertPos);
401 return newState;
402 }
403
makeWithStore(const StoreRef & store) const404 ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
405 ProgramState NewSt(*this);
406 NewSt.setStore(store);
407 return getStateManager().getPersistentState(NewSt);
408 }
409
setStore(const StoreRef & newStore)410 void ProgramState::setStore(const StoreRef &newStore) {
411 Store newStoreStore = newStore.getStore();
412 if (newStoreStore)
413 stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
414 if (store)
415 stateMgr->getStoreManager().decrementReferenceCount(store);
416 store = newStoreStore;
417 }
418
419 //===----------------------------------------------------------------------===//
420 // State pretty-printing.
421 //===----------------------------------------------------------------------===//
422
print(raw_ostream & Out,const char * NL,const char * Sep) const423 void ProgramState::print(raw_ostream &Out,
424 const char *NL, const char *Sep) const {
425 // Print the store.
426 ProgramStateManager &Mgr = getStateManager();
427 Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
428
429 // Print out the environment.
430 Env.print(Out, NL, Sep);
431
432 // Print out the constraints.
433 Mgr.getConstraintManager().print(this, Out, NL, Sep);
434
435 // Print checker-specific data.
436 Mgr.getOwningEngine()->printState(Out, this, NL, Sep);
437 }
438
printDOT(raw_ostream & Out) const439 void ProgramState::printDOT(raw_ostream &Out) const {
440 print(Out, "\\l", "\\|");
441 }
442
dump() const443 void ProgramState::dump() const {
444 print(llvm::errs());
445 }
446
printTaint(raw_ostream & Out,const char * NL,const char * Sep) const447 void ProgramState::printTaint(raw_ostream &Out,
448 const char *NL, const char *Sep) const {
449 TaintMapImpl TM = get<TaintMap>();
450
451 if (!TM.isEmpty())
452 Out <<"Tainted Symbols:" << NL;
453
454 for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) {
455 Out << I->first << " : " << I->second << NL;
456 }
457 }
458
dumpTaint() const459 void ProgramState::dumpTaint() const {
460 printTaint(llvm::errs());
461 }
462
463 //===----------------------------------------------------------------------===//
464 // Generic Data Map.
465 //===----------------------------------------------------------------------===//
466
FindGDM(void * K) const467 void *const* ProgramState::FindGDM(void *K) const {
468 return GDM.lookup(K);
469 }
470
471 void*
FindGDMContext(void * K,void * (* CreateContext)(llvm::BumpPtrAllocator &),void (* DeleteContext)(void *))472 ProgramStateManager::FindGDMContext(void *K,
473 void *(*CreateContext)(llvm::BumpPtrAllocator&),
474 void (*DeleteContext)(void*)) {
475
476 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
477 if (!p.first) {
478 p.first = CreateContext(Alloc);
479 p.second = DeleteContext;
480 }
481
482 return p.first;
483 }
484
addGDM(ProgramStateRef St,void * Key,void * Data)485 ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){
486 ProgramState::GenericDataMap M1 = St->getGDM();
487 ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
488
489 if (M1 == M2)
490 return St;
491
492 ProgramState NewSt = *St;
493 NewSt.GDM = M2;
494 return getPersistentState(NewSt);
495 }
496
removeGDM(ProgramStateRef state,void * Key)497 ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) {
498 ProgramState::GenericDataMap OldM = state->getGDM();
499 ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
500
501 if (NewM == OldM)
502 return state;
503
504 ProgramState NewState = *state;
505 NewState.GDM = NewM;
506 return getPersistentState(NewState);
507 }
508
scan(nonloc::LazyCompoundVal val)509 bool ScanReachableSymbols::scan(nonloc::LazyCompoundVal val) {
510 bool wasVisited = !visited.insert(val.getCVData()).second;
511 if (wasVisited)
512 return true;
513
514 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
515 // FIXME: We don't really want to use getBaseRegion() here because pointer
516 // arithmetic doesn't apply, but scanReachableSymbols only accepts base
517 // regions right now.
518 const MemRegion *R = val.getRegion()->getBaseRegion();
519 return StoreMgr.scanReachableSymbols(val.getStore(), R, *this);
520 }
521
scan(nonloc::CompoundVal val)522 bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
523 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
524 if (!scan(*I))
525 return false;
526
527 return true;
528 }
529
scan(const SymExpr * sym)530 bool ScanReachableSymbols::scan(const SymExpr *sym) {
531 bool wasVisited = !visited.insert(sym).second;
532 if (wasVisited)
533 return true;
534
535 if (!visitor.VisitSymbol(sym))
536 return false;
537
538 // TODO: should be rewritten using SymExpr::symbol_iterator.
539 switch (sym->getKind()) {
540 case SymExpr::RegionValueKind:
541 case SymExpr::ConjuredKind:
542 case SymExpr::DerivedKind:
543 case SymExpr::ExtentKind:
544 case SymExpr::MetadataKind:
545 break;
546 case SymExpr::CastSymbolKind:
547 return scan(cast<SymbolCast>(sym)->getOperand());
548 case SymExpr::SymIntKind:
549 return scan(cast<SymIntExpr>(sym)->getLHS());
550 case SymExpr::IntSymKind:
551 return scan(cast<IntSymExpr>(sym)->getRHS());
552 case SymExpr::SymSymKind: {
553 const SymSymExpr *x = cast<SymSymExpr>(sym);
554 return scan(x->getLHS()) && scan(x->getRHS());
555 }
556 }
557 return true;
558 }
559
scan(SVal val)560 bool ScanReachableSymbols::scan(SVal val) {
561 if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>())
562 return scan(X->getRegion());
563
564 if (Optional<nonloc::LazyCompoundVal> X =
565 val.getAs<nonloc::LazyCompoundVal>())
566 return scan(*X);
567
568 if (Optional<nonloc::LocAsInteger> X = val.getAs<nonloc::LocAsInteger>())
569 return scan(X->getLoc());
570
571 if (SymbolRef Sym = val.getAsSymbol())
572 return scan(Sym);
573
574 if (const SymExpr *Sym = val.getAsSymbolicExpression())
575 return scan(Sym);
576
577 if (Optional<nonloc::CompoundVal> X = val.getAs<nonloc::CompoundVal>())
578 return scan(*X);
579
580 return true;
581 }
582
scan(const MemRegion * R)583 bool ScanReachableSymbols::scan(const MemRegion *R) {
584 if (isa<MemSpaceRegion>(R))
585 return true;
586
587 bool wasVisited = !visited.insert(R).second;
588 if (wasVisited)
589 return true;
590
591 if (!visitor.VisitMemRegion(R))
592 return false;
593
594 // If this is a symbolic region, visit the symbol for the region.
595 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
596 if (!visitor.VisitSymbol(SR->getSymbol()))
597 return false;
598
599 // If this is a subregion, also visit the parent regions.
600 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
601 const MemRegion *Super = SR->getSuperRegion();
602 if (!scan(Super))
603 return false;
604
605 // When we reach the topmost region, scan all symbols in it.
606 if (isa<MemSpaceRegion>(Super)) {
607 StoreManager &StoreMgr = state->getStateManager().getStoreManager();
608 if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
609 return false;
610 }
611 }
612
613 // Regions captured by a block are also implicitly reachable.
614 if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
615 BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
616 E = BDR->referenced_vars_end();
617 for ( ; I != E; ++I) {
618 if (!scan(I.getCapturedRegion()))
619 return false;
620 }
621 }
622
623 return true;
624 }
625
scanReachableSymbols(SVal val,SymbolVisitor & visitor) const626 bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
627 ScanReachableSymbols S(this, visitor);
628 return S.scan(val);
629 }
630
scanReachableSymbols(const SVal * I,const SVal * E,SymbolVisitor & visitor) const631 bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
632 SymbolVisitor &visitor) const {
633 ScanReachableSymbols S(this, visitor);
634 for ( ; I != E; ++I) {
635 if (!S.scan(*I))
636 return false;
637 }
638 return true;
639 }
640
scanReachableSymbols(const MemRegion * const * I,const MemRegion * const * E,SymbolVisitor & visitor) const641 bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
642 const MemRegion * const *E,
643 SymbolVisitor &visitor) const {
644 ScanReachableSymbols S(this, visitor);
645 for ( ; I != E; ++I) {
646 if (!S.scan(*I))
647 return false;
648 }
649 return true;
650 }
651
addTaint(const Stmt * S,const LocationContext * LCtx,TaintTagType Kind) const652 ProgramStateRef ProgramState::addTaint(const Stmt *S,
653 const LocationContext *LCtx,
654 TaintTagType Kind) const {
655 if (const Expr *E = dyn_cast_or_null<Expr>(S))
656 S = E->IgnoreParens();
657
658 SymbolRef Sym = getSVal(S, LCtx).getAsSymbol();
659 if (Sym)
660 return addTaint(Sym, Kind);
661
662 const MemRegion *R = getSVal(S, LCtx).getAsRegion();
663 addTaint(R, Kind);
664
665 // Cannot add taint, so just return the state.
666 return this;
667 }
668
addTaint(const MemRegion * R,TaintTagType Kind) const669 ProgramStateRef ProgramState::addTaint(const MemRegion *R,
670 TaintTagType Kind) const {
671 if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R))
672 return addTaint(SR->getSymbol(), Kind);
673 return this;
674 }
675
addTaint(SymbolRef Sym,TaintTagType Kind) const676 ProgramStateRef ProgramState::addTaint(SymbolRef Sym,
677 TaintTagType Kind) const {
678 // If this is a symbol cast, remove the cast before adding the taint. Taint
679 // is cast agnostic.
680 while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym))
681 Sym = SC->getOperand();
682
683 ProgramStateRef NewState = set<TaintMap>(Sym, Kind);
684 assert(NewState);
685 return NewState;
686 }
687
isTainted(const Stmt * S,const LocationContext * LCtx,TaintTagType Kind) const688 bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx,
689 TaintTagType Kind) const {
690 if (const Expr *E = dyn_cast_or_null<Expr>(S))
691 S = E->IgnoreParens();
692
693 SVal val = getSVal(S, LCtx);
694 return isTainted(val, Kind);
695 }
696
isTainted(SVal V,TaintTagType Kind) const697 bool ProgramState::isTainted(SVal V, TaintTagType Kind) const {
698 if (const SymExpr *Sym = V.getAsSymExpr())
699 return isTainted(Sym, Kind);
700 if (const MemRegion *Reg = V.getAsRegion())
701 return isTainted(Reg, Kind);
702 return false;
703 }
704
isTainted(const MemRegion * Reg,TaintTagType K) const705 bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const {
706 if (!Reg)
707 return false;
708
709 // Element region (array element) is tainted if either the base or the offset
710 // are tainted.
711 if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg))
712 return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K);
713
714 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg))
715 return isTainted(SR->getSymbol(), K);
716
717 if (const SubRegion *ER = dyn_cast<SubRegion>(Reg))
718 return isTainted(ER->getSuperRegion(), K);
719
720 return false;
721 }
722
isTainted(SymbolRef Sym,TaintTagType Kind) const723 bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const {
724 if (!Sym)
725 return false;
726
727 // Traverse all the symbols this symbol depends on to see if any are tainted.
728 bool Tainted = false;
729 for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
730 SI != SE; ++SI) {
731 if (!isa<SymbolData>(*SI))
732 continue;
733
734 const TaintTagType *Tag = get<TaintMap>(*SI);
735 Tainted = (Tag && *Tag == Kind);
736
737 // If this is a SymbolDerived with a tainted parent, it's also tainted.
738 if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI))
739 Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind);
740
741 // If memory region is tainted, data is also tainted.
742 if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI))
743 Tainted = Tainted || isTainted(SRV->getRegion(), Kind);
744
745 // If If this is a SymbolCast from a tainted value, it's also tainted.
746 if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI))
747 Tainted = Tainted || isTainted(SC->getOperand(), Kind);
748
749 if (Tainted)
750 return true;
751 }
752
753 return Tainted;
754 }
755
756 /// The GDM component containing the dynamic type info. This is a map from a
757 /// symbol to its most likely type.
REGISTER_TRAIT_WITH_PROGRAMSTATE(DynamicTypeMap,CLANG_ENTO_PROGRAMSTATE_MAP (const MemRegion *,DynamicTypeInfo))758 REGISTER_TRAIT_WITH_PROGRAMSTATE(DynamicTypeMap,
759 CLANG_ENTO_PROGRAMSTATE_MAP(const MemRegion *,
760 DynamicTypeInfo))
761
762 DynamicTypeInfo ProgramState::getDynamicTypeInfo(const MemRegion *Reg) const {
763 Reg = Reg->StripCasts();
764
765 // Look up the dynamic type in the GDM.
766 const DynamicTypeInfo *GDMType = get<DynamicTypeMap>(Reg);
767 if (GDMType)
768 return *GDMType;
769
770 // Otherwise, fall back to what we know about the region.
771 if (const TypedRegion *TR = dyn_cast<TypedRegion>(Reg))
772 return DynamicTypeInfo(TR->getLocationType(), /*CanBeSubclass=*/false);
773
774 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) {
775 SymbolRef Sym = SR->getSymbol();
776 return DynamicTypeInfo(Sym->getType());
777 }
778
779 return DynamicTypeInfo();
780 }
781
setDynamicTypeInfo(const MemRegion * Reg,DynamicTypeInfo NewTy) const782 ProgramStateRef ProgramState::setDynamicTypeInfo(const MemRegion *Reg,
783 DynamicTypeInfo NewTy) const {
784 Reg = Reg->StripCasts();
785 ProgramStateRef NewState = set<DynamicTypeMap>(Reg, NewTy);
786 assert(NewState);
787 return NewState;
788 }
789