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 /// Create a new state by binding the value 'V' to the statement 'S' in the 205 /// state's environment. 206 ProgramStateRef BindExpr(const Stmt *S, const LocationContext *LCtx, 207 SVal V, bool Invalidate = true) const; 208 209 ProgramStateRef bindLoc(Loc location, 210 SVal V, 211 bool notifyChanges = true) const; 212 213 ProgramStateRef bindLoc(SVal location, SVal V) const; 214 215 ProgramStateRef bindDefault(SVal loc, SVal V) const; 216 217 ProgramStateRef killBinding(Loc LV) const; 218 219 /// \brief Returns the state with bindings for the given regions 220 /// cleared from the store. 221 /// 222 /// Optionally invalidates global regions as well. 223 /// 224 /// \param Regions the set of regions to be invalidated. 225 /// \param E the expression that caused the invalidation. 226 /// \param BlockCount The number of times the current basic block has been 227 // visited. 228 /// \param CausesPointerEscape the flag is set to true when 229 /// the invalidation entails escape of a symbol (representing a 230 /// pointer). For example, due to it being passed as an argument in a 231 /// call. 232 /// \param IS the set of invalidated symbols. 233 /// \param Call if non-null, the invalidated regions represent parameters to 234 /// the call and should be considered directly invalidated. 235 /// \param ConstRegions the set of regions whose contents are accessible, 236 /// even though the regions themselves should not be invalidated. 237 ProgramStateRef 238 invalidateRegions(ArrayRef<const MemRegion *> Regions, const Expr *E, 239 unsigned BlockCount, const LocationContext *LCtx, 240 bool CausesPointerEscape, InvalidatedSymbols *IS = 0, 241 const CallEvent *Call = 0, 242 ArrayRef<const MemRegion *> ConstRegions = 243 ArrayRef<const MemRegion *>()) const; 244 245 ProgramStateRef 246 invalidateRegions(ArrayRef<SVal> Regions, const Expr *E, 247 unsigned BlockCount, const LocationContext *LCtx, 248 bool CausesPointerEscape, InvalidatedSymbols *IS = 0, 249 const CallEvent *Call = 0, 250 ArrayRef<SVal> ConstRegions = ArrayRef<SVal>()) const; 251 252 /// enterStackFrame - Returns the state for entry to the given stack frame, 253 /// preserving the current state. 254 ProgramStateRef enterStackFrame(const CallEvent &Call, 255 const StackFrameContext *CalleeCtx) const; 256 257 /// Get the lvalue for a variable reference. 258 Loc getLValue(const VarDecl *D, const LocationContext *LC) const; 259 260 Loc getLValue(const CompoundLiteralExpr *literal, 261 const LocationContext *LC) const; 262 263 /// Get the lvalue for an ivar reference. 264 SVal getLValue(const ObjCIvarDecl *decl, SVal base) const; 265 266 /// Get the lvalue for a field reference. 267 SVal getLValue(const FieldDecl *decl, SVal Base) const; 268 269 /// Get the lvalue for an indirect field reference. 270 SVal getLValue(const IndirectFieldDecl *decl, SVal Base) const; 271 272 /// Get the lvalue for an array index. 273 SVal getLValue(QualType ElementType, SVal Idx, SVal Base) const; 274 275 /// Returns the SVal bound to the statement 'S' in the state's environment. 276 SVal getSVal(const Stmt *S, const LocationContext *LCtx) const; 277 278 SVal getSValAsScalarOrLoc(const Stmt *Ex, const LocationContext *LCtx) const; 279 280 /// \brief Return the value bound to the specified location. 281 /// Returns UnknownVal() if none found. 282 SVal getSVal(Loc LV, QualType T = QualType()) const; 283 284 /// Returns the "raw" SVal bound to LV before any value simplfication. 285 SVal getRawSVal(Loc LV, QualType T= QualType()) const; 286 287 /// \brief Return the value bound to the specified location. 288 /// Returns UnknownVal() if none found. 289 SVal getSVal(const MemRegion* R) const; 290 291 SVal getSValAsScalarOrLoc(const MemRegion *R) const; 292 293 /// \brief Visits the symbols reachable from the given SVal using the provided 294 /// SymbolVisitor. 295 /// 296 /// This is a convenience API. Consider using ScanReachableSymbols class 297 /// directly when making multiple scans on the same state with the same 298 /// visitor to avoid repeated initialization cost. 299 /// \sa ScanReachableSymbols 300 bool scanReachableSymbols(SVal val, SymbolVisitor& visitor) const; 301 302 /// \brief Visits the symbols reachable from the SVals in the given range 303 /// using the provided SymbolVisitor. 304 bool scanReachableSymbols(const SVal *I, const SVal *E, 305 SymbolVisitor &visitor) const; 306 307 /// \brief Visits the symbols reachable from the regions in the given 308 /// MemRegions range using the provided SymbolVisitor. 309 bool scanReachableSymbols(const MemRegion * const *I, 310 const MemRegion * const *E, 311 SymbolVisitor &visitor) const; 312 313 template <typename CB> CB scanReachableSymbols(SVal val) const; 314 template <typename CB> CB scanReachableSymbols(const SVal *beg, 315 const SVal *end) const; 316 317 template <typename CB> CB 318 scanReachableSymbols(const MemRegion * const *beg, 319 const MemRegion * const *end) const; 320 321 /// Create a new state in which the statement is marked as tainted. 322 ProgramStateRef addTaint(const Stmt *S, const LocationContext *LCtx, 323 TaintTagType Kind = TaintTagGeneric) const; 324 325 /// Create a new state in which the symbol is marked as tainted. 326 ProgramStateRef addTaint(SymbolRef S, 327 TaintTagType Kind = TaintTagGeneric) const; 328 329 /// Create a new state in which the region symbol is marked as tainted. 330 ProgramStateRef addTaint(const MemRegion *R, 331 TaintTagType Kind = TaintTagGeneric) const; 332 333 /// Check if the statement is tainted in the current state. 334 bool isTainted(const Stmt *S, const LocationContext *LCtx, 335 TaintTagType Kind = TaintTagGeneric) const; 336 bool isTainted(SVal V, TaintTagType Kind = TaintTagGeneric) const; 337 bool isTainted(SymbolRef Sym, TaintTagType Kind = TaintTagGeneric) const; 338 bool isTainted(const MemRegion *Reg, TaintTagType Kind=TaintTagGeneric) const; 339 340 /// \brief Get dynamic type information for a region. 341 DynamicTypeInfo getDynamicTypeInfo(const MemRegion *Reg) const; 342 343 /// \brief Set dynamic type information of the region; return the new state. 344 ProgramStateRef setDynamicTypeInfo(const MemRegion *Reg, 345 DynamicTypeInfo NewTy) const; 346 347 /// \brief Set dynamic type information of the region; return the new state. 348 ProgramStateRef setDynamicTypeInfo(const MemRegion *Reg, 349 QualType NewTy, 350 bool CanBeSubClassed = true) const { 351 return setDynamicTypeInfo(Reg, DynamicTypeInfo(NewTy, CanBeSubClassed)); 352 } 353 354 //==---------------------------------------------------------------------==// 355 // Accessing the Generic Data Map (GDM). 356 //==---------------------------------------------------------------------==// 357 358 void *const* FindGDM(void *K) const; 359 360 template<typename T> 361 ProgramStateRef add(typename ProgramStateTrait<T>::key_type K) const; 362 363 template <typename T> 364 typename ProgramStateTrait<T>::data_type 365 get() const { 366 return ProgramStateTrait<T>::MakeData(FindGDM(ProgramStateTrait<T>::GDMIndex())); 367 } 368 369 template<typename T> 370 typename ProgramStateTrait<T>::lookup_type 371 get(typename ProgramStateTrait<T>::key_type key) const { 372 void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex()); 373 return ProgramStateTrait<T>::Lookup(ProgramStateTrait<T>::MakeData(d), key); 374 } 375 376 template <typename T> 377 typename ProgramStateTrait<T>::context_type get_context() const; 378 379 380 template<typename T> 381 ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K) const; 382 383 template<typename T> 384 ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K, 385 typename ProgramStateTrait<T>::context_type C) const; 386 template <typename T> 387 ProgramStateRef remove() const; 388 389 template<typename T> 390 ProgramStateRef set(typename ProgramStateTrait<T>::data_type D) const; 391 392 template<typename T> 393 ProgramStateRef set(typename ProgramStateTrait<T>::key_type K, 394 typename ProgramStateTrait<T>::value_type E) const; 395 396 template<typename T> 397 ProgramStateRef set(typename ProgramStateTrait<T>::key_type K, 398 typename ProgramStateTrait<T>::value_type E, 399 typename ProgramStateTrait<T>::context_type C) const; 400 401 template<typename T> 402 bool contains(typename ProgramStateTrait<T>::key_type key) const { 403 void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex()); 404 return ProgramStateTrait<T>::Contains(ProgramStateTrait<T>::MakeData(d), key); 405 } 406 407 // Pretty-printing. 408 void print(raw_ostream &Out, const char *nl = "\n", 409 const char *sep = "") const; 410 void printDOT(raw_ostream &Out) const; 411 void printTaint(raw_ostream &Out, const char *nl = "\n", 412 const char *sep = "") const; 413 414 void dump() const; 415 void dumpTaint() const; 416 417 private: 418 friend void ProgramStateRetain(const ProgramState *state); 419 friend void ProgramStateRelease(const ProgramState *state); 420 421 /// \sa invalidateValues() 422 /// \sa invalidateRegions() 423 ProgramStateRef 424 invalidateRegionsImpl(ArrayRef<SVal> Values, 425 const Expr *E, unsigned BlockCount, 426 const LocationContext *LCtx, 427 bool ResultsInSymbolEscape, 428 InvalidatedSymbols &IS, 429 const CallEvent *Call, 430 ArrayRef<SVal> ConstValues) const; 431 }; 432 433 //===----------------------------------------------------------------------===// 434 // ProgramStateManager - Factory object for ProgramStates. 435 //===----------------------------------------------------------------------===// 436 437 class ProgramStateManager { 438 friend class ProgramState; 439 friend void ProgramStateRelease(const ProgramState *state); 440 private: 441 /// Eng - The SubEngine that owns this state manager. 442 SubEngine *Eng; /* Can be null. */ 443 444 EnvironmentManager EnvMgr; 445 OwningPtr<StoreManager> StoreMgr; 446 OwningPtr<ConstraintManager> ConstraintMgr; 447 448 ProgramState::GenericDataMap::Factory GDMFactory; 449 450 typedef llvm::DenseMap<void*,std::pair<void*,void (*)(void*)> > GDMContextsTy; 451 GDMContextsTy GDMContexts; 452 453 /// StateSet - FoldingSet containing all the states created for analyzing 454 /// a particular function. This is used to unique states. 455 llvm::FoldingSet<ProgramState> StateSet; 456 457 /// Object that manages the data for all created SVals. 458 OwningPtr<SValBuilder> svalBuilder; 459 460 /// Manages memory for created CallEvents. 461 OwningPtr<CallEventManager> CallEventMgr; 462 463 /// A BumpPtrAllocator to allocate states. 464 llvm::BumpPtrAllocator &Alloc; 465 466 /// A vector of ProgramStates that we can reuse. 467 std::vector<ProgramState *> freeStates; 468 469 public: 470 ProgramStateManager(ASTContext &Ctx, 471 StoreManagerCreator CreateStoreManager, 472 ConstraintManagerCreator CreateConstraintManager, 473 llvm::BumpPtrAllocator& alloc, 474 SubEngine *subeng); 475 476 ~ProgramStateManager(); 477 478 ProgramStateRef getInitialState(const LocationContext *InitLoc); 479 480 ASTContext &getContext() { return svalBuilder->getContext(); } 481 const ASTContext &getContext() const { return svalBuilder->getContext(); } 482 483 BasicValueFactory &getBasicVals() { 484 return svalBuilder->getBasicValueFactory(); 485 } 486 487 SValBuilder &getSValBuilder() { 488 return *svalBuilder; 489 } 490 491 SymbolManager &getSymbolManager() { 492 return svalBuilder->getSymbolManager(); 493 } 494 const SymbolManager &getSymbolManager() const { 495 return svalBuilder->getSymbolManager(); 496 } 497 498 llvm::BumpPtrAllocator& getAllocator() { return Alloc; } 499 500 MemRegionManager& getRegionManager() { 501 return svalBuilder->getRegionManager(); 502 } 503 const MemRegionManager& getRegionManager() const { 504 return svalBuilder->getRegionManager(); 505 } 506 507 CallEventManager &getCallEventManager() { return *CallEventMgr; } 508 509 StoreManager& getStoreManager() { return *StoreMgr; } 510 ConstraintManager& getConstraintManager() { return *ConstraintMgr; } 511 SubEngine* getOwningEngine() { return Eng; } 512 513 ProgramStateRef removeDeadBindings(ProgramStateRef St, 514 const StackFrameContext *LCtx, 515 SymbolReaper& SymReaper); 516 517 public: 518 519 SVal ArrayToPointer(Loc Array, QualType ElementTy) { 520 return StoreMgr->ArrayToPointer(Array, ElementTy); 521 } 522 523 // Methods that manipulate the GDM. 524 ProgramStateRef addGDM(ProgramStateRef St, void *Key, void *Data); 525 ProgramStateRef removeGDM(ProgramStateRef state, void *Key); 526 527 // Methods that query & manipulate the Store. 528 529 void iterBindings(ProgramStateRef state, StoreManager::BindingsHandler& F) { 530 StoreMgr->iterBindings(state->getStore(), F); 531 } 532 533 ProgramStateRef getPersistentState(ProgramState &Impl); 534 ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState, 535 ProgramStateRef GDMState); 536 537 bool haveEqualEnvironments(ProgramStateRef S1, ProgramStateRef S2) { 538 return S1->Env == S2->Env; 539 } 540 541 bool haveEqualStores(ProgramStateRef S1, ProgramStateRef S2) { 542 return S1->store == S2->store; 543 } 544 545 //==---------------------------------------------------------------------==// 546 // Generic Data Map methods. 547 //==---------------------------------------------------------------------==// 548 // 549 // ProgramStateManager and ProgramState support a "generic data map" that allows 550 // different clients of ProgramState objects to embed arbitrary data within a 551 // ProgramState object. The generic data map is essentially an immutable map 552 // from a "tag" (that acts as the "key" for a client) and opaque values. 553 // Tags/keys and values are simply void* values. The typical way that clients 554 // generate unique tags are by taking the address of a static variable. 555 // Clients are responsible for ensuring that data values referred to by a 556 // the data pointer are immutable (and thus are essentially purely functional 557 // data). 558 // 559 // The templated methods below use the ProgramStateTrait<T> class 560 // to resolve keys into the GDM and to return data values to clients. 561 // 562 563 // Trait based GDM dispatch. 564 template <typename T> 565 ProgramStateRef set(ProgramStateRef st, typename ProgramStateTrait<T>::data_type D) { 566 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 567 ProgramStateTrait<T>::MakeVoidPtr(D)); 568 } 569 570 template<typename T> 571 ProgramStateRef set(ProgramStateRef st, 572 typename ProgramStateTrait<T>::key_type K, 573 typename ProgramStateTrait<T>::value_type V, 574 typename ProgramStateTrait<T>::context_type C) { 575 576 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 577 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Set(st->get<T>(), K, V, C))); 578 } 579 580 template <typename T> 581 ProgramStateRef add(ProgramStateRef st, 582 typename ProgramStateTrait<T>::key_type K, 583 typename ProgramStateTrait<T>::context_type C) { 584 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 585 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Add(st->get<T>(), K, C))); 586 } 587 588 template <typename T> 589 ProgramStateRef remove(ProgramStateRef st, 590 typename ProgramStateTrait<T>::key_type K, 591 typename ProgramStateTrait<T>::context_type C) { 592 593 return addGDM(st, ProgramStateTrait<T>::GDMIndex(), 594 ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Remove(st->get<T>(), K, C))); 595 } 596 597 template <typename T> 598 ProgramStateRef remove(ProgramStateRef st) { 599 return removeGDM(st, ProgramStateTrait<T>::GDMIndex()); 600 } 601 602 void *FindGDMContext(void *index, 603 void *(*CreateContext)(llvm::BumpPtrAllocator&), 604 void (*DeleteContext)(void*)); 605 606 template <typename T> 607 typename ProgramStateTrait<T>::context_type get_context() { 608 void *p = FindGDMContext(ProgramStateTrait<T>::GDMIndex(), 609 ProgramStateTrait<T>::CreateContext, 610 ProgramStateTrait<T>::DeleteContext); 611 612 return ProgramStateTrait<T>::MakeContext(p); 613 } 614 615 void EndPath(ProgramStateRef St) { 616 ConstraintMgr->EndPath(St); 617 } 618 }; 619 620 621 //===----------------------------------------------------------------------===// 622 // Out-of-line method definitions for ProgramState. 623 //===----------------------------------------------------------------------===// 624 625 inline ConstraintManager &ProgramState::getConstraintManager() const { 626 return stateMgr->getConstraintManager(); 627 } 628 629 inline const VarRegion* ProgramState::getRegion(const VarDecl *D, 630 const LocationContext *LC) const 631 { 632 return getStateManager().getRegionManager().getVarRegion(D, LC); 633 } 634 635 inline ProgramStateRef ProgramState::assume(DefinedOrUnknownSVal Cond, 636 bool Assumption) const { 637 if (Cond.isUnknown()) 638 return this; 639 640 return getStateManager().ConstraintMgr 641 ->assume(this, Cond.castAs<DefinedSVal>(), Assumption); 642 } 643 644 inline std::pair<ProgramStateRef , ProgramStateRef > 645 ProgramState::assume(DefinedOrUnknownSVal Cond) const { 646 if (Cond.isUnknown()) 647 return std::make_pair(this, this); 648 649 return getStateManager().ConstraintMgr 650 ->assumeDual(this, Cond.castAs<DefinedSVal>()); 651 } 652 653 inline ProgramStateRef ProgramState::bindLoc(SVal LV, SVal V) const { 654 if (Optional<Loc> L = LV.getAs<Loc>()) 655 return bindLoc(*L, V); 656 return this; 657 } 658 659 inline Loc ProgramState::getLValue(const VarDecl *VD, 660 const LocationContext *LC) const { 661 return getStateManager().StoreMgr->getLValueVar(VD, LC); 662 } 663 664 inline Loc ProgramState::getLValue(const CompoundLiteralExpr *literal, 665 const LocationContext *LC) const { 666 return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC); 667 } 668 669 inline SVal ProgramState::getLValue(const ObjCIvarDecl *D, SVal Base) const { 670 return getStateManager().StoreMgr->getLValueIvar(D, Base); 671 } 672 673 inline SVal ProgramState::getLValue(const FieldDecl *D, SVal Base) const { 674 return getStateManager().StoreMgr->getLValueField(D, Base); 675 } 676 677 inline SVal ProgramState::getLValue(const IndirectFieldDecl *D, 678 SVal Base) const { 679 StoreManager &SM = *getStateManager().StoreMgr; 680 for (IndirectFieldDecl::chain_iterator I = D->chain_begin(), 681 E = D->chain_end(); 682 I != E; ++I) { 683 Base = SM.getLValueField(cast<FieldDecl>(*I), Base); 684 } 685 686 return Base; 687 } 688 689 inline SVal ProgramState::getLValue(QualType ElementType, SVal Idx, SVal Base) const{ 690 if (Optional<NonLoc> N = Idx.getAs<NonLoc>()) 691 return getStateManager().StoreMgr->getLValueElement(ElementType, *N, Base); 692 return UnknownVal(); 693 } 694 695 inline SVal ProgramState::getSVal(const Stmt *Ex, 696 const LocationContext *LCtx) const{ 697 return Env.getSVal(EnvironmentEntry(Ex, LCtx), 698 *getStateManager().svalBuilder); 699 } 700 701 inline SVal 702 ProgramState::getSValAsScalarOrLoc(const Stmt *S, 703 const LocationContext *LCtx) const { 704 if (const Expr *Ex = dyn_cast<Expr>(S)) { 705 QualType T = Ex->getType(); 706 if (Ex->isGLValue() || Loc::isLocType(T) || 707 T->isIntegralOrEnumerationType()) 708 return getSVal(S, LCtx); 709 } 710 711 return UnknownVal(); 712 } 713 714 inline SVal ProgramState::getRawSVal(Loc LV, QualType T) const { 715 return getStateManager().StoreMgr->getBinding(getStore(), LV, T); 716 } 717 718 inline SVal ProgramState::getSVal(const MemRegion* R) const { 719 return getStateManager().StoreMgr->getBinding(getStore(), 720 loc::MemRegionVal(R)); 721 } 722 723 inline BasicValueFactory &ProgramState::getBasicVals() const { 724 return getStateManager().getBasicVals(); 725 } 726 727 inline SymbolManager &ProgramState::getSymbolManager() const { 728 return getStateManager().getSymbolManager(); 729 } 730 731 template<typename T> 732 ProgramStateRef ProgramState::add(typename ProgramStateTrait<T>::key_type K) const { 733 return getStateManager().add<T>(this, K, get_context<T>()); 734 } 735 736 template <typename T> 737 typename ProgramStateTrait<T>::context_type ProgramState::get_context() const { 738 return getStateManager().get_context<T>(); 739 } 740 741 template<typename T> 742 ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K) const { 743 return getStateManager().remove<T>(this, K, get_context<T>()); 744 } 745 746 template<typename T> 747 ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K, 748 typename ProgramStateTrait<T>::context_type C) const { 749 return getStateManager().remove<T>(this, K, C); 750 } 751 752 template <typename T> 753 ProgramStateRef ProgramState::remove() const { 754 return getStateManager().remove<T>(this); 755 } 756 757 template<typename T> 758 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::data_type D) const { 759 return getStateManager().set<T>(this, D); 760 } 761 762 template<typename T> 763 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K, 764 typename ProgramStateTrait<T>::value_type E) const { 765 return getStateManager().set<T>(this, K, E, get_context<T>()); 766 } 767 768 template<typename T> 769 ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K, 770 typename ProgramStateTrait<T>::value_type E, 771 typename ProgramStateTrait<T>::context_type C) const { 772 return getStateManager().set<T>(this, K, E, C); 773 } 774 775 template <typename CB> 776 CB ProgramState::scanReachableSymbols(SVal val) const { 777 CB cb(this); 778 scanReachableSymbols(val, cb); 779 return cb; 780 } 781 782 template <typename CB> 783 CB ProgramState::scanReachableSymbols(const SVal *beg, const SVal *end) const { 784 CB cb(this); 785 scanReachableSymbols(beg, end, cb); 786 return cb; 787 } 788 789 template <typename CB> 790 CB ProgramState::scanReachableSymbols(const MemRegion * const *beg, 791 const MemRegion * const *end) const { 792 CB cb(this); 793 scanReachableSymbols(beg, end, cb); 794 return cb; 795 } 796 797 /// \class ScanReachableSymbols 798 /// A Utility class that allows to visit the reachable symbols using a custom 799 /// SymbolVisitor. 800 class ScanReachableSymbols { 801 typedef llvm::DenseMap<const void*, unsigned> VisitedItems; 802 803 VisitedItems visited; 804 ProgramStateRef state; 805 SymbolVisitor &visitor; 806 public: 807 808 ScanReachableSymbols(ProgramStateRef st, SymbolVisitor& v) 809 : state(st), visitor(v) {} 810 811 bool scan(nonloc::CompoundVal val); 812 bool scan(SVal val); 813 bool scan(const MemRegion *R); 814 bool scan(const SymExpr *sym); 815 }; 816 817 } // end ento namespace 818 819 } // end clang namespace 820 821 #endif 822