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 "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h" 21 #include "llvm/Support/raw_ostream.h" 22 23 using namespace clang; 24 using namespace ento; 25 26 namespace clang { namespace ento { 27 /// Increments the number of times this state is referenced. 28 29 void ProgramStateRetain(const ProgramState *state) { 30 ++const_cast<ProgramState*>(state)->refCount; 31 } 32 33 /// Decrement the number of times this state is referenced. 34 void ProgramStateRelease(const ProgramState *state) { 35 assert(state->refCount > 0); 36 ProgramState *s = const_cast<ProgramState*>(state); 37 if (--s->refCount == 0) { 38 ProgramStateManager &Mgr = s->getStateManager(); 39 Mgr.StateSet.RemoveNode(s); 40 s->~ProgramState(); 41 Mgr.freeStates.push_back(s); 42 } 43 } 44 }} 45 46 ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env, 47 StoreRef st, GenericDataMap gdm) 48 : stateMgr(mgr), 49 Env(env), 50 store(st.getStore()), 51 GDM(gdm), 52 refCount(0) { 53 stateMgr->getStoreManager().incrementReferenceCount(store); 54 } 55 56 ProgramState::ProgramState(const ProgramState &RHS) 57 : llvm::FoldingSetNode(), 58 stateMgr(RHS.stateMgr), 59 Env(RHS.Env), 60 store(RHS.store), 61 GDM(RHS.GDM), 62 refCount(0) { 63 stateMgr->getStoreManager().incrementReferenceCount(store); 64 } 65 66 ProgramState::~ProgramState() { 67 if (store) 68 stateMgr->getStoreManager().decrementReferenceCount(store); 69 } 70 71 ProgramStateManager::ProgramStateManager(ASTContext &Ctx, 72 StoreManagerCreator CreateSMgr, 73 ConstraintManagerCreator CreateCMgr, 74 llvm::BumpPtrAllocator &alloc, 75 SubEngine *SubEng) 76 : Eng(SubEng), EnvMgr(alloc), GDMFactory(alloc), 77 svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)), 78 CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) { 79 StoreMgr = (*CreateSMgr)(*this); 80 ConstraintMgr = (*CreateCMgr)(*this, SubEng); 81 } 82 83 84 ProgramStateManager::~ProgramStateManager() { 85 for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end(); 86 I!=E; ++I) 87 I->second.second(I->second.first); 88 } 89 90 ProgramStateRef 91 ProgramStateManager::removeDeadBindings(ProgramStateRef state, 92 const StackFrameContext *LCtx, 93 SymbolReaper& SymReaper) { 94 95 // This code essentially performs a "mark-and-sweep" of the VariableBindings. 96 // The roots are any Block-level exprs and Decls that our liveness algorithm 97 // tells us are live. We then see what Decls they may reference, and keep 98 // those around. This code more than likely can be made faster, and the 99 // frequency of which this method is called should be experimented with 100 // for optimum performance. 101 ProgramState NewState = *state; 102 103 NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state); 104 105 // Clean up the store. 106 StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx, 107 SymReaper); 108 NewState.setStore(newStore); 109 SymReaper.setReapedStore(newStore); 110 111 ProgramStateRef Result = getPersistentState(NewState); 112 return ConstraintMgr->removeDeadBindings(Result, SymReaper); 113 } 114 115 ProgramStateRef ProgramState::bindLoc(Loc LV, 116 SVal V, 117 const LocationContext *LCtx, 118 bool notifyChanges) const { 119 ProgramStateManager &Mgr = getStateManager(); 120 ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(), 121 LV, V)); 122 const MemRegion *MR = LV.getAsRegion(); 123 if (MR && Mgr.getOwningEngine() && notifyChanges) 124 return Mgr.getOwningEngine()->processRegionChange(newState, MR, LCtx); 125 126 return newState; 127 } 128 129 ProgramStateRef 130 ProgramState::bindDefaultInitial(SVal loc, SVal V, 131 const LocationContext *LCtx) const { 132 ProgramStateManager &Mgr = getStateManager(); 133 const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion(); 134 const StoreRef &newStore = Mgr.StoreMgr->BindDefaultInitial(getStore(), R, V); 135 ProgramStateRef new_state = makeWithStore(newStore); 136 return Mgr.getOwningEngine() 137 ? Mgr.getOwningEngine()->processRegionChange(new_state, R, LCtx) 138 : new_state; 139 } 140 141 ProgramStateRef 142 ProgramState::bindDefaultZero(SVal loc, const LocationContext *LCtx) const { 143 ProgramStateManager &Mgr = getStateManager(); 144 const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion(); 145 const StoreRef &newStore = Mgr.StoreMgr->BindDefaultZero(getStore(), R); 146 ProgramStateRef new_state = makeWithStore(newStore); 147 return Mgr.getOwningEngine() 148 ? Mgr.getOwningEngine()->processRegionChange(new_state, R, LCtx) 149 : new_state; 150 } 151 152 typedef ArrayRef<const MemRegion *> RegionList; 153 typedef ArrayRef<SVal> ValueList; 154 155 ProgramStateRef 156 ProgramState::invalidateRegions(RegionList Regions, 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 SmallVector<SVal, 8> Values; 164 for (RegionList::const_iterator I = Regions.begin(), 165 End = Regions.end(); I != End; ++I) 166 Values.push_back(loc::MemRegionVal(*I)); 167 168 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape, 169 IS, ITraits, Call); 170 } 171 172 ProgramStateRef 173 ProgramState::invalidateRegions(ValueList Values, 174 const Expr *E, unsigned Count, 175 const LocationContext *LCtx, 176 bool CausedByPointerEscape, 177 InvalidatedSymbols *IS, 178 const CallEvent *Call, 179 RegionAndSymbolInvalidationTraits *ITraits) const { 180 181 return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape, 182 IS, ITraits, Call); 183 } 184 185 ProgramStateRef 186 ProgramState::invalidateRegionsImpl(ValueList Values, 187 const Expr *E, unsigned Count, 188 const LocationContext *LCtx, 189 bool CausedByPointerEscape, 190 InvalidatedSymbols *IS, 191 RegionAndSymbolInvalidationTraits *ITraits, 192 const CallEvent *Call) const { 193 ProgramStateManager &Mgr = getStateManager(); 194 SubEngine* Eng = Mgr.getOwningEngine(); 195 196 InvalidatedSymbols Invalidated; 197 if (!IS) 198 IS = &Invalidated; 199 200 RegionAndSymbolInvalidationTraits ITraitsLocal; 201 if (!ITraits) 202 ITraits = &ITraitsLocal; 203 204 if (Eng) { 205 StoreManager::InvalidatedRegions TopLevelInvalidated; 206 StoreManager::InvalidatedRegions Invalidated; 207 const StoreRef &newStore 208 = Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call, 209 *IS, *ITraits, &TopLevelInvalidated, 210 &Invalidated); 211 212 ProgramStateRef newState = makeWithStore(newStore); 213 214 if (CausedByPointerEscape) { 215 newState = Eng->notifyCheckersOfPointerEscape(newState, IS, 216 TopLevelInvalidated, 217 Invalidated, Call, 218 *ITraits); 219 } 220 221 return Eng->processRegionChanges(newState, IS, TopLevelInvalidated, 222 Invalidated, LCtx, Call); 223 } 224 225 const StoreRef &newStore = 226 Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call, 227 *IS, *ITraits, nullptr, nullptr); 228 return makeWithStore(newStore); 229 } 230 231 ProgramStateRef ProgramState::killBinding(Loc LV) const { 232 assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead."); 233 234 Store OldStore = getStore(); 235 const StoreRef &newStore = 236 getStateManager().StoreMgr->killBinding(OldStore, LV); 237 238 if (newStore.getStore() == OldStore) 239 return this; 240 241 return makeWithStore(newStore); 242 } 243 244 ProgramStateRef 245 ProgramState::enterStackFrame(const CallEvent &Call, 246 const StackFrameContext *CalleeCtx) const { 247 const StoreRef &NewStore = 248 getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx); 249 return makeWithStore(NewStore); 250 } 251 252 SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const { 253 // We only want to do fetches from regions that we can actually bind 254 // values. For example, SymbolicRegions of type 'id<...>' cannot 255 // have direct bindings (but their can be bindings on their subregions). 256 if (!R->isBoundable()) 257 return UnknownVal(); 258 259 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { 260 QualType T = TR->getValueType(); 261 if (Loc::isLocType(T) || T->isIntegralOrEnumerationType()) 262 return getSVal(R); 263 } 264 265 return UnknownVal(); 266 } 267 268 SVal ProgramState::getSVal(Loc location, QualType T) const { 269 SVal V = getRawSVal(location, T); 270 271 // If 'V' is a symbolic value that is *perfectly* constrained to 272 // be a constant value, use that value instead to lessen the burden 273 // on later analysis stages (so we have less symbolic values to reason 274 // about). 275 // We only go into this branch if we can convert the APSInt value we have 276 // to the type of T, which is not always the case (e.g. for void). 277 if (!T.isNull() && (T->isIntegralOrEnumerationType() || Loc::isLocType(T))) { 278 if (SymbolRef sym = V.getAsSymbol()) { 279 if (const llvm::APSInt *Int = getStateManager() 280 .getConstraintManager() 281 .getSymVal(this, sym)) { 282 // FIXME: Because we don't correctly model (yet) sign-extension 283 // and truncation of symbolic values, we need to convert 284 // the integer value to the correct signedness and bitwidth. 285 // 286 // This shows up in the following: 287 // 288 // char foo(); 289 // unsigned x = foo(); 290 // if (x == 54) 291 // ... 292 // 293 // The symbolic value stored to 'x' is actually the conjured 294 // symbol for the call to foo(); the type of that symbol is 'char', 295 // not unsigned. 296 const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int); 297 298 if (V.getAs<Loc>()) 299 return loc::ConcreteInt(NewV); 300 else 301 return nonloc::ConcreteInt(NewV); 302 } 303 } 304 } 305 306 return V; 307 } 308 309 ProgramStateRef ProgramState::BindExpr(const Stmt *S, 310 const LocationContext *LCtx, 311 SVal V, bool Invalidate) const{ 312 Environment NewEnv = 313 getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V, 314 Invalidate); 315 if (NewEnv == Env) 316 return this; 317 318 ProgramState NewSt = *this; 319 NewSt.Env = NewEnv; 320 return getStateManager().getPersistentState(NewSt); 321 } 322 323 ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx, 324 DefinedOrUnknownSVal UpperBound, 325 bool Assumption, 326 QualType indexTy) const { 327 if (Idx.isUnknown() || UpperBound.isUnknown()) 328 return this; 329 330 // Build an expression for 0 <= Idx < UpperBound. 331 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed. 332 // FIXME: This should probably be part of SValBuilder. 333 ProgramStateManager &SM = getStateManager(); 334 SValBuilder &svalBuilder = SM.getSValBuilder(); 335 ASTContext &Ctx = svalBuilder.getContext(); 336 337 // Get the offset: the minimum value of the array index type. 338 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); 339 // FIXME: This should be using ValueManager::ArrayindexTy...somehow. 340 if (indexTy.isNull()) 341 indexTy = Ctx.IntTy; 342 nonloc::ConcreteInt Min(BVF.getMinValue(indexTy)); 343 344 // Adjust the index. 345 SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add, 346 Idx.castAs<NonLoc>(), Min, indexTy); 347 if (newIdx.isUnknownOrUndef()) 348 return this; 349 350 // Adjust the upper bound. 351 SVal newBound = 352 svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(), 353 Min, indexTy); 354 355 if (newBound.isUnknownOrUndef()) 356 return this; 357 358 // Build the actual comparison. 359 SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(), 360 newBound.castAs<NonLoc>(), Ctx.IntTy); 361 if (inBound.isUnknownOrUndef()) 362 return this; 363 364 // Finally, let the constraint manager take care of it. 365 ConstraintManager &CM = SM.getConstraintManager(); 366 return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption); 367 } 368 369 ConditionTruthVal ProgramState::isNonNull(SVal V) const { 370 ConditionTruthVal IsNull = isNull(V); 371 if (IsNull.isUnderconstrained()) 372 return IsNull; 373 return ConditionTruthVal(!IsNull.getValue()); 374 } 375 376 ConditionTruthVal ProgramState::areEqual(SVal Lhs, SVal Rhs) const { 377 return stateMgr->getSValBuilder().areEqual(this, Lhs, Rhs); 378 } 379 380 ConditionTruthVal ProgramState::isNull(SVal V) const { 381 if (V.isZeroConstant()) 382 return true; 383 384 if (V.isConstant()) 385 return false; 386 387 SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true); 388 if (!Sym) 389 return ConditionTruthVal(); 390 391 return getStateManager().ConstraintMgr->isNull(this, Sym); 392 } 393 394 ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) { 395 ProgramState State(this, 396 EnvMgr.getInitialEnvironment(), 397 StoreMgr->getInitialStore(InitLoc), 398 GDMFactory.getEmptyMap()); 399 400 return getPersistentState(State); 401 } 402 403 ProgramStateRef ProgramStateManager::getPersistentStateWithGDM( 404 ProgramStateRef FromState, 405 ProgramStateRef GDMState) { 406 ProgramState NewState(*FromState); 407 NewState.GDM = GDMState->GDM; 408 return getPersistentState(NewState); 409 } 410 411 ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) { 412 413 llvm::FoldingSetNodeID ID; 414 State.Profile(ID); 415 void *InsertPos; 416 417 if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) 418 return I; 419 420 ProgramState *newState = nullptr; 421 if (!freeStates.empty()) { 422 newState = freeStates.back(); 423 freeStates.pop_back(); 424 } 425 else { 426 newState = (ProgramState*) Alloc.Allocate<ProgramState>(); 427 } 428 new (newState) ProgramState(State); 429 StateSet.InsertNode(newState, InsertPos); 430 return newState; 431 } 432 433 ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const { 434 ProgramState NewSt(*this); 435 NewSt.setStore(store); 436 return getStateManager().getPersistentState(NewSt); 437 } 438 439 void ProgramState::setStore(const StoreRef &newStore) { 440 Store newStoreStore = newStore.getStore(); 441 if (newStoreStore) 442 stateMgr->getStoreManager().incrementReferenceCount(newStoreStore); 443 if (store) 444 stateMgr->getStoreManager().decrementReferenceCount(store); 445 store = newStoreStore; 446 } 447 448 //===----------------------------------------------------------------------===// 449 // State pretty-printing. 450 //===----------------------------------------------------------------------===// 451 452 void ProgramState::print(raw_ostream &Out, const char *NL, const char *Sep, 453 const LocationContext *LC) const { 454 // Print the store. 455 ProgramStateManager &Mgr = getStateManager(); 456 Mgr.getStoreManager().print(getStore(), Out, NL, Sep); 457 458 // Print out the environment. 459 Env.print(Out, NL, Sep, LC); 460 461 // Print out the constraints. 462 Mgr.getConstraintManager().print(this, Out, NL, Sep); 463 464 // Print out the tracked dynamic types. 465 printDynamicTypeInfo(this, Out, NL, Sep); 466 467 // Print out tainted symbols. 468 printTaint(Out, NL, Sep); 469 470 // Print checker-specific data. 471 Mgr.getOwningEngine()->printState(Out, this, NL, Sep, LC); 472 } 473 474 void ProgramState::printDOT(raw_ostream &Out, const LocationContext *LC) const { 475 print(Out, "\\l", "\\|", LC); 476 } 477 478 LLVM_DUMP_METHOD void ProgramState::dump() const { 479 print(llvm::errs()); 480 } 481 482 void ProgramState::printTaint(raw_ostream &Out, 483 const char *NL, const char *Sep) const { 484 TaintMapImpl TM = get<TaintMap>(); 485 486 if (!TM.isEmpty()) 487 Out <<"Tainted symbols:" << NL; 488 489 for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) { 490 Out << I->first << " : " << I->second << NL; 491 } 492 } 493 494 void ProgramState::dumpTaint() const { 495 printTaint(llvm::errs()); 496 } 497 498 //===----------------------------------------------------------------------===// 499 // Generic Data Map. 500 //===----------------------------------------------------------------------===// 501 502 void *const* ProgramState::FindGDM(void *K) const { 503 return GDM.lookup(K); 504 } 505 506 void* 507 ProgramStateManager::FindGDMContext(void *K, 508 void *(*CreateContext)(llvm::BumpPtrAllocator&), 509 void (*DeleteContext)(void*)) { 510 511 std::pair<void*, void (*)(void*)>& p = GDMContexts[K]; 512 if (!p.first) { 513 p.first = CreateContext(Alloc); 514 p.second = DeleteContext; 515 } 516 517 return p.first; 518 } 519 520 ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){ 521 ProgramState::GenericDataMap M1 = St->getGDM(); 522 ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data); 523 524 if (M1 == M2) 525 return St; 526 527 ProgramState NewSt = *St; 528 NewSt.GDM = M2; 529 return getPersistentState(NewSt); 530 } 531 532 ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) { 533 ProgramState::GenericDataMap OldM = state->getGDM(); 534 ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key); 535 536 if (NewM == OldM) 537 return state; 538 539 ProgramState NewState = *state; 540 NewState.GDM = NewM; 541 return getPersistentState(NewState); 542 } 543 544 bool ScanReachableSymbols::scan(nonloc::LazyCompoundVal val) { 545 bool wasVisited = !visited.insert(val.getCVData()).second; 546 if (wasVisited) 547 return true; 548 549 StoreManager &StoreMgr = state->getStateManager().getStoreManager(); 550 // FIXME: We don't really want to use getBaseRegion() here because pointer 551 // arithmetic doesn't apply, but scanReachableSymbols only accepts base 552 // regions right now. 553 const MemRegion *R = val.getRegion()->getBaseRegion(); 554 return StoreMgr.scanReachableSymbols(val.getStore(), R, *this); 555 } 556 557 bool ScanReachableSymbols::scan(nonloc::CompoundVal val) { 558 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I) 559 if (!scan(*I)) 560 return false; 561 562 return true; 563 } 564 565 bool ScanReachableSymbols::scan(const SymExpr *sym) { 566 for (SymExpr::symbol_iterator SI = sym->symbol_begin(), 567 SE = sym->symbol_end(); 568 SI != SE; ++SI) { 569 bool wasVisited = !visited.insert(*SI).second; 570 if (wasVisited) 571 continue; 572 573 if (!visitor.VisitSymbol(*SI)) 574 return false; 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 return addTaint(getSVal(S, LCtx), Kind); 679 } 680 681 ProgramStateRef ProgramState::addTaint(SVal V, 682 TaintTagType Kind) const { 683 SymbolRef Sym = V.getAsSymbol(); 684 if (Sym) 685 return addTaint(Sym, Kind); 686 687 // If the SVal represents a structure, try to mass-taint all values within the 688 // structure. For now it only works efficiently on lazy compound values that 689 // were conjured during a conservative evaluation of a function - either as 690 // return values of functions that return structures or arrays by value, or as 691 // values of structures or arrays passed into the function by reference, 692 // directly or through pointer aliasing. Such lazy compound values are 693 // characterized by having exactly one binding in their captured store within 694 // their parent region, which is a conjured symbol default-bound to the base 695 // region of the parent region. 696 if (auto LCV = V.getAs<nonloc::LazyCompoundVal>()) { 697 if (Optional<SVal> binding = getStateManager().StoreMgr->getDefaultBinding(*LCV)) { 698 if (SymbolRef Sym = binding->getAsSymbol()) 699 return addPartialTaint(Sym, LCV->getRegion(), Kind); 700 } 701 } 702 703 const MemRegion *R = V.getAsRegion(); 704 return addTaint(R, Kind); 705 } 706 707 ProgramStateRef ProgramState::addTaint(const MemRegion *R, 708 TaintTagType Kind) const { 709 if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R)) 710 return addTaint(SR->getSymbol(), Kind); 711 return this; 712 } 713 714 ProgramStateRef ProgramState::addTaint(SymbolRef Sym, 715 TaintTagType Kind) const { 716 // If this is a symbol cast, remove the cast before adding the taint. Taint 717 // is cast agnostic. 718 while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym)) 719 Sym = SC->getOperand(); 720 721 ProgramStateRef NewState = set<TaintMap>(Sym, Kind); 722 assert(NewState); 723 return NewState; 724 } 725 726 ProgramStateRef ProgramState::addPartialTaint(SymbolRef ParentSym, 727 const SubRegion *SubRegion, 728 TaintTagType Kind) const { 729 // Ignore partial taint if the entire parent symbol is already tainted. 730 if (contains<TaintMap>(ParentSym) && *get<TaintMap>(ParentSym) == Kind) 731 return this; 732 733 // Partial taint applies if only a portion of the symbol is tainted. 734 if (SubRegion == SubRegion->getBaseRegion()) 735 return addTaint(ParentSym, Kind); 736 737 const TaintedSubRegions *SavedRegs = get<DerivedSymTaint>(ParentSym); 738 TaintedSubRegions Regs = 739 SavedRegs ? *SavedRegs : stateMgr->TSRFactory.getEmptyMap(); 740 741 Regs = stateMgr->TSRFactory.add(Regs, SubRegion, Kind); 742 ProgramStateRef NewState = set<DerivedSymTaint>(ParentSym, Regs); 743 assert(NewState); 744 return NewState; 745 } 746 747 bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx, 748 TaintTagType Kind) const { 749 if (const Expr *E = dyn_cast_or_null<Expr>(S)) 750 S = E->IgnoreParens(); 751 752 SVal val = getSVal(S, LCtx); 753 return isTainted(val, Kind); 754 } 755 756 bool ProgramState::isTainted(SVal V, TaintTagType Kind) const { 757 if (const SymExpr *Sym = V.getAsSymExpr()) 758 return isTainted(Sym, Kind); 759 if (const MemRegion *Reg = V.getAsRegion()) 760 return isTainted(Reg, Kind); 761 return false; 762 } 763 764 bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const { 765 if (!Reg) 766 return false; 767 768 // Element region (array element) is tainted if either the base or the offset 769 // are tainted. 770 if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg)) 771 return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K); 772 773 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) 774 return isTainted(SR->getSymbol(), K); 775 776 if (const SubRegion *ER = dyn_cast<SubRegion>(Reg)) 777 return isTainted(ER->getSuperRegion(), K); 778 779 return false; 780 } 781 782 bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const { 783 if (!Sym) 784 return false; 785 786 // Traverse all the symbols this symbol depends on to see if any are tainted. 787 for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end(); 788 SI != SE; ++SI) { 789 if (!isa<SymbolData>(*SI)) 790 continue; 791 792 if (const TaintTagType *Tag = get<TaintMap>(*SI)) { 793 if (*Tag == Kind) 794 return true; 795 } 796 797 if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI)) { 798 // If this is a SymbolDerived with a tainted parent, it's also tainted. 799 if (isTainted(SD->getParentSymbol(), Kind)) 800 return true; 801 802 // If this is a SymbolDerived with the same parent symbol as another 803 // tainted SymbolDerived and a region that's a sub-region of that tainted 804 // symbol, it's also tainted. 805 if (const TaintedSubRegions *Regs = 806 get<DerivedSymTaint>(SD->getParentSymbol())) { 807 const TypedValueRegion *R = SD->getRegion(); 808 for (auto I : *Regs) { 809 // FIXME: The logic to identify tainted regions could be more 810 // complete. For example, this would not currently identify 811 // overlapping fields in a union as tainted. To identify this we can 812 // check for overlapping/nested byte offsets. 813 if (Kind == I.second && R->isSubRegionOf(I.first)) 814 return true; 815 } 816 } 817 } 818 819 // If memory region is tainted, data is also tainted. 820 if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI)) { 821 if (isTainted(SRV->getRegion(), Kind)) 822 return true; 823 } 824 825 // If this is a SymbolCast from a tainted value, it's also tainted. 826 if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI)) { 827 if (isTainted(SC->getOperand(), Kind)) 828 return true; 829 } 830 } 831 832 return false; 833 } 834 835