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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-24 16:42:12 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-24 16:42:12 +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"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2009-11-11 20:33:27 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-11 20:33:27 +08:00
|
|
|
|
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class ArrayBoundChecker :
|
2011-03-01 09:16:21 +08:00
|
|
|
public Checker<check::Location> {
|
2012-02-05 10:12:40 +08:00
|
|
|
mutable OwningPtr<BuiltinBug> BT;
|
2009-11-11 20:33:27 +08:00
|
|
|
public:
|
2011-10-06 08:43:15 +08:00
|
|
|
void checkLocation(SVal l, bool isLoad, const Stmt* S,
|
|
|
|
CheckerContext &C) const;
|
2009-11-11 20:33:27 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-10-06 08:43:15 +08:00
|
|
|
void ArrayBoundChecker::checkLocation(SVal l, bool isLoad, const Stmt* LoadS,
|
2011-02-24 16:42:12 +08:00
|
|
|
CheckerContext &C) const {
|
2009-11-11 20:33:27 +08:00
|
|
|
// 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;
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2009-11-11 20:33:27 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef StInBound = state->assumeInBound(Idx, NumElements, true);
|
|
|
|
ProgramStateRef StOutBound = state->assumeInBound(Idx, NumElements, false);
|
2009-11-11 20:33:27 +08:00
|
|
|
if (StOutBound && !StInBound) {
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink(StOutBound);
|
2009-11-11 20:33:27 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT)
|
2011-02-24 16:42:12 +08:00
|
|
|
BT.reset(new BuiltinBug("Out-of-bound array access",
|
|
|
|
"Access out-of-bound array element (buffer overflow)"));
|
2009-11-11 20:33:27 +08:00
|
|
|
|
|
|
|
// 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.
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *report =
|
|
|
|
new BugReport(*BT, BT->getDescription(), N);
|
2009-11-11 20:33:27 +08:00
|
|
|
|
2011-10-06 08:43:15 +08:00
|
|
|
report->addRange(LoadS->getSourceRange());
|
2009-11-11 20:33:27 +08:00
|
|
|
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.
|
2011-10-27 05:06:34 +08:00
|
|
|
C.addTransition(StInBound);
|
2009-11-11 20:33:27 +08:00
|
|
|
}
|
2011-02-24 16:42:12 +08:00
|
|
|
|
|
|
|
void ento::registerArrayBoundChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ArrayBoundChecker>();
|
|
|
|
}
|