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