2008-02-14 22:13:12 +00:00
|
|
|
//=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
|
2008-01-31 02:35:41 +00:00
|
|
|
//
|
2008-01-31 06:49:09 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
2008-01-15 23:55:06 +00:00
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-02-14 22:13:12 +00:00
|
|
|
// This file defines a meta-engine for path-sensitive dataflow analysis that
|
|
|
|
// is built on GREngine, but provides the boilerplate to execute transfer
|
|
|
|
// functions and build the ExplodedGraph at the expression level.
|
2008-01-15 23:55:06 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-14 22:13:12 +00:00
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
|
2008-02-14 22:36:46 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-02-14 22:16:04 +00:00
|
|
|
|
2008-02-27 06:07:00 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "llvm/Support/GraphWriter.h"
|
|
|
|
#include <sstream>
|
|
|
|
#endif
|
|
|
|
|
2008-02-14 22:16:04 +00:00
|
|
|
using namespace clang;
|
|
|
|
using llvm::dyn_cast;
|
|
|
|
using llvm::cast;
|
|
|
|
using llvm::APSInt;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-02-27 06:47:26 +00:00
|
|
|
GRExprEngine::StateTy 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();
|
|
|
|
|
|
|
|
ValueStateImpl StateImpl = *StateMgr.getInitialState().getImpl();
|
|
|
|
|
|
|
|
for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
|
|
|
|
|
|
|
|
VarDecl* VD = cast<VarDecl>(const_cast<ScopedDecl*>(I->first));
|
|
|
|
|
|
|
|
if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD)) {
|
|
|
|
RVal X = RVal::GetSymbolValue(SymMgr, VD);
|
|
|
|
StateMgr.BindVar(StateImpl, VD, X);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return StateMgr.getPersistentState(StateImpl);
|
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
GRExprEngine::StateTy
|
2008-02-21 18:02:17 +00:00
|
|
|
GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal& V) {
|
2008-02-07 04:16:04 +00:00
|
|
|
|
2008-02-04 21:59:01 +00:00
|
|
|
if (!StateCleaned) {
|
|
|
|
St = RemoveDeadBindings(CurrentStmt, St);
|
|
|
|
StateCleaned = true;
|
|
|
|
}
|
2008-02-07 04:16:04 +00:00
|
|
|
|
2008-02-04 21:59:01 +00:00
|
|
|
bool isBlkExpr = false;
|
2008-02-07 04:16:04 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
if (Ex == CurrentStmt) {
|
|
|
|
isBlkExpr = getCFG().isBlkExpr(Ex);
|
2008-02-04 21:59:01 +00:00
|
|
|
|
|
|
|
if (!isBlkExpr)
|
|
|
|
return St;
|
|
|
|
}
|
2008-02-07 04:16:04 +00:00
|
|
|
|
2008-02-26 23:37:01 +00:00
|
|
|
return StateMgr.SetRVal(St, Ex, V, isBlkExpr, false);
|
2008-02-04 21:59:01 +00:00
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
const GRExprEngine::StateTy::BufferTy&
|
2008-02-21 18:02:17 +00:00
|
|
|
GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal::BufferTy& RB,
|
2008-02-05 19:35:18 +00:00
|
|
|
StateTy::BufferTy& RetBuf) {
|
|
|
|
|
|
|
|
assert (RetBuf.empty());
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
for (RVal::BufferTy::const_iterator I = RB.begin(), E = RB.end(); I!=E; ++I)
|
|
|
|
RetBuf.push_back(SetRVal(St, Ex, *I));
|
2008-02-05 19:35:18 +00:00
|
|
|
|
|
|
|
return RetBuf;
|
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
GRExprEngine::StateTy
|
2008-02-21 18:02:17 +00:00
|
|
|
GRExprEngine::SetRVal(StateTy St, const LVal& LV, const RVal& RV) {
|
2008-02-04 21:59:01 +00:00
|
|
|
|
|
|
|
if (!StateCleaned) {
|
|
|
|
St = RemoveDeadBindings(CurrentStmt, St);
|
|
|
|
StateCleaned = true;
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
return StateMgr.SetRVal(St, LV, RV);
|
2008-02-04 21:59:01 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 19:05:15 +00:00
|
|
|
GRExprEngine::StateTy
|
|
|
|
GRExprEngine::MarkBranch(StateTy St, Stmt* Terminator, bool branchTaken) {
|
|
|
|
|
|
|
|
switch (Terminator->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
return St;
|
|
|
|
|
|
|
|
case Stmt::BinaryOperatorClass: { // '&&' and '||'
|
|
|
|
|
|
|
|
BinaryOperator* B = cast<BinaryOperator>(Terminator);
|
|
|
|
BinaryOperator::Opcode Op = B->getOpcode();
|
|
|
|
|
|
|
|
assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr);
|
|
|
|
|
|
|
|
// For &&, if we take the true branch, then the value of the whole
|
|
|
|
// expression is that of the RHS expression.
|
|
|
|
//
|
|
|
|
// For ||, if we take the false branch, then the value of the whole
|
|
|
|
// expression is that of the RHS expression.
|
|
|
|
|
|
|
|
Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) ||
|
|
|
|
(Op == BinaryOperator::LOr && !branchTaken)
|
|
|
|
? B->getRHS() : B->getLHS();
|
|
|
|
|
|
|
|
return SetBlkExprRVal(St, B, UninitializedVal(Ex));
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::ConditionalOperatorClass: { // ?:
|
|
|
|
|
|
|
|
ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
|
|
|
|
|
|
|
|
// For ?, if branchTaken == true then the value is either the LHS or
|
|
|
|
// the condition itself. (GNU extension).
|
|
|
|
|
|
|
|
Expr* Ex;
|
|
|
|
|
|
|
|
if (branchTaken)
|
|
|
|
Ex = C->getLHS() ? C->getLHS() : C->getCond();
|
|
|
|
else
|
|
|
|
Ex = C->getRHS();
|
|
|
|
|
|
|
|
return SetBlkExprRVal(St, C, UninitializedVal(Ex));
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::ChooseExprClass: { // ?:
|
|
|
|
|
|
|
|
ChooseExpr* C = cast<ChooseExpr>(Terminator);
|
|
|
|
|
|
|
|
Expr* Ex = branchTaken ? C->getLHS() : C->getRHS();
|
|
|
|
return SetBlkExprRVal(St, C, UninitializedVal(Ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term,
|
2008-02-21 18:02:17 +00:00
|
|
|
BranchNodeBuilder& builder) {
|
2008-01-30 23:03:39 +00:00
|
|
|
|
2008-02-11 19:21:59 +00:00
|
|
|
// Remove old bindings for subexpressions.
|
|
|
|
StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState());
|
2008-02-05 00:26:40 +00:00
|
|
|
|
2008-02-15 22:29:00 +00:00
|
|
|
// Check for NULL conditions; e.g. "for(;;)"
|
|
|
|
if (!Condition) {
|
|
|
|
builder.markInfeasible(false);
|
|
|
|
|
|
|
|
// Get the current block counter.
|
|
|
|
GRBlockCounter BC = builder.getBlockCounter();
|
|
|
|
unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
|
|
|
|
unsigned NumVisited = BC.getNumVisited(BlockID);
|
|
|
|
|
|
|
|
if (NumVisited < 1) builder.generateNode(PrevState, true);
|
|
|
|
else builder.markInfeasible(true);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
RVal V = GetRVal(PrevState, Condition);
|
2008-01-30 23:03:39 +00:00
|
|
|
|
|
|
|
switch (V.getBaseKind()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
case RVal::UnknownKind:
|
2008-02-26 19:40:44 +00:00
|
|
|
builder.generateNode(MarkBranch(PrevState, Term, true), true);
|
|
|
|
builder.generateNode(MarkBranch(PrevState, Term, false), false);
|
2008-01-30 23:03:39 +00:00
|
|
|
return;
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
case RVal::UninitializedKind: {
|
2008-01-30 23:03:39 +00:00
|
|
|
NodeTy* N = builder.generateNode(PrevState, true);
|
|
|
|
|
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
|
|
|
UninitBranches.insert(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.markInfeasible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-02-15 22:29:00 +00:00
|
|
|
|
2008-02-12 18:08:17 +00:00
|
|
|
// Get the current block counter.
|
|
|
|
GRBlockCounter BC = builder.getBlockCounter();
|
2008-02-12 19:49:57 +00:00
|
|
|
unsigned BlockID = builder.getTargetBlock(true)->getBlockID();
|
|
|
|
unsigned NumVisited = BC.getNumVisited(BlockID);
|
2008-02-05 00:26:40 +00:00
|
|
|
|
2008-02-12 18:08:17 +00:00
|
|
|
if (isa<nonlval::ConcreteInt>(V) ||
|
|
|
|
BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) {
|
|
|
|
|
|
|
|
// Process the true branch.
|
2008-01-30 23:03:39 +00:00
|
|
|
|
2008-02-12 18:08:17 +00:00
|
|
|
bool isFeasible = true;
|
|
|
|
|
|
|
|
StateTy St = Assume(PrevState, V, true, isFeasible);
|
|
|
|
|
|
|
|
if (isFeasible)
|
2008-02-26 19:05:15 +00:00
|
|
|
builder.generateNode(MarkBranch(St, Term, true), true);
|
2008-02-12 18:08:17 +00:00
|
|
|
else
|
|
|
|
builder.markInfeasible(true);
|
2008-01-30 23:03:39 +00:00
|
|
|
}
|
2008-02-12 18:08:17 +00:00
|
|
|
else
|
|
|
|
builder.markInfeasible(true);
|
2008-01-29 23:32:35 +00:00
|
|
|
|
2008-02-12 19:49:57 +00:00
|
|
|
BlockID = builder.getTargetBlock(false)->getBlockID();
|
|
|
|
NumVisited = BC.getNumVisited(BlockID);
|
2008-01-29 23:32:35 +00:00
|
|
|
|
2008-02-12 18:08:17 +00:00
|
|
|
if (isa<nonlval::ConcreteInt>(V) ||
|
|
|
|
BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) {
|
|
|
|
|
|
|
|
// Process the false branch.
|
|
|
|
|
|
|
|
bool isFeasible = false;
|
|
|
|
|
|
|
|
StateTy St = Assume(PrevState, V, false, isFeasible);
|
|
|
|
|
|
|
|
if (isFeasible)
|
2008-02-26 19:05:15 +00:00
|
|
|
builder.generateNode(MarkBranch(St, Term, false), false);
|
2008-02-12 18:08:17 +00:00
|
|
|
else
|
|
|
|
builder.markInfeasible(false);
|
|
|
|
}
|
2008-02-05 00:26:40 +00:00
|
|
|
else
|
|
|
|
builder.markInfeasible(false);
|
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
/// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor
|
2008-02-13 00:24:44 +00:00
|
|
|
/// nodes by processing the 'effects' of a computed goto jump.
|
2008-02-13 17:41:41 +00:00
|
|
|
void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) {
|
2008-02-13 00:24:44 +00:00
|
|
|
|
|
|
|
StateTy St = builder.getState();
|
2008-02-21 18:02:17 +00:00
|
|
|
RVal V = GetRVal(St, builder.getTarget());
|
2008-02-13 00:24:44 +00:00
|
|
|
|
|
|
|
// Three possibilities:
|
|
|
|
//
|
|
|
|
// (1) We know the computed label.
|
|
|
|
// (2) The label is NULL (or some other constant), or Uninitialized.
|
|
|
|
// (3) We have no clue about the label. Dispatch to all targets.
|
|
|
|
//
|
|
|
|
|
|
|
|
typedef IndirectGotoNodeBuilder::iterator iterator;
|
|
|
|
|
|
|
|
if (isa<lval::GotoLabel>(V)) {
|
|
|
|
LabelStmt* L = cast<lval::GotoLabel>(V).getLabel();
|
|
|
|
|
|
|
|
for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) {
|
2008-02-13 17:27:37 +00:00
|
|
|
if (I.getLabel() == L) {
|
|
|
|
builder.generateNode(I, St);
|
2008-02-13 00:24:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (false && "No block with label.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) {
|
|
|
|
// Dispatch to the first target and mark it as a sink.
|
2008-02-13 17:27:37 +00:00
|
|
|
NodeTy* N = builder.generateNode(builder.begin(), St, true);
|
2008-02-13 00:24:44 +00:00
|
|
|
UninitBranches.insert(N);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is really a catch-all. We don't support symbolics yet.
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
assert (V.isUnknown());
|
2008-02-13 00:24:44 +00:00
|
|
|
|
|
|
|
for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
|
2008-02-13 17:27:37 +00:00
|
|
|
builder.generateNode(I, St);
|
2008-02-13 00:24:44 +00:00
|
|
|
}
|
2008-02-05 00:26:40 +00:00
|
|
|
|
2008-02-13 23:08:21 +00:00
|
|
|
/// ProcessSwitch - Called by GRCoreEngine. Used to generate successor
|
|
|
|
/// nodes by processing the 'effects' of a switch statement.
|
|
|
|
void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) {
|
|
|
|
|
|
|
|
typedef SwitchNodeBuilder::iterator iterator;
|
|
|
|
|
|
|
|
StateTy St = builder.getState();
|
2008-02-18 22:57:02 +00:00
|
|
|
Expr* CondE = builder.getCondition();
|
2008-02-21 18:02:17 +00:00
|
|
|
RVal CondV = GetRVal(St, CondE);
|
2008-02-13 23:08:21 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
if (CondV.isUninit()) {
|
2008-02-13 23:08:21 +00:00
|
|
|
NodeTy* N = builder.generateDefaultCaseNode(St, true);
|
|
|
|
UninitBranches.insert(N);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
StateTy DefaultSt = St;
|
|
|
|
|
|
|
|
// While most of this can be assumed (such as the signedness), having it
|
|
|
|
// just computed makes sure everything makes the same assumptions end-to-end.
|
2008-02-18 22:57:02 +00:00
|
|
|
|
|
|
|
unsigned bits = getContext().getTypeSize(CondE->getType(),
|
|
|
|
CondE->getExprLoc());
|
|
|
|
|
2008-02-13 23:08:21 +00:00
|
|
|
APSInt V1(bits, false);
|
|
|
|
APSInt V2 = V1;
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) {
|
2008-02-13 23:08:21 +00:00
|
|
|
|
|
|
|
CaseStmt* Case = cast<CaseStmt>(I.getCase());
|
|
|
|
|
|
|
|
// Evaluate the case.
|
|
|
|
if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) {
|
|
|
|
assert (false && "Case condition must evaluate to an integer constant.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the RHS of the case, if it exists.
|
|
|
|
|
|
|
|
if (Expr* E = Case->getRHS()) {
|
|
|
|
if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) {
|
|
|
|
assert (false &&
|
|
|
|
"Case condition (RHS) must evaluate to an integer constant.");
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (V1 <= V2);
|
|
|
|
}
|
|
|
|
else V2 = V1;
|
|
|
|
|
|
|
|
// FIXME: Eventually we should replace the logic below with a range
|
|
|
|
// comparison, rather than concretize the values within the range.
|
2008-02-21 18:02:17 +00:00
|
|
|
// This should be easy once we have "ranges" for NonLVals.
|
2008-02-13 23:08:21 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1));
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal);
|
2008-02-13 23:08:21 +00:00
|
|
|
|
|
|
|
// Now "assume" that the case matches.
|
2008-02-18 22:57:02 +00:00
|
|
|
bool isFeasible = false;
|
2008-02-13 23:08:21 +00:00
|
|
|
|
2008-02-14 19:37:24 +00:00
|
|
|
StateTy StNew = Assume(St, Res, true, isFeasible);
|
2008-02-13 23:08:21 +00:00
|
|
|
|
|
|
|
if (isFeasible) {
|
|
|
|
builder.generateCaseStmtNode(I, StNew);
|
|
|
|
|
|
|
|
// If CondV evaluates to a constant, then we know that this
|
|
|
|
// is the *only* case that we can take, so stop evaluating the
|
|
|
|
// others.
|
|
|
|
if (isa<nonlval::ConcreteInt>(CondV))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now "assume" that the case doesn't match. Add this state
|
|
|
|
// to the default state (if it is feasible).
|
|
|
|
|
2008-02-14 19:37:24 +00:00
|
|
|
StNew = Assume(DefaultSt, Res, false, isFeasible);
|
2008-02-13 23:08:21 +00:00
|
|
|
|
|
|
|
if (isFeasible)
|
|
|
|
DefaultSt = StNew;
|
|
|
|
|
|
|
|
// Concretize the next value in the range.
|
|
|
|
++V1;
|
|
|
|
|
|
|
|
} while (V1 < V2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here, than we know that the default branch is
|
|
|
|
// possible.
|
|
|
|
builder.generateDefaultCaseNode(DefaultSt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred,
|
2008-02-21 18:02:17 +00:00
|
|
|
NodeSet& Dst) {
|
2008-02-19 00:22:37 +00:00
|
|
|
|
2008-02-26 19:05:15 +00:00
|
|
|
assert (B->getOpcode() == BinaryOperator::LAnd ||
|
|
|
|
B->getOpcode() == BinaryOperator::LOr);
|
|
|
|
|
|
|
|
assert (B == CurrentStmt && getCFG().isBlkExpr(B));
|
|
|
|
|
|
|
|
StateTy St = Pred->getState();
|
|
|
|
RVal X = GetBlkExprRVal(St, B);
|
|
|
|
|
|
|
|
assert (X.isUninit());
|
|
|
|
|
|
|
|
Expr* Ex = (Expr*) cast<UninitializedVal>(X).getData();
|
|
|
|
|
|
|
|
assert (Ex);
|
|
|
|
|
|
|
|
if (Ex == B->getRHS()) {
|
|
|
|
|
|
|
|
X = GetBlkExprRVal(St, Ex);
|
|
|
|
|
2008-02-26 19:40:44 +00:00
|
|
|
// Handle uninitialized values.
|
|
|
|
|
|
|
|
if (X.isUninit()) {
|
|
|
|
Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-26 19:05:15 +00:00
|
|
|
// We took the RHS. Because the value of the '&&' or '||' expression must
|
|
|
|
// evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0
|
|
|
|
// or 1. Alternatively, we could take a lazy approach, and calculate this
|
|
|
|
// value later when necessary. We don't have the machinery in place for
|
|
|
|
// this right now, and since most logical expressions are used for branches,
|
|
|
|
// the payoff is not likely to be large. Instead, we do eager evaluation.
|
|
|
|
|
|
|
|
bool isFeasible = false;
|
|
|
|
StateTy NewState = Assume(St, X, true, isFeasible);
|
|
|
|
|
|
|
|
if (isFeasible)
|
|
|
|
Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(1U, B)));
|
|
|
|
|
|
|
|
isFeasible = false;
|
|
|
|
NewState = Assume(St, X, false, isFeasible);
|
|
|
|
|
|
|
|
if (isFeasible)
|
|
|
|
Nodify(Dst, B, Pred, SetBlkExprRVal(NewState, B, MakeConstantVal(0U, B)));
|
2008-02-05 00:26:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-02-26 19:05:15 +00:00
|
|
|
// We took the LHS expression. Depending on whether we are '&&' or
|
|
|
|
// '||' we know what the value of the expression is via properties of
|
|
|
|
// the short-circuiting.
|
2008-02-05 00:26:40 +00:00
|
|
|
|
2008-02-26 19:05:15 +00:00
|
|
|
X = MakeConstantVal( B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, B);
|
|
|
|
Nodify(Dst, B, Pred, SetBlkExprRVal(St, B, X));
|
|
|
|
}
|
2008-01-29 23:32:35 +00:00
|
|
|
}
|
2008-02-26 19:05:15 +00:00
|
|
|
|
2008-02-05 00:26:40 +00:00
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) {
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
Builder = &builder;
|
2008-01-23 19:59:44 +00:00
|
|
|
StmtEntryNode = builder.getLastNode();
|
|
|
|
CurrentStmt = S;
|
|
|
|
NodeSet Dst;
|
|
|
|
StateCleaned = false;
|
|
|
|
|
|
|
|
Visit(S, StmtEntryNode, Dst);
|
|
|
|
|
|
|
|
// If no nodes were generated, generate a new node that has all the
|
|
|
|
// dead mappings removed.
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) {
|
|
|
|
StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState());
|
|
|
|
builder.generateNode(S, St, StmtEntryNode);
|
|
|
|
}
|
2008-01-18 00:41:32 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
// For safety, NULL out these variables.
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
CurrentStmt = NULL;
|
|
|
|
StmtEntryNode = NULL;
|
|
|
|
Builder = NULL;
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
GRExprEngine::NodeTy*
|
|
|
|
GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) {
|
2008-01-23 19:59:44 +00:00
|
|
|
|
|
|
|
// If the state hasn't changed, don't generate a new node.
|
2008-02-07 15:20:13 +00:00
|
|
|
if (St == Pred->getState())
|
2008-02-07 05:48:01 +00:00
|
|
|
return NULL;
|
2008-01-16 00:53:15 +00:00
|
|
|
|
2008-02-07 05:48:01 +00:00
|
|
|
NodeTy* N = Builder->generateNode(S, St, Pred);
|
|
|
|
Dst.Add(N);
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-07 05:48:01 +00:00
|
|
|
return N;
|
2008-01-16 00:53:15 +00:00
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred,
|
2008-02-05 19:35:18 +00:00
|
|
|
const StateTy::BufferTy& SB) {
|
|
|
|
|
|
|
|
for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I)
|
|
|
|
Nodify(Dst, S, Pred, *I);
|
|
|
|
}
|
|
|
|
|
2008-02-13 18:06:44 +00:00
|
|
|
void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-07 04:16:04 +00:00
|
|
|
if (D != CurrentStmt) {
|
|
|
|
Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are here, we are loading the value of the decl and binding
|
|
|
|
// it to the block-level expression.
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
StateTy St = Pred->getState();
|
|
|
|
Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D)));
|
2008-02-07 04:16:04 +00:00
|
|
|
}
|
|
|
|
|
2008-02-19 01:44:53 +00:00
|
|
|
void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred,
|
2008-02-21 18:02:17 +00:00
|
|
|
CallExpr::arg_iterator AI,
|
|
|
|
CallExpr::arg_iterator AE,
|
2008-02-19 01:44:53 +00:00
|
|
|
NodeSet& Dst) {
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
// Process the arguments.
|
|
|
|
|
|
|
|
if (AI != AE) {
|
|
|
|
|
2008-02-19 01:44:53 +00:00
|
|
|
NodeSet DstTmp;
|
2008-02-27 00:44:11 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
Visit(*AI, Pred, DstTmp);
|
2008-02-27 00:44:11 +00:00
|
|
|
if (DstTmp.empty()) DstTmp.Add(Pred);
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
++AI;
|
2008-02-19 01:44:53 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI)
|
|
|
|
VisitCall(CE, *DI, AI, AE, Dst);
|
2008-02-19 01:44:53 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here we have processed all of the arguments. Evaluate
|
|
|
|
// the callee expression.
|
2008-02-25 21:16:03 +00:00
|
|
|
NodeSet DstTmp;
|
|
|
|
Expr* Callee = CE->getCallee()->IgnoreParenCasts();
|
|
|
|
|
|
|
|
VisitLVal(Callee, Pred, DstTmp);
|
2008-02-27 00:46:25 +00:00
|
|
|
if (DstTmp.empty()) DstTmp.Add(Pred);
|
2008-02-19 01:44:53 +00:00
|
|
|
|
|
|
|
// Finally, evaluate the function call.
|
2008-02-21 18:02:17 +00:00
|
|
|
for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) {
|
|
|
|
|
2008-02-19 01:44:53 +00:00
|
|
|
StateTy St = (*DI)->getState();
|
2008-02-25 21:16:03 +00:00
|
|
|
RVal L = GetLVal(St, Callee);
|
2008-02-19 01:44:53 +00:00
|
|
|
|
|
|
|
// Check for uninitialized control-flow.
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
if (L.isUninit()) {
|
|
|
|
|
2008-02-19 01:44:53 +00:00
|
|
|
NodeTy* N = Builder->generateNode(CE, St, *DI);
|
|
|
|
N->markAsSink();
|
|
|
|
UninitBranches.insert(N);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-02-21 19:46:04 +00:00
|
|
|
if (L.isUnknown()) {
|
|
|
|
// Invalidate all arguments passed in by reference (LVals).
|
|
|
|
for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
RVal V = GetRVal(St, *I);
|
|
|
|
|
|
|
|
if (isa<LVal>(V))
|
|
|
|
St = SetRVal(St, cast<LVal>(V), UnknownVal());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
St = EvalCall(CE, cast<LVal>(L), (*DI)->getState());
|
|
|
|
|
|
|
|
Nodify(Dst, CE, *DI, St);
|
2008-02-19 01:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){
|
2008-01-24 02:02:54 +00:00
|
|
|
|
2008-02-19 18:52:54 +00:00
|
|
|
NodeSet S1;
|
2008-02-21 18:02:17 +00:00
|
|
|
Visit(Ex, Pred, S1);
|
2008-01-24 02:02:54 +00:00
|
|
|
|
2008-02-19 18:52:54 +00:00
|
|
|
QualType T = CastE->getType();
|
|
|
|
|
2008-02-19 18:47:04 +00:00
|
|
|
// Check for redundant casts or casting to "void"
|
|
|
|
if (T->isVoidType() ||
|
2008-02-21 18:02:17 +00:00
|
|
|
Ex->getType() == T ||
|
|
|
|
(T->isPointerType() && Ex->getType()->isFunctionType())) {
|
2008-02-19 18:52:54 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1)
|
2008-02-19 18:52:54 +00:00
|
|
|
Dst.Add(*I1);
|
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
|
2008-01-24 02:02:54 +00:00
|
|
|
NodeTy* N = *I1;
|
|
|
|
StateTy St = N->getState();
|
2008-02-21 18:02:17 +00:00
|
|
|
RVal V = GetRVal(St, Ex);
|
2008-02-21 18:43:30 +00:00
|
|
|
Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(V, CastE->getType())));
|
2008-01-24 02:02:54 +00:00
|
|
|
}
|
2008-01-24 20:55:43 +00:00
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred,
|
2008-02-21 18:02:17 +00:00
|
|
|
GRExprEngine::NodeSet& Dst) {
|
2008-01-24 20:55:43 +00:00
|
|
|
|
|
|
|
StateTy St = Pred->getState();
|
|
|
|
|
|
|
|
for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator())
|
2008-01-28 22:51:57 +00:00
|
|
|
if (const VarDecl* VD = dyn_cast<VarDecl>(D)) {
|
2008-02-19 00:29:51 +00:00
|
|
|
|
|
|
|
// FIXME: Add support for local arrays.
|
|
|
|
if (VD->getType()->isArrayType())
|
|
|
|
continue;
|
|
|
|
|
2008-02-26 00:20:52 +00:00
|
|
|
// FIXME: static variables have an initializer, but the second
|
|
|
|
// time a function is called those values may not be current.
|
2008-02-21 18:02:17 +00:00
|
|
|
const Expr* Ex = VD->getInit();
|
|
|
|
|
|
|
|
St = SetRVal(St, lval::DeclVal(VD),
|
|
|
|
Ex ? GetRVal(St, Ex) : UninitializedVal());
|
2008-01-28 22:51:57 +00:00
|
|
|
}
|
2008-01-24 20:55:43 +00:00
|
|
|
|
|
|
|
Nodify(Dst, DS, Pred, St);
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
if (Dst.empty()) { Dst.Add(Pred); }
|
2008-01-24 20:55:43 +00:00
|
|
|
}
|
2008-01-24 02:02:54 +00:00
|
|
|
|
2008-02-05 00:26:40 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R,
|
2008-02-05 00:26:40 +00:00
|
|
|
NodeTy* Pred, NodeSet& Dst) {
|
|
|
|
|
2008-02-26 19:05:15 +00:00
|
|
|
assert (Ex == CurrentStmt && getCFG().isBlkExpr(Ex));
|
|
|
|
|
2008-02-05 00:26:40 +00:00
|
|
|
StateTy St = Pred->getState();
|
2008-02-26 19:05:15 +00:00
|
|
|
RVal X = GetBlkExprRVal(St, Ex);
|
2008-02-05 00:26:40 +00:00
|
|
|
|
2008-02-26 19:05:15 +00:00
|
|
|
assert (X.isUninit());
|
2008-02-05 00:26:40 +00:00
|
|
|
|
2008-02-26 19:05:15 +00:00
|
|
|
Expr* SE = (Expr*) cast<UninitializedVal>(X).getData();
|
|
|
|
|
|
|
|
assert (SE);
|
|
|
|
|
|
|
|
X = GetBlkExprRVal(St, SE);
|
2008-02-26 23:37:01 +00:00
|
|
|
|
|
|
|
// Make sure that we invalidate the previous binding.
|
|
|
|
Nodify(Dst, Ex, Pred, StateMgr.SetRVal(St, Ex, X, true, true));
|
2008-02-05 00:26:40 +00:00
|
|
|
}
|
|
|
|
|
2008-02-12 19:49:57 +00:00
|
|
|
/// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type).
|
2008-02-21 18:02:17 +00:00
|
|
|
void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex,
|
|
|
|
NodeTy* Pred,
|
|
|
|
NodeSet& Dst) {
|
2008-02-21 18:43:30 +00:00
|
|
|
|
|
|
|
assert (Ex->isSizeOf() && "FIXME: AlignOf(Expr) not yet implemented.");
|
2008-02-12 19:49:57 +00:00
|
|
|
|
|
|
|
// 6.5.3.4 sizeof: "The result type is an integer."
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
QualType T = Ex->getArgumentType();
|
2008-02-21 18:43:30 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-21 18:15:29 +00:00
|
|
|
// FIXME: Add support for VLAs.
|
2008-02-15 12:28:27 +00:00
|
|
|
if (!T.getTypePtr()->isConstantSizeType())
|
2008-02-12 19:49:57 +00:00
|
|
|
return;
|
|
|
|
|
2008-02-21 18:15:29 +00:00
|
|
|
|
|
|
|
uint64_t size = 1; // Handle sizeof(void)
|
|
|
|
|
|
|
|
if (T != getContext().VoidTy) {
|
|
|
|
SourceLocation Loc = Ex->getExprLoc();
|
|
|
|
size = getContext().getTypeSize(T, Loc) / 8;
|
|
|
|
}
|
2008-02-12 19:49:57 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
Nodify(Dst, Ex, Pred,
|
|
|
|
SetRVal(Pred->getState(), Ex,
|
2008-02-21 18:15:29 +00:00
|
|
|
NonLVal::MakeVal(ValMgr, size, Ex->getType())));
|
2008-02-12 19:49:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-02-20 04:02:35 +00:00
|
|
|
void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) {
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
Expr* Ex = U->getSubExpr()->IgnoreParens();
|
2008-02-20 04:02:35 +00:00
|
|
|
|
|
|
|
NodeSet DstTmp;
|
2008-02-07 04:16:04 +00:00
|
|
|
|
2008-02-26 03:44:25 +00:00
|
|
|
if (isa<DeclRefExpr>(Ex))
|
2008-02-20 04:02:35 +00:00
|
|
|
DstTmp.Add(Pred);
|
2008-02-07 04:16:04 +00:00
|
|
|
else
|
2008-02-21 18:02:17 +00:00
|
|
|
Visit(Ex, Pred, DstTmp);
|
2008-02-20 04:02:35 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) {
|
2008-02-20 04:02:35 +00:00
|
|
|
|
|
|
|
NodeTy* N = *I;
|
|
|
|
StateTy St = N->getState();
|
|
|
|
|
|
|
|
// FIXME: Bifurcate when dereferencing a symbolic with no constraints?
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
RVal V = GetRVal(St, Ex);
|
|
|
|
|
|
|
|
// Check for dereferences of uninitialized values.
|
2008-02-20 04:02:35 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
if (V.isUninit()) {
|
|
|
|
|
2008-02-20 04:02:35 +00:00
|
|
|
NodeTy* Succ = Builder->generateNode(U, St, N);
|
|
|
|
|
|
|
|
if (Succ) {
|
|
|
|
Succ->markAsSink();
|
|
|
|
UninitDeref.insert(Succ);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
// Check for dereferences of unknown values. Treat as No-Ops.
|
|
|
|
|
|
|
|
if (V.isUnknown()) {
|
2008-02-20 04:02:35 +00:00
|
|
|
Dst.Add(N);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// After a dereference, one of two possible situations arise:
|
|
|
|
// (1) A crash, because the pointer was NULL.
|
|
|
|
// (2) The pointer is not NULL, and the dereference works.
|
|
|
|
//
|
|
|
|
// We add these assumptions.
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
LVal LV = cast<LVal>(V);
|
2008-02-20 04:02:35 +00:00
|
|
|
bool isFeasibleNotNull;
|
|
|
|
|
|
|
|
// "Assume" that the pointer is Not-NULL.
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
StateTy StNotNull = Assume(St, LV, true, isFeasibleNotNull);
|
2008-01-24 02:28:56 +00:00
|
|
|
|
2008-02-20 04:02:35 +00:00
|
|
|
if (isFeasibleNotNull) {
|
|
|
|
|
|
|
|
// FIXME: Currently symbolic analysis "generates" new symbols
|
|
|
|
// for the contents of values. We need a better approach.
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
Nodify(Dst, U, N, SetRVal(StNotNull, U,
|
|
|
|
GetRVal(StNotNull, LV, U->getType())));
|
2008-02-20 04:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isFeasibleNull;
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
// Now "assume" that the pointer is NULL.
|
|
|
|
|
|
|
|
StateTy StNull = Assume(St, LV, false, isFeasibleNull);
|
2008-02-20 04:02:35 +00:00
|
|
|
|
|
|
|
if (isFeasibleNull) {
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-20 04:02:35 +00:00
|
|
|
// We don't use "Nodify" here because the node will be a sink
|
|
|
|
// and we have no intention of processing it later.
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-20 04:02:35 +00:00
|
|
|
NodeTy* NullNode = Builder->generateNode(U, StNull, N);
|
|
|
|
|
|
|
|
if (NullNode) {
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-20 04:02:35 +00:00
|
|
|
NullNode->markAsSink();
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode);
|
|
|
|
else ExplicitNullDeref.insert(NullNode);
|
2008-02-20 04:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred,
|
|
|
|
NodeSet& Dst) {
|
2008-02-20 04:02:35 +00:00
|
|
|
|
|
|
|
NodeSet S1;
|
|
|
|
|
|
|
|
assert (U->getOpcode() != UnaryOperator::Deref);
|
2008-02-21 18:02:17 +00:00
|
|
|
assert (U->getOpcode() != UnaryOperator::SizeOf);
|
|
|
|
assert (U->getOpcode() != UnaryOperator::AlignOf);
|
|
|
|
|
|
|
|
bool use_GetLVal = false;
|
2008-02-20 04:02:35 +00:00
|
|
|
|
|
|
|
switch (U->getOpcode()) {
|
|
|
|
case UnaryOperator::PostInc:
|
|
|
|
case UnaryOperator::PostDec:
|
|
|
|
case UnaryOperator::PreInc:
|
|
|
|
case UnaryOperator::PreDec:
|
|
|
|
case UnaryOperator::AddrOf:
|
2008-02-21 18:02:17 +00:00
|
|
|
// Evalue subexpression as an LVal.
|
|
|
|
use_GetLVal = true;
|
|
|
|
VisitLVal(U->getSubExpr(), Pred, S1);
|
2008-02-20 04:02:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Visit(U->getSubExpr(), Pred, S1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) {
|
2008-02-20 04:02:35 +00:00
|
|
|
|
2008-01-24 02:28:56 +00:00
|
|
|
NodeTy* N1 = *I1;
|
|
|
|
StateTy St = N1->getState();
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) :
|
|
|
|
GetRVal(St, U->getSubExpr());
|
|
|
|
|
|
|
|
if (SubV.isUnknown()) {
|
|
|
|
Dst.Add(N1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SubV.isUninit()) {
|
|
|
|
Nodify(Dst, U, N1, SetRVal(St, U, SubV));
|
|
|
|
continue;
|
|
|
|
}
|
2008-01-24 02:28:56 +00:00
|
|
|
|
2008-02-15 22:09:30 +00:00
|
|
|
if (U->isIncrementDecrementOp()) {
|
2008-02-20 04:02:35 +00:00
|
|
|
|
|
|
|
// Handle ++ and -- (both pre- and post-increment).
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
LVal SubLV = cast<LVal>(SubV);
|
|
|
|
RVal V = GetRVal(St, SubLV, U->getType());
|
|
|
|
|
2008-02-21 19:15:37 +00:00
|
|
|
if (V.isUnknown()) {
|
|
|
|
Dst.Add(N1);
|
|
|
|
continue;
|
|
|
|
}
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
// Propagate uninitialized values.
|
|
|
|
if (V.isUninit()) {
|
|
|
|
Nodify(Dst, U, N1, SetRVal(St, U, V));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-02-21 19:29:23 +00:00
|
|
|
// Handle all other values.
|
2008-02-15 22:09:30 +00:00
|
|
|
|
|
|
|
BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add
|
|
|
|
: BinaryOperator::Sub;
|
|
|
|
|
2008-02-21 19:29:23 +00:00
|
|
|
RVal Result = EvalBinOp(Op, V, MakeConstantVal(1U, U));
|
2008-02-15 22:09:30 +00:00
|
|
|
|
|
|
|
if (U->isPostfix())
|
2008-02-21 18:02:17 +00:00
|
|
|
St = SetRVal(SetRVal(St, U, V), SubLV, Result);
|
2008-02-15 22:09:30 +00:00
|
|
|
else
|
2008-02-21 18:02:17 +00:00
|
|
|
St = SetRVal(SetRVal(St, U, Result), SubLV, Result);
|
2008-02-06 22:50:25 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
Nodify(Dst, U, N1, St);
|
2008-02-15 22:09:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle all other unary operators.
|
|
|
|
|
|
|
|
switch (U->getOpcode()) {
|
2008-02-06 22:50:25 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
case UnaryOperator::Minus:
|
|
|
|
St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV)));
|
2008-01-24 08:20:02 +00:00
|
|
|
break;
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
case UnaryOperator::Not:
|
|
|
|
St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV)));
|
2008-02-20 04:12:31 +00:00
|
|
|
break;
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
case UnaryOperator::LNot:
|
2008-02-04 16:58:30 +00:00
|
|
|
|
2008-02-06 17:56:00 +00:00
|
|
|
// C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
|
|
|
|
//
|
|
|
|
// Note: technically we do "E == 0", but this is the same in the
|
|
|
|
// transfer functions as "0 == E".
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
if (isa<LVal>(SubV)) {
|
|
|
|
lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth());
|
|
|
|
RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V);
|
|
|
|
St = SetRVal(St, U, Result);
|
2008-02-06 17:56:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-02-22 00:42:36 +00:00
|
|
|
Expr* Ex = U->getSubExpr();
|
|
|
|
nonlval::ConcreteInt V(ValMgr.getValue(0, Ex->getType()));
|
2008-02-21 18:02:17 +00:00
|
|
|
RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V);
|
|
|
|
St = SetRVal(St, U, Result);
|
2008-02-06 17:56:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2008-01-31 02:35:41 +00:00
|
|
|
case UnaryOperator::AddrOf: {
|
2008-02-21 18:02:17 +00:00
|
|
|
assert (isa<LVal>(SubV));
|
|
|
|
St = SetRVal(St, U, SubV);
|
2008-01-31 02:35:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-02-07 05:48:01 +00:00
|
|
|
|
2008-01-24 02:28:56 +00:00
|
|
|
default: ;
|
|
|
|
assert (false && "Not implemented.");
|
2008-02-21 18:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Nodify(Dst, U, N1, St);
|
2008-01-24 02:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred,
|
|
|
|
NodeSet& Dst) {
|
2008-02-20 04:02:35 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
QualType T = U->getSubExpr()->getType();
|
|
|
|
|
|
|
|
// FIXME: Add support for VLAs.
|
|
|
|
if (!T.getTypePtr()->isConstantSizeType())
|
|
|
|
return;
|
2008-02-20 04:02:35 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
SourceLocation Loc = U->getExprLoc();
|
|
|
|
uint64_t size = getContext().getTypeSize(T, Loc) / 8;
|
|
|
|
StateTy St = Pred->getState();
|
|
|
|
St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc));
|
|
|
|
|
|
|
|
Nodify(Dst, U, Pred, St);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) {
|
|
|
|
|
|
|
|
assert (Ex != CurrentStmt && !getCFG().isBlkExpr(Ex));
|
|
|
|
|
|
|
|
Ex = Ex->IgnoreParens();
|
|
|
|
|
2008-02-27 06:47:26 +00:00
|
|
|
if (isa<DeclRefExpr>(Ex)) {
|
2008-02-07 04:16:04 +00:00
|
|
|
Dst.Add(Pred);
|
2008-02-07 01:08:27 +00:00
|
|
|
return;
|
2008-02-07 04:16:04 +00:00
|
|
|
}
|
2008-02-07 01:08:27 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) {
|
2008-02-07 01:08:27 +00:00
|
|
|
if (U->getOpcode() == UnaryOperator::Deref) {
|
2008-02-21 18:02:17 +00:00
|
|
|
Ex = U->getSubExpr()->IgnoreParens();
|
2008-02-20 04:02:35 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
if (isa<DeclRefExpr>(Ex))
|
2008-02-20 04:02:35 +00:00
|
|
|
Dst.Add(Pred);
|
|
|
|
else
|
2008-02-21 18:02:17 +00:00
|
|
|
Visit(Ex, Pred, Dst);
|
2008-02-20 04:02:35 +00:00
|
|
|
|
2008-02-07 01:08:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
Visit(Ex, Pred, Dst);
|
2008-02-07 01:08:27 +00:00
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
void GRExprEngine::VisitBinaryOperator(BinaryOperator* B,
|
2008-02-13 23:08:21 +00:00
|
|
|
GRExprEngine::NodeTy* Pred,
|
|
|
|
GRExprEngine::NodeSet& Dst) {
|
2008-01-23 19:59:44 +00:00
|
|
|
NodeSet S1;
|
2008-02-07 01:08:27 +00:00
|
|
|
|
|
|
|
if (B->isAssignmentOp())
|
2008-02-21 18:02:17 +00:00
|
|
|
VisitLVal(B->getLHS(), Pred, S1);
|
2008-02-07 01:08:27 +00:00
|
|
|
else
|
|
|
|
Visit(B->getLHS(), Pred, S1);
|
2008-01-16 00:53:15 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) {
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
NodeTy* N1 = *I1;
|
2008-01-16 00:53:15 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
// When getting the value for the LHS, check if we are in an assignment.
|
2008-02-21 18:02:17 +00:00
|
|
|
// In such cases, we want to (initially) treat the LHS as an LVal,
|
|
|
|
// so we use GetLVal instead of GetRVal so that DeclRefExpr's are
|
|
|
|
// evaluated to LValDecl's instead of to an NonLVal.
|
|
|
|
|
|
|
|
RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS())
|
|
|
|
: GetRVal(N1->getState(), B->getLHS());
|
|
|
|
|
|
|
|
// Visit the RHS...
|
|
|
|
|
|
|
|
NodeSet S2;
|
2008-01-23 19:59:44 +00:00
|
|
|
Visit(B->getRHS(), N1, S2);
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
// Process the binary operator.
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) {
|
2008-02-06 22:50:25 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
NodeTy* N2 = *I2;
|
2008-02-21 18:02:17 +00:00
|
|
|
StateTy St = N2->getState();
|
2008-02-25 18:42:54 +00:00
|
|
|
Expr* RHS = B->getRHS();
|
|
|
|
RVal RightV = GetRVal(St, RHS);
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-02-06 22:50:25 +00:00
|
|
|
BinaryOperator::Opcode Op = B->getOpcode();
|
|
|
|
|
2008-02-25 18:42:54 +00:00
|
|
|
if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
|
|
|
|
&& RHS->getType()->isIntegerType()) {
|
2008-02-26 02:15:56 +00:00
|
|
|
|
|
|
|
// Check if the denominator is uninitialized.
|
2008-02-25 18:42:54 +00:00
|
|
|
|
2008-02-26 22:27:51 +00:00
|
|
|
if (!RightV.isUnknown()) {
|
|
|
|
|
|
|
|
if (RightV.isUninit()) {
|
|
|
|
NodeTy* DivUninit = Builder->generateNode(B, St, N2);
|
|
|
|
|
|
|
|
if (DivUninit) {
|
|
|
|
DivUninit->markAsSink();
|
|
|
|
BadDivides.insert(DivUninit);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for divide/remainder-by-zero.
|
|
|
|
//
|
|
|
|
// First, "assume" that the denominator is 0 or uninitialized.
|
|
|
|
|
|
|
|
bool isFeasible = false;
|
|
|
|
StateTy ZeroSt = Assume(St, RightV, false, isFeasible);
|
2008-02-26 02:15:56 +00:00
|
|
|
|
2008-02-26 22:27:51 +00:00
|
|
|
if (isFeasible) {
|
|
|
|
NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
|
|
|
|
|
|
|
|
if (DivZeroNode) {
|
|
|
|
DivZeroNode->markAsSink();
|
|
|
|
BadDivides.insert(DivZeroNode);
|
|
|
|
}
|
2008-02-26 02:15:56 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 22:27:51 +00:00
|
|
|
// Second, "assume" that the denominator cannot be 0.
|
2008-02-26 02:15:56 +00:00
|
|
|
|
2008-02-26 22:27:51 +00:00
|
|
|
isFeasible = false;
|
|
|
|
St = Assume(St, RightV, true, isFeasible);
|
2008-02-25 17:51:31 +00:00
|
|
|
|
2008-02-26 22:27:51 +00:00
|
|
|
if (!isFeasible)
|
|
|
|
continue;
|
2008-02-25 17:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fall-through. The logic below processes the divide.
|
|
|
|
}
|
|
|
|
|
2008-02-06 22:50:25 +00:00
|
|
|
if (Op <= BinaryOperator::Or) {
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
// Process non-assignements except commas or short-circuited
|
|
|
|
// logical expressions (LAnd and LOr).
|
|
|
|
|
|
|
|
RVal Result = EvalBinOp(Op, LeftV, RightV);
|
|
|
|
|
|
|
|
if (Result.isUnknown()) {
|
|
|
|
Dst.Add(N2);
|
2008-02-07 01:08:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
Nodify(Dst, B, N2, SetRVal(St, B, Result));
|
2008-02-06 22:50:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
// Process assignments.
|
|
|
|
|
|
|
|
switch (Op) {
|
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
case BinaryOperator::Assign: {
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
// Simple assignments.
|
2008-02-19 00:22:37 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
if (LeftV.isUninit()) {
|
2008-02-19 00:22:37 +00:00
|
|
|
HandleUninitializedStore(B, N2);
|
2008-02-21 18:02:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LeftV.isUnknown()) {
|
|
|
|
St = SetRVal(St, B, RightV);
|
|
|
|
break;
|
|
|
|
}
|
2008-02-19 00:22:37 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV);
|
2008-01-23 19:59:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-02-06 22:50:25 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
// Compound assignment operators.
|
|
|
|
|
|
|
|
default: {
|
2008-01-23 23:38:00 +00:00
|
|
|
|
2008-02-26 22:27:51 +00:00
|
|
|
assert (B->isCompoundAssignmentOp());
|
|
|
|
|
|
|
|
if (Op >= BinaryOperator::AndAssign)
|
|
|
|
((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And);
|
|
|
|
else
|
|
|
|
((int&) Op) -= BinaryOperator::MulAssign;
|
|
|
|
|
|
|
|
// Check if the LHS is uninitialized.
|
2008-02-19 00:22:37 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
if (LeftV.isUninit()) {
|
2008-02-19 00:22:37 +00:00
|
|
|
HandleUninitializedStore(B, N2);
|
2008-02-21 18:02:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LeftV.isUnknown()) {
|
|
|
|
|
|
|
|
// While we do not know the location to store RightV,
|
|
|
|
// the entire expression does evaluate to RightV.
|
|
|
|
|
|
|
|
if (RightV.isUnknown()) {
|
|
|
|
Dst.Add(N2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
St = SetRVal(St, B, RightV);
|
2008-02-19 00:22:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
// At this pointer we know that the LHS evaluates to an LVal
|
|
|
|
// that is neither "Unknown" or "Unintialized."
|
|
|
|
|
|
|
|
LVal LeftLV = cast<LVal>(LeftV);
|
|
|
|
|
|
|
|
// Fetch the value of the LHS (the value of the variable, etc.).
|
|
|
|
|
|
|
|
RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType());
|
|
|
|
|
2008-02-26 22:27:51 +00:00
|
|
|
// Propagate uninitialized value (left-side). We
|
|
|
|
// propogate uninitialized values for the RHS below when
|
|
|
|
// we also check for divide-by-zero.
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
if (V.isUninit()) {
|
|
|
|
St = SetRVal(St, B, V);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Propagate unknown values.
|
|
|
|
|
2008-02-21 19:15:37 +00:00
|
|
|
if (V.isUnknown()) {
|
|
|
|
Dst.Add(N2);
|
|
|
|
continue;
|
|
|
|
}
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
if (RightV.isUnknown()) {
|
|
|
|
St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-26 22:27:51 +00:00
|
|
|
// At this point:
|
|
|
|
//
|
|
|
|
// The LHS is not Uninit/Unknown.
|
|
|
|
// The RHS is not Unknown.
|
2008-01-29 19:43:15 +00:00
|
|
|
|
2008-02-21 18:43:30 +00:00
|
|
|
// Get the computation type.
|
|
|
|
QualType CTy = cast<CompoundAssignOperator>(B)->getComputationType();
|
|
|
|
|
|
|
|
// Perform promotions.
|
|
|
|
V = EvalCast(V, CTy);
|
2008-02-21 18:46:24 +00:00
|
|
|
RightV = EvalCast(RightV, CTy);
|
2008-02-21 18:43:30 +00:00
|
|
|
|
|
|
|
// Evaluate operands and promote to result type.
|
2008-02-25 17:51:31 +00:00
|
|
|
|
2008-02-25 18:42:54 +00:00
|
|
|
if ((Op == BinaryOperator::Div || Op == BinaryOperator::Rem)
|
|
|
|
&& RHS->getType()->isIntegerType()) {
|
|
|
|
|
2008-02-26 02:15:56 +00:00
|
|
|
// Check if the denominator is uninitialized.
|
|
|
|
|
|
|
|
if (RightV.isUninit()) {
|
|
|
|
NodeTy* DivUninit = Builder->generateNode(B, St, N2);
|
|
|
|
|
|
|
|
if (DivUninit) {
|
|
|
|
DivUninit->markAsSink();
|
|
|
|
BadDivides.insert(DivUninit);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2008-02-26 22:27:51 +00:00
|
|
|
|
2008-02-25 17:51:31 +00:00
|
|
|
// First, "assume" that the denominator is 0.
|
|
|
|
|
|
|
|
bool isFeasible = false;
|
|
|
|
StateTy ZeroSt = Assume(St, RightV, false, isFeasible);
|
|
|
|
|
|
|
|
if (isFeasible) {
|
|
|
|
NodeTy* DivZeroNode = Builder->generateNode(B, ZeroSt, N2);
|
|
|
|
|
|
|
|
if (DivZeroNode) {
|
|
|
|
DivZeroNode->markAsSink();
|
2008-02-26 02:15:56 +00:00
|
|
|
BadDivides.insert(DivZeroNode);
|
2008-02-25 17:51:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second, "assume" that the denominator cannot be 0.
|
|
|
|
|
|
|
|
isFeasible = false;
|
|
|
|
St = Assume(St, RightV, true, isFeasible);
|
|
|
|
|
|
|
|
if (!isFeasible)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Fall-through. The logic below processes the divide.
|
|
|
|
}
|
2008-02-26 22:27:51 +00:00
|
|
|
else {
|
|
|
|
|
|
|
|
// Propagate uninitialized values (right-side).
|
|
|
|
|
|
|
|
if (RightV.isUninit()) {
|
|
|
|
St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2008-02-25 17:51:31 +00:00
|
|
|
|
2008-02-21 18:43:30 +00:00
|
|
|
RVal Result = EvalCast(EvalBinOp(Op, V, RightV), B->getType());
|
2008-02-21 18:02:17 +00:00
|
|
|
St = SetRVal(SetRVal(St, B, Result), LeftLV, Result);
|
2008-02-06 22:50:25 +00:00
|
|
|
}
|
2008-01-23 19:59:44 +00:00
|
|
|
}
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
Nodify(Dst, B, N2, St);
|
2008-01-16 00:53:15 +00:00
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) {
|
2008-02-19 00:22:37 +00:00
|
|
|
NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred);
|
|
|
|
N->markAsSink();
|
|
|
|
UninitStores.insert(N);
|
|
|
|
}
|
2008-01-15 23:55:06 +00:00
|
|
|
|
2008-02-19 00:22:37 +00:00
|
|
|
void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) {
|
2008-01-16 18:18:48 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
// FIXME: add metadata to the CFG so that we can disable
|
|
|
|
// this check when we KNOW that there is no block-level subexpression.
|
|
|
|
// The motivation is that this check requires a hashtable lookup.
|
2008-01-16 19:42:59 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
if (S != CurrentStmt && getCFG().isBlkExpr(S)) {
|
|
|
|
Dst.Add(Pred);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (S->getStmtClass()) {
|
2008-02-12 21:37:25 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// Cases we intentionally have "default" handle:
|
2008-02-26 19:17:09 +00:00
|
|
|
// AddrLabelExpr, IntegerLiteral, CharacterLiteral
|
2008-02-12 21:37:25 +00:00
|
|
|
|
|
|
|
Dst.Add(Pred); // No-op. Simply propagate the current state unchanged.
|
|
|
|
break;
|
|
|
|
|
2008-02-08 20:29:23 +00:00
|
|
|
case Stmt::BinaryOperatorClass: {
|
|
|
|
BinaryOperator* B = cast<BinaryOperator>(S);
|
2008-02-05 00:26:40 +00:00
|
|
|
|
2008-02-08 20:29:23 +00:00
|
|
|
if (B->isLogicalOp()) {
|
|
|
|
VisitLogicalExpr(B, Pred, Dst);
|
2008-02-05 00:26:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-02-08 20:29:23 +00:00
|
|
|
else if (B->getOpcode() == BinaryOperator::Comma) {
|
2008-02-08 07:05:39 +00:00
|
|
|
StateTy St = Pred->getState();
|
2008-02-21 18:02:17 +00:00
|
|
|
Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS())));
|
2008-02-08 07:05:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-02-05 00:26:40 +00:00
|
|
|
|
2008-01-23 19:59:44 +00:00
|
|
|
VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
|
|
|
|
break;
|
2008-02-12 19:49:57 +00:00
|
|
|
}
|
2008-02-19 01:44:53 +00:00
|
|
|
|
|
|
|
case Stmt::CallExprClass: {
|
|
|
|
CallExpr* C = cast<CallExpr>(S);
|
|
|
|
VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst);
|
|
|
|
break;
|
|
|
|
}
|
2008-02-12 19:49:57 +00:00
|
|
|
|
|
|
|
case Stmt::CastExprClass: {
|
|
|
|
CastExpr* C = cast<CastExpr>(S);
|
|
|
|
VisitCast(C, C->getSubExpr(), Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
2008-02-26 19:17:09 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
// FIXME: ChooseExpr is really a constant. We need to fix
|
|
|
|
// the CFG do not model them as explicit control-flow.
|
|
|
|
|
2008-02-12 19:49:57 +00:00
|
|
|
case Stmt::ChooseExprClass: { // __builtin_choose_expr
|
|
|
|
ChooseExpr* C = cast<ChooseExpr>(S);
|
|
|
|
VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
|
|
|
|
break;
|
2008-02-08 07:05:39 +00:00
|
|
|
}
|
|
|
|
|
2008-02-12 19:49:57 +00:00
|
|
|
case Stmt::CompoundAssignOperatorClass:
|
|
|
|
VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
|
2008-01-24 02:28:56 +00:00
|
|
|
break;
|
|
|
|
|
2008-02-12 19:49:57 +00:00
|
|
|
case Stmt::ConditionalOperatorClass: { // '?' operator
|
|
|
|
ConditionalOperator* C = cast<ConditionalOperator>(S);
|
|
|
|
VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
|
2008-01-23 19:59:44 +00:00
|
|
|
break;
|
2008-02-12 19:49:57 +00:00
|
|
|
}
|
|
|
|
|
2008-02-07 04:16:04 +00:00
|
|
|
case Stmt::DeclRefExprClass:
|
|
|
|
VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst);
|
|
|
|
break;
|
2008-01-23 19:59:44 +00:00
|
|
|
|
2008-02-12 19:49:57 +00:00
|
|
|
case Stmt::DeclStmtClass:
|
|
|
|
VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
|
|
|
|
break;
|
|
|
|
|
2008-01-24 02:02:54 +00:00
|
|
|
case Stmt::ImplicitCastExprClass: {
|
|
|
|
ImplicitCastExpr* C = cast<ImplicitCastExpr>(S);
|
|
|
|
VisitCast(C, C->getSubExpr(), Pred, Dst);
|
|
|
|
break;
|
|
|
|
}
|
2008-02-12 19:49:57 +00:00
|
|
|
|
|
|
|
case Stmt::ParenExprClass:
|
|
|
|
Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst);
|
2008-01-24 02:02:54 +00:00
|
|
|
break;
|
|
|
|
|
2008-02-12 19:49:57 +00:00
|
|
|
case Stmt::SizeOfAlignOfTypeExprClass:
|
|
|
|
VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst);
|
2008-02-05 00:26:40 +00:00
|
|
|
break;
|
2008-02-12 19:49:57 +00:00
|
|
|
|
|
|
|
case Stmt::StmtExprClass: {
|
|
|
|
StmtExpr* SE = cast<StmtExpr>(S);
|
|
|
|
|
|
|
|
StateTy St = Pred->getState();
|
|
|
|
Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin());
|
2008-02-21 18:02:17 +00:00
|
|
|
Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr)));
|
2008-02-12 19:49:57 +00:00
|
|
|
break;
|
2008-02-05 00:26:40 +00:00
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
// FIXME: We may wish to always bind state to ReturnStmts so
|
|
|
|
// that users can quickly query what was the state at the
|
|
|
|
// exit points of a function.
|
|
|
|
|
2008-02-12 19:49:57 +00:00
|
|
|
case Stmt::ReturnStmtClass: {
|
2008-02-07 01:08:27 +00:00
|
|
|
if (Expr* R = cast<ReturnStmt>(S)->getRetValue())
|
|
|
|
Visit(R, Pred, Dst);
|
|
|
|
else
|
|
|
|
Dst.Add(Pred);
|
|
|
|
|
|
|
|
break;
|
2008-02-12 19:49:57 +00:00
|
|
|
}
|
2008-02-07 01:08:27 +00:00
|
|
|
|
2008-02-20 04:02:35 +00:00
|
|
|
case Stmt::UnaryOperatorClass: {
|
|
|
|
UnaryOperator* U = cast<UnaryOperator>(S);
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
switch (U->getOpcode()) {
|
|
|
|
case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break;
|
|
|
|
case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break;
|
|
|
|
case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break;
|
|
|
|
default: VisitUnaryOperator(U, Pred, Dst); break;
|
|
|
|
}
|
2008-02-20 04:02:35 +00:00
|
|
|
|
2008-01-24 20:55:43 +00:00
|
|
|
break;
|
2008-02-20 04:02:35 +00:00
|
|
|
}
|
2008-01-17 18:25:22 +00:00
|
|
|
}
|
2008-01-16 19:42:59 +00:00
|
|
|
}
|
|
|
|
|
2008-01-30 23:03:39 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// "Assume" logic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LVal Cond,
|
2008-02-18 22:57:02 +00:00
|
|
|
bool Assumption,
|
2008-02-21 18:02:17 +00:00
|
|
|
bool& isFeasible) {
|
2008-02-01 06:36:40 +00:00
|
|
|
switch (Cond.getSubKind()) {
|
|
|
|
default:
|
2008-02-21 18:02:17 +00:00
|
|
|
assert (false && "'Assume' not implemented for this LVal.");
|
2008-02-01 06:36:40 +00:00
|
|
|
return St;
|
|
|
|
|
2008-02-06 00:54:14 +00:00
|
|
|
case lval::SymbolValKind:
|
|
|
|
if (Assumption)
|
|
|
|
return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
|
|
|
|
ValMgr.getZeroWithPtrWidth(), isFeasible);
|
|
|
|
else
|
|
|
|
return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
|
|
|
|
ValMgr.getZeroWithPtrWidth(), isFeasible);
|
|
|
|
|
2008-02-06 04:31:33 +00:00
|
|
|
|
2008-02-05 21:52:21 +00:00
|
|
|
case lval::DeclValKind:
|
2008-02-22 00:54:56 +00:00
|
|
|
case lval::FuncValKind:
|
|
|
|
case lval::GotoLabelKind:
|
2008-02-01 06:36:40 +00:00
|
|
|
isFeasible = Assumption;
|
|
|
|
return St;
|
2008-02-06 00:54:14 +00:00
|
|
|
|
2008-02-05 21:52:21 +00:00
|
|
|
case lval::ConcreteIntKind: {
|
|
|
|
bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
|
2008-02-01 06:36:40 +00:00
|
|
|
isFeasible = b ? Assumption : !Assumption;
|
|
|
|
return St;
|
|
|
|
}
|
|
|
|
}
|
2008-01-30 23:03:39 +00:00
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLVal Cond,
|
2008-02-06 00:54:14 +00:00
|
|
|
bool Assumption,
|
2008-02-21 18:02:17 +00:00
|
|
|
bool& isFeasible) {
|
2008-01-30 23:03:39 +00:00
|
|
|
switch (Cond.getSubKind()) {
|
|
|
|
default:
|
2008-02-21 18:02:17 +00:00
|
|
|
assert (false && "'Assume' not implemented for this NonLVal.");
|
2008-01-30 23:03:39 +00:00
|
|
|
return St;
|
|
|
|
|
2008-02-06 17:32:17 +00:00
|
|
|
|
|
|
|
case nonlval::SymbolValKind: {
|
2008-02-12 21:37:25 +00:00
|
|
|
nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
|
2008-02-06 17:32:17 +00:00
|
|
|
SymbolID sym = SV.getSymbol();
|
|
|
|
|
|
|
|
if (Assumption)
|
|
|
|
return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
|
|
|
|
isFeasible);
|
|
|
|
else
|
|
|
|
return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)),
|
|
|
|
isFeasible);
|
|
|
|
}
|
|
|
|
|
2008-02-06 04:31:33 +00:00
|
|
|
case nonlval::SymIntConstraintValKind:
|
|
|
|
return
|
|
|
|
AssumeSymInt(St, Assumption,
|
|
|
|
cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
|
|
|
|
isFeasible);
|
|
|
|
|
2008-02-05 21:52:21 +00:00
|
|
|
case nonlval::ConcreteIntKind: {
|
|
|
|
bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
|
2008-01-30 23:03:39 +00:00
|
|
|
isFeasible = b ? Assumption : !Assumption;
|
|
|
|
return St;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
GRExprEngine::StateTy
|
|
|
|
GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym,
|
2008-02-06 00:54:14 +00:00
|
|
|
const llvm::APSInt& V, bool& isFeasible) {
|
2008-02-18 22:57:02 +00:00
|
|
|
|
2008-02-06 00:54:14 +00:00
|
|
|
// First, determine if sym == X, where X != V.
|
|
|
|
if (const llvm::APSInt* X = St.getSymVal(sym)) {
|
|
|
|
isFeasible = *X != V;
|
|
|
|
return St;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second, determine if sym != V.
|
|
|
|
if (St.isNotEqual(sym, V)) {
|
|
|
|
isFeasible = true;
|
|
|
|
return St;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here, sym is not a constant and we don't know if it is != V.
|
|
|
|
// Make that assumption.
|
|
|
|
|
|
|
|
isFeasible = true;
|
|
|
|
return StateMgr.AddNE(St, sym, V);
|
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
GRExprEngine::StateTy
|
|
|
|
GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym,
|
2008-02-06 00:54:14 +00:00
|
|
|
const llvm::APSInt& V, bool& isFeasible) {
|
|
|
|
|
|
|
|
// First, determine if sym == X, where X != V.
|
|
|
|
if (const llvm::APSInt* X = St.getSymVal(sym)) {
|
|
|
|
isFeasible = *X == V;
|
|
|
|
return St;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second, determine if sym != V.
|
|
|
|
if (St.isNotEqual(sym, V)) {
|
|
|
|
isFeasible = false;
|
|
|
|
return St;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here, sym is not a constant and we don't know if it is == V.
|
|
|
|
// Make that assumption.
|
|
|
|
|
|
|
|
isFeasible = true;
|
|
|
|
return StateMgr.AddEQ(St, sym, V);
|
|
|
|
}
|
2008-01-30 23:03:39 +00:00
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
GRExprEngine::StateTy
|
|
|
|
GRExprEngine::AssumeSymInt(StateTy St, bool Assumption,
|
2008-02-06 04:31:33 +00:00
|
|
|
const SymIntConstraint& C, bool& isFeasible) {
|
|
|
|
|
|
|
|
switch (C.getOpcode()) {
|
|
|
|
default:
|
|
|
|
// No logic yet for other operators.
|
|
|
|
return St;
|
|
|
|
|
|
|
|
case BinaryOperator::EQ:
|
|
|
|
if (Assumption)
|
|
|
|
return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
|
|
|
|
else
|
|
|
|
return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
|
|
|
|
|
|
|
|
case BinaryOperator::NE:
|
|
|
|
if (Assumption)
|
|
|
|
return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
|
|
|
|
else
|
|
|
|
return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-16 18:18:48 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-02-14 22:36:46 +00:00
|
|
|
// Visualization.
|
2008-01-16 18:18:48 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-16 21:46:15 +00:00
|
|
|
#ifndef NDEBUG
|
2008-02-13 17:41:41 +00:00
|
|
|
static GRExprEngine* GraphPrintCheckerState;
|
2008-01-30 23:24:39 +00:00
|
|
|
|
2008-01-16 21:46:15 +00:00
|
|
|
namespace llvm {
|
|
|
|
template<>
|
2008-02-13 17:41:41 +00:00
|
|
|
struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> :
|
2008-01-16 21:46:15 +00:00
|
|
|
public DefaultDOTGraphTraits {
|
2008-02-08 21:10:02 +00:00
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) {
|
2008-02-08 21:10:02 +00:00
|
|
|
|
|
|
|
Out << "Variables:\\l";
|
|
|
|
|
2008-01-24 22:27:20 +00:00
|
|
|
bool isFirst = true;
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(),
|
2008-02-08 21:10:02 +00:00
|
|
|
E=St.vb_end(); I!=E;++I) {
|
|
|
|
|
|
|
|
if (isFirst)
|
|
|
|
isFirst = false;
|
|
|
|
else
|
|
|
|
Out << "\\l";
|
|
|
|
|
|
|
|
Out << ' ' << I.getKey()->getName() << " : ";
|
|
|
|
I.getData().print(Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-02-11 19:21:59 +00:00
|
|
|
|
2008-02-13 18:06:44 +00:00
|
|
|
static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
|
2008-02-11 19:21:59 +00:00
|
|
|
|
2008-02-08 21:10:02 +00:00
|
|
|
bool isFirst = true;
|
2008-02-11 19:21:59 +00:00
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end();
|
2008-02-11 19:21:59 +00:00
|
|
|
I != E;++I) {
|
2008-02-08 21:10:02 +00:00
|
|
|
|
2008-02-11 19:21:59 +00:00
|
|
|
if (isFirst) {
|
|
|
|
Out << "\\l\\lSub-Expressions:\\l";
|
|
|
|
isFirst = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Out << "\\l";
|
|
|
|
|
|
|
|
Out << " (" << (void*) I.getKey() << ") ";
|
|
|
|
I.getKey()->printPretty(Out);
|
|
|
|
Out << " : ";
|
|
|
|
I.getData().print(Out);
|
|
|
|
}
|
|
|
|
}
|
2008-01-24 22:27:20 +00:00
|
|
|
|
2008-02-13 18:06:44 +00:00
|
|
|
static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){
|
2008-02-11 19:21:59 +00:00
|
|
|
|
|
|
|
bool isFirst = true;
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end();
|
2008-02-11 19:21:59 +00:00
|
|
|
I != E; ++I) {
|
2008-01-24 22:27:20 +00:00
|
|
|
if (isFirst) {
|
2008-02-11 19:21:59 +00:00
|
|
|
Out << "\\l\\lBlock-level Expressions:\\l";
|
2008-01-24 22:27:20 +00:00
|
|
|
isFirst = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Out << "\\l";
|
2008-02-08 21:10:02 +00:00
|
|
|
|
2008-02-11 19:21:59 +00:00
|
|
|
Out << " (" << (void*) I.getKey() << ") ";
|
|
|
|
I.getKey()->printPretty(Out);
|
2008-01-24 22:27:20 +00:00
|
|
|
Out << " : ";
|
|
|
|
I.getData().print(Out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) {
|
2008-02-21 18:02:17 +00:00
|
|
|
ValueState::ConstEqTy CE = St.getImpl()->ConstEq;
|
2008-02-06 03:56:15 +00:00
|
|
|
|
|
|
|
if (CE.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Out << "\\l\\|'==' constraints:";
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I)
|
2008-02-06 03:56:15 +00:00
|
|
|
Out << "\\l $" << I.getKey() << " : " << I.getData()->toString();
|
|
|
|
}
|
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) {
|
2008-02-21 18:02:17 +00:00
|
|
|
ValueState::ConstNotEqTy NE = St.getImpl()->ConstNotEq;
|
2008-02-06 03:56:15 +00:00
|
|
|
|
|
|
|
if (NE.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Out << "\\l\\|'!=' constraints:";
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end();
|
2008-02-06 03:56:15 +00:00
|
|
|
I != EI; ++I){
|
|
|
|
|
|
|
|
Out << "\\l $" << I.getKey() << " : ";
|
|
|
|
bool isFirst = true;
|
|
|
|
|
|
|
|
ValueState::IntSetTy::iterator J=I.getData().begin(),
|
|
|
|
EJ=I.getData().end();
|
|
|
|
for ( ; J != EJ; ++J) {
|
|
|
|
if (isFirst) isFirst = false;
|
|
|
|
else Out << ", ";
|
|
|
|
|
|
|
|
Out << (*J)->toString();
|
|
|
|
}
|
|
|
|
}
|
2008-02-14 22:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) {
|
|
|
|
|
|
|
|
if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
|
2008-02-19 00:22:37 +00:00
|
|
|
GraphPrintCheckerState->isExplicitNullDeref(N) ||
|
2008-02-19 20:53:06 +00:00
|
|
|
GraphPrintCheckerState->isUninitDeref(N) ||
|
2008-02-19 00:22:37 +00:00
|
|
|
GraphPrintCheckerState->isUninitStore(N) ||
|
2008-02-26 21:31:18 +00:00
|
|
|
GraphPrintCheckerState->isUninitControlFlow(N) ||
|
|
|
|
GraphPrintCheckerState->isBadDivide(N))
|
2008-02-14 22:54:53 +00:00
|
|
|
return "color=\"red\",style=\"filled\"";
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
2008-02-06 03:56:15 +00:00
|
|
|
|
2008-02-13 17:41:41 +00:00
|
|
|
static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) {
|
2008-01-16 21:46:15 +00:00
|
|
|
std::ostringstream Out;
|
2008-01-23 22:30:44 +00:00
|
|
|
|
|
|
|
// Program Location.
|
2008-01-16 21:46:15 +00:00
|
|
|
ProgramPoint Loc = N->getLocation();
|
|
|
|
|
|
|
|
switch (Loc.getKind()) {
|
|
|
|
case ProgramPoint::BlockEntranceKind:
|
|
|
|
Out << "Block Entrance: B"
|
|
|
|
<< cast<BlockEntrance>(Loc).getBlock()->getBlockID();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ProgramPoint::BlockExitKind:
|
|
|
|
assert (false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ProgramPoint::PostStmtKind: {
|
|
|
|
const PostStmt& L = cast<PostStmt>(Loc);
|
2008-01-24 22:27:20 +00:00
|
|
|
Out << L.getStmt()->getStmtClassName() << ':'
|
|
|
|
<< (void*) L.getStmt() << ' ';
|
|
|
|
|
2008-01-16 21:46:15 +00:00
|
|
|
L.getStmt()->printPretty(Out);
|
2008-02-07 05:48:01 +00:00
|
|
|
|
|
|
|
if (GraphPrintCheckerState->isImplicitNullDeref(N)) {
|
|
|
|
Out << "\\|Implicit-Null Dereference.\\l";
|
|
|
|
}
|
2008-02-07 06:04:18 +00:00
|
|
|
else if (GraphPrintCheckerState->isExplicitNullDeref(N)) {
|
|
|
|
Out << "\\|Explicit-Null Dereference.\\l";
|
|
|
|
}
|
2008-02-19 20:53:06 +00:00
|
|
|
else if (GraphPrintCheckerState->isUninitDeref(N)) {
|
|
|
|
Out << "\\|Dereference of uninitialied value.\\l";
|
|
|
|
}
|
2008-02-19 00:22:37 +00:00
|
|
|
else if (GraphPrintCheckerState->isUninitStore(N)) {
|
2008-02-21 18:02:17 +00:00
|
|
|
Out << "\\|Store to Uninitialized LVal.";
|
2008-02-19 00:22:37 +00:00
|
|
|
}
|
2008-02-26 21:31:18 +00:00
|
|
|
else if (GraphPrintCheckerState->isBadDivide(N)) {
|
|
|
|
Out << "\\|Divide-by zero or uninitialized value.";
|
|
|
|
}
|
2008-02-07 05:48:01 +00:00
|
|
|
|
2008-01-16 21:46:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
const BlockEdge& E = cast<BlockEdge>(Loc);
|
|
|
|
Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
|
|
|
|
<< E.getDst()->getBlockID() << ')';
|
2008-01-30 23:03:39 +00:00
|
|
|
|
|
|
|
if (Stmt* T = E.getSrc()->getTerminator()) {
|
|
|
|
Out << "\\|Terminator: ";
|
|
|
|
E.getSrc()->printTerminator(Out);
|
|
|
|
|
2008-02-13 23:08:21 +00:00
|
|
|
if (isa<SwitchStmt>(T)) {
|
|
|
|
Stmt* Label = E.getDst()->getLabel();
|
|
|
|
|
|
|
|
if (Label) {
|
|
|
|
if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
|
|
|
|
Out << "\\lcase ";
|
|
|
|
C->getLHS()->printPretty(Out);
|
|
|
|
|
|
|
|
if (Stmt* RHS = C->getRHS()) {
|
|
|
|
Out << " .. ";
|
|
|
|
RHS->printPretty(Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << ":";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert (isa<DefaultStmt>(Label));
|
|
|
|
Out << "\\ldefault:";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Out << "\\l(implicit) default:";
|
|
|
|
}
|
|
|
|
else if (isa<IndirectGotoStmt>(T)) {
|
2008-01-30 23:03:39 +00:00
|
|
|
// FIXME
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Out << "\\lCondition: ";
|
|
|
|
if (*E.getSrc()->succ_begin() == E.getDst())
|
|
|
|
Out << "true";
|
|
|
|
else
|
|
|
|
Out << "false";
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << "\\l";
|
|
|
|
}
|
2008-01-30 23:24:39 +00:00
|
|
|
|
|
|
|
if (GraphPrintCheckerState->isUninitControlFlow(N)) {
|
|
|
|
Out << "\\|Control-flow based on\\lUninitialized value.\\l";
|
|
|
|
}
|
2008-01-16 21:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-05 07:17:49 +00:00
|
|
|
Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|";
|
2008-02-08 21:10:02 +00:00
|
|
|
|
2008-02-11 19:21:59 +00:00
|
|
|
N->getState().printDOT(Out);
|
2008-01-16 21:46:15 +00:00
|
|
|
|
2008-01-23 22:30:44 +00:00
|
|
|
Out << "\\l";
|
2008-01-16 21:46:15 +00:00
|
|
|
return Out.str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end llvm namespace
|
|
|
|
#endif
|
|
|
|
|
2008-02-14 22:36:46 +00:00
|
|
|
void GRExprEngine::ViewGraph() {
|
2008-01-16 21:46:15 +00:00
|
|
|
#ifndef NDEBUG
|
2008-02-14 22:36:46 +00:00
|
|
|
GraphPrintCheckerState = this;
|
|
|
|
llvm::ViewGraph(*G.roots_begin(), "GRExprEngine");
|
2008-01-30 23:24:39 +00:00
|
|
|
GraphPrintCheckerState = NULL;
|
2008-02-14 22:36:46 +00:00
|
|
|
#endif
|
2008-01-16 18:18:48 +00:00
|
|
|
}
|