Static Value-Flow Analysis
Loading...
Searching...
No Matches
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
34#include "Util/Z3Expr.h"
35#include "Util/GeneralType.h"
36
37namespace SVF
38{
39
41{
42 friend class SVFIR2AbsState;
43
44public:
47
48protected:
51
52public:
53 RelExeState() = default;
54
56
61
62 virtual ~RelExeState() = default;
63
65
66 RelExeState(RelExeState &&rhs) noexcept: _varToVal(std::move(rhs._varToVal)),
67 _addrToVal(std::move(rhs._addrToVal))
68 {
69
70 }
71
73 {
74 if (&rhs != this)
75 {
76 _varToVal = std::move(rhs._varToVal);
77 _addrToVal = std::move(rhs._addrToVal);
78 }
79 return *this;
80 }
81
83 bool operator==(const RelExeState &rhs) const;
84
86 inline bool operator!=(const RelExeState &rhs) const
87 {
88 return !(*this == rhs);
89 }
90
92 bool operator<(const RelExeState &rhs) const;
93
94
95 static z3::context &getContext()
96 {
97 return Z3Expr::getContext();
98 }
99
101 {
102 return _varToVal;
103 }
104
106 {
107 return _addrToVal;
108 }
109
111 {
112 return getZ3Expr(varId);
113 }
114
115 u32_t hash() const
116 {
117 size_t h = getVarToVal().size() * 2;
119 for (const auto &t: getVarToVal())
120 {
121 h ^= hf(t.first) + 0x9e3779b9 + (h << 6) + (h >> 2);
122 h ^= hf(t.second.id()) + 0x9e3779b9 + (h << 6) + (h >> 2);
123 }
124
125 size_t h2 = getVarToVal().size() * 2;
126
127 for (const auto &t: getLocToVal())
128 {
129 h2 ^= hf(t.first) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);
130 h2 ^= hf(t.second.id()) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);
131 }
133
134 return pairH(std::make_pair(h, h2));
135 }
136
138 inline bool existsVar(u32_t varId) const
139 {
140 return _varToVal.count(varId);
141 }
142
144 virtual inline Z3Expr &getZ3Expr(u32_t varId)
145 {
146 return _varToVal[varId];
147 }
148
150 virtual inline Z3Expr toZ3Expr(u32_t varId) const
151 {
152 return getContext().int_const(std::to_string(varId).c_str());
153 }
154
156 void extractSubVars(const Z3Expr &expr, Set<u32_t> &res);
157
159 void extractCmpVars(const Z3Expr &expr, Set<u32_t> &res);
160
163
165 void store(const Z3Expr &loc, const Z3Expr &value);
166
168 Z3Expr &load(const Z3Expr &loc);
169
175
177 static inline bool isVirtualMemAddress(u32_t val)
178 {
179 if (val == 0)
180 assert(false && "val cannot be 0");
182 }
183
186 {
188 }
189
191 static inline s32_t z3Expr2NumValue(const Z3Expr &e)
192 {
193 assert(e.is_numeral() && "not numeral?");
194 return e.get_numeral_int64();
195 }
196
198 void printExprValues();
199
200private:
201 bool eqVarToValMap(const VarToValMap &lhs, const VarToValMap &rhs) const;
202
203 bool lessThanVarToValMap(const VarToValMap &lhs, const VarToValMap &rhs) const;
204
205protected:
206 inline void store(u32_t objId, const Z3Expr &z3Expr)
207 {
208 _addrToVal[objId] = z3Expr.simplify();
209 }
210
212 {
213 return _addrToVal[objId];
214 }
215}; // end class RelExeState
216} // end namespace SVF
217
218template<>
219struct std::hash<SVF::RelExeState>
220{
221 size_t operator()(const SVF::RelExeState &exeState) const
222 {
223 return exeState.hash();
224 }
225};
226
227#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.
static u32_t getVirtualMemAddress(u32_t idx)
The physical address starts with 0x7f...... + idx.
static bool isVirtualMemAddress(u32_t val)
Check bit value of val start with 0x7F000000, filter by 0xFF000000.
VarToValMap AddrToValMap
Definition RelExeState.h:46
u32_t hash() const
RelExeState(RelExeState &&rhs) noexcept
Definition RelExeState.h:66
bool lessThanVarToValMap(const VarToValMap &lhs, const VarToValMap &rhs) const
static u32_t getVirtualMemAddress(u32_t idx)
The physical address starts with 0x7f...... + idx.
friend class SVFIR2AbsState
Definition RelExeState.h:42
Z3Expr & load(u32_t objId)
void store(u32_t objId, const Z3Expr &z3Expr)
void extractSubVars(const Z3Expr &expr, Set< u32_t > &res)
Extract sub SVFVar IDs of a Z3Expr.
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:57
Z3Expr & operator[](u32_t varId)
RelExeState & operator=(const RelExeState &rhs)
RelExeState & operator=(RelExeState &&rhs) noexcept
Definition RelExeState.h:72
const AddrToValMap & getLocToVal() const
virtual Z3Expr toZ3Expr(u32_t varId) const
Return Z3 expression lazily based on SVFVar ID.
bool existsVar(u32_t varId) const
Return true if map has varId.
virtual ~RelExeState()=default
RelExeState(VarToValMap &varToVal, AddrToValMap &locToVal)
Definition RelExeState.h:55
Z3Expr & load(const Z3Expr &loc)
Load value at location.
bool eqVarToValMap(const VarToValMap &lhs, const VarToValMap &rhs) const
const VarToValMap & getVarToVal() const
static u32_t getInternalID(u32_t idx)
Return the internal index if idx is an address otherwise return the value of idx.
VarToValMap _varToVal
Definition RelExeState.h:49
Z3Expr buildRelZ3Expr(u32_t cmp, s32_t succ, Set< u32_t > &vars, Set< u32_t > &initVars)
Build relational Z3Expr.
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.
AddrToValMap _addrToVal
Definition RelExeState.h:50
static bool isVirtualMemAddress(u32_t val)
Check bit value of val start with 0x7F000000, filter by 0xFF000000.
virtual Z3Expr & getZ3Expr(u32_t varId)
Return Z3 expression eagerly based on SVFVar ID.
bool operator!=(const RelExeState &rhs) const
Overloading Operator!=.
Definition RelExeState.h:86
static z3::context & getContext()
Definition RelExeState.h:95
void extractCmpVars(const Z3Expr &expr, Set< u32_t > &res)
Extract all related SVFVar IDs based on compare expr.
Map< u32_t, Z3Expr > VarToValMap
Definition RelExeState.h:45
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
for isBitcode
Definition BasicTypes.h:70
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
signed s32_t
Definition GeneralType.h:68
unsigned u32_t
Definition GeneralType.h:67
size_t operator()(const SVF::RelExeState &exeState) const