ProgramState.h revision 5eca482fe895ea57bc82410222e6426c09e63284
1e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o//== ProgramState.h - Path-sensitive "State" for tracking values -*- C++ -*--=//
2e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o//
3e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o//                     The LLVM Compiler Infrastructure
4e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o//
550b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o// This file is distributed under the University of Illinois Open Source
6e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o// License. See LICENSE.TXT for details.
7e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o//
8e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o//===----------------------------------------------------------------------===//
9e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o//
10e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o//  This file defines SymbolRef, ExprBindKey, and ProgramState*.
11e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o//
12e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o//===----------------------------------------------------------------------===//
13b697f9d01c5f07842426f7d8e918bf3110028662Theodore Ts'o
14e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o#ifndef LLVM_CLANG_GR_VALUESTATE_H
15e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o#define LLVM_CLANG_GR_VALUESTATE_H
16e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
17e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o#include "clang/Basic/LLVM.h"
187a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o#include "clang/StaticAnalyzer/Core/PathSensitive/ConstraintManager.h"
19e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o#include "clang/StaticAnalyzer/Core/PathSensitive/Environment.h"
2050b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
21e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
227a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o#include "clang/StaticAnalyzer/Core/PathSensitive/TaintTag.h"
23e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o#include "llvm/ADT/PointerIntPair.h"
247a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o#include "llvm/ADT/FoldingSet.h"
25e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o#include "llvm/ADT/ImmutableMap.h"
26e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
27e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'onamespace llvm {
28e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'oclass APSInt;
29e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'oclass BumpPtrAllocator;
30e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o}
31e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
32e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'onamespace clang {
3378c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'oclass ASTContext;
3478c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'o
3578c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'onamespace ento {
3678c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'o
3778c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'oclass CallOrObjCMessage;
3878c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'oclass ProgramStateManager;
3978c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'otypedef ConstraintManager* (*ConstraintManagerCreator)(ProgramStateManager&,
4078c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'o                                                       SubEngine&);
4178c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'otypedef StoreManager* (*StoreManagerCreator)(ProgramStateManager&);
4278c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'o
4378c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'o//===----------------------------------------------------------------------===//
4478c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'o// ProgramStateTrait - Traits used by the Generic Data Map of a ProgramState.
457a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o//===----------------------------------------------------------------------===//
46e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
47e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'otemplate <typename T> struct ProgramStatePartialTrait;
48e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
49e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'otemplate <typename T> struct ProgramStateTrait {
50f0a22d0fd3ec3f45b562af5afba8811f72b94a28Theodore Ts'o  typedef typename T::data_type data_type;
5150b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  static inline void *GDMIndex() { return &T::TagInt; }
5278c7d0efae91cde1a992f69c1a0157b39e971670Theodore Ts'o  static inline void *MakeVoidPtr(data_type D) { return (void*) D; }
53e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  static inline data_type MakeData(void *const* P) {
54e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o    return P ? (data_type) *P : (data_type) 0;
55e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  }
56e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o};
5745e338f5332a54295893dba2e32cc093d1316f60Jim Meyering
5845e338f5332a54295893dba2e32cc093d1316f60Jim Meyeringclass ProgramStateManager;
59e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
60e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o/// \class ProgramState
61e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o/// ProgramState - This class encapsulates:
62e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o///
63e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o///    1. A mapping from expressions to values (Environment)
6476b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o///    2. A mapping from locations to values (Store)
65e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o///    3. Constraints on symbolic values (GenericDataMap)
66e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o///
6750b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o///  Together these represent the "abstract state" of a program.
68e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o///
69e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o///  ProgramState is intended to be used as a functional object; that is,
70e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o///  once it is created and made "persistent" in a FoldingSet, its
7150b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o///  values will never change.
72e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'oclass ProgramState : public llvm::FoldingSetNode {
73e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'opublic:
74e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  typedef llvm::ImmutableSet<llvm::APSInt*>                IntSetTy;
7576b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o  typedef llvm::ImmutableMap<void*, void*>                 GenericDataMap;
7676b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o
77e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'oprivate:
7850b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  void operator=(const ProgramState& R) const; // Do not implement.
7950b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o
80e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  friend class ProgramStateManager;
8150b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  friend class ExplodedGraph;
82e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  friend class ExplodedNode;
83e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
84efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o  ProgramStateManager *stateMgr;
85c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  Environment Env;           // Maps a Stmt to its current SVal.
86c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  Store store;               // Maps a location to its current value.
87c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  GenericDataMap   GDM;      // Custom data stored by a client of this class.
88c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  unsigned refCount;
89d90be5b1437b839e5f1afcee7073798d833e4534Theodore Ts'o
90c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  /// makeWithStore - Return a ProgramState with the same values as the current
91c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  ///  state with the exception of using the specified Store.
92c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  const ProgramState *makeWithStore(const StoreRef &store) const;
93c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
94d90be5b1437b839e5f1afcee7073798d833e4534Theodore Ts'o  void setStore(const StoreRef &storeRef);
95c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
96c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'opublic:
97c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
98c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  /// This ctor is used when creating the first ProgramState object.
99c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  ProgramState(ProgramStateManager *mgr, const Environment& env,
100e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o          StoreRef st, GenericDataMap gdm);
101e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
102e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  /// Copy ctor - We must explicitly define this or else the "Next" ptr
103e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  ///  in FoldingSetNode will also get copied.
10450b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  ProgramState(const ProgramState &RHS);
105e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
10650b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  ~ProgramState();
107e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
108e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  /// Return the ProgramStateManager associated with this state.
10976b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o  ProgramStateManager &getStateManager() const { return *stateMgr; }
110e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
111e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  /// Return true if this state is referenced by a persistent ExplodedNode.
112e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  bool referencedByExplodedNode() const { return refCount > 0; }
11350b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o
11476b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o  /// getEnvironment - Return the environment associated with this state.
115f0a22d0fd3ec3f45b562af5afba8811f72b94a28Theodore Ts'o  ///  The environment is the mapping from expressions to values.
116f0a22d0fd3ec3f45b562af5afba8811f72b94a28Theodore Ts'o  const Environment& getEnvironment() const { return Env; }
117e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
118e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  /// Return the store associated with this state.  The store
119e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  ///  is a mapping from locations to values.
120e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  Store getStore() const { return store; }
121e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
122e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
123e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  /// getGDM - Return the generic data map associated with this state.
124e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  GenericDataMap getGDM() const { return GDM; }
12550b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o
126efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o  void setGDM(GenericDataMap gdm) { GDM = gdm; }
12779dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o
128e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  /// Profile - Profile the contents of a ProgramState object for use in a
12950b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  ///  FoldingSet.  Two ProgramState objects are considered equal if they
13079dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  ///  have the same Environment, Store, and GenericDataMap.
131e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  static void Profile(llvm::FoldingSetNodeID& ID, const ProgramState *V) {
13250b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o    V->Env.Profile(ID);
13350b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o    ID.AddPointer(V->store);
134ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o    V->GDM.Profile(ID);
135e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  }
13650b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o
137e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  /// Profile - Used to profile the contents of this object for inclusion
138e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  ///  in a FoldingSet.
1397e89304878045b35c811201b04d3efd38916dbd1Theodore Ts'o  void Profile(llvm::FoldingSetNodeID& ID) const {
14079dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o    Profile(ID, this);
141ba5e38494e6b42d77f76d9ad18744388958eca7cTheodore Ts'o  }
142efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
143ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  BasicValueFactory &getBasicVals() const;
144ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  SymbolManager &getSymbolManager() const;
145ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o
146ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  //==---------------------------------------------------------------------==//
147ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  // Constraints on values.
148ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  //==---------------------------------------------------------------------==//
149ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  //
150ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  // Each ProgramState records constraints on symbolic values.  These constraints
151ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  // are managed using the ConstraintManager associated with a ProgramStateManager.
152ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  // As constraints gradually accrue on symbolic values, added constraints
153ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  // may conflict and indicate that a state is infeasible (as no real values
15450b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  // could satisfy all the constraints).  This is the principal mechanism
15550b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  // for modeling path-sensitivity in ExprEngine/ProgramState.
15679dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  //
15750b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  // Various "assume" methods form the interface for adding constraints to
15879dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  // symbolic values.  A call to 'assume' indicates an assumption being placed
15950b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  // on one or symbolic values.  'assume' methods take the following inputs:
16050b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  //
16150b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  //  (1) A ProgramState object representing the current state.
16250b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  //
16350b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  //  (2) The assumed constraint (which is specific to a given "assume" method).
16479dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  //
16579dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  //  (3) A binary value "Assumption" that indicates whether the constraint is
16679dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  //      assumed to be true or false.
16779dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  //
16879dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  // The output of "assume*" is a new ProgramState object with the added constraints.
16979dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  // If no new state is feasible, NULL is returned.
17079dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  //
17179dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o
17279dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  const ProgramState *assume(DefinedOrUnknownSVal cond, bool assumption) const;
17379dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o
17479dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  /// This method assumes both "true" and "false" for 'cond', and
175efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o  ///  returns both corresponding states.  It's shorthand for doing
17679dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  ///  'assume' twice.
17779dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  std::pair<const ProgramState*, const ProgramState*>
17879dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  assume(DefinedOrUnknownSVal cond) const;
17979dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o
18079dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  const ProgramState *assumeInBound(DefinedOrUnknownSVal idx,
18179dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o                               DefinedOrUnknownSVal upperBound,
18279dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o                               bool assumption) const;
18379dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o
18479dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  /// Utility method for getting regions.
18579dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  const VarRegion* getRegion(const VarDecl *D, const LocationContext *LC) const;
18679dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o
18779dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  //==---------------------------------------------------------------------==//
18879dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  // Binding and retrieving values to/from the environment and symbolic store.
18979dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  //==---------------------------------------------------------------------==//
19079dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o
19179dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  /// BindCompoundLiteral - Return the state that has the bindings currently
19279dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  ///  in this state plus the bindings for the CompoundLiteral.
19350b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  const ProgramState *bindCompoundLiteral(const CompoundLiteralExpr *CL,
194e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o                                     const LocationContext *LC,
195efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o                                     SVal V) const;
1966fb57a92d19696c123d02d89cfa1f1e6703dfc32Theodore Ts'o
197ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  /// Create a new state by binding the value 'V' to the statement 'S' in the
198ac7dd696d60a2cb6d27c2c4855fefc7b26a26d72Theodore Ts'o  /// state's environment.
199efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o  const ProgramState *BindExpr(const Stmt *S, const LocationContext *LCtx,
20050b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o                               SVal V, bool Invalidate = true) const;
20150b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o
202e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  /// Create a new state by binding the value 'V' and location 'locaton' to the
20350b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  /// statement 'S' in the state's environment.
20450b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  const ProgramState *bindExprAndLocation(const Stmt *S,
20550b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o                                          const LocationContext *LCtx,
20650b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o                                          SVal location, SVal V) const;
20745e338f5332a54295893dba2e32cc093d1316f60Jim Meyering
20850b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  const ProgramState *bindDecl(const VarRegion *VR, SVal V) const;
20950b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o
21050b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  const ProgramState *bindDeclWithNoInit(const VarRegion *VR) const;
211e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
212e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  const ProgramState *bindLoc(Loc location, SVal V) const;
21350b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o
214e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  const ProgramState *bindLoc(SVal location, SVal V) const;
2157a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
216e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  const ProgramState *bindDefault(SVal loc, SVal V) const;
217e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
21850b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  const ProgramState *unbindLoc(Loc LV) const;
2197a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2207a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// invalidateRegions - Returns the state with bindings for the given regions
2217a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  ///  cleared from the store. The regions are provided as a continuous array
2227a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  ///  from Begin to End. Optionally invalidates global regions as well.
223e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  const ProgramState *invalidateRegions(ArrayRef<const MemRegion *> Regions,
2247a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o                               const Expr *E, unsigned BlockCount,
225e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o                               StoreManager::InvalidatedSymbols *IS = 0,
226e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o                               const CallOrObjCMessage *Call = 0) const;
227e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
228f0a22d0fd3ec3f45b562af5afba8811f72b94a28Theodore Ts'o  /// enterStackFrame - Returns the state for entry to the given stack frame,
2297a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  ///  preserving the current state.
230e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  const ProgramState *enterStackFrame(const StackFrameContext *frame) const;
2317a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
232e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  /// Get the lvalue for a variable reference.
23350b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  Loc getLValue(const VarDecl *D, const LocationContext *LC) const;
2347a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2357a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// Get the lvalue for a StringLiteral.
236e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  Loc getLValue(const StringLiteral *literal) const;
237e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o
238e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  Loc getLValue(const CompoundLiteralExpr *literal,
239e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o                const LocationContext *LC) const;
2407a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2417a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// Get the lvalue for an ivar reference.
242e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  SVal getLValue(const ObjCIvarDecl *decl, SVal base) const;
2437a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
24450b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  /// Get the lvalue for a field reference.
2457a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  SVal getLValue(const FieldDecl *decl, SVal Base) const;
2467a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
247e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  /// Get the lvalue for an array index.
2487a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  SVal getLValue(QualType ElementType, SVal Idx, SVal Base) const;
2497a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
250e12f2ae74c2eb8997bf13adf8fdd7e7313971eaeTheodore Ts'o  const llvm::APSInt *getSymVal(SymbolRef sym) const;
2517a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2527a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// Returns the SVal bound to the statement 'S' in the state's environment.
2537a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  SVal getSVal(const Stmt *S, const LocationContext *LCtx,
25450b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o               bool useOnlyDirectBindings = false) const;
2557a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2567a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  SVal getSValAsScalarOrLoc(const Stmt *Ex, const LocationContext *LCtx) const;
2577a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2587a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  SVal getSVal(Loc LV, QualType T = QualType()) const;
2597a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2607a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// Returns the "raw" SVal bound to LV before any value simplfication.
2617a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  SVal getRawSVal(Loc LV, QualType T= QualType()) const;
2627a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2637a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  SVal getSVal(const MemRegion* R) const;
2647a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2657a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  SVal getSValAsScalarOrLoc(const MemRegion *R) const;
2667a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2677a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// \brief Visits the symbols reachable from the given SVal using the provided
2687a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// SymbolVisitor.
2697a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  ///
2707a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// This is a convenience API. Consider using ScanReachableSymbols class
2717a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// directly when making multiple scans on the same state with the same
2727a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// visitor to avoid repeated initialization cost.
2737a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// \sa ScanReachableSymbols
274efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o  bool scanReachableSymbols(SVal val, SymbolVisitor& visitor) const;
2757a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2767a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// \brief Visits the symbols reachable from the SVals in the given range
2777a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// using the provided SymbolVisitor.
2787a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  bool scanReachableSymbols(const SVal *I, const SVal *E,
2797a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o                            SymbolVisitor &visitor) const;
2807a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2817a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// \brief Visits the symbols reachable from the regions in the given
2827a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// MemRegions range using the provided SymbolVisitor.
2837a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  bool scanReachableSymbols(const MemRegion * const *I,
2847a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o                            const MemRegion * const *E,
2857a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o                            SymbolVisitor &visitor) const;
2867a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2877a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  template <typename CB> CB scanReachableSymbols(SVal val) const;
2887a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  template <typename CB> CB scanReachableSymbols(const SVal *beg,
2897a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o                                                 const SVal *end) const;
2907a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2917a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  template <typename CB> CB
2927a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  scanReachableSymbols(const MemRegion * const *beg,
2937a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o                       const MemRegion * const *end) const;
2947a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2957a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// Create a new state in which the statement is marked as tainted.
2967a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  const ProgramState* addTaint(const Stmt *S, const LocationContext *LCtx,
2977a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o                               TaintTagType Kind = TaintTagGeneric) const;
2987a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
2997a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// Create a new state in which the symbol is marked as tainted.
3007a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  const ProgramState* addTaint(SymbolRef S,
301efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o                               TaintTagType Kind = TaintTagGeneric) const;
3027a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
3037a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// Create a new state in which the region symbol is marked as tainted.
3047a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  const ProgramState* addTaint(const MemRegion *R,
3057a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o                               TaintTagType Kind = TaintTagGeneric) const;
3067a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
3077a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  /// Check if the statement is tainted in the current state.
3087a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  bool isTainted(const Stmt *S, const LocationContext *LCtx,
3097a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o                 TaintTagType Kind = TaintTagGeneric) const;
3107a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  bool isTainted(SVal V, TaintTagType Kind = TaintTagGeneric) const;
3117a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  bool isTainted(const SymExpr* Sym, TaintTagType Kind = TaintTagGeneric) const;
3127a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  bool isTainted(const MemRegion *Reg, TaintTagType Kind=TaintTagGeneric) const;
3137a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
3147a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  //==---------------------------------------------------------------------==//
3157a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  // Accessing the Generic Data Map (GDM).
3167a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  //==---------------------------------------------------------------------==//
3177a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
3187a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  void *const* FindGDM(void *K) const;
3197a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
3207a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  template<typename T>
3217a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  const ProgramState *add(typename ProgramStateTrait<T>::key_type K) const;
3227a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
3237a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  template <typename T>
324ce72b862c59da24ba16b354d687549276a24f908Theodore Ts'o  typename ProgramStateTrait<T>::data_type
325ce72b862c59da24ba16b354d687549276a24f908Theodore Ts'o  get() const {
326ce72b862c59da24ba16b354d687549276a24f908Theodore Ts'o    return ProgramStateTrait<T>::MakeData(FindGDM(ProgramStateTrait<T>::GDMIndex()));
3277a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  }
3287a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o
3297a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  template<typename T>
3307a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  typename ProgramStateTrait<T>::lookup_type
3317a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o  get(typename ProgramStateTrait<T>::key_type key) const {
33279dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o    void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex());
333ce72b862c59da24ba16b354d687549276a24f908Theodore Ts'o    return ProgramStateTrait<T>::Lookup(ProgramStateTrait<T>::MakeData(d), key);
334ce72b862c59da24ba16b354d687549276a24f908Theodore Ts'o  }
33576b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o
336ed6acfa337ca74912079b85196cf1263f6daf1a2Theodore Ts'o  template <typename T>
33776b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o  typename ProgramStateTrait<T>::context_type get_context() const;
33876b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o
33976b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o
34076b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o  template<typename T>
34179dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  const ProgramState *remove(typename ProgramStateTrait<T>::key_type K) const;
342efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
343f0a22d0fd3ec3f45b562af5afba8811f72b94a28Theodore Ts'o  template<typename T>
344efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o  const ProgramState *remove(typename ProgramStateTrait<T>::key_type K,
34576b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o                        typename ProgramStateTrait<T>::context_type C) const;
346ce72b862c59da24ba16b354d687549276a24f908Theodore Ts'o  template <typename T>
347f0a22d0fd3ec3f45b562af5afba8811f72b94a28Theodore Ts'o  const ProgramState *remove() const;
348ce72b862c59da24ba16b354d687549276a24f908Theodore Ts'o
34976b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o  template<typename T>
35050b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  const ProgramState *set(typename ProgramStateTrait<T>::data_type D) const;
35150b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o
352efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o  template<typename T>
35350b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o  const ProgramState *set(typename ProgramStateTrait<T>::key_type K,
35476b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o                     typename ProgramStateTrait<T>::value_type E) const;
355ce72b862c59da24ba16b354d687549276a24f908Theodore Ts'o
356b697f9d01c5f07842426f7d8e918bf3110028662Theodore Ts'o  template<typename T>
357b697f9d01c5f07842426f7d8e918bf3110028662Theodore Ts'o  const ProgramState *set(typename ProgramStateTrait<T>::key_type K,
35879dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o                     typename ProgramStateTrait<T>::value_type E,
359ce72b862c59da24ba16b354d687549276a24f908Theodore Ts'o                     typename ProgramStateTrait<T>::context_type C) const;
36050b380b4d4ab668bad45033e3a8aaf93c7f42844Theodore Ts'o
36176b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o  template<typename T>
36276b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o  bool contains(typename ProgramStateTrait<T>::key_type key) const {
36379dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o    void *const* d = FindGDM(ProgramStateTrait<T>::GDMIndex());
36418d12963335b04a402d097af1d714e8708805adaTheodore Ts'o    return ProgramStateTrait<T>::Contains(ProgramStateTrait<T>::MakeData(d), key);
365b697f9d01c5f07842426f7d8e918bf3110028662Theodore Ts'o  }
36679dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o
36779dd234a799434b6dc8365c49e743f00eb09d2fdTheodore Ts'o  // Pretty-printing.
36876b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o  void print(raw_ostream &Out, const char *nl = "\n",
369ed6acfa337ca74912079b85196cf1263f6daf1a2Theodore Ts'o             const char *sep = "") const;
370ed6acfa337ca74912079b85196cf1263f6daf1a2Theodore Ts'o
371ed6acfa337ca74912079b85196cf1263f6daf1a2Theodore Ts'o  void printDOT(raw_ostream &Out) const;
372ed6acfa337ca74912079b85196cf1263f6daf1a2Theodore Ts'o
373ed6acfa337ca74912079b85196cf1263f6daf1a2Theodore Ts'o  void dump() const;
374ed6acfa337ca74912079b85196cf1263f6daf1a2Theodore Ts'o
375ed6acfa337ca74912079b85196cf1263f6daf1a2Theodore Ts'oprivate:
376f0a22d0fd3ec3f45b562af5afba8811f72b94a28Theodore Ts'o  /// Increments the number of times this state is referenced by ExplodeNodes.
377849c6102507c93ca96efa68c0e575514e7560467Theodore Ts'o  void incrementReferenceCount() { ++refCount; }
378849c6102507c93ca96efa68c0e575514e7560467Theodore Ts'o
37976b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o  /// Decrement the number of times this state is referenced by ExplodeNodes.
38076b07bb1bc9cbcb70a94cb235954eaac993920adTheodore Ts'o  void decrementReferenceCount() {
381ce72b862c59da24ba16b354d687549276a24f908Theodore Ts'o    assert(refCount > 0);
3827a603aa89fcffb8798eca34ca3858db6f0393046Theodore Ts'o    --refCount;
383c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  }
384c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
385c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  const ProgramState *
386c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  invalidateRegionsImpl(ArrayRef<const MemRegion *> Regions,
387c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o                        const Expr *E, unsigned BlockCount,
388c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o                        StoreManager::InvalidatedSymbols &IS,
389c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o                        const CallOrObjCMessage *Call) const;
390c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o};
391c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
392c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'oclass ProgramStateSet {
393c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  typedef llvm::SmallPtrSet<const ProgramState*,5> ImplTy;
394c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  ImplTy Impl;
395efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'opublic:
396c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  ProgramStateSet() {}
39712a829dcdc57fb8ddc4887b07b40136288b6e7feMatthias Andree
398c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  inline void Add(const ProgramState *St) {
399c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o    Impl.insert(St);
400c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  }
401c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
402c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  typedef ImplTy::const_iterator iterator;
403c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
404c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  inline unsigned size() const { return Impl.size();  }
405c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  inline bool empty()    const { return Impl.empty(); }
406c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
407c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  inline iterator begin() const { return Impl.begin(); }
408c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  inline iterator end() const { return Impl.end();   }
409c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
410c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  class AutoPopulate {
411c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o    ProgramStateSet &S;
412c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o    unsigned StartSize;
413c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o    const ProgramState *St;
414c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  public:
415c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o    AutoPopulate(ProgramStateSet &s, const ProgramState *st)
416c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o      : S(s), StartSize(S.size()), St(st) {}
417c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
418c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o    ~AutoPopulate() {
419c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o      if (StartSize == S.size())
420c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o        S.Add(St);
421c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o    }
422c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  };
423efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o};
424c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
425c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o//===----------------------------------------------------------------------===//
426c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o// ProgramStateManager - Factory object for ProgramStates.
427c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o//===----------------------------------------------------------------------===//
428c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
429c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'oclass ProgramStateManager {
430c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  friend class ProgramState;
431c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'oprivate:
432c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  /// Eng - The SubEngine that owns this state manager.
433c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  SubEngine *Eng; /* Can be null. */
434c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
435c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  EnvironmentManager                   EnvMgr;
436c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  llvm::OwningPtr<StoreManager>        StoreMgr;
437c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  llvm::OwningPtr<ConstraintManager>   ConstraintMgr;
438c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
439c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  ProgramState::GenericDataMap::Factory     GDMFactory;
440c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
441c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  typedef llvm::DenseMap<void*,std::pair<void*,void (*)(void*)> > GDMContextsTy;
442c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  GDMContextsTy GDMContexts;
443c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
444c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  /// StateSet - FoldingSet containing all the states created for analyzing
445c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  ///  a particular function.  This is used to unique states.
446c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  llvm::FoldingSet<ProgramState> StateSet;
447c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
448efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o  /// Object that manages the data for all created SVals.
449438961210d6b3231deb934009447867b4c1570f3Theodore Ts'o  llvm::OwningPtr<SValBuilder> svalBuilder;
450c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
451c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  /// A BumpPtrAllocator to allocate states.
452c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  llvm::BumpPtrAllocator &Alloc;
453c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
454c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  /// A vector of recently allocated ProgramStates that can potentially be
455efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o  /// reused.
456c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  std::vector<ProgramState *> recentlyAllocatedStates;
457c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
458c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  /// A vector of ProgramStates that we can reuse.
459c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  std::vector<ProgramState *> freeStates;
460c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o
461c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'opublic:
462c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o  ProgramStateManager(ASTContext &Ctx,
463c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o                 StoreManagerCreator CreateStoreManager,
464c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o                 ConstraintManagerCreator CreateConstraintManager,
465c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o                 llvm::BumpPtrAllocator& alloc,
466c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o                 SubEngine &subeng)
467c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o    : Eng(&subeng),
468c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o      EnvMgr(alloc),
469c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o      GDMFactory(alloc),
470c37543df2f58f454c29fece5997bd0e9d6ee7533Theodore Ts'o      svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
471      Alloc(alloc) {
472    StoreMgr.reset((*CreateStoreManager)(*this));
473    ConstraintMgr.reset((*CreateConstraintManager)(*this, subeng));
474  }
475
476  ProgramStateManager(ASTContext &Ctx,
477                 StoreManagerCreator CreateStoreManager,
478                 ConstraintManager* ConstraintManagerPtr,
479                 llvm::BumpPtrAllocator& alloc)
480    : Eng(0),
481      EnvMgr(alloc),
482      GDMFactory(alloc),
483      svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
484      Alloc(alloc) {
485    StoreMgr.reset((*CreateStoreManager)(*this));
486    ConstraintMgr.reset(ConstraintManagerPtr);
487  }
488
489  ~ProgramStateManager();
490
491  const ProgramState *getInitialState(const LocationContext *InitLoc);
492
493  ASTContext &getContext() { return svalBuilder->getContext(); }
494  const ASTContext &getContext() const { return svalBuilder->getContext(); }
495
496  BasicValueFactory &getBasicVals() {
497    return svalBuilder->getBasicValueFactory();
498  }
499  const BasicValueFactory& getBasicVals() const {
500    return svalBuilder->getBasicValueFactory();
501  }
502
503  SValBuilder &getSValBuilder() {
504    return *svalBuilder;
505  }
506
507  SymbolManager &getSymbolManager() {
508    return svalBuilder->getSymbolManager();
509  }
510  const SymbolManager &getSymbolManager() const {
511    return svalBuilder->getSymbolManager();
512  }
513
514  llvm::BumpPtrAllocator& getAllocator() { return Alloc; }
515
516  MemRegionManager& getRegionManager() {
517    return svalBuilder->getRegionManager();
518  }
519  const MemRegionManager& getRegionManager() const {
520    return svalBuilder->getRegionManager();
521  }
522
523  StoreManager& getStoreManager() { return *StoreMgr; }
524  ConstraintManager& getConstraintManager() { return *ConstraintMgr; }
525  SubEngine* getOwningEngine() { return Eng; }
526
527  const ProgramState *removeDeadBindings(const ProgramState *St,
528                                    const StackFrameContext *LCtx,
529                                    SymbolReaper& SymReaper);
530
531  /// Marshal a new state for the callee in another translation unit.
532  /// 'state' is owned by the caller's engine.
533  const ProgramState *MarshalState(const ProgramState *state, const StackFrameContext *L);
534
535public:
536
537  SVal ArrayToPointer(Loc Array) {
538    return StoreMgr->ArrayToPointer(Array);
539  }
540
541  // Methods that manipulate the GDM.
542  const ProgramState *addGDM(const ProgramState *St, void *Key, void *Data);
543  const ProgramState *removeGDM(const ProgramState *state, void *Key);
544
545  // Methods that query & manipulate the Store.
546
547  void iterBindings(const ProgramState *state, StoreManager::BindingsHandler& F) {
548    StoreMgr->iterBindings(state->getStore(), F);
549  }
550
551  const ProgramState *getPersistentState(ProgramState &Impl);
552  const ProgramState *getPersistentStateWithGDM(const ProgramState *FromState,
553                                           const ProgramState *GDMState);
554
555  bool haveEqualEnvironments(const ProgramState * S1, const ProgramState * S2) {
556    return S1->Env == S2->Env;
557  }
558
559  bool haveEqualStores(const ProgramState * S1, const ProgramState * S2) {
560    return S1->store == S2->store;
561  }
562
563  /// Periodically called by ExprEngine to recycle ProgramStates that were
564  /// created but never used for creating an ExplodedNode.
565  void recycleUnusedStates();
566
567  //==---------------------------------------------------------------------==//
568  // Generic Data Map methods.
569  //==---------------------------------------------------------------------==//
570  //
571  // ProgramStateManager and ProgramState support a "generic data map" that allows
572  // different clients of ProgramState objects to embed arbitrary data within a
573  // ProgramState object.  The generic data map is essentially an immutable map
574  // from a "tag" (that acts as the "key" for a client) and opaque values.
575  // Tags/keys and values are simply void* values.  The typical way that clients
576  // generate unique tags are by taking the address of a static variable.
577  // Clients are responsible for ensuring that data values referred to by a
578  // the data pointer are immutable (and thus are essentially purely functional
579  // data).
580  //
581  // The templated methods below use the ProgramStateTrait<T> class
582  // to resolve keys into the GDM and to return data values to clients.
583  //
584
585  // Trait based GDM dispatch.
586  template <typename T>
587  const ProgramState *set(const ProgramState *st, typename ProgramStateTrait<T>::data_type D) {
588    return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
589                  ProgramStateTrait<T>::MakeVoidPtr(D));
590  }
591
592  template<typename T>
593  const ProgramState *set(const ProgramState *st,
594                     typename ProgramStateTrait<T>::key_type K,
595                     typename ProgramStateTrait<T>::value_type V,
596                     typename ProgramStateTrait<T>::context_type C) {
597
598    return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
599     ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Set(st->get<T>(), K, V, C)));
600  }
601
602  template <typename T>
603  const ProgramState *add(const ProgramState *st,
604                     typename ProgramStateTrait<T>::key_type K,
605                     typename ProgramStateTrait<T>::context_type C) {
606    return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
607        ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Add(st->get<T>(), K, C)));
608  }
609
610  template <typename T>
611  const ProgramState *remove(const ProgramState *st,
612                        typename ProgramStateTrait<T>::key_type K,
613                        typename ProgramStateTrait<T>::context_type C) {
614
615    return addGDM(st, ProgramStateTrait<T>::GDMIndex(),
616     ProgramStateTrait<T>::MakeVoidPtr(ProgramStateTrait<T>::Remove(st->get<T>(), K, C)));
617  }
618
619  template <typename T>
620  const ProgramState *remove(const ProgramState *st) {
621    return removeGDM(st, ProgramStateTrait<T>::GDMIndex());
622  }
623
624  void *FindGDMContext(void *index,
625                       void *(*CreateContext)(llvm::BumpPtrAllocator&),
626                       void  (*DeleteContext)(void*));
627
628  template <typename T>
629  typename ProgramStateTrait<T>::context_type get_context() {
630    void *p = FindGDMContext(ProgramStateTrait<T>::GDMIndex(),
631                             ProgramStateTrait<T>::CreateContext,
632                             ProgramStateTrait<T>::DeleteContext);
633
634    return ProgramStateTrait<T>::MakeContext(p);
635  }
636
637  const llvm::APSInt* getSymVal(const ProgramState *St, SymbolRef sym) {
638    return ConstraintMgr->getSymVal(St, sym);
639  }
640
641  void EndPath(const ProgramState *St) {
642    ConstraintMgr->EndPath(St);
643  }
644};
645
646
647//===----------------------------------------------------------------------===//
648// Out-of-line method definitions for ProgramState.
649//===----------------------------------------------------------------------===//
650
651inline const VarRegion* ProgramState::getRegion(const VarDecl *D,
652                                                const LocationContext *LC) const
653{
654  return getStateManager().getRegionManager().getVarRegion(D, LC);
655}
656
657inline const ProgramState *ProgramState::assume(DefinedOrUnknownSVal Cond,
658                                      bool Assumption) const {
659  if (Cond.isUnknown())
660    return this;
661
662  return getStateManager().ConstraintMgr->assume(this, cast<DefinedSVal>(Cond),
663                                                 Assumption);
664}
665
666inline std::pair<const ProgramState*, const ProgramState*>
667ProgramState::assume(DefinedOrUnknownSVal Cond) const {
668  if (Cond.isUnknown())
669    return std::make_pair(this, this);
670
671  return getStateManager().ConstraintMgr->assumeDual(this,
672                                                     cast<DefinedSVal>(Cond));
673}
674
675inline const ProgramState *ProgramState::bindLoc(SVal LV, SVal V) const {
676  return !isa<Loc>(LV) ? this : bindLoc(cast<Loc>(LV), V);
677}
678
679inline Loc ProgramState::getLValue(const VarDecl *VD,
680                               const LocationContext *LC) const {
681  return getStateManager().StoreMgr->getLValueVar(VD, LC);
682}
683
684inline Loc ProgramState::getLValue(const StringLiteral *literal) const {
685  return getStateManager().StoreMgr->getLValueString(literal);
686}
687
688inline Loc ProgramState::getLValue(const CompoundLiteralExpr *literal,
689                               const LocationContext *LC) const {
690  return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC);
691}
692
693inline SVal ProgramState::getLValue(const ObjCIvarDecl *D, SVal Base) const {
694  return getStateManager().StoreMgr->getLValueIvar(D, Base);
695}
696
697inline SVal ProgramState::getLValue(const FieldDecl *D, SVal Base) const {
698  return getStateManager().StoreMgr->getLValueField(D, Base);
699}
700
701inline SVal ProgramState::getLValue(QualType ElementType, SVal Idx, SVal Base) const{
702  if (NonLoc *N = dyn_cast<NonLoc>(&Idx))
703    return getStateManager().StoreMgr->getLValueElement(ElementType, *N, Base);
704  return UnknownVal();
705}
706
707inline const llvm::APSInt *ProgramState::getSymVal(SymbolRef sym) const {
708  return getStateManager().getSymVal(this, sym);
709}
710
711inline SVal ProgramState::getSVal(const Stmt *Ex, const LocationContext *LCtx,
712                                  bool useOnlyDirectBindings) const{
713  return Env.getSVal(EnvironmentEntry(Ex, LCtx),
714                     *getStateManager().svalBuilder,
715                     useOnlyDirectBindings);
716}
717
718inline SVal
719ProgramState::getSValAsScalarOrLoc(const Stmt *S,
720                                   const LocationContext *LCtx) const {
721  if (const Expr *Ex = dyn_cast<Expr>(S)) {
722    QualType T = Ex->getType();
723    if (Ex->isLValue() || Loc::isLocType(T) || T->isIntegerType())
724      return getSVal(S, LCtx);
725  }
726
727  return UnknownVal();
728}
729
730inline SVal ProgramState::getRawSVal(Loc LV, QualType T) const {
731  return getStateManager().StoreMgr->Retrieve(getStore(), LV, T);
732}
733
734inline SVal ProgramState::getSVal(const MemRegion* R) const {
735  return getStateManager().StoreMgr->Retrieve(getStore(), loc::MemRegionVal(R));
736}
737
738inline BasicValueFactory &ProgramState::getBasicVals() const {
739  return getStateManager().getBasicVals();
740}
741
742inline SymbolManager &ProgramState::getSymbolManager() const {
743  return getStateManager().getSymbolManager();
744}
745
746template<typename T>
747const ProgramState *ProgramState::add(typename ProgramStateTrait<T>::key_type K) const {
748  return getStateManager().add<T>(this, K, get_context<T>());
749}
750
751template <typename T>
752typename ProgramStateTrait<T>::context_type ProgramState::get_context() const {
753  return getStateManager().get_context<T>();
754}
755
756template<typename T>
757const ProgramState *ProgramState::remove(typename ProgramStateTrait<T>::key_type K) const {
758  return getStateManager().remove<T>(this, K, get_context<T>());
759}
760
761template<typename T>
762const ProgramState *ProgramState::remove(typename ProgramStateTrait<T>::key_type K,
763                               typename ProgramStateTrait<T>::context_type C) const {
764  return getStateManager().remove<T>(this, K, C);
765}
766
767template <typename T>
768const ProgramState *ProgramState::remove() const {
769  return getStateManager().remove<T>(this);
770}
771
772template<typename T>
773const ProgramState *ProgramState::set(typename ProgramStateTrait<T>::data_type D) const {
774  return getStateManager().set<T>(this, D);
775}
776
777template<typename T>
778const ProgramState *ProgramState::set(typename ProgramStateTrait<T>::key_type K,
779                            typename ProgramStateTrait<T>::value_type E) const {
780  return getStateManager().set<T>(this, K, E, get_context<T>());
781}
782
783template<typename T>
784const ProgramState *ProgramState::set(typename ProgramStateTrait<T>::key_type K,
785                            typename ProgramStateTrait<T>::value_type E,
786                            typename ProgramStateTrait<T>::context_type C) const {
787  return getStateManager().set<T>(this, K, E, C);
788}
789
790template <typename CB>
791CB ProgramState::scanReachableSymbols(SVal val) const {
792  CB cb(this);
793  scanReachableSymbols(val, cb);
794  return cb;
795}
796
797template <typename CB>
798CB ProgramState::scanReachableSymbols(const SVal *beg, const SVal *end) const {
799  CB cb(this);
800  scanReachableSymbols(beg, end, cb);
801  return cb;
802}
803
804template <typename CB>
805CB ProgramState::scanReachableSymbols(const MemRegion * const *beg,
806                                 const MemRegion * const *end) const {
807  CB cb(this);
808  scanReachableSymbols(beg, end, cb);
809  return cb;
810}
811
812/// \class ScanReachableSymbols
813/// A Utility class that allows to visit the reachable symbols using a custom
814/// SymbolVisitor.
815class ScanReachableSymbols : public SubRegionMap::Visitor  {
816  virtual void anchor();
817  typedef llvm::DenseMap<const void*, unsigned> VisitedItems;
818
819  VisitedItems visited;
820  const ProgramState *state;
821  SymbolVisitor &visitor;
822  llvm::OwningPtr<SubRegionMap> SRM;
823public:
824
825  ScanReachableSymbols(const ProgramState *st, SymbolVisitor& v)
826    : state(st), visitor(v) {}
827
828  bool scan(nonloc::CompoundVal val);
829  bool scan(SVal val);
830  bool scan(const MemRegion *R);
831  bool scan(const SymExpr *sym);
832
833  // From SubRegionMap::Visitor.
834  bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) {
835    return scan(SubRegion);
836  }
837};
838
839} // end GR namespace
840
841} // end clang namespace
842
843#endif
844