RegionStore.cpp revision deb8f5d533b7bcd962976ecdbc1464fe754b6de0
1//== RegionStore.cpp - Field-sensitive store model --------------*- 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 a basic region store model. In this model, we do have field 11// sensitivity. But we assume nothing about the heap shape. So recursive data 12// structures are largely ignored. Basically we do 1-limiting analysis. 13// Parameter pointers are assumed with no aliasing. Pointee objects of 14// parameters are created lazily. 15// 16//===----------------------------------------------------------------------===// 17#include "clang/AST/Attr.h" 18#include "clang/AST/CharUnits.h" 19#include "clang/Analysis/Analyses/LiveVariables.h" 20#include "clang/Analysis/AnalysisContext.h" 21#include "clang/Basic/TargetInfo.h" 22#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 23#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 24#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 25#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" 26#include "llvm/ADT/ImmutableList.h" 27#include "llvm/ADT/ImmutableMap.h" 28#include "llvm/ADT/Optional.h" 29#include "llvm/Support/raw_ostream.h" 30 31using namespace clang; 32using namespace ento; 33 34//===----------------------------------------------------------------------===// 35// Representation of binding keys. 36//===----------------------------------------------------------------------===// 37 38namespace { 39class BindingKey { 40public: 41 enum Kind { Default = 0x0, Direct = 0x1 }; 42private: 43 enum { Symbolic = 0x2 }; 44 45 llvm::PointerIntPair<const MemRegion *, 2> P; 46 uint64_t Data; 47 48 /// Create a key for a binding to region \p r, which has a symbolic offset 49 /// from region \p Base. 50 explicit BindingKey(const SubRegion *r, const SubRegion *Base, Kind k) 51 : P(r, k | Symbolic), Data(reinterpret_cast<uintptr_t>(Base)) { 52 assert(r && Base && "Must have known regions."); 53 assert(getConcreteOffsetRegion() == Base && "Failed to store base region"); 54 } 55 56 /// Create a key for a binding at \p offset from base region \p r. 57 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k) 58 : P(r, k), Data(offset) { 59 assert(r && "Must have known regions."); 60 assert(getOffset() == offset && "Failed to store offset"); 61 assert((r == r->getBaseRegion() || isa<ObjCIvarRegion>(r)) && "Not a base"); 62 } 63public: 64 65 bool isDirect() const { return P.getInt() & Direct; } 66 bool hasSymbolicOffset() const { return P.getInt() & Symbolic; } 67 68 const MemRegion *getRegion() const { return P.getPointer(); } 69 uint64_t getOffset() const { 70 assert(!hasSymbolicOffset()); 71 return Data; 72 } 73 74 const SubRegion *getConcreteOffsetRegion() const { 75 assert(hasSymbolicOffset()); 76 return reinterpret_cast<const SubRegion *>(static_cast<uintptr_t>(Data)); 77 } 78 79 const MemRegion *getBaseRegion() const { 80 if (hasSymbolicOffset()) 81 return getConcreteOffsetRegion()->getBaseRegion(); 82 return getRegion()->getBaseRegion(); 83 } 84 85 void Profile(llvm::FoldingSetNodeID& ID) const { 86 ID.AddPointer(P.getOpaqueValue()); 87 ID.AddInteger(Data); 88 } 89 90 static BindingKey Make(const MemRegion *R, Kind k); 91 92 bool operator<(const BindingKey &X) const { 93 if (P.getOpaqueValue() < X.P.getOpaqueValue()) 94 return true; 95 if (P.getOpaqueValue() > X.P.getOpaqueValue()) 96 return false; 97 return Data < X.Data; 98 } 99 100 bool operator==(const BindingKey &X) const { 101 return P.getOpaqueValue() == X.P.getOpaqueValue() && 102 Data == X.Data; 103 } 104 105 LLVM_ATTRIBUTE_USED void dump() const; 106}; 107} // end anonymous namespace 108 109BindingKey BindingKey::Make(const MemRegion *R, Kind k) { 110 const RegionOffset &RO = R->getAsOffset(); 111 if (RO.hasSymbolicOffset()) 112 return BindingKey(cast<SubRegion>(R), cast<SubRegion>(RO.getRegion()), k); 113 114 return BindingKey(RO.getRegion(), RO.getOffset(), k); 115} 116 117namespace llvm { 118 static inline 119 raw_ostream &operator<<(raw_ostream &os, BindingKey K) { 120 os << '(' << K.getRegion(); 121 if (!K.hasSymbolicOffset()) 122 os << ',' << K.getOffset(); 123 os << ',' << (K.isDirect() ? "direct" : "default") 124 << ')'; 125 return os; 126 } 127} // end llvm namespace 128 129void BindingKey::dump() const { 130 llvm::errs() << *this; 131} 132 133//===----------------------------------------------------------------------===// 134// Actual Store type. 135//===----------------------------------------------------------------------===// 136 137typedef llvm::ImmutableMap<BindingKey, SVal> ClusterBindings; 138typedef llvm::ImmutableMapRef<BindingKey, SVal> ClusterBindingsRef; 139 140typedef llvm::ImmutableMap<const MemRegion *, ClusterBindings> 141 RegionBindings; 142 143namespace { 144class RegionBindingsRef : public llvm::ImmutableMapRef<const MemRegion *, 145 ClusterBindings> { 146 ClusterBindings::Factory &CBFactory; 147public: 148 typedef llvm::ImmutableMapRef<const MemRegion *, ClusterBindings> 149 ParentTy; 150 151 RegionBindingsRef(ClusterBindings::Factory &CBFactory, 152 const RegionBindings::TreeTy *T, 153 RegionBindings::TreeTy::Factory *F) 154 : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(T, F), 155 CBFactory(CBFactory) {} 156 157 RegionBindingsRef(const ParentTy &P, ClusterBindings::Factory &CBFactory) 158 : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(P), 159 CBFactory(CBFactory) {} 160 161 RegionBindingsRef add(key_type_ref K, data_type_ref D) const { 162 return RegionBindingsRef(static_cast<const ParentTy*>(this)->add(K, D), 163 CBFactory); 164 } 165 166 RegionBindingsRef remove(key_type_ref K) const { 167 return RegionBindingsRef(static_cast<const ParentTy*>(this)->remove(K), 168 CBFactory); 169 } 170 171 RegionBindingsRef addBinding(BindingKey K, SVal V) const; 172 173 RegionBindingsRef addBinding(const MemRegion *R, 174 BindingKey::Kind k, SVal V) const; 175 176 RegionBindingsRef &operator=(const RegionBindingsRef &X) { 177 *static_cast<ParentTy*>(this) = X; 178 return *this; 179 } 180 181 const SVal *lookup(BindingKey K) const; 182 const SVal *lookup(const MemRegion *R, BindingKey::Kind k) const; 183 const ClusterBindings *lookup(const MemRegion *R) const { 184 return static_cast<const ParentTy*>(this)->lookup(R); 185 } 186 187 RegionBindingsRef removeBinding(BindingKey K); 188 189 RegionBindingsRef removeBinding(const MemRegion *R, 190 BindingKey::Kind k); 191 192 RegionBindingsRef removeBinding(const MemRegion *R) { 193 return removeBinding(R, BindingKey::Direct). 194 removeBinding(R, BindingKey::Default); 195 } 196 197 Optional<SVal> getDirectBinding(const MemRegion *R) const; 198 199 /// getDefaultBinding - Returns an SVal* representing an optional default 200 /// binding associated with a region and its subregions. 201 Optional<SVal> getDefaultBinding(const MemRegion *R) const; 202 203 /// Return the internal tree as a Store. 204 Store asStore() const { 205 return asImmutableMap().getRootWithoutRetain(); 206 } 207 208 void dump(raw_ostream &OS, const char *nl) const { 209 for (iterator I = begin(), E = end(); I != E; ++I) { 210 const ClusterBindings &Cluster = I.getData(); 211 for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 212 CI != CE; ++CI) { 213 OS << ' ' << CI.getKey() << " : " << CI.getData() << nl; 214 } 215 OS << nl; 216 } 217 } 218 219 LLVM_ATTRIBUTE_USED void dump() const { 220 dump(llvm::errs(), "\n"); 221 } 222}; 223} // end anonymous namespace 224 225typedef const RegionBindingsRef& RegionBindingsConstRef; 226 227Optional<SVal> RegionBindingsRef::getDirectBinding(const MemRegion *R) const { 228 return Optional<SVal>::create(lookup(R, BindingKey::Direct)); 229} 230 231Optional<SVal> RegionBindingsRef::getDefaultBinding(const MemRegion *R) const { 232 if (R->isBoundable()) 233 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) 234 if (TR->getValueType()->isUnionType()) 235 return UnknownVal(); 236 237 return Optional<SVal>::create(lookup(R, BindingKey::Default)); 238} 239 240RegionBindingsRef RegionBindingsRef::addBinding(BindingKey K, SVal V) const { 241 const MemRegion *Base = K.getBaseRegion(); 242 243 const ClusterBindings *ExistingCluster = lookup(Base); 244 ClusterBindings Cluster = (ExistingCluster ? *ExistingCluster 245 : CBFactory.getEmptyMap()); 246 247 ClusterBindings NewCluster = CBFactory.add(Cluster, K, V); 248 return add(Base, NewCluster); 249} 250 251 252RegionBindingsRef RegionBindingsRef::addBinding(const MemRegion *R, 253 BindingKey::Kind k, 254 SVal V) const { 255 return addBinding(BindingKey::Make(R, k), V); 256} 257 258const SVal *RegionBindingsRef::lookup(BindingKey K) const { 259 const ClusterBindings *Cluster = lookup(K.getBaseRegion()); 260 if (!Cluster) 261 return 0; 262 return Cluster->lookup(K); 263} 264 265const SVal *RegionBindingsRef::lookup(const MemRegion *R, 266 BindingKey::Kind k) const { 267 return lookup(BindingKey::Make(R, k)); 268} 269 270RegionBindingsRef RegionBindingsRef::removeBinding(BindingKey K) { 271 const MemRegion *Base = K.getBaseRegion(); 272 const ClusterBindings *Cluster = lookup(Base); 273 if (!Cluster) 274 return *this; 275 276 ClusterBindings NewCluster = CBFactory.remove(*Cluster, K); 277 if (NewCluster.isEmpty()) 278 return remove(Base); 279 return add(Base, NewCluster); 280} 281 282RegionBindingsRef RegionBindingsRef::removeBinding(const MemRegion *R, 283 BindingKey::Kind k){ 284 return removeBinding(BindingKey::Make(R, k)); 285} 286 287//===----------------------------------------------------------------------===// 288// Fine-grained control of RegionStoreManager. 289//===----------------------------------------------------------------------===// 290 291namespace { 292struct minimal_features_tag {}; 293struct maximal_features_tag {}; 294 295class RegionStoreFeatures { 296 bool SupportsFields; 297public: 298 RegionStoreFeatures(minimal_features_tag) : 299 SupportsFields(false) {} 300 301 RegionStoreFeatures(maximal_features_tag) : 302 SupportsFields(true) {} 303 304 void enableFields(bool t) { SupportsFields = t; } 305 306 bool supportsFields() const { return SupportsFields; } 307}; 308} 309 310//===----------------------------------------------------------------------===// 311// Main RegionStore logic. 312//===----------------------------------------------------------------------===// 313 314namespace { 315 316class RegionStoreManager : public StoreManager { 317public: 318 const RegionStoreFeatures Features; 319 RegionBindings::Factory RBFactory; 320 mutable ClusterBindings::Factory CBFactory; 321 322 typedef std::vector<SVal> SValListTy; 323private: 324 typedef llvm::DenseMap<const LazyCompoundValData *, 325 SValListTy> LazyBindingsMapTy; 326 LazyBindingsMapTy LazyBindingsMap; 327 328public: 329 RegionStoreManager(ProgramStateManager& mgr, const RegionStoreFeatures &f) 330 : StoreManager(mgr), Features(f), 331 RBFactory(mgr.getAllocator()), CBFactory(mgr.getAllocator()) {} 332 333 334 /// setImplicitDefaultValue - Set the default binding for the provided 335 /// MemRegion to the value implicitly defined for compound literals when 336 /// the value is not specified. 337 RegionBindingsRef setImplicitDefaultValue(RegionBindingsConstRef B, 338 const MemRegion *R, QualType T); 339 340 /// ArrayToPointer - Emulates the "decay" of an array to a pointer 341 /// type. 'Array' represents the lvalue of the array being decayed 342 /// to a pointer, and the returned SVal represents the decayed 343 /// version of that lvalue (i.e., a pointer to the first element of 344 /// the array). This is called by ExprEngine when evaluating 345 /// casts from arrays to pointers. 346 SVal ArrayToPointer(Loc Array); 347 348 StoreRef getInitialStore(const LocationContext *InitLoc) { 349 return StoreRef(RBFactory.getEmptyMap().getRootWithoutRetain(), *this); 350 } 351 352 //===-------------------------------------------------------------------===// 353 // Binding values to regions. 354 //===-------------------------------------------------------------------===// 355 RegionBindingsRef invalidateGlobalRegion(MemRegion::Kind K, 356 const Expr *Ex, 357 unsigned Count, 358 const LocationContext *LCtx, 359 RegionBindingsRef B, 360 InvalidatedRegions *Invalidated); 361 362 StoreRef invalidateRegions(Store store, ArrayRef<const MemRegion *> Regions, 363 const Expr *E, unsigned Count, 364 const LocationContext *LCtx, 365 InvalidatedSymbols &IS, 366 const CallEvent *Call, 367 InvalidatedRegions *Invalidated); 368 369 bool scanReachableSymbols(Store S, const MemRegion *R, 370 ScanReachableSymbols &Callbacks); 371 372 RegionBindingsRef removeSubRegionBindings(RegionBindingsConstRef B, 373 const SubRegion *R); 374 375public: // Part of public interface to class. 376 377 virtual StoreRef Bind(Store store, Loc LV, SVal V) { 378 return StoreRef(bind(getRegionBindings(store), LV, V).asStore(), *this); 379 } 380 381 RegionBindingsRef bind(RegionBindingsConstRef B, Loc LV, SVal V); 382 383 // BindDefault is only used to initialize a region with a default value. 384 StoreRef BindDefault(Store store, const MemRegion *R, SVal V) { 385 RegionBindingsRef B = getRegionBindings(store); 386 assert(!B.lookup(R, BindingKey::Default)); 387 assert(!B.lookup(R, BindingKey::Direct)); 388 return StoreRef(B.addBinding(R, BindingKey::Default, V) 389 .asImmutableMap() 390 .getRootWithoutRetain(), *this); 391 } 392 393 /// \brief Create a new store that binds a value to a compound literal. 394 /// 395 /// \param ST The original store whose bindings are the basis for the new 396 /// store. 397 /// 398 /// \param CL The compound literal to bind (the binding key). 399 /// 400 /// \param LC The LocationContext for the binding. 401 /// 402 /// \param V The value to bind to the compound literal. 403 StoreRef bindCompoundLiteral(Store ST, 404 const CompoundLiteralExpr *CL, 405 const LocationContext *LC, SVal V); 406 407 /// BindStruct - Bind a compound value to a structure. 408 RegionBindingsRef bindStruct(RegionBindingsConstRef B, 409 const TypedValueRegion* R, SVal V); 410 411 /// BindVector - Bind a compound value to a vector. 412 RegionBindingsRef bindVector(RegionBindingsConstRef B, 413 const TypedValueRegion* R, SVal V); 414 415 RegionBindingsRef bindArray(RegionBindingsConstRef B, 416 const TypedValueRegion* R, 417 SVal V); 418 419 /// Clears out all bindings in the given region and assigns a new value 420 /// as a Default binding. 421 RegionBindingsRef bindAggregate(RegionBindingsConstRef B, 422 const TypedRegion *R, 423 SVal DefaultVal); 424 425 /// \brief Create a new store with the specified binding removed. 426 /// \param ST the original store, that is the basis for the new store. 427 /// \param L the location whose binding should be removed. 428 virtual StoreRef killBinding(Store ST, Loc L); 429 430 void incrementReferenceCount(Store store) { 431 getRegionBindings(store).manualRetain(); 432 } 433 434 /// If the StoreManager supports it, decrement the reference count of 435 /// the specified Store object. If the reference count hits 0, the memory 436 /// associated with the object is recycled. 437 void decrementReferenceCount(Store store) { 438 getRegionBindings(store).manualRelease(); 439 } 440 441 bool includedInBindings(Store store, const MemRegion *region) const; 442 443 /// \brief Return the value bound to specified location in a given state. 444 /// 445 /// The high level logic for this method is this: 446 /// getBinding (L) 447 /// if L has binding 448 /// return L's binding 449 /// else if L is in killset 450 /// return unknown 451 /// else 452 /// if L is on stack or heap 453 /// return undefined 454 /// else 455 /// return symbolic 456 virtual SVal getBinding(Store S, Loc L, QualType T) { 457 return getBinding(getRegionBindings(S), L, T); 458 } 459 460 SVal getBinding(RegionBindingsConstRef B, Loc L, QualType T = QualType()); 461 462 SVal getBindingForElement(RegionBindingsConstRef B, const ElementRegion *R); 463 464 SVal getBindingForField(RegionBindingsConstRef B, const FieldRegion *R); 465 466 SVal getBindingForObjCIvar(RegionBindingsConstRef B, const ObjCIvarRegion *R); 467 468 SVal getBindingForVar(RegionBindingsConstRef B, const VarRegion *R); 469 470 SVal getBindingForLazySymbol(const TypedValueRegion *R); 471 472 SVal getBindingForFieldOrElementCommon(RegionBindingsConstRef B, 473 const TypedValueRegion *R, 474 QualType Ty, 475 const MemRegion *superR); 476 477 SVal getLazyBinding(const SubRegion *LazyBindingRegion, 478 RegionBindingsRef LazyBinding); 479 480 /// Get bindings for the values in a struct and return a CompoundVal, used 481 /// when doing struct copy: 482 /// struct s x, y; 483 /// x = y; 484 /// y's value is retrieved by this method. 485 SVal getBindingForStruct(RegionBindingsConstRef B, const TypedValueRegion *R); 486 SVal getBindingForArray(RegionBindingsConstRef B, const TypedValueRegion *R); 487 NonLoc createLazyBinding(RegionBindingsConstRef B, const TypedValueRegion *R); 488 489 /// Used to lazily generate derived symbols for bindings that are defined 490 /// implicitly by default bindings in a super region. 491 /// 492 /// Note that callers may need to specially handle LazyCompoundVals, which 493 /// are returned as is in case the caller needs to treat them differently. 494 Optional<SVal> getBindingForDerivedDefaultValue(RegionBindingsConstRef B, 495 const MemRegion *superR, 496 const TypedValueRegion *R, 497 QualType Ty); 498 499 /// Get the state and region whose binding this region \p R corresponds to. 500 /// 501 /// If there is no lazy binding for \p R, the returned value will have a null 502 /// \c second. Note that a null pointer can represents a valid Store. 503 std::pair<Store, const SubRegion *> 504 findLazyBinding(RegionBindingsConstRef B, const SubRegion *R, 505 const SubRegion *originalRegion); 506 507 /// Returns the cached set of interesting SVals contained within a lazy 508 /// binding. 509 /// 510 /// The precise value of "interesting" is determined for the purposes of 511 /// RegionStore's internal analysis. It must always contain all regions and 512 /// symbols, but may omit constants and other kinds of SVal. 513 const SValListTy &getInterestingValues(nonloc::LazyCompoundVal LCV); 514 515 //===------------------------------------------------------------------===// 516 // State pruning. 517 //===------------------------------------------------------------------===// 518 519 /// removeDeadBindings - Scans the RegionStore of 'state' for dead values. 520 /// It returns a new Store with these values removed. 521 StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx, 522 SymbolReaper& SymReaper); 523 524 //===------------------------------------------------------------------===// 525 // Region "extents". 526 //===------------------------------------------------------------------===// 527 528 // FIXME: This method will soon be eliminated; see the note in Store.h. 529 DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state, 530 const MemRegion* R, QualType EleTy); 531 532 //===------------------------------------------------------------------===// 533 // Utility methods. 534 //===------------------------------------------------------------------===// 535 536 RegionBindingsRef getRegionBindings(Store store) const { 537 return RegionBindingsRef(CBFactory, 538 static_cast<const RegionBindings::TreeTy*>(store), 539 RBFactory.getTreeFactory()); 540 } 541 542 void print(Store store, raw_ostream &Out, const char* nl, 543 const char *sep); 544 545 void iterBindings(Store store, BindingsHandler& f) { 546 RegionBindingsRef B = getRegionBindings(store); 547 for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) { 548 const ClusterBindings &Cluster = I.getData(); 549 for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 550 CI != CE; ++CI) { 551 const BindingKey &K = CI.getKey(); 552 if (!K.isDirect()) 553 continue; 554 if (const SubRegion *R = dyn_cast<SubRegion>(K.getRegion())) { 555 // FIXME: Possibly incorporate the offset? 556 if (!f.HandleBinding(*this, store, R, CI.getData())) 557 return; 558 } 559 } 560 } 561 } 562}; 563 564} // end anonymous namespace 565 566//===----------------------------------------------------------------------===// 567// RegionStore creation. 568//===----------------------------------------------------------------------===// 569 570StoreManager *ento::CreateRegionStoreManager(ProgramStateManager& StMgr) { 571 RegionStoreFeatures F = maximal_features_tag(); 572 return new RegionStoreManager(StMgr, F); 573} 574 575StoreManager * 576ento::CreateFieldsOnlyRegionStoreManager(ProgramStateManager &StMgr) { 577 RegionStoreFeatures F = minimal_features_tag(); 578 F.enableFields(true); 579 return new RegionStoreManager(StMgr, F); 580} 581 582 583//===----------------------------------------------------------------------===// 584// Region Cluster analysis. 585//===----------------------------------------------------------------------===// 586 587namespace { 588template <typename DERIVED> 589class ClusterAnalysis { 590protected: 591 typedef llvm::DenseMap<const MemRegion *, const ClusterBindings *> ClusterMap; 592 typedef SmallVector<const MemRegion *, 10> WorkList; 593 594 llvm::SmallPtrSet<const ClusterBindings *, 16> Visited; 595 596 WorkList WL; 597 598 RegionStoreManager &RM; 599 ASTContext &Ctx; 600 SValBuilder &svalBuilder; 601 602 RegionBindingsRef B; 603 604 const bool includeGlobals; 605 606 const ClusterBindings *getCluster(const MemRegion *R) { 607 return B.lookup(R); 608 } 609 610public: 611 ClusterAnalysis(RegionStoreManager &rm, ProgramStateManager &StateMgr, 612 RegionBindingsRef b, const bool includeGlobals) 613 : RM(rm), Ctx(StateMgr.getContext()), 614 svalBuilder(StateMgr.getSValBuilder()), 615 B(b), includeGlobals(includeGlobals) {} 616 617 RegionBindingsRef getRegionBindings() const { return B; } 618 619 bool isVisited(const MemRegion *R) { 620 return Visited.count(getCluster(R)); 621 } 622 623 void GenerateClusters() { 624 // Scan the entire set of bindings and record the region clusters. 625 for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); 626 RI != RE; ++RI){ 627 const MemRegion *Base = RI.getKey(); 628 629 const ClusterBindings &Cluster = RI.getData(); 630 assert(!Cluster.isEmpty() && "Empty clusters should be removed"); 631 static_cast<DERIVED*>(this)->VisitAddedToCluster(Base, Cluster); 632 633 if (includeGlobals) 634 if (isa<NonStaticGlobalSpaceRegion>(Base->getMemorySpace())) 635 AddToWorkList(Base, &Cluster); 636 } 637 } 638 639 bool AddToWorkList(const MemRegion *R, const ClusterBindings *C) { 640 if (C && !Visited.insert(C)) 641 return false; 642 WL.push_back(R); 643 return true; 644 } 645 646 bool AddToWorkList(const MemRegion *R) { 647 const MemRegion *baseR = R->getBaseRegion(); 648 return AddToWorkList(baseR, getCluster(baseR)); 649 } 650 651 void RunWorkList() { 652 while (!WL.empty()) { 653 const MemRegion *baseR = WL.pop_back_val(); 654 655 // First visit the cluster. 656 if (const ClusterBindings *Cluster = getCluster(baseR)) 657 static_cast<DERIVED*>(this)->VisitCluster(baseR, *Cluster); 658 659 // Next, visit the base region. 660 static_cast<DERIVED*>(this)->VisitBaseRegion(baseR); 661 } 662 } 663 664public: 665 void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C) {} 666 void VisitCluster(const MemRegion *baseR, const ClusterBindings &C) {} 667 void VisitBaseRegion(const MemRegion *baseR) {} 668}; 669} 670 671//===----------------------------------------------------------------------===// 672// Binding invalidation. 673//===----------------------------------------------------------------------===// 674 675bool RegionStoreManager::scanReachableSymbols(Store S, const MemRegion *R, 676 ScanReachableSymbols &Callbacks) { 677 assert(R == R->getBaseRegion() && "Should only be called for base regions"); 678 RegionBindingsRef B = getRegionBindings(S); 679 const ClusterBindings *Cluster = B.lookup(R); 680 681 if (!Cluster) 682 return true; 683 684 for (ClusterBindings::iterator RI = Cluster->begin(), RE = Cluster->end(); 685 RI != RE; ++RI) { 686 if (!Callbacks.scan(RI.getData())) 687 return false; 688 } 689 690 return true; 691} 692 693static inline bool isUnionField(const FieldRegion *FR) { 694 return FR->getDecl()->getParent()->isUnion(); 695} 696 697typedef SmallVector<const FieldDecl *, 8> FieldVector; 698 699void getSymbolicOffsetFields(BindingKey K, FieldVector &Fields) { 700 assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys"); 701 702 const MemRegion *Base = K.getConcreteOffsetRegion(); 703 const MemRegion *R = K.getRegion(); 704 705 while (R != Base) { 706 if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) 707 if (!isUnionField(FR)) 708 Fields.push_back(FR->getDecl()); 709 710 R = cast<SubRegion>(R)->getSuperRegion(); 711 } 712} 713 714static bool isCompatibleWithFields(BindingKey K, const FieldVector &Fields) { 715 assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys"); 716 717 if (Fields.empty()) 718 return true; 719 720 FieldVector FieldsInBindingKey; 721 getSymbolicOffsetFields(K, FieldsInBindingKey); 722 723 ptrdiff_t Delta = FieldsInBindingKey.size() - Fields.size(); 724 if (Delta >= 0) 725 return std::equal(FieldsInBindingKey.begin() + Delta, 726 FieldsInBindingKey.end(), 727 Fields.begin()); 728 else 729 return std::equal(FieldsInBindingKey.begin(), FieldsInBindingKey.end(), 730 Fields.begin() - Delta); 731} 732 733/// Collects all keys in \p Cluster that may refer to bindings within \p Top. 734/// 735/// The \p IncludeAllDefaultBindings parameter specifies whether to include 736/// default bindings that may extend beyond \p Top itself, e.g. if \p Top is 737/// an aggregate within a larger aggregate with a default binding. 738static void collectSubRegionKeys(SmallVectorImpl<BindingKey> &Keys, 739 SValBuilder &SVB, 740 const ClusterBindings &Cluster, 741 const SubRegion *Top, BindingKey TopKey, 742 bool IncludeAllDefaultBindings) { 743 FieldVector FieldsInSymbolicSubregions; 744 if (TopKey.hasSymbolicOffset()) { 745 getSymbolicOffsetFields(TopKey, FieldsInSymbolicSubregions); 746 Top = cast<SubRegion>(TopKey.getConcreteOffsetRegion()); 747 TopKey = BindingKey::Make(Top, BindingKey::Default); 748 } 749 750 // This assumes the region being invalidated is char-aligned. This isn't 751 // true for bitfields, but since bitfields have no subregions they shouldn't 752 // be using this function anyway. 753 uint64_t Length = UINT64_MAX; 754 SVal Extent = Top->getExtent(SVB); 755 if (Optional<nonloc::ConcreteInt> ExtentCI = 756 Extent.getAs<nonloc::ConcreteInt>()) { 757 const llvm::APSInt &ExtentInt = ExtentCI->getValue(); 758 assert(ExtentInt.isNonNegative() || ExtentInt.isUnsigned()); 759 // Extents are in bytes but region offsets are in bits. Be careful! 760 Length = ExtentInt.getLimitedValue() * SVB.getContext().getCharWidth(); 761 } 762 763 for (ClusterBindings::iterator I = Cluster.begin(), E = Cluster.end(); 764 I != E; ++I) { 765 BindingKey NextKey = I.getKey(); 766 if (NextKey.getRegion() == TopKey.getRegion()) { 767 // FIXME: This doesn't catch the case where we're really invalidating a 768 // region with a symbolic offset. Example: 769 // R: points[i].y 770 // Next: points[0].x 771 772 if (NextKey.getOffset() > TopKey.getOffset() && 773 NextKey.getOffset() - TopKey.getOffset() < Length) { 774 // Case 1: The next binding is inside the region we're invalidating. 775 // Remove it. 776 Keys.push_back(NextKey); 777 778 } else if (NextKey.getOffset() == TopKey.getOffset()) { 779 // Case 2: The next binding is at the same offset as the region we're 780 // invalidating. In this case, we need to leave default bindings alone, 781 // since they may be providing a default value for a regions beyond what 782 // we're invalidating. 783 // FIXME: This is probably incorrect; consider invalidating an outer 784 // struct whose first field is bound to a LazyCompoundVal. 785 if (IncludeAllDefaultBindings || NextKey.isDirect()) 786 Keys.push_back(NextKey); 787 } 788 789 } else if (NextKey.hasSymbolicOffset()) { 790 const MemRegion *Base = NextKey.getConcreteOffsetRegion(); 791 if (Top->isSubRegionOf(Base)) { 792 // Case 3: The next key is symbolic and we just changed something within 793 // its concrete region. We don't know if the binding is still valid, so 794 // we'll be conservative and remove it. 795 if (IncludeAllDefaultBindings || NextKey.isDirect()) 796 if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions)) 797 Keys.push_back(NextKey); 798 } else if (const SubRegion *BaseSR = dyn_cast<SubRegion>(Base)) { 799 // Case 4: The next key is symbolic, but we changed a known 800 // super-region. In this case the binding is certainly no longer valid. 801 if (Top == Base || BaseSR->isSubRegionOf(Top)) 802 if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions)) 803 Keys.push_back(NextKey); 804 } 805 } 806 } 807} 808 809static void collectSubRegionKeys(SmallVectorImpl<BindingKey> &Keys, 810 SValBuilder &SVB, 811 const ClusterBindings &Cluster, 812 const SubRegion *Top, 813 bool IncludeAllDefaultBindings) { 814 collectSubRegionKeys(Keys, SVB, Cluster, Top, 815 BindingKey::Make(Top, BindingKey::Default), 816 IncludeAllDefaultBindings); 817} 818 819RegionBindingsRef 820RegionStoreManager::removeSubRegionBindings(RegionBindingsConstRef B, 821 const SubRegion *Top) { 822 BindingKey TopKey = BindingKey::Make(Top, BindingKey::Default); 823 const MemRegion *ClusterHead = TopKey.getBaseRegion(); 824 if (Top == ClusterHead) { 825 // We can remove an entire cluster's bindings all in one go. 826 return B.remove(Top); 827 } 828 829 const ClusterBindings *Cluster = B.lookup(ClusterHead); 830 if (!Cluster) 831 return B; 832 833 SmallVector<BindingKey, 32> Keys; 834 collectSubRegionKeys(Keys, svalBuilder, *Cluster, Top, TopKey, 835 /*IncludeAllDefaultBindings=*/false); 836 837 ClusterBindingsRef Result(*Cluster, CBFactory); 838 for (SmallVectorImpl<BindingKey>::const_iterator I = Keys.begin(), 839 E = Keys.end(); 840 I != E; ++I) 841 Result = Result.remove(*I); 842 843 // If we're invalidating a region with a symbolic offset, we need to make sure 844 // we don't treat the base region as uninitialized anymore. 845 // FIXME: This isn't very precise; see the example in the loop. 846 if (TopKey.hasSymbolicOffset()) { 847 const SubRegion *Concrete = TopKey.getConcreteOffsetRegion(); 848 Result = Result.add(BindingKey::Make(Concrete, BindingKey::Default), 849 UnknownVal()); 850 } 851 852 if (Result.isEmpty()) 853 return B.remove(ClusterHead); 854 return B.add(ClusterHead, Result.asImmutableMap()); 855} 856 857namespace { 858class invalidateRegionsWorker : public ClusterAnalysis<invalidateRegionsWorker> 859{ 860 const Expr *Ex; 861 unsigned Count; 862 const LocationContext *LCtx; 863 InvalidatedSymbols &IS; 864 StoreManager::InvalidatedRegions *Regions; 865public: 866 invalidateRegionsWorker(RegionStoreManager &rm, 867 ProgramStateManager &stateMgr, 868 RegionBindingsRef b, 869 const Expr *ex, unsigned count, 870 const LocationContext *lctx, 871 InvalidatedSymbols &is, 872 StoreManager::InvalidatedRegions *r, 873 bool includeGlobals) 874 : ClusterAnalysis<invalidateRegionsWorker>(rm, stateMgr, b, includeGlobals), 875 Ex(ex), Count(count), LCtx(lctx), IS(is), Regions(r) {} 876 877 void VisitCluster(const MemRegion *baseR, const ClusterBindings &C); 878 void VisitBaseRegion(const MemRegion *baseR); 879 880private: 881 void VisitBinding(SVal V); 882}; 883} 884 885void invalidateRegionsWorker::VisitBinding(SVal V) { 886 // A symbol? Mark it touched by the invalidation. 887 if (SymbolRef Sym = V.getAsSymbol()) 888 IS.insert(Sym); 889 890 if (const MemRegion *R = V.getAsRegion()) { 891 AddToWorkList(R); 892 return; 893 } 894 895 // Is it a LazyCompoundVal? All references get invalidated as well. 896 if (Optional<nonloc::LazyCompoundVal> LCS = 897 V.getAs<nonloc::LazyCompoundVal>()) { 898 899 const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS); 900 901 for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(), 902 E = Vals.end(); 903 I != E; ++I) 904 VisitBinding(*I); 905 906 return; 907 } 908} 909 910void invalidateRegionsWorker::VisitCluster(const MemRegion *BaseR, 911 const ClusterBindings &C) { 912 for (ClusterBindings::iterator I = C.begin(), E = C.end(); I != E; ++I) 913 VisitBinding(I.getData()); 914 915 B = B.remove(BaseR); 916} 917 918void invalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) { 919 // Symbolic region? Mark that symbol touched by the invalidation. 920 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) 921 IS.insert(SR->getSymbol()); 922 923 // BlockDataRegion? If so, invalidate captured variables that are passed 924 // by reference. 925 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) { 926 for (BlockDataRegion::referenced_vars_iterator 927 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ; 928 BI != BE; ++BI) { 929 const VarRegion *VR = BI.getCapturedRegion(); 930 const VarDecl *VD = VR->getDecl(); 931 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage()) { 932 AddToWorkList(VR); 933 } 934 else if (Loc::isLocType(VR->getValueType())) { 935 // Map the current bindings to a Store to retrieve the value 936 // of the binding. If that binding itself is a region, we should 937 // invalidate that region. This is because a block may capture 938 // a pointer value, but the thing pointed by that pointer may 939 // get invalidated. 940 SVal V = RM.getBinding(B, loc::MemRegionVal(VR)); 941 if (Optional<Loc> L = V.getAs<Loc>()) { 942 if (const MemRegion *LR = L->getAsRegion()) 943 AddToWorkList(LR); 944 } 945 } 946 } 947 return; 948 } 949 950 // Otherwise, we have a normal data region. Record that we touched the region. 951 if (Regions) 952 Regions->push_back(baseR); 953 954 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) { 955 // Invalidate the region by setting its default value to 956 // conjured symbol. The type of the symbol is irrelavant. 957 DefinedOrUnknownSVal V = 958 svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, Ctx.IntTy, Count); 959 B = B.addBinding(baseR, BindingKey::Default, V); 960 return; 961 } 962 963 if (!baseR->isBoundable()) 964 return; 965 966 const TypedValueRegion *TR = cast<TypedValueRegion>(baseR); 967 QualType T = TR->getValueType(); 968 969 // Invalidate the binding. 970 if (T->isStructureOrClassType()) { 971 // Invalidate the region by setting its default value to 972 // conjured symbol. The type of the symbol is irrelavant. 973 DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, 974 Ctx.IntTy, Count); 975 B = B.addBinding(baseR, BindingKey::Default, V); 976 return; 977 } 978 979 if (const ArrayType *AT = Ctx.getAsArrayType(T)) { 980 // Set the default value of the array to conjured symbol. 981 DefinedOrUnknownSVal V = 982 svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, 983 AT->getElementType(), Count); 984 B = B.addBinding(baseR, BindingKey::Default, V); 985 return; 986 } 987 988 if (includeGlobals && 989 isa<NonStaticGlobalSpaceRegion>(baseR->getMemorySpace())) { 990 // If the region is a global and we are invalidating all globals, 991 // just erase the entry. This causes all globals to be lazily 992 // symbolicated from the same base symbol. 993 B = B.removeBinding(baseR); 994 return; 995 } 996 997 998 DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, 999 T,Count); 1000 assert(SymbolManager::canSymbolicate(T) || V.isUnknown()); 1001 B = B.addBinding(baseR, BindingKey::Direct, V); 1002} 1003 1004RegionBindingsRef 1005RegionStoreManager::invalidateGlobalRegion(MemRegion::Kind K, 1006 const Expr *Ex, 1007 unsigned Count, 1008 const LocationContext *LCtx, 1009 RegionBindingsRef B, 1010 InvalidatedRegions *Invalidated) { 1011 // Bind the globals memory space to a new symbol that we will use to derive 1012 // the bindings for all globals. 1013 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion(K); 1014 SVal V = svalBuilder.conjureSymbolVal(/* SymbolTag = */ (const void*) GS, Ex, LCtx, 1015 /* type does not matter */ Ctx.IntTy, 1016 Count); 1017 1018 B = B.removeBinding(GS) 1019 .addBinding(BindingKey::Make(GS, BindingKey::Default), V); 1020 1021 // Even if there are no bindings in the global scope, we still need to 1022 // record that we touched it. 1023 if (Invalidated) 1024 Invalidated->push_back(GS); 1025 1026 return B; 1027} 1028 1029StoreRef 1030RegionStoreManager::invalidateRegions(Store store, 1031 ArrayRef<const MemRegion *> Regions, 1032 const Expr *Ex, unsigned Count, 1033 const LocationContext *LCtx, 1034 InvalidatedSymbols &IS, 1035 const CallEvent *Call, 1036 InvalidatedRegions *Invalidated) { 1037 invalidateRegionsWorker W(*this, StateMgr, 1038 RegionStoreManager::getRegionBindings(store), 1039 Ex, Count, LCtx, IS, Invalidated, false); 1040 1041 // Scan the bindings and generate the clusters. 1042 W.GenerateClusters(); 1043 1044 // Add the regions to the worklist. 1045 for (ArrayRef<const MemRegion *>::iterator 1046 I = Regions.begin(), E = Regions.end(); I != E; ++I) 1047 W.AddToWorkList(*I); 1048 1049 W.RunWorkList(); 1050 1051 // Return the new bindings. 1052 RegionBindingsRef B = W.getRegionBindings(); 1053 1054 // For all globals which are not static nor immutable: determine which global 1055 // regions should be invalidated and invalidate them. 1056 // TODO: This could possibly be more precise with modules. 1057 // 1058 // System calls invalidate only system globals. 1059 if (Call && Call->isInSystemHeader()) { 1060 B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind, 1061 Ex, Count, LCtx, B, Invalidated); 1062 // Internal calls might invalidate both system and internal globals. 1063 } else { 1064 B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind, 1065 Ex, Count, LCtx, B, Invalidated); 1066 B = invalidateGlobalRegion(MemRegion::GlobalInternalSpaceRegionKind, 1067 Ex, Count, LCtx, B, Invalidated); 1068 } 1069 1070 return StoreRef(B.asStore(), *this); 1071} 1072 1073//===----------------------------------------------------------------------===// 1074// Extents for regions. 1075//===----------------------------------------------------------------------===// 1076 1077DefinedOrUnknownSVal 1078RegionStoreManager::getSizeInElements(ProgramStateRef state, 1079 const MemRegion *R, 1080 QualType EleTy) { 1081 SVal Size = cast<SubRegion>(R)->getExtent(svalBuilder); 1082 const llvm::APSInt *SizeInt = svalBuilder.getKnownValue(state, Size); 1083 if (!SizeInt) 1084 return UnknownVal(); 1085 1086 CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue()); 1087 1088 if (Ctx.getAsVariableArrayType(EleTy)) { 1089 // FIXME: We need to track extra state to properly record the size 1090 // of VLAs. Returning UnknownVal here, however, is a stop-gap so that 1091 // we don't have a divide-by-zero below. 1092 return UnknownVal(); 1093 } 1094 1095 CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy); 1096 1097 // If a variable is reinterpreted as a type that doesn't fit into a larger 1098 // type evenly, round it down. 1099 // This is a signed value, since it's used in arithmetic with signed indices. 1100 return svalBuilder.makeIntVal(RegionSize / EleSize, false); 1101} 1102 1103//===----------------------------------------------------------------------===// 1104// Location and region casting. 1105//===----------------------------------------------------------------------===// 1106 1107/// ArrayToPointer - Emulates the "decay" of an array to a pointer 1108/// type. 'Array' represents the lvalue of the array being decayed 1109/// to a pointer, and the returned SVal represents the decayed 1110/// version of that lvalue (i.e., a pointer to the first element of 1111/// the array). This is called by ExprEngine when evaluating casts 1112/// from arrays to pointers. 1113SVal RegionStoreManager::ArrayToPointer(Loc Array) { 1114 if (!Array.getAs<loc::MemRegionVal>()) 1115 return UnknownVal(); 1116 1117 const MemRegion* R = Array.castAs<loc::MemRegionVal>().getRegion(); 1118 const TypedValueRegion* ArrayR = dyn_cast<TypedValueRegion>(R); 1119 1120 if (!ArrayR) 1121 return UnknownVal(); 1122 1123 // Strip off typedefs from the ArrayRegion's ValueType. 1124 QualType T = ArrayR->getValueType().getDesugaredType(Ctx); 1125 const ArrayType *AT = cast<ArrayType>(T); 1126 T = AT->getElementType(); 1127 1128 NonLoc ZeroIdx = svalBuilder.makeZeroArrayIndex(); 1129 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx)); 1130} 1131 1132//===----------------------------------------------------------------------===// 1133// Loading values from regions. 1134//===----------------------------------------------------------------------===// 1135 1136SVal RegionStoreManager::getBinding(RegionBindingsConstRef B, Loc L, QualType T) { 1137 assert(!L.getAs<UnknownVal>() && "location unknown"); 1138 assert(!L.getAs<UndefinedVal>() && "location undefined"); 1139 1140 // For access to concrete addresses, return UnknownVal. Checks 1141 // for null dereferences (and similar errors) are done by checkers, not 1142 // the Store. 1143 // FIXME: We can consider lazily symbolicating such memory, but we really 1144 // should defer this when we can reason easily about symbolicating arrays 1145 // of bytes. 1146 if (L.getAs<loc::ConcreteInt>()) { 1147 return UnknownVal(); 1148 } 1149 if (!L.getAs<loc::MemRegionVal>()) { 1150 return UnknownVal(); 1151 } 1152 1153 const MemRegion *MR = L.castAs<loc::MemRegionVal>().getRegion(); 1154 1155 if (isa<AllocaRegion>(MR) || 1156 isa<SymbolicRegion>(MR) || 1157 isa<CodeTextRegion>(MR)) { 1158 if (T.isNull()) { 1159 if (const TypedRegion *TR = dyn_cast<TypedRegion>(MR)) 1160 T = TR->getLocationType(); 1161 else { 1162 const SymbolicRegion *SR = cast<SymbolicRegion>(MR); 1163 T = SR->getSymbol()->getType(); 1164 } 1165 } 1166 MR = GetElementZeroRegion(MR, T); 1167 } 1168 1169 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument 1170 // instead of 'Loc', and have the other Loc cases handled at a higher level. 1171 const TypedValueRegion *R = cast<TypedValueRegion>(MR); 1172 QualType RTy = R->getValueType(); 1173 1174 // FIXME: we do not yet model the parts of a complex type, so treat the 1175 // whole thing as "unknown". 1176 if (RTy->isAnyComplexType()) 1177 return UnknownVal(); 1178 1179 // FIXME: We should eventually handle funny addressing. e.g.: 1180 // 1181 // int x = ...; 1182 // int *p = &x; 1183 // char *q = (char*) p; 1184 // char c = *q; // returns the first byte of 'x'. 1185 // 1186 // Such funny addressing will occur due to layering of regions. 1187 if (RTy->isStructureOrClassType()) 1188 return getBindingForStruct(B, R); 1189 1190 // FIXME: Handle unions. 1191 if (RTy->isUnionType()) 1192 return UnknownVal(); 1193 1194 if (RTy->isArrayType()) { 1195 if (RTy->isConstantArrayType()) 1196 return getBindingForArray(B, R); 1197 else 1198 return UnknownVal(); 1199 } 1200 1201 // FIXME: handle Vector types. 1202 if (RTy->isVectorType()) 1203 return UnknownVal(); 1204 1205 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) 1206 return CastRetrievedVal(getBindingForField(B, FR), FR, T, false); 1207 1208 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) { 1209 // FIXME: Here we actually perform an implicit conversion from the loaded 1210 // value to the element type. Eventually we want to compose these values 1211 // more intelligently. For example, an 'element' can encompass multiple 1212 // bound regions (e.g., several bound bytes), or could be a subset of 1213 // a larger value. 1214 return CastRetrievedVal(getBindingForElement(B, ER), ER, T, false); 1215 } 1216 1217 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) { 1218 // FIXME: Here we actually perform an implicit conversion from the loaded 1219 // value to the ivar type. What we should model is stores to ivars 1220 // that blow past the extent of the ivar. If the address of the ivar is 1221 // reinterpretted, it is possible we stored a different value that could 1222 // fit within the ivar. Either we need to cast these when storing them 1223 // or reinterpret them lazily (as we do here). 1224 return CastRetrievedVal(getBindingForObjCIvar(B, IVR), IVR, T, false); 1225 } 1226 1227 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { 1228 // FIXME: Here we actually perform an implicit conversion from the loaded 1229 // value to the variable type. What we should model is stores to variables 1230 // that blow past the extent of the variable. If the address of the 1231 // variable is reinterpretted, it is possible we stored a different value 1232 // that could fit within the variable. Either we need to cast these when 1233 // storing them or reinterpret them lazily (as we do here). 1234 return CastRetrievedVal(getBindingForVar(B, VR), VR, T, false); 1235 } 1236 1237 const SVal *V = B.lookup(R, BindingKey::Direct); 1238 1239 // Check if the region has a binding. 1240 if (V) 1241 return *V; 1242 1243 // The location does not have a bound value. This means that it has 1244 // the value it had upon its creation and/or entry to the analyzed 1245 // function/method. These are either symbolic values or 'undefined'. 1246 if (R->hasStackNonParametersStorage()) { 1247 // All stack variables are considered to have undefined values 1248 // upon creation. All heap allocated blocks are considered to 1249 // have undefined values as well unless they are explicitly bound 1250 // to specific values. 1251 return UndefinedVal(); 1252 } 1253 1254 // All other values are symbolic. 1255 return svalBuilder.getRegionValueSymbolVal(R); 1256} 1257 1258/// Checks to see if store \p B has a lazy binding for region \p R. 1259/// 1260/// If \p AllowSubregionBindings is \c false, a lazy binding will be rejected 1261/// if there are additional bindings within \p R. 1262/// 1263/// Note that unlike RegionStoreManager::findLazyBinding, this will not search 1264/// for lazy bindings for super-regions of \p R. 1265static Optional<nonloc::LazyCompoundVal> 1266getExistingLazyBinding(SValBuilder &SVB, RegionBindingsConstRef B, 1267 const SubRegion *R, bool AllowSubregionBindings) { 1268 Optional<SVal> V = B.getDefaultBinding(R); 1269 if (!V) 1270 return None; 1271 1272 Optional<nonloc::LazyCompoundVal> LCV = V->getAs<nonloc::LazyCompoundVal>(); 1273 if (!LCV) 1274 return None; 1275 1276 // If the LCV is for a subregion, the types won't match, and we shouldn't 1277 // reuse the binding. Unfortuately we can only check this if the destination 1278 // region is a TypedValueRegion. 1279 if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(R)) { 1280 QualType RegionTy = TVR->getValueType(); 1281 QualType SourceRegionTy = LCV->getRegion()->getValueType(); 1282 if (!SVB.getContext().hasSameUnqualifiedType(RegionTy, SourceRegionTy)) 1283 return None; 1284 } 1285 1286 if (!AllowSubregionBindings) { 1287 // If there are any other bindings within this region, we shouldn't reuse 1288 // the top-level binding. 1289 SmallVector<BindingKey, 16> Keys; 1290 collectSubRegionKeys(Keys, SVB, *B.lookup(R->getBaseRegion()), R, 1291 /*IncludeAllDefaultBindings=*/true); 1292 if (Keys.size() > 1) 1293 return None; 1294 } 1295 1296 return *LCV; 1297} 1298 1299 1300std::pair<Store, const SubRegion *> 1301RegionStoreManager::findLazyBinding(RegionBindingsConstRef B, 1302 const SubRegion *R, 1303 const SubRegion *originalRegion) { 1304 if (originalRegion != R) { 1305 if (Optional<nonloc::LazyCompoundVal> V = 1306 getExistingLazyBinding(svalBuilder, B, R, true)) 1307 return std::make_pair(V->getStore(), V->getRegion()); 1308 } 1309 1310 typedef std::pair<Store, const SubRegion *> StoreRegionPair; 1311 StoreRegionPair Result = StoreRegionPair(); 1312 1313 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { 1314 Result = findLazyBinding(B, cast<SubRegion>(ER->getSuperRegion()), 1315 originalRegion); 1316 1317 if (Result.second) 1318 Result.second = MRMgr.getElementRegionWithSuper(ER, Result.second); 1319 1320 } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) { 1321 Result = findLazyBinding(B, cast<SubRegion>(FR->getSuperRegion()), 1322 originalRegion); 1323 1324 if (Result.second) 1325 Result.second = MRMgr.getFieldRegionWithSuper(FR, Result.second); 1326 1327 } else if (const CXXBaseObjectRegion *BaseReg = 1328 dyn_cast<CXXBaseObjectRegion>(R)) { 1329 // C++ base object region is another kind of region that we should blast 1330 // through to look for lazy compound value. It is like a field region. 1331 Result = findLazyBinding(B, cast<SubRegion>(BaseReg->getSuperRegion()), 1332 originalRegion); 1333 1334 if (Result.second) 1335 Result.second = MRMgr.getCXXBaseObjectRegionWithSuper(BaseReg, 1336 Result.second); 1337 } 1338 1339 return Result; 1340} 1341 1342SVal RegionStoreManager::getBindingForElement(RegionBindingsConstRef B, 1343 const ElementRegion* R) { 1344 // We do not currently model bindings of the CompoundLiteralregion. 1345 if (isa<CompoundLiteralRegion>(R->getBaseRegion())) 1346 return UnknownVal(); 1347 1348 // Check if the region has a binding. 1349 if (const Optional<SVal> &V = B.getDirectBinding(R)) 1350 return *V; 1351 1352 const MemRegion* superR = R->getSuperRegion(); 1353 1354 // Check if the region is an element region of a string literal. 1355 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) { 1356 // FIXME: Handle loads from strings where the literal is treated as 1357 // an integer, e.g., *((unsigned int*)"hello") 1358 QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType(); 1359 if (T != Ctx.getCanonicalType(R->getElementType())) 1360 return UnknownVal(); 1361 1362 const StringLiteral *Str = StrR->getStringLiteral(); 1363 SVal Idx = R->getIndex(); 1364 if (Optional<nonloc::ConcreteInt> CI = Idx.getAs<nonloc::ConcreteInt>()) { 1365 int64_t i = CI->getValue().getSExtValue(); 1366 // Abort on string underrun. This can be possible by arbitrary 1367 // clients of getBindingForElement(). 1368 if (i < 0) 1369 return UndefinedVal(); 1370 int64_t length = Str->getLength(); 1371 // Technically, only i == length is guaranteed to be null. 1372 // However, such overflows should be caught before reaching this point; 1373 // the only time such an access would be made is if a string literal was 1374 // used to initialize a larger array. 1375 char c = (i >= length) ? '\0' : Str->getCodeUnit(i); 1376 return svalBuilder.makeIntVal(c, T); 1377 } 1378 } 1379 1380 // Check for loads from a code text region. For such loads, just give up. 1381 if (isa<CodeTextRegion>(superR)) 1382 return UnknownVal(); 1383 1384 // Handle the case where we are indexing into a larger scalar object. 1385 // For example, this handles: 1386 // int x = ... 1387 // char *y = &x; 1388 // return *y; 1389 // FIXME: This is a hack, and doesn't do anything really intelligent yet. 1390 const RegionRawOffset &O = R->getAsArrayOffset(); 1391 1392 // If we cannot reason about the offset, return an unknown value. 1393 if (!O.getRegion()) 1394 return UnknownVal(); 1395 1396 if (const TypedValueRegion *baseR = 1397 dyn_cast_or_null<TypedValueRegion>(O.getRegion())) { 1398 QualType baseT = baseR->getValueType(); 1399 if (baseT->isScalarType()) { 1400 QualType elemT = R->getElementType(); 1401 if (elemT->isScalarType()) { 1402 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) { 1403 if (const Optional<SVal> &V = B.getDirectBinding(superR)) { 1404 if (SymbolRef parentSym = V->getAsSymbol()) 1405 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R); 1406 1407 if (V->isUnknownOrUndef()) 1408 return *V; 1409 // Other cases: give up. We are indexing into a larger object 1410 // that has some value, but we don't know how to handle that yet. 1411 return UnknownVal(); 1412 } 1413 } 1414 } 1415 } 1416 } 1417 return getBindingForFieldOrElementCommon(B, R, R->getElementType(), 1418 superR); 1419} 1420 1421SVal RegionStoreManager::getBindingForField(RegionBindingsConstRef B, 1422 const FieldRegion* R) { 1423 1424 // Check if the region has a binding. 1425 if (const Optional<SVal> &V = B.getDirectBinding(R)) 1426 return *V; 1427 1428 QualType Ty = R->getValueType(); 1429 return getBindingForFieldOrElementCommon(B, R, Ty, R->getSuperRegion()); 1430} 1431 1432Optional<SVal> 1433RegionStoreManager::getBindingForDerivedDefaultValue(RegionBindingsConstRef B, 1434 const MemRegion *superR, 1435 const TypedValueRegion *R, 1436 QualType Ty) { 1437 1438 if (const Optional<SVal> &D = B.getDefaultBinding(superR)) { 1439 const SVal &val = D.getValue(); 1440 if (SymbolRef parentSym = val.getAsSymbol()) 1441 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R); 1442 1443 if (val.isZeroConstant()) 1444 return svalBuilder.makeZeroVal(Ty); 1445 1446 if (val.isUnknownOrUndef()) 1447 return val; 1448 1449 // Lazy bindings are usually handled through getExistingLazyBinding(). 1450 // We should unify these two code paths at some point. 1451 if (val.getAs<nonloc::LazyCompoundVal>()) 1452 return val; 1453 1454 llvm_unreachable("Unknown default value"); 1455 } 1456 1457 return None; 1458} 1459 1460SVal RegionStoreManager::getLazyBinding(const SubRegion *LazyBindingRegion, 1461 RegionBindingsRef LazyBinding) { 1462 SVal Result; 1463 if (const ElementRegion *ER = dyn_cast<ElementRegion>(LazyBindingRegion)) 1464 Result = getBindingForElement(LazyBinding, ER); 1465 else 1466 Result = getBindingForField(LazyBinding, 1467 cast<FieldRegion>(LazyBindingRegion)); 1468 1469 // FIXME: This is a hack to deal with RegionStore's inability to distinguish a 1470 // default value for /part/ of an aggregate from a default value for the 1471 // /entire/ aggregate. The most common case of this is when struct Outer 1472 // has as its first member a struct Inner, which is copied in from a stack 1473 // variable. In this case, even if the Outer's default value is symbolic, 0, 1474 // or unknown, it gets overridden by the Inner's default value of undefined. 1475 // 1476 // This is a general problem -- if the Inner is zero-initialized, the Outer 1477 // will now look zero-initialized. The proper way to solve this is with a 1478 // new version of RegionStore that tracks the extent of a binding as well 1479 // as the offset. 1480 // 1481 // This hack only takes care of the undefined case because that can very 1482 // quickly result in a warning. 1483 if (Result.isUndef()) 1484 Result = UnknownVal(); 1485 1486 return Result; 1487} 1488 1489SVal 1490RegionStoreManager::getBindingForFieldOrElementCommon(RegionBindingsConstRef B, 1491 const TypedValueRegion *R, 1492 QualType Ty, 1493 const MemRegion *superR) { 1494 1495 // At this point we have already checked in either getBindingForElement or 1496 // getBindingForField if 'R' has a direct binding. 1497 1498 // Lazy binding? 1499 Store lazyBindingStore = NULL; 1500 const SubRegion *lazyBindingRegion = NULL; 1501 llvm::tie(lazyBindingStore, lazyBindingRegion) = findLazyBinding(B, R, R); 1502 if (lazyBindingRegion) 1503 return getLazyBinding(lazyBindingRegion, 1504 getRegionBindings(lazyBindingStore)); 1505 1506 // Record whether or not we see a symbolic index. That can completely 1507 // be out of scope of our lookup. 1508 bool hasSymbolicIndex = false; 1509 1510 // FIXME: This is a hack to deal with RegionStore's inability to distinguish a 1511 // default value for /part/ of an aggregate from a default value for the 1512 // /entire/ aggregate. The most common case of this is when struct Outer 1513 // has as its first member a struct Inner, which is copied in from a stack 1514 // variable. In this case, even if the Outer's default value is symbolic, 0, 1515 // or unknown, it gets overridden by the Inner's default value of undefined. 1516 // 1517 // This is a general problem -- if the Inner is zero-initialized, the Outer 1518 // will now look zero-initialized. The proper way to solve this is with a 1519 // new version of RegionStore that tracks the extent of a binding as well 1520 // as the offset. 1521 // 1522 // This hack only takes care of the undefined case because that can very 1523 // quickly result in a warning. 1524 bool hasPartialLazyBinding = false; 1525 1526 const SubRegion *Base = dyn_cast<SubRegion>(superR); 1527 while (Base) { 1528 if (Optional<SVal> D = getBindingForDerivedDefaultValue(B, Base, R, Ty)) { 1529 if (D->getAs<nonloc::LazyCompoundVal>()) { 1530 hasPartialLazyBinding = true; 1531 break; 1532 } 1533 1534 return *D; 1535 } 1536 1537 if (const ElementRegion *ER = dyn_cast<ElementRegion>(Base)) { 1538 NonLoc index = ER->getIndex(); 1539 if (!index.isConstant()) 1540 hasSymbolicIndex = true; 1541 } 1542 1543 // If our super region is a field or element itself, walk up the region 1544 // hierarchy to see if there is a default value installed in an ancestor. 1545 Base = dyn_cast<SubRegion>(Base->getSuperRegion()); 1546 } 1547 1548 if (R->hasStackNonParametersStorage()) { 1549 if (isa<ElementRegion>(R)) { 1550 // Currently we don't reason specially about Clang-style vectors. Check 1551 // if superR is a vector and if so return Unknown. 1552 if (const TypedValueRegion *typedSuperR = 1553 dyn_cast<TypedValueRegion>(superR)) { 1554 if (typedSuperR->getValueType()->isVectorType()) 1555 return UnknownVal(); 1556 } 1557 } 1558 1559 // FIXME: We also need to take ElementRegions with symbolic indexes into 1560 // account. This case handles both directly accessing an ElementRegion 1561 // with a symbolic offset, but also fields within an element with 1562 // a symbolic offset. 1563 if (hasSymbolicIndex) 1564 return UnknownVal(); 1565 1566 if (!hasPartialLazyBinding) 1567 return UndefinedVal(); 1568 } 1569 1570 // All other values are symbolic. 1571 return svalBuilder.getRegionValueSymbolVal(R); 1572} 1573 1574SVal RegionStoreManager::getBindingForObjCIvar(RegionBindingsConstRef B, 1575 const ObjCIvarRegion* R) { 1576 // Check if the region has a binding. 1577 if (const Optional<SVal> &V = B.getDirectBinding(R)) 1578 return *V; 1579 1580 const MemRegion *superR = R->getSuperRegion(); 1581 1582 // Check if the super region has a default binding. 1583 if (const Optional<SVal> &V = B.getDefaultBinding(superR)) { 1584 if (SymbolRef parentSym = V->getAsSymbol()) 1585 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R); 1586 1587 // Other cases: give up. 1588 return UnknownVal(); 1589 } 1590 1591 return getBindingForLazySymbol(R); 1592} 1593 1594static Optional<SVal> getConstValue(SValBuilder &SVB, const VarDecl *VD) { 1595 ASTContext &Ctx = SVB.getContext(); 1596 if (!VD->getType().isConstQualified()) 1597 return None; 1598 1599 const Expr *Init = VD->getInit(); 1600 if (!Init) 1601 return None; 1602 1603 llvm::APSInt Result; 1604 if (!Init->isGLValue() && Init->EvaluateAsInt(Result, Ctx)) 1605 return SVB.makeIntVal(Result); 1606 1607 if (Init->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)) 1608 return SVB.makeNull(); 1609 1610 // FIXME: Handle other possible constant expressions. 1611 return None; 1612} 1613 1614SVal RegionStoreManager::getBindingForVar(RegionBindingsConstRef B, 1615 const VarRegion *R) { 1616 1617 // Check if the region has a binding. 1618 if (const Optional<SVal> &V = B.getDirectBinding(R)) 1619 return *V; 1620 1621 // Lazily derive a value for the VarRegion. 1622 const VarDecl *VD = R->getDecl(); 1623 const MemSpaceRegion *MS = R->getMemorySpace(); 1624 1625 // Arguments are always symbolic. 1626 if (isa<StackArgumentsSpaceRegion>(MS)) 1627 return svalBuilder.getRegionValueSymbolVal(R); 1628 1629 // Is 'VD' declared constant? If so, retrieve the constant value. 1630 if (Optional<SVal> V = getConstValue(svalBuilder, VD)) 1631 return *V; 1632 1633 // This must come after the check for constants because closure-captured 1634 // constant variables may appear in UnknownSpaceRegion. 1635 if (isa<UnknownSpaceRegion>(MS)) 1636 return svalBuilder.getRegionValueSymbolVal(R); 1637 1638 if (isa<GlobalsSpaceRegion>(MS)) { 1639 QualType T = VD->getType(); 1640 1641 // Function-scoped static variables are default-initialized to 0; if they 1642 // have an initializer, it would have been processed by now. 1643 if (isa<StaticGlobalSpaceRegion>(MS)) 1644 return svalBuilder.makeZeroVal(T); 1645 1646 if (Optional<SVal> V = getBindingForDerivedDefaultValue(B, MS, R, T)) { 1647 assert(!V->getAs<nonloc::LazyCompoundVal>()); 1648 return V.getValue(); 1649 } 1650 1651 return svalBuilder.getRegionValueSymbolVal(R); 1652 } 1653 1654 return UndefinedVal(); 1655} 1656 1657SVal RegionStoreManager::getBindingForLazySymbol(const TypedValueRegion *R) { 1658 // All other values are symbolic. 1659 return svalBuilder.getRegionValueSymbolVal(R); 1660} 1661 1662const RegionStoreManager::SValListTy & 1663RegionStoreManager::getInterestingValues(nonloc::LazyCompoundVal LCV) { 1664 // First, check the cache. 1665 LazyBindingsMapTy::iterator I = LazyBindingsMap.find(LCV.getCVData()); 1666 if (I != LazyBindingsMap.end()) 1667 return I->second; 1668 1669 // If we don't have a list of values cached, start constructing it. 1670 SValListTy List; 1671 1672 const SubRegion *LazyR = LCV.getRegion(); 1673 RegionBindingsRef B = getRegionBindings(LCV.getStore()); 1674 1675 // If this region had /no/ bindings at the time, there are no interesting 1676 // values to return. 1677 const ClusterBindings *Cluster = B.lookup(LazyR->getBaseRegion()); 1678 if (!Cluster) 1679 return (LazyBindingsMap[LCV.getCVData()] = llvm_move(List)); 1680 1681 SmallVector<BindingKey, 32> Keys; 1682 collectSubRegionKeys(Keys, svalBuilder, *Cluster, LazyR, 1683 /*IncludeAllDefaultBindings=*/true); 1684 for (SmallVectorImpl<BindingKey>::const_iterator I = Keys.begin(), 1685 E = Keys.end(); 1686 I != E; ++I) { 1687 SVal V = *Cluster->lookup(*I); 1688 if (V.isUnknownOrUndef() || V.isConstant()) 1689 continue; 1690 1691 if (Optional<nonloc::LazyCompoundVal> InnerLCV = 1692 V.getAs<nonloc::LazyCompoundVal>()) { 1693 const SValListTy &InnerList = getInterestingValues(*InnerLCV); 1694 List.insert(List.end(), InnerList.begin(), InnerList.end()); 1695 continue; 1696 } 1697 1698 List.push_back(V); 1699 } 1700 1701 return (LazyBindingsMap[LCV.getCVData()] = llvm_move(List)); 1702} 1703 1704NonLoc RegionStoreManager::createLazyBinding(RegionBindingsConstRef B, 1705 const TypedValueRegion *R) { 1706 if (Optional<nonloc::LazyCompoundVal> V = 1707 getExistingLazyBinding(svalBuilder, B, R, false)) 1708 return *V; 1709 1710 return svalBuilder.makeLazyCompoundVal(StoreRef(B.asStore(), *this), R); 1711} 1712 1713SVal RegionStoreManager::getBindingForStruct(RegionBindingsConstRef B, 1714 const TypedValueRegion *R) { 1715 const RecordDecl *RD = R->getValueType()->castAs<RecordType>()->getDecl(); 1716 if (RD->field_empty()) 1717 return UnknownVal(); 1718 1719 return createLazyBinding(B, R); 1720} 1721 1722SVal RegionStoreManager::getBindingForArray(RegionBindingsConstRef B, 1723 const TypedValueRegion *R) { 1724 assert(Ctx.getAsConstantArrayType(R->getValueType()) && 1725 "Only constant array types can have compound bindings."); 1726 1727 return createLazyBinding(B, R); 1728} 1729 1730bool RegionStoreManager::includedInBindings(Store store, 1731 const MemRegion *region) const { 1732 RegionBindingsRef B = getRegionBindings(store); 1733 region = region->getBaseRegion(); 1734 1735 // Quick path: if the base is the head of a cluster, the region is live. 1736 if (B.lookup(region)) 1737 return true; 1738 1739 // Slow path: if the region is the VALUE of any binding, it is live. 1740 for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) { 1741 const ClusterBindings &Cluster = RI.getData(); 1742 for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 1743 CI != CE; ++CI) { 1744 const SVal &D = CI.getData(); 1745 if (const MemRegion *R = D.getAsRegion()) 1746 if (R->getBaseRegion() == region) 1747 return true; 1748 } 1749 } 1750 1751 return false; 1752} 1753 1754//===----------------------------------------------------------------------===// 1755// Binding values to regions. 1756//===----------------------------------------------------------------------===// 1757 1758StoreRef RegionStoreManager::killBinding(Store ST, Loc L) { 1759 if (Optional<loc::MemRegionVal> LV = L.getAs<loc::MemRegionVal>()) 1760 if (const MemRegion* R = LV->getRegion()) 1761 return StoreRef(getRegionBindings(ST).removeBinding(R) 1762 .asImmutableMap() 1763 .getRootWithoutRetain(), 1764 *this); 1765 1766 return StoreRef(ST, *this); 1767} 1768 1769RegionBindingsRef 1770RegionStoreManager::bind(RegionBindingsConstRef B, Loc L, SVal V) { 1771 if (L.getAs<loc::ConcreteInt>()) 1772 return B; 1773 1774 // If we get here, the location should be a region. 1775 const MemRegion *R = L.castAs<loc::MemRegionVal>().getRegion(); 1776 1777 // Check if the region is a struct region. 1778 if (const TypedValueRegion* TR = dyn_cast<TypedValueRegion>(R)) { 1779 QualType Ty = TR->getValueType(); 1780 if (Ty->isArrayType()) 1781 return bindArray(B, TR, V); 1782 if (Ty->isStructureOrClassType()) 1783 return bindStruct(B, TR, V); 1784 if (Ty->isVectorType()) 1785 return bindVector(B, TR, V); 1786 } 1787 1788 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) { 1789 // Binding directly to a symbolic region should be treated as binding 1790 // to element 0. 1791 QualType T = SR->getSymbol()->getType(); 1792 if (T->isAnyPointerType() || T->isReferenceType()) 1793 T = T->getPointeeType(); 1794 1795 R = GetElementZeroRegion(SR, T); 1796 } 1797 1798 // Clear out bindings that may overlap with this binding. 1799 RegionBindingsRef NewB = removeSubRegionBindings(B, cast<SubRegion>(R)); 1800 return NewB.addBinding(BindingKey::Make(R, BindingKey::Direct), V); 1801} 1802 1803// FIXME: this method should be merged into Bind(). 1804StoreRef RegionStoreManager::bindCompoundLiteral(Store ST, 1805 const CompoundLiteralExpr *CL, 1806 const LocationContext *LC, 1807 SVal V) { 1808 return Bind(ST, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)), V); 1809} 1810 1811RegionBindingsRef 1812RegionStoreManager::setImplicitDefaultValue(RegionBindingsConstRef B, 1813 const MemRegion *R, 1814 QualType T) { 1815 SVal V; 1816 1817 if (Loc::isLocType(T)) 1818 V = svalBuilder.makeNull(); 1819 else if (T->isIntegerType()) 1820 V = svalBuilder.makeZeroVal(T); 1821 else if (T->isStructureOrClassType() || T->isArrayType()) { 1822 // Set the default value to a zero constant when it is a structure 1823 // or array. The type doesn't really matter. 1824 V = svalBuilder.makeZeroVal(Ctx.IntTy); 1825 } 1826 else { 1827 // We can't represent values of this type, but we still need to set a value 1828 // to record that the region has been initialized. 1829 // If this assertion ever fires, a new case should be added above -- we 1830 // should know how to default-initialize any value we can symbolicate. 1831 assert(!SymbolManager::canSymbolicate(T) && "This type is representable"); 1832 V = UnknownVal(); 1833 } 1834 1835 return B.addBinding(R, BindingKey::Default, V); 1836} 1837 1838RegionBindingsRef 1839RegionStoreManager::bindArray(RegionBindingsConstRef B, 1840 const TypedValueRegion* R, 1841 SVal Init) { 1842 1843 const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType())); 1844 QualType ElementTy = AT->getElementType(); 1845 Optional<uint64_t> Size; 1846 1847 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT)) 1848 Size = CAT->getSize().getZExtValue(); 1849 1850 // Check if the init expr is a string literal. 1851 if (Optional<loc::MemRegionVal> MRV = Init.getAs<loc::MemRegionVal>()) { 1852 const StringRegion *S = cast<StringRegion>(MRV->getRegion()); 1853 1854 // Treat the string as a lazy compound value. 1855 StoreRef store(B.asStore(), *this); 1856 nonloc::LazyCompoundVal LCV = svalBuilder.makeLazyCompoundVal(store, S) 1857 .castAs<nonloc::LazyCompoundVal>(); 1858 return bindAggregate(B, R, LCV); 1859 } 1860 1861 // Handle lazy compound values. 1862 if (Init.getAs<nonloc::LazyCompoundVal>()) 1863 return bindAggregate(B, R, Init); 1864 1865 // Remaining case: explicit compound values. 1866 1867 if (Init.isUnknown()) 1868 return setImplicitDefaultValue(B, R, ElementTy); 1869 1870 const nonloc::CompoundVal& CV = Init.castAs<nonloc::CompoundVal>(); 1871 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); 1872 uint64_t i = 0; 1873 1874 RegionBindingsRef NewB(B); 1875 1876 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) { 1877 // The init list might be shorter than the array length. 1878 if (VI == VE) 1879 break; 1880 1881 const NonLoc &Idx = svalBuilder.makeArrayIndex(i); 1882 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx); 1883 1884 if (ElementTy->isStructureOrClassType()) 1885 NewB = bindStruct(NewB, ER, *VI); 1886 else if (ElementTy->isArrayType()) 1887 NewB = bindArray(NewB, ER, *VI); 1888 else 1889 NewB = bind(NewB, svalBuilder.makeLoc(ER), *VI); 1890 } 1891 1892 // If the init list is shorter than the array length, set the 1893 // array default value. 1894 if (Size.hasValue() && i < Size.getValue()) 1895 NewB = setImplicitDefaultValue(NewB, R, ElementTy); 1896 1897 return NewB; 1898} 1899 1900RegionBindingsRef RegionStoreManager::bindVector(RegionBindingsConstRef B, 1901 const TypedValueRegion* R, 1902 SVal V) { 1903 QualType T = R->getValueType(); 1904 assert(T->isVectorType()); 1905 const VectorType *VT = T->getAs<VectorType>(); // Use getAs for typedefs. 1906 1907 // Handle lazy compound values and symbolic values. 1908 if (V.getAs<nonloc::LazyCompoundVal>() || V.getAs<nonloc::SymbolVal>()) 1909 return bindAggregate(B, R, V); 1910 1911 // We may get non-CompoundVal accidentally due to imprecise cast logic or 1912 // that we are binding symbolic struct value. Kill the field values, and if 1913 // the value is symbolic go and bind it as a "default" binding. 1914 if (!V.getAs<nonloc::CompoundVal>()) { 1915 return bindAggregate(B, R, UnknownVal()); 1916 } 1917 1918 QualType ElemType = VT->getElementType(); 1919 nonloc::CompoundVal CV = V.castAs<nonloc::CompoundVal>(); 1920 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); 1921 unsigned index = 0, numElements = VT->getNumElements(); 1922 RegionBindingsRef NewB(B); 1923 1924 for ( ; index != numElements ; ++index) { 1925 if (VI == VE) 1926 break; 1927 1928 NonLoc Idx = svalBuilder.makeArrayIndex(index); 1929 const ElementRegion *ER = MRMgr.getElementRegion(ElemType, Idx, R, Ctx); 1930 1931 if (ElemType->isArrayType()) 1932 NewB = bindArray(NewB, ER, *VI); 1933 else if (ElemType->isStructureOrClassType()) 1934 NewB = bindStruct(NewB, ER, *VI); 1935 else 1936 NewB = bind(NewB, svalBuilder.makeLoc(ER), *VI); 1937 } 1938 return NewB; 1939} 1940 1941RegionBindingsRef RegionStoreManager::bindStruct(RegionBindingsConstRef B, 1942 const TypedValueRegion* R, 1943 SVal V) { 1944 if (!Features.supportsFields()) 1945 return B; 1946 1947 QualType T = R->getValueType(); 1948 assert(T->isStructureOrClassType()); 1949 1950 const RecordType* RT = T->getAs<RecordType>(); 1951 RecordDecl *RD = RT->getDecl(); 1952 1953 if (!RD->isCompleteDefinition()) 1954 return B; 1955 1956 // Handle lazy compound values and symbolic values. 1957 if (V.getAs<nonloc::LazyCompoundVal>() || V.getAs<nonloc::SymbolVal>()) 1958 return bindAggregate(B, R, V); 1959 1960 // We may get non-CompoundVal accidentally due to imprecise cast logic or 1961 // that we are binding symbolic struct value. Kill the field values, and if 1962 // the value is symbolic go and bind it as a "default" binding. 1963 if (V.isUnknown() || !V.getAs<nonloc::CompoundVal>()) 1964 return bindAggregate(B, R, UnknownVal()); 1965 1966 const nonloc::CompoundVal& CV = V.castAs<nonloc::CompoundVal>(); 1967 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); 1968 1969 RecordDecl::field_iterator FI, FE; 1970 RegionBindingsRef NewB(B); 1971 1972 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI) { 1973 1974 if (VI == VE) 1975 break; 1976 1977 // Skip any unnamed bitfields to stay in sync with the initializers. 1978 if (FI->isUnnamedBitfield()) 1979 continue; 1980 1981 QualType FTy = FI->getType(); 1982 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); 1983 1984 if (FTy->isArrayType()) 1985 NewB = bindArray(NewB, FR, *VI); 1986 else if (FTy->isStructureOrClassType()) 1987 NewB = bindStruct(NewB, FR, *VI); 1988 else 1989 NewB = bind(NewB, svalBuilder.makeLoc(FR), *VI); 1990 ++VI; 1991 } 1992 1993 // There may be fewer values in the initialize list than the fields of struct. 1994 if (FI != FE) { 1995 NewB = NewB.addBinding(R, BindingKey::Default, 1996 svalBuilder.makeIntVal(0, false)); 1997 } 1998 1999 return NewB; 2000} 2001 2002RegionBindingsRef 2003RegionStoreManager::bindAggregate(RegionBindingsConstRef B, 2004 const TypedRegion *R, 2005 SVal Val) { 2006 // Remove the old bindings, using 'R' as the root of all regions 2007 // we will invalidate. Then add the new binding. 2008 return removeSubRegionBindings(B, R).addBinding(R, BindingKey::Default, Val); 2009} 2010 2011//===----------------------------------------------------------------------===// 2012// State pruning. 2013//===----------------------------------------------------------------------===// 2014 2015namespace { 2016class removeDeadBindingsWorker : 2017 public ClusterAnalysis<removeDeadBindingsWorker> { 2018 SmallVector<const SymbolicRegion*, 12> Postponed; 2019 SymbolReaper &SymReaper; 2020 const StackFrameContext *CurrentLCtx; 2021 2022public: 2023 removeDeadBindingsWorker(RegionStoreManager &rm, 2024 ProgramStateManager &stateMgr, 2025 RegionBindingsRef b, SymbolReaper &symReaper, 2026 const StackFrameContext *LCtx) 2027 : ClusterAnalysis<removeDeadBindingsWorker>(rm, stateMgr, b, 2028 /* includeGlobals = */ false), 2029 SymReaper(symReaper), CurrentLCtx(LCtx) {} 2030 2031 // Called by ClusterAnalysis. 2032 void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C); 2033 void VisitCluster(const MemRegion *baseR, const ClusterBindings &C); 2034 2035 bool UpdatePostponed(); 2036 void VisitBinding(SVal V); 2037}; 2038} 2039 2040void removeDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR, 2041 const ClusterBindings &C) { 2042 2043 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) { 2044 if (SymReaper.isLive(VR)) 2045 AddToWorkList(baseR, &C); 2046 2047 return; 2048 } 2049 2050 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) { 2051 if (SymReaper.isLive(SR->getSymbol())) 2052 AddToWorkList(SR, &C); 2053 else 2054 Postponed.push_back(SR); 2055 2056 return; 2057 } 2058 2059 if (isa<NonStaticGlobalSpaceRegion>(baseR)) { 2060 AddToWorkList(baseR, &C); 2061 return; 2062 } 2063 2064 // CXXThisRegion in the current or parent location context is live. 2065 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) { 2066 const StackArgumentsSpaceRegion *StackReg = 2067 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion()); 2068 const StackFrameContext *RegCtx = StackReg->getStackFrame(); 2069 if (CurrentLCtx && 2070 (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))) 2071 AddToWorkList(TR, &C); 2072 } 2073} 2074 2075void removeDeadBindingsWorker::VisitCluster(const MemRegion *baseR, 2076 const ClusterBindings &C) { 2077 // Mark the symbol for any SymbolicRegion with live bindings as live itself. 2078 // This means we should continue to track that symbol. 2079 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(baseR)) 2080 SymReaper.markLive(SymR->getSymbol()); 2081 2082 for (ClusterBindings::iterator I = C.begin(), E = C.end(); I != E; ++I) 2083 VisitBinding(I.getData()); 2084} 2085 2086void removeDeadBindingsWorker::VisitBinding(SVal V) { 2087 // Is it a LazyCompoundVal? All referenced regions are live as well. 2088 if (Optional<nonloc::LazyCompoundVal> LCS = 2089 V.getAs<nonloc::LazyCompoundVal>()) { 2090 2091 const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS); 2092 2093 for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(), 2094 E = Vals.end(); 2095 I != E; ++I) 2096 VisitBinding(*I); 2097 2098 return; 2099 } 2100 2101 // If V is a region, then add it to the worklist. 2102 if (const MemRegion *R = V.getAsRegion()) { 2103 AddToWorkList(R); 2104 2105 // All regions captured by a block are also live. 2106 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) { 2107 BlockDataRegion::referenced_vars_iterator I = BR->referenced_vars_begin(), 2108 E = BR->referenced_vars_end(); 2109 for ( ; I != E; ++I) 2110 AddToWorkList(I.getCapturedRegion()); 2111 } 2112 } 2113 2114 2115 // Update the set of live symbols. 2116 for (SymExpr::symbol_iterator SI = V.symbol_begin(), SE = V.symbol_end(); 2117 SI!=SE; ++SI) 2118 SymReaper.markLive(*SI); 2119} 2120 2121bool removeDeadBindingsWorker::UpdatePostponed() { 2122 // See if any postponed SymbolicRegions are actually live now, after 2123 // having done a scan. 2124 bool changed = false; 2125 2126 for (SmallVectorImpl<const SymbolicRegion*>::iterator 2127 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) { 2128 if (const SymbolicRegion *SR = *I) { 2129 if (SymReaper.isLive(SR->getSymbol())) { 2130 changed |= AddToWorkList(SR); 2131 *I = NULL; 2132 } 2133 } 2134 } 2135 2136 return changed; 2137} 2138 2139StoreRef RegionStoreManager::removeDeadBindings(Store store, 2140 const StackFrameContext *LCtx, 2141 SymbolReaper& SymReaper) { 2142 RegionBindingsRef B = getRegionBindings(store); 2143 removeDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx); 2144 W.GenerateClusters(); 2145 2146 // Enqueue the region roots onto the worklist. 2147 for (SymbolReaper::region_iterator I = SymReaper.region_begin(), 2148 E = SymReaper.region_end(); I != E; ++I) { 2149 W.AddToWorkList(*I); 2150 } 2151 2152 do W.RunWorkList(); while (W.UpdatePostponed()); 2153 2154 // We have now scanned the store, marking reachable regions and symbols 2155 // as live. We now remove all the regions that are dead from the store 2156 // as well as update DSymbols with the set symbols that are now dead. 2157 for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) { 2158 const MemRegion *Base = I.getKey(); 2159 2160 // If the cluster has been visited, we know the region has been marked. 2161 if (W.isVisited(Base)) 2162 continue; 2163 2164 // Remove the dead entry. 2165 B = B.remove(Base); 2166 2167 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(Base)) 2168 SymReaper.maybeDead(SymR->getSymbol()); 2169 2170 // Mark all non-live symbols that this binding references as dead. 2171 const ClusterBindings &Cluster = I.getData(); 2172 for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 2173 CI != CE; ++CI) { 2174 SVal X = CI.getData(); 2175 SymExpr::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); 2176 for (; SI != SE; ++SI) 2177 SymReaper.maybeDead(*SI); 2178 } 2179 } 2180 2181 return StoreRef(B.asStore(), *this); 2182} 2183 2184//===----------------------------------------------------------------------===// 2185// Utility methods. 2186//===----------------------------------------------------------------------===// 2187 2188void RegionStoreManager::print(Store store, raw_ostream &OS, 2189 const char* nl, const char *sep) { 2190 RegionBindingsRef B = getRegionBindings(store); 2191 OS << "Store (direct and default bindings), " 2192 << B.asStore() 2193 << " :" << nl; 2194 B.dump(OS, nl); 2195} 2196