2008-11-13 03:24:17 +08:00
|
|
|
//==- GRCoreEngine.cpp - Path-Sensitive Dataflow Engine ------------*- C++ -*-//
|
2009-09-09 23:08:12 +08:00
|
|
|
//
|
2008-01-15 07:24:37 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a generic engine for intraprocedural, path-sensitive,
|
|
|
|
// dataflow analysis via graph reachability engine.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-14 01:41:41 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRCoreEngine.h"
|
2009-08-06 18:00:15 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
|
2008-01-15 07:24:37 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include <vector>
|
2008-12-17 06:13:33 +08:00
|
|
|
#include <queue>
|
2008-01-15 07:24:37 +08:00
|
|
|
|
|
|
|
using llvm::cast;
|
|
|
|
using llvm::isa;
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-12-17 06:13:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Worklist classes for exploration of reachable states.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
namespace {
|
2009-07-15 17:04:01 +08:00
|
|
|
class VISIBILITY_HIDDEN DFS : public GRWorkList {
|
2008-01-15 07:24:37 +08:00
|
|
|
llvm::SmallVector<GRWorkListUnit,20> Stack;
|
|
|
|
public:
|
|
|
|
virtual bool hasWork() const {
|
|
|
|
return !Stack.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Enqueue(const GRWorkListUnit& U) {
|
|
|
|
Stack.push_back(U);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual GRWorkListUnit Dequeue() {
|
|
|
|
assert (!Stack.empty());
|
|
|
|
const GRWorkListUnit& U = Stack.back();
|
|
|
|
Stack.pop_back(); // This technically "invalidates" U, but we are fine.
|
|
|
|
return U;
|
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-02 06:18:46 +08:00
|
|
|
class VISIBILITY_HIDDEN BFS : public GRWorkList {
|
|
|
|
std::queue<GRWorkListUnit> Queue;
|
|
|
|
public:
|
|
|
|
virtual bool hasWork() const {
|
|
|
|
return !Queue.empty();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-02 06:18:46 +08:00
|
|
|
virtual void Enqueue(const GRWorkListUnit& U) {
|
|
|
|
Queue.push(U);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-02 06:18:46 +08:00
|
|
|
virtual GRWorkListUnit Dequeue() {
|
|
|
|
// Don't use const reference. The subsequent pop_back() might make it
|
|
|
|
// unsafe.
|
2009-09-09 23:08:12 +08:00
|
|
|
GRWorkListUnit U = Queue.front();
|
2009-05-02 06:18:46 +08:00
|
|
|
Queue.pop();
|
|
|
|
return U;
|
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-01-17 02:18:48 +08:00
|
|
|
// Place the dstor for GRWorkList here because it contains virtual member
|
|
|
|
// functions, and we the code for the dstor generated in one compilation unit.
|
|
|
|
GRWorkList::~GRWorkList() {}
|
|
|
|
|
2009-05-02 06:18:46 +08:00
|
|
|
GRWorkList *GRWorkList::MakeDFS() { return new DFS(); }
|
|
|
|
GRWorkList *GRWorkList::MakeBFS() { return new BFS(); }
|
2008-01-15 07:24:37 +08:00
|
|
|
|
2008-12-17 06:13:33 +08:00
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN BFSBlockDFSContents : public GRWorkList {
|
|
|
|
std::queue<GRWorkListUnit> Queue;
|
|
|
|
llvm::SmallVector<GRWorkListUnit,20> Stack;
|
|
|
|
public:
|
|
|
|
virtual bool hasWork() const {
|
|
|
|
return !Queue.empty() || !Stack.empty();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:13:33 +08:00
|
|
|
virtual void Enqueue(const GRWorkListUnit& U) {
|
|
|
|
if (isa<BlockEntrance>(U.getNode()->getLocation()))
|
|
|
|
Queue.push(U);
|
|
|
|
else
|
|
|
|
Stack.push_back(U);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:13:33 +08:00
|
|
|
virtual GRWorkListUnit Dequeue() {
|
|
|
|
// Process all basic blocks to completion.
|
|
|
|
if (!Stack.empty()) {
|
|
|
|
const GRWorkListUnit& U = Stack.back();
|
|
|
|
Stack.pop_back(); // This technically "invalidates" U, but we are fine.
|
|
|
|
return U;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:13:33 +08:00
|
|
|
assert(!Queue.empty());
|
|
|
|
// Don't use const reference. The subsequent pop_back() might make it
|
|
|
|
// unsafe.
|
2009-09-09 23:08:12 +08:00
|
|
|
GRWorkListUnit U = Queue.front();
|
2008-12-17 06:13:33 +08:00
|
|
|
Queue.pop();
|
2009-09-09 23:08:12 +08:00
|
|
|
return U;
|
2008-12-17 06:13:33 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
GRWorkList* GRWorkList::MakeBFSBlockDFSContents() {
|
|
|
|
return new BFSBlockDFSContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Core analysis engine.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRCoreEngine::ProcessEndPath(GREndPathNodeBuilder& Builder) {
|
2009-08-06 18:00:15 +08:00
|
|
|
SubEngine.ProcessEndPath(Builder);
|
|
|
|
}
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRCoreEngine::ProcessStmt(Stmt* S, GRStmtNodeBuilder& Builder) {
|
2009-08-06 18:00:15 +08:00
|
|
|
SubEngine.ProcessStmt(S, Builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GRCoreEngine::ProcessBlockEntrance(CFGBlock* Blk, const GRState* State,
|
2009-09-09 23:08:12 +08:00
|
|
|
GRBlockCounter BC) {
|
2009-08-06 18:00:15 +08:00
|
|
|
return SubEngine.ProcessBlockEntrance(Blk, State, BC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRCoreEngine::ProcessBranch(Stmt* Condition, Stmt* Terminator,
|
2009-08-06 20:48:26 +08:00
|
|
|
GRBranchNodeBuilder& Builder) {
|
2009-09-09 23:08:12 +08:00
|
|
|
SubEngine.ProcessBranch(Condition, Terminator, Builder);
|
2009-08-06 18:00:15 +08:00
|
|
|
}
|
2008-12-17 06:13:33 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRCoreEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& Builder) {
|
2009-08-06 18:00:15 +08:00
|
|
|
SubEngine.ProcessIndirectGoto(Builder);
|
|
|
|
}
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRCoreEngine::ProcessSwitch(GRSwitchNodeBuilder& Builder) {
|
2009-08-06 18:00:15 +08:00
|
|
|
SubEngine.ProcessSwitch(Builder);
|
|
|
|
}
|
2009-08-06 20:48:26 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
/// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
|
2009-08-15 11:17:38 +08:00
|
|
|
bool GRCoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
if (G->num_roots() == 0) { // Initialize the analysis by constructing
|
|
|
|
// the root if none exists.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-25 11:33:41 +08:00
|
|
|
CFGBlock* Entry = &(L->getCFG()->getEntry());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
assert (Entry->empty() &&
|
2008-01-15 07:24:37 +08:00
|
|
|
"Entry block must be empty.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
assert (Entry->succ_size() == 1 &&
|
|
|
|
"Entry block must have 1 successor.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
// Get the solitary successor.
|
2009-09-09 23:08:12 +08:00
|
|
|
CFGBlock* Succ = *(Entry->succ_begin());
|
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
// Construct an edge representing the
|
|
|
|
// starting location in the function.
|
2009-08-15 11:17:38 +08:00
|
|
|
BlockEdge StartLoc(Entry, Succ, L);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 02:08:17 +08:00
|
|
|
// Set the current block counter to being empty.
|
|
|
|
WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
// Generate the root.
|
2009-08-17 14:19:58 +08:00
|
|
|
GenerateNode(StartLoc, getInitialState(L), 0);
|
2008-01-15 07:24:37 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
while (Steps && WList->hasWork()) {
|
|
|
|
--Steps;
|
|
|
|
const GRWorkListUnit& WU = WList->Dequeue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 02:08:17 +08:00
|
|
|
// Set the current block counter.
|
|
|
|
WList->setBlockCounter(WU.getBlockCounter());
|
|
|
|
|
|
|
|
// Retrieve the node.
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode* Node = WU.getNode();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
// Dispatch on the location type.
|
|
|
|
switch (Node->getLocation().getKind()) {
|
2008-12-17 06:13:33 +08:00
|
|
|
case ProgramPoint::BlockEdgeKind:
|
2008-01-15 07:24:37 +08:00
|
|
|
HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
case ProgramPoint::BlockEntranceKind:
|
|
|
|
HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node);
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
case ProgramPoint::BlockExitKind:
|
2008-01-15 08:24:08 +08:00
|
|
|
assert (false && "BlockExit location never occur in forward analysis.");
|
2008-01-15 07:24:37 +08:00
|
|
|
break;
|
2008-12-17 06:13:33 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert(isa<PostStmt>(Node->getLocation()));
|
2008-01-15 07:24:37 +08:00
|
|
|
HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(),
|
|
|
|
WU.getIndex(), Node);
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-01-15 07:24:37 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
return WList->hasWork();
|
|
|
|
}
|
|
|
|
|
2009-08-06 18:00:15 +08:00
|
|
|
|
|
|
|
void GRCoreEngine::HandleBlockEdge(const BlockEdge& L, ExplodedNode* Pred) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
CFGBlock* Blk = L.getDst();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Check if we are entering the EXIT block.
|
2009-08-25 11:33:41 +08:00
|
|
|
if (Blk == &(Pred->getLocationContext()->getCFG()->getExit())) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
assert (Pred->getLocationContext()->getCFG()->getExit().size() == 0
|
2008-01-29 08:33:40 +08:00
|
|
|
&& "EXIT block cannot contain Stmts.");
|
2008-01-15 07:24:37 +08:00
|
|
|
|
2008-04-12 06:03:04 +08:00
|
|
|
// Process the final state transition.
|
2009-08-06 20:48:26 +08:00
|
|
|
GREndPathNodeBuilder Builder(Blk, Pred, this);
|
2008-04-12 06:03:04 +08:00
|
|
|
ProcessEndPath(Builder);
|
2008-01-15 07:24:37 +08:00
|
|
|
|
|
|
|
// This path is done. Don't enqueue any more nodes.
|
|
|
|
return;
|
|
|
|
}
|
2008-03-01 04:27:50 +08:00
|
|
|
|
|
|
|
// FIXME: Should we allow ProcessBlockEntrance to also manipulate state?
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-01 04:27:50 +08:00
|
|
|
if (ProcessBlockEntrance(Blk, Pred->State, WList->getBlockCounter()))
|
2009-08-15 11:17:38 +08:00
|
|
|
GenerateNode(BlockEntrance(Blk, Pred->getLocationContext()), Pred->State, Pred);
|
2008-01-15 07:24:37 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 18:00:15 +08:00
|
|
|
void GRCoreEngine::HandleBlockEntrance(const BlockEntrance& L,
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* Pred) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 02:08:17 +08:00
|
|
|
// Increment the block counter.
|
|
|
|
GRBlockCounter Counter = WList->getBlockCounter();
|
|
|
|
Counter = BCounterFactory.IncrementCount(Counter, L.getBlock()->getBlockID());
|
|
|
|
WList->setBlockCounter(Counter);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// Process the entrance of the block.
|
2008-01-15 07:24:37 +08:00
|
|
|
if (Stmt* S = L.getFirstStmt()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
GRStmtNodeBuilder Builder(L.getBlock(), 0, Pred, this,
|
2009-08-06 20:48:26 +08:00
|
|
|
SubEngine.getStateManager());
|
2008-01-15 07:24:37 +08:00
|
|
|
ProcessStmt(S, Builder);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
2008-01-15 08:24:08 +08:00
|
|
|
HandleBlockExit(L.getBlock(), Pred);
|
2008-01-15 07:24:37 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 18:00:15 +08:00
|
|
|
void GRCoreEngine::HandleBlockExit(CFGBlock * B, ExplodedNode* Pred) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-30 06:56:11 +08:00
|
|
|
if (Stmt* Term = B->getTerminator()) {
|
|
|
|
switch (Term->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
assert(false && "Analysis for this terminator not implemented.");
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 05:51:20 +08:00
|
|
|
case Stmt::BinaryOperatorClass: // '&&' and '||'
|
|
|
|
HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-05 08:26:40 +08:00
|
|
|
case Stmt::ConditionalOperatorClass:
|
|
|
|
HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred);
|
2008-02-13 05:51:20 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 05:51:20 +08:00
|
|
|
// FIXME: Use constant-folding in CFG construction to simplify this
|
|
|
|
// case.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-05 08:26:40 +08:00
|
|
|
case Stmt::ChooseExprClass:
|
|
|
|
HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
|
2008-02-13 05:51:20 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 05:51:20 +08:00
|
|
|
case Stmt::DoStmtClass:
|
|
|
|
HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-30 06:56:11 +08:00
|
|
|
case Stmt::ForStmtClass:
|
|
|
|
HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
|
2008-02-13 05:51:20 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 00:56:51 +08:00
|
|
|
case Stmt::ContinueStmtClass:
|
|
|
|
case Stmt::BreakStmtClass:
|
2009-09-09 23:08:12 +08:00
|
|
|
case Stmt::GotoStmtClass:
|
2008-01-30 06:56:11 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 05:51:20 +08:00
|
|
|
case Stmt::IfStmtClass:
|
|
|
|
HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 08:24:44 +08:00
|
|
|
case Stmt::IndirectGotoStmtClass: {
|
|
|
|
// Only 1 successor: the indirect goto dispatch block.
|
|
|
|
assert (B->succ_size() == 1);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
GRIndirectGotoNodeBuilder
|
2008-02-13 08:24:44 +08:00
|
|
|
builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
|
|
|
|
*(B->succ_begin()), this);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 08:24:44 +08:00
|
|
|
ProcessIndirectGoto(builder);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-13 03:24:17 +08:00
|
|
|
case Stmt::ObjCForCollectionStmtClass: {
|
|
|
|
// In the case of ObjCForCollectionStmt, it appears twice in a CFG:
|
|
|
|
//
|
|
|
|
// (1) inside a basic block, which represents the binding of the
|
|
|
|
// 'element' variable to a value.
|
|
|
|
// (2) in a terminator, which represents the branch.
|
|
|
|
//
|
|
|
|
// For (1), subengines will bind a value (i.e., 0 or 1) indicating
|
|
|
|
// whether or not collection contains any more elements. We cannot
|
|
|
|
// just test to see if the element is nil because a container can
|
|
|
|
// contain nil elements.
|
|
|
|
HandleBranch(Term, Term, B, Pred);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
case Stmt::SwitchStmtClass: {
|
2009-08-06 20:48:26 +08:00
|
|
|
GRSwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
|
|
|
|
this);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
ProcessSwitch(builder);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-30 06:56:11 +08:00
|
|
|
case Stmt::WhileStmtClass:
|
|
|
|
HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
|
2008-02-13 05:51:20 +08:00
|
|
|
return;
|
2008-01-30 06:56:11 +08:00
|
|
|
}
|
|
|
|
}
|
2008-02-13 05:51:20 +08:00
|
|
|
|
|
|
|
assert (B->succ_size() == 1 &&
|
|
|
|
"Blocks with no terminator should have at most 1 successor.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
GenerateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
|
2009-08-15 11:17:38 +08:00
|
|
|
Pred->State, Pred);
|
2008-01-15 07:24:37 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 18:00:15 +08:00
|
|
|
void GRCoreEngine::HandleBranch(Stmt* Cond, Stmt* Term, CFGBlock * B,
|
|
|
|
ExplodedNode* Pred) {
|
2008-01-30 06:56:11 +08:00
|
|
|
assert (B->succ_size() == 2);
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
GRBranchNodeBuilder Builder(B, *(B->succ_begin()), *(B->succ_begin()+1),
|
|
|
|
Pred, this);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-30 06:56:11 +08:00
|
|
|
ProcessBranch(Cond, Term, Builder);
|
|
|
|
}
|
|
|
|
|
2009-08-06 18:00:15 +08:00
|
|
|
void GRCoreEngine::HandlePostStmt(const PostStmt& L, CFGBlock* B,
|
2009-08-06 09:32:16 +08:00
|
|
|
unsigned StmtIdx, ExplodedNode* Pred) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
assert (!B->empty());
|
|
|
|
|
2008-01-15 08:24:08 +08:00
|
|
|
if (StmtIdx == B->size())
|
|
|
|
HandleBlockExit(B, Pred);
|
2008-01-15 07:24:37 +08:00
|
|
|
else {
|
2009-09-09 23:08:12 +08:00
|
|
|
GRStmtNodeBuilder Builder(B, StmtIdx, Pred, this,
|
2009-08-06 20:48:26 +08:00
|
|
|
SubEngine.getStateManager());
|
2008-01-17 06:13:19 +08:00
|
|
|
ProcessStmt((*B)[StmtIdx], Builder);
|
2008-01-15 07:24:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GenerateNode - Utility method to generate nodes, hook up successors,
|
|
|
|
/// and add nodes to the worklist.
|
2009-09-09 23:08:12 +08:00
|
|
|
void GRCoreEngine::GenerateNode(const ProgramPoint& Loc,
|
2009-08-06 18:00:15 +08:00
|
|
|
const GRState* State, ExplodedNode* Pred) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
bool IsNew;
|
2009-08-06 14:28:40 +08:00
|
|
|
ExplodedNode* Node = G->getNode(Loc, State, &IsNew);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (Pred)
|
2009-10-07 08:42:52 +08:00
|
|
|
Node->addPredecessor(Pred, *G); // Link 'Node' with its predecessor.
|
2008-01-15 07:24:37 +08:00
|
|
|
else {
|
|
|
|
assert (IsNew);
|
|
|
|
G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
// Only add 'Node' to the worklist if it was freshly generated.
|
2008-02-13 02:08:17 +08:00
|
|
|
if (IsNew) WList->Enqueue(Node);
|
2008-01-15 07:24:37 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
GRStmtNodeBuilder::GRStmtNodeBuilder(CFGBlock* b, unsigned idx,
|
|
|
|
ExplodedNode* N, GRCoreEngine* e,
|
|
|
|
GRStateManager &mgr)
|
2009-09-09 23:08:12 +08:00
|
|
|
: Eng(*e), B(*b), Idx(idx), Pred(N), LastNode(N), Mgr(mgr), Auditor(0),
|
2009-08-06 20:48:26 +08:00
|
|
|
PurgingDeadSymbols(false), BuildSinks(false), HasGeneratedNode(false),
|
|
|
|
PointKind(ProgramPoint::PostStmtKind), Tag(0) {
|
2008-01-15 07:24:37 +08:00
|
|
|
Deferred.insert(N);
|
2009-08-06 20:48:26 +08:00
|
|
|
CleanedState = getLastNode()->getState();
|
2008-01-15 07:24:37 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
GRStmtNodeBuilder::~GRStmtNodeBuilder() {
|
2008-01-15 07:24:37 +08:00
|
|
|
for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
|
2008-01-31 07:03:39 +08:00
|
|
|
if (!(*I)->isSink())
|
2008-01-15 07:24:37 +08:00
|
|
|
GenerateAutoTransition(*I);
|
|
|
|
}
|
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
void GRStmtNodeBuilder::GenerateAutoTransition(ExplodedNode* N) {
|
2008-01-31 07:03:39 +08:00
|
|
|
assert (!N->isSink());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-15 11:17:38 +08:00
|
|
|
PostStmt Loc(getStmt(), N->getLocationContext());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
if (Loc == N->getLocation()) {
|
|
|
|
// Note: 'N' should be a fresh node because otherwise it shouldn't be
|
|
|
|
// a member of Deferred.
|
|
|
|
Eng.WList->Enqueue(N, B, Idx+1);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
bool IsNew;
|
2009-08-06 14:28:40 +08:00
|
|
|
ExplodedNode* Succ = Eng.G->getNode(Loc, N->State, &IsNew);
|
2009-10-07 08:42:52 +08:00
|
|
|
Succ->addPredecessor(N, *Eng.G);
|
2008-01-15 07:24:37 +08:00
|
|
|
|
|
|
|
if (IsNew)
|
|
|
|
Eng.WList->Enqueue(Succ, B, Idx+1);
|
|
|
|
}
|
|
|
|
|
2009-07-23 06:35:28 +08:00
|
|
|
static inline PostStmt GetPostLoc(const Stmt* S, ProgramPoint::Kind K,
|
2009-08-15 11:17:38 +08:00
|
|
|
const LocationContext *L, const void *tag) {
|
2008-06-18 13:34:07 +08:00
|
|
|
switch (K) {
|
|
|
|
default:
|
|
|
|
assert(false && "Invalid PostXXXKind.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-18 13:34:07 +08:00
|
|
|
case ProgramPoint::PostStmtKind:
|
2009-08-15 11:17:38 +08:00
|
|
|
return PostStmt(S, L, tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-18 13:34:07 +08:00
|
|
|
case ProgramPoint::PostLoadKind:
|
2009-08-15 11:17:38 +08:00
|
|
|
return PostLoad(S, L, tag);
|
2008-12-17 06:13:33 +08:00
|
|
|
|
|
|
|
case ProgramPoint::PostUndefLocationCheckFailedKind:
|
2009-08-15 11:17:38 +08:00
|
|
|
return PostUndefLocationCheckFailed(S, L, tag);
|
2008-12-17 06:13:33 +08:00
|
|
|
|
|
|
|
case ProgramPoint::PostLocationChecksSucceedKind:
|
2009-08-15 11:17:38 +08:00
|
|
|
return PostLocationChecksSucceed(S, L, tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:13:33 +08:00
|
|
|
case ProgramPoint::PostOutOfBoundsCheckFailedKind:
|
2009-08-15 11:17:38 +08:00
|
|
|
return PostOutOfBoundsCheckFailed(S, L, tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 06:13:33 +08:00
|
|
|
case ProgramPoint::PostNullCheckFailedKind:
|
2009-08-15 11:17:38 +08:00
|
|
|
return PostNullCheckFailed(S, L, tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 04:49:23 +08:00
|
|
|
case ProgramPoint::PostStoreKind:
|
2009-08-15 11:17:38 +08:00
|
|
|
return PostStore(S, L, tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-08 02:27:16 +08:00
|
|
|
case ProgramPoint::PostLValueKind:
|
2009-08-15 11:17:38 +08:00
|
|
|
return PostLValue(S, L, tag);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-18 13:34:07 +08:00
|
|
|
case ProgramPoint::PostPurgeDeadSymbolsKind:
|
2009-08-15 11:17:38 +08:00
|
|
|
return PostPurgeDeadSymbols(S, L, tag);
|
2008-06-18 13:34:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode*
|
2009-08-06 20:48:26 +08:00
|
|
|
GRStmtNodeBuilder::generateNodeInternal(const Stmt* S, const GRState* State,
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode* Pred,
|
2009-04-11 08:11:10 +08:00
|
|
|
ProgramPoint::Kind K,
|
|
|
|
const void *tag) {
|
2009-07-23 05:40:46 +08:00
|
|
|
return K == ProgramPoint::PreStmtKind
|
2009-09-09 23:08:12 +08:00
|
|
|
? generateNodeInternal(PreStmt(S, Pred->getLocationContext(),tag),
|
2009-08-15 11:17:38 +08:00
|
|
|
State, Pred)
|
|
|
|
: generateNodeInternal(GetPostLoc(S, K, Pred->getLocationContext(), tag),
|
2009-09-09 23:08:12 +08:00
|
|
|
State, Pred);
|
2009-02-20 07:45:28 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode*
|
2009-08-06 20:48:26 +08:00
|
|
|
GRStmtNodeBuilder::generateNodeInternal(const ProgramPoint &Loc,
|
2009-08-06 14:28:40 +08:00
|
|
|
const GRState* State,
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode* Pred) {
|
2008-01-15 07:24:37 +08:00
|
|
|
bool IsNew;
|
2009-08-06 14:28:40 +08:00
|
|
|
ExplodedNode* N = Eng.G->getNode(Loc, State, &IsNew);
|
2009-10-07 08:42:52 +08:00
|
|
|
N->addPredecessor(Pred, *Eng.G);
|
2008-01-15 07:24:37 +08:00
|
|
|
Deferred.erase(Pred);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
if (IsNew) {
|
|
|
|
Deferred.insert(N);
|
|
|
|
LastNode = N;
|
|
|
|
return N;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-15 07:24:37 +08:00
|
|
|
LastNode = NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
return NULL;
|
2008-01-15 07:24:37 +08:00
|
|
|
}
|
2008-01-30 06:56:11 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
ExplodedNode* GRBranchNodeBuilder::generateNode(const GRState* State,
|
|
|
|
bool branch) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-21 02:44:36 +08:00
|
|
|
// If the branch has been marked infeasible we should not generate a node.
|
|
|
|
if (!isFeasible(branch))
|
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-30 06:56:11 +08:00
|
|
|
bool IsNew;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode* Succ =
|
2009-08-15 11:17:38 +08:00
|
|
|
Eng.G->getNode(BlockEdge(Src,branch ? DstT:DstF,Pred->getLocationContext()),
|
|
|
|
State, &IsNew);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-07 08:42:52 +08:00
|
|
|
Succ->addPredecessor(Pred, *Eng.G);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-21 02:44:36 +08:00
|
|
|
if (branch)
|
|
|
|
GeneratedTrue = true;
|
|
|
|
else
|
2009-09-09 23:08:12 +08:00
|
|
|
GeneratedFalse = true;
|
|
|
|
|
2008-01-31 07:03:39 +08:00
|
|
|
if (IsNew) {
|
2008-01-31 07:24:39 +08:00
|
|
|
Deferred.push_back(Succ);
|
2008-01-31 07:03:39 +08:00
|
|
|
return Succ;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-31 07:03:39 +08:00
|
|
|
return NULL;
|
2008-01-30 06:56:11 +08:00
|
|
|
}
|
2008-01-30 07:32:35 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
GRBranchNodeBuilder::~GRBranchNodeBuilder() {
|
|
|
|
if (!GeneratedTrue) generateNode(Pred->State, true);
|
|
|
|
if (!GeneratedFalse) generateNode(Pred->State, false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-31 07:24:39 +08:00
|
|
|
for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
|
2008-02-13 02:08:17 +08:00
|
|
|
if (!(*I)->isSink()) Eng.WList->Enqueue(*I);
|
2008-01-30 07:32:35 +08:00
|
|
|
}
|
2008-02-13 08:24:44 +08:00
|
|
|
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode*
|
2009-08-06 20:48:26 +08:00
|
|
|
GRIndirectGotoNodeBuilder::generateNode(const iterator& I, const GRState* St,
|
|
|
|
bool isSink) {
|
2008-02-13 08:24:44 +08:00
|
|
|
bool IsNew;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
|
2009-08-15 11:17:38 +08:00
|
|
|
Pred->getLocationContext()), St, &IsNew);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-07 08:42:52 +08:00
|
|
|
Succ->addPredecessor(Pred, *Eng.G);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 08:24:44 +08:00
|
|
|
if (IsNew) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 08:24:44 +08:00
|
|
|
if (isSink)
|
|
|
|
Succ->markAsSink();
|
|
|
|
else
|
|
|
|
Eng.WList->Enqueue(Succ);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 08:24:44 +08:00
|
|
|
return Succ;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-13 08:24:44 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-02-14 07:08:21 +08:00
|
|
|
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode*
|
2009-08-06 20:48:26 +08:00
|
|
|
GRSwitchNodeBuilder::generateCaseStmtNode(const iterator& I, const GRState* St){
|
2008-02-14 07:08:21 +08:00
|
|
|
|
|
|
|
bool IsNew;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-15 11:17:38 +08:00
|
|
|
ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
|
|
|
|
Pred->getLocationContext()), St, &IsNew);
|
2009-10-07 08:42:52 +08:00
|
|
|
Succ->addPredecessor(Pred, *Eng.G);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
if (IsNew) {
|
|
|
|
Eng.WList->Enqueue(Succ);
|
|
|
|
return Succ;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode*
|
2009-08-06 20:48:26 +08:00
|
|
|
GRSwitchNodeBuilder::generateDefaultCaseNode(const GRState* St, bool isSink) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
// Get the block for the default case.
|
|
|
|
assert (Src->succ_rbegin() != Src->succ_rend());
|
|
|
|
CFGBlock* DefaultBlock = *Src->succ_rbegin();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
bool IsNew;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-15 11:17:38 +08:00
|
|
|
ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
|
|
|
|
Pred->getLocationContext()), St, &IsNew);
|
2009-10-07 08:42:52 +08:00
|
|
|
Succ->addPredecessor(Pred, *Eng.G);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
if (IsNew) {
|
|
|
|
if (isSink)
|
|
|
|
Succ->markAsSink();
|
|
|
|
else
|
|
|
|
Eng.WList->Enqueue(Succ);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
return Succ;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-14 07:08:21 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-04-12 06:03:04 +08:00
|
|
|
|
2009-08-06 20:48:26 +08:00
|
|
|
GREndPathNodeBuilder::~GREndPathNodeBuilder() {
|
2008-04-12 06:03:04 +08:00
|
|
|
// Auto-generate an EOP node if one has not been generated.
|
2009-08-06 20:48:26 +08:00
|
|
|
if (!HasGeneratedNode) generateNode(Pred->State);
|
2008-04-12 06:03:04 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
ExplodedNode*
|
2009-08-06 20:48:26 +08:00
|
|
|
GREndPathNodeBuilder::generateNode(const GRState* State, const void *tag,
|
|
|
|
ExplodedNode* P) {
|
2009-09-09 23:08:12 +08:00
|
|
|
HasGeneratedNode = true;
|
2008-04-12 06:03:04 +08:00
|
|
|
bool IsNew;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ExplodedNode* Node = Eng.G->getNode(BlockEntrance(&B,
|
2009-08-15 11:17:38 +08:00
|
|
|
Pred->getLocationContext(), tag), State, &IsNew);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-07 08:42:52 +08:00
|
|
|
Node->addPredecessor(P ? P : Pred, *Eng.G);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-12 06:03:04 +08:00
|
|
|
if (IsNew) {
|
|
|
|
Eng.G->addEndOfPath(Node);
|
2008-04-19 00:30:14 +08:00
|
|
|
return Node;
|
2008-04-12 06:03:04 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-19 00:30:14 +08:00
|
|
|
return NULL;
|
2008-04-12 06:03:04 +08:00
|
|
|
}
|