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