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