-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathSMTLib2Interface.cpp
299 lines (259 loc) · 8.75 KB
/
SMTLib2Interface.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
/*
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/SMTLib2Interface.h>
#include <libsmtutil/SMTLib2Parser.h>
#include <libsolutil/Keccak256.h>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <range/v3/algorithm/find_if.hpp>
#include <array>
#include <fstream>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
using namespace solidity;
using namespace solidity::util;
using namespace solidity::frontend;
using namespace solidity::smtutil;
SMTLib2Interface::SMTLib2Interface(
std::map<h256, std::string> _queryResponses,
ReadCallback::Callback _smtCallback,
std::optional<unsigned> _queryTimeout
):
BMCSolverInterface(_queryTimeout),
m_queryResponses(std::move(_queryResponses)),
m_smtCallback(std::move(_smtCallback))
{
reset();
}
void SMTLib2Interface::reset()
{
m_commands.clear();
m_context.clear();
m_commands.setOption("produce-models", "true");
if (m_queryTimeout)
m_commands.setOption("timeout", std::to_string(*m_queryTimeout));
m_commands.setLogic("ALL");
m_context.setTupleDeclarationCallback([&](TupleSort const& tupleSort){
m_commands.declareTuple(
tupleSort.name,
tupleSort.members,
tupleSort.components
| ranges::views::transform([&](SortPointer const& sort){ return m_context.toSmtLibSort(sort); })
| ranges::to<std::vector>()
);
});
}
void SMTLib2Interface::push()
{
m_commands.push();
}
void SMTLib2Interface::pop()
{
m_commands.pop();
}
void SMTLib2Interface::declareVariable(std::string const& _name, SortPointer const& _sort)
{
smtAssert(_sort);
if (_sort->kind == Kind::Function)
declareFunction(_name, _sort);
else if (!m_context.isDeclared(_name))
{
m_context.declare(_name, _sort);
m_commands.declareVariable(_name, toSmtLibSort(_sort));
}
}
void SMTLib2Interface::declareFunction(std::string const& _name, SortPointer const& _sort)
{
smtAssert(_sort);
smtAssert(_sort->kind == Kind::Function);
if (!m_context.isDeclared(_name))
{
auto const& fSort = std::dynamic_pointer_cast<FunctionSort>(_sort);
auto domain = toSmtLibSort(fSort->domain);
std::string codomain = toSmtLibSort(fSort->codomain);
m_context.declare(_name, _sort);
m_commands.declareFunction(_name, domain, codomain);
}
}
void SMTLib2Interface::addAssertion(Expression const& _expr)
{
m_commands.assertion(toSExpr(_expr));
}
namespace
{
std::vector<std::string> parseValuesFromResponse(std::string const& _response)
{
std::stringstream ss(_response);
std::string answer;
ss >> answer;
smtSolverInteractionRequire(answer == "sat", "SMT: Parsing model values only possible after sat answer");
std::vector<SMTLib2Expression> parsedOutput;
SMTLib2Parser parser(ss);
try
{
while (!parser.isEOF())
parsedOutput.push_back(parser.parseExpression());
}
catch(SMTLib2Parser::ParsingException&)
{
smtSolverInteractionRequire(false, "Error during parsing SMT answer");
}
smtSolverInteractionRequire(parsedOutput.size() == 1, "SMT: Expected model values as a single s-expression");
auto const& values = parsedOutput[0];
smtSolverInteractionRequire(!isAtom(values), "Invalid format of values in SMT answer");
std::vector<std::string> parsedValues;
for (auto const& nameValuePair: asSubExpressions(values))
{
smtSolverInteractionRequire(!isAtom(nameValuePair), "Invalid format of values in SMT answer");
smtSolverInteractionRequire(asSubExpressions(nameValuePair).size() == 2, "Invalid format of values in SMT answer");
auto const& value = asSubExpressions(nameValuePair)[1];
parsedValues.push_back(value.toString());
}
return parsedValues;
}
} // namespace
std::pair<CheckResult, std::vector<std::string>> SMTLib2Interface::check(std::vector<Expression> const& _expressionsToEvaluate)
{
std::string response = querySolver(dumpQuery(_expressionsToEvaluate));
CheckResult result;
// TODO proper parsing
if (boost::starts_with(response, "sat"))
result = CheckResult::SATISFIABLE;
else if (boost::starts_with(response, "unsat"))
result = CheckResult::UNSATISFIABLE;
else if (boost::starts_with(response, "unknown"))
result = CheckResult::UNKNOWN;
else
result = CheckResult::ERROR;
std::vector<std::string> values;
if (result == CheckResult::SATISFIABLE && !_expressionsToEvaluate.empty())
values = parseValuesFromResponse(response);
return std::make_pair(result, values);
}
std::string SMTLib2Interface::toSmtLibSort(SortPointer _sort)
{
return m_context.toSmtLibSort(std::move(_sort));
}
std::vector<std::string> SMTLib2Interface::toSmtLibSort(std::vector<SortPointer> const& _sorts)
{
std::vector<std::string> ssorts;
ssorts.reserve(_sorts.size());
for (auto const& sort: _sorts)
ssorts.push_back(toSmtLibSort(sort));
return ssorts;
}
std::string SMTLib2Interface::toSExpr(solidity::smtutil::Expression const& _expr)
{
return m_context.toSExpr(_expr);
}
std::string SMTLib2Interface::checkSatAndGetValuesCommand(std::vector<Expression> const& _expressionsToEvaluate)
{
std::string command;
if (_expressionsToEvaluate.empty())
command = "(check-sat)\n";
else
{
// TODO make sure these are unique
for (size_t i = 0; i < _expressionsToEvaluate.size(); i++)
{
auto const& e = _expressionsToEvaluate.at(i);
smtAssert(e.sort->kind == Kind::Int || e.sort->kind == Kind::Bool, "Invalid sort for expression to evaluate.");
command += "(declare-const |EVALEXPR_" + std::to_string(i) + "| " + (e.sort->kind == Kind::Int ? "Int" : "Bool") + ")\n";
command += "(assert (= |EVALEXPR_" + std::to_string(i) + "| " + toSExpr(e) + "))\n";
}
command += "(check-sat)\n";
command += "(get-value (";
for (size_t i = 0; i < _expressionsToEvaluate.size(); i++)
command += "|EVALEXPR_" + std::to_string(i) + "| ";
command += "))\n";
}
return command;
}
std::string SMTLib2Interface::querySolver(std::string const& _input)
{
h256 inputHash = keccak256(_input);
if (m_queryResponses.count(inputHash))
return m_queryResponses.at(inputHash);
if (m_smtCallback)
{
setupSmtCallback();
auto result = m_smtCallback(ReadCallback::kindString(ReadCallback::Kind::SMTQuery), _input);
if (result.success)
return result.responseOrErrorMessage;
}
m_unhandledQueries.push_back(_input);
return "unknown\n";
}
std::string SMTLib2Interface::dumpQuery(std::vector<Expression> const& _expressionsToEvaluate)
{
return m_commands.toString() + '\n' + checkSatAndGetValuesCommand(_expressionsToEvaluate);
}
void SMTLib2Commands::push() {
m_frameLimits.push_back(m_commands.size());
}
void SMTLib2Commands::pop() {
smtAssert(!m_commands.empty());
auto limit = m_frameLimits.back();
m_frameLimits.pop_back();
while (m_commands.size() > limit)
m_commands.pop_back();
}
std::string SMTLib2Commands::toString() const {
return boost::algorithm::join(m_commands, "\n");
}
void SMTLib2Commands::clear() {
m_commands.clear();
m_frameLimits.clear();
}
void SMTLib2Commands::assertion(std::string _expr) {
m_commands.push_back("(assert " + std::move(_expr) + ')');
}
void SMTLib2Commands::setOption(std::string _name, std::string _value)
{
m_commands.push_back("(set-option :" + std::move(_name) + ' ' + std::move(_value) + ')');
}
void SMTLib2Commands::setLogic(std::string _logic)
{
m_commands.push_back("(set-logic " + std::move(_logic) + ')');
}
void SMTLib2Commands::declareVariable(std::string _name, std::string _sort)
{
m_commands.push_back("(declare-fun |" + std::move(_name) + "| () " + std::move(_sort) + ')');
}
void SMTLib2Commands::declareFunction(std::string const& _name, std::vector<std::string> const& _domain, std::string const& _codomain)
{
std::stringstream ss;
ss << "(declare-fun |" << _name << "| " << '(' << boost::join(_domain, " ")
<< ')' << ' ' << _codomain << ')';
m_commands.push_back(ss.str());
}
void SMTLib2Commands::declareTuple(
std::string const& _name,
std::vector<std::string> const& _memberNames,
std::vector<std::string> const& _memberSorts
)
{
auto quotedName = '|' + _name + '|';
std::stringstream ss;
ss << "(declare-datatypes ((" << quotedName << " 0)) (((" << quotedName;
for (auto && [memberName, memberSort]: ranges::views::zip(_memberNames, _memberSorts))
ss << " (|" << memberName << "| " << memberSort << ")";
ss << "))))";
auto declaration = ss.str();
m_commands.push_back(ss.str());
}