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 28 void ProgramStateRetain(const ProgramState *state) { 29 ++const_cast<ProgramState*>(state)->refCount; 30 } 31 32 /// Decrement the number of times this state is referenced. 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 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 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 65 ProgramState::~ProgramState() { 66 if (store) 67 stateMgr->getStoreManager().decrementReferenceCount(store); 68 } 69 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 = (*CreateSMgr)(*this); 79 ConstraintMgr = (*CreateCMgr)(*this, SubEng); 80 } 81 82 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 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 114 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 125 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 139 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 156 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 169 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 179 InvalidatedSymbols Invalidated; 180 if (!IS) 181 IS = &Invalidated; 182 183 RegionAndSymbolInvalidationTraits ITraitsLocal; 184 if (!ITraits) 185 ITraits = &ITraitsLocal; 186 187 if (Eng) { 188 StoreManager::InvalidatedRegions TopLevelInvalidated; 189 StoreManager::InvalidatedRegions Invalidated; 190 const StoreRef &newStore 191 = Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call, 192 *IS, *ITraits, &TopLevelInvalidated, 193 &Invalidated); 194 195 ProgramStateRef newState = makeWithStore(newStore); 196 197 if (CausedByPointerEscape) { 198 newState = Eng->notifyCheckersOfPointerEscape(newState, IS, 199 TopLevelInvalidated, 200 Invalidated, Call, 201 *ITraits); 202 } 203 204 return Eng->processRegionChanges(newState, IS, TopLevelInvalidated, 205 Invalidated, Call); 206 } 207 208 const StoreRef &newStore = 209 Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call, 210 *IS, *ITraits, nullptr, nullptr); 211 return makeWithStore(newStore); 212 } 213 214 ProgramStateRef ProgramState::killBinding(Loc LV) const { 215 assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead."); 216 217 Store OldStore = getStore(); 218 const StoreRef &newStore = 219 getStateManager().StoreMgr->killBinding(OldStore, LV); 220 221 if (newStore.getStore() == OldStore) 222 return this; 223 224 return makeWithStore(newStore); 225 } 226 227 ProgramStateRef 228 ProgramState::enterStackFrame(const CallEvent &Call, 229 const StackFrameContext *CalleeCtx) const { 230 const StoreRef &NewStore = 231 getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx); 232 return makeWithStore(NewStore); 233 } 234 235 SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const { 236 // We only want to do fetches from regions that we can actually bind 237 // values. For example, SymbolicRegions of type 'id<...>' cannot 238 // have direct bindings (but their can be bindings on their subregions). 239 if (!R->isBoundable()) 240 return UnknownVal(); 241 242 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { 243 QualType T = TR->getValueType(); 244 if (Loc::isLocType(T) || T->isIntegralOrEnumerationType()) 245 return getSVal(R); 246 } 247 248 return UnknownVal(); 249 } 250 251 SVal ProgramState::getSVal(Loc location, QualType T) const { 252 SVal V = getRawSVal(cast<Loc>(location), T); 253 254 // If 'V' is a symbolic value that is *perfectly* constrained to 255 // be a constant value, use that value instead to lessen the burden 256 // on later analysis stages (so we have less symbolic values to reason 257 // about). 258 if (!T.isNull()) { 259 if (SymbolRef sym = V.getAsSymbol()) { 260 if (const llvm::APSInt *Int = getStateManager() 261 .getConstraintManager() 262 .getSymVal(this, sym)) { 263 // FIXME: Because we don't correctly model (yet) sign-extension 264 // and truncation of symbolic values, we need to convert 265 // the integer value to the correct signedness and bitwidth. 266 // 267 // This shows up in the following: 268 // 269 // char foo(); 270 // unsigned x = foo(); 271 // if (x == 54) 272 // ... 273 // 274 // The symbolic value stored to 'x' is actually the conjured 275 // symbol for the call to foo(); the type of that symbol is 'char', 276 // not unsigned. 277 const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int); 278 279 if (V.getAs<Loc>()) 280 return loc::ConcreteInt(NewV); 281 else 282 return nonloc::ConcreteInt(NewV); 283 } 284 } 285 } 286 287 return V; 288 } 289 290 ProgramStateRef ProgramState::BindExpr(const Stmt *S, 291 const LocationContext *LCtx, 292 SVal V, bool Invalidate) const{ 293 Environment NewEnv = 294 getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V, 295 Invalidate); 296 if (NewEnv == Env) 297 return this; 298 299 ProgramState NewSt = *this; 300 NewSt.Env = NewEnv; 301 return getStateManager().getPersistentState(NewSt); 302 } 303 304 ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx, 305 DefinedOrUnknownSVal UpperBound, 306 bool Assumption, 307 QualType indexTy) const { 308 if (Idx.isUnknown() || UpperBound.isUnknown()) 309 return this; 310 311 // Build an expression for 0 <= Idx < UpperBound. 312 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed. 313 // FIXME: This should probably be part of SValBuilder. 314 ProgramStateManager &SM = getStateManager(); 315 SValBuilder &svalBuilder = SM.getSValBuilder(); 316 ASTContext &Ctx = svalBuilder.getContext(); 317 318 // Get the offset: the minimum value of the array index type. 319 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); 320 // FIXME: This should be using ValueManager::ArrayindexTy...somehow. 321 if (indexTy.isNull()) 322 indexTy = Ctx.IntTy; 323 nonloc::ConcreteInt Min(BVF.getMinValue(indexTy)); 324 325 // Adjust the index. 326 SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add, 327 Idx.castAs<NonLoc>(), Min, indexTy); 328 if (newIdx.isUnknownOrUndef()) 329 return this; 330 331 // Adjust the upper bound. 332 SVal newBound = 333 svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(), 334 Min, indexTy); 335 336 if (newBound.isUnknownOrUndef()) 337 return this; 338 339 // Build the actual comparison. 340 SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(), 341 newBound.castAs<NonLoc>(), Ctx.IntTy); 342 if (inBound.isUnknownOrUndef()) 343 return this; 344 345 // Finally, let the constraint manager take care of it. 346 ConstraintManager &CM = SM.getConstraintManager(); 347 return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption); 348 } 349 350 ConditionTruthVal ProgramState::isNull(SVal V) const { 351 if (V.isZeroConstant()) 352 return true; 353 354 if (V.isConstant()) 355 return false; 356 357 SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true); 358 if (!Sym) 359 return ConditionTruthVal(); 360 361 return getStateManager().ConstraintMgr->isNull(this, Sym); 362 } 363 364 ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) { 365 ProgramState State(this, 366 EnvMgr.getInitialEnvironment(), 367 StoreMgr->getInitialStore(InitLoc), 368 GDMFactory.getEmptyMap()); 369 370 return getPersistentState(State); 371 } 372 373 ProgramStateRef ProgramStateManager::getPersistentStateWithGDM( 374 ProgramStateRef FromState, 375 ProgramStateRef GDMState) { 376 ProgramState NewState(*FromState); 377 NewState.GDM = GDMState->GDM; 378 return getPersistentState(NewState); 379 } 380 381 ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) { 382 383 llvm::FoldingSetNodeID ID; 384 State.Profile(ID); 385 void *InsertPos; 386 387 if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) 388 return I; 389 390 ProgramState *newState = nullptr; 391 if (!freeStates.empty()) { 392 newState = freeStates.back(); 393 freeStates.pop_back(); 394 } 395 else { 396 newState = (ProgramState*) Alloc.Allocate<ProgramState>(); 397 } 398 new (newState) ProgramState(State); 399 StateSet.InsertNode(newState, InsertPos); 400 return newState; 401 } 402 403 ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const { 404 ProgramState NewSt(*this); 405 NewSt.setStore(store); 406 return getStateManager().getPersistentState(NewSt); 407 } 408 409 void ProgramState::setStore(const StoreRef &newStore) { 410 Store newStoreStore = newStore.getStore(); 411 if (newStoreStore) 412 stateMgr->getStoreManager().incrementReferenceCount(newStoreStore); 413 if (store) 414 stateMgr->getStoreManager().decrementReferenceCount(store); 415 store = newStoreStore; 416 } 417 418 //===----------------------------------------------------------------------===// 419 // State pretty-printing. 420 //===----------------------------------------------------------------------===// 421 422 void ProgramState::print(raw_ostream &Out, 423 const char *NL, const char *Sep) const { 424 // Print the store. 425 ProgramStateManager &Mgr = getStateManager(); 426 Mgr.getStoreManager().print(getStore(), Out, NL, Sep); 427 428 // Print out the environment. 429 Env.print(Out, NL, Sep); 430 431 // Print out the constraints. 432 Mgr.getConstraintManager().print(this, Out, NL, Sep); 433 434 // Print checker-specific data. 435 Mgr.getOwningEngine()->printState(Out, this, NL, Sep); 436 } 437 438 void ProgramState::printDOT(raw_ostream &Out) const { 439 print(Out, "\\l", "\\|"); 440 } 441 442 LLVM_DUMP_METHOD void ProgramState::dump() const { 443 print(llvm::errs()); 444 } 445 446 void ProgramState::printTaint(raw_ostream &Out, 447 const char *NL, const char *Sep) const { 448 TaintMapImpl TM = get<TaintMap>(); 449 450 if (!TM.isEmpty()) 451 Out <<"Tainted Symbols:" << NL; 452 453 for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) { 454 Out << I->first << " : " << I->second << NL; 455 } 456 } 457 458 void ProgramState::dumpTaint() const { 459 printTaint(llvm::errs()); 460 } 461 462 //===----------------------------------------------------------------------===// 463 // Generic Data Map. 464 //===----------------------------------------------------------------------===// 465 466 void *const* ProgramState::FindGDM(void *K) const { 467 return GDM.lookup(K); 468 } 469 470 void* 471 ProgramStateManager::FindGDMContext(void *K, 472 void *(*CreateContext)(llvm::BumpPtrAllocator&), 473 void (*DeleteContext)(void*)) { 474 475 std::pair<void*, void (*)(void*)>& p = GDMContexts[K]; 476 if (!p.first) { 477 p.first = CreateContext(Alloc); 478 p.second = DeleteContext; 479 } 480 481 return p.first; 482 } 483 484 ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){ 485 ProgramState::GenericDataMap M1 = St->getGDM(); 486 ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data); 487 488 if (M1 == M2) 489 return St; 490 491 ProgramState NewSt = *St; 492 NewSt.GDM = M2; 493 return getPersistentState(NewSt); 494 } 495 496 ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) { 497 ProgramState::GenericDataMap OldM = state->getGDM(); 498 ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key); 499 500 if (NewM == OldM) 501 return state; 502 503 ProgramState NewState = *state; 504 NewState.GDM = NewM; 505 return getPersistentState(NewState); 506 } 507 508 bool ScanReachableSymbols::scan(nonloc::LazyCompoundVal val) { 509 bool wasVisited = !visited.insert(val.getCVData()).second; 510 if (wasVisited) 511 return true; 512 513 StoreManager &StoreMgr = state->getStateManager().getStoreManager(); 514 // FIXME: We don't really want to use getBaseRegion() here because pointer 515 // arithmetic doesn't apply, but scanReachableSymbols only accepts base 516 // regions right now. 517 const MemRegion *R = val.getRegion()->getBaseRegion(); 518 return StoreMgr.scanReachableSymbols(val.getStore(), R, *this); 519 } 520 521 bool ScanReachableSymbols::scan(nonloc::CompoundVal val) { 522 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I) 523 if (!scan(*I)) 524 return false; 525 526 return true; 527 } 528 529 bool ScanReachableSymbols::scan(const SymExpr *sym) { 530 for (SymExpr::symbol_iterator SI = sym->symbol_begin(), 531 SE = sym->symbol_end(); 532 SI != SE; ++SI) { 533 bool wasVisited = !visited.insert(*SI).second; 534 if (wasVisited) 535 continue; 536 537 if (!visitor.VisitSymbol(*SI)) 538 return false; 539 } 540 541 return true; 542 } 543 544 bool ScanReachableSymbols::scan(SVal val) { 545 if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>()) 546 return scan(X->getRegion()); 547 548 if (Optional<nonloc::LazyCompoundVal> X = 549 val.getAs<nonloc::LazyCompoundVal>()) 550 return scan(*X); 551 552 if (Optional<nonloc::LocAsInteger> X = val.getAs<nonloc::LocAsInteger>()) 553 return scan(X->getLoc()); 554 555 if (SymbolRef Sym = val.getAsSymbol()) 556 return scan(Sym); 557 558 if (const SymExpr *Sym = val.getAsSymbolicExpression()) 559 return scan(Sym); 560 561 if (Optional<nonloc::CompoundVal> X = val.getAs<nonloc::CompoundVal>()) 562 return scan(*X); 563 564 return true; 565 } 566 567 bool ScanReachableSymbols::scan(const MemRegion *R) { 568 if (isa<MemSpaceRegion>(R)) 569 return true; 570 571 bool wasVisited = !visited.insert(R).second; 572 if (wasVisited) 573 return true; 574 575 if (!visitor.VisitMemRegion(R)) 576 return false; 577 578 // If this is a symbolic region, visit the symbol for the region. 579 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) 580 if (!visitor.VisitSymbol(SR->getSymbol())) 581 return false; 582 583 // If this is a subregion, also visit the parent regions. 584 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) { 585 const MemRegion *Super = SR->getSuperRegion(); 586 if (!scan(Super)) 587 return false; 588 589 // When we reach the topmost region, scan all symbols in it. 590 if (isa<MemSpaceRegion>(Super)) { 591 StoreManager &StoreMgr = state->getStateManager().getStoreManager(); 592 if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this)) 593 return false; 594 } 595 } 596 597 // Regions captured by a block are also implicitly reachable. 598 if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) { 599 BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(), 600 E = BDR->referenced_vars_end(); 601 for ( ; I != E; ++I) { 602 if (!scan(I.getCapturedRegion())) 603 return false; 604 } 605 } 606 607 return true; 608 } 609 610 bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const { 611 ScanReachableSymbols S(this, visitor); 612 return S.scan(val); 613 } 614 615 bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E, 616 SymbolVisitor &visitor) const { 617 ScanReachableSymbols S(this, visitor); 618 for ( ; I != E; ++I) { 619 if (!S.scan(*I)) 620 return false; 621 } 622 return true; 623 } 624 625 bool ProgramState::scanReachableSymbols(const MemRegion * const *I, 626 const MemRegion * const *E, 627 SymbolVisitor &visitor) const { 628 ScanReachableSymbols S(this, visitor); 629 for ( ; I != E; ++I) { 630 if (!S.scan(*I)) 631 return false; 632 } 633 return true; 634 } 635 636 ProgramStateRef ProgramState::addTaint(const Stmt *S, 637 const LocationContext *LCtx, 638 TaintTagType Kind) const { 639 if (const Expr *E = dyn_cast_or_null<Expr>(S)) 640 S = E->IgnoreParens(); 641 642 SymbolRef Sym = getSVal(S, LCtx).getAsSymbol(); 643 if (Sym) 644 return addTaint(Sym, Kind); 645 646 const MemRegion *R = getSVal(S, LCtx).getAsRegion(); 647 addTaint(R, Kind); 648 649 // Cannot add taint, so just return the state. 650 return this; 651 } 652 653 ProgramStateRef ProgramState::addTaint(const MemRegion *R, 654 TaintTagType Kind) const { 655 if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R)) 656 return addTaint(SR->getSymbol(), Kind); 657 return this; 658 } 659 660 ProgramStateRef ProgramState::addTaint(SymbolRef Sym, 661 TaintTagType Kind) const { 662 // If this is a symbol cast, remove the cast before adding the taint. Taint 663 // is cast agnostic. 664 while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym)) 665 Sym = SC->getOperand(); 666 667 ProgramStateRef NewState = set<TaintMap>(Sym, Kind); 668 assert(NewState); 669 return NewState; 670 } 671 672 bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx, 673 TaintTagType Kind) const { 674 if (const Expr *E = dyn_cast_or_null<Expr>(S)) 675 S = E->IgnoreParens(); 676 677 SVal val = getSVal(S, LCtx); 678 return isTainted(val, Kind); 679 } 680 681 bool ProgramState::isTainted(SVal V, TaintTagType Kind) const { 682 if (const SymExpr *Sym = V.getAsSymExpr()) 683 return isTainted(Sym, Kind); 684 if (const MemRegion *Reg = V.getAsRegion()) 685 return isTainted(Reg, Kind); 686 return false; 687 } 688 689 bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const { 690 if (!Reg) 691 return false; 692 693 // Element region (array element) is tainted if either the base or the offset 694 // are tainted. 695 if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg)) 696 return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K); 697 698 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) 699 return isTainted(SR->getSymbol(), K); 700 701 if (const SubRegion *ER = dyn_cast<SubRegion>(Reg)) 702 return isTainted(ER->getSuperRegion(), K); 703 704 return false; 705 } 706 707 bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const { 708 if (!Sym) 709 return false; 710 711 // Traverse all the symbols this symbol depends on to see if any are tainted. 712 bool Tainted = false; 713 for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end(); 714 SI != SE; ++SI) { 715 if (!isa<SymbolData>(*SI)) 716 continue; 717 718 const TaintTagType *Tag = get<TaintMap>(*SI); 719 Tainted = (Tag && *Tag == Kind); 720 721 // If this is a SymbolDerived with a tainted parent, it's also tainted. 722 if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI)) 723 Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind); 724 725 // If memory region is tainted, data is also tainted. 726 if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI)) 727 Tainted = Tainted || isTainted(SRV->getRegion(), Kind); 728 729 // If If this is a SymbolCast from a tainted value, it's also tainted. 730 if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI)) 731 Tainted = Tainted || isTainted(SC->getOperand(), Kind); 732 733 if (Tainted) 734 return true; 735 } 736 737 return Tainted; 738 } 739 740