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