2008-02-14 18:28:23 +00:00
|
|
|
// GRSimpleVals.cpp - Transfer functions for tracking simple values -*- C++ -*--
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-03-06 10:40:09 +00:00
|
|
|
// This file defines GRSimpleVals, a sub-class of GRTransferFuncs that
|
2008-02-14 18:28:23 +00:00
|
|
|
// provides transfer functions for performing simple value tracking with
|
|
|
|
// limited support for symbolics.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "GRSimpleVals.h"
|
2008-03-27 17:17:22 +00:00
|
|
|
#include "BasicObjCFoundationChecks.h"
|
2008-04-02 22:03:53 +00:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-02-27 06:07:00 +00:00
|
|
|
#include "clang/Analysis/PathSensitive/ValueState.h"
|
2008-03-31 18:26:32 +00:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
2008-02-27 17:56:16 +00:00
|
|
|
#include <sstream>
|
2008-02-14 18:28:23 +00:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-04-02 07:05:46 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Bug Descriptions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace bugdesc {
|
|
|
|
|
|
|
|
struct NullDeref {
|
2008-04-02 18:02:54 +00:00
|
|
|
static const char* getName() { return "Null dereference"; }
|
2008-04-02 07:05:46 +00:00
|
|
|
|
|
|
|
static PathDiagnosticPiece* getEndPath(SourceManager& SMgr,
|
|
|
|
ExplodedNode<ValueState> *N);
|
|
|
|
};
|
|
|
|
|
|
|
|
PathDiagnosticPiece* NullDeref::getEndPath(SourceManager& SMgr,
|
|
|
|
ExplodedNode<ValueState> *N) {
|
|
|
|
|
|
|
|
Expr* E = cast<Expr>(cast<PostStmt>(N->getLocation()).getStmt());
|
|
|
|
|
|
|
|
// FIXME: Do better ranges for different kinds of null dereferences.
|
2008-02-26 21:31:18 +00:00
|
|
|
|
2008-04-02 07:05:46 +00:00
|
|
|
FullSourceLoc L(E->getLocStart(), SMgr);
|
|
|
|
|
|
|
|
PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getName());
|
|
|
|
P->addRange(E->getSourceRange());
|
|
|
|
|
|
|
|
return P;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace: bugdesc
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-02 05:15:22 +00:00
|
|
|
template <typename ITERATOR>
|
|
|
|
static inline ExplodedNode<ValueState>* GetNode(ITERATOR I) {
|
|
|
|
return *I;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
static inline ExplodedNode<ValueState>*
|
|
|
|
GetNode(GRExprEngine::undef_arg_iterator I) {
|
|
|
|
return I->first;
|
|
|
|
}
|
|
|
|
|
2008-03-04 00:42:54 +00:00
|
|
|
template <typename ITERATOR>
|
2008-03-14 18:14:50 +00:00
|
|
|
static inline ProgramPoint GetLocation(ITERATOR I) {
|
|
|
|
return (*I)->getLocation();
|
2008-03-04 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2008-04-02 05:15:22 +00:00
|
|
|
static inline ProgramPoint GetLocation(GRExprEngine::undef_arg_iterator I) {
|
2008-03-14 18:14:50 +00:00
|
|
|
return I->first->getLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Stmt* GetStmt(const ProgramPoint& P) {
|
|
|
|
if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) {
|
|
|
|
return PS->getStmt();
|
|
|
|
}
|
|
|
|
else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
|
|
|
|
return BE->getSrc()->getTerminator();
|
|
|
|
}
|
2008-04-02 22:03:53 +00:00
|
|
|
else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) {
|
|
|
|
return BE->getFirstStmt();
|
|
|
|
}
|
2008-03-14 18:14:50 +00:00
|
|
|
|
|
|
|
assert (false && "Unsupported ProgramPoint.");
|
|
|
|
return NULL;
|
2008-03-04 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
2008-04-02 05:15:22 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pathless Warnings
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-04 00:42:54 +00:00
|
|
|
template <typename ITERATOR>
|
2008-03-31 18:26:32 +00:00
|
|
|
static void EmitDiag(Diagnostic& Diag, PathDiagnosticClient* PD,
|
|
|
|
SourceManager& SrcMgr,
|
|
|
|
unsigned ErrorDiag, ITERATOR I) {
|
2008-03-04 00:42:54 +00:00
|
|
|
|
2008-03-14 18:14:50 +00:00
|
|
|
Stmt* S = GetStmt(GetLocation(I));
|
2008-03-31 18:44:32 +00:00
|
|
|
SourceRange R = S->getSourceRange();
|
|
|
|
Diag.Report(PD, FullSourceLoc(S->getLocStart(), SrcMgr), ErrorDiag,
|
|
|
|
NULL, 0, &R, 1);
|
2008-03-04 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2008-03-31 18:26:32 +00:00
|
|
|
static void EmitDiag(Diagnostic& Diag, PathDiagnosticClient* PD,
|
|
|
|
SourceManager& SrcMgr, unsigned ErrorDiag,
|
|
|
|
GRExprEngine::undef_arg_iterator I) {
|
2008-03-04 00:42:54 +00:00
|
|
|
|
2008-03-14 18:14:50 +00:00
|
|
|
Stmt* S1 = GetStmt(GetLocation(I));
|
2008-03-04 00:42:54 +00:00
|
|
|
Expr* E2 = cast<Expr>(I->second);
|
|
|
|
|
2008-03-14 18:14:50 +00:00
|
|
|
SourceLocation Loc = S1->getLocStart();
|
2008-03-04 00:42:54 +00:00
|
|
|
SourceRange R = E2->getSourceRange();
|
2008-03-31 18:26:32 +00:00
|
|
|
Diag.Report(PD, FullSourceLoc(Loc, SrcMgr), ErrorDiag, 0, 0, &R, 1);
|
2008-03-04 00:42:54 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 21:31:18 +00:00
|
|
|
template <typename ITERATOR>
|
2008-04-02 07:05:46 +00:00
|
|
|
static void EmitWarning(Diagnostic& Diag, PathDiagnosticClient* PD,
|
|
|
|
SourceManager& SrcMgr,
|
|
|
|
ITERATOR I, ITERATOR E, const char* msg) {
|
2008-02-26 21:31:18 +00:00
|
|
|
|
2008-02-27 17:56:16 +00:00
|
|
|
std::ostringstream Out;
|
2008-04-02 05:15:22 +00:00
|
|
|
std::string Str(msg);
|
2008-03-31 20:42:43 +00:00
|
|
|
|
|
|
|
if (!PD) {
|
|
|
|
Out << "[CHECKER] " << msg;
|
2008-04-01 22:35:58 +00:00
|
|
|
Str = Out.str();
|
|
|
|
msg = Str.c_str();
|
2008-03-31 20:42:43 +00:00
|
|
|
}
|
2008-02-27 17:56:16 +00:00
|
|
|
|
2008-02-27 20:47:56 +00:00
|
|
|
bool isFirst = true;
|
2008-03-15 07:58:36 +00:00
|
|
|
unsigned ErrorDiag = 0;
|
2008-03-04 00:42:54 +00:00
|
|
|
llvm::SmallPtrSet<void*,10> CachedErrors;
|
2008-02-26 21:31:18 +00:00
|
|
|
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
|
|
|
|
if (isFirst) {
|
|
|
|
isFirst = false;
|
|
|
|
ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, msg);
|
|
|
|
}
|
2008-02-28 20:38:16 +00:00
|
|
|
else {
|
|
|
|
|
|
|
|
// HACK: Cache the location of the error. Don't emit the same
|
|
|
|
// warning for the same error type that occurs at the same program
|
|
|
|
// location but along a different path.
|
2008-03-04 00:42:54 +00:00
|
|
|
void* p = GetLocation(I).getRawData();
|
2008-02-28 20:38:16 +00:00
|
|
|
|
|
|
|
if (CachedErrors.count(p))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CachedErrors.insert(p);
|
|
|
|
}
|
2008-03-04 00:42:54 +00:00
|
|
|
|
2008-04-02 07:05:46 +00:00
|
|
|
EmitDiag(Diag, PD, SrcMgr, ErrorDiag, I);
|
2008-02-26 21:31:18 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-04-02 07:05:46 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Path warnings.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static void GeneratePathDiagnostic(PathDiagnostic& PD,
|
2008-04-02 22:03:53 +00:00
|
|
|
ASTContext& Ctx,
|
2008-04-02 07:05:46 +00:00
|
|
|
ExplodedNode<ValueState>* N) {
|
|
|
|
|
2008-04-02 22:03:53 +00:00
|
|
|
SourceManager& SMgr = Ctx.getSourceManager();
|
2008-04-02 07:05:46 +00:00
|
|
|
|
2008-04-02 22:03:53 +00:00
|
|
|
while (!N->pred_empty()) {
|
2008-04-02 07:05:46 +00:00
|
|
|
|
2008-04-02 22:03:53 +00:00
|
|
|
ExplodedNode<ValueState>* LastNode = N;
|
|
|
|
N = *(N->pred_begin());
|
2008-04-02 07:05:46 +00:00
|
|
|
|
2008-04-02 22:03:53 +00:00
|
|
|
ProgramPoint P = N->getLocation();
|
2008-04-02 07:05:46 +00:00
|
|
|
|
2008-04-02 22:03:53 +00:00
|
|
|
if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) {
|
2008-04-02 07:05:46 +00:00
|
|
|
|
2008-04-02 22:03:53 +00:00
|
|
|
CFGBlock* Src = BE->getSrc();
|
|
|
|
CFGBlock* Dst = BE->getDst();
|
2008-04-02 07:05:46 +00:00
|
|
|
|
2008-04-02 22:03:53 +00:00
|
|
|
Stmt* T = Src->getTerminator();
|
2008-04-02 07:05:46 +00:00
|
|
|
|
2008-04-02 22:03:53 +00:00
|
|
|
if (!T)
|
|
|
|
continue;
|
|
|
|
|
2008-04-02 07:05:46 +00:00
|
|
|
FullSourceLoc L(T->getLocStart(), SMgr);
|
|
|
|
|
2008-04-02 22:03:53 +00:00
|
|
|
switch (T->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::GotoStmtClass:
|
|
|
|
case Stmt::IndirectGotoStmtClass: {
|
|
|
|
|
|
|
|
Stmt* S = GetStmt(LastNode->getLocation());
|
|
|
|
|
|
|
|
if (!S)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
|
|
|
|
os << "Control jumps to line "
|
|
|
|
<< SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
|
|
|
|
|
|
|
|
PD.push_front(new PathDiagnosticPiece(L, os.str()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::SwitchStmtClass: {
|
|
|
|
|
|
|
|
// Figure out what case arm we took.
|
|
|
|
|
|
|
|
Stmt* S = Dst->getLabel();
|
|
|
|
|
|
|
|
if (!S)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case Stmt::DefaultStmtClass: {
|
|
|
|
|
|
|
|
os << "Control jumps to the 'default' case at line "
|
|
|
|
<< SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::CaseStmtClass: {
|
|
|
|
|
|
|
|
os << "Control jumps to 'case ";
|
|
|
|
|
|
|
|
Expr* CondE = cast<SwitchStmt>(T)->getCond();
|
|
|
|
unsigned bits = Ctx.getTypeSize(CondE->getType());
|
|
|
|
|
|
|
|
llvm::APSInt V1(bits, false);
|
|
|
|
|
|
|
|
CaseStmt* Case = cast<CaseStmt>(S);
|
|
|
|
|
|
|
|
if (!Case->getLHS()->isIntegerConstantExpr(V1, Ctx, 0, true)) {
|
|
|
|
assert (false &&
|
|
|
|
"Case condition must evaluate to an integer constant.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
os << V1.toString();
|
|
|
|
|
|
|
|
// Get the RHS of the case, if it exists.
|
|
|
|
|
|
|
|
if (Expr* E = Case->getRHS()) {
|
|
|
|
|
|
|
|
llvm::APSInt V2(bits, false);
|
|
|
|
|
|
|
|
if (!E->isIntegerConstantExpr(V2, Ctx, 0, true)) {
|
|
|
|
assert (false &&
|
2008-04-02 22:08:09 +00:00
|
|
|
"Case condition (RHS) must evaluate to an integer constant.");
|
2008-04-02 22:03:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
os << " .. " << V2.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
os << ":' at line "
|
|
|
|
<< SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n";
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PD.push_front(new PathDiagnosticPiece(L, os.str()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case Stmt::DoStmtClass:
|
|
|
|
case Stmt::WhileStmtClass:
|
|
|
|
case Stmt::ForStmtClass:
|
|
|
|
case Stmt::IfStmtClass: {
|
|
|
|
|
|
|
|
if (*(Src->succ_begin()+1) == Dst)
|
|
|
|
PD.push_front(new PathDiagnosticPiece(L, "Taking false branch."));
|
|
|
|
else
|
|
|
|
PD.push_front(new PathDiagnosticPiece(L, "Taking true branch."));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-02 07:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ITERATOR, typename DESC>
|
2008-04-02 22:03:53 +00:00
|
|
|
static void Report(PathDiagnosticClient& PDC, ASTContext& Ctx, DESC,
|
2008-04-02 07:05:46 +00:00
|
|
|
ITERATOR I, ITERATOR E) {
|
|
|
|
|
|
|
|
|
|
|
|
if (I == E)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char* BugName = DESC::getName();
|
|
|
|
|
|
|
|
llvm::SmallPtrSet<void*,10> CachedErrors;
|
|
|
|
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
|
|
|
|
// HACK: Cache the location of the error. Don't emit the same
|
|
|
|
// warning for the same error type that occurs at the same program
|
|
|
|
// location but along a different path.
|
|
|
|
void* p = GetLocation(I).getRawData();
|
|
|
|
|
|
|
|
if (CachedErrors.count(p))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CachedErrors.insert(p);
|
|
|
|
|
|
|
|
// Create the PathDiagnostic.
|
|
|
|
|
|
|
|
PathDiagnostic D(BugName);
|
|
|
|
|
|
|
|
// Get the end-of-path diagnostic.
|
2008-04-02 22:03:53 +00:00
|
|
|
D.push_back(DESC::getEndPath(Ctx.getSourceManager(), GetNode(I)));
|
2008-04-02 07:05:46 +00:00
|
|
|
|
|
|
|
// Generate the rest of the diagnostic.
|
2008-04-02 22:03:53 +00:00
|
|
|
GeneratePathDiagnostic(D, Ctx, GetNode(I));
|
2008-04-02 07:05:46 +00:00
|
|
|
|
|
|
|
PDC.HandlePathDiagnostic(D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-02 05:15:22 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Analysis Driver.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-02 07:05:46 +00:00
|
|
|
namespace clang {
|
|
|
|
|
2008-03-14 17:31:00 +00:00
|
|
|
unsigned RunGRSimpleVals(CFG& cfg, Decl& CD, ASTContext& Ctx,
|
2008-04-02 07:05:46 +00:00
|
|
|
Diagnostic& Diag, PathDiagnosticClient* PD,
|
|
|
|
bool Visualize, bool TrimGraph) {
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-03-14 17:31:00 +00:00
|
|
|
GRCoreEngine<GRExprEngine> Eng(cfg, CD, Ctx);
|
2008-04-02 07:05:46 +00:00
|
|
|
GRExprEngine* CS = &Eng.getCheckerState();
|
2008-03-27 17:17:22 +00:00
|
|
|
|
|
|
|
// Set base transfer functions.
|
2008-02-21 18:02:17 +00:00
|
|
|
GRSimpleVals GRSV;
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->setTransferFunctions(GRSV);
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-03-27 17:17:22 +00:00
|
|
|
// Add extra checkers.
|
|
|
|
llvm::OwningPtr<GRSimpleAPICheck> FoundationCheck(
|
2008-04-02 07:05:46 +00:00
|
|
|
CreateBasicObjCFoundationChecks(Ctx, &CS->getStateManager()));
|
2008-03-27 17:17:22 +00:00
|
|
|
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->AddObjCMessageExprCheck(FoundationCheck.get());
|
2008-03-27 17:17:22 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
// Execute the worklist algorithm.
|
2008-03-31 18:26:32 +00:00
|
|
|
Eng.ExecuteWorkList(120000);
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-26 21:31:18 +00:00
|
|
|
SourceManager& SrcMgr = Ctx.getSourceManager();
|
|
|
|
|
2008-04-02 07:05:46 +00:00
|
|
|
if (!PD)
|
|
|
|
EmitWarning(Diag, PD, SrcMgr,
|
|
|
|
CS->null_derefs_begin(), CS->null_derefs_end(),
|
|
|
|
"Dereference of NULL pointer.");
|
|
|
|
else
|
2008-04-02 22:03:53 +00:00
|
|
|
Report(*PD, Ctx, bugdesc::NullDeref(),
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->null_derefs_begin(), CS->null_derefs_end());
|
|
|
|
|
2008-02-26 21:31:18 +00:00
|
|
|
|
2008-03-31 18:26:32 +00:00
|
|
|
EmitWarning(Diag, PD, SrcMgr,
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->undef_derefs_begin(),
|
|
|
|
CS->undef_derefs_end(),
|
2008-02-28 09:25:22 +00:00
|
|
|
"Dereference of undefined value.");
|
2008-02-26 21:31:18 +00:00
|
|
|
|
2008-03-31 18:26:32 +00:00
|
|
|
EmitWarning(Diag, PD, SrcMgr,
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->undef_branches_begin(),
|
|
|
|
CS->undef_branches_end(),
|
2008-03-25 16:40:05 +00:00
|
|
|
"Branch condition evaluates to an uninitialized value.");
|
2008-02-26 21:31:18 +00:00
|
|
|
|
2008-03-31 18:26:32 +00:00
|
|
|
EmitWarning(Diag, PD, SrcMgr,
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->explicit_bad_divides_begin(),
|
|
|
|
CS->explicit_bad_divides_end(),
|
2008-02-28 09:25:22 +00:00
|
|
|
"Division by zero/undefined value.");
|
2008-02-28 20:32:03 +00:00
|
|
|
|
2008-03-31 18:26:32 +00:00
|
|
|
EmitWarning(Diag, PD, SrcMgr,
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->undef_results_begin(),
|
|
|
|
CS->undef_results_end(),
|
2008-02-28 20:32:03 +00:00
|
|
|
"Result of operation is undefined.");
|
2008-02-29 23:14:48 +00:00
|
|
|
|
2008-03-31 18:26:32 +00:00
|
|
|
EmitWarning(Diag, PD, SrcMgr,
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->bad_calls_begin(),
|
|
|
|
CS->bad_calls_end(),
|
2008-02-29 23:14:48 +00:00
|
|
|
"Call using a NULL or undefined function pointer value.");
|
2008-02-29 23:53:11 +00:00
|
|
|
|
2008-03-31 18:26:32 +00:00
|
|
|
EmitWarning(Diag, PD, SrcMgr,
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->undef_arg_begin(),
|
|
|
|
CS->undef_arg_end(),
|
2008-03-25 16:40:05 +00:00
|
|
|
"Pass-by-value argument in function is undefined.");
|
2008-03-14 18:14:50 +00:00
|
|
|
|
2008-03-31 18:26:32 +00:00
|
|
|
EmitWarning(Diag, PD, SrcMgr,
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->msg_expr_undef_arg_begin(),
|
|
|
|
CS->msg_expr_undef_arg_end(),
|
2008-03-25 16:40:05 +00:00
|
|
|
"Pass-by-value argument in message expression is undefined.");
|
|
|
|
|
2008-03-31 18:26:32 +00:00
|
|
|
EmitWarning(Diag, PD, SrcMgr,
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->undef_receivers_begin(),
|
|
|
|
CS->undef_receivers_end(),
|
2008-03-25 16:40:05 +00:00
|
|
|
"Receiver in message expression is an uninitialized value.");
|
2008-03-31 15:02:58 +00:00
|
|
|
|
2008-03-31 18:26:32 +00:00
|
|
|
EmitWarning(Diag, PD, SrcMgr,
|
2008-04-02 07:05:46 +00:00
|
|
|
CS->ret_stackaddr_begin(),
|
|
|
|
CS->ret_stackaddr_end(),
|
2008-03-31 15:02:58 +00:00
|
|
|
"Address of stack-allocated variable returned.");
|
2008-03-25 16:40:05 +00:00
|
|
|
|
2008-03-27 21:15:17 +00:00
|
|
|
FoundationCheck.get()->ReportResults(Diag);
|
2008-02-14 22:36:46 +00:00
|
|
|
#ifndef NDEBUG
|
2008-04-02 07:05:46 +00:00
|
|
|
if (Visualize) CS->ViewGraph(TrimGraph);
|
2008-02-19 00:22:37 +00:00
|
|
|
#endif
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
return Eng.getGraph().size();
|
2008-02-21 18:02:17 +00:00
|
|
|
}
|
|
|
|
|
2008-02-14 22:36:46 +00:00
|
|
|
} // end clang namespace
|
|
|
|
|
2008-02-14 18:28:23 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function for Casts.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, NonLVal X, QualType T) {
|
2008-02-18 22:57:02 +00:00
|
|
|
|
2008-02-14 18:28:23 +00:00
|
|
|
if (!isa<nonlval::ConcreteInt>(X))
|
|
|
|
return UnknownVal();
|
2008-03-12 01:21:45 +00:00
|
|
|
|
|
|
|
BasicValueFactory& BasicVals = Eng.getBasicVals();
|
2008-02-14 18:28:23 +00:00
|
|
|
|
|
|
|
llvm::APSInt V = cast<nonlval::ConcreteInt>(X).getValue();
|
2008-03-14 18:14:50 +00:00
|
|
|
V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType()
|
|
|
|
|| T->isObjCQualifiedIdType());
|
2008-03-12 01:21:45 +00:00
|
|
|
V.extOrTrunc(Eng.getContext().getTypeSize(T));
|
2008-02-14 18:28:23 +00:00
|
|
|
|
2008-02-21 18:43:30 +00:00
|
|
|
if (T->isPointerType())
|
2008-03-07 20:13:31 +00:00
|
|
|
return lval::ConcreteInt(BasicVals.getValue(V));
|
2008-02-14 18:28:23 +00:00
|
|
|
else
|
2008-03-07 20:13:31 +00:00
|
|
|
return nonlval::ConcreteInt(BasicVals.getValue(V));
|
2008-02-14 18:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Casts.
|
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, LVal X, QualType T) {
|
2008-02-18 22:57:02 +00:00
|
|
|
|
2008-04-02 17:45:06 +00:00
|
|
|
if (T->isPointerLikeType() || T->isObjCQualifiedIdType())
|
2008-02-14 18:28:23 +00:00
|
|
|
return X;
|
|
|
|
|
2008-02-21 18:43:30 +00:00
|
|
|
assert (T->isIntegerType());
|
2008-02-14 18:28:23 +00:00
|
|
|
|
|
|
|
if (!isa<lval::ConcreteInt>(X))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
BasicValueFactory& BasicVals = Eng.getBasicVals();
|
|
|
|
|
2008-02-14 18:28:23 +00:00
|
|
|
llvm::APSInt V = cast<lval::ConcreteInt>(X).getValue();
|
|
|
|
V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType());
|
2008-03-12 01:21:45 +00:00
|
|
|
V.extOrTrunc(Eng.getContext().getTypeSize(T));
|
2008-02-14 18:28:23 +00:00
|
|
|
|
2008-03-07 20:13:31 +00:00
|
|
|
return nonlval::ConcreteInt(BasicVals.getValue(V));
|
2008-02-14 18:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unary operators.
|
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
RVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLVal X){
|
2008-02-18 22:57:02 +00:00
|
|
|
|
2008-02-14 18:40:24 +00:00
|
|
|
switch (X.getSubKind()) {
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-14 18:40:24 +00:00
|
|
|
case nonlval::ConcreteIntKind:
|
2008-03-12 01:21:45 +00:00
|
|
|
return cast<nonlval::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U);
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-14 18:40:24 +00:00
|
|
|
default:
|
2008-02-21 18:02:17 +00:00
|
|
|
return UnknownVal();
|
2008-02-20 04:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
RVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLVal X) {
|
2008-02-20 04:12:31 +00:00
|
|
|
|
2008-02-14 18:40:24 +00:00
|
|
|
switch (X.getSubKind()) {
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-14 18:40:24 +00:00
|
|
|
case nonlval::ConcreteIntKind:
|
2008-03-12 01:21:45 +00:00
|
|
|
return cast<nonlval::ConcreteInt>(X).EvalComplement(Eng.getBasicVals());
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-14 18:40:24 +00:00
|
|
|
default:
|
2008-02-21 18:02:17 +00:00
|
|
|
return UnknownVal();
|
2008-02-14 18:40:24 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-14 19:37:24 +00:00
|
|
|
|
|
|
|
// Binary operators.
|
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
|
|
|
|
NonLVal L, NonLVal R) {
|
|
|
|
|
|
|
|
BasicValueFactory& BasicVals = Eng.getBasicVals();
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
while (1) {
|
2008-02-14 19:37:24 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
switch (L.getSubKind()) {
|
2008-02-14 19:37:24 +00:00
|
|
|
default:
|
2008-02-21 19:10:12 +00:00
|
|
|
return UnknownVal();
|
2008-02-14 19:37:24 +00:00
|
|
|
|
|
|
|
case nonlval::ConcreteIntKind:
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
if (isa<nonlval::ConcreteInt>(R)) {
|
|
|
|
const nonlval::ConcreteInt& L_CI = cast<nonlval::ConcreteInt>(L);
|
|
|
|
const nonlval::ConcreteInt& R_CI = cast<nonlval::ConcreteInt>(R);
|
2008-03-07 20:13:31 +00:00
|
|
|
return L_CI.EvalBinOp(BasicVals, Op, R_CI);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-02-21 18:02:17 +00:00
|
|
|
NonLVal tmp = R;
|
|
|
|
R = L;
|
|
|
|
L = tmp;
|
2008-02-14 19:37:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
case nonlval::SymbolValKind: {
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
if (isa<nonlval::ConcreteInt>(R)) {
|
2008-02-14 19:37:24 +00:00
|
|
|
const SymIntConstraint& C =
|
2008-03-07 20:13:31 +00:00
|
|
|
BasicVals.getConstraint(cast<nonlval::SymbolVal>(L).getSymbol(), Op,
|
2008-03-12 01:21:45 +00:00
|
|
|
cast<nonlval::ConcreteInt>(R).getValue());
|
2008-02-14 19:37:24 +00:00
|
|
|
|
|
|
|
return nonlval::SymIntConstraintVal(C);
|
|
|
|
}
|
|
|
|
else
|
2008-02-21 18:02:17 +00:00
|
|
|
return UnknownVal();
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-15 00:52:26 +00:00
|
|
|
|
2008-02-15 23:15:23 +00:00
|
|
|
// Binary Operators (except assignments and comma).
|
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
|
2008-02-21 18:02:17 +00:00
|
|
|
LVal L, LVal R) {
|
2008-02-18 22:57:02 +00:00
|
|
|
|
2008-02-15 23:15:23 +00:00
|
|
|
switch (Op) {
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-15 23:15:23 +00:00
|
|
|
default:
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
case BinaryOperator::EQ:
|
2008-03-12 01:21:45 +00:00
|
|
|
return EvalEQ(Eng, L, R);
|
2008-02-15 23:15:23 +00:00
|
|
|
|
|
|
|
case BinaryOperator::NE:
|
2008-03-12 01:21:45 +00:00
|
|
|
return EvalNE(Eng, L, R);
|
2008-02-15 23:15:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-15 00:52:26 +00:00
|
|
|
// Pointer arithmetic.
|
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
|
2008-02-21 18:02:17 +00:00
|
|
|
LVal L, NonLVal R) {
|
|
|
|
return UnknownVal();
|
2008-02-15 00:52:26 +00:00
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
// Equality operators for LVals.
|
2008-02-14 19:37:24 +00:00
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
RVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, LVal L, LVal R) {
|
|
|
|
|
|
|
|
BasicValueFactory& BasicVals = Eng.getBasicVals();
|
2008-02-18 22:57:02 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
switch (L.getSubKind()) {
|
|
|
|
|
2008-02-14 19:37:24 +00:00
|
|
|
default:
|
2008-02-21 18:02:17 +00:00
|
|
|
assert(false && "EQ not implemented for this LVal.");
|
|
|
|
return UnknownVal();
|
2008-02-14 19:37:24 +00:00
|
|
|
|
|
|
|
case lval::ConcreteIntKind:
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
if (isa<lval::ConcreteInt>(R)) {
|
|
|
|
bool b = cast<lval::ConcreteInt>(L).getValue() ==
|
|
|
|
cast<lval::ConcreteInt>(R).getValue();
|
2008-02-14 19:37:24 +00:00
|
|
|
|
2008-03-07 20:13:31 +00:00
|
|
|
return NonLVal::MakeIntTruthVal(BasicVals, b);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
2008-02-21 18:02:17 +00:00
|
|
|
else if (isa<lval::SymbolVal>(R)) {
|
2008-02-14 19:37:24 +00:00
|
|
|
|
|
|
|
const SymIntConstraint& C =
|
2008-03-07 20:13:31 +00:00
|
|
|
BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(),
|
2008-02-14 19:37:24 +00:00
|
|
|
BinaryOperator::EQ,
|
2008-02-21 18:02:17 +00:00
|
|
|
cast<lval::ConcreteInt>(L).getValue());
|
2008-02-14 19:37:24 +00:00
|
|
|
|
|
|
|
return nonlval::SymIntConstraintVal(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
case lval::SymbolValKind: {
|
|
|
|
|
|
|
|
if (isa<lval::ConcreteInt>(R)) {
|
|
|
|
const SymIntConstraint& C =
|
2008-03-07 20:13:31 +00:00
|
|
|
BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(),
|
2008-02-21 18:02:17 +00:00
|
|
|
BinaryOperator::EQ,
|
|
|
|
cast<lval::ConcreteInt>(R).getValue());
|
2008-02-14 19:37:24 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
return nonlval::SymIntConstraintVal(C);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2008-02-22 18:41:59 +00:00
|
|
|
// FIXME: Implement == for lval Symbols. This is mainly useful
|
|
|
|
// in iterator loops when traversing a buffer, e.g. while(z != zTerm).
|
|
|
|
// Since this is not useful for many checkers we'll punt on this for
|
|
|
|
// now.
|
|
|
|
|
|
|
|
return UnknownVal();
|
2008-02-21 18:02:17 +00:00
|
|
|
}
|
2008-02-14 19:37:24 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
case lval::DeclValKind:
|
2008-02-22 00:54:56 +00:00
|
|
|
case lval::FuncValKind:
|
|
|
|
case lval::GotoLabelKind:
|
2008-03-07 20:13:31 +00:00
|
|
|
return NonLVal::MakeIntTruthVal(BasicVals, L == R);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2008-03-07 20:13:31 +00:00
|
|
|
return NonLVal::MakeIntTruthVal(BasicVals, false);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
RVal GRSimpleVals::EvalNE(GRExprEngine& Eng, LVal L, LVal R) {
|
2008-02-18 22:57:02 +00:00
|
|
|
|
2008-03-12 01:21:45 +00:00
|
|
|
BasicValueFactory& BasicVals = Eng.getBasicVals();
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
switch (L.getSubKind()) {
|
2008-02-14 19:37:24 +00:00
|
|
|
|
|
|
|
default:
|
2008-02-21 18:02:17 +00:00
|
|
|
assert(false && "NE not implemented for this LVal.");
|
|
|
|
return UnknownVal();
|
2008-02-14 19:37:24 +00:00
|
|
|
|
|
|
|
case lval::ConcreteIntKind:
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
if (isa<lval::ConcreteInt>(R)) {
|
|
|
|
bool b = cast<lval::ConcreteInt>(L).getValue() !=
|
|
|
|
cast<lval::ConcreteInt>(R).getValue();
|
2008-02-14 19:37:24 +00:00
|
|
|
|
2008-03-07 20:13:31 +00:00
|
|
|
return NonLVal::MakeIntTruthVal(BasicVals, b);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
2008-02-21 18:02:17 +00:00
|
|
|
else if (isa<lval::SymbolVal>(R)) {
|
2008-02-14 19:37:24 +00:00
|
|
|
const SymIntConstraint& C =
|
2008-03-07 20:13:31 +00:00
|
|
|
BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(),
|
2008-03-12 01:21:45 +00:00
|
|
|
BinaryOperator::NE,
|
|
|
|
cast<lval::ConcreteInt>(L).getValue());
|
2008-02-14 19:37:24 +00:00
|
|
|
|
|
|
|
return nonlval::SymIntConstraintVal(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
case lval::SymbolValKind: {
|
|
|
|
if (isa<lval::ConcreteInt>(R)) {
|
|
|
|
const SymIntConstraint& C =
|
2008-03-07 20:13:31 +00:00
|
|
|
BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(),
|
2008-03-12 01:21:45 +00:00
|
|
|
BinaryOperator::NE,
|
|
|
|
cast<lval::ConcreteInt>(R).getValue());
|
2008-02-14 19:37:24 +00:00
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
return nonlval::SymIntConstraintVal(C);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2008-02-22 18:41:59 +00:00
|
|
|
// FIXME: Implement != for lval Symbols. This is mainly useful
|
|
|
|
// in iterator loops when traversing a buffer, e.g. while(z != zTerm).
|
|
|
|
// Since this is not useful for many checkers we'll punt on this for
|
|
|
|
// now.
|
|
|
|
|
|
|
|
return UnknownVal();
|
2008-02-14 19:37:24 +00:00
|
|
|
|
|
|
|
break;
|
2008-02-21 18:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case lval::DeclValKind:
|
2008-02-22 00:54:56 +00:00
|
|
|
case lval::FuncValKind:
|
|
|
|
case lval::GotoLabelKind:
|
2008-03-07 20:13:31 +00:00
|
|
|
return NonLVal::MakeIntTruthVal(BasicVals, L != R);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2008-03-07 20:13:31 +00:00
|
|
|
return NonLVal::MakeIntTruthVal(BasicVals, true);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
2008-02-26 23:04:29 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function for Function Calls.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-05 00:33:14 +00:00
|
|
|
void GRSimpleVals::EvalCall(ExplodedNodeSet<ValueState>& Dst,
|
2008-03-12 01:21:45 +00:00
|
|
|
GRExprEngine& Eng,
|
2008-03-05 00:33:14 +00:00
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
CallExpr* CE, LVal L,
|
|
|
|
ExplodedNode<ValueState>* Pred) {
|
|
|
|
|
2008-03-12 21:04:07 +00:00
|
|
|
ValueStateManager& StateMgr = Eng.getStateManager();
|
|
|
|
ValueState* St = Builder.GetState(Pred);
|
2008-02-26 23:04:29 +00:00
|
|
|
|
|
|
|
// Invalidate all arguments passed in by reference (LVals).
|
|
|
|
|
|
|
|
for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
|
2008-03-12 21:04:07 +00:00
|
|
|
RVal V = StateMgr.GetRVal(St, *I);
|
2008-02-26 23:04:29 +00:00
|
|
|
|
|
|
|
if (isa<LVal>(V))
|
2008-03-12 21:04:07 +00:00
|
|
|
St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal());
|
2008-02-26 23:04:29 +00:00
|
|
|
}
|
2008-03-12 21:04:07 +00:00
|
|
|
|
|
|
|
// Make up a symbol for the return value of this function.
|
|
|
|
|
|
|
|
if (CE->getType() != Eng.getContext().VoidTy) {
|
|
|
|
unsigned Count = Builder.getCurrentBlockCount();
|
2008-03-12 21:45:47 +00:00
|
|
|
SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
|
2008-03-12 21:04:07 +00:00
|
|
|
|
|
|
|
RVal X = CE->getType()->isPointerType()
|
|
|
|
? cast<RVal>(lval::SymbolVal(Sym))
|
|
|
|
: cast<RVal>(nonlval::SymbolVal(Sym));
|
|
|
|
|
|
|
|
St = StateMgr.SetRVal(St, CE, X, Eng.getCFG().isBlkExpr(CE), false);
|
|
|
|
}
|
2008-03-05 00:33:14 +00:00
|
|
|
|
2008-03-21 21:30:14 +00:00
|
|
|
Builder.MakeNode(Dst, CE, Pred, St);
|
2008-02-26 23:04:29 +00:00
|
|
|
}
|