2008-03-08 04:13:31 +08:00
|
|
|
//=== BasicValueFactory.cpp - Basic values for Path Sens analysis --*- C++ -*-//
|
2008-02-16 09:12:31 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-03-08 04:13:31 +08:00
|
|
|
// This file defines BasicValueFactory, a class that manages the lifetime
|
2010-12-23 02:53:44 +08:00
|
|
|
// of APSInt objects and symbolic constraints used by ExprEngine
|
2008-03-08 04:13:31 +08:00
|
|
|
// and related classes.
|
2008-02-16 09:12:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-07-05 01:04:04 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
|
2011-03-09 07:39:37 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
|
2008-02-16 09:12:31 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2008-02-16 09:12:31 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
|
2008-10-31 01:44:46 +08:00
|
|
|
llvm::ImmutableList<SVal> L) {
|
2008-10-30 12:58:00 +08:00
|
|
|
T.Profile(ID);
|
2008-10-31 01:44:46 +08:00
|
|
|
ID.AddPointer(L.getInternalPointer());
|
2008-10-30 12:58:00 +08:00
|
|
|
}
|
|
|
|
|
2009-08-06 09:20:57 +08:00
|
|
|
void LazyCompoundValData::Profile(llvm::FoldingSetNodeID& ID,
|
2011-03-09 07:18:00 +08:00
|
|
|
const StoreRef &store,
|
2011-08-13 04:02:48 +08:00
|
|
|
const TypedValueRegion *region) {
|
2011-03-09 07:18:00 +08:00
|
|
|
ID.AddPointer(store.getStore());
|
2009-08-06 09:20:57 +08:00
|
|
|
ID.AddPointer(region);
|
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef std::pair<SVal, uintptr_t> SValData;
|
|
|
|
typedef std::pair<SVal, SVal> SValPair;
|
2008-04-30 07:24:44 +08:00
|
|
|
|
2008-04-23 05:10:18 +08:00
|
|
|
namespace llvm {
|
2008-10-17 13:57:07 +08:00
|
|
|
template<> struct FoldingSetTrait<SValData> {
|
|
|
|
static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
|
2008-04-23 05:10:18 +08:00
|
|
|
X.first.Profile(ID);
|
2008-04-30 06:17:41 +08:00
|
|
|
ID.AddPointer( (void*) X.second);
|
2008-04-23 05:10:18 +08:00
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
template<> struct FoldingSetTrait<SValPair> {
|
|
|
|
static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
|
2008-04-30 07:24:44 +08:00
|
|
|
X.first.Profile(ID);
|
|
|
|
X.second.Profile(ID);
|
|
|
|
}
|
|
|
|
};
|
2008-04-23 05:10:18 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData> >
|
|
|
|
PersistentSValsTy;
|
2008-04-23 05:10:18 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair> >
|
|
|
|
PersistentSValPairsTy;
|
2008-04-30 07:24:44 +08:00
|
|
|
|
2008-03-08 04:13:31 +08:00
|
|
|
BasicValueFactory::~BasicValueFactory() {
|
2008-02-16 09:12:31 +08:00
|
|
|
// Note that the dstor for the contents of APSIntSet will never be called,
|
|
|
|
// so we iterate over the set and invoke the dstor for each APSInt. This
|
|
|
|
// frees an aux. memory allocated to represent very large constants.
|
|
|
|
for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
|
|
|
|
I->getValue().~APSInt();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
delete (PersistentSValsTy*) PersistentSVals;
|
2008-10-17 13:57:07 +08:00
|
|
|
delete (PersistentSValPairsTy*) PersistentSValPairs;
|
2008-02-16 09:12:31 +08:00
|
|
|
}
|
|
|
|
|
2008-03-08 04:13:31 +08:00
|
|
|
const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) {
|
2008-02-16 09:12:31 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2008-02-16 09:12:31 +08:00
|
|
|
typedef llvm::FoldingSetNodeWrapper<llvm::APSInt> FoldNodeTy;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-16 09:12:31 +08:00
|
|
|
X.Profile(ID);
|
|
|
|
FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (!P) {
|
2008-02-16 09:12:31 +08:00
|
|
|
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
|
|
|
|
new (P) FoldNodeTy(X);
|
|
|
|
APSIntSet.InsertNode(P, InsertPos);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-16 09:12:31 +08:00
|
|
|
return *P;
|
|
|
|
}
|
|
|
|
|
2008-11-22 21:21:46 +08:00
|
|
|
const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X,
|
|
|
|
bool isUnsigned) {
|
|
|
|
llvm::APSInt V(X, isUnsigned);
|
|
|
|
return getValue(V);
|
|
|
|
}
|
|
|
|
|
2008-03-08 04:13:31 +08:00
|
|
|
const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
|
2008-02-16 09:12:31 +08:00
|
|
|
bool isUnsigned) {
|
|
|
|
llvm::APSInt V(BitWidth, isUnsigned);
|
2009-09-09 23:08:12 +08:00
|
|
|
V = X;
|
2008-02-16 09:12:31 +08:00
|
|
|
return getValue(V);
|
|
|
|
}
|
|
|
|
|
2008-03-08 04:13:31 +08:00
|
|
|
const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-09-02 01:39:24 +08:00
|
|
|
return getValue(getAPSIntType(T).getValue(X));
|
2008-02-16 09:12:31 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
const CompoundValData*
|
2008-10-31 01:44:46 +08:00
|
|
|
BasicValueFactory::getCompoundValData(QualType T,
|
|
|
|
llvm::ImmutableList<SVal> Vals) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-30 12:58:00 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2008-10-31 01:44:46 +08:00
|
|
|
CompoundValData::Profile(ID, T, Vals);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2008-10-30 12:58:00 +08:00
|
|
|
|
|
|
|
CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
|
|
|
|
if (!D) {
|
|
|
|
D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>();
|
2008-10-31 01:44:46 +08:00
|
|
|
new (D) CompoundValData(T, Vals);
|
2008-10-30 12:58:00 +08:00
|
|
|
CompoundValDataSet.InsertNode(D, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2009-08-06 09:20:57 +08:00
|
|
|
const LazyCompoundValData*
|
2011-03-09 07:18:00 +08:00
|
|
|
BasicValueFactory::getLazyCompoundValData(const StoreRef &store,
|
2011-08-13 04:02:48 +08:00
|
|
|
const TypedValueRegion *region) {
|
2009-08-06 09:20:57 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2010-02-05 10:26:30 +08:00
|
|
|
LazyCompoundValData::Profile(ID, store, region);
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 09:20:57 +08:00
|
|
|
LazyCompoundValData *D =
|
|
|
|
LazyCompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 09:20:57 +08:00
|
|
|
if (!D) {
|
|
|
|
D = (LazyCompoundValData*) BPAlloc.Allocate<LazyCompoundValData>();
|
2010-02-05 10:26:30 +08:00
|
|
|
new (D) LazyCompoundValData(store, region);
|
2009-08-06 09:20:57 +08:00
|
|
|
LazyCompoundValDataSet.InsertNode(D, InsertPos);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-06 09:20:57 +08:00
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
const llvm::APSInt*
|
2010-12-02 05:57:22 +08:00
|
|
|
BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
|
2008-02-16 09:12:31 +08:00
|
|
|
const llvm::APSInt& V1, const llvm::APSInt& V2) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-16 09:12:31 +08:00
|
|
|
switch (Op) {
|
|
|
|
default:
|
|
|
|
assert (false && "Invalid Opcode.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Mul:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 * V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Div:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 / V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Rem:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 % V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Add:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 + V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Sub:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 - V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Shl: {
|
2008-02-29 04:32:03 +08:00
|
|
|
|
|
|
|
// FIXME: This logic should probably go higher up, where we can
|
|
|
|
// test these conditions symbolically.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
// FIXME: Expand these checks to include all undefined behavior.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
if (V2.isSigned() && V2.isNegative())
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
uint64_t Amt = V2.getZExtValue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-01-17 02:02:23 +08:00
|
|
|
if (Amt >= V1.getBitWidth())
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1.operator<<( (unsigned) Amt ));
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Shr: {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
// FIXME: This logic should probably go higher up, where we can
|
|
|
|
// test these conditions symbolically.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
// FIXME: Expand these checks to include all undefined behavior.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
if (V2.isSigned() && V2.isNegative())
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
uint64_t Amt = V2.getZExtValue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-01-17 02:02:23 +08:00
|
|
|
if (Amt >= V1.getBitWidth())
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1.operator>>( (unsigned) Amt ));
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LT:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 < V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_GT:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 > V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LE:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 <= V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_GE:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 >= V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 == V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getTruthValue( V1 != V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-16 09:12:31 +08:00
|
|
|
// Note: LAnd, LOr, Comma are handled specially by higher-level logic.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_And:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 & V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Or:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 | V2 );
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Xor:
|
2008-02-29 04:32:03 +08:00
|
|
|
return &getValue( V1 ^ V2 );
|
2008-02-16 09:12:31 +08:00
|
|
|
}
|
|
|
|
}
|
2008-04-23 05:10:18 +08:00
|
|
|
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
const std::pair<SVal, uintptr_t>&
|
|
|
|
BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-23 05:10:18 +08:00
|
|
|
// Lazily create the folding set.
|
2008-10-17 13:57:07 +08:00
|
|
|
if (!PersistentSVals) PersistentSVals = new PersistentSValsTy();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-23 05:10:18 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2008-04-23 05:10:18 +08:00
|
|
|
V.Profile(ID);
|
2008-04-30 06:17:41 +08:00
|
|
|
ID.AddPointer((void*) Data);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef llvm::FoldingSetNodeWrapper<SValData> FoldNodeTy;
|
2008-04-23 05:10:18 +08:00
|
|
|
FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (!P) {
|
2008-04-23 05:10:18 +08:00
|
|
|
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
|
2008-04-30 06:17:41 +08:00
|
|
|
new (P) FoldNodeTy(std::make_pair(V, Data));
|
2008-04-23 05:10:18 +08:00
|
|
|
Map.InsertNode(P, InsertPos);
|
|
|
|
}
|
|
|
|
|
2008-04-30 06:17:41 +08:00
|
|
|
return P->getValue();
|
2008-04-23 05:10:18 +08:00
|
|
|
}
|
2008-04-30 07:24:44 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
const std::pair<SVal, SVal>&
|
|
|
|
BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 07:24:44 +08:00
|
|
|
// Lazily create the folding set.
|
2008-10-17 13:57:07 +08:00
|
|
|
if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 07:24:44 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2011-08-13 07:37:29 +08:00
|
|
|
void *InsertPos;
|
2008-04-30 07:24:44 +08:00
|
|
|
V1.Profile(ID);
|
|
|
|
V2.Profile(ID);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
typedef llvm::FoldingSetNodeWrapper<SValPair> FoldNodeTy;
|
2008-04-30 07:24:44 +08:00
|
|
|
FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
if (!P) {
|
2008-04-30 07:24:44 +08:00
|
|
|
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
|
|
|
|
new (P) FoldNodeTy(std::make_pair(V1, V2));
|
|
|
|
Map.InsertNode(P, InsertPos);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-30 07:24:44 +08:00
|
|
|
return P->getValue();
|
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
const SVal* BasicValueFactory::getPersistentSVal(SVal X) {
|
|
|
|
return &getPersistentSValWithData(X, 0).first;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|