MemRegion.cpp revision 4e4d08403ca5cfd4d558fa2936215d3a4e5a528d
1//== MemRegion.cpp - Abstract memory regions for static analysis --*- C++ -*--// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines MemRegion and its subclasses. MemRegion defines a 11// partially-typed abstraction of memory useful for path-sensitive dataflow 12// analyses. 13// 14//===----------------------------------------------------------------------===// 15 16#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 17#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 18#include "clang/Analysis/AnalysisContext.h" 19#include "clang/Analysis/Support/BumpVector.h" 20#include "clang/AST/CharUnits.h" 21#include "clang/AST/DeclObjC.h" 22#include "clang/AST/RecordLayout.h" 23#include "clang/Basic/SourceManager.h" 24#include "llvm/Support/raw_ostream.h" 25 26using namespace clang; 27using namespace ento; 28 29//===----------------------------------------------------------------------===// 30// MemRegion Construction. 31//===----------------------------------------------------------------------===// 32 33template<typename RegionTy> struct MemRegionManagerTrait; 34 35template <typename RegionTy, typename A1> 36RegionTy* MemRegionManager::getRegion(const A1 a1) { 37 38 const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion = 39 MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1); 40 41 llvm::FoldingSetNodeID ID; 42 RegionTy::ProfileRegion(ID, a1, superRegion); 43 void *InsertPos; 44 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, 45 InsertPos)); 46 47 if (!R) { 48 R = (RegionTy*) A.Allocate<RegionTy>(); 49 new (R) RegionTy(a1, superRegion); 50 Regions.InsertNode(R, InsertPos); 51 } 52 53 return R; 54} 55 56template <typename RegionTy, typename A1> 57RegionTy* MemRegionManager::getSubRegion(const A1 a1, 58 const MemRegion *superRegion) { 59 llvm::FoldingSetNodeID ID; 60 RegionTy::ProfileRegion(ID, a1, superRegion); 61 void *InsertPos; 62 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, 63 InsertPos)); 64 65 if (!R) { 66 R = (RegionTy*) A.Allocate<RegionTy>(); 67 new (R) RegionTy(a1, superRegion); 68 Regions.InsertNode(R, InsertPos); 69 } 70 71 return R; 72} 73 74template <typename RegionTy, typename A1, typename A2> 75RegionTy* MemRegionManager::getRegion(const A1 a1, const A2 a2) { 76 77 const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion = 78 MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1, a2); 79 80 llvm::FoldingSetNodeID ID; 81 RegionTy::ProfileRegion(ID, a1, a2, superRegion); 82 void *InsertPos; 83 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, 84 InsertPos)); 85 86 if (!R) { 87 R = (RegionTy*) A.Allocate<RegionTy>(); 88 new (R) RegionTy(a1, a2, superRegion); 89 Regions.InsertNode(R, InsertPos); 90 } 91 92 return R; 93} 94 95template <typename RegionTy, typename A1, typename A2> 96RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2, 97 const MemRegion *superRegion) { 98 99 llvm::FoldingSetNodeID ID; 100 RegionTy::ProfileRegion(ID, a1, a2, superRegion); 101 void *InsertPos; 102 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, 103 InsertPos)); 104 105 if (!R) { 106 R = (RegionTy*) A.Allocate<RegionTy>(); 107 new (R) RegionTy(a1, a2, superRegion); 108 Regions.InsertNode(R, InsertPos); 109 } 110 111 return R; 112} 113 114template <typename RegionTy, typename A1, typename A2, typename A3> 115RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2, const A3 a3, 116 const MemRegion *superRegion) { 117 118 llvm::FoldingSetNodeID ID; 119 RegionTy::ProfileRegion(ID, a1, a2, a3, superRegion); 120 void *InsertPos; 121 RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, 122 InsertPos)); 123 124 if (!R) { 125 R = (RegionTy*) A.Allocate<RegionTy>(); 126 new (R) RegionTy(a1, a2, a3, superRegion); 127 Regions.InsertNode(R, InsertPos); 128 } 129 130 return R; 131} 132 133//===----------------------------------------------------------------------===// 134// Object destruction. 135//===----------------------------------------------------------------------===// 136 137MemRegion::~MemRegion() {} 138 139MemRegionManager::~MemRegionManager() { 140 // All regions and their data are BumpPtrAllocated. No need to call 141 // their destructors. 142} 143 144//===----------------------------------------------------------------------===// 145// Basic methods. 146//===----------------------------------------------------------------------===// 147 148bool SubRegion::isSubRegionOf(const MemRegion* R) const { 149 const MemRegion* r = getSuperRegion(); 150 while (r != 0) { 151 if (r == R) 152 return true; 153 if (const SubRegion* sr = dyn_cast<SubRegion>(r)) 154 r = sr->getSuperRegion(); 155 else 156 break; 157 } 158 return false; 159} 160 161MemRegionManager* SubRegion::getMemRegionManager() const { 162 const SubRegion* r = this; 163 do { 164 const MemRegion *superRegion = r->getSuperRegion(); 165 if (const SubRegion *sr = dyn_cast<SubRegion>(superRegion)) { 166 r = sr; 167 continue; 168 } 169 return superRegion->getMemRegionManager(); 170 } while (1); 171} 172 173const StackFrameContext *VarRegion::getStackFrame() const { 174 const StackSpaceRegion *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace()); 175 return SSR ? SSR->getStackFrame() : NULL; 176} 177 178//===----------------------------------------------------------------------===// 179// Region extents. 180//===----------------------------------------------------------------------===// 181 182DefinedOrUnknownSVal DeclRegion::getExtent(SValBuilder &svalBuilder) const { 183 ASTContext &Ctx = svalBuilder.getContext(); 184 QualType T = getDesugaredValueType(Ctx); 185 186 if (isa<VariableArrayType>(T)) 187 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this)); 188 if (isa<IncompleteArrayType>(T)) 189 return UnknownVal(); 190 191 CharUnits size = Ctx.getTypeSizeInChars(T); 192 QualType sizeTy = svalBuilder.getArrayIndexType(); 193 return svalBuilder.makeIntVal(size.getQuantity(), sizeTy); 194} 195 196DefinedOrUnknownSVal FieldRegion::getExtent(SValBuilder &svalBuilder) const { 197 DefinedOrUnknownSVal Extent = DeclRegion::getExtent(svalBuilder); 198 199 // A zero-length array at the end of a struct often stands for dynamically- 200 // allocated extra memory. 201 if (Extent.isZeroConstant()) { 202 QualType T = getDesugaredValueType(svalBuilder.getContext()); 203 204 if (isa<ConstantArrayType>(T)) 205 return UnknownVal(); 206 } 207 208 return Extent; 209} 210 211DefinedOrUnknownSVal AllocaRegion::getExtent(SValBuilder &svalBuilder) const { 212 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this)); 213} 214 215DefinedOrUnknownSVal SymbolicRegion::getExtent(SValBuilder &svalBuilder) const { 216 return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this)); 217} 218 219DefinedOrUnknownSVal StringRegion::getExtent(SValBuilder &svalBuilder) const { 220 return svalBuilder.makeIntVal(getStringLiteral()->getByteLength()+1, 221 svalBuilder.getArrayIndexType()); 222} 223 224ObjCIvarRegion::ObjCIvarRegion(const ObjCIvarDecl *ivd, const MemRegion* sReg) 225 : DeclRegion(ivd, sReg, ObjCIvarRegionKind) {} 226 227const ObjCIvarDecl *ObjCIvarRegion::getDecl() const { 228 return cast<ObjCIvarDecl>(D); 229} 230 231QualType ObjCIvarRegion::getValueType() const { 232 return getDecl()->getType(); 233} 234 235QualType CXXBaseObjectRegion::getValueType() const { 236 return QualType(decl->getTypeForDecl(), 0); 237} 238 239//===----------------------------------------------------------------------===// 240// FoldingSet profiling. 241//===----------------------------------------------------------------------===// 242 243void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const { 244 ID.AddInteger((unsigned)getKind()); 245} 246 247void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const { 248 ID.AddInteger((unsigned)getKind()); 249 ID.AddPointer(getStackFrame()); 250} 251 252void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const { 253 ID.AddInteger((unsigned)getKind()); 254 ID.AddPointer(getCodeRegion()); 255} 256 257void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, 258 const StringLiteral* Str, 259 const MemRegion* superRegion) { 260 ID.AddInteger((unsigned) StringRegionKind); 261 ID.AddPointer(Str); 262 ID.AddPointer(superRegion); 263} 264 265void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, 266 const ObjCStringLiteral* Str, 267 const MemRegion* superRegion) { 268 ID.AddInteger((unsigned) ObjCStringRegionKind); 269 ID.AddPointer(Str); 270 ID.AddPointer(superRegion); 271} 272 273void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, 274 const Expr *Ex, unsigned cnt, 275 const MemRegion *) { 276 ID.AddInteger((unsigned) AllocaRegionKind); 277 ID.AddPointer(Ex); 278 ID.AddInteger(cnt); 279} 280 281void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const { 282 ProfileRegion(ID, Ex, Cnt, superRegion); 283} 284 285void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const { 286 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion); 287} 288 289void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, 290 const CompoundLiteralExpr *CL, 291 const MemRegion* superRegion) { 292 ID.AddInteger((unsigned) CompoundLiteralRegionKind); 293 ID.AddPointer(CL); 294 ID.AddPointer(superRegion); 295} 296 297void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, 298 const PointerType *PT, 299 const MemRegion *sRegion) { 300 ID.AddInteger((unsigned) CXXThisRegionKind); 301 ID.AddPointer(PT); 302 ID.AddPointer(sRegion); 303} 304 305void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const { 306 CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion); 307} 308 309void ObjCIvarRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, 310 const ObjCIvarDecl *ivd, 311 const MemRegion* superRegion) { 312 DeclRegion::ProfileRegion(ID, ivd, superRegion, ObjCIvarRegionKind); 313} 314 315void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl *D, 316 const MemRegion* superRegion, Kind k) { 317 ID.AddInteger((unsigned) k); 318 ID.AddPointer(D); 319 ID.AddPointer(superRegion); 320} 321 322void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const { 323 DeclRegion::ProfileRegion(ID, D, superRegion, getKind()); 324} 325 326void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const { 327 VarRegion::ProfileRegion(ID, getDecl(), superRegion); 328} 329 330void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym, 331 const MemRegion *sreg) { 332 ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind); 333 ID.Add(sym); 334 ID.AddPointer(sreg); 335} 336 337void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const { 338 SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion()); 339} 340 341void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, 342 QualType ElementType, SVal Idx, 343 const MemRegion* superRegion) { 344 ID.AddInteger(MemRegion::ElementRegionKind); 345 ID.Add(ElementType); 346 ID.AddPointer(superRegion); 347 Idx.Profile(ID); 348} 349 350void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const { 351 ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion); 352} 353 354void FunctionTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, 355 const FunctionDecl *FD, 356 const MemRegion*) { 357 ID.AddInteger(MemRegion::FunctionTextRegionKind); 358 ID.AddPointer(FD); 359} 360 361void FunctionTextRegion::Profile(llvm::FoldingSetNodeID& ID) const { 362 FunctionTextRegion::ProfileRegion(ID, FD, superRegion); 363} 364 365void BlockTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, 366 const BlockDecl *BD, CanQualType, 367 const AnalysisDeclContext *AC, 368 const MemRegion*) { 369 ID.AddInteger(MemRegion::BlockTextRegionKind); 370 ID.AddPointer(BD); 371} 372 373void BlockTextRegion::Profile(llvm::FoldingSetNodeID& ID) const { 374 BlockTextRegion::ProfileRegion(ID, BD, locTy, AC, superRegion); 375} 376 377void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, 378 const BlockTextRegion *BC, 379 const LocationContext *LC, 380 const MemRegion *sReg) { 381 ID.AddInteger(MemRegion::BlockDataRegionKind); 382 ID.AddPointer(BC); 383 ID.AddPointer(LC); 384 ID.AddPointer(sReg); 385} 386 387void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const { 388 BlockDataRegion::ProfileRegion(ID, BC, LC, getSuperRegion()); 389} 390 391void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, 392 Expr const *Ex, 393 const MemRegion *sReg) { 394 ID.AddPointer(Ex); 395 ID.AddPointer(sReg); 396} 397 398void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const { 399 ProfileRegion(ID, Ex, getSuperRegion()); 400} 401 402void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID, 403 const CXXRecordDecl *decl, 404 const MemRegion *sReg) { 405 ID.AddPointer(decl); 406 ID.AddPointer(sReg); 407} 408 409void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const { 410 ProfileRegion(ID, decl, superRegion); 411} 412 413//===----------------------------------------------------------------------===// 414// Region anchors. 415//===----------------------------------------------------------------------===// 416 417void GlobalsSpaceRegion::anchor() { } 418void HeapSpaceRegion::anchor() { } 419void UnknownSpaceRegion::anchor() { } 420void StackLocalsSpaceRegion::anchor() { } 421void StackArgumentsSpaceRegion::anchor() { } 422void TypedRegion::anchor() { } 423void TypedValueRegion::anchor() { } 424void CodeTextRegion::anchor() { } 425void SubRegion::anchor() { } 426 427//===----------------------------------------------------------------------===// 428// Region pretty-printing. 429//===----------------------------------------------------------------------===// 430 431void MemRegion::dump() const { 432 dumpToStream(llvm::errs()); 433} 434 435std::string MemRegion::getString() const { 436 std::string s; 437 llvm::raw_string_ostream os(s); 438 dumpToStream(os); 439 return os.str(); 440} 441 442void MemRegion::dumpToStream(raw_ostream &os) const { 443 os << "<Unknown Region>"; 444} 445 446void AllocaRegion::dumpToStream(raw_ostream &os) const { 447 os << "alloca{" << (void*) Ex << ',' << Cnt << '}'; 448} 449 450void FunctionTextRegion::dumpToStream(raw_ostream &os) const { 451 os << "code{" << getDecl()->getDeclName().getAsString() << '}'; 452} 453 454void BlockTextRegion::dumpToStream(raw_ostream &os) const { 455 os << "block_code{" << (void*) this << '}'; 456} 457 458void BlockDataRegion::dumpToStream(raw_ostream &os) const { 459 os << "block_data{" << BC << '}'; 460} 461 462void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const { 463 // FIXME: More elaborate pretty-printing. 464 os << "{ " << (void*) CL << " }"; 465} 466 467void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const { 468 os << "temp_object{" << getValueType().getAsString() << ',' 469 << (void*) Ex << '}'; 470} 471 472void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const { 473 os << "base " << decl->getName(); 474} 475 476void CXXThisRegion::dumpToStream(raw_ostream &os) const { 477 os << "this"; 478} 479 480void ElementRegion::dumpToStream(raw_ostream &os) const { 481 os << "element{" << superRegion << ',' 482 << Index << ',' << getElementType().getAsString() << '}'; 483} 484 485void FieldRegion::dumpToStream(raw_ostream &os) const { 486 os << superRegion << "->" << *getDecl(); 487} 488 489void ObjCIvarRegion::dumpToStream(raw_ostream &os) const { 490 os << "ivar{" << superRegion << ',' << *getDecl() << '}'; 491} 492 493void StringRegion::dumpToStream(raw_ostream &os) const { 494 Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOpts())); 495} 496 497void ObjCStringRegion::dumpToStream(raw_ostream &os) const { 498 Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOpts())); 499} 500 501void SymbolicRegion::dumpToStream(raw_ostream &os) const { 502 os << "SymRegion{" << sym << '}'; 503} 504 505void VarRegion::dumpToStream(raw_ostream &os) const { 506 os << *cast<VarDecl>(D); 507} 508 509void RegionRawOffset::dump() const { 510 dumpToStream(llvm::errs()); 511} 512 513void RegionRawOffset::dumpToStream(raw_ostream &os) const { 514 os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}'; 515} 516 517void StaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const { 518 os << "StaticGlobalsMemSpace{" << CR << '}'; 519} 520 521void NonStaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const { 522 os << "NonStaticGlobalSpaceRegion"; 523} 524 525void GlobalInternalSpaceRegion::dumpToStream(raw_ostream &os) const { 526 os << "GlobalInternalSpaceRegion"; 527} 528 529void GlobalSystemSpaceRegion::dumpToStream(raw_ostream &os) const { 530 os << "GlobalSystemSpaceRegion"; 531} 532 533void GlobalImmutableSpaceRegion::dumpToStream(raw_ostream &os) const { 534 os << "GlobalImmutableSpaceRegion"; 535} 536 537//===----------------------------------------------------------------------===// 538// MemRegionManager methods. 539//===----------------------------------------------------------------------===// 540 541template <typename REG> 542const REG *MemRegionManager::LazyAllocate(REG*& region) { 543 if (!region) { 544 region = (REG*) A.Allocate<REG>(); 545 new (region) REG(this); 546 } 547 548 return region; 549} 550 551template <typename REG, typename ARG> 552const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) { 553 if (!region) { 554 region = (REG*) A.Allocate<REG>(); 555 new (region) REG(this, a); 556 } 557 558 return region; 559} 560 561const StackLocalsSpaceRegion* 562MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) { 563 assert(STC); 564 StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC]; 565 566 if (R) 567 return R; 568 569 R = A.Allocate<StackLocalsSpaceRegion>(); 570 new (R) StackLocalsSpaceRegion(this, STC); 571 return R; 572} 573 574const StackArgumentsSpaceRegion * 575MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) { 576 assert(STC); 577 StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC]; 578 579 if (R) 580 return R; 581 582 R = A.Allocate<StackArgumentsSpaceRegion>(); 583 new (R) StackArgumentsSpaceRegion(this, STC); 584 return R; 585} 586 587const GlobalsSpaceRegion 588*MemRegionManager::getGlobalsRegion(MemRegion::Kind K, 589 const CodeTextRegion *CR) { 590 if (!CR) { 591 if (K == MemRegion::GlobalSystemSpaceRegionKind) 592 return LazyAllocate(SystemGlobals); 593 if (K == MemRegion::GlobalImmutableSpaceRegionKind) 594 return LazyAllocate(ImmutableGlobals); 595 assert(K == MemRegion::GlobalInternalSpaceRegionKind); 596 return LazyAllocate(InternalGlobals); 597 } 598 599 assert(K == MemRegion::StaticGlobalSpaceRegionKind); 600 StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR]; 601 if (R) 602 return R; 603 604 R = A.Allocate<StaticGlobalSpaceRegion>(); 605 new (R) StaticGlobalSpaceRegion(this, CR); 606 return R; 607} 608 609const HeapSpaceRegion *MemRegionManager::getHeapRegion() { 610 return LazyAllocate(heap); 611} 612 613const MemSpaceRegion *MemRegionManager::getUnknownRegion() { 614 return LazyAllocate(unknown); 615} 616 617const MemSpaceRegion *MemRegionManager::getCodeRegion() { 618 return LazyAllocate(code); 619} 620 621//===----------------------------------------------------------------------===// 622// Constructing regions. 623//===----------------------------------------------------------------------===// 624const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){ 625 return getSubRegion<StringRegion>(Str, getGlobalsRegion()); 626} 627 628const ObjCStringRegion * 629MemRegionManager::getObjCStringRegion(const ObjCStringLiteral* Str){ 630 return getSubRegion<ObjCStringRegion>(Str, getGlobalsRegion()); 631} 632 633const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D, 634 const LocationContext *LC) { 635 const MemRegion *sReg = 0; 636 637 if (D->hasGlobalStorage() && !D->isStaticLocal()) { 638 639 // First handle the globals defined in system headers. 640 if (C.getSourceManager().isInSystemHeader(D->getLocation())) { 641 // Whitelist the system globals which often DO GET modified, assume the 642 // rest are immutable. 643 if (D->getName().find("errno") != StringRef::npos) 644 sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind); 645 else 646 sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind); 647 648 // Treat other globals as GlobalInternal unless they are constants. 649 } else { 650 QualType GQT = D->getType(); 651 const Type *GT = GQT.getTypePtrOrNull(); 652 // TODO: We could walk the complex types here and see if everything is 653 // constified. 654 if (GT && GQT.isConstQualified() && GT->isArithmeticType()) 655 sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind); 656 else 657 sReg = getGlobalsRegion(); 658 } 659 660 // Finally handle static locals. 661 } else { 662 // FIXME: Once we implement scope handling, we will need to properly lookup 663 // 'D' to the proper LocationContext. 664 const DeclContext *DC = D->getDeclContext(); 665 const StackFrameContext *STC = LC->getStackFrameForDeclContext(DC); 666 667 if (!STC) 668 sReg = getUnknownRegion(); 669 else { 670 if (D->hasLocalStorage()) { 671 sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) 672 ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC)) 673 : static_cast<const MemRegion*>(getStackLocalsRegion(STC)); 674 } 675 else { 676 assert(D->isStaticLocal()); 677 const Decl *D = STC->getDecl(); 678 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 679 sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind, 680 getFunctionTextRegion(FD)); 681 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { 682 const BlockTextRegion *BTR = 683 getBlockTextRegion(BD, 684 C.getCanonicalType(BD->getSignatureAsWritten()->getType()), 685 STC->getAnalysisDeclContext()); 686 sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind, 687 BTR); 688 } 689 else { 690 // FIXME: For ObjC-methods, we need a new CodeTextRegion. For now 691 // just use the main global memspace. 692 sReg = getGlobalsRegion(); 693 } 694 } 695 } 696 } 697 698 return getSubRegion<VarRegion>(D, sReg); 699} 700 701const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D, 702 const MemRegion *superR) { 703 return getSubRegion<VarRegion>(D, superR); 704} 705 706const BlockDataRegion * 707MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC, 708 const LocationContext *LC) { 709 const MemRegion *sReg = 0; 710 const BlockDecl *BD = BC->getDecl(); 711 if (!BD->hasCaptures()) { 712 // This handles 'static' blocks. 713 sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind); 714 } 715 else { 716 if (LC) { 717 // FIXME: Once we implement scope handling, we want the parent region 718 // to be the scope. 719 const StackFrameContext *STC = LC->getCurrentStackFrame(); 720 assert(STC); 721 sReg = getStackLocalsRegion(STC); 722 } 723 else { 724 // We allow 'LC' to be NULL for cases where want BlockDataRegions 725 // without context-sensitivity. 726 sReg = getUnknownRegion(); 727 } 728 } 729 730 return getSubRegion<BlockDataRegion>(BC, LC, sReg); 731} 732 733const CompoundLiteralRegion* 734MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL, 735 const LocationContext *LC) { 736 737 const MemRegion *sReg = 0; 738 739 if (CL->isFileScope()) 740 sReg = getGlobalsRegion(); 741 else { 742 const StackFrameContext *STC = LC->getCurrentStackFrame(); 743 assert(STC); 744 sReg = getStackLocalsRegion(STC); 745 } 746 747 return getSubRegion<CompoundLiteralRegion>(CL, sReg); 748} 749 750const ElementRegion* 751MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx, 752 const MemRegion* superRegion, 753 ASTContext &Ctx){ 754 755 QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType(); 756 757 llvm::FoldingSetNodeID ID; 758 ElementRegion::ProfileRegion(ID, T, Idx, superRegion); 759 760 void *InsertPos; 761 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); 762 ElementRegion* R = cast_or_null<ElementRegion>(data); 763 764 if (!R) { 765 R = (ElementRegion*) A.Allocate<ElementRegion>(); 766 new (R) ElementRegion(T, Idx, superRegion); 767 Regions.InsertNode(R, InsertPos); 768 } 769 770 return R; 771} 772 773const FunctionTextRegion * 774MemRegionManager::getFunctionTextRegion(const FunctionDecl *FD) { 775 return getSubRegion<FunctionTextRegion>(FD, getCodeRegion()); 776} 777 778const BlockTextRegion * 779MemRegionManager::getBlockTextRegion(const BlockDecl *BD, CanQualType locTy, 780 AnalysisDeclContext *AC) { 781 return getSubRegion<BlockTextRegion>(BD, locTy, AC, getCodeRegion()); 782} 783 784 785/// getSymbolicRegion - Retrieve or create a "symbolic" memory region. 786const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) { 787 return getSubRegion<SymbolicRegion>(sym, getUnknownRegion()); 788} 789 790const FieldRegion* 791MemRegionManager::getFieldRegion(const FieldDecl *d, 792 const MemRegion* superRegion){ 793 return getSubRegion<FieldRegion>(d, superRegion); 794} 795 796const ObjCIvarRegion* 797MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl *d, 798 const MemRegion* superRegion) { 799 return getSubRegion<ObjCIvarRegion>(d, superRegion); 800} 801 802const CXXTempObjectRegion* 803MemRegionManager::getCXXTempObjectRegion(Expr const *E, 804 LocationContext const *LC) { 805 const StackFrameContext *SFC = LC->getCurrentStackFrame(); 806 assert(SFC); 807 return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC)); 808} 809 810const CXXBaseObjectRegion * 811MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *decl, 812 const MemRegion *superRegion) { 813 return getSubRegion<CXXBaseObjectRegion>(decl, superRegion); 814} 815 816const CXXThisRegion* 817MemRegionManager::getCXXThisRegion(QualType thisPointerTy, 818 const LocationContext *LC) { 819 const StackFrameContext *STC = LC->getCurrentStackFrame(); 820 assert(STC); 821 const PointerType *PT = thisPointerTy->getAs<PointerType>(); 822 assert(PT); 823 return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC)); 824} 825 826const AllocaRegion* 827MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt, 828 const LocationContext *LC) { 829 const StackFrameContext *STC = LC->getCurrentStackFrame(); 830 assert(STC); 831 return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC)); 832} 833 834const MemSpaceRegion *MemRegion::getMemorySpace() const { 835 const MemRegion *R = this; 836 const SubRegion* SR = dyn_cast<SubRegion>(this); 837 838 while (SR) { 839 R = SR->getSuperRegion(); 840 SR = dyn_cast<SubRegion>(R); 841 } 842 843 return dyn_cast<MemSpaceRegion>(R); 844} 845 846bool MemRegion::hasStackStorage() const { 847 return isa<StackSpaceRegion>(getMemorySpace()); 848} 849 850bool MemRegion::hasStackNonParametersStorage() const { 851 return isa<StackLocalsSpaceRegion>(getMemorySpace()); 852} 853 854bool MemRegion::hasStackParametersStorage() const { 855 return isa<StackArgumentsSpaceRegion>(getMemorySpace()); 856} 857 858bool MemRegion::hasGlobalsOrParametersStorage() const { 859 const MemSpaceRegion *MS = getMemorySpace(); 860 return isa<StackArgumentsSpaceRegion>(MS) || 861 isa<GlobalsSpaceRegion>(MS); 862} 863 864// getBaseRegion strips away all elements and fields, and get the base region 865// of them. 866const MemRegion *MemRegion::getBaseRegion() const { 867 const MemRegion *R = this; 868 while (true) { 869 switch (R->getKind()) { 870 case MemRegion::ElementRegionKind: 871 case MemRegion::FieldRegionKind: 872 case MemRegion::ObjCIvarRegionKind: 873 case MemRegion::CXXBaseObjectRegionKind: 874 R = cast<SubRegion>(R)->getSuperRegion(); 875 continue; 876 default: 877 break; 878 } 879 break; 880 } 881 return R; 882} 883 884//===----------------------------------------------------------------------===// 885// View handling. 886//===----------------------------------------------------------------------===// 887 888const MemRegion *MemRegion::StripCasts() const { 889 const MemRegion *R = this; 890 while (true) { 891 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { 892 // FIXME: generalize. Essentially we want to strip away ElementRegions 893 // that were layered on a symbolic region because of casts. We only 894 // want to strip away ElementRegions, however, where the index is 0. 895 SVal index = ER->getIndex(); 896 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) { 897 if (CI->getValue().getSExtValue() == 0) { 898 R = ER->getSuperRegion(); 899 continue; 900 } 901 } 902 } 903 break; 904 } 905 return R; 906} 907 908// FIXME: Merge with the implementation of the same method in Store.cpp 909static bool IsCompleteType(ASTContext &Ctx, QualType Ty) { 910 if (const RecordType *RT = Ty->getAs<RecordType>()) { 911 const RecordDecl *D = RT->getDecl(); 912 if (!D->getDefinition()) 913 return false; 914 } 915 916 return true; 917} 918 919RegionRawOffset ElementRegion::getAsArrayOffset() const { 920 CharUnits offset = CharUnits::Zero(); 921 const ElementRegion *ER = this; 922 const MemRegion *superR = NULL; 923 ASTContext &C = getContext(); 924 925 // FIXME: Handle multi-dimensional arrays. 926 927 while (ER) { 928 superR = ER->getSuperRegion(); 929 930 // FIXME: generalize to symbolic offsets. 931 SVal index = ER->getIndex(); 932 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) { 933 // Update the offset. 934 int64_t i = CI->getValue().getSExtValue(); 935 936 if (i != 0) { 937 QualType elemType = ER->getElementType(); 938 939 // If we are pointing to an incomplete type, go no further. 940 if (!IsCompleteType(C, elemType)) { 941 superR = ER; 942 break; 943 } 944 945 CharUnits size = C.getTypeSizeInChars(elemType); 946 offset += (i * size); 947 } 948 949 // Go to the next ElementRegion (if any). 950 ER = dyn_cast<ElementRegion>(superR); 951 continue; 952 } 953 954 return NULL; 955 } 956 957 assert(superR && "super region cannot be NULL"); 958 return RegionRawOffset(superR, offset); 959} 960 961RegionOffset MemRegion::getAsOffset() const { 962 const MemRegion *R = this; 963 int64_t Offset = 0; 964 965 while (1) { 966 switch (R->getKind()) { 967 default: 968 return RegionOffset(0); 969 case SymbolicRegionKind: 970 case AllocaRegionKind: 971 case CompoundLiteralRegionKind: 972 case CXXThisRegionKind: 973 case StringRegionKind: 974 case VarRegionKind: 975 case CXXTempObjectRegionKind: 976 goto Finish; 977 case ElementRegionKind: { 978 const ElementRegion *ER = cast<ElementRegion>(R); 979 QualType EleTy = ER->getValueType(); 980 981 if (!IsCompleteType(getContext(), EleTy)) 982 return RegionOffset(0); 983 984 SVal Index = ER->getIndex(); 985 if (const nonloc::ConcreteInt *CI=dyn_cast<nonloc::ConcreteInt>(&Index)) { 986 int64_t i = CI->getValue().getSExtValue(); 987 CharUnits Size = getContext().getTypeSizeInChars(EleTy); 988 Offset += i * Size.getQuantity() * 8; 989 } else { 990 // We cannot compute offset for non-concrete index. 991 return RegionOffset(0); 992 } 993 R = ER->getSuperRegion(); 994 break; 995 } 996 case FieldRegionKind: { 997 const FieldRegion *FR = cast<FieldRegion>(R); 998 const RecordDecl *RD = FR->getDecl()->getParent(); 999 if (!RD->isCompleteDefinition()) 1000 // We cannot compute offset for incomplete type. 1001 return RegionOffset(0); 1002 // Get the field number. 1003 unsigned idx = 0; 1004 for (RecordDecl::field_iterator FI = RD->field_begin(), 1005 FE = RD->field_end(); FI != FE; ++FI, ++idx) 1006 if (FR->getDecl() == *FI) 1007 break; 1008 1009 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 1010 // This is offset in bits. 1011 Offset += Layout.getFieldOffset(idx); 1012 R = FR->getSuperRegion(); 1013 break; 1014 } 1015 } 1016 } 1017 1018 Finish: 1019 return RegionOffset(R, Offset); 1020} 1021 1022//===----------------------------------------------------------------------===// 1023// BlockDataRegion 1024//===----------------------------------------------------------------------===// 1025 1026void BlockDataRegion::LazyInitializeReferencedVars() { 1027 if (ReferencedVars) 1028 return; 1029 1030 AnalysisDeclContext *AC = getCodeRegion()->getAnalysisDeclContext(); 1031 AnalysisDeclContext::referenced_decls_iterator I, E; 1032 llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl()); 1033 1034 if (I == E) { 1035 ReferencedVars = (void*) 0x1; 1036 return; 1037 } 1038 1039 MemRegionManager &MemMgr = *getMemRegionManager(); 1040 llvm::BumpPtrAllocator &A = MemMgr.getAllocator(); 1041 BumpVectorContext BC(A); 1042 1043 typedef BumpVector<const MemRegion*> VarVec; 1044 VarVec *BV = (VarVec*) A.Allocate<VarVec>(); 1045 new (BV) VarVec(BC, E - I); 1046 1047 for ( ; I != E; ++I) { 1048 const VarDecl *VD = *I; 1049 const VarRegion *VR = 0; 1050 1051 if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage()) 1052 VR = MemMgr.getVarRegion(VD, this); 1053 else { 1054 if (LC) 1055 VR = MemMgr.getVarRegion(VD, LC); 1056 else { 1057 VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion()); 1058 } 1059 } 1060 1061 assert(VR); 1062 BV->push_back(VR, BC); 1063 } 1064 1065 ReferencedVars = BV; 1066} 1067 1068BlockDataRegion::referenced_vars_iterator 1069BlockDataRegion::referenced_vars_begin() const { 1070 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars(); 1071 1072 BumpVector<const MemRegion*> *Vec = 1073 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars); 1074 1075 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ? 1076 NULL : Vec->begin()); 1077} 1078 1079BlockDataRegion::referenced_vars_iterator 1080BlockDataRegion::referenced_vars_end() const { 1081 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars(); 1082 1083 BumpVector<const MemRegion*> *Vec = 1084 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars); 1085 1086 return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ? 1087 NULL : Vec->end()); 1088} 1089