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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This files defines GRSimpleVals, a sub-class of GRTransferFuncs that
|
|
|
|
// provides transfer functions for performing simple value tracking with
|
|
|
|
// limited support for symbolics.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "GRSimpleVals.h"
|
2008-02-14 22:36:46 +00:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2008-02-14 18:28:23 +00:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-02-14 22:36:46 +00:00
|
|
|
namespace clang {
|
2008-02-26 21:31:18 +00:00
|
|
|
|
|
|
|
template <typename ITERATOR>
|
|
|
|
static void EmitWarning(Diagnostic& Diag, SourceManager& SrcMgr,
|
|
|
|
ITERATOR I, ITERATOR E, const char* msg) {
|
|
|
|
|
|
|
|
bool isFirst;
|
|
|
|
unsigned ErrorDiag;
|
|
|
|
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
|
|
|
|
if (isFirst) {
|
|
|
|
isFirst = false;
|
|
|
|
ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
const PostStmt& L = cast<PostStmt>((*I)->getLocation());
|
|
|
|
Expr* Exp = cast<Expr>(L.getStmt());
|
|
|
|
|
|
|
|
Diag.Report(FullSourceLoc(Exp->getExprLoc(), SrcMgr), ErrorDiag);
|
|
|
|
}
|
|
|
|
}
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
unsigned RunGRSimpleVals(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx,
|
|
|
|
Diagnostic& Diag, bool Visualize) {
|
|
|
|
|
|
|
|
if (Diag.hasErrorOccurred())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx);
|
|
|
|
GRExprEngine* CheckerState = &Engine.getCheckerState();
|
|
|
|
GRSimpleVals GRSV;
|
|
|
|
CheckerState->setTransferFunctions(GRSV);
|
|
|
|
|
|
|
|
// Execute the worklist algorithm.
|
2008-02-26 21:31:18 +00:00
|
|
|
Engine.ExecuteWorkList(20000);
|
2008-02-21 18:02:17 +00:00
|
|
|
|
2008-02-26 21:31:18 +00:00
|
|
|
SourceManager& SrcMgr = Ctx.getSourceManager();
|
|
|
|
|
|
|
|
EmitWarning(Diag, SrcMgr,
|
|
|
|
CheckerState->null_derefs_begin(),
|
|
|
|
CheckerState->null_derefs_end(),
|
|
|
|
"NULL pointer is dereferenced after it is checked for NULL.");
|
|
|
|
|
|
|
|
EmitWarning(Diag, SrcMgr,
|
|
|
|
CheckerState->uninit_derefs_begin(),
|
|
|
|
CheckerState->uninit_derefs_end(),
|
|
|
|
"Dereference of uninitialized value.");
|
|
|
|
|
|
|
|
EmitWarning(Diag, SrcMgr,
|
|
|
|
CheckerState->uninit_derefs_begin(),
|
|
|
|
CheckerState->uninit_derefs_end(),
|
|
|
|
"Dereference of uninitialized value.");
|
|
|
|
|
|
|
|
EmitWarning(Diag, SrcMgr,
|
|
|
|
CheckerState->bad_divides_begin(),
|
|
|
|
CheckerState->bad_divides_end(),
|
|
|
|
"Division by zero/uninitialized value.");
|
2008-02-14 22:36:46 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2008-02-21 18:02:17 +00:00
|
|
|
if (Visualize) CheckerState->ViewGraph();
|
2008-02-19 00:22:37 +00:00
|
|
|
#endif
|
2008-02-21 18:02:17 +00:00
|
|
|
|
|
|
|
return Engine.getGraph().size();
|
|
|
|
}
|
|
|
|
|
2008-02-14 22:36:46 +00:00
|
|
|
} // end clang namespace
|
|
|
|
|
2008-02-14 18:28:23 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function for Casts.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-21 18:43:30 +00:00
|
|
|
RVal GRSimpleVals::EvalCast(ValueManager& ValMgr, 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();
|
|
|
|
|
|
|
|
llvm::APSInt V = cast<nonlval::ConcreteInt>(X).getValue();
|
|
|
|
V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType());
|
2008-02-21 18:43:30 +00:00
|
|
|
V.extOrTrunc(ValMgr.getContext().getTypeSize(T, SourceLocation()));
|
2008-02-14 18:28:23 +00:00
|
|
|
|
2008-02-21 18:43:30 +00:00
|
|
|
if (T->isPointerType())
|
2008-02-14 18:28:23 +00:00
|
|
|
return lval::ConcreteInt(ValMgr.getValue(V));
|
|
|
|
else
|
|
|
|
return nonlval::ConcreteInt(ValMgr.getValue(V));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Casts.
|
|
|
|
|
2008-02-21 18:43:30 +00:00
|
|
|
RVal GRSimpleVals::EvalCast(ValueManager& ValMgr, LVal X, QualType T) {
|
2008-02-18 22:57:02 +00:00
|
|
|
|
2008-02-21 18:43:30 +00:00
|
|
|
if (T->isPointerType())
|
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();
|
|
|
|
|
|
|
|
llvm::APSInt V = cast<lval::ConcreteInt>(X).getValue();
|
|
|
|
V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType());
|
2008-02-21 18:43:30 +00:00
|
|
|
V.extOrTrunc(ValMgr.getContext().getTypeSize(T, SourceLocation()));
|
2008-02-14 18:28:23 +00:00
|
|
|
|
|
|
|
return nonlval::ConcreteInt(ValMgr.getValue(V));
|
2008-02-14 18:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unary operators.
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
RVal GRSimpleVals::EvalMinus(ValueManager& ValMgr, 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:
|
|
|
|
return cast<nonlval::ConcreteInt>(X).EvalMinus(ValMgr, 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-02-21 18:02:17 +00:00
|
|
|
RVal GRSimpleVals::EvalComplement(ValueManager& ValMgr, 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:
|
|
|
|
return cast<nonlval::ConcreteInt>(X).EvalComplement(ValMgr);
|
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-02-21 18:02:17 +00:00
|
|
|
RVal GRSimpleVals::EvalBinOp(ValueManager& ValMgr, BinaryOperator::Opcode Op,
|
|
|
|
NonLVal L, NonLVal R) {
|
|
|
|
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);
|
|
|
|
return L_CI.EvalBinOp(ValMgr, 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-02-21 18:02:17 +00:00
|
|
|
ValMgr.getConstraint(cast<nonlval::SymbolVal>(L).getSymbol(), Op,
|
|
|
|
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-02-21 18:02:17 +00:00
|
|
|
RVal GRSimpleVals::EvalBinOp(ValueManager& ValMgr, BinaryOperator::Opcode Op,
|
|
|
|
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-02-21 18:02:17 +00:00
|
|
|
return EvalEQ(ValMgr, L, R);
|
2008-02-15 23:15:23 +00:00
|
|
|
|
|
|
|
case BinaryOperator::NE:
|
2008-02-21 18:02:17 +00:00
|
|
|
return EvalNE(ValMgr, L, R);
|
2008-02-15 23:15:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-15 00:52:26 +00:00
|
|
|
// Pointer arithmetic.
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
RVal GRSimpleVals::EvalBinOp(ValueManager& ValMgr, BinaryOperator::Opcode Op,
|
|
|
|
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-02-21 18:02:17 +00:00
|
|
|
RVal GRSimpleVals::EvalEQ(ValueManager& ValMgr, LVal L, LVal R) {
|
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-02-21 18:02:17 +00:00
|
|
|
return NonLVal::MakeIntTruthVal(ValMgr, 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-02-21 18:02:17 +00:00
|
|
|
ValMgr.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 =
|
|
|
|
ValMgr.getConstraint(cast<lval::SymbolVal>(L).getSymbol(),
|
|
|
|
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:
|
|
|
|
return NonLVal::MakeIntTruthVal(ValMgr, L == R);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
return NonLVal::MakeIntTruthVal(ValMgr, false);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
RVal GRSimpleVals::EvalNE(ValueManager& ValMgr, LVal L, LVal R) {
|
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 && "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-02-21 18:02:17 +00:00
|
|
|
return NonLVal::MakeIntTruthVal(ValMgr, 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-02-21 18:02:17 +00:00
|
|
|
ValMgr.getConstraint(cast<lval::SymbolVal>(R).getSymbol(),
|
2008-02-14 19:37:24 +00:00
|
|
|
BinaryOperator::NE,
|
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 =
|
|
|
|
ValMgr.getConstraint(cast<lval::SymbolVal>(L).getSymbol(),
|
|
|
|
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:
|
|
|
|
return NonLVal::MakeIntTruthVal(ValMgr, L != R);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2008-02-21 18:02:17 +00:00
|
|
|
return NonLVal::MakeIntTruthVal(ValMgr, true);
|
2008-02-14 19:37:24 +00:00
|
|
|
}
|