2010-02-03 17:10:32 +08:00
|
|
|
//=== FlatStore.cpp - Flat region-based store model -------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Checker/PathSensitive/GRState.h"
|
|
|
|
#include "llvm/ADT/ImmutableIntervalMap.h"
|
2010-02-09 04:24:21 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-02-03 17:10:32 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-02-08 13:40:07 +08:00
|
|
|
using llvm::Interval;
|
2010-02-03 17:10:32 +08:00
|
|
|
|
|
|
|
// The actual store type.
|
|
|
|
typedef llvm::ImmutableIntervalMap<SVal> BindingVal;
|
|
|
|
typedef llvm::ImmutableMap<const MemRegion *, BindingVal> RegionBindings;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class FlatStoreManager : public StoreManager {
|
|
|
|
RegionBindings::Factory RBFactory;
|
|
|
|
BindingVal::Factory BVFactory;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FlatStoreManager(GRStateManager &mgr)
|
|
|
|
: StoreManager(mgr),
|
|
|
|
RBFactory(mgr.getAllocator()),
|
|
|
|
BVFactory(mgr.getAllocator()) {}
|
|
|
|
|
2010-02-08 13:40:07 +08:00
|
|
|
SVal Retrieve(Store store, Loc L, QualType T);
|
|
|
|
Store Bind(Store store, Loc L, SVal val);
|
2010-02-03 17:10:32 +08:00
|
|
|
Store Remove(Store St, Loc L);
|
2010-02-05 13:06:13 +08:00
|
|
|
Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* cl,
|
|
|
|
const LocationContext *LC, SVal v);
|
2010-02-03 17:10:32 +08:00
|
|
|
|
|
|
|
Store getInitialStore(const LocationContext *InitLoc) {
|
|
|
|
return RBFactory.GetEmptyMap().getRoot();
|
|
|
|
}
|
|
|
|
|
2010-02-05 13:18:47 +08:00
|
|
|
SubRegionMap *getSubRegionMap(Store store) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-03 17:10:32 +08:00
|
|
|
|
|
|
|
SVal ArrayToPointer(Loc Array);
|
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,
|
2010-02-05 13:34:29 +08:00
|
|
|
llvm::SmallVectorImpl<const MemRegion*>& RegionRoots){
|
2010-05-26 11:27:35 +08:00
|
|
|
return StateMgr.getPersistentState(state);
|
2010-02-05 13:34:29 +08:00
|
|
|
}
|
2010-02-03 17:10:32 +08:00
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store BindDecl(Store store, const VarRegion *VR, SVal initVal);
|
2010-02-03 17:10:32 +08:00
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store BindDeclWithNoInit(Store store, const VarRegion *VR);
|
2010-02-03 17:10:32 +08:00
|
|
|
|
|
|
|
typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols;
|
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
|
|
|
|
unsigned Count, InvalidatedSymbols *IS);
|
2010-07-02 04:16:50 +08:00
|
|
|
|
|
|
|
Store InvalidateRegions(Store store, const MemRegion * const *I,
|
|
|
|
const MemRegion * const *E, const Expr *Ex,
|
|
|
|
unsigned Count, InvalidatedSymbols *IS,
|
|
|
|
bool invalidateGlobals);
|
2010-02-03 17:10:32 +08:00
|
|
|
|
|
|
|
void print(Store store, llvm::raw_ostream& Out, const char* nl,
|
|
|
|
const char *sep);
|
|
|
|
void iterBindings(Store store, BindingsHandler& f);
|
2010-02-08 13:40:07 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
static RegionBindings getRegionBindings(Store store) {
|
|
|
|
return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
|
|
|
|
}
|
|
|
|
|
|
|
|
Interval RegionToInterval(const MemRegion *R);
|
|
|
|
|
|
|
|
SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T);
|
2010-02-03 17:10:32 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
StoreManager *clang::CreateFlatStoreManager(GRStateManager &StMgr) {
|
|
|
|
return new FlatStoreManager(StMgr);
|
|
|
|
}
|
|
|
|
|
2010-02-08 13:40:07 +08:00
|
|
|
SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) {
|
|
|
|
const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
|
|
|
|
Interval I = RegionToInterval(R);
|
|
|
|
RegionBindings B = getRegionBindings(store);
|
|
|
|
const BindingVal *BV = B.lookup(R);
|
|
|
|
if (BV) {
|
|
|
|
const SVal *V = BVFactory.Lookup(*BV, I);
|
|
|
|
if (V)
|
|
|
|
return *V;
|
|
|
|
else
|
|
|
|
return RetrieveRegionWithNoBinding(R, T);
|
|
|
|
}
|
|
|
|
return RetrieveRegionWithNoBinding(R, T);
|
2010-02-03 17:10:32 +08:00
|
|
|
}
|
|
|
|
|
2010-02-08 13:40:07 +08:00
|
|
|
SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R,
|
|
|
|
QualType T) {
|
|
|
|
if (R->hasStackNonParametersStorage())
|
|
|
|
return UndefinedVal();
|
|
|
|
else
|
2010-03-01 14:56:52 +08:00
|
|
|
return ValMgr.getRegionValueSymbolVal(cast<TypedRegion>(R));
|
2010-02-08 13:40:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Store FlatStoreManager::Bind(Store store, Loc L, SVal val) {
|
|
|
|
const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
|
|
|
|
RegionBindings B = getRegionBindings(store);
|
|
|
|
const BindingVal *V = B.lookup(R);
|
|
|
|
|
|
|
|
BindingVal BV = BVFactory.GetEmptyMap();
|
|
|
|
if (V)
|
|
|
|
BV = *V;
|
|
|
|
|
|
|
|
Interval I = RegionToInterval(R);
|
|
|
|
BV = BVFactory.Add(BV, I, val);
|
|
|
|
B = RBFactory.Add(B, R, BV);
|
|
|
|
return B.getRoot();
|
2010-02-03 17:10:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Store FlatStoreManager::Remove(Store store, Loc L) {
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store FlatStoreManager::BindCompoundLiteral(Store store,
|
|
|
|
const CompoundLiteralExpr* cl,
|
|
|
|
const LocationContext *LC,
|
|
|
|
SVal v) {
|
|
|
|
return store;
|
2010-02-03 17:10:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SVal FlatStoreManager::ArrayToPointer(Loc Array) {
|
|
|
|
return Array;
|
|
|
|
}
|
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR,
|
|
|
|
SVal initVal) {
|
|
|
|
return store;
|
2010-02-03 17:10:32 +08:00
|
|
|
}
|
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) {
|
|
|
|
return store;
|
2010-02-03 17:10:32 +08:00
|
|
|
}
|
|
|
|
|
2010-07-02 04:16:50 +08:00
|
|
|
Store FlatStoreManager::InvalidateRegions(Store store,
|
|
|
|
const MemRegion * const *I,
|
|
|
|
const MemRegion * const *E,
|
|
|
|
const Expr *Ex, unsigned Count,
|
|
|
|
InvalidatedSymbols *IS,
|
|
|
|
bool invalidateGlobals) {
|
|
|
|
assert(false && "Not implemented");
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
2010-02-05 13:06:13 +08:00
|
|
|
Store FlatStoreManager::InvalidateRegion(Store store, const MemRegion *R,
|
|
|
|
const Expr *E, unsigned Count,
|
|
|
|
InvalidatedSymbols *IS) {
|
2010-07-02 04:16:50 +08:00
|
|
|
assert(false && "Not implemented");
|
2010-02-05 13:06:13 +08:00
|
|
|
return store;
|
2010-02-03 17:10:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlatStoreManager::print(Store store, llvm::raw_ostream& Out,
|
|
|
|
const char* nl, const char *sep) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) {
|
|
|
|
}
|
2010-02-08 13:40:07 +08:00
|
|
|
|
|
|
|
Interval FlatStoreManager::RegionToInterval(const MemRegion *R) {
|
|
|
|
switch (R->getKind()) {
|
|
|
|
case MemRegion::VarRegionKind: {
|
2010-02-08 14:00:22 +08:00
|
|
|
QualType T = cast<VarRegion>(R)->getValueType(Ctx);
|
|
|
|
uint64_t Size = Ctx.getTypeSize(T);
|
2010-02-08 13:40:07 +08:00
|
|
|
return Interval(0, Size-1);
|
|
|
|
}
|
|
|
|
default:
|
2010-02-09 04:24:21 +08:00
|
|
|
llvm_unreachable("Region kind unhandled.");
|
|
|
|
return Interval(0, 0);
|
2010-02-08 13:40:07 +08:00
|
|
|
}
|
|
|
|
}
|