2008-07-11 06:03:41 +08:00
|
|
|
//== BasicStore.cpp - Basic map from Locations to Values --------*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defined the BasicStore and BasicStoreManager classes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-06 02:08:28 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2008-08-29 07:31:31 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "clang/Analysis/AnalysisContext.h"
|
|
|
|
#include "clang/Checker/PathSensitive/GRState.h"
|
2009-08-23 20:08:50 +08:00
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
2008-07-11 06:03:41 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
|
2008-09-03 11:06:11 +08:00
|
|
|
|
2008-07-11 06:03:41 +08:00
|
|
|
namespace {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-28 14:07:30 +08:00
|
|
|
class BasicStoreSubRegionMap : public SubRegionMap {
|
2009-03-03 09:35:36 +08:00
|
|
|
public:
|
|
|
|
BasicStoreSubRegionMap() {}
|
|
|
|
|
2009-03-03 10:51:43 +08:00
|
|
|
bool iterSubRegions(const MemRegion* R, Visitor& V) const {
|
2009-03-06 00:41:21 +08:00
|
|
|
return true; // Do nothing. No subregions.
|
2009-03-03 09:35:36 +08:00
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-28 14:07:30 +08:00
|
|
|
class BasicStoreManager : public StoreManager {
|
2009-03-06 00:32:59 +08:00
|
|
|
BindingsTy::Factory VBFactory;
|
2008-07-11 06:03:41 +08:00
|
|
|
public:
|
2009-07-30 05:43:22 +08:00
|
|
|
BasicStoreManager(GRStateManager& mgr)
|
2009-08-22 07:25:54 +08:00
|
|
|
: StoreManager(mgr), VBFactory(mgr.getAllocator()) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-25 04:32:16 +08:00
|
|
|
~BasicStoreManager() {}
|
2008-07-11 06:03:41 +08:00
|
|
|
|
2010-02-05 13:18:47 +08:00
|
|
|
SubRegionMap *getSubRegionMap(Store store) {
|
2009-03-04 03:02:42 +08:00
|
|
|
return new BasicStoreSubRegionMap();
|
2009-03-03 09:35:36 +08:00
|
|
|
}
|
|
|
|
|
2010-02-05 11:01:53 +08:00
|
|
|
SVal Retrieve(Store store, Loc loc, QualType T = QualType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
|
|
|
|
unsigned Count, InvalidatedSymbols *IS);
|
2008-12-20 14:32:12 +08:00
|
|
|
|
2009-08-22 07:25:54 +08:00
|
|
|
Store scanForIvars(Stmt *B, const Decl* SelfDecl,
|
|
|
|
const MemRegion *SelfRegion, Store St);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-08 16:48:05 +08:00
|
|
|
Store Bind(Store St, Loc loc, SVal V);
|
2009-01-08 06:56:17 +08:00
|
|
|
Store Remove(Store St, Loc loc);
|
2009-08-17 14:19:58 +08:00
|
|
|
Store getInitialStore(const LocationContext *InitLoc);
|
2008-10-07 09:31:04 +08:00
|
|
|
|
2008-10-16 14:09:51 +08:00
|
|
|
// FIXME: Investigate what is using this. This method should be removed.
|
2009-08-22 06:28:32 +08:00
|
|
|
virtual Loc getLoc(const VarDecl* VD, const LocationContext *LC) {
|
|
|
|
return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
|
2008-10-07 09:31:04 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store BindCompoundLiteral(Store store, const CompoundLiteralExpr*,
|
|
|
|
const LocationContext*, SVal val) {
|
|
|
|
return store;
|
2008-10-28 05:54:31 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-25 04:32:16 +08:00
|
|
|
/// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
|
|
|
|
/// conversions between arrays and pointers.
|
2009-03-30 13:55:46 +08:00
|
|
|
SVal ArrayToPointer(Loc Array) { return Array; }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-05 08:47:52 +08:00
|
|
|
/// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
|
2009-08-02 12:45:08 +08:00
|
|
|
/// It updatees the GRState object in place with the values removed.
|
2010-07-02 04:09:55 +08:00
|
|
|
const GRState *RemoveDeadBindings(GRState &state,
|
2010-03-17 11:35:08 +08:00
|
|
|
const StackFrameContext *LCtx,
|
|
|
|
SymbolReaper& SymReaper,
|
2009-08-02 12:45:08 +08:00
|
|
|
llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
|
2008-08-22 06:34:01 +08:00
|
|
|
|
2008-10-25 04:32:16 +08:00
|
|
|
void iterBindings(Store store, BindingsHandler& f);
|
2008-09-03 11:06:11 +08:00
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store BindDecl(Store store, const VarRegion *VR, SVal InitVal) {
|
|
|
|
return BindDeclInternal(store, VR, &InitVal);
|
2008-12-20 14:32:12 +08:00
|
|
|
}
|
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store BindDeclWithNoInit(Store store, const VarRegion *VR) {
|
|
|
|
return BindDeclInternal(store, VR, 0);
|
2008-12-20 14:32:12 +08:00
|
|
|
}
|
|
|
|
|
2009-11-04 08:09:15 +08:00
|
|
|
Store BindDeclInternal(Store store, const VarRegion *VR, SVal *InitVal);
|
2008-08-22 06:34:01 +08:00
|
|
|
|
2009-03-06 00:32:59 +08:00
|
|
|
static inline BindingsTy GetBindings(Store store) {
|
|
|
|
return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
|
2008-08-20 06:24:03 +08:00
|
|
|
}
|
|
|
|
|
2009-06-25 07:06:47 +08:00
|
|
|
void print(Store store, llvm::raw_ostream& Out, const char* nl,
|
|
|
|
const char *sep);
|
2009-04-03 15:33:13 +08:00
|
|
|
|
|
|
|
private:
|
2010-03-03 05:43:54 +08:00
|
|
|
SVal LazyRetrieve(Store store, const TypedRegion *R);
|
|
|
|
|
2009-04-03 15:33:13 +08:00
|
|
|
ASTContext& getContext() { return StateMgr.getContext(); }
|
2008-09-03 11:06:11 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-11 06:03:41 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
2008-08-29 07:31:31 +08:00
|
|
|
StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
|
|
|
|
return new BasicStoreManager(StMgr);
|
2008-08-26 03:33:03 +08:00
|
|
|
}
|
2008-10-21 14:54:23 +08:00
|
|
|
|
2009-04-30 00:03:27 +08:00
|
|
|
static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
|
2009-04-11 08:11:10 +08:00
|
|
|
bool foundPointer = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
while (1) {
|
2009-07-30 05:53:49 +08:00
|
|
|
const PointerType *PT = T->getAs<PointerType>();
|
2009-04-11 08:11:10 +08:00
|
|
|
if (!PT) {
|
|
|
|
if (!foundPointer)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-30 00:03:27 +08:00
|
|
|
// intptr_t* or intptr_t**, etc?
|
|
|
|
if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
QualType X = C.getCanonicalType(T).getUnqualifiedType();
|
|
|
|
return X == C.VoidTy;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 08:11:10 +08:00
|
|
|
foundPointer = true;
|
|
|
|
T = PT->getPointeeType();
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-04-11 08:11:10 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-03-03 05:43:54 +08:00
|
|
|
SVal BasicStoreManager::LazyRetrieve(Store store, const TypedRegion *R) {
|
|
|
|
const VarRegion *VR = dyn_cast<VarRegion>(R);
|
|
|
|
if (!VR)
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
const VarDecl *VD = VR->getDecl();
|
|
|
|
QualType T = VD->getType();
|
|
|
|
|
|
|
|
// Only handle simple types that we can symbolicate.
|
|
|
|
if (!SymbolManager::canSymbolicate(T) || !T->isScalarType())
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// Globals and parameters start with symbolic values.
|
|
|
|
// Local variables initially are undefined.
|
2010-03-10 08:18:08 +08:00
|
|
|
if (VR->hasGlobalsOrParametersStorage() ||
|
|
|
|
isa<UnknownSpaceRegion>(VR->getMemorySpace()))
|
2010-03-03 05:43:54 +08:00
|
|
|
return ValMgr.getRegionValueSymbolVal(R);
|
|
|
|
return UndefinedVal();
|
|
|
|
}
|
|
|
|
|
2010-02-05 11:01:53 +08:00
|
|
|
SVal BasicStoreManager::Retrieve(Store store, Loc loc, QualType T) {
|
2009-01-08 06:56:17 +08:00
|
|
|
if (isa<UnknownVal>(loc))
|
2010-02-04 10:39:47 +08:00
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-26 04:51:30 +08:00
|
|
|
assert(!isa<UndefinedVal>(loc));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-08 06:56:17 +08:00
|
|
|
switch (loc.getSubKind()) {
|
2008-07-11 06:03:41 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::MemRegionKind: {
|
2009-03-06 02:08:28 +08:00
|
|
|
const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-06 02:08:28 +08:00
|
|
|
if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
|
2010-02-04 10:39:47 +08:00
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-05 11:01:53 +08:00
|
|
|
BindingsTy B = GetBindings(store);
|
2009-08-26 04:51:30 +08:00
|
|
|
BindingsTy::data_type *Val = B.lookup(R);
|
2010-03-03 05:43:54 +08:00
|
|
|
const TypedRegion *TR = cast<TypedRegion>(R);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-03-03 05:43:54 +08:00
|
|
|
if (Val)
|
|
|
|
return CastRetrievedVal(*Val, TR, T);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-03-03 05:43:54 +08:00
|
|
|
SVal V = LazyRetrieve(store, TR);
|
|
|
|
return V.isUnknownOrUndef() ? V : CastRetrievedVal(V, TR, T);
|
2008-07-11 06:03:41 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::ConcreteIntKind:
|
|
|
|
// Some clients may call GetSVal with such an option simply because
|
|
|
|
// they are doing a quick scan through their Locs (potentially to
|
2008-07-11 06:03:41 +08:00
|
|
|
// invalidate their bindings). Just return Undefined.
|
2010-02-04 10:39:47 +08:00
|
|
|
return UndefinedVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-11 06:03:41 +08:00
|
|
|
default:
|
2008-10-17 13:57:07 +08:00
|
|
|
assert (false && "Invalid Loc.");
|
2008-07-11 06:03:41 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-04 10:39:47 +08:00
|
|
|
return UnknownVal();
|
2008-07-11 06:03:41 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-08 16:48:05 +08:00
|
|
|
Store BasicStoreManager::Bind(Store store, Loc loc, SVal V) {
|
2009-06-28 18:16:11 +08:00
|
|
|
if (isa<loc::ConcreteInt>(loc))
|
|
|
|
return store;
|
|
|
|
|
2009-06-28 17:26:15 +08:00
|
|
|
const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
|
|
|
|
ASTContext &C = StateMgr.getContext();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-28 17:26:15 +08:00
|
|
|
// Special case: handle store of pointer values (Loc) to pointers via
|
|
|
|
// a cast to intXX_t*, void*, etc. This is needed to handle
|
|
|
|
// OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
|
|
|
|
if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
|
|
|
|
if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
|
|
|
|
// FIXME: Should check for index 0.
|
|
|
|
QualType T = ER->getLocationType(C);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-28 17:26:15 +08:00
|
|
|
if (isHigherOrderRawPtr(T, C))
|
|
|
|
R = ER->getSuperRegion();
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
2009-06-28 17:26:15 +08:00
|
|
|
if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
|
|
|
|
return store;
|
2009-08-22 07:25:54 +08:00
|
|
|
|
2009-08-26 07:29:04 +08:00
|
|
|
const TypedRegion *TyR = cast<TypedRegion>(R);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-26 07:29:04 +08:00
|
|
|
// Do not bind to arrays. We need to explicitly check for this so that
|
|
|
|
// we do not encounter any weirdness of trying to load/store from arrays.
|
|
|
|
if (TyR->isBoundable() && TyR->getValueType(C)->isArrayType())
|
2009-09-09 23:08:12 +08:00
|
|
|
return store;
|
2009-08-26 07:29:04 +08:00
|
|
|
|
2009-06-28 17:26:15 +08:00
|
|
|
if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
|
|
|
|
// Only convert 'V' to a location iff the underlying region type
|
|
|
|
// is a location as well.
|
|
|
|
// FIXME: We are allowing a store of an arbitrary location to
|
|
|
|
// a pointer. We may wish to flag a type error here if the types
|
|
|
|
// are incompatible. This may also cause lots of breakage
|
|
|
|
// elsewhere. Food for thought.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (TyR->isBoundable() && Loc::IsLocType(TyR->getValueType(C)))
|
2009-08-26 07:29:04 +08:00
|
|
|
V = X->getLoc();
|
2008-07-11 06:03:41 +08:00
|
|
|
}
|
2009-06-28 17:26:15 +08:00
|
|
|
|
|
|
|
BindingsTy B = GetBindings(store);
|
|
|
|
return V.isUnknown()
|
|
|
|
? VBFactory.Remove(B, R).getRoot()
|
|
|
|
: VBFactory.Add(B, R, V).getRoot();
|
2008-07-11 06:03:41 +08:00
|
|
|
}
|
|
|
|
|
2009-01-08 06:56:17 +08:00
|
|
|
Store BasicStoreManager::Remove(Store store, Loc loc) {
|
|
|
|
switch (loc.getSubKind()) {
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::MemRegionKind: {
|
2009-03-06 02:08:28 +08:00
|
|
|
const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-06 02:08:28 +08:00
|
|
|
if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
|
2008-10-04 13:50:14 +08:00
|
|
|
return store;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-06 02:08:28 +08:00
|
|
|
return VBFactory.Remove(GetBindings(store), R).getRoot();
|
2008-07-18 02:38:48 +08:00
|
|
|
}
|
2008-07-11 06:03:41 +08:00
|
|
|
default:
|
2008-10-17 13:57:07 +08:00
|
|
|
assert ("Remove for given Loc type not yet implemented.");
|
2008-07-18 02:38:48 +08:00
|
|
|
return store;
|
2008-07-11 06:03:41 +08:00
|
|
|
}
|
|
|
|
}
|
2008-07-18 02:38:48 +08:00
|
|
|
|
2010-07-02 04:09:55 +08:00
|
|
|
const GRState *BasicStoreManager::RemoveDeadBindings(GRState &state,
|
2010-03-17 11:35:08 +08:00
|
|
|
const StackFrameContext *LCtx,
|
2010-02-05 13:34:29 +08:00
|
|
|
SymbolReaper& SymReaper,
|
2009-08-02 12:45:08 +08:00
|
|
|
llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
|
2009-09-09 23:08:12 +08:00
|
|
|
{
|
2010-05-26 11:27:35 +08:00
|
|
|
Store store = state.getStore();
|
2009-03-06 00:32:59 +08:00
|
|
|
BindingsTy B = GetBindings(store);
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef SVal::symbol_iterator symbol_iterator;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-18 02:38:48 +08:00
|
|
|
// Iterate over the variable bindings.
|
2009-03-06 00:32:59 +08:00
|
|
|
for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
|
2009-03-06 02:08:28 +08:00
|
|
|
if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
|
2010-07-02 04:09:55 +08:00
|
|
|
if (SymReaper.isLive(VR))
|
2009-03-06 02:08:28 +08:00
|
|
|
RegionRoots.push_back(VR);
|
|
|
|
else
|
|
|
|
continue;
|
2008-07-18 02:38:48 +08:00
|
|
|
}
|
2009-03-06 02:08:28 +08:00
|
|
|
else if (isa<ObjCIvarRegion>(I.getKey())) {
|
|
|
|
RegionRoots.push_back(I.getKey());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-06 02:08:28 +08:00
|
|
|
// Mark the bindings in the data as live.
|
|
|
|
SVal X = I.getData();
|
|
|
|
for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
|
|
|
|
SymReaper.markLive(*SI);
|
2009-03-06 00:31:07 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-18 02:38:48 +08:00
|
|
|
// Scan for live variables and live symbols.
|
2009-03-06 02:08:28 +08:00
|
|
|
llvm::SmallPtrSet<const MemRegion*, 10> Marked;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
while (!RegionRoots.empty()) {
|
2008-10-18 06:52:40 +08:00
|
|
|
const MemRegion* MR = RegionRoots.back();
|
2008-10-04 13:50:14 +08:00
|
|
|
RegionRoots.pop_back();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 06:52:40 +08:00
|
|
|
while (MR) {
|
|
|
|
if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
|
2009-01-22 06:26:05 +08:00
|
|
|
SymReaper.markLive(SymR->getSymbol());
|
2008-10-18 06:52:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-03-06 02:08:28 +08:00
|
|
|
else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
|
|
|
|
if (Marked.count(MR))
|
2008-10-18 06:52:40 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
Marked.insert(MR);
|
2010-02-05 13:34:29 +08:00
|
|
|
SVal X = Retrieve(store, loc::MemRegionVal(MR));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 06:52:40 +08:00
|
|
|
// FIXME: We need to handle symbols nested in region definitions.
|
2009-01-22 06:26:05 +08:00
|
|
|
for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
|
|
|
|
SymReaper.markLive(*SI);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 06:52:40 +08:00
|
|
|
if (!isa<loc::MemRegionVal>(X))
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 06:52:40 +08:00
|
|
|
const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
|
|
|
|
RegionRoots.push_back(LVD.getRegion());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
|
|
|
|
MR = R->getSuperRegion();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2008-07-18 02:38:48 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Remove dead variable bindings.
|
2009-03-06 00:32:59 +08:00
|
|
|
for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
|
2009-03-06 02:08:28 +08:00
|
|
|
const MemRegion* R = I.getKey();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
if (!Marked.count(R)) {
|
2009-06-23 17:02:15 +08:00
|
|
|
store = Remove(store, ValMgr.makeLoc(R));
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal X = I.getData();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-18 02:38:48 +08:00
|
|
|
for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
|
2009-01-22 06:26:05 +08:00
|
|
|
SymReaper.maybeDead(*SI);
|
2008-07-18 02:38:48 +08:00
|
|
|
}
|
2008-10-04 13:50:14 +08:00
|
|
|
}
|
2009-03-06 02:08:28 +08:00
|
|
|
|
2010-05-26 11:27:35 +08:00
|
|
|
state.setStore(store);
|
|
|
|
return StateMgr.getPersistentState(state);
|
2008-07-18 02:38:48 +08:00
|
|
|
}
|
2008-08-20 00:51:45 +08:00
|
|
|
|
2009-08-22 07:25:54 +08:00
|
|
|
Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl,
|
|
|
|
const MemRegion *SelfRegion, Store St) {
|
2009-03-06 02:08:28 +08:00
|
|
|
for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
|
|
|
|
CI != CE; ++CI) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-06 02:08:28 +08:00
|
|
|
if (!*CI)
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-06 02:08:28 +08:00
|
|
|
// Check if the statement is an ivar reference. We only
|
|
|
|
// care about self.ivar.
|
|
|
|
if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
|
|
|
|
const Expr *Base = IV->getBase()->IgnoreParenCasts();
|
|
|
|
if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
|
|
|
|
if (DR->getDecl() == SelfDecl) {
|
2010-03-01 14:56:52 +08:00
|
|
|
const ObjCIvarRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
|
2009-09-09 23:08:12 +08:00
|
|
|
SelfRegion);
|
|
|
|
SVal X = ValMgr.getRegionValueSymbolVal(IVR);
|
2010-02-08 16:48:05 +08:00
|
|
|
St = Bind(St, ValMgr.makeLoc(IVR), X);
|
2009-03-06 02:08:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-08-22 07:25:54 +08:00
|
|
|
St = scanForIvars(*CI, SelfDecl, SelfRegion, St);
|
2009-03-06 02:08:28 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-06 02:08:28 +08:00
|
|
|
return St;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) {
|
2008-08-20 00:51:45 +08:00
|
|
|
// The LiveVariables information already has a compilation of all VarDecls
|
|
|
|
// used in the function. Iterate through this set, and "symbolicate"
|
|
|
|
// any VarDecl whose value originally comes from outside the function.
|
|
|
|
typedef LiveVariables::AnalysisDataTy LVDataTy;
|
2009-08-17 14:19:58 +08:00
|
|
|
LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData();
|
2008-08-20 00:51:45 +08:00
|
|
|
Store St = VBFactory.GetEmptyMap().getRoot();
|
|
|
|
|
|
|
|
for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
|
2008-10-22 00:13:35 +08:00
|
|
|
NamedDecl* ND = const_cast<NamedDecl*>(I->first);
|
2008-08-20 00:51:45 +08:00
|
|
|
|
2008-10-25 04:32:16 +08:00
|
|
|
// Handle implicit parameters.
|
|
|
|
if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
const Decl& CD = *InitLoc->getDecl();
|
2008-10-25 04:32:16 +08:00
|
|
|
if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
|
|
|
|
if (MD->getSelfDecl() == PD) {
|
2009-12-17 07:53:37 +08:00
|
|
|
// FIXME: Add type constraints (when they become available) to
|
|
|
|
// SelfRegion? (i.e., it implements MD->getClassInterface()).
|
2010-03-01 14:56:52 +08:00
|
|
|
const VarRegion *VR = MRMgr.getVarRegion(PD, InitLoc);
|
2009-12-17 07:53:37 +08:00
|
|
|
const MemRegion *SelfRegion =
|
2010-03-03 05:43:54 +08:00
|
|
|
ValMgr.getRegionValueSymbolVal(VR).getAsRegion();
|
|
|
|
assert(SelfRegion);
|
2010-02-08 16:48:05 +08:00
|
|
|
St = Bind(St, ValMgr.makeLoc(VR), loc::MemRegionVal(SelfRegion));
|
2009-03-06 02:08:28 +08:00
|
|
|
// Scan the method for ivar references. While this requires an
|
|
|
|
// entire AST scan, the cost should not be high in practice.
|
2009-08-22 07:25:54 +08:00
|
|
|
St = scanForIvars(MD->getBody(), PD, SelfRegion, St);
|
2008-10-25 04:32:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-20 00:51:45 +08:00
|
|
|
}
|
2010-03-03 05:43:54 +08:00
|
|
|
|
2008-08-20 00:51:45 +08:00
|
|
|
return St;
|
|
|
|
}
|
2008-08-20 06:24:03 +08:00
|
|
|
|
2009-11-04 08:09:15 +08:00
|
|
|
Store BasicStoreManager::BindDeclInternal(Store store, const VarRegion* VR,
|
2008-12-20 14:32:12 +08:00
|
|
|
SVal* InitVal) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-23 08:50:55 +08:00
|
|
|
BasicValueFactory& BasicVals = StateMgr.getBasicVals();
|
2009-11-04 08:09:15 +08:00
|
|
|
const VarDecl *VD = VR->getDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-22 06:34:01 +08:00
|
|
|
// BasicStore does not model arrays and structs.
|
2010-04-27 05:31:17 +08:00
|
|
|
if (VD->getType()->isArrayType() || VD->getType()->isStructureOrClassType())
|
2008-08-22 06:34:01 +08:00
|
|
|
return store;
|
|
|
|
|
|
|
|
if (VD->hasGlobalStorage()) {
|
|
|
|
// Handle variables with global storage: extern, static, PrivateExtern.
|
|
|
|
|
|
|
|
// FIXME:: static variables may have an initializer, but the second time a
|
|
|
|
// function is called those values may not be current. Currently, a function
|
|
|
|
// will not be called more than once.
|
|
|
|
|
|
|
|
// Static global variables should not be visited here.
|
|
|
|
assert(!(VD->getStorageClass() == VarDecl::Static &&
|
|
|
|
VD->isFileVarDecl()));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-22 06:34:01 +08:00
|
|
|
// Process static variables.
|
|
|
|
if (VD->getStorageClass() == VarDecl::Static) {
|
|
|
|
// C99: 6.7.8 Initialization
|
|
|
|
// If an object that has static storage duration is not initialized
|
2009-09-09 23:08:12 +08:00
|
|
|
// explicitly, then:
|
|
|
|
// —if it has pointer type, it is initialized to a null pointer;
|
|
|
|
// —if it has arithmetic type, it is initialized to (positive or
|
2008-08-22 06:34:01 +08:00
|
|
|
// unsigned) zero;
|
2008-11-13 03:18:35 +08:00
|
|
|
if (!InitVal) {
|
2008-08-22 06:34:01 +08:00
|
|
|
QualType T = VD->getType();
|
2008-10-17 13:57:07 +08:00
|
|
|
if (Loc::IsLocType(T))
|
2010-02-08 16:48:05 +08:00
|
|
|
store = Bind(store, loc::MemRegionVal(VR),
|
2008-10-21 14:27:32 +08:00
|
|
|
loc::ConcreteInt(BasicVals.getValue(0, T)));
|
2008-08-22 06:34:01 +08:00
|
|
|
else if (T->isIntegerType())
|
2010-02-08 16:48:05 +08:00
|
|
|
store = Bind(store, loc::MemRegionVal(VR),
|
2008-10-21 14:27:32 +08:00
|
|
|
nonloc::ConcreteInt(BasicVals.getValue(0, T)));
|
2008-08-22 06:34:01 +08:00
|
|
|
else {
|
|
|
|
// assert(0 && "ignore other types of variables");
|
|
|
|
}
|
|
|
|
} else {
|
2010-02-08 16:48:05 +08:00
|
|
|
store = Bind(store, loc::MemRegionVal(VR), *InitVal);
|
2008-08-22 06:34:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Process local scalar variables.
|
|
|
|
QualType T = VD->getType();
|
2009-07-03 08:36:16 +08:00
|
|
|
if (ValMgr.getSymbolManager().canSymbolicate(T)) {
|
2008-11-13 03:18:35 +08:00
|
|
|
SVal V = InitVal ? *InitVal : UndefinedVal();
|
2010-02-08 16:48:05 +08:00
|
|
|
store = Bind(store, loc::MemRegionVal(VR), V);
|
2008-08-22 06:34:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
2009-06-25 07:06:47 +08:00
|
|
|
void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
|
2008-08-20 06:24:03 +08:00
|
|
|
const char* nl, const char *sep) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-06 00:32:59 +08:00
|
|
|
BindingsTy B = GetBindings(store);
|
2008-08-20 06:24:03 +08:00
|
|
|
Out << "Variables:" << nl;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-20 06:24:03 +08:00
|
|
|
bool isFirst = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-06 00:32:59 +08:00
|
|
|
for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
|
2009-06-25 07:06:47 +08:00
|
|
|
if (isFirst)
|
|
|
|
isFirst = false;
|
|
|
|
else
|
|
|
|
Out << nl;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-14 07:53:06 +08:00
|
|
|
Out << ' ' << I.getKey() << " : " << I.getData();
|
2008-08-20 06:24:03 +08:00
|
|
|
}
|
|
|
|
}
|
2008-08-29 08:47:32 +08:00
|
|
|
|
2008-09-03 11:06:11 +08:00
|
|
|
|
|
|
|
void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
|
2009-03-06 00:32:59 +08:00
|
|
|
BindingsTy B = GetBindings(store);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-06 00:32:59 +08:00
|
|
|
for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
|
2010-06-17 08:24:37 +08:00
|
|
|
if (!f.HandleBinding(*this, store, I.getKey(), I.getData()))
|
|
|
|
return;
|
2008-09-03 11:06:11 +08:00
|
|
|
|
2008-08-29 08:47:32 +08:00
|
|
|
}
|
2008-09-03 11:06:11 +08:00
|
|
|
|
|
|
|
StoreManager::BindingsHandler::~BindingsHandler() {}
|
2009-07-30 02:16:25 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Binding invalidation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store BasicStoreManager::InvalidateRegion(Store store,
|
|
|
|
const MemRegion *R,
|
|
|
|
const Expr *E,
|
|
|
|
unsigned Count,
|
|
|
|
InvalidatedSymbols *IS) {
|
2009-11-10 10:17:20 +08:00
|
|
|
R = R->StripCasts();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-30 02:16:25 +08:00
|
|
|
if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
|
2010-02-05 13:06:13 +08:00
|
|
|
return store;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-16 08:30:49 +08:00
|
|
|
if (IS) {
|
2010-02-05 13:06:13 +08:00
|
|
|
BindingsTy B = GetBindings(store);
|
2009-10-16 08:30:49 +08:00
|
|
|
if (BindingsTy::data_type *Val = B.lookup(R)) {
|
|
|
|
if (SymbolRef Sym = Val->getAsSymbol())
|
|
|
|
IS->insert(Sym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-30 02:16:25 +08:00
|
|
|
QualType T = cast<TypedRegion>(R)->getValueType(R->getContext());
|
2009-09-28 04:45:21 +08:00
|
|
|
SVal V = ValMgr.getConjuredSymbolVal(R, E, T, Count);
|
2010-02-05 13:06:13 +08:00
|
|
|
return Bind(store, loc::MemRegionVal(R), V);
|
2009-07-30 02:16:25 +08:00
|
|
|
}
|
|
|
|
|