ProgramState.cpp revision 732cdf383f9030ff2b9fb28dfbdae2285ded80c6
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/Analysis/CFG.h" 15#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 16#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" 17#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 18#include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h" 19#include "clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h" 20#include "llvm/Support/raw_ostream.h" 21 22using namespace clang; 23using namespace ento; 24 25namespace clang { namespace ento { 26/// Increments the number of times this state is referenced. 27 28void ProgramStateRetain(const ProgramState *state) { 29 ++const_cast<ProgramState*>(state)->refCount; 30} 31 32/// Decrement the number of times this state is referenced. 33void ProgramStateRelease(const ProgramState *state) { 34 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 = cast<loc::MemRegionVal>(loc).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 143ProgramStateRef 144ProgramState::invalidateRegions(ArrayRef<const MemRegion *> Regions, 145 const Expr *E, unsigned Count, 146 const LocationContext *LCtx, 147 StoreManager::InvalidatedSymbols *IS, 148 const CallEvent *Call) const { 149 if (!IS) { 150 StoreManager::InvalidatedSymbols invalidated; 151 return invalidateRegionsImpl(Regions, E, Count, LCtx, 152 invalidated, Call); 153 } 154 return invalidateRegionsImpl(Regions, E, Count, LCtx, *IS, Call); 155} 156 157ProgramStateRef 158ProgramState::invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions, 159 const Expr *E, unsigned Count, 160 const LocationContext *LCtx, 161 StoreManager::InvalidatedSymbols &IS, 162 const CallEvent *Call) const { 163 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 it's most likely type. 717namespace clang { 718namespace ento { 719typedef llvm::ImmutableMap<const MemRegion *, DynamicTypeInfo> DynamicTypeMap; 720template<> struct ProgramStateTrait<DynamicTypeMap> 721 : public ProgramStatePartialTrait<DynamicTypeMap> { 722 static void *GDMIndex() { static int index; return &index; } 723}; 724}} 725 726DynamicTypeInfo ProgramState::getDynamicTypeInfo(const MemRegion *Reg) const { 727 Reg = Reg->StripCasts(); 728 729 // Look up the dynamic type in the GDM. 730 const DynamicTypeInfo *GDMType = get<DynamicTypeMap>(Reg); 731 if (GDMType) 732 return *GDMType; 733 734 // Otherwise, fall back to what we know about the region. 735 if (const TypedRegion *TR = dyn_cast<TypedRegion>(Reg)) 736 return DynamicTypeInfo(TR->getLocationType(), /*CanBeSubclass=*/false); 737 738 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) { 739 SymbolRef Sym = SR->getSymbol(); 740 return DynamicTypeInfo(Sym->getType()); 741 } 742 743 return DynamicTypeInfo(); 744} 745 746ProgramStateRef ProgramState::setDynamicTypeInfo(const MemRegion *Reg, 747 DynamicTypeInfo NewTy) const { 748 Reg = Reg->StripCasts(); 749 ProgramStateRef NewState = set<DynamicTypeMap>(Reg, NewTy); 750 assert(NewState); 751 return NewState; 752} 753