2008-10-04 13:50:14 +08:00
|
|
|
//== MemRegion.cpp - Abstract memory regions for static analysis --*- C++ -*--//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines MemRegion and its subclasses. MemRegion defines a
|
|
|
|
// partially-typed abstraction of memory useful for path-sensitive dataflow
|
|
|
|
// analyses.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/MemRegion.h"
|
2009-08-01 14:17:29 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/ValueManager.h"
|
2009-08-22 07:25:54 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/AnalysisContext.h"
|
2009-11-26 10:34:36 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2008-10-04 13:50:14 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemRegion Construction.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
template<typename RegionTy> struct MemRegionManagerTrait;
|
|
|
|
|
|
|
|
template <typename RegionTy, typename A1>
|
|
|
|
RegionTy* MemRegionManager::getRegion(const A1 a1) {
|
|
|
|
|
|
|
|
const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
|
|
|
|
MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1);
|
|
|
|
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
RegionTy::ProfileRegion(ID, a1, superRegion);
|
|
|
|
void* InsertPos;
|
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
|
|
|
|
|
|
|
if (!R) {
|
|
|
|
R = (RegionTy*) A.Allocate<RegionTy>();
|
|
|
|
new (R) RegionTy(a1, superRegion);
|
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename RegionTy, typename A1>
|
|
|
|
RegionTy* MemRegionManager::getSubRegion(const A1 a1,
|
|
|
|
const MemRegion *superRegion) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
RegionTy::ProfileRegion(ID, a1, superRegion);
|
|
|
|
void* InsertPos;
|
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
|
|
|
|
|
|
|
if (!R) {
|
|
|
|
R = (RegionTy*) A.Allocate<RegionTy>();
|
|
|
|
new (R) RegionTy(a1, superRegion);
|
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename RegionTy, typename A1, typename A2>
|
|
|
|
RegionTy* MemRegionManager::getRegion(const A1 a1, const A2 a2) {
|
|
|
|
|
|
|
|
const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
|
|
|
|
MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1, a2);
|
|
|
|
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
RegionTy::ProfileRegion(ID, a1, a2, superRegion);
|
|
|
|
void* InsertPos;
|
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
|
|
|
|
|
|
|
if (!R) {
|
|
|
|
R = (RegionTy*) A.Allocate<RegionTy>();
|
|
|
|
new (R) RegionTy(a1, a2, superRegion);
|
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename RegionTy, typename A1, typename A2>
|
|
|
|
RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2,
|
|
|
|
const MemRegion *superRegion) {
|
|
|
|
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
RegionTy::ProfileRegion(ID, a1, a2, superRegion);
|
|
|
|
void* InsertPos;
|
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
|
|
|
|
|
|
|
if (!R) {
|
|
|
|
R = (RegionTy*) A.Allocate<RegionTy>();
|
|
|
|
new (R) RegionTy(a1, a2, superRegion);
|
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Traits for constructing regions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
template <> struct MemRegionManagerTrait<AllocaRegion> {
|
|
|
|
typedef MemRegion SuperRegionTy;
|
|
|
|
static const SuperRegionTy* getSuperRegion(MemRegionManager& MRMgr,
|
|
|
|
const Expr *, unsigned) {
|
|
|
|
return MRMgr.getStackRegion();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MemRegionManagerTrait<CompoundLiteralRegion> {
|
|
|
|
typedef MemRegion SuperRegionTy;
|
|
|
|
static const SuperRegionTy* getSuperRegion(MemRegionManager& MRMgr,
|
|
|
|
const CompoundLiteralExpr *CL) {
|
|
|
|
|
|
|
|
return CL->isFileScope() ? MRMgr.getGlobalsRegion()
|
|
|
|
: MRMgr.getStackRegion();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MemRegionManagerTrait<StringRegion> {
|
|
|
|
typedef MemSpaceRegion SuperRegionTy;
|
|
|
|
static const SuperRegionTy* getSuperRegion(MemRegionManager& MRMgr,
|
|
|
|
const StringLiteral*) {
|
|
|
|
return MRMgr.getGlobalsRegion();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MemRegionManagerTrait<VarRegion> {
|
|
|
|
typedef MemRegion SuperRegionTy;
|
|
|
|
static const SuperRegionTy* getSuperRegion(MemRegionManager &MRMgr,
|
|
|
|
const VarDecl *D,
|
|
|
|
const LocationContext *LC) {
|
|
|
|
|
|
|
|
// FIXME: Make stack regions have a location context?
|
|
|
|
|
|
|
|
if (D->hasLocalStorage()) {
|
|
|
|
return isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
|
|
|
|
? MRMgr.getStackArgumentsRegion() : MRMgr.getStackRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
return MRMgr.getGlobalsRegion();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MemRegionManagerTrait<SymbolicRegion> {
|
|
|
|
typedef MemRegion SuperRegionTy;
|
|
|
|
static const SuperRegionTy* getSuperRegion(MemRegionManager& MRMgr,
|
|
|
|
SymbolRef) {
|
|
|
|
return MRMgr.getUnknownRegion();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct MemRegionManagerTrait<FunctionTextRegion> {
|
|
|
|
typedef MemSpaceRegion SuperRegionTy;
|
|
|
|
static const SuperRegionTy* getSuperRegion(MemRegionManager& MRMgr,
|
|
|
|
const FunctionDecl*) {
|
|
|
|
return MRMgr.getCodeRegion();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<> struct MemRegionManagerTrait<BlockTextRegion> {
|
|
|
|
typedef MemSpaceRegion SuperRegionTy;
|
|
|
|
static const SuperRegionTy* getSuperRegion(MemRegionManager& MRMgr,
|
|
|
|
const BlockDecl*, CanQualType) {
|
|
|
|
return MRMgr.getCodeRegion();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-23 07:13:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-11-26 10:34:36 +08:00
|
|
|
// Object destruction.
|
2009-06-23 07:13:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-10-04 13:50:14 +08:00
|
|
|
|
|
|
|
MemRegion::~MemRegion() {}
|
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
MemRegionManager::~MemRegionManager() {
|
|
|
|
// All regions and their data are BumpPtrAllocated. No need to call
|
|
|
|
// their destructors.
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Basic methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-08 21:17:14 +08:00
|
|
|
bool SubRegion::isSubRegionOf(const MemRegion* R) const {
|
|
|
|
const MemRegion* r = getSuperRegion();
|
|
|
|
while (r != 0) {
|
|
|
|
if (r == R)
|
|
|
|
return true;
|
|
|
|
if (const SubRegion* sr = dyn_cast<SubRegion>(r))
|
|
|
|
r = sr->getSuperRegion();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-06-23 08:46:41 +08:00
|
|
|
MemRegionManager* SubRegion::getMemRegionManager() const {
|
|
|
|
const SubRegion* r = this;
|
|
|
|
do {
|
|
|
|
const MemRegion *superRegion = r->getSuperRegion();
|
|
|
|
if (const SubRegion *sr = dyn_cast<SubRegion>(superRegion)) {
|
|
|
|
r = sr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return superRegion->getMemRegionManager();
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
ID.AddInteger((unsigned)getKind());
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const StringLiteral* Str,
|
2008-10-25 22:13:41 +08:00
|
|
|
const MemRegion* superRegion) {
|
|
|
|
ID.AddInteger((unsigned) StringRegionKind);
|
|
|
|
ID.AddPointer(Str);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2008-11-02 08:34:33 +08:00
|
|
|
void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2009-06-23 08:15:41 +08:00
|
|
|
const Expr* Ex, unsigned cnt,
|
|
|
|
const MemRegion *) {
|
2008-11-02 08:34:33 +08:00
|
|
|
ID.AddInteger((unsigned) AllocaRegionKind);
|
|
|
|
ID.AddPointer(Ex);
|
|
|
|
ID.AddInteger(cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2009-06-23 08:15:41 +08:00
|
|
|
ProfileRegion(ID, Ex, Cnt, superRegion);
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
2008-10-28 04:57:58 +08:00
|
|
|
void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const CompoundLiteralExpr* CL,
|
|
|
|
const MemRegion* superRegion) {
|
|
|
|
ID.AddInteger((unsigned) CompoundLiteralRegionKind);
|
|
|
|
ID.AddPointer(CL);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D,
|
|
|
|
const MemRegion* superRegion, Kind k) {
|
|
|
|
ID.AddInteger((unsigned) k);
|
|
|
|
ID.AddPointer(D);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
|
|
|
|
}
|
|
|
|
|
2009-08-22 06:28:32 +08:00
|
|
|
void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
VarRegion::ProfileRegion(ID, getDecl(), LC, superRegion);
|
|
|
|
}
|
|
|
|
|
2009-06-23 07:13:13 +08:00
|
|
|
void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
|
|
|
|
const MemRegion *sreg) {
|
2008-10-18 04:28:54 +08:00
|
|
|
ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
|
2008-12-05 10:39:38 +08:00
|
|
|
ID.Add(sym);
|
2009-06-23 07:13:13 +08:00
|
|
|
ID.AddPointer(sreg);
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2009-06-23 07:13:13 +08:00
|
|
|
SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
2009-05-04 14:18:28 +08:00
|
|
|
void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType ElementType, SVal Idx,
|
2008-10-21 13:27:10 +08:00
|
|
|
const MemRegion* superRegion) {
|
|
|
|
ID.AddInteger(MemRegion::ElementRegionKind);
|
2009-05-04 14:18:28 +08:00
|
|
|
ID.Add(ElementType);
|
2008-10-21 13:27:10 +08:00
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
Idx.Profile(ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2009-05-04 14:18:28 +08:00
|
|
|
ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
|
2008-10-21 13:27:10 +08:00
|
|
|
}
|
2008-10-27 21:17:02 +08:00
|
|
|
|
2009-11-25 09:32:22 +08:00
|
|
|
void FunctionTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const FunctionDecl *FD,
|
|
|
|
const MemRegion*) {
|
|
|
|
ID.AddInteger(MemRegion::FunctionTextRegionKind);
|
2009-08-28 12:49:15 +08:00
|
|
|
ID.AddPointer(FD);
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2009-11-25 09:32:22 +08:00
|
|
|
void FunctionTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
FunctionTextRegion::ProfileRegion(ID, FD, superRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const BlockDecl *BD, CanQualType,
|
|
|
|
const MemRegion*) {
|
|
|
|
ID.AddInteger(MemRegion::BlockTextRegionKind);
|
|
|
|
ID.AddPointer(BD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
BlockTextRegion::ProfileRegion(ID, BD, locTy, superRegion);
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2009-11-26 07:53:07 +08:00
|
|
|
void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const BlockTextRegion *BC,
|
|
|
|
const LocationContext *LC,
|
|
|
|
const MemRegion *) {
|
|
|
|
ID.AddInteger(MemRegion::BlockDataRegionKind);
|
|
|
|
ID.AddPointer(BC);
|
|
|
|
ID.AddPointer(LC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
BlockDataRegion::ProfileRegion(ID, BC, LC, NULL);
|
|
|
|
}
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region pretty-printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-14 07:31:04 +08:00
|
|
|
void MemRegion::dump() const {
|
|
|
|
dumpToStream(llvm::errs());
|
2009-07-03 01:24:10 +08:00
|
|
|
}
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
std::string MemRegion::getString() const {
|
|
|
|
std::string s;
|
|
|
|
llvm::raw_string_ostream os(s);
|
2009-07-14 07:31:04 +08:00
|
|
|
dumpToStream(os);
|
2008-10-04 13:50:14 +08:00
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2009-07-14 07:31:04 +08:00
|
|
|
void MemRegion::dumpToStream(llvm::raw_ostream& os) const {
|
2008-10-04 13:50:14 +08:00
|
|
|
os << "<Unknown Region>";
|
|
|
|
}
|
|
|
|
|
2009-07-14 07:31:04 +08:00
|
|
|
void AllocaRegion::dumpToStream(llvm::raw_ostream& os) const {
|
2008-11-02 08:34:33 +08:00
|
|
|
os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
|
|
|
|
}
|
|
|
|
|
2009-11-25 09:32:22 +08:00
|
|
|
void FunctionTextRegion::dumpToStream(llvm::raw_ostream& os) const {
|
2009-08-28 12:49:15 +08:00
|
|
|
os << "code{" << getDecl()->getDeclName().getAsString() << '}';
|
2009-04-22 03:56:58 +08:00
|
|
|
}
|
|
|
|
|
2009-11-25 09:32:22 +08:00
|
|
|
void BlockTextRegion::dumpToStream(llvm::raw_ostream& os) const {
|
2009-11-26 07:53:07 +08:00
|
|
|
os << "block_code{" << (void*) this << '}';
|
2009-11-25 09:32:22 +08:00
|
|
|
}
|
|
|
|
|
2009-11-26 07:53:07 +08:00
|
|
|
void BlockDataRegion::dumpToStream(llvm::raw_ostream& os) const {
|
|
|
|
os << "block_data{" << BC << '}';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-14 07:31:04 +08:00
|
|
|
void CompoundLiteralRegion::dumpToStream(llvm::raw_ostream& os) const {
|
2009-04-22 02:09:22 +08:00
|
|
|
// FIXME: More elaborate pretty-printing.
|
|
|
|
os << "{ " << (void*) CL << " }";
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-07-14 07:31:04 +08:00
|
|
|
void ElementRegion::dumpToStream(llvm::raw_ostream& os) const {
|
2009-08-01 14:17:29 +08:00
|
|
|
os << "element{" << superRegion << ','
|
|
|
|
<< Index << ',' << getElementType().getAsString() << '}';
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
2009-07-14 07:31:04 +08:00
|
|
|
void FieldRegion::dumpToStream(llvm::raw_ostream& os) const {
|
|
|
|
os << superRegion << "->" << getDecl()->getNameAsString();
|
2008-10-18 05:05:44 +08:00
|
|
|
}
|
|
|
|
|
2009-07-20 04:36:24 +08:00
|
|
|
void ObjCIvarRegion::dumpToStream(llvm::raw_ostream& os) const {
|
|
|
|
os << "ivar{" << superRegion << ',' << getDecl()->getNameAsString() << '}';
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void StringRegion::dumpToStream(llvm::raw_ostream& os) const {
|
2009-07-20 04:38:24 +08:00
|
|
|
Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOptions()));
|
2008-10-24 14:30:07 +08:00
|
|
|
}
|
|
|
|
|
2009-07-14 07:31:04 +08:00
|
|
|
void SymbolicRegion::dumpToStream(llvm::raw_ostream& os) const {
|
2009-07-14 07:38:57 +08:00
|
|
|
os << "SymRegion{" << sym << '}';
|
2008-10-28 04:57:58 +08:00
|
|
|
}
|
|
|
|
|
2009-07-14 07:31:04 +08:00
|
|
|
void VarRegion::dumpToStream(llvm::raw_ostream& os) const {
|
2009-04-22 02:09:22 +08:00
|
|
|
os << cast<VarDecl>(D)->getNameAsString();
|
2008-11-10 21:05:26 +08:00
|
|
|
}
|
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
void RegionRawOffset::dump() const {
|
|
|
|
dumpToStream(llvm::errs());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegionRawOffset::dumpToStream(llvm::raw_ostream& os) const {
|
|
|
|
os << "raw_offset{" << getRegion() << ',' << getByteOffset() << '}';
|
|
|
|
}
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemRegionManager methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-08-01 14:17:29 +08:00
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const MemSpaceRegion *MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!region) {
|
2008-10-04 13:50:14 +08:00
|
|
|
region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
|
2009-06-23 08:46:41 +08:00
|
|
|
new (region) MemSpaceRegion(this);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
2009-06-23 08:46:41 +08:00
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const MemSpaceRegion *MemRegionManager::getStackRegion() {
|
2008-10-04 13:50:14 +08:00
|
|
|
return LazyAllocate(stack);
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const MemSpaceRegion *MemRegionManager::getStackArgumentsRegion() {
|
2009-07-03 02:14:59 +08:00
|
|
|
return LazyAllocate(stackArguments);
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const MemSpaceRegion *MemRegionManager::getGlobalsRegion() {
|
2008-10-04 13:50:14 +08:00
|
|
|
return LazyAllocate(globals);
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const MemSpaceRegion *MemRegionManager::getHeapRegion() {
|
2008-10-04 13:50:14 +08:00
|
|
|
return LazyAllocate(heap);
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const MemSpaceRegion *MemRegionManager::getUnknownRegion() {
|
2008-10-08 10:50:44 +08:00
|
|
|
return LazyAllocate(unknown);
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const MemSpaceRegion *MemRegionManager::getCodeRegion() {
|
2009-04-10 16:45:10 +08:00
|
|
|
return LazyAllocate(code);
|
|
|
|
}
|
|
|
|
|
2009-06-23 07:13:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Constructing regions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-10-25 22:13:41 +08:00
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) {
|
2009-06-23 07:13:13 +08:00
|
|
|
return getRegion<StringRegion>(Str);
|
2008-10-25 22:13:41 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
|
|
|
|
const LocationContext *LC) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-22 07:25:54 +08:00
|
|
|
// FIXME: Once we implement scope handling, we will need to properly lookup
|
|
|
|
// 'D' to the proper LocationContext. For now, just strip down to the
|
|
|
|
// StackFrame.
|
|
|
|
while (!isa<StackFrameContext>(LC))
|
|
|
|
LC = LC->getParent();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-22 06:28:32 +08:00
|
|
|
return getRegion<VarRegion>(D, LC);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const BlockDataRegion *
|
|
|
|
MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC,
|
|
|
|
const LocationContext *LC) {
|
2009-11-26 07:53:07 +08:00
|
|
|
// FIXME: Once we implement scope handling, we will need to properly lookup
|
|
|
|
// 'D' to the proper LocationContext. For now, just strip down to the
|
|
|
|
// StackFrame.
|
|
|
|
while (!isa<StackFrameContext>(LC))
|
|
|
|
LC = LC->getParent();
|
|
|
|
|
|
|
|
return getSubRegion<BlockDataRegion>(BC, LC, getStackRegion());
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const CompoundLiteralRegion*
|
2008-10-28 04:57:58 +08:00
|
|
|
MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) {
|
2009-06-23 07:13:13 +08:00
|
|
|
return getRegion<CompoundLiteralRegion>(CL);
|
2008-10-28 04:57:58 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const ElementRegion*
|
2009-05-04 14:18:28 +08:00
|
|
|
MemRegionManager::getElementRegion(QualType elementType, SVal Idx,
|
2009-07-16 09:33:37 +08:00
|
|
|
const MemRegion* superRegion,
|
|
|
|
ASTContext& Ctx){
|
2009-06-16 17:55:50 +08:00
|
|
|
|
|
|
|
QualType T = Ctx.getCanonicalType(elementType);
|
2008-12-14 03:24:37 +08:00
|
|
|
|
2008-10-21 13:27:10 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2009-06-16 17:55:50 +08:00
|
|
|
ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
|
2008-10-21 13:27:10 +08:00
|
|
|
|
|
|
|
void* InsertPos;
|
|
|
|
MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
ElementRegion* R = cast_or_null<ElementRegion>(data);
|
|
|
|
|
|
|
|
if (!R) {
|
|
|
|
R = (ElementRegion*) A.Allocate<ElementRegion>();
|
2009-06-16 17:55:50 +08:00
|
|
|
new (R) ElementRegion(T, Idx, superRegion);
|
2008-10-21 13:27:10 +08:00
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const FunctionTextRegion *
|
2009-11-25 09:32:22 +08:00
|
|
|
MemRegionManager::getFunctionTextRegion(const FunctionDecl *FD) {
|
|
|
|
return getRegion<FunctionTextRegion>(FD);
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const BlockTextRegion *
|
|
|
|
MemRegionManager::getBlockTextRegion(const BlockDecl *BD, CanQualType locTy) {
|
2009-11-25 09:32:22 +08:00
|
|
|
return getRegion<BlockTextRegion>(BD, locTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-18 04:28:54 +08:00
|
|
|
/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
|
2009-12-04 08:26:31 +08:00
|
|
|
const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) {
|
2009-06-23 07:13:13 +08:00
|
|
|
return getRegion<SymbolicRegion>(sym);
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const FieldRegion *
|
|
|
|
MemRegionManager::getFieldRegion(const FieldDecl* d,
|
|
|
|
const MemRegion* superRegion){
|
2009-07-11 00:51:45 +08:00
|
|
|
return getSubRegion<FieldRegion>(d, superRegion);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const ObjCIvarRegion*
|
2008-10-18 04:28:54 +08:00
|
|
|
MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
|
|
|
|
const MemRegion* superRegion) {
|
2009-07-11 00:51:45 +08:00
|
|
|
return getSubRegion<ObjCIvarRegion>(d, superRegion);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const ObjCObjectRegion*
|
2008-10-25 04:30:08 +08:00
|
|
|
MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
|
2009-06-23 08:04:09 +08:00
|
|
|
const MemRegion* superRegion) {
|
2009-07-11 00:51:45 +08:00
|
|
|
return getSubRegion<ObjCObjectRegion>(d, superRegion);
|
2008-10-25 04:30:08 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const AllocaRegion*
|
|
|
|
MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
|
2009-06-23 08:15:41 +08:00
|
|
|
return getRegion<AllocaRegion>(E, cnt);
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
const MemSpaceRegion *MemRegion::getMemorySpace() const {
|
|
|
|
const MemRegion *R = this;
|
|
|
|
const SubRegion* SR = dyn_cast<SubRegion>(this);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 04:28:54 +08:00
|
|
|
while (SR) {
|
2009-06-24 02:17:08 +08:00
|
|
|
R = SR->getSuperRegion();
|
|
|
|
SR = dyn_cast<SubRegion>(R);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
return dyn_cast<MemSpaceRegion>(R);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
2009-04-11 08:11:10 +08:00
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
bool MemRegion::hasStackStorage() const {
|
2009-07-03 02:14:59 +08:00
|
|
|
if (const MemSpaceRegion *MS = getMemorySpace()) {
|
|
|
|
MemRegionManager *Mgr = getMemRegionManager();
|
|
|
|
return MS == Mgr->getStackRegion() || MS == Mgr->getStackArgumentsRegion();
|
|
|
|
}
|
2009-06-23 10:51:21 +08:00
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-06-23 10:51:21 +08:00
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
bool MemRegion::hasHeapStorage() const {
|
|
|
|
if (const MemSpaceRegion *MS = getMemorySpace())
|
|
|
|
return MS == getMemRegionManager()->getHeapRegion();
|
2009-06-23 10:51:21 +08:00
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-06-23 10:51:21 +08:00
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
bool MemRegion::hasHeapOrStackStorage() const {
|
|
|
|
if (const MemSpaceRegion *MS = getMemorySpace()) {
|
|
|
|
MemRegionManager *Mgr = getMemRegionManager();
|
2009-07-03 02:14:59 +08:00
|
|
|
return MS == Mgr->getHeapRegion()
|
|
|
|
|| MS == Mgr->getStackRegion()
|
|
|
|
|| MS == Mgr->getStackArgumentsRegion();
|
2009-06-23 10:51:21 +08:00
|
|
|
}
|
|
|
|
return false;
|
2009-07-03 02:25:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MemRegion::hasGlobalsStorage() const {
|
|
|
|
if (const MemSpaceRegion *MS = getMemorySpace())
|
|
|
|
return MS == getMemRegionManager()->getGlobalsRegion();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:02:15 +08:00
|
|
|
bool MemRegion::hasParametersStorage() const {
|
|
|
|
if (const MemSpaceRegion *MS = getMemorySpace())
|
|
|
|
return MS == getMemRegionManager()->getStackArgumentsRegion();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-03 06:02:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-03 02:25:09 +08:00
|
|
|
bool MemRegion::hasGlobalsOrParametersStorage() const {
|
|
|
|
if (const MemSpaceRegion *MS = getMemorySpace()) {
|
|
|
|
MemRegionManager *Mgr = getMemRegionManager();
|
|
|
|
return MS == Mgr->getGlobalsRegion()
|
|
|
|
|| MS == Mgr->getStackArgumentsRegion();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-04-11 08:11:10 +08:00
|
|
|
|
2009-11-10 10:37:53 +08:00
|
|
|
// getBaseRegion strips away all elements and fields, and get the base region
|
|
|
|
// of them.
|
|
|
|
const MemRegion *MemRegion::getBaseRegion() const {
|
|
|
|
const MemRegion *R = this;
|
|
|
|
while (true) {
|
|
|
|
if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
|
|
|
|
R = ER->getSuperRegion();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
|
|
|
|
R = FR->getSuperRegion();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// View handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-10 10:17:20 +08:00
|
|
|
const MemRegion *MemRegion::StripCasts() const {
|
2009-07-30 02:14:27 +08:00
|
|
|
const MemRegion *R = this;
|
|
|
|
while (true) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
|
2009-07-30 02:14:27 +08:00
|
|
|
// FIXME: generalize. Essentially we want to strip away ElementRegions
|
|
|
|
// that were layered on a symbolic region because of casts. We only
|
|
|
|
// want to strip away ElementRegions, however, where the index is 0.
|
|
|
|
SVal index = ER->getIndex();
|
|
|
|
if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
|
2009-08-01 14:17:29 +08:00
|
|
|
if (CI->getValue().getSExtValue() == 0) {
|
2009-07-30 02:14:27 +08:00
|
|
|
R = ER->getSuperRegion();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return R;
|
|
|
|
}
|
2009-08-01 14:17:29 +08:00
|
|
|
|
|
|
|
// FIXME: Merge with the implementation of the same method in Store.cpp
|
|
|
|
static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
|
|
|
|
if (const RecordType *RT = Ty->getAs<RecordType>()) {
|
|
|
|
const RecordDecl *D = RT->getDecl();
|
|
|
|
if (!D->getDefinition(Ctx))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionRawOffset ElementRegion::getAsRawOffset() const {
|
|
|
|
int64_t offset = 0;
|
|
|
|
const ElementRegion *ER = this;
|
|
|
|
const MemRegion *superR = NULL;
|
|
|
|
ASTContext &C = getContext();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// FIXME: Handle multi-dimensional arrays.
|
|
|
|
|
|
|
|
while (ER) {
|
|
|
|
superR = ER->getSuperRegion();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// FIXME: generalize to symbolic offsets.
|
|
|
|
SVal index = ER->getIndex();
|
|
|
|
if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) {
|
|
|
|
// Update the offset.
|
|
|
|
int64_t i = CI->getValue().getSExtValue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
if (i != 0) {
|
|
|
|
QualType elemType = ER->getElementType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
// If we are pointing to an incomplete type, go no further.
|
|
|
|
if (!IsCompleteType(C, elemType)) {
|
|
|
|
superR = ER;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
int64_t size = (int64_t) (C.getTypeSize(elemType) / 8);
|
|
|
|
offset += (i * size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go to the next ElementRegion (if any).
|
|
|
|
ER = dyn_cast<ElementRegion>(superR);
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
assert(superR && "super region cannot be NULL");
|
|
|
|
return RegionRawOffset(superR, offset);
|
|
|
|
}
|
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BlockDataRegion
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void BlockDataRegion::LazyInitializeReferencedVars() {
|
|
|
|
if (ReferencedVars)
|
|
|
|
return;
|
|
|
|
|
|
|
|
AnalysisContext *AC = LC->getAnalysisContext();
|
|
|
|
AnalysisContext::referenced_decls_iterator I, E;
|
|
|
|
llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl());
|
|
|
|
|
|
|
|
if (I == E) {
|
|
|
|
ReferencedVars = (void*) 0x1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemRegionManager &MemMgr = *getMemRegionManager();
|
|
|
|
llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
|
|
|
|
BumpVectorContext BC(A);
|
|
|
|
|
|
|
|
typedef BumpVector<const MemRegion*> VarVec;
|
|
|
|
VarVec *BV = (VarVec*) A.Allocate<VarVec>();
|
2009-12-02 06:12:34 +08:00
|
|
|
new (BV) VarVec(BC, E - I);
|
2009-11-26 10:34:36 +08:00
|
|
|
|
|
|
|
for ( ; I != E; ++I)
|
|
|
|
BV->push_back(MemMgr.getVarRegion(*I, LC), BC);
|
|
|
|
|
|
|
|
ReferencedVars = BV;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockDataRegion::referenced_vars_iterator
|
|
|
|
BlockDataRegion::referenced_vars_begin() const {
|
|
|
|
const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
|
|
|
|
|
|
|
|
BumpVector<const MemRegion*> *Vec =
|
|
|
|
static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
|
|
|
|
|
2009-12-03 16:09:21 +08:00
|
|
|
return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
|
|
|
|
NULL : Vec->begin());
|
2009-11-26 10:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockDataRegion::referenced_vars_iterator
|
|
|
|
BlockDataRegion::referenced_vars_end() const {
|
|
|
|
const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
|
|
|
|
|
|
|
|
BumpVector<const MemRegion*> *Vec =
|
|
|
|
static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
|
|
|
|
|
2009-12-03 16:09:21 +08:00
|
|
|
return BlockDataRegion::referenced_vars_iterator(Vec == (void*) 0x1 ?
|
|
|
|
NULL : Vec->end());
|
2009-11-26 10:34:36 +08:00
|
|
|
}
|