2008-02-01 03:34:24 +08: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-10-17 13:57:07 +08:00
|
|
|
// This file defines SVal, Loc, and NonLoc, classes that represent
|
2008-02-01 03:34:24 +08:00
|
|
|
// abstract r-values for use with path-sensitive value tracking.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
2011-01-25 08:04:03 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2008-02-01 03:34:24 +08:00
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2008-02-01 03:34:24 +08:00
|
|
|
using llvm::APSInt;
|
|
|
|
|
2008-02-15 07:25:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-03-26 11:35:11 +08:00
|
|
|
// Symbol iteration within an SVal.
|
2008-02-15 07:25:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-01 03:34:24 +08:00
|
|
|
|
2009-03-26 11:35:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-02-07 06:50:25 +08:00
|
|
|
|
2009-05-12 18:10:00 +08:00
|
|
|
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-08-28 12:49:15 +08:00
|
|
|
const FunctionDecl *SVal::getAsFunctionDecl() const {
|
2009-04-20 13:24:46 +08:00
|
|
|
if (const loc::MemRegionVal* X = dyn_cast<loc::MemRegionVal>(this)) {
|
|
|
|
const MemRegion* R = X->getRegion();
|
2009-11-25 09:32:22 +08:00
|
|
|
if (const FunctionTextRegion *CTR = R->getAs<FunctionTextRegion>())
|
2009-08-28 12:49:15 +08:00
|
|
|
return CTR->getDecl();
|
2009-04-20 13:24:46 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 03:58:05 +08:00
|
|
|
return 0;
|
2009-04-20 13:24:46 +08:00
|
|
|
}
|
|
|
|
|
2012-03-17 07:24:16 +08:00
|
|
|
/// \brief If this SVal is a location (subclasses Loc) and wraps a symbol,
|
|
|
|
/// return that SymbolRef. Otherwise return 0.
|
|
|
|
///
|
|
|
|
/// Implicit casts (ex: void* -> char*) can turn Symbolic region into Element
|
|
|
|
/// region. If that is the case, gets the underlining region.
|
2009-03-04 06:06:47 +08:00
|
|
|
SymbolRef SVal::getAsLocSymbol() const {
|
2012-03-17 07:24:16 +08:00
|
|
|
// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
|
2010-08-21 19:00:26 +08:00
|
|
|
if (const nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(this))
|
|
|
|
return X->getLoc().getAsLocSymbol();
|
|
|
|
|
2009-03-04 06:06:47 +08:00
|
|
|
if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) {
|
2011-02-17 05:13:32 +08:00
|
|
|
const MemRegion *R = X->stripCasts();
|
2009-07-30 05:43:22 +08:00
|
|
|
if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
|
|
|
|
return SymR->getSymbol();
|
2009-03-04 06:06:47 +08:00
|
|
|
}
|
2011-11-17 03:58:05 +08:00
|
|
|
return 0;
|
2009-03-04 06:06:47 +08:00
|
|
|
}
|
|
|
|
|
2010-03-10 12:58:55 +08:00
|
|
|
/// Get the symbol in the SVal or its base region.
|
|
|
|
SymbolRef SVal::getLocSymbolInBase() const {
|
|
|
|
const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this);
|
|
|
|
|
|
|
|
if (!X)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const MemRegion *R = X->getRegion();
|
|
|
|
|
|
|
|
while (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
|
|
|
|
if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SR))
|
|
|
|
return SymR->getSymbol();
|
|
|
|
else
|
|
|
|
R = SR->getSuperRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-07 07:12:27 +08:00
|
|
|
// TODO: The next 3 functions have to be simplified.
|
2012-03-17 07:24:16 +08:00
|
|
|
|
|
|
|
/// \brief If this SVal wraps a symbol return that SymbolRef.
|
2009-04-20 13:24:46 +08:00
|
|
|
/// Otherwise return 0.
|
2009-03-04 06:06:47 +08:00
|
|
|
SymbolRef SVal::getAsSymbol() const {
|
2012-03-17 07:24:16 +08:00
|
|
|
// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
|
2009-03-04 06:06:47 +08:00
|
|
|
if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
|
|
|
|
return X->getSymbol();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 06:06:47 +08:00
|
|
|
return getAsLocSymbol();
|
|
|
|
}
|
|
|
|
|
2009-03-26 11:35:11 +08:00
|
|
|
/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
|
|
|
|
/// return that expression. Otherwise return NULL.
|
|
|
|
const SymExpr *SVal::getAsSymbolicExpression() const {
|
2011-12-06 02:58:30 +08:00
|
|
|
if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
|
|
|
|
return X->getSymbol();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-26 11:35:11 +08:00
|
|
|
return getAsSymbol();
|
|
|
|
}
|
2009-06-30 19:52:40 +08:00
|
|
|
|
2011-11-18 06:57:01 +08:00
|
|
|
const SymExpr* SVal::getAsSymExpr() const {
|
|
|
|
const SymExpr* Sym = getAsSymbol();
|
|
|
|
if (!Sym)
|
|
|
|
Sym = getAsSymbolicExpression();
|
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
|
2009-06-30 19:52:40 +08:00
|
|
|
const MemRegion *SVal::getAsRegion() const {
|
|
|
|
if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this))
|
|
|
|
return X->getRegion();
|
|
|
|
|
2010-01-11 14:52:53 +08:00
|
|
|
if (const nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(this)) {
|
|
|
|
return X->getLoc().getAsRegion();
|
|
|
|
}
|
|
|
|
|
2009-06-30 19:52:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-03-26 11:35:11 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
const MemRegion *loc::MemRegionVal::stripCasts() const {
|
2009-07-30 02:14:27 +08:00
|
|
|
const MemRegion *R = getRegion();
|
2009-11-10 10:17:20 +08:00
|
|
|
return R ? R->StripCasts() : NULL;
|
2009-07-30 02:14:27 +08:00
|
|
|
}
|
|
|
|
|
2010-02-05 10:26:30 +08:00
|
|
|
const void *nonloc::LazyCompoundVal::getStore() const {
|
|
|
|
return static_cast<const LazyCompoundValData*>(Data)->getStore();
|
2009-08-06 09:20:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const TypedRegion *nonloc::LazyCompoundVal::getRegion() const {
|
|
|
|
return static_cast<const LazyCompoundValData*>(Data)->getRegion();
|
|
|
|
}
|
|
|
|
|
2008-10-31 02:01:28 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Other Iterators.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
|
|
|
|
return getValue()->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
|
|
|
|
return getValue()->end();
|
|
|
|
}
|
|
|
|
|
2008-07-18 23:54:51 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Useful predicates.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-09 14:52:44 +08:00
|
|
|
bool SVal::isConstant() const {
|
|
|
|
return isa<nonloc::ConcreteInt>(this) || isa<loc::ConcreteInt>(this);
|
|
|
|
}
|
|
|
|
|
2010-07-07 05:43:29 +08:00
|
|
|
bool SVal::isConstant(int I) const {
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<loc::ConcreteInt>(*this))
|
2010-07-07 05:43:29 +08:00
|
|
|
return cast<loc::ConcreteInt>(*this).getValue() == I;
|
2008-10-17 13:57:07 +08:00
|
|
|
else if (isa<nonloc::ConcreteInt>(*this))
|
2010-07-07 05:43:29 +08:00
|
|
|
return cast<nonloc::ConcreteInt>(*this).getValue() == I;
|
2008-07-18 23:54:51 +08:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-07 05:43:29 +08:00
|
|
|
bool SVal::isZeroConstant() const {
|
|
|
|
return isConstant(0);
|
|
|
|
}
|
|
|
|
|
2008-07-18 23:54:51 +08:00
|
|
|
|
2008-02-07 06:50:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-10-17 13:57:07 +08:00
|
|
|
// Transfer function dispatch for Non-Locs.
|
2008-02-07 06:50:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal nonloc::ConcreteInt::evalBinOp(SValBuilder &svalBuilder,
|
2009-06-26 08:05:51 +08:00
|
|
|
BinaryOperator::Opcode Op,
|
2009-09-09 23:08:12 +08:00
|
|
|
const nonloc::ConcreteInt& R) const {
|
2008-07-18 23:59:33 +08:00
|
|
|
const llvm::APSInt* X =
|
2010-12-02 15:49:45 +08:00
|
|
|
svalBuilder.getBasicValueFactory().evalAPSInt(Op, getValue(), R.getValue());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
if (X)
|
2008-10-17 13:57:07 +08:00
|
|
|
return nonloc::ConcreteInt(*X);
|
2008-02-29 04:32:03 +08:00
|
|
|
else
|
|
|
|
return UndefinedVal();
|
2008-02-07 06:50:25 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
nonloc::ConcreteInt
|
2010-12-02 15:49:45 +08:00
|
|
|
nonloc::ConcreteInt::evalComplement(SValBuilder &svalBuilder) const {
|
|
|
|
return svalBuilder.makeIntVal(~getValue());
|
2008-02-07 06:50:25 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
nonloc::ConcreteInt
|
|
|
|
nonloc::ConcreteInt::evalMinus(SValBuilder &svalBuilder) const {
|
|
|
|
return svalBuilder.makeIntVal(-getValue());
|
2008-02-05 00:58:30 +08:00
|
|
|
}
|
|
|
|
|
2008-02-07 06:50:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-10-17 13:57:07 +08:00
|
|
|
// Transfer function dispatch for Locs.
|
2008-02-07 06:50:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-02-05 00:58:30 +08:00
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
SVal loc::ConcreteInt::evalBinOp(BasicValueFactory& BasicVals,
|
2008-10-31 01:53:23 +08:00
|
|
|
BinaryOperator::Opcode Op,
|
|
|
|
const loc::ConcreteInt& R) const {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
assert (Op == BO_Add || Op == BO_Sub ||
|
|
|
|
(Op >= BO_LT && Op <= BO_NE));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
const llvm::APSInt* X = BasicVals.evalAPSInt(Op, getValue(), R.getValue());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
if (X)
|
2008-10-17 13:57:07 +08:00
|
|
|
return loc::ConcreteInt(*X);
|
2008-02-29 04:32:03 +08:00
|
|
|
else
|
|
|
|
return UndefinedVal();
|
2008-02-01 03:34:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pretty-Printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-14 07:53:06 +08:00
|
|
|
void SVal::dump() const { dumpToStream(llvm::errs()); }
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void SVal::dumpToStream(raw_ostream &os) const {
|
2009-09-09 23:08:12 +08:00
|
|
|
switch (getBaseKind()) {
|
2008-10-24 14:00:12 +08:00
|
|
|
case UnknownKind:
|
2010-09-15 11:13:30 +08:00
|
|
|
os << "Unknown";
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
case NonLocKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
cast<NonLoc>(this)->dumpToStream(os);
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
case LocKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
cast<Loc>(this)->dumpToStream(os);
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
case UndefinedKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
os << "Undefined";
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void NonLoc::dumpToStream(raw_ostream &os) const {
|
2009-09-09 23:08:12 +08:00
|
|
|
switch (getSubKind()) {
|
2010-12-23 10:42:31 +08:00
|
|
|
case nonloc::ConcreteIntKind: {
|
|
|
|
const nonloc::ConcreteInt& C = *cast<nonloc::ConcreteInt>(this);
|
2010-12-23 10:42:46 +08:00
|
|
|
if (C.getValue().isUnsigned())
|
|
|
|
os << C.getValue().getZExtValue();
|
|
|
|
else
|
|
|
|
os << C.getValue().getSExtValue();
|
|
|
|
os << ' ' << (C.getValue().isUnsigned() ? 'U' : 'S')
|
2010-12-23 10:42:31 +08:00
|
|
|
<< C.getValue().getBitWidth() << 'b';
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2010-12-23 10:42:31 +08:00
|
|
|
}
|
2011-12-06 02:58:30 +08:00
|
|
|
case nonloc::SymbolValKind: {
|
|
|
|
os << cast<nonloc::SymbolVal>(this)->getSymbol();
|
2008-10-24 14:00:12 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-10-24 14:00:12 +08:00
|
|
|
case nonloc::LocAsIntegerKind: {
|
|
|
|
const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
|
2009-07-14 07:53:06 +08:00
|
|
|
os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
|
2008-10-24 14:00:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-10-31 02:01:28 +08:00
|
|
|
case nonloc::CompoundValKind: {
|
|
|
|
const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
|
2009-07-15 04:21:36 +08:00
|
|
|
os << "compoundVal{";
|
2008-10-31 02:35:10 +08:00
|
|
|
bool first = true;
|
|
|
|
for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (first) {
|
2009-07-14 07:53:06 +08:00
|
|
|
os << ' '; first = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
os << ", ";
|
|
|
|
|
|
|
|
(*I).dumpToStream(os);
|
2008-10-31 02:35:10 +08:00
|
|
|
}
|
2009-07-15 04:21:36 +08:00
|
|
|
os << "}";
|
2008-10-31 02:01:28 +08:00
|
|
|
break;
|
2009-08-06 09:20:57 +08:00
|
|
|
}
|
|
|
|
case nonloc::LazyCompoundValKind: {
|
|
|
|
const nonloc::LazyCompoundVal &C = *cast<nonloc::LazyCompoundVal>(this);
|
2010-04-20 00:39:44 +08:00
|
|
|
os << "lazyCompoundVal{" << const_cast<void *>(C.getStore())
|
|
|
|
<< ',' << C.getRegion()
|
2009-08-06 09:20:57 +08:00
|
|
|
<< '}';
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-10-24 14:00:12 +08:00
|
|
|
default:
|
|
|
|
assert (false && "Pretty-printed not implemented for this NonLoc.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
void Loc::dumpToStream(raw_ostream &os) const {
|
2009-09-09 23:08:12 +08:00
|
|
|
switch (getSubKind()) {
|
2008-10-24 14:00:12 +08:00
|
|
|
case loc::ConcreteIntKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
os << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() << " (Loc)";
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
case loc::GotoLabelKind:
|
2011-02-17 15:39:24 +08:00
|
|
|
os << "&&" << cast<loc::GotoLabel>(this)->getLabel()->getName();
|
2008-10-24 14:00:12 +08:00
|
|
|
break;
|
|
|
|
case loc::MemRegionKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
os << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
default:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Pretty-printing not implemented for this Loc.");
|
2008-10-24 14:00:12 +08:00
|
|
|
}
|
|
|
|
}
|