2008-07-23 00:21:24 +08:00
|
|
|
//=-- GRExprEngineInternalChecks.cpp - Builtin GRExprEngine Checks---*- 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 the BugType classes used by GRExprEngine to report
|
|
|
|
// bugs derived from builtin checks in the path-sensitive engine.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
|
2009-07-23 05:46:56 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/CheckerVisitor.h"
|
2009-05-07 08:45:33 +08:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
2008-10-31 08:13:20 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-07-23 00:21:24 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-10-31 08:18:30 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-07-23 00:21:24 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2009-07-23 01:55:28 +08:00
|
|
|
using namespace clang::bugreporter;
|
2008-07-23 00:21:24 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
template <typename ITERATOR> inline
|
2008-08-13 12:27:00 +08:00
|
|
|
ExplodedNode<GRState>* GetNode(ITERATOR I) {
|
2008-07-23 00:21:24 +08:00
|
|
|
return *I;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <> inline
|
2008-08-13 12:27:00 +08:00
|
|
|
ExplodedNode<GRState>* GetNode(GRExprEngine::undef_arg_iterator I) {
|
2008-07-23 00:21:24 +08:00
|
|
|
return I->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Bug Descriptions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-05-07 08:45:33 +08:00
|
|
|
|
2009-05-14 03:16:35 +08:00
|
|
|
class VISIBILITY_HIDDEN BuiltinBugReport : public RangedBugReport {
|
2009-05-07 08:45:33 +08:00
|
|
|
public:
|
|
|
|
BuiltinBugReport(BugType& bt, const char* desc,
|
2009-05-14 03:16:35 +08:00
|
|
|
ExplodedNode<GRState> *n)
|
|
|
|
: RangedBugReport(bt, desc, n) {}
|
2009-05-07 08:45:33 +08:00
|
|
|
|
2009-05-15 13:25:09 +08:00
|
|
|
BuiltinBugReport(BugType& bt, const char *shortDesc, const char *desc,
|
|
|
|
ExplodedNode<GRState> *n)
|
|
|
|
: RangedBugReport(bt, shortDesc, desc, n) {}
|
|
|
|
|
2009-05-07 08:45:33 +08:00
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N);
|
|
|
|
};
|
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
class VISIBILITY_HIDDEN BuiltinBug : public BugType {
|
|
|
|
GRExprEngine &Eng;
|
2008-12-09 08:44:16 +08:00
|
|
|
protected:
|
2009-02-05 07:49:09 +08:00
|
|
|
const std::string desc;
|
2008-07-23 00:21:24 +08:00
|
|
|
public:
|
2009-02-05 07:49:09 +08:00
|
|
|
BuiltinBug(GRExprEngine *eng, const char* n, const char* d)
|
2009-05-14 03:16:35 +08:00
|
|
|
: BugType(n, "Logic errors"), Eng(*eng), desc(d) {}
|
2009-02-05 07:49:09 +08:00
|
|
|
|
|
|
|
BuiltinBug(GRExprEngine *eng, const char* n)
|
2009-05-14 03:16:35 +08:00
|
|
|
: BugType(n, "Logic errors"), Eng(*eng), desc(n) {}
|
2008-08-01 04:31:27 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
virtual void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) = 0;
|
|
|
|
|
|
|
|
void FlushReports(BugReporter& BR) { FlushReportsImpl(BR, Eng); }
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-05-07 08:45:33 +08:00
|
|
|
virtual void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {}
|
|
|
|
|
|
|
|
template <typename ITER> void Emit(BugReporter& BR, ITER I, ITER E);
|
2008-07-23 00:21:24 +08:00
|
|
|
};
|
|
|
|
|
2009-05-07 08:45:33 +08:00
|
|
|
|
|
|
|
template <typename ITER>
|
|
|
|
void BuiltinBug::Emit(BugReporter& BR, ITER I, ITER E) {
|
|
|
|
for (; I != E; ++I) BR.EmitReport(new BuiltinBugReport(*this, desc.c_str(),
|
|
|
|
GetNode(I)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BuiltinBugReport::registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N) {
|
|
|
|
static_cast<BuiltinBug&>(getBugType()).registerInitialVisitors(BRC, N, this);
|
|
|
|
}
|
|
|
|
|
2008-07-23 00:21:24 +08:00
|
|
|
class VISIBILITY_HIDDEN NullDeref : public BuiltinBug {
|
|
|
|
public:
|
2009-02-05 07:49:09 +08:00
|
|
|
NullDeref(GRExprEngine* eng)
|
2009-04-07 12:54:31 +08:00
|
|
|
: BuiltinBug(eng,"Null dereference", "Dereference of null pointer") {}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-07-23 00:21:24 +08:00
|
|
|
Emit(BR, Eng.null_derefs_begin(), Eng.null_derefs_end());
|
|
|
|
}
|
2009-05-07 08:45:33 +08:00
|
|
|
|
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {
|
2009-05-15 13:25:09 +08:00
|
|
|
registerTrackNullOrUndefValue(BRC, GetDerefExpr(N), N);
|
2009-05-07 08:45:33 +08:00
|
|
|
}
|
2008-07-23 00:21:24 +08:00
|
|
|
};
|
|
|
|
|
2009-05-14 03:16:35 +08:00
|
|
|
class VISIBILITY_HIDDEN NilReceiverStructRet : public BuiltinBug {
|
2009-02-19 12:06:22 +08:00
|
|
|
public:
|
|
|
|
NilReceiverStructRet(GRExprEngine* eng) :
|
2009-05-14 03:16:35 +08:00
|
|
|
BuiltinBug(eng, "'nil' receiver with struct return type") {}
|
2009-02-19 12:06:22 +08:00
|
|
|
|
2009-05-14 03:16:35 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2009-02-19 12:06:22 +08:00
|
|
|
for (GRExprEngine::nil_receiver_struct_ret_iterator
|
|
|
|
I=Eng.nil_receiver_struct_ret_begin(),
|
|
|
|
E=Eng.nil_receiver_struct_ret_end(); I!=E; ++I) {
|
|
|
|
|
|
|
|
std::string sbuf;
|
|
|
|
llvm::raw_string_ostream os(sbuf);
|
|
|
|
PostStmt P = cast<PostStmt>((*I)->getLocation());
|
|
|
|
ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
|
|
|
|
os << "The receiver in the message expression is 'nil' and results in the"
|
|
|
|
" returned value (of type '"
|
|
|
|
<< ME->getType().getAsString()
|
|
|
|
<< "') to be garbage or otherwise undefined.";
|
|
|
|
|
2009-05-14 03:16:35 +08:00
|
|
|
BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
|
2009-02-19 12:06:22 +08:00
|
|
|
R->addRange(ME->getReceiver()->getSourceRange());
|
|
|
|
BR.EmitReport(R);
|
|
|
|
}
|
|
|
|
}
|
2009-05-14 03:16:35 +08:00
|
|
|
|
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {
|
2009-05-15 13:25:09 +08:00
|
|
|
registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
|
2009-05-14 03:16:35 +08:00
|
|
|
}
|
2009-02-19 12:06:22 +08:00
|
|
|
};
|
2009-04-08 11:07:17 +08:00
|
|
|
|
2009-05-14 03:16:35 +08:00
|
|
|
class VISIBILITY_HIDDEN NilReceiverLargerThanVoidPtrRet : public BuiltinBug {
|
2009-04-08 11:07:17 +08:00
|
|
|
public:
|
|
|
|
NilReceiverLargerThanVoidPtrRet(GRExprEngine* eng) :
|
2009-05-14 03:16:35 +08:00
|
|
|
BuiltinBug(eng,
|
|
|
|
"'nil' receiver with return type larger than sizeof(void *)") {}
|
2009-04-08 11:07:17 +08:00
|
|
|
|
2009-05-14 03:16:35 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2009-04-08 11:07:17 +08:00
|
|
|
for (GRExprEngine::nil_receiver_larger_than_voidptr_ret_iterator
|
|
|
|
I=Eng.nil_receiver_larger_than_voidptr_ret_begin(),
|
|
|
|
E=Eng.nil_receiver_larger_than_voidptr_ret_end(); I!=E; ++I) {
|
|
|
|
|
|
|
|
std::string sbuf;
|
|
|
|
llvm::raw_string_ostream os(sbuf);
|
|
|
|
PostStmt P = cast<PostStmt>((*I)->getLocation());
|
|
|
|
ObjCMessageExpr *ME = cast<ObjCMessageExpr>(P.getStmt());
|
|
|
|
os << "The receiver in the message expression is 'nil' and results in the"
|
|
|
|
" returned value (of type '"
|
|
|
|
<< ME->getType().getAsString()
|
|
|
|
<< "' and of size "
|
|
|
|
<< Eng.getContext().getTypeSize(ME->getType()) / 8
|
|
|
|
<< " bytes) to be garbage or otherwise undefined.";
|
|
|
|
|
2009-05-14 03:16:35 +08:00
|
|
|
BuiltinBugReport *R = new BuiltinBugReport(*this, os.str().c_str(), *I);
|
2009-04-08 11:07:17 +08:00
|
|
|
R->addRange(ME->getReceiver()->getSourceRange());
|
|
|
|
BR.EmitReport(R);
|
|
|
|
}
|
2009-05-14 03:16:35 +08:00
|
|
|
}
|
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {
|
2009-05-15 13:25:09 +08:00
|
|
|
registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
|
2009-04-08 11:07:17 +08:00
|
|
|
}
|
|
|
|
};
|
2009-02-19 12:06:22 +08:00
|
|
|
|
2008-07-23 00:21:24 +08:00
|
|
|
class VISIBILITY_HIDDEN UndefinedDeref : public BuiltinBug {
|
|
|
|
public:
|
2009-02-05 07:49:09 +08:00
|
|
|
UndefinedDeref(GRExprEngine* eng)
|
2009-03-01 13:43:22 +08:00
|
|
|
: BuiltinBug(eng,"Dereference of undefined pointer value") {}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-07-23 00:21:24 +08:00
|
|
|
Emit(BR, Eng.undef_derefs_begin(), Eng.undef_derefs_end());
|
|
|
|
}
|
2009-05-14 03:16:35 +08:00
|
|
|
|
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {
|
2009-05-15 13:25:09 +08:00
|
|
|
registerTrackNullOrUndefValue(BRC, GetDerefExpr(N), N);
|
2009-05-14 03:16:35 +08:00
|
|
|
}
|
2008-07-23 00:21:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN DivZero : public BuiltinBug {
|
|
|
|
public:
|
2009-02-05 07:49:09 +08:00
|
|
|
DivZero(GRExprEngine* eng)
|
2009-04-02 10:40:26 +08:00
|
|
|
: BuiltinBug(eng,"Division-by-zero",
|
|
|
|
"Division by zero or undefined value.") {}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-07-23 00:21:24 +08:00
|
|
|
Emit(BR, Eng.explicit_bad_divides_begin(), Eng.explicit_bad_divides_end());
|
|
|
|
}
|
2009-05-15 13:25:09 +08:00
|
|
|
|
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {
|
|
|
|
registerTrackNullOrUndefValue(BRC, GetDenomExpr(N), N);
|
|
|
|
}
|
2008-07-23 00:21:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN UndefResult : public BuiltinBug {
|
|
|
|
public:
|
2009-04-02 10:40:26 +08:00
|
|
|
UndefResult(GRExprEngine* eng) : BuiltinBug(eng,"Undefined result",
|
2008-07-23 00:21:24 +08:00
|
|
|
"Result of operation is undefined.") {}
|
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-07-23 00:21:24 +08:00
|
|
|
Emit(BR, Eng.undef_results_begin(), Eng.undef_results_end());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN BadCall : public BuiltinBug {
|
|
|
|
public:
|
2009-02-05 07:49:09 +08:00
|
|
|
BadCall(GRExprEngine *eng)
|
2009-04-02 10:40:26 +08:00
|
|
|
: BuiltinBug(eng, "Invalid function call",
|
2009-03-01 13:43:22 +08:00
|
|
|
"Called function pointer is a null or undefined pointer value") {}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-07-23 00:21:24 +08:00
|
|
|
Emit(BR, Eng.bad_calls_begin(), Eng.bad_calls_end());
|
|
|
|
}
|
2009-05-15 13:25:09 +08:00
|
|
|
|
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {
|
|
|
|
registerTrackNullOrUndefValue(BRC, GetCalleeExpr(N), N);
|
|
|
|
}
|
2008-07-23 00:21:24 +08:00
|
|
|
};
|
|
|
|
|
2009-05-15 13:25:09 +08:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN ArgReport : public BuiltinBugReport {
|
|
|
|
const Stmt *Arg;
|
2008-07-23 00:21:24 +08:00
|
|
|
public:
|
2009-05-15 13:25:09 +08:00
|
|
|
ArgReport(BugType& bt, const char* desc, ExplodedNode<GRState> *n,
|
|
|
|
const Stmt *arg)
|
|
|
|
: BuiltinBugReport(bt, desc, n), Arg(arg) {}
|
|
|
|
|
|
|
|
ArgReport(BugType& bt, const char *shortDesc, const char *desc,
|
|
|
|
ExplodedNode<GRState> *n, const Stmt *arg)
|
|
|
|
: BuiltinBugReport(bt, shortDesc, desc, n), Arg(arg) {}
|
|
|
|
|
|
|
|
const Stmt *getArg() const { return Arg; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN BadArg : public BuiltinBug {
|
|
|
|
public:
|
2009-04-02 10:40:26 +08:00
|
|
|
BadArg(GRExprEngine* eng) : BuiltinBug(eng,"Uninitialized argument",
|
2009-03-01 13:43:22 +08:00
|
|
|
"Pass-by-value argument in function call is undefined.") {}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
BadArg(GRExprEngine* eng, const char* d)
|
2009-04-02 10:40:26 +08:00
|
|
|
: BuiltinBug(eng,"Uninitialized argument", d) {}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-07-23 00:21:24 +08:00
|
|
|
for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(),
|
|
|
|
E = Eng.undef_arg_end(); I!=E; ++I) {
|
|
|
|
// Generate a report for this bug.
|
2009-05-15 13:25:09 +08:00
|
|
|
ArgReport *report = new ArgReport(*this, desc.c_str(), I->first,
|
|
|
|
I->second);
|
2009-02-05 07:49:09 +08:00
|
|
|
report->addRange(I->second->getSourceRange());
|
|
|
|
BR.EmitReport(report);
|
2008-07-23 00:21:24 +08:00
|
|
|
}
|
|
|
|
}
|
2009-05-15 13:25:09 +08:00
|
|
|
|
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {
|
|
|
|
registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
|
|
|
|
N);
|
|
|
|
}
|
2008-07-23 00:21:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
|
|
|
|
public:
|
2009-02-05 07:49:09 +08:00
|
|
|
BadMsgExprArg(GRExprEngine* eng)
|
2009-04-02 10:40:26 +08:00
|
|
|
: BadArg(eng,"Pass-by-value argument in message expression is undefined"){}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-07-23 00:21:24 +08:00
|
|
|
for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
|
2009-02-05 07:49:09 +08:00
|
|
|
E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
|
2008-07-23 00:21:24 +08:00
|
|
|
// Generate a report for this bug.
|
2009-05-15 13:25:09 +08:00
|
|
|
ArgReport *report = new ArgReport(*this, desc.c_str(), I->first,
|
|
|
|
I->second);
|
2009-02-05 07:49:09 +08:00
|
|
|
report->addRange(I->second->getSourceRange());
|
|
|
|
BR.EmitReport(report);
|
2008-07-23 00:21:24 +08:00
|
|
|
}
|
2009-05-15 13:25:09 +08:00
|
|
|
}
|
2008-07-23 00:21:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN BadReceiver : public BuiltinBug {
|
|
|
|
public:
|
2009-02-05 07:49:09 +08:00
|
|
|
BadReceiver(GRExprEngine* eng)
|
2009-04-02 10:40:26 +08:00
|
|
|
: BuiltinBug(eng,"Uninitialized receiver",
|
|
|
|
"Receiver in message expression is an uninitialized value") {}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-12-09 06:47:34 +08:00
|
|
|
for (GRExprEngine::ErrorNodes::iterator I=Eng.undef_receivers_begin(),
|
2008-07-23 00:21:24 +08:00
|
|
|
End = Eng.undef_receivers_end(); I!=End; ++I) {
|
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2009-05-14 03:16:35 +08:00
|
|
|
BuiltinBugReport *report = new BuiltinBugReport(*this, desc.c_str(), *I);
|
2008-08-13 12:27:00 +08:00
|
|
|
ExplodedNode<GRState>* N = *I;
|
2008-07-23 00:21:24 +08:00
|
|
|
Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
|
|
|
|
Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
|
|
|
|
assert (E && "Receiver cannot be NULL");
|
2009-02-05 07:49:09 +08:00
|
|
|
report->addRange(E->getSourceRange());
|
|
|
|
BR.EmitReport(report);
|
2009-05-14 03:16:35 +08:00
|
|
|
}
|
|
|
|
}
|
2009-05-15 13:25:09 +08:00
|
|
|
|
2009-05-14 03:16:35 +08:00
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {
|
2009-05-15 13:25:09 +08:00
|
|
|
registerTrackNullOrUndefValue(BRC, GetReceiverExpr(N), N);
|
|
|
|
}
|
2008-07-23 00:21:24 +08:00
|
|
|
};
|
2008-11-21 08:27:44 +08:00
|
|
|
|
2008-07-23 00:21:24 +08:00
|
|
|
class VISIBILITY_HIDDEN RetStack : public BuiltinBug {
|
|
|
|
public:
|
2009-03-01 13:43:22 +08:00
|
|
|
RetStack(GRExprEngine* eng)
|
2009-04-02 10:40:26 +08:00
|
|
|
: BuiltinBug(eng, "Return of address to stack-allocated memory") {}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-07-31 01:49:12 +08:00
|
|
|
for (GRExprEngine::ret_stackaddr_iterator I=Eng.ret_stackaddr_begin(),
|
|
|
|
End = Eng.ret_stackaddr_end(); I!=End; ++I) {
|
2008-08-01 04:31:27 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
ExplodedNode<GRState>* N = *I;
|
2008-07-31 01:49:12 +08:00
|
|
|
Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
|
|
|
|
Expr* E = cast<ReturnStmt>(S)->getRetValue();
|
|
|
|
assert (E && "Return expression cannot be NULL");
|
2008-08-01 04:31:27 +08:00
|
|
|
|
|
|
|
// Get the value associated with E.
|
2009-06-19 07:58:37 +08:00
|
|
|
loc::MemRegionVal V = cast<loc::MemRegionVal>(N->getState()->getSVal(E));
|
2008-08-01 04:31:27 +08:00
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2008-10-31 08:18:30 +08:00
|
|
|
std::string buf;
|
|
|
|
llvm::raw_string_ostream os(buf);
|
2008-10-31 08:13:20 +08:00
|
|
|
SourceRange R;
|
2008-08-01 04:31:27 +08:00
|
|
|
|
2008-10-31 08:13:20 +08:00
|
|
|
// Check if the region is a compound literal.
|
|
|
|
if (const CompoundLiteralRegion* CR =
|
|
|
|
dyn_cast<CompoundLiteralRegion>(V.getRegion())) {
|
|
|
|
|
|
|
|
const CompoundLiteralExpr* CL = CR->getLiteralExpr();
|
|
|
|
os << "Address of stack memory associated with a compound literal "
|
|
|
|
"declared on line "
|
2009-01-16 15:36:28 +08:00
|
|
|
<< BR.getSourceManager()
|
|
|
|
.getInstantiationLineNumber(CL->getLocStart())
|
2008-10-31 08:13:20 +08:00
|
|
|
<< " returned.";
|
|
|
|
|
|
|
|
R = CL->getSourceRange();
|
|
|
|
}
|
2008-11-02 08:35:25 +08:00
|
|
|
else if (const AllocaRegion* AR = dyn_cast<AllocaRegion>(V.getRegion())) {
|
|
|
|
const Expr* ARE = AR->getExpr();
|
|
|
|
SourceLocation L = ARE->getLocStart();
|
|
|
|
R = ARE->getSourceRange();
|
|
|
|
|
|
|
|
os << "Address of stack memory allocated by call to alloca() on line "
|
2009-01-16 15:36:28 +08:00
|
|
|
<< BR.getSourceManager().getInstantiationLineNumber(L)
|
2008-11-02 08:35:25 +08:00
|
|
|
<< " returned.";
|
|
|
|
}
|
2008-10-31 08:13:20 +08:00
|
|
|
else {
|
|
|
|
os << "Address of stack memory associated with local variable '"
|
|
|
|
<< V.getRegion()->getString() << "' returned.";
|
|
|
|
}
|
2008-08-01 04:31:27 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
RangedBugReport *report = new RangedBugReport(*this, os.str().c_str(), N);
|
|
|
|
report->addRange(E->getSourceRange());
|
|
|
|
if (R.isValid()) report->addRange(R);
|
|
|
|
BR.EmitReport(report);
|
2008-07-31 01:49:12 +08:00
|
|
|
}
|
2008-07-23 00:21:24 +08:00
|
|
|
}
|
|
|
|
};
|
2008-11-21 08:27:44 +08:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN RetUndef : public BuiltinBug {
|
|
|
|
public:
|
2009-04-02 10:40:26 +08:00
|
|
|
RetUndef(GRExprEngine* eng) : BuiltinBug(eng, "Uninitialized return value",
|
2009-05-14 03:16:35 +08:00
|
|
|
"Uninitialized or undefined value returned to caller.") {}
|
2008-11-21 08:27:44 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-11-21 08:27:44 +08:00
|
|
|
Emit(BR, Eng.ret_undef_begin(), Eng.ret_undef_end());
|
|
|
|
}
|
2009-05-14 03:16:35 +08:00
|
|
|
|
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {
|
2009-05-15 13:25:09 +08:00
|
|
|
registerTrackNullOrUndefValue(BRC, GetRetValExpr(N), N);
|
|
|
|
}
|
2008-11-21 08:27:44 +08:00
|
|
|
};
|
2008-07-23 00:21:24 +08:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN UndefBranch : public BuiltinBug {
|
|
|
|
struct VISIBILITY_HIDDEN FindUndefExpr {
|
2008-08-13 12:27:00 +08:00
|
|
|
GRStateManager& VM;
|
|
|
|
const GRState* St;
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2008-07-31 01:49:12 +08:00
|
|
|
Expr* FindExpr(Expr* Ex) {
|
2008-07-23 00:21:24 +08:00
|
|
|
if (!MatchesCriteria(Ex))
|
2008-07-31 01:49:12 +08:00
|
|
|
return 0;
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2008-07-31 01:49:12 +08:00
|
|
|
for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
|
2008-07-23 00:21:24 +08:00
|
|
|
if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
|
|
|
|
Expr* E2 = FindExpr(ExI);
|
|
|
|
if (E2) return E2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ex;
|
|
|
|
}
|
|
|
|
|
2009-06-19 07:58:37 +08:00
|
|
|
bool MatchesCriteria(Expr* Ex) { return St->getSVal(Ex).isUndef(); }
|
2008-07-23 00:21:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2009-02-05 07:49:09 +08:00
|
|
|
UndefBranch(GRExprEngine *eng)
|
2009-04-02 10:40:26 +08:00
|
|
|
: BuiltinBug(eng,"Use of uninitialized value",
|
2008-07-23 00:21:24 +08:00
|
|
|
"Branch condition evaluates to an uninitialized value.") {}
|
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-07-23 00:21:24 +08:00
|
|
|
for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
|
|
|
|
E=Eng.undef_branches_end(); I!=E; ++I) {
|
|
|
|
|
|
|
|
// 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>((*I)->getLocation());
|
|
|
|
Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
|
|
|
|
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 (!(*I)->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.
|
2008-08-13 12:27:00 +08:00
|
|
|
ExplodedNode<GRState> *N = *(*I)->pred_begin();
|
2008-07-23 00:21:24 +08:00
|
|
|
ProgramPoint P = N->getLocation();
|
2008-08-13 12:27:00 +08:00
|
|
|
const GRState* St = (*I)->getState();
|
2008-07-23 00:21:24 +08:00
|
|
|
|
|
|
|
if (PostStmt* PS = dyn_cast<PostStmt>(&P))
|
|
|
|
if (PS->getStmt() == Ex)
|
|
|
|
St = N->getState();
|
|
|
|
|
|
|
|
FindUndefExpr FindIt(Eng.getStateManager(), St);
|
|
|
|
Ex = FindIt.FindExpr(Ex);
|
|
|
|
|
2009-05-15 13:25:09 +08:00
|
|
|
ArgReport *R = new ArgReport(*this, desc.c_str(), *I, Ex);
|
2009-02-05 07:49:09 +08:00
|
|
|
R->addRange(Ex->getSourceRange());
|
|
|
|
BR.EmitReport(R);
|
2008-07-23 00:21:24 +08:00
|
|
|
}
|
|
|
|
}
|
2009-05-15 13:25:09 +08:00
|
|
|
|
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {
|
|
|
|
registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
|
|
|
|
N);
|
|
|
|
}
|
2008-07-23 00:21:24 +08:00
|
|
|
};
|
|
|
|
|
2008-11-23 13:52:28 +08:00
|
|
|
class VISIBILITY_HIDDEN OutOfBoundMemoryAccess : public BuiltinBug {
|
|
|
|
public:
|
2009-02-05 07:49:09 +08:00
|
|
|
OutOfBoundMemoryAccess(GRExprEngine* eng)
|
2009-04-02 10:40:26 +08:00
|
|
|
: BuiltinBug(eng,"Out-of-bounds memory access",
|
2009-02-05 07:49:09 +08:00
|
|
|
"Load or store into an out-of-bound memory position.") {}
|
2008-11-23 13:52:28 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-11-23 13:52:28 +08:00
|
|
|
Emit(BR, Eng.explicit_oob_memacc_begin(), Eng.explicit_oob_memacc_end());
|
|
|
|
}
|
|
|
|
};
|
2008-12-09 06:47:34 +08:00
|
|
|
|
2008-12-09 08:44:16 +08:00
|
|
|
class VISIBILITY_HIDDEN BadSizeVLA : public BuiltinBug {
|
2008-12-09 06:47:34 +08:00
|
|
|
public:
|
2009-04-02 10:40:26 +08:00
|
|
|
BadSizeVLA(GRExprEngine* eng) :
|
|
|
|
BuiltinBug(eng, "Bad variable-length array (VLA) size") {}
|
2008-12-09 06:47:34 +08:00
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
void FlushReportsImpl(BugReporter& BR, GRExprEngine& Eng) {
|
2008-12-09 06:47:34 +08:00
|
|
|
for (GRExprEngine::ErrorNodes::iterator
|
2008-12-09 08:44:16 +08:00
|
|
|
I = Eng.ExplicitBadSizedVLA.begin(),
|
|
|
|
E = Eng.ExplicitBadSizedVLA.end(); I!=E; ++I) {
|
|
|
|
|
|
|
|
// Determine whether this was a 'zero-sized' VLA or a VLA with an
|
|
|
|
// undefined size.
|
|
|
|
GRExprEngine::NodeTy* N = *I;
|
|
|
|
PostStmt PS = cast<PostStmt>(N->getLocation());
|
2008-12-09 06:47:34 +08:00
|
|
|
DeclStmt *DS = cast<DeclStmt>(PS.getStmt());
|
|
|
|
VarDecl* VD = cast<VarDecl>(*DS->decl_begin());
|
|
|
|
QualType T = Eng.getContext().getCanonicalType(VD->getType());
|
|
|
|
VariableArrayType* VT = cast<VariableArrayType>(T);
|
2008-12-09 08:44:16 +08:00
|
|
|
Expr* SizeExpr = VT->getSizeExpr();
|
2008-12-09 06:47:34 +08:00
|
|
|
|
2008-12-09 08:44:16 +08:00
|
|
|
std::string buf;
|
|
|
|
llvm::raw_string_ostream os(buf);
|
2009-04-02 10:40:26 +08:00
|
|
|
os << "The expression used to specify the number of elements in the "
|
|
|
|
"variable-length array (VLA) '"
|
2009-02-05 07:49:09 +08:00
|
|
|
<< VD->getNameAsString() << "' evaluates to ";
|
2008-12-09 08:44:16 +08:00
|
|
|
|
2009-06-19 07:58:37 +08:00
|
|
|
bool isUndefined = N->getState()->getSVal(SizeExpr).isUndef();
|
2009-04-30 05:58:13 +08:00
|
|
|
|
|
|
|
if (isUndefined)
|
2008-12-09 08:44:16 +08:00
|
|
|
os << "an undefined or garbage value.";
|
2009-02-05 07:49:09 +08:00
|
|
|
else
|
|
|
|
os << "0. VLAs with no elements have undefined behavior.";
|
2009-04-30 05:58:13 +08:00
|
|
|
|
|
|
|
std::string shortBuf;
|
|
|
|
llvm::raw_string_ostream os_short(shortBuf);
|
|
|
|
os_short << "Variable-length array '" << VD->getNameAsString() << "' "
|
2009-05-10 13:11:21 +08:00
|
|
|
<< (isUndefined ? "garbage value for array size"
|
|
|
|
: "has zero elements (undefined behavior)");
|
2009-04-30 05:58:13 +08:00
|
|
|
|
2009-05-15 13:25:09 +08:00
|
|
|
ArgReport *report = new ArgReport(*this, os_short.str().c_str(),
|
|
|
|
os.str().c_str(), N, SizeExpr);
|
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
report->addRange(SizeExpr->getSourceRange());
|
|
|
|
BR.EmitReport(report);
|
2008-12-09 06:47:34 +08:00
|
|
|
}
|
|
|
|
}
|
2009-05-15 13:25:09 +08:00
|
|
|
|
|
|
|
void registerInitialVisitors(BugReporterContext& BRC,
|
|
|
|
const ExplodedNode<GRState>* N,
|
|
|
|
BuiltinBugReport *R) {
|
|
|
|
registerTrackNullOrUndefValue(BRC, static_cast<ArgReport*>(R)->getArg(),
|
|
|
|
N);
|
|
|
|
}
|
2008-12-09 06:47:34 +08:00
|
|
|
};
|
2008-11-23 13:52:28 +08:00
|
|
|
|
2008-07-23 00:21:24 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// __attribute__(nonnull) checking
|
|
|
|
|
2009-07-23 05:46:56 +08:00
|
|
|
class VISIBILITY_HIDDEN CheckAttrNonNull :
|
|
|
|
public CheckerVisitor<CheckAttrNonNull> {
|
|
|
|
|
2009-02-05 07:49:09 +08:00
|
|
|
BugType *BT;
|
2008-07-23 00:21:24 +08:00
|
|
|
|
|
|
|
public:
|
2009-07-23 05:46:56 +08:00
|
|
|
CheckAttrNonNull() : BT(0) {}
|
|
|
|
|
|
|
|
const void *getTag() {
|
|
|
|
static int x = 0;
|
|
|
|
return &x;
|
|
|
|
}
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-07-23 05:46:56 +08:00
|
|
|
void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE) {
|
|
|
|
const GRState *state = C.getState();
|
|
|
|
const GRState *originalState = state;
|
2008-07-23 00:21:24 +08:00
|
|
|
|
2009-07-23 05:46:56 +08:00
|
|
|
// Check if the callee has a 'nonnull' attribute.
|
2009-06-19 07:58:37 +08:00
|
|
|
SVal X = state->getSVal(CE->getCallee());
|
2009-07-23 05:46:56 +08:00
|
|
|
|
2009-04-20 13:24:46 +08:00
|
|
|
const FunctionDecl* FD = X.getAsFunctionDecl();
|
|
|
|
if (!FD)
|
2009-07-23 05:46:56 +08:00
|
|
|
return;
|
2009-04-20 13:24:46 +08:00
|
|
|
|
2009-07-23 05:46:56 +08:00
|
|
|
const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
|
2008-07-23 00:21:24 +08:00
|
|
|
if (!Att)
|
2009-07-23 05:46:56 +08:00
|
|
|
return;
|
2008-07-23 00:21:24 +08:00
|
|
|
|
|
|
|
// Iterate through the arguments of CE and check them for null.
|
|
|
|
unsigned idx = 0;
|
|
|
|
|
2009-07-23 05:46:56 +08:00
|
|
|
for (CallExpr::const_arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
|
2008-07-23 00:21:24 +08:00
|
|
|
++I, ++idx) {
|
2009-07-23 05:46:56 +08:00
|
|
|
|
|
|
|
if (!Att->isNonNull(idx))
|
2008-07-23 00:21:24 +08:00
|
|
|
continue;
|
|
|
|
|
2009-07-23 05:46:56 +08:00
|
|
|
ConstraintManager &CM = C.getConstraintManager();
|
|
|
|
const GRState *stateNotNull, *stateNull;
|
|
|
|
llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state,
|
|
|
|
state->getSVal(*I));
|
|
|
|
|
|
|
|
if (stateNull && !stateNotNull) {
|
|
|
|
// Generate an error node. Check for a null node in case
|
|
|
|
// we cache out.
|
|
|
|
if (ExplodedNode<GRState> *errorNode = C.generateNode(CE, stateNull)) {
|
|
|
|
|
|
|
|
// Lazily allocate the BugType object if it hasn't already been
|
|
|
|
// created. Ownership is transferred to the BugReporter object once
|
|
|
|
// the BugReport is passed to 'EmitWarning'.
|
|
|
|
if (!BT)
|
|
|
|
BT = new BugType("Argument with 'nonnull' attribute passed null",
|
|
|
|
"API");
|
|
|
|
|
|
|
|
RangedBugReport *R =
|
|
|
|
new RangedBugReport(*BT, "Null pointer passed as an argument to a "
|
|
|
|
"'nonnull' parameter", errorNode);
|
|
|
|
|
|
|
|
// Highlight the range of the argument that was null.
|
|
|
|
R->addRange((*I)->getSourceRange());
|
|
|
|
|
|
|
|
// Emit the bug report.
|
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always return. Either we cached out or we just emitted an error.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a pointer value passed the check we should assume that it is
|
|
|
|
// indeed not null from this point forward.
|
|
|
|
assert(stateNotNull);
|
|
|
|
state = stateNotNull;
|
2008-07-23 00:21:24 +08:00
|
|
|
}
|
2009-07-23 05:46:56 +08:00
|
|
|
|
|
|
|
// If we reach here all of the arguments passed the nonnull check.
|
|
|
|
// If 'state' has been updated generated a new node.
|
|
|
|
if (state != originalState)
|
|
|
|
C.generateNode(CE, state);
|
2008-07-23 00:21:24 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check registration.
|
2009-02-05 07:49:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-07-23 00:21:24 +08:00
|
|
|
|
|
|
|
void GRExprEngine::RegisterInternalChecks() {
|
2009-02-05 07:49:09 +08:00
|
|
|
// Register internal "built-in" BugTypes with the BugReporter. These BugTypes
|
|
|
|
// are different than what probably many checks will do since they don't
|
|
|
|
// create BugReports on-the-fly but instead wait until GRExprEngine finishes
|
|
|
|
// analyzing a function. Generation of BugReport objects is done via a call
|
|
|
|
// to 'FlushReports' from BugReporter.
|
|
|
|
BR.Register(new NullDeref(this));
|
|
|
|
BR.Register(new UndefinedDeref(this));
|
|
|
|
BR.Register(new UndefBranch(this));
|
|
|
|
BR.Register(new DivZero(this));
|
|
|
|
BR.Register(new UndefResult(this));
|
|
|
|
BR.Register(new BadCall(this));
|
|
|
|
BR.Register(new RetStack(this));
|
|
|
|
BR.Register(new RetUndef(this));
|
|
|
|
BR.Register(new BadArg(this));
|
|
|
|
BR.Register(new BadMsgExprArg(this));
|
|
|
|
BR.Register(new BadReceiver(this));
|
|
|
|
BR.Register(new OutOfBoundMemoryAccess(this));
|
|
|
|
BR.Register(new BadSizeVLA(this));
|
2009-02-19 12:06:22 +08:00
|
|
|
BR.Register(new NilReceiverStructRet(this));
|
2009-04-08 11:07:17 +08:00
|
|
|
BR.Register(new NilReceiverLargerThanVoidPtrRet(this));
|
2009-02-05 07:49:09 +08:00
|
|
|
|
|
|
|
// The following checks do not need to have their associated BugTypes
|
|
|
|
// explicitly registered with the BugReporter. If they issue any BugReports,
|
|
|
|
// their associated BugType will get registered with the BugReporter
|
|
|
|
// automatically. Note that the check itself is owned by the GRExprEngine
|
|
|
|
// object.
|
2009-07-23 05:46:56 +08:00
|
|
|
registerCheck(new CheckAttrNonNull());
|
2008-07-23 00:21:24 +08:00
|
|
|
}
|