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