2009-10-31 01:24:47 +08:00
|
|
|
//== NullDerefChecker.cpp - Null dereference checker ------------*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-12-23 02:53:44 +08:00
|
|
|
// This defines NullDerefChecker, a builtin check in ExprEngine that performs
|
2009-10-31 01:24:47 +08:00
|
|
|
// checks for null pointers at loads and stores.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-03-01 01:36:18 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2009-10-31 01:24:47 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-10-31 01:24:47 +08:00
|
|
|
|
2009-11-11 11:26:34 +08:00
|
|
|
namespace {
|
2011-03-01 01:36:18 +08:00
|
|
|
class DereferenceChecker
|
2011-03-01 09:16:21 +08:00
|
|
|
: public Checker< check::Location,
|
2011-03-01 01:36:18 +08:00
|
|
|
EventDispatcher<ImplicitNullDerefEvent> > {
|
|
|
|
mutable llvm::OwningPtr<BuiltinBug> BT_null;
|
|
|
|
mutable llvm::OwningPtr<BuiltinBug> BT_undef;
|
|
|
|
|
2009-11-11 11:26:34 +08:00
|
|
|
public:
|
2011-03-01 01:36:18 +08:00
|
|
|
void checkLocation(SVal location, bool isLoad, CheckerContext &C) const;
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
static void AddDerefSource(raw_ostream &os,
|
|
|
|
SmallVectorImpl<SourceRange> &Ranges,
|
2011-03-01 01:36:18 +08:00
|
|
|
const Expr *Ex, bool loadedFrom = false);
|
2009-11-11 11:26:34 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2009-10-31 01:24:47 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void DereferenceChecker::AddDerefSource(raw_ostream &os,
|
|
|
|
SmallVectorImpl<SourceRange> &Ranges,
|
2010-10-26 08:06:13 +08:00
|
|
|
const Expr *Ex,
|
|
|
|
bool loadedFrom) {
|
2010-12-04 11:47:34 +08:00
|
|
|
Ex = Ex->IgnoreParenLValueCasts();
|
2010-10-26 08:06:13 +08:00
|
|
|
switch (Ex->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
case Stmt::DeclRefExprClass: {
|
|
|
|
const DeclRefExpr *DR = cast<DeclRefExpr>(Ex);
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
|
|
|
os << " (" << (loadedFrom ? "loaded from" : "from")
|
|
|
|
<< " variable '" << VD->getName() << "')";
|
|
|
|
Ranges.push_back(DR->getSourceRange());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case Stmt::MemberExprClass: {
|
|
|
|
const MemberExpr *ME = cast<MemberExpr>(Ex);
|
|
|
|
os << " (" << (loadedFrom ? "loaded from" : "via")
|
|
|
|
<< " field '" << ME->getMemberNameInfo() << "')";
|
|
|
|
SourceLocation L = ME->getMemberLoc();
|
|
|
|
Ranges.push_back(SourceRange(L, L));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
void DereferenceChecker::checkLocation(SVal l, bool isLoad,
|
|
|
|
CheckerContext &C) const {
|
2009-11-11 11:26:34 +08:00
|
|
|
// Check for dereference of an undefined value.
|
|
|
|
if (l.isUndef()) {
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2009-11-11 11:26:34 +08:00
|
|
|
if (!BT_undef)
|
2011-03-01 01:36:18 +08:00
|
|
|
BT_undef.reset(new BuiltinBug("Dereference of undefined pointer value"));
|
2010-03-23 09:11:38 +08:00
|
|
|
|
2009-11-11 11:26:34 +08:00
|
|
|
EnhancedBugReport *report =
|
2009-11-14 20:08:24 +08:00
|
|
|
new EnhancedBugReport(*BT_undef, BT_undef->getDescription(), N);
|
2009-11-11 11:26:34 +08:00
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
|
|
|
|
bugreporter::GetDerefExpr(N));
|
|
|
|
C.EmitReport(report);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2010-03-23 09:11:38 +08:00
|
|
|
|
2009-11-11 11:26:34 +08:00
|
|
|
DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(l);
|
2010-03-23 09:11:38 +08:00
|
|
|
|
|
|
|
// Check for null dereferences.
|
2009-11-11 11:26:34 +08:00
|
|
|
if (!isa<Loc>(location))
|
|
|
|
return;
|
2010-03-23 09:11:38 +08:00
|
|
|
|
2011-03-01 01:36:18 +08:00
|
|
|
const Stmt *S = C.getStmt();
|
2009-11-11 11:26:34 +08:00
|
|
|
const GRState *state = C.getState();
|
|
|
|
const GRState *notNullState, *nullState;
|
2010-12-02 06:16:56 +08:00
|
|
|
llvm::tie(notNullState, nullState) = state->assume(location);
|
2010-03-23 09:11:38 +08:00
|
|
|
|
2009-11-11 11:26:34 +08:00
|
|
|
// The explicit NULL case.
|
|
|
|
if (nullState) {
|
2010-03-23 09:11:38 +08:00
|
|
|
if (!notNullState) {
|
2009-11-21 09:50:48 +08:00
|
|
|
// Generate an error node.
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink(nullState);
|
2009-11-21 09:50:48 +08:00
|
|
|
if (!N)
|
2009-11-11 11:26:34 +08:00
|
|
|
return;
|
2010-03-23 09:11:38 +08:00
|
|
|
|
2009-11-21 09:50:48 +08:00
|
|
|
// We know that 'location' cannot be non-null. This is what
|
2010-03-23 09:11:38 +08:00
|
|
|
// we call an "explicit" null dereference.
|
2009-11-21 09:50:48 +08:00
|
|
|
if (!BT_null)
|
2011-03-01 01:36:18 +08:00
|
|
|
BT_null.reset(new BuiltinBug("Dereference of null pointer"));
|
2010-03-23 09:11:38 +08:00
|
|
|
|
2009-11-24 09:33:10 +08:00
|
|
|
llvm::SmallString<100> buf;
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<SourceRange, 2> Ranges;
|
2010-12-16 15:46:53 +08:00
|
|
|
|
|
|
|
// Walk through lvalue casts to get the original expression
|
|
|
|
// that syntactically caused the load.
|
|
|
|
if (const Expr *expr = dyn_cast<Expr>(S))
|
|
|
|
S = expr->IgnoreParenLValueCasts();
|
2009-11-24 09:33:10 +08:00
|
|
|
|
|
|
|
switch (S->getStmtClass()) {
|
2010-10-26 08:06:13 +08:00
|
|
|
case Stmt::ArraySubscriptExprClass: {
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
os << "Array access";
|
|
|
|
const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(S);
|
|
|
|
AddDerefSource(os, Ranges, AE->getBase()->IgnoreParenCasts());
|
|
|
|
os << " results in a null pointer dereference";
|
|
|
|
break;
|
|
|
|
}
|
2009-11-24 09:33:10 +08:00
|
|
|
case Stmt::UnaryOperatorClass: {
|
2010-10-26 08:06:13 +08:00
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
os << "Dereference of null pointer";
|
2009-11-24 09:33:10 +08:00
|
|
|
const UnaryOperator *U = cast<UnaryOperator>(S);
|
2010-10-26 08:06:13 +08:00
|
|
|
AddDerefSource(os, Ranges, U->getSubExpr()->IgnoreParens(), true);
|
2010-03-23 09:11:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Stmt::MemberExprClass: {
|
|
|
|
const MemberExpr *M = cast<MemberExpr>(S);
|
2010-10-26 08:06:13 +08:00
|
|
|
if (M->isArrow()) {
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
os << "Access to field '" << M->getMemberNameInfo()
|
|
|
|
<< "' results in a dereference of a null pointer";
|
|
|
|
AddDerefSource(os, Ranges, M->getBase()->IgnoreParenCasts(), true);
|
|
|
|
}
|
2010-03-23 09:11:38 +08:00
|
|
|
break;
|
2009-11-24 09:33:10 +08:00
|
|
|
}
|
2010-09-11 04:20:49 +08:00
|
|
|
case Stmt::ObjCIvarRefExprClass: {
|
|
|
|
const ObjCIvarRefExpr *IV = cast<ObjCIvarRefExpr>(S);
|
|
|
|
if (const DeclRefExpr *DR =
|
|
|
|
dyn_cast<DeclRefExpr>(IV->getBase()->IgnoreParenCasts())) {
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
os << "Instance variable access (via '" << VD->getName()
|
|
|
|
<< "') results in a null pointer dereference";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ranges.push_back(IV->getSourceRange());
|
|
|
|
break;
|
|
|
|
}
|
2009-11-24 09:33:10 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-11-04 02:41:06 +08:00
|
|
|
|
2009-11-21 09:50:48 +08:00
|
|
|
EnhancedBugReport *report =
|
2009-11-24 09:33:10 +08:00
|
|
|
new EnhancedBugReport(*BT_null,
|
|
|
|
buf.empty() ? BT_null->getDescription():buf.str(),
|
|
|
|
N);
|
|
|
|
|
2009-11-21 09:50:48 +08:00
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
|
|
|
|
bugreporter::GetDerefExpr(N));
|
2010-03-23 09:11:38 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
for (SmallVectorImpl<SourceRange>::iterator
|
2010-03-23 09:11:38 +08:00
|
|
|
I = Ranges.begin(), E = Ranges.end(); I!=E; ++I)
|
|
|
|
report->addRange(*I);
|
|
|
|
|
2009-11-21 09:50:48 +08:00
|
|
|
C.EmitReport(report);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
2009-11-11 11:26:34 +08:00
|
|
|
// Otherwise, we have the case where the location could either be
|
|
|
|
// null or not-null. Record the error node as an "implicit" null
|
2010-03-23 09:11:38 +08:00
|
|
|
// dereference.
|
2011-03-01 01:36:18 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink(nullState)) {
|
|
|
|
ImplicitNullDerefEvent event = { l, isLoad, N, &C.getBugReporter() };
|
|
|
|
dispatchEvent(event);
|
|
|
|
}
|
2009-11-04 02:41:06 +08:00
|
|
|
}
|
|
|
|
}
|
2010-03-23 09:11:38 +08:00
|
|
|
|
2009-11-11 11:26:34 +08:00
|
|
|
// From this point forward, we know that the location is not null.
|
2009-11-24 06:22:01 +08:00
|
|
|
C.addTransition(notNullState);
|
2009-11-04 02:41:06 +08:00
|
|
|
}
|
2011-03-01 01:36:18 +08:00
|
|
|
|
|
|
|
void ento::registerDereferenceChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<DereferenceChecker>();
|
|
|
|
}
|