MemRegion.cpp revision 581deb3da481053c4993c7600f97acf7768caac5
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 537void MemRegion::dumpPretty(raw_ostream &os) const { 538 return; 539} 540 541void VarRegion::dumpPretty(raw_ostream &os) const { 542 os << getDecl()->getName(); 543} 544 545void FieldRegion::dumpPretty(raw_ostream &os) const { 546 superRegion->dumpPretty(os); 547 os << "->" << getDecl(); 548} 549 550//===----------------------------------------------------------------------===// 551// MemRegionManager methods. 552//===----------------------------------------------------------------------===// 553 554template <typename REG> 555const REG *MemRegionManager::LazyAllocate(REG*& region) { 556 if (!region) { 557 region = (REG*) A.Allocate<REG>(); 558 new (region) REG(this); 559 } 560 561 return region; 562} 563 564template <typename REG, typename ARG> 565const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) { 566 if (!region) { 567 region = (REG*) A.Allocate<REG>(); 568 new (region) REG(this, a); 569 } 570 571 return region; 572} 573 574const StackLocalsSpaceRegion* 575MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) { 576 assert(STC); 577 StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC]; 578 579 if (R) 580 return R; 581 582 R = A.Allocate<StackLocalsSpaceRegion>(); 583 new (R) StackLocalsSpaceRegion(this, STC); 584 return R; 585} 586 587const StackArgumentsSpaceRegion * 588MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) { 589 assert(STC); 590 StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC]; 591 592 if (R) 593 return R; 594 595 R = A.Allocate<StackArgumentsSpaceRegion>(); 596 new (R) StackArgumentsSpaceRegion(this, STC); 597 return R; 598} 599 600const GlobalsSpaceRegion 601*MemRegionManager::getGlobalsRegion(MemRegion::Kind K, 602 const CodeTextRegion *CR) { 603 if (!CR) { 604 if (K == MemRegion::GlobalSystemSpaceRegionKind) 605 return LazyAllocate(SystemGlobals); 606 if (K == MemRegion::GlobalImmutableSpaceRegionKind) 607 return LazyAllocate(ImmutableGlobals); 608 assert(K == MemRegion::GlobalInternalSpaceRegionKind); 609 return LazyAllocate(InternalGlobals); 610 } 611 612 assert(K == MemRegion::StaticGlobalSpaceRegionKind); 613 StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR]; 614 if (R) 615 return R; 616 617 R = A.Allocate<StaticGlobalSpaceRegion>(); 618 new (R) StaticGlobalSpaceRegion(this, CR); 619 return R; 620} 621 622const HeapSpaceRegion *MemRegionManager::getHeapRegion() { 623 return LazyAllocate(heap); 624} 625 626const MemSpaceRegion *MemRegionManager::getUnknownRegion() { 627 return LazyAllocate(unknown); 628} 629 630const MemSpaceRegion *MemRegionManager::getCodeRegion() { 631 return LazyAllocate(code); 632} 633 634//===----------------------------------------------------------------------===// 635// Constructing regions. 636//===----------------------------------------------------------------------===// 637const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){ 638 return getSubRegion<StringRegion>(Str, getGlobalsRegion()); 639} 640 641const ObjCStringRegion * 642MemRegionManager::getObjCStringRegion(const ObjCStringLiteral* Str){ 643 return getSubRegion<ObjCStringRegion>(Str, getGlobalsRegion()); 644} 645 646/// Look through a chain of LocationContexts to either find the 647/// StackFrameContext that matches a DeclContext, or find a VarRegion 648/// for a variable captured by a block. 649static llvm::PointerUnion<const StackFrameContext *, const VarRegion *> 650getStackOrCaptureRegionForDeclContext(const LocationContext *LC, 651 const DeclContext *DC, 652 const VarDecl *VD) { 653 while (LC) { 654 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) { 655 if (cast<DeclContext>(SFC->getDecl()) == DC) 656 return SFC; 657 } 658 if (const BlockInvocationContext *BC = 659 dyn_cast<BlockInvocationContext>(LC)) { 660 const BlockDataRegion *BR = 661 static_cast<const BlockDataRegion*>(BC->getContextData()); 662 // FIXME: This can be made more efficient. 663 for (BlockDataRegion::referenced_vars_iterator 664 I = BR->referenced_vars_begin(), 665 E = BR->referenced_vars_end(); I != E; ++I) { 666 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getOriginalRegion())) 667 if (VR->getDecl() == VD) 668 return cast<VarRegion>(I.getCapturedRegion()); 669 } 670 } 671 672 LC = LC->getParent(); 673 } 674 return (const StackFrameContext*)0; 675} 676 677const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D, 678 const LocationContext *LC) { 679 const MemRegion *sReg = 0; 680 681 if (D->hasGlobalStorage() && !D->isStaticLocal()) { 682 683 // First handle the globals defined in system headers. 684 if (C.getSourceManager().isInSystemHeader(D->getLocation())) { 685 // Whitelist the system globals which often DO GET modified, assume the 686 // rest are immutable. 687 if (D->getName().find("errno") != StringRef::npos) 688 sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind); 689 else 690 sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind); 691 692 // Treat other globals as GlobalInternal unless they are constants. 693 } else { 694 QualType GQT = D->getType(); 695 const Type *GT = GQT.getTypePtrOrNull(); 696 // TODO: We could walk the complex types here and see if everything is 697 // constified. 698 if (GT && GQT.isConstQualified() && GT->isArithmeticType()) 699 sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind); 700 else 701 sReg = getGlobalsRegion(); 702 } 703 704 // Finally handle static locals. 705 } else { 706 // FIXME: Once we implement scope handling, we will need to properly lookup 707 // 'D' to the proper LocationContext. 708 const DeclContext *DC = D->getDeclContext(); 709 llvm::PointerUnion<const StackFrameContext *, const VarRegion *> V = 710 getStackOrCaptureRegionForDeclContext(LC, DC, D); 711 712 if (V.is<const VarRegion*>()) 713 return V.get<const VarRegion*>(); 714 715 const StackFrameContext *STC = V.get<const StackFrameContext*>(); 716 717 if (!STC) 718 sReg = getUnknownRegion(); 719 else { 720 if (D->hasLocalStorage()) { 721 sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) 722 ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC)) 723 : static_cast<const MemRegion*>(getStackLocalsRegion(STC)); 724 } 725 else { 726 assert(D->isStaticLocal()); 727 const Decl *D = STC->getDecl(); 728 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) 729 sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind, 730 getFunctionTextRegion(FD)); 731 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { 732 const BlockTextRegion *BTR = 733 getBlockTextRegion(BD, 734 C.getCanonicalType(BD->getSignatureAsWritten()->getType()), 735 STC->getAnalysisDeclContext()); 736 sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind, 737 BTR); 738 } 739 else { 740 // FIXME: For ObjC-methods, we need a new CodeTextRegion. For now 741 // just use the main global memspace. 742 sReg = getGlobalsRegion(); 743 } 744 } 745 } 746 } 747 748 return getSubRegion<VarRegion>(D, sReg); 749} 750 751const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D, 752 const MemRegion *superR) { 753 return getSubRegion<VarRegion>(D, superR); 754} 755 756const BlockDataRegion * 757MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC, 758 const LocationContext *LC) { 759 const MemRegion *sReg = 0; 760 const BlockDecl *BD = BC->getDecl(); 761 if (!BD->hasCaptures()) { 762 // This handles 'static' blocks. 763 sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind); 764 } 765 else { 766 if (LC) { 767 // FIXME: Once we implement scope handling, we want the parent region 768 // to be the scope. 769 const StackFrameContext *STC = LC->getCurrentStackFrame(); 770 assert(STC); 771 sReg = getStackLocalsRegion(STC); 772 } 773 else { 774 // We allow 'LC' to be NULL for cases where want BlockDataRegions 775 // without context-sensitivity. 776 sReg = getUnknownRegion(); 777 } 778 } 779 780 return getSubRegion<BlockDataRegion>(BC, LC, sReg); 781} 782 783const CompoundLiteralRegion* 784MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL, 785 const LocationContext *LC) { 786 787 const MemRegion *sReg = 0; 788 789 if (CL->isFileScope()) 790 sReg = getGlobalsRegion(); 791 else { 792 const StackFrameContext *STC = LC->getCurrentStackFrame(); 793 assert(STC); 794 sReg = getStackLocalsRegion(STC); 795 } 796 797 return getSubRegion<CompoundLiteralRegion>(CL, sReg); 798} 799 800const ElementRegion* 801MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx, 802 const MemRegion* superRegion, 803 ASTContext &Ctx){ 804 805 QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType(); 806 807 llvm::FoldingSetNodeID ID; 808 ElementRegion::ProfileRegion(ID, T, Idx, superRegion); 809 810 void *InsertPos; 811 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos); 812 ElementRegion* R = cast_or_null<ElementRegion>(data); 813 814 if (!R) { 815 R = (ElementRegion*) A.Allocate<ElementRegion>(); 816 new (R) ElementRegion(T, Idx, superRegion); 817 Regions.InsertNode(R, InsertPos); 818 } 819 820 return R; 821} 822 823const FunctionTextRegion * 824MemRegionManager::getFunctionTextRegion(const FunctionDecl *FD) { 825 return getSubRegion<FunctionTextRegion>(FD, getCodeRegion()); 826} 827 828const BlockTextRegion * 829MemRegionManager::getBlockTextRegion(const BlockDecl *BD, CanQualType locTy, 830 AnalysisDeclContext *AC) { 831 return getSubRegion<BlockTextRegion>(BD, locTy, AC, getCodeRegion()); 832} 833 834 835/// getSymbolicRegion - Retrieve or create a "symbolic" memory region. 836const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) { 837 return getSubRegion<SymbolicRegion>(sym, getUnknownRegion()); 838} 839 840const FieldRegion* 841MemRegionManager::getFieldRegion(const FieldDecl *d, 842 const MemRegion* superRegion){ 843 return getSubRegion<FieldRegion>(d, superRegion); 844} 845 846const ObjCIvarRegion* 847MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl *d, 848 const MemRegion* superRegion) { 849 return getSubRegion<ObjCIvarRegion>(d, superRegion); 850} 851 852const CXXTempObjectRegion* 853MemRegionManager::getCXXTempObjectRegion(Expr const *E, 854 LocationContext const *LC) { 855 const StackFrameContext *SFC = LC->getCurrentStackFrame(); 856 assert(SFC); 857 return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC)); 858} 859 860const CXXBaseObjectRegion * 861MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *decl, 862 const MemRegion *superRegion) { 863 return getSubRegion<CXXBaseObjectRegion>(decl, superRegion); 864} 865 866const CXXThisRegion* 867MemRegionManager::getCXXThisRegion(QualType thisPointerTy, 868 const LocationContext *LC) { 869 const StackFrameContext *STC = LC->getCurrentStackFrame(); 870 assert(STC); 871 const PointerType *PT = thisPointerTy->getAs<PointerType>(); 872 assert(PT); 873 return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC)); 874} 875 876const AllocaRegion* 877MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt, 878 const LocationContext *LC) { 879 const StackFrameContext *STC = LC->getCurrentStackFrame(); 880 assert(STC); 881 return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC)); 882} 883 884const MemSpaceRegion *MemRegion::getMemorySpace() const { 885 const MemRegion *R = this; 886 const SubRegion* SR = dyn_cast<SubRegion>(this); 887 888 while (SR) { 889 R = SR->getSuperRegion(); 890 SR = dyn_cast<SubRegion>(R); 891 } 892 893 return dyn_cast<MemSpaceRegion>(R); 894} 895 896bool MemRegion::hasStackStorage() const { 897 return isa<StackSpaceRegion>(getMemorySpace()); 898} 899 900bool MemRegion::hasStackNonParametersStorage() const { 901 return isa<StackLocalsSpaceRegion>(getMemorySpace()); 902} 903 904bool MemRegion::hasStackParametersStorage() const { 905 return isa<StackArgumentsSpaceRegion>(getMemorySpace()); 906} 907 908bool MemRegion::hasGlobalsOrParametersStorage() const { 909 const MemSpaceRegion *MS = getMemorySpace(); 910 return isa<StackArgumentsSpaceRegion>(MS) || 911 isa<GlobalsSpaceRegion>(MS); 912} 913 914// getBaseRegion strips away all elements and fields, and get the base region 915// of them. 916const MemRegion *MemRegion::getBaseRegion() const { 917 const MemRegion *R = this; 918 while (true) { 919 switch (R->getKind()) { 920 case MemRegion::ElementRegionKind: 921 case MemRegion::FieldRegionKind: 922 case MemRegion::ObjCIvarRegionKind: 923 case MemRegion::CXXBaseObjectRegionKind: 924 R = cast<SubRegion>(R)->getSuperRegion(); 925 continue; 926 default: 927 break; 928 } 929 break; 930 } 931 return R; 932} 933 934//===----------------------------------------------------------------------===// 935// View handling. 936//===----------------------------------------------------------------------===// 937 938const MemRegion *MemRegion::StripCasts() const { 939 const MemRegion *R = this; 940 while (true) { 941 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { 942 // FIXME: generalize. Essentially we want to strip away ElementRegions 943 // that were layered on a symbolic region because of casts. We only 944 // want to strip away ElementRegions, however, where the index is 0. 945 SVal index = ER->getIndex(); 946 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) { 947 if (CI->getValue().getSExtValue() == 0) { 948 R = ER->getSuperRegion(); 949 continue; 950 } 951 } 952 } 953 break; 954 } 955 return R; 956} 957 958// FIXME: Merge with the implementation of the same method in Store.cpp 959static bool IsCompleteType(ASTContext &Ctx, QualType Ty) { 960 if (const RecordType *RT = Ty->getAs<RecordType>()) { 961 const RecordDecl *D = RT->getDecl(); 962 if (!D->getDefinition()) 963 return false; 964 } 965 966 return true; 967} 968 969RegionRawOffset ElementRegion::getAsArrayOffset() const { 970 CharUnits offset = CharUnits::Zero(); 971 const ElementRegion *ER = this; 972 const MemRegion *superR = NULL; 973 ASTContext &C = getContext(); 974 975 // FIXME: Handle multi-dimensional arrays. 976 977 while (ER) { 978 superR = ER->getSuperRegion(); 979 980 // FIXME: generalize to symbolic offsets. 981 SVal index = ER->getIndex(); 982 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) { 983 // Update the offset. 984 int64_t i = CI->getValue().getSExtValue(); 985 986 if (i != 0) { 987 QualType elemType = ER->getElementType(); 988 989 // If we are pointing to an incomplete type, go no further. 990 if (!IsCompleteType(C, elemType)) { 991 superR = ER; 992 break; 993 } 994 995 CharUnits size = C.getTypeSizeInChars(elemType); 996 offset += (i * size); 997 } 998 999 // Go to the next ElementRegion (if any). 1000 ER = dyn_cast<ElementRegion>(superR); 1001 continue; 1002 } 1003 1004 return NULL; 1005 } 1006 1007 assert(superR && "super region cannot be NULL"); 1008 return RegionRawOffset(superR, offset); 1009} 1010 1011RegionOffset MemRegion::getAsOffset() const { 1012 const MemRegion *R = this; 1013 int64_t Offset = 0; 1014 1015 while (1) { 1016 switch (R->getKind()) { 1017 default: 1018 return RegionOffset(0); 1019 case SymbolicRegionKind: 1020 case AllocaRegionKind: 1021 case CompoundLiteralRegionKind: 1022 case CXXThisRegionKind: 1023 case StringRegionKind: 1024 case VarRegionKind: 1025 case CXXTempObjectRegionKind: 1026 goto Finish; 1027 case ElementRegionKind: { 1028 const ElementRegion *ER = cast<ElementRegion>(R); 1029 QualType EleTy = ER->getValueType(); 1030 1031 if (!IsCompleteType(getContext(), EleTy)) 1032 return RegionOffset(0); 1033 1034 SVal Index = ER->getIndex(); 1035 if (const nonloc::ConcreteInt *CI=dyn_cast<nonloc::ConcreteInt>(&Index)) { 1036 int64_t i = CI->getValue().getSExtValue(); 1037 CharUnits Size = getContext().getTypeSizeInChars(EleTy); 1038 Offset += i * Size.getQuantity() * 8; 1039 } else { 1040 // We cannot compute offset for non-concrete index. 1041 return RegionOffset(0); 1042 } 1043 R = ER->getSuperRegion(); 1044 break; 1045 } 1046 case FieldRegionKind: { 1047 const FieldRegion *FR = cast<FieldRegion>(R); 1048 const RecordDecl *RD = FR->getDecl()->getParent(); 1049 if (!RD->isCompleteDefinition()) 1050 // We cannot compute offset for incomplete type. 1051 return RegionOffset(0); 1052 // Get the field number. 1053 unsigned idx = 0; 1054 for (RecordDecl::field_iterator FI = RD->field_begin(), 1055 FE = RD->field_end(); FI != FE; ++FI, ++idx) 1056 if (FR->getDecl() == *FI) 1057 break; 1058 1059 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 1060 // This is offset in bits. 1061 Offset += Layout.getFieldOffset(idx); 1062 R = FR->getSuperRegion(); 1063 break; 1064 } 1065 } 1066 } 1067 1068 Finish: 1069 return RegionOffset(R, Offset); 1070} 1071 1072//===----------------------------------------------------------------------===// 1073// BlockDataRegion 1074//===----------------------------------------------------------------------===// 1075 1076void BlockDataRegion::LazyInitializeReferencedVars() { 1077 if (ReferencedVars) 1078 return; 1079 1080 AnalysisDeclContext *AC = getCodeRegion()->getAnalysisDeclContext(); 1081 AnalysisDeclContext::referenced_decls_iterator I, E; 1082 llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl()); 1083 1084 if (I == E) { 1085 ReferencedVars = (void*) 0x1; 1086 return; 1087 } 1088 1089 MemRegionManager &MemMgr = *getMemRegionManager(); 1090 llvm::BumpPtrAllocator &A = MemMgr.getAllocator(); 1091 BumpVectorContext BC(A); 1092 1093 typedef BumpVector<const MemRegion*> VarVec; 1094 VarVec *BV = (VarVec*) A.Allocate<VarVec>(); 1095 new (BV) VarVec(BC, E - I); 1096 VarVec *BVOriginal = (VarVec*) A.Allocate<VarVec>(); 1097 new (BVOriginal) VarVec(BC, E - I); 1098 1099 for ( ; I != E; ++I) { 1100 const VarDecl *VD = *I; 1101 const VarRegion *VR = 0; 1102 const VarRegion *OriginalVR = 0; 1103 1104 if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage()) { 1105 VR = MemMgr.getVarRegion(VD, this); 1106 OriginalVR = MemMgr.getVarRegion(VD, LC); 1107 } 1108 else { 1109 if (LC) { 1110 VR = MemMgr.getVarRegion(VD, LC); 1111 OriginalVR = VR; 1112 } 1113 else { 1114 VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion()); 1115 OriginalVR = MemMgr.getVarRegion(VD, LC); 1116 } 1117 } 1118 1119 assert(VR); 1120 assert(OriginalVR); 1121 BV->push_back(VR, BC); 1122 BVOriginal->push_back(OriginalVR, BC); 1123 } 1124 1125 ReferencedVars = BV; 1126 OriginalVars = BVOriginal; 1127} 1128 1129BlockDataRegion::referenced_vars_iterator 1130BlockDataRegion::referenced_vars_begin() const { 1131 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars(); 1132 1133 BumpVector<const MemRegion*> *Vec = 1134 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars); 1135 1136 if (Vec == (void*) 0x1) 1137 return BlockDataRegion::referenced_vars_iterator(0, 0); 1138 1139 BumpVector<const MemRegion*> *VecOriginal = 1140 static_cast<BumpVector<const MemRegion*>*>(OriginalVars); 1141 1142 return BlockDataRegion::referenced_vars_iterator(Vec->begin(), 1143 VecOriginal->begin()); 1144} 1145 1146BlockDataRegion::referenced_vars_iterator 1147BlockDataRegion::referenced_vars_end() const { 1148 const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars(); 1149 1150 BumpVector<const MemRegion*> *Vec = 1151 static_cast<BumpVector<const MemRegion*>*>(ReferencedVars); 1152 1153 if (Vec == (void*) 0x1) 1154 return BlockDataRegion::referenced_vars_iterator(0, 0); 1155 1156 BumpVector<const MemRegion*> *VecOriginal = 1157 static_cast<BumpVector<const MemRegion*>*>(OriginalVars); 1158 1159 return BlockDataRegion::referenced_vars_iterator(Vec->end(), 1160 VecOriginal->end()); 1161} 1162