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