2008-08-13 12:27:00 +08:00
|
|
|
//= GRState*cpp - Path-Sens. "State" for tracking valuues -----*- C++ -*--=//
|
2008-02-05 15:17:49 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-08-13 12:27:00 +08:00
|
|
|
// This file defines SymbolID, ExprBindKey, and GRState*
|
2008-02-05 15:17:49 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-17 10:59:30 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRStateTrait.h"
|
2008-08-13 12:27:00 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRState.h"
|
2008-07-18 07:15:45 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
|
2008-08-28 07:13:01 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2008-08-24 06:23:37 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-08-28 07:13:01 +08:00
|
|
|
|
2008-02-05 05:59:22 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-08-28 07:13:01 +08:00
|
|
|
// Give the vtable for ConstraintManager somewhere to live.
|
|
|
|
ConstraintManager::~ConstraintManager() {}
|
|
|
|
|
2008-08-16 08:49:49 +08:00
|
|
|
GRStateManager::~GRStateManager() {
|
|
|
|
for (std::vector<GRState::Printer*>::iterator I=Printers.begin(),
|
|
|
|
E=Printers.end(); I!=E; ++I)
|
|
|
|
delete *I;
|
|
|
|
|
|
|
|
for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
|
|
|
|
I!=E; ++I)
|
|
|
|
I->second.second(I->second.first);
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState*
|
|
|
|
GRStateManager::RemoveDeadBindings(const GRState* St, Stmt* Loc,
|
2008-08-16 08:49:49 +08:00
|
|
|
const LiveVariables& Liveness,
|
|
|
|
DeadSymbolsTy& DSymbols) {
|
2008-02-09 03:17:19 +08:00
|
|
|
|
|
|
|
// This code essentially performs a "mark-and-sweep" of the VariableBindings.
|
|
|
|
// The roots are any Block-level exprs and Decls that our liveness algorithm
|
|
|
|
// tells us are live. We then see what Decls they may reference, and keep
|
|
|
|
// those around. This code more than likely can be made faster, and the
|
|
|
|
// frequency of which this method is called should be experimented with
|
2008-10-04 13:50:14 +08:00
|
|
|
// for optimum performance.
|
|
|
|
llvm::SmallVector<const MemRegion*, 10> RegionRoots;
|
2008-07-18 02:38:48 +08:00
|
|
|
StoreManager::LiveSymbolsTy LSymbols;
|
2008-08-13 12:27:00 +08:00
|
|
|
GRState NewSt = *St;
|
2008-07-18 02:38:48 +08:00
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
NewSt.Env =
|
|
|
|
EnvMgr.RemoveDeadBindings(NewSt.Env, Loc, Liveness, RegionRoots, LSymbols);
|
2008-02-09 05:10:02 +08:00
|
|
|
|
2008-07-18 02:38:48 +08:00
|
|
|
// Clean up the store.
|
|
|
|
DSymbols.clear();
|
2008-10-16 14:09:51 +08:00
|
|
|
NewSt.St = StoreMgr->RemoveDeadBindings(St->getStore(), Loc, Liveness,
|
2008-10-04 13:50:14 +08:00
|
|
|
RegionRoots, LSymbols, DSymbols);
|
2008-08-16 08:49:49 +08:00
|
|
|
|
2008-08-29 22:52:36 +08:00
|
|
|
return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewSt),
|
|
|
|
LSymbols, DSymbols);
|
2008-02-09 03:17:19 +08:00
|
|
|
}
|
2008-02-06 08:54:14 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* GRStateManager::SetRVal(const GRState* St, LVal LV,
|
2008-07-11 06:03:41 +08:00
|
|
|
RVal V) {
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-07-11 06:03:41 +08:00
|
|
|
Store OldStore = St->getStore();
|
2008-10-16 14:09:51 +08:00
|
|
|
Store NewStore = StoreMgr->SetRVal(OldStore, LV, V);
|
2008-02-07 12:16:04 +08:00
|
|
|
|
2008-07-11 06:03:41 +08:00
|
|
|
if (NewStore == OldStore)
|
|
|
|
return St;
|
2008-02-19 06:57:02 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
GRState NewSt = *St;
|
2008-07-11 06:03:41 +08:00
|
|
|
NewSt.St = NewStore;
|
|
|
|
return getPersistentState(NewSt);
|
|
|
|
}
|
2008-03-16 06:07:05 +08:00
|
|
|
|
2008-08-22 06:34:01 +08:00
|
|
|
const GRState* GRStateManager::AddDecl(const GRState* St, const VarDecl* VD,
|
|
|
|
Expr* Ex, unsigned Count) {
|
|
|
|
Store OldStore = St->getStore();
|
|
|
|
Store NewStore;
|
|
|
|
|
|
|
|
if (Ex)
|
2008-10-16 14:09:51 +08:00
|
|
|
NewStore = StoreMgr->AddDecl(OldStore, VD, Ex,
|
2008-08-22 06:34:01 +08:00
|
|
|
GetRVal(St, Ex), Count);
|
|
|
|
else
|
2008-10-16 14:09:51 +08:00
|
|
|
NewStore = StoreMgr->AddDecl(OldStore, VD, Ex);
|
2008-08-22 06:34:01 +08:00
|
|
|
|
|
|
|
if (NewStore == OldStore)
|
|
|
|
return St;
|
2008-08-23 08:50:55 +08:00
|
|
|
|
2008-08-22 06:34:01 +08:00
|
|
|
GRState NewSt = *St;
|
|
|
|
NewSt.St = NewStore;
|
|
|
|
return getPersistentState(NewSt);
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* GRStateManager::Unbind(const GRState* St, LVal LV) {
|
2008-07-11 06:03:41 +08:00
|
|
|
Store OldStore = St->getStore();
|
2008-10-16 14:09:51 +08:00
|
|
|
Store NewStore = StoreMgr->Remove(OldStore, LV);
|
2008-02-05 05:59:22 +08:00
|
|
|
|
2008-07-11 06:03:41 +08:00
|
|
|
if (NewStore == OldStore)
|
|
|
|
return St;
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
GRState NewSt = *St;
|
2008-07-11 06:03:41 +08:00
|
|
|
NewSt.St = NewStore;
|
|
|
|
return getPersistentState(NewSt);
|
2008-02-05 05:59:22 +08:00
|
|
|
}
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* GRStateManager::getInitialState() {
|
2008-02-27 07:37:01 +08:00
|
|
|
|
2008-08-20 00:51:45 +08:00
|
|
|
GRState StateImpl(EnvMgr.getInitialEnvironment(),
|
2008-10-16 14:09:51 +08:00
|
|
|
StoreMgr->getInitialStore(),
|
2008-08-17 11:10:22 +08:00
|
|
|
GDMFactory.GetEmptyMap());
|
2008-08-20 00:51:45 +08:00
|
|
|
|
2008-02-05 15:17:49 +08:00
|
|
|
return getPersistentState(StateImpl);
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* GRStateManager::getPersistentState(GRState& State) {
|
2008-02-05 15:17:49 +08:00
|
|
|
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
State.Profile(ID);
|
2008-02-12 03:21:59 +08:00
|
|
|
void* InsertPos;
|
2008-02-05 15:17:49 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
|
2008-02-05 15:17:49 +08:00
|
|
|
return I;
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
GRState* I = (GRState*) Alloc.Allocate<GRState>();
|
|
|
|
new (I) GRState(State);
|
2008-02-05 15:17:49 +08:00
|
|
|
StateSet.InsertNode(I, InsertPos);
|
|
|
|
return I;
|
|
|
|
}
|
2008-02-12 03:21:59 +08:00
|
|
|
|
2008-03-05 02:30:35 +08:00
|
|
|
|
2008-08-16 08:49:49 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// State pretty-printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-03-12 02:57:24 +08:00
|
|
|
|
2008-08-20 06:24:03 +08:00
|
|
|
void GRState::print(std::ostream& Out, StoreManager& StoreMgr,
|
2008-08-29 22:52:36 +08:00
|
|
|
ConstraintManager& ConstraintMgr,
|
2008-08-20 06:24:03 +08:00
|
|
|
Printer** Beg, Printer** End,
|
2008-08-14 05:24:49 +08:00
|
|
|
const char* nl, const char* sep) const {
|
2008-02-12 03:21:59 +08:00
|
|
|
|
2008-08-20 06:24:03 +08:00
|
|
|
// Print the store.
|
|
|
|
StoreMgr.print(getStore(), Out, nl, sep);
|
2008-02-12 03:21:59 +08:00
|
|
|
|
|
|
|
// Print Subexpression bindings.
|
2008-08-20 06:24:03 +08:00
|
|
|
bool isFirst = true;
|
2008-02-12 03:21:59 +08:00
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
|
2008-02-12 03:21:59 +08:00
|
|
|
|
|
|
|
if (isFirst) {
|
2008-03-05 02:30:35 +08:00
|
|
|
Out << nl << nl << "Sub-Expressions:" << nl;
|
2008-02-12 03:21:59 +08:00
|
|
|
isFirst = false;
|
|
|
|
}
|
2008-03-05 02:30:35 +08:00
|
|
|
else { Out << nl; }
|
2008-02-12 03:21:59 +08:00
|
|
|
|
|
|
|
Out << " (" << (void*) I.getKey() << ") ";
|
2008-09-13 13:16:45 +08:00
|
|
|
llvm::raw_os_ostream OutS(Out);
|
|
|
|
I.getKey()->printPretty(OutS);
|
|
|
|
OutS.flush();
|
2008-02-12 03:21:59 +08:00
|
|
|
Out << " : ";
|
|
|
|
I.getData().print(Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print block-expression bindings.
|
|
|
|
isFirst = true;
|
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
|
2008-02-12 03:21:59 +08:00
|
|
|
|
|
|
|
if (isFirst) {
|
2008-03-05 02:30:35 +08:00
|
|
|
Out << nl << nl << "Block-level Expressions:" << nl;
|
2008-02-12 03:21:59 +08:00
|
|
|
isFirst = false;
|
|
|
|
}
|
2008-03-05 02:30:35 +08:00
|
|
|
else { Out << nl; }
|
2008-02-12 03:21:59 +08:00
|
|
|
|
|
|
|
Out << " (" << (void*) I.getKey() << ") ";
|
2008-09-13 13:16:45 +08:00
|
|
|
llvm::raw_os_ostream OutS(Out);
|
|
|
|
I.getKey()->printPretty(OutS);
|
|
|
|
OutS.flush();
|
2008-02-12 03:21:59 +08:00
|
|
|
Out << " : ";
|
|
|
|
I.getData().print(Out);
|
|
|
|
}
|
|
|
|
|
2008-08-29 22:52:36 +08:00
|
|
|
ConstraintMgr.print(this, Out, nl, sep);
|
2008-03-12 02:57:24 +08:00
|
|
|
|
2008-08-14 05:24:49 +08:00
|
|
|
// Print checker-specific data.
|
|
|
|
for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep);
|
2008-02-12 03:21:59 +08:00
|
|
|
}
|
2008-07-18 07:15:45 +08:00
|
|
|
|
2008-08-16 08:49:49 +08:00
|
|
|
void GRStateRef::printDOT(std::ostream& Out) const {
|
|
|
|
print(Out, "\\l", "\\|");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRStateRef::printStdErr() const {
|
|
|
|
print(*llvm::cerr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRStateRef::print(std::ostream& Out, const char* nl, const char* sep)const{
|
|
|
|
GRState::Printer **beg = Mgr->Printers.empty() ? 0 : &Mgr->Printers[0];
|
|
|
|
GRState::Printer **end = !beg ? 0 : beg + Mgr->Printers.size();
|
2008-10-16 14:09:51 +08:00
|
|
|
St->print(Out, *Mgr->StoreMgr, *Mgr->ConstraintMgr, beg, end, nl, sep);
|
2008-08-16 08:49:49 +08:00
|
|
|
}
|
|
|
|
|
2008-08-15 05:16:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Generic Data Map.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void* const* GRState::FindGDM(void* K) const {
|
|
|
|
return GDM.lookup(K);
|
|
|
|
}
|
|
|
|
|
2008-08-16 08:49:49 +08:00
|
|
|
void*
|
|
|
|
GRStateManager::FindGDMContext(void* K,
|
|
|
|
void* (*CreateContext)(llvm::BumpPtrAllocator&),
|
|
|
|
void (*DeleteContext)(void*)) {
|
|
|
|
|
|
|
|
std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
|
|
|
|
if (!p.first) {
|
|
|
|
p.first = CreateContext(Alloc);
|
|
|
|
p.second = DeleteContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.first;
|
|
|
|
}
|
|
|
|
|
2008-08-15 05:16:54 +08:00
|
|
|
const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
|
|
|
|
GRState::GenericDataMap M1 = St->getGDM();
|
|
|
|
GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
|
|
|
|
|
|
|
|
if (M1 == M2)
|
|
|
|
return St;
|
|
|
|
|
|
|
|
GRState NewSt = *St;
|
|
|
|
NewSt.GDM = M2;
|
|
|
|
return getPersistentState(NewSt);
|
|
|
|
}
|
2008-07-22 08:46:16 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Queries.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
|
2008-08-16 08:49:49 +08:00
|
|
|
const llvm::APSInt& Y) {
|
|
|
|
|
2008-07-22 08:46:16 +08:00
|
|
|
RVal V = GetRVal(state, Ex);
|
|
|
|
|
|
|
|
if (lval::ConcreteInt* X = dyn_cast<lval::ConcreteInt>(&V))
|
|
|
|
return X->getValue() == Y;
|
|
|
|
|
|
|
|
if (nonlval::ConcreteInt* X = dyn_cast<nonlval::ConcreteInt>(&V))
|
|
|
|
return X->getValue() == Y;
|
|
|
|
|
|
|
|
if (nonlval::SymbolVal* X = dyn_cast<nonlval::SymbolVal>(&V))
|
2008-08-29 22:52:36 +08:00
|
|
|
return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
|
2008-07-22 08:46:16 +08:00
|
|
|
|
|
|
|
if (lval::SymbolVal* X = dyn_cast<lval::SymbolVal>(&V))
|
2008-08-29 22:52:36 +08:00
|
|
|
return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
|
2008-07-22 08:46:16 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-16 08:49:49 +08:00
|
|
|
bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
|
2008-07-22 08:46:16 +08:00
|
|
|
return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
|
|
|
|
}
|
2008-09-19 07:09:54 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Persistent values for indexing into the Generic Data Map.
|
|
|
|
|
|
|
|
int GRState::NullDerefTag::TagInt = 0;
|
|
|
|
|