From caa3724b1d525a888982f94a6ae2b527eb3bca7d Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Tue, 19 Aug 2008 16:51:45 +0000 Subject: [PATCH] Patch by Zhongxing Xu! This patch extends BasicStoreManager::getInitialStore() to include code that symbolicates input variables. It also removes redundant handling of ImplicitParamDecl, since it is a subclass of VarDecl. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@54993 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../clang/Analysis/PathSensitive/GRState.h | 14 ++++-- include/clang/Analysis/PathSensitive/Store.h | 3 +- lib/Analysis/BasicStore.cpp | 40 +++++++++++++++-- lib/Analysis/GRExprEngine.cpp | 44 +------------------ lib/Analysis/GRState.cpp | 5 ++- 5 files changed, 54 insertions(+), 52 deletions(-) diff --git a/include/clang/Analysis/PathSensitive/GRState.h b/include/clang/Analysis/PathSensitive/GRState.h index 39d79bc3ae..1944cca8fa 100644 --- a/include/clang/Analysis/PathSensitive/GRState.h +++ b/include/clang/Analysis/PathSensitive/GRState.h @@ -270,7 +270,11 @@ private: /// TF - Object that represents a bundle of transfer functions /// for manipulating and creating RVals. GRTransferFuncs* TF; - + + /// Liveness - live-variables information of the ValueDecl* and block-level + /// Expr* in the CFG. Used to get initial store and prune out dead state. + LiveVariables& Liveness; + private: Environment RemoveBlkExpr(const Environment& Env, Expr* E) { @@ -284,7 +288,7 @@ private: public: GRStateManager(ASTContext& Ctx, StoreManager* stmgr, - llvm::BumpPtrAllocator& alloc, CFG& c) + llvm::BumpPtrAllocator& alloc, CFG& c, LiveVariables& L) : EnvMgr(alloc), StMgr(stmgr), ISetFactory(alloc), @@ -292,7 +296,8 @@ public: BasicVals(Ctx, alloc), SymMgr(alloc), Alloc(alloc), - cfg(c) {} + cfg(c), + Liveness(L) {} ~GRStateManager(); @@ -301,7 +306,8 @@ public: BasicValueFactory& getBasicVals() { return BasicVals; } const BasicValueFactory& getBasicVals() const { return BasicVals; } SymbolManager& getSymbolManager() { return SymMgr; } - + LiveVariables& getLiveVariables() { return Liveness; } + typedef StoreManager::DeadSymbolsTy DeadSymbolsTy; const GRState* RemoveDeadBindings(const GRState* St, Stmt* Loc, diff --git a/include/clang/Analysis/PathSensitive/Store.h b/include/clang/Analysis/PathSensitive/Store.h index 657ce8d954..acc3d8e8bb 100644 --- a/include/clang/Analysis/PathSensitive/Store.h +++ b/include/clang/Analysis/PathSensitive/Store.h @@ -23,6 +23,7 @@ namespace clang { typedef const void* Store; +class GRStateManager; class LiveVariables; class Stmt; @@ -36,7 +37,7 @@ public: virtual RVal GetRVal(Store St, LVal LV, QualType T = QualType()) = 0; virtual Store SetRVal(Store St, LVal LV, RVal V) = 0; virtual Store Remove(Store St, LVal LV) = 0; - virtual Store getInitialStore() = 0; + virtual Store getInitialStore(GRStateManager& StateMgr) = 0; virtual Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live, diff --git a/lib/Analysis/BasicStore.cpp b/lib/Analysis/BasicStore.cpp index 64a230975f..15284567e6 100644 --- a/lib/Analysis/BasicStore.cpp +++ b/lib/Analysis/BasicStore.cpp @@ -13,6 +13,7 @@ #include "clang/Analysis/Analyses/LiveVariables.h" #include "clang/Analysis/PathSensitive/BasicStore.h" +#include "clang/Analysis/PathSensitive/GRState.h" #include "llvm/ADT/ImmutableMap.h" #include "llvm/Support/Compiler.h" @@ -32,9 +33,7 @@ public: virtual Store SetRVal(Store St, LVal LV, RVal V); virtual Store Remove(Store St, LVal LV); - virtual Store getInitialStore() { - return VBFactory.GetEmptyMap().getRoot(); - } + virtual Store getInitialStore(GRStateManager& StateMgr); virtual Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live, @@ -200,3 +199,38 @@ Store BasicStoreManager::RemoveDeadBindings(Store store, return store; } + +Store BasicStoreManager::getInitialStore(GRStateManager& StateMgr) { + // 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; + LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); + + Store St = VBFactory.GetEmptyMap().getRoot(); + + for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { + ScopedDecl* SD = const_cast(I->first); + + if (VarDecl* VD = dyn_cast(SD)) { + // Punt on static variables for now. + if (VD->getStorageClass() == VarDecl::Static) + continue; + + // Only handle pointers and integers for now. + QualType T = VD->getType(); + if (LVal::IsLValType(T) || T->isIntegerType()) { + // Initialize globals and parameters to symbolic values. + // Initialize local variables to undefined. + RVal X = (VD->hasGlobalStorage() || isa(VD) || + isa(VD)) + ? RVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) + : UndefinedVal(); + + St = SetRVal(St, lval::DeclVal(VD), X); + } + } + } + return St; +} diff --git a/lib/Analysis/GRExprEngine.cpp b/lib/Analysis/GRExprEngine.cpp index 18c5a5825e..7433509c85 100644 --- a/lib/Analysis/GRExprEngine.cpp +++ b/lib/Analysis/GRExprEngine.cpp @@ -121,7 +121,7 @@ GRExprEngine::GRExprEngine(CFG& cfg, Decl& CD, ASTContext& Ctx, Liveness(L), Builder(NULL), StateMgr(G.getContext(), CreateBasicStoreManager(G.getAllocator()), - G.getAllocator(), G.getCFG()), + G.getAllocator(), G.getCFG(), L), SymMgr(StateMgr.getSymbolManager()), CurrentStmt(NULL), NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL), @@ -189,47 +189,7 @@ void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) { } const GRState* GRExprEngine::getInitialState() { - - // 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; - LVDataTy& D = Liveness.getAnalysisData(); - - GRState StateImpl = *StateMgr.getInitialState(); - - for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { - - ScopedDecl *SD = const_cast(I->first); - if (VarDecl* VD = dyn_cast(SD)) { - // Punt on static variables for now. - if (VD->getStorageClass() == VarDecl::Static) - continue; - - // Only handle pointers and integers for now. - QualType T = VD->getType(); - if (!(LVal::IsLValType(T) || T->isIntegerType())) - continue; - - // Initialize globals and parameters to symbolic values. - // Initialize local variables to undefined. - RVal X = (VD->hasGlobalStorage() || isa(VD) || - isa(VD)) - ? RVal::GetSymbolValue(SymMgr, VD) - : UndefinedVal(); - - StateMgr.SetRVal(StateImpl, lval::DeclVal(VD), X); - - } else if (ImplicitParamDecl *IPD = dyn_cast(SD)) { - RVal X = RVal::GetSymbolValue(SymMgr, IPD); - StateMgr.SetRVal(StateImpl, lval::DeclVal(IPD), X); - } - - - } - - return StateMgr.getPersistentState(StateImpl); + return StateMgr.getInitialState(); } //===----------------------------------------------------------------------===// diff --git a/lib/Analysis/GRState.cpp b/lib/Analysis/GRState.cpp index cd81c57f90..e4022a2663 100644 --- a/lib/Analysis/GRState.cpp +++ b/lib/Analysis/GRState.cpp @@ -211,9 +211,10 @@ const GRState* GRStateManager::AddEQ(const GRState* St, SymbolID sym, const GRState* GRStateManager::getInitialState() { - GRState StateImpl(EnvMgr.getInitialEnvironment(), StMgr->getInitialStore(), + GRState StateImpl(EnvMgr.getInitialEnvironment(), + StMgr->getInitialStore(*this), GDMFactory.GetEmptyMap()); - + return getPersistentState(StateImpl); } -- 2.50.1