2011-10-20 17:14:49 -04:00
|
|
|
//== ProgramState.h - Path-sensitive "State" for tracking values -*- C++ -*--=//
|
2009-06-02 13:58:47 -04:00
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
2012-12-02 08:20:44 -05:00
|
|
|
// This file defines the state of the program along the analysisa path.
|
2009-06-02 13:58:47 -04:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
#ifndef LLVM_CLANG_GR_VALUESTATE_H
|
|
|
|
|
#define LLVM_CLANG_GR_VALUESTATE_H
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
#include "clang/Basic/LLVM.h"
|
2011-02-20 08:06:31 -05:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ConstraintManager.h"
|
2012-12-02 08:20:44 -05:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeInfo.h"
|
2011-02-20 08:06:31 -05:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/Environment.h"
|
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
|
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
|
2012-04-14 10:01:31 -04:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
|
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/TaintTag.h"
|
2011-02-20 08:06:31 -05:00
|
|
|
#include "llvm/ADT/PointerIntPair.h"
|
2009-06-02 13:58:47 -04:00
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
|
|
|
|
|
2010-04-02 04:55:10 -04:00
|
|
|
namespace llvm {
|
|
|
|
|
class APSInt;
|
|
|
|
|
class BumpPtrAllocator;
|
|
|
|
|
}
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2010-04-02 04:55:10 -04:00
|
|
|
namespace clang {
|
|
|
|
|
class ASTContext;
|
2011-02-20 08:06:31 -05:00
|
|
|
|
|
|
|
|
namespace ento {
|
|
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
class CallEvent;
|
|
|
|
|
class CallEventManager;
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
typedef ConstraintManager* (*ConstraintManagerCreator)(ProgramStateManager&,
|
2012-12-02 08:20:44 -05:00
|
|
|
SubEngine*);
|
2011-10-20 17:14:49 -04:00
|
|
|
typedef StoreManager* (*StoreManagerCreator)(ProgramStateManager&);
|
2009-06-02 13:58:47 -04:00
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-10-20 17:14:49 -04:00
|
|
|
// ProgramStateTrait - Traits used by the Generic Data Map of a ProgramState.
|
2009-06-02 13:58:47 -04:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
template <typename T> struct ProgramStatePartialTrait;
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
template <typename T> struct ProgramStateTrait {
|
2009-06-02 13:58:47 -04:00
|
|
|
typedef typename T::data_type data_type;
|
2011-10-20 17:14:49 -04:00
|
|
|
static inline void *MakeVoidPtr(data_type D) { return (void*) D; }
|
|
|
|
|
static inline data_type MakeData(void *const* P) {
|
2009-06-02 13:58:47 -04:00
|
|
|
return P ? (data_type) *P : (data_type) 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
/// \class ProgramState
|
|
|
|
|
/// ProgramState - This class encapsulates:
|
2011-02-20 08:06:31 -05:00
|
|
|
///
|
|
|
|
|
/// 1. A mapping from expressions to values (Environment)
|
|
|
|
|
/// 2. A mapping from locations to values (Store)
|
|
|
|
|
/// 3. Constraints on symbolic values (GenericDataMap)
|
|
|
|
|
///
|
|
|
|
|
/// Together these represent the "abstract state" of a program.
|
|
|
|
|
///
|
2011-10-20 17:14:49 -04:00
|
|
|
/// ProgramState is intended to be used as a functional object; that is,
|
2011-02-20 08:06:31 -05:00
|
|
|
/// once it is created and made "persistent" in a FoldingSet, its
|
|
|
|
|
/// values will never change.
|
2011-10-20 17:14:49 -04:00
|
|
|
class ProgramState : public llvm::FoldingSetNode {
|
2009-10-14 14:03:49 -04:00
|
|
|
public:
|
2009-06-02 13:58:47 -04:00
|
|
|
typedef llvm::ImmutableSet<llvm::APSInt*> IntSetTy;
|
2009-10-14 14:03:49 -04:00
|
|
|
typedef llvm::ImmutableMap<void*, void*> GenericDataMap;
|
|
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
private:
|
2012-12-02 08:20:44 -05:00
|
|
|
void operator=(const ProgramState& R) LLVM_DELETED_FUNCTION;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
friend class ProgramStateManager;
|
2011-02-20 08:06:31 -05:00
|
|
|
friend class ExplodedGraph;
|
|
|
|
|
friend class ExplodedNode;
|
2009-06-22 04:08:35 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
ProgramStateManager *stateMgr;
|
2011-02-20 08:06:31 -05:00
|
|
|
Environment Env; // Maps a Stmt to its current SVal.
|
|
|
|
|
Store store; // Maps a location to its current value.
|
|
|
|
|
GenericDataMap GDM; // Custom data stored by a client of this class.
|
|
|
|
|
unsigned refCount;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
/// makeWithStore - Return a ProgramState with the same values as the current
|
2010-09-17 11:54:40 -04:00
|
|
|
/// state with the exception of using the specified Store.
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef makeWithStore(const StoreRef &store) const;
|
2011-02-20 08:06:31 -05:00
|
|
|
|
|
|
|
|
void setStore(const StoreRef &storeRef);
|
2010-09-17 11:54:40 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
public:
|
2011-10-20 17:14:49 -04:00
|
|
|
/// This ctor is used when creating the first ProgramState object.
|
|
|
|
|
ProgramState(ProgramStateManager *mgr, const Environment& env,
|
2011-02-20 08:06:31 -05:00
|
|
|
StoreRef st, GenericDataMap gdm);
|
|
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
/// Copy ctor - We must explicitly define this or else the "Next" ptr
|
|
|
|
|
/// in FoldingSetNode will also get copied.
|
2011-10-20 17:14:49 -04:00
|
|
|
ProgramState(const ProgramState &RHS);
|
2011-02-20 08:06:31 -05:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
~ProgramState();
|
2011-02-20 08:06:31 -05:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
/// Return the ProgramStateManager associated with this state.
|
2012-12-02 08:20:44 -05:00
|
|
|
ProgramStateManager &getStateManager() const {
|
|
|
|
|
return *stateMgr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Return the ConstraintManager.
|
|
|
|
|
ConstraintManager &getConstraintManager() const;
|
2011-02-20 08:06:31 -05:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
/// getEnvironment - Return the environment associated with this state.
|
|
|
|
|
/// The environment is the mapping from expressions to values.
|
|
|
|
|
const Environment& getEnvironment() const { return Env; }
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
/// Return the store associated with this state. The store
|
2009-06-02 13:58:47 -04:00
|
|
|
/// is a mapping from locations to values.
|
2011-02-20 08:06:31 -05:00
|
|
|
Store getStore() const { return store; }
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
/// getGDM - Return the generic data map associated with this state.
|
|
|
|
|
GenericDataMap getGDM() const { return GDM; }
|
2009-10-14 14:03:49 -04:00
|
|
|
|
|
|
|
|
void setGDM(GenericDataMap gdm) { GDM = gdm; }
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
/// Profile - Profile the contents of a ProgramState object for use in a
|
|
|
|
|
/// FoldingSet. Two ProgramState objects are considered equal if they
|
2011-02-20 08:06:31 -05:00
|
|
|
/// have the same Environment, Store, and GenericDataMap.
|
2011-10-20 17:14:49 -04:00
|
|
|
static void Profile(llvm::FoldingSetNodeID& ID, const ProgramState *V) {
|
2009-06-02 13:58:47 -04:00
|
|
|
V->Env.Profile(ID);
|
2011-02-20 08:06:31 -05:00
|
|
|
ID.AddPointer(V->store);
|
2009-06-02 13:58:47 -04:00
|
|
|
V->GDM.Profile(ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Profile - Used to profile the contents of this object for inclusion
|
|
|
|
|
/// in a FoldingSet.
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
|
Profile(ID, this);
|
|
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
BasicValueFactory &getBasicVals() const;
|
|
|
|
|
SymbolManager &getSymbolManager() const;
|
2010-01-01 05:34:51 -05:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
//==---------------------------------------------------------------------==//
|
|
|
|
|
// Constraints on values.
|
|
|
|
|
//==---------------------------------------------------------------------==//
|
|
|
|
|
//
|
2011-10-20 17:14:49 -04:00
|
|
|
// Each ProgramState records constraints on symbolic values. These constraints
|
|
|
|
|
// are managed using the ConstraintManager associated with a ProgramStateManager.
|
2009-06-22 04:08:35 -04:00
|
|
|
// As constraints gradually accrue on symbolic values, added constraints
|
|
|
|
|
// may conflict and indicate that a state is infeasible (as no real values
|
|
|
|
|
// could satisfy all the constraints). This is the principal mechanism
|
2011-10-20 17:14:49 -04:00
|
|
|
// for modeling path-sensitivity in ExprEngine/ProgramState.
|
2009-06-22 04:08:35 -04:00
|
|
|
//
|
2011-02-20 08:06:31 -05:00
|
|
|
// Various "assume" methods form the interface for adding constraints to
|
|
|
|
|
// symbolic values. A call to 'assume' indicates an assumption being placed
|
|
|
|
|
// on one or symbolic values. 'assume' methods take the following inputs:
|
2009-06-22 04:08:35 -04:00
|
|
|
//
|
2011-10-20 17:14:49 -04:00
|
|
|
// (1) A ProgramState object representing the current state.
|
2009-06-22 04:08:35 -04:00
|
|
|
//
|
2011-02-20 08:06:31 -05:00
|
|
|
// (2) The assumed constraint (which is specific to a given "assume" method).
|
2009-06-22 04:08:35 -04:00
|
|
|
//
|
|
|
|
|
// (3) A binary value "Assumption" that indicates whether the constraint is
|
|
|
|
|
// assumed to be true or false.
|
|
|
|
|
//
|
2011-10-20 17:14:49 -04:00
|
|
|
// The output of "assume*" is a new ProgramState object with the added constraints.
|
2011-02-20 08:06:31 -05:00
|
|
|
// If no new state is feasible, NULL is returned.
|
2009-06-22 04:08:35 -04:00
|
|
|
//
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef assume(DefinedOrUnknownSVal cond, bool assumption) const;
|
2011-02-20 08:06:31 -05:00
|
|
|
|
|
|
|
|
/// This method assumes both "true" and "false" for 'cond', and
|
|
|
|
|
/// returns both corresponding states. It's shorthand for doing
|
|
|
|
|
/// 'assume' twice.
|
2012-04-14 10:01:31 -04:00
|
|
|
std::pair<ProgramStateRef , ProgramStateRef >
|
2011-02-20 08:06:31 -05:00
|
|
|
assume(DefinedOrUnknownSVal cond) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef assumeInBound(DefinedOrUnknownSVal idx,
|
2009-10-14 14:03:49 -04:00
|
|
|
DefinedOrUnknownSVal upperBound,
|
2012-04-14 10:01:31 -04:00
|
|
|
bool assumption,
|
|
|
|
|
QualType IndexType = QualType()) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
/// Utility method for getting regions.
|
2009-10-14 14:03:49 -04:00
|
|
|
const VarRegion* getRegion(const VarDecl *D, const LocationContext *LC) const;
|
2009-06-27 06:45:02 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
//==---------------------------------------------------------------------==//
|
|
|
|
|
// Binding and retrieving values to/from the environment and symbolic store.
|
|
|
|
|
//==---------------------------------------------------------------------==//
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-12-02 08:20:44 -05:00
|
|
|
/// \brief Create a new state with the specified CompoundLiteral binding.
|
|
|
|
|
/// \param CL the compound literal expression (the binding key)
|
|
|
|
|
/// \param LC the LocationContext of the binding
|
|
|
|
|
/// \param V the value to bind.
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef bindCompoundLiteral(const CompoundLiteralExpr *CL,
|
2012-12-02 08:20:44 -05:00
|
|
|
const LocationContext *LC,
|
|
|
|
|
SVal V) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2010-09-17 11:54:40 -04:00
|
|
|
/// Create a new state by binding the value 'V' to the statement 'S' in the
|
|
|
|
|
/// state's environment.
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef BindExpr(const Stmt *S, const LocationContext *LCtx,
|
|
|
|
|
SVal V, bool Invalidate = true) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2010-09-17 11:54:40 -04:00
|
|
|
/// Create a new state by binding the value 'V' and location 'locaton' to the
|
|
|
|
|
/// statement 'S' in the state's environment.
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef bindExprAndLocation(const Stmt *S,
|
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
|
SVal location, SVal V) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-12-02 08:20:44 -05:00
|
|
|
ProgramStateRef bindLoc(Loc location,
|
|
|
|
|
SVal V,
|
|
|
|
|
bool notifyChanges = true) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef bindLoc(SVal location, SVal V) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef bindDefault(SVal loc, SVal V) const;
|
2010-07-13 13:21:42 -04:00
|
|
|
|
2012-12-02 08:20:44 -05:00
|
|
|
ProgramStateRef killBinding(Loc LV) const;
|
2010-09-17 11:54:40 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
/// invalidateRegions - Returns the state with bindings for the given regions
|
2010-09-17 11:54:40 -04:00
|
|
|
/// cleared from the store. The regions are provided as a continuous array
|
|
|
|
|
/// from Begin to End. Optionally invalidates global regions as well.
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef invalidateRegions(ArrayRef<const MemRegion *> Regions,
|
|
|
|
|
const Expr *E, unsigned BlockCount,
|
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
|
StoreManager::InvalidatedSymbols *IS = 0,
|
2012-08-15 16:02:54 -04:00
|
|
|
const CallEvent *Call = 0) const;
|
2010-09-17 11:54:40 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
/// enterStackFrame - Returns the state for entry to the given stack frame,
|
2010-09-17 11:54:40 -04:00
|
|
|
/// preserving the current state.
|
2012-08-15 16:02:54 -04:00
|
|
|
ProgramStateRef enterStackFrame(const CallEvent &Call,
|
|
|
|
|
const StackFrameContext *CalleeCtx) const;
|
2010-09-17 11:54:40 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
/// Get the lvalue for a variable reference.
|
2010-07-13 13:21:42 -04:00
|
|
|
Loc getLValue(const VarDecl *D, const LocationContext *LC) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2010-07-13 13:21:42 -04:00
|
|
|
Loc getLValue(const CompoundLiteralExpr *literal,
|
|
|
|
|
const LocationContext *LC) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
/// Get the lvalue for an ivar reference.
|
|
|
|
|
SVal getLValue(const ObjCIvarDecl *decl, SVal base) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
/// Get the lvalue for a field reference.
|
2009-10-14 14:03:49 -04:00
|
|
|
SVal getLValue(const FieldDecl *decl, SVal Base) const;
|
|
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// Get the lvalue for an indirect field reference.
|
|
|
|
|
SVal getLValue(const IndirectFieldDecl *decl, SVal Base) const;
|
|
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
/// Get the lvalue for an array index.
|
2009-10-14 14:03:49 -04:00
|
|
|
SVal getLValue(QualType ElementType, SVal Idx, SVal Base) const;
|
|
|
|
|
|
2010-09-17 11:54:40 -04:00
|
|
|
/// Returns the SVal bound to the statement 'S' in the state's environment.
|
2012-12-02 08:20:44 -05:00
|
|
|
SVal getSVal(const Stmt *S, const LocationContext *LCtx) const;
|
2010-09-17 11:54:40 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
SVal getSValAsScalarOrLoc(const Stmt *Ex, const LocationContext *LCtx) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
/// \brief Return the value bound to the specified location.
|
|
|
|
|
/// Returns UnknownVal() if none found.
|
2009-06-22 04:08:35 -04:00
|
|
|
SVal getSVal(Loc LV, QualType T = QualType()) const;
|
2011-02-20 08:06:31 -05:00
|
|
|
|
|
|
|
|
/// Returns the "raw" SVal bound to LV before any value simplfication.
|
|
|
|
|
SVal getRawSVal(Loc LV, QualType T= QualType()) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
/// \brief Return the value bound to the specified location.
|
|
|
|
|
/// Returns UnknownVal() if none found.
|
2009-06-22 04:08:35 -04:00
|
|
|
SVal getSVal(const MemRegion* R) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
SVal getSValAsScalarOrLoc(const MemRegion *R) const;
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
/// \brief Visits the symbols reachable from the given SVal using the provided
|
|
|
|
|
/// SymbolVisitor.
|
|
|
|
|
///
|
|
|
|
|
/// This is a convenience API. Consider using ScanReachableSymbols class
|
|
|
|
|
/// directly when making multiple scans on the same state with the same
|
|
|
|
|
/// visitor to avoid repeated initialization cost.
|
|
|
|
|
/// \sa ScanReachableSymbols
|
2009-06-22 04:08:35 -04:00
|
|
|
bool scanReachableSymbols(SVal val, SymbolVisitor& visitor) const;
|
2009-12-01 06:08:04 -05:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
/// \brief Visits the symbols reachable from the SVals in the given range
|
|
|
|
|
/// using the provided SymbolVisitor.
|
2009-12-01 06:08:04 -05:00
|
|
|
bool scanReachableSymbols(const SVal *I, const SVal *E,
|
|
|
|
|
SymbolVisitor &visitor) const;
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
/// \brief Visits the symbols reachable from the regions in the given
|
|
|
|
|
/// MemRegions range using the provided SymbolVisitor.
|
2009-12-01 06:08:04 -05:00
|
|
|
bool scanReachableSymbols(const MemRegion * const *I,
|
|
|
|
|
const MemRegion * const *E,
|
|
|
|
|
SymbolVisitor &visitor) const;
|
2009-06-22 04:08:35 -04:00
|
|
|
|
|
|
|
|
template <typename CB> CB scanReachableSymbols(SVal val) const;
|
2009-12-01 06:08:04 -05:00
|
|
|
template <typename CB> CB scanReachableSymbols(const SVal *beg,
|
|
|
|
|
const SVal *end) const;
|
|
|
|
|
|
|
|
|
|
template <typename CB> CB
|
|
|
|
|
scanReachableSymbols(const MemRegion * const *beg,
|
|
|
|
|
const MemRegion * const *end) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
/// Create a new state in which the statement is marked as tainted.
|
|
|
|
|
ProgramStateRef addTaint(const Stmt *S, const LocationContext *LCtx,
|
|
|
|
|
TaintTagType Kind = TaintTagGeneric) const;
|
|
|
|
|
|
|
|
|
|
/// Create a new state in which the symbol is marked as tainted.
|
|
|
|
|
ProgramStateRef addTaint(SymbolRef S,
|
|
|
|
|
TaintTagType Kind = TaintTagGeneric) const;
|
|
|
|
|
|
|
|
|
|
/// Create a new state in which the region symbol is marked as tainted.
|
|
|
|
|
ProgramStateRef addTaint(const MemRegion *R,
|
|
|
|
|
TaintTagType Kind = TaintTagGeneric) const;
|
|
|
|
|
|
|
|
|
|
/// Check if the statement is tainted in the current state.
|
|
|
|
|
bool isTainted(const Stmt *S, const LocationContext *LCtx,
|
|
|
|
|
TaintTagType Kind = TaintTagGeneric) const;
|
|
|
|
|
bool isTainted(SVal V, TaintTagType Kind = TaintTagGeneric) const;
|
|
|
|
|
bool isTainted(SymbolRef Sym, TaintTagType Kind = TaintTagGeneric) const;
|
|
|
|
|
bool isTainted(const MemRegion *Reg, TaintTagType Kind=TaintTagGeneric) const;
|
|
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// \brief Get dynamic type information for a region.
|
|
|
|
|
DynamicTypeInfo getDynamicTypeInfo(const MemRegion *Reg) const;
|
|
|
|
|
|
|
|
|
|
/// \brief Set dynamic type information of the region; return the new state.
|
|
|
|
|
ProgramStateRef setDynamicTypeInfo(const MemRegion *Reg,
|
|
|
|
|
DynamicTypeInfo NewTy) const;
|
|
|
|
|
|
|
|
|
|
/// \brief Set dynamic type information of the region; return the new state.
|
|
|
|
|
ProgramStateRef setDynamicTypeInfo(const MemRegion *Reg,
|
|
|
|
|
QualType NewTy,
|
|
|
|
|
bool CanBeSubClassed = true) const {
|
|
|
|
|
return setDynamicTypeInfo(Reg, DynamicTypeInfo(NewTy, CanBeSubClassed));
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
//==---------------------------------------------------------------------==//
|
|
|
|
|
// Accessing the Generic Data Map (GDM).
|
|
|
|
|
//==---------------------------------------------------------------------==//
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
void *const* FindGDM(void *K) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef add(typename ProgramStateTrait<T>::key_type K) const;
|
2009-06-22 04:08:35 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
template <typename T>
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::data_type
|
2009-06-02 13:58:47 -04:00
|
|
|
get() const {
|
2011-10-20 17:14:49 -04:00
|
|
|
return ProgramStateTrait<T>::MakeData(FindGDM(ProgramStateTrait<T>::GDMIndex()));
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
template<typename T>
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::lookup_type
|
|
|
|
|
get(typename ProgramStateTrait<T>::key_type key) const {
|
|
|
|
|
void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex());
|
|
|
|
|
return ProgramStateTrait<T>::Lookup(ProgramStateTrait<T>::MakeData(d), key);
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template <typename T>
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::context_type get_context() const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
|
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K) const;
|
2009-06-22 04:08:35 -04:00
|
|
|
|
|
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef remove(typename ProgramStateTrait<T>::key_type K,
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::context_type C) const;
|
2010-04-02 04:55:10 -04:00
|
|
|
template <typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef remove() const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef set(typename ProgramStateTrait<T>::data_type D) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef set(typename ProgramStateTrait<T>::key_type K,
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::value_type E) const;
|
2009-06-22 04:08:35 -04:00
|
|
|
|
|
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef set(typename ProgramStateTrait<T>::key_type K,
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::value_type E,
|
|
|
|
|
typename ProgramStateTrait<T>::context_type C) const;
|
2009-06-22 04:08:35 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
template<typename T>
|
2011-10-20 17:14:49 -04:00
|
|
|
bool contains(typename ProgramStateTrait<T>::key_type key) const {
|
|
|
|
|
void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex());
|
|
|
|
|
return ProgramStateTrait<T>::Contains(ProgramStateTrait<T>::MakeData(d), key);
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
// Pretty-printing.
|
2012-04-14 10:01:31 -04:00
|
|
|
void print(raw_ostream &Out, const char *nl = "\n",
|
2009-10-14 14:03:49 -04:00
|
|
|
const char *sep = "") const;
|
2012-04-14 10:01:31 -04:00
|
|
|
void printDOT(raw_ostream &Out) const;
|
|
|
|
|
void printTaint(raw_ostream &Out, const char *nl = "\n",
|
|
|
|
|
const char *sep = "") const;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
void dump() const;
|
|
|
|
|
void dumpTaint() const;
|
2011-02-20 08:06:31 -05:00
|
|
|
|
|
|
|
|
private:
|
2012-04-14 10:01:31 -04:00
|
|
|
friend void ProgramStateRetain(const ProgramState *state);
|
|
|
|
|
friend void ProgramStateRelease(const ProgramState *state);
|
2011-06-12 11:46:16 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef
|
2011-10-20 17:14:49 -04:00
|
|
|
invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions,
|
|
|
|
|
const Expr *E, unsigned BlockCount,
|
2012-04-14 10:01:31 -04:00
|
|
|
const LocationContext *LCtx,
|
2011-10-20 17:14:49 -04:00
|
|
|
StoreManager::InvalidatedSymbols &IS,
|
2012-08-15 16:02:54 -04:00
|
|
|
const CallEvent *Call) const;
|
2009-10-14 14:03:49 -04:00
|
|
|
};
|
|
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-10-20 17:14:49 -04:00
|
|
|
// ProgramStateManager - Factory object for ProgramStates.
|
2009-06-02 13:58:47 -04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
class ProgramStateManager {
|
|
|
|
|
friend class ProgramState;
|
2012-04-14 10:01:31 -04:00
|
|
|
friend void ProgramStateRelease(const ProgramState *state);
|
2009-06-02 13:58:47 -04:00
|
|
|
private:
|
2011-02-20 08:06:31 -05:00
|
|
|
/// Eng - The SubEngine that owns this state manager.
|
|
|
|
|
SubEngine *Eng; /* Can be null. */
|
2010-09-17 11:54:40 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
EnvironmentManager EnvMgr;
|
2012-04-14 10:01:31 -04:00
|
|
|
OwningPtr<StoreManager> StoreMgr;
|
|
|
|
|
OwningPtr<ConstraintManager> ConstraintMgr;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
ProgramState::GenericDataMap::Factory GDMFactory;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
typedef llvm::DenseMap<void*,std::pair<void*,void (*)(void*)> > GDMContextsTy;
|
|
|
|
|
GDMContextsTy GDMContexts;
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
/// StateSet - FoldingSet containing all the states created for analyzing
|
|
|
|
|
/// a particular function. This is used to unique states.
|
2011-10-20 17:14:49 -04:00
|
|
|
llvm::FoldingSet<ProgramState> StateSet;
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
/// Object that manages the data for all created SVals.
|
2012-04-14 10:01:31 -04:00
|
|
|
OwningPtr<SValBuilder> svalBuilder;
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
/// Manages memory for created CallEvents.
|
|
|
|
|
OwningPtr<CallEventManager> CallEventMgr;
|
|
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
/// A BumpPtrAllocator to allocate states.
|
2010-01-15 10:39:40 -05:00
|
|
|
llvm::BumpPtrAllocator &Alloc;
|
2011-02-20 08:06:31 -05:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
/// A vector of ProgramStates that we can reuse.
|
|
|
|
|
std::vector<ProgramState *> freeStates;
|
2011-02-20 08:06:31 -05:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
public:
|
2011-10-20 17:14:49 -04:00
|
|
|
ProgramStateManager(ASTContext &Ctx,
|
2009-06-02 13:58:47 -04:00
|
|
|
StoreManagerCreator CreateStoreManager,
|
|
|
|
|
ConstraintManagerCreator CreateConstraintManager,
|
2010-01-15 10:39:40 -05:00
|
|
|
llvm::BumpPtrAllocator& alloc,
|
2012-12-02 08:20:44 -05:00
|
|
|
SubEngine *subeng);
|
2011-02-20 08:06:31 -05:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
~ProgramStateManager();
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef getInitialState(const LocationContext *InitLoc);
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
ASTContext &getContext() { return svalBuilder->getContext(); }
|
|
|
|
|
const ASTContext &getContext() const { return svalBuilder->getContext(); }
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
BasicValueFactory &getBasicVals() {
|
2011-02-20 08:06:31 -05:00
|
|
|
return svalBuilder->getBasicValueFactory();
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
2011-02-20 08:06:31 -05:00
|
|
|
|
|
|
|
|
SValBuilder &getSValBuilder() {
|
|
|
|
|
return *svalBuilder;
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
SymbolManager &getSymbolManager() {
|
2011-02-20 08:06:31 -05:00
|
|
|
return svalBuilder->getSymbolManager();
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
|
|
|
|
const SymbolManager &getSymbolManager() const {
|
2011-02-20 08:06:31 -05:00
|
|
|
return svalBuilder->getSymbolManager();
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
llvm::BumpPtrAllocator& getAllocator() { return Alloc; }
|
|
|
|
|
|
|
|
|
|
MemRegionManager& getRegionManager() {
|
2011-02-20 08:06:31 -05:00
|
|
|
return svalBuilder->getRegionManager();
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
|
|
|
|
const MemRegionManager& getRegionManager() const {
|
2011-02-20 08:06:31 -05:00
|
|
|
return svalBuilder->getRegionManager();
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
CallEventManager &getCallEventManager() { return *CallEventMgr; }
|
|
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
StoreManager& getStoreManager() { return *StoreMgr; }
|
|
|
|
|
ConstraintManager& getConstraintManager() { return *ConstraintMgr; }
|
2011-02-20 08:06:31 -05:00
|
|
|
SubEngine* getOwningEngine() { return Eng; }
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef removeDeadBindings(ProgramStateRef St,
|
2010-03-21 06:50:08 -04:00
|
|
|
const StackFrameContext *LCtx,
|
2009-06-02 13:58:47 -04:00
|
|
|
SymbolReaper& SymReaper);
|
|
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
public:
|
|
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
SVal ArrayToPointer(Loc Array) {
|
|
|
|
|
return StoreMgr->ArrayToPointer(Array);
|
|
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
// Methods that manipulate the GDM.
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef addGDM(ProgramStateRef St, void *Key, void *Data);
|
|
|
|
|
ProgramStateRef removeGDM(ProgramStateRef state, void *Key);
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
// Methods that query & manipulate the Store.
|
|
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
void iterBindings(ProgramStateRef state, StoreManager::BindingsHandler& F) {
|
2009-06-02 13:58:47 -04:00
|
|
|
StoreMgr->iterBindings(state->getStore(), F);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef getPersistentState(ProgramState &Impl);
|
|
|
|
|
ProgramStateRef getPersistentStateWithGDM(ProgramStateRef FromState,
|
|
|
|
|
ProgramStateRef GDMState);
|
2011-10-20 17:14:49 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
bool haveEqualEnvironments(ProgramStateRef S1, ProgramStateRef S2) {
|
2011-10-20 17:14:49 -04:00
|
|
|
return S1->Env == S2->Env;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
bool haveEqualStores(ProgramStateRef S1, ProgramStateRef S2) {
|
2011-10-20 17:14:49 -04:00
|
|
|
return S1->store == S2->store;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
//==---------------------------------------------------------------------==//
|
|
|
|
|
// Generic Data Map methods.
|
|
|
|
|
//==---------------------------------------------------------------------==//
|
|
|
|
|
//
|
2011-10-20 17:14:49 -04:00
|
|
|
// ProgramStateManager and ProgramState support a "generic data map" that allows
|
|
|
|
|
// different clients of ProgramState objects to embed arbitrary data within a
|
|
|
|
|
// ProgramState object. The generic data map is essentially an immutable map
|
2009-06-02 13:58:47 -04:00
|
|
|
// from a "tag" (that acts as the "key" for a client) and opaque values.
|
|
|
|
|
// Tags/keys and values are simply void* values. The typical way that clients
|
|
|
|
|
// generate unique tags are by taking the address of a static variable.
|
|
|
|
|
// Clients are responsible for ensuring that data values referred to by a
|
|
|
|
|
// the data pointer are immutable (and thus are essentially purely functional
|
|
|
|
|
// data).
|
|
|
|
|
//
|
2011-10-20 17:14:49 -04:00
|
|
|
// The templated methods below use the ProgramStateTrait<T> class
|
2009-06-02 13:58:47 -04:00
|
|
|
// to resolve keys into the GDM and to return data values to clients.
|
|
|
|
|
//
|
2009-10-14 14:03:49 -04:00
|
|
|
|
|
|
|
|
// Trait based GDM dispatch.
|
2009-06-02 13:58:47 -04:00
|
|
|
template <typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef set(ProgramStateRef st, typename ProgramStateTrait<T>::data_type D) {
|
2011-10-20 17:14:49 -04:00
|
|
|
return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
|
|
|
|
|
ProgramStateTrait<T>::MakeVoidPtr(D));
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef set(ProgramStateRef st,
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::key_type K,
|
|
|
|
|
typename ProgramStateTrait<T>::value_type V,
|
|
|
|
|
typename ProgramStateTrait<T>::context_type C) {
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
|
|
|
|
|
ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Set(st->get<T>(), K, V, C)));
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef add(ProgramStateRef st,
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::key_type K,
|
|
|
|
|
typename ProgramStateTrait<T>::context_type C) {
|
|
|
|
|
return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
|
|
|
|
|
ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Add(st->get<T>(), K, C)));
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef remove(ProgramStateRef st,
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::key_type K,
|
|
|
|
|
typename ProgramStateTrait<T>::context_type C) {
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
|
|
|
|
|
ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Remove(st->get<T>(), K, C)));
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2010-04-02 04:55:10 -04:00
|
|
|
template <typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef remove(ProgramStateRef st) {
|
2011-10-20 17:14:49 -04:00
|
|
|
return removeGDM(st, ProgramStateTrait<T>::GDMIndex());
|
2010-04-02 04:55:10 -04:00
|
|
|
}
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
void *FindGDMContext(void *index,
|
|
|
|
|
void *(*CreateContext)(llvm::BumpPtrAllocator&),
|
2009-06-02 13:58:47 -04:00
|
|
|
void (*DeleteContext)(void*));
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
template <typename T>
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::context_type get_context() {
|
|
|
|
|
void *p = FindGDMContext(ProgramStateTrait<T>::GDMIndex(),
|
|
|
|
|
ProgramStateTrait<T>::CreateContext,
|
|
|
|
|
ProgramStateTrait<T>::DeleteContext);
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
return ProgramStateTrait<T>::MakeContext(p);
|
2009-06-02 13:58:47 -04:00
|
|
|
}
|
|
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
void EndPath(ProgramStateRef St) {
|
2009-06-02 13:58:47 -04:00
|
|
|
ConstraintMgr->EndPath(St);
|
|
|
|
|
}
|
|
|
|
|
};
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-10-20 17:14:49 -04:00
|
|
|
// Out-of-line method definitions for ProgramState.
|
2009-06-02 13:58:47 -04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2012-12-02 08:20:44 -05:00
|
|
|
inline ConstraintManager &ProgramState::getConstraintManager() const {
|
|
|
|
|
return stateMgr->getConstraintManager();
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
inline const VarRegion* ProgramState::getRegion(const VarDecl *D,
|
2012-04-14 10:01:31 -04:00
|
|
|
const LocationContext *LC) const
|
|
|
|
|
{
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().getRegionManager().getVarRegion(D, LC);
|
2009-06-27 06:45:02 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
inline ProgramStateRef ProgramState::assume(DefinedOrUnknownSVal Cond,
|
2009-10-14 14:03:49 -04:00
|
|
|
bool Assumption) const {
|
|
|
|
|
if (Cond.isUnknown())
|
|
|
|
|
return this;
|
2009-06-27 06:45:02 -04:00
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
return getStateManager().ConstraintMgr->assume(this, cast<DefinedSVal>(Cond),
|
2009-10-14 14:03:49 -04:00
|
|
|
Assumption);
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-11-18 09:59:57 -05:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
inline std::pair<ProgramStateRef , ProgramStateRef >
|
2011-10-20 17:14:49 -04:00
|
|
|
ProgramState::assume(DefinedOrUnknownSVal Cond) const {
|
2009-11-18 09:59:57 -05:00
|
|
|
if (Cond.isUnknown())
|
|
|
|
|
return std::make_pair(this, this);
|
|
|
|
|
|
2011-02-20 08:06:31 -05:00
|
|
|
return getStateManager().ConstraintMgr->assumeDual(this,
|
2009-11-18 09:59:57 -05:00
|
|
|
cast<DefinedSVal>(Cond));
|
|
|
|
|
}
|
2009-06-22 04:08:35 -04:00
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
inline ProgramStateRef ProgramState::bindLoc(SVal LV, SVal V) const {
|
2009-06-22 04:08:35 -04:00
|
|
|
return !isa<Loc>(LV) ? this : bindLoc(cast<Loc>(LV), V);
|
|
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
inline Loc ProgramState::getLValue(const VarDecl *VD,
|
2009-10-14 14:03:49 -04:00
|
|
|
const LocationContext *LC) const {
|
|
|
|
|
return getStateManager().StoreMgr->getLValueVar(VD, LC);
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
inline Loc ProgramState::getLValue(const CompoundLiteralExpr *literal,
|
2009-12-15 13:49:47 -05:00
|
|
|
const LocationContext *LC) const {
|
|
|
|
|
return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC);
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
inline SVal ProgramState::getLValue(const ObjCIvarDecl *D, SVal Base) const {
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().StoreMgr->getLValueIvar(D, Base);
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
inline SVal ProgramState::getLValue(const FieldDecl *D, SVal Base) const {
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().StoreMgr->getLValueField(D, Base);
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-08-15 16:02:54 -04:00
|
|
|
inline SVal ProgramState::getLValue(const IndirectFieldDecl *D,
|
|
|
|
|
SVal Base) const {
|
|
|
|
|
StoreManager &SM = *getStateManager().StoreMgr;
|
|
|
|
|
for (IndirectFieldDecl::chain_iterator I = D->chain_begin(),
|
|
|
|
|
E = D->chain_end();
|
|
|
|
|
I != E; ++I) {
|
|
|
|
|
Base = SM.getLValueField(cast<FieldDecl>(*I), Base);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Base;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
inline SVal ProgramState::getLValue(QualType ElementType, SVal Idx, SVal Base) const{
|
2011-02-20 08:06:31 -05:00
|
|
|
if (NonLoc *N = dyn_cast<NonLoc>(&Idx))
|
|
|
|
|
return getStateManager().StoreMgr->getLValueElement(ElementType, *N, Base);
|
|
|
|
|
return UnknownVal();
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2012-12-02 08:20:44 -05:00
|
|
|
inline SVal ProgramState::getSVal(const Stmt *Ex,
|
|
|
|
|
const LocationContext *LCtx) const{
|
2012-04-14 10:01:31 -04:00
|
|
|
return Env.getSVal(EnvironmentEntry(Ex, LCtx),
|
2012-12-02 08:20:44 -05:00
|
|
|
*getStateManager().svalBuilder);
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
|
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
inline SVal
|
|
|
|
|
ProgramState::getSValAsScalarOrLoc(const Stmt *S,
|
|
|
|
|
const LocationContext *LCtx) const {
|
2009-06-23 15:32:16 -04:00
|
|
|
if (const Expr *Ex = dyn_cast<Expr>(S)) {
|
|
|
|
|
QualType T = Ex->getType();
|
2012-08-15 16:02:54 -04:00
|
|
|
if (Ex->isGLValue() || Loc::isLocType(T) || T->isIntegerType())
|
2012-04-14 10:01:31 -04:00
|
|
|
return getSVal(S, LCtx);
|
2009-06-23 15:32:16 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-23 15:32:16 -04:00
|
|
|
return UnknownVal();
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
inline SVal ProgramState::getRawSVal(Loc LV, QualType T) const {
|
2012-04-14 10:01:31 -04:00
|
|
|
return getStateManager().StoreMgr->getBinding(getStore(), LV, T);
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
inline SVal ProgramState::getSVal(const MemRegion* R) const {
|
2012-04-14 10:01:31 -04:00
|
|
|
return getStateManager().StoreMgr->getBinding(getStore(),
|
|
|
|
|
loc::MemRegionVal(R));
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
inline BasicValueFactory &ProgramState::getBasicVals() const {
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().getBasicVals();
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
inline SymbolManager &ProgramState::getSymbolManager() const {
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().getSymbolManager();
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef ProgramState::add(typename ProgramStateTrait<T>::key_type K) const {
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().add<T>(this, K, get_context<T>());
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template <typename T>
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::context_type ProgramState::get_context() const {
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().get_context<T>();
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K) const {
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().remove<T>(this, K, get_context<T>());
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef ProgramState::remove(typename ProgramStateTrait<T>::key_type K,
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::context_type C) const {
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().remove<T>(this, K, C);
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2010-04-02 04:55:10 -04:00
|
|
|
template <typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef ProgramState::remove() const {
|
2010-04-02 04:55:10 -04:00
|
|
|
return getStateManager().remove<T>(this);
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::data_type D) const {
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().set<T>(this, D);
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K,
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::value_type E) const {
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().set<T>(this, K, E, get_context<T>());
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template<typename T>
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef ProgramState::set(typename ProgramStateTrait<T>::key_type K,
|
2011-10-20 17:14:49 -04:00
|
|
|
typename ProgramStateTrait<T>::value_type E,
|
|
|
|
|
typename ProgramStateTrait<T>::context_type C) const {
|
2009-10-14 14:03:49 -04:00
|
|
|
return getStateManager().set<T>(this, K, E, C);
|
2009-06-22 04:08:35 -04:00
|
|
|
}
|
2009-10-14 14:03:49 -04:00
|
|
|
|
2009-06-22 04:08:35 -04:00
|
|
|
template <typename CB>
|
2011-10-20 17:14:49 -04:00
|
|
|
CB ProgramState::scanReachableSymbols(SVal val) const {
|
2009-06-22 04:08:35 -04:00
|
|
|
CB cb(this);
|
|
|
|
|
scanReachableSymbols(val, cb);
|
|
|
|
|
return cb;
|
|
|
|
|
}
|
2009-12-01 06:08:04 -05:00
|
|
|
|
|
|
|
|
template <typename CB>
|
2011-10-20 17:14:49 -04:00
|
|
|
CB ProgramState::scanReachableSymbols(const SVal *beg, const SVal *end) const {
|
2009-12-01 06:08:04 -05:00
|
|
|
CB cb(this);
|
|
|
|
|
scanReachableSymbols(beg, end, cb);
|
|
|
|
|
return cb;
|
|
|
|
|
}
|
2009-06-02 13:58:47 -04:00
|
|
|
|
2009-12-01 06:08:04 -05:00
|
|
|
template <typename CB>
|
2011-10-20 17:14:49 -04:00
|
|
|
CB ProgramState::scanReachableSymbols(const MemRegion * const *beg,
|
2009-12-01 06:08:04 -05:00
|
|
|
const MemRegion * const *end) const {
|
|
|
|
|
CB cb(this);
|
|
|
|
|
scanReachableSymbols(beg, end, cb);
|
|
|
|
|
return cb;
|
|
|
|
|
}
|
2011-02-20 08:06:31 -05:00
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
/// \class ScanReachableSymbols
|
|
|
|
|
/// A Utility class that allows to visit the reachable symbols using a custom
|
|
|
|
|
/// SymbolVisitor.
|
2012-08-15 16:02:54 -04:00
|
|
|
class ScanReachableSymbols {
|
2011-10-20 17:14:49 -04:00
|
|
|
typedef llvm::DenseMap<const void*, unsigned> VisitedItems;
|
|
|
|
|
|
|
|
|
|
VisitedItems visited;
|
2012-04-14 10:01:31 -04:00
|
|
|
ProgramStateRef state;
|
2011-10-20 17:14:49 -04:00
|
|
|
SymbolVisitor &visitor;
|
|
|
|
|
public:
|
|
|
|
|
|
2012-04-14 10:01:31 -04:00
|
|
|
ScanReachableSymbols(ProgramStateRef st, SymbolVisitor& v)
|
2011-10-20 17:14:49 -04:00
|
|
|
: state(st), visitor(v) {}
|
|
|
|
|
|
|
|
|
|
bool scan(nonloc::CompoundVal val);
|
|
|
|
|
bool scan(SVal val);
|
|
|
|
|
bool scan(const MemRegion *R);
|
|
|
|
|
bool scan(const SymExpr *sym);
|
|
|
|
|
};
|
|
|
|
|
|
2012-12-02 08:20:44 -05:00
|
|
|
} // end ento namespace
|
2011-02-20 08:06:31 -05:00
|
|
|
|
2009-06-02 13:58:47 -04:00
|
|
|
} // end clang namespace
|
|
|
|
|
|
|
|
|
|
#endif
|