2009-11-04 09:43:07 +08:00
|
|
|
//=== VLASizeChecker.cpp - Undefined dereference checker --------*- C++ -*-===//
|
2009-11-03 20:13:38 +08:00
|
|
|
//
|
|
|
|
// 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 VLASizeChecker, a builtin check in ExprEngine that
|
2009-11-04 09:43:07 +08:00
|
|
|
// performs checks for declaration of VLA of undefined or zero size.
|
2010-07-07 07:33:54 +08:00
|
|
|
// In addition, VLASizeChecker is responsible for defining the extent
|
|
|
|
// of the MemRegion that represents a VLA.
|
2009-11-03 20:13:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-09 06:30:02 +08:00
|
|
|
#include "InternalChecks.h"
|
2010-07-05 08:50:15 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2010-12-24 03:38:26 +08:00
|
|
|
#include "clang/StaticAnalyzer/BugReporter/BugType.h"
|
|
|
|
#include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
|
|
|
|
#include "clang/StaticAnalyzer/PathSensitive/ExprEngine.h"
|
2009-11-03 20:13:38 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-03 20:13:38 +08:00
|
|
|
|
2009-11-07 05:51:50 +08:00
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class VLASizeChecker : public CheckerVisitor<VLASizeChecker> {
|
2009-11-07 11:56:57 +08:00
|
|
|
BugType *BT_zero;
|
|
|
|
BugType *BT_undef;
|
2009-11-07 05:51:50 +08:00
|
|
|
|
|
|
|
public:
|
2009-11-07 11:56:57 +08:00
|
|
|
VLASizeChecker() : BT_zero(0), BT_undef(0) {}
|
|
|
|
static void *getTag() { static int tag = 0; return &tag; }
|
|
|
|
void PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS);
|
2009-11-07 05:51:50 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2010-12-23 15:20:52 +08:00
|
|
|
void ento::RegisterVLASizeChecker(ExprEngine &Eng) {
|
2009-11-07 11:56:57 +08:00
|
|
|
Eng.registerCheck(new VLASizeChecker());
|
2009-11-04 09:43:07 +08:00
|
|
|
}
|
|
|
|
|
2009-11-07 11:56:57 +08:00
|
|
|
void VLASizeChecker::PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS) {
|
|
|
|
if (!DS->isSingleDecl())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
|
|
|
|
if (!VD)
|
|
|
|
return;
|
2010-07-05 08:50:15 +08:00
|
|
|
|
|
|
|
ASTContext &Ctx = C.getASTContext();
|
|
|
|
const VariableArrayType *VLA = Ctx.getAsVariableArrayType(VD->getType());
|
2009-11-07 11:56:57 +08:00
|
|
|
if (!VLA)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: Handle multi-dimensional VLAs.
|
|
|
|
const Expr* SE = VLA->getSizeExpr();
|
|
|
|
const GRState *state = C.getState();
|
2010-02-09 00:18:51 +08:00
|
|
|
SVal sizeV = state->getSVal(SE);
|
2009-11-07 11:56:57 +08:00
|
|
|
|
|
|
|
if (sizeV.isUndef()) {
|
|
|
|
// Generate an error node.
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink();
|
2009-11-07 11:56:57 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT_undef)
|
|
|
|
BT_undef = new BuiltinBug("Declared variable-length array (VLA) uses a "
|
|
|
|
"garbage value as its size");
|
|
|
|
|
|
|
|
EnhancedBugReport *report =
|
2009-11-14 20:08:24 +08:00
|
|
|
new EnhancedBugReport(*BT_undef, BT_undef->getName(), N);
|
2009-11-07 11:56:57 +08:00
|
|
|
report->addRange(SE->getSourceRange());
|
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE);
|
|
|
|
C.EmitReport(report);
|
|
|
|
return;
|
2009-11-04 09:43:07 +08:00
|
|
|
}
|
2010-07-05 08:50:15 +08:00
|
|
|
|
|
|
|
// See if the size value is known. It can't be undefined because we would have
|
|
|
|
// warned about that already.
|
|
|
|
if (sizeV.isUnknown())
|
|
|
|
return;
|
2009-11-07 11:56:57 +08:00
|
|
|
|
|
|
|
// Check if the size is zero.
|
2010-07-05 08:50:15 +08:00
|
|
|
DefinedSVal sizeD = cast<DefinedSVal>(sizeV);
|
2009-11-07 11:56:57 +08:00
|
|
|
|
|
|
|
const GRState *stateNotZero, *stateZero;
|
2010-12-02 06:16:56 +08:00
|
|
|
llvm::tie(stateNotZero, stateZero) = state->assume(sizeD);
|
2009-11-07 11:56:57 +08:00
|
|
|
|
|
|
|
if (stateZero && !stateNotZero) {
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode* N = C.generateSink(stateZero);
|
2009-11-07 11:56:57 +08:00
|
|
|
if (!BT_zero)
|
|
|
|
BT_zero = new BuiltinBug("Declared variable-length array (VLA) has zero "
|
|
|
|
"size");
|
|
|
|
|
|
|
|
EnhancedBugReport *report =
|
2009-11-14 20:08:24 +08:00
|
|
|
new EnhancedBugReport(*BT_zero, BT_zero->getName(), N);
|
2009-11-07 11:56:57 +08:00
|
|
|
report->addRange(SE->getSourceRange());
|
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE);
|
|
|
|
C.EmitReport(report);
|
|
|
|
return;
|
2009-11-03 20:13:38 +08:00
|
|
|
}
|
2009-11-07 11:56:57 +08:00
|
|
|
|
|
|
|
// From this point on, assume that the size is not zero.
|
2010-07-05 08:50:15 +08:00
|
|
|
state = stateNotZero;
|
|
|
|
|
2010-07-07 07:33:54 +08:00
|
|
|
// VLASizeChecker is responsible for defining the extent of the array being
|
|
|
|
// declared. We do this by multiplying the array length by the element size,
|
|
|
|
// then matching that with the array region's extent symbol.
|
|
|
|
|
2010-07-05 08:50:15 +08:00
|
|
|
// Convert the array length to size_t.
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2010-07-05 08:50:15 +08:00
|
|
|
QualType SizeTy = Ctx.getSizeType();
|
2010-12-02 15:49:45 +08:00
|
|
|
NonLoc ArrayLength = cast<NonLoc>(svalBuilder.evalCast(sizeD, SizeTy,
|
|
|
|
SE->getType()));
|
2010-07-05 08:50:15 +08:00
|
|
|
|
|
|
|
// Get the element size.
|
|
|
|
CharUnits EleSize = Ctx.getTypeSizeInChars(VLA->getElementType());
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal EleSizeVal = svalBuilder.makeIntVal(EleSize.getQuantity(), SizeTy);
|
2010-07-05 08:50:15 +08:00
|
|
|
|
|
|
|
// Multiply the array length by the element size.
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal ArraySizeVal = svalBuilder.evalBinOpNN(state, BO_Mul, ArrayLength,
|
|
|
|
cast<NonLoc>(EleSizeVal), SizeTy);
|
2010-07-05 08:50:15 +08:00
|
|
|
|
2010-12-02 06:16:56 +08:00
|
|
|
// Finally, assume that the array's extent matches the given size.
|
2010-07-05 08:50:15 +08:00
|
|
|
const LocationContext *LC = C.getPredecessor()->getLocationContext();
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal Extent =
|
|
|
|
state->getRegion(VD, LC)->getExtent(svalBuilder);
|
2010-07-05 08:50:15 +08:00
|
|
|
DefinedOrUnknownSVal ArraySize = cast<DefinedOrUnknownSVal>(ArraySizeVal);
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal sizeIsKnown =
|
|
|
|
svalBuilder.evalEQ(state, Extent, ArraySize);
|
|
|
|
state = state->assume(sizeIsKnown, true);
|
2010-07-05 08:50:15 +08:00
|
|
|
|
2010-07-06 15:08:47 +08:00
|
|
|
// Assume should not fail at this point.
|
|
|
|
assert(state);
|
|
|
|
|
2010-07-05 08:50:15 +08:00
|
|
|
// Remember our assumptions!
|
|
|
|
C.addTransition(state);
|
2009-11-03 20:13:38 +08:00
|
|
|
}
|