opnsense-src/lib/Checker/SVals.cpp

328 lines
10 KiB
C++
Raw Normal View History

2009-06-02 13:58:47 -04:00
//= RValues.cpp - Abstract RValues for Path-Sens. Value Tracking -*- 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 SVal, Loc, and NonLoc, classes that represent
// abstract r-values for use with path-sensitive value tracking.
//
//===----------------------------------------------------------------------===//
2010-02-16 04:31:36 -05:00
#include "clang/Checker/PathSensitive/GRState.h"
2009-06-02 13:58:47 -04:00
#include "clang/Basic/IdentifierTable.h"
using namespace clang;
using llvm::dyn_cast;
using llvm::cast;
using llvm::APSInt;
//===----------------------------------------------------------------------===//
// Symbol iteration within an SVal.
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
// Utility methods.
//===----------------------------------------------------------------------===//
bool SVal::hasConjuredSymbol() const {
if (const nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(this)) {
SymbolRef sym = SV->getSymbol();
if (isa<SymbolConjured>(sym))
return true;
}
if (const loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(this)) {
const MemRegion *R = RV->getRegion();
if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
SymbolRef sym = SR->getSymbol();
if (isa<SymbolConjured>(sym))
return true;
}
}
return false;
}
2009-10-14 14:03:49 -04:00
const FunctionDecl *SVal::getAsFunctionDecl() const {
2009-06-02 13:58:47 -04:00
if (const loc::MemRegionVal* X = dyn_cast<loc::MemRegionVal>(this)) {
const MemRegion* R = X->getRegion();
2009-12-01 06:08:04 -05:00
if (const FunctionTextRegion *CTR = R->getAs<FunctionTextRegion>())
2009-10-14 14:03:49 -04:00
return CTR->getDecl();
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
return NULL;
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
/// getAsLocSymbol - If this SVal is a location (subclasses Loc) and
2009-06-02 13:58:47 -04:00
/// wraps a symbol, return that SymbolRef. Otherwise return 0.
// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
SymbolRef SVal::getAsLocSymbol() const {
if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) {
2009-11-18 09:59:57 -05:00
const MemRegion *R = X->StripCasts();
2009-10-14 14:03:49 -04:00
if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
return SymR->getSymbol();
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
return NULL;
2009-06-02 13:58:47 -04:00
}
/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
/// Otherwise return 0.
// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
SymbolRef SVal::getAsSymbol() const {
if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
return X->getSymbol();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
return Y;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
return getAsLocSymbol();
}
/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
/// return that expression. Otherwise return NULL.
const SymExpr *SVal::getAsSymbolicExpression() const {
if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
return X->getSymbolicExpression();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
return getAsSymbol();
}
2009-07-04 09:58:54 -04:00
const MemRegion *SVal::getAsRegion() const {
if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this))
return X->getRegion();
2010-01-15 10:39:40 -05:00
if (const nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(this)) {
return X->getLoc().getAsRegion();
}
2009-07-04 09:58:54 -04:00
return 0;
}
2009-06-02 13:58:47 -04:00
2009-11-18 09:59:57 -05:00
const MemRegion *loc::MemRegionVal::StripCasts() const {
2009-10-14 14:03:49 -04:00
const MemRegion *R = getRegion();
2009-11-18 09:59:57 -05:00
return R ? R->StripCasts() : NULL;
2009-10-14 14:03:49 -04:00
}
2009-06-02 13:58:47 -04:00
bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
return itr == X.itr;
}
bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
return itr != X.itr;
}
SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
itr.push_back(SE);
2009-10-14 14:03:49 -04:00
while (!isa<SymbolData>(itr.back())) expand();
2009-06-02 13:58:47 -04:00
}
SVal::symbol_iterator& SVal::symbol_iterator::operator++() {
assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
assert(isa<SymbolData>(itr.back()));
2009-10-14 14:03:49 -04:00
itr.pop_back();
2009-06-02 13:58:47 -04:00
if (!itr.empty())
while (!isa<SymbolData>(itr.back())) expand();
return *this;
}
SymbolRef SVal::symbol_iterator::operator*() {
assert(!itr.empty() && "attempting to dereference an 'end' iterator");
return cast<SymbolData>(itr.back());
}
void SVal::symbol_iterator::expand() {
const SymExpr *SE = itr.back();
itr.pop_back();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
itr.push_back(SIE->getLHS());
return;
2009-10-14 14:03:49 -04:00
}
2009-06-02 13:58:47 -04:00
else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
itr.push_back(SSE->getLHS());
itr.push_back(SSE->getRHS());
return;
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
assert(false && "unhandled expansion case");
}
2010-02-16 04:31:36 -05:00
const void *nonloc::LazyCompoundVal::getStore() const {
return static_cast<const LazyCompoundValData*>(Data)->getStore();
2009-10-14 14:03:49 -04:00
}
const TypedRegion *nonloc::LazyCompoundVal::getRegion() const {
return static_cast<const LazyCompoundValData*>(Data)->getRegion();
}
2009-06-02 13:58:47 -04:00
//===----------------------------------------------------------------------===//
// Other Iterators.
//===----------------------------------------------------------------------===//
nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
return getValue()->begin();
}
nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
return getValue()->end();
}
//===----------------------------------------------------------------------===//
// Useful predicates.
//===----------------------------------------------------------------------===//
2009-11-18 09:59:57 -05:00
bool SVal::isConstant() const {
return isa<nonloc::ConcreteInt>(this) || isa<loc::ConcreteInt>(this);
}
2009-06-02 13:58:47 -04:00
bool SVal::isZeroConstant() const {
if (isa<loc::ConcreteInt>(*this))
return cast<loc::ConcreteInt>(*this).getValue() == 0;
else if (isa<nonloc::ConcreteInt>(*this))
return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
else
return false;
}
//===----------------------------------------------------------------------===//
// Transfer function dispatch for Non-Locs.
//===----------------------------------------------------------------------===//
2009-06-27 06:45:02 -04:00
SVal nonloc::ConcreteInt::evalBinOp(ValueManager &ValMgr,
BinaryOperator::Opcode Op,
2009-10-14 14:03:49 -04:00
const nonloc::ConcreteInt& R) const {
2009-06-02 13:58:47 -04:00
const llvm::APSInt* X =
2009-06-27 06:45:02 -04:00
ValMgr.getBasicValueFactory().EvaluateAPSInt(Op, getValue(), R.getValue());
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (X)
return nonloc::ConcreteInt(*X);
else
return UndefinedVal();
}
nonloc::ConcreteInt
2009-06-27 06:45:02 -04:00
nonloc::ConcreteInt::evalComplement(ValueManager &ValMgr) const {
return ValMgr.makeIntVal(~getValue());
2009-06-02 13:58:47 -04:00
}
2009-06-27 06:45:02 -04:00
nonloc::ConcreteInt nonloc::ConcreteInt::evalMinus(ValueManager &ValMgr) const {
return ValMgr.makeIntVal(-getValue());
2009-06-02 13:58:47 -04:00
}
//===----------------------------------------------------------------------===//
// Transfer function dispatch for Locs.
//===----------------------------------------------------------------------===//
SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
BinaryOperator::Opcode Op,
const loc::ConcreteInt& R) const {
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
(Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (X)
return loc::ConcreteInt(*X);
else
return UndefinedVal();
}
//===----------------------------------------------------------------------===//
// Pretty-Printing.
//===----------------------------------------------------------------------===//
2009-10-14 14:03:49 -04:00
void SVal::dump() const { dumpToStream(llvm::errs()); }
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
void SVal::dumpToStream(llvm::raw_ostream& os) const {
2009-06-02 13:58:47 -04:00
switch (getBaseKind()) {
case UnknownKind:
2009-10-14 14:03:49 -04:00
os << "Invalid";
break;
2009-06-02 13:58:47 -04:00
case NonLocKind:
2009-10-14 14:03:49 -04:00
cast<NonLoc>(this)->dumpToStream(os);
break;
2009-06-02 13:58:47 -04:00
case LocKind:
2009-10-14 14:03:49 -04:00
cast<Loc>(this)->dumpToStream(os);
break;
2009-06-02 13:58:47 -04:00
case UndefinedKind:
2009-10-14 14:03:49 -04:00
os << "Undefined";
break;
2009-06-02 13:58:47 -04:00
default:
assert (false && "Invalid SVal.");
}
}
2009-10-14 14:03:49 -04:00
void NonLoc::dumpToStream(llvm::raw_ostream& os) const {
switch (getSubKind()) {
2009-06-02 13:58:47 -04:00
case nonloc::ConcreteIntKind:
2009-10-14 14:03:49 -04:00
os << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
2009-06-02 13:58:47 -04:00
if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
2009-10-14 14:03:49 -04:00
os << 'U';
2009-06-02 13:58:47 -04:00
break;
case nonloc::SymbolValKind:
2009-10-14 14:03:49 -04:00
os << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
2009-06-02 13:58:47 -04:00
break;
case nonloc::SymExprValKind: {
const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
const SymExpr *SE = C.getSymbolicExpression();
2009-10-14 14:03:49 -04:00
os << SE;
2009-06-02 13:58:47 -04:00
break;
}
case nonloc::LocAsIntegerKind: {
const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
2009-10-14 14:03:49 -04:00
os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
2009-06-02 13:58:47 -04:00
break;
}
case nonloc::CompoundValKind: {
const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
2009-10-14 14:03:49 -04:00
os << "compoundVal{";
2009-06-02 13:58:47 -04:00
bool first = true;
for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
2009-10-14 14:03:49 -04:00
if (first) {
os << ' '; first = false;
}
else
os << ", ";
(*I).dumpToStream(os);
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
os << "}";
break;
}
case nonloc::LazyCompoundValKind: {
const nonloc::LazyCompoundVal &C = *cast<nonloc::LazyCompoundVal>(this);
2010-02-16 04:31:36 -05:00
os << "lazyCompoundVal{" << (void*) C.getStore() << ',' << C.getRegion()
2009-10-14 14:03:49 -04:00
<< '}';
2009-06-02 13:58:47 -04:00
break;
}
default:
assert (false && "Pretty-printed not implemented for this NonLoc.");
break;
}
}
2009-10-14 14:03:49 -04:00
void Loc::dumpToStream(llvm::raw_ostream& os) const {
switch (getSubKind()) {
2009-06-02 13:58:47 -04:00
case loc::ConcreteIntKind:
2009-10-14 14:03:49 -04:00
os << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() << " (Loc)";
2009-06-02 13:58:47 -04:00
break;
case loc::GotoLabelKind:
2009-10-14 14:03:49 -04:00
os << "&&" << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
2009-06-02 13:58:47 -04:00
break;
case loc::MemRegionKind:
2009-10-14 14:03:49 -04:00
os << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
2009-06-02 13:58:47 -04:00
break;
default:
2009-10-14 14:03:49 -04:00
assert(false && "Pretty-printing not implemented for this Loc.");
2009-06-02 13:58:47 -04:00
break;
}
}