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-28 09:27:54 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:27:54 +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"
|
2011-02-28 09:27:54 +08:00
|
|
|
#include "clang/AST/CharUnits.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 {
|
2011-03-01 09:16:21 +08:00
|
|
|
class VLASizeChecker : public Checker< check::PreStmt<DeclStmt> > {
|
2011-02-28 09:27:54 +08:00
|
|
|
mutable llvm::OwningPtr<BugType> BT_zero;
|
|
|
|
mutable llvm::OwningPtr<BugType> BT_undef;
|
2009-11-07 05:51:50 +08:00
|
|
|
|
|
|
|
public:
|
2011-02-28 09:27:54 +08:00
|
|
|
void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
|
2009-11-07 05:51:50 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-02-28 09:27:54 +08:00
|
|
|
void VLASizeChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
|
2009-11-07 11:56:57 +08:00
|
|
|
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.
|
2011-08-13 07:37:29 +08:00
|
|
|
const Expr *SE = VLA->getSizeExpr();
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *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)
|
2011-02-28 09:27:54 +08:00
|
|
|
BT_undef.reset(new BuiltinBug("Declared variable-length array (VLA) "
|
|
|
|
"uses a garbage value as its size"));
|
2009-11-07 11:56:57 +08:00
|
|
|
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *report =
|
|
|
|
new BugReport(*BT_undef, BT_undef->getName(), N);
|
2009-11-07 11:56:57 +08:00
|
|
|
report->addRange(SE->getSourceRange());
|
2011-08-20 06:33:38 +08:00
|
|
|
report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, SE));
|
2009-11-07 11:56:57 +08:00
|
|
|
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
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *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) {
|
2011-08-13 07:37:29 +08:00
|
|
|
ExplodedNode *N = C.generateSink(stateZero);
|
2009-11-07 11:56:57 +08:00
|
|
|
if (!BT_zero)
|
2011-02-28 09:27:54 +08:00
|
|
|
BT_zero.reset(new BuiltinBug("Declared variable-length array (VLA) has "
|
|
|
|
"zero size"));
|
2009-11-07 11:56:57 +08:00
|
|
|
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *report =
|
|
|
|
new BugReport(*BT_zero, BT_zero->getName(), N);
|
2009-11-07 11:56:57 +08:00
|
|
|
report->addRange(SE->getSourceRange());
|
2011-08-20 06:33:38 +08:00
|
|
|
report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, SE));
|
2009-11-07 11:56:57 +08:00
|
|
|
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
|
|
|
}
|
2011-02-28 09:27:54 +08:00
|
|
|
|
|
|
|
void ento::registerVLASizeChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<VLASizeChecker>();
|
|
|
|
}
|