2009-02-15 01:08:39 +08:00
|
|
|
//== RangeConstraintManager.cpp - Manage range constraints.------*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
// This file defines RangeConstraintManager, a class that tracks simple
|
2011-08-16 06:09:50 +08:00
|
|
|
// equality and inequality constraints on symbolic values of ProgramState.
|
2009-02-15 01:08:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SimpleConstraintManager.h"
|
2012-05-08 11:27:16 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
|
2009-02-15 01:08:39 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/ADT/ImmutableSet.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2009-11-28 14:07:30 +08:00
|
|
|
namespace { class ConstraintRange {}; }
|
2009-02-18 03:28:04 +08:00
|
|
|
static int ConstraintRangeIndex = 0;
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
/// A Range represents the closed range [from, to]. The caller must
|
|
|
|
/// guarantee that from <= to. Note that Range is immutable, so as not
|
|
|
|
/// to subvert RangeSet's immutability.
|
2009-02-18 13:22:01 +08:00
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class Range : public std::pair<const llvm::APSInt*,
|
2009-02-18 13:22:01 +08:00
|
|
|
const llvm::APSInt*> {
|
2009-02-15 01:08:39 +08:00
|
|
|
public:
|
|
|
|
Range(const llvm::APSInt &from, const llvm::APSInt &to)
|
2009-02-18 03:28:04 +08:00
|
|
|
: std::pair<const llvm::APSInt*, const llvm::APSInt*>(&from, &to) {
|
2009-02-15 01:08:39 +08:00
|
|
|
assert(from <= to);
|
|
|
|
}
|
|
|
|
bool Includes(const llvm::APSInt &v) const {
|
2009-02-18 03:28:04 +08:00
|
|
|
return *first <= v && v <= *second;
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
|
|
|
const llvm::APSInt &From() const {
|
2009-02-18 03:28:04 +08:00
|
|
|
return *first;
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
|
|
|
const llvm::APSInt &To() const {
|
2009-02-18 03:28:04 +08:00
|
|
|
return *second;
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
2009-02-18 03:28:04 +08:00
|
|
|
const llvm::APSInt *getConcreteValue() const {
|
|
|
|
return &From() == &To() ? &From() : NULL;
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const {
|
2009-02-18 03:28:04 +08:00
|
|
|
ID.AddPointer(&From());
|
|
|
|
ID.AddPointer(&To());
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-02-18 13:22:01 +08:00
|
|
|
|
2009-11-28 14:07:30 +08:00
|
|
|
class RangeTrait : public llvm::ImutContainerInfo<Range> {
|
2009-02-18 13:22:01 +08:00
|
|
|
public:
|
|
|
|
// When comparing if one Range is less than another, we should compare
|
2009-02-19 01:42:44 +08:00
|
|
|
// the actual APSInt values instead of their pointers. This keeps the order
|
|
|
|
// consistent (instead of comparing by pointer values) and can potentially
|
|
|
|
// be used to speed up some of the operations in RangeSet.
|
2009-02-18 13:22:01 +08:00
|
|
|
static inline bool isLess(key_type_ref lhs, key_type_ref rhs) {
|
2009-09-09 23:08:12 +08:00
|
|
|
return *lhs.first < *rhs.first || (!(*rhs.first < *lhs.first) &&
|
2009-02-18 13:22:01 +08:00
|
|
|
*lhs.second < *rhs.second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
/// RangeSet contains a set of ranges. If the set is empty, then
|
|
|
|
/// there the value of a symbol is overly constrained and there are no
|
|
|
|
/// possible values for that symbol.
|
2009-11-28 14:07:30 +08:00
|
|
|
class RangeSet {
|
2009-02-18 13:22:01 +08:00
|
|
|
typedef llvm::ImmutableSet<Range, RangeTrait> PrimRangeSet;
|
2009-02-15 01:08:39 +08:00
|
|
|
PrimRangeSet ranges; // no need to make const, since it is an
|
|
|
|
// ImmutableSet - this allows default operator=
|
2009-09-09 23:08:12 +08:00
|
|
|
// to work.
|
2009-02-15 01:08:39 +08:00
|
|
|
public:
|
2009-02-18 03:28:04 +08:00
|
|
|
typedef PrimRangeSet::Factory Factory;
|
|
|
|
typedef PrimRangeSet::iterator iterator;
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
RangeSet(PrimRangeSet RS) : ranges(RS) {}
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
iterator begin() const { return ranges.begin(); }
|
|
|
|
iterator end() const { return ranges.end(); }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
bool isEmpty() const { return ranges.isEmpty(); }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
/// Construct a new RangeSet representing '{ [from, to] }'.
|
|
|
|
RangeSet(Factory &F, const llvm::APSInt &from, const llvm::APSInt &to)
|
2010-11-24 08:54:37 +08:00
|
|
|
: ranges(F.add(F.getEmptySet(), Range(from, to))) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
/// Profile - Generates a hash profile of this RangeSet for use
|
|
|
|
/// by FoldingSet.
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) const { ranges.Profile(ID); }
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
/// getConcreteValue - If a symbol is contrained to equal a specific integer
|
|
|
|
/// constant then this method returns that value. Otherwise, it returns
|
|
|
|
/// NULL.
|
|
|
|
const llvm::APSInt* getConcreteValue() const {
|
|
|
|
return ranges.isSingleton() ? ranges.begin()->getConcreteValue() : 0;
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
|
|
|
|
2010-06-19 06:49:11 +08:00
|
|
|
private:
|
|
|
|
void IntersectInRange(BasicValueFactory &BV, Factory &F,
|
|
|
|
const llvm::APSInt &Lower,
|
|
|
|
const llvm::APSInt &Upper,
|
|
|
|
PrimRangeSet &newRanges,
|
|
|
|
PrimRangeSet::iterator &i,
|
|
|
|
PrimRangeSet::iterator &e) const {
|
|
|
|
// There are six cases for each range R in the set:
|
|
|
|
// 1. R is entirely before the intersection range.
|
|
|
|
// 2. R is entirely after the intersection range.
|
|
|
|
// 3. R contains the entire intersection range.
|
|
|
|
// 4. R starts before the intersection range and ends in the middle.
|
|
|
|
// 5. R starts in the middle of the intersection range and ends after it.
|
|
|
|
// 6. R is entirely contained in the intersection range.
|
|
|
|
// These correspond to each of the conditions below.
|
|
|
|
for (/* i = begin(), e = end() */; i != e; ++i) {
|
|
|
|
if (i->To() < Lower) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i->From() > Upper) {
|
2009-02-18 03:28:04 +08:00
|
|
|
break;
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
|
|
|
|
2010-06-19 06:49:11 +08:00
|
|
|
if (i->Includes(Lower)) {
|
|
|
|
if (i->Includes(Upper)) {
|
2010-11-24 08:54:37 +08:00
|
|
|
newRanges = F.add(newRanges, Range(BV.getValue(Lower),
|
2010-06-19 06:49:11 +08:00
|
|
|
BV.getValue(Upper)));
|
|
|
|
break;
|
|
|
|
} else
|
2010-11-24 08:54:37 +08:00
|
|
|
newRanges = F.add(newRanges, Range(BV.getValue(Lower), i->To()));
|
2010-06-19 06:49:11 +08:00
|
|
|
} else {
|
|
|
|
if (i->Includes(Upper)) {
|
2010-11-24 08:54:37 +08:00
|
|
|
newRanges = F.add(newRanges, Range(i->From(), BV.getValue(Upper)));
|
2010-06-19 06:49:11 +08:00
|
|
|
break;
|
|
|
|
} else
|
2010-11-24 08:54:37 +08:00
|
|
|
newRanges = F.add(newRanges, *i);
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:27:16 +08:00
|
|
|
const llvm::APSInt &getMinValue() const {
|
|
|
|
assert(!isEmpty());
|
|
|
|
return ranges.begin()->From();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pin(llvm::APSInt &Lower, llvm::APSInt &Upper) const {
|
|
|
|
// This function has nine cases, the cartesian product of range-testing
|
|
|
|
// both the upper and lower bounds against the symbol's type.
|
|
|
|
// Each case requires a different pinning operation.
|
|
|
|
// The function returns false if the described range is entirely outside
|
|
|
|
// the range of values for the associated symbol.
|
|
|
|
APSIntType Type(getMinValue());
|
|
|
|
APSIntType::RangeTestResultKind LowerTest = Type.testInRange(Lower);
|
|
|
|
APSIntType::RangeTestResultKind UpperTest = Type.testInRange(Upper);
|
|
|
|
|
|
|
|
switch (LowerTest) {
|
|
|
|
case APSIntType::RTR_Below:
|
|
|
|
switch (UpperTest) {
|
|
|
|
case APSIntType::RTR_Below:
|
|
|
|
// The entire range is outside the symbol's set of possible values.
|
|
|
|
// If this is a conventionally-ordered range, the state is infeasible.
|
|
|
|
if (Lower < Upper)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// However, if the range wraps around, it spans all possible values.
|
|
|
|
Lower = Type.getMinValue();
|
|
|
|
Upper = Type.getMaxValue();
|
|
|
|
break;
|
|
|
|
case APSIntType::RTR_Within:
|
|
|
|
// The range starts below what's possible but ends within it. Pin.
|
|
|
|
Lower = Type.getMinValue();
|
|
|
|
Type.apply(Upper);
|
|
|
|
break;
|
|
|
|
case APSIntType::RTR_Above:
|
|
|
|
// The range spans all possible values for the symbol. Pin.
|
|
|
|
Lower = Type.getMinValue();
|
|
|
|
Upper = Type.getMaxValue();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case APSIntType::RTR_Within:
|
|
|
|
switch (UpperTest) {
|
|
|
|
case APSIntType::RTR_Below:
|
|
|
|
// The range wraps around, but all lower values are not possible.
|
|
|
|
Type.apply(Lower);
|
|
|
|
Upper = Type.getMaxValue();
|
|
|
|
break;
|
|
|
|
case APSIntType::RTR_Within:
|
|
|
|
// The range may or may not wrap around, but both limits are valid.
|
|
|
|
Type.apply(Lower);
|
|
|
|
Type.apply(Upper);
|
|
|
|
break;
|
|
|
|
case APSIntType::RTR_Above:
|
|
|
|
// The range starts within what's possible but ends above it. Pin.
|
|
|
|
Type.apply(Lower);
|
|
|
|
Upper = Type.getMaxValue();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case APSIntType::RTR_Above:
|
|
|
|
switch (UpperTest) {
|
|
|
|
case APSIntType::RTR_Below:
|
|
|
|
// The range wraps but is outside the symbol's set of possible values.
|
|
|
|
return false;
|
|
|
|
case APSIntType::RTR_Within:
|
|
|
|
// The range starts above what's possible but ends within it (wrap).
|
|
|
|
Lower = Type.getMinValue();
|
|
|
|
Type.apply(Upper);
|
|
|
|
break;
|
|
|
|
case APSIntType::RTR_Above:
|
|
|
|
// The entire range is outside the symbol's set of possible values.
|
|
|
|
// If this is a conventionally-ordered range, the state is infeasible.
|
|
|
|
if (Lower < Upper)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// However, if the range wraps around, it spans all possible values.
|
|
|
|
Lower = Type.getMinValue();
|
|
|
|
Upper = Type.getMaxValue();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-19 06:49:11 +08:00
|
|
|
public:
|
|
|
|
// Returns a set containing the values in the receiving set, intersected with
|
|
|
|
// the closed range [Lower, Upper]. Unlike the Range type, this range uses
|
|
|
|
// modular arithmetic, corresponding to the common treatment of C integer
|
|
|
|
// overflow. Thus, if the Lower bound is greater than the Upper bound, the
|
|
|
|
// range is taken to wrap around. This is equivalent to taking the
|
|
|
|
// intersection with the two ranges [Min, Upper] and [Lower, Max],
|
|
|
|
// or, alternatively, /removing/ all integers between Upper and Lower.
|
|
|
|
RangeSet Intersect(BasicValueFactory &BV, Factory &F,
|
2012-05-08 11:27:16 +08:00
|
|
|
llvm::APSInt Lower, llvm::APSInt Upper) const {
|
|
|
|
if (!pin(Lower, Upper))
|
|
|
|
return F.getEmptySet();
|
|
|
|
|
2010-11-24 08:54:37 +08:00
|
|
|
PrimRangeSet newRanges = F.getEmptySet();
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2010-06-19 06:49:11 +08:00
|
|
|
PrimRangeSet::iterator i = begin(), e = end();
|
|
|
|
if (Lower <= Upper)
|
|
|
|
IntersectInRange(BV, F, Lower, Upper, newRanges, i, e);
|
|
|
|
else {
|
|
|
|
// The order of the next two statements is important!
|
|
|
|
// IntersectInRange() does not reset the iteration state for i and e.
|
|
|
|
// Therefore, the lower range most be handled first.
|
|
|
|
IntersectInRange(BV, F, BV.getMinValue(Upper), Upper, newRanges, i, e);
|
|
|
|
IntersectInRange(BV, F, Lower, BV.getMaxValue(Lower), newRanges, i, e);
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
2012-05-08 11:27:16 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
return newRanges;
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void print(raw_ostream &os) const {
|
2009-02-18 03:28:04 +08:00
|
|
|
bool isFirst = true;
|
2009-02-15 01:08:39 +08:00
|
|
|
os << "{ ";
|
2009-02-18 03:28:04 +08:00
|
|
|
for (iterator i = begin(), e = end(); i != e; ++i) {
|
|
|
|
if (isFirst)
|
|
|
|
isFirst = false;
|
|
|
|
else
|
2009-02-15 01:08:39 +08:00
|
|
|
os << ", ";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-15 01:08:39 +08:00
|
|
|
os << '[' << i->From().toString(10) << ", " << i->To().toString(10)
|
|
|
|
<< ']';
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
os << " }";
|
2009-02-18 03:28:04 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-15 01:08:39 +08:00
|
|
|
bool operator==(const RangeSet &other) const {
|
|
|
|
return ranges == other.ranges;
|
|
|
|
}
|
|
|
|
};
|
2009-02-18 13:22:01 +08:00
|
|
|
} // end anonymous namespace
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
typedef llvm::ImmutableMap<SymbolRef,RangeSet> ConstraintRangeTy;
|
2009-02-15 01:08:39 +08:00
|
|
|
|
|
|
|
namespace clang {
|
2010-12-23 15:20:52 +08:00
|
|
|
namespace ento {
|
2009-02-15 01:08:39 +08:00
|
|
|
template<>
|
2011-08-16 06:09:50 +08:00
|
|
|
struct ProgramStateTrait<ConstraintRange>
|
|
|
|
: public ProgramStatePartialTrait<ConstraintRangeTy> {
|
2011-08-13 07:37:29 +08:00
|
|
|
static inline void *GDMIndex() { return &ConstraintRangeIndex; }
|
2009-02-15 01:08:39 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2010-12-23 02:53:20 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-15 01:08:39 +08:00
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class RangeConstraintManager : public SimpleConstraintManager{
|
2012-01-27 05:29:00 +08:00
|
|
|
RangeSet GetRange(ProgramStateRef state, SymbolRef sym);
|
2009-02-15 01:08:39 +08:00
|
|
|
public:
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeConstraintManager(SubEngine &subengine, BasicValueFactory &BVF)
|
|
|
|
: SimpleConstraintManager(subengine, BVF) {}
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef assumeSymNE(ProgramStateRef state, SymbolRef sym,
|
2010-06-19 06:49:11 +08:00
|
|
|
const llvm::APSInt& Int,
|
|
|
|
const llvm::APSInt& Adjustment);
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef assumeSymEQ(ProgramStateRef state, SymbolRef sym,
|
2010-06-19 06:49:11 +08:00
|
|
|
const llvm::APSInt& Int,
|
|
|
|
const llvm::APSInt& Adjustment);
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef assumeSymLT(ProgramStateRef state, SymbolRef sym,
|
2010-06-19 06:49:11 +08:00
|
|
|
const llvm::APSInt& Int,
|
|
|
|
const llvm::APSInt& Adjustment);
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef assumeSymGT(ProgramStateRef state, SymbolRef sym,
|
2010-06-19 06:49:11 +08:00
|
|
|
const llvm::APSInt& Int,
|
|
|
|
const llvm::APSInt& Adjustment);
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef assumeSymGE(ProgramStateRef state, SymbolRef sym,
|
2010-06-19 06:49:11 +08:00
|
|
|
const llvm::APSInt& Int,
|
|
|
|
const llvm::APSInt& Adjustment);
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef assumeSymLE(ProgramStateRef state, SymbolRef sym,
|
2010-06-19 06:49:11 +08:00
|
|
|
const llvm::APSInt& Int,
|
|
|
|
const llvm::APSInt& Adjustment);
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
const llvm::APSInt* getSymVal(ProgramStateRef St, SymbolRef sym) const;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
// FIXME: Refactor into SimpleConstraintManager?
|
2012-01-27 05:29:00 +08:00
|
|
|
bool isEqual(ProgramStateRef St, SymbolRef sym, const llvm::APSInt& V) const {
|
2009-02-18 03:28:04 +08:00
|
|
|
const llvm::APSInt *i = getSymVal(St, sym);
|
|
|
|
return i ? *i == V : false;
|
|
|
|
}
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef removeDeadBindings(ProgramStateRef St, SymbolReaper& SymReaper);
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
void print(ProgramStateRef St, raw_ostream &Out,
|
2009-02-15 01:08:39 +08:00
|
|
|
const char* nl, const char *sep);
|
|
|
|
|
|
|
|
private:
|
2009-02-18 03:28:04 +08:00
|
|
|
RangeSet::Factory F;
|
2009-02-15 01:08:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2012-05-08 11:27:16 +08:00
|
|
|
ConstraintManager *
|
|
|
|
ento::CreateRangeConstraintManager(ProgramStateManager &StMgr, SubEngine &Eng) {
|
|
|
|
return new RangeConstraintManager(Eng, StMgr.getBasicVals());
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
const llvm::APSInt* RangeConstraintManager::getSymVal(ProgramStateRef St,
|
2009-02-15 01:08:39 +08:00
|
|
|
SymbolRef sym) const {
|
2009-02-18 03:28:04 +08:00
|
|
|
const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(sym);
|
|
|
|
return T ? T->getConcreteValue() : NULL;
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Scan all symbols referenced by the constraints. If the symbol is not alive
|
|
|
|
/// as marked in LSymbols, mark it as dead in DSymbols.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef
|
|
|
|
RangeConstraintManager::removeDeadBindings(ProgramStateRef state,
|
2009-02-15 01:08:39 +08:00
|
|
|
SymbolReaper& SymReaper) {
|
|
|
|
|
2009-06-18 06:28:13 +08:00
|
|
|
ConstraintRangeTy CR = state->get<ConstraintRange>();
|
|
|
|
ConstraintRangeTy::Factory& CRFactory = state->get_context<ConstraintRange>();
|
2009-02-15 01:08:39 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) {
|
2009-09-09 23:08:12 +08:00
|
|
|
SymbolRef sym = I.getKey();
|
2009-02-15 01:08:39 +08:00
|
|
|
if (SymReaper.maybeDead(sym))
|
2010-11-24 08:54:37 +08:00
|
|
|
CR = CRFactory.remove(CR, sym);
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-18 06:28:13 +08:00
|
|
|
return state->set<ConstraintRange>(CR);
|
2009-02-18 03:28:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RangeSet
|
2012-01-27 05:29:00 +08:00
|
|
|
RangeConstraintManager::GetRange(ProgramStateRef state, SymbolRef sym) {
|
2009-06-18 06:28:13 +08:00
|
|
|
if (ConstraintRangeTy::data_type* V = state->get<ConstraintRange>(sym))
|
2009-02-18 03:28:04 +08:00
|
|
|
return *V;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
// Lazily generate a new RangeSet representing all possible values for the
|
|
|
|
// given symbol type.
|
2012-05-08 11:27:16 +08:00
|
|
|
BasicValueFactory &BV = getBasicVals();
|
|
|
|
QualType T = sym->getType(BV.getContext());
|
2009-02-18 03:28:04 +08:00
|
|
|
return RangeSet(F, BV.getMinValue(T), BV.getMaxValue(T));
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
//===------------------------------------------------------------------------===
|
2010-12-02 06:16:56 +08:00
|
|
|
// assumeSymX methods: public interface for RangeConstraintManager.
|
2009-02-18 03:28:04 +08:00
|
|
|
//===------------------------------------------------------------------------===/
|
|
|
|
|
2010-06-19 06:49:11 +08:00
|
|
|
// The syntax for ranges below is mathematical, using [x, y] for closed ranges
|
|
|
|
// and (x, y) for open ranges. These ranges are modular, corresponding with
|
|
|
|
// a common treatment of C integer overflow. This means that these methods
|
|
|
|
// do not have to worry about overflow; RangeSet::Intersect can handle such a
|
|
|
|
// "wraparound" range.
|
|
|
|
// As an example, the range [UINT_MAX-1, 3) contains five values: UINT_MAX-1,
|
|
|
|
// UINT_MAX, 0, 1, and 2.
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeConstraintManager::assumeSymNE(ProgramStateRef St, SymbolRef Sym,
|
|
|
|
const llvm::APSInt &Int,
|
|
|
|
const llvm::APSInt &Adjustment) {
|
|
|
|
// Before we do any real work, see if the value can even show up.
|
|
|
|
APSIntType AdjustmentType(Adjustment);
|
|
|
|
if (AdjustmentType.testInRange(Int) != APSIntType::RTR_Within)
|
|
|
|
return St;
|
|
|
|
|
|
|
|
llvm::APSInt Lower = AdjustmentType.convert(Int) - Adjustment;
|
2010-06-19 06:49:11 +08:00
|
|
|
llvm::APSInt Upper = Lower;
|
|
|
|
--Lower;
|
|
|
|
++Upper;
|
|
|
|
|
|
|
|
// [Int-Adjustment+1, Int-Adjustment-1]
|
|
|
|
// Notice that the lower bound is greater than the upper bound.
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Upper, Lower);
|
|
|
|
return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
|
2009-02-18 03:28:04 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeConstraintManager::assumeSymEQ(ProgramStateRef St, SymbolRef Sym,
|
|
|
|
const llvm::APSInt &Int,
|
|
|
|
const llvm::APSInt &Adjustment) {
|
|
|
|
// Before we do any real work, see if the value can even show up.
|
|
|
|
APSIntType AdjustmentType(Adjustment);
|
|
|
|
if (AdjustmentType.testInRange(Int) != APSIntType::RTR_Within)
|
|
|
|
return NULL;
|
|
|
|
|
2010-06-19 06:49:11 +08:00
|
|
|
// [Int-Adjustment, Int-Adjustment]
|
2012-05-08 11:27:16 +08:00
|
|
|
llvm::APSInt AdjInt = AdjustmentType.convert(Int) - Adjustment;
|
|
|
|
RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, AdjInt, AdjInt);
|
|
|
|
return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeConstraintManager::assumeSymLT(ProgramStateRef St, SymbolRef Sym,
|
|
|
|
const llvm::APSInt &Int,
|
|
|
|
const llvm::APSInt &Adjustment) {
|
|
|
|
// Before we do any real work, see if the value can even show up.
|
|
|
|
APSIntType AdjustmentType(Adjustment);
|
|
|
|
switch (AdjustmentType.testInRange(Int)) {
|
|
|
|
case APSIntType::RTR_Below:
|
|
|
|
return NULL;
|
|
|
|
case APSIntType::RTR_Within:
|
|
|
|
break;
|
|
|
|
case APSIntType::RTR_Above:
|
|
|
|
return St;
|
|
|
|
}
|
2010-06-19 06:49:11 +08:00
|
|
|
|
|
|
|
// Special case for Int == Min. This is always false.
|
2012-05-08 11:27:16 +08:00
|
|
|
llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
|
|
|
|
llvm::APSInt Min = AdjustmentType.getMinValue();
|
|
|
|
if (ComparisonVal == Min)
|
2010-06-19 06:49:11 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
llvm::APSInt Lower = Min-Adjustment;
|
2012-05-08 11:27:16 +08:00
|
|
|
llvm::APSInt Upper = ComparisonVal-Adjustment;
|
2010-06-19 06:49:11 +08:00
|
|
|
--Upper;
|
|
|
|
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
|
|
|
|
return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeConstraintManager::assumeSymGT(ProgramStateRef St, SymbolRef Sym,
|
|
|
|
const llvm::APSInt &Int,
|
|
|
|
const llvm::APSInt &Adjustment) {
|
|
|
|
// Before we do any real work, see if the value can even show up.
|
|
|
|
APSIntType AdjustmentType(Adjustment);
|
|
|
|
switch (AdjustmentType.testInRange(Int)) {
|
|
|
|
case APSIntType::RTR_Below:
|
|
|
|
return St;
|
|
|
|
case APSIntType::RTR_Within:
|
|
|
|
break;
|
|
|
|
case APSIntType::RTR_Above:
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-06-19 06:49:11 +08:00
|
|
|
|
|
|
|
// Special case for Int == Max. This is always false.
|
2012-05-08 11:27:16 +08:00
|
|
|
llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
|
|
|
|
llvm::APSInt Max = AdjustmentType.getMaxValue();
|
|
|
|
if (ComparisonVal == Max)
|
2010-06-19 06:49:11 +08:00
|
|
|
return NULL;
|
|
|
|
|
2012-05-08 11:27:16 +08:00
|
|
|
llvm::APSInt Lower = ComparisonVal-Adjustment;
|
2010-06-19 06:49:11 +08:00
|
|
|
llvm::APSInt Upper = Max-Adjustment;
|
|
|
|
++Lower;
|
|
|
|
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
|
|
|
|
return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeConstraintManager::assumeSymGE(ProgramStateRef St, SymbolRef Sym,
|
|
|
|
const llvm::APSInt &Int,
|
|
|
|
const llvm::APSInt &Adjustment) {
|
|
|
|
// Before we do any real work, see if the value can even show up.
|
|
|
|
APSIntType AdjustmentType(Adjustment);
|
|
|
|
switch (AdjustmentType.testInRange(Int)) {
|
|
|
|
case APSIntType::RTR_Below:
|
|
|
|
return St;
|
|
|
|
case APSIntType::RTR_Within:
|
|
|
|
break;
|
|
|
|
case APSIntType::RTR_Above:
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-06-19 06:49:11 +08:00
|
|
|
|
|
|
|
// Special case for Int == Min. This is always feasible.
|
2012-05-08 11:27:16 +08:00
|
|
|
llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
|
|
|
|
llvm::APSInt Min = AdjustmentType.getMinValue();
|
|
|
|
if (ComparisonVal == Min)
|
|
|
|
return St;
|
2010-06-19 06:49:11 +08:00
|
|
|
|
2012-05-08 11:27:16 +08:00
|
|
|
llvm::APSInt Max = AdjustmentType.getMaxValue();
|
|
|
|
llvm::APSInt Lower = ComparisonVal-Adjustment;
|
2010-06-19 06:49:11 +08:00
|
|
|
llvm::APSInt Upper = Max-Adjustment;
|
|
|
|
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
|
|
|
|
return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeConstraintManager::assumeSymLE(ProgramStateRef St, SymbolRef Sym,
|
|
|
|
const llvm::APSInt &Int,
|
|
|
|
const llvm::APSInt &Adjustment) {
|
|
|
|
// Before we do any real work, see if the value can even show up.
|
|
|
|
APSIntType AdjustmentType(Adjustment);
|
|
|
|
switch (AdjustmentType.testInRange(Int)) {
|
|
|
|
case APSIntType::RTR_Below:
|
|
|
|
return NULL;
|
|
|
|
case APSIntType::RTR_Within:
|
|
|
|
break;
|
|
|
|
case APSIntType::RTR_Above:
|
|
|
|
return St;
|
|
|
|
}
|
2010-06-19 06:49:11 +08:00
|
|
|
|
|
|
|
// Special case for Int == Max. This is always feasible.
|
2012-05-08 11:27:16 +08:00
|
|
|
llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
|
|
|
|
llvm::APSInt Max = AdjustmentType.getMaxValue();
|
|
|
|
if (ComparisonVal == Max)
|
|
|
|
return St;
|
2010-06-19 06:49:11 +08:00
|
|
|
|
2012-05-08 11:27:16 +08:00
|
|
|
llvm::APSInt Min = AdjustmentType.getMinValue();
|
2010-06-19 06:49:11 +08:00
|
|
|
llvm::APSInt Lower = Min-Adjustment;
|
2012-05-08 11:27:16 +08:00
|
|
|
llvm::APSInt Upper = ComparisonVal-Adjustment;
|
2010-06-19 06:49:11 +08:00
|
|
|
|
2012-05-08 11:27:16 +08:00
|
|
|
RangeSet New = GetRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
|
|
|
|
return New.isEmpty() ? NULL : St->set<ConstraintRange>(Sym, New);
|
2010-06-19 06:49:11 +08:00
|
|
|
}
|
2009-02-18 03:28:04 +08:00
|
|
|
|
|
|
|
//===------------------------------------------------------------------------===
|
|
|
|
// Pretty-printing.
|
|
|
|
//===------------------------------------------------------------------------===/
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
void RangeConstraintManager::print(ProgramStateRef St, raw_ostream &Out,
|
2009-02-15 01:08:39 +08:00
|
|
|
const char* nl, const char *sep) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-18 03:28:04 +08:00
|
|
|
ConstraintRangeTy Ranges = St->get<ConstraintRange>();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-06 02:58:14 +08:00
|
|
|
if (Ranges.isEmpty()) {
|
|
|
|
Out << nl << sep << "Ranges are empty." << nl;
|
2009-02-17 02:42:56 +08:00
|
|
|
return;
|
2011-12-06 02:58:14 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-06 02:58:14 +08:00
|
|
|
Out << nl << sep << "Ranges of symbol values:";
|
2009-02-18 03:28:04 +08:00
|
|
|
for (ConstraintRangeTy::iterator I=Ranges.begin(), E=Ranges.end(); I!=E; ++I){
|
2009-04-22 06:37:11 +08:00
|
|
|
Out << nl << ' ' << I.getKey() << " : ";
|
2009-06-25 07:06:47 +08:00
|
|
|
I.getData().print(Out);
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|
2011-12-06 02:58:14 +08:00
|
|
|
Out << nl;
|
2009-02-15 01:08:39 +08:00
|
|
|
}
|