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
36namespace SVF
37{
38
40{
41 friend class SVFIR2AbsState;
42
43public:
46
47protected:
50
51public:
52 RelExeState() = default;
53
55
60
61 virtual ~RelExeState() = default;
62
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
105 {
106 return _addrToVal;
107 }
108
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
162
164 void store(const Z3Expr &loc, const Z3Expr &value);
165
167 Z3Expr &load(const Z3Expr &loc);
168
174
176 static inline bool isVirtualMemAddress(u32_t val)
177 {
178 if (val == 0)
179 assert(false && "val cannot be 0");
181 }
182
185 {
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
199private:
200 bool eqVarToValMap(const VarToValMap &lhs, const VarToValMap &rhs) const;
201
202 bool lessThanVarToValMap(const VarToValMap &lhs, const VarToValMap &rhs) const;
203
204protected:
205 inline void store(u32_t objId, const Z3Expr &z3Expr)
206 {
207 _addrToVal[objId] = z3Expr.simplify();
208 }
209
211 {
212 return _addrToVal[objId];
213 }
214}; // end class RelExeState
215} // end namespace SVF
216
217template<>
218struct 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.
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:45
u32_t hash() const
RelExeState(RelExeState &&rhs) noexcept
Definition RelExeState.h:65
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:41
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:56
Z3Expr & operator[](u32_t varId)
RelExeState & operator=(const RelExeState &rhs)
RelExeState & operator=(RelExeState &&rhs) noexcept
Definition RelExeState.h:71
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:54
Z3Expr & load(const Z3Expr &loc)
Load value at location.
bool eqVarToValMap(const VarToValMap &lhs, const VarToValMap &rhs) const
const VarToValMap & getVarToVal() const
Definition RelExeState.h:99
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:48
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:49
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:85
static z3::context & getContext()
Definition RelExeState.h:94
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: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
for isBitcode
Definition BasicTypes.h:68
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
signed s32_t
Definition GeneralType.h:47
unsigned u32_t
Definition GeneralType.h:46
size_t operator()(const SVF::RelExeState &exeState) const