2009-11-23 11:20:54 +08:00
|
|
|
//=== UndefBranchChecker.cpp -----------------------------------*- C++ -*--===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines UndefBranchChecker, which checks for undefined branch
|
|
|
|
// condition.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "GRExprEngineInternalChecks.h"
|
2010-03-28 05:19:47 +08:00
|
|
|
#include "clang/Checker/BugReporter/BugType.h"
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "clang/Checker/PathSensitive/Checker.h"
|
2009-11-23 11:20:54 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-11-28 14:07:30 +08:00
|
|
|
class UndefBranchChecker : public Checker {
|
2009-11-23 11:20:54 +08:00
|
|
|
BuiltinBug *BT;
|
2009-11-23 11:29:59 +08:00
|
|
|
|
2009-11-28 14:07:30 +08:00
|
|
|
struct FindUndefExpr {
|
2009-11-23 11:29:59 +08:00
|
|
|
GRStateManager& VM;
|
|
|
|
const GRState* St;
|
|
|
|
|
|
|
|
FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
|
|
|
|
|
2010-07-20 14:22:24 +08:00
|
|
|
const Expr* FindExpr(const Expr* Ex) {
|
2009-11-23 11:29:59 +08:00
|
|
|
if (!MatchesCriteria(Ex))
|
|
|
|
return 0;
|
|
|
|
|
2010-07-20 14:22:24 +08:00
|
|
|
for (Stmt::const_child_iterator I = Ex->child_begin(),
|
|
|
|
E = Ex->child_end();I!=E;++I)
|
|
|
|
if (const Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
|
|
|
|
const Expr* E2 = FindExpr(ExI);
|
2009-11-23 11:29:59 +08:00
|
|
|
if (E2) return E2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ex;
|
|
|
|
}
|
|
|
|
|
2010-07-20 14:22:24 +08:00
|
|
|
bool MatchesCriteria(const Expr* Ex) { return St->getSVal(Ex).isUndef(); }
|
2009-11-23 11:29:59 +08:00
|
|
|
};
|
|
|
|
|
2009-11-23 11:20:54 +08:00
|
|
|
public:
|
|
|
|
UndefBranchChecker() : BT(0) {}
|
|
|
|
static void *getTag();
|
|
|
|
void VisitBranchCondition(GRBranchNodeBuilder &Builder, GRExprEngine &Eng,
|
2010-07-20 14:22:24 +08:00
|
|
|
const Stmt *Condition, void *tag);
|
2009-11-23 11:20:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang::RegisterUndefBranchChecker(GRExprEngine &Eng) {
|
|
|
|
Eng.registerCheck(new UndefBranchChecker());
|
|
|
|
}
|
|
|
|
|
|
|
|
void *UndefBranchChecker::getTag() {
|
|
|
|
static int x;
|
|
|
|
return &x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UndefBranchChecker::VisitBranchCondition(GRBranchNodeBuilder &Builder,
|
|
|
|
GRExprEngine &Eng,
|
2010-07-20 14:22:24 +08:00
|
|
|
const Stmt *Condition, void *tag){
|
2009-11-23 11:20:54 +08:00
|
|
|
const GRState *state = Builder.getState();
|
2010-02-09 00:18:51 +08:00
|
|
|
SVal X = state->getSVal(Condition);
|
2009-11-23 11:20:54 +08:00
|
|
|
if (X.isUndef()) {
|
|
|
|
ExplodedNode *N = Builder.generateNode(state, true);
|
|
|
|
if (N) {
|
|
|
|
N->markAsSink();
|
|
|
|
if (!BT)
|
2009-11-24 01:58:48 +08:00
|
|
|
BT = new BuiltinBug("Branch condition evaluates to a garbage value");
|
2009-11-23 11:29:59 +08:00
|
|
|
|
|
|
|
// What's going on here: we want to highlight the subexpression of the
|
|
|
|
// condition that is the most likely source of the "uninitialized
|
|
|
|
// branch condition." We do a recursive walk of the condition's
|
|
|
|
// subexpressions and roughly look for the most nested subexpression
|
|
|
|
// that binds to Undefined. We then highlight that expression's range.
|
|
|
|
BlockEdge B = cast<BlockEdge>(N->getLocation());
|
2010-07-20 14:22:24 +08:00
|
|
|
const Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
|
2009-11-23 11:29:59 +08:00
|
|
|
assert (Ex && "Block must have a terminator.");
|
|
|
|
|
|
|
|
// Get the predecessor node and check if is a PostStmt with the Stmt
|
|
|
|
// being the terminator condition. We want to inspect the state
|
|
|
|
// of that node instead because it will contain main information about
|
|
|
|
// the subexpressions.
|
|
|
|
assert (!N->pred_empty());
|
|
|
|
|
|
|
|
// Note: any predecessor will do. They should have identical state,
|
|
|
|
// since all the BlockEdge did was act as an error sink since the value
|
|
|
|
// had to already be undefined.
|
|
|
|
ExplodedNode *PrevN = *N->pred_begin();
|
|
|
|
ProgramPoint P = PrevN->getLocation();
|
|
|
|
const GRState* St = N->getState();
|
|
|
|
|
|
|
|
if (PostStmt* PS = dyn_cast<PostStmt>(&P))
|
|
|
|
if (PS->getStmt() == Ex)
|
|
|
|
St = PrevN->getState();
|
|
|
|
|
|
|
|
FindUndefExpr FindIt(Eng.getStateManager(), St);
|
|
|
|
Ex = FindIt.FindExpr(Ex);
|
2009-11-24 02:12:03 +08:00
|
|
|
|
|
|
|
// Emit the bug report.
|
|
|
|
EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getDescription(),N);
|
|
|
|
R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex);
|
2009-11-23 11:29:59 +08:00
|
|
|
R->addRange(Ex->getSourceRange());
|
|
|
|
|
2009-11-23 11:20:54 +08:00
|
|
|
Eng.getBugReporter().EmitReport(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
Builder.markInfeasible(true);
|
|
|
|
Builder.markInfeasible(false);
|
|
|
|
}
|
|
|
|
}
|