2009-07-23 01:55:28 +08:00
|
|
|
// BugReporterVisitors.cpp - Helpers for reporting bugs -----------*- 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 a set of BugReporter "visitors" which can be used to
|
|
|
|
// enhance the diagnostics reported for a bug.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/ExprObjC.h"
|
2010-01-26 01:10:22 +08:00
|
|
|
#include "clang/Checker/BugReporter/BugReporter.h"
|
|
|
|
#include "clang/Checker/BugReporter/PathDiagnostic.h"
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "clang/Checker/PathSensitive/GRState.h"
|
2009-07-23 01:55:28 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
const Stmt *clang::bugreporter::GetDerefExpr(const ExplodedNode *N) {
|
2009-07-23 01:55:28 +08:00
|
|
|
// Pattern match for a few useful cases (do something smarter later):
|
|
|
|
// a[0], p->f, *p
|
|
|
|
const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
|
|
|
|
if (U->getOpcode() == UnaryOperator::Deref)
|
|
|
|
return U->getSubExpr()->IgnoreParenCasts();
|
|
|
|
}
|
|
|
|
else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
|
|
|
|
return ME->getBase()->IgnoreParenCasts();
|
|
|
|
}
|
|
|
|
else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
|
|
|
|
// Retrieve the base for arrays since BasicStoreManager doesn't know how
|
|
|
|
// to reason about them.
|
|
|
|
return AE->getBase();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
return NULL;
|
2009-07-23 01:55:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Stmt*
|
2009-08-06 09:32:16 +08:00
|
|
|
clang::bugreporter::GetReceiverExpr(const ExplodedNode *N){
|
2009-07-23 01:55:28 +08:00
|
|
|
const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
|
|
|
|
if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))
|
|
|
|
return ME->getReceiver();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Stmt*
|
2009-08-06 09:32:16 +08:00
|
|
|
clang::bugreporter::GetDenomExpr(const ExplodedNode *N) {
|
2009-09-02 21:26:26 +08:00
|
|
|
const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
|
2009-07-23 01:55:28 +08:00
|
|
|
if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
|
|
|
|
return BE->getRHS();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Stmt*
|
2009-08-06 09:32:16 +08:00
|
|
|
clang::bugreporter::GetCalleeExpr(const ExplodedNode *N) {
|
2009-09-02 16:10:35 +08:00
|
|
|
// Callee is checked as a PreVisit to the CallExpr.
|
|
|
|
const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
|
2009-07-23 01:55:28 +08:00
|
|
|
if (const CallExpr *CE = dyn_cast<CallExpr>(S))
|
|
|
|
return CE->getCallee();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Stmt*
|
2009-08-06 09:32:16 +08:00
|
|
|
clang::bugreporter::GetRetValExpr(const ExplodedNode *N) {
|
2009-07-23 01:55:28 +08:00
|
|
|
const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
|
|
|
|
if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
|
|
|
|
return RS->getRetValue();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Definitions for bug reporter visitors.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class FindLastStoreBRVisitor : public BugReporterVisitor {
|
2009-07-23 01:55:28 +08:00
|
|
|
const MemRegion *R;
|
|
|
|
SVal V;
|
|
|
|
bool satisfied;
|
2009-08-06 09:32:16 +08:00
|
|
|
const ExplodedNode *StoreSite;
|
2009-07-23 01:55:28 +08:00
|
|
|
public:
|
|
|
|
FindLastStoreBRVisitor(SVal v, const MemRegion *r)
|
|
|
|
: R(r), V(v), satisfied(false), StoreSite(0) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
|
|
|
|
const ExplodedNode *PrevN,
|
2009-07-23 01:55:28 +08:00
|
|
|
BugReporterContext& BRC) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (satisfied)
|
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (!StoreSite) {
|
2009-08-06 09:32:16 +08:00
|
|
|
const ExplodedNode *Node = N, *Last = NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
for ( ; Node ; Last = Node, Node = Node->getFirstPred()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
|
|
|
|
if (const PostStmt *P = Node->getLocationAs<PostStmt>())
|
|
|
|
if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
|
|
|
|
if (DS->getSingleDecl() == VR->getDecl()) {
|
|
|
|
Last = Node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-09 00:18:51 +08:00
|
|
|
if (Node->getState()->getSVal(R) != V)
|
2009-07-23 01:55:28 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (!Node || !Last) {
|
|
|
|
satisfied = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
StoreSite = Last;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (StoreSite != N)
|
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
satisfied = true;
|
|
|
|
std::string sbuf;
|
|
|
|
llvm::raw_string_ostream os(sbuf);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
|
|
|
|
if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
|
|
|
|
os << "Variable '" << VR->getDecl()->getNameAsString() << "' ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (isa<loc::ConcreteInt>(V)) {
|
|
|
|
bool b = false;
|
|
|
|
ASTContext &C = BRC.getASTContext();
|
|
|
|
if (R->isBoundable()) {
|
|
|
|
if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
|
|
|
|
if (TR->getValueType(C)->isObjCObjectPointerType()) {
|
|
|
|
os << "initialized to nil";
|
|
|
|
b = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (!b)
|
|
|
|
os << "initialized to a null pointer value";
|
|
|
|
}
|
|
|
|
else if (isa<nonloc::ConcreteInt>(V)) {
|
|
|
|
os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
|
|
|
|
}
|
|
|
|
else if (V.isUndef()) {
|
|
|
|
if (isa<VarRegion>(R)) {
|
|
|
|
const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
|
|
|
|
if (VD->getInit())
|
|
|
|
os << "initialized to a garbage value";
|
|
|
|
else
|
2009-09-09 23:08:12 +08:00
|
|
|
os << "declared without an initial value";
|
|
|
|
}
|
2009-07-23 01:55:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (os.str().empty()) {
|
2009-07-23 01:55:28 +08:00
|
|
|
if (isa<loc::ConcreteInt>(V)) {
|
|
|
|
bool b = false;
|
|
|
|
ASTContext &C = BRC.getASTContext();
|
|
|
|
if (R->isBoundable()) {
|
|
|
|
if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
|
|
|
|
if (TR->getValueType(C)->isObjCObjectPointerType()) {
|
|
|
|
os << "nil object reference stored to ";
|
|
|
|
b = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (!b)
|
|
|
|
os << "Null pointer value stored to ";
|
|
|
|
}
|
|
|
|
else if (V.isUndef()) {
|
|
|
|
os << "Uninitialized value stored to ";
|
|
|
|
}
|
2009-08-18 09:05:30 +08:00
|
|
|
else if (isa<nonloc::ConcreteInt>(V)) {
|
|
|
|
os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
|
|
|
|
<< " is assigned to ";
|
|
|
|
}
|
2009-07-23 01:55:28 +08:00
|
|
|
else
|
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
|
|
|
|
os << '\'' << VR->getDecl()->getNameAsString() << '\'';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
// FIXME: Refactor this into BugReporterContext.
|
2009-09-09 23:08:12 +08:00
|
|
|
const Stmt *S = 0;
|
2009-07-23 01:55:28 +08:00
|
|
|
ProgramPoint P = N->getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
|
|
|
|
CFGBlock *BSrc = BE->getSrc();
|
|
|
|
S = BSrc->getTerminatorCondition();
|
|
|
|
}
|
|
|
|
else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
|
|
|
|
S = PS->getStmt();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (!S)
|
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
// Construct a new PathDiagnosticPiece.
|
|
|
|
PathDiagnosticLocation L(S, BRC.getSourceManager());
|
|
|
|
return new PathDiagnosticEventPiece(L, os.str());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R,
|
|
|
|
SVal V) {
|
|
|
|
BRC.addVisitor(new FindLastStoreBRVisitor(V, R));
|
|
|
|
}
|
|
|
|
|
2009-11-28 14:07:30 +08:00
|
|
|
class TrackConstraintBRVisitor : public BugReporterVisitor {
|
2009-09-12 06:07:28 +08:00
|
|
|
DefinedSVal Constraint;
|
2009-07-23 01:55:28 +08:00
|
|
|
const bool Assumption;
|
|
|
|
bool isSatisfied;
|
|
|
|
public:
|
2009-09-12 06:07:28 +08:00
|
|
|
TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption)
|
2009-07-23 01:55:28 +08:00
|
|
|
: Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 09:32:16 +08:00
|
|
|
PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
|
|
|
|
const ExplodedNode *PrevN,
|
2009-07-23 01:55:28 +08:00
|
|
|
BugReporterContext& BRC) {
|
|
|
|
if (isSatisfied)
|
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
// Check if in the previous state it was feasible for this constraint
|
|
|
|
// to *not* be true.
|
2009-09-12 06:07:28 +08:00
|
|
|
if (PrevN->getState()->Assume(Constraint, !Assumption)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
isSatisfied = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
// As a sanity check, make sure that the negation of the constraint
|
|
|
|
// was infeasible in the current state. If it is feasible, we somehow
|
|
|
|
// missed the transition point.
|
2009-09-12 06:07:28 +08:00
|
|
|
if (N->getState()->Assume(Constraint, !Assumption))
|
2009-07-23 01:55:28 +08:00
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
// We found the transition point for the constraint. We now need to
|
2009-09-09 23:08:12 +08:00
|
|
|
// pretty-print the constraint. (work-in-progress)
|
2009-07-23 01:55:28 +08:00
|
|
|
std::string sbuf;
|
|
|
|
llvm::raw_string_ostream os(sbuf);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (isa<Loc>(Constraint)) {
|
|
|
|
os << "Assuming pointer value is ";
|
|
|
|
os << (Assumption ? "non-null" : "null");
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (os.str().empty())
|
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
// FIXME: Refactor this into BugReporterContext.
|
2009-09-09 23:08:12 +08:00
|
|
|
const Stmt *S = 0;
|
2009-07-23 01:55:28 +08:00
|
|
|
ProgramPoint P = N->getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
|
|
|
|
CFGBlock *BSrc = BE->getSrc();
|
|
|
|
S = BSrc->getTerminatorCondition();
|
|
|
|
}
|
|
|
|
else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) {
|
|
|
|
S = PS->getStmt();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (!S)
|
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
// Construct a new PathDiagnosticPiece.
|
|
|
|
PathDiagnosticLocation L(S, BRC.getSourceManager());
|
|
|
|
return new PathDiagnosticEventPiece(L, os.str());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
return NULL;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-07-23 01:55:28 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2009-09-12 06:07:28 +08:00
|
|
|
static void registerTrackConstraint(BugReporterContext& BRC,
|
|
|
|
DefinedSVal Constraint,
|
2009-07-23 01:55:28 +08:00
|
|
|
bool Assumption) {
|
2009-09-09 23:08:12 +08:00
|
|
|
BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption));
|
2009-07-23 01:55:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void clang::bugreporter::registerTrackNullOrUndefValue(BugReporterContext& BRC,
|
2009-08-18 09:05:30 +08:00
|
|
|
const void *data,
|
2009-08-06 09:32:16 +08:00
|
|
|
const ExplodedNode* N) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-18 09:05:30 +08:00
|
|
|
const Stmt *S = static_cast<const Stmt*>(data);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (!S)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
GRStateManager &StateMgr = BRC.getStateManager();
|
2009-09-09 23:08:12 +08:00
|
|
|
const GRState *state = N->getState();
|
|
|
|
|
|
|
|
if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) {
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
2009-07-23 01:55:28 +08:00
|
|
|
const VarRegion *R =
|
2009-08-22 06:28:32 +08:00
|
|
|
StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
// What did we load?
|
2010-02-09 00:18:51 +08:00
|
|
|
SVal V = state->getSVal(S);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)
|
2009-07-23 01:55:28 +08:00
|
|
|
|| V.isUndef()) {
|
|
|
|
registerFindLastStore(BRC, R, V);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-09 00:18:51 +08:00
|
|
|
SVal V = state->getSValAsScalarOrLoc(S);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
// Uncomment this to find cases where we aren't properly getting the
|
|
|
|
// base value that was dereferenced.
|
|
|
|
// assert(!V.isUnknownOrUndef());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
// Is it a symbolic value?
|
|
|
|
if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
|
|
|
|
const SubRegion *R = cast<SubRegion>(L->getRegion());
|
|
|
|
while (R && !isa<SymbolicRegion>(R)) {
|
|
|
|
R = dyn_cast<SubRegion>(R->getSuperRegion());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-23 01:55:28 +08:00
|
|
|
if (R) {
|
|
|
|
assert(isa<SymbolicRegion>(R));
|
|
|
|
registerTrackConstraint(BRC, loc::MemRegionVal(R), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|