2010-12-23 10:42:43 +08:00
|
|
|
//== ArrayBoundCheckerV2.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 ArrayBoundCheckerV2, which is a path-sensitive check
|
|
|
|
// which looks for an out-of-bound array element access.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-28 09:26:57 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:26:57 +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/PathSensitive/ExprEngine.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-12-23 10:42:43 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-12-23 10:42:43 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ArrayBoundCheckerV2 :
|
2011-03-01 09:16:21 +08:00
|
|
|
public Checker<check::Location> {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BuiltinBug> BT;
|
|
|
|
|
2012-01-21 13:07:33 +08:00
|
|
|
enum OOB_Kind { OOB_Precedes, OOB_Excedes, OOB_Tainted };
|
2010-12-23 10:42:43 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
void reportOOB(CheckerContext &C, ProgramStateRef errorState,
|
2011-02-28 09:26:57 +08:00
|
|
|
OOB_Kind kind) const;
|
2010-12-23 10:42:43 +08:00
|
|
|
|
|
|
|
public:
|
2011-10-06 08:43:15 +08:00
|
|
|
void checkLocation(SVal l, bool isLoad, const Stmt*S,
|
|
|
|
CheckerContext &C) const;
|
2010-12-23 10:42:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: Eventually replace RegionRawOffset with this class.
|
|
|
|
class RegionRawOffsetV2 {
|
|
|
|
private:
|
|
|
|
const SubRegion *baseRegion;
|
|
|
|
SVal byteOffset;
|
2014-05-27 10:45:47 +08:00
|
|
|
|
2010-12-23 10:42:43 +08:00
|
|
|
RegionRawOffsetV2()
|
2014-05-27 10:45:47 +08:00
|
|
|
: baseRegion(nullptr), byteOffset(UnknownVal()) {}
|
2010-12-23 10:42:43 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
RegionRawOffsetV2(const SubRegion* base, SVal offset)
|
|
|
|
: baseRegion(base), byteOffset(offset) {}
|
|
|
|
|
2013-02-20 13:52:05 +08:00
|
|
|
NonLoc getByteOffset() const { return byteOffset.castAs<NonLoc>(); }
|
2010-12-23 10:42:43 +08:00
|
|
|
const SubRegion *getRegion() const { return baseRegion; }
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
static RegionRawOffsetV2 computeOffset(ProgramStateRef state,
|
2010-12-23 10:42:43 +08:00
|
|
|
SValBuilder &svalBuilder,
|
|
|
|
SVal location);
|
|
|
|
|
|
|
|
void dump() const;
|
2011-08-13 07:37:29 +08:00
|
|
|
void dumpToStream(raw_ostream &os) const;
|
2010-12-23 10:42:43 +08:00
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2010-12-23 10:42:43 +08:00
|
|
|
|
2011-04-13 01:21:33 +08:00
|
|
|
static SVal computeExtentBegin(SValBuilder &svalBuilder,
|
|
|
|
const MemRegion *region) {
|
|
|
|
while (true)
|
|
|
|
switch (region->getKind()) {
|
|
|
|
default:
|
|
|
|
return svalBuilder.makeZeroArrayIndex();
|
|
|
|
case MemRegion::SymbolicRegionKind:
|
|
|
|
// FIXME: improve this later by tracking symbolic lower bounds
|
|
|
|
// for symbolic regions.
|
|
|
|
return UnknownVal();
|
|
|
|
case MemRegion::ElementRegionKind:
|
|
|
|
region = cast<SubRegion>(region)->getSuperRegion();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:26:57 +08:00
|
|
|
void ArrayBoundCheckerV2::checkLocation(SVal location, bool isLoad,
|
2011-10-06 08:43:15 +08:00
|
|
|
const Stmt* LoadS,
|
2011-02-28 09:26:57 +08:00
|
|
|
CheckerContext &checkerContext) const {
|
2010-12-23 10:42:43 +08:00
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
// NOTE: Instead of using ProgramState::assumeInBound(), we are prototyping
|
2010-12-23 10:42:43 +08:00
|
|
|
// some new logic here that reasons directly about memory region extents.
|
|
|
|
// Once that logic is more mature, we can bring it back to assumeInBound()
|
|
|
|
// for all clients to use.
|
|
|
|
//
|
|
|
|
// The algorithm we are using here for bounds checking is to see if the
|
|
|
|
// memory access is within the extent of the base region. Since we
|
|
|
|
// have some flexibility in defining the base region, we can achieve
|
|
|
|
// various levels of conservatism in our buffer overflow checking.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = checkerContext.getState();
|
|
|
|
ProgramStateRef originalState = state;
|
2010-12-23 10:42:43 +08:00
|
|
|
|
|
|
|
SValBuilder &svalBuilder = checkerContext.getSValBuilder();
|
|
|
|
const RegionRawOffsetV2 &rawOffset =
|
|
|
|
RegionRawOffsetV2::computeOffset(state, svalBuilder, location);
|
|
|
|
|
|
|
|
if (!rawOffset.getRegion())
|
|
|
|
return;
|
|
|
|
|
2011-04-13 01:21:33 +08:00
|
|
|
// CHECK LOWER BOUND: Is byteOffset < extent begin?
|
|
|
|
// If so, we are doing a load/store
|
2010-12-23 10:42:43 +08:00
|
|
|
// before the first valid offset in the memory region.
|
|
|
|
|
2011-04-13 01:21:33 +08:00
|
|
|
SVal extentBegin = computeExtentBegin(svalBuilder, rawOffset.getRegion());
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<NonLoc> NV = extentBegin.getAs<NonLoc>()) {
|
2013-02-20 13:52:05 +08:00
|
|
|
SVal lowerBound =
|
|
|
|
svalBuilder.evalBinOpNN(state, BO_LT, rawOffset.getByteOffset(), *NV,
|
2011-04-13 01:21:33 +08:00
|
|
|
svalBuilder.getConditionType());
|
2010-12-23 10:42:43 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<NonLoc> lowerBoundToCheck = lowerBound.getAs<NonLoc>();
|
2011-04-13 01:21:33 +08:00
|
|
|
if (!lowerBoundToCheck)
|
|
|
|
return;
|
2010-12-23 10:42:43 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state_precedesLowerBound, state_withinLowerBound;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(state_precedesLowerBound, state_withinLowerBound) =
|
2010-12-23 10:42:43 +08:00
|
|
|
state->assume(*lowerBoundToCheck);
|
|
|
|
|
2011-04-13 01:21:33 +08:00
|
|
|
// Are we constrained enough to definitely precede the lower bound?
|
|
|
|
if (state_precedesLowerBound && !state_withinLowerBound) {
|
|
|
|
reportOOB(checkerContext, state_precedesLowerBound, OOB_Precedes);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-23 10:42:43 +08:00
|
|
|
|
2011-04-13 01:21:33 +08:00
|
|
|
// Otherwise, assume the constraint of the lower bound.
|
|
|
|
assert(state_withinLowerBound);
|
|
|
|
state = state_withinLowerBound;
|
|
|
|
}
|
2010-12-23 10:42:43 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
// CHECK UPPER BOUND: Is byteOffset >= extent(baseRegion)? If so,
|
|
|
|
// we are doing a load/store after the last valid offset.
|
|
|
|
DefinedOrUnknownSVal extentVal =
|
|
|
|
rawOffset.getRegion()->getExtent(svalBuilder);
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!extentVal.getAs<NonLoc>())
|
2010-12-23 10:42:43 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
SVal upperbound
|
|
|
|
= svalBuilder.evalBinOpNN(state, BO_GE, rawOffset.getByteOffset(),
|
2013-02-20 13:52:05 +08:00
|
|
|
extentVal.castAs<NonLoc>(),
|
2010-12-23 10:42:43 +08:00
|
|
|
svalBuilder.getConditionType());
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<NonLoc> upperboundToCheck = upperbound.getAs<NonLoc>();
|
2010-12-23 10:42:43 +08:00
|
|
|
if (!upperboundToCheck)
|
|
|
|
break;
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state_exceedsUpperBound, state_withinUpperBound;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(state_exceedsUpperBound, state_withinUpperBound) =
|
2010-12-23 10:42:43 +08:00
|
|
|
state->assume(*upperboundToCheck);
|
2011-11-17 03:58:17 +08:00
|
|
|
|
|
|
|
// If we are under constrained and the index variables are tainted, report.
|
|
|
|
if (state_exceedsUpperBound && state_withinUpperBound) {
|
|
|
|
if (state->isTainted(rawOffset.getByteOffset()))
|
2012-01-21 13:07:33 +08:00
|
|
|
reportOOB(checkerContext, state_exceedsUpperBound, OOB_Tainted);
|
2011-11-17 03:58:17 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-12-23 10:42:43 +08:00
|
|
|
|
2011-11-17 03:58:17 +08:00
|
|
|
// If we are constrained enough to definitely exceed the upper bound, report.
|
|
|
|
if (state_exceedsUpperBound) {
|
|
|
|
assert(!state_withinUpperBound);
|
2010-12-23 10:42:43 +08:00
|
|
|
reportOOB(checkerContext, state_exceedsUpperBound, OOB_Excedes);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(state_withinUpperBound);
|
|
|
|
state = state_withinUpperBound;
|
|
|
|
}
|
|
|
|
while (false);
|
|
|
|
|
|
|
|
if (state != originalState)
|
2011-10-27 05:06:34 +08:00
|
|
|
checkerContext.addTransition(state);
|
2010-12-23 10:42:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayBoundCheckerV2::reportOOB(CheckerContext &checkerContext,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef errorState,
|
2011-02-28 09:26:57 +08:00
|
|
|
OOB_Kind kind) const {
|
2010-12-23 10:42:43 +08:00
|
|
|
|
|
|
|
ExplodedNode *errorNode = checkerContext.generateSink(errorState);
|
|
|
|
if (!errorNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT.reset(new BuiltinBug(this, "Out-of-bound access"));
|
2010-12-23 10:42:43 +08:00
|
|
|
|
|
|
|
// FIXME: This diagnostics are preliminary. We should get far better
|
|
|
|
// diagnostics for explaining buffer overruns.
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> buf;
|
2010-12-23 10:42:43 +08:00
|
|
|
llvm::raw_svector_ostream os(buf);
|
2012-01-21 13:07:33 +08:00
|
|
|
os << "Out of bound memory access ";
|
|
|
|
switch (kind) {
|
|
|
|
case OOB_Precedes:
|
|
|
|
os << "(accessed memory precedes memory block)";
|
|
|
|
break;
|
|
|
|
case OOB_Excedes:
|
|
|
|
os << "(access exceeds upper limit of memory block)";
|
|
|
|
break;
|
|
|
|
case OOB_Tainted:
|
|
|
|
os << "(index is tainted)";
|
|
|
|
break;
|
|
|
|
}
|
2010-12-23 10:42:43 +08:00
|
|
|
|
2015-06-23 21:15:32 +08:00
|
|
|
checkerContext.emitReport(
|
|
|
|
llvm::make_unique<BugReport>(*BT, os.str(), errorNode));
|
2010-12-23 10:42:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegionRawOffsetV2::dump() const {
|
|
|
|
dumpToStream(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void RegionRawOffsetV2::dumpToStream(raw_ostream &os) const {
|
2010-12-23 10:42:43 +08:00
|
|
|
os << "raw_offset_v2{" << getRegion() << ',' << getByteOffset() << '}';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Lazily computes a value to be used by 'computeOffset'. If 'val'
|
|
|
|
// is unknown or undefined, we lazily substitute '0'. Otherwise,
|
|
|
|
// return 'val'.
|
|
|
|
static inline SVal getValue(SVal val, SValBuilder &svalBuilder) {
|
2013-02-20 13:52:05 +08:00
|
|
|
return val.getAs<UndefinedVal>() ? svalBuilder.makeArrayIndex(0) : val;
|
2010-12-23 10:42:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scale a base value by a scaling factor, and return the scaled
|
|
|
|
// value as an SVal. Used by 'computeOffset'.
|
2012-01-27 05:29:00 +08:00
|
|
|
static inline SVal scaleValue(ProgramStateRef state,
|
2010-12-23 10:42:43 +08:00
|
|
|
NonLoc baseVal, CharUnits scaling,
|
|
|
|
SValBuilder &sb) {
|
|
|
|
return sb.evalBinOpNN(state, BO_Mul, baseVal,
|
|
|
|
sb.makeArrayIndex(scaling.getQuantity()),
|
|
|
|
sb.getArrayIndexType());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add an SVal to another, treating unknown and undefined values as
|
|
|
|
// summing to UnknownVal. Used by 'computeOffset'.
|
2012-01-27 05:29:00 +08:00
|
|
|
static SVal addValue(ProgramStateRef state, SVal x, SVal y,
|
2010-12-23 10:42:43 +08:00
|
|
|
SValBuilder &svalBuilder) {
|
|
|
|
// We treat UnknownVals and UndefinedVals the same here because we
|
|
|
|
// only care about computing offsets.
|
|
|
|
if (x.isUnknownOrUndef() || y.isUnknownOrUndef())
|
|
|
|
return UnknownVal();
|
2013-02-20 13:52:05 +08:00
|
|
|
|
|
|
|
return svalBuilder.evalBinOpNN(state, BO_Add, x.castAs<NonLoc>(),
|
|
|
|
y.castAs<NonLoc>(),
|
2010-12-23 10:42:43 +08:00
|
|
|
svalBuilder.getArrayIndexType());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compute a raw byte offset from a base region. Used for array bounds
|
|
|
|
/// checking.
|
2012-01-27 05:29:00 +08:00
|
|
|
RegionRawOffsetV2 RegionRawOffsetV2::computeOffset(ProgramStateRef state,
|
2010-12-23 10:42:43 +08:00
|
|
|
SValBuilder &svalBuilder,
|
|
|
|
SVal location)
|
|
|
|
{
|
|
|
|
const MemRegion *region = location.getAsRegion();
|
|
|
|
SVal offset = UndefinedVal();
|
|
|
|
|
|
|
|
while (region) {
|
|
|
|
switch (region->getKind()) {
|
|
|
|
default: {
|
2011-04-13 01:21:33 +08:00
|
|
|
if (const SubRegion *subReg = dyn_cast<SubRegion>(region)) {
|
|
|
|
offset = getValue(offset, svalBuilder);
|
2010-12-23 10:42:43 +08:00
|
|
|
if (!offset.isUnknownOrUndef())
|
|
|
|
return RegionRawOffsetV2(subReg, offset);
|
2011-04-13 01:21:33 +08:00
|
|
|
}
|
2010-12-23 10:42:43 +08:00
|
|
|
return RegionRawOffsetV2();
|
|
|
|
}
|
|
|
|
case MemRegion::ElementRegionKind: {
|
|
|
|
const ElementRegion *elemReg = cast<ElementRegion>(region);
|
|
|
|
SVal index = elemReg->getIndex();
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!index.getAs<NonLoc>())
|
2010-12-23 10:42:43 +08:00
|
|
|
return RegionRawOffsetV2();
|
|
|
|
QualType elemType = elemReg->getElementType();
|
|
|
|
// If the element is an incomplete type, go no further.
|
|
|
|
ASTContext &astContext = svalBuilder.getContext();
|
2014-10-04 05:49:03 +08:00
|
|
|
if (elemType->isIncompleteType())
|
2010-12-23 10:42:43 +08:00
|
|
|
return RegionRawOffsetV2();
|
|
|
|
|
|
|
|
// Update the offset.
|
|
|
|
offset = addValue(state,
|
|
|
|
getValue(offset, svalBuilder),
|
|
|
|
scaleValue(state,
|
2013-02-20 13:52:05 +08:00
|
|
|
index.castAs<NonLoc>(),
|
2011-11-17 03:58:17 +08:00
|
|
|
astContext.getTypeSizeInChars(elemType),
|
|
|
|
svalBuilder),
|
2010-12-23 10:42:43 +08:00
|
|
|
svalBuilder);
|
|
|
|
|
|
|
|
if (offset.isUnknownOrUndef())
|
|
|
|
return RegionRawOffsetV2();
|
|
|
|
|
|
|
|
region = elemReg->getSuperRegion();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RegionRawOffsetV2();
|
|
|
|
}
|
|
|
|
|
2011-02-28 09:26:57 +08:00
|
|
|
void ento::registerArrayBoundCheckerV2(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ArrayBoundCheckerV2>();
|
|
|
|
}
|