Static Value-Flow Analysis
RelExeState.h
Go to the documentation of this file.
1 //===- RelExeState.h ----Relation Execution States for Interval Domains-------//
2 //
3 // SVF: Static Value-Flow Analysis
4 //
5 // Copyright (C) <2013-2022> <Yulei Sui>
6 //
7 
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
17 
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //
21 //===----------------------------------------------------------------------===//
22 /*
23  * RelExeState.h
24  *
25  * Created on: Aug 15, 2022
26  * Author: Jiawei Ren, Xiao Cheng
27  *
28  */
29 
30 #ifndef Z3_EXAMPLE_RELEXESTATE_H
31 #define Z3_EXAMPLE_RELEXESTATE_H
32 
33 #include "AE/Core/AddressValue.h"
34 #include "Util/Z3Expr.h"
35 
36 namespace SVF
37 {
38 
40 {
41  friend class SVFIR2AbsState;
42 
43 public:
46 
47 protected:
50 
51 public:
52  RelExeState() = default;
53 
54  RelExeState(VarToValMap &varToVal, AddrToValMap&locToVal) : _varToVal(varToVal), _addrToVal(locToVal) {}
55 
57  {
58 
59  }
60 
61  virtual ~RelExeState() = default;
62 
63  RelExeState &operator=(const RelExeState &rhs);
64 
65  RelExeState(RelExeState &&rhs) noexcept: _varToVal(std::move(rhs._varToVal)),
66  _addrToVal(std::move(rhs._addrToVal))
67  {
68 
69  }
70 
72  {
73  if (&rhs != this)
74  {
75  _varToVal = std::move(rhs._varToVal);
76  _addrToVal = std::move(rhs._addrToVal);
77  }
78  return *this;
79  }
80 
82  bool operator==(const RelExeState &rhs) const;
83 
85  inline bool operator!=(const RelExeState &rhs) const
86  {
87  return !(*this == rhs);
88  }
89 
91  bool operator<(const RelExeState &rhs) const;
92 
93 
94  static z3::context &getContext()
95  {
96  return Z3Expr::getContext();
97  }
98 
99  const VarToValMap &getVarToVal() const
100  {
101  return _varToVal;
102  }
103 
104  const AddrToValMap&getLocToVal() const
105  {
106  return _addrToVal;
107  }
108 
109  inline Z3Expr &operator[](u32_t varId)
110  {
111  return getZ3Expr(varId);
112  }
113 
114  u32_t hash() const
115  {
116  size_t h = getVarToVal().size() * 2;
118  for (const auto &t: getVarToVal())
119  {
120  h ^= hf(t.first) + 0x9e3779b9 + (h << 6) + (h >> 2);
121  h ^= hf(t.second.id()) + 0x9e3779b9 + (h << 6) + (h >> 2);
122  }
123 
124  size_t h2 = getVarToVal().size() * 2;
125 
126  for (const auto &t: getLocToVal())
127  {
128  h2 ^= hf(t.first) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);
129  h2 ^= hf(t.second.id()) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);
130  }
132 
133  return pairH(std::make_pair(h, h2));
134  }
135 
137  inline bool existsVar(u32_t varId) const
138  {
139  return _varToVal.count(varId);
140  }
141 
143  virtual inline Z3Expr &getZ3Expr(u32_t varId)
144  {
145  return _varToVal[varId];
146  }
147 
149  virtual inline Z3Expr toZ3Expr(u32_t varId) const
150  {
151  return getContext().int_const(std::to_string(varId).c_str());
152  }
153 
155  void extractSubVars(const Z3Expr &expr, Set<u32_t> &res);
156 
158  void extractCmpVars(const Z3Expr &expr, Set<u32_t> &res);
159 
161  Z3Expr buildRelZ3Expr(u32_t cmp, s32_t succ, Set<u32_t> &vars, Set<u32_t> &initVars);
162 
164  void store(const Z3Expr &loc, const Z3Expr &value);
165 
167  Z3Expr &load(const Z3Expr &loc);
168 
170  static inline u32_t getVirtualMemAddress(u32_t idx)
171  {
173  }
174 
176  static inline bool isVirtualMemAddress(u32_t val)
177  {
178  if (val == 0)
179  assert(false && "val cannot be 0");
181  }
182 
184  static inline u32_t getInternalID(u32_t idx)
185  {
186  return AddressValue::getInternalID(idx);
187  }
188 
190  static inline s32_t z3Expr2NumValue(const Z3Expr &e)
191  {
192  assert(e.is_numeral() && "not numeral?");
193  return e.get_numeral_int64();
194  }
195 
197  void printExprValues();
198 
199 private:
200  bool eqVarToValMap(const VarToValMap &lhs, const VarToValMap &rhs) const;
201 
202  bool lessThanVarToValMap(const VarToValMap &lhs, const VarToValMap &rhs) const;
203 
204 protected:
205  inline void store(u32_t objId, const Z3Expr &z3Expr)
206  {
207  _addrToVal[objId] = z3Expr.simplify();
208  }
209 
210  inline Z3Expr &load(u32_t objId)
211  {
212  return _addrToVal[objId];
213  }
214 }; // end class RelExeState
215 } // end namespace SVF
216 
217 template<>
218 struct std::hash<SVF::RelExeState>
219 {
220  size_t operator()(const SVF::RelExeState &exeState) const
221  {
222  return exeState.hash();
223  }
224 };
225 
226 #endif //Z3_EXAMPLE_RELEXESTATE_H
static u32_t getInternalID(u32_t idx)
Return the internal index if idx is an address otherwise return the value of idx.
Definition: AddressValue.h:226
static u32_t getVirtualMemAddress(u32_t idx)
The physical address starts with 0x7f...... + idx.
Definition: AddressValue.h:212
static bool isVirtualMemAddress(u32_t val)
Check bit value of val start with 0x7F000000, filter by 0xFF000000.
Definition: AddressValue.h:220
VarToValMap AddrToValMap
Definition: RelExeState.h:45
u32_t hash() const
Definition: RelExeState.h:114
RelExeState(RelExeState &&rhs) noexcept
Definition: RelExeState.h:65
bool lessThanVarToValMap(const VarToValMap &lhs, const VarToValMap &rhs) const
RelExeState & operator=(RelExeState &&rhs) noexcept
Definition: RelExeState.h:71
static u32_t getVirtualMemAddress(u32_t idx)
The physical address starts with 0x7f...... + idx.
Definition: RelExeState.h:170
friend class SVFIR2AbsState
Definition: RelExeState.h:41
void store(u32_t objId, const Z3Expr &z3Expr)
Definition: RelExeState.h:205
void extractSubVars(const Z3Expr &expr, Set< u32_t > &res)
Extract sub SVFVar IDs of a Z3Expr.
Definition: RelExeState.cpp:45
RelExeState()=default
void printExprValues()
Print values of all expressions.
void store(const Z3Expr &loc, const Z3Expr &value)
Store value to location.
RelExeState(const RelExeState &rhs)
Definition: RelExeState.h:56
Z3Expr & load(u32_t objId)
Definition: RelExeState.h:210
RelExeState & operator=(const RelExeState &rhs)
virtual Z3Expr toZ3Expr(u32_t varId) const
Return Z3 expression lazily based on SVFVar ID.
Definition: RelExeState.h:149
bool existsVar(u32_t varId) const
Return true if map has varId.
Definition: RelExeState.h:137
const AddrToValMap & getLocToVal() const
Definition: RelExeState.h:104
virtual ~RelExeState()=default
Z3Expr & operator[](u32_t varId)
Definition: RelExeState.h:109
const VarToValMap & getVarToVal() const
Definition: RelExeState.h:99
RelExeState(VarToValMap &varToVal, AddrToValMap &locToVal)
Definition: RelExeState.h:54
static z3::context & getContext()
Definition: RelExeState.h:94
Z3Expr & load(const Z3Expr &loc)
Load value at location.
bool eqVarToValMap(const VarToValMap &lhs, const VarToValMap &rhs) const
static u32_t getInternalID(u32_t idx)
Return the internal index if idx is an address otherwise return the value of idx.
Definition: RelExeState.h:184
VarToValMap _varToVal
Definition: RelExeState.h:48
Z3Expr buildRelZ3Expr(u32_t cmp, s32_t succ, Set< u32_t > &vars, Set< u32_t > &initVars)
Build relational Z3Expr.
Definition: RelExeState.cpp:90
bool operator==(const RelExeState &rhs) const
Overloading Operator==.
bool operator<(const RelExeState &rhs) const
Overloading Operator==.
static s32_t z3Expr2NumValue(const Z3Expr &e)
Return int value from an expression if it is a numeral, otherwise return an approximate value.
Definition: RelExeState.h:190
virtual Z3Expr & getZ3Expr(u32_t varId)
Return Z3 expression eagerly based on SVFVar ID.
Definition: RelExeState.h:143
AddrToValMap _addrToVal
Definition: RelExeState.h:49
static bool isVirtualMemAddress(u32_t val)
Check bit value of val start with 0x7F000000, filter by 0xFF000000.
Definition: RelExeState.h:176
bool operator!=(const RelExeState &rhs) const
Overloading Operator!=.
Definition: RelExeState.h:85
void extractCmpVars(const Z3Expr &expr, Set< u32_t > &res)
Extract all related SVFVar IDs based on compare expr.
Definition: RelExeState.cpp:66
Map< u32_t, Z3Expr > VarToValMap
Definition: RelExeState.h:44
static z3::context & getContext()
Get z3 context, singleton design here to make sure we only have one context.
Definition: Z3Expr.cpp:66
bool is_numeral() const
Definition: Z3Expr.h:127
int64_t get_numeral_int64() const
Definition: Z3Expr.h:142
Z3Expr simplify() const
Definition: Z3Expr.h:137
constexpr std::remove_reference< T >::type && move(T &&t) noexcept
Definition: SVFUtil.h:447
for isBitcode
Definition: BasicTypes.h:68
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: GeneralType.h:101
signed s32_t
Definition: GeneralType.h:47
unsigned u32_t
Definition: GeneralType.h:46
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: GeneralType.h:96
provide extra hash function for std::pair handling
Definition: GeneralType.h:85
size_t operator()(const SVF::RelExeState &exeState) const
Definition: RelExeState.h:220