2010-12-02 05:28:31 +08:00
|
|
|
// SValBuilder.cpp - Basic class for all SValBuilder implementations -*- C++ -*-
|
2009-07-22 05:03:30 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-12-02 05:28:31 +08:00
|
|
|
// This file defines SValBuilder, the base class for all (complete) SValBuilder
|
2009-07-22 05:03:30 +08:00
|
|
|
// implementations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/GRState.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
|
2009-07-22 05:03:30 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-07-22 05:03:30 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Basic SVal creation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedOrUnknownSVal SValBuilder::makeZeroVal(QualType type) {
|
|
|
|
if (Loc::isLocType(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeNull();
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (type->isIntegerType())
|
|
|
|
return makeIntVal(0, type);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
// FIXME: Handle floats.
|
|
|
|
// FIXME: Handle structs.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
|
2011-03-01 08:45:32 +08:00
|
|
|
const llvm::APSInt& rhs, QualType type) {
|
2010-12-02 15:49:45 +08:00
|
|
|
// The Environment ensures we always get a persistent APSInt in
|
|
|
|
// BasicValueFactory, so we don't need to get the APSInt from
|
|
|
|
// BasicValueFactory again.
|
2011-03-01 08:45:32 +08:00
|
|
|
assert(!Loc::isLocType(type));
|
|
|
|
return nonloc::SymExprVal(SymMgr.getSymIntExpr(lhs, op, rhs, type));
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
|
2011-03-01 08:45:32 +08:00
|
|
|
const SymExpr *rhs, QualType type) {
|
2010-12-02 15:49:45 +08:00
|
|
|
assert(SymMgr.getType(lhs) == SymMgr.getType(rhs));
|
2011-03-01 08:45:32 +08:00
|
|
|
assert(!Loc::isLocType(type));
|
|
|
|
return nonloc::SymExprVal(SymMgr.getSymSymExpr(lhs, op, rhs, type));
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SVal SValBuilder::convertToArrayIndex(SVal val) {
|
|
|
|
if (val.isUnknownOrUndef())
|
|
|
|
return val;
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
// Common case: we have an appropriately sized integer.
|
2011-03-01 08:45:32 +08:00
|
|
|
if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&val)) {
|
2010-12-02 15:49:45 +08:00
|
|
|
const llvm::APSInt& I = CI->getValue();
|
|
|
|
if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
|
2011-03-01 08:45:32 +08:00
|
|
|
return val;
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromNonLoc(cast<NonLoc>(val), ArrayIndexTy);
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DefinedOrUnknownSVal
|
2011-08-13 04:02:48 +08:00
|
|
|
SValBuilder::getRegionValueSymbolVal(const TypedValueRegion* region) {
|
2011-03-01 08:45:32 +08:00
|
|
|
QualType T = region->getValueType();
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
if (!SymbolManager::canSymbolicate(T))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SymbolRef sym = SymMgr.getRegionValueSymbol(region);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
if (Loc::isLocType(T))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedOrUnknownSVal SValBuilder::getConjuredSymbolVal(const void *symbolTag,
|
|
|
|
const Expr *expr,
|
|
|
|
unsigned count) {
|
|
|
|
QualType T = expr->getType();
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
if (!SymbolManager::canSymbolicate(T))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SymbolRef sym = SymMgr.getConjuredSymbol(expr, count, symbolTag);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
if (Loc::isLocType(T))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedOrUnknownSVal SValBuilder::getConjuredSymbolVal(const void *symbolTag,
|
|
|
|
const Expr *expr,
|
|
|
|
QualType type,
|
|
|
|
unsigned count) {
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (!SymbolManager::canSymbolicate(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return UnknownVal();
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SymbolRef sym = SymMgr.getConjuredSymbol(expr, type, count, symbolTag);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (Loc::isLocType(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag,
|
|
|
|
const MemRegion *region,
|
|
|
|
const Expr *expr, QualType type,
|
|
|
|
unsigned count) {
|
|
|
|
assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type");
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SymbolRef sym =
|
|
|
|
SymMgr.getMetadataSymbol(region, expr, type, count, symbolTag);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (Loc::isLocType(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
DefinedOrUnknownSVal
|
|
|
|
SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
|
2011-08-13 04:02:48 +08:00
|
|
|
const TypedValueRegion *region) {
|
2011-03-01 08:45:32 +08:00
|
|
|
QualType T = region->getValueType();
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
if (!SymbolManager::canSymbolicate(T))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
if (Loc::isLocType(T))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) {
|
2011-03-01 08:45:32 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getFunctionTextRegion(func));
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block,
|
|
|
|
CanQualType locTy,
|
|
|
|
const LocationContext *locContext) {
|
2010-12-02 15:49:45 +08:00
|
|
|
const BlockTextRegion *BC =
|
2011-03-01 08:45:32 +08:00
|
|
|
MemMgr.getBlockTextRegion(block, locTy, locContext->getAnalysisContext());
|
|
|
|
const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, locContext);
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(BD);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-08-26 02:44:25 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SVal SValBuilder::evalBinOp(const GRState *state, BinaryOperator::Opcode op,
|
|
|
|
SVal lhs, SVal rhs, QualType type) {
|
2009-08-26 02:44:25 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (lhs.isUndef() || rhs.isUndef())
|
2009-08-26 02:44:25 +08:00
|
|
|
return UndefinedVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (lhs.isUnknown() || rhs.isUnknown())
|
2009-08-26 02:44:25 +08:00
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (isa<Loc>(lhs)) {
|
|
|
|
if (isa<Loc>(rhs))
|
|
|
|
return evalBinOpLL(state, op, cast<Loc>(lhs), cast<Loc>(rhs), type);
|
2009-08-26 02:44:25 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalBinOpLN(state, op, cast<Loc>(lhs), cast<NonLoc>(rhs), type);
|
2009-08-26 02:44:25 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (isa<Loc>(rhs)) {
|
2010-06-28 16:26:15 +08:00
|
|
|
// Support pointer arithmetic where the addend is on the left
|
|
|
|
// and the pointer on the right.
|
2011-03-01 08:45:32 +08:00
|
|
|
assert(op == BO_Add);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-26 02:44:25 +08:00
|
|
|
// Commute the operands.
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalBinOpLN(state, op, cast<Loc>(rhs), cast<NonLoc>(lhs), type);
|
2009-08-26 02:44:25 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalBinOpNN(state, op, cast<NonLoc>(lhs), cast<NonLoc>(rhs), type);
|
2009-08-26 02:44:25 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedOrUnknownSVal SValBuilder::evalEQ(const GRState *state,
|
|
|
|
DefinedOrUnknownSVal lhs,
|
|
|
|
DefinedOrUnknownSVal rhs) {
|
|
|
|
return cast<DefinedOrUnknownSVal>(evalBinOp(state, BO_EQ, lhs, rhs,
|
2010-12-02 15:49:45 +08:00
|
|
|
Context.IntTy));
|
2009-09-12 06:07:28 +08:00
|
|
|
}
|
|
|
|
|
2010-11-26 15:15:40 +08:00
|
|
|
// FIXME: should rewrite according to the cast kind.
|
2010-12-02 05:57:22 +08:00
|
|
|
SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) {
|
2009-07-22 05:03:30 +08:00
|
|
|
if (val.isUnknownOrUndef() || castTy == originalTy)
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// For const casts, just propagate the value.
|
2010-01-05 17:27:03 +08:00
|
|
|
if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
|
2010-12-02 15:49:45 +08:00
|
|
|
if (Context.hasSameUnqualifiedType(castTy, originalTy))
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-03 05:11:40 +08:00
|
|
|
// Check for casts to real or complex numbers. We don't handle these at all
|
|
|
|
// right now.
|
|
|
|
if (castTy->isFloatingType() || castTy->isAnyComplexType())
|
2010-02-04 12:56:43 +08:00
|
|
|
return UnknownVal();
|
2010-02-03 05:11:40 +08:00
|
|
|
|
|
|
|
// Check for casts from integers to integers.
|
2010-01-14 11:45:06 +08:00
|
|
|
if (castTy->isIntegerType() && originalTy->isIntegerType())
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromNonLoc(cast<NonLoc>(val), castTy);
|
2010-01-14 11:45:06 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from pointers to integers.
|
2011-02-17 05:13:32 +08:00
|
|
|
if (castTy->isIntegerType() && Loc::isLocType(originalTy))
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromLoc(cast<Loc>(val), castTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from integers to pointers.
|
2011-02-17 05:13:32 +08:00
|
|
|
if (Loc::isLocType(castTy) && originalTy->isIntegerType()) {
|
2009-07-22 05:03:30 +08:00
|
|
|
if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) {
|
2009-12-23 10:52:14 +08:00
|
|
|
if (const MemRegion *R = LV->getLoc().getAsRegion()) {
|
2010-12-02 15:49:45 +08:00
|
|
|
StoreManager &storeMgr = StateMgr.getStoreManager();
|
2011-02-12 03:48:15 +08:00
|
|
|
R = storeMgr.castRegion(R, castTy);
|
2010-02-04 12:56:43 +08:00
|
|
|
return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
|
2009-12-23 10:52:14 +08:00
|
|
|
}
|
2010-02-04 12:56:43 +08:00
|
|
|
return LV->getLoc();
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
|
|
|
goto DispatchCast;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Just pass through function and block pointers.
|
|
|
|
if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
|
2011-02-17 05:13:32 +08:00
|
|
|
assert(Loc::isLocType(castTy));
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from array type to another type.
|
|
|
|
if (originalTy->isArrayType()) {
|
|
|
|
// We will always decay to a pointer.
|
2010-12-02 15:49:45 +08:00
|
|
|
val = StateMgr.ArrayToPointer(cast<Loc>(val));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Are we casting from an array to a pointer? If so just pass on
|
|
|
|
// the decayed value.
|
|
|
|
if (castTy->isPointerType())
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Are we casting from an array to an integer? If so, cast the decayed
|
|
|
|
// pointer value to an integer.
|
|
|
|
assert(castTy->isIntegerType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// FIXME: Keep these here for now in case we decide soon that we
|
|
|
|
// need the original decayed type.
|
|
|
|
// QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
|
|
|
|
// QualType pointerTy = C.getPointerType(elemTy);
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromLoc(cast<Loc>(val), castTy);
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from a region to a specific type.
|
|
|
|
if (const MemRegion *R = val.getAsRegion()) {
|
|
|
|
// FIXME: We should handle the case where we strip off view layers to get
|
|
|
|
// to a desugared type.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
if (!Loc::isLocType(castTy)) {
|
2010-11-16 04:09:42 +08:00
|
|
|
// FIXME: There can be gross cases where one casts the result of a function
|
|
|
|
// (that returns a pointer) to some other value that happens to fit
|
|
|
|
// within that pointer value. We currently have no good way to
|
|
|
|
// model such operations. When this happens, the underlying operation
|
|
|
|
// is that the caller is reasoning about bits. Conceptually we are
|
|
|
|
// layering a "view" of a location on top of those bits. Perhaps
|
|
|
|
// we need to be more lazy about mutual possible views, even on an
|
|
|
|
// SVal? This may be necessary for bit-level reasoning as well.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// We get a symbolic function pointer for a dereference of a function
|
|
|
|
// pointer, but it is of function type. Example:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// struct FPRec {
|
2009-09-09 23:08:12 +08:00
|
|
|
// void (*my_func)(int * x);
|
2009-07-22 05:03:30 +08:00
|
|
|
// };
|
|
|
|
//
|
|
|
|
// int bar(int x);
|
|
|
|
//
|
|
|
|
// int f1_a(struct FPRec* foo) {
|
|
|
|
// int x;
|
|
|
|
// (*foo->my_func)(&x);
|
|
|
|
// return bar(x)+1; // no-warning
|
|
|
|
// }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() ||
|
2011-02-19 16:03:18 +08:00
|
|
|
originalTy->isBlockPointerType() || castTy->isReferenceType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
StoreManager &storeMgr = StateMgr.getStoreManager();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-14 14:55:01 +08:00
|
|
|
// Delegate to store manager to get the result of casting a region to a
|
|
|
|
// different type. If the MemRegion* returned is NULL, this expression
|
2010-12-02 05:57:22 +08:00
|
|
|
// Evaluates to UnknownVal.
|
2011-02-12 03:48:15 +08:00
|
|
|
R = storeMgr.castRegion(R, castTy);
|
2010-02-04 12:56:43 +08:00
|
|
|
return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
DispatchCast:
|
2009-12-23 10:52:14 +08:00
|
|
|
// All other cases.
|
2011-03-01 08:45:32 +08:00
|
|
|
return isa<Loc>(val) ? evalCastFromLoc(cast<Loc>(val), castTy)
|
|
|
|
: evalCastFromNonLoc(cast<NonLoc>(val), castTy);
|
2009-09-12 06:07:28 +08:00
|
|
|
}
|