Static Value-Flow Analysis
Loading...
Searching...
No Matches
AddressValue.h
Go to the documentation of this file.
1//===- AddressValue.h ----Address Value Sets-------------------------//
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 * AddressValue.h
24 *
25 * Created on: Jul 9, 2022
26 * Author: Xiao Cheng
27 *
28 */
29
30#ifndef Z3_EXAMPLE_ADDRESSVALUE_H
31#define Z3_EXAMPLE_ADDRESSVALUE_H
32
33#define AddressMask 0x7f000000
34#define FlippedAddressMask (AddressMask^0xffffffff)
35// the address of the black hole, getVirtualMemAddress(2);
36#define BlackHoleAddr 0x7f000000 + 2
37
38#include "Util/GeneralType.h"
39#include <sstream>
40
41namespace SVF
42{
44{
45public:
47private:
49public:
52
54 AddressValue(const Set<u32_t> &addrs) : _addrs(addrs) {}
55
57
59 ~AddressValue() = default;
60
63
65 AddressValue(AddressValue &&other) noexcept: _addrs(std::move(other._addrs)) {}
66
69 {
70 if (!this->equals(other))
71 {
73 }
74 return *this;
75 }
76
79 {
80 if (this != &other)
81 {
82 _addrs = std::move(other._addrs);
83 }
84 return *this;
85 }
86
87 bool equals(const AddressValue &rhs) const
88 {
89 return _addrs == rhs._addrs;
90 }
91
92 AddrSet::const_iterator begin() const
93 {
94 return _addrs.cbegin();
95 }
96
97 AddrSet::const_iterator end() const
98 {
99 return _addrs.cend();
100 }
101
102 bool empty() const
103 {
104 return _addrs.empty();
105 }
106
107 u32_t size() const
108 {
109 return _addrs.size();
110 }
111
112 std::pair<AddressValue::AddrSet::iterator, bool> insert(u32_t id)
113 {
114 return _addrs.insert(id);
115 }
116
117 const AddrSet &getVals() const
118 {
119 return _addrs;
120 }
121
122 void setVals(const AddrSet &vals)
123 {
124 _addrs = vals;
125 }
126
129 {
130 bool changed = false;
131 for (const auto &addr: other)
132 {
133 if (!_addrs.count(addr))
134 {
135 if (insert(addr).second)
136 changed = true;
137 }
138 }
139 return changed;
140 }
141
144 {
145 AddrSet s;
146 for (const auto &id: other._addrs)
147 {
148 if (_addrs.find(id) != _addrs.end())
149 {
150 s.insert(id);
151 }
152 }
153 bool changed = (_addrs != s);
154 _addrs = std::move(s);
155 return changed;
156 }
157
159 bool contains(u32_t id) const
160 {
161 return _addrs.count(id);
162 }
163
165 {
166 AddressValue v = *this;
168 return !v.empty();
169 }
170
171 inline bool isTop() const
172 {
173 return *this->begin() == BlackHoleAddr;
174 }
175
176 inline bool isBottom() const
177 {
178 return empty();
179 }
180
181 inline void setTop()
182 {
184 }
185
186 inline void setBottom()
187 {
188 _addrs.clear();
189 }
190
191 const std::string toString() const
192 {
193 std::string str;
194 std::stringstream rawStr(str);
195 if (this->isBottom())
196 {
197 rawStr << "⊥";
198 }
199 else
200 {
201 rawStr << "[";
202 for (auto it = _addrs.begin(), eit = _addrs.end(); it!= eit; ++it)
203 {
204 rawStr << *it << ", ";
205 }
206 rawStr << "]";
207 }
208 return rawStr.str();
209 }
210
213 {
214 // 0 is the null address, should not be used as a virtual address
215 assert(idx != 0 && "idx can’t be 0 because it represents a nullptr");
216 return AddressMask + idx;
217 }
218
220 static inline bool isVirtualMemAddress(u32_t val)
221 {
222 return (val & 0xff000000) == AddressMask && val != AddressMask + 0;
223 }
224
227 {
228 return (idx & FlippedAddressMask);
229 }
230};
231} // end namespace SVF
232#endif //Z3_EXAMPLE_ADDRESSVALUE_H
#define AddressMask
#define BlackHoleAddr
#define FlippedAddressMask
~AddressValue()=default
Default destructor.
bool meet_with(const AddressValue &other)
Return a intersected AddressValue.
bool equals(const AddressValue &rhs) const
std::pair< AddressValue::AddrSet::iterator, bool > insert(u32_t id)
void setVals(const AddrSet &vals)
const std::string toString() const
bool hasIntersect(const AddressValue &other)
AddressValue(const Set< u32_t > &addrs)
Constructor.
static u32_t getInternalID(u32_t idx)
Return the internal index if idx is an address otherwise return the value of idx.
AddressValue()
Default constructor.
Set< u32_t > AddrSet
AddressValue(AddressValue &&other) noexcept
Move constructor.
bool contains(u32_t id) const
Return true if the AddressValue contains n.
AddrSet::const_iterator end() const
const AddrSet & getVals() const
bool isTop() const
bool isBottom() const
static u32_t getVirtualMemAddress(u32_t idx)
The physical address starts with 0x7f...... + idx.
bool empty() const
static bool isVirtualMemAddress(u32_t val)
Check bit value of val start with 0x7F000000, filter by 0xFF000000.
AddressValue(const AddressValue &other)
Copy constructor.
bool join_with(const AddressValue &other)
Current AddressValue joins with another AddressValue.
AddressValue & operator=(const AddressValue &other)
Copy operator=.
AddressValue(u32_t addr)
AddressValue & operator=(AddressValue &&other) noexcept
Move operator=.
u32_t size() const
AddrSet::const_iterator begin() const
for isBitcode
Definition BasicTypes.h:68
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
unsigned u32_t
Definition GeneralType.h:46