opnsense-src/lib/StaticAnalyzer/Core/BasicValueFactory.cpp

289 lines
7.7 KiB
C++
Raw Normal View History

2009-06-02 13:58:47 -04:00
//=== BasicValueFactory.cpp - Basic values for Path Sens analysis --*- C++ -*-//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines BasicValueFactory, a class that manages the lifetime
// of APSInt objects and symbolic constraints used by ExprEngine
2009-06-02 13:58:47 -04:00
// and related classes.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/ASTContext.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
2009-06-02 13:58:47 -04:00
using namespace clang;
using namespace ento;
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
2009-06-02 13:58:47 -04:00
llvm::ImmutableList<SVal> L) {
T.Profile(ID);
ID.AddPointer(L.getInternalPointer());
}
2009-10-14 14:03:49 -04:00
void LazyCompoundValData::Profile(llvm::FoldingSetNodeID& ID,
const StoreRef &store,
const TypedValueRegion *region) {
ID.AddPointer(store.getStore());
2009-10-14 14:03:49 -04:00
ID.AddPointer(region);
}
2009-06-02 13:58:47 -04:00
typedef std::pair<SVal, uintptr_t> SValData;
typedef std::pair<SVal, SVal> SValPair;
namespace llvm {
template<> struct FoldingSetTrait<SValData> {
static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
X.first.Profile(ID);
ID.AddPointer( (void*) X.second);
}
};
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
template<> struct FoldingSetTrait<SValPair> {
static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
X.first.Profile(ID);
X.second.Profile(ID);
}
};
}
typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData> >
PersistentSValsTy;
typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair> >
PersistentSValPairsTy;
BasicValueFactory::~BasicValueFactory() {
// 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-10-14 14:03:49 -04:00
delete (PersistentSValsTy*) PersistentSVals;
2009-06-02 13:58:47 -04:00
delete (PersistentSValPairsTy*) PersistentSValPairs;
}
const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) {
llvm::FoldingSetNodeID ID;
void *InsertPos;
2009-06-02 13:58:47 -04:00
typedef llvm::FoldingSetNodeWrapper<llvm::APSInt> FoldNodeTy;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
X.Profile(ID);
FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
2009-10-14 14:03:49 -04:00
if (!P) {
2009-06-02 13:58:47 -04:00
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
new (P) FoldNodeTy(X);
APSIntSet.InsertNode(P, InsertPos);
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
return *P;
}
const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X,
bool isUnsigned) {
llvm::APSInt V(X, isUnsigned);
return getValue(V);
}
const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
bool isUnsigned) {
llvm::APSInt V(BitWidth, isUnsigned);
2009-10-14 14:03:49 -04:00
V = X;
2009-06-02 13:58:47 -04:00
return getValue(V);
}
const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) {
2009-10-14 14:03:49 -04:00
return getValue(getAPSIntType(T).getValue(X));
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
const CompoundValData*
2009-06-02 13:58:47 -04:00
BasicValueFactory::getCompoundValData(QualType T,
llvm::ImmutableList<SVal> Vals) {
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
llvm::FoldingSetNodeID ID;
CompoundValData::Profile(ID, T, Vals);
void *InsertPos;
2009-06-02 13:58:47 -04:00
CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
if (!D) {
D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>();
new (D) CompoundValData(T, Vals);
CompoundValDataSet.InsertNode(D, InsertPos);
}
return D;
}
2009-10-14 14:03:49 -04:00
const LazyCompoundValData*
BasicValueFactory::getLazyCompoundValData(const StoreRef &store,
const TypedValueRegion *region) {
2009-10-14 14:03:49 -04:00
llvm::FoldingSetNodeID ID;
2010-02-16 04:31:36 -05:00
LazyCompoundValData::Profile(ID, store, region);
void *InsertPos;
2009-10-14 14:03:49 -04:00
LazyCompoundValData *D =
LazyCompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
if (!D) {
D = (LazyCompoundValData*) BPAlloc.Allocate<LazyCompoundValData>();
2010-02-16 04:31:36 -05:00
new (D) LazyCompoundValData(store, region);
2009-10-14 14:03:49 -04:00
LazyCompoundValDataSet.InsertNode(D, InsertPos);
}
return D;
}
2009-06-02 13:58:47 -04:00
const llvm::APSInt*
BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
2009-06-02 13:58:47 -04:00
const llvm::APSInt& V1, const llvm::APSInt& V2) {
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
switch (Op) {
default:
assert (false && "Invalid Opcode.");
2009-10-14 14:03:49 -04:00
case BO_Mul:
2009-06-02 13:58:47 -04:00
return &getValue( V1 * V2 );
2009-10-14 14:03:49 -04:00
case BO_Div:
2009-06-02 13:58:47 -04:00
return &getValue( V1 / V2 );
2009-10-14 14:03:49 -04:00
case BO_Rem:
2009-06-02 13:58:47 -04:00
return &getValue( V1 % V2 );
2009-10-14 14:03:49 -04:00
case BO_Add:
2009-06-02 13:58:47 -04:00
return &getValue( V1 + V2 );
2009-10-14 14:03:49 -04:00
case BO_Sub:
2009-06-02 13:58:47 -04:00
return &getValue( V1 - V2 );
2009-10-14 14:03:49 -04:00
case BO_Shl: {
2009-06-02 13:58:47 -04:00
// FIXME: This logic should probably go higher up, where we can
// test these conditions symbolically.
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// FIXME: Expand these checks to include all undefined behavior.
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (V2.isSigned() && V2.isNegative())
return NULL;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
uint64_t Amt = V2.getZExtValue();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (Amt > V1.getBitWidth())
return NULL;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
return &getValue( V1.operator<<( (unsigned) Amt ));
}
2009-10-14 14:03:49 -04:00
case BO_Shr: {
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// FIXME: This logic should probably go higher up, where we can
// test these conditions symbolically.
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// FIXME: Expand these checks to include all undefined behavior.
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (V2.isSigned() && V2.isNegative())
return NULL;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
uint64_t Amt = V2.getZExtValue();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (Amt > V1.getBitWidth())
return NULL;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
return &getValue( V1.operator>>( (unsigned) Amt ));
}
2009-10-14 14:03:49 -04:00
case BO_LT:
2009-06-02 13:58:47 -04:00
return &getTruthValue( V1 < V2 );
2009-10-14 14:03:49 -04:00
case BO_GT:
2009-06-02 13:58:47 -04:00
return &getTruthValue( V1 > V2 );
2009-10-14 14:03:49 -04:00
case BO_LE:
2009-06-02 13:58:47 -04:00
return &getTruthValue( V1 <= V2 );
2009-10-14 14:03:49 -04:00
case BO_GE:
2009-06-02 13:58:47 -04:00
return &getTruthValue( V1 >= V2 );
2009-10-14 14:03:49 -04:00
case BO_EQ:
2009-06-02 13:58:47 -04:00
return &getTruthValue( V1 == V2 );
2009-10-14 14:03:49 -04:00
case BO_NE:
2009-06-02 13:58:47 -04:00
return &getTruthValue( V1 != V2 );
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Note: LAnd, LOr, Comma are handled specially by higher-level logic.
2009-10-14 14:03:49 -04:00
case BO_And:
2009-06-02 13:58:47 -04:00
return &getValue( V1 & V2 );
2009-10-14 14:03:49 -04:00
case BO_Or:
2009-06-02 13:58:47 -04:00
return &getValue( V1 | V2 );
2009-10-14 14:03:49 -04:00
case BO_Xor:
2009-06-02 13:58:47 -04:00
return &getValue( V1 ^ V2 );
}
}
const std::pair<SVal, uintptr_t>&
BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) {
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Lazily create the folding set.
if (!PersistentSVals) PersistentSVals = new PersistentSValsTy();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
llvm::FoldingSetNodeID ID;
void *InsertPos;
2009-06-02 13:58:47 -04:00
V.Profile(ID);
ID.AddPointer((void*) Data);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
typedef llvm::FoldingSetNodeWrapper<SValData> FoldNodeTy;
FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
2009-10-14 14:03:49 -04:00
if (!P) {
2009-06-02 13:58:47 -04:00
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
new (P) FoldNodeTy(std::make_pair(V, Data));
Map.InsertNode(P, InsertPos);
}
return P->getValue();
}
const std::pair<SVal, SVal>&
BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) {
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Lazily create the folding set.
if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
llvm::FoldingSetNodeID ID;
void *InsertPos;
2009-06-02 13:58:47 -04:00
V1.Profile(ID);
V2.Profile(ID);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
typedef llvm::FoldingSetNodeWrapper<SValPair> FoldNodeTy;
FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
2009-10-14 14:03:49 -04:00
if (!P) {
2009-06-02 13:58:47 -04:00
P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
new (P) FoldNodeTy(std::make_pair(V1, V2));
Map.InsertNode(P, InsertPos);
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
return P->getValue();
}
const SVal* BasicValueFactory::getPersistentSVal(SVal X) {
return &getPersistentSValWithData(X, 0).first;
2009-10-14 14:03:49 -04:00
}