2011-02-15 02:13:31 +08:00
|
|
|
//===--- CheckerManager.cpp - Static Analyzer Checker Manager -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Defines the Static Analyzer Checker Manager.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2011-08-13 07:04:46 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-23 01:30:38 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2012-07-27 05:39:41 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2011-02-23 01:30:38 +08:00
|
|
|
#include "clang/Analysis/ProgramPoint.h"
|
2011-02-18 05:39:24 +08:00
|
|
|
#include "clang/AST/DeclBase.h"
|
2011-02-15 02:13:31 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
2011-03-01 03:49:17 +08:00
|
|
|
bool CheckerManager::hasPathSensitiveCheckers() const {
|
|
|
|
return !StmtCheckers.empty() ||
|
|
|
|
!PreObjCMessageCheckers.empty() ||
|
|
|
|
!PostObjCMessageCheckers.empty() ||
|
2012-07-03 03:28:16 +08:00
|
|
|
!PreCallCheckers.empty() ||
|
|
|
|
!PostCallCheckers.empty() ||
|
2011-03-01 03:49:17 +08:00
|
|
|
!LocationCheckers.empty() ||
|
|
|
|
!BindCheckers.empty() ||
|
|
|
|
!EndAnalysisCheckers.empty() ||
|
|
|
|
!EndPathCheckers.empty() ||
|
|
|
|
!BranchConditionCheckers.empty() ||
|
|
|
|
!LiveSymbolsCheckers.empty() ||
|
|
|
|
!DeadSymbolsCheckers.empty() ||
|
|
|
|
!RegionChangesCheckers.empty() ||
|
|
|
|
!EvalAssumeCheckers.empty() ||
|
2012-10-13 13:05:13 +08:00
|
|
|
!EvalCallCheckers.empty();
|
2011-03-01 03:49:17 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 01:36:09 +08:00
|
|
|
void CheckerManager::finishedCheckerRegistration() {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Make sure that for every event that has listeners, there is at least
|
|
|
|
// one dispatcher registered for it.
|
|
|
|
for (llvm::DenseMap<EventTag, EventInfo>::iterator
|
|
|
|
I = Events.begin(), E = Events.end(); I != E; ++I)
|
|
|
|
assert(I->second.HasDispatcher && "No dispatcher registered for an event");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Functions for running checkers for AST traversing..
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-18 05:39:24 +08:00
|
|
|
void CheckerManager::runCheckersOnASTDecl(const Decl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) {
|
|
|
|
assert(D);
|
|
|
|
|
|
|
|
unsigned DeclKind = D->getKind();
|
|
|
|
CachedDeclCheckers *checkers = 0;
|
|
|
|
CachedDeclCheckersMapTy::iterator CCI = CachedDeclCheckersMap.find(DeclKind);
|
|
|
|
if (CCI != CachedDeclCheckersMap.end()) {
|
|
|
|
checkers = &(CCI->second);
|
|
|
|
} else {
|
|
|
|
// Find the checkers that should run for this Decl and cache them.
|
|
|
|
checkers = &CachedDeclCheckersMap[DeclKind];
|
|
|
|
for (unsigned i = 0, e = DeclCheckers.size(); i != e; ++i) {
|
|
|
|
DeclCheckerInfo &info = DeclCheckers[i];
|
|
|
|
if (info.IsForDeclFn(D))
|
2011-02-23 01:30:38 +08:00
|
|
|
checkers->push_back(info.CheckFn);
|
2011-02-18 05:39:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(checkers);
|
|
|
|
for (CachedDeclCheckers::iterator
|
2011-02-23 01:30:38 +08:00
|
|
|
I = checkers->begin(), E = checkers->end(); I != E; ++I)
|
|
|
|
(*I)(D, mgr, BR);
|
2011-02-18 05:39:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerManager::runCheckersOnASTBody(const Decl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) {
|
|
|
|
assert(D && D->hasBody());
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
for (unsigned i = 0, e = BodyCheckers.size(); i != e; ++i)
|
|
|
|
BodyCheckers[i](D, mgr, BR);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Functions for running checkers for path-sensitive checking.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
template <typename CHECK_CTX>
|
2011-02-24 03:38:41 +08:00
|
|
|
static void expandGraphWithCheckers(CHECK_CTX checkCtx,
|
|
|
|
ExplodedNodeSet &Dst,
|
2011-02-24 03:38:45 +08:00
|
|
|
const ExplodedNodeSet &Src) {
|
2011-10-25 02:25:58 +08:00
|
|
|
const NodeBuilderContext &BldrCtx = checkCtx.Eng.getBuilderContext();
|
|
|
|
if (Src.empty())
|
|
|
|
return;
|
2011-02-23 01:30:38 +08:00
|
|
|
|
2011-02-24 03:38:39 +08:00
|
|
|
typename CHECK_CTX::CheckersTy::const_iterator
|
|
|
|
I = checkCtx.checkers_begin(), E = checkCtx.checkers_end();
|
|
|
|
if (I == E) {
|
2011-02-23 01:30:38 +08:00
|
|
|
Dst.insert(Src);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-24 03:38:45 +08:00
|
|
|
ExplodedNodeSet Tmp1, Tmp2;
|
|
|
|
const ExplodedNodeSet *PrevSet = &Src;
|
2011-02-23 01:30:38 +08:00
|
|
|
|
2011-02-24 03:38:39 +08:00
|
|
|
for (; I != E; ++I) {
|
2011-02-23 01:30:38 +08:00
|
|
|
ExplodedNodeSet *CurrSet = 0;
|
|
|
|
if (I+1 == E)
|
|
|
|
CurrSet = &Dst;
|
|
|
|
else {
|
2011-02-24 03:38:45 +08:00
|
|
|
CurrSet = (PrevSet == &Tmp1) ? &Tmp2 : &Tmp1;
|
2011-02-23 01:30:38 +08:00
|
|
|
CurrSet->clear();
|
|
|
|
}
|
|
|
|
|
2011-10-25 02:25:58 +08:00
|
|
|
NodeBuilder B(*PrevSet, *CurrSet, BldrCtx);
|
2011-02-23 01:30:38 +08:00
|
|
|
for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end();
|
2011-10-25 02:25:58 +08:00
|
|
|
NI != NE; ++NI) {
|
|
|
|
checkCtx.runChecker(*I, B, *NI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all the produced transitions are sinks, stop.
|
|
|
|
if (CurrSet->empty())
|
|
|
|
return;
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
// Update which NodeSet is the current one.
|
|
|
|
PrevSet = CurrSet;
|
2011-02-18 05:39:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
namespace {
|
|
|
|
struct CheckStmtContext {
|
2011-07-23 18:55:15 +08:00
|
|
|
typedef SmallVectorImpl<CheckerManager::CheckStmtFunc> CheckersTy;
|
2011-02-23 01:30:38 +08:00
|
|
|
bool IsPreVisit;
|
|
|
|
const CheckersTy &Checkers;
|
|
|
|
const Stmt *S;
|
|
|
|
ExprEngine &Eng;
|
2012-07-31 07:39:47 +08:00
|
|
|
bool WasInlined;
|
2011-02-23 01:30:38 +08:00
|
|
|
|
2011-02-24 03:38:39 +08:00
|
|
|
CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
|
|
|
|
CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
CheckStmtContext(bool isPreVisit, const CheckersTy &checkers,
|
2012-03-23 14:26:56 +08:00
|
|
|
const Stmt *s, ExprEngine &eng, bool wasInlined = false)
|
|
|
|
: IsPreVisit(isPreVisit), Checkers(checkers), S(s), Eng(eng),
|
2012-07-31 07:39:47 +08:00
|
|
|
WasInlined(wasInlined) {}
|
2011-02-23 01:30:38 +08:00
|
|
|
|
|
|
|
void runChecker(CheckerManager::CheckStmtFunc checkFn,
|
2011-10-25 02:25:58 +08:00
|
|
|
NodeBuilder &Bldr, ExplodedNode *Pred) {
|
2011-02-23 01:30:38 +08:00
|
|
|
// FIXME: Remove respondsToCallback from CheckerContext;
|
2011-10-08 05:52:33 +08:00
|
|
|
ProgramPoint::Kind K = IsPreVisit ? ProgramPoint::PreStmtKind :
|
|
|
|
ProgramPoint::PostStmtKind;
|
|
|
|
const ProgramPoint &L = ProgramPoint::getProgramPoint(S, K,
|
|
|
|
Pred->getLocationContext(), checkFn.Checker);
|
2012-07-31 07:39:47 +08:00
|
|
|
CheckerContext C(Bldr, Eng, Pred, L, WasInlined);
|
2011-02-23 01:30:38 +08:00
|
|
|
checkFn(S, C);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Run checkers for visiting Stmts.
|
|
|
|
void CheckerManager::runCheckersForStmt(bool isPreVisit,
|
|
|
|
ExplodedNodeSet &Dst,
|
2011-02-24 03:38:45 +08:00
|
|
|
const ExplodedNodeSet &Src,
|
2011-02-23 01:30:38 +08:00
|
|
|
const Stmt *S,
|
2012-03-23 14:26:56 +08:00
|
|
|
ExprEngine &Eng,
|
2012-07-31 07:39:47 +08:00
|
|
|
bool WasInlined) {
|
2011-02-23 01:30:38 +08:00
|
|
|
CheckStmtContext C(isPreVisit, *getCachedStmtCheckersFor(S, isPreVisit),
|
2012-07-31 07:39:47 +08:00
|
|
|
S, Eng, WasInlined);
|
2011-02-24 03:38:41 +08:00
|
|
|
expandGraphWithCheckers(C, Dst, Src);
|
2011-02-23 01:30:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct CheckObjCMessageContext {
|
|
|
|
typedef std::vector<CheckerManager::CheckObjCMessageFunc> CheckersTy;
|
2012-07-31 07:39:47 +08:00
|
|
|
bool IsPreVisit, WasInlined;
|
2011-02-23 01:30:38 +08:00
|
|
|
const CheckersTy &Checkers;
|
2012-07-03 03:28:04 +08:00
|
|
|
const ObjCMethodCall &Msg;
|
2011-02-23 01:30:38 +08:00
|
|
|
ExprEngine &Eng;
|
|
|
|
|
2011-02-24 03:38:39 +08:00
|
|
|
CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
|
|
|
|
CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
CheckObjCMessageContext(bool isPreVisit, const CheckersTy &checkers,
|
2012-07-31 07:39:47 +08:00
|
|
|
const ObjCMethodCall &msg, ExprEngine &eng,
|
|
|
|
bool wasInlined)
|
|
|
|
: IsPreVisit(isPreVisit), WasInlined(wasInlined), Checkers(checkers),
|
|
|
|
Msg(msg), Eng(eng) { }
|
2011-02-23 01:30:38 +08:00
|
|
|
|
|
|
|
void runChecker(CheckerManager::CheckObjCMessageFunc checkFn,
|
2011-10-25 02:25:58 +08:00
|
|
|
NodeBuilder &Bldr, ExplodedNode *Pred) {
|
2012-07-31 04:22:09 +08:00
|
|
|
const ProgramPoint &L = Msg.getProgramPoint(IsPreVisit,checkFn.Checker);
|
2012-07-31 07:39:47 +08:00
|
|
|
CheckerContext C(Bldr, Eng, Pred, L, WasInlined);
|
2011-10-08 05:52:33 +08:00
|
|
|
|
2012-07-31 04:22:09 +08:00
|
|
|
checkFn(*Msg.cloneWithState<ObjCMethodCall>(Pred->getState()), C);
|
2011-02-23 01:30:38 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Run checkers for visiting obj-c messages.
|
|
|
|
void CheckerManager::runCheckersForObjCMessage(bool isPreVisit,
|
|
|
|
ExplodedNodeSet &Dst,
|
2011-02-24 03:38:45 +08:00
|
|
|
const ExplodedNodeSet &Src,
|
2012-07-03 03:28:04 +08:00
|
|
|
const ObjCMethodCall &msg,
|
2012-07-31 07:39:47 +08:00
|
|
|
ExprEngine &Eng,
|
|
|
|
bool WasInlined) {
|
2011-02-23 08:16:07 +08:00
|
|
|
CheckObjCMessageContext C(isPreVisit,
|
|
|
|
isPreVisit ? PreObjCMessageCheckers
|
|
|
|
: PostObjCMessageCheckers,
|
2012-07-31 07:39:47 +08:00
|
|
|
msg, Eng, WasInlined);
|
2011-02-24 03:38:41 +08:00
|
|
|
expandGraphWithCheckers(C, Dst, Src);
|
2011-02-23 01:30:38 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:16 +08:00
|
|
|
namespace {
|
|
|
|
// FIXME: This has all the same signatures as CheckObjCMessageContext.
|
|
|
|
// Is there a way we can merge the two?
|
|
|
|
struct CheckCallContext {
|
|
|
|
typedef std::vector<CheckerManager::CheckCallFunc> CheckersTy;
|
2012-07-31 07:39:47 +08:00
|
|
|
bool IsPreVisit, WasInlined;
|
2012-07-03 03:28:16 +08:00
|
|
|
const CheckersTy &Checkers;
|
|
|
|
const CallEvent &Call;
|
|
|
|
ExprEngine &Eng;
|
|
|
|
|
|
|
|
CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
|
|
|
|
CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
|
|
|
|
|
|
|
|
CheckCallContext(bool isPreVisit, const CheckersTy &checkers,
|
2012-07-31 07:39:47 +08:00
|
|
|
const CallEvent &call, ExprEngine &eng,
|
|
|
|
bool wasInlined)
|
|
|
|
: IsPreVisit(isPreVisit), WasInlined(wasInlined), Checkers(checkers),
|
|
|
|
Call(call), Eng(eng) { }
|
2012-07-03 03:28:16 +08:00
|
|
|
|
|
|
|
void runChecker(CheckerManager::CheckCallFunc checkFn,
|
|
|
|
NodeBuilder &Bldr, ExplodedNode *Pred) {
|
2012-07-31 04:22:09 +08:00
|
|
|
const ProgramPoint &L = Call.getProgramPoint(IsPreVisit,checkFn.Checker);
|
2012-07-31 07:39:47 +08:00
|
|
|
CheckerContext C(Bldr, Eng, Pred, L, WasInlined);
|
2012-07-03 03:28:16 +08:00
|
|
|
|
2012-07-31 04:22:09 +08:00
|
|
|
checkFn(*Call.cloneWithState(Pred->getState()), C);
|
2012-07-03 03:28:16 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Run checkers for visiting an abstract call event.
|
|
|
|
void CheckerManager::runCheckersForCallEvent(bool isPreVisit,
|
|
|
|
ExplodedNodeSet &Dst,
|
|
|
|
const ExplodedNodeSet &Src,
|
|
|
|
const CallEvent &Call,
|
2012-07-31 07:39:47 +08:00
|
|
|
ExprEngine &Eng,
|
|
|
|
bool WasInlined) {
|
2012-07-03 03:28:16 +08:00
|
|
|
CheckCallContext C(isPreVisit,
|
|
|
|
isPreVisit ? PreCallCheckers
|
|
|
|
: PostCallCheckers,
|
2012-07-31 07:39:47 +08:00
|
|
|
Call, Eng, WasInlined);
|
2012-07-03 03:28:16 +08:00
|
|
|
expandGraphWithCheckers(C, Dst, Src);
|
|
|
|
}
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
namespace {
|
|
|
|
struct CheckLocationContext {
|
|
|
|
typedef std::vector<CheckerManager::CheckLocationFunc> CheckersTy;
|
|
|
|
const CheckersTy &Checkers;
|
|
|
|
SVal Loc;
|
|
|
|
bool IsLoad;
|
2012-04-07 06:10:18 +08:00
|
|
|
const Stmt *NodeEx; /* Will become a CFGStmt */
|
|
|
|
const Stmt *BoundEx;
|
2011-02-23 01:30:38 +08:00
|
|
|
ExprEngine &Eng;
|
|
|
|
|
2011-02-24 03:38:39 +08:00
|
|
|
CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
|
|
|
|
CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
CheckLocationContext(const CheckersTy &checkers,
|
2012-04-07 06:10:18 +08:00
|
|
|
SVal loc, bool isLoad, const Stmt *NodeEx,
|
|
|
|
const Stmt *BoundEx,
|
|
|
|
ExprEngine &eng)
|
|
|
|
: Checkers(checkers), Loc(loc), IsLoad(isLoad), NodeEx(NodeEx),
|
|
|
|
BoundEx(BoundEx), Eng(eng) {}
|
2011-02-23 01:30:38 +08:00
|
|
|
|
|
|
|
void runChecker(CheckerManager::CheckLocationFunc checkFn,
|
2011-10-25 02:25:58 +08:00
|
|
|
NodeBuilder &Bldr, ExplodedNode *Pred) {
|
2011-10-08 05:52:33 +08:00
|
|
|
ProgramPoint::Kind K = IsLoad ? ProgramPoint::PreLoadKind :
|
|
|
|
ProgramPoint::PreStoreKind;
|
2012-04-07 06:10:18 +08:00
|
|
|
const ProgramPoint &L =
|
|
|
|
ProgramPoint::getProgramPoint(NodeEx, K,
|
|
|
|
Pred->getLocationContext(),
|
|
|
|
checkFn.Checker);
|
2011-10-26 03:57:06 +08:00
|
|
|
CheckerContext C(Bldr, Eng, Pred, L);
|
2012-04-07 06:10:18 +08:00
|
|
|
checkFn(Loc, IsLoad, BoundEx, C);
|
2011-02-23 01:30:38 +08:00
|
|
|
}
|
|
|
|
};
|
2011-02-18 05:39:24 +08:00
|
|
|
}
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
/// \brief Run checkers for load/store of a location.
|
2011-10-25 02:26:19 +08:00
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
void CheckerManager::runCheckersForLocation(ExplodedNodeSet &Dst,
|
2011-02-24 03:38:45 +08:00
|
|
|
const ExplodedNodeSet &Src,
|
2011-02-23 01:30:38 +08:00
|
|
|
SVal location, bool isLoad,
|
2012-04-07 06:10:18 +08:00
|
|
|
const Stmt *NodeEx,
|
|
|
|
const Stmt *BoundEx,
|
|
|
|
ExprEngine &Eng) {
|
|
|
|
CheckLocationContext C(LocationCheckers, location, isLoad, NodeEx,
|
|
|
|
BoundEx, Eng);
|
2011-02-24 03:38:41 +08:00
|
|
|
expandGraphWithCheckers(C, Dst, Src);
|
2011-02-18 05:39:24 +08:00
|
|
|
}
|
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
namespace {
|
|
|
|
struct CheckBindContext {
|
|
|
|
typedef std::vector<CheckerManager::CheckBindFunc> CheckersTy;
|
|
|
|
const CheckersTy &Checkers;
|
|
|
|
SVal Loc;
|
|
|
|
SVal Val;
|
|
|
|
const Stmt *S;
|
|
|
|
ExprEngine &Eng;
|
2012-08-25 09:06:23 +08:00
|
|
|
const ProgramPoint &PP;
|
2011-02-28 09:26:35 +08:00
|
|
|
|
|
|
|
CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
|
|
|
|
CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
|
|
|
|
|
|
|
|
CheckBindContext(const CheckersTy &checkers,
|
2011-10-25 02:26:19 +08:00
|
|
|
SVal loc, SVal val, const Stmt *s, ExprEngine &eng,
|
2012-08-25 09:06:23 +08:00
|
|
|
const ProgramPoint &pp)
|
|
|
|
: Checkers(checkers), Loc(loc), Val(val), S(s), Eng(eng), PP(pp) {}
|
2011-02-28 09:26:35 +08:00
|
|
|
|
|
|
|
void runChecker(CheckerManager::CheckBindFunc checkFn,
|
2011-10-25 02:25:58 +08:00
|
|
|
NodeBuilder &Bldr, ExplodedNode *Pred) {
|
2012-08-25 09:06:23 +08:00
|
|
|
const ProgramPoint &L = PP.withTag(checkFn.Checker);
|
2011-10-26 03:57:06 +08:00
|
|
|
CheckerContext C(Bldr, Eng, Pred, L);
|
2011-10-08 05:52:33 +08:00
|
|
|
|
2011-10-06 08:43:15 +08:00
|
|
|
checkFn(Loc, Val, S, C);
|
2011-02-28 09:26:35 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Run checkers for binding of a value to a location.
|
|
|
|
void CheckerManager::runCheckersForBind(ExplodedNodeSet &Dst,
|
|
|
|
const ExplodedNodeSet &Src,
|
|
|
|
SVal location, SVal val,
|
2011-10-25 02:26:19 +08:00
|
|
|
const Stmt *S, ExprEngine &Eng,
|
2012-08-25 09:06:23 +08:00
|
|
|
const ProgramPoint &PP) {
|
|
|
|
CheckBindContext C(BindCheckers, location, val, S, Eng, PP);
|
2011-02-28 09:26:35 +08:00
|
|
|
expandGraphWithCheckers(C, Dst, Src);
|
|
|
|
}
|
|
|
|
|
2011-02-23 15:19:23 +08:00
|
|
|
void CheckerManager::runCheckersForEndAnalysis(ExplodedGraph &G,
|
|
|
|
BugReporter &BR,
|
|
|
|
ExprEngine &Eng) {
|
|
|
|
for (unsigned i = 0, e = EndAnalysisCheckers.size(); i != e; ++i)
|
|
|
|
EndAnalysisCheckers[i](G, BR, Eng);
|
|
|
|
}
|
|
|
|
|
2011-02-24 05:04:54 +08:00
|
|
|
/// \brief Run checkers for end of path.
|
2011-10-26 03:56:48 +08:00
|
|
|
// Note, We do not chain the checker output (like in expandGraphWithCheckers)
|
|
|
|
// for this callback since end of path nodes are expected to be final.
|
|
|
|
void CheckerManager::runCheckersForEndPath(NodeBuilderContext &BC,
|
|
|
|
ExplodedNodeSet &Dst,
|
2011-02-24 05:04:54 +08:00
|
|
|
ExprEngine &Eng) {
|
2011-10-26 03:56:48 +08:00
|
|
|
ExplodedNode *Pred = BC.Pred;
|
|
|
|
|
|
|
|
// We define the builder outside of the loop bacause if at least one checkers
|
|
|
|
// creates a sucsessor for Pred, we do not need to generate an
|
|
|
|
// autotransition for it.
|
|
|
|
NodeBuilder Bldr(Pred, Dst, BC);
|
2011-02-24 05:04:54 +08:00
|
|
|
for (unsigned i = 0, e = EndPathCheckers.size(); i != e; ++i) {
|
2011-10-26 03:56:48 +08:00
|
|
|
CheckEndPathFunc checkFn = EndPathCheckers[i];
|
|
|
|
|
|
|
|
const ProgramPoint &L = BlockEntrance(BC.Block,
|
|
|
|
Pred->getLocationContext(),
|
|
|
|
checkFn.Checker);
|
2011-10-26 03:57:06 +08:00
|
|
|
CheckerContext C(Bldr, Eng, Pred, L);
|
2011-10-26 03:56:48 +08:00
|
|
|
checkFn(C);
|
2011-02-24 05:04:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-26 03:56:54 +08:00
|
|
|
namespace {
|
|
|
|
struct CheckBranchConditionContext {
|
|
|
|
typedef std::vector<CheckerManager::CheckBranchConditionFunc> CheckersTy;
|
|
|
|
const CheckersTy &Checkers;
|
|
|
|
const Stmt *Condition;
|
|
|
|
ExprEngine &Eng;
|
|
|
|
|
|
|
|
CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
|
|
|
|
CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
|
|
|
|
|
|
|
|
CheckBranchConditionContext(const CheckersTy &checkers,
|
|
|
|
const Stmt *Cond, ExprEngine &eng)
|
|
|
|
: Checkers(checkers), Condition(Cond), Eng(eng) {}
|
|
|
|
|
|
|
|
void runChecker(CheckerManager::CheckBranchConditionFunc checkFn,
|
|
|
|
NodeBuilder &Bldr, ExplodedNode *Pred) {
|
|
|
|
ProgramPoint L = PostCondition(Condition, Pred->getLocationContext(),
|
|
|
|
checkFn.Checker);
|
2011-10-26 03:57:06 +08:00
|
|
|
CheckerContext C(Bldr, Eng, Pred, L);
|
2011-10-26 03:56:54 +08:00
|
|
|
checkFn(Condition, C);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:27:33 +08:00
|
|
|
/// \brief Run checkers for branch condition.
|
2011-10-26 03:56:54 +08:00
|
|
|
void CheckerManager::runCheckersForBranchCondition(const Stmt *Condition,
|
|
|
|
ExplodedNodeSet &Dst,
|
2011-10-19 07:06:44 +08:00
|
|
|
ExplodedNode *Pred,
|
2011-02-28 09:27:33 +08:00
|
|
|
ExprEngine &Eng) {
|
2011-10-26 03:56:54 +08:00
|
|
|
ExplodedNodeSet Src;
|
|
|
|
Src.insert(Pred);
|
|
|
|
CheckBranchConditionContext C(BranchConditionCheckers, Condition, Eng);
|
|
|
|
expandGraphWithCheckers(C, Dst, Src);
|
2011-02-28 09:27:33 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
/// \brief Run checkers for live symbols.
|
2012-01-27 05:29:00 +08:00
|
|
|
void CheckerManager::runCheckersForLiveSymbols(ProgramStateRef state,
|
2011-02-24 09:05:30 +08:00
|
|
|
SymbolReaper &SymReaper) {
|
|
|
|
for (unsigned i = 0, e = LiveSymbolsCheckers.size(); i != e; ++i)
|
|
|
|
LiveSymbolsCheckers[i](state, SymReaper);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct CheckDeadSymbolsContext {
|
|
|
|
typedef std::vector<CheckerManager::CheckDeadSymbolsFunc> CheckersTy;
|
|
|
|
const CheckersTy &Checkers;
|
|
|
|
SymbolReaper &SR;
|
|
|
|
const Stmt *S;
|
|
|
|
ExprEngine &Eng;
|
2012-04-21 05:59:08 +08:00
|
|
|
ProgramPoint::Kind ProgarmPointKind;
|
2011-02-24 09:05:30 +08:00
|
|
|
|
|
|
|
CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
|
|
|
|
CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
|
|
|
|
|
|
|
|
CheckDeadSymbolsContext(const CheckersTy &checkers, SymbolReaper &sr,
|
2012-04-21 05:59:08 +08:00
|
|
|
const Stmt *s, ExprEngine &eng,
|
|
|
|
ProgramPoint::Kind K)
|
|
|
|
: Checkers(checkers), SR(sr), S(s), Eng(eng), ProgarmPointKind(K) { }
|
2011-02-24 09:05:30 +08:00
|
|
|
|
|
|
|
void runChecker(CheckerManager::CheckDeadSymbolsFunc checkFn,
|
2011-10-25 02:25:58 +08:00
|
|
|
NodeBuilder &Bldr, ExplodedNode *Pred) {
|
2012-04-21 05:59:08 +08:00
|
|
|
const ProgramPoint &L = ProgramPoint::getProgramPoint(S, ProgarmPointKind,
|
2011-10-08 05:52:33 +08:00
|
|
|
Pred->getLocationContext(), checkFn.Checker);
|
2011-10-26 03:57:06 +08:00
|
|
|
CheckerContext C(Bldr, Eng, Pred, L);
|
2011-10-08 05:52:33 +08:00
|
|
|
|
2012-04-21 05:59:08 +08:00
|
|
|
// Note, do not pass the statement to the checkers without letting them
|
|
|
|
// differentiate if we ran remove dead bindings before or after the
|
|
|
|
// statement.
|
2011-02-24 09:05:30 +08:00
|
|
|
checkFn(SR, C);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Run checkers for dead symbols.
|
|
|
|
void CheckerManager::runCheckersForDeadSymbols(ExplodedNodeSet &Dst,
|
|
|
|
const ExplodedNodeSet &Src,
|
|
|
|
SymbolReaper &SymReaper,
|
|
|
|
const Stmt *S,
|
2012-04-21 05:59:08 +08:00
|
|
|
ExprEngine &Eng,
|
|
|
|
ProgramPoint::Kind K) {
|
|
|
|
CheckDeadSymbolsContext C(DeadSymbolsCheckers, SymReaper, S, Eng, K);
|
2011-02-24 09:05:30 +08:00
|
|
|
expandGraphWithCheckers(C, Dst, Src);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief True if at least one checker wants to check region changes.
|
2012-01-27 05:29:00 +08:00
|
|
|
bool CheckerManager::wantsRegionChangeUpdate(ProgramStateRef state) {
|
2011-02-24 09:05:30 +08:00
|
|
|
for (unsigned i = 0, e = RegionChangesCheckers.size(); i != e; ++i)
|
|
|
|
if (RegionChangesCheckers[i].WantUpdateFn(state))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Run checkers for region changes.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef
|
|
|
|
CheckerManager::runCheckersForRegionChanges(ProgramStateRef state,
|
2011-05-03 03:42:42 +08:00
|
|
|
const StoreManager::InvalidatedSymbols *invalidated,
|
2011-08-28 06:51:26 +08:00
|
|
|
ArrayRef<const MemRegion *> ExplicitRegions,
|
2012-02-15 05:55:24 +08:00
|
|
|
ArrayRef<const MemRegion *> Regions,
|
2012-07-03 03:27:35 +08:00
|
|
|
const CallEvent *Call) {
|
2011-02-24 09:05:30 +08:00
|
|
|
for (unsigned i = 0, e = RegionChangesCheckers.size(); i != e; ++i) {
|
|
|
|
// If any checker declares the state infeasible (or if it starts that way),
|
|
|
|
// bail out.
|
|
|
|
if (!state)
|
|
|
|
return NULL;
|
2011-08-28 06:51:26 +08:00
|
|
|
state = RegionChangesCheckers[i].CheckFn(state, invalidated,
|
2012-02-15 05:55:24 +08:00
|
|
|
ExplicitRegions, Regions, Call);
|
2011-02-24 09:05:30 +08:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
/// \brief Run checkers for handling assumptions on symbolic values.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef
|
|
|
|
CheckerManager::runCheckersForEvalAssume(ProgramStateRef state,
|
2011-02-28 09:26:35 +08:00
|
|
|
SVal Cond, bool Assumption) {
|
|
|
|
for (unsigned i = 0, e = EvalAssumeCheckers.size(); i != e; ++i) {
|
|
|
|
// If any checker declares the state infeasible (or if it starts that way),
|
|
|
|
// bail out.
|
|
|
|
if (!state)
|
|
|
|
return NULL;
|
|
|
|
state = EvalAssumeCheckers[i](state, Cond, Assumption);
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2011-02-24 03:38:39 +08:00
|
|
|
/// \brief Run checkers for evaluating a call.
|
|
|
|
/// Only one checker will evaluate the call.
|
|
|
|
void CheckerManager::runCheckersForEvalCall(ExplodedNodeSet &Dst,
|
|
|
|
const ExplodedNodeSet &Src,
|
2012-08-14 07:46:05 +08:00
|
|
|
const CallEvent &Call,
|
2012-07-03 03:28:09 +08:00
|
|
|
ExprEngine &Eng) {
|
2012-08-14 07:46:05 +08:00
|
|
|
const CallExpr *CE = cast<CallExpr>(Call.getOriginExpr());
|
2011-02-24 03:38:39 +08:00
|
|
|
for (ExplodedNodeSet::iterator
|
|
|
|
NI = Src.begin(), NE = Src.end(); NI != NE; ++NI) {
|
|
|
|
ExplodedNode *Pred = *NI;
|
|
|
|
bool anyEvaluated = false;
|
2011-10-06 07:37:30 +08:00
|
|
|
|
2011-10-25 02:25:58 +08:00
|
|
|
ExplodedNodeSet checkDst;
|
|
|
|
NodeBuilder B(Pred, checkDst, Eng.getBuilderContext());
|
2012-10-13 13:05:13 +08:00
|
|
|
|
|
|
|
// Check if any of the EvalCall callbacks can evaluate the call.
|
2011-02-24 03:38:39 +08:00
|
|
|
for (std::vector<EvalCallFunc>::iterator
|
|
|
|
EI = EvalCallCheckers.begin(), EE = EvalCallCheckers.end();
|
|
|
|
EI != EE; ++EI) {
|
2011-10-08 05:52:33 +08:00
|
|
|
ProgramPoint::Kind K = ProgramPoint::PostStmtKind;
|
|
|
|
const ProgramPoint &L = ProgramPoint::getProgramPoint(CE, K,
|
|
|
|
Pred->getLocationContext(), EI->Checker);
|
2011-10-12 01:29:59 +08:00
|
|
|
bool evaluated = false;
|
|
|
|
{ // CheckerContext generates transitions(populates checkDest) on
|
|
|
|
// destruction, so introduce the scope to make sure it gets properly
|
|
|
|
// populated.
|
2011-10-26 03:57:06 +08:00
|
|
|
CheckerContext C(B, Eng, Pred, L);
|
2011-10-12 01:29:59 +08:00
|
|
|
evaluated = (*EI)(CE, C);
|
|
|
|
}
|
2011-02-24 03:38:39 +08:00
|
|
|
assert(!(evaluated && anyEvaluated)
|
|
|
|
&& "There are more than one checkers evaluating the call");
|
|
|
|
if (evaluated) {
|
|
|
|
anyEvaluated = true;
|
|
|
|
Dst.insert(checkDst);
|
|
|
|
#ifdef NDEBUG
|
|
|
|
break; // on release don't check that no other checker also evals.
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-06 07:37:30 +08:00
|
|
|
// If none of the checkers evaluated the call, ask ExprEngine to handle it.
|
2012-07-20 07:38:13 +08:00
|
|
|
if (!anyEvaluated) {
|
|
|
|
NodeBuilder B(Pred, Dst, Eng.getBuilderContext());
|
|
|
|
Eng.defaultEvalCall(B, Pred, Call);
|
|
|
|
}
|
2011-02-24 03:38:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 11:41:17 +08:00
|
|
|
/// \brief Run checkers for the entire Translation Unit.
|
|
|
|
void CheckerManager::runCheckersOnEndOfTranslationUnit(
|
|
|
|
const TranslationUnitDecl *TU,
|
|
|
|
AnalysisManager &mgr,
|
|
|
|
BugReporter &BR) {
|
|
|
|
for (unsigned i = 0, e = EndOfTranslationUnitCheckers.size(); i != e; ++i)
|
|
|
|
EndOfTranslationUnitCheckers[i](TU, mgr, BR);
|
|
|
|
}
|
|
|
|
|
2011-08-29 03:11:56 +08:00
|
|
|
void CheckerManager::runCheckersForPrintState(raw_ostream &Out,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef State,
|
2011-08-29 03:11:56 +08:00
|
|
|
const char *NL, const char *Sep) {
|
|
|
|
for (llvm::DenseMap<CheckerTag, CheckerRef>::iterator
|
|
|
|
I = CheckerTags.begin(), E = CheckerTags.end(); I != E; ++I)
|
|
|
|
I->second->printState(Out, State, NL, Sep);
|
|
|
|
}
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Internal registration functions for AST traversing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void CheckerManager::_registerForDecl(CheckDeclFunc checkfn,
|
|
|
|
HandlesDeclFunc isForDeclFn) {
|
|
|
|
DeclCheckerInfo info = { checkfn, isForDeclFn };
|
|
|
|
DeclCheckers.push_back(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerManager::_registerForBody(CheckDeclFunc checkfn) {
|
|
|
|
BodyCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Internal registration functions for path-sensitive checking.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void CheckerManager::_registerForPreStmt(CheckStmtFunc checkfn,
|
|
|
|
HandlesStmtFunc isForStmtFn) {
|
|
|
|
StmtCheckerInfo info = { checkfn, isForStmtFn, /*IsPreVisit*/true };
|
|
|
|
StmtCheckers.push_back(info);
|
|
|
|
}
|
|
|
|
void CheckerManager::_registerForPostStmt(CheckStmtFunc checkfn,
|
|
|
|
HandlesStmtFunc isForStmtFn) {
|
|
|
|
StmtCheckerInfo info = { checkfn, isForStmtFn, /*IsPreVisit*/false };
|
|
|
|
StmtCheckers.push_back(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerManager::_registerForPreObjCMessage(CheckObjCMessageFunc checkfn) {
|
|
|
|
PreObjCMessageCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
void CheckerManager::_registerForPostObjCMessage(CheckObjCMessageFunc checkfn) {
|
|
|
|
PostObjCMessageCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:16 +08:00
|
|
|
void CheckerManager::_registerForPreCall(CheckCallFunc checkfn) {
|
|
|
|
PreCallCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
void CheckerManager::_registerForPostCall(CheckCallFunc checkfn) {
|
|
|
|
PostCallCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
void CheckerManager::_registerForLocation(CheckLocationFunc checkfn) {
|
|
|
|
LocationCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
void CheckerManager::_registerForBind(CheckBindFunc checkfn) {
|
|
|
|
BindCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
2011-02-23 15:19:23 +08:00
|
|
|
void CheckerManager::_registerForEndAnalysis(CheckEndAnalysisFunc checkfn) {
|
|
|
|
EndAnalysisCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
2011-02-24 05:04:54 +08:00
|
|
|
void CheckerManager::_registerForEndPath(CheckEndPathFunc checkfn) {
|
|
|
|
EndPathCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:27:33 +08:00
|
|
|
void CheckerManager::_registerForBranchCondition(
|
|
|
|
CheckBranchConditionFunc checkfn) {
|
|
|
|
BranchConditionCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CheckerManager::_registerForLiveSymbols(CheckLiveSymbolsFunc checkfn) {
|
|
|
|
LiveSymbolsCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerManager::_registerForDeadSymbols(CheckDeadSymbolsFunc checkfn) {
|
|
|
|
DeadSymbolsCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerManager::_registerForRegionChanges(CheckRegionChangesFunc checkfn,
|
|
|
|
WantsRegionChangeUpdateFunc wantUpdateFn) {
|
|
|
|
RegionChangesCheckerInfo info = {checkfn, wantUpdateFn};
|
|
|
|
RegionChangesCheckers.push_back(info);
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:26:35 +08:00
|
|
|
void CheckerManager::_registerForEvalAssume(EvalAssumeFunc checkfn) {
|
|
|
|
EvalAssumeCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
2011-02-24 03:38:39 +08:00
|
|
|
void CheckerManager::_registerForEvalCall(EvalCallFunc checkfn) {
|
|
|
|
EvalCallCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
2011-05-05 11:41:17 +08:00
|
|
|
void CheckerManager::_registerForEndOfTranslationUnit(
|
|
|
|
CheckEndOfTranslationUnit checkfn) {
|
|
|
|
EndOfTranslationUnitCheckers.push_back(checkfn);
|
|
|
|
}
|
|
|
|
|
2011-02-23 01:30:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Implementation details.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
CheckerManager::CachedStmtCheckers *
|
|
|
|
CheckerManager::getCachedStmtCheckersFor(const Stmt *S, bool isPreVisit) {
|
|
|
|
assert(S);
|
|
|
|
|
|
|
|
CachedStmtCheckersKey key(S->getStmtClass(), isPreVisit);
|
|
|
|
CachedStmtCheckers *checkers = 0;
|
|
|
|
CachedStmtCheckersMapTy::iterator CCI = CachedStmtCheckersMap.find(key);
|
|
|
|
if (CCI != CachedStmtCheckersMap.end()) {
|
|
|
|
checkers = &(CCI->second);
|
|
|
|
} else {
|
|
|
|
// Find the checkers that should run for this Stmt and cache them.
|
|
|
|
checkers = &CachedStmtCheckersMap[key];
|
|
|
|
for (unsigned i = 0, e = StmtCheckers.size(); i != e; ++i) {
|
|
|
|
StmtCheckerInfo &info = StmtCheckers[i];
|
|
|
|
if (info.IsPreVisit == isPreVisit && info.IsForStmtFn(S))
|
|
|
|
checkers->push_back(info.CheckFn);
|
|
|
|
}
|
2011-02-18 05:39:24 +08:00
|
|
|
}
|
2011-02-23 01:30:38 +08:00
|
|
|
|
|
|
|
assert(checkers);
|
|
|
|
return checkers;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckerManager::~CheckerManager() {
|
|
|
|
for (unsigned i = 0, e = CheckerDtors.size(); i != e; ++i)
|
|
|
|
CheckerDtors[i]();
|
2011-02-18 05:39:24 +08:00
|
|
|
}
|