1 //== ProgramState.h - 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 defines the state of the program along the analysisa path. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_GR_VALUESTATE_H 15 #define LLVM_CLANG_GR_VALUESTATE_H 16 17 #include "clang/Basic/LLVM.h" 18 #include "clang/StaticAnalyzer/Core/PathSensitive/ConstraintManager.h" 19 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeInfo.h" 20 #include "clang/StaticAnalyzer/Core/PathSensitive/Environment.h" 21 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h" 22 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 23 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h" 24 #include "clang/StaticAnalyzer/Core/PathSensitive/TaintTag.h" 25 #include "llvm/ADT/FoldingSet.h" 26 #include "llvm/ADT/ImmutableMap.h" 27 #include "llvm/ADT/PointerIntPair.h" 28 29 namespace llvm { 30 class APSInt; 31 class BumpPtrAllocator; 32 } 33 34 namespace clang { 35 class ASTContext; 36 37 namespace ento { 38 39 class CallEvent; 40 class CallEventManager; 41 42 typedef ConstraintManager* (*ConstraintManagerCreator)(ProgramStateManager&, 43 SubEngine*); 44 typedef StoreManager* (*StoreManagerCreator)(ProgramStateManager&); 45 46 //===----------------------------------------------------------------------===// 47 // ProgramStateTrait - Traits used by the Generic Data Map of a ProgramState. 48 //===----------------------------------------------------------------------===// 49 50 template <typename T> struct ProgramStatePartialTrait; 51 52 template <typename T> struct ProgramStateTrait { 53 typedef typename T::data_type data_type; 54 static inline void *MakeVoidPtr(data_type D) { return (void*) D; } 55 static inline data_type MakeData(void *const* P) { 56 return P ? (data_type) *P : (data_type) 0; 57 } 58 }; 59 60 /// \class ProgramState 61 /// ProgramState - This class encapsulates: 62 /// 63 /// 1. A mapping from expressions to values (Environment) 64 /// 2. A mapping from locations to values (Store) 65 /// 3. Constraints on symbolic values (GenericDataMap) 66 /// 67 /// Together these represent the "abstract state" of a program. 68 /// 69 /// ProgramState is intended to be used as a functional object; that is, 70 /// once it is created and made "persistent" in a FoldingSet, its 71 /// values will never change. 72 class ProgramState : public llvm::FoldingSetNode { 73 public: 74 typedef llvm::ImmutableSet<llvm::APSInt*> IntSetTy; 75 typedef llvm::ImmutableMap<void*, void*> GenericDataMap; 76 77 private: 78 void operator=(const ProgramState& R) LLVM_DELETED_FUNCTION; 79 80 friend class ProgramStateManager; 81 friend class ExplodedGraph; 82 friend class ExplodedNode; 83 84 ProgramStateManager *stateMgr; 85 Environment Env; // Maps a Stmt to its current SVal. 86 Store store; // Maps a location to its current value. 87 GenericDataMap GDM; // Custom data stored by a client of this class. 88 unsigned refCount; 89 90 /// makeWithStore - Return a ProgramState with the same values as the current 91 /// state with the exception of using the specified Store. 92 ProgramStateRef makeWithStore(const StoreRef &store) const; 93 94 void setStore(const StoreRef &storeRef); 95 96 public: 97 /// This ctor is used when creating the first ProgramState object. 98 ProgramState(ProgramStateManager *mgr, const Environment& env, 99 StoreRef st, GenericDataMap gdm); 100 101 /// Copy ctor - We must explicitly define this or else the "Next" ptr 102 /// in FoldingSetNode will also get copied. 103 ProgramState(const ProgramState &RHS); 104 105 ~ProgramState(); 106 107 /// Return the ProgramStateManager associated with this state. 108 ProgramStateManager &getStateManager() const { 109 return *stateMgr; 110 } 111 112 /// Return the ConstraintManager. 113 ConstraintManager &getConstraintManager() const; 114 115 /// getEnvironment - Return the environment associated with this state. 116 /// The environment is the mapping from expressions to values. 117 const Environment& getEnvironment() const { return Env; } 118 119 /// Return the store associated with this state. The store 120 /// is a mapping from locations to values. 121 Store getStore() const { return store; } 122 123 124 /// getGDM - Return the generic data map associated with this state. 125 GenericDataMap getGDM() const { return GDM; } 126 127 void setGDM(GenericDataMap gdm) { GDM = gdm; } 128 129 /// Profile - Profile the contents of a ProgramState object for use in a 130 /// FoldingSet. Two ProgramState objects are considered equal if they 131 /// have the same Environment, Store, and GenericDataMap. 132 static void Profile(llvm::FoldingSetNodeID& ID, const ProgramState *V) { 133 V->Env.Profile(ID); 134 ID.AddPointer(V->store); 135 V->GDM.Profile(ID); 136 } 137 138 /// Profile - Used to profile the contents of this object for inclusion 139 /// in a FoldingSet. 140 void Profile(llvm::FoldingSetNodeID& ID) const { 141 Profile(ID, this); 142 } 143 144 BasicValueFactory &getBasicVals() const; 145 SymbolManager &getSymbolManager() const; 146 147 //==---------------------------------------------------------------------==// 148 // Constraints on values. 149 //==---------------------------------------------------------------------==// 150 // 151 // Each ProgramState records constraints on symbolic values. These constraints 152 // are managed using the ConstraintManager associated with a ProgramStateManager. 153 // As constraints gradually accrue on symbolic values, added constraints 154 // may conflict and indicate that a state is infeasible (as no real values 155 // could satisfy all the constraints). This is the principal mechanism 156 // for modeling path-sensitivity in ExprEngine/ProgramState. 157 // 158 // Various "assume" methods form the interface for adding constraints to 159 // symbolic values. A call to 'assume' indicates an assumption being placed 160 // on one or symbolic values. 'assume' methods take the following inputs: 161 // 162 // (1) A ProgramState object representing the current state. 163 // 164 // (2) The assumed constraint (which is specific to a given "assume" method). 165 // 166 // (3) A binary value "Assumption" that indicates whether the constraint is 167 // assumed to be true or false. 168 // 169 // The output of "assume*" is a new ProgramState object with the added constraints. 170 // If no new state is feasible, NULL is returned. 171 // 172 173 /// Assumes that the value of \p cond is zero (if \p assumption is "false") 174 /// or non-zero (if \p assumption is "true"). 175 /// 176 /// This returns a new state with the added constraint on \p cond. 177 /// If no new state is feasible, NULL is returned. 178 ProgramStateRef assume(DefinedOrUnknownSVal cond, bool assumption) const; 179 180 /// Assumes both "true" and "false" for \p cond, and returns both 181 /// corresponding states (respectively). 182 /// 183 /// This is more efficient than calling assume() twice. Note that one (but not 184 /// both) of the returned states may be NULL. 185 std::pair<ProgramStateRef, ProgramStateRef> 186 assume(DefinedOrUnknownSVal cond) const; 187 188 ProgramStateRef assumeInBound(DefinedOrUnknownSVal idx, 189 DefinedOrUnknownSVal upperBound, 190 bool assumption, 191 QualType IndexType = QualType()) const; 192 193 /// \brief Check if the given SVal is constrained to zero or is a zero 194 /// constant. 195 ConditionTruthVal isNull(SVal V) const; 196 197 /// Utility method for getting regions. 198 const VarRegion* getRegion(const VarDecl *D, const LocationContext *LC) const; 199 200 //==---------------------------------------------------------------------==// 201 // Binding and retrieving values to/from the environment and symbolic store. 202 //==---------------------------------------------------------------------==// 203 204 /// \brief Create a new state with the specified CompoundLiteral binding. 205 /// \param CL the compound literal expression (the binding key) 206 /// \param LC the LocationContext of the binding 207 /// \param V the value to bind. 208 ProgramStateRef bindCompoundLiteral(const CompoundLiteralExpr *CL, 209 const LocationContext *LC, 210 SVal V) const; 211 212 /// Create a new state by binding the value 'V' to the statement 'S' in the 213 /// state's environment. 214 ProgramStateRef BindExpr(const Stmt *S, const LocationContext *LCtx, 215 SVal V, bool Invalidate = true) const; 216 217 ProgramStateRef bindLoc(Loc location, 218 SVal V, 219 bool notifyChanges = true) const; 220 221 ProgramStateRef bindLoc(SVal location, SVal V) const; 222 223 ProgramStateRef bindDefault(SVal loc, SVal V) const; 224 225 ProgramStateRef killBinding(Loc LV) const; 226 227 /// \brief Returns the state with bindings for the given regions 228 /// cleared from the store. 229 /// 230 /// Optionally invalidates global regions as well. 231 /// 232 /// \param Regions the set of regions to be invalidated. 233 /// \param E the expression that caused the invalidation. 234 /// \param BlockCount The number of times the current basic block has been 235 // visited. 236 /// \param CausesPointerEscape the flag is set to true when 237 /// the invalidation entails escape of a symbol (representing a 238 /// pointer). For example, due to it being passed as an argument in a 239 /// call. 240 /// \param IS the set of invalidated symbols. 241 /// \param Call if non-null, the invalidated regions represent parameters to 242 /// the call and should be considered directly invalidated. 243 ProgramStateRef invalidateRegions(ArrayRef<const MemRegion *> Regions, 244 const Expr *E, unsigned BlockCount, 245 const LocationContext *LCtx, 246 bool CausesPointerEscape, 247 InvalidatedSymbols *IS = 0, 248 const CallEvent *Call = 0) const; 249 250 /// enterStackFrame - Returns the state for entry to the given stack frame, 251 /// preserving the current state. 252 ProgramStateRef enterStackFrame(const CallEvent &Call, 253 const StackFrameContext *CalleeCtx) const; 254 255 /// Get the lvalue for a variable reference. 256 Loc getLValue(const VarDecl *D, const LocationContext *LC) const; 257 258 Loc getLValue(const CompoundLiteralExpr *literal, 259 const LocationContext *LC) const; 260 261 /// Get the lvalue for an ivar reference. 262 SVal getLValue(const ObjCIvarDecl *decl, SVal base) const; 263 264 /// Get the lvalue for a field reference. 265 SVal getLValue(const FieldDecl *decl, SVal Base) const; 266 267 /// Get the lvalue for an indirect field reference. 268 SVal getLValue(const IndirectFieldDecl *decl, SVal Base) const; 269 270 /// Get the lvalue for an array index. 271 SVal getLValue(QualType ElementType, SVal Idx, SVal Base) const; 272 273 /// Returns the SVal bound to the statement 'S' in the state's environment. 274 SVal getSVal(const Stmt *S, const LocationContext *LCtx) const; 275 276 SVal getSValAsScalarOrLoc(const Stmt *Ex, const LocationContext *LCtx) const; 277 278 /// \brief Return the value bound to the specified location. 279 /// Returns UnknownVal() if none found. 280 SVal getSVal(Loc LV, QualType T = QualType()) const; 281 282 /// Returns the "raw" SVal bound to LV before any value simplfication. 283 SVal getRawSVal(Loc LV, QualType T= QualType()) const; 284 285 /// \brief Return the value bound to the specified location. 286 /// Returns UnknownVal() if none found. 287 SVal getSVal(const MemRegion* R) const; 288 289 SVal getSValAsScalarOrLoc(const MemRegion *R) const; 290 291 /// \brief Visits the symbols reachable from the given SVal using the provided 292 /// SymbolVisitor. 293 /// 294 /// This is a convenience API. Consider using ScanReachableSymbols class 295 /// directly when making multiple scans on the same state with the same 296 /// visitor to avoid repeated initialization cost. 297 /// \sa ScanReachableSymbols 298 bool scanReachableSymbols(SVal val, SymbolVisitor& visitor) const; 299 300 /// \brief Visits the symbols reachable from the SVals in the given range 301 /// using the provided SymbolVisitor. 302 bool scanReachableSymbols(const SVal *I, const SVal *E, 303 SymbolVisitor &visitor) const; 304 305 /// \brief Visits the symbols reachable from the regions in the given 306 /// MemRegions range using the provided SymbolVisitor. 307 bool scanReachableSymbols(const MemRegion * const *I, 308 const MemRegion * const *E, 309 SymbolVisitor &visitor) const; 310 311 template <typename CB> CB scanReachableSymbols(SVal val) const; 312 template <typename CB> CB scanReachableSymbols(const SVal *beg, 313 const SVal *end) const; 314 315 template <typename CB> CB 316 scanReachableSymbols(const MemRegion * const *beg, 317 const MemRegion * const *end) const; 318 319 /// Create a new state in which the statement is marked as tainted. 320 ProgramStateRef addTaint(const Stmt *S, const LocationContext *LCtx, 321 TaintTagType Kind = TaintTagGeneric) const; 322 323 /// Create a new state in which the symbol is marked as tainted. 324 ProgramStateRef addTaint(SymbolRef S, 325 TaintTagType Kind = TaintTagGeneric) const; 326 327 /// Create a new state in which the region symbol is marked as tainted. 328 ProgramStateRef addTaint(const MemRegion *R, 329 TaintTagType Kind = TaintTagGeneric) const; 330 331 /// Check if the statement is tainted in the current state. 332 bool isTainted(const Stmt *S, const LocationContext *LCtx, 333 TaintTagType Kind = TaintTagGeneric) const; 334 bool isTainted(SVal V, TaintTagType Kind = TaintTagGeneric) const; 335 bool isTainted(SymbolRef Sym, TaintTagType Kind = TaintTagGeneric) const; 336 bool isTainted(const MemRegion *Reg, TaintTagType Kind=TaintTagGeneric) const; 337 338 /// \brief Get dynamic type information for a region. 339 DynamicTypeInfo getDynamicTypeInfo(const MemRegion *Reg) const; 340 341 /// \brief Set dynamic type information of the region; return the new state. 342 ProgramStateRef setDynamicTypeInfo(const MemRegion *Reg, 343 DynamicTypeInfo NewTy) const; 344 345 /// \brief Set dynamic type information of the region; return the new state. 346 ProgramStateRef setDynamicTypeInfo(const MemRegion *Reg, 347 QualType NewTy, 348 bool CanBeSubClassed = true) const { 349 return setDynamicTypeInfo(Reg, DynamicTypeInfo(NewTy, CanBeSubClassed)); 350 } 351 352 //==---------------------------------------------------------------------==// 353 // Accessing the Generic Data Map (GDM). 354 //==---------------------------------------------------------------------==// 355 356 void *const* FindGDM(void *K) const; 357 358 template<typename T> 359 ProgramStateRef add(typename ProgramStateTrait<T>::key_type K) const; 360 361 template <typename T> 362 typename ProgramStateTrait<T>::data_type 363 get() const { 364 return ProgramStateTrait<T>::MakeData(FindGDM(ProgramStateTrait<T>::GDMIndex())); 365 } 366 367 template<typename T> 368 typename ProgramStateTrait<T>::lookup_type 369 get(typename ProgramStateTrait<T>::key_type key) const { 370 void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex()); 371 return ProgramStateTrait<T>::Lookup(ProgramStateTrait<T>::MakeData(d), key); 372 } 373 374 template <typename T> 375 typename ProgramStateTrait<T>::context_type get_context() const; 376 377 378 template<typename T> 379 ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K) const; 380 381 template<typename T> 382 ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K, 383 typename ProgramStateTrait<T>::context_type C) const; 384 template <typename T> 385 ProgramStateRef remove() const; 386 387 template<typename T> 388 ProgramStateRef set(typename ProgramStateTrait<T>::data_type D) const; 389 390 template<typename T> 391 ProgramStateRef set(typename ProgramStateTrait<T>::key_type K, 392 typename ProgramStateTrait<T>::value_type E) const; 393 394 template<typename T> 395 ProgramStateRef set(typename ProgramStateTrait<T>::key_type K, 396 typename ProgramStateTrait<T>::value_type E, 397 typename ProgramStateTrait<T>::context_type C) const; 398 399 template<typename T> 400 bool contains(typename ProgramStateTrait<T>::key_type key) const { 401 void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex()); 402 return ProgramStateTrait<T>::Contains(ProgramStateTrait<T>::MakeData(d), key); 403 } 404 405 // Pretty-printing. 406 void print(raw_ostream &Out, const char *nl = "\n", 407 const char *sep = "") const; 408 void printDOT(raw_ostream &Out) const; 409 void printTaint(raw_ostream &Out, const char *nl = "\n", 410 const char *sep = "") const; 411 412 void dump() const; 413 void dumpTaint() const; 414 415 private: 416 friend void ProgramStateRetain(const ProgramState *state); 417 friend void ProgramStateRelease(const ProgramState *state); 418 419 ProgramStateRef 420 invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions, 421 const Expr *E, unsigned BlockCount, 422 const LocationContext *LCtx, 423 bool ResultsInSymbolEscape, 424 InvalidatedSymbols &IS, 425 const CallEvent *Call) const; 426 }; 427 428 //===----------------------------------------------------------------------===// 429 // ProgramStateManager - Factory object for ProgramStates. 430 //===----------------------------------------------------------------------===// 431 432 class ProgramStateManager { 433 friend class ProgramState; 434 friend void ProgramStateRelease(const ProgramState *state); 435 private: 436 /// Eng - The SubEngine that owns this state manager. 437 SubEngine *Eng; /* Can be null. */ 438 439 EnvironmentManager EnvMgr; 440 OwningPtr<StoreManager> StoreMgr; 441 OwningPtr<ConstraintManager> ConstraintMgr; 442 443 ProgramState::GenericDataMap::Factory GDMFactory; 444 445 typedef llvm::DenseMap<void*,std::pair<void*,void (*)(void*)> > GDMContextsTy; 446 GDMContextsTy GDMContexts; 447 448 /// StateSet - FoldingSet containing all the states created for analyzing 449 /// a particular function. This is used to unique states. 450 llvm::FoldingSet<ProgramState> StateSet; 451 452 /// Object that manages the data for all created SVals. 453 OwningPtr<SValBuilder> svalBuilder; 454 455 /// Manages memory for created CallEvents. 456 OwningPtr<CallEventManager> CallEventMgr; 457 458 /// A BumpPtrAllocator to allocate states. 459 llvm::BumpPtrAllocator &Alloc; 460 461 /// A vector of ProgramStates that we can reuse. 462 std::vector<ProgramState *> freeStates; 463 464 public: 465 ProgramStateManager(ASTContext &Ctx, 466 StoreManagerCreator CreateStoreManager, 467 ConstraintManagerCreator CreateConstraintManager, 468 llvm::BumpPtrAllocator& alloc, 469 SubEngine *subeng); 470 471 ~ProgramStateManager(); 472 473 ProgramStateRef getInitialState(const LocationContext *InitLoc); 474 475 ASTContext &getContext() { return svalBuilder->getContext(); } 476 const ASTContext &getContext() const { return svalBuilder->getContext(); } 477 478 BasicValueFactory &getBasicVals() { 479 return svalBuilder->getBasicValueFactory(); 480 } 481 482 SValBuilder &getSValBuilder() { 483 return *svalBuilder; 484 } 485 486 SymbolManager &getSymbolManager() { 487 return svalBuilder->getSymbolManager(); 488 } 489 const SymbolManager &getSymbolManager() const { 490 return svalBuilder->getSymbolManager(); 491 } 492 493 llvm::BumpPtrAllocator& getAllocator() { return Alloc; } 494 495 MemRegionManager& getRegionManager() { 496 return svalBuilder->getRegionManager(); 497 } 498 const MemRegionManager& getRegionManager() const { 499 return svalBuilder->getRegionManager(); 500 } 501 502 CallEventManager &getCallEventManager() { return *CallEventMgr; } 503 504 StoreManager& getStoreManager() { return *StoreMgr; } 505 ConstraintManager& getConstraintManager() { return *ConstraintMgr; } 506 SubEngine* getOwningEngine() { return Eng; } 507 508 ProgramStateRef removeDeadBindings(ProgramStateRef St, 509 const StackFrameContext *LCtx, 510 SymbolReaper& SymReaper); 511 512 public: 513 514 SVal ArrayToPointer(Loc Array) { 515 return StoreMgr->ArrayToPointer(Array); 516 } 517 518 // Methods that manipulate the GDM. 519 ProgramStateRef addGDM(ProgramStateRef St, void *Key, void *Data); 520 ProgramStateRef removeGDM(ProgramStateRef state, void *Key); 521 522 // Methods that query & manipulate the Store. 523 524 void iterBindings(ProgramStateRef state, StoreManager::BindingsHandler& F) { 525 StoreMgr->iterBindings(state->getStore(), F); 526 } 527 528 ProgramStateRef getPersistentState(ProgramState &Impl); 529 ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState, 530 ProgramStateRef GDMState); 531 532 bool haveEqualEnvironments(ProgramStateRef S1, ProgramStateRef S2) { 533 return S1->Env == S2->Env; 534 } 535 536 bool haveEqualStores(ProgramStateRef S1, ProgramStateRef S2) { 537 return S1->store == S2->store; 538 } 539 540 //==---------------------------------------------------------------------==// 541 // Generic Data Map methods. 542 //==---------------------------------------------------------------------==// 543 // 544 // ProgramStateManager and ProgramState support a "generic data map" that allows 545 // different clients of ProgramState objects to embed arbitrary data within a 546 // ProgramState object. The generic data map is essentially an immutable map 547 // from a "tag" (that acts as the "key" for a client) and opaque values. 548 // Tags/keys and values are simply void* values. The typical way that clients 549 // generate unique tags are by taking the address of a static variable. 550 // Clients are responsible for ensuring that data values referred to by a 551 // the data pointer are immutable (and thus are essentially purely functional 552 // data). 553 // 554 // The templated methods below use the ProgramStateTrait<T> class 555 // to resolve keys into the GDM and to return data values to clients. 556 // 557 558 // Trait based GDM dispatch. 559 template <typename T> 560 ProgramStateRef set(ProgramStateRef st, typename ProgramStateTrait<T>::data_type D) { 561 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 562 ProgramStateTrait<T>::MakeVoidPtr(D)); 563 } 564 565 template<typename T> 566 ProgramStateRef set(ProgramStateRef st, 567 typename ProgramStateTrait<T>::key_type K, 568 typename ProgramStateTrait<T>::value_type V, 569 typename ProgramStateTrait<T>::context_type C) { 570 571 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 572 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Set(st->get<T>(), K, V, C))); 573 } 574 575 template <typename T> 576 ProgramStateRef add(ProgramStateRef st, 577 typename ProgramStateTrait<T>::key_type K, 578 typename ProgramStateTrait<T>::context_type C) { 579 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 580 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Add(st->get<T>(), K, C))); 581 } 582 583 template <typename T> 584 ProgramStateRef remove(ProgramStateRef st, 585 typename ProgramStateTrait<T>::key_type K, 586 typename ProgramStateTrait<T>::context_type C) { 587 588 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 589 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Remove(st->get<T>(), K, C))); 590 } 591 592 template <typename T> 593 ProgramStateRef remove(ProgramStateRef st) { 594 return removeGDM(st, ProgramStateTrait<T>::GDMIndex()); 595 } 596 597 void *FindGDMContext(void *index, 598 void *(*CreateContext)(llvm::BumpPtrAllocator&), 599 void (*DeleteContext)(void*)); 600 601 template <typename T> 602 typename ProgramStateTrait<T>::context_type get_context() { 603 void *p = FindGDMContext(ProgramStateTrait<T>::GDMIndex(), 604 ProgramStateTrait<T>::CreateContext, 605 ProgramStateTrait<T>::DeleteContext); 606 607 return ProgramStateTrait<T>::MakeContext(p); 608 } 609 610 void EndPath(ProgramStateRef St) { 611 ConstraintMgr->EndPath(St); 612 } 613 }; 614 615 616 //===----------------------------------------------------------------------===// 617 // Out-of-line method definitions for ProgramState. 618 //===----------------------------------------------------------------------===// 619 620 inline ConstraintManager &ProgramState::getConstraintManager() const { 621 return stateMgr->getConstraintManager(); 622 } 623 624 inline const VarRegion* ProgramState::getRegion(const VarDecl *D, 625 const LocationContext *LC) const 626 { 627 return getStateManager().getRegionManager().getVarRegion(D, LC); 628 } 629 630 inline ProgramStateRef ProgramState::assume(DefinedOrUnknownSVal Cond, 631 bool Assumption) const { 632 if (Cond.isUnknown()) 633 return this; 634 635 return getStateManager().ConstraintMgr 636 ->assume(this, Cond.castAs<DefinedSVal>(), Assumption); 637 } 638 639 inline std::pair<ProgramStateRef , ProgramStateRef > 640 ProgramState::assume(DefinedOrUnknownSVal Cond) const { 641 if (Cond.isUnknown()) 642 return std::make_pair(this, this); 643 644 return getStateManager().ConstraintMgr 645 ->assumeDual(this, Cond.castAs<DefinedSVal>()); 646 } 647 648 inline ProgramStateRef ProgramState::bindLoc(SVal LV, SVal V) const { 649 if (Optional<Loc> L = LV.getAs<Loc>()) 650 return bindLoc(*L, V); 651 return this; 652 } 653 654 inline Loc ProgramState::getLValue(const VarDecl *VD, 655 const LocationContext *LC) const { 656 return getStateManager().StoreMgr->getLValueVar(VD, LC); 657 } 658 659 inline Loc ProgramState::getLValue(const CompoundLiteralExpr *literal, 660 const LocationContext *LC) const { 661 return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC); 662 } 663 664 inline SVal ProgramState::getLValue(const ObjCIvarDecl *D, SVal Base) const { 665 return getStateManager().StoreMgr->getLValueIvar(D, Base); 666 } 667 668 inline SVal ProgramState::getLValue(const FieldDecl *D, SVal Base) const { 669 return getStateManager().StoreMgr->getLValueField(D, Base); 670 } 671 672 inline SVal ProgramState::getLValue(const IndirectFieldDecl *D, 673 SVal Base) const { 674 StoreManager &SM = *getStateManager().StoreMgr; 675 for (IndirectFieldDecl::chain_iterator I = D->chain_begin(), 676 E = D->chain_end(); 677 I != E; ++I) { 678 Base = SM.getLValueField(cast<FieldDecl>(*I), Base); 679 } 680 681 return Base; 682 } 683 684 inline SVal ProgramState::getLValue(QualType ElementType, SVal Idx, SVal Base) const{ 685 if (Optional<NonLoc> N = Idx.getAs<NonLoc>()) 686 return getStateManager().StoreMgr->getLValueElement(ElementType, *N, Base); 687 return UnknownVal(); 688 } 689 690 inline SVal ProgramState::getSVal(const Stmt *Ex, 691 const LocationContext *LCtx) const{ 692 return Env.getSVal(EnvironmentEntry(Ex, LCtx), 693 *getStateManager().svalBuilder); 694 } 695 696 inline SVal 697 ProgramState::getSValAsScalarOrLoc(const Stmt *S, 698 const LocationContext *LCtx) const { 699 if (const Expr *Ex = dyn_cast<Expr>(S)) { 700 QualType T = Ex->getType(); 701 if (Ex->isGLValue() || Loc::isLocType(T) || T->isIntegerType()) 702 return getSVal(S, LCtx); 703 } 704 705 return UnknownVal(); 706 } 707 708 inline SVal ProgramState::getRawSVal(Loc LV, QualType T) const { 709 return getStateManager().StoreMgr->getBinding(getStore(), LV, T); 710 } 711 712 inline SVal ProgramState::getSVal(const MemRegion* R) const { 713 return getStateManager().StoreMgr->getBinding(getStore(), 714 loc::MemRegionVal(R)); 715 } 716 717 inline BasicValueFactory &ProgramState::getBasicVals() const { 718 return getStateManager().getBasicVals(); 719 } 720 721 inline SymbolManager &ProgramState::getSymbolManager() const { 722 return getStateManager().getSymbolManager(); 723 } 724 725 template<typename T> 726 ProgramStateRef ProgramState::add(typename ProgramStateTrait<T>::key_type K) const { 727 return getStateManager().add<T>(this, K, get_context<T>()); 728 } 729 730 template <typename T> 731 typename ProgramStateTrait<T>::context_type ProgramState::get_context() const { 732 return getStateManager().get_context<T>(); 733 } 734 735 template<typename T> 736 ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K) const { 737 return getStateManager().remove<T>(this, K, get_context<T>()); 738 } 739 740 template<typename T> 741 ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K, 742 typename ProgramStateTrait<T>::context_type C) const { 743 return getStateManager().remove<T>(this, K, C); 744 } 745 746 template <typename T> 747 ProgramStateRef ProgramState::remove() const { 748 return getStateManager().remove<T>(this); 749 } 750 751 template<typename T> 752 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::data_type D) const { 753 return getStateManager().set<T>(this, D); 754 } 755 756 template<typename T> 757 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K, 758 typename ProgramStateTrait<T>::value_type E) const { 759 return getStateManager().set<T>(this, K, E, get_context<T>()); 760 } 761 762 template<typename T> 763 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K, 764 typename ProgramStateTrait<T>::value_type E, 765 typename ProgramStateTrait<T>::context_type C) const { 766 return getStateManager().set<T>(this, K, E, C); 767 } 768 769 template <typename CB> 770 CB ProgramState::scanReachableSymbols(SVal val) const { 771 CB cb(this); 772 scanReachableSymbols(val, cb); 773 return cb; 774 } 775 776 template <typename CB> 777 CB ProgramState::scanReachableSymbols(const SVal *beg, const SVal *end) const { 778 CB cb(this); 779 scanReachableSymbols(beg, end, cb); 780 return cb; 781 } 782 783 template <typename CB> 784 CB ProgramState::scanReachableSymbols(const MemRegion * const *beg, 785 const MemRegion * const *end) const { 786 CB cb(this); 787 scanReachableSymbols(beg, end, cb); 788 return cb; 789 } 790 791 /// \class ScanReachableSymbols 792 /// A Utility class that allows to visit the reachable symbols using a custom 793 /// SymbolVisitor. 794 class ScanReachableSymbols { 795 typedef llvm::DenseMap<const void*, unsigned> VisitedItems; 796 797 VisitedItems visited; 798 ProgramStateRef state; 799 SymbolVisitor &visitor; 800 public: 801 802 ScanReachableSymbols(ProgramStateRef st, SymbolVisitor& v) 803 : state(st), visitor(v) {} 804 805 bool scan(nonloc::CompoundVal val); 806 bool scan(SVal val); 807 bool scan(const MemRegion *R); 808 bool scan(const SymExpr *sym); 809 }; 810 811 } // end ento namespace 812 813 } // end clang namespace 814 815 #endif 816