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