-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
Copy pathSMTLib2Context.cpp
314 lines (285 loc) · 9.67 KB
/
SMTLib2Context.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <libsmtutil/SMTLib2Context.h>
#include <boost/functional/hash.hpp>
#include <range/v3/algorithm/find_if.hpp>
namespace solidity::smtutil
{
std::size_t SortPairHash::operator()(std::pair<SortId, SortId> const& _pair) const
{
std::size_t seed = 0;
boost::hash_combine(seed, _pair.first);
boost::hash_combine(seed, _pair.second);
return seed;
}
SMTLib2Context::SMTLib2Context()
{
clear();
}
bool SMTLib2Context::isDeclared(std::string const& _name) const
{
return m_functions.count(_name) > 0;
}
void SMTLib2Context::declare(std::string const& _name, SortPointer const& _sort)
{
auto [_, inserted] = m_functions.insert({_name, _sort});
smtAssert(inserted, "Trying to redeclare SMT function!");
}
SortPointer SMTLib2Context::getDeclaredSort(std::string const& _name) const
{
smtAssert(isDeclared(_name));
return m_functions.at(_name);
}
void SMTLib2Context::clear() {
m_functions.clear();
m_knownTypes.clear();
m_arraySorts.clear();
m_tupleSorts.clear();
m_bitVectorSorts.clear();
m_callback = {};
m_knownTypes.emplace_back(std::make_unique<SMTLibSort>(Kind::Bool, std::string("Bool"), std::vector<SortId>{}, SortId{0u}));
m_knownTypes.emplace_back(std::make_unique<SMTLibSort>(Kind::Int, std::string("Int"), std::vector<SortId>{}, SortId{1u}));
assert(m_boolSort == m_knownTypes[0]->id);
assert(m_intSort == m_knownTypes[1]->id);
}
SortId SMTLib2Context::resolve(SortPointer const& _sort)
{
switch (_sort->kind)
{
case Kind::Int:
return m_intSort;
case Kind::Bool:
return m_boolSort;
case Kind::BitVector:
return resolveBitVectorSort(dynamic_cast<BitVectorSort const&>(*_sort));
case Kind::Array:
return resolveArraySort(dynamic_cast<ArraySort const&>(*_sort));
case Kind::Tuple:
return resolveTupleSort(dynamic_cast<TupleSort const&>(*_sort));
default:
smtAssert(false, "Invalid SMT sort");
}
}
SortPointer SMTLib2Context::unresolve(SortId _sortId) const
{
smtAssert(_sortId < m_knownTypes.size());
auto const& type = *m_knownTypes[_sortId];
switch (type.kind)
{
case Kind::Int:
return SortProvider::sintSort;
case Kind::Bool:
return SortProvider::boolSort;
case Kind::BitVector:
{
auto it = ranges::find_if(m_bitVectorSorts, [&](auto const& entry) { return entry.second == _sortId; });
smtAssert(it != m_bitVectorSorts.end());
return std::make_shared<BitVectorSort>(it->first);
}
case Kind::Array:
{
auto it = ranges::find_if(m_arraySorts, [&](auto const& entry) { return entry.second == _sortId; });
smtAssert(it != m_arraySorts.end());
return std::make_shared<ArraySort>(unresolve(it->first.first), unresolve(it->first.second));
}
case Kind::Tuple:
{
auto const& tupleType = dynamic_cast<TupleType const&>(type);
std::vector<std::string> memberNames;
std::vector<SortPointer> memberTypes;
for (auto&& [name, sortId] : tupleType.accessors)
{
memberNames.push_back(name);
memberTypes.push_back(unresolve(sortId));
}
return std::make_shared<TupleSort>(tupleType.name, std::move(memberNames), std::move(memberTypes));
}
default:
smtAssert(false, "Invalid SMT sort");
}
}
SortId SMTLib2Context::resolveBitVectorSort(BitVectorSort const& _sort)
{
auto size = _sort.size;
auto it = m_bitVectorSorts.find(size);
if (it == m_bitVectorSorts.end())
{
auto newId = static_cast<uint32_t>(m_knownTypes.size());
m_knownTypes.emplace_back(std::make_unique<SMTLibSort>(Kind::BitVector, "(_ BitVec " + std::to_string(size) + ')', std::vector<SortId>{}, SortId{newId}));
auto&& [newIt, inserted] = m_bitVectorSorts.emplace(size, SortId{newId});
smtAssert(inserted);
return newIt->second;
}
return it->second;
}
SortId SMTLib2Context::resolveArraySort(ArraySort const& _sort)
{
smtAssert(_sort.domain && _sort.range);
auto domainSort = resolve(_sort.domain);
auto rangeSort = resolve(_sort.range);
auto pair = std::make_pair(domainSort, rangeSort);
auto it = m_arraySorts.find(pair);
if (it == m_arraySorts.end())
{
auto newId = static_cast<uint32_t>(m_knownTypes.size());
m_knownTypes.emplace_back(std::make_unique<SMTLibSort>(Kind::Array, "Array", std::vector<SortId>{domainSort, rangeSort}, SortId{newId}));
auto&& [newIt, inserted] = m_arraySorts.emplace(pair, SortId{newId});
smtAssert(inserted);
return newIt->second;
}
return it->second;
}
SortId SMTLib2Context::resolveTupleSort(TupleSort const& _sort)
{
auto const& tupleName = _sort.name;
auto it = m_tupleSorts.find(tupleName);
if (it == m_tupleSorts.end())
{
std::vector<std::pair<std::string, SortId>> accessors;
smtAssert(_sort.members.size() == _sort.components.size());
for (std::size_t i = 0u; i < _sort.members.size(); ++i)
accessors.emplace_back(_sort.members[i], resolve(_sort.components[i]));
auto newId = static_cast<uint32_t>(m_knownTypes.size());
m_knownTypes.emplace_back(std::make_unique<TupleType>(tupleName, std::move(accessors), SortId{newId}));
auto&& [newIt, inserted] = m_tupleSorts.emplace(tupleName, SortId{newId});
smtAssert(inserted);
if (m_callback)
m_callback(_sort);
return newIt->second;
}
return it->second;
}
std::string SMTLib2Context::toString(SortId _id)
{
auto const& sort = m_knownTypes.at(_id);
switch (sort->kind)
{
case Kind::Int:
return "Int";
case Kind::Bool:
return "Bool";
case Kind::BitVector:
return dynamic_cast<SMTLibSort const&>(*sort).name;
case Kind::Array:
{
auto const& arraySort = dynamic_cast<SMTLibSort const&>(*sort);
smtAssert(arraySort.args.size() == 2);
return "(Array " + toString(arraySort.args.at(0)) + ' ' + toString(arraySort.args.at(1)) + ')';
}
case Kind::Tuple:
{
auto const& tupleType = dynamic_cast<TupleType const&>(*sort);
return '|' + tupleType.name + '|';
}
default:
smtAssert(false, "Invalid SMT sort");
}
}
std::string SMTLib2Context::toSmtLibSort(solidity::smtutil::SortPointer const& _sort)
{
return toString(resolve(_sort));
}
std::string SMTLib2Context::toSExpr(Expression const& _expr)
{
if (_expr.arguments.empty())
return _expr.name;
std::string sexpr = "(";
if (_expr.name == "int2bv")
{
size_t size = std::stoul(_expr.arguments[1].name);
auto arg = toSExpr(_expr.arguments.front());
auto int2bv = "(_ int2bv " + std::to_string(size) + ")";
// Some solvers treat all BVs as unsigned, so we need to manually apply 2's complement if needed.
sexpr += std::string("ite ") +
"(>= " + arg + " 0) " +
"(" + int2bv + " " + arg + ") " +
"(bvneg (" + int2bv + " (- " + arg + ")))";
}
else if (_expr.name == "bv2int")
{
auto intSort = std::dynamic_pointer_cast<IntSort>(_expr.sort);
smtAssert(intSort, "");
auto arg = toSExpr(_expr.arguments.front());
auto nat = "(bv2nat " + arg + ")";
if (!intSort->isSigned)
return nat;
auto bvSort = std::dynamic_pointer_cast<BitVectorSort>(_expr.arguments.front().sort);
smtAssert(bvSort, "");
auto size = std::to_string(bvSort->size);
auto pos = std::to_string(bvSort->size - 1);
// Some solvers treat all BVs as unsigned, so we need to manually apply 2's complement if needed.
sexpr += std::string("ite ") +
"(= ((_ extract " + pos + " " + pos + ")" + arg + ") #b0) " +
nat + " " +
"(- (bv2nat (bvneg " + arg + ")))";
}
else if (_expr.name == "const_array")
{
smtAssert(_expr.arguments.size() == 2, "");
auto sortSort = std::dynamic_pointer_cast<SortSort>(_expr.arguments.at(0).sort);
smtAssert(sortSort, "");
auto arraySort = std::dynamic_pointer_cast<ArraySort>(sortSort->inner);
smtAssert(arraySort, "");
sexpr += "(as const " + toSmtLibSort(arraySort) + ") ";
sexpr += toSExpr(_expr.arguments.at(1));
}
else if (_expr.name == "tuple_get")
{
smtAssert(_expr.arguments.size() == 2, "");
auto tupleSort = std::dynamic_pointer_cast<TupleSort>(_expr.arguments.at(0).sort);
size_t index = std::stoul(_expr.arguments.at(1).name);
smtAssert(index < tupleSort->members.size(), "");
sexpr += "|" + tupleSort->members.at(index) + "| " + toSExpr(_expr.arguments.at(0));
}
else if (_expr.name == "tuple_constructor")
{
auto tupleSort = std::dynamic_pointer_cast<TupleSort>(_expr.sort);
smtAssert(tupleSort, "");
sexpr += "|" + tupleSort->name + "|";
for (auto const& arg: _expr.arguments)
sexpr += " " + toSExpr(arg);
}
else
{
sexpr += _expr.name;
for (auto const& arg: _expr.arguments)
sexpr += " " + toSExpr(arg);
}
sexpr += ")";
return sexpr;
}
std::optional<SortPointer> SMTLib2Context::getTupleType(std::string const& _name) const
{
auto it = m_tupleSorts.find(_name);
return it == m_tupleSorts.end() ? std::nullopt : std::optional<SortPointer>(unresolve(it->second));
}
std::optional<std::pair<std::string, SortPointer>> SMTLib2Context::getTupleAccessor(std::string const& _name) const
{
for (auto&& [_, sortId] : m_tupleSorts)
{
auto const& type = m_knownTypes.at(sortId);
smtAssert(type->kind == Kind::Tuple);
auto const& tupleType = dynamic_cast<TupleType const&>(*type);
for (auto&& [memberName, memberSort] : tupleType.accessors)
if (memberName == _name)
return std::make_pair(memberName, unresolve(memberSort));
}
return std::nullopt;
}
void SMTLib2Context::setTupleDeclarationCallback(TupleDeclarationCallback _callback)
{
m_callback = std::move(_callback);
}
} // namespace solidity::smtutil