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