1 //== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines a basic region store model. In this model, we do have field 10 // sensitivity. But we assume nothing about the heap shape. So recursive data 11 // structures are largely ignored. Basically we do 1-limiting analysis. 12 // Parameter pointers are assumed with no aliasing. Pointee objects of 13 // parameters are created lazily. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "clang/AST/Attr.h" 18 #include "clang/AST/CharUnits.h" 19 #include "clang/ASTMatchers/ASTMatchFinder.h" 20 #include "clang/Analysis/Analyses/LiveVariables.h" 21 #include "clang/Analysis/AnalysisDeclContext.h" 22 #include "clang/Basic/JsonSupport.h" 23 #include "clang/Basic/TargetInfo.h" 24 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 25 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 26 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 27 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 28 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" 29 #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h" 30 #include "llvm/ADT/ImmutableMap.h" 31 #include "llvm/ADT/Optional.h" 32 #include "llvm/Support/raw_ostream.h" 33 #include <utility> 34 35 using namespace clang; 36 using namespace ento; 37 38 //===----------------------------------------------------------------------===// 39 // Representation of binding keys. 40 //===----------------------------------------------------------------------===// 41 42 namespace { 43 class BindingKey { 44 public: 45 enum Kind { Default = 0x0, Direct = 0x1 }; 46 private: 47 enum { Symbolic = 0x2 }; 48 49 llvm::PointerIntPair<const MemRegion *, 2> P; 50 uint64_t Data; 51 52 /// Create a key for a binding to region \p r, which has a symbolic offset 53 /// from region \p Base. 54 explicit BindingKey(const SubRegion *r, const SubRegion *Base, Kind k) 55 : P(r, k | Symbolic), Data(reinterpret_cast<uintptr_t>(Base)) { 56 assert(r && Base && "Must have known regions."); 57 assert(getConcreteOffsetRegion() == Base && "Failed to store base region"); 58 } 59 60 /// Create a key for a binding at \p offset from base region \p r. 61 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k) 62 : P(r, k), Data(offset) { 63 assert(r && "Must have known regions."); 64 assert(getOffset() == offset && "Failed to store offset"); 65 assert((r == r->getBaseRegion() || isa<ObjCIvarRegion>(r) || 66 isa <CXXDerivedObjectRegion>(r)) && 67 "Not a base"); 68 } 69 public: 70 71 bool isDirect() const { return P.getInt() & Direct; } 72 bool hasSymbolicOffset() const { return P.getInt() & Symbolic; } 73 74 const MemRegion *getRegion() const { return P.getPointer(); } 75 uint64_t getOffset() const { 76 assert(!hasSymbolicOffset()); 77 return Data; 78 } 79 80 const SubRegion *getConcreteOffsetRegion() const { 81 assert(hasSymbolicOffset()); 82 return reinterpret_cast<const SubRegion *>(static_cast<uintptr_t>(Data)); 83 } 84 85 const MemRegion *getBaseRegion() const { 86 if (hasSymbolicOffset()) 87 return getConcreteOffsetRegion()->getBaseRegion(); 88 return getRegion()->getBaseRegion(); 89 } 90 91 void Profile(llvm::FoldingSetNodeID& ID) const { 92 ID.AddPointer(P.getOpaqueValue()); 93 ID.AddInteger(Data); 94 } 95 96 static BindingKey Make(const MemRegion *R, Kind k); 97 98 bool operator<(const BindingKey &X) const { 99 if (P.getOpaqueValue() < X.P.getOpaqueValue()) 100 return true; 101 if (P.getOpaqueValue() > X.P.getOpaqueValue()) 102 return false; 103 return Data < X.Data; 104 } 105 106 bool operator==(const BindingKey &X) const { 107 return P.getOpaqueValue() == X.P.getOpaqueValue() && 108 Data == X.Data; 109 } 110 111 void dump() const; 112 }; 113 } // end anonymous namespace 114 115 BindingKey BindingKey::Make(const MemRegion *R, Kind k) { 116 const RegionOffset &RO = R->getAsOffset(); 117 if (RO.hasSymbolicOffset()) 118 return BindingKey(cast<SubRegion>(R), cast<SubRegion>(RO.getRegion()), k); 119 120 return BindingKey(RO.getRegion(), RO.getOffset(), k); 121 } 122 123 namespace llvm { 124 static inline raw_ostream &operator<<(raw_ostream &Out, BindingKey K) { 125 Out << "\"kind\": \"" << (K.isDirect() ? "Direct" : "Default") 126 << "\", \"offset\": "; 127 128 if (!K.hasSymbolicOffset()) 129 Out << K.getOffset(); 130 else 131 Out << "null"; 132 133 return Out; 134 } 135 136 } // namespace llvm 137 138 LLVM_DUMP_METHOD void BindingKey::dump() const { llvm::errs() << *this; } 139 140 //===----------------------------------------------------------------------===// 141 // Actual Store type. 142 //===----------------------------------------------------------------------===// 143 144 typedef llvm::ImmutableMap<BindingKey, SVal> ClusterBindings; 145 typedef llvm::ImmutableMapRef<BindingKey, SVal> ClusterBindingsRef; 146 typedef std::pair<BindingKey, SVal> BindingPair; 147 148 typedef llvm::ImmutableMap<const MemRegion *, ClusterBindings> 149 RegionBindings; 150 151 namespace { 152 class RegionBindingsRef : public llvm::ImmutableMapRef<const MemRegion *, 153 ClusterBindings> { 154 ClusterBindings::Factory *CBFactory; 155 156 public: 157 typedef llvm::ImmutableMapRef<const MemRegion *, ClusterBindings> 158 ParentTy; 159 160 RegionBindingsRef(ClusterBindings::Factory &CBFactory, 161 const RegionBindings::TreeTy *T, 162 RegionBindings::TreeTy::Factory *F) 163 : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(T, F), 164 CBFactory(&CBFactory) {} 165 166 RegionBindingsRef(const ParentTy &P, ClusterBindings::Factory &CBFactory) 167 : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(P), 168 CBFactory(&CBFactory) {} 169 170 RegionBindingsRef add(key_type_ref K, data_type_ref D) const { 171 return RegionBindingsRef(static_cast<const ParentTy *>(this)->add(K, D), 172 *CBFactory); 173 } 174 175 RegionBindingsRef remove(key_type_ref K) const { 176 return RegionBindingsRef(static_cast<const ParentTy *>(this)->remove(K), 177 *CBFactory); 178 } 179 180 RegionBindingsRef addBinding(BindingKey K, SVal V) const; 181 182 RegionBindingsRef addBinding(const MemRegion *R, 183 BindingKey::Kind k, SVal V) const; 184 185 const SVal *lookup(BindingKey K) const; 186 const SVal *lookup(const MemRegion *R, BindingKey::Kind k) const; 187 using llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>::lookup; 188 189 RegionBindingsRef removeBinding(BindingKey K); 190 191 RegionBindingsRef removeBinding(const MemRegion *R, 192 BindingKey::Kind k); 193 194 RegionBindingsRef removeBinding(const MemRegion *R) { 195 return removeBinding(R, BindingKey::Direct). 196 removeBinding(R, BindingKey::Default); 197 } 198 199 Optional<SVal> getDirectBinding(const MemRegion *R) const; 200 201 /// getDefaultBinding - Returns an SVal* representing an optional default 202 /// binding associated with a region and its subregions. 203 Optional<SVal> getDefaultBinding(const MemRegion *R) const; 204 205 /// Return the internal tree as a Store. 206 Store asStore() const { 207 return asImmutableMap().getRootWithoutRetain(); 208 } 209 210 void printJson(raw_ostream &Out, const char *NL = "\n", 211 unsigned int Space = 0, bool IsDot = false) const { 212 for (iterator I = begin(); I != end(); ++I) { 213 // TODO: We might need a .printJson for I.getKey() as well. 214 Indent(Out, Space, IsDot) 215 << "{ \"cluster\": \"" << I.getKey() << "\", \"pointer\": \"" 216 << (const void *)I.getKey() << "\", \"items\": [" << NL; 217 218 ++Space; 219 const ClusterBindings &CB = I.getData(); 220 for (ClusterBindings::iterator CI = CB.begin(); CI != CB.end(); ++CI) { 221 Indent(Out, Space, IsDot) << "{ " << CI.getKey() << ", \"value\": "; 222 CI.getData().printJson(Out, /*AddQuotes=*/true); 223 Out << " }"; 224 if (std::next(CI) != CB.end()) 225 Out << ','; 226 Out << NL; 227 } 228 229 --Space; 230 Indent(Out, Space, IsDot) << "]}"; 231 if (std::next(I) != end()) 232 Out << ','; 233 Out << NL; 234 } 235 } 236 237 LLVM_DUMP_METHOD void dump() const { printJson(llvm::errs()); } 238 }; 239 } // end anonymous namespace 240 241 typedef const RegionBindingsRef& RegionBindingsConstRef; 242 243 Optional<SVal> RegionBindingsRef::getDirectBinding(const MemRegion *R) const { 244 return Optional<SVal>::create(lookup(R, BindingKey::Direct)); 245 } 246 247 Optional<SVal> RegionBindingsRef::getDefaultBinding(const MemRegion *R) const { 248 return Optional<SVal>::create(lookup(R, BindingKey::Default)); 249 } 250 251 RegionBindingsRef RegionBindingsRef::addBinding(BindingKey K, SVal V) const { 252 const MemRegion *Base = K.getBaseRegion(); 253 254 const ClusterBindings *ExistingCluster = lookup(Base); 255 ClusterBindings Cluster = 256 (ExistingCluster ? *ExistingCluster : CBFactory->getEmptyMap()); 257 258 ClusterBindings NewCluster = CBFactory->add(Cluster, K, V); 259 return add(Base, NewCluster); 260 } 261 262 263 RegionBindingsRef RegionBindingsRef::addBinding(const MemRegion *R, 264 BindingKey::Kind k, 265 SVal V) const { 266 return addBinding(BindingKey::Make(R, k), V); 267 } 268 269 const SVal *RegionBindingsRef::lookup(BindingKey K) const { 270 const ClusterBindings *Cluster = lookup(K.getBaseRegion()); 271 if (!Cluster) 272 return nullptr; 273 return Cluster->lookup(K); 274 } 275 276 const SVal *RegionBindingsRef::lookup(const MemRegion *R, 277 BindingKey::Kind k) const { 278 return lookup(BindingKey::Make(R, k)); 279 } 280 281 RegionBindingsRef RegionBindingsRef::removeBinding(BindingKey K) { 282 const MemRegion *Base = K.getBaseRegion(); 283 const ClusterBindings *Cluster = lookup(Base); 284 if (!Cluster) 285 return *this; 286 287 ClusterBindings NewCluster = CBFactory->remove(*Cluster, K); 288 if (NewCluster.isEmpty()) 289 return remove(Base); 290 return add(Base, NewCluster); 291 } 292 293 RegionBindingsRef RegionBindingsRef::removeBinding(const MemRegion *R, 294 BindingKey::Kind k){ 295 return removeBinding(BindingKey::Make(R, k)); 296 } 297 298 //===----------------------------------------------------------------------===// 299 // Fine-grained control of RegionStoreManager. 300 //===----------------------------------------------------------------------===// 301 302 namespace { 303 struct minimal_features_tag {}; 304 struct maximal_features_tag {}; 305 306 class RegionStoreFeatures { 307 bool SupportsFields; 308 public: 309 RegionStoreFeatures(minimal_features_tag) : 310 SupportsFields(false) {} 311 312 RegionStoreFeatures(maximal_features_tag) : 313 SupportsFields(true) {} 314 315 void enableFields(bool t) { SupportsFields = t; } 316 317 bool supportsFields() const { return SupportsFields; } 318 }; 319 } 320 321 //===----------------------------------------------------------------------===// 322 // Main RegionStore logic. 323 //===----------------------------------------------------------------------===// 324 325 namespace { 326 class InvalidateRegionsWorker; 327 328 class RegionStoreManager : public StoreManager { 329 public: 330 const RegionStoreFeatures Features; 331 332 RegionBindings::Factory RBFactory; 333 mutable ClusterBindings::Factory CBFactory; 334 335 typedef std::vector<SVal> SValListTy; 336 private: 337 typedef llvm::DenseMap<const LazyCompoundValData *, 338 SValListTy> LazyBindingsMapTy; 339 LazyBindingsMapTy LazyBindingsMap; 340 341 /// The largest number of fields a struct can have and still be 342 /// considered "small". 343 /// 344 /// This is currently used to decide whether or not it is worth "forcing" a 345 /// LazyCompoundVal on bind. 346 /// 347 /// This is controlled by 'region-store-small-struct-limit' option. 348 /// To disable all small-struct-dependent behavior, set the option to "0". 349 unsigned SmallStructLimit; 350 351 /// A helper used to populate the work list with the given set of 352 /// regions. 353 void populateWorkList(InvalidateRegionsWorker &W, 354 ArrayRef<SVal> Values, 355 InvalidatedRegions *TopLevelRegions); 356 357 public: 358 RegionStoreManager(ProgramStateManager& mgr, const RegionStoreFeatures &f) 359 : StoreManager(mgr), Features(f), 360 RBFactory(mgr.getAllocator()), CBFactory(mgr.getAllocator()), 361 SmallStructLimit(0) { 362 SubEngine &Eng = StateMgr.getOwningEngine(); 363 AnalyzerOptions &Options = Eng.getAnalysisManager().options; 364 SmallStructLimit = Options.RegionStoreSmallStructLimit; 365 } 366 367 368 /// setImplicitDefaultValue - Set the default binding for the provided 369 /// MemRegion to the value implicitly defined for compound literals when 370 /// the value is not specified. 371 RegionBindingsRef setImplicitDefaultValue(RegionBindingsConstRef B, 372 const MemRegion *R, QualType T); 373 374 /// ArrayToPointer - Emulates the "decay" of an array to a pointer 375 /// type. 'Array' represents the lvalue of the array being decayed 376 /// to a pointer, and the returned SVal represents the decayed 377 /// version of that lvalue (i.e., a pointer to the first element of 378 /// the array). This is called by ExprEngine when evaluating 379 /// casts from arrays to pointers. 380 SVal ArrayToPointer(Loc Array, QualType ElementTy) override; 381 382 StoreRef getInitialStore(const LocationContext *InitLoc) override { 383 return StoreRef(RBFactory.getEmptyMap().getRootWithoutRetain(), *this); 384 } 385 386 //===-------------------------------------------------------------------===// 387 // Binding values to regions. 388 //===-------------------------------------------------------------------===// 389 RegionBindingsRef invalidateGlobalRegion(MemRegion::Kind K, 390 const Expr *Ex, 391 unsigned Count, 392 const LocationContext *LCtx, 393 RegionBindingsRef B, 394 InvalidatedRegions *Invalidated); 395 396 StoreRef invalidateRegions(Store store, 397 ArrayRef<SVal> Values, 398 const Expr *E, unsigned Count, 399 const LocationContext *LCtx, 400 const CallEvent *Call, 401 InvalidatedSymbols &IS, 402 RegionAndSymbolInvalidationTraits &ITraits, 403 InvalidatedRegions *Invalidated, 404 InvalidatedRegions *InvalidatedTopLevel) override; 405 406 bool scanReachableSymbols(Store S, const MemRegion *R, 407 ScanReachableSymbols &Callbacks) override; 408 409 RegionBindingsRef removeSubRegionBindings(RegionBindingsConstRef B, 410 const SubRegion *R); 411 412 public: // Part of public interface to class. 413 414 StoreRef Bind(Store store, Loc LV, SVal V) override { 415 return StoreRef(bind(getRegionBindings(store), LV, V).asStore(), *this); 416 } 417 418 RegionBindingsRef bind(RegionBindingsConstRef B, Loc LV, SVal V); 419 420 // BindDefaultInitial is only used to initialize a region with 421 // a default value. 422 StoreRef BindDefaultInitial(Store store, const MemRegion *R, 423 SVal V) override { 424 RegionBindingsRef B = getRegionBindings(store); 425 // Use other APIs when you have to wipe the region that was initialized 426 // earlier. 427 assert(!(B.getDefaultBinding(R) || B.getDirectBinding(R)) && 428 "Double initialization!"); 429 B = B.addBinding(BindingKey::Make(R, BindingKey::Default), V); 430 return StoreRef(B.asImmutableMap().getRootWithoutRetain(), *this); 431 } 432 433 // BindDefaultZero is used for zeroing constructors that may accidentally 434 // overwrite existing bindings. 435 StoreRef BindDefaultZero(Store store, const MemRegion *R) override { 436 // FIXME: The offsets of empty bases can be tricky because of 437 // of the so called "empty base class optimization". 438 // If a base class has been optimized out 439 // we should not try to create a binding, otherwise we should. 440 // Unfortunately, at the moment ASTRecordLayout doesn't expose 441 // the actual sizes of the empty bases 442 // and trying to infer them from offsets/alignments 443 // seems to be error-prone and non-trivial because of the trailing padding. 444 // As a temporary mitigation we don't create bindings for empty bases. 445 if (const auto *BR = dyn_cast<CXXBaseObjectRegion>(R)) 446 if (BR->getDecl()->isEmpty()) 447 return StoreRef(store, *this); 448 449 RegionBindingsRef B = getRegionBindings(store); 450 SVal V = svalBuilder.makeZeroVal(Ctx.CharTy); 451 B = removeSubRegionBindings(B, cast<SubRegion>(R)); 452 B = B.addBinding(BindingKey::Make(R, BindingKey::Default), V); 453 return StoreRef(B.asImmutableMap().getRootWithoutRetain(), *this); 454 } 455 456 /// Attempt to extract the fields of \p LCV and bind them to the struct region 457 /// \p R. 458 /// 459 /// This path is used when it seems advantageous to "force" loading the values 460 /// within a LazyCompoundVal to bind memberwise to the struct region, rather 461 /// than using a Default binding at the base of the entire region. This is a 462 /// heuristic attempting to avoid building long chains of LazyCompoundVals. 463 /// 464 /// \returns The updated store bindings, or \c None if binding non-lazily 465 /// would be too expensive. 466 Optional<RegionBindingsRef> tryBindSmallStruct(RegionBindingsConstRef B, 467 const TypedValueRegion *R, 468 const RecordDecl *RD, 469 nonloc::LazyCompoundVal LCV); 470 471 /// BindStruct - Bind a compound value to a structure. 472 RegionBindingsRef bindStruct(RegionBindingsConstRef B, 473 const TypedValueRegion* R, SVal V); 474 475 /// BindVector - Bind a compound value to a vector. 476 RegionBindingsRef bindVector(RegionBindingsConstRef B, 477 const TypedValueRegion* R, SVal V); 478 479 RegionBindingsRef bindArray(RegionBindingsConstRef B, 480 const TypedValueRegion* R, 481 SVal V); 482 483 /// Clears out all bindings in the given region and assigns a new value 484 /// as a Default binding. 485 RegionBindingsRef bindAggregate(RegionBindingsConstRef B, 486 const TypedRegion *R, 487 SVal DefaultVal); 488 489 /// Create a new store with the specified binding removed. 490 /// \param ST the original store, that is the basis for the new store. 491 /// \param L the location whose binding should be removed. 492 StoreRef killBinding(Store ST, Loc L) override; 493 494 void incrementReferenceCount(Store store) override { 495 getRegionBindings(store).manualRetain(); 496 } 497 498 /// If the StoreManager supports it, decrement the reference count of 499 /// the specified Store object. If the reference count hits 0, the memory 500 /// associated with the object is recycled. 501 void decrementReferenceCount(Store store) override { 502 getRegionBindings(store).manualRelease(); 503 } 504 505 bool includedInBindings(Store store, const MemRegion *region) const override; 506 507 /// Return the value bound to specified location in a given state. 508 /// 509 /// The high level logic for this method is this: 510 /// getBinding (L) 511 /// if L has binding 512 /// return L's binding 513 /// else if L is in killset 514 /// return unknown 515 /// else 516 /// if L is on stack or heap 517 /// return undefined 518 /// else 519 /// return symbolic 520 SVal getBinding(Store S, Loc L, QualType T) override { 521 return getBinding(getRegionBindings(S), L, T); 522 } 523 524 Optional<SVal> getDefaultBinding(Store S, const MemRegion *R) override { 525 RegionBindingsRef B = getRegionBindings(S); 526 // Default bindings are always applied over a base region so look up the 527 // base region's default binding, otherwise the lookup will fail when R 528 // is at an offset from R->getBaseRegion(). 529 return B.getDefaultBinding(R->getBaseRegion()); 530 } 531 532 SVal getBinding(RegionBindingsConstRef B, Loc L, QualType T = QualType()); 533 534 SVal getBindingForElement(RegionBindingsConstRef B, const ElementRegion *R); 535 536 SVal getBindingForField(RegionBindingsConstRef B, const FieldRegion *R); 537 538 SVal getBindingForObjCIvar(RegionBindingsConstRef B, const ObjCIvarRegion *R); 539 540 SVal getBindingForVar(RegionBindingsConstRef B, const VarRegion *R); 541 542 SVal getBindingForLazySymbol(const TypedValueRegion *R); 543 544 SVal getBindingForFieldOrElementCommon(RegionBindingsConstRef B, 545 const TypedValueRegion *R, 546 QualType Ty); 547 548 SVal getLazyBinding(const SubRegion *LazyBindingRegion, 549 RegionBindingsRef LazyBinding); 550 551 /// Get bindings for the values in a struct and return a CompoundVal, used 552 /// when doing struct copy: 553 /// struct s x, y; 554 /// x = y; 555 /// y's value is retrieved by this method. 556 SVal getBindingForStruct(RegionBindingsConstRef B, const TypedValueRegion *R); 557 SVal getBindingForArray(RegionBindingsConstRef B, const TypedValueRegion *R); 558 NonLoc createLazyBinding(RegionBindingsConstRef B, const TypedValueRegion *R); 559 560 /// Used to lazily generate derived symbols for bindings that are defined 561 /// implicitly by default bindings in a super region. 562 /// 563 /// Note that callers may need to specially handle LazyCompoundVals, which 564 /// are returned as is in case the caller needs to treat them differently. 565 Optional<SVal> getBindingForDerivedDefaultValue(RegionBindingsConstRef B, 566 const MemRegion *superR, 567 const TypedValueRegion *R, 568 QualType Ty); 569 570 /// Get the state and region whose binding this region \p R corresponds to. 571 /// 572 /// If there is no lazy binding for \p R, the returned value will have a null 573 /// \c second. Note that a null pointer can represents a valid Store. 574 std::pair<Store, const SubRegion *> 575 findLazyBinding(RegionBindingsConstRef B, const SubRegion *R, 576 const SubRegion *originalRegion); 577 578 /// Returns the cached set of interesting SVals contained within a lazy 579 /// binding. 580 /// 581 /// The precise value of "interesting" is determined for the purposes of 582 /// RegionStore's internal analysis. It must always contain all regions and 583 /// symbols, but may omit constants and other kinds of SVal. 584 const SValListTy &getInterestingValues(nonloc::LazyCompoundVal LCV); 585 586 //===------------------------------------------------------------------===// 587 // State pruning. 588 //===------------------------------------------------------------------===// 589 590 /// removeDeadBindings - Scans the RegionStore of 'state' for dead values. 591 /// It returns a new Store with these values removed. 592 StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx, 593 SymbolReaper& SymReaper) override; 594 595 //===------------------------------------------------------------------===// 596 // Region "extents". 597 //===------------------------------------------------------------------===// 598 599 // FIXME: This method will soon be eliminated; see the note in Store.h. 600 DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state, 601 const MemRegion* R, 602 QualType EleTy) override; 603 604 //===------------------------------------------------------------------===// 605 // Utility methods. 606 //===------------------------------------------------------------------===// 607 608 RegionBindingsRef getRegionBindings(Store store) const { 609 return RegionBindingsRef(CBFactory, 610 static_cast<const RegionBindings::TreeTy*>(store), 611 RBFactory.getTreeFactory()); 612 } 613 614 void printJson(raw_ostream &Out, Store S, const char *NL = "\n", 615 unsigned int Space = 0, bool IsDot = false) const override; 616 617 void iterBindings(Store store, BindingsHandler& f) override { 618 RegionBindingsRef B = getRegionBindings(store); 619 for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) { 620 const ClusterBindings &Cluster = I.getData(); 621 for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 622 CI != CE; ++CI) { 623 const BindingKey &K = CI.getKey(); 624 if (!K.isDirect()) 625 continue; 626 if (const SubRegion *R = dyn_cast<SubRegion>(K.getRegion())) { 627 // FIXME: Possibly incorporate the offset? 628 if (!f.HandleBinding(*this, store, R, CI.getData())) 629 return; 630 } 631 } 632 } 633 } 634 }; 635 636 } // end anonymous namespace 637 638 //===----------------------------------------------------------------------===// 639 // RegionStore creation. 640 //===----------------------------------------------------------------------===// 641 642 std::unique_ptr<StoreManager> 643 ento::CreateRegionStoreManager(ProgramStateManager &StMgr) { 644 RegionStoreFeatures F = maximal_features_tag(); 645 return llvm::make_unique<RegionStoreManager>(StMgr, F); 646 } 647 648 std::unique_ptr<StoreManager> 649 ento::CreateFieldsOnlyRegionStoreManager(ProgramStateManager &StMgr) { 650 RegionStoreFeatures F = minimal_features_tag(); 651 F.enableFields(true); 652 return llvm::make_unique<RegionStoreManager>(StMgr, F); 653 } 654 655 656 //===----------------------------------------------------------------------===// 657 // Region Cluster analysis. 658 //===----------------------------------------------------------------------===// 659 660 namespace { 661 /// Used to determine which global regions are automatically included in the 662 /// initial worklist of a ClusterAnalysis. 663 enum GlobalsFilterKind { 664 /// Don't include any global regions. 665 GFK_None, 666 /// Only include system globals. 667 GFK_SystemOnly, 668 /// Include all global regions. 669 GFK_All 670 }; 671 672 template <typename DERIVED> 673 class ClusterAnalysis { 674 protected: 675 typedef llvm::DenseMap<const MemRegion *, const ClusterBindings *> ClusterMap; 676 typedef const MemRegion * WorkListElement; 677 typedef SmallVector<WorkListElement, 10> WorkList; 678 679 llvm::SmallPtrSet<const ClusterBindings *, 16> Visited; 680 681 WorkList WL; 682 683 RegionStoreManager &RM; 684 ASTContext &Ctx; 685 SValBuilder &svalBuilder; 686 687 RegionBindingsRef B; 688 689 690 protected: 691 const ClusterBindings *getCluster(const MemRegion *R) { 692 return B.lookup(R); 693 } 694 695 /// Returns true if all clusters in the given memspace should be initially 696 /// included in the cluster analysis. Subclasses may provide their 697 /// own implementation. 698 bool includeEntireMemorySpace(const MemRegion *Base) { 699 return false; 700 } 701 702 public: 703 ClusterAnalysis(RegionStoreManager &rm, ProgramStateManager &StateMgr, 704 RegionBindingsRef b) 705 : RM(rm), Ctx(StateMgr.getContext()), 706 svalBuilder(StateMgr.getSValBuilder()), B(std::move(b)) {} 707 708 RegionBindingsRef getRegionBindings() const { return B; } 709 710 bool isVisited(const MemRegion *R) { 711 return Visited.count(getCluster(R)); 712 } 713 714 void GenerateClusters() { 715 // Scan the entire set of bindings and record the region clusters. 716 for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); 717 RI != RE; ++RI){ 718 const MemRegion *Base = RI.getKey(); 719 720 const ClusterBindings &Cluster = RI.getData(); 721 assert(!Cluster.isEmpty() && "Empty clusters should be removed"); 722 static_cast<DERIVED*>(this)->VisitAddedToCluster(Base, Cluster); 723 724 // If the base's memspace should be entirely invalidated, add the cluster 725 // to the workspace up front. 726 if (static_cast<DERIVED*>(this)->includeEntireMemorySpace(Base)) 727 AddToWorkList(WorkListElement(Base), &Cluster); 728 } 729 } 730 731 bool AddToWorkList(WorkListElement E, const ClusterBindings *C) { 732 if (C && !Visited.insert(C).second) 733 return false; 734 WL.push_back(E); 735 return true; 736 } 737 738 bool AddToWorkList(const MemRegion *R) { 739 return static_cast<DERIVED*>(this)->AddToWorkList(R); 740 } 741 742 void RunWorkList() { 743 while (!WL.empty()) { 744 WorkListElement E = WL.pop_back_val(); 745 const MemRegion *BaseR = E; 746 747 static_cast<DERIVED*>(this)->VisitCluster(BaseR, getCluster(BaseR)); 748 } 749 } 750 751 void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C) {} 752 void VisitCluster(const MemRegion *baseR, const ClusterBindings *C) {} 753 754 void VisitCluster(const MemRegion *BaseR, const ClusterBindings *C, 755 bool Flag) { 756 static_cast<DERIVED*>(this)->VisitCluster(BaseR, C); 757 } 758 }; 759 } 760 761 //===----------------------------------------------------------------------===// 762 // Binding invalidation. 763 //===----------------------------------------------------------------------===// 764 765 bool RegionStoreManager::scanReachableSymbols(Store S, const MemRegion *R, 766 ScanReachableSymbols &Callbacks) { 767 assert(R == R->getBaseRegion() && "Should only be called for base regions"); 768 RegionBindingsRef B = getRegionBindings(S); 769 const ClusterBindings *Cluster = B.lookup(R); 770 771 if (!Cluster) 772 return true; 773 774 for (ClusterBindings::iterator RI = Cluster->begin(), RE = Cluster->end(); 775 RI != RE; ++RI) { 776 if (!Callbacks.scan(RI.getData())) 777 return false; 778 } 779 780 return true; 781 } 782 783 static inline bool isUnionField(const FieldRegion *FR) { 784 return FR->getDecl()->getParent()->isUnion(); 785 } 786 787 typedef SmallVector<const FieldDecl *, 8> FieldVector; 788 789 static void getSymbolicOffsetFields(BindingKey K, FieldVector &Fields) { 790 assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys"); 791 792 const MemRegion *Base = K.getConcreteOffsetRegion(); 793 const MemRegion *R = K.getRegion(); 794 795 while (R != Base) { 796 if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) 797 if (!isUnionField(FR)) 798 Fields.push_back(FR->getDecl()); 799 800 R = cast<SubRegion>(R)->getSuperRegion(); 801 } 802 } 803 804 static bool isCompatibleWithFields(BindingKey K, const FieldVector &Fields) { 805 assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys"); 806 807 if (Fields.empty()) 808 return true; 809 810 FieldVector FieldsInBindingKey; 811 getSymbolicOffsetFields(K, FieldsInBindingKey); 812 813 ptrdiff_t Delta = FieldsInBindingKey.size() - Fields.size(); 814 if (Delta >= 0) 815 return std::equal(FieldsInBindingKey.begin() + Delta, 816 FieldsInBindingKey.end(), 817 Fields.begin()); 818 else 819 return std::equal(FieldsInBindingKey.begin(), FieldsInBindingKey.end(), 820 Fields.begin() - Delta); 821 } 822 823 /// Collects all bindings in \p Cluster that may refer to bindings within 824 /// \p Top. 825 /// 826 /// Each binding is a pair whose \c first is the key (a BindingKey) and whose 827 /// \c second is the value (an SVal). 828 /// 829 /// The \p IncludeAllDefaultBindings parameter specifies whether to include 830 /// default bindings that may extend beyond \p Top itself, e.g. if \p Top is 831 /// an aggregate within a larger aggregate with a default binding. 832 static void 833 collectSubRegionBindings(SmallVectorImpl<BindingPair> &Bindings, 834 SValBuilder &SVB, const ClusterBindings &Cluster, 835 const SubRegion *Top, BindingKey TopKey, 836 bool IncludeAllDefaultBindings) { 837 FieldVector FieldsInSymbolicSubregions; 838 if (TopKey.hasSymbolicOffset()) { 839 getSymbolicOffsetFields(TopKey, FieldsInSymbolicSubregions); 840 Top = TopKey.getConcreteOffsetRegion(); 841 TopKey = BindingKey::Make(Top, BindingKey::Default); 842 } 843 844 // Find the length (in bits) of the region being invalidated. 845 uint64_t Length = UINT64_MAX; 846 SVal Extent = Top->getExtent(SVB); 847 if (Optional<nonloc::ConcreteInt> ExtentCI = 848 Extent.getAs<nonloc::ConcreteInt>()) { 849 const llvm::APSInt &ExtentInt = ExtentCI->getValue(); 850 assert(ExtentInt.isNonNegative() || ExtentInt.isUnsigned()); 851 // Extents are in bytes but region offsets are in bits. Be careful! 852 Length = ExtentInt.getLimitedValue() * SVB.getContext().getCharWidth(); 853 } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(Top)) { 854 if (FR->getDecl()->isBitField()) 855 Length = FR->getDecl()->getBitWidthValue(SVB.getContext()); 856 } 857 858 for (ClusterBindings::iterator I = Cluster.begin(), E = Cluster.end(); 859 I != E; ++I) { 860 BindingKey NextKey = I.getKey(); 861 if (NextKey.getRegion() == TopKey.getRegion()) { 862 // FIXME: This doesn't catch the case where we're really invalidating a 863 // region with a symbolic offset. Example: 864 // R: points[i].y 865 // Next: points[0].x 866 867 if (NextKey.getOffset() > TopKey.getOffset() && 868 NextKey.getOffset() - TopKey.getOffset() < Length) { 869 // Case 1: The next binding is inside the region we're invalidating. 870 // Include it. 871 Bindings.push_back(*I); 872 873 } else if (NextKey.getOffset() == TopKey.getOffset()) { 874 // Case 2: The next binding is at the same offset as the region we're 875 // invalidating. In this case, we need to leave default bindings alone, 876 // since they may be providing a default value for a regions beyond what 877 // we're invalidating. 878 // FIXME: This is probably incorrect; consider invalidating an outer 879 // struct whose first field is bound to a LazyCompoundVal. 880 if (IncludeAllDefaultBindings || NextKey.isDirect()) 881 Bindings.push_back(*I); 882 } 883 884 } else if (NextKey.hasSymbolicOffset()) { 885 const MemRegion *Base = NextKey.getConcreteOffsetRegion(); 886 if (Top->isSubRegionOf(Base) && Top != Base) { 887 // Case 3: The next key is symbolic and we just changed something within 888 // its concrete region. We don't know if the binding is still valid, so 889 // we'll be conservative and include it. 890 if (IncludeAllDefaultBindings || NextKey.isDirect()) 891 if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions)) 892 Bindings.push_back(*I); 893 } else if (const SubRegion *BaseSR = dyn_cast<SubRegion>(Base)) { 894 // Case 4: The next key is symbolic, but we changed a known 895 // super-region. In this case the binding is certainly included. 896 if (BaseSR->isSubRegionOf(Top)) 897 if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions)) 898 Bindings.push_back(*I); 899 } 900 } 901 } 902 } 903 904 static void 905 collectSubRegionBindings(SmallVectorImpl<BindingPair> &Bindings, 906 SValBuilder &SVB, const ClusterBindings &Cluster, 907 const SubRegion *Top, bool IncludeAllDefaultBindings) { 908 collectSubRegionBindings(Bindings, SVB, Cluster, Top, 909 BindingKey::Make(Top, BindingKey::Default), 910 IncludeAllDefaultBindings); 911 } 912 913 RegionBindingsRef 914 RegionStoreManager::removeSubRegionBindings(RegionBindingsConstRef B, 915 const SubRegion *Top) { 916 BindingKey TopKey = BindingKey::Make(Top, BindingKey::Default); 917 const MemRegion *ClusterHead = TopKey.getBaseRegion(); 918 919 if (Top == ClusterHead) { 920 // We can remove an entire cluster's bindings all in one go. 921 return B.remove(Top); 922 } 923 924 const ClusterBindings *Cluster = B.lookup(ClusterHead); 925 if (!Cluster) { 926 // If we're invalidating a region with a symbolic offset, we need to make 927 // sure we don't treat the base region as uninitialized anymore. 928 if (TopKey.hasSymbolicOffset()) { 929 const SubRegion *Concrete = TopKey.getConcreteOffsetRegion(); 930 return B.addBinding(Concrete, BindingKey::Default, UnknownVal()); 931 } 932 return B; 933 } 934 935 SmallVector<BindingPair, 32> Bindings; 936 collectSubRegionBindings(Bindings, svalBuilder, *Cluster, Top, TopKey, 937 /*IncludeAllDefaultBindings=*/false); 938 939 ClusterBindingsRef Result(*Cluster, CBFactory); 940 for (SmallVectorImpl<BindingPair>::const_iterator I = Bindings.begin(), 941 E = Bindings.end(); 942 I != E; ++I) 943 Result = Result.remove(I->first); 944 945 // If we're invalidating a region with a symbolic offset, we need to make sure 946 // we don't treat the base region as uninitialized anymore. 947 // FIXME: This isn't very precise; see the example in 948 // collectSubRegionBindings. 949 if (TopKey.hasSymbolicOffset()) { 950 const SubRegion *Concrete = TopKey.getConcreteOffsetRegion(); 951 Result = Result.add(BindingKey::Make(Concrete, BindingKey::Default), 952 UnknownVal()); 953 } 954 955 if (Result.isEmpty()) 956 return B.remove(ClusterHead); 957 return B.add(ClusterHead, Result.asImmutableMap()); 958 } 959 960 namespace { 961 class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker> 962 { 963 const Expr *Ex; 964 unsigned Count; 965 const LocationContext *LCtx; 966 InvalidatedSymbols &IS; 967 RegionAndSymbolInvalidationTraits &ITraits; 968 StoreManager::InvalidatedRegions *Regions; 969 GlobalsFilterKind GlobalsFilter; 970 public: 971 InvalidateRegionsWorker(RegionStoreManager &rm, 972 ProgramStateManager &stateMgr, 973 RegionBindingsRef b, 974 const Expr *ex, unsigned count, 975 const LocationContext *lctx, 976 InvalidatedSymbols &is, 977 RegionAndSymbolInvalidationTraits &ITraitsIn, 978 StoreManager::InvalidatedRegions *r, 979 GlobalsFilterKind GFK) 980 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b), 981 Ex(ex), Count(count), LCtx(lctx), IS(is), ITraits(ITraitsIn), Regions(r), 982 GlobalsFilter(GFK) {} 983 984 void VisitCluster(const MemRegion *baseR, const ClusterBindings *C); 985 void VisitBinding(SVal V); 986 987 using ClusterAnalysis::AddToWorkList; 988 989 bool AddToWorkList(const MemRegion *R); 990 991 /// Returns true if all clusters in the memory space for \p Base should be 992 /// be invalidated. 993 bool includeEntireMemorySpace(const MemRegion *Base); 994 995 /// Returns true if the memory space of the given region is one of the global 996 /// regions specially included at the start of invalidation. 997 bool isInitiallyIncludedGlobalRegion(const MemRegion *R); 998 }; 999 } 1000 1001 bool InvalidateRegionsWorker::AddToWorkList(const MemRegion *R) { 1002 bool doNotInvalidateSuperRegion = ITraits.hasTrait( 1003 R, RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion); 1004 const MemRegion *BaseR = doNotInvalidateSuperRegion ? R : R->getBaseRegion(); 1005 return AddToWorkList(WorkListElement(BaseR), getCluster(BaseR)); 1006 } 1007 1008 void InvalidateRegionsWorker::VisitBinding(SVal V) { 1009 // A symbol? Mark it touched by the invalidation. 1010 if (SymbolRef Sym = V.getAsSymbol()) 1011 IS.insert(Sym); 1012 1013 if (const MemRegion *R = V.getAsRegion()) { 1014 AddToWorkList(R); 1015 return; 1016 } 1017 1018 // Is it a LazyCompoundVal? All references get invalidated as well. 1019 if (Optional<nonloc::LazyCompoundVal> LCS = 1020 V.getAs<nonloc::LazyCompoundVal>()) { 1021 1022 const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS); 1023 1024 for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(), 1025 E = Vals.end(); 1026 I != E; ++I) 1027 VisitBinding(*I); 1028 1029 return; 1030 } 1031 } 1032 1033 void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR, 1034 const ClusterBindings *C) { 1035 1036 bool PreserveRegionsContents = 1037 ITraits.hasTrait(baseR, 1038 RegionAndSymbolInvalidationTraits::TK_PreserveContents); 1039 1040 if (C) { 1041 for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E; ++I) 1042 VisitBinding(I.getData()); 1043 1044 // Invalidate regions contents. 1045 if (!PreserveRegionsContents) 1046 B = B.remove(baseR); 1047 } 1048 1049 if (const auto *TO = dyn_cast<TypedValueRegion>(baseR)) { 1050 if (const auto *RD = TO->getValueType()->getAsCXXRecordDecl()) { 1051 1052 // Lambdas can affect all static local variables without explicitly 1053 // capturing those. 1054 // We invalidate all static locals referenced inside the lambda body. 1055 if (RD->isLambda() && RD->getLambdaCallOperator()->getBody()) { 1056 using namespace ast_matchers; 1057 1058 const char *DeclBind = "DeclBind"; 1059 StatementMatcher RefToStatic = stmt(hasDescendant(declRefExpr( 1060 to(varDecl(hasStaticStorageDuration()).bind(DeclBind))))); 1061 auto Matches = 1062 match(RefToStatic, *RD->getLambdaCallOperator()->getBody(), 1063 RD->getASTContext()); 1064 1065 for (BoundNodes &Match : Matches) { 1066 auto *VD = Match.getNodeAs<VarDecl>(DeclBind); 1067 const VarRegion *ToInvalidate = 1068 RM.getRegionManager().getVarRegion(VD, LCtx); 1069 AddToWorkList(ToInvalidate); 1070 } 1071 } 1072 } 1073 } 1074 1075 // BlockDataRegion? If so, invalidate captured variables that are passed 1076 // by reference. 1077 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) { 1078 for (BlockDataRegion::referenced_vars_iterator 1079 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ; 1080 BI != BE; ++BI) { 1081 const VarRegion *VR = BI.getCapturedRegion(); 1082 const VarDecl *VD = VR->getDecl(); 1083 if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage()) { 1084 AddToWorkList(VR); 1085 } 1086 else if (Loc::isLocType(VR->getValueType())) { 1087 // Map the current bindings to a Store to retrieve the value 1088 // of the binding. If that binding itself is a region, we should 1089 // invalidate that region. This is because a block may capture 1090 // a pointer value, but the thing pointed by that pointer may 1091 // get invalidated. 1092 SVal V = RM.getBinding(B, loc::MemRegionVal(VR)); 1093 if (Optional<Loc> L = V.getAs<Loc>()) { 1094 if (const MemRegion *LR = L->getAsRegion()) 1095 AddToWorkList(LR); 1096 } 1097 } 1098 } 1099 return; 1100 } 1101 1102 // Symbolic region? 1103 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) 1104 IS.insert(SR->getSymbol()); 1105 1106 // Nothing else should be done in the case when we preserve regions context. 1107 if (PreserveRegionsContents) 1108 return; 1109 1110 // Otherwise, we have a normal data region. Record that we touched the region. 1111 if (Regions) 1112 Regions->push_back(baseR); 1113 1114 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) { 1115 // Invalidate the region by setting its default value to 1116 // conjured symbol. The type of the symbol is irrelevant. 1117 DefinedOrUnknownSVal V = 1118 svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, Ctx.IntTy, Count); 1119 B = B.addBinding(baseR, BindingKey::Default, V); 1120 return; 1121 } 1122 1123 if (!baseR->isBoundable()) 1124 return; 1125 1126 const TypedValueRegion *TR = cast<TypedValueRegion>(baseR); 1127 QualType T = TR->getValueType(); 1128 1129 if (isInitiallyIncludedGlobalRegion(baseR)) { 1130 // If the region is a global and we are invalidating all globals, 1131 // erasing the entry is good enough. This causes all globals to be lazily 1132 // symbolicated from the same base symbol. 1133 return; 1134 } 1135 1136 if (T->isRecordType()) { 1137 // Invalidate the region by setting its default value to 1138 // conjured symbol. The type of the symbol is irrelevant. 1139 DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, 1140 Ctx.IntTy, Count); 1141 B = B.addBinding(baseR, BindingKey::Default, V); 1142 return; 1143 } 1144 1145 if (const ArrayType *AT = Ctx.getAsArrayType(T)) { 1146 bool doNotInvalidateSuperRegion = ITraits.hasTrait( 1147 baseR, 1148 RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion); 1149 1150 if (doNotInvalidateSuperRegion) { 1151 // We are not doing blank invalidation of the whole array region so we 1152 // have to manually invalidate each elements. 1153 Optional<uint64_t> NumElements; 1154 1155 // Compute lower and upper offsets for region within array. 1156 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 1157 NumElements = CAT->getSize().getZExtValue(); 1158 if (!NumElements) // We are not dealing with a constant size array 1159 goto conjure_default; 1160 QualType ElementTy = AT->getElementType(); 1161 uint64_t ElemSize = Ctx.getTypeSize(ElementTy); 1162 const RegionOffset &RO = baseR->getAsOffset(); 1163 const MemRegion *SuperR = baseR->getBaseRegion(); 1164 if (RO.hasSymbolicOffset()) { 1165 // If base region has a symbolic offset, 1166 // we revert to invalidating the super region. 1167 if (SuperR) 1168 AddToWorkList(SuperR); 1169 goto conjure_default; 1170 } 1171 1172 uint64_t LowerOffset = RO.getOffset(); 1173 uint64_t UpperOffset = LowerOffset + *NumElements * ElemSize; 1174 bool UpperOverflow = UpperOffset < LowerOffset; 1175 1176 // Invalidate regions which are within array boundaries, 1177 // or have a symbolic offset. 1178 if (!SuperR) 1179 goto conjure_default; 1180 1181 const ClusterBindings *C = B.lookup(SuperR); 1182 if (!C) 1183 goto conjure_default; 1184 1185 for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E; 1186 ++I) { 1187 const BindingKey &BK = I.getKey(); 1188 Optional<uint64_t> ROffset = 1189 BK.hasSymbolicOffset() ? Optional<uint64_t>() : BK.getOffset(); 1190 1191 // Check offset is not symbolic and within array's boundaries. 1192 // Handles arrays of 0 elements and of 0-sized elements as well. 1193 if (!ROffset || 1194 ((*ROffset >= LowerOffset && *ROffset < UpperOffset) || 1195 (UpperOverflow && 1196 (*ROffset >= LowerOffset || *ROffset < UpperOffset)) || 1197 (LowerOffset == UpperOffset && *ROffset == LowerOffset))) { 1198 B = B.removeBinding(I.getKey()); 1199 // Bound symbolic regions need to be invalidated for dead symbol 1200 // detection. 1201 SVal V = I.getData(); 1202 const MemRegion *R = V.getAsRegion(); 1203 if (R && isa<SymbolicRegion>(R)) 1204 VisitBinding(V); 1205 } 1206 } 1207 } 1208 conjure_default: 1209 // Set the default value of the array to conjured symbol. 1210 DefinedOrUnknownSVal V = 1211 svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, 1212 AT->getElementType(), Count); 1213 B = B.addBinding(baseR, BindingKey::Default, V); 1214 return; 1215 } 1216 1217 DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, 1218 T,Count); 1219 assert(SymbolManager::canSymbolicate(T) || V.isUnknown()); 1220 B = B.addBinding(baseR, BindingKey::Direct, V); 1221 } 1222 1223 bool InvalidateRegionsWorker::isInitiallyIncludedGlobalRegion( 1224 const MemRegion *R) { 1225 switch (GlobalsFilter) { 1226 case GFK_None: 1227 return false; 1228 case GFK_SystemOnly: 1229 return isa<GlobalSystemSpaceRegion>(R->getMemorySpace()); 1230 case GFK_All: 1231 return isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace()); 1232 } 1233 1234 llvm_unreachable("unknown globals filter"); 1235 } 1236 1237 bool InvalidateRegionsWorker::includeEntireMemorySpace(const MemRegion *Base) { 1238 if (isInitiallyIncludedGlobalRegion(Base)) 1239 return true; 1240 1241 const MemSpaceRegion *MemSpace = Base->getMemorySpace(); 1242 return ITraits.hasTrait(MemSpace, 1243 RegionAndSymbolInvalidationTraits::TK_EntireMemSpace); 1244 } 1245 1246 RegionBindingsRef 1247 RegionStoreManager::invalidateGlobalRegion(MemRegion::Kind K, 1248 const Expr *Ex, 1249 unsigned Count, 1250 const LocationContext *LCtx, 1251 RegionBindingsRef B, 1252 InvalidatedRegions *Invalidated) { 1253 // Bind the globals memory space to a new symbol that we will use to derive 1254 // the bindings for all globals. 1255 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion(K); 1256 SVal V = svalBuilder.conjureSymbolVal(/* SymbolTag = */ (const void*) GS, Ex, LCtx, 1257 /* type does not matter */ Ctx.IntTy, 1258 Count); 1259 1260 B = B.removeBinding(GS) 1261 .addBinding(BindingKey::Make(GS, BindingKey::Default), V); 1262 1263 // Even if there are no bindings in the global scope, we still need to 1264 // record that we touched it. 1265 if (Invalidated) 1266 Invalidated->push_back(GS); 1267 1268 return B; 1269 } 1270 1271 void RegionStoreManager::populateWorkList(InvalidateRegionsWorker &W, 1272 ArrayRef<SVal> Values, 1273 InvalidatedRegions *TopLevelRegions) { 1274 for (ArrayRef<SVal>::iterator I = Values.begin(), 1275 E = Values.end(); I != E; ++I) { 1276 SVal V = *I; 1277 if (Optional<nonloc::LazyCompoundVal> LCS = 1278 V.getAs<nonloc::LazyCompoundVal>()) { 1279 1280 const SValListTy &Vals = getInterestingValues(*LCS); 1281 1282 for (SValListTy::const_iterator I = Vals.begin(), 1283 E = Vals.end(); I != E; ++I) { 1284 // Note: the last argument is false here because these are 1285 // non-top-level regions. 1286 if (const MemRegion *R = (*I).getAsRegion()) 1287 W.AddToWorkList(R); 1288 } 1289 continue; 1290 } 1291 1292 if (const MemRegion *R = V.getAsRegion()) { 1293 if (TopLevelRegions) 1294 TopLevelRegions->push_back(R); 1295 W.AddToWorkList(R); 1296 continue; 1297 } 1298 } 1299 } 1300 1301 StoreRef 1302 RegionStoreManager::invalidateRegions(Store store, 1303 ArrayRef<SVal> Values, 1304 const Expr *Ex, unsigned Count, 1305 const LocationContext *LCtx, 1306 const CallEvent *Call, 1307 InvalidatedSymbols &IS, 1308 RegionAndSymbolInvalidationTraits &ITraits, 1309 InvalidatedRegions *TopLevelRegions, 1310 InvalidatedRegions *Invalidated) { 1311 GlobalsFilterKind GlobalsFilter; 1312 if (Call) { 1313 if (Call->isInSystemHeader()) 1314 GlobalsFilter = GFK_SystemOnly; 1315 else 1316 GlobalsFilter = GFK_All; 1317 } else { 1318 GlobalsFilter = GFK_None; 1319 } 1320 1321 RegionBindingsRef B = getRegionBindings(store); 1322 InvalidateRegionsWorker W(*this, StateMgr, B, Ex, Count, LCtx, IS, ITraits, 1323 Invalidated, GlobalsFilter); 1324 1325 // Scan the bindings and generate the clusters. 1326 W.GenerateClusters(); 1327 1328 // Add the regions to the worklist. 1329 populateWorkList(W, Values, TopLevelRegions); 1330 1331 W.RunWorkList(); 1332 1333 // Return the new bindings. 1334 B = W.getRegionBindings(); 1335 1336 // For calls, determine which global regions should be invalidated and 1337 // invalidate them. (Note that function-static and immutable globals are never 1338 // invalidated by this.) 1339 // TODO: This could possibly be more precise with modules. 1340 switch (GlobalsFilter) { 1341 case GFK_All: 1342 B = invalidateGlobalRegion(MemRegion::GlobalInternalSpaceRegionKind, 1343 Ex, Count, LCtx, B, Invalidated); 1344 LLVM_FALLTHROUGH; 1345 case GFK_SystemOnly: 1346 B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind, 1347 Ex, Count, LCtx, B, Invalidated); 1348 LLVM_FALLTHROUGH; 1349 case GFK_None: 1350 break; 1351 } 1352 1353 return StoreRef(B.asStore(), *this); 1354 } 1355 1356 //===----------------------------------------------------------------------===// 1357 // Extents for regions. 1358 //===----------------------------------------------------------------------===// 1359 1360 DefinedOrUnknownSVal 1361 RegionStoreManager::getSizeInElements(ProgramStateRef state, 1362 const MemRegion *R, 1363 QualType EleTy) { 1364 SVal Size = cast<SubRegion>(R)->getExtent(svalBuilder); 1365 const llvm::APSInt *SizeInt = svalBuilder.getKnownValue(state, Size); 1366 if (!SizeInt) 1367 return UnknownVal(); 1368 1369 CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue()); 1370 1371 if (Ctx.getAsVariableArrayType(EleTy)) { 1372 // FIXME: We need to track extra state to properly record the size 1373 // of VLAs. Returning UnknownVal here, however, is a stop-gap so that 1374 // we don't have a divide-by-zero below. 1375 return UnknownVal(); 1376 } 1377 1378 CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy); 1379 1380 // If a variable is reinterpreted as a type that doesn't fit into a larger 1381 // type evenly, round it down. 1382 // This is a signed value, since it's used in arithmetic with signed indices. 1383 return svalBuilder.makeIntVal(RegionSize / EleSize, 1384 svalBuilder.getArrayIndexType()); 1385 } 1386 1387 //===----------------------------------------------------------------------===// 1388 // Location and region casting. 1389 //===----------------------------------------------------------------------===// 1390 1391 /// ArrayToPointer - Emulates the "decay" of an array to a pointer 1392 /// type. 'Array' represents the lvalue of the array being decayed 1393 /// to a pointer, and the returned SVal represents the decayed 1394 /// version of that lvalue (i.e., a pointer to the first element of 1395 /// the array). This is called by ExprEngine when evaluating casts 1396 /// from arrays to pointers. 1397 SVal RegionStoreManager::ArrayToPointer(Loc Array, QualType T) { 1398 if (Array.getAs<loc::ConcreteInt>()) 1399 return Array; 1400 1401 if (!Array.getAs<loc::MemRegionVal>()) 1402 return UnknownVal(); 1403 1404 const SubRegion *R = 1405 cast<SubRegion>(Array.castAs<loc::MemRegionVal>().getRegion()); 1406 NonLoc ZeroIdx = svalBuilder.makeZeroArrayIndex(); 1407 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, R, Ctx)); 1408 } 1409 1410 //===----------------------------------------------------------------------===// 1411 // Loading values from regions. 1412 //===----------------------------------------------------------------------===// 1413 1414 SVal RegionStoreManager::getBinding(RegionBindingsConstRef B, Loc L, QualType T) { 1415 assert(!L.getAs<UnknownVal>() && "location unknown"); 1416 assert(!L.getAs<UndefinedVal>() && "location undefined"); 1417 1418 // For access to concrete addresses, return UnknownVal. Checks 1419 // for null dereferences (and similar errors) are done by checkers, not 1420 // the Store. 1421 // FIXME: We can consider lazily symbolicating such memory, but we really 1422 // should defer this when we can reason easily about symbolicating arrays 1423 // of bytes. 1424 if (L.getAs<loc::ConcreteInt>()) { 1425 return UnknownVal(); 1426 } 1427 if (!L.getAs<loc::MemRegionVal>()) { 1428 return UnknownVal(); 1429 } 1430 1431 const MemRegion *MR = L.castAs<loc::MemRegionVal>().getRegion(); 1432 1433 if (isa<BlockDataRegion>(MR)) { 1434 return UnknownVal(); 1435 } 1436 1437 if (!isa<TypedValueRegion>(MR)) { 1438 if (T.isNull()) { 1439 if (const TypedRegion *TR = dyn_cast<TypedRegion>(MR)) 1440 T = TR->getLocationType()->getPointeeType(); 1441 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR)) 1442 T = SR->getSymbol()->getType()->getPointeeType(); 1443 } 1444 assert(!T.isNull() && "Unable to auto-detect binding type!"); 1445 assert(!T->isVoidType() && "Attempting to dereference a void pointer!"); 1446 MR = GetElementZeroRegion(cast<SubRegion>(MR), T); 1447 } else { 1448 T = cast<TypedValueRegion>(MR)->getValueType(); 1449 } 1450 1451 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument 1452 // instead of 'Loc', and have the other Loc cases handled at a higher level. 1453 const TypedValueRegion *R = cast<TypedValueRegion>(MR); 1454 QualType RTy = R->getValueType(); 1455 1456 // FIXME: we do not yet model the parts of a complex type, so treat the 1457 // whole thing as "unknown". 1458 if (RTy->isAnyComplexType()) 1459 return UnknownVal(); 1460 1461 // FIXME: We should eventually handle funny addressing. e.g.: 1462 // 1463 // int x = ...; 1464 // int *p = &x; 1465 // char *q = (char*) p; 1466 // char c = *q; // returns the first byte of 'x'. 1467 // 1468 // Such funny addressing will occur due to layering of regions. 1469 if (RTy->isStructureOrClassType()) 1470 return getBindingForStruct(B, R); 1471 1472 // FIXME: Handle unions. 1473 if (RTy->isUnionType()) 1474 return createLazyBinding(B, R); 1475 1476 if (RTy->isArrayType()) { 1477 if (RTy->isConstantArrayType()) 1478 return getBindingForArray(B, R); 1479 else 1480 return UnknownVal(); 1481 } 1482 1483 // FIXME: handle Vector types. 1484 if (RTy->isVectorType()) 1485 return UnknownVal(); 1486 1487 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) 1488 return CastRetrievedVal(getBindingForField(B, FR), FR, T); 1489 1490 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) { 1491 // FIXME: Here we actually perform an implicit conversion from the loaded 1492 // value to the element type. Eventually we want to compose these values 1493 // more intelligently. For example, an 'element' can encompass multiple 1494 // bound regions (e.g., several bound bytes), or could be a subset of 1495 // a larger value. 1496 return CastRetrievedVal(getBindingForElement(B, ER), ER, T); 1497 } 1498 1499 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) { 1500 // FIXME: Here we actually perform an implicit conversion from the loaded 1501 // value to the ivar type. What we should model is stores to ivars 1502 // that blow past the extent of the ivar. If the address of the ivar is 1503 // reinterpretted, it is possible we stored a different value that could 1504 // fit within the ivar. Either we need to cast these when storing them 1505 // or reinterpret them lazily (as we do here). 1506 return CastRetrievedVal(getBindingForObjCIvar(B, IVR), IVR, T); 1507 } 1508 1509 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { 1510 // FIXME: Here we actually perform an implicit conversion from the loaded 1511 // value to the variable type. What we should model is stores to variables 1512 // that blow past the extent of the variable. If the address of the 1513 // variable is reinterpretted, it is possible we stored a different value 1514 // that could fit within the variable. Either we need to cast these when 1515 // storing them or reinterpret them lazily (as we do here). 1516 return CastRetrievedVal(getBindingForVar(B, VR), VR, T); 1517 } 1518 1519 const SVal *V = B.lookup(R, BindingKey::Direct); 1520 1521 // Check if the region has a binding. 1522 if (V) 1523 return *V; 1524 1525 // The location does not have a bound value. This means that it has 1526 // the value it had upon its creation and/or entry to the analyzed 1527 // function/method. These are either symbolic values or 'undefined'. 1528 if (R->hasStackNonParametersStorage()) { 1529 // All stack variables are considered to have undefined values 1530 // upon creation. All heap allocated blocks are considered to 1531 // have undefined values as well unless they are explicitly bound 1532 // to specific values. 1533 return UndefinedVal(); 1534 } 1535 1536 // All other values are symbolic. 1537 return svalBuilder.getRegionValueSymbolVal(R); 1538 } 1539 1540 static QualType getUnderlyingType(const SubRegion *R) { 1541 QualType RegionTy; 1542 if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(R)) 1543 RegionTy = TVR->getValueType(); 1544 1545 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) 1546 RegionTy = SR->getSymbol()->getType(); 1547 1548 return RegionTy; 1549 } 1550 1551 /// Checks to see if store \p B has a lazy binding for region \p R. 1552 /// 1553 /// If \p AllowSubregionBindings is \c false, a lazy binding will be rejected 1554 /// if there are additional bindings within \p R. 1555 /// 1556 /// Note that unlike RegionStoreManager::findLazyBinding, this will not search 1557 /// for lazy bindings for super-regions of \p R. 1558 static Optional<nonloc::LazyCompoundVal> 1559 getExistingLazyBinding(SValBuilder &SVB, RegionBindingsConstRef B, 1560 const SubRegion *R, bool AllowSubregionBindings) { 1561 Optional<SVal> V = B.getDefaultBinding(R); 1562 if (!V) 1563 return None; 1564 1565 Optional<nonloc::LazyCompoundVal> LCV = V->getAs<nonloc::LazyCompoundVal>(); 1566 if (!LCV) 1567 return None; 1568 1569 // If the LCV is for a subregion, the types might not match, and we shouldn't 1570 // reuse the binding. 1571 QualType RegionTy = getUnderlyingType(R); 1572 if (!RegionTy.isNull() && 1573 !RegionTy->isVoidPointerType()) { 1574 QualType SourceRegionTy = LCV->getRegion()->getValueType(); 1575 if (!SVB.getContext().hasSameUnqualifiedType(RegionTy, SourceRegionTy)) 1576 return None; 1577 } 1578 1579 if (!AllowSubregionBindings) { 1580 // If there are any other bindings within this region, we shouldn't reuse 1581 // the top-level binding. 1582 SmallVector<BindingPair, 16> Bindings; 1583 collectSubRegionBindings(Bindings, SVB, *B.lookup(R->getBaseRegion()), R, 1584 /*IncludeAllDefaultBindings=*/true); 1585 if (Bindings.size() > 1) 1586 return None; 1587 } 1588 1589 return *LCV; 1590 } 1591 1592 1593 std::pair<Store, const SubRegion *> 1594 RegionStoreManager::findLazyBinding(RegionBindingsConstRef B, 1595 const SubRegion *R, 1596 const SubRegion *originalRegion) { 1597 if (originalRegion != R) { 1598 if (Optional<nonloc::LazyCompoundVal> V = 1599 getExistingLazyBinding(svalBuilder, B, R, true)) 1600 return std::make_pair(V->getStore(), V->getRegion()); 1601 } 1602 1603 typedef std::pair<Store, const SubRegion *> StoreRegionPair; 1604 StoreRegionPair Result = StoreRegionPair(); 1605 1606 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { 1607 Result = findLazyBinding(B, cast<SubRegion>(ER->getSuperRegion()), 1608 originalRegion); 1609 1610 if (Result.second) 1611 Result.second = MRMgr.getElementRegionWithSuper(ER, Result.second); 1612 1613 } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) { 1614 Result = findLazyBinding(B, cast<SubRegion>(FR->getSuperRegion()), 1615 originalRegion); 1616 1617 if (Result.second) 1618 Result.second = MRMgr.getFieldRegionWithSuper(FR, Result.second); 1619 1620 } else if (const CXXBaseObjectRegion *BaseReg = 1621 dyn_cast<CXXBaseObjectRegion>(R)) { 1622 // C++ base object region is another kind of region that we should blast 1623 // through to look for lazy compound value. It is like a field region. 1624 Result = findLazyBinding(B, cast<SubRegion>(BaseReg->getSuperRegion()), 1625 originalRegion); 1626 1627 if (Result.second) 1628 Result.second = MRMgr.getCXXBaseObjectRegionWithSuper(BaseReg, 1629 Result.second); 1630 } 1631 1632 return Result; 1633 } 1634 1635 SVal RegionStoreManager::getBindingForElement(RegionBindingsConstRef B, 1636 const ElementRegion* R) { 1637 // We do not currently model bindings of the CompoundLiteralregion. 1638 if (isa<CompoundLiteralRegion>(R->getBaseRegion())) 1639 return UnknownVal(); 1640 1641 // Check if the region has a binding. 1642 if (const Optional<SVal> &V = B.getDirectBinding(R)) 1643 return *V; 1644 1645 const MemRegion* superR = R->getSuperRegion(); 1646 1647 // Check if the region is an element region of a string literal. 1648 if (const StringRegion *StrR = dyn_cast<StringRegion>(superR)) { 1649 // FIXME: Handle loads from strings where the literal is treated as 1650 // an integer, e.g., *((unsigned int*)"hello") 1651 QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType(); 1652 if (!Ctx.hasSameUnqualifiedType(T, R->getElementType())) 1653 return UnknownVal(); 1654 1655 const StringLiteral *Str = StrR->getStringLiteral(); 1656 SVal Idx = R->getIndex(); 1657 if (Optional<nonloc::ConcreteInt> CI = Idx.getAs<nonloc::ConcreteInt>()) { 1658 int64_t i = CI->getValue().getSExtValue(); 1659 // Abort on string underrun. This can be possible by arbitrary 1660 // clients of getBindingForElement(). 1661 if (i < 0) 1662 return UndefinedVal(); 1663 int64_t length = Str->getLength(); 1664 // Technically, only i == length is guaranteed to be null. 1665 // However, such overflows should be caught before reaching this point; 1666 // the only time such an access would be made is if a string literal was 1667 // used to initialize a larger array. 1668 char c = (i >= length) ? '\0' : Str->getCodeUnit(i); 1669 return svalBuilder.makeIntVal(c, T); 1670 } 1671 } else if (const VarRegion *VR = dyn_cast<VarRegion>(superR)) { 1672 // Check if the containing array is const and has an initialized value. 1673 const VarDecl *VD = VR->getDecl(); 1674 // Either the array or the array element has to be const. 1675 if (VD->getType().isConstQualified() || R->getElementType().isConstQualified()) { 1676 if (const Expr *Init = VD->getAnyInitializer()) { 1677 if (const auto *InitList = dyn_cast<InitListExpr>(Init)) { 1678 // The array index has to be known. 1679 if (auto CI = R->getIndex().getAs<nonloc::ConcreteInt>()) { 1680 int64_t i = CI->getValue().getSExtValue(); 1681 // If it is known that the index is out of bounds, we can return 1682 // an undefined value. 1683 if (i < 0) 1684 return UndefinedVal(); 1685 1686 if (auto CAT = Ctx.getAsConstantArrayType(VD->getType())) 1687 if (CAT->getSize().sle(i)) 1688 return UndefinedVal(); 1689 1690 // If there is a list, but no init, it must be zero. 1691 if (i >= InitList->getNumInits()) 1692 return svalBuilder.makeZeroVal(R->getElementType()); 1693 1694 if (const Expr *ElemInit = InitList->getInit(i)) 1695 if (Optional<SVal> V = svalBuilder.getConstantVal(ElemInit)) 1696 return *V; 1697 } 1698 } 1699 } 1700 } 1701 } 1702 1703 // Check for loads from a code text region. For such loads, just give up. 1704 if (isa<CodeTextRegion>(superR)) 1705 return UnknownVal(); 1706 1707 // Handle the case where we are indexing into a larger scalar object. 1708 // For example, this handles: 1709 // int x = ... 1710 // char *y = &x; 1711 // return *y; 1712 // FIXME: This is a hack, and doesn't do anything really intelligent yet. 1713 const RegionRawOffset &O = R->getAsArrayOffset(); 1714 1715 // If we cannot reason about the offset, return an unknown value. 1716 if (!O.getRegion()) 1717 return UnknownVal(); 1718 1719 if (const TypedValueRegion *baseR = 1720 dyn_cast_or_null<TypedValueRegion>(O.getRegion())) { 1721 QualType baseT = baseR->getValueType(); 1722 if (baseT->isScalarType()) { 1723 QualType elemT = R->getElementType(); 1724 if (elemT->isScalarType()) { 1725 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) { 1726 if (const Optional<SVal> &V = B.getDirectBinding(superR)) { 1727 if (SymbolRef parentSym = V->getAsSymbol()) 1728 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R); 1729 1730 if (V->isUnknownOrUndef()) 1731 return *V; 1732 // Other cases: give up. We are indexing into a larger object 1733 // that has some value, but we don't know how to handle that yet. 1734 return UnknownVal(); 1735 } 1736 } 1737 } 1738 } 1739 } 1740 return getBindingForFieldOrElementCommon(B, R, R->getElementType()); 1741 } 1742 1743 SVal RegionStoreManager::getBindingForField(RegionBindingsConstRef B, 1744 const FieldRegion* R) { 1745 1746 // Check if the region has a binding. 1747 if (const Optional<SVal> &V = B.getDirectBinding(R)) 1748 return *V; 1749 1750 // Is the field declared constant and has an in-class initializer? 1751 const FieldDecl *FD = R->getDecl(); 1752 QualType Ty = FD->getType(); 1753 if (Ty.isConstQualified()) 1754 if (const Expr *Init = FD->getInClassInitializer()) 1755 if (Optional<SVal> V = svalBuilder.getConstantVal(Init)) 1756 return *V; 1757 1758 // If the containing record was initialized, try to get its constant value. 1759 const MemRegion* superR = R->getSuperRegion(); 1760 if (const auto *VR = dyn_cast<VarRegion>(superR)) { 1761 const VarDecl *VD = VR->getDecl(); 1762 QualType RecordVarTy = VD->getType(); 1763 unsigned Index = FD->getFieldIndex(); 1764 // Either the record variable or the field has to be const qualified. 1765 if (RecordVarTy.isConstQualified() || Ty.isConstQualified()) 1766 if (const Expr *Init = VD->getAnyInitializer()) 1767 if (const auto *InitList = dyn_cast<InitListExpr>(Init)) { 1768 if (Index < InitList->getNumInits()) { 1769 if (const Expr *FieldInit = InitList->getInit(Index)) 1770 if (Optional<SVal> V = svalBuilder.getConstantVal(FieldInit)) 1771 return *V; 1772 } else { 1773 return svalBuilder.makeZeroVal(Ty); 1774 } 1775 } 1776 } 1777 1778 return getBindingForFieldOrElementCommon(B, R, Ty); 1779 } 1780 1781 Optional<SVal> 1782 RegionStoreManager::getBindingForDerivedDefaultValue(RegionBindingsConstRef B, 1783 const MemRegion *superR, 1784 const TypedValueRegion *R, 1785 QualType Ty) { 1786 1787 if (const Optional<SVal> &D = B.getDefaultBinding(superR)) { 1788 const SVal &val = D.getValue(); 1789 if (SymbolRef parentSym = val.getAsSymbol()) 1790 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R); 1791 1792 if (val.isZeroConstant()) 1793 return svalBuilder.makeZeroVal(Ty); 1794 1795 if (val.isUnknownOrUndef()) 1796 return val; 1797 1798 // Lazy bindings are usually handled through getExistingLazyBinding(). 1799 // We should unify these two code paths at some point. 1800 if (val.getAs<nonloc::LazyCompoundVal>() || 1801 val.getAs<nonloc::CompoundVal>()) 1802 return val; 1803 1804 llvm_unreachable("Unknown default value"); 1805 } 1806 1807 return None; 1808 } 1809 1810 SVal RegionStoreManager::getLazyBinding(const SubRegion *LazyBindingRegion, 1811 RegionBindingsRef LazyBinding) { 1812 SVal Result; 1813 if (const ElementRegion *ER = dyn_cast<ElementRegion>(LazyBindingRegion)) 1814 Result = getBindingForElement(LazyBinding, ER); 1815 else 1816 Result = getBindingForField(LazyBinding, 1817 cast<FieldRegion>(LazyBindingRegion)); 1818 1819 // FIXME: This is a hack to deal with RegionStore's inability to distinguish a 1820 // default value for /part/ of an aggregate from a default value for the 1821 // /entire/ aggregate. The most common case of this is when struct Outer 1822 // has as its first member a struct Inner, which is copied in from a stack 1823 // variable. In this case, even if the Outer's default value is symbolic, 0, 1824 // or unknown, it gets overridden by the Inner's default value of undefined. 1825 // 1826 // This is a general problem -- if the Inner is zero-initialized, the Outer 1827 // will now look zero-initialized. The proper way to solve this is with a 1828 // new version of RegionStore that tracks the extent of a binding as well 1829 // as the offset. 1830 // 1831 // This hack only takes care of the undefined case because that can very 1832 // quickly result in a warning. 1833 if (Result.isUndef()) 1834 Result = UnknownVal(); 1835 1836 return Result; 1837 } 1838 1839 SVal 1840 RegionStoreManager::getBindingForFieldOrElementCommon(RegionBindingsConstRef B, 1841 const TypedValueRegion *R, 1842 QualType Ty) { 1843 1844 // At this point we have already checked in either getBindingForElement or 1845 // getBindingForField if 'R' has a direct binding. 1846 1847 // Lazy binding? 1848 Store lazyBindingStore = nullptr; 1849 const SubRegion *lazyBindingRegion = nullptr; 1850 std::tie(lazyBindingStore, lazyBindingRegion) = findLazyBinding(B, R, R); 1851 if (lazyBindingRegion) 1852 return getLazyBinding(lazyBindingRegion, 1853 getRegionBindings(lazyBindingStore)); 1854 1855 // Record whether or not we see a symbolic index. That can completely 1856 // be out of scope of our lookup. 1857 bool hasSymbolicIndex = false; 1858 1859 // FIXME: This is a hack to deal with RegionStore's inability to distinguish a 1860 // default value for /part/ of an aggregate from a default value for the 1861 // /entire/ aggregate. The most common case of this is when struct Outer 1862 // has as its first member a struct Inner, which is copied in from a stack 1863 // variable. In this case, even if the Outer's default value is symbolic, 0, 1864 // or unknown, it gets overridden by the Inner's default value of undefined. 1865 // 1866 // This is a general problem -- if the Inner is zero-initialized, the Outer 1867 // will now look zero-initialized. The proper way to solve this is with a 1868 // new version of RegionStore that tracks the extent of a binding as well 1869 // as the offset. 1870 // 1871 // This hack only takes care of the undefined case because that can very 1872 // quickly result in a warning. 1873 bool hasPartialLazyBinding = false; 1874 1875 const SubRegion *SR = R; 1876 while (SR) { 1877 const MemRegion *Base = SR->getSuperRegion(); 1878 if (Optional<SVal> D = getBindingForDerivedDefaultValue(B, Base, R, Ty)) { 1879 if (D->getAs<nonloc::LazyCompoundVal>()) { 1880 hasPartialLazyBinding = true; 1881 break; 1882 } 1883 1884 return *D; 1885 } 1886 1887 if (const ElementRegion *ER = dyn_cast<ElementRegion>(Base)) { 1888 NonLoc index = ER->getIndex(); 1889 if (!index.isConstant()) 1890 hasSymbolicIndex = true; 1891 } 1892 1893 // If our super region is a field or element itself, walk up the region 1894 // hierarchy to see if there is a default value installed in an ancestor. 1895 SR = dyn_cast<SubRegion>(Base); 1896 } 1897 1898 if (R->hasStackNonParametersStorage()) { 1899 if (isa<ElementRegion>(R)) { 1900 // Currently we don't reason specially about Clang-style vectors. Check 1901 // if superR is a vector and if so return Unknown. 1902 if (const TypedValueRegion *typedSuperR = 1903 dyn_cast<TypedValueRegion>(R->getSuperRegion())) { 1904 if (typedSuperR->getValueType()->isVectorType()) 1905 return UnknownVal(); 1906 } 1907 } 1908 1909 // FIXME: We also need to take ElementRegions with symbolic indexes into 1910 // account. This case handles both directly accessing an ElementRegion 1911 // with a symbolic offset, but also fields within an element with 1912 // a symbolic offset. 1913 if (hasSymbolicIndex) 1914 return UnknownVal(); 1915 1916 if (!hasPartialLazyBinding) 1917 return UndefinedVal(); 1918 } 1919 1920 // All other values are symbolic. 1921 return svalBuilder.getRegionValueSymbolVal(R); 1922 } 1923 1924 SVal RegionStoreManager::getBindingForObjCIvar(RegionBindingsConstRef B, 1925 const ObjCIvarRegion* R) { 1926 // Check if the region has a binding. 1927 if (const Optional<SVal> &V = B.getDirectBinding(R)) 1928 return *V; 1929 1930 const MemRegion *superR = R->getSuperRegion(); 1931 1932 // Check if the super region has a default binding. 1933 if (const Optional<SVal> &V = B.getDefaultBinding(superR)) { 1934 if (SymbolRef parentSym = V->getAsSymbol()) 1935 return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R); 1936 1937 // Other cases: give up. 1938 return UnknownVal(); 1939 } 1940 1941 return getBindingForLazySymbol(R); 1942 } 1943 1944 SVal RegionStoreManager::getBindingForVar(RegionBindingsConstRef B, 1945 const VarRegion *R) { 1946 1947 // Check if the region has a binding. 1948 if (Optional<SVal> V = B.getDirectBinding(R)) 1949 return *V; 1950 1951 if (Optional<SVal> V = B.getDefaultBinding(R)) 1952 return *V; 1953 1954 // Lazily derive a value for the VarRegion. 1955 const VarDecl *VD = R->getDecl(); 1956 const MemSpaceRegion *MS = R->getMemorySpace(); 1957 1958 // Arguments are always symbolic. 1959 if (isa<StackArgumentsSpaceRegion>(MS)) 1960 return svalBuilder.getRegionValueSymbolVal(R); 1961 1962 // Is 'VD' declared constant? If so, retrieve the constant value. 1963 if (VD->getType().isConstQualified()) { 1964 if (const Expr *Init = VD->getAnyInitializer()) { 1965 if (Optional<SVal> V = svalBuilder.getConstantVal(Init)) 1966 return *V; 1967 1968 // If the variable is const qualified and has an initializer but 1969 // we couldn't evaluate initializer to a value, treat the value as 1970 // unknown. 1971 return UnknownVal(); 1972 } 1973 } 1974 1975 // This must come after the check for constants because closure-captured 1976 // constant variables may appear in UnknownSpaceRegion. 1977 if (isa<UnknownSpaceRegion>(MS)) 1978 return svalBuilder.getRegionValueSymbolVal(R); 1979 1980 if (isa<GlobalsSpaceRegion>(MS)) { 1981 QualType T = VD->getType(); 1982 1983 // Function-scoped static variables are default-initialized to 0; if they 1984 // have an initializer, it would have been processed by now. 1985 // FIXME: This is only true when we're starting analysis from main(). 1986 // We're losing a lot of coverage here. 1987 if (isa<StaticGlobalSpaceRegion>(MS)) 1988 return svalBuilder.makeZeroVal(T); 1989 1990 if (Optional<SVal> V = getBindingForDerivedDefaultValue(B, MS, R, T)) { 1991 assert(!V->getAs<nonloc::LazyCompoundVal>()); 1992 return V.getValue(); 1993 } 1994 1995 return svalBuilder.getRegionValueSymbolVal(R); 1996 } 1997 1998 return UndefinedVal(); 1999 } 2000 2001 SVal RegionStoreManager::getBindingForLazySymbol(const TypedValueRegion *R) { 2002 // All other values are symbolic. 2003 return svalBuilder.getRegionValueSymbolVal(R); 2004 } 2005 2006 const RegionStoreManager::SValListTy & 2007 RegionStoreManager::getInterestingValues(nonloc::LazyCompoundVal LCV) { 2008 // First, check the cache. 2009 LazyBindingsMapTy::iterator I = LazyBindingsMap.find(LCV.getCVData()); 2010 if (I != LazyBindingsMap.end()) 2011 return I->second; 2012 2013 // If we don't have a list of values cached, start constructing it. 2014 SValListTy List; 2015 2016 const SubRegion *LazyR = LCV.getRegion(); 2017 RegionBindingsRef B = getRegionBindings(LCV.getStore()); 2018 2019 // If this region had /no/ bindings at the time, there are no interesting 2020 // values to return. 2021 const ClusterBindings *Cluster = B.lookup(LazyR->getBaseRegion()); 2022 if (!Cluster) 2023 return (LazyBindingsMap[LCV.getCVData()] = std::move(List)); 2024 2025 SmallVector<BindingPair, 32> Bindings; 2026 collectSubRegionBindings(Bindings, svalBuilder, *Cluster, LazyR, 2027 /*IncludeAllDefaultBindings=*/true); 2028 for (SmallVectorImpl<BindingPair>::const_iterator I = Bindings.begin(), 2029 E = Bindings.end(); 2030 I != E; ++I) { 2031 SVal V = I->second; 2032 if (V.isUnknownOrUndef() || V.isConstant()) 2033 continue; 2034 2035 if (Optional<nonloc::LazyCompoundVal> InnerLCV = 2036 V.getAs<nonloc::LazyCompoundVal>()) { 2037 const SValListTy &InnerList = getInterestingValues(*InnerLCV); 2038 List.insert(List.end(), InnerList.begin(), InnerList.end()); 2039 continue; 2040 } 2041 2042 List.push_back(V); 2043 } 2044 2045 return (LazyBindingsMap[LCV.getCVData()] = std::move(List)); 2046 } 2047 2048 NonLoc RegionStoreManager::createLazyBinding(RegionBindingsConstRef B, 2049 const TypedValueRegion *R) { 2050 if (Optional<nonloc::LazyCompoundVal> V = 2051 getExistingLazyBinding(svalBuilder, B, R, false)) 2052 return *V; 2053 2054 return svalBuilder.makeLazyCompoundVal(StoreRef(B.asStore(), *this), R); 2055 } 2056 2057 static bool isRecordEmpty(const RecordDecl *RD) { 2058 if (!RD->field_empty()) 2059 return false; 2060 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) 2061 return CRD->getNumBases() == 0; 2062 return true; 2063 } 2064 2065 SVal RegionStoreManager::getBindingForStruct(RegionBindingsConstRef B, 2066 const TypedValueRegion *R) { 2067 const RecordDecl *RD = R->getValueType()->castAs<RecordType>()->getDecl(); 2068 if (!RD->getDefinition() || isRecordEmpty(RD)) 2069 return UnknownVal(); 2070 2071 return createLazyBinding(B, R); 2072 } 2073 2074 SVal RegionStoreManager::getBindingForArray(RegionBindingsConstRef B, 2075 const TypedValueRegion *R) { 2076 assert(Ctx.getAsConstantArrayType(R->getValueType()) && 2077 "Only constant array types can have compound bindings."); 2078 2079 return createLazyBinding(B, R); 2080 } 2081 2082 bool RegionStoreManager::includedInBindings(Store store, 2083 const MemRegion *region) const { 2084 RegionBindingsRef B = getRegionBindings(store); 2085 region = region->getBaseRegion(); 2086 2087 // Quick path: if the base is the head of a cluster, the region is live. 2088 if (B.lookup(region)) 2089 return true; 2090 2091 // Slow path: if the region is the VALUE of any binding, it is live. 2092 for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) { 2093 const ClusterBindings &Cluster = RI.getData(); 2094 for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end(); 2095 CI != CE; ++CI) { 2096 const SVal &D = CI.getData(); 2097 if (const MemRegion *R = D.getAsRegion()) 2098 if (R->getBaseRegion() == region) 2099 return true; 2100 } 2101 } 2102 2103 return false; 2104 } 2105 2106 //===----------------------------------------------------------------------===// 2107 // Binding values to regions. 2108 //===----------------------------------------------------------------------===// 2109 2110 StoreRef RegionStoreManager::killBinding(Store ST, Loc L) { 2111 if (Optional<loc::MemRegionVal> LV = L.getAs<loc::MemRegionVal>()) 2112 if (const MemRegion* R = LV->getRegion()) 2113 return StoreRef(getRegionBindings(ST).removeBinding(R) 2114 .asImmutableMap() 2115 .getRootWithoutRetain(), 2116 *this); 2117 2118 return StoreRef(ST, *this); 2119 } 2120 2121 RegionBindingsRef 2122 RegionStoreManager::bind(RegionBindingsConstRef B, Loc L, SVal V) { 2123 if (L.getAs<loc::ConcreteInt>()) 2124 return B; 2125 2126 // If we get here, the location should be a region. 2127 const MemRegion *R = L.castAs<loc::MemRegionVal>().getRegion(); 2128 2129 // Check if the region is a struct region. 2130 if (const TypedValueRegion* TR = dyn_cast<TypedValueRegion>(R)) { 2131 QualType Ty = TR->getValueType(); 2132 if (Ty->isArrayType()) 2133 return bindArray(B, TR, V); 2134 if (Ty->isStructureOrClassType()) 2135 return bindStruct(B, TR, V); 2136 if (Ty->isVectorType()) 2137 return bindVector(B, TR, V); 2138 if (Ty->isUnionType()) 2139 return bindAggregate(B, TR, V); 2140 } 2141 2142 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) { 2143 // Binding directly to a symbolic region should be treated as binding 2144 // to element 0. 2145 QualType T = SR->getSymbol()->getType(); 2146 if (T->isAnyPointerType() || T->isReferenceType()) 2147 T = T->getPointeeType(); 2148 2149 R = GetElementZeroRegion(SR, T); 2150 } 2151 2152 assert((!isa<CXXThisRegion>(R) || !B.lookup(R)) && 2153 "'this' pointer is not an l-value and is not assignable"); 2154 2155 // Clear out bindings that may overlap with this binding. 2156 RegionBindingsRef NewB = removeSubRegionBindings(B, cast<SubRegion>(R)); 2157 return NewB.addBinding(BindingKey::Make(R, BindingKey::Direct), V); 2158 } 2159 2160 RegionBindingsRef 2161 RegionStoreManager::setImplicitDefaultValue(RegionBindingsConstRef B, 2162 const MemRegion *R, 2163 QualType T) { 2164 SVal V; 2165 2166 if (Loc::isLocType(T)) 2167 V = svalBuilder.makeNull(); 2168 else if (T->isIntegralOrEnumerationType()) 2169 V = svalBuilder.makeZeroVal(T); 2170 else if (T->isStructureOrClassType() || T->isArrayType()) { 2171 // Set the default value to a zero constant when it is a structure 2172 // or array. The type doesn't really matter. 2173 V = svalBuilder.makeZeroVal(Ctx.IntTy); 2174 } 2175 else { 2176 // We can't represent values of this type, but we still need to set a value 2177 // to record that the region has been initialized. 2178 // If this assertion ever fires, a new case should be added above -- we 2179 // should know how to default-initialize any value we can symbolicate. 2180 assert(!SymbolManager::canSymbolicate(T) && "This type is representable"); 2181 V = UnknownVal(); 2182 } 2183 2184 return B.addBinding(R, BindingKey::Default, V); 2185 } 2186 2187 RegionBindingsRef 2188 RegionStoreManager::bindArray(RegionBindingsConstRef B, 2189 const TypedValueRegion* R, 2190 SVal Init) { 2191 2192 const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType())); 2193 QualType ElementTy = AT->getElementType(); 2194 Optional<uint64_t> Size; 2195 2196 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT)) 2197 Size = CAT->getSize().getZExtValue(); 2198 2199 // Check if the init expr is a literal. If so, bind the rvalue instead. 2200 // FIXME: It's not responsibility of the Store to transform this lvalue 2201 // to rvalue. ExprEngine or maybe even CFG should do this before binding. 2202 if (Optional<loc::MemRegionVal> MRV = Init.getAs<loc::MemRegionVal>()) { 2203 SVal V = getBinding(B.asStore(), *MRV, R->getValueType()); 2204 return bindAggregate(B, R, V); 2205 } 2206 2207 // Handle lazy compound values. 2208 if (Init.getAs<nonloc::LazyCompoundVal>()) 2209 return bindAggregate(B, R, Init); 2210 2211 if (Init.isUnknown()) 2212 return bindAggregate(B, R, UnknownVal()); 2213 2214 // Remaining case: explicit compound values. 2215 const nonloc::CompoundVal& CV = Init.castAs<nonloc::CompoundVal>(); 2216 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); 2217 uint64_t i = 0; 2218 2219 RegionBindingsRef NewB(B); 2220 2221 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) { 2222 // The init list might be shorter than the array length. 2223 if (VI == VE) 2224 break; 2225 2226 const NonLoc &Idx = svalBuilder.makeArrayIndex(i); 2227 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx); 2228 2229 if (ElementTy->isStructureOrClassType()) 2230 NewB = bindStruct(NewB, ER, *VI); 2231 else if (ElementTy->isArrayType()) 2232 NewB = bindArray(NewB, ER, *VI); 2233 else 2234 NewB = bind(NewB, loc::MemRegionVal(ER), *VI); 2235 } 2236 2237 // If the init list is shorter than the array length (or the array has 2238 // variable length), set the array default value. Values that are already set 2239 // are not overwritten. 2240 if (!Size.hasValue() || i < Size.getValue()) 2241 NewB = setImplicitDefaultValue(NewB, R, ElementTy); 2242 2243 return NewB; 2244 } 2245 2246 RegionBindingsRef RegionStoreManager::bindVector(RegionBindingsConstRef B, 2247 const TypedValueRegion* R, 2248 SVal V) { 2249 QualType T = R->getValueType(); 2250 assert(T->isVectorType()); 2251 const VectorType *VT = T->getAs<VectorType>(); // Use getAs for typedefs. 2252 2253 // Handle lazy compound values and symbolic values. 2254 if (V.getAs<nonloc::LazyCompoundVal>() || V.getAs<nonloc::SymbolVal>()) 2255 return bindAggregate(B, R, V); 2256 2257 // We may get non-CompoundVal accidentally due to imprecise cast logic or 2258 // that we are binding symbolic struct value. Kill the field values, and if 2259 // the value is symbolic go and bind it as a "default" binding. 2260 if (!V.getAs<nonloc::CompoundVal>()) { 2261 return bindAggregate(B, R, UnknownVal()); 2262 } 2263 2264 QualType ElemType = VT->getElementType(); 2265 nonloc::CompoundVal CV = V.castAs<nonloc::CompoundVal>(); 2266 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); 2267 unsigned index = 0, numElements = VT->getNumElements(); 2268 RegionBindingsRef NewB(B); 2269 2270 for ( ; index != numElements ; ++index) { 2271 if (VI == VE) 2272 break; 2273 2274 NonLoc Idx = svalBuilder.makeArrayIndex(index); 2275 const ElementRegion *ER = MRMgr.getElementRegion(ElemType, Idx, R, Ctx); 2276 2277 if (ElemType->isArrayType()) 2278 NewB = bindArray(NewB, ER, *VI); 2279 else if (ElemType->isStructureOrClassType()) 2280 NewB = bindStruct(NewB, ER, *VI); 2281 else 2282 NewB = bind(NewB, loc::MemRegionVal(ER), *VI); 2283 } 2284 return NewB; 2285 } 2286 2287 Optional<RegionBindingsRef> 2288 RegionStoreManager::tryBindSmallStruct(RegionBindingsConstRef B, 2289 const TypedValueRegion *R, 2290 const RecordDecl *RD, 2291 nonloc::LazyCompoundVal LCV) { 2292 FieldVector Fields; 2293 2294 if (const CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(RD)) 2295 if (Class->getNumBases() != 0 || Class->getNumVBases() != 0) 2296 return None; 2297 2298 for (const auto *FD : RD->fields()) { 2299 if (FD->isUnnamedBitfield()) 2300 continue; 2301 2302 // If there are too many fields, or if any of the fields are aggregates, 2303 // just use the LCV as a default binding. 2304 if (Fields.size() == SmallStructLimit) 2305 return None; 2306 2307 QualType Ty = FD->getType(); 2308 if (!(Ty->isScalarType() || Ty->isReferenceType())) 2309 return None; 2310 2311 Fields.push_back(FD); 2312 } 2313 2314 RegionBindingsRef NewB = B; 2315 2316 for (FieldVector::iterator I = Fields.begin(), E = Fields.end(); I != E; ++I){ 2317 const FieldRegion *SourceFR = MRMgr.getFieldRegion(*I, LCV.getRegion()); 2318 SVal V = getBindingForField(getRegionBindings(LCV.getStore()), SourceFR); 2319 2320 const FieldRegion *DestFR = MRMgr.getFieldRegion(*I, R); 2321 NewB = bind(NewB, loc::MemRegionVal(DestFR), V); 2322 } 2323 2324 return NewB; 2325 } 2326 2327 RegionBindingsRef RegionStoreManager::bindStruct(RegionBindingsConstRef B, 2328 const TypedValueRegion* R, 2329 SVal V) { 2330 if (!Features.supportsFields()) 2331 return B; 2332 2333 QualType T = R->getValueType(); 2334 assert(T->isStructureOrClassType()); 2335 2336 const RecordType* RT = T->getAs<RecordType>(); 2337 const RecordDecl *RD = RT->getDecl(); 2338 2339 if (!RD->isCompleteDefinition()) 2340 return B; 2341 2342 // Handle lazy compound values and symbolic values. 2343 if (Optional<nonloc::LazyCompoundVal> LCV = 2344 V.getAs<nonloc::LazyCompoundVal>()) { 2345 if (Optional<RegionBindingsRef> NewB = tryBindSmallStruct(B, R, RD, *LCV)) 2346 return *NewB; 2347 return bindAggregate(B, R, V); 2348 } 2349 if (V.getAs<nonloc::SymbolVal>()) 2350 return bindAggregate(B, R, V); 2351 2352 // We may get non-CompoundVal accidentally due to imprecise cast logic or 2353 // that we are binding symbolic struct value. Kill the field values, and if 2354 // the value is symbolic go and bind it as a "default" binding. 2355 if (V.isUnknown() || !V.getAs<nonloc::CompoundVal>()) 2356 return bindAggregate(B, R, UnknownVal()); 2357 2358 // The raw CompoundVal is essentially a symbolic InitListExpr: an (immutable) 2359 // list of other values. It appears pretty much only when there's an actual 2360 // initializer list expression in the program, and the analyzer tries to 2361 // unwrap it as soon as possible. 2362 // This code is where such unwrap happens: when the compound value is put into 2363 // the object that it was supposed to initialize (it's an *initializer* list, 2364 // after all), instead of binding the whole value to the whole object, we bind 2365 // sub-values to sub-objects. Sub-values may themselves be compound values, 2366 // and in this case the procedure becomes recursive. 2367 // FIXME: The annoying part about compound values is that they don't carry 2368 // any sort of information about which value corresponds to which sub-object. 2369 // It's simply a list of values in the middle of nowhere; we expect to match 2370 // them to sub-objects, essentially, "by index": first value binds to 2371 // the first field, second value binds to the second field, etc. 2372 // It would have been much safer to organize non-lazy compound values as 2373 // a mapping from fields/bases to values. 2374 const nonloc::CompoundVal& CV = V.castAs<nonloc::CompoundVal>(); 2375 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end(); 2376 2377 RegionBindingsRef NewB(B); 2378 2379 // In C++17 aggregates may have base classes, handle those as well. 2380 // They appear before fields in the initializer list / compound value. 2381 if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) { 2382 // If the object was constructed with a constructor, its value is a 2383 // LazyCompoundVal. If it's a raw CompoundVal, it means that we're 2384 // performing aggregate initialization. The only exception from this 2385 // rule is sending an Objective-C++ message that returns a C++ object 2386 // to a nil receiver; in this case the semantics is to return a 2387 // zero-initialized object even if it's a C++ object that doesn't have 2388 // this sort of constructor; the CompoundVal is empty in this case. 2389 assert((CRD->isAggregate() || (Ctx.getLangOpts().ObjC && VI == VE)) && 2390 "Non-aggregates are constructed with a constructor!"); 2391 2392 for (const auto &B : CRD->bases()) { 2393 // (Multiple inheritance is fine though.) 2394 assert(!B.isVirtual() && "Aggregates cannot have virtual base classes!"); 2395 2396 if (VI == VE) 2397 break; 2398 2399 QualType BTy = B.getType(); 2400 assert(BTy->isStructureOrClassType() && "Base classes must be classes!"); 2401 2402 const CXXRecordDecl *BRD = BTy->getAsCXXRecordDecl(); 2403 assert(BRD && "Base classes must be C++ classes!"); 2404 2405 const CXXBaseObjectRegion *BR = 2406 MRMgr.getCXXBaseObjectRegion(BRD, R, /*IsVirtual=*/false); 2407 2408 NewB = bindStruct(NewB, BR, *VI); 2409 2410 ++VI; 2411 } 2412 } 2413 2414 RecordDecl::field_iterator FI, FE; 2415 2416 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI) { 2417 2418 if (VI == VE) 2419 break; 2420 2421 // Skip any unnamed bitfields to stay in sync with the initializers. 2422 if (FI->isUnnamedBitfield()) 2423 continue; 2424 2425 QualType FTy = FI->getType(); 2426 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R); 2427 2428 if (FTy->isArrayType()) 2429 NewB = bindArray(NewB, FR, *VI); 2430 else if (FTy->isStructureOrClassType()) 2431 NewB = bindStruct(NewB, FR, *VI); 2432 else 2433 NewB = bind(NewB, loc::MemRegionVal(FR), *VI); 2434 ++VI; 2435 } 2436 2437 // There may be fewer values in the initialize list than the fields of struct. 2438 if (FI != FE) { 2439 NewB = NewB.addBinding(R, BindingKey::Default, 2440 svalBuilder.makeIntVal(0, false)); 2441 } 2442 2443 return NewB; 2444 } 2445 2446 RegionBindingsRef 2447 RegionStoreManager::bindAggregate(RegionBindingsConstRef B, 2448 const TypedRegion *R, 2449 SVal Val) { 2450 // Remove the old bindings, using 'R' as the root of all regions 2451 // we will invalidate. Then add the new binding. 2452 return removeSubRegionBindings(B, R).addBinding(R, BindingKey::Default, Val); 2453 } 2454 2455 //===----------------------------------------------------------------------===// 2456 // State pruning. 2457 //===----------------------------------------------------------------------===// 2458 2459 namespace { 2460 class RemoveDeadBindingsWorker 2461 : public ClusterAnalysis<RemoveDeadBindingsWorker> { 2462 SmallVector<const SymbolicRegion *, 12> Postponed; 2463 SymbolReaper &SymReaper; 2464 const StackFrameContext *CurrentLCtx; 2465 2466 public: 2467 RemoveDeadBindingsWorker(RegionStoreManager &rm, 2468 ProgramStateManager &stateMgr, 2469 RegionBindingsRef b, SymbolReaper &symReaper, 2470 const StackFrameContext *LCtx) 2471 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b), 2472 SymReaper(symReaper), CurrentLCtx(LCtx) {} 2473 2474 // Called by ClusterAnalysis. 2475 void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C); 2476 void VisitCluster(const MemRegion *baseR, const ClusterBindings *C); 2477 using ClusterAnalysis<RemoveDeadBindingsWorker>::VisitCluster; 2478 2479 using ClusterAnalysis::AddToWorkList; 2480 2481 bool AddToWorkList(const MemRegion *R); 2482 2483 bool UpdatePostponed(); 2484 void VisitBinding(SVal V); 2485 }; 2486 } 2487 2488 bool RemoveDeadBindingsWorker::AddToWorkList(const MemRegion *R) { 2489 const MemRegion *BaseR = R->getBaseRegion(); 2490 return AddToWorkList(WorkListElement(BaseR), getCluster(BaseR)); 2491 } 2492 2493 void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR, 2494 const ClusterBindings &C) { 2495 2496 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) { 2497 if (SymReaper.isLive(VR)) 2498 AddToWorkList(baseR, &C); 2499 2500 return; 2501 } 2502 2503 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) { 2504 if (SymReaper.isLive(SR->getSymbol())) 2505 AddToWorkList(SR, &C); 2506 else 2507 Postponed.push_back(SR); 2508 2509 return; 2510 } 2511 2512 if (isa<NonStaticGlobalSpaceRegion>(baseR)) { 2513 AddToWorkList(baseR, &C); 2514 return; 2515 } 2516 2517 // CXXThisRegion in the current or parent location context is live. 2518 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) { 2519 const auto *StackReg = 2520 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion()); 2521 const StackFrameContext *RegCtx = StackReg->getStackFrame(); 2522 if (CurrentLCtx && 2523 (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))) 2524 AddToWorkList(TR, &C); 2525 } 2526 } 2527 2528 void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR, 2529 const ClusterBindings *C) { 2530 if (!C) 2531 return; 2532 2533 // Mark the symbol for any SymbolicRegion with live bindings as live itself. 2534 // This means we should continue to track that symbol. 2535 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(baseR)) 2536 SymReaper.markLive(SymR->getSymbol()); 2537 2538 for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E; ++I) { 2539 // Element index of a binding key is live. 2540 SymReaper.markElementIndicesLive(I.getKey().getRegion()); 2541 2542 VisitBinding(I.getData()); 2543 } 2544 } 2545 2546 void RemoveDeadBindingsWorker::VisitBinding(SVal V) { 2547 // Is it a LazyCompoundVal? All referenced regions are live as well. 2548 if (Optional<nonloc::LazyCompoundVal> LCS = 2549 V.getAs<nonloc::LazyCompoundVal>()) { 2550 2551 const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS); 2552 2553 for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(), 2554 E = Vals.end(); 2555 I != E; ++I) 2556 VisitBinding(*I); 2557 2558 return; 2559 } 2560 2561 // If V is a region, then add it to the worklist. 2562 if (const MemRegion *R = V.getAsRegion()) { 2563 AddToWorkList(R); 2564 SymReaper.markLive(R); 2565 2566 // All regions captured by a block are also live. 2567 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) { 2568 BlockDataRegion::referenced_vars_iterator I = BR->referenced_vars_begin(), 2569 E = BR->referenced_vars_end(); 2570 for ( ; I != E; ++I) 2571 AddToWorkList(I.getCapturedRegion()); 2572 } 2573 } 2574 2575 2576 // Update the set of live symbols. 2577 for (auto SI = V.symbol_begin(), SE = V.symbol_end(); SI!=SE; ++SI) 2578 SymReaper.markLive(*SI); 2579 } 2580 2581 bool RemoveDeadBindingsWorker::UpdatePostponed() { 2582 // See if any postponed SymbolicRegions are actually live now, after 2583 // having done a scan. 2584 bool Changed = false; 2585 2586 for (auto I = Postponed.begin(), E = Postponed.end(); I != E; ++I) { 2587 if (const SymbolicRegion *SR = *I) { 2588 if (SymReaper.isLive(SR->getSymbol())) { 2589 Changed |= AddToWorkList(SR); 2590 *I = nullptr; 2591 } 2592 } 2593 } 2594 2595 return Changed; 2596 } 2597 2598 StoreRef RegionStoreManager::removeDeadBindings(Store store, 2599 const StackFrameContext *LCtx, 2600 SymbolReaper& SymReaper) { 2601 RegionBindingsRef B = getRegionBindings(store); 2602 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx); 2603 W.GenerateClusters(); 2604 2605 // Enqueue the region roots onto the worklist. 2606 for (SymbolReaper::region_iterator I = SymReaper.region_begin(), 2607 E = SymReaper.region_end(); I != E; ++I) { 2608 W.AddToWorkList(*I); 2609 } 2610 2611 do W.RunWorkList(); while (W.UpdatePostponed()); 2612 2613 // We have now scanned the store, marking reachable regions and symbols 2614 // as live. We now remove all the regions that are dead from the store 2615 // as well as update DSymbols with the set symbols that are now dead. 2616 for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) { 2617 const MemRegion *Base = I.getKey(); 2618 2619 // If the cluster has been visited, we know the region has been marked. 2620 // Otherwise, remove the dead entry. 2621 if (!W.isVisited(Base)) 2622 B = B.remove(Base); 2623 } 2624 2625 return StoreRef(B.asStore(), *this); 2626 } 2627 2628 //===----------------------------------------------------------------------===// 2629 // Utility methods. 2630 //===----------------------------------------------------------------------===// 2631 2632 void RegionStoreManager::printJson(raw_ostream &Out, Store S, const char *NL, 2633 unsigned int Space, bool IsDot) const { 2634 RegionBindingsRef Bindings = getRegionBindings(S); 2635 2636 Indent(Out, Space, IsDot) << "\"store\": "; 2637 2638 if (Bindings.isEmpty()) { 2639 Out << "null," << NL; 2640 return; 2641 } 2642 2643 Out << "{ \"pointer\": \"" << Bindings.asStore() << "\", \"items\": [" << NL; 2644 Bindings.printJson(Out, NL, Space + 1, IsDot); 2645 Indent(Out, Space, IsDot) << "]}," << NL; 2646 } 2647