ProgramState.cpp revision 243830
174462Salfred//= ProgramState.cpp - Path-Sensitive "State" for tracking values --*- C++ -*--= 274462Salfred// 3272850Shrs// The LLVM Compiler Infrastructure 4272850Shrs// 58870Srgrimes// This file is distributed under the University of Illinois Open Source 6272850Shrs// License. See LICENSE.TXT for details. 7272850Shrs// 8272850Shrs//===----------------------------------------------------------------------===// 98870Srgrimes// 10272850Shrs// This file implements ProgramState and ProgramStateManager. 11272850Shrs// 12272850Shrs//===----------------------------------------------------------------------===// 13272850Shrs 14272850Shrs#include "clang/Analysis/CFG.h" 15272850Shrs#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 16272850Shrs#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" 17272850Shrs#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 18272850Shrs#include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h" 198870Srgrimes#include "clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h" 20272850Shrs#include "llvm/Support/raw_ostream.h" 21272850Shrs 22272850Shrsusing namespace clang; 23272850Shrsusing namespace ento; 24272850Shrs 25272850Shrsnamespace clang { namespace ento { 26272850Shrs/// Increments the number of times this state is referenced. 27272850Shrs 28272850Shrsvoid ProgramStateRetain(const ProgramState *state) { 29272850Shrs ++const_cast<ProgramState*>(state)->refCount; 30272850Shrs} 31272850Shrs 321902Swollman/// Decrement the number of times this state is referenced. 331902Swollmanvoid ProgramStateRelease(const ProgramState *state) { 341902Swollman assert(state->refCount > 0); 35136582Sobrien ProgramState *s = const_cast<ProgramState*>(state); 3692986Sobrien if (--s->refCount == 0) { 371902Swollman ProgramStateManager &Mgr = s->getStateManager(); 3892986Sobrien Mgr.StateSet.RemoveNode(s); 3992986Sobrien s->~ProgramState(); 401902Swollman Mgr.freeStates.push_back(s); 411902Swollman } 421902Swollman} 431902Swollman}} 441902Swollman 451902SwollmanProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env, 461902Swollman StoreRef st, GenericDataMap gdm) 471902Swollman : stateMgr(mgr), 4874462Salfred Env(env), 4974462Salfred store(st.getStore()), 50101067Snectar GDM(gdm), 511902Swollman refCount(0) { 521902Swollman stateMgr->getStoreManager().incrementReferenceCount(store); 5311669Sphk} 5474462Salfred 551902SwollmanProgramState::ProgramState(const ProgramState &RHS) 561902Swollman : llvm::FoldingSetNode(), 5774462Salfred stateMgr(RHS.stateMgr), 581902Swollman Env(RHS.Env), 591902Swollman store(RHS.store), 601902Swollman GDM(RHS.GDM), 611902Swollman refCount(0) { 621902Swollman stateMgr->getStoreManager().incrementReferenceCount(store); 631902Swollman} 641902Swollman 651902SwollmanProgramState::~ProgramState() { 661902Swollman if (store) 671902Swollman stateMgr->getStoreManager().decrementReferenceCount(store); 6874462Salfred} 691902Swollman 701902SwollmanProgramStateManager::ProgramStateManager(ASTContext &Ctx, 711902Swollman StoreManagerCreator CreateSMgr, 721902Swollman ConstraintManagerCreator CreateCMgr, 731902Swollman llvm::BumpPtrAllocator &alloc, 741902Swollman SubEngine *SubEng) 7574462Salfred : Eng(SubEng), EnvMgr(alloc), GDMFactory(alloc), 7674462Salfred svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)), 7774462Salfred CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) { 7874462Salfred StoreMgr.reset((*CreateSMgr)(*this)); 7974462Salfred ConstraintMgr.reset((*CreateCMgr)(*this, SubEng)); 801902Swollman} 811902Swollman 82101045Sdarrenr 831902SwollmanProgramStateManager::~ProgramStateManager() { 841902Swollman for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end(); 851902Swollman I!=E; ++I) 86101147Snectar I->second.second(I->second.first); 87101045Sdarrenr} 881902Swollman 891902SwollmanProgramStateRef 901902SwollmanProgramStateManager::removeDeadBindings(ProgramStateRef state, 911902Swollman const StackFrameContext *LCtx, 921902Swollman SymbolReaper& SymReaper) { 931902Swollman 941902Swollman // This code essentially performs a "mark-and-sweep" of the VariableBindings. 951902Swollman // The roots are any Block-level exprs and Decls that our liveness algorithm 961902Swollman // tells us are live. We then see what Decls they may reference, and keep 971902Swollman // those around. This code more than likely can be made faster, and the 981902Swollman // frequency of which this method is called should be experimented with 991902Swollman // for optimum performance. 1001902Swollman ProgramState NewState = *state; 1011902Swollman 1021902Swollman NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state); 10374462Salfred 1041902Swollman // Clean up the store. 1051902Swollman StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx, 10621062Speter SymReaper); 1071902Swollman NewState.setStore(newStore); 1081902Swollman SymReaper.setReapedStore(newStore); 1091902Swollman 1101902Swollman ProgramStateRef Result = getPersistentState(NewState); 11174462Salfred return ConstraintMgr->removeDeadBindings(Result, SymReaper); 11274462Salfred} 11374462Salfred 1141902SwollmanProgramStateRef ProgramState::bindCompoundLiteral(const CompoundLiteralExpr *CL, 11574462Salfred const LocationContext *LC, 1161902Swollman SVal V) const { 1171902Swollman const StoreRef &newStore = 1181902Swollman getStateManager().StoreMgr->bindCompoundLiteral(getStore(), CL, LC, V); 1191902Swollman return makeWithStore(newStore); 12074462Salfred} 1211902Swollman 1221902SwollmanProgramStateRef ProgramState::bindLoc(Loc LV, SVal V, bool notifyChanges) const { 1231902Swollman ProgramStateManager &Mgr = getStateManager(); 1241902Swollman ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(), 1251902Swollman LV, V)); 1261902Swollman const MemRegion *MR = LV.getAsRegion(); 1271902Swollman if (MR && Mgr.getOwningEngine() && notifyChanges) 1281902Swollman return Mgr.getOwningEngine()->processRegionChange(newState, MR); 1291902Swollman 1301902Swollman return newState; 1311902Swollman} 1321902Swollman 1331902SwollmanProgramStateRef ProgramState::bindDefault(SVal loc, SVal V) const { 1341902Swollman ProgramStateManager &Mgr = getStateManager(); 1351902Swollman const MemRegion *R = cast<loc::MemRegionVal>(loc).getRegion(); 1361902Swollman const StoreRef &newStore = Mgr.StoreMgr->BindDefault(getStore(), R, V); 1371902Swollman ProgramStateRef new_state = makeWithStore(newStore); 1381902Swollman return Mgr.getOwningEngine() ? 1391902Swollman Mgr.getOwningEngine()->processRegionChange(new_state, R) : 1401902Swollman new_state; 1411902Swollman} 1421902Swollman 1431902SwollmanProgramStateRef 1441902SwollmanProgramState::invalidateRegions(ArrayRef<const MemRegion *> Regions, 1451902Swollman const Expr *E, unsigned Count, 14674462Salfred const LocationContext *LCtx, 14774462Salfred StoreManager::InvalidatedSymbols *IS, 14874462Salfred const CallEvent *Call) const { 14974462Salfred if (!IS) { 15074462Salfred StoreManager::InvalidatedSymbols invalidated; 1511902Swollman return invalidateRegionsImpl(Regions, E, Count, LCtx, 15274462Salfred invalidated, Call); 15374462Salfred } 1541902Swollman return invalidateRegionsImpl(Regions, E, Count, LCtx, *IS, Call); 1551902Swollman} 1561902Swollman 157101045SdarrenrProgramStateRef 1581902SwollmanProgramState::invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions, 1591902Swollman const Expr *E, unsigned Count, 1601902Swollman const LocationContext *LCtx, 1611902Swollman StoreManager::InvalidatedSymbols &IS, 16274462Salfred const CallEvent *Call) const { 1631902Swollman ProgramStateManager &Mgr = getStateManager(); 164 SubEngine* Eng = Mgr.getOwningEngine(); 165 166 if (Eng && Eng->wantsRegionChangeUpdate(this)) { 167 StoreManager::InvalidatedRegions Invalidated; 168 const StoreRef &newStore 169 = Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, LCtx, IS, 170 Call, &Invalidated); 171 ProgramStateRef newState = makeWithStore(newStore); 172 return Eng->processRegionChanges(newState, &IS, Regions, Invalidated, Call); 173 } 174 175 const StoreRef &newStore = 176 Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, LCtx, IS, 177 Call, NULL); 178 return makeWithStore(newStore); 179} 180 181ProgramStateRef ProgramState::killBinding(Loc LV) const { 182 assert(!isa<loc::MemRegionVal>(LV) && "Use invalidateRegion instead."); 183 184 Store OldStore = getStore(); 185 const StoreRef &newStore = 186 getStateManager().StoreMgr->killBinding(OldStore, LV); 187 188 if (newStore.getStore() == OldStore) 189 return this; 190 191 return makeWithStore(newStore); 192} 193 194ProgramStateRef 195ProgramState::enterStackFrame(const CallEvent &Call, 196 const StackFrameContext *CalleeCtx) const { 197 const StoreRef &NewStore = 198 getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx); 199 return makeWithStore(NewStore); 200} 201 202SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const { 203 // We only want to do fetches from regions that we can actually bind 204 // values. For example, SymbolicRegions of type 'id<...>' cannot 205 // have direct bindings (but their can be bindings on their subregions). 206 if (!R->isBoundable()) 207 return UnknownVal(); 208 209 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { 210 QualType T = TR->getValueType(); 211 if (Loc::isLocType(T) || T->isIntegerType()) 212 return getSVal(R); 213 } 214 215 return UnknownVal(); 216} 217 218SVal ProgramState::getSVal(Loc location, QualType T) const { 219 SVal V = getRawSVal(cast<Loc>(location), T); 220 221 // If 'V' is a symbolic value that is *perfectly* constrained to 222 // be a constant value, use that value instead to lessen the burden 223 // on later analysis stages (so we have less symbolic values to reason 224 // about). 225 if (!T.isNull()) { 226 if (SymbolRef sym = V.getAsSymbol()) { 227 if (const llvm::APSInt *Int = getStateManager() 228 .getConstraintManager() 229 .getSymVal(this, sym)) { 230 // FIXME: Because we don't correctly model (yet) sign-extension 231 // and truncation of symbolic values, we need to convert 232 // the integer value to the correct signedness and bitwidth. 233 // 234 // This shows up in the following: 235 // 236 // char foo(); 237 // unsigned x = foo(); 238 // if (x == 54) 239 // ... 240 // 241 // The symbolic value stored to 'x' is actually the conjured 242 // symbol for the call to foo(); the type of that symbol is 'char', 243 // not unsigned. 244 const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int); 245 246 if (isa<Loc>(V)) 247 return loc::ConcreteInt(NewV); 248 else 249 return nonloc::ConcreteInt(NewV); 250 } 251 } 252 } 253 254 return V; 255} 256 257ProgramStateRef ProgramState::BindExpr(const Stmt *S, 258 const LocationContext *LCtx, 259 SVal V, bool Invalidate) const{ 260 Environment NewEnv = 261 getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V, 262 Invalidate); 263 if (NewEnv == Env) 264 return this; 265 266 ProgramState NewSt = *this; 267 NewSt.Env = NewEnv; 268 return getStateManager().getPersistentState(NewSt); 269} 270 271ProgramStateRef 272ProgramState::bindExprAndLocation(const Stmt *S, const LocationContext *LCtx, 273 SVal location, 274 SVal V) const { 275 Environment NewEnv = 276 getStateManager().EnvMgr.bindExprAndLocation(Env, 277 EnvironmentEntry(S, LCtx), 278 location, V); 279 280 if (NewEnv == Env) 281 return this; 282 283 ProgramState NewSt = *this; 284 NewSt.Env = NewEnv; 285 return getStateManager().getPersistentState(NewSt); 286} 287 288ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx, 289 DefinedOrUnknownSVal UpperBound, 290 bool Assumption, 291 QualType indexTy) const { 292 if (Idx.isUnknown() || UpperBound.isUnknown()) 293 return this; 294 295 // Build an expression for 0 <= Idx < UpperBound. 296 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed. 297 // FIXME: This should probably be part of SValBuilder. 298 ProgramStateManager &SM = getStateManager(); 299 SValBuilder &svalBuilder = SM.getSValBuilder(); 300 ASTContext &Ctx = svalBuilder.getContext(); 301 302 // Get the offset: the minimum value of the array index type. 303 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); 304 // FIXME: This should be using ValueManager::ArrayindexTy...somehow. 305 if (indexTy.isNull()) 306 indexTy = Ctx.IntTy; 307 nonloc::ConcreteInt Min(BVF.getMinValue(indexTy)); 308 309 // Adjust the index. 310 SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add, 311 cast<NonLoc>(Idx), Min, indexTy); 312 if (newIdx.isUnknownOrUndef()) 313 return this; 314 315 // Adjust the upper bound. 316 SVal newBound = 317 svalBuilder.evalBinOpNN(this, BO_Add, cast<NonLoc>(UpperBound), 318 Min, indexTy); 319 320 if (newBound.isUnknownOrUndef()) 321 return this; 322 323 // Build the actual comparison. 324 SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, 325 cast<NonLoc>(newIdx), cast<NonLoc>(newBound), 326 Ctx.IntTy); 327 if (inBound.isUnknownOrUndef()) 328 return this; 329 330 // Finally, let the constraint manager take care of it. 331 ConstraintManager &CM = SM.getConstraintManager(); 332 return CM.assume(this, cast<DefinedSVal>(inBound), Assumption); 333} 334 335ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) { 336 ProgramState State(this, 337 EnvMgr.getInitialEnvironment(), 338 StoreMgr->getInitialStore(InitLoc), 339 GDMFactory.getEmptyMap()); 340 341 return getPersistentState(State); 342} 343 344ProgramStateRef ProgramStateManager::getPersistentStateWithGDM( 345 ProgramStateRef FromState, 346 ProgramStateRef GDMState) { 347 ProgramState NewState(*FromState); 348 NewState.GDM = GDMState->GDM; 349 return getPersistentState(NewState); 350} 351 352ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) { 353 354 llvm::FoldingSetNodeID ID; 355 State.Profile(ID); 356 void *InsertPos; 357 358 if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) 359 return I; 360 361 ProgramState *newState = 0; 362 if (!freeStates.empty()) { 363 newState = freeStates.back(); 364 freeStates.pop_back(); 365 } 366 else { 367 newState = (ProgramState*) Alloc.Allocate<ProgramState>(); 368 } 369 new (newState) ProgramState(State); 370 StateSet.InsertNode(newState, InsertPos); 371 return newState; 372} 373 374ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const { 375 ProgramState NewSt(*this); 376 NewSt.setStore(store); 377 return getStateManager().getPersistentState(NewSt); 378} 379 380void ProgramState::setStore(const StoreRef &newStore) { 381 Store newStoreStore = newStore.getStore(); 382 if (newStoreStore) 383 stateMgr->getStoreManager().incrementReferenceCount(newStoreStore); 384 if (store) 385 stateMgr->getStoreManager().decrementReferenceCount(store); 386 store = newStoreStore; 387} 388 389//===----------------------------------------------------------------------===// 390// State pretty-printing. 391//===----------------------------------------------------------------------===// 392 393void ProgramState::print(raw_ostream &Out, 394 const char *NL, const char *Sep) const { 395 // Print the store. 396 ProgramStateManager &Mgr = getStateManager(); 397 Mgr.getStoreManager().print(getStore(), Out, NL, Sep); 398 399 // Print out the environment. 400 Env.print(Out, NL, Sep); 401 402 // Print out the constraints. 403 Mgr.getConstraintManager().print(this, Out, NL, Sep); 404 405 // Print checker-specific data. 406 Mgr.getOwningEngine()->printState(Out, this, NL, Sep); 407} 408 409void ProgramState::printDOT(raw_ostream &Out) const { 410 print(Out, "\\l", "\\|"); 411} 412 413void ProgramState::dump() const { 414 print(llvm::errs()); 415} 416 417void ProgramState::printTaint(raw_ostream &Out, 418 const char *NL, const char *Sep) const { 419 TaintMapImpl TM = get<TaintMap>(); 420 421 if (!TM.isEmpty()) 422 Out <<"Tainted Symbols:" << NL; 423 424 for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) { 425 Out << I->first << " : " << I->second << NL; 426 } 427} 428 429void ProgramState::dumpTaint() const { 430 printTaint(llvm::errs()); 431} 432 433//===----------------------------------------------------------------------===// 434// Generic Data Map. 435//===----------------------------------------------------------------------===// 436 437void *const* ProgramState::FindGDM(void *K) const { 438 return GDM.lookup(K); 439} 440 441void* 442ProgramStateManager::FindGDMContext(void *K, 443 void *(*CreateContext)(llvm::BumpPtrAllocator&), 444 void (*DeleteContext)(void*)) { 445 446 std::pair<void*, void (*)(void*)>& p = GDMContexts[K]; 447 if (!p.first) { 448 p.first = CreateContext(Alloc); 449 p.second = DeleteContext; 450 } 451 452 return p.first; 453} 454 455ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){ 456 ProgramState::GenericDataMap M1 = St->getGDM(); 457 ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data); 458 459 if (M1 == M2) 460 return St; 461 462 ProgramState NewSt = *St; 463 NewSt.GDM = M2; 464 return getPersistentState(NewSt); 465} 466 467ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) { 468 ProgramState::GenericDataMap OldM = state->getGDM(); 469 ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key); 470 471 if (NewM == OldM) 472 return state; 473 474 ProgramState NewState = *state; 475 NewState.GDM = NewM; 476 return getPersistentState(NewState); 477} 478 479bool ScanReachableSymbols::scan(nonloc::CompoundVal val) { 480 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I) 481 if (!scan(*I)) 482 return false; 483 484 return true; 485} 486 487bool ScanReachableSymbols::scan(const SymExpr *sym) { 488 unsigned &isVisited = visited[sym]; 489 if (isVisited) 490 return true; 491 isVisited = 1; 492 493 if (!visitor.VisitSymbol(sym)) 494 return false; 495 496 // TODO: should be rewritten using SymExpr::symbol_iterator. 497 switch (sym->getKind()) { 498 case SymExpr::RegionValueKind: 499 case SymExpr::ConjuredKind: 500 case SymExpr::DerivedKind: 501 case SymExpr::ExtentKind: 502 case SymExpr::MetadataKind: 503 break; 504 case SymExpr::CastSymbolKind: 505 return scan(cast<SymbolCast>(sym)->getOperand()); 506 case SymExpr::SymIntKind: 507 return scan(cast<SymIntExpr>(sym)->getLHS()); 508 case SymExpr::IntSymKind: 509 return scan(cast<IntSymExpr>(sym)->getRHS()); 510 case SymExpr::SymSymKind: { 511 const SymSymExpr *x = cast<SymSymExpr>(sym); 512 return scan(x->getLHS()) && scan(x->getRHS()); 513 } 514 } 515 return true; 516} 517 518bool ScanReachableSymbols::scan(SVal val) { 519 if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val)) 520 return scan(X->getRegion()); 521 522 if (nonloc::LazyCompoundVal *X = dyn_cast<nonloc::LazyCompoundVal>(&val)) 523 return scan(X->getRegion()); 524 525 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val)) 526 return scan(X->getLoc()); 527 528 if (SymbolRef Sym = val.getAsSymbol()) 529 return scan(Sym); 530 531 if (const SymExpr *Sym = val.getAsSymbolicExpression()) 532 return scan(Sym); 533 534 if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val)) 535 return scan(*X); 536 537 return true; 538} 539 540bool ScanReachableSymbols::scan(const MemRegion *R) { 541 if (isa<MemSpaceRegion>(R)) 542 return true; 543 544 unsigned &isVisited = visited[R]; 545 if (isVisited) 546 return true; 547 isVisited = 1; 548 549 550 if (!visitor.VisitMemRegion(R)) 551 return false; 552 553 // If this is a symbolic region, visit the symbol for the region. 554 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) 555 if (!visitor.VisitSymbol(SR->getSymbol())) 556 return false; 557 558 // If this is a subregion, also visit the parent regions. 559 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) { 560 const MemRegion *Super = SR->getSuperRegion(); 561 if (!scan(Super)) 562 return false; 563 564 // When we reach the topmost region, scan all symbols in it. 565 if (isa<MemSpaceRegion>(Super)) { 566 StoreManager &StoreMgr = state->getStateManager().getStoreManager(); 567 if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this)) 568 return false; 569 } 570 } 571 572 // Regions captured by a block are also implicitly reachable. 573 if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) { 574 BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(), 575 E = BDR->referenced_vars_end(); 576 for ( ; I != E; ++I) { 577 if (!scan(I.getCapturedRegion())) 578 return false; 579 } 580 } 581 582 return true; 583} 584 585bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const { 586 ScanReachableSymbols S(this, visitor); 587 return S.scan(val); 588} 589 590bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E, 591 SymbolVisitor &visitor) const { 592 ScanReachableSymbols S(this, visitor); 593 for ( ; I != E; ++I) { 594 if (!S.scan(*I)) 595 return false; 596 } 597 return true; 598} 599 600bool ProgramState::scanReachableSymbols(const MemRegion * const *I, 601 const MemRegion * const *E, 602 SymbolVisitor &visitor) const { 603 ScanReachableSymbols S(this, visitor); 604 for ( ; I != E; ++I) { 605 if (!S.scan(*I)) 606 return false; 607 } 608 return true; 609} 610 611ProgramStateRef ProgramState::addTaint(const Stmt *S, 612 const LocationContext *LCtx, 613 TaintTagType Kind) const { 614 if (const Expr *E = dyn_cast_or_null<Expr>(S)) 615 S = E->IgnoreParens(); 616 617 SymbolRef Sym = getSVal(S, LCtx).getAsSymbol(); 618 if (Sym) 619 return addTaint(Sym, Kind); 620 621 const MemRegion *R = getSVal(S, LCtx).getAsRegion(); 622 addTaint(R, Kind); 623 624 // Cannot add taint, so just return the state. 625 return this; 626} 627 628ProgramStateRef ProgramState::addTaint(const MemRegion *R, 629 TaintTagType Kind) const { 630 if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R)) 631 return addTaint(SR->getSymbol(), Kind); 632 return this; 633} 634 635ProgramStateRef ProgramState::addTaint(SymbolRef Sym, 636 TaintTagType Kind) const { 637 // If this is a symbol cast, remove the cast before adding the taint. Taint 638 // is cast agnostic. 639 while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym)) 640 Sym = SC->getOperand(); 641 642 ProgramStateRef NewState = set<TaintMap>(Sym, Kind); 643 assert(NewState); 644 return NewState; 645} 646 647bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx, 648 TaintTagType Kind) const { 649 if (const Expr *E = dyn_cast_or_null<Expr>(S)) 650 S = E->IgnoreParens(); 651 652 SVal val = getSVal(S, LCtx); 653 return isTainted(val, Kind); 654} 655 656bool ProgramState::isTainted(SVal V, TaintTagType Kind) const { 657 if (const SymExpr *Sym = V.getAsSymExpr()) 658 return isTainted(Sym, Kind); 659 if (const MemRegion *Reg = V.getAsRegion()) 660 return isTainted(Reg, Kind); 661 return false; 662} 663 664bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const { 665 if (!Reg) 666 return false; 667 668 // Element region (array element) is tainted if either the base or the offset 669 // are tainted. 670 if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg)) 671 return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K); 672 673 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) 674 return isTainted(SR->getSymbol(), K); 675 676 if (const SubRegion *ER = dyn_cast<SubRegion>(Reg)) 677 return isTainted(ER->getSuperRegion(), K); 678 679 return false; 680} 681 682bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const { 683 if (!Sym) 684 return false; 685 686 // Traverse all the symbols this symbol depends on to see if any are tainted. 687 bool Tainted = false; 688 for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end(); 689 SI != SE; ++SI) { 690 if (!isa<SymbolData>(*SI)) 691 continue; 692 693 const TaintTagType *Tag = get<TaintMap>(*SI); 694 Tainted = (Tag && *Tag == Kind); 695 696 // If this is a SymbolDerived with a tainted parent, it's also tainted. 697 if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI)) 698 Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind); 699 700 // If memory region is tainted, data is also tainted. 701 if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI)) 702 Tainted = Tainted || isTainted(SRV->getRegion(), Kind); 703 704 // If If this is a SymbolCast from a tainted value, it's also tainted. 705 if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI)) 706 Tainted = Tainted || isTainted(SC->getOperand(), Kind); 707 708 if (Tainted) 709 return true; 710 } 711 712 return Tainted; 713} 714 715/// The GDM component containing the dynamic type info. This is a map from a 716/// symbol to its most likely type. 717REGISTER_TRAIT_WITH_PROGRAMSTATE(DynamicTypeMap, 718 CLANG_ENTO_PROGRAMSTATE_MAP(const MemRegion *, 719 DynamicTypeInfo)) 720 721DynamicTypeInfo ProgramState::getDynamicTypeInfo(const MemRegion *Reg) const { 722 Reg = Reg->StripCasts(); 723 724 // Look up the dynamic type in the GDM. 725 const DynamicTypeInfo *GDMType = get<DynamicTypeMap>(Reg); 726 if (GDMType) 727 return *GDMType; 728 729 // Otherwise, fall back to what we know about the region. 730 if (const TypedRegion *TR = dyn_cast<TypedRegion>(Reg)) 731 return DynamicTypeInfo(TR->getLocationType(), /*CanBeSubclass=*/false); 732 733 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) { 734 SymbolRef Sym = SR->getSymbol(); 735 return DynamicTypeInfo(Sym->getType()); 736 } 737 738 return DynamicTypeInfo(); 739} 740 741ProgramStateRef ProgramState::setDynamicTypeInfo(const MemRegion *Reg, 742 DynamicTypeInfo NewTy) const { 743 Reg = Reg->StripCasts(); 744 ProgramStateRef NewState = set<DynamicTypeMap>(Reg, NewTy); 745 assert(NewState); 746 return NewState; 747} 748