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"
|
|
|
|
|
|
|
|
#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
|
|
|
|
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
|
|
|
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region "killsets".
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-01-13 09:49:57 +08:00
|
|
|
// RegionStore lazily adds value bindings to regions when the analyzer handles
|
|
|
|
// assignment statements. Killsets track which default values have been
|
|
|
|
// killed, thus distinguishing between "unknown" values and default
|
|
|
|
// values. Regions are added to killset only when they are assigned "unknown"
|
|
|
|
// directly, otherwise we should have their value in the region bindings.
|
2008-12-24 09:05:03 +08:00
|
|
|
//
|
|
|
|
namespace { class VISIBILITY_HIDDEN RegionKills {}; }
|
2008-12-05 08:47:52 +08:00
|
|
|
static int RegionKillsIndex = 0;
|
2008-12-04 10:08:27 +08:00
|
|
|
namespace clang {
|
2008-12-05 08:47:52 +08:00
|
|
|
template<> struct GRStateTrait<RegionKills>
|
2008-12-24 09:05:03 +08:00
|
|
|
: public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
|
2008-12-05 08:47:52 +08:00
|
|
|
static void* GDMIndex() { return &RegionKillsIndex; }
|
2008-12-04 10:08:27 +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 {
|
|
|
|
RegionBindingsTy::Factory RBFactory;
|
2008-12-24 09:05:03 +08:00
|
|
|
RegionViews::Factory RVFactory;
|
2008-11-16 12:07:26 +08:00
|
|
|
|
2008-10-08 10:50:44 +08:00
|
|
|
GRStateManager& StateMgr;
|
2009-01-23 07:43:57 +08:00
|
|
|
const MemRegion* SelfRegion;
|
|
|
|
const ImplicitParamDecl *SelfDecl;
|
2008-10-08 10:50:44 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
RegionStoreManager(GRStateManager& mgr)
|
2009-01-08 06:18:50 +08:00
|
|
|
: StoreManager(mgr.getAllocator()),
|
|
|
|
RBFactory(mgr.getAllocator()),
|
2008-11-16 12:07:26 +08:00
|
|
|
RVFactory(mgr.getAllocator()),
|
2009-01-23 07:43:57 +08:00
|
|
|
StateMgr(mgr), SelfRegion(0), SelfDecl(0) {
|
|
|
|
if (const ObjCMethodDecl* MD =
|
|
|
|
dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
|
|
|
|
SelfDecl = MD->getSelfDecl();
|
|
|
|
}
|
2008-10-08 10:50:44 +08:00
|
|
|
|
|
|
|
virtual ~RegionStoreManager() {}
|
|
|
|
|
2008-10-24 09:38:55 +08:00
|
|
|
MemRegionManager& getRegionManager() { return MRMgr; }
|
2008-10-28 05:54:31 +08:00
|
|
|
|
2009-03-04 03:02:42 +08:00
|
|
|
SubRegionMap* getSubRegionMap(const GRState *state);
|
2009-03-03 09:35:36 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
const GRState* BindCompoundLiteral(const GRState* St,
|
|
|
|
const CompoundLiteralExpr* CL, SVal V);
|
2008-10-24 09:38:55 +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.
|
2008-10-25 22:18:57 +08:00
|
|
|
SVal getLValueString(const GRState* St, const StringLiteral* S);
|
|
|
|
|
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.
|
2008-11-07 18:38:33 +08:00
|
|
|
SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
|
|
|
|
|
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.
|
2008-10-22 21:44:38 +08:00
|
|
|
SVal getLValueVar(const GRState* St, const VarDecl* VD);
|
|
|
|
|
|
|
|
SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
|
|
|
|
|
|
|
|
SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
|
2009-03-05 12:50:08 +08:00
|
|
|
|
|
|
|
SVal getLValueFieldOrIvar(const GRState* St, SVal Base, const Decl* D);
|
2008-10-22 21:44:38 +08:00
|
|
|
|
2008-10-24 09:09:32 +08:00
|
|
|
SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
|
|
|
|
|
2008-11-22 21:21:46 +08:00
|
|
|
SVal getSizeInElements(const GRState* St, const MemRegion* R);
|
|
|
|
|
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.
|
2008-10-24 09:09:32 +08:00
|
|
|
SVal ArrayToPointer(SVal Array);
|
|
|
|
|
2008-12-14 05:49:13 +08:00
|
|
|
/// CastRegion - Used by GRExprEngine::VisitCast to handle casts from
|
|
|
|
/// a MemRegion* to a specific location type. 'R' is the region being
|
|
|
|
/// casted and 'CastToTy' the result type of the cast.
|
|
|
|
CastResult CastRegion(const GRState* state, const MemRegion* R,
|
|
|
|
QualType CastToTy);
|
2008-11-16 12:07:26 +08:00
|
|
|
|
2009-03-02 15:52:23 +08:00
|
|
|
SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R);
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
/// 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
|
2008-12-05 08:47:52 +08:00
|
|
|
SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
|
2008-10-22 21:44:38 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
const GRState* Bind(const GRState* St, Loc LV, SVal V);
|
2008-10-08 10:50:44 +08:00
|
|
|
|
2008-12-16 10:36:30 +08:00
|
|
|
Store Remove(Store store, Loc LV);
|
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
|
|
|
}
|
2008-12-04 10:08:27 +08:00
|
|
|
|
2008-12-05 08:47:52 +08:00
|
|
|
/// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
|
|
|
|
/// It returns a new Store with these values removed, and populates LSymbols
|
|
|
|
// and DSymbols with the known set of live and dead symbols respectively.
|
|
|
|
Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
|
2009-01-22 06:26:05 +08:00
|
|
|
SymbolReaper& SymReaper,
|
|
|
|
llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
|
2008-10-24 09:38:55 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
|
|
|
|
|
|
|
|
const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
|
|
|
|
return St;
|
|
|
|
}
|
2008-10-21 13:29:26 +08:00
|
|
|
|
2008-11-24 17:44:56 +08:00
|
|
|
const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
|
|
|
|
|
2008-10-08 10:50:44 +08:00
|
|
|
static inline RegionBindingsTy GetRegionBindings(Store store) {
|
2008-12-16 10:36:30 +08:00
|
|
|
return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
|
2008-10-08 10:50:44 +08:00
|
|
|
}
|
2008-10-24 09:38:55 +08:00
|
|
|
|
2008-10-24 12:33:15 +08:00
|
|
|
void print(Store store, std::ostream& Out, const char* nl, const char *sep);
|
2008-10-24 09:38:55 +08:00
|
|
|
|
|
|
|
void iterBindings(Store store, BindingsHandler& f) {
|
|
|
|
// FIXME: Implement.
|
|
|
|
}
|
2008-10-24 16:42:28 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Loc getVarLoc(const VarDecl* VD) {
|
|
|
|
return loc::MemRegionVal(MRMgr.getVarRegion(VD));
|
|
|
|
}
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
|
2008-10-31 15:16:08 +08:00
|
|
|
|
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.
|
2008-12-20 14:32:12 +08:00
|
|
|
SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
|
2008-12-04 09:12:41 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
|
2008-11-23 12:30:35 +08:00
|
|
|
|
2009-01-13 09:49:57 +08:00
|
|
|
/// KillStruct - Set the entire struct to unknown.
|
|
|
|
const GRState* KillStruct(const GRState* St, const TypedRegion* R);
|
|
|
|
|
2008-10-31 15:16:08 +08:00
|
|
|
// Utility methods.
|
|
|
|
BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
|
|
|
|
ASTContext& getContext() { return StateMgr.getContext(); }
|
2008-11-23 12:30:35 +08:00
|
|
|
SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
|
2008-11-16 12:07:26 +08:00
|
|
|
|
|
|
|
const GRState* AddRegionView(const GRState* St,
|
|
|
|
const MemRegion* View, const MemRegion* Base);
|
2009-01-13 09:49:57 +08:00
|
|
|
const GRState* RemoveRegionView(const GRState* St,
|
|
|
|
const MemRegion* View, const MemRegion* Base);
|
2008-10-08 10:50:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-10-24 09:04:59 +08:00
|
|
|
StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
|
2008-10-24 09:38:55 +08:00
|
|
|
return new RegionStoreManager(StMgr);
|
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
|
|
|
|
|
|
|
/// 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.
|
2008-10-25 22:18:57 +08:00
|
|
|
SVal RegionStoreManager::getLValueString(const GRState* St,
|
|
|
|
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.
|
2008-10-22 21:44:38 +08:00
|
|
|
SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
|
|
|
|
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
|
|
|
|
RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
|
|
|
|
const CompoundLiteralExpr* CL) {
|
2008-11-07 18:38:33 +08:00
|
|
|
return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
|
|
|
|
}
|
|
|
|
|
2008-10-22 21:44:38 +08:00
|
|
|
SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
|
|
|
|
SVal Base) {
|
2009-03-05 12:50:08 +08:00
|
|
|
return getLValueFieldOrIvar(St, Base, D);
|
2008-10-22 21:44:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
|
|
|
|
const FieldDecl* D) {
|
2009-03-05 12:50:08 +08:00
|
|
|
return getLValueFieldOrIvar(St, Base, D);
|
|
|
|
}
|
|
|
|
|
|
|
|
SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base,
|
|
|
|
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::SymbolValKind:
|
2009-02-05 14:57:29 +08:00
|
|
|
BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol(),
|
|
|
|
StateMgr.getSymbolManager());
|
2008-10-22 21:44:38 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case loc::GotoLabelKind:
|
|
|
|
case loc::FuncValKind:
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2008-10-24 09:09:32 +08:00
|
|
|
SVal RegionStoreManager::getLValueElement(const GRState* St,
|
|
|
|
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-02-06 16:44:27 +08:00
|
|
|
const TypedRegion* BaseRegion = 0;
|
|
|
|
|
|
|
|
if (isa<loc::SymbolVal>(Base))
|
|
|
|
BaseRegion = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(Base).getSymbol(),
|
|
|
|
StateMgr.getSymbolManager());
|
|
|
|
else
|
|
|
|
BaseRegion = cast<TypedRegion>(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-03-01 13:44:08 +08:00
|
|
|
// Observe that 'p' binds to an TypedViewRegion<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);
|
|
|
|
Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
|
|
|
|
}
|
|
|
|
}
|
2009-01-23 04:27:48 +08:00
|
|
|
return loc::MemRegionVal(MRMgr.getElementRegion(Offset, BaseRegion));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-23 04:27:48 +08:00
|
|
|
const TypedRegion *ArrayR = ElemR->getArrayRegion();
|
|
|
|
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-02-19 16:37:16 +08:00
|
|
|
NewIdx = NonLoc::MakeVal(getBasicVals(), 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-01-23 04:27:48 +08:00
|
|
|
return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, ArrayR));
|
2008-10-24 09:09:32 +08:00
|
|
|
}
|
|
|
|
|
2008-11-22 21:21:46 +08:00
|
|
|
SVal RegionStoreManager::getSizeInElements(const GRState* St,
|
|
|
|
const MemRegion* R) {
|
|
|
|
if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
|
|
|
|
// Get the type of the variable.
|
2009-01-30 08:08:43 +08:00
|
|
|
QualType T = VR->getDesugaredRValueType(getContext());
|
2008-11-22 21:21:46 +08:00
|
|
|
|
2009-01-30 08:08:43 +08:00
|
|
|
// 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 NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clients can use ordinary variables as if they were arrays. These
|
|
|
|
// essentially are arrays of size 1.
|
|
|
|
return NonLoc::MakeIntVal(getBasicVals(), 1, false);
|
2008-11-22 21:21:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
|
2008-11-24 10:18:56 +08:00
|
|
|
const StringLiteral* Str = SR->getStringLiteral();
|
2008-11-24 10:30:48 +08:00
|
|
|
// We intentionally made the size value signed because it participates in
|
|
|
|
// operations with signed indices.
|
2009-01-30 08:08:43 +08:00
|
|
|
return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
|
2008-11-22 21:21:46 +08:00
|
|
|
}
|
|
|
|
|
2009-03-01 13:44:08 +08:00
|
|
|
if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) {
|
2009-01-23 07:56:56 +08:00
|
|
|
#if 0
|
|
|
|
// FIXME: This logic doesn't really work, as we can have all sorts of
|
|
|
|
// weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
|
|
|
|
// The weird cases come in when arbitrary casting comes into play, violating
|
|
|
|
// any type-safe programming.
|
|
|
|
|
2008-11-24 17:44:56 +08:00
|
|
|
GRStateRef state(St, StateMgr);
|
|
|
|
|
|
|
|
// Get the size of the super region in bytes.
|
2008-12-24 09:05:03 +08:00
|
|
|
const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
|
|
|
|
assert(Extent && "region extent not exist");
|
2008-11-24 17:44:56 +08:00
|
|
|
|
|
|
|
// Assume it's ConcreteInt for now.
|
2008-12-24 09:05:03 +08:00
|
|
|
llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
|
2008-11-24 17:44:56 +08:00
|
|
|
|
|
|
|
// Get the size of the element in bits.
|
2008-12-14 05:49:13 +08:00
|
|
|
QualType LvT = ATR->getLValueType(getContext());
|
|
|
|
QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
|
2008-11-24 17:44:56 +08:00
|
|
|
|
|
|
|
uint64_t X = getContext().getTypeSize(ElemTy);
|
|
|
|
|
|
|
|
const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
|
|
|
|
false);
|
|
|
|
|
|
|
|
// Calculate the number of elements.
|
|
|
|
|
|
|
|
// FIXME: What do we do with signed-ness problem? Shall we make all APSInts
|
|
|
|
// signed?
|
|
|
|
if (SSize.isUnsigned())
|
|
|
|
SSize.setIsSigned(true);
|
|
|
|
|
|
|
|
// FIXME: move this operation into BasicVals.
|
|
|
|
const llvm::APSInt S =
|
|
|
|
(SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
|
|
|
|
|
|
|
|
return NonLoc::MakeVal(getBasicVals(), S);
|
2009-01-23 07:56:56 +08:00
|
|
|
#else
|
|
|
|
ATR = ATR;
|
|
|
|
return UnknownVal();
|
|
|
|
#endif
|
2008-11-22 21:21:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
|
|
|
|
// FIXME: Unsupported yet.
|
|
|
|
FR = 0;
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
2008-11-22 21:23:00 +08:00
|
|
|
|
2009-02-06 16:51:30 +08:00
|
|
|
if (isa<SymbolicRegion>(R)) {
|
2009-02-06 16:44:27 +08:00
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
2008-11-22 21:21:46 +08:00
|
|
|
assert(0 && "Other regions are not supported yet.");
|
2009-01-07 03:12:06 +08:00
|
|
|
return UnknownVal();
|
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.
|
2008-10-24 09:09:32 +08:00
|
|
|
SVal RegionStoreManager::ArrayToPointer(SVal Array) {
|
2008-12-24 15:46:32 +08:00
|
|
|
// FIXME: This should be factored into GRExprEngine. This allows
|
|
|
|
// us to pass a "loc" instead of an "SVal" for "Array".
|
2008-12-14 03:24:37 +08:00
|
|
|
if (Array.isUnknownOrUndef())
|
|
|
|
return Array;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2008-11-23 12:30:35 +08:00
|
|
|
nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
|
2008-10-26 10:23:57 +08:00
|
|
|
ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
|
|
|
|
|
|
|
|
return loc::MemRegionVal(ER);
|
2008-10-24 09:09:32 +08:00
|
|
|
}
|
|
|
|
|
2008-12-14 05:49:13 +08:00
|
|
|
StoreManager::CastResult
|
|
|
|
RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
|
|
|
|
QualType CastToTy) {
|
|
|
|
|
|
|
|
// Return the same region if the region types are compatible.
|
|
|
|
if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
|
|
|
|
ASTContext& Ctx = StateMgr.getContext();
|
|
|
|
QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
|
|
|
|
QualType Tb = Ctx.getCanonicalType(CastToTy);
|
|
|
|
|
|
|
|
if (Ta == Tb)
|
|
|
|
return CastResult(state, R);
|
2008-11-16 12:07:26 +08:00
|
|
|
}
|
2008-12-24 15:46:32 +08:00
|
|
|
|
|
|
|
// FIXME: We should handle the case when we are casting *back* to a
|
|
|
|
// previous type. For example:
|
|
|
|
//
|
|
|
|
// void* x = ...;
|
|
|
|
// char* y = (char*) x;
|
|
|
|
// void* z = (void*) y; // <-- we should get the same region that is
|
|
|
|
// bound to 'x'
|
2009-03-01 13:44:08 +08:00
|
|
|
const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
|
2008-12-14 05:49:13 +08:00
|
|
|
return CastResult(AddRegionView(state, ViewR, R), ViewR);
|
2008-11-16 12:07:26 +08:00
|
|
|
}
|
|
|
|
|
2009-03-02 15:52:23 +08:00
|
|
|
SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
|
|
|
|
// Assume the base location is MemRegionVal(ElementRegion).
|
2009-03-03 10:51:43 +08:00
|
|
|
if (!isa<loc::MemRegionVal>(L))
|
2009-03-02 15:52:23 +08:00
|
|
|
return UnknownVal();
|
|
|
|
|
2009-03-11 15:43:49 +08:00
|
|
|
const TypedRegion* TR
|
|
|
|
= cast<TypedRegion>(cast<loc::MemRegionVal>(L).getRegion());
|
|
|
|
|
|
|
|
const ElementRegion* ER = dyn_cast<ElementRegion>(TR);
|
|
|
|
|
|
|
|
if (!ER) {
|
|
|
|
// If the region is not element region, create one with index 0. This can
|
|
|
|
// happen in the following example:
|
|
|
|
// char *p = foo();
|
|
|
|
// p += 3;
|
|
|
|
// Note that p binds to a TypedViewRegion(SymbolicRegion).
|
|
|
|
nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
|
|
|
|
ER = MRMgr.getElementRegion(Idx, TR);
|
|
|
|
}
|
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()));
|
|
|
|
SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
|
2009-03-02 15:52:23 +08:00
|
|
|
const MemRegion* NewER = MRMgr.getElementRegion(NewIdx,
|
|
|
|
ER->getArrayRegion());
|
|
|
|
return Loc::MakeVal(NewER);
|
|
|
|
|
2009-03-03 10:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return UnknownVal();
|
2009-03-02 15:52:23 +08:00
|
|
|
}
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
SVal RegionStoreManager::Retrieve(const GRState* St, 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: What does loc::SymbolVal represent? It represents the value
|
|
|
|
// of a location but that value is not known. In the future we should
|
|
|
|
// handle potential aliasing relationships; e.g. a loc::SymbolVal could
|
|
|
|
// be an alias for a particular region.
|
2009-02-20 13:19:30 +08:00
|
|
|
// Example:
|
|
|
|
// void foo(char* buf) {
|
|
|
|
// char c = *buf;
|
|
|
|
// }
|
|
|
|
if (isa<loc::SymbolVal>(L)) {
|
2008-12-20 14:32:12 +08:00
|
|
|
return UnknownVal();
|
2009-02-20 13:19:30 +08:00
|
|
|
}
|
2008-10-21 13:29:26 +08:00
|
|
|
|
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
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
// FIXME: Should this be refactored into GRExprEngine or GRStateManager?
|
|
|
|
// It seems that all StoreManagers would do the same thing here.
|
2008-12-20 14:32:12 +08:00
|
|
|
if (isa<loc::FuncVal>(L))
|
|
|
|
return L;
|
2008-10-21 13:29:26 +08:00
|
|
|
|
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-03-09 17:15:51 +08:00
|
|
|
const TypedRegion* R
|
|
|
|
= cast<TypedRegion>(cast<loc::MemRegionVal>(L).getRegion());
|
2008-12-20 14:32:12 +08:00
|
|
|
assert(R && "bad region");
|
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-03-09 17:15:51 +08:00
|
|
|
QualType RTy = R->getRValueType(getContext());
|
|
|
|
if (RTy->isStructureType())
|
|
|
|
return RetrieveStruct(St, R);
|
|
|
|
// FIXME: handle Vector types.
|
|
|
|
if (RTy->isVectorType())
|
2009-02-24 10:23:11 +08:00
|
|
|
return UnknownVal();
|
2008-12-20 14:32:12 +08:00
|
|
|
|
|
|
|
RegionBindingsTy B = GetRegionBindings(St->getStore());
|
|
|
|
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
|
|
|
|
|
|
|
GRStateRef state(St, StateMgr);
|
2008-12-20 14:32:12 +08:00
|
|
|
|
|
|
|
// Check if the region is in killset.
|
|
|
|
if (state.contains<RegionKills>(R))
|
|
|
|
return UnknownVal();
|
2008-10-21 13:29:26 +08:00
|
|
|
|
2009-03-05 12:50:08 +08:00
|
|
|
// If the region is an element or field, it may have a default value.
|
2009-01-23 19:22:12 +08:00
|
|
|
if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
|
|
|
|
const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
|
|
|
|
GRStateTrait<RegionDefaultValue>::lookup_type D =
|
|
|
|
state.get<RegionDefaultValue>(SuperR);
|
|
|
|
if (D)
|
|
|
|
return *D;
|
|
|
|
}
|
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.
|
|
|
|
return SVal::GetRValueSymbolVal(getSymbolManager(), R);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
|
|
if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
|
|
|
|
VD->hasGlobalStorage()) {
|
2009-02-19 17:56:08 +08:00
|
|
|
QualType VTy = VD->getType();
|
|
|
|
if (Loc::IsLocType(VTy) || VTy->isIntegerType())
|
|
|
|
return SVal::GetRValueSymbolVal(getSymbolManager(), VR);
|
|
|
|
else
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
2009-03-05 12:50:08 +08:00
|
|
|
}
|
2009-02-24 10:23:11 +08:00
|
|
|
|
2008-12-24 15:46:32 +08:00
|
|
|
if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
|
|
|
|
// 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-03-09 17:31:22 +08:00
|
|
|
// All other integer values are symbolic.
|
|
|
|
if (Loc::IsLocType(RTy) || RTy->isIntegerType())
|
|
|
|
return SVal::GetRValueSymbolVal(getSymbolManager(), R);
|
|
|
|
else
|
|
|
|
return UnknownVal();
|
2008-10-21 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
|
|
|
|
|
|
|
|
Store store = St->getStore();
|
|
|
|
GRStateRef state(St, StateMgr);
|
|
|
|
|
2008-12-14 05:49:13 +08:00
|
|
|
// FIXME: Verify we want getRValueType instead of getLValueType.
|
|
|
|
QualType T = R->getRValueType(getContext());
|
2008-10-31 15:16:08 +08:00
|
|
|
assert(T->isStructureType());
|
|
|
|
|
|
|
|
const RecordType* RT = cast<RecordType>(T.getTypePtr());
|
|
|
|
RecordDecl* RD = RT->getDecl();
|
|
|
|
assert(RD->isDefinition());
|
|
|
|
|
|
|
|
llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
|
|
|
|
|
2008-12-12 04:41:00 +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);
|
2008-12-20 14:32:12 +08:00
|
|
|
RegionBindingsTy B = GetRegionBindings(store);
|
2008-10-31 16:10:01 +08:00
|
|
|
RegionBindingsTy::data_type* data = B.lookup(FR);
|
2008-10-31 15:16:08 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
SVal FieldValue;
|
|
|
|
if (data)
|
|
|
|
FieldValue = *data;
|
|
|
|
else if (state.contains<RegionKills>(FR))
|
|
|
|
FieldValue = UnknownVal();
|
|
|
|
else {
|
|
|
|
if (MRMgr.onStack(FR) || MRMgr.onHeap(FR))
|
|
|
|
FieldValue = UndefinedVal();
|
|
|
|
else
|
Static analyzer: Remove a bunch of outdated SymbolData objects and
their associated APIs. We no longer need separate SymbolData objects
for fields, variables, etc. Instead, we now associated symbols with
the "rvalue" of a MemRegion (i.e., the value stored at that region).
Now we only have two kinds of SymbolData objects: SymbolRegionRValue
and SymbolConjured.
This cleanup also makes the distinction between a SymbolicRegion and a
symbolic value that is a location much clearer. A SymbolicRegion
represents a chunk of symbolic memory, while a symbolic location is
just a "pointer" with different possible values. Without any specific
knowledge, a symbolic location resolves (i.e., via a dereference) to a
SymbolicRegion. In the future, when we do better alias reasoning, a
symbolic location can become an alias for another location, thus
merging the constraints on the referred SymbolicRegion with the other
region.
llvm-svn: 62769
2009-01-23 02:23:34 +08:00
|
|
|
FieldValue = SVal::GetRValueSymbolVal(getSymbolManager(), FR);
|
2008-12-20 14:32:12 +08:00
|
|
|
}
|
2008-10-31 15:16:08 +08:00
|
|
|
|
|
|
|
StructVal = getBasicVals().consVals(FieldValue, StructVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
|
|
|
|
}
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
|
|
|
|
// Currently we don't bind value to symbolic location. But if the logic is
|
|
|
|
// made clear, we might change this decision.
|
|
|
|
if (isa<loc::SymbolVal>(L))
|
|
|
|
return St;
|
2008-10-08 10:50:44 +08:00
|
|
|
|
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();
|
2008-10-31 16:10:01 +08:00
|
|
|
assert(R);
|
|
|
|
|
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))
|
2008-12-14 05:49:13 +08:00
|
|
|
// FIXME: Verify we want getRValueType().
|
|
|
|
if (TR->getRValueType(getContext())->isStructureType())
|
2008-12-20 14:32:12 +08:00
|
|
|
return BindStruct(St, TR, V);
|
2008-10-08 10:50:44 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
Store store = St->getStore();
|
2008-10-08 10:50:44 +08:00
|
|
|
RegionBindingsTy B = GetRegionBindings(store);
|
2008-12-20 14:32:12 +08:00
|
|
|
|
|
|
|
if (V.isUnknown()) {
|
|
|
|
// Remove the binding.
|
|
|
|
store = RBFactory.Remove(B, R).getRoot();
|
|
|
|
|
|
|
|
// Add the region to the killset.
|
|
|
|
GRStateRef state(St, StateMgr);
|
|
|
|
St = state.add<RegionKills>(R);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
store = RBFactory.Add(B, R, V).getRoot();
|
|
|
|
|
|
|
|
return StateMgr.MakeStateWithStore(St, store);
|
2008-10-08 10:50:44 +08:00
|
|
|
}
|
|
|
|
|
2008-12-16 10:36:30 +08:00
|
|
|
Store RegionStoreManager::Remove(Store store, Loc L) {
|
2009-01-21 14:57:53 +08:00
|
|
|
const MemRegion* R = 0;
|
|
|
|
|
|
|
|
if (isa<loc::MemRegionVal>(L))
|
|
|
|
R = cast<loc::MemRegionVal>(L).getRegion();
|
|
|
|
else if (isa<loc::SymbolVal>(L))
|
2009-02-05 14:57:29 +08:00
|
|
|
R = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(L).getSymbol(),
|
|
|
|
StateMgr.getSymbolManager());
|
2009-01-21 14:57:53 +08:00
|
|
|
|
|
|
|
if (R) {
|
|
|
|
RegionBindingsTy B = GetRegionBindings(store);
|
|
|
|
return RBFactory.Remove(B, R).getRoot();
|
|
|
|
}
|
|
|
|
|
|
|
|
return store;
|
2008-12-16 10:36:30 +08:00
|
|
|
}
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
const GRState* RegionStoreManager::BindDecl(const GRState* St,
|
|
|
|
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())
|
2008-12-20 14:32:12 +08:00
|
|
|
return BindArray(St, VR, InitVal);
|
2009-01-21 14:57:53 +08:00
|
|
|
if (T->isStructureType())
|
2008-12-20 14:32:12 +08:00
|
|
|
return BindStruct(St, VR, InitVal);
|
2008-10-31 18:24:47 +08:00
|
|
|
|
2009-01-21 14:57:53 +08:00
|
|
|
return Bind(St, Loc::MakeVal(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().
|
|
|
|
const GRState*
|
|
|
|
RegionStoreManager::BindCompoundLiteral(const GRState* St,
|
|
|
|
const CompoundLiteralExpr* CL, SVal V) {
|
2008-11-07 18:38:33 +08:00
|
|
|
CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
|
2008-12-20 14:32:12 +08:00
|
|
|
return Bind(St, loc::MemRegionVal(R), V);
|
2008-11-07 18:38:33 +08:00
|
|
|
}
|
|
|
|
|
2008-11-24 17:44:56 +08:00
|
|
|
const GRState* RegionStoreManager::setExtent(const GRState* St,
|
|
|
|
const MemRegion* R, SVal Extent) {
|
|
|
|
GRStateRef state(St, StateMgr);
|
2008-12-24 09:05:03 +08:00
|
|
|
return state.set<RegionExtents>(R, Extent);
|
2008-11-24 17:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-22 06:26:05 +08:00
|
|
|
static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
|
2009-03-04 08:11:38 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-01-22 06:26:05 +08:00
|
|
|
for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
|
|
|
|
SymReaper.markLive(*SI);
|
2008-12-04 10:08:27 +08:00
|
|
|
}
|
|
|
|
|
2008-12-05 08:47:52 +08:00
|
|
|
Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
|
2009-01-22 06:26:05 +08:00
|
|
|
SymbolReaper& SymReaper,
|
|
|
|
llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
|
|
|
|
{
|
2008-11-10 17:39:04 +08:00
|
|
|
|
2008-12-05 08:47:52 +08:00
|
|
|
Store store = state->getStore();
|
2008-11-10 17:39:04 +08:00
|
|
|
RegionBindingsTy B = GetRegionBindings(store);
|
2008-12-04 10:08:27 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
// For other regions we populate our region backmap.
|
2009-03-18 09:54:31 +08:00
|
|
|
|
|
|
|
llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
|
|
|
|
|
2008-11-10 17:39:04 +08:00
|
|
|
for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
|
2009-03-18 09:54:31 +08:00
|
|
|
IntermediateRoots.push_back(I.getKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!IntermediateRoots.empty()) {
|
|
|
|
const MemRegion* R = IntermediateRoots.back();
|
|
|
|
IntermediateRoots.pop_back();
|
|
|
|
|
2008-12-04 10:08:27 +08:00
|
|
|
if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
|
2009-01-22 06:26:05 +08:00
|
|
|
if (SymReaper.isLive(Loc, VR->getDecl()))
|
2008-12-04 10:08:27 +08:00
|
|
|
RegionRoots.push_back(VR); // This is a live "root".
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Get the super region for R.
|
|
|
|
const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
|
2009-03-18 09:54:31 +08:00
|
|
|
|
2008-12-04 10:08:27 +08:00
|
|
|
// Get the current set of subregions for SuperR.
|
|
|
|
const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
|
|
|
|
SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
|
2009-03-18 09:54:31 +08:00
|
|
|
|
2008-12-04 10:08:27 +08:00
|
|
|
// Add R to the subregions of SuperR.
|
|
|
|
SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
|
2009-03-18 09:54:31 +08:00
|
|
|
|
|
|
|
// Super region may be VarRegion or subregion of another VarRegion. Add it
|
|
|
|
// to the work list.
|
|
|
|
if (isa<SubRegion>(SuperR))
|
|
|
|
IntermediateRoots.push_back(SuperR);
|
2009-03-05 12:50:08 +08:00
|
|
|
}
|
2008-12-04 10:08:27 +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))
|
2009-01-22 06:26:05 +08:00
|
|
|
SymReaper.markLive(SymR->getSymbol());
|
2008-12-04 10:08:27 +08:00
|
|
|
|
|
|
|
// Get the data binding for R (if any).
|
|
|
|
RegionBindingsTy::data_type* Xptr = B.lookup(R);
|
|
|
|
if (Xptr) {
|
|
|
|
SVal X = *Xptr;
|
2009-01-22 06:26:05 +08:00
|
|
|
UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
|
2008-12-04 10:08:27 +08:00
|
|
|
|
|
|
|
// If X is a region, then add it the RegionRoots.
|
|
|
|
if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
|
|
|
|
RegionRoots.push_back(RegionX->getRegion());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2008-11-10 17:39:04 +08:00
|
|
|
}
|
2008-12-04 10:08:27 +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
|
2009-03-05 12:50:08 +08:00
|
|
|
// as well as update DSymbols with the set symbols that are now dead.
|
2008-12-04 10:08:27 +08:00
|
|
|
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.
|
2008-12-16 10:36:30 +08:00
|
|
|
store = Remove(store, Loc::MakeVal(R));
|
2008-12-04 10:08:27 +08:00
|
|
|
|
|
|
|
// Mark all non-live symbols that this region references as dead.
|
2009-01-22 06:26:05 +08:00
|
|
|
if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
|
|
|
|
SymReaper.maybeDead(SymR->getSymbol());
|
2008-11-10 17:39:04 +08:00
|
|
|
|
2008-12-04 10:08:27 +08:00
|
|
|
SVal X = I.getData();
|
|
|
|
SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
|
2009-01-22 06:26:05 +08:00
|
|
|
for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
|
2008-12-04 10:08:27 +08:00
|
|
|
}
|
|
|
|
|
2008-11-10 17:39:04 +08:00
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
2008-10-24 14:01:33 +08:00
|
|
|
void RegionStoreManager::print(Store store, std::ostream& Out,
|
|
|
|
const char* nl, const char *sep) {
|
|
|
|
llvm::raw_os_ostream OS(Out);
|
|
|
|
RegionBindingsTy B = GetRegionBindings(store);
|
|
|
|
OS << "Store:" << nl;
|
|
|
|
|
|
|
|
for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
|
|
|
|
OS << ' '; I.getKey()->print(OS); OS << " : ";
|
|
|
|
I.getData().print(OS); OS << nl;
|
|
|
|
}
|
2008-10-24 12:33:15 +08:00
|
|
|
}
|
2008-10-24 16:42:28 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
const GRState* RegionStoreManager::BindArray(const GRState* St,
|
|
|
|
const TypedRegion* R, SVal Init) {
|
2008-12-14 05:49:13 +08:00
|
|
|
|
|
|
|
// FIXME: Verify we should use getLValueType or getRValueType.
|
2008-12-14 11:14:52 +08:00
|
|
|
QualType T = R->getRValueType(getContext());
|
2008-10-31 18:24:47 +08:00
|
|
|
assert(T->isArrayType());
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
// When we are binding the whole array, it always has default value 0.
|
|
|
|
GRStateRef state(St, StateMgr);
|
2009-01-30 08:08:43 +08:00
|
|
|
St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
|
|
|
|
false));
|
2008-12-20 14:32:12 +08:00
|
|
|
|
2008-10-31 18:24:47 +08:00
|
|
|
ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
|
|
|
|
|
2008-11-30 13:49:49 +08:00
|
|
|
llvm::APSInt Size(CAT->getSize(), false);
|
2009-01-27 03:54:12 +08:00
|
|
|
llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
|
|
|
|
Size.isUnsigned());
|
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;
|
|
|
|
|
2008-11-30 13:49:49 +08:00
|
|
|
SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
|
|
|
|
ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
|
|
|
|
St = Bind(St, loc::MemRegionVal(ER), V);
|
2008-11-30 13:49:49 +08:00
|
|
|
}
|
|
|
|
|
2009-03-09 14:49:50 +08:00
|
|
|
return St;
|
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) {
|
2008-10-31 18:24:47 +08:00
|
|
|
// The init list might be shorter than the array decl.
|
2008-12-20 14:32:12 +08:00
|
|
|
if (VI == VE)
|
|
|
|
break;
|
2008-10-24 16:42:28 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
|
|
|
|
ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
|
2008-11-19 19:06:24 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
if (CAT->getElementType()->isStructureType())
|
|
|
|
St = BindStruct(St, ER, *VI);
|
|
|
|
else
|
|
|
|
St = Bind(St, Loc::MakeVal(ER), *VI);
|
2008-11-19 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
2009-03-09 14:49:50 +08:00
|
|
|
return St;
|
2008-11-19 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
const GRState*
|
|
|
|
RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
|
2008-12-14 05:49:13 +08:00
|
|
|
// FIXME: Verify that we should use getRValueType or getLValueType.
|
|
|
|
QualType T = R->getRValueType(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())
|
|
|
|
return St;
|
2008-10-31 18:53:01 +08:00
|
|
|
|
2009-01-13 09:49:57 +08:00
|
|
|
if (V.isUnknown())
|
|
|
|
return KillStruct(St, R);
|
|
|
|
|
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();
|
|
|
|
RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
for (; FI != FE; ++FI, ++VI) {
|
2008-10-31 18:53:01 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
// There may be fewer values than fields only when we are initializing a
|
|
|
|
// struct decl. In this case, mark the region as having default value.
|
|
|
|
if (VI == VE) {
|
2008-12-24 15:29:24 +08:00
|
|
|
GRStateRef state(St, StateMgr);
|
2009-01-30 08:08:43 +08:00
|
|
|
const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
|
|
|
|
St = state.set<RegionDefaultValue>(R, Idx);
|
2008-12-20 14:32:12 +08:00
|
|
|
break;
|
2008-10-31 18:53:01 +08:00
|
|
|
}
|
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())
|
|
|
|
St = Bind(St, Loc::MakeVal(FR), *VI);
|
2008-10-24 16:42:28 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
else if (FTy->isArrayType())
|
|
|
|
St = BindArray(St, FR, *VI);
|
2008-10-24 16:42:28 +08:00
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
else if (FTy->isStructureType())
|
|
|
|
St = BindStruct(St, FR, *VI);
|
2008-11-19 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
2008-12-20 14:32:12 +08:00
|
|
|
return St;
|
2008-11-19 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
2009-01-13 09:49:57 +08:00
|
|
|
const GRState* RegionStoreManager::KillStruct(const GRState* St,
|
|
|
|
const TypedRegion* R){
|
|
|
|
GRStateRef state(St, StateMgr);
|
|
|
|
|
|
|
|
// Kill the struct region because it is assigned "unknown".
|
|
|
|
St = state.add<RegionKills>(R);
|
|
|
|
|
|
|
|
// Set the default value of the struct region to "unknown".
|
|
|
|
St = state.set<RegionDefaultValue>(R, UnknownVal());
|
|
|
|
|
|
|
|
Store store = St->getStore();
|
|
|
|
RegionBindingsTy B = GetRegionBindings(store);
|
|
|
|
|
|
|
|
// Remove all bindings for the subregions of the struct.
|
|
|
|
for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
|
|
|
|
const MemRegion* r = I.getKey();
|
|
|
|
if (const SubRegion* sr = dyn_cast<SubRegion>(r))
|
|
|
|
if (sr->isSubRegionOf(R))
|
|
|
|
store = Remove(store, Loc::MakeVal(sr));
|
2009-01-13 11:07:41 +08:00
|
|
|
// FIXME: Maybe we should also remove the bindings for the "views" of the
|
|
|
|
// subregions.
|
2009-01-13 09:49:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return StateMgr.MakeStateWithStore(St, store);
|
|
|
|
}
|
|
|
|
|
2008-11-16 12:07:26 +08:00
|
|
|
const GRState* RegionStoreManager::AddRegionView(const GRState* St,
|
|
|
|
const MemRegion* View,
|
|
|
|
const MemRegion* Base) {
|
|
|
|
GRStateRef state(St, StateMgr);
|
|
|
|
|
|
|
|
// First, retrieve the region view of the base region.
|
2008-12-24 09:05:03 +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.
|
2008-12-24 09:05:03 +08:00
|
|
|
return state.set<RegionViewMap>(Base, L);
|
2008-11-16 12:07:26 +08:00
|
|
|
}
|
2009-01-13 09:49:57 +08:00
|
|
|
|
|
|
|
const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
|
|
|
|
const MemRegion* View,
|
|
|
|
const MemRegion* Base) {
|
|
|
|
GRStateRef state(St, StateMgr);
|
|
|
|
|
|
|
|
// Retrieve the region view of the base region.
|
|
|
|
const RegionViews* d = state.get<RegionViewMap>(Base);
|
|
|
|
|
|
|
|
// If the base region has no view, return.
|
|
|
|
if (!d)
|
|
|
|
return St;
|
|
|
|
|
|
|
|
// Remove the view.
|
|
|
|
RegionViews V = *d;
|
|
|
|
V = RVFactory.Remove(V, View);
|
|
|
|
|
|
|
|
return state.set<RegionViewMap>(Base, V);
|
|
|
|
}
|