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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
|
2012-12-01 23:09:41 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2010-01-12 01:06:35 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2012-01-28 20:06:22 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2010-08-02 12:56:14 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2017-09-07 05:45:03 +08:00
|
|
|
#include "clang/Analysis/AnalysisDeclContext.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/Support/BumpVector.h"
|
2012-01-05 07:54:01 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-02-27 08:05:04 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
#include<functional>
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "MemRegion"
|
2008-10-04 13:50:14 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2008-10-04 13:50:14 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemRegion Construction.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-04-13 17:56:07 +08:00
|
|
|
template <typename RegionTy, typename SuperTy, typename Arg1Ty>
|
|
|
|
RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1,
|
|
|
|
const SuperTy *superRegion) {
|
2009-12-04 08:05:57 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2017-04-13 17:56:07 +08:00
|
|
|
RegionTy::ProfileRegion(ID, arg1, superRegion);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2009-12-04 08:05:57 +08:00
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
if (!R) {
|
2016-02-03 23:20:51 +08:00
|
|
|
R = A.Allocate<RegionTy>();
|
2017-04-13 17:56:07 +08:00
|
|
|
new (R) RegionTy(arg1, superRegion);
|
2009-12-04 08:05:57 +08:00
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:56:07 +08:00
|
|
|
template <typename RegionTy, typename SuperTy, typename Arg1Ty, typename Arg2Ty>
|
|
|
|
RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
|
|
|
|
const SuperTy *superRegion) {
|
2009-12-04 08:05:57 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2017-04-13 17:56:07 +08:00
|
|
|
RegionTy::ProfileRegion(ID, arg1, arg2, superRegion);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2009-12-04 08:05:57 +08:00
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
if (!R) {
|
2016-02-03 23:20:51 +08:00
|
|
|
R = A.Allocate<RegionTy>();
|
2017-04-13 17:56:07 +08:00
|
|
|
new (R) RegionTy(arg1, arg2, superRegion);
|
2009-12-04 08:05:57 +08:00
|
|
|
Regions.InsertNode(R, InsertPos);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-04 08:05:57 +08:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:56:07 +08:00
|
|
|
template <typename RegionTy, typename SuperTy,
|
|
|
|
typename Arg1Ty, typename Arg2Ty, typename Arg3Ty>
|
|
|
|
RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
|
|
|
|
const Arg3Ty arg3,
|
|
|
|
const SuperTy *superRegion) {
|
2009-12-08 06:05:27 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2017-04-13 17:56:07 +08:00
|
|
|
RegionTy::ProfileRegion(ID, arg1, arg2, arg3, superRegion);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2009-12-08 06:05:27 +08:00
|
|
|
RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
if (!R) {
|
2016-02-03 23:20:51 +08:00
|
|
|
R = A.Allocate<RegionTy>();
|
2017-04-13 17:56:07 +08:00
|
|
|
new (R) RegionTy(arg1, arg2, arg3, superRegion);
|
2009-12-08 06:05:27 +08:00
|
|
|
Regions.InsertNode(R, InsertPos);
|
2009-12-04 08:05:57 +08:00
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
return R;
|
|
|
|
}
|
2009-12-04 08:05:57 +08:00
|
|
|
|
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
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
MemRegion::~MemRegion() {}
|
2008-10-04 13:50:14 +08:00
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
MemRegionManager::~MemRegionManager() {
|
|
|
|
// All regions and their data are BumpPtrAllocated. No need to call
|
|
|
|
// their destructors.
|
|
|
|
}
|
2009-11-26 10:34:36 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Basic methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-08 21:17:14 +08:00
|
|
|
bool SubRegion::isSubRegionOf(const MemRegion* R) const {
|
2018-01-18 04:27:26 +08:00
|
|
|
const MemRegion* r = this;
|
|
|
|
do {
|
2009-01-08 21:17:14 +08:00
|
|
|
if (r == R)
|
|
|
|
return true;
|
|
|
|
if (const SubRegion* sr = dyn_cast<SubRegion>(r))
|
|
|
|
r = sr->getSuperRegion();
|
|
|
|
else
|
|
|
|
break;
|
2018-01-18 04:27:26 +08:00
|
|
|
} while (r != nullptr);
|
2009-01-08 21:17:14 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-12-15 06:15:06 +08:00
|
|
|
const StackFrameContext *VarRegion::getStackFrame() const {
|
|
|
|
const StackSpaceRegion *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
|
2014-05-27 10:45:47 +08:00
|
|
|
return SSR ? SSR->getStackFrame() : nullptr;
|
2009-12-15 06:15:06 +08:00
|
|
|
}
|
|
|
|
|
2010-07-04 08:00:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region extents.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-09 02:23:27 +08:00
|
|
|
DefinedOrUnknownSVal TypedValueRegion::getExtent(SValBuilder &svalBuilder) const {
|
2011-08-13 07:37:29 +08:00
|
|
|
ASTContext &Ctx = svalBuilder.getContext();
|
2010-12-10 19:01:00 +08:00
|
|
|
QualType T = getDesugaredValueType(Ctx);
|
2010-07-04 08:00:41 +08:00
|
|
|
|
2010-07-05 08:50:15 +08:00
|
|
|
if (isa<VariableArrayType>(T))
|
2010-12-02 15:49:45 +08:00
|
|
|
return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
|
2013-05-30 04:50:34 +08:00
|
|
|
if (T->isIncompleteType())
|
2010-07-04 08:00:41 +08:00
|
|
|
return UnknownVal();
|
|
|
|
|
2010-12-23 10:42:36 +08:00
|
|
|
CharUnits size = Ctx.getTypeSizeInChars(T);
|
|
|
|
QualType sizeTy = svalBuilder.getArrayIndexType();
|
|
|
|
return svalBuilder.makeIntVal(size.getQuantity(), sizeTy);
|
2010-07-04 08:00:41 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal FieldRegion::getExtent(SValBuilder &svalBuilder) const {
|
2013-03-02 07:03:17 +08:00
|
|
|
// Force callers to deal with bitfields explicitly.
|
|
|
|
if (getDecl()->isBitField())
|
|
|
|
return UnknownVal();
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal Extent = DeclRegion::getExtent(svalBuilder);
|
2010-07-04 08:00:41 +08:00
|
|
|
|
|
|
|
// A zero-length array at the end of a struct often stands for dynamically-
|
|
|
|
// allocated extra memory.
|
|
|
|
if (Extent.isZeroConstant()) {
|
2010-12-10 19:01:00 +08:00
|
|
|
QualType T = getDesugaredValueType(svalBuilder.getContext());
|
2010-07-04 08:00:41 +08:00
|
|
|
|
|
|
|
if (isa<ConstantArrayType>(T))
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Extent;
|
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal AllocaRegion::getExtent(SValBuilder &svalBuilder) const {
|
|
|
|
return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
|
2010-07-04 08:00:41 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal SymbolicRegion::getExtent(SValBuilder &svalBuilder) const {
|
|
|
|
return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
|
2010-07-04 08:00:41 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal StringRegion::getExtent(SValBuilder &svalBuilder) const {
|
2010-12-23 10:42:36 +08:00
|
|
|
return svalBuilder.makeIntVal(getStringLiteral()->getByteLength()+1,
|
|
|
|
svalBuilder.getArrayIndexType());
|
2010-07-04 08:00:41 +08:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:56:07 +08:00
|
|
|
ObjCIvarRegion::ObjCIvarRegion(const ObjCIvarDecl *ivd, const SubRegion *sReg)
|
|
|
|
: DeclRegion(ivd, sReg, ObjCIvarRegionKind) {}
|
2012-01-28 20:06:22 +08:00
|
|
|
|
|
|
|
const ObjCIvarDecl *ObjCIvarRegion::getDecl() const {
|
|
|
|
return cast<ObjCIvarDecl>(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType ObjCIvarRegion::getValueType() const {
|
|
|
|
return getDecl()->getType();
|
|
|
|
}
|
|
|
|
|
2010-11-26 16:21:53 +08:00
|
|
|
QualType CXXBaseObjectRegion::getValueType() const {
|
2013-02-21 11:12:32 +08:00
|
|
|
return QualType(getDecl()->getTypeForDecl(), 0);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2009-12-15 06:15:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FoldingSet profiling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void MemSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(getKind()));
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(getKind()));
|
2009-12-08 06:05:27 +08:00
|
|
|
ID.AddPointer(getStackFrame());
|
|
|
|
}
|
|
|
|
|
2010-07-02 04:16:50 +08:00
|
|
|
void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(getKind()));
|
2010-07-02 04:16:50 +08:00
|
|
|
ID.AddPointer(getCodeRegion());
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(StringRegionKind));
|
2008-10-25 22:13:41 +08:00
|
|
|
ID.AddPointer(Str);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2012-02-28 08:56:05 +08:00
|
|
|
void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const ObjCStringLiteral* Str,
|
|
|
|
const MemRegion* superRegion) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(ObjCStringRegionKind));
|
2012-02-28 08:56:05 +08:00
|
|
|
ID.AddPointer(Str);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2008-11-02 08:34:33 +08:00
|
|
|
void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2011-08-13 07:37:29 +08:00
|
|
|
const Expr *Ex, unsigned cnt,
|
2012-11-27 03:11:46 +08:00
|
|
|
const MemRegion *superRegion) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(AllocaRegionKind));
|
2008-11-02 08:34:33 +08:00
|
|
|
ID.AddPointer(Ex);
|
|
|
|
ID.AddInteger(cnt);
|
2012-11-27 03:11:46 +08:00
|
|
|
ID.AddPointer(superRegion);
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2011-08-13 07:37:29 +08:00
|
|
|
const CompoundLiteralExpr *CL,
|
2008-10-28 04:57:58 +08:00
|
|
|
const MemRegion* superRegion) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(CompoundLiteralRegionKind));
|
2008-10-28 04:57:58 +08:00
|
|
|
ID.AddPointer(CL);
|
|
|
|
ID.AddPointer(superRegion);
|
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
|
|
|
const PointerType *PT,
|
|
|
|
const MemRegion *sRegion) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(CXXThisRegionKind));
|
2010-01-05 10:18:06 +08:00
|
|
|
ID.AddPointer(PT);
|
|
|
|
ID.AddPointer(sRegion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
|
|
|
CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2012-01-28 20:06:22 +08:00
|
|
|
void ObjCIvarRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
|
|
|
const ObjCIvarDecl *ivd,
|
|
|
|
const MemRegion* superRegion) {
|
|
|
|
DeclRegion::ProfileRegion(ID, ivd, superRegion, ObjCIvarRegionKind);
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl *D,
|
2008-10-04 13:50:14 +08:00
|
|
|
const MemRegion* superRegion, Kind k) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<unsigned>(k));
|
2008-10-04 13:50:14 +08:00
|
|
|
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 {
|
2009-12-08 06:05:27 +08:00
|
|
|
VarRegion::ProfileRegion(ID, getDecl(), superRegion);
|
2009-08-22 06:28:32 +08:00
|
|
|
}
|
|
|
|
|
2009-06-23 07:13:13 +08:00
|
|
|
void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
|
|
|
|
const MemRegion *sreg) {
|
2016-02-03 23:20:51 +08:00
|
|
|
ID.AddInteger(static_cast<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
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void FunctionCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2012-09-18 03:13:56 +08:00
|
|
|
const NamedDecl *FD,
|
2009-11-25 09:32:22 +08:00
|
|
|
const MemRegion*) {
|
2016-01-13 21:49:29 +08:00
|
|
|
ID.AddInteger(MemRegion::FunctionCodeRegionKind);
|
2009-08-28 12:49:15 +08:00
|
|
|
ID.AddPointer(FD);
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void FunctionCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
FunctionCodeRegion::ProfileRegion(ID, FD, superRegion);
|
2009-11-25 09:32:22 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void BlockCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2009-12-08 06:05:27 +08:00
|
|
|
const BlockDecl *BD, CanQualType,
|
2011-10-24 09:32:45 +08:00
|
|
|
const AnalysisDeclContext *AC,
|
2009-12-08 06:05:27 +08:00
|
|
|
const MemRegion*) {
|
2016-01-13 21:49:29 +08:00
|
|
|
ID.AddInteger(MemRegion::BlockCodeRegionKind);
|
2009-11-25 09:32:22 +08:00
|
|
|
ID.AddPointer(BD);
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void BlockCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
BlockCodeRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2009-11-26 07:53:07 +08:00
|
|
|
void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
|
2016-01-13 21:49:29 +08:00
|
|
|
const BlockCodeRegion *BC,
|
2009-11-26 07:53:07 +08:00
|
|
|
const LocationContext *LC,
|
2013-11-20 08:11:42 +08:00
|
|
|
unsigned BlkCount,
|
2009-12-08 06:05:27 +08:00
|
|
|
const MemRegion *sReg) {
|
2009-11-26 07:53:07 +08:00
|
|
|
ID.AddInteger(MemRegion::BlockDataRegionKind);
|
|
|
|
ID.AddPointer(BC);
|
|
|
|
ID.AddPointer(LC);
|
2013-11-20 08:11:42 +08:00
|
|
|
ID.AddInteger(BlkCount);
|
2009-12-08 06:05:27 +08:00
|
|
|
ID.AddPointer(sReg);
|
2009-11-26 07:53:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
|
2013-11-20 08:11:42 +08:00
|
|
|
BlockDataRegion::ProfileRegion(ID, BC, LC, BlockCount, getSuperRegion());
|
2009-11-26 07:53:07 +08:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:52:48 +08:00
|
|
|
void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
|
|
|
Expr const *Ex,
|
|
|
|
const MemRegion *sReg) {
|
2010-01-09 17:16:47 +08:00
|
|
|
ID.AddPointer(Ex);
|
2009-12-16 19:27:52 +08:00
|
|
|
ID.AddPointer(sReg);
|
|
|
|
}
|
|
|
|
|
2010-11-26 16:52:48 +08:00
|
|
|
void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2010-01-09 17:16:47 +08:00
|
|
|
ProfileRegion(ID, Ex, getSuperRegion());
|
2009-12-16 19:27:52 +08:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:21:53 +08:00
|
|
|
void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
|
2013-02-21 11:12:32 +08:00
|
|
|
const CXXRecordDecl *RD,
|
|
|
|
bool IsVirtual,
|
|
|
|
const MemRegion *SReg) {
|
|
|
|
ID.AddPointer(RD);
|
|
|
|
ID.AddBoolean(IsVirtual);
|
|
|
|
ID.AddPointer(SReg);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
|
2013-02-21 11:12:32 +08:00
|
|
|
ProfileRegion(ID, getDecl(), isVirtual(), superRegion);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region anchors.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void GlobalsSpaceRegion::anchor() { }
|
2017-04-13 17:48:05 +08:00
|
|
|
void NonStaticGlobalSpaceRegion::anchor() { }
|
|
|
|
void StackSpaceRegion::anchor() { }
|
2011-12-20 10:48:34 +08:00
|
|
|
void TypedRegion::anchor() { }
|
|
|
|
void TypedValueRegion::anchor() { }
|
|
|
|
void CodeTextRegion::anchor() { }
|
|
|
|
void SubRegion::anchor() { }
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region pretty-printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void MemRegion::dump() const {
|
2009-07-14 07:31:04 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void MemRegion::dumpToStream(raw_ostream &os) const {
|
2008-10-04 13:50:14 +08:00
|
|
|
os << "<Unknown Region>";
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void AllocaRegion::dumpToStream(raw_ostream &os) const {
|
2016-02-03 23:20:51 +08:00
|
|
|
os << "alloca{" << static_cast<const void*>(Ex) << ',' << Cnt << '}';
|
2008-11-02 08:34:33 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void FunctionCodeRegion::dumpToStream(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
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void BlockCodeRegion::dumpToStream(raw_ostream &os) const {
|
2016-02-03 23:20:51 +08:00
|
|
|
os << "block_code{" << static_cast<const void*>(this) << '}';
|
2009-11-25 09:32:22 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void BlockDataRegion::dumpToStream(raw_ostream &os) const {
|
2013-11-20 08:11:42 +08:00
|
|
|
os << "block_data{" << BC;
|
|
|
|
os << "; ";
|
|
|
|
for (BlockDataRegion::referenced_vars_iterator
|
|
|
|
I = referenced_vars_begin(),
|
|
|
|
E = referenced_vars_end(); I != E; ++I)
|
|
|
|
os << "(" << I.getCapturedRegion() << "," <<
|
|
|
|
I.getOriginalRegion() << ") ";
|
|
|
|
os << '}';
|
2009-11-26 07:53:07 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const {
|
2009-04-22 02:09:22 +08:00
|
|
|
// FIXME: More elaborate pretty-printing.
|
2016-02-03 23:20:51 +08:00
|
|
|
os << "{ " << static_cast<const void*>(CL) << " }";
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const {
|
2011-08-26 08:41:31 +08:00
|
|
|
os << "temp_object{" << getValueType().getAsString() << ','
|
2016-02-03 23:20:51 +08:00
|
|
|
<< static_cast<const void*>(Ex) << '}';
|
2010-11-25 10:07:24 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const {
|
2013-02-21 11:12:32 +08:00
|
|
|
os << "base{" << superRegion << ',' << getDecl()->getName() << '}';
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void CXXThisRegion::dumpToStream(raw_ostream &os) const {
|
2010-01-05 10:18:06 +08:00
|
|
|
os << "this";
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void ElementRegion::dumpToStream(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
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void FieldRegion::dumpToStream(raw_ostream &os) const {
|
2011-10-15 02:45:37 +08:00
|
|
|
os << superRegion << "->" << *getDecl();
|
2008-10-18 05:05:44 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void ObjCIvarRegion::dumpToStream(raw_ostream &os) const {
|
2011-10-15 02:45:37 +08:00
|
|
|
os << "ivar{" << superRegion << ',' << *getDecl() << '}';
|
2009-07-20 04:36:24 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void StringRegion::dumpToStream(raw_ostream &os) const {
|
2014-06-10 06:53:25 +08:00
|
|
|
assert(Str != nullptr && "Expecting non-null StringLiteral");
|
2014-05-27 10:45:47 +08:00
|
|
|
Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
|
2008-10-24 14:30:07 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 08:56:05 +08:00
|
|
|
void ObjCStringRegion::dumpToStream(raw_ostream &os) const {
|
2014-06-10 06:53:25 +08:00
|
|
|
assert(Str != nullptr && "Expecting non-null ObjCStringLiteral");
|
2014-05-27 10:45:47 +08:00
|
|
|
Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
|
2012-02-28 08:56:05 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void SymbolicRegion::dumpToStream(raw_ostream &os) const {
|
2017-12-06 01:14:39 +08:00
|
|
|
if (isa<HeapSpaceRegion>(getSuperRegion()))
|
|
|
|
os << "Heap";
|
2009-07-14 07:38:57 +08:00
|
|
|
os << "SymRegion{" << sym << '}';
|
2008-10-28 04:57:58 +08:00
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void VarRegion::dumpToStream(raw_ostream &os) const {
|
2011-10-15 02:45:37 +08:00
|
|
|
os << *cast<VarDecl>(D);
|
2008-11-10 21:05:26 +08:00
|
|
|
}
|
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void RegionRawOffset::dump() const {
|
2009-08-01 14:17:29 +08:00
|
|
|
dumpToStream(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void RegionRawOffset::dumpToStream(raw_ostream &os) const {
|
2011-01-24 09:55:39 +08:00
|
|
|
os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}';
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
void CodeSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "CodeSpaceRegion";
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void StaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const {
|
2010-07-07 07:37:21 +08:00
|
|
|
os << "StaticGlobalsMemSpace{" << CR << '}';
|
|
|
|
}
|
|
|
|
|
2012-01-05 07:54:01 +08:00
|
|
|
void GlobalInternalSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "GlobalInternalSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSystemSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "GlobalSystemSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalImmutableSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "GlobalImmutableSpaceRegion";
|
|
|
|
}
|
|
|
|
|
2012-06-07 04:47:00 +08:00
|
|
|
void HeapSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "HeapSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnknownSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "UnknownSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackArgumentsSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "StackArgumentsSpaceRegion";
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackLocalsSpaceRegion::dumpToStream(raw_ostream &os) const {
|
|
|
|
os << "StackLocalsSpaceRegion";
|
|
|
|
}
|
|
|
|
|
2012-08-09 02:23:36 +08:00
|
|
|
bool MemRegion::canPrintPretty() const {
|
2013-04-20 09:15:36 +08:00
|
|
|
return canPrintPrettyAsExpr();
|
2012-08-09 02:23:36 +08:00
|
|
|
}
|
|
|
|
|
2013-04-16 06:37:59 +08:00
|
|
|
bool MemRegion::canPrintPrettyAsExpr() const {
|
2013-04-20 09:15:36 +08:00
|
|
|
return false;
|
2013-04-16 06:37:59 +08:00
|
|
|
}
|
|
|
|
|
2012-08-09 02:23:36 +08:00
|
|
|
void MemRegion::printPretty(raw_ostream &os) const {
|
2013-04-13 02:40:21 +08:00
|
|
|
assert(canPrintPretty() && "This region cannot be printed pretty.");
|
|
|
|
os << "'";
|
2013-04-16 06:37:59 +08:00
|
|
|
printPrettyAsExpr(os);
|
2013-04-13 02:40:21 +08:00
|
|
|
os << "'";
|
|
|
|
}
|
|
|
|
|
2013-04-16 06:37:59 +08:00
|
|
|
void MemRegion::printPrettyAsExpr(raw_ostream &os) const {
|
|
|
|
llvm_unreachable("This region cannot be printed pretty.");
|
2012-03-22 03:45:08 +08:00
|
|
|
}
|
|
|
|
|
2013-04-20 09:15:36 +08:00
|
|
|
bool VarRegion::canPrintPrettyAsExpr() const {
|
2012-08-09 02:23:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-16 06:37:59 +08:00
|
|
|
void VarRegion::printPrettyAsExpr(raw_ostream &os) const {
|
2012-03-22 03:45:08 +08:00
|
|
|
os << getDecl()->getName();
|
|
|
|
}
|
|
|
|
|
2013-04-20 09:15:36 +08:00
|
|
|
bool ObjCIvarRegion::canPrintPrettyAsExpr() const {
|
2013-02-24 15:21:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-16 06:37:59 +08:00
|
|
|
void ObjCIvarRegion::printPrettyAsExpr(raw_ostream &os) const {
|
2013-02-24 15:21:01 +08:00
|
|
|
os << getDecl()->getName();
|
|
|
|
}
|
|
|
|
|
2012-08-09 02:23:36 +08:00
|
|
|
bool FieldRegion::canPrintPretty() const {
|
2013-04-13 02:40:21 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-16 06:37:59 +08:00
|
|
|
bool FieldRegion::canPrintPrettyAsExpr() const {
|
|
|
|
return superRegion->canPrintPrettyAsExpr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FieldRegion::printPrettyAsExpr(raw_ostream &os) const {
|
|
|
|
assert(canPrintPrettyAsExpr());
|
|
|
|
superRegion->printPrettyAsExpr(os);
|
|
|
|
os << "." << getDecl()->getName();
|
2012-08-09 02:23:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FieldRegion::printPretty(raw_ostream &os) const {
|
2013-04-16 06:37:59 +08:00
|
|
|
if (canPrintPrettyAsExpr()) {
|
2013-04-13 02:40:21 +08:00
|
|
|
os << "\'";
|
2013-04-16 06:37:59 +08:00
|
|
|
printPrettyAsExpr(os);
|
2013-04-13 02:40:21 +08:00
|
|
|
os << "'";
|
|
|
|
} else {
|
2013-04-16 06:37:59 +08:00
|
|
|
os << "field " << "\'" << getDecl()->getName() << "'";
|
2013-04-13 02:40:21 +08:00
|
|
|
}
|
2012-03-22 03:45:08 +08:00
|
|
|
}
|
|
|
|
|
2013-04-16 06:38:04 +08:00
|
|
|
bool CXXBaseObjectRegion::canPrintPrettyAsExpr() const {
|
2013-04-16 06:37:59 +08:00
|
|
|
return superRegion->canPrintPrettyAsExpr();
|
|
|
|
}
|
|
|
|
|
2013-04-16 06:38:04 +08:00
|
|
|
void CXXBaseObjectRegion::printPrettyAsExpr(raw_ostream &os) const {
|
|
|
|
superRegion->printPrettyAsExpr(os);
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:22:41 +08:00
|
|
|
std::string MemRegion::getDescriptiveName(bool UseQuotes) const {
|
|
|
|
std::string VariableName;
|
|
|
|
std::string ArrayIndices;
|
|
|
|
const MemRegion *R = this;
|
|
|
|
SmallString<50> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
|
|
|
|
// Obtain array indices to add them to the variable name.
|
|
|
|
const ElementRegion *ER = nullptr;
|
|
|
|
while ((ER = R->getAs<ElementRegion>())) {
|
|
|
|
// Index is a ConcreteInt.
|
|
|
|
if (auto CI = ER->getIndex().getAs<nonloc::ConcreteInt>()) {
|
|
|
|
llvm::SmallString<2> Idx;
|
|
|
|
CI->getValue().toString(Idx);
|
|
|
|
ArrayIndices = (llvm::Twine("[") + Idx.str() + "]" + ArrayIndices).str();
|
|
|
|
}
|
|
|
|
// If not a ConcreteInt, try to obtain the variable
|
|
|
|
// name by calling 'getDescriptiveName' recursively.
|
|
|
|
else {
|
|
|
|
std::string Idx = ER->getDescriptiveName(false);
|
|
|
|
if (!Idx.empty()) {
|
|
|
|
ArrayIndices = (llvm::Twine("[") + Idx + "]" + ArrayIndices).str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
R = ER->getSuperRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get variable name.
|
|
|
|
if (R && R->canPrintPrettyAsExpr()) {
|
|
|
|
R->printPrettyAsExpr(os);
|
|
|
|
if (UseQuotes) {
|
|
|
|
return (llvm::Twine("'") + os.str() + ArrayIndices + "'").str();
|
|
|
|
} else {
|
|
|
|
return (llvm::Twine(os.str()) + ArrayIndices).str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return VariableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceRange MemRegion::sourceRange() const {
|
|
|
|
const VarRegion *const VR = dyn_cast<VarRegion>(this->getBaseRegion());
|
|
|
|
const FieldRegion *const FR = dyn_cast<FieldRegion>(this);
|
|
|
|
|
|
|
|
// Check for more specific regions first.
|
|
|
|
// FieldRegion
|
|
|
|
if (FR) {
|
|
|
|
return FR->getDecl()->getSourceRange();
|
|
|
|
}
|
|
|
|
// VarRegion
|
|
|
|
else if (VR) {
|
|
|
|
return VR->getDecl()->getSourceRange();
|
|
|
|
}
|
|
|
|
// Return invalid source range (can be checked by client).
|
|
|
|
else {
|
|
|
|
return SourceRange{};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemRegionManager methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-08-01 14:17:29 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
template <typename REG>
|
|
|
|
const REG *MemRegionManager::LazyAllocate(REG*& region) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!region) {
|
2016-02-03 23:20:51 +08:00
|
|
|
region = A.Allocate<REG>();
|
2009-12-08 06:05:27 +08:00
|
|
|
new (region) REG(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-08 06:05:27 +08:00
|
|
|
template <typename REG, typename ARG>
|
|
|
|
const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
|
|
|
|
if (!region) {
|
2016-02-03 23:20:51 +08:00
|
|
|
region = A.Allocate<REG>();
|
2009-12-08 06:05:27 +08:00
|
|
|
new (region) REG(this, a);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StackLocalsSpaceRegion*
|
2009-12-11 14:43:27 +08:00
|
|
|
MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
|
|
|
|
assert(STC);
|
2010-02-17 16:46:50 +08:00
|
|
|
StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
|
|
|
|
|
|
|
|
if (R)
|
|
|
|
return R;
|
|
|
|
|
|
|
|
R = A.Allocate<StackLocalsSpaceRegion>();
|
|
|
|
new (R) StackLocalsSpaceRegion(this, STC);
|
|
|
|
return R;
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
const StackArgumentsSpaceRegion *
|
|
|
|
MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
|
2009-12-11 14:43:27 +08:00
|
|
|
assert(STC);
|
2010-02-17 16:46:50 +08:00
|
|
|
StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
|
|
|
|
|
|
|
|
if (R)
|
|
|
|
return R;
|
|
|
|
|
|
|
|
R = A.Allocate<StackArgumentsSpaceRegion>();
|
|
|
|
new (R) StackArgumentsSpaceRegion(this, STC);
|
|
|
|
return R;
|
2009-07-03 02:14:59 +08:00
|
|
|
}
|
|
|
|
|
2010-07-02 04:16:50 +08:00
|
|
|
const GlobalsSpaceRegion
|
2012-01-05 07:54:01 +08:00
|
|
|
*MemRegionManager::getGlobalsRegion(MemRegion::Kind K,
|
|
|
|
const CodeTextRegion *CR) {
|
|
|
|
if (!CR) {
|
|
|
|
if (K == MemRegion::GlobalSystemSpaceRegionKind)
|
|
|
|
return LazyAllocate(SystemGlobals);
|
|
|
|
if (K == MemRegion::GlobalImmutableSpaceRegionKind)
|
|
|
|
return LazyAllocate(ImmutableGlobals);
|
|
|
|
assert(K == MemRegion::GlobalInternalSpaceRegionKind);
|
|
|
|
return LazyAllocate(InternalGlobals);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2012-01-05 07:54:01 +08:00
|
|
|
assert(K == MemRegion::StaticGlobalSpaceRegionKind);
|
2010-07-02 04:16:50 +08:00
|
|
|
StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
|
|
|
|
if (R)
|
|
|
|
return R;
|
|
|
|
|
|
|
|
R = A.Allocate<StaticGlobalSpaceRegion>();
|
|
|
|
new (R) StaticGlobalSpaceRegion(this, CR);
|
|
|
|
return R;
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
|
2008-10-04 13:50:14 +08:00
|
|
|
return LazyAllocate(heap);
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
const UnknownSpaceRegion *MemRegionManager::getUnknownRegion() {
|
2008-10-08 10:50:44 +08:00
|
|
|
return LazyAllocate(unknown);
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
const CodeSpaceRegion *MemRegionManager::getCodeRegion() {
|
2009-04-10 16:45:10 +08:00
|
|
|
return LazyAllocate(code);
|
|
|
|
}
|
|
|
|
|
2009-06-23 07:13:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Constructing regions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-07-02 04:16:50 +08:00
|
|
|
const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){
|
2017-04-13 17:56:07 +08:00
|
|
|
return getSubRegion<StringRegion>(
|
|
|
|
Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion()));
|
2008-10-25 22:13:41 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 08:56:05 +08:00
|
|
|
const ObjCStringRegion *
|
|
|
|
MemRegionManager::getObjCStringRegion(const ObjCStringLiteral* Str){
|
2017-04-13 17:56:07 +08:00
|
|
|
return getSubRegion<ObjCStringRegion>(
|
|
|
|
Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion()));
|
2012-02-28 08:56:05 +08:00
|
|
|
}
|
|
|
|
|
2012-06-02 04:04:04 +08:00
|
|
|
/// Look through a chain of LocationContexts to either find the
|
|
|
|
/// StackFrameContext that matches a DeclContext, or find a VarRegion
|
|
|
|
/// for a variable captured by a block.
|
|
|
|
static llvm::PointerUnion<const StackFrameContext *, const VarRegion *>
|
|
|
|
getStackOrCaptureRegionForDeclContext(const LocationContext *LC,
|
|
|
|
const DeclContext *DC,
|
|
|
|
const VarDecl *VD) {
|
|
|
|
while (LC) {
|
|
|
|
if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
|
|
|
|
if (cast<DeclContext>(SFC->getDecl()) == DC)
|
|
|
|
return SFC;
|
|
|
|
}
|
|
|
|
if (const BlockInvocationContext *BC =
|
|
|
|
dyn_cast<BlockInvocationContext>(LC)) {
|
|
|
|
const BlockDataRegion *BR =
|
|
|
|
static_cast<const BlockDataRegion*>(BC->getContextData());
|
|
|
|
// FIXME: This can be made more efficient.
|
|
|
|
for (BlockDataRegion::referenced_vars_iterator
|
|
|
|
I = BR->referenced_vars_begin(),
|
|
|
|
E = BR->referenced_vars_end(); I != E; ++I) {
|
|
|
|
if (const VarRegion *VR = dyn_cast<VarRegion>(I.getOriginalRegion()))
|
|
|
|
if (VR->getDecl() == VD)
|
|
|
|
return cast<VarRegion>(I.getCapturedRegion());
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-06-02 04:04:04 +08:00
|
|
|
LC = LC->getParent();
|
|
|
|
}
|
2014-05-27 10:45:47 +08:00
|
|
|
return (const StackFrameContext *)nullptr;
|
2012-06-02 04:04:04 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
|
|
|
|
const LocationContext *LC) {
|
2014-05-27 10:45:47 +08:00
|
|
|
const MemRegion *sReg = nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-05 07:54:01 +08:00
|
|
|
if (D->hasGlobalStorage() && !D->isStaticLocal()) {
|
|
|
|
|
|
|
|
// First handle the globals defined in system headers.
|
|
|
|
if (C.getSourceManager().isInSystemHeader(D->getLocation())) {
|
|
|
|
// Whitelist the system globals which often DO GET modified, assume the
|
|
|
|
// rest are immutable.
|
|
|
|
if (D->getName().find("errno") != StringRef::npos)
|
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind);
|
|
|
|
else
|
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
|
|
|
|
|
|
|
|
// Treat other globals as GlobalInternal unless they are constants.
|
|
|
|
} else {
|
|
|
|
QualType GQT = D->getType();
|
|
|
|
const Type *GT = GQT.getTypePtrOrNull();
|
|
|
|
// TODO: We could walk the complex types here and see if everything is
|
|
|
|
// constified.
|
|
|
|
if (GT && GQT.isConstQualified() && GT->isArithmeticType())
|
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
|
|
|
|
else
|
|
|
|
sReg = getGlobalsRegion();
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2017-01-24 10:10:59 +08:00
|
|
|
// Finally handle static locals.
|
2012-01-05 07:54:01 +08:00
|
|
|
} else {
|
2009-12-08 06:05:27 +08:00
|
|
|
// FIXME: Once we implement scope handling, we will need to properly lookup
|
|
|
|
// 'D' to the proper LocationContext.
|
2009-12-11 14:43:27 +08:00
|
|
|
const DeclContext *DC = D->getDeclContext();
|
2012-06-02 04:04:04 +08:00
|
|
|
llvm::PointerUnion<const StackFrameContext *, const VarRegion *> V =
|
|
|
|
getStackOrCaptureRegionForDeclContext(LC, DC, D);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-06-02 04:04:04 +08:00
|
|
|
if (V.is<const VarRegion*>())
|
|
|
|
return V.get<const VarRegion*>();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-06-02 04:04:04 +08:00
|
|
|
const StackFrameContext *STC = V.get<const StackFrameContext*>();
|
2009-12-11 14:43:27 +08:00
|
|
|
|
2017-01-25 18:21:45 +08:00
|
|
|
if (!STC) {
|
|
|
|
// FIXME: Assign a more sensible memory space to static locals
|
|
|
|
// we see from within blocks that we analyze as top-level declarations.
|
2017-01-24 10:10:59 +08:00
|
|
|
sReg = getUnknownRegion();
|
2017-01-25 18:21:45 +08:00
|
|
|
} else {
|
2010-07-02 04:16:50 +08:00
|
|
|
if (D->hasLocalStorage()) {
|
|
|
|
sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
|
|
|
|
? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
|
|
|
|
: static_cast<const MemRegion*>(getStackLocalsRegion(STC));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(D->isStaticLocal());
|
2012-09-18 03:13:56 +08:00
|
|
|
const Decl *STCD = STC->getDecl();
|
|
|
|
if (isa<FunctionDecl>(STCD) || isa<ObjCMethodDecl>(STCD))
|
2012-01-05 07:54:01 +08:00
|
|
|
sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
|
2016-01-13 21:49:29 +08:00
|
|
|
getFunctionCodeRegion(cast<NamedDecl>(STCD)));
|
2012-09-18 03:13:56 +08:00
|
|
|
else if (const BlockDecl *BD = dyn_cast<BlockDecl>(STCD)) {
|
2017-01-24 10:10:59 +08:00
|
|
|
// FIXME: The fallback type here is totally bogus -- though it should
|
|
|
|
// never be queried, it will prevent uniquing with the real
|
|
|
|
// BlockCodeRegion. Ideally we'd fix the AST so that we always had a
|
|
|
|
// signature.
|
|
|
|
QualType T;
|
|
|
|
if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten())
|
|
|
|
T = TSI->getType();
|
|
|
|
if (T.isNull())
|
|
|
|
T = getContext().VoidTy;
|
|
|
|
if (!T->getAs<FunctionType>())
|
|
|
|
T = getContext().getFunctionNoProtoType(T);
|
|
|
|
T = getContext().getBlockPointerType(T);
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
const BlockCodeRegion *BTR =
|
2017-01-24 10:10:59 +08:00
|
|
|
getBlockCodeRegion(BD, C.getCanonicalType(T),
|
|
|
|
STC->getAnalysisDeclContext());
|
2012-01-05 07:54:01 +08:00
|
|
|
sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
|
|
|
|
BTR);
|
2010-07-02 04:16:50 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sReg = getGlobalsRegion();
|
|
|
|
}
|
|
|
|
}
|
2009-12-11 14:43:27 +08:00
|
|
|
}
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
return getSubRegion<VarRegion>(D, sReg);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
|
|
|
|
const MemRegion *superR) {
|
|
|
|
return getSubRegion<VarRegion>(D, superR);
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const BlockDataRegion *
|
2016-01-13 21:49:29 +08:00
|
|
|
MemRegionManager::getBlockDataRegion(const BlockCodeRegion *BC,
|
2013-11-20 08:11:42 +08:00
|
|
|
const LocationContext *LC,
|
|
|
|
unsigned blockCount) {
|
2017-04-13 17:56:07 +08:00
|
|
|
const MemSpaceRegion *sReg = nullptr;
|
2012-02-19 06:41:01 +08:00
|
|
|
const BlockDecl *BD = BC->getDecl();
|
|
|
|
if (!BD->hasCaptures()) {
|
|
|
|
// This handles 'static' blocks.
|
|
|
|
sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
|
|
|
else {
|
2012-02-19 06:41:01 +08:00
|
|
|
if (LC) {
|
|
|
|
// FIXME: Once we implement scope handling, we want the parent region
|
|
|
|
// to be the scope.
|
|
|
|
const StackFrameContext *STC = LC->getCurrentStackFrame();
|
|
|
|
assert(STC);
|
|
|
|
sReg = getStackLocalsRegion(STC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We allow 'LC' to be NULL for cases where want BlockDataRegions
|
|
|
|
// without context-sensitivity.
|
|
|
|
sReg = getUnknownRegion();
|
|
|
|
}
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
|
|
|
|
2013-11-20 08:11:42 +08:00
|
|
|
return getSubRegion<BlockDataRegion>(BC, LC, blockCount, sReg);
|
2009-11-26 07:53:07 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 19:50:42 +08:00
|
|
|
const CXXTempObjectRegion *
|
|
|
|
MemRegionManager::getCXXStaticTempObjectRegion(const Expr *Ex) {
|
|
|
|
return getSubRegion<CXXTempObjectRegion>(
|
2014-05-27 10:45:47 +08:00
|
|
|
Ex, getGlobalsRegion(MemRegion::GlobalInternalSpaceRegionKind, nullptr));
|
2013-07-26 19:50:42 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const CompoundLiteralRegion*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
|
2009-12-08 06:05:27 +08:00
|
|
|
const LocationContext *LC) {
|
2017-04-13 17:56:07 +08:00
|
|
|
const MemSpaceRegion *sReg = nullptr;
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
if (CL->isFileScope())
|
|
|
|
sReg = getGlobalsRegion();
|
|
|
|
else {
|
|
|
|
const StackFrameContext *STC = LC->getCurrentStackFrame();
|
|
|
|
assert(STC);
|
|
|
|
sReg = getStackLocalsRegion(STC);
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
return getSubRegion<CompoundLiteralRegion>(CL, sReg);
|
2008-10-28 04:57:58 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const ElementRegion*
|
2010-09-15 11:13:30 +08:00
|
|
|
MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
|
2017-04-13 17:56:07 +08:00
|
|
|
const SubRegion* superRegion,
|
2011-08-13 07:37:29 +08:00
|
|
|
ASTContext &Ctx){
|
2010-05-27 08:29:00 +08:00
|
|
|
QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
|
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
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2008-10-21 13:27:10 +08:00
|
|
|
MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
ElementRegion* R = cast_or_null<ElementRegion>(data);
|
|
|
|
|
|
|
|
if (!R) {
|
2016-02-03 23:20:51 +08:00
|
|
|
R = 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;
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
const FunctionCodeRegion *
|
|
|
|
MemRegionManager::getFunctionCodeRegion(const NamedDecl *FD) {
|
|
|
|
return getSubRegion<FunctionCodeRegion>(FD, getCodeRegion());
|
2009-04-10 16:45:10 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
const BlockCodeRegion *
|
|
|
|
MemRegionManager::getBlockCodeRegion(const BlockDecl *BD, CanQualType locTy,
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext *AC) {
|
2016-01-13 21:49:29 +08:00
|
|
|
return getSubRegion<BlockCodeRegion>(BD, locTy, AC, getCodeRegion());
|
2009-11-25 09:32:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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-12-08 06:05:27 +08:00
|
|
|
return getSubRegion<SymbolicRegion>(sym, getUnknownRegion());
|
2008-10-18 04:28:54 +08:00
|
|
|
}
|
|
|
|
|
2012-06-07 11:57:32 +08:00
|
|
|
const SymbolicRegion *MemRegionManager::getSymbolicHeapRegion(SymbolRef Sym) {
|
|
|
|
return getSubRegion<SymbolicRegion>(Sym, getHeapRegion());
|
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
const FieldRegion*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getFieldRegion(const FieldDecl *d,
|
2017-04-13 17:56:07 +08:00
|
|
|
const SubRegion* 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*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl *d,
|
2017-04-13 17:56:07 +08:00
|
|
|
const SubRegion* superRegion) {
|
2009-07-11 00:51:45 +08:00
|
|
|
return getSubRegion<ObjCIvarRegion>(d, superRegion);
|
2008-10-25 04:30:08 +08:00
|
|
|
}
|
|
|
|
|
2010-11-26 16:52:48 +08:00
|
|
|
const CXXTempObjectRegion*
|
|
|
|
MemRegionManager::getCXXTempObjectRegion(Expr const *E,
|
|
|
|
LocationContext const *LC) {
|
2010-01-09 17:16:47 +08:00
|
|
|
const StackFrameContext *SFC = LC->getCurrentStackFrame();
|
|
|
|
assert(SFC);
|
2010-11-26 16:52:48 +08:00
|
|
|
return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC));
|
2009-12-16 19:27:52 +08:00
|
|
|
}
|
|
|
|
|
2013-02-21 11:12:32 +08:00
|
|
|
/// Checks whether \p BaseClass is a valid virtual or direct non-virtual base
|
|
|
|
/// class of the type of \p Super.
|
|
|
|
static bool isValidBaseClass(const CXXRecordDecl *BaseClass,
|
|
|
|
const TypedValueRegion *Super,
|
|
|
|
bool IsVirtual) {
|
2013-02-23 03:33:13 +08:00
|
|
|
BaseClass = BaseClass->getCanonicalDecl();
|
|
|
|
|
2013-02-21 11:12:32 +08:00
|
|
|
const CXXRecordDecl *Class = Super->getValueType()->getAsCXXRecordDecl();
|
|
|
|
if (!Class)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (IsVirtual)
|
|
|
|
return Class->isVirtuallyDerivedFrom(BaseClass);
|
|
|
|
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : Class->bases()) {
|
|
|
|
if (I.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == BaseClass)
|
2013-02-21 11:12:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-08-14 06:11:34 +08:00
|
|
|
|
2013-02-21 11:12:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXBaseObjectRegion *
|
|
|
|
MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *RD,
|
2017-04-13 17:56:07 +08:00
|
|
|
const SubRegion *Super,
|
2013-02-21 11:12:32 +08:00
|
|
|
bool IsVirtual) {
|
2013-02-21 12:40:10 +08:00
|
|
|
if (isa<TypedValueRegion>(Super)) {
|
|
|
|
assert(isValidBaseClass(RD, dyn_cast<TypedValueRegion>(Super), IsVirtual));
|
2013-07-27 11:34:50 +08:00
|
|
|
(void)&isValidBaseClass;
|
2013-02-21 11:12:32 +08:00
|
|
|
|
|
|
|
if (IsVirtual) {
|
|
|
|
// Virtual base regions should not be layered, since the layout rules
|
|
|
|
// are different.
|
|
|
|
while (const CXXBaseObjectRegion *Base =
|
|
|
|
dyn_cast<CXXBaseObjectRegion>(Super)) {
|
2017-04-13 17:56:07 +08:00
|
|
|
Super = cast<SubRegion>(Base->getSuperRegion());
|
2012-08-14 06:11:34 +08:00
|
|
|
}
|
2013-02-21 11:12:32 +08:00
|
|
|
assert(Super && !isa<MemSpaceRegion>(Super));
|
2012-08-14 06:11:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 11:12:32 +08:00
|
|
|
return getSubRegion<CXXBaseObjectRegion>(RD, IsVirtual, Super);
|
2010-11-26 16:21:53 +08:00
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
const CXXThisRegion*
|
|
|
|
MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
|
|
|
|
const LocationContext *LC) {
|
|
|
|
const PointerType *PT = thisPointerTy->getAs<PointerType>();
|
|
|
|
assert(PT);
|
2015-09-12 00:55:01 +08:00
|
|
|
// Inside the body of the operator() of a lambda a this expr might refer to an
|
|
|
|
// object in one of the parent location contexts.
|
|
|
|
const auto *D = dyn_cast<CXXMethodDecl>(LC->getDecl());
|
|
|
|
// FIXME: when operator() of lambda is analyzed as a top level function and
|
|
|
|
// 'this' refers to a this to the enclosing scope, there is no right region to
|
|
|
|
// return.
|
|
|
|
while (!LC->inTopFrame() &&
|
2015-10-27 20:36:26 +08:00
|
|
|
(!D || D->isStatic() ||
|
|
|
|
PT != D->getThisType(getContext())->getAs<PointerType>())) {
|
2015-09-12 00:55:01 +08:00
|
|
|
LC = LC->getParent();
|
|
|
|
D = dyn_cast<CXXMethodDecl>(LC->getDecl());
|
|
|
|
}
|
|
|
|
const StackFrameContext *STC = LC->getCurrentStackFrame();
|
|
|
|
assert(STC);
|
2010-01-05 10:18:06 +08:00
|
|
|
return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:26:31 +08:00
|
|
|
const AllocaRegion*
|
2011-08-13 07:37:29 +08:00
|
|
|
MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt,
|
2009-12-08 06:05:27 +08:00
|
|
|
const LocationContext *LC) {
|
|
|
|
const StackFrameContext *STC = LC->getCurrentStackFrame();
|
|
|
|
assert(STC);
|
|
|
|
return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
|
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-12-08 06:05:27 +08:00
|
|
|
return isa<StackSpaceRegion>(getMemorySpace());
|
2009-06-24 02:17:08 +08:00
|
|
|
}
|
2009-06-23 10:51:21 +08:00
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
bool MemRegion::hasStackNonParametersStorage() const {
|
|
|
|
return isa<StackLocalsSpaceRegion>(getMemorySpace());
|
2009-07-03 02:25:09 +08:00
|
|
|
}
|
|
|
|
|
2010-01-05 10:18:06 +08:00
|
|
|
bool MemRegion::hasStackParametersStorage() const {
|
2009-12-08 06:05:27 +08:00
|
|
|
return isa<StackArgumentsSpaceRegion>(getMemorySpace());
|
2009-07-03 06:02:15 +08:00
|
|
|
}
|
|
|
|
|
2009-07-03 02:25:09 +08:00
|
|
|
bool MemRegion::hasGlobalsOrParametersStorage() const {
|
2009-12-08 06:05:27 +08:00
|
|
|
const MemSpaceRegion *MS = getMemorySpace();
|
|
|
|
return isa<StackArgumentsSpaceRegion>(MS) ||
|
|
|
|
isa<GlobalsSpaceRegion>(MS);
|
2009-07-03 02:25:09 +08:00
|
|
|
}
|
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) {
|
2010-04-07 06:06:03 +08:00
|
|
|
switch (R->getKind()) {
|
|
|
|
case MemRegion::ElementRegionKind:
|
|
|
|
case MemRegion::FieldRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2011-01-13 20:46:31 +08:00
|
|
|
case MemRegion::CXXBaseObjectRegionKind:
|
2010-04-07 06:06:03 +08:00
|
|
|
R = cast<SubRegion>(R)->getSuperRegion();
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
2009-11-10 10:37:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2012-09-13 08:37:12 +08:00
|
|
|
bool MemRegion::isSubRegionOf(const MemRegion *R) const {
|
2012-09-13 06:57:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// View handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-14 06:11:34 +08:00
|
|
|
const MemRegion *MemRegion::StripCasts(bool StripBaseCasts) const {
|
2009-07-30 02:14:27 +08:00
|
|
|
const MemRegion *R = this;
|
|
|
|
while (true) {
|
2012-07-12 08:16:25 +08:00
|
|
|
switch (R->getKind()) {
|
|
|
|
case ElementRegionKind: {
|
|
|
|
const ElementRegion *ER = cast<ElementRegion>(R);
|
|
|
|
if (!ER->getIndex().isZeroConstant())
|
|
|
|
return R;
|
|
|
|
R = ER->getSuperRegion();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CXXBaseObjectRegionKind:
|
2012-08-14 06:11:34 +08:00
|
|
|
if (!StripBaseCasts)
|
|
|
|
return R;
|
2012-07-12 08:16:25 +08:00
|
|
|
R = cast<CXXBaseObjectRegion>(R)->getSuperRegion();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return R;
|
2009-07-30 02:14:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-01 14:17:29 +08:00
|
|
|
|
2013-04-20 09:15:42 +08:00
|
|
|
const SymbolicRegion *MemRegion::getSymbolicBase() const {
|
|
|
|
const SubRegion *SubR = dyn_cast<SubRegion>(this);
|
|
|
|
|
|
|
|
while (SubR) {
|
|
|
|
if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SubR))
|
|
|
|
return SymR;
|
|
|
|
SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
|
|
|
|
}
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-04-20 09:15:42 +08:00
|
|
|
}
|
|
|
|
|
2018-02-27 08:05:04 +08:00
|
|
|
/// Perform a given operation on two integers, return whether it overflows.
|
|
|
|
/// Optionally write the resulting output into \p Res.
|
|
|
|
static bool checkedOp(
|
|
|
|
int64_t LHS,
|
|
|
|
int64_t RHS,
|
|
|
|
std::function<llvm::APInt(llvm::APInt *, const llvm::APInt &, bool &)> Op,
|
|
|
|
int64_t *Res = nullptr) {
|
|
|
|
llvm::APInt ALHS(/*BitSize=*/64, LHS, /*Signed=*/true);
|
|
|
|
llvm::APInt ARHS(/*BitSize=*/64, RHS, /*Signed=*/true);
|
|
|
|
bool Overflow;
|
|
|
|
llvm::APInt Out = Op(&ALHS, ARHS, Overflow);
|
|
|
|
if (!Overflow && Res)
|
|
|
|
*Res = Out.getSExtValue();
|
|
|
|
return Overflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool checkedAdd(
|
|
|
|
int64_t LHS,
|
|
|
|
int64_t RHS,
|
|
|
|
int64_t *Res=nullptr) {
|
|
|
|
return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov, Res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool checkedMul(
|
|
|
|
int64_t LHS,
|
|
|
|
int64_t RHS,
|
|
|
|
int64_t *Res=nullptr) {
|
|
|
|
return checkedOp(LHS, RHS, &llvm::APInt::smul_ov, Res);
|
|
|
|
}
|
|
|
|
|
2010-08-02 12:56:14 +08:00
|
|
|
RegionRawOffset ElementRegion::getAsArrayOffset() const {
|
2010-01-12 01:06:35 +08:00
|
|
|
CharUnits offset = CharUnits::Zero();
|
2009-08-01 14:17:29 +08:00
|
|
|
const ElementRegion *ER = this;
|
2014-05-27 10:45:47 +08:00
|
|
|
const MemRegion *superR = nullptr;
|
2009-08-01 14:17:29 +08:00
|
|
|
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();
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::ConcreteInt> CI = index.getAs<nonloc::ConcreteInt>()) {
|
2009-08-01 14:17:29 +08:00
|
|
|
// 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.
|
2014-10-04 05:49:03 +08:00
|
|
|
if (elemType->isIncompleteType()) {
|
2009-08-01 14:17:29 +08:00
|
|
|
superR = ER;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-12 01:06:35 +08:00
|
|
|
CharUnits size = C.getTypeSizeInChars(elemType);
|
2018-02-27 08:05:04 +08:00
|
|
|
|
|
|
|
int64_t Mult;
|
|
|
|
bool Overflow = checkedAdd(i, size.getQuantity(), &Mult);
|
|
|
|
if (!Overflow)
|
|
|
|
Overflow = checkedMul(Mult, offset.getQuantity());
|
|
|
|
if (Overflow) {
|
|
|
|
DEBUG(llvm::dbgs() << "MemRegion::getAsArrayOffset: "
|
|
|
|
<< "offset overflowing, returning unknown\n");
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
offset += (i * size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go to the next ElementRegion (if any).
|
|
|
|
ER = dyn_cast<ElementRegion>(superR);
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 14:17:29 +08:00
|
|
|
assert(superR && "super region cannot be NULL");
|
2011-01-24 09:55:39 +08:00
|
|
|
return RegionRawOffset(superR, offset);
|
2009-08-01 14:17:29 +08:00
|
|
|
}
|
|
|
|
|
2013-02-26 02:36:15 +08:00
|
|
|
|
|
|
|
/// Returns true if \p Base is an immediate base class of \p Child
|
|
|
|
static bool isImmediateBase(const CXXRecordDecl *Child,
|
|
|
|
const CXXRecordDecl *Base) {
|
2015-12-05 08:22:36 +08:00
|
|
|
assert(Child && "Child must not be null");
|
2013-02-26 02:36:15 +08:00
|
|
|
// Note that we do NOT canonicalize the base class here, because
|
|
|
|
// ASTRecordLayout doesn't either. If that leads us down the wrong path,
|
|
|
|
// so be it; at least we won't crash.
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : Child->bases()) {
|
|
|
|
if (I.getType()->getAsCXXRecordDecl() == Base)
|
2013-02-26 02:36:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
RegionOffset MemRegion::getAsOffset() const {
|
|
|
|
const MemRegion *R = this;
|
2014-05-27 10:45:47 +08:00
|
|
|
const MemRegion *SymbolicOffsetBase = nullptr;
|
2010-08-03 14:34:25 +08:00
|
|
|
int64_t Offset = 0;
|
2010-08-03 12:52:05 +08:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
switch (R->getKind()) {
|
2016-01-13 21:49:29 +08:00
|
|
|
case CodeSpaceRegionKind:
|
2013-02-15 08:32:03 +08:00
|
|
|
case StackLocalsSpaceRegionKind:
|
|
|
|
case StackArgumentsSpaceRegionKind:
|
|
|
|
case HeapSpaceRegionKind:
|
|
|
|
case UnknownSpaceRegionKind:
|
|
|
|
case StaticGlobalSpaceRegionKind:
|
|
|
|
case GlobalInternalSpaceRegionKind:
|
|
|
|
case GlobalSystemSpaceRegionKind:
|
|
|
|
case GlobalImmutableSpaceRegionKind:
|
|
|
|
// Stores can bind directly to a region space to set a default value.
|
|
|
|
assert(Offset == 0 && !SymbolicOffsetBase);
|
|
|
|
goto Finish;
|
|
|
|
|
2016-01-13 21:49:29 +08:00
|
|
|
case FunctionCodeRegionKind:
|
|
|
|
case BlockCodeRegionKind:
|
2013-02-15 08:32:03 +08:00
|
|
|
case BlockDataRegionKind:
|
|
|
|
// These will never have bindings, but may end up having values requested
|
|
|
|
// if the user does some strange casting.
|
|
|
|
if (Offset != 0)
|
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
goto Finish;
|
2012-08-10 06:55:37 +08:00
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
case SymbolicRegionKind:
|
|
|
|
case AllocaRegionKind:
|
|
|
|
case CompoundLiteralRegionKind:
|
|
|
|
case CXXThisRegionKind:
|
|
|
|
case StringRegionKind:
|
2013-02-15 08:32:03 +08:00
|
|
|
case ObjCStringRegionKind:
|
2010-08-03 12:52:05 +08:00
|
|
|
case VarRegionKind:
|
2010-11-26 16:52:48 +08:00
|
|
|
case CXXTempObjectRegionKind:
|
2013-02-15 08:32:03 +08:00
|
|
|
// Usual base regions.
|
2010-08-03 12:52:05 +08:00
|
|
|
goto Finish;
|
2012-08-10 06:55:37 +08:00
|
|
|
|
2012-08-10 06:55:51 +08:00
|
|
|
case ObjCIvarRegionKind:
|
|
|
|
// This is a little strange, but it's a compromise between
|
|
|
|
// ObjCIvarRegions having unknown compile-time offsets (when using the
|
|
|
|
// non-fragile runtime) and yet still being distinct, non-overlapping
|
|
|
|
// regions. Thus we treat them as "like" base regions for the purposes
|
|
|
|
// of computing offsets.
|
|
|
|
goto Finish;
|
|
|
|
|
2012-08-09 02:23:27 +08:00
|
|
|
case CXXBaseObjectRegionKind: {
|
|
|
|
const CXXBaseObjectRegion *BOR = cast<CXXBaseObjectRegion>(R);
|
|
|
|
R = BOR->getSuperRegion();
|
|
|
|
|
|
|
|
QualType Ty;
|
2013-02-26 02:36:15 +08:00
|
|
|
bool RootIsSymbolic = false;
|
2012-08-09 02:23:27 +08:00
|
|
|
if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(R)) {
|
|
|
|
Ty = TVR->getDesugaredValueType(getContext());
|
|
|
|
} else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
|
|
|
|
// If our base region is symbolic, we don't know what type it really is.
|
|
|
|
// Pretend the type of the symbol is the true dynamic type.
|
|
|
|
// (This will at least be self-consistent for the life of the symbol.)
|
2012-09-26 14:00:14 +08:00
|
|
|
Ty = SR->getSymbol()->getType()->getPointeeType();
|
2013-02-26 02:36:15 +08:00
|
|
|
RootIsSymbolic = true;
|
2012-08-09 02:23:27 +08:00
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-08-09 02:23:27 +08:00
|
|
|
const CXXRecordDecl *Child = Ty->getAsCXXRecordDecl();
|
2012-08-16 01:33:34 +08:00
|
|
|
if (!Child) {
|
2012-08-09 02:23:27 +08:00
|
|
|
// We cannot compute the offset of the base class.
|
2012-08-10 06:55:37 +08:00
|
|
|
SymbolicOffsetBase = R;
|
2015-12-05 08:22:36 +08:00
|
|
|
} else {
|
|
|
|
if (RootIsSymbolic) {
|
|
|
|
// Base layers on symbolic regions may not be type-correct.
|
|
|
|
// Double-check the inheritance here, and revert to a symbolic offset
|
|
|
|
// if it's invalid (e.g. due to a reinterpret_cast).
|
|
|
|
if (BOR->isVirtual()) {
|
|
|
|
if (!Child->isVirtuallyDerivedFrom(BOR->getDecl()))
|
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
} else {
|
|
|
|
if (!isImmediateBase(Child, BOR->getDecl()))
|
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
}
|
2013-02-26 02:36:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-10 06:55:37 +08:00
|
|
|
// Don't bother calculating precise offsets if we already have a
|
|
|
|
// symbolic offset somewhere in the chain.
|
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
continue;
|
|
|
|
|
2012-08-09 02:23:27 +08:00
|
|
|
CharUnits BaseOffset;
|
2013-02-21 11:12:32 +08:00
|
|
|
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Child);
|
|
|
|
if (BOR->isVirtual())
|
|
|
|
BaseOffset = Layout.getVBaseClassOffset(BOR->getDecl());
|
2012-08-09 02:23:27 +08:00
|
|
|
else
|
2013-02-21 11:12:32 +08:00
|
|
|
BaseOffset = Layout.getBaseClassOffset(BOR->getDecl());
|
2012-08-09 02:23:27 +08:00
|
|
|
|
|
|
|
// The base offset is in chars, not in bits.
|
|
|
|
Offset += BaseOffset.getQuantity() * getContext().getCharWidth();
|
|
|
|
break;
|
|
|
|
}
|
2010-08-03 12:52:05 +08:00
|
|
|
case ElementRegionKind: {
|
|
|
|
const ElementRegion *ER = cast<ElementRegion>(R);
|
2012-08-10 06:55:37 +08:00
|
|
|
R = ER->getSuperRegion();
|
2010-08-03 12:52:05 +08:00
|
|
|
|
2012-08-10 06:55:37 +08:00
|
|
|
QualType EleTy = ER->getValueType();
|
2014-10-04 05:49:03 +08:00
|
|
|
if (EleTy->isIncompleteType()) {
|
2012-08-10 06:55:37 +08:00
|
|
|
// We cannot compute the offset of the base class.
|
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
continue;
|
|
|
|
}
|
2010-08-03 12:52:05 +08:00
|
|
|
|
|
|
|
SVal Index = ER->getIndex();
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::ConcreteInt> CI =
|
2013-02-20 13:52:05 +08:00
|
|
|
Index.getAs<nonloc::ConcreteInt>()) {
|
2012-08-10 06:55:37 +08:00
|
|
|
// Don't bother calculating precise offsets if we already have a
|
2015-09-08 11:50:52 +08:00
|
|
|
// symbolic offset somewhere in the chain.
|
2012-08-10 06:55:37 +08:00
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
continue;
|
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
int64_t i = CI->getValue().getSExtValue();
|
2012-08-09 02:23:27 +08:00
|
|
|
// This type size is in bits.
|
|
|
|
Offset += i * getContext().getTypeSize(EleTy);
|
2010-08-03 12:52:05 +08:00
|
|
|
} else {
|
|
|
|
// We cannot compute offset for non-concrete index.
|
2012-08-10 06:55:37 +08:00
|
|
|
SymbolicOffsetBase = R;
|
2010-08-03 12:52:05 +08:00
|
|
|
}
|
2010-08-02 12:56:14 +08:00
|
|
|
break;
|
2010-08-03 12:52:05 +08:00
|
|
|
}
|
|
|
|
case FieldRegionKind: {
|
|
|
|
const FieldRegion *FR = cast<FieldRegion>(R);
|
2012-08-10 06:55:37 +08:00
|
|
|
R = FR->getSuperRegion();
|
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
const RecordDecl *RD = FR->getDecl()->getParent();
|
2012-10-11 07:23:21 +08:00
|
|
|
if (RD->isUnion() || !RD->isCompleteDefinition()) {
|
2010-08-03 12:52:05 +08:00
|
|
|
// We cannot compute offset for incomplete type.
|
2012-10-11 07:23:21 +08:00
|
|
|
// For unions, we could treat everything as offset 0, but we'd rather
|
|
|
|
// treat each field as a symbolic offset so they aren't stored on top
|
|
|
|
// of each other, since we depend on things in typed regions actually
|
|
|
|
// matching their types.
|
2012-08-10 06:55:37 +08:00
|
|
|
SymbolicOffsetBase = R;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't bother calculating precise offsets if we already have a
|
|
|
|
// symbolic offset somewhere in the chain.
|
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
continue;
|
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
// Get the field number.
|
|
|
|
unsigned idx = 0;
|
2015-09-08 11:50:52 +08:00
|
|
|
for (RecordDecl::field_iterator FI = RD->field_begin(),
|
2016-02-03 23:20:51 +08:00
|
|
|
FE = RD->field_end(); FI != FE; ++FI, ++idx) {
|
2012-06-07 04:45:41 +08:00
|
|
|
if (FR->getDecl() == *FI)
|
2010-08-03 12:52:05 +08:00
|
|
|
break;
|
2016-02-03 23:20:51 +08:00
|
|
|
}
|
2010-08-03 12:52:05 +08:00
|
|
|
const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
|
|
|
|
// This is offset in bits.
|
|
|
|
Offset += Layout.getFieldOffset(idx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-02 12:56:14 +08:00
|
|
|
|
2010-08-03 12:52:05 +08:00
|
|
|
Finish:
|
2012-08-10 06:55:37 +08:00
|
|
|
if (SymbolicOffsetBase)
|
|
|
|
return RegionOffset(SymbolicOffsetBase, RegionOffset::Symbolic);
|
2010-08-03 12:52:05 +08:00
|
|
|
return RegionOffset(R, Offset);
|
2010-08-02 12:56:14 +08:00
|
|
|
}
|
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BlockDataRegion
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-06 15:17:13 +08:00
|
|
|
std::pair<const VarRegion *, const VarRegion *>
|
|
|
|
BlockDataRegion::getCaptureRegions(const VarDecl *VD) {
|
|
|
|
MemRegionManager &MemMgr = *getMemRegionManager();
|
2014-05-27 10:45:47 +08:00
|
|
|
const VarRegion *VR = nullptr;
|
|
|
|
const VarRegion *OriginalVR = nullptr;
|
2012-12-06 15:17:13 +08:00
|
|
|
|
2013-12-19 10:39:40 +08:00
|
|
|
if (!VD->hasAttr<BlocksAttr>() && VD->hasLocalStorage()) {
|
2012-12-06 15:17:13 +08:00
|
|
|
VR = MemMgr.getVarRegion(VD, this);
|
|
|
|
OriginalVR = MemMgr.getVarRegion(VD, LC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (LC) {
|
|
|
|
VR = MemMgr.getVarRegion(VD, LC);
|
|
|
|
OriginalVR = VR;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
|
|
|
|
OriginalVR = MemMgr.getVarRegion(VD, LC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_pair(VR, OriginalVR);
|
|
|
|
}
|
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
void BlockDataRegion::LazyInitializeReferencedVars() {
|
|
|
|
if (ReferencedVars)
|
|
|
|
return;
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext *AC = getCodeRegion()->getAnalysisDeclContext();
|
2015-02-07 01:25:10 +08:00
|
|
|
const auto &ReferencedBlockVars = AC->getReferencedBlockVars(BC->getDecl());
|
|
|
|
auto NumBlockVars =
|
|
|
|
std::distance(ReferencedBlockVars.begin(), ReferencedBlockVars.end());
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2015-02-07 01:25:10 +08:00
|
|
|
if (NumBlockVars == 0) {
|
2009-11-26 10:34:36 +08:00
|
|
|
ReferencedVars = (void*) 0x1;
|
|
|
|
return;
|
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
MemRegionManager &MemMgr = *getMemRegionManager();
|
|
|
|
llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
|
|
|
|
BumpVectorContext BC(A);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
typedef BumpVector<const MemRegion*> VarVec;
|
2016-01-29 21:53:26 +08:00
|
|
|
VarVec *BV = A.Allocate<VarVec>();
|
2015-02-07 01:25:10 +08:00
|
|
|
new (BV) VarVec(BC, NumBlockVars);
|
2016-01-29 21:53:26 +08:00
|
|
|
VarVec *BVOriginal = A.Allocate<VarVec>();
|
2015-02-07 01:25:10 +08:00
|
|
|
new (BVOriginal) VarVec(BC, NumBlockVars);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2015-02-07 01:25:10 +08:00
|
|
|
for (const VarDecl *VD : ReferencedBlockVars) {
|
2014-05-27 10:45:47 +08:00
|
|
|
const VarRegion *VR = nullptr;
|
|
|
|
const VarRegion *OriginalVR = nullptr;
|
2015-02-07 01:25:10 +08:00
|
|
|
std::tie(VR, OriginalVR) = getCaptureRegions(VD);
|
2009-12-08 06:05:27 +08:00
|
|
|
assert(VR);
|
2012-05-05 05:48:42 +08:00
|
|
|
assert(OriginalVR);
|
2009-12-08 06:05:27 +08:00
|
|
|
BV->push_back(VR, BC);
|
2012-05-05 05:48:42 +08:00
|
|
|
BVOriginal->push_back(OriginalVR, BC);
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2009-11-26 10:34:36 +08:00
|
|
|
ReferencedVars = BV;
|
2012-05-05 05:48:42 +08:00
|
|
|
OriginalVars = BVOriginal;
|
2009-11-26 10:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockDataRegion::referenced_vars_iterator
|
|
|
|
BlockDataRegion::referenced_vars_begin() const {
|
|
|
|
const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
|
|
|
|
|
|
|
|
BumpVector<const MemRegion*> *Vec =
|
|
|
|
static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2012-05-05 05:48:42 +08:00
|
|
|
if (Vec == (void*) 0x1)
|
2014-05-27 10:45:47 +08:00
|
|
|
return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
|
|
|
|
|
2012-05-05 05:48:42 +08:00
|
|
|
BumpVector<const MemRegion*> *VecOriginal =
|
|
|
|
static_cast<BumpVector<const MemRegion*>*>(OriginalVars);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-05-05 05:48:42 +08:00
|
|
|
return BlockDataRegion::referenced_vars_iterator(Vec->begin(),
|
|
|
|
VecOriginal->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);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
2012-05-05 05:48:42 +08:00
|
|
|
if (Vec == (void*) 0x1)
|
2014-05-27 10:45:47 +08:00
|
|
|
return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
|
|
|
|
|
2012-05-05 05:48:42 +08:00
|
|
|
BumpVector<const MemRegion*> *VecOriginal =
|
|
|
|
static_cast<BumpVector<const MemRegion*>*>(OriginalVars);
|
|
|
|
|
|
|
|
return BlockDataRegion::referenced_vars_iterator(Vec->end(),
|
|
|
|
VecOriginal->end());
|
2009-11-26 10:34:36 +08:00
|
|
|
}
|
2013-02-06 06:00:19 +08:00
|
|
|
|
|
|
|
const VarRegion *BlockDataRegion::getOriginalRegion(const VarRegion *R) const {
|
|
|
|
for (referenced_vars_iterator I = referenced_vars_begin(),
|
|
|
|
E = referenced_vars_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I.getCapturedRegion() == R)
|
|
|
|
return I.getOriginalRegion();
|
|
|
|
}
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2013-02-06 06:00:19 +08:00
|
|
|
}
|
2013-09-25 07:47:29 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RegionAndSymbolInvalidationTraits
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
void RegionAndSymbolInvalidationTraits::setTrait(SymbolRef Sym,
|
2013-09-25 07:47:29 +08:00
|
|
|
InvalidationKinds IK) {
|
|
|
|
SymTraitsMap[Sym] |= IK;
|
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
void RegionAndSymbolInvalidationTraits::setTrait(const MemRegion *MR,
|
2013-09-25 07:47:29 +08:00
|
|
|
InvalidationKinds IK) {
|
|
|
|
assert(MR);
|
|
|
|
if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
|
|
|
|
setTrait(SR->getSymbol(), IK);
|
|
|
|
else
|
|
|
|
MRTraitsMap[MR] |= IK;
|
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
bool RegionAndSymbolInvalidationTraits::hasTrait(SymbolRef Sym,
|
2016-02-03 23:20:51 +08:00
|
|
|
InvalidationKinds IK) const {
|
2013-09-25 07:47:29 +08:00
|
|
|
const_symbol_iterator I = SymTraitsMap.find(Sym);
|
|
|
|
if (I != SymTraitsMap.end())
|
|
|
|
return I->second & IK;
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
return false;
|
2013-09-25 07:47:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RegionAndSymbolInvalidationTraits::hasTrait(const MemRegion *MR,
|
2016-02-03 23:20:51 +08:00
|
|
|
InvalidationKinds IK) const {
|
2013-09-25 07:47:29 +08:00
|
|
|
if (!MR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
|
|
|
|
return hasTrait(SR->getSymbol(), IK);
|
|
|
|
|
|
|
|
const_region_iterator I = MRTraitsMap.find(MR);
|
|
|
|
if (I != MRTraitsMap.end())
|
|
|
|
return I->second & IK;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|