ProgramState.cpp revision bf53dfac8195835028bd6347433f7dbebcc29fc1
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 "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 bool ResultsInPointerEscape, 148 InvalidatedSymbols *IS, 149 const CallEvent *Call) const { 150 if (!IS) { 151 InvalidatedSymbols invalidated; 152 return invalidateRegionsImpl(Regions, E, Count, LCtx, 153 ResultsInPointerEscape, 154 invalidated, Call); 155 } 156 return invalidateRegionsImpl(Regions, E, Count, LCtx, ResultsInPointerEscape, 157 *IS, Call); 158} 159 160ProgramStateRef 161ProgramState::invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions, 162 const Expr *E, unsigned Count, 163 const LocationContext *LCtx, 164 bool ResultsInPointerEscape, 165 InvalidatedSymbols &IS, 166 const CallEvent *Call) const { 167 ProgramStateManager &Mgr = getStateManager(); 168 SubEngine* Eng = Mgr.getOwningEngine(); 169 170 if (Eng) { 171 StoreManager::InvalidatedRegions Invalidated; 172 const StoreRef &newStore 173 = Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, LCtx, IS, 174 Call, &Invalidated); 175 176 ProgramStateRef newState = makeWithStore(newStore); 177 178 if (ResultsInPointerEscape) 179 newState = Eng->processPointerEscapedOnInvalidateRegions(newState, 180 &IS, Regions, Invalidated, Call); 181 182 return Eng->processRegionChanges(newState, &IS, Regions, Invalidated, Call); 183 } 184 185 const StoreRef &newStore = 186 Mgr.StoreMgr->invalidateRegions(getStore(), Regions, E, Count, LCtx, IS, 187 Call, NULL); 188 return makeWithStore(newStore); 189} 190 191ProgramStateRef ProgramState::killBinding(Loc LV) const { 192 assert(!isa<loc::MemRegionVal>(LV) && "Use invalidateRegion instead."); 193 194 Store OldStore = getStore(); 195 const StoreRef &newStore = 196 getStateManager().StoreMgr->killBinding(OldStore, LV); 197 198 if (newStore.getStore() == OldStore) 199 return this; 200 201 return makeWithStore(newStore); 202} 203 204ProgramStateRef 205ProgramState::enterStackFrame(const CallEvent &Call, 206 const StackFrameContext *CalleeCtx) const { 207 const StoreRef &NewStore = 208 getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx); 209 return makeWithStore(NewStore); 210} 211 212SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const { 213 // We only want to do fetches from regions that we can actually bind 214 // values. For example, SymbolicRegions of type 'id<...>' cannot 215 // have direct bindings (but their can be bindings on their subregions). 216 if (!R->isBoundable()) 217 return UnknownVal(); 218 219 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { 220 QualType T = TR->getValueType(); 221 if (Loc::isLocType(T) || T->isIntegerType()) 222 return getSVal(R); 223 } 224 225 return UnknownVal(); 226} 227 228SVal ProgramState::getSVal(Loc location, QualType T) const { 229 SVal V = getRawSVal(cast<Loc>(location), T); 230 231 // If 'V' is a symbolic value that is *perfectly* constrained to 232 // be a constant value, use that value instead to lessen the burden 233 // on later analysis stages (so we have less symbolic values to reason 234 // about). 235 if (!T.isNull()) { 236 if (SymbolRef sym = V.getAsSymbol()) { 237 if (const llvm::APSInt *Int = getStateManager() 238 .getConstraintManager() 239 .getSymVal(this, sym)) { 240 // FIXME: Because we don't correctly model (yet) sign-extension 241 // and truncation of symbolic values, we need to convert 242 // the integer value to the correct signedness and bitwidth. 243 // 244 // This shows up in the following: 245 // 246 // char foo(); 247 // unsigned x = foo(); 248 // if (x == 54) 249 // ... 250 // 251 // The symbolic value stored to 'x' is actually the conjured 252 // symbol for the call to foo(); the type of that symbol is 'char', 253 // not unsigned. 254 const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int); 255 256 if (isa<Loc>(V)) 257 return loc::ConcreteInt(NewV); 258 else 259 return nonloc::ConcreteInt(NewV); 260 } 261 } 262 } 263 264 return V; 265} 266 267ProgramStateRef ProgramState::BindExpr(const Stmt *S, 268 const LocationContext *LCtx, 269 SVal V, bool Invalidate) const{ 270 Environment NewEnv = 271 getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V, 272 Invalidate); 273 if (NewEnv == Env) 274 return this; 275 276 ProgramState NewSt = *this; 277 NewSt.Env = NewEnv; 278 return getStateManager().getPersistentState(NewSt); 279} 280 281ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx, 282 DefinedOrUnknownSVal UpperBound, 283 bool Assumption, 284 QualType indexTy) const { 285 if (Idx.isUnknown() || UpperBound.isUnknown()) 286 return this; 287 288 // Build an expression for 0 <= Idx < UpperBound. 289 // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed. 290 // FIXME: This should probably be part of SValBuilder. 291 ProgramStateManager &SM = getStateManager(); 292 SValBuilder &svalBuilder = SM.getSValBuilder(); 293 ASTContext &Ctx = svalBuilder.getContext(); 294 295 // Get the offset: the minimum value of the array index type. 296 BasicValueFactory &BVF = svalBuilder.getBasicValueFactory(); 297 // FIXME: This should be using ValueManager::ArrayindexTy...somehow. 298 if (indexTy.isNull()) 299 indexTy = Ctx.IntTy; 300 nonloc::ConcreteInt Min(BVF.getMinValue(indexTy)); 301 302 // Adjust the index. 303 SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add, 304 cast<NonLoc>(Idx), Min, indexTy); 305 if (newIdx.isUnknownOrUndef()) 306 return this; 307 308 // Adjust the upper bound. 309 SVal newBound = 310 svalBuilder.evalBinOpNN(this, BO_Add, cast<NonLoc>(UpperBound), 311 Min, indexTy); 312 313 if (newBound.isUnknownOrUndef()) 314 return this; 315 316 // Build the actual comparison. 317 SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, 318 cast<NonLoc>(newIdx), cast<NonLoc>(newBound), 319 Ctx.IntTy); 320 if (inBound.isUnknownOrUndef()) 321 return this; 322 323 // Finally, let the constraint manager take care of it. 324 ConstraintManager &CM = SM.getConstraintManager(); 325 return CM.assume(this, cast<DefinedSVal>(inBound), Assumption); 326} 327 328ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) { 329 ProgramState State(this, 330 EnvMgr.getInitialEnvironment(), 331 StoreMgr->getInitialStore(InitLoc), 332 GDMFactory.getEmptyMap()); 333 334 return getPersistentState(State); 335} 336 337ProgramStateRef ProgramStateManager::getPersistentStateWithGDM( 338 ProgramStateRef FromState, 339 ProgramStateRef GDMState) { 340 ProgramState NewState(*FromState); 341 NewState.GDM = GDMState->GDM; 342 return getPersistentState(NewState); 343} 344 345ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) { 346 347 llvm::FoldingSetNodeID ID; 348 State.Profile(ID); 349 void *InsertPos; 350 351 if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) 352 return I; 353 354 ProgramState *newState = 0; 355 if (!freeStates.empty()) { 356 newState = freeStates.back(); 357 freeStates.pop_back(); 358 } 359 else { 360 newState = (ProgramState*) Alloc.Allocate<ProgramState>(); 361 } 362 new (newState) ProgramState(State); 363 StateSet.InsertNode(newState, InsertPos); 364 return newState; 365} 366 367ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const { 368 ProgramState NewSt(*this); 369 NewSt.setStore(store); 370 return getStateManager().getPersistentState(NewSt); 371} 372 373void ProgramState::setStore(const StoreRef &newStore) { 374 Store newStoreStore = newStore.getStore(); 375 if (newStoreStore) 376 stateMgr->getStoreManager().incrementReferenceCount(newStoreStore); 377 if (store) 378 stateMgr->getStoreManager().decrementReferenceCount(store); 379 store = newStoreStore; 380} 381 382//===----------------------------------------------------------------------===// 383// State pretty-printing. 384//===----------------------------------------------------------------------===// 385 386void ProgramState::print(raw_ostream &Out, 387 const char *NL, const char *Sep) const { 388 // Print the store. 389 ProgramStateManager &Mgr = getStateManager(); 390 Mgr.getStoreManager().print(getStore(), Out, NL, Sep); 391 392 // Print out the environment. 393 Env.print(Out, NL, Sep); 394 395 // Print out the constraints. 396 Mgr.getConstraintManager().print(this, Out, NL, Sep); 397 398 // Print checker-specific data. 399 Mgr.getOwningEngine()->printState(Out, this, NL, Sep); 400} 401 402void ProgramState::printDOT(raw_ostream &Out) const { 403 print(Out, "\\l", "\\|"); 404} 405 406void ProgramState::dump() const { 407 print(llvm::errs()); 408} 409 410void ProgramState::printTaint(raw_ostream &Out, 411 const char *NL, const char *Sep) const { 412 TaintMapImpl TM = get<TaintMap>(); 413 414 if (!TM.isEmpty()) 415 Out <<"Tainted Symbols:" << NL; 416 417 for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) { 418 Out << I->first << " : " << I->second << NL; 419 } 420} 421 422void ProgramState::dumpTaint() const { 423 printTaint(llvm::errs()); 424} 425 426//===----------------------------------------------------------------------===// 427// Generic Data Map. 428//===----------------------------------------------------------------------===// 429 430void *const* ProgramState::FindGDM(void *K) const { 431 return GDM.lookup(K); 432} 433 434void* 435ProgramStateManager::FindGDMContext(void *K, 436 void *(*CreateContext)(llvm::BumpPtrAllocator&), 437 void (*DeleteContext)(void*)) { 438 439 std::pair<void*, void (*)(void*)>& p = GDMContexts[K]; 440 if (!p.first) { 441 p.first = CreateContext(Alloc); 442 p.second = DeleteContext; 443 } 444 445 return p.first; 446} 447 448ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){ 449 ProgramState::GenericDataMap M1 = St->getGDM(); 450 ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data); 451 452 if (M1 == M2) 453 return St; 454 455 ProgramState NewSt = *St; 456 NewSt.GDM = M2; 457 return getPersistentState(NewSt); 458} 459 460ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) { 461 ProgramState::GenericDataMap OldM = state->getGDM(); 462 ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key); 463 464 if (NewM == OldM) 465 return state; 466 467 ProgramState NewState = *state; 468 NewState.GDM = NewM; 469 return getPersistentState(NewState); 470} 471 472bool ScanReachableSymbols::scan(nonloc::CompoundVal val) { 473 for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I) 474 if (!scan(*I)) 475 return false; 476 477 return true; 478} 479 480bool ScanReachableSymbols::scan(const SymExpr *sym) { 481 unsigned &isVisited = visited[sym]; 482 if (isVisited) 483 return true; 484 isVisited = 1; 485 486 if (!visitor.VisitSymbol(sym)) 487 return false; 488 489 // TODO: should be rewritten using SymExpr::symbol_iterator. 490 switch (sym->getKind()) { 491 case SymExpr::RegionValueKind: 492 case SymExpr::ConjuredKind: 493 case SymExpr::DerivedKind: 494 case SymExpr::ExtentKind: 495 case SymExpr::MetadataKind: 496 break; 497 case SymExpr::CastSymbolKind: 498 return scan(cast<SymbolCast>(sym)->getOperand()); 499 case SymExpr::SymIntKind: 500 return scan(cast<SymIntExpr>(sym)->getLHS()); 501 case SymExpr::IntSymKind: 502 return scan(cast<IntSymExpr>(sym)->getRHS()); 503 case SymExpr::SymSymKind: { 504 const SymSymExpr *x = cast<SymSymExpr>(sym); 505 return scan(x->getLHS()) && scan(x->getRHS()); 506 } 507 } 508 return true; 509} 510 511bool ScanReachableSymbols::scan(SVal val) { 512 if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val)) 513 return scan(X->getRegion()); 514 515 if (nonloc::LazyCompoundVal *X = dyn_cast<nonloc::LazyCompoundVal>(&val)) 516 return scan(X->getRegion()); 517 518 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val)) 519 return scan(X->getLoc()); 520 521 if (SymbolRef Sym = val.getAsSymbol()) 522 return scan(Sym); 523 524 if (const SymExpr *Sym = val.getAsSymbolicExpression()) 525 return scan(Sym); 526 527 if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val)) 528 return scan(*X); 529 530 return true; 531} 532 533bool ScanReachableSymbols::scan(const MemRegion *R) { 534 if (isa<MemSpaceRegion>(R)) 535 return true; 536 537 unsigned &isVisited = visited[R]; 538 if (isVisited) 539 return true; 540 isVisited = 1; 541 542 543 if (!visitor.VisitMemRegion(R)) 544 return false; 545 546 // If this is a symbolic region, visit the symbol for the region. 547 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) 548 if (!visitor.VisitSymbol(SR->getSymbol())) 549 return false; 550 551 // If this is a subregion, also visit the parent regions. 552 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) { 553 const MemRegion *Super = SR->getSuperRegion(); 554 if (!scan(Super)) 555 return false; 556 557 // When we reach the topmost region, scan all symbols in it. 558 if (isa<MemSpaceRegion>(Super)) { 559 StoreManager &StoreMgr = state->getStateManager().getStoreManager(); 560 if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this)) 561 return false; 562 } 563 } 564 565 // Regions captured by a block are also implicitly reachable. 566 if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) { 567 BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(), 568 E = BDR->referenced_vars_end(); 569 for ( ; I != E; ++I) { 570 if (!scan(I.getCapturedRegion())) 571 return false; 572 } 573 } 574 575 return true; 576} 577 578bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const { 579 ScanReachableSymbols S(this, visitor); 580 return S.scan(val); 581} 582 583bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E, 584 SymbolVisitor &visitor) const { 585 ScanReachableSymbols S(this, visitor); 586 for ( ; I != E; ++I) { 587 if (!S.scan(*I)) 588 return false; 589 } 590 return true; 591} 592 593bool ProgramState::scanReachableSymbols(const MemRegion * const *I, 594 const MemRegion * const *E, 595 SymbolVisitor &visitor) const { 596 ScanReachableSymbols S(this, visitor); 597 for ( ; I != E; ++I) { 598 if (!S.scan(*I)) 599 return false; 600 } 601 return true; 602} 603 604ProgramStateRef ProgramState::addTaint(const Stmt *S, 605 const LocationContext *LCtx, 606 TaintTagType Kind) const { 607 if (const Expr *E = dyn_cast_or_null<Expr>(S)) 608 S = E->IgnoreParens(); 609 610 SymbolRef Sym = getSVal(S, LCtx).getAsSymbol(); 611 if (Sym) 612 return addTaint(Sym, Kind); 613 614 const MemRegion *R = getSVal(S, LCtx).getAsRegion(); 615 addTaint(R, Kind); 616 617 // Cannot add taint, so just return the state. 618 return this; 619} 620 621ProgramStateRef ProgramState::addTaint(const MemRegion *R, 622 TaintTagType Kind) const { 623 if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R)) 624 return addTaint(SR->getSymbol(), Kind); 625 return this; 626} 627 628ProgramStateRef ProgramState::addTaint(SymbolRef Sym, 629 TaintTagType Kind) const { 630 // If this is a symbol cast, remove the cast before adding the taint. Taint 631 // is cast agnostic. 632 while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym)) 633 Sym = SC->getOperand(); 634 635 ProgramStateRef NewState = set<TaintMap>(Sym, Kind); 636 assert(NewState); 637 return NewState; 638} 639 640bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx, 641 TaintTagType Kind) const { 642 if (const Expr *E = dyn_cast_or_null<Expr>(S)) 643 S = E->IgnoreParens(); 644 645 SVal val = getSVal(S, LCtx); 646 return isTainted(val, Kind); 647} 648 649bool ProgramState::isTainted(SVal V, TaintTagType Kind) const { 650 if (const SymExpr *Sym = V.getAsSymExpr()) 651 return isTainted(Sym, Kind); 652 if (const MemRegion *Reg = V.getAsRegion()) 653 return isTainted(Reg, Kind); 654 return false; 655} 656 657bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const { 658 if (!Reg) 659 return false; 660 661 // Element region (array element) is tainted if either the base or the offset 662 // are tainted. 663 if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg)) 664 return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K); 665 666 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) 667 return isTainted(SR->getSymbol(), K); 668 669 if (const SubRegion *ER = dyn_cast<SubRegion>(Reg)) 670 return isTainted(ER->getSuperRegion(), K); 671 672 return false; 673} 674 675bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const { 676 if (!Sym) 677 return false; 678 679 // Traverse all the symbols this symbol depends on to see if any are tainted. 680 bool Tainted = false; 681 for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end(); 682 SI != SE; ++SI) { 683 if (!isa<SymbolData>(*SI)) 684 continue; 685 686 const TaintTagType *Tag = get<TaintMap>(*SI); 687 Tainted = (Tag && *Tag == Kind); 688 689 // If this is a SymbolDerived with a tainted parent, it's also tainted. 690 if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI)) 691 Tainted = Tainted || isTainted(SD->getParentSymbol(), Kind); 692 693 // If memory region is tainted, data is also tainted. 694 if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI)) 695 Tainted = Tainted || isTainted(SRV->getRegion(), Kind); 696 697 // If If this is a SymbolCast from a tainted value, it's also tainted. 698 if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI)) 699 Tainted = Tainted || isTainted(SC->getOperand(), Kind); 700 701 if (Tainted) 702 return true; 703 } 704 705 return Tainted; 706} 707 708/// The GDM component containing the dynamic type info. This is a map from a 709/// symbol to its most likely type. 710REGISTER_TRAIT_WITH_PROGRAMSTATE(DynamicTypeMap, 711 CLANG_ENTO_PROGRAMSTATE_MAP(const MemRegion *, 712 DynamicTypeInfo)) 713 714DynamicTypeInfo ProgramState::getDynamicTypeInfo(const MemRegion *Reg) const { 715 Reg = Reg->StripCasts(); 716 717 // Look up the dynamic type in the GDM. 718 const DynamicTypeInfo *GDMType = get<DynamicTypeMap>(Reg); 719 if (GDMType) 720 return *GDMType; 721 722 // Otherwise, fall back to what we know about the region. 723 if (const TypedRegion *TR = dyn_cast<TypedRegion>(Reg)) 724 return DynamicTypeInfo(TR->getLocationType(), /*CanBeSubclass=*/false); 725 726 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg)) { 727 SymbolRef Sym = SR->getSymbol(); 728 return DynamicTypeInfo(Sym->getType()); 729 } 730 731 return DynamicTypeInfo(); 732} 733 734ProgramStateRef ProgramState::setDynamicTypeInfo(const MemRegion *Reg, 735 DynamicTypeInfo NewTy) const { 736 Reg = Reg->StripCasts(); 737 ProgramStateRef NewState = set<DynamicTypeMap>(Reg, NewTy); 738 assert(NewState); 739 return NewState; 740} 741