2010-12-02 05:28:31 +08:00
|
|
|
// SimpleSValBuilder.cpp - A basic SValBuilder -----------------------*- C++ -*-
|
2009-06-26 08:25:05 +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 SimpleSValBuilder, a basic implementation of SValBuilder.
|
2009-06-26 08:25:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-05-08 11:26:58 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
2009-06-26 08:25:05 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-06-26 08:25:05 +08:00
|
|
|
|
|
|
|
namespace {
|
2010-12-02 05:28:31 +08:00
|
|
|
class SimpleSValBuilder : public SValBuilder {
|
2009-07-22 05:03:30 +08:00
|
|
|
protected:
|
2011-12-07 07:12:27 +08:00
|
|
|
virtual SVal dispatchCast(SVal val, QualType castTy);
|
2011-03-01 08:45:32 +08:00
|
|
|
virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy);
|
|
|
|
virtual SVal evalCastFromLoc(Loc val, QualType castTy);
|
2009-07-22 05:03:30 +08:00
|
|
|
|
2009-06-26 08:25:05 +08:00
|
|
|
public:
|
2010-12-02 15:49:45 +08:00
|
|
|
SimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
|
2011-08-16 06:09:50 +08:00
|
|
|
ProgramStateManager &stateMgr)
|
2010-12-02 15:49:45 +08:00
|
|
|
: SValBuilder(alloc, context, stateMgr) {}
|
2010-12-02 05:28:31 +08:00
|
|
|
virtual ~SimpleSValBuilder() {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
virtual SVal evalMinus(NonLoc val);
|
|
|
|
virtual SVal evalComplement(NonLoc val);
|
2012-01-27 05:29:00 +08:00
|
|
|
virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op,
|
2009-10-06 09:39:48 +08:00
|
|
|
NonLoc lhs, NonLoc rhs, QualType resultTy);
|
2012-01-27 05:29:00 +08:00
|
|
|
virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op,
|
2010-06-28 16:26:15 +08:00
|
|
|
Loc lhs, Loc rhs, QualType resultTy);
|
2012-01-27 05:29:00 +08:00
|
|
|
virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op,
|
2009-06-26 08:25:05 +08:00
|
|
|
Loc lhs, NonLoc rhs, QualType resultTy);
|
2010-07-04 08:00:41 +08:00
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
/// getKnownValue - evaluates a given SVal. If the SVal has only one possible
|
2010-07-04 08:00:41 +08:00
|
|
|
/// (integer) value, that value is returned. Otherwise, returns NULL.
|
2012-01-27 05:29:00 +08:00
|
|
|
virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal V);
|
2010-06-20 12:56:29 +08:00
|
|
|
|
|
|
|
SVal MakeSymIntVal(const SymExpr *LHS, BinaryOperator::Opcode op,
|
|
|
|
const llvm::APSInt &RHS, QualType resultTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2009-06-26 08:25:05 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2010-12-23 15:20:52 +08:00
|
|
|
SValBuilder *ento::createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
|
|
|
|
ASTContext &context,
|
2011-08-16 06:09:50 +08:00
|
|
|
ProgramStateManager &stateMgr) {
|
2010-12-02 15:49:45 +08:00
|
|
|
return new SimpleSValBuilder(alloc, context, stateMgr);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function for Casts.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-12-16 05:33:26 +08:00
|
|
|
SVal SimpleSValBuilder::dispatchCast(SVal Val, QualType CastTy) {
|
|
|
|
assert(isa<Loc>(&Val) || isa<NonLoc>(&Val));
|
|
|
|
return isa<Loc>(Val) ? evalCastFromLoc(cast<Loc>(Val), CastTy)
|
|
|
|
: evalCastFromNonLoc(cast<NonLoc>(Val), CastTy);
|
2011-12-07 07:12:27 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SVal SimpleSValBuilder::evalCastFromNonLoc(NonLoc val, QualType castTy) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
bool isLocType = Loc::isLocType(castTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-21 08:12:07 +08:00
|
|
|
if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) {
|
|
|
|
if (isLocType)
|
2009-07-21 05:39:27 +08:00
|
|
|
return LI->getLoc();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-11 10:33:26 +08:00
|
|
|
// FIXME: Correctly support promotions/truncations.
|
2010-12-02 15:49:45 +08:00
|
|
|
unsigned castSize = Context.getTypeSize(castTy);
|
2010-01-11 10:33:26 +08:00
|
|
|
if (castSize == LI->getNumBits())
|
2009-07-21 08:12:07 +08:00
|
|
|
return val;
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeLocAsInteger(LI->getLoc(), castSize);
|
2009-07-21 08:12:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const SymExpr *se = val.getAsSymbolicExpression()) {
|
2010-12-02 15:49:45 +08:00
|
|
|
QualType T = Context.getCanonicalType(se->getType(Context));
|
2011-12-09 11:34:02 +08:00
|
|
|
// If types are the same or both are integers, ignore the cast.
|
2009-09-25 08:18:15 +08:00
|
|
|
// FIXME: Remove this hack when we support symbolic truncation/extension.
|
|
|
|
// HACK: If both castTy and T are integers, ignore the cast. This is
|
|
|
|
// not a permanent solution. Eventually we want to precisely handle
|
|
|
|
// extension/truncation of symbolic integers. This prevents us from losing
|
|
|
|
// precision when we assign 'x = y' and 'y' is symbolic and x and y are
|
|
|
|
// different integer types.
|
2011-12-09 11:34:02 +08:00
|
|
|
if (haveSameType(T, castTy))
|
2009-09-25 08:18:15 +08:00
|
|
|
return val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-07 07:12:27 +08:00
|
|
|
if (!isLocType)
|
|
|
|
return makeNonLoc(se, T, castTy);
|
2009-07-21 08:12:07 +08:00
|
|
|
return UnknownVal();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-09 06:38:43 +08:00
|
|
|
// If value is a non integer constant, produce unknown.
|
2009-06-26 08:25:05 +08:00
|
|
|
if (!isa<nonloc::ConcreteInt>(val))
|
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-09 06:38:43 +08:00
|
|
|
// Only handle casts from integers to integers - if val is an integer constant
|
|
|
|
// being cast to a non integer type, produce unknown.
|
2009-06-26 08:25:05 +08:00
|
|
|
if (!isLocType && !castTy->isIntegerType())
|
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-26 08:25:05 +08:00
|
|
|
llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue();
|
2012-05-08 11:26:58 +08:00
|
|
|
BasicVals.getAPSIntType(castTy).apply(i);
|
2009-06-26 08:25:05 +08:00
|
|
|
|
|
|
|
if (isLocType)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntLocVal(i);
|
2009-06-26 08:25:05 +08:00
|
|
|
else
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntVal(i);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SVal SimpleSValBuilder::evalCastFromLoc(Loc val, QualType castTy) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-26 08:25:05 +08:00
|
|
|
// Casts from pointers -> pointers, just return the lval.
|
|
|
|
//
|
|
|
|
// Casts from pointers -> references, just return the lval. These
|
|
|
|
// can be introduced by the frontend for corner cases, e.g
|
|
|
|
// casting from va_list* to __builtin_va_list&.
|
|
|
|
//
|
2011-02-17 05:13:32 +08:00
|
|
|
if (Loc::isLocType(castTy) || castTy->isReferenceType())
|
2009-06-26 08:25:05 +08:00
|
|
|
return val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-26 08:25:05 +08:00
|
|
|
// FIXME: Handle transparent unions where a value can be "transparently"
|
|
|
|
// lifted into a union type.
|
|
|
|
if (castTy->isUnionType())
|
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-17 01:54:33 +08:00
|
|
|
if (castTy->isIntegerType()) {
|
2010-12-02 15:49:45 +08:00
|
|
|
unsigned BitWidth = Context.getTypeSize(castTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
|
2010-04-17 01:54:33 +08:00
|
|
|
if (!isa<loc::ConcreteInt>(val))
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeLocAsInteger(val, BitWidth);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-17 01:54:33 +08:00
|
|
|
llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue();
|
2012-05-08 11:26:58 +08:00
|
|
|
BasicVals.getAPSIntType(castTy).apply(i);
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntVal(i);
|
2010-04-17 01:54:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// All other cases: return 'UnknownVal'. This includes casting pointers
|
|
|
|
// to floats, which is probably badness it itself, but this is a good
|
|
|
|
// intermediate solution until we do something better.
|
|
|
|
return UnknownVal();
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function for unary operators.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
SVal SimpleSValBuilder::evalMinus(NonLoc val) {
|
2009-09-09 23:08:12 +08:00
|
|
|
switch (val.getSubKind()) {
|
2009-06-26 08:25:05 +08:00
|
|
|
case nonloc::ConcreteIntKind:
|
2010-12-02 15:49:45 +08:00
|
|
|
return cast<nonloc::ConcreteInt>(val).evalMinus(*this);
|
2009-06-26 08:25:05 +08:00
|
|
|
default:
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
SVal SimpleSValBuilder::evalComplement(NonLoc X) {
|
2009-06-26 08:25:05 +08:00
|
|
|
switch (X.getSubKind()) {
|
|
|
|
case nonloc::ConcreteIntKind:
|
2010-12-02 15:49:45 +08:00
|
|
|
return cast<nonloc::ConcreteInt>(X).evalComplement(*this);
|
2009-06-26 08:25:05 +08:00
|
|
|
default:
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function for binary operators.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
|
|
|
|
switch (op) {
|
|
|
|
default:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Invalid opcode.");
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LT: return BO_GE;
|
|
|
|
case BO_GT: return BO_LE;
|
|
|
|
case BO_LE: return BO_GT;
|
|
|
|
case BO_GE: return BO_LT;
|
|
|
|
case BO_EQ: return BO_NE;
|
|
|
|
case BO_NE: return BO_EQ;
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-28 16:26:15 +08:00
|
|
|
static BinaryOperator::Opcode ReverseComparison(BinaryOperator::Opcode op) {
|
|
|
|
switch (op) {
|
|
|
|
default:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Invalid opcode.");
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LT: return BO_GT;
|
|
|
|
case BO_GT: return BO_LT;
|
|
|
|
case BO_LE: return BO_GE;
|
|
|
|
case BO_GE: return BO_LE;
|
|
|
|
case BO_EQ:
|
|
|
|
case BO_NE:
|
2010-06-28 16:26:15 +08:00
|
|
|
return op;
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-02 05:28:31 +08:00
|
|
|
SVal SimpleSValBuilder::MakeSymIntVal(const SymExpr *LHS,
|
2010-06-20 12:56:29 +08:00
|
|
|
BinaryOperator::Opcode op,
|
|
|
|
const llvm::APSInt &RHS,
|
|
|
|
QualType resultTy) {
|
|
|
|
bool isIdempotent = false;
|
|
|
|
|
|
|
|
// Check for a few special cases with known reductions first.
|
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
// We can't reduce this case; just treat it normally.
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Mul:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a*0 and a*1
|
|
|
|
if (RHS == 0)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntVal(0, resultTy);
|
2010-06-20 12:56:29 +08:00
|
|
|
else if (RHS == 1)
|
|
|
|
isIdempotent = true;
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Div:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a/0 and a/1
|
|
|
|
if (RHS == 0)
|
|
|
|
// This is also handled elsewhere.
|
|
|
|
return UndefinedVal();
|
|
|
|
else if (RHS == 1)
|
|
|
|
isIdempotent = true;
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Rem:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a%0 and a%1
|
|
|
|
if (RHS == 0)
|
|
|
|
// This is also handled elsewhere.
|
|
|
|
return UndefinedVal();
|
|
|
|
else if (RHS == 1)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntVal(0, resultTy);
|
2010-06-20 12:56:29 +08:00
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Add:
|
|
|
|
case BO_Sub:
|
|
|
|
case BO_Shl:
|
|
|
|
case BO_Shr:
|
|
|
|
case BO_Xor:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a+0, a-0, a<<0, a>>0, a^0
|
|
|
|
if (RHS == 0)
|
|
|
|
isIdempotent = true;
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_And:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a&0 and a&(~0)
|
|
|
|
if (RHS == 0)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntVal(0, resultTy);
|
2010-06-20 12:56:29 +08:00
|
|
|
else if (RHS.isAllOnesValue())
|
|
|
|
isIdempotent = true;
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Or:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a|0 and a|(~0)
|
|
|
|
if (RHS == 0)
|
|
|
|
isIdempotent = true;
|
|
|
|
else if (RHS.isAllOnesValue()) {
|
2010-12-02 15:49:45 +08:00
|
|
|
const llvm::APSInt &Result = BasicVals.Convert(resultTy, RHS);
|
2010-06-20 12:56:29 +08:00
|
|
|
return nonloc::ConcreteInt(Result);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Idempotent ops (like a*1) can still change the type of an expression.
|
2011-03-01 08:45:32 +08:00
|
|
|
// Wrap the LHS up in a NonLoc again and let evalCastFromNonLoc do the
|
|
|
|
// dirty work.
|
2011-12-06 02:58:30 +08:00
|
|
|
if (isIdempotent)
|
2011-12-06 02:58:25 +08:00
|
|
|
return evalCastFromNonLoc(nonloc::SymbolVal(LHS), resultTy);
|
2010-06-20 12:56:29 +08:00
|
|
|
|
|
|
|
// If we reach this point, the expression cannot be simplified.
|
2012-05-07 07:40:02 +08:00
|
|
|
// Make a SymbolVal for the entire expression, after converting the RHS.
|
|
|
|
const llvm::APSInt *ConvertedRHS = &RHS;
|
|
|
|
if (BinaryOperator::isComparisonOp(op)) {
|
|
|
|
// We're looking for a type big enough to compare the symbolic value
|
|
|
|
// with the given constant.
|
|
|
|
// FIXME: This is an approximation of Sema::UsualArithmeticConversions.
|
|
|
|
ASTContext &Ctx = getContext();
|
|
|
|
QualType SymbolType = LHS->getType(Ctx);
|
|
|
|
uint64_t ValWidth = RHS.getBitWidth();
|
|
|
|
uint64_t TypeWidth = Ctx.getTypeSize(SymbolType);
|
|
|
|
|
|
|
|
if (ValWidth < TypeWidth) {
|
|
|
|
// If the value is too small, extend it.
|
|
|
|
ConvertedRHS = &BasicVals.Convert(SymbolType, RHS);
|
|
|
|
} else if (ValWidth == TypeWidth) {
|
|
|
|
// If the value is signed but the symbol is unsigned, do the comparison
|
|
|
|
// in unsigned space. [C99 6.3.1.8]
|
|
|
|
// (For the opposite case, the value is already unsigned.)
|
|
|
|
if (RHS.isSigned() && !SymbolType->isSignedIntegerOrEnumerationType())
|
|
|
|
ConvertedRHS = &BasicVals.Convert(SymbolType, RHS);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
ConvertedRHS = &BasicVals.Convert(resultTy, RHS);
|
|
|
|
|
|
|
|
return makeNonLoc(LHS, op, *ConvertedRHS, resultTy);
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
SVal SimpleSValBuilder::evalBinOpNN(ProgramStateRef state,
|
2009-10-06 09:39:48 +08:00
|
|
|
BinaryOperator::Opcode op,
|
2009-06-26 08:25:05 +08:00
|
|
|
NonLoc lhs, NonLoc rhs,
|
2009-07-14 05:55:12 +08:00
|
|
|
QualType resultTy) {
|
2012-05-03 10:13:53 +08:00
|
|
|
NonLoc InputLHS = lhs;
|
|
|
|
NonLoc InputRHS = rhs;
|
|
|
|
|
2009-07-14 05:55:12 +08:00
|
|
|
// Handle trivial case where left-side and right-side are the same.
|
|
|
|
if (lhs == rhs)
|
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_GE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LT:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_NE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Xor:
|
|
|
|
case BO_Sub:
|
2012-09-05 03:34:58 +08:00
|
|
|
if (resultTy->isIntegralOrEnumerationType())
|
|
|
|
return makeIntVal(0, resultTy);
|
|
|
|
return evalCastFromNonLoc(makeIntVal(0, /*Unsigned=*/false), resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Or:
|
|
|
|
case BO_And:
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromNonLoc(lhs, resultTy);
|
2009-07-14 05:55:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-26 08:25:05 +08:00
|
|
|
while (1) {
|
|
|
|
switch (lhs.getSubKind()) {
|
|
|
|
default:
|
2012-05-02 05:10:26 +08:00
|
|
|
return makeSymExprValNN(state, op, lhs, rhs, resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
case nonloc::LocAsIntegerKind: {
|
2009-09-09 23:08:12 +08:00
|
|
|
Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
|
2009-06-26 08:25:05 +08:00
|
|
|
switch (rhs.getSubKind()) {
|
|
|
|
case nonloc::LocAsIntegerKind:
|
2010-12-02 05:57:22 +08:00
|
|
|
return evalBinOpLL(state, op, lhsL,
|
2010-06-28 16:26:15 +08:00
|
|
|
cast<nonloc::LocAsInteger>(rhs).getLoc(),
|
2009-09-09 23:08:12 +08:00
|
|
|
resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
case nonloc::ConcreteIntKind: {
|
|
|
|
// Transform the integer into a location and compare.
|
|
|
|
llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
|
2012-05-08 11:26:58 +08:00
|
|
|
BasicVals.getAPSIntType(Context.VoidPtrTy).apply(i);
|
2010-12-02 15:49:45 +08:00
|
|
|
return evalBinOpLL(state, op, lhsL, makeLoc(i), resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
default:
|
2009-06-26 08:25:05 +08:00
|
|
|
switch (op) {
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
default:
|
|
|
|
// This case also handles pointer arithmetic.
|
2012-05-03 10:13:53 +08:00
|
|
|
return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
case nonloc::ConcreteIntKind: {
|
2012-05-07 07:40:02 +08:00
|
|
|
llvm::APSInt LHSValue = cast<nonloc::ConcreteInt>(lhs).getValue();
|
|
|
|
|
|
|
|
// If we're dealing with two known constants, just perform the operation.
|
|
|
|
if (const llvm::APSInt *KnownRHSValue = getKnownValue(state, rhs)) {
|
|
|
|
llvm::APSInt RHSValue = *KnownRHSValue;
|
|
|
|
if (BinaryOperator::isComparisonOp(op)) {
|
|
|
|
// We're looking for a type big enough to compare the two values.
|
2012-05-08 11:26:58 +08:00
|
|
|
// FIXME: This is not correct. char + short will result in a promotion
|
|
|
|
// to int. Unfortunately we have lost types by this point.
|
|
|
|
APSIntType CompareType = std::max(APSIntType(LHSValue),
|
|
|
|
APSIntType(RHSValue));
|
|
|
|
CompareType.apply(LHSValue);
|
|
|
|
CompareType.apply(RHSValue);
|
2012-05-07 07:40:02 +08:00
|
|
|
} else if (!BinaryOperator::isShiftOp(op)) {
|
2012-05-08 11:26:58 +08:00
|
|
|
APSIntType IntType = BasicVals.getAPSIntType(resultTy);
|
|
|
|
IntType.apply(LHSValue);
|
|
|
|
IntType.apply(RHSValue);
|
2011-06-15 12:55:49 +08:00
|
|
|
}
|
2012-05-07 07:40:02 +08:00
|
|
|
|
|
|
|
const llvm::APSInt *Result =
|
|
|
|
BasicVals.evalAPSInt(op, LHSValue, RHSValue);
|
|
|
|
if (!Result)
|
|
|
|
return UndefinedVal();
|
|
|
|
|
|
|
|
return nonloc::ConcreteInt(*Result);
|
2011-06-15 12:55:49 +08:00
|
|
|
}
|
|
|
|
|
2012-05-07 07:40:02 +08:00
|
|
|
// Swap the left and right sides and flip the operator if doing so
|
|
|
|
// allows us to better reason about the expression (this is a form
|
|
|
|
// of expression canonicalization).
|
|
|
|
// While we're at it, catch some special cases for non-commutative ops.
|
|
|
|
switch (op) {
|
|
|
|
case BO_LT:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_GE:
|
|
|
|
op = ReverseComparison(op);
|
|
|
|
// FALL-THROUGH
|
|
|
|
case BO_EQ:
|
|
|
|
case BO_NE:
|
|
|
|
case BO_Add:
|
|
|
|
case BO_Mul:
|
|
|
|
case BO_And:
|
|
|
|
case BO_Xor:
|
|
|
|
case BO_Or:
|
|
|
|
std::swap(lhs, rhs);
|
|
|
|
continue;
|
|
|
|
case BO_Shr:
|
|
|
|
// (~0)>>a
|
|
|
|
if (LHSValue.isAllOnesValue() && LHSValue.isSigned())
|
|
|
|
return evalCastFromNonLoc(lhs, resultTy);
|
|
|
|
// FALL-THROUGH
|
|
|
|
case BO_Shl:
|
|
|
|
// 0<<a and 0>>a
|
|
|
|
if (LHSValue == 0)
|
|
|
|
return evalCastFromNonLoc(lhs, resultTy);
|
|
|
|
return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy);
|
|
|
|
default:
|
|
|
|
return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case nonloc::SymbolValKind: {
|
2012-05-07 07:40:02 +08:00
|
|
|
// We only handle LHS as simple symbols or SymIntExprs.
|
|
|
|
SymbolRef Sym = cast<nonloc::SymbolVal>(lhs).getSymbol();
|
2011-12-06 02:58:30 +08:00
|
|
|
|
|
|
|
// LHS is a symbolic expression.
|
2012-05-07 07:40:02 +08:00
|
|
|
if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(Sym)) {
|
2011-12-06 02:58:30 +08:00
|
|
|
|
|
|
|
// Is this a logical not? (!x is represented as x == 0.)
|
|
|
|
if (op == BO_EQ && rhs.isZeroConstant()) {
|
|
|
|
// We know how to negate certain expressions. Simplify them here.
|
|
|
|
|
|
|
|
BinaryOperator::Opcode opc = symIntExpr->getOpcode();
|
|
|
|
switch (opc) {
|
|
|
|
default:
|
|
|
|
// We don't know how to negate this operation.
|
|
|
|
// Just handle it as if it were a normal comparison to 0.
|
|
|
|
break;
|
|
|
|
case BO_LAnd:
|
|
|
|
case BO_LOr:
|
|
|
|
llvm_unreachable("Logical operators handled by branching logic.");
|
|
|
|
case BO_Assign:
|
|
|
|
case BO_MulAssign:
|
|
|
|
case BO_DivAssign:
|
|
|
|
case BO_RemAssign:
|
|
|
|
case BO_AddAssign:
|
|
|
|
case BO_SubAssign:
|
|
|
|
case BO_ShlAssign:
|
|
|
|
case BO_ShrAssign:
|
|
|
|
case BO_AndAssign:
|
|
|
|
case BO_XorAssign:
|
|
|
|
case BO_OrAssign:
|
|
|
|
case BO_Comma:
|
|
|
|
llvm_unreachable("'=' and ',' operators handled by ExprEngine.");
|
|
|
|
case BO_PtrMemD:
|
|
|
|
case BO_PtrMemI:
|
|
|
|
llvm_unreachable("Pointer arithmetic not handled here.");
|
|
|
|
case BO_LT:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_GE:
|
|
|
|
case BO_EQ:
|
|
|
|
case BO_NE:
|
|
|
|
// Negate the comparison and make a value.
|
|
|
|
opc = NegateComparison(opc);
|
|
|
|
assert(symIntExpr->getType(Context) == resultTy);
|
|
|
|
return makeNonLoc(symIntExpr->getLHS(), opc,
|
|
|
|
symIntExpr->getRHS(), resultTy);
|
2009-10-06 11:44:49 +08:00
|
|
|
}
|
2009-10-06 09:39:48 +08:00
|
|
|
}
|
2010-08-10 04:31:57 +08:00
|
|
|
|
2011-12-06 02:58:30 +08:00
|
|
|
// For now, only handle expressions whose RHS is a constant.
|
2012-05-07 07:40:02 +08:00
|
|
|
if (const llvm::APSInt *RHSValue = getKnownValue(state, rhs)) {
|
|
|
|
// If both the LHS and the current expression are additive,
|
|
|
|
// fold their constants and try again.
|
|
|
|
if (BinaryOperator::isAdditiveOp(op)) {
|
|
|
|
BinaryOperator::Opcode lop = symIntExpr->getOpcode();
|
|
|
|
if (BinaryOperator::isAdditiveOp(lop)) {
|
|
|
|
// Convert the two constants to a common type, then combine them.
|
|
|
|
|
|
|
|
// resultTy may not be the best type to convert to, but it's
|
|
|
|
// probably the best choice in expressions with mixed type
|
|
|
|
// (such as x+1U+2LL). The rules for implicit conversions should
|
|
|
|
// choose a reasonable type to preserve the expression, and will
|
|
|
|
// at least match how the value is going to be used.
|
2012-05-08 11:26:58 +08:00
|
|
|
APSIntType IntType = BasicVals.getAPSIntType(resultTy);
|
|
|
|
const llvm::APSInt &first = IntType.convert(symIntExpr->getRHS());
|
|
|
|
const llvm::APSInt &second = IntType.convert(*RHSValue);
|
2012-05-07 07:40:02 +08:00
|
|
|
|
|
|
|
const llvm::APSInt *newRHS;
|
|
|
|
if (lop == op)
|
|
|
|
newRHS = BasicVals.evalAPSInt(BO_Add, first, second);
|
|
|
|
else
|
|
|
|
newRHS = BasicVals.evalAPSInt(BO_Sub, first, second);
|
|
|
|
|
|
|
|
assert(newRHS && "Invalid operation despite common type!");
|
|
|
|
rhs = nonloc::ConcreteInt(*newRHS);
|
|
|
|
lhs = nonloc::SymbolVal(symIntExpr->getLHS());
|
|
|
|
op = lop;
|
|
|
|
continue;
|
|
|
|
}
|
2010-08-10 04:31:57 +08:00
|
|
|
}
|
2012-05-07 07:40:02 +08:00
|
|
|
|
|
|
|
// Otherwise, make a SymIntExpr out of the expression.
|
|
|
|
return MakeSymIntVal(symIntExpr, op, *RHSValue, resultTy);
|
2010-08-10 04:31:57 +08:00
|
|
|
}
|
|
|
|
|
2009-06-26 08:25:05 +08:00
|
|
|
|
2012-05-07 07:40:02 +08:00
|
|
|
} else if (isa<SymbolData>(Sym)) {
|
2011-12-06 02:58:30 +08:00
|
|
|
// Does the symbol simplify to a constant? If so, "fold" the constant
|
|
|
|
// by setting 'lhs' to a ConcreteInt and try again.
|
2012-05-07 07:40:02 +08:00
|
|
|
if (const llvm::APSInt *Constant = state->getSymVal(Sym)) {
|
|
|
|
lhs = nonloc::ConcreteInt(*Constant);
|
|
|
|
continue;
|
2011-12-06 02:58:30 +08:00
|
|
|
}
|
|
|
|
|
2012-05-07 07:40:02 +08:00
|
|
|
// Is the RHS a constant?
|
|
|
|
if (const llvm::APSInt *RHSValue = getKnownValue(state, rhs))
|
|
|
|
return MakeSymIntVal(Sym, op, *RHSValue, resultTy);
|
2011-12-06 02:58:30 +08:00
|
|
|
}
|
2012-05-07 07:40:02 +08:00
|
|
|
|
|
|
|
// Give up -- this is not a symbolic expression we can handle.
|
|
|
|
return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-28 16:26:15 +08:00
|
|
|
// FIXME: all this logic will change if/when we have MemRegion::getLocation().
|
2012-01-27 05:29:00 +08:00
|
|
|
SVal SimpleSValBuilder::evalBinOpLL(ProgramStateRef state,
|
2010-06-28 16:26:15 +08:00
|
|
|
BinaryOperator::Opcode op,
|
|
|
|
Loc lhs, Loc rhs,
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType resultTy) {
|
2010-06-28 16:26:15 +08:00
|
|
|
// Only comparisons and subtractions are valid operations on two pointers.
|
|
|
|
// See [C99 6.5.5 through 6.5.14] or [C++0x 5.6 through 5.15].
|
2010-12-02 05:57:22 +08:00
|
|
|
// However, if a pointer is casted to an integer, evalBinOpNN may end up
|
2010-06-30 09:35:20 +08:00
|
|
|
// calling this function with another operation (PR7527). We don't attempt to
|
|
|
|
// model this for now, but it could be useful, particularly when the
|
|
|
|
// "location" is actually an integer value that's been passed through a void*.
|
2010-08-25 19:45:40 +08:00
|
|
|
if (!(BinaryOperator::isComparisonOp(op) || op == BO_Sub))
|
2010-06-30 09:35:20 +08:00
|
|
|
return UnknownVal();
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
// Special cases for when both sides are identical.
|
|
|
|
if (lhs == rhs) {
|
|
|
|
switch (op) {
|
2009-06-26 08:25:05 +08:00
|
|
|
default:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Unimplemented operation for two identical values");
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Sub:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeZeroVal(resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_GE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
|
|
|
case BO_LT:
|
|
|
|
case BO_GT:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (lhs.getSubKind()) {
|
|
|
|
default:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Ordering not implemented for this Loc.");
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
case loc::GotoLabelKind:
|
|
|
|
// The only thing we know about labels is that they're non-null.
|
|
|
|
if (rhs.isZeroConstant()) {
|
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Sub:
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromLoc(lhs, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_LT:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_GE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// There may be two labels for the same location, and a function region may
|
|
|
|
// have the same address as a label at the start of the function (depending
|
|
|
|
// on the ABI).
|
|
|
|
// FIXME: we can probably do a comparison against other MemRegions, though.
|
|
|
|
// FIXME: is there a way to tell if two labels refer to the same location?
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
case loc::ConcreteIntKind: {
|
|
|
|
// If one of the operands is a symbol and the other is a constant,
|
|
|
|
// build an expression for use by the constraint manager.
|
|
|
|
if (SymbolRef rSym = rhs.getAsLocSymbol()) {
|
|
|
|
// We can only build expressions with symbols on the left,
|
|
|
|
// so we need a reversible operator.
|
|
|
|
if (!BinaryOperator::isComparisonOp(op))
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
const llvm::APSInt &lVal = cast<loc::ConcreteInt>(lhs).getValue();
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeNonLoc(rSym, ReverseComparison(op), lVal, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If both operands are constants, just perform the operation.
|
|
|
|
if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) {
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal ResultVal = cast<loc::ConcreteInt>(lhs).evalBinOp(BasicVals, op,
|
|
|
|
*rInt);
|
2010-06-28 16:26:15 +08:00
|
|
|
if (Loc *Result = dyn_cast<Loc>(&ResultVal))
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromLoc(*Result, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
else
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case comparisons against NULL.
|
|
|
|
// This must come after the test if the RHS is a symbol, which is used to
|
|
|
|
// build constraints. The address of any non-symbolic region is guaranteed
|
|
|
|
// to be non-NULL, as is any label.
|
|
|
|
assert(isa<loc::MemRegionVal>(rhs) || isa<loc::GotoLabel>(rhs));
|
|
|
|
if (lhs.isZeroConstant()) {
|
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_GE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
|
|
|
case BO_LT:
|
|
|
|
case BO_LE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparing an arbitrary integer to a region or label address is
|
|
|
|
// completely unknowable.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
case loc::MemRegionKind: {
|
|
|
|
if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) {
|
|
|
|
// If one of the operands is a symbol and the other is a constant,
|
|
|
|
// build an expression for use by the constraint manager.
|
|
|
|
if (SymbolRef lSym = lhs.getAsLocSymbol())
|
|
|
|
return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy);
|
|
|
|
|
|
|
|
// Special case comparisons to NULL.
|
|
|
|
// This must come after the test if the LHS is a symbol, which is used to
|
|
|
|
// build constraints. The address of any non-symbolic region is guaranteed
|
|
|
|
// to be non-NULL.
|
|
|
|
if (rInt->isZeroConstant()) {
|
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Sub:
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromLoc(lhs, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
|
|
|
case BO_LT:
|
|
|
|
case BO_LE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_GE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparing a region to an arbitrary integer is completely unknowable.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get both values as regions, if possible.
|
|
|
|
const MemRegion *LeftMR = lhs.getAsRegion();
|
|
|
|
assert(LeftMR && "MemRegionKind SVal doesn't have a region!");
|
|
|
|
|
|
|
|
const MemRegion *RightMR = rhs.getAsRegion();
|
|
|
|
if (!RightMR)
|
|
|
|
// The RHS is probably a label, which in theory could address a region.
|
|
|
|
// FIXME: we can probably make a more useful statement about non-code
|
|
|
|
// regions, though.
|
|
|
|
return UnknownVal();
|
|
|
|
|
2012-06-07 11:57:32 +08:00
|
|
|
const MemSpaceRegion *LeftMS = LeftMR->getMemorySpace();
|
|
|
|
const MemSpaceRegion *RightMS = RightMR->getMemorySpace();
|
|
|
|
const MemSpaceRegion *UnknownMS = MemMgr.getUnknownRegion();
|
2010-06-28 16:26:15 +08:00
|
|
|
const MemRegion *LeftBase = LeftMR->getBaseRegion();
|
|
|
|
const MemRegion *RightBase = RightMR->getBaseRegion();
|
2012-06-07 11:57:32 +08:00
|
|
|
|
|
|
|
// If the two regions are from different known memory spaces they cannot be
|
|
|
|
// equal. Also, assume that no symbolic region (whose memory space is
|
|
|
|
// unknown) is on the stack.
|
|
|
|
if (LeftMS != RightMS &&
|
|
|
|
((LeftMS != UnknownMS && RightMS != UnknownMS) ||
|
|
|
|
(isa<StackSpaceRegion>(LeftMS) || isa<StackSpaceRegion>(RightMS)))) {
|
2010-06-28 16:26:15 +08:00
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
return UnknownVal();
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 11:57:32 +08:00
|
|
|
// If both values wrap regions, see if they're from different base regions.
|
|
|
|
// Note, heap base symbolic regions are assumed to not alias with
|
|
|
|
// each other; for example, we assume that malloc returns different address
|
|
|
|
// on each invocation.
|
|
|
|
if (LeftBase != RightBase &&
|
|
|
|
((!isa<SymbolicRegion>(LeftBase) && !isa<SymbolicRegion>(RightBase)) ||
|
2012-06-08 04:18:08 +08:00
|
|
|
(isa<HeapSpaceRegion>(LeftMS) || isa<HeapSpaceRegion>(RightMS))) ){
|
2012-03-06 07:06:19 +08:00
|
|
|
switch (op) {
|
2012-06-07 11:57:32 +08:00
|
|
|
default:
|
|
|
|
return UnknownVal();
|
|
|
|
case BO_EQ:
|
|
|
|
return makeTruthVal(false, resultTy);
|
|
|
|
case BO_NE:
|
|
|
|
return makeTruthVal(true, resultTy);
|
2012-03-06 07:06:19 +08:00
|
|
|
}
|
|
|
|
}
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
// FIXME: If/when there is a getAsRawOffset() for FieldRegions, this
|
|
|
|
// ElementRegion path and the FieldRegion path below should be unified.
|
|
|
|
if (const ElementRegion *LeftER = dyn_cast<ElementRegion>(LeftMR)) {
|
|
|
|
// First see if the right region is also an ElementRegion.
|
|
|
|
const ElementRegion *RightER = dyn_cast<ElementRegion>(RightMR);
|
|
|
|
if (!RightER)
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// Next, see if the two ERs have the same super-region and matching types.
|
|
|
|
// FIXME: This should do something useful even if the types don't match,
|
|
|
|
// though if both indexes are constant the RegionRawOffset path will
|
|
|
|
// give the correct answer.
|
|
|
|
if (LeftER->getSuperRegion() == RightER->getSuperRegion() &&
|
|
|
|
LeftER->getElementType() == RightER->getElementType()) {
|
|
|
|
// Get the left index and cast it to the correct type.
|
|
|
|
// If the index is unknown or undefined, bail out here.
|
|
|
|
SVal LeftIndexVal = LeftER->getIndex();
|
|
|
|
NonLoc *LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
|
|
|
|
if (!LeftIndex)
|
|
|
|
return UnknownVal();
|
2011-03-01 08:45:32 +08:00
|
|
|
LeftIndexVal = evalCastFromNonLoc(*LeftIndex, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
|
|
|
|
if (!LeftIndex)
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// Do the same for the right index.
|
|
|
|
SVal RightIndexVal = RightER->getIndex();
|
|
|
|
NonLoc *RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
|
|
|
|
if (!RightIndex)
|
|
|
|
return UnknownVal();
|
2011-03-01 08:45:32 +08:00
|
|
|
RightIndexVal = evalCastFromNonLoc(*RightIndex, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
|
|
|
|
if (!RightIndex)
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// Actually perform the operation.
|
2010-12-02 05:57:22 +08:00
|
|
|
// evalBinOpNN expects the two indexes to already be the right type.
|
|
|
|
return evalBinOpNN(state, op, *LeftIndex, *RightIndex, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the element indexes aren't comparable, see if the raw offsets are.
|
2010-08-02 12:56:14 +08:00
|
|
|
RegionRawOffset LeftOffset = LeftER->getAsArrayOffset();
|
|
|
|
RegionRawOffset RightOffset = RightER->getAsArrayOffset();
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
if (LeftOffset.getRegion() != NULL &&
|
|
|
|
LeftOffset.getRegion() == RightOffset.getRegion()) {
|
2011-01-24 09:55:39 +08:00
|
|
|
CharUnits left = LeftOffset.getOffset();
|
|
|
|
CharUnits right = RightOffset.getOffset();
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
return UnknownVal();
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LT:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left < right, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_GT:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left > right, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left <= right, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_GE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left >= right, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left == right, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left != right, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, we have no way of comparing the ElementRegions.
|
2009-10-06 09:39:48 +08:00
|
|
|
return UnknownVal();
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// See if both regions are fields of the same structure.
|
|
|
|
// FIXME: This doesn't handle nesting, inheritance, or Objective-C ivars.
|
|
|
|
if (const FieldRegion *LeftFR = dyn_cast<FieldRegion>(LeftMR)) {
|
|
|
|
// Only comparisons are meaningful here!
|
|
|
|
if (!BinaryOperator::isComparisonOp(op))
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// First see if the right region is also a FieldRegion.
|
|
|
|
const FieldRegion *RightFR = dyn_cast<FieldRegion>(RightMR);
|
|
|
|
if (!RightFR)
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// Next, see if the two FRs have the same super-region.
|
|
|
|
// FIXME: This doesn't handle casts yet, and simply stripping the casts
|
|
|
|
// doesn't help.
|
|
|
|
if (LeftFR->getSuperRegion() != RightFR->getSuperRegion())
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
const FieldDecl *LeftFD = LeftFR->getDecl();
|
|
|
|
const FieldDecl *RightFD = RightFR->getDecl();
|
|
|
|
const RecordDecl *RD = LeftFD->getParent();
|
|
|
|
|
|
|
|
// Make sure the two FRs are from the same kind of record. Just in case!
|
|
|
|
// FIXME: This is probably where inheritance would be a problem.
|
|
|
|
if (RD != RightFD->getParent())
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// We know for sure that the two fields are not the same, since that
|
|
|
|
// would have given us the same SVal.
|
2010-08-25 19:45:40 +08:00
|
|
|
if (op == BO_EQ)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
if (op == BO_NE)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
// Iterate through the fields and see which one comes first.
|
|
|
|
// [C99 6.7.2.1.13] "Within a structure object, the non-bit-field
|
|
|
|
// members and the units in which bit-fields reside have addresses that
|
|
|
|
// increase in the order in which they are declared."
|
2010-08-25 19:45:40 +08:00
|
|
|
bool leftFirst = (op == BO_LT || op == BO_LE);
|
2010-06-28 16:26:15 +08:00
|
|
|
for (RecordDecl::field_iterator I = RD->field_begin(),
|
|
|
|
E = RD->field_end(); I!=E; ++I) {
|
2012-06-07 04:45:41 +08:00
|
|
|
if (*I == LeftFD)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(leftFirst, resultTy);
|
2012-06-07 04:45:41 +08:00
|
|
|
if (*I == RightFD)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(!leftFirst, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Fields not found in parent record's definition");
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here, we have no way of comparing the regions.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
SVal SimpleSValBuilder::evalBinOpLN(ProgramStateRef state,
|
2009-06-26 08:25:05 +08:00
|
|
|
BinaryOperator::Opcode op,
|
2009-09-09 23:08:12 +08:00
|
|
|
Loc lhs, NonLoc rhs, QualType resultTy) {
|
2010-12-24 16:39:33 +08:00
|
|
|
|
|
|
|
// Special case: rhs is a zero constant.
|
|
|
|
if (rhs.isZeroConstant())
|
|
|
|
return lhs;
|
|
|
|
|
2009-06-26 08:25:05 +08:00
|
|
|
// Special case: 'rhs' is an integer that has the same width as a pointer and
|
|
|
|
// we are using the integer location in a comparison. Normally this cannot be
|
|
|
|
// triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
|
|
|
|
// can generate comparisons that trigger this code.
|
|
|
|
// FIXME: Are all locations guaranteed to have pointer width?
|
2010-06-28 16:26:15 +08:00
|
|
|
if (BinaryOperator::isComparisonOp(op)) {
|
2009-06-26 08:25:05 +08:00
|
|
|
if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
|
|
|
|
const llvm::APSInt *x = &rhsInt->getValue();
|
2010-12-02 15:49:45 +08:00
|
|
|
ASTContext &ctx = Context;
|
2009-06-26 08:25:05 +08:00
|
|
|
if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
|
|
|
|
// Convert the signedness of the integer (if necessary).
|
|
|
|
if (x->isSigned())
|
2010-12-02 15:49:45 +08:00
|
|
|
x = &getBasicValueFactory().getValue(*x, true);
|
2009-06-26 08:25:05 +08:00
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
return evalBinOpLL(state, op, lhs, loc::ConcreteInt(*x), resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
2012-05-08 03:40:49 +08:00
|
|
|
return UnknownVal();
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
2010-09-03 09:07:06 +08:00
|
|
|
|
|
|
|
// We are dealing with pointer arithmetic.
|
|
|
|
|
|
|
|
// Handle pointer arithmetic on constant values.
|
|
|
|
if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
|
|
|
|
if (loc::ConcreteInt *lhsInt = dyn_cast<loc::ConcreteInt>(&lhs)) {
|
|
|
|
const llvm::APSInt &leftI = lhsInt->getValue();
|
|
|
|
assert(leftI.isUnsigned());
|
|
|
|
llvm::APSInt rightI(rhsInt->getValue(), /* isUnsigned */ true);
|
|
|
|
|
|
|
|
// Convert the bitwidth of rightI. This should deal with overflow
|
|
|
|
// since we are dealing with concrete values.
|
2010-12-07 16:25:34 +08:00
|
|
|
rightI = rightI.extOrTrunc(leftI.getBitWidth());
|
2010-09-03 09:07:06 +08:00
|
|
|
|
|
|
|
// Offset the increment by the pointer size.
|
|
|
|
llvm::APSInt Multiplicand(rightI.getBitWidth(), /* isUnsigned */ true);
|
|
|
|
rightI *= Multiplicand;
|
|
|
|
|
|
|
|
// Compute the adjusted pointer.
|
|
|
|
switch (op) {
|
|
|
|
case BO_Add:
|
|
|
|
rightI = leftI + rightI;
|
|
|
|
break;
|
|
|
|
case BO_Sub:
|
|
|
|
rightI = leftI - rightI;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid pointer arithmetic operation");
|
|
|
|
}
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::ConcreteInt(getBasicValueFactory().getValue(rightI));
|
2010-09-03 09:07:06 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-24 16:39:33 +08:00
|
|
|
// Handle cases where 'lhs' is a region.
|
|
|
|
if (const MemRegion *region = lhs.getAsRegion()) {
|
|
|
|
rhs = cast<NonLoc>(convertToArrayIndex(rhs));
|
|
|
|
SVal index = UnknownVal();
|
|
|
|
const MemRegion *superR = 0;
|
|
|
|
QualType elementType;
|
|
|
|
|
|
|
|
if (const ElementRegion *elemReg = dyn_cast<ElementRegion>(region)) {
|
2011-04-12 11:49:37 +08:00
|
|
|
assert(op == BO_Add || op == BO_Sub);
|
|
|
|
index = evalBinOpNN(state, op, elemReg->getIndex(), rhs,
|
2010-12-24 16:39:33 +08:00
|
|
|
getArrayIndexType());
|
|
|
|
superR = elemReg->getSuperRegion();
|
|
|
|
elementType = elemReg->getElementType();
|
|
|
|
}
|
|
|
|
else if (isa<SubRegion>(region)) {
|
|
|
|
superR = region;
|
|
|
|
index = rhs;
|
|
|
|
if (const PointerType *PT = resultTy->getAs<PointerType>()) {
|
|
|
|
elementType = PT->getPointeeType();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const ObjCObjectPointerType *OT =
|
|
|
|
resultTy->getAs<ObjCObjectPointerType>();
|
|
|
|
elementType = OT->getPointeeType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NonLoc *indexV = dyn_cast<NonLoc>(&index)) {
|
|
|
|
return loc::MemRegionVal(MemMgr.getElementRegion(elementType, *indexV,
|
|
|
|
superR, getContext()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return UnknownVal();
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
2010-07-04 08:00:41 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
const llvm::APSInt *SimpleSValBuilder::getKnownValue(ProgramStateRef state,
|
2010-07-04 08:00:41 +08:00
|
|
|
SVal V) {
|
|
|
|
if (V.isUnknownOrUndef())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
|
|
|
|
return &X->getValue();
|
|
|
|
|
|
|
|
if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
|
|
|
|
return &X->getValue();
|
|
|
|
|
|
|
|
if (SymbolRef Sym = V.getAsSymbol())
|
|
|
|
return state->getSymVal(Sym);
|
|
|
|
|
|
|
|
// FIXME: Add support for SymExprs.
|
|
|
|
return NULL;
|
|
|
|
}
|