Static Value-Flow Analysis
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 
41 namespace SVF
42 {
44 {
45 public:
47 private:
49 public:
52 
54  AddressValue(const Set<u32_t> &addrs) : _addrs(addrs) {}
55 
56  AddressValue(u32_t addr) : _addrs({addr}) {}
57 
59  ~AddressValue() = default;
60 
62  AddressValue(const AddressValue &other) : _addrs(other._addrs) {}
63 
65  AddressValue(AddressValue &&other) noexcept: _addrs(std::move(other._addrs)) {}
66 
69  {
70  if (!this->equals(other))
71  {
72  _addrs = other._addrs;
73  }
74  return *this;
75  }
76 
78  AddressValue &operator=(AddressValue &&other) noexcept
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 
128  bool join_with(const AddressValue &other)
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 
143  bool meet_with(const AddressValue &other)
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 
164  bool hasIntersect(const AddressValue &other)
165  {
166  AddressValue v = *this;
167  v.meet_with(other);
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  {
183  *this = AddressValue(BlackHoleAddr);
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 
212  static inline u32_t getVirtualMemAddress(u32_t idx)
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 
226  static inline u32_t getInternalID(u32_t idx)
227  {
228  return (idx & FlippedAddressMask);
229  }
230 };
231 } // end namespace SVF
232 #endif //Z3_EXAMPLE_ADDRESSVALUE_H
#define AddressMask
Definition: AddressValue.h:33
#define BlackHoleAddr
Definition: AddressValue.h:36
#define FlippedAddressMask
Definition: AddressValue.h:34
const char *const string
Definition: cJSON.h:172
~AddressValue()=default
Default destructor.
bool meet_with(const AddressValue &other)
Return a intersected AddressValue.
Definition: AddressValue.h:143
bool equals(const AddressValue &rhs) const
Definition: AddressValue.h:87
void setVals(const AddrSet &vals)
Definition: AddressValue.h:122
const std::string toString() const
Definition: AddressValue.h:191
bool hasIntersect(const AddressValue &other)
Definition: AddressValue.h:164
AddressValue(const Set< u32_t > &addrs)
Constructor.
Definition: AddressValue.h:54
const AddrSet & getVals() const
Definition: AddressValue.h:117
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
std::pair< AddressValue::AddrSet::iterator, bool > insert(u32_t id)
Definition: AddressValue.h:112
AddressValue()
Default constructor.
Definition: AddressValue.h:51
Set< u32_t > AddrSet
Definition: AddressValue.h:46
AddressValue(AddressValue &&other) noexcept
Move constructor.
Definition: AddressValue.h:65
bool contains(u32_t id) const
Return true if the AddressValue contains n.
Definition: AddressValue.h:159
AddrSet::const_iterator end() const
Definition: AddressValue.h:97
bool isTop() const
Definition: AddressValue.h:171
bool isBottom() const
Definition: AddressValue.h:176
static u32_t getVirtualMemAddress(u32_t idx)
The physical address starts with 0x7f...... + idx.
Definition: AddressValue.h:212
bool empty() const
Definition: AddressValue.h:102
static bool isVirtualMemAddress(u32_t val)
Check bit value of val start with 0x7F000000, filter by 0xFF000000.
Definition: AddressValue.h:220
AddressValue & operator=(AddressValue &&other) noexcept
Move operator=.
Definition: AddressValue.h:78
AddressValue(const AddressValue &other)
Copy constructor.
Definition: AddressValue.h:62
bool join_with(const AddressValue &other)
Current AddressValue joins with another AddressValue.
Definition: AddressValue.h:128
AddressValue(u32_t addr)
Definition: AddressValue.h:56
u32_t size() const
Definition: AddressValue.h:107
AddrSet::const_iterator begin() const
Definition: AddressValue.h:92
AddressValue & operator=(const AddressValue &other)
Copy operator=.
Definition: AddressValue.h:68
constexpr std::remove_reference< T >::type && move(T &&t) noexcept
Definition: SVFUtil.h:447
for isBitcode
Definition: BasicTypes.h:68
unsigned u32_t
Definition: GeneralType.h:46
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: GeneralType.h:96