svf-lib 1.0.973 → 1.0.976

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,182 @@
1
+ //===- BDDExpr.h -- Context/Path BDD conditions----------------------------//
2
+ //
3
+ // SVF: Static Value-Flow Analysis
4
+ //
5
+ // Copyright (C) <2013-2017> <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 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 General Public License for more details.
17
+
18
+ // You should have received a copy of the GNU General Public License
19
+ // along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ //
21
+ //===----------------------------------------------------------------------===//
22
+
23
+ /*
24
+ * Condition.h
25
+ *
26
+ * Created on: Oct 19, 2021
27
+ * Author: Yulei and Xiao
28
+ */
29
+
30
+ #ifndef BITVECTORCOND_H_
31
+ #define BITVECTORCOND_H_
32
+
33
+ #include "Util/BasicTypes.h"
34
+ #include <cstdio>
35
+
36
+ #include "CUDD/cuddInt.h"
37
+ #include "z3++.h"
38
+
39
+ namespace SVF {
40
+
41
+ class BDDExprManager {
42
+ public:
43
+ typedef DdNode BDDExpr;
44
+ typedef Map<u32_t, BDDExpr *> IndexToBDDExpr;
45
+ private:
46
+ static BDDExprManager *bddExprMgr;
47
+ IndexToBDDExpr indexToBddCondMap;
48
+ DdManager *m_bdd_mgr;
49
+
50
+ inline BDDExpr *BddOne() const {
51
+ return Cudd_ReadOne(m_bdd_mgr);
52
+ }
53
+
54
+ inline BDDExpr *BddZero() const {
55
+ return Cudd_ReadLogicZero(m_bdd_mgr);
56
+ }
57
+
58
+ /// Constructor
59
+ BDDExprManager();
60
+
61
+ public:
62
+ typedef Map<const BDDExpr *, const Instruction *> CondToTermInstMap; // map a condition to its branch instruction
63
+ static u32_t totalCondNum; // a counter for fresh condition
64
+ /// Singleton design here to make sure we only have one instance during any analysis
65
+ //@{
66
+ static BDDExprManager *getBDDExprMgr();
67
+
68
+ static void releaseBDDExprMgr() {
69
+ delete bddExprMgr;
70
+ bddExprMgr = nullptr;
71
+ }
72
+ //@}
73
+
74
+ /// Destructor
75
+ virtual ~BDDExprManager();
76
+
77
+ BDDExpr *createCond(u32_t i) {
78
+ assert(indexToBddCondMap.find(i) == indexToBddCondMap.end() &&
79
+ "This should be fresh index to create new BDD");
80
+ BDDExpr *bddCond = Cudd_bddIthVar(m_bdd_mgr, i);
81
+ return indexToBddCondMap.emplace(i, bddCond).first->second;
82
+ }
83
+
84
+ /// Create a fresh condition to encode each program branch
85
+ virtual BDDExpr *createFreshBranchCond(const Instruction *inst);
86
+
87
+ /// Return the number of condition expressions
88
+ virtual inline u32_t getCondNumber() {
89
+ return Cudd_ReadNodeCount(m_bdd_mgr);
90
+ }
91
+
92
+
93
+ /// Return the unique true condition
94
+ inline BDDExpr *getTrueCond() const {
95
+ return trueCond;
96
+ }
97
+
98
+ /// Return the unique false condition
99
+ inline BDDExpr *getFalseCond() const {
100
+ return falseCond;
101
+ }
102
+
103
+ /// Operations on conditions.
104
+ //@{
105
+ virtual BDDExpr *AND(BDDExpr *lhs, BDDExpr *rhs);
106
+
107
+ virtual BDDExpr *OR(BDDExpr *lhs, BDDExpr *rhs);
108
+
109
+ virtual BDDExpr *NEG(BDDExpr *lhs);
110
+ //@}
111
+
112
+ virtual bool isNegCond(const BDDExpr *cond) {
113
+ return false;
114
+ }
115
+
116
+ /// Whether the condition is satisfiable
117
+ virtual bool isSatisfiable(const BDDExpr *cond) {
118
+ return cond != getFalseCond();
119
+ }
120
+
121
+ /// Whether lhs and rhs are equivalent branch conditions
122
+ virtual bool isEquivalentBranchCond(const BDDExpr *lhs, const BDDExpr *rhs){
123
+ return lhs == rhs;
124
+ }
125
+
126
+ /// Whether **All Paths** are reachable
127
+ bool isAllPathReachable(const BDDExpr *e);
128
+
129
+ /// Get condition using condition id (z3 ast id)
130
+ virtual BDDExpr *getCond(u32_t id) {
131
+ auto it = indexToBddCondMap.find(id);
132
+ assert(it != indexToBddCondMap.end() && "condition not found!");
133
+ return it->second;
134
+ }
135
+
136
+ /// Get/Set llvm conditional expression
137
+ //{@
138
+ inline const Instruction *getCondInst(const BDDExpr *cond) const {
139
+ CondToTermInstMap::const_iterator it = condToInstMap.find(cond);
140
+ assert(it != condToInstMap.end() && "this should be a fresh condition");
141
+ return it->second;
142
+ }
143
+
144
+ inline void setCondInst(const BDDExpr *cond, const Instruction *inst) {
145
+ assert(condToInstMap.find(cond) == condToInstMap.end() && "this should be a fresh condition");
146
+ condToInstMap[cond] = inst;
147
+ }
148
+ //@}
149
+
150
+ /**
151
+ * Utilities for dumping conditions. These methods use global functions from CUDD
152
+ * package and they can be removed outside this class scope to be used by others.
153
+ */
154
+ void ddClearFlag(BDDExpr *f) const;
155
+
156
+ void BddSupportStep(BDDExpr *f, NodeBS &support) const;
157
+
158
+ void extractSubConds(const BDDExpr *f, NodeBS &support) const;
159
+
160
+ void dump(const BDDExpr *lhs, OutStream &O);
161
+
162
+ std::string dumpStr(const BDDExpr *e) const;
163
+
164
+ inline std::string getMemUsage() {
165
+ return std::to_string(Cudd_ReadMemoryInUse(m_bdd_mgr));
166
+ }
167
+
168
+ protected:
169
+ static BDDExpr *trueCond;
170
+ static BDDExpr *falseCond;
171
+
172
+ private:
173
+
174
+ CondToTermInstMap condToInstMap; ///< map condition to llvm instruction
175
+
176
+ };
177
+
178
+
179
+
180
+ } // End namespace SVF
181
+
182
+ #endif /* BITVECTORCOND_H_ */
@@ -30,7 +30,7 @@
30
30
  #ifndef DPITEM_H_
31
31
  #define DPITEM_H_
32
32
 
33
- #include "Util/Conditions.h"
33
+ #include "Util/BDDExpr.h"
34
34
  #include "MemoryModel/ConditionalPT.h"
35
35
  #include <algorithm> // std::sort
36
36
 
@@ -0,0 +1,172 @@
1
+ //===- ExeState.h -- Execution State ----------------------------//
2
+ //
3
+ // SVF: Static Value-Flow Analysis
4
+ //
5
+ // Copyright (C) <2013-2017> <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 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 General Public License for more details.
17
+
18
+ // You should have received a copy of the GNU General Public License
19
+ // along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ //
21
+ //===----------------------------------------------------------------------===//
22
+ /*
23
+ * ExeState.h
24
+ *
25
+ * Created on: April 29, 2022
26
+ * Author: Xiao
27
+ */
28
+
29
+ #ifndef SVF_EXESTATE_H
30
+ #define SVF_EXESTATE_H
31
+ #include "Util/Z3Expr.h"
32
+ #include "MemoryModel/SVFVariables.h"
33
+
34
+ namespace SVF {
35
+
36
+ #define AddressMask 0x7f000000
37
+ #define FlippedAddressMask (AddressMask^0xffffffff)
38
+
39
+ class ExeState {
40
+ public:
41
+ typedef Map<u32_t, Z3Expr> VarToValMap;
42
+ typedef Map<u32_t, Z3Expr> LocToValMap;
43
+
44
+ protected:
45
+ VarToValMap varToVal;
46
+ LocToValMap locToVal;
47
+ Z3Expr pathConstraint;
48
+
49
+ public:
50
+ ExeState() : pathConstraint(getContext().bool_val(true)) {}
51
+
52
+ ExeState(const Z3Expr &_pc, VarToValMap &_varToValMap, LocToValMap &_locToValMap) : varToVal(_varToValMap),
53
+ locToVal(_locToValMap),
54
+ pathConstraint(_pc) {}
55
+
56
+ ExeState(const ExeState &rhs) : varToVal(rhs.getVarToVal()), locToVal(rhs.getLocToVal()),
57
+ pathConstraint(rhs.getPathConstraint()) {
58
+
59
+ }
60
+
61
+ virtual ~ExeState() = default;
62
+
63
+ ExeState &operator=(const ExeState &rhs);
64
+
65
+ /// Overloading Operator==
66
+ bool operator==(const ExeState &rhs) const;
67
+
68
+ /// Overloading Operator!=
69
+ inline bool operator!=(const ExeState &rhs) const {
70
+ return !(*this == rhs);
71
+ }
72
+
73
+ /// Overloading Operator==
74
+ bool operator<(const ExeState &rhs) const;
75
+
76
+ z3::context &getContext() {
77
+ return Z3Expr::getContext();
78
+ }
79
+
80
+ const VarToValMap &getVarToVal() const {
81
+ return varToVal;
82
+ }
83
+
84
+ const LocToValMap &getLocToVal() const {
85
+ return locToVal;
86
+ }
87
+
88
+ const Z3Expr &getPathConstraint() const {
89
+ return pathConstraint;
90
+ }
91
+
92
+ void setPathConstraint(const Z3Expr &pc) {
93
+ pathConstraint = pc.simplify();
94
+ }
95
+
96
+ inline Z3Expr &operator[](u32_t varId) {
97
+ return getZ3Expr(varId);
98
+ }
99
+
100
+ /// Init Z3Expr for ValVar
101
+ void initValVar(const ValVar *valVar, Z3Expr &e);
102
+
103
+ /// Init Z3Expr for ObjVar
104
+ void initObjVar(const ObjVar *objVar, Z3Expr &e);
105
+
106
+ /// Return Z3 expression based on SVFVar ID
107
+ Z3Expr &getZ3Expr(u32_t varId);
108
+
109
+ /// Store value to location
110
+ void store(const Z3Expr &loc, const Z3Expr &value);
111
+
112
+ /// Load value at location
113
+ Z3Expr &load(const Z3Expr &loc);
114
+
115
+ /// The physical address starts with 0x7f...... + idx
116
+ inline u32_t getVirtualMemAddress(u32_t idx) const {
117
+ return AddressMask + idx;
118
+ }
119
+
120
+ /// Check bit value of val start with 0x7F000000, filter by 0xFF000000
121
+ inline bool isVirtualMemAddress(u32_t val) {
122
+ return (val & 0xff000000) == AddressMask;
123
+ }
124
+
125
+ /// Return the internal index if idx is an address otherwise return the value of idx
126
+ inline u32_t getInternalID(u32_t idx) const {
127
+ return (idx & FlippedAddressMask);
128
+ }
129
+
130
+ /// Return int value from an expression if it is a numeral, otherwise return an approximate value
131
+ inline s32_t z3Expr2NumValue(const Z3Expr &e) {
132
+ assert(e.is_numeral() && "not numeral?");
133
+ return e.get_numeral_int64();
134
+ }
135
+
136
+ /// Print values of all expressions
137
+ void printExprValues();
138
+
139
+ protected:
140
+ inline void store(u32_t objId, const Z3Expr &z3Expr) {
141
+ locToVal[objId] = z3Expr.simplify();
142
+ }
143
+
144
+ inline Z3Expr &load(u32_t objId) {
145
+ return locToVal[objId];
146
+ }
147
+ };
148
+ }
149
+
150
+ template<>
151
+ struct std::hash<SVF::ExeState> {
152
+ size_t operator()(const SVF::ExeState &exeState) const {
153
+
154
+ size_t h = exeState.getVarToVal().size() * 2;
155
+ SVF::Hash<SVF::u32_t> hf;
156
+ for (const auto &t: exeState.getVarToVal()) {
157
+ h ^= hf(t.first) + 0x9e3779b9 + (h << 6) + (h >> 2);
158
+ h ^= hf(t.second.id()) + 0x9e3779b9 + (h << 6) + (h >> 2);
159
+ }
160
+
161
+ size_t h2 = exeState.getVarToVal().size() * 2;
162
+
163
+ for (const auto &t: exeState.getLocToVal()) {
164
+ h2 ^= hf(t.first) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);
165
+ h2 ^= hf(t.second.id()) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);
166
+ }
167
+ SVF::Hash<std::pair<SVF::u32_t, SVF::u32_t>> pairH;
168
+
169
+ return pairH(make_pair(pairH(make_pair(h, h2)), exeState.getPathConstraint().id()));
170
+ }
171
+ };
172
+ #endif //SVF_EXESTATE_H
@@ -10,7 +10,6 @@
10
10
  #include "Util/NodeIDAllocator.h"
11
11
  #include "MSSA/MemSSA.h"
12
12
  #include "WPA/WPAPass.h"
13
- #include "Util/Conditions.h"
14
13
 
15
14
  namespace SVF
16
15
  {
@@ -255,8 +254,6 @@ public:
255
254
  // DOTGraphTraits
256
255
  static const llvm::cl::opt<bool> ShowHiddenNode;
257
256
 
258
- // Conditions
259
- static const llvm::cl::opt<CondManager::CondMgrKind> ConditionType;
260
257
  };
261
258
  } // namespace SVF
262
259
 
@@ -0,0 +1,235 @@
1
+ //===- Z3Expr.h -- Z3 conditions----------------------------//
2
+ //
3
+ // SVF: Static Value-Flow Analysis
4
+ //
5
+ // Copyright (C) <2013-2017> <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 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 General Public License for more details.
17
+
18
+ // You should have received a copy of the GNU General Public License
19
+ // along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ //
21
+ //===----------------------------------------------------------------------===//
22
+ /*
23
+ * Z3Expr.h
24
+ *
25
+ * Created on: April 29, 2022
26
+ * Author: Xiao
27
+ */
28
+
29
+ #ifndef Z3_EXAMPLE_Z3EXPR_H
30
+ #define Z3_EXAMPLE_Z3EXPR_H
31
+
32
+ #include "z3++.h"
33
+ #include "Util/SVFBasicTypes.h"
34
+
35
+ namespace SVF {
36
+
37
+ class Z3Expr {
38
+ public:
39
+ static z3::context *ctx;
40
+
41
+ private:
42
+ z3::expr e;
43
+
44
+ Z3Expr(float f); // placeholder don't support floating point expression
45
+ Z3Expr(double f); // placeholder don't support floating point expression
46
+
47
+ public:
48
+
49
+ Z3Expr() : e(nullExpr()) {
50
+ }
51
+
52
+ Z3Expr(const z3::expr &_e) : e(_e) {
53
+ }
54
+
55
+ Z3Expr(int i) : e(getContext().int_val(i)) {
56
+ }
57
+
58
+ Z3Expr(const Z3Expr &z3Expr) : e(z3Expr.getExpr()) {
59
+ }
60
+
61
+ virtual ~Z3Expr() = default;
62
+
63
+ inline Z3Expr &operator=(const Z3Expr &rhs) {
64
+ if (this->id() != rhs.id()) {
65
+ e = rhs.getExpr();
66
+ }
67
+ return *this;
68
+ }
69
+
70
+ const z3::expr &getExpr() const {
71
+ return e;
72
+ }
73
+
74
+ /// Get z3 context, singleton design here to make sure we only have one context
75
+ static z3::context &getContext() {
76
+ if (ctx == nullptr) {
77
+ ctx = new z3::context();
78
+ }
79
+ return *ctx;
80
+ }
81
+
82
+ /// release z3 context
83
+ static void releaseContext() {
84
+ delete ctx;
85
+ ctx = nullptr;
86
+ }
87
+
88
+ /// null expression
89
+ static z3::expr nullExpr() {
90
+ return getContext().int_const("null");
91
+ }
92
+
93
+ /// get id
94
+ inline u32_t id() const {
95
+ return e.id();
96
+ }
97
+
98
+ inline const std::string to_string() const {
99
+ return e.to_string();
100
+ }
101
+
102
+ inline bool is_numeral() const {
103
+ return e.is_numeral();
104
+ }
105
+
106
+ inline bool is_bool() const {
107
+ return e.is_bool();
108
+ }
109
+
110
+ inline Z3Expr simplify() const {
111
+ return e.simplify();
112
+ }
113
+
114
+ inline int64_t get_numeral_int64() const {
115
+ return e.get_numeral_int64();
116
+ }
117
+
118
+ inline int get_numeral_int() const {
119
+ return e.get_numeral_int();
120
+ }
121
+
122
+ //{% reload operator
123
+ friend Z3Expr operator==(const Z3Expr &lhs, const Z3Expr &rhs) {
124
+ return lhs.getExpr() == rhs.getExpr();
125
+ }
126
+
127
+ friend Z3Expr operator!=(const Z3Expr &lhs, const Z3Expr &rhs) {
128
+ return lhs.getExpr() != rhs.getExpr();
129
+ }
130
+
131
+ friend Z3Expr operator>(const Z3Expr &lhs, const Z3Expr &rhs) {
132
+ return lhs.getExpr() > rhs.getExpr();
133
+ }
134
+
135
+ friend Z3Expr operator<(const Z3Expr &lhs, const Z3Expr &rhs) {
136
+ return lhs.getExpr() < rhs.getExpr();
137
+ }
138
+
139
+ friend Z3Expr operator<=(const Z3Expr &lhs, const Z3Expr &rhs) {
140
+ return lhs.getExpr() <= rhs.getExpr();
141
+ }
142
+
143
+ friend Z3Expr operator>=(const Z3Expr &lhs, const Z3Expr &rhs) {
144
+ return lhs.getExpr() >= rhs.getExpr();
145
+ }
146
+
147
+ friend Z3Expr operator+(const Z3Expr &lhs, const Z3Expr &rhs) {
148
+ return lhs.getExpr() + rhs.getExpr();
149
+ }
150
+
151
+ friend Z3Expr operator-(const Z3Expr &lhs, const Z3Expr &rhs) {
152
+ return lhs.getExpr() - rhs.getExpr();
153
+ }
154
+
155
+ friend Z3Expr operator*(const Z3Expr &lhs, const Z3Expr &rhs) {
156
+ return lhs.getExpr() * rhs.getExpr();
157
+ }
158
+
159
+ friend Z3Expr operator/(const Z3Expr &lhs, const Z3Expr &rhs) {
160
+ return lhs.getExpr() / rhs.getExpr();
161
+ }
162
+
163
+ friend Z3Expr operator%(const Z3Expr &lhs, const Z3Expr &rhs) {
164
+ return lhs.getExpr() % rhs.getExpr();
165
+ }
166
+
167
+ friend Z3Expr operator^(const Z3Expr &lhs, const Z3Expr &rhs) {
168
+ return lhs.getExpr() ^ rhs.getExpr();
169
+ }
170
+
171
+ friend Z3Expr operator&(const Z3Expr &lhs, const Z3Expr &rhs) {
172
+ return lhs.getExpr() & rhs.getExpr();
173
+ }
174
+
175
+ friend Z3Expr operator|(const Z3Expr &lhs, const Z3Expr &rhs) {
176
+ return lhs.getExpr() | rhs.getExpr();
177
+ }
178
+
179
+ friend Z3Expr ashr(const Z3Expr &lhs, const Z3Expr &rhs) {
180
+ return ashr(lhs.getExpr(), rhs.getExpr());
181
+ }
182
+
183
+ friend Z3Expr shl(const Z3Expr &lhs, const Z3Expr &rhs) {
184
+ return shl(lhs.getExpr(), rhs.getExpr());
185
+ }
186
+
187
+ friend Z3Expr int2bv(u32_t n, const Z3Expr &e) {
188
+ return int2bv(n, e.getExpr());
189
+ }
190
+
191
+ friend Z3Expr bv2int(const Z3Expr &e, bool isSigned) {
192
+ return z3::bv2int(e.getExpr(), isSigned);
193
+ }
194
+
195
+ friend Z3Expr operator&&(const Z3Expr &lhs, const Z3Expr &rhs) {
196
+ return lhs.getExpr() && rhs.getExpr();
197
+ }
198
+
199
+ friend Z3Expr operator||(const Z3Expr &lhs, const Z3Expr &rhs) {
200
+ return lhs.getExpr() || rhs.getExpr();
201
+ }
202
+
203
+ friend Z3Expr operator!(const Z3Expr &lhs) {
204
+ return !lhs.getExpr();
205
+ }
206
+
207
+ friend Z3Expr ite(const Z3Expr& cond, const Z3Expr& lhs, const Z3Expr& rhs) {
208
+ return ite(cond.getExpr(), lhs.getExpr(), rhs.getExpr());
209
+ }
210
+
211
+ friend std::ostream &operator<<(std::ostream &out, const Z3Expr &expr) {
212
+ out << expr.getExpr();
213
+ return out;
214
+ }
215
+
216
+ friend bool eq(const Z3Expr &lhs, const Z3Expr &rhs) {
217
+ return eq(lhs.getExpr(), rhs.getExpr());
218
+ }
219
+
220
+ z3::sort get_sort() const {
221
+ return getExpr().get_sort();
222
+ }
223
+ //%}
224
+ };
225
+ }
226
+
227
+ /// Specialise hash for AbsCxtDPItem.
228
+ template<>
229
+ struct std::hash<SVF::Z3Expr> {
230
+ size_t operator()(const SVF::Z3Expr &z3Expr) const {
231
+ return z3Expr.id();
232
+ }
233
+ };
234
+
235
+ #endif //Z3_EXAMPLE_Z3EXPR_H
@@ -13,7 +13,7 @@
13
13
  #include "SVF-FE/DCHG.h"
14
14
  #include "Graphs/SVFGOPT.h"
15
15
  #include "MSSA/SVFGBuilder.h"
16
- #include "Util/TypeBasedHeapCloning.h"
16
+ #include "TypeBasedHeapCloning.h"
17
17
  #include "WPA/FlowSensitive.h"
18
18
  #include "MemoryModel/PointsTo.h"
19
19