2008-10-08 10:50:44 +08:00
|
|
|
//== RegionStore.cpp - Field-sensitive store model --------------*- 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 a basic region store model. In this model, we do have field
|
|
|
|
// sensitivity. But we assume nothing about the heap shape. So recursive data
|
|
|
|
// structures are largely ignored. Basically we do 1-limiting analysis.
|
|
|
|
// Parameter pointers are assumed with no aliasing. Pointee objects of
|
|
|
|
// parameters are created lazily.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Analysis/PathSensitive/MemRegion.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/GRState.h"
|
2008-11-16 12:07:26 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRStateTrait.h"
|
2008-10-08 10:50:44 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2009-05-06 19:51:48 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-10-08 10:50:44 +08:00
|
|
|
|
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
2008-11-16 12:07:26 +08:00
|
|
|
#include "llvm/ADT/ImmutableList.h"
|
2008-10-24 14:01:33 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-10-08 10:50:44 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-11-24 17:44:56 +08:00
|
|
|
// Actual Store type.
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
|
2008-11-24 17:44:56 +08:00
|
|
|
|
2009-06-17 06:36:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Fine-grained control of RegionStoreManager.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct VISIBILITY_HIDDEN minimal_features_tag {};
|
|
|
|
struct VISIBILITY_HIDDEN maximal_features_tag {};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN RegionStoreFeatures {
|
|
|
|
bool SupportsFields;
|
|
|
|
bool SupportsRemaining;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RegionStoreFeatures(minimal_features_tag) :
|
|
|
|
SupportsFields(false), SupportsRemaining(false) {}
|
|
|
|
|
|
|
|
RegionStoreFeatures(maximal_features_tag) :
|
|
|
|
SupportsFields(true), SupportsRemaining(false) {}
|
|
|
|
|
|
|
|
void enableFields(bool t) { SupportsFields = t; }
|
|
|
|
|
|
|
|
bool supportsFields() const { return SupportsFields; }
|
|
|
|
bool supportsRemaining() const { return SupportsRemaining; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-12-24 09:05:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region "Views"
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// MemRegions can be layered on top of each other. This GDM entry tracks
|
|
|
|
// what are the MemRegions that layer a given MemRegion.
|
|
|
|
//
|
2009-01-13 09:49:57 +08:00
|
|
|
typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
|
2008-12-24 09:05:03 +08:00
|
|
|
namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
|
|
|
|
static int RegionViewMapIndex = 0;
|
2008-11-16 12:07:26 +08:00
|
|
|
namespace clang {
|
2008-12-24 09:05:03 +08:00
|
|
|
template<> struct GRStateTrait<RegionViewMap>
|
|
|
|
: public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
|
|
|
|
RegionViews> > {
|
|
|
|
|
|
|
|
static void* GDMIndex() { return &RegionViewMapIndex; }
|
|
|
|
};
|
2008-11-16 12:07:26 +08:00
|
|
|
}
|
2008-10-08 10:50:44 +08:00
|
|
|
|
2009-05-06 16:33:50 +08:00
|
|
|
// RegionCasts records the current cast type of a region.
|
|
|
|
namespace { class VISIBILITY_HIDDEN RegionCasts {}; }
|
|
|
|
static int RegionCastsIndex = 0;
|
|
|
|
namespace clang {
|
|
|
|
template<> struct GRStateTrait<RegionCasts>
|
|
|
|
: public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
|
|
|
|
QualType> > {
|
|
|
|
static void* GDMIndex() { return &RegionCastsIndex; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-12-24 09:05:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region "Extents"
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// MemRegions represent chunks of memory with a size (their "extent"). This
|
|
|
|
// GDM entry tracks the extents for regions. Extents are in bytes.
|
2009-01-08 06:18:50 +08:00
|
|
|
//
|
2008-12-24 09:05:03 +08:00
|
|
|
namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
|
|
|
|
static int RegionExtentsIndex = 0;
|
2008-11-24 17:44:56 +08:00
|
|
|
namespace clang {
|
2008-12-24 09:05:03 +08:00
|
|
|
template<> struct GRStateTrait<RegionExtents>
|
|
|
|
: public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
|
|
|
|
static void* GDMIndex() { return &RegionExtentsIndex; }
|
|
|
|
};
|
2008-11-24 17:44:56 +08:00
|
|
|
}
|
|
|
|
|
2008-12-24 09:05:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-01-13 09:49:57 +08:00
|
|
|
// Regions with default values.
|
2008-12-24 09:05:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-01-13 09:49:57 +08:00
|
|
|
// This GDM entry tracks what regions have a default value if they have no bound
|
|
|
|
// value and have not been killed.
|
2008-12-24 09:05:03 +08:00
|
|
|
//
|
|
|
|
namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
|
|
|
|
static int RegionDefaultValueIndex = 0;
|
|
|
|
namespace clang {
|
|
|
|
template<> struct GRStateTrait<RegionDefaultValue>
|
|
|
|
: public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
|
|
|
|
static void* GDMIndex() { return &RegionDefaultValueIndex; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Main RegionStore logic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-12-04 10:08:27 +08:00
|
|
|
|
2008-10-08 10:50:44 +08:00
|
|
|
namespace {
|
|
|
|
|
2009-03-03 09:35:36 +08:00
|
|
|
class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
|
|
|
|
typedef llvm::DenseMap<const MemRegion*,
|
|
|
|
llvm::ImmutableSet<const MemRegion*> > Map;
|
|
|
|
|
|
|
|
llvm::ImmutableSet<const MemRegion*>::Factory F;
|
|
|
|
Map M;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void add(const MemRegion* Parent, const MemRegion* SubRegion) {
|
|
|
|
Map::iterator I = M.find(Parent);
|
|
|
|
M.insert(std::make_pair(Parent,
|
|
|
|
F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
|
|
|
|
}
|
|
|
|
|
|
|
|
~RegionStoreSubRegionMap() {}
|
|
|
|
|
2009-03-03 10:51:43 +08:00
|
|
|
bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
|
2009-03-03 09:35:36 +08:00
|
|
|
Map::iterator I = M.find(Parent);
|
|
|
|
|
|
|
|
if (I == M.end())
|
2009-03-03 10:51:43 +08:00
|
|
|
return true;
|
2009-03-03 09:35:36 +08:00
|
|
|
|
|
|
|
llvm::ImmutableSet<const MemRegion*> S = I->second;
|
|
|
|
for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
|
|
|
|
SI != SE; ++SI) {
|
|
|
|
if (!V.Visit(Parent, *SI))
|
2009-03-03 10:51:43 +08:00
|
|
|
return false;
|
2009-03-03 09:35:36 +08:00
|
|
|
}
|
2009-03-03 10:51:43 +08:00
|
|
|
|
|
|
|
return true;
|
2009-03-03 09:35:36 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-10-08 10:50:44 +08:00
|
|
|
class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
|
2009-06-17 06:36:44 +08:00
|
|
|
const RegionStoreFeatures Features;
|
2008-10-08 10:50:44 +08:00
|
|
|
RegionBindingsTy::Factory RBFactory;
|
2008-12-24 09:05:03 +08:00
|
|
|
RegionViews::Factory RVFactory;
|
2008-11-16 12:07:26 +08:00
|
|
|
|
2009-01-23 07:43:57 +08:00
|
|
|
const MemRegion* SelfRegion;
|
|
|
|
const ImplicitParamDecl *SelfDecl;
|
2008-10-08 10:50:44 +08:00
|
|
|
|
|
|
|
public:
|
2009-06-17 06:36:44 +08:00
|
|
|
RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
|
2009-07-07 04:21:51 +08:00
|
|
|
: StoreManager(mgr, true),
|
2009-06-17 06:36:44 +08:00
|
|
|
Features(f),
|
2009-01-08 06:18:50 +08:00
|
|
|
RBFactory(mgr.getAllocator()),
|
2008-11-16 12:07:26 +08:00
|
|
|
RVFactory(mgr.getAllocator()),
|
2009-04-22 05:51:34 +08:00
|
|
|
SelfRegion(0), SelfDecl(0) {
|
2009-01-23 07:43:57 +08:00
|
|
|
if (const ObjCMethodDecl* MD =
|
|
|
|
dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
|
|
|
|
SelfDecl = MD->getSelfDecl();
|
|
|
|
}
|
2008-10-08 10:50:44 +08:00
|
|
|
|
|
|
|
virtual ~RegionStoreManager() {}
|
|
|
|
|
2009-03-04 03:02:42 +08:00
|
|
|
SubRegionMap* getSubRegionMap(const GRState *state);
|
2009-03-03 09:35:36 +08:00
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
/// getLValueString - Returns an SVal representing the lvalue of a
|
|
|
|
/// StringLiteral. Within RegionStore a StringLiteral has an
|
|
|
|
/// associated StringRegion, and the lvalue of a StringLiteral is
|
|
|
|
/// the lvalue of that region.
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal getLValueString(const GRState *state, const StringLiteral* S);
|
2008-10-25 22:18:57 +08:00
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
/// getLValueCompoundLiteral - Returns an SVal representing the
|
|
|
|
/// lvalue of a compound literal. Within RegionStore a compound
|
|
|
|
/// literal has an associated region, and the lvalue of the
|
|
|
|
/// compound literal is the lvalue of that region.
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
|
2008-11-07 18:38:33 +08:00
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
/// getLValueVar - Returns an SVal that represents the lvalue of a
|
|
|
|
/// variable. Within RegionStore a variable has an associated
|
|
|
|
/// VarRegion, and the lvalue of the variable is the lvalue of that region.
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal getLValueVar(const GRState *state, const VarDecl* VD);
|
2008-10-22 21:44:38 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
|
2008-10-22 21:44:38 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
|
2009-03-05 12:50:08 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
|
2008-10-22 21:44:38 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal getLValueElement(const GRState *state, QualType elementType,
|
2009-05-04 14:18:28 +08:00
|
|
|
SVal Base, SVal Offset);
|
2008-10-24 09:09:32 +08:00
|
|
|
|
2008-11-22 21:21:46 +08:00
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
/// ArrayToPointer - Emulates the "decay" of an array to a pointer
|
|
|
|
/// type. 'Array' represents the lvalue of the array being decayed
|
|
|
|
/// to a pointer, and the returned SVal represents the decayed
|
|
|
|
/// version of that lvalue (i.e., a pointer to the first element of
|
|
|
|
/// the array). This is called by GRExprEngine when evaluating
|
|
|
|
/// casts from arrays to pointers.
|
2009-03-30 13:55:46 +08:00
|
|
|
SVal ArrayToPointer(Loc Array);
|
2008-10-24 09:09:32 +08:00
|
|
|
|
2009-06-25 07:06:47 +08:00
|
|
|
SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
|
2009-06-26 08:41:43 +08:00
|
|
|
NonLoc R, QualType resultTy);
|
2008-10-24 09:38:55 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
|
2008-10-25 04:32:16 +08:00
|
|
|
|
|
|
|
/// getSelfRegion - Returns the region for the 'self' (Objective-C) or
|
|
|
|
/// 'this' object (C++). When used when analyzing a normal function this
|
|
|
|
/// method returns NULL.
|
|
|
|
const MemRegion* getSelfRegion(Store) {
|
2009-01-23 07:43:57 +08:00
|
|
|
if (!SelfDecl)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!SelfRegion) {
|
|
|
|
const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
|
|
|
|
SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
|
|
|
|
MRMgr.getHeapRegion());
|
|
|
|
}
|
|
|
|
|
|
|
|
return SelfRegion;
|
2008-10-25 04:32:16 +08:00
|
|
|
}
|
2009-06-18 06:02:04 +08:00
|
|
|
|
|
|
|
//===-------------------------------------------------------------------===//
|
|
|
|
// Binding values to regions.
|
|
|
|
//===-------------------------------------------------------------------===//
|
2008-12-20 14:32:12 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *Bind(const GRState *state, Loc LV, SVal V);
|
2008-10-21 13:29:26 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *BindCompoundLiteral(const GRState *state,
|
|
|
|
const CompoundLiteralExpr* CL, SVal V);
|
|
|
|
|
|
|
|
const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
|
2008-11-24 17:44:56 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
|
|
|
|
return state;
|
2008-10-08 10:50:44 +08:00
|
|
|
}
|
2008-10-24 09:38:55 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
/// BindStruct - Bind a compound value to a structure.
|
|
|
|
const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
|
|
|
|
|
|
|
|
const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
|
|
|
|
|
|
|
|
/// KillStruct - Set the entire struct to unknown.
|
|
|
|
const GRState *KillStruct(const GRState *state, const TypedRegion* R);
|
2008-10-24 09:38:55 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
|
2008-10-31 15:16:08 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
Store Remove(Store store, Loc LV);
|
|
|
|
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
// Loading values from regions.
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// The high level logic for this method is this:
|
|
|
|
/// Retrieve (L)
|
|
|
|
/// if L has binding
|
|
|
|
/// return L's binding
|
|
|
|
/// else if L is in killset
|
|
|
|
/// return unknown
|
|
|
|
/// else
|
|
|
|
/// if L is on stack or heap
|
|
|
|
/// return undefined
|
|
|
|
/// else
|
|
|
|
/// return symbolic
|
|
|
|
SVal Retrieve(const GRState *state, Loc L, QualType T = QualType());
|
2009-06-25 12:50:44 +08:00
|
|
|
|
2009-06-25 13:29:39 +08:00
|
|
|
SVal RetrieveElement(const GRState* state, const ElementRegion* R);
|
|
|
|
|
2009-06-25 12:50:44 +08:00
|
|
|
SVal RetrieveField(const GRState* state, const FieldRegion* R);
|
|
|
|
|
2008-12-04 09:12:41 +08:00
|
|
|
/// Retrieve the values in a struct and return a CompoundVal, used when doing
|
|
|
|
/// struct copy:
|
|
|
|
/// struct s x, y;
|
|
|
|
/// x = y;
|
|
|
|
/// y's value is retrieved by this method.
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
|
|
|
|
|
|
|
|
SVal RetrieveArray(const GRState *St, const TypedRegion* R);
|
2008-12-04 09:12:41 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
// State pruning.
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
|
|
|
|
/// It returns a new Store with these values removed.
|
|
|
|
Store RemoveDeadBindings(const GRState *state, Stmt* Loc, SymbolReaper& SymReaper,
|
|
|
|
llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
|
2009-05-03 08:27:40 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
// Region "extents".
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
|
|
|
|
SVal getSizeInElements(const GRState *state, const MemRegion* R);
|
2008-11-23 12:30:35 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
// Region "views".
|
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
const GRState *AddRegionView(const GRState *state, const MemRegion* View,
|
|
|
|
const MemRegion* Base);
|
|
|
|
|
|
|
|
const GRState *RemoveRegionView(const GRState *state, const MemRegion* View,
|
|
|
|
const MemRegion* Base);
|
2009-01-13 09:49:57 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
//===------------------------------------------------------------------===//
|
2008-10-31 15:16:08 +08:00
|
|
|
// Utility methods.
|
2009-06-18 06:02:04 +08:00
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
|
2009-07-07 04:21:51 +08:00
|
|
|
const GRState *setCastType(const GRState *state, const MemRegion* R,
|
|
|
|
QualType T);
|
2009-06-18 06:02:04 +08:00
|
|
|
|
2009-07-14 09:12:46 +08:00
|
|
|
const QualType *getCastType(const GRState *state, const MemRegion *R) {
|
|
|
|
return state->get<RegionCasts>(R);
|
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
static inline RegionBindingsTy GetRegionBindings(Store store) {
|
|
|
|
return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
|
|
|
|
}
|
|
|
|
|
2009-06-25 07:06:47 +08:00
|
|
|
void print(Store store, llvm::raw_ostream& Out, const char* nl,
|
|
|
|
const char *sep);
|
2009-05-08 09:33:18 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
void iterBindings(Store store, BindingsHandler& f) {
|
|
|
|
// FIXME: Implement.
|
|
|
|
}
|
2008-11-16 12:07:26 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
// FIXME: Remove.
|
|
|
|
BasicValueFactory& getBasicVals() {
|
|
|
|
return StateMgr.getBasicVals();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Remove.
|
|
|
|
ASTContext& getContext() { return StateMgr.getContext(); }
|
2008-10-08 10:50:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2009-07-14 09:12:46 +08:00
|
|
|
static bool isGenericPtr(ASTContext &Ctx, QualType Ty) {
|
|
|
|
if (Ty->isObjCIdType() || Ty->isObjCQualifiedIdType())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
Ty = Ctx.getCanonicalType(Ty);
|
|
|
|
|
|
|
|
if (Ty->isVoidType())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (const PointerType *PT = Ty->getAsPointerType()) {
|
|
|
|
Ty = PT->getPointeeType();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-06-17 06:36:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// RegionStore creation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
|
|
|
|
RegionStoreFeatures F = maximal_features_tag();
|
|
|
|
return new RegionStoreManager(StMgr, F);
|
|
|
|
}
|
|
|
|
|
|
|
|
StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
|
|
|
|
RegionStoreFeatures F = minimal_features_tag();
|
|
|
|
F.enableFields(true);
|
|
|
|
return new RegionStoreManager(StMgr, F);
|
2008-10-24 09:04:59 +08:00
|
|
|
}
|
|
|
|
|
2009-03-04 03:02:42 +08:00
|
|
|
SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
|
2009-03-03 09:35:36 +08:00
|
|
|
RegionBindingsTy B = GetRegionBindings(state->getStore());
|
|
|
|
RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
|
|
|
|
|
|
|
|
for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
|
|
|
|
if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
|
|
|
|
M->add(R->getSuperRegion(), R);
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:02:42 +08:00
|
|
|
return M;
|
2009-03-03 09:35:36 +08:00
|
|
|
}
|
2008-12-24 15:46:32 +08:00
|
|
|
|
2009-06-17 06:36:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// getLValueXXX methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
/// getLValueString - Returns an SVal representing the lvalue of a
|
|
|
|
/// StringLiteral. Within RegionStore a StringLiteral has an
|
|
|
|
/// associated StringRegion, and the lvalue of a StringLiteral is the
|
|
|
|
/// lvalue of that region.
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal RegionStoreManager::getLValueString(const GRState *St,
|
2008-10-25 22:18:57 +08:00
|
|
|
const StringLiteral* S) {
|
|
|
|
return loc::MemRegionVal(MRMgr.getStringRegion(S));
|
|
|
|
}
|
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
/// getLValueVar - Returns an SVal that represents the lvalue of a
|
|
|
|
/// variable. Within RegionStore a variable has an associated
|
|
|
|
/// VarRegion, and the lvalue of the variable is the lvalue of that region.
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
|
2008-10-22 21:44:38 +08:00
|
|
|
return loc::MemRegionVal(MRMgr.getVarRegion(VD));
|
|
|
|
}
|
2008-11-07 18:38:33 +08:00
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
|
|
|
|
/// of a compound literal. Within RegionStore a compound literal
|
|
|
|
/// has an associated region, and the lvalue of the compound literal
|
|
|
|
/// is the lvalue of that region.
|
|
|
|
SVal
|
2009-06-18 06:02:04 +08:00
|
|
|
RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
|
2008-12-24 15:46:32 +08:00
|
|
|
const CompoundLiteralExpr* CL) {
|
2008-11-07 18:38:33 +08:00
|
|
|
return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
|
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
|
2008-10-22 21:44:38 +08:00
|
|
|
SVal Base) {
|
2009-03-05 12:50:08 +08:00
|
|
|
return getLValueFieldOrIvar(St, Base, D);
|
2008-10-22 21:44:38 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
|
2008-10-22 21:44:38 +08:00
|
|
|
const FieldDecl* D) {
|
2009-03-05 12:50:08 +08:00
|
|
|
return getLValueFieldOrIvar(St, Base, D);
|
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
|
2009-03-05 12:50:08 +08:00
|
|
|
const Decl* D) {
|
2008-10-22 21:44:38 +08:00
|
|
|
if (Base.isUnknownOrUndef())
|
|
|
|
return Base;
|
|
|
|
|
|
|
|
Loc BaseL = cast<Loc>(Base);
|
|
|
|
const MemRegion* BaseR = 0;
|
|
|
|
|
|
|
|
switch (BaseL.getSubKind()) {
|
|
|
|
case loc::MemRegionKind:
|
|
|
|
BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case loc::GotoLabelKind:
|
|
|
|
// These are anormal cases. Flag an undefined value.
|
|
|
|
return UndefinedVal();
|
|
|
|
|
|
|
|
case loc::ConcreteIntKind:
|
|
|
|
// While these seem funny, this can happen through casts.
|
|
|
|
// FIXME: What we should return is the field offset. For example,
|
|
|
|
// add the field offset to the integer value. That way funny things
|
|
|
|
// like this work properly: &(((struct foo *) 0xa)->f)
|
|
|
|
return Base;
|
|
|
|
|
|
|
|
default:
|
2008-11-07 16:57:30 +08:00
|
|
|
assert(0 && "Unhandled Base.");
|
2008-10-22 21:44:38 +08:00
|
|
|
return Base;
|
|
|
|
}
|
2009-03-05 12:50:08 +08:00
|
|
|
|
|
|
|
// NOTE: We must have this check first because ObjCIvarDecl is a subclass
|
|
|
|
// of FieldDecl.
|
|
|
|
if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
|
|
|
|
return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
|
2008-10-22 21:44:38 +08:00
|
|
|
|
2009-03-05 12:50:08 +08:00
|
|
|
return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
|
2008-10-22 21:44:38 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal RegionStoreManager::getLValueElement(const GRState *St,
|
2009-05-04 14:18:28 +08:00
|
|
|
QualType elementType,
|
2008-10-24 09:09:32 +08:00
|
|
|
SVal Base, SVal Offset) {
|
|
|
|
|
2009-03-10 06:44:49 +08:00
|
|
|
// If the base is an unknown or undefined value, just return it back.
|
|
|
|
// FIXME: For absolute pointer addresses, we just return that value back as
|
|
|
|
// well, although in reality we should return the offset added to that
|
|
|
|
// value.
|
|
|
|
if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
|
2008-10-27 20:23:17 +08:00
|
|
|
return Base;
|
|
|
|
|
2009-01-23 04:27:48 +08:00
|
|
|
// Only handle integer offsets... for now.
|
|
|
|
if (!isa<nonloc::ConcreteInt>(Offset))
|
2008-11-13 17:48:44 +08:00
|
|
|
return UnknownVal();
|
2008-10-24 09:09:32 +08:00
|
|
|
|
2009-05-09 21:20:07 +08:00
|
|
|
const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
|
2008-10-24 09:09:32 +08:00
|
|
|
|
2009-01-23 04:27:48 +08:00
|
|
|
// Pointer of any type can be cast and used as array base.
|
|
|
|
const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
|
|
|
|
|
|
|
|
if (!ElemR) {
|
|
|
|
//
|
|
|
|
// If the base region is not an ElementRegion, create one.
|
|
|
|
// This can happen in the following example:
|
|
|
|
//
|
|
|
|
// char *p = __builtin_alloc(10);
|
|
|
|
// p[1] = 8;
|
|
|
|
//
|
2009-05-09 21:20:07 +08:00
|
|
|
// Observe that 'p' binds to an AllocaRegion.
|
2009-01-23 04:27:48 +08:00
|
|
|
//
|
2009-02-19 16:37:16 +08:00
|
|
|
|
|
|
|
// Offset might be unsigned. We have to convert it to signed ConcreteInt.
|
|
|
|
if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
|
|
|
|
const llvm::APSInt& OffI = CI->getValue();
|
|
|
|
if (OffI.isUnsigned()) {
|
|
|
|
llvm::APSInt Tmp = OffI;
|
|
|
|
Tmp.setIsSigned(true);
|
2009-06-23 17:02:15 +08:00
|
|
|
Offset = ValMgr.makeIntVal(Tmp);
|
2009-02-19 16:37:16 +08:00
|
|
|
}
|
|
|
|
}
|
2009-05-04 14:18:28 +08:00
|
|
|
return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
|
2009-06-16 17:55:50 +08:00
|
|
|
BaseRegion, getContext()));
|
2009-01-23 04:27:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SVal BaseIdx = ElemR->getIndex();
|
|
|
|
|
|
|
|
if (!isa<nonloc::ConcreteInt>(BaseIdx))
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
|
|
|
|
const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
|
|
|
|
assert(BaseIdxI.isSigned());
|
|
|
|
|
|
|
|
// FIXME: This appears to be the assumption of this code. We should review
|
|
|
|
// whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
|
|
|
|
// can't we need to put a comment here. If it can, we should handle it.
|
|
|
|
assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
|
2008-11-13 17:15:14 +08:00
|
|
|
|
2009-05-09 21:20:07 +08:00
|
|
|
const MemRegion *ArrayR = ElemR->getSuperRegion();
|
2009-01-23 04:27:48 +08:00
|
|
|
SVal NewIdx;
|
|
|
|
|
|
|
|
if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
|
|
|
|
// 'Offset' might be unsigned. We have to convert it to signed and
|
|
|
|
// possibly extend it.
|
|
|
|
llvm::APSInt Tmp = OffI;
|
|
|
|
|
|
|
|
if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
|
|
|
|
Tmp.extend(BaseIdxI.getBitWidth());
|
|
|
|
|
|
|
|
Tmp.setIsSigned(true);
|
|
|
|
Tmp += BaseIdxI; // Compute the new offset.
|
2009-06-23 17:02:15 +08:00
|
|
|
NewIdx = ValMgr.makeIntVal(Tmp);
|
2008-10-24 09:09:32 +08:00
|
|
|
}
|
2009-01-23 04:27:48 +08:00
|
|
|
else
|
|
|
|
NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
|
2008-10-24 09:09:32 +08:00
|
|
|
|
2009-06-16 17:55:50 +08:00
|
|
|
return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
|
|
|
|
getContext()));
|
2008-10-24 09:09:32 +08:00
|
|
|
}
|
|
|
|
|
2009-06-17 06:36:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Extents for regions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal RegionStoreManager::getSizeInElements(const GRState *state,
|
2009-07-11 06:30:06 +08:00
|
|
|
const MemRegion *R) {
|
|
|
|
|
|
|
|
switch (R->getKind()) {
|
|
|
|
case MemRegion::MemSpaceRegionKind:
|
|
|
|
assert(0 && "Cannot index into a MemSpace");
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
case MemRegion::CodeTextRegionKind:
|
|
|
|
// Technically this can happen if people do funny things with casts.
|
|
|
|
return UnknownVal();
|
2008-11-22 21:21:46 +08:00
|
|
|
|
2009-07-11 06:30:06 +08:00
|
|
|
// Not yet handled.
|
|
|
|
case MemRegion::AllocaRegionKind:
|
|
|
|
case MemRegion::CompoundLiteralRegionKind:
|
|
|
|
case MemRegion::ElementRegionKind:
|
|
|
|
case MemRegion::FieldRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
|
|
|
case MemRegion::ObjCObjectRegionKind:
|
|
|
|
case MemRegion::SymbolicRegionKind:
|
2009-01-30 08:08:43 +08:00
|
|
|
return UnknownVal();
|
2009-07-11 06:30:06 +08:00
|
|
|
|
|
|
|
case MemRegion::StringRegionKind: {
|
|
|
|
const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
|
|
|
|
// We intentionally made the size value signed because it participates in
|
|
|
|
// operations with signed indices.
|
|
|
|
return ValMgr.makeIntVal(Str->getByteLength()+1, false);
|
2009-01-30 08:08:43 +08:00
|
|
|
}
|
2009-07-11 06:30:06 +08:00
|
|
|
|
|
|
|
// TypedViewRegion will soon be removed.
|
|
|
|
case MemRegion::TypedViewRegionKind:
|
|
|
|
return UnknownVal();
|
2009-05-06 16:33:50 +08:00
|
|
|
|
2009-07-11 06:30:06 +08:00
|
|
|
case MemRegion::VarRegionKind: {
|
|
|
|
const VarRegion* VR = cast<VarRegion>(R);
|
|
|
|
// Get the type of the variable.
|
|
|
|
QualType T = VR->getDesugaredValueType(getContext());
|
|
|
|
|
|
|
|
// FIXME: Handle variable-length arrays.
|
|
|
|
if (isa<VariableArrayType>(T))
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
|
|
|
|
// return the size as signed integer.
|
|
|
|
return ValMgr.makeIntVal(CAT->getSize(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const QualType* CastTy = state->get<RegionCasts>(VR);
|
|
|
|
|
|
|
|
// If the VarRegion is cast to other type, compute the size with respect to
|
|
|
|
// that type.
|
|
|
|
if (CastTy) {
|
|
|
|
QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
|
|
|
|
QualType VarTy = VR->getValueType(getContext());
|
|
|
|
uint64_t EleSize = getContext().getTypeSize(EleTy);
|
|
|
|
uint64_t VarSize = getContext().getTypeSize(VarTy);
|
|
|
|
assert(VarSize != 0);
|
|
|
|
return ValMgr.makeIntVal(VarSize/EleSize, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clients can use ordinary variables as if they were arrays. These
|
|
|
|
// essentially are arrays of size 1.
|
|
|
|
return ValMgr.makeIntVal(1, false);
|
2009-05-06 19:51:48 +08:00
|
|
|
}
|
2009-07-11 06:30:06 +08:00
|
|
|
|
|
|
|
case MemRegion::BEG_DECL_REGIONS:
|
|
|
|
case MemRegion::END_DECL_REGIONS:
|
|
|
|
case MemRegion::BEG_TYPED_REGIONS:
|
|
|
|
case MemRegion::END_TYPED_REGIONS:
|
|
|
|
assert(0 && "Infeasible region");
|
|
|
|
return UnknownVal();
|
2009-05-06 16:08:27 +08:00
|
|
|
}
|
2009-07-11 06:30:06 +08:00
|
|
|
|
|
|
|
assert(0 && "Unreachable");
|
2009-01-07 03:12:06 +08:00
|
|
|
return UnknownVal();
|
2008-11-22 21:21:46 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *RegionStoreManager::setExtent(const GRState *state,
|
|
|
|
const MemRegion *region,
|
|
|
|
SVal extent) {
|
|
|
|
return state->set<RegionExtents>(region, extent);
|
2009-06-17 06:36:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Location and region casting.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
/// ArrayToPointer - Emulates the "decay" of an array to a pointer
|
|
|
|
/// type. 'Array' represents the lvalue of the array being decayed
|
|
|
|
/// to a pointer, and the returned SVal represents the decayed
|
|
|
|
/// version of that lvalue (i.e., a pointer to the first element of
|
|
|
|
/// the array). This is called by GRExprEngine when evaluating casts
|
|
|
|
/// from arrays to pointers.
|
2009-03-30 13:55:46 +08:00
|
|
|
SVal RegionStoreManager::ArrayToPointer(Loc Array) {
|
2008-12-14 03:24:37 +08:00
|
|
|
if (!isa<loc::MemRegionVal>(Array))
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
|
|
|
|
const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
|
|
|
|
|
2009-01-13 09:03:27 +08:00
|
|
|
if (!ArrayR)
|
2008-12-14 03:24:37 +08:00
|
|
|
return UnknownVal();
|
|
|
|
|
2009-05-09 11:57:34 +08:00
|
|
|
// Strip off typedefs from the ArrayRegion's ValueType.
|
|
|
|
QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
|
2009-05-04 14:18:28 +08:00
|
|
|
ArrayType *AT = cast<ArrayType>(T);
|
|
|
|
T = AT->getElementType();
|
|
|
|
|
2008-11-23 12:30:35 +08:00
|
|
|
nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
|
2009-06-16 17:55:50 +08:00
|
|
|
ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR, getContext());
|
2008-10-26 10:23:57 +08:00
|
|
|
|
|
|
|
return loc::MemRegionVal(ER);
|
2008-10-24 09:09:32 +08:00
|
|
|
}
|
|
|
|
|
2009-06-17 06:36:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pointer arithmetic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-20 17:00:16 +08:00
|
|
|
SVal RegionStoreManager::EvalBinOp(const GRState *state,
|
2009-06-26 08:41:43 +08:00
|
|
|
BinaryOperator::Opcode Op, Loc L, NonLoc R,
|
|
|
|
QualType resultTy) {
|
2009-05-09 23:18:12 +08:00
|
|
|
// Assume the base location is MemRegionVal.
|
2009-03-03 10:51:43 +08:00
|
|
|
if (!isa<loc::MemRegionVal>(L))
|
2009-03-02 15:52:23 +08:00
|
|
|
return UnknownVal();
|
|
|
|
|
2009-04-03 15:33:13 +08:00
|
|
|
const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
|
2009-05-09 23:18:12 +08:00
|
|
|
const ElementRegion *ER = 0;
|
2009-05-20 17:00:16 +08:00
|
|
|
|
2009-07-11 08:58:27 +08:00
|
|
|
switch (MR->getKind()) {
|
|
|
|
case MemRegion::SymbolicRegionKind: {
|
|
|
|
const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
|
|
|
|
QualType T;
|
|
|
|
// If the SymbolicRegion was cast to another type, use that type.
|
|
|
|
if (const QualType *t = state->get<RegionCasts>(SR))
|
|
|
|
T = *t;
|
|
|
|
else {
|
|
|
|
// Otherwise use the symbol's type.
|
|
|
|
SymbolRef Sym = SR->getSymbol();
|
|
|
|
T = Sym->getType(getContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType EleTy = T->getAsPointerType()->getPointeeType();
|
|
|
|
SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
|
|
|
|
ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
|
|
|
|
break;
|
2009-06-19 12:51:14 +08:00
|
|
|
}
|
2009-07-11 08:58:27 +08:00
|
|
|
case MemRegion::AllocaRegionKind: {
|
|
|
|
// Get the alloca region's current cast type.
|
|
|
|
const AllocaRegion *AR = cast<AllocaRegion>(MR);
|
|
|
|
QualType T = *(state->get<RegionCasts>(AR));
|
|
|
|
QualType EleTy = T->getAsPointerType()->getPointeeType();
|
|
|
|
SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
|
|
|
|
ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MemRegion::ElementRegionKind: {
|
|
|
|
ER = cast<ElementRegion>(MR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not yet handled.
|
|
|
|
case MemRegion::VarRegionKind:
|
|
|
|
case MemRegion::StringRegionKind:
|
|
|
|
case MemRegion::CompoundLiteralRegionKind:
|
|
|
|
case MemRegion::FieldRegionKind:
|
|
|
|
case MemRegion::ObjCObjectRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// TypedViewRegion will soon be removed.
|
|
|
|
case MemRegion::TypedViewRegionKind:
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
case MemRegion::CodeTextRegionKind:
|
|
|
|
// Technically this can happen if people do funny things with casts.
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
case MemRegion::MemSpaceRegionKind:
|
|
|
|
assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
case MemRegion::BEG_DECL_REGIONS:
|
|
|
|
case MemRegion::END_DECL_REGIONS:
|
|
|
|
case MemRegion::BEG_TYPED_REGIONS:
|
|
|
|
case MemRegion::END_TYPED_REGIONS:
|
|
|
|
assert(0 && "Infeasible region");
|
|
|
|
return UnknownVal();
|
2009-06-21 21:24:24 +08:00
|
|
|
}
|
2009-03-02 15:52:23 +08:00
|
|
|
|
|
|
|
SVal Idx = ER->getIndex();
|
|
|
|
nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
|
|
|
|
nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
|
|
|
|
|
|
|
|
// Only support concrete integer indexes for now.
|
|
|
|
if (Base && Offset) {
|
2009-03-13 23:35:24 +08:00
|
|
|
// FIXME: For now, convert the signedness and bitwidth of offset in case
|
|
|
|
// they don't match. This can result from pointer arithmetic. In reality,
|
|
|
|
// we should figure out what are the proper semantics and implement them.
|
|
|
|
//
|
2009-03-13 23:39:16 +08:00
|
|
|
// This addresses the test case test/Analysis/ptr-arith.c
|
|
|
|
//
|
2009-03-13 23:35:24 +08:00
|
|
|
nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
|
|
|
|
Offset->getValue()));
|
2009-06-26 08:05:51 +08:00
|
|
|
SVal NewIdx = Base->evalBinOp(ValMgr, Op, OffConverted);
|
2009-05-04 14:18:28 +08:00
|
|
|
const MemRegion* NewER =
|
2009-06-16 17:55:50 +08:00
|
|
|
MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion(),
|
|
|
|
getContext());
|
2009-06-23 17:02:15 +08:00
|
|
|
return ValMgr.makeLoc(NewER);
|
2009-03-02 15:52:23 +08:00
|
|
|
|
2009-03-03 10:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return UnknownVal();
|
2009-03-02 15:52:23 +08:00
|
|
|
}
|
|
|
|
|
2009-06-17 06:36:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Loading values from regions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-15 10:31:43 +08:00
|
|
|
static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
|
|
|
|
RTy = Ctx.getCanonicalType(RTy);
|
|
|
|
UsedTy = Ctx.getCanonicalType(UsedTy);
|
|
|
|
|
|
|
|
if (RTy == UsedTy)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !(Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy));
|
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
|
|
|
|
|
2008-10-21 13:29:26 +08:00
|
|
|
assert(!isa<UnknownVal>(L) && "location unknown");
|
|
|
|
assert(!isa<UndefinedVal>(L) && "location undefined");
|
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
// FIXME: Is this even possible? Shouldn't this be treated as a null
|
|
|
|
// dereference at a higher level?
|
2008-12-20 14:32:12 +08:00
|
|
|
if (isa<loc::ConcreteInt>(L))
|
|
|
|
return UndefinedVal();
|
2008-10-31 15:16:08 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
|
2009-04-03 15:33:13 +08:00
|
|
|
|
2009-05-20 17:18:48 +08:00
|
|
|
// FIXME: return symbolic value for these cases.
|
2009-04-03 15:33:13 +08:00
|
|
|
// Example:
|
|
|
|
// void f(int* p) { int x = *p; }
|
2009-05-20 17:18:48 +08:00
|
|
|
// char* p = alloca();
|
|
|
|
// read(p);
|
|
|
|
// c = *p;
|
2009-07-15 04:48:22 +08:00
|
|
|
if (isa<AllocaRegion>(MR))
|
2009-04-03 15:33:13 +08:00
|
|
|
return UnknownVal();
|
2009-07-15 04:48:22 +08:00
|
|
|
|
|
|
|
if (isa<SymbolicRegion>(MR)) {
|
|
|
|
ASTContext &Ctx = getContext();
|
|
|
|
SVal idx = ValMgr.makeIntVal(0, Ctx.IntTy);
|
2009-07-15 10:31:43 +08:00
|
|
|
assert(!T.isNull());
|
2009-07-15 04:48:22 +08:00
|
|
|
MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
|
|
|
|
}
|
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
// FIXME: Perhaps this method should just take a 'const MemRegion*' argument
|
|
|
|
// instead of 'Loc', and have the other Loc cases handled at a higher level.
|
2009-06-18 06:02:04 +08:00
|
|
|
const TypedRegion *R = cast<TypedRegion>(MR);
|
2009-07-15 10:31:43 +08:00
|
|
|
QualType RTy = R->getValueType(getContext());
|
2008-10-21 13:29:26 +08:00
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
// FIXME: We should eventually handle funny addressing. e.g.:
|
|
|
|
//
|
|
|
|
// int x = ...;
|
|
|
|
// int *p = &x;
|
|
|
|
// char *q = (char*) p;
|
|
|
|
// char c = *q; // returns the first byte of 'x'.
|
|
|
|
//
|
|
|
|
// Such funny addressing will occur due to layering of regions.
|
|
|
|
|
2009-07-15 10:31:43 +08:00
|
|
|
ASTContext &Ctx = getContext();
|
|
|
|
if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
|
|
|
|
SVal idx = ValMgr.makeIntVal(0, Ctx.IntTy);
|
|
|
|
R = MRMgr.getElementRegion(T, idx, R, Ctx);
|
|
|
|
RTy = T;
|
|
|
|
assert(RTy == R->getValueType(Ctx));
|
|
|
|
}
|
2009-05-03 08:27:40 +08:00
|
|
|
|
2009-03-09 17:15:51 +08:00
|
|
|
if (RTy->isStructureType())
|
2009-06-18 06:02:04 +08:00
|
|
|
return RetrieveStruct(state, R);
|
2009-05-03 08:27:40 +08:00
|
|
|
|
|
|
|
if (RTy->isArrayType())
|
2009-06-18 06:02:04 +08:00
|
|
|
return RetrieveArray(state, R);
|
2009-05-03 08:27:40 +08:00
|
|
|
|
2009-03-09 17:15:51 +08:00
|
|
|
// FIXME: handle Vector types.
|
|
|
|
if (RTy->isVectorType())
|
2009-02-24 10:23:11 +08:00
|
|
|
return UnknownVal();
|
2009-06-28 22:16:39 +08:00
|
|
|
|
|
|
|
if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
|
|
|
|
return RetrieveField(state, FR);
|
|
|
|
|
|
|
|
if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
|
|
|
|
return RetrieveElement(state, ER);
|
2008-12-20 14:32:12 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
RegionBindingsTy B = GetRegionBindings(state->getStore());
|
2008-12-20 14:32:12 +08:00
|
|
|
RegionBindingsTy::data_type* V = B.lookup(R);
|
2008-10-21 13:29:26 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
// Check if the region has a binding.
|
|
|
|
if (V)
|
|
|
|
return *V;
|
2008-12-24 15:46:32 +08:00
|
|
|
|
2009-03-05 12:50:08 +08:00
|
|
|
if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
|
|
|
|
const MemRegion *SR = IVR->getSuperRegion();
|
|
|
|
|
|
|
|
// If the super region is 'self' then return the symbol representing
|
|
|
|
// the value of the ivar upon entry to the method.
|
|
|
|
if (SR == SelfRegion) {
|
|
|
|
// FIXME: Do we need to handle the case where the super region
|
|
|
|
// has a view? We want to canonicalize the bindings.
|
2009-05-09 12:08:27 +08:00
|
|
|
return ValMgr.getRegionValueSymbolVal(R);
|
2009-03-05 12:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we need a new symbol. For now return Unknown.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
2009-01-23 19:22:12 +08:00
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
// The location does not have a bound value. This means that it has
|
|
|
|
// the value it had upon its creation and/or entry to the analyzed
|
|
|
|
// function/method. These are either symbolic values or 'undefined'.
|
|
|
|
|
|
|
|
// We treat function parameters as symbolic values.
|
2009-01-23 07:43:57 +08:00
|
|
|
if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
|
|
|
|
const VarDecl *VD = VR->getDecl();
|
|
|
|
|
2009-03-04 08:23:05 +08:00
|
|
|
if (VD == SelfDecl)
|
|
|
|
return loc::MemRegionVal(getSelfRegion(0));
|
|
|
|
|
2009-07-03 06:16:42 +08:00
|
|
|
if (VR->hasGlobalsOrParametersStorage())
|
|
|
|
return ValMgr.getRegionValueSymbolValOrUnknown(VR, VD->getType());
|
2009-03-05 12:50:08 +08:00
|
|
|
}
|
2009-02-24 10:23:11 +08:00
|
|
|
|
2009-06-24 02:17:08 +08:00
|
|
|
if (R->hasHeapOrStackStorage()) {
|
2008-12-24 15:46:32 +08:00
|
|
|
// All stack variables are considered to have undefined values
|
|
|
|
// upon creation. All heap allocated blocks are considered to
|
|
|
|
// have undefined values as well unless they are explicitly bound
|
|
|
|
// to specific values.
|
2008-12-20 14:32:12 +08:00
|
|
|
return UndefinedVal();
|
2008-12-24 15:46:32 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 14:29:10 +08:00
|
|
|
// If the region is already cast to another type, use that type to create the
|
|
|
|
// symbol value.
|
|
|
|
if (const QualType *p = state->get<RegionCasts>(R)) {
|
|
|
|
QualType T = *p;
|
|
|
|
RTy = T->getAsPointerType()->getPointeeType();
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:16:42 +08:00
|
|
|
// All other values are symbolic.
|
|
|
|
return ValMgr.getRegionValueSymbolValOrUnknown(R, RTy);
|
2008-10-21 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2009-06-25 13:29:39 +08:00
|
|
|
SVal RegionStoreManager::RetrieveElement(const GRState* state,
|
|
|
|
const ElementRegion* R) {
|
|
|
|
// Check if the region has a binding.
|
|
|
|
RegionBindingsTy B = GetRegionBindings(state->getStore());
|
2009-07-02 07:19:52 +08:00
|
|
|
if (const SVal* V = B.lookup(R))
|
2009-06-25 13:29:39 +08:00
|
|
|
return *V;
|
|
|
|
|
2009-07-02 07:19:52 +08:00
|
|
|
const MemRegion* superR = R->getSuperRegion();
|
|
|
|
|
2009-06-25 13:29:39 +08:00
|
|
|
// Check if the region is an element region of a string literal.
|
2009-07-02 07:19:52 +08:00
|
|
|
if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
|
2009-06-25 13:29:39 +08:00
|
|
|
const StringLiteral *Str = StrR->getStringLiteral();
|
|
|
|
SVal Idx = R->getIndex();
|
|
|
|
if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
|
|
|
|
int64_t i = CI->getValue().getSExtValue();
|
|
|
|
char c;
|
|
|
|
if (i == Str->getByteLength())
|
|
|
|
c = '\0';
|
|
|
|
else
|
|
|
|
c = Str->getStrData()[i];
|
|
|
|
return ValMgr.makeIntVal(c, getContext().CharTy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-30 20:32:59 +08:00
|
|
|
// Check if the super region has a default value.
|
2009-07-02 07:19:52 +08:00
|
|
|
if (const SVal *D = state->get<RegionDefaultValue>(superR)) {
|
2009-06-25 13:29:39 +08:00
|
|
|
if (D->hasConjuredSymbol())
|
|
|
|
return ValMgr.getRegionValueSymbolVal(R);
|
|
|
|
else
|
|
|
|
return *D;
|
|
|
|
}
|
|
|
|
|
2009-06-30 20:32:59 +08:00
|
|
|
// Check if the super region has a binding.
|
2009-07-15 10:31:43 +08:00
|
|
|
if (const SVal *V = B.lookup(superR)) {
|
|
|
|
if (SymbolRef parentSym = V->getAsSymbol())
|
|
|
|
return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
|
|
|
|
|
|
|
|
// Other cases: give up.
|
2009-07-03 14:11:41 +08:00
|
|
|
return UnknownVal();
|
2009-06-30 20:32:59 +08:00
|
|
|
}
|
2009-07-02 07:19:52 +08:00
|
|
|
|
|
|
|
if (R->hasHeapStorage()) {
|
|
|
|
// FIXME: If the region has heap storage and we know nothing special
|
|
|
|
// about its bindings, should we instead return UnknownVal? Seems like
|
|
|
|
// we should only return UndefinedVal in the cases where we know the value
|
|
|
|
// will be undefined.
|
|
|
|
return UndefinedVal();
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:02:15 +08:00
|
|
|
if (R->hasStackStorage() && !R->hasParametersStorage()) {
|
2009-07-02 07:19:52 +08:00
|
|
|
// Currently we don't reason specially about Clang-style vectors. Check
|
|
|
|
// if superR is a vector and if so return Unknown.
|
|
|
|
if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
|
|
|
|
if (typedSuperR->getValueType(getContext())->isVectorType())
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
2009-06-30 20:32:59 +08:00
|
|
|
|
2009-06-25 13:29:39 +08:00
|
|
|
return UndefinedVal();
|
2009-07-02 07:19:52 +08:00
|
|
|
}
|
2009-06-25 13:29:39 +08:00
|
|
|
|
|
|
|
QualType Ty = R->getValueType(getContext());
|
|
|
|
|
|
|
|
// If the region is already cast to another type, use that type to create the
|
|
|
|
// symbol value.
|
|
|
|
if (const QualType *p = state->get<RegionCasts>(R))
|
|
|
|
Ty = (*p)->getAsPointerType()->getPointeeType();
|
|
|
|
|
2009-07-03 06:16:42 +08:00
|
|
|
return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
|
2009-06-25 13:29:39 +08:00
|
|
|
}
|
|
|
|
|
2009-06-25 12:50:44 +08:00
|
|
|
SVal RegionStoreManager::RetrieveField(const GRState* state,
|
|
|
|
const FieldRegion* R) {
|
|
|
|
QualType Ty = R->getValueType(getContext());
|
|
|
|
|
|
|
|
// Check if the region has a binding.
|
|
|
|
RegionBindingsTy B = GetRegionBindings(state->getStore());
|
2009-07-02 07:30:34 +08:00
|
|
|
if (const SVal* V = B.lookup(R))
|
2009-06-25 12:50:44 +08:00
|
|
|
return *V;
|
|
|
|
|
2009-07-02 07:30:34 +08:00
|
|
|
const MemRegion* superR = R->getSuperRegion();
|
|
|
|
if (const SVal* D = state->get<RegionDefaultValue>(superR)) {
|
2009-06-25 12:50:44 +08:00
|
|
|
if (D->hasConjuredSymbol())
|
|
|
|
return ValMgr.getRegionValueSymbolVal(R);
|
|
|
|
|
|
|
|
if (D->isZeroConstant())
|
|
|
|
return ValMgr.makeZeroVal(Ty);
|
|
|
|
|
|
|
|
if (D->isUnknown())
|
|
|
|
return *D;
|
|
|
|
|
|
|
|
assert(0 && "Unknown default value");
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:02:15 +08:00
|
|
|
// FIXME: Is this correct? Should it be UnknownVal?
|
|
|
|
if (R->hasHeapStorage())
|
|
|
|
return UndefinedVal();
|
|
|
|
|
|
|
|
if (R->hasStackStorage() && !R->hasParametersStorage())
|
2009-06-25 12:50:44 +08:00
|
|
|
return UndefinedVal();
|
|
|
|
|
|
|
|
// If the region is already cast to another type, use that type to create the
|
|
|
|
// symbol value.
|
|
|
|
if (const QualType *p = state->get<RegionCasts>(R)) {
|
|
|
|
QualType tmp = *p;
|
|
|
|
Ty = tmp->getAsPointerType()->getPointeeType();
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:16:42 +08:00
|
|
|
// All other values are symbolic.
|
|
|
|
return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
|
2009-06-25 12:50:44 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 14:29:10 +08:00
|
|
|
SVal RegionStoreManager::RetrieveStruct(const GRState *state,
|
|
|
|
const TypedRegion* R){
|
2009-05-09 11:57:34 +08:00
|
|
|
QualType T = R->getValueType(getContext());
|
2008-10-31 15:16:08 +08:00
|
|
|
assert(T->isStructureType());
|
|
|
|
|
2009-06-11 15:27:30 +08:00
|
|
|
const RecordType* RT = T->getAsStructureType();
|
2008-10-31 15:16:08 +08:00
|
|
|
RecordDecl* RD = RT->getDecl();
|
|
|
|
assert(RD->isDefinition());
|
|
|
|
|
|
|
|
llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
// FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
|
|
|
|
// reverse iterator, we should implement one.
|
2009-06-30 10:36:12 +08:00
|
|
|
std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
|
2008-12-12 00:49:14 +08:00
|
|
|
|
2008-12-12 04:41:00 +08:00
|
|
|
for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
|
|
|
|
FieldEnd = Fields.rend();
|
|
|
|
Field != FieldEnd; ++Field) {
|
|
|
|
FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
|
2009-05-03 08:27:40 +08:00
|
|
|
QualType FTy = (*Field)->getType();
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy);
|
2008-10-31 15:16:08 +08:00
|
|
|
StructVal = getBasicVals().consVals(FieldValue, StructVal);
|
|
|
|
}
|
|
|
|
|
2009-06-23 17:02:15 +08:00
|
|
|
return ValMgr.makeCompoundVal(T, StructVal);
|
2008-10-31 15:16:08 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal RegionStoreManager::RetrieveArray(const GRState *state,
|
|
|
|
const TypedRegion * R) {
|
|
|
|
|
2009-05-09 11:57:34 +08:00
|
|
|
QualType T = R->getValueType(getContext());
|
2009-05-03 08:27:40 +08:00
|
|
|
ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
|
|
|
|
|
|
|
|
llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
|
|
|
|
llvm::APSInt Size(CAT->getSize(), false);
|
2009-05-11 20:48:56 +08:00
|
|
|
llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
|
2009-05-03 08:27:40 +08:00
|
|
|
|
|
|
|
for (; i < Size; ++i) {
|
2009-06-23 17:02:15 +08:00
|
|
|
SVal Idx = ValMgr.makeIntVal(i);
|
2009-06-16 17:55:50 +08:00
|
|
|
ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
|
|
|
|
getContext());
|
2009-05-04 14:18:28 +08:00
|
|
|
QualType ETy = ER->getElementType();
|
2009-06-18 06:02:04 +08:00
|
|
|
SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy);
|
2009-05-03 08:27:40 +08:00
|
|
|
ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
|
|
|
|
}
|
|
|
|
|
2009-06-23 17:02:15 +08:00
|
|
|
return ValMgr.makeCompoundVal(T, ArrayVal);
|
2009-05-03 08:27:40 +08:00
|
|
|
}
|
|
|
|
|
2009-06-17 06:36:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Binding values to regions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Store RegionStoreManager::Remove(Store store, Loc L) {
|
|
|
|
const MemRegion* R = 0;
|
|
|
|
|
|
|
|
if (isa<loc::MemRegionVal>(L))
|
|
|
|
R = cast<loc::MemRegionVal>(L).getRegion();
|
|
|
|
|
|
|
|
if (R) {
|
|
|
|
RegionBindingsTy B = GetRegionBindings(store);
|
|
|
|
return RBFactory.Remove(B, R).getRoot();
|
|
|
|
}
|
|
|
|
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
|
2009-06-28 18:16:11 +08:00
|
|
|
if (isa<loc::ConcreteInt>(L))
|
|
|
|
return state;
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
// If we get here, the location should be a region.
|
|
|
|
const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
|
2009-06-17 06:36:44 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
// Check if the region is a struct region.
|
2008-10-31 16:10:01 +08:00
|
|
|
if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
|
2009-05-09 11:57:34 +08:00
|
|
|
if (TR->getValueType(getContext())->isStructureType())
|
2009-06-18 06:02:04 +08:00
|
|
|
return BindStruct(state, TR, V);
|
2009-06-17 06:36:44 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
RegionBindingsTy B = GetRegionBindings(state->getStore());
|
2009-06-17 06:36:44 +08:00
|
|
|
|
2009-06-25 13:52:16 +08:00
|
|
|
B = RBFactory.Add(B, R, V);
|
2009-01-21 14:57:53 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
return state->makeWithStore(B.getRoot());
|
2008-12-16 10:36:30 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *RegionStoreManager::BindDecl(const GRState *state,
|
2008-12-20 14:32:12 +08:00
|
|
|
const VarDecl* VD, SVal InitVal) {
|
2008-10-21 13:29:26 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
QualType T = VD->getType();
|
|
|
|
VarRegion* VR = MRMgr.getVarRegion(VD);
|
2008-10-21 13:29:26 +08:00
|
|
|
|
2009-01-21 14:57:53 +08:00
|
|
|
if (T->isArrayType())
|
2009-06-18 06:02:04 +08:00
|
|
|
return BindArray(state, VR, InitVal);
|
2009-01-21 14:57:53 +08:00
|
|
|
if (T->isStructureType())
|
2009-06-18 06:02:04 +08:00
|
|
|
return BindStruct(state, VR, InitVal);
|
2008-10-31 18:24:47 +08:00
|
|
|
|
2009-06-23 17:02:15 +08:00
|
|
|
return Bind(state, ValMgr.makeLoc(VR), InitVal);
|
2008-10-21 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
// FIXME: this method should be merged into Bind().
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *
|
|
|
|
RegionStoreManager::BindCompoundLiteral(const GRState *state,
|
|
|
|
const CompoundLiteralExpr* CL,
|
|
|
|
SVal V) {
|
|
|
|
|
2008-11-07 18:38:33 +08:00
|
|
|
CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
|
2009-06-18 06:02:04 +08:00
|
|
|
return Bind(state, loc::MemRegionVal(R), V);
|
2008-11-07 18:38:33 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *RegionStoreManager::BindArray(const GRState *state,
|
|
|
|
const TypedRegion* R,
|
|
|
|
SVal Init) {
|
|
|
|
|
2009-05-09 11:57:34 +08:00
|
|
|
QualType T = R->getValueType(getContext());
|
2008-10-31 18:24:47 +08:00
|
|
|
ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
|
2009-06-23 13:23:38 +08:00
|
|
|
QualType ElementTy = CAT->getElementType();
|
2008-10-31 18:24:47 +08:00
|
|
|
|
2008-11-30 13:49:49 +08:00
|
|
|
llvm::APSInt Size(CAT->getSize(), false);
|
2009-06-23 13:23:38 +08:00
|
|
|
llvm::APSInt i(llvm::APInt::getNullValue(Size.getBitWidth()), false);
|
2008-11-30 13:49:49 +08:00
|
|
|
|
|
|
|
// Check if the init expr is a StringLiteral.
|
|
|
|
if (isa<loc::MemRegionVal>(Init)) {
|
|
|
|
const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
|
|
|
|
const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
|
|
|
|
const char* str = S->getStrData();
|
|
|
|
unsigned len = S->getByteLength();
|
|
|
|
unsigned j = 0;
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
// Copy bytes from the string literal into the target array. Trailing bytes
|
|
|
|
// in the array that are not covered by the string literal are initialized
|
|
|
|
// to zero.
|
2008-11-30 13:49:49 +08:00
|
|
|
for (; i < Size; ++i, ++j) {
|
2008-12-20 14:32:12 +08:00
|
|
|
if (j >= len)
|
|
|
|
break;
|
|
|
|
|
2009-06-23 14:13:19 +08:00
|
|
|
SVal Idx = ValMgr.makeIntVal(i);
|
2009-06-23 13:23:38 +08:00
|
|
|
ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx,R,getContext());
|
2008-11-30 13:49:49 +08:00
|
|
|
|
2009-06-23 17:02:15 +08:00
|
|
|
SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
|
2009-06-18 06:02:04 +08:00
|
|
|
state = Bind(state, loc::MemRegionVal(ER), V);
|
2008-11-30 13:49:49 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
return state;
|
2008-11-30 13:49:49 +08:00
|
|
|
}
|
2008-10-31 18:24:47 +08:00
|
|
|
|
|
|
|
nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
|
|
|
|
nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
for (; i < Size; ++i, ++VI) {
|
2009-06-23 13:23:38 +08:00
|
|
|
// The init list might be shorter than the array length.
|
2008-12-20 14:32:12 +08:00
|
|
|
if (VI == VE)
|
|
|
|
break;
|
2008-10-24 16:42:28 +08:00
|
|
|
|
2009-06-23 14:13:19 +08:00
|
|
|
SVal Idx = ValMgr.makeIntVal(i);
|
2009-06-23 13:23:38 +08:00
|
|
|
ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
|
2008-11-19 19:06:24 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
if (CAT->getElementType()->isStructureType())
|
2009-06-18 06:02:04 +08:00
|
|
|
state = BindStruct(state, ER, *VI);
|
2008-12-20 14:32:12 +08:00
|
|
|
else
|
2009-06-23 17:02:15 +08:00
|
|
|
state = Bind(state, ValMgr.makeLoc(ER), *VI);
|
2008-11-19 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
2009-06-24 08:56:31 +08:00
|
|
|
// If the init list is shorter than the array length, set the array default
|
|
|
|
// value.
|
|
|
|
if (i < Size) {
|
|
|
|
if (ElementTy->isIntegerType()) {
|
2009-06-23 13:23:38 +08:00
|
|
|
SVal V = ValMgr.makeZeroVal(ElementTy);
|
2009-06-24 08:56:31 +08:00
|
|
|
state = setDefaultValue(state, R, V);
|
2009-06-23 13:23:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
return state;
|
2008-11-19 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *
|
|
|
|
RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
|
|
|
|
SVal V) {
|
|
|
|
|
|
|
|
if (!Features.supportsFields())
|
|
|
|
return state;
|
|
|
|
|
2009-05-09 11:57:34 +08:00
|
|
|
QualType T = R->getValueType(getContext());
|
2008-10-31 18:53:01 +08:00
|
|
|
assert(T->isStructureType());
|
|
|
|
|
2009-03-12 11:45:35 +08:00
|
|
|
const RecordType* RT = T->getAsRecordType();
|
2008-10-31 18:53:01 +08:00
|
|
|
RecordDecl* RD = RT->getDecl();
|
2009-03-11 17:07:35 +08:00
|
|
|
|
|
|
|
if (!RD->isDefinition())
|
2009-06-18 06:02:04 +08:00
|
|
|
return state;
|
2008-10-31 18:53:01 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
// We may get non-CompoundVal accidentally due to imprecise cast logic.
|
|
|
|
// Ignore them and kill the field values.
|
|
|
|
if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
|
|
|
|
return KillStruct(state, R);
|
2009-06-11 17:11:27 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
|
2008-10-31 18:53:01 +08:00
|
|
|
nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
|
2009-06-23 13:43:16 +08:00
|
|
|
|
|
|
|
RecordDecl::field_iterator FI, FE;
|
|
|
|
|
2009-06-30 10:36:12 +08:00
|
|
|
for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
|
2008-10-31 18:53:01 +08:00
|
|
|
|
2009-06-23 13:43:16 +08:00
|
|
|
if (VI == VE)
|
2008-12-20 14:32:12 +08:00
|
|
|
break;
|
2008-10-31 19:02:48 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
QualType FTy = (*FI)->getType();
|
|
|
|
FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
|
2008-10-31 19:02:48 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
if (Loc::IsLocType(FTy) || FTy->isIntegerType())
|
2009-06-23 17:02:15 +08:00
|
|
|
state = Bind(state, ValMgr.makeLoc(FR), *VI);
|
2008-12-20 14:32:12 +08:00
|
|
|
else if (FTy->isArrayType())
|
2009-06-18 06:02:04 +08:00
|
|
|
state = BindArray(state, FR, *VI);
|
2008-12-20 14:32:12 +08:00
|
|
|
else if (FTy->isStructureType())
|
2009-06-18 06:02:04 +08:00
|
|
|
state = BindStruct(state, FR, *VI);
|
2008-11-19 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
2009-06-23 13:43:16 +08:00
|
|
|
// There may be fewer values in the initialize list than the fields of struct.
|
2009-06-25 12:50:44 +08:00
|
|
|
if (FI != FE)
|
|
|
|
state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
|
2009-06-23 13:43:16 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
return state;
|
2008-11-19 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *RegionStoreManager::KillStruct(const GRState *state,
|
2009-01-13 09:49:57 +08:00
|
|
|
const TypedRegion* R){
|
|
|
|
|
2009-06-25 13:52:16 +08:00
|
|
|
// Set the default value of the struct region to "unknown".
|
|
|
|
state = state->set<RegionDefaultValue>(R, UnknownVal());
|
2009-01-13 09:49:57 +08:00
|
|
|
|
|
|
|
// Remove all bindings for the subregions of the struct.
|
2009-06-25 13:52:16 +08:00
|
|
|
Store store = state->getStore();
|
|
|
|
RegionBindingsTy B = GetRegionBindings(store);
|
2009-01-13 09:49:57 +08:00
|
|
|
for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
|
2009-06-18 06:02:04 +08:00
|
|
|
const MemRegion* R = I.getKey();
|
|
|
|
if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
|
|
|
|
if (subRegion->isSubRegionOf(R))
|
2009-06-23 17:02:15 +08:00
|
|
|
store = Remove(store, ValMgr.makeLoc(subRegion));
|
2009-01-13 09:49:57 +08:00
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
return state->makeWithStore(store);
|
2009-01-13 09:49:57 +08:00
|
|
|
}
|
|
|
|
|
2009-06-17 06:36:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region views.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *RegionStoreManager::AddRegionView(const GRState *state,
|
|
|
|
const MemRegion* View,
|
|
|
|
const MemRegion* Base) {
|
2008-11-16 12:07:26 +08:00
|
|
|
|
|
|
|
// First, retrieve the region view of the base region.
|
2009-06-18 06:02:04 +08:00
|
|
|
const RegionViews* d = state->get<RegionViewMap>(Base);
|
2009-01-13 09:49:57 +08:00
|
|
|
RegionViews L = d ? *d : RVFactory.GetEmptySet();
|
2008-11-16 12:07:26 +08:00
|
|
|
|
|
|
|
// Now add View to the region view.
|
2009-01-13 09:49:57 +08:00
|
|
|
L = RVFactory.Add(L, View);
|
2008-11-16 12:07:26 +08:00
|
|
|
|
|
|
|
// Create a new state with the new region view.
|
2009-06-18 06:02:04 +08:00
|
|
|
return state->set<RegionViewMap>(Base, L);
|
2008-11-16 12:07:26 +08:00
|
|
|
}
|
2009-01-13 09:49:57 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *RegionStoreManager::RemoveRegionView(const GRState *state,
|
|
|
|
const MemRegion* View,
|
|
|
|
const MemRegion* Base) {
|
2009-01-13 09:49:57 +08:00
|
|
|
// Retrieve the region view of the base region.
|
2009-06-18 06:02:04 +08:00
|
|
|
const RegionViews* d = state->get<RegionViewMap>(Base);
|
2009-01-13 09:49:57 +08:00
|
|
|
|
|
|
|
// If the base region has no view, return.
|
|
|
|
if (!d)
|
2009-06-18 06:02:04 +08:00
|
|
|
return state;
|
2009-01-13 09:49:57 +08:00
|
|
|
|
|
|
|
// Remove the view.
|
2009-06-18 06:02:04 +08:00
|
|
|
return state->set<RegionViewMap>(Base, RVFactory.Remove(*d, View));
|
2009-01-13 09:49:57 +08:00
|
|
|
}
|
2009-05-06 16:33:50 +08:00
|
|
|
|
2009-06-18 14:29:10 +08:00
|
|
|
const GRState *RegionStoreManager::setCastType(const GRState *state,
|
|
|
|
const MemRegion* R, QualType T) {
|
2009-07-14 09:12:46 +08:00
|
|
|
// We do not record generic cast type, since we are using cast type to
|
|
|
|
// invlidate regions, and generic type is meaningless for invalidating
|
|
|
|
// regions.
|
|
|
|
// If the region already has a cast type before, that type is preserved.
|
|
|
|
// FIXME: is this the right thing to do?
|
|
|
|
if (isGenericPtr(getContext(), T))
|
|
|
|
return state;
|
2009-06-18 06:02:04 +08:00
|
|
|
return state->set<RegionCasts>(R, T);
|
2009-05-06 16:33:50 +08:00
|
|
|
}
|
2009-05-12 18:10:00 +08:00
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
|
|
|
|
const MemRegion* R, SVal V) {
|
|
|
|
return state->set<RegionDefaultValue>(R, V);
|
2009-05-12 18:10:00 +08:00
|
|
|
}
|
2009-06-17 06:36:44 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// State pruning.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
|
|
|
|
if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
|
|
|
|
const MemRegion *R = XR->getRegion();
|
|
|
|
|
|
|
|
while (R) {
|
|
|
|
if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
|
|
|
|
SymReaper.markLive(SR->getSymbol());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
|
|
|
|
R = SR->getSuperRegion();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
|
|
|
|
SymReaper.markLive(*SI);
|
|
|
|
}
|
|
|
|
|
2009-06-18 06:02:04 +08:00
|
|
|
Store RegionStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
|
2009-06-17 06:36:44 +08:00
|
|
|
SymbolReaper& SymReaper,
|
|
|
|
llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
|
2009-06-18 06:02:04 +08:00
|
|
|
{
|
2009-06-17 06:36:44 +08:00
|
|
|
Store store = state->getStore();
|
|
|
|
RegionBindingsTy B = GetRegionBindings(store);
|
|
|
|
|
|
|
|
// Lazily constructed backmap from MemRegions to SubRegions.
|
|
|
|
typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
|
|
|
|
typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
|
|
|
|
|
|
|
|
// FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
|
|
|
|
// the ability to reuse memory. This way we can keep TmpAlloc around as
|
|
|
|
// an instance variable of RegionStoreManager (avoiding repeated malloc
|
|
|
|
// overhead).
|
|
|
|
llvm::BumpPtrAllocator TmpAlloc;
|
|
|
|
|
|
|
|
// Factory objects.
|
|
|
|
SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
|
|
|
|
SubRegionsTy::Factory SubRegF(TmpAlloc);
|
|
|
|
|
|
|
|
// The backmap from regions to subregions.
|
|
|
|
SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
|
|
|
|
|
|
|
|
// Do a pass over the regions in the store. For VarRegions we check if
|
|
|
|
// the variable is still live and if so add it to the list of live roots.
|
2009-06-18 06:02:04 +08:00
|
|
|
// For other regions we populate our region backmap.
|
2009-06-17 06:36:44 +08:00
|
|
|
llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
|
|
|
|
|
|
|
|
for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
|
|
|
|
IntermediateRoots.push_back(I.getKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!IntermediateRoots.empty()) {
|
|
|
|
const MemRegion* R = IntermediateRoots.back();
|
|
|
|
IntermediateRoots.pop_back();
|
|
|
|
|
|
|
|
if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
|
2009-06-30 20:32:59 +08:00
|
|
|
if (SymReaper.isLive(Loc, VR->getDecl())) {
|
2009-06-17 06:36:44 +08:00
|
|
|
RegionRoots.push_back(VR); // This is a live "root".
|
2009-06-30 20:32:59 +08:00
|
|
|
}
|
2009-06-17 06:36:44 +08:00
|
|
|
}
|
|
|
|
else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
|
|
|
|
if (SymReaper.isLive(SR->getSymbol()))
|
|
|
|
RegionRoots.push_back(SR);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Get the super region for R.
|
2009-07-02 07:30:34 +08:00
|
|
|
const MemRegion* superR = cast<SubRegion>(R)->getSuperRegion();
|
2009-06-17 06:36:44 +08:00
|
|
|
|
|
|
|
// Get the current set of subregions for SuperR.
|
2009-07-02 07:30:34 +08:00
|
|
|
const SubRegionsTy* SRptr = SubRegMap.lookup(superR);
|
2009-06-17 06:36:44 +08:00
|
|
|
SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet();
|
|
|
|
|
|
|
|
// Add R to the subregions of SuperR.
|
2009-07-02 07:30:34 +08:00
|
|
|
SubRegMap = SubRegMapF.Add(SubRegMap, superR, SubRegF.Add(SRs, R));
|
2009-06-17 06:36:44 +08:00
|
|
|
|
|
|
|
// Super region may be VarRegion or subregion of another VarRegion. Add it
|
|
|
|
// to the work list.
|
2009-07-02 07:30:34 +08:00
|
|
|
if (isa<SubRegion>(superR))
|
|
|
|
IntermediateRoots.push_back(superR);
|
2009-06-17 06:36:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the worklist of RegionRoots. This performs a "mark-and-sweep"
|
|
|
|
// of the store. We want to find all live symbols and dead regions.
|
|
|
|
llvm::SmallPtrSet<const MemRegion*, 10> Marked;
|
|
|
|
|
|
|
|
while (!RegionRoots.empty()) {
|
|
|
|
// Dequeue the next region on the worklist.
|
|
|
|
const MemRegion* R = RegionRoots.back();
|
|
|
|
RegionRoots.pop_back();
|
|
|
|
|
|
|
|
// Check if we have already processed this region.
|
|
|
|
if (Marked.count(R)) continue;
|
|
|
|
|
|
|
|
// Mark this region as processed. This is needed for termination in case
|
|
|
|
// a region is referenced more than once.
|
|
|
|
Marked.insert(R);
|
|
|
|
|
|
|
|
// Mark the symbol for any live SymbolicRegion as "live". This means we
|
|
|
|
// should continue to track that symbol.
|
|
|
|
if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
|
|
|
|
SymReaper.markLive(SymR->getSymbol());
|
|
|
|
|
|
|
|
// Get the data binding for R (if any).
|
|
|
|
RegionBindingsTy::data_type* Xptr = B.lookup(R);
|
|
|
|
if (Xptr) {
|
|
|
|
SVal X = *Xptr;
|
|
|
|
UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
|
|
|
|
|
2009-06-30 20:32:59 +08:00
|
|
|
// If X is a region, then add it to the RegionRoots.
|
|
|
|
if (const MemRegion *RX = X.getAsRegion()) {
|
|
|
|
RegionRoots.push_back(RX);
|
|
|
|
|
|
|
|
// Mark the super region of the RX as live.
|
|
|
|
// e.g.: int x; char *y = (char*) &x; if (*y) ...
|
|
|
|
// 'y' => element region. 'x' is its super region.
|
|
|
|
// We only add one level super region for now.
|
2009-07-01 10:12:57 +08:00
|
|
|
// FIXME: maybe multiple level of super regions should be added.
|
2009-06-30 20:32:59 +08:00
|
|
|
if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
|
|
|
|
RegionRoots.push_back(SR->getSuperRegion());
|
|
|
|
}
|
|
|
|
}
|
2009-06-17 06:36:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the subregions of R. These are RegionRoots as well since they
|
|
|
|
// represent values that are also bound to R.
|
|
|
|
const SubRegionsTy* SRptr = SubRegMap.lookup(R);
|
|
|
|
if (!SRptr) continue;
|
|
|
|
SubRegionsTy SR = *SRptr;
|
|
|
|
|
|
|
|
for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
|
|
|
|
RegionRoots.push_back(*I);
|
2009-06-30 20:32:59 +08:00
|
|
|
|
2009-06-17 06:36:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We have now scanned the store, marking reachable regions and symbols
|
|
|
|
// as live. We now remove all the regions that are dead from the store
|
|
|
|
// as well as update DSymbols with the set symbols that are now dead.
|
|
|
|
for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
|
|
|
|
const MemRegion* R = I.getKey();
|
|
|
|
// If this region live? Is so, none of its symbols are dead.
|
|
|
|
if (Marked.count(R))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Remove this dead region from the store.
|
2009-06-23 17:02:15 +08:00
|
|
|
store = Remove(store, ValMgr.makeLoc(R));
|
2009-06-17 06:36:44 +08:00
|
|
|
|
|
|
|
// Mark all non-live symbols that this region references as dead.
|
|
|
|
if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
|
|
|
|
SymReaper.maybeDead(SymR->getSymbol());
|
|
|
|
|
|
|
|
SVal X = I.getData();
|
|
|
|
SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
|
|
|
|
for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-25 07:06:47 +08:00
|
|
|
void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
|
2009-06-17 06:36:44 +08:00
|
|
|
const char* nl, const char *sep) {
|
|
|
|
RegionBindingsTy B = GetRegionBindings(store);
|
|
|
|
OS << "Store:" << nl;
|
|
|
|
|
2009-07-14 07:53:06 +08:00
|
|
|
for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
|
|
|
|
OS << ' ' << I.getKey() << " : " << I.getData() << nl;
|
2009-06-17 06:36:44 +08:00
|
|
|
}
|