2009-11-11 20:33:27 +08:00
|
|
|
//== ArrayBoundChecker.cpp ------------------------------*- 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 ArrayBoundChecker, which is a path-sensitive check
|
|
|
|
// which looks for an out-of-bound array element access.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "GRExprEngineInternalChecks.h"
|
2010-03-28 05:19:47 +08:00
|
|
|
#include "clang/Checker/BugReporter/BugType.h"
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "clang/Checker/PathSensitive/CheckerVisitor.h"
|
2010-03-28 05:19:47 +08:00
|
|
|
#include "clang/Checker/PathSensitive/GRExprEngine.h"
|
2009-11-11 20:33:27 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class ArrayBoundChecker :
|
2009-11-11 20:33:27 +08:00
|
|
|
public CheckerVisitor<ArrayBoundChecker> {
|
|
|
|
BuiltinBug *BT;
|
|
|
|
public:
|
|
|
|
ArrayBoundChecker() : BT(0) {}
|
|
|
|
static void *getTag();
|
|
|
|
void VisitLocation(CheckerContext &C, const Stmt *S, SVal l);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang::RegisterArrayBoundChecker(GRExprEngine &Eng) {
|
|
|
|
Eng.registerCheck(new ArrayBoundChecker());
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ArrayBoundChecker::getTag() {
|
|
|
|
static int x = 0; return &x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayBoundChecker::VisitLocation(CheckerContext &C, const Stmt *S, SVal l){
|
|
|
|
// Check for out of bound array element access.
|
|
|
|
const MemRegion *R = l.getAsRegion();
|
|
|
|
if (!R)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ElementRegion *ER = dyn_cast<ElementRegion>(R);
|
|
|
|
if (!ER)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the index of the accessed element.
|
2010-09-09 18:51:37 +08:00
|
|
|
DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
|
2009-11-11 20:33:27 +08:00
|
|
|
|
2010-11-26 17:14:07 +08:00
|
|
|
// Zero index is always in bound, this also passes ElementRegions created for
|
|
|
|
// pointer casts.
|
|
|
|
if (Idx.isZeroConstant())
|
|
|
|
return;
|
|
|
|
|
2009-11-11 20:33:27 +08:00
|
|
|
const GRState *state = C.getState();
|
|
|
|
|
|
|
|
// Get the size of the array.
|
2009-11-12 10:48:32 +08:00
|
|
|
DefinedOrUnknownSVal NumElements
|
2010-01-18 16:54:31 +08:00
|
|
|
= C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(),
|
2010-08-11 14:10:55 +08:00
|
|
|
ER->getValueType());
|
2009-11-11 20:33:27 +08:00
|
|
|
|
|
|
|
const GRState *StInBound = state->AssumeInBound(Idx, NumElements, true);
|
|
|
|
const GRState *StOutBound = state->AssumeInBound(Idx, NumElements, false);
|
|
|
|
if (StOutBound && !StInBound) {
|
2009-11-24 06:22:01 +08:00
|
|
|
ExplodedNode *N = C.GenerateSink(StOutBound);
|
2009-11-11 20:33:27 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT)
|
|
|
|
BT = new BuiltinBug("Out-of-bound array access",
|
|
|
|
"Access out-of-bound array element (buffer overflow)");
|
|
|
|
|
|
|
|
// FIXME: It would be nice to eventually make this diagnostic more clear,
|
|
|
|
// e.g., by referencing the original declaration or by saying *why* this
|
|
|
|
// reference is outside the range.
|
|
|
|
|
|
|
|
// Generate a report for this bug.
|
|
|
|
RangedBugReport *report =
|
2009-11-14 20:08:24 +08:00
|
|
|
new RangedBugReport(*BT, BT->getDescription(), N);
|
2009-11-11 20:33:27 +08:00
|
|
|
|
|
|
|
report->addRange(S->getSourceRange());
|
|
|
|
C.EmitReport(report);
|
2009-11-24 07:23:26 +08:00
|
|
|
return;
|
2009-11-11 20:33:27 +08:00
|
|
|
}
|
2009-11-24 07:23:26 +08:00
|
|
|
|
|
|
|
// Array bound check succeeded. From this point forward the array bound
|
|
|
|
// should always succeed.
|
|
|
|
assert(StInBound);
|
|
|
|
C.addTransition(StInBound);
|
2009-11-11 20:33:27 +08:00
|
|
|
}
|