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