2017-07-10 16:13:38 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-07-10 16:13:38 +00:00
|
|
|
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/SMTLib2Interface.h>
|
2017-07-10 16:13:38 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Keccak256.h>
|
2017-10-13 13:19:53 +00:00
|
|
|
|
2017-07-10 19:21:11 +00:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2018-12-17 17:26:10 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2017-07-10 19:21:11 +00:00
|
|
|
|
2021-05-18 17:12:06 +00:00
|
|
|
#include <range/v3/algorithm/find_if.hpp>
|
|
|
|
|
2018-12-17 17:26:10 +00:00
|
|
|
#include <array>
|
2017-07-10 19:21:11 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
#include <stdexcept>
|
2020-04-03 13:10:16 +00:00
|
|
|
#include <string>
|
2020-04-01 03:04:29 +00:00
|
|
|
#include <utility>
|
2017-07-10 19:21:11 +00:00
|
|
|
|
2017-07-10 16:13:38 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::frontend;
|
2020-05-19 12:14:46 +00:00
|
|
|
using namespace solidity::smtutil;
|
2017-07-10 16:13:38 +00:00
|
|
|
|
2019-09-17 14:06:43 +00:00
|
|
|
SMTLib2Interface::SMTLib2Interface(
|
2020-05-11 17:56:29 +00:00
|
|
|
map<h256, string> _queryResponses,
|
2020-11-02 20:20:20 +00:00
|
|
|
ReadCallback::Callback _smtCallback,
|
|
|
|
optional<unsigned> _queryTimeout
|
2019-09-17 14:06:43 +00:00
|
|
|
):
|
2020-11-02 20:20:20 +00:00
|
|
|
SolverInterface(_queryTimeout),
|
2022-08-23 17:28:45 +00:00
|
|
|
m_queryResponses(std::move(_queryResponses)),
|
|
|
|
m_smtCallback(std::move(_smtCallback))
|
2017-07-10 19:21:11 +00:00
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTLib2Interface::reset()
|
|
|
|
{
|
|
|
|
m_accumulatedOutput.clear();
|
|
|
|
m_accumulatedOutput.emplace_back();
|
2018-11-21 15:57:02 +00:00
|
|
|
m_variables.clear();
|
2020-04-03 13:10:16 +00:00
|
|
|
m_userSorts.clear();
|
2017-07-11 11:26:43 +00:00
|
|
|
write("(set-option :produce-models true)");
|
2020-11-02 20:20:20 +00:00
|
|
|
if (m_queryTimeout)
|
|
|
|
write("(set-option :timeout " + to_string(*m_queryTimeout) + ")");
|
2019-09-18 20:33:00 +00:00
|
|
|
write("(set-logic ALL)");
|
2017-07-10 19:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SMTLib2Interface::push()
|
|
|
|
{
|
|
|
|
m_accumulatedOutput.emplace_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTLib2Interface::pop()
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(!m_accumulatedOutput.empty(), "");
|
2017-07-10 19:21:11 +00:00
|
|
|
m_accumulatedOutput.pop_back();
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:35:31 +00:00
|
|
|
void SMTLib2Interface::declareVariable(string const& _name, SortPointer const& _sort)
|
2017-07-10 19:21:11 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_sort, "");
|
2019-09-24 15:35:31 +00:00
|
|
|
if (_sort->kind == Kind::Function)
|
2018-11-21 15:57:02 +00:00
|
|
|
declareFunction(_name, _sort);
|
|
|
|
else if (!m_variables.count(_name))
|
|
|
|
{
|
2019-09-24 15:35:31 +00:00
|
|
|
m_variables.emplace(_name, _sort);
|
|
|
|
write("(declare-fun |" + _name + "| () " + toSmtLibSort(*_sort) + ')');
|
2018-11-21 15:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:35:31 +00:00
|
|
|
void SMTLib2Interface::declareFunction(string const& _name, SortPointer const& _sort)
|
2018-11-21 15:57:02 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_sort, "");
|
|
|
|
smtAssert(_sort->kind == Kind::Function, "");
|
2018-04-17 21:46:53 +00:00
|
|
|
// TODO Use domain and codomain as key as well
|
2018-11-21 15:57:02 +00:00
|
|
|
if (!m_variables.count(_name))
|
2018-04-17 21:46:53 +00:00
|
|
|
{
|
2019-09-24 15:35:31 +00:00
|
|
|
auto const& fSort = dynamic_pointer_cast<FunctionSort>(_sort);
|
|
|
|
string domain = toSmtLibSort(fSort->domain);
|
|
|
|
string codomain = toSmtLibSort(*fSort->codomain);
|
|
|
|
m_variables.emplace(_name, _sort);
|
2018-04-17 21:46:53 +00:00
|
|
|
write(
|
|
|
|
"(declare-fun |" +
|
|
|
|
_name +
|
2018-11-21 15:57:02 +00:00
|
|
|
"| " +
|
2018-10-25 14:00:09 +00:00
|
|
|
domain +
|
2018-11-21 15:57:02 +00:00
|
|
|
" " +
|
|
|
|
codomain +
|
2018-04-17 21:46:53 +00:00
|
|
|
")"
|
|
|
|
);
|
|
|
|
}
|
2017-07-10 19:21:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
void SMTLib2Interface::addAssertion(Expression const& _expr)
|
2017-07-10 19:21:11 +00:00
|
|
|
{
|
2017-07-13 16:22:51 +00:00
|
|
|
write("(assert " + toSExpr(_expr) + ")");
|
2017-07-10 19:21:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
pair<CheckResult, vector<string>> SMTLib2Interface::check(vector<Expression> const& _expressionsToEvaluate)
|
2017-07-10 19:21:11 +00:00
|
|
|
{
|
2017-07-13 19:06:29 +00:00
|
|
|
string response = querySolver(
|
2017-07-11 11:26:43 +00:00
|
|
|
boost::algorithm::join(m_accumulatedOutput, "\n") +
|
|
|
|
checkSatAndGetValuesCommand(_expressionsToEvaluate)
|
|
|
|
);
|
2017-07-13 19:06:29 +00:00
|
|
|
|
2017-07-10 19:21:11 +00:00
|
|
|
CheckResult result;
|
|
|
|
// TODO proper parsing
|
|
|
|
if (boost::starts_with(response, "sat\n"))
|
2017-08-21 15:02:47 +00:00
|
|
|
result = CheckResult::SATISFIABLE;
|
2017-07-10 19:21:11 +00:00
|
|
|
else if (boost::starts_with(response, "unsat\n"))
|
2017-08-21 15:02:47 +00:00
|
|
|
result = CheckResult::UNSATISFIABLE;
|
2017-07-10 19:21:11 +00:00
|
|
|
else if (boost::starts_with(response, "unknown\n"))
|
|
|
|
result = CheckResult::UNKNOWN;
|
|
|
|
else
|
2017-07-11 11:26:43 +00:00
|
|
|
result = CheckResult::ERROR;
|
2017-07-10 19:21:11 +00:00
|
|
|
|
|
|
|
vector<string> values;
|
2020-05-13 11:08:48 +00:00
|
|
|
if (result == CheckResult::SATISFIABLE && !_expressionsToEvaluate.empty())
|
2017-07-10 19:21:11 +00:00
|
|
|
values = parseValues(find(response.cbegin(), response.cend(), '\n'), response.cend());
|
|
|
|
return make_pair(result, values);
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
string SMTLib2Interface::toSExpr(Expression const& _expr)
|
2017-07-13 16:22:51 +00:00
|
|
|
{
|
|
|
|
if (_expr.arguments.empty())
|
|
|
|
return _expr.name;
|
2019-09-18 20:33:00 +00:00
|
|
|
|
|
|
|
std::string sexpr = "(";
|
2020-05-13 11:08:48 +00:00
|
|
|
if (_expr.name == "int2bv")
|
|
|
|
{
|
2020-06-15 10:51:58 +00:00
|
|
|
size_t size = std::stoul(_expr.arguments[1].name);
|
2020-05-13 11:08:48 +00:00
|
|
|
auto arg = toSExpr(_expr.arguments.front());
|
|
|
|
auto int2bv = "(_ int2bv " + to_string(size) + ")";
|
|
|
|
// Some solvers treat all BVs as unsigned, so we need to manually apply 2's complement if needed.
|
|
|
|
sexpr += string("ite ") +
|
|
|
|
"(>= " + arg + " 0) " +
|
|
|
|
"(" + int2bv + " " + arg + ") " +
|
|
|
|
"(bvneg (" + int2bv + " (- " + arg + ")))";
|
|
|
|
}
|
|
|
|
else if (_expr.name == "bv2int")
|
|
|
|
{
|
|
|
|
auto intSort = 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 = dynamic_pointer_cast<BitVectorSort>(_expr.arguments.front().sort);
|
|
|
|
smtAssert(bvSort, "");
|
|
|
|
auto size = to_string(bvSort->size);
|
|
|
|
auto pos = to_string(bvSort->size - 1);
|
|
|
|
|
|
|
|
// Some solvers treat all BVs as unsigned, so we need to manually apply 2's complement if needed.
|
|
|
|
sexpr += string("ite ") +
|
|
|
|
"(= ((_ extract " + pos + " " + pos + ")" + arg + ") #b0) " +
|
|
|
|
nat + " " +
|
2021-05-18 21:01:13 +00:00
|
|
|
"(- (bv2nat (bvneg " + arg + ")))";
|
2020-05-13 11:08:48 +00:00
|
|
|
}
|
|
|
|
else if (_expr.name == "const_array")
|
2019-09-18 20:33:00 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_expr.arguments.size() == 2, "");
|
2019-09-18 20:33:00 +00:00
|
|
|
auto sortSort = std::dynamic_pointer_cast<SortSort>(_expr.arguments.at(0).sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(sortSort, "");
|
2019-09-18 20:33:00 +00:00
|
|
|
auto arraySort = dynamic_pointer_cast<ArraySort>(sortSort->inner);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(arraySort, "");
|
2019-09-18 20:33:00 +00:00
|
|
|
sexpr += "(as const " + toSmtLibSort(*arraySort) + ") ";
|
|
|
|
sexpr += toSExpr(_expr.arguments.at(1));
|
|
|
|
}
|
2020-04-03 13:10:16 +00:00
|
|
|
else if (_expr.name == "tuple_get")
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_expr.arguments.size() == 2, "");
|
2020-04-03 13:10:16 +00:00
|
|
|
auto tupleSort = dynamic_pointer_cast<TupleSort>(_expr.arguments.at(0).sort);
|
2020-06-02 13:37:45 +00:00
|
|
|
size_t index = std::stoul(_expr.arguments.at(1).name);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(index < tupleSort->members.size(), "");
|
2020-04-14 09:09:38 +00:00
|
|
|
sexpr += "|" + tupleSort->members.at(index) + "| " + toSExpr(_expr.arguments.at(0));
|
|
|
|
}
|
|
|
|
else if (_expr.name == "tuple_constructor")
|
|
|
|
{
|
|
|
|
auto tupleSort = dynamic_pointer_cast<TupleSort>(_expr.sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(tupleSort, "");
|
2020-04-14 09:09:38 +00:00
|
|
|
sexpr += "|" + tupleSort->name + "|";
|
|
|
|
for (auto const& arg: _expr.arguments)
|
|
|
|
sexpr += " " + toSExpr(arg);
|
2020-04-03 13:10:16 +00:00
|
|
|
}
|
2019-09-18 20:33:00 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sexpr += _expr.name;
|
|
|
|
for (auto const& arg: _expr.arguments)
|
|
|
|
sexpr += " " + toSExpr(arg);
|
|
|
|
}
|
2017-07-13 16:22:51 +00:00
|
|
|
sexpr += ")";
|
|
|
|
return sexpr;
|
|
|
|
}
|
|
|
|
|
2018-11-21 14:13:50 +00:00
|
|
|
string SMTLib2Interface::toSmtLibSort(Sort const& _sort)
|
2018-10-25 14:00:09 +00:00
|
|
|
{
|
2018-11-21 14:13:50 +00:00
|
|
|
switch (_sort.kind)
|
2018-10-25 14:00:09 +00:00
|
|
|
{
|
2018-11-21 14:13:50 +00:00
|
|
|
case Kind::Int:
|
2018-10-25 14:00:09 +00:00
|
|
|
return "Int";
|
2018-11-21 14:13:50 +00:00
|
|
|
case Kind::Bool:
|
2018-10-25 14:00:09 +00:00
|
|
|
return "Bool";
|
2020-05-11 17:56:29 +00:00
|
|
|
case Kind::BitVector:
|
|
|
|
return "(_ BitVec " + to_string(dynamic_cast<BitVectorSort const&>(_sort).size) + ")";
|
2018-11-22 10:24:12 +00:00
|
|
|
case Kind::Array:
|
|
|
|
{
|
|
|
|
auto const& arraySort = dynamic_cast<ArraySort const&>(_sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(arraySort.domain && arraySort.range, "");
|
2018-11-22 10:24:12 +00:00
|
|
|
return "(Array " + toSmtLibSort(*arraySort.domain) + ' ' + toSmtLibSort(*arraySort.range) + ')';
|
|
|
|
}
|
2020-04-03 13:10:16 +00:00
|
|
|
case Kind::Tuple:
|
|
|
|
{
|
|
|
|
auto const& tupleSort = dynamic_cast<TupleSort const&>(_sort);
|
2020-04-14 09:09:38 +00:00
|
|
|
string tupleName = "|" + tupleSort.name + "|";
|
2021-05-18 17:12:06 +00:00
|
|
|
auto isName = [&](auto entry) { return entry.first == tupleName; };
|
|
|
|
if (ranges::find_if(m_userSorts, isName) == m_userSorts.end())
|
2020-04-03 13:10:16 +00:00
|
|
|
{
|
2020-04-14 09:09:38 +00:00
|
|
|
string decl("(declare-datatypes ((" + tupleName + " 0)) (((" + tupleName);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(tupleSort.members.size() == tupleSort.components.size(), "");
|
2020-04-03 13:10:16 +00:00
|
|
|
for (unsigned i = 0; i < tupleSort.members.size(); ++i)
|
2020-04-14 09:09:38 +00:00
|
|
|
decl += " (|" + tupleSort.members.at(i) + "| " + toSmtLibSort(*tupleSort.components.at(i)) + ")";
|
2020-04-03 13:10:16 +00:00
|
|
|
decl += "))))";
|
2021-05-18 17:12:06 +00:00
|
|
|
m_userSorts.emplace_back(tupleName, decl);
|
2020-04-03 13:10:16 +00:00
|
|
|
write(decl);
|
|
|
|
}
|
|
|
|
|
2020-04-14 09:09:38 +00:00
|
|
|
return tupleName;
|
2020-04-03 13:10:16 +00:00
|
|
|
}
|
2018-10-25 14:00:09 +00:00
|
|
|
default:
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, "Invalid SMT sort");
|
2018-10-25 14:00:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:57:02 +00:00
|
|
|
string SMTLib2Interface::toSmtLibSort(vector<SortPointer> const& _sorts)
|
|
|
|
{
|
|
|
|
string ssort("(");
|
|
|
|
for (auto const& sort: _sorts)
|
|
|
|
ssort += toSmtLibSort(*sort) + " ";
|
|
|
|
ssort += ")";
|
|
|
|
return ssort;
|
|
|
|
}
|
|
|
|
|
2017-07-10 19:21:11 +00:00
|
|
|
void SMTLib2Interface::write(string _data)
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(!m_accumulatedOutput.empty(), "");
|
2022-08-23 17:28:45 +00:00
|
|
|
m_accumulatedOutput.back() += std::move(_data) + "\n";
|
2017-07-10 19:21:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
string SMTLib2Interface::checkSatAndGetValuesCommand(vector<Expression> const& _expressionsToEvaluate)
|
2017-07-10 19:21:11 +00:00
|
|
|
{
|
2017-07-11 11:26:43 +00:00
|
|
|
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);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(e.sort->kind == Kind::Int || e.sort->kind == Kind::Bool, "Invalid sort for expression to evaluate.");
|
2018-11-21 14:13:50 +00:00
|
|
|
command += "(declare-const |EVALEXPR_" + to_string(i) + "| " + (e.sort->kind == Kind::Int ? "Int" : "Bool") + ")\n";
|
2017-07-13 16:22:51 +00:00
|
|
|
command += "(assert (= |EVALEXPR_" + to_string(i) + "| " + toSExpr(e) + "))\n";
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
command += "(check-sat)\n";
|
|
|
|
command += "(get-value (";
|
|
|
|
for (size_t i = 0; i < _expressionsToEvaluate.size(); i++)
|
|
|
|
command += "|EVALEXPR_" + to_string(i) + "| ";
|
|
|
|
command += "))\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return command;
|
2017-07-10 19:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> SMTLib2Interface::parseValues(string::const_iterator _start, string::const_iterator _end)
|
|
|
|
{
|
|
|
|
vector<string> values;
|
|
|
|
while (_start < _end)
|
|
|
|
{
|
|
|
|
auto valStart = find(_start, _end, ' ');
|
|
|
|
if (valStart < _end)
|
|
|
|
++valStart;
|
|
|
|
auto valEnd = find(valStart, _end, ')');
|
|
|
|
values.emplace_back(valStart, valEnd);
|
|
|
|
_start = find(valEnd, _end, '(');
|
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
2017-07-13 19:06:29 +00:00
|
|
|
|
|
|
|
string SMTLib2Interface::querySolver(string const& _input)
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
h256 inputHash = keccak256(_input);
|
2018-08-03 15:04:56 +00:00
|
|
|
if (m_queryResponses.count(inputHash))
|
|
|
|
return m_queryResponses.at(inputHash);
|
2019-09-17 14:06:43 +00:00
|
|
|
if (m_smtCallback)
|
2017-10-13 13:19:53 +00:00
|
|
|
{
|
2019-09-17 14:06:43 +00:00
|
|
|
auto result = m_smtCallback(ReadCallback::kindString(ReadCallback::Kind::SMTQuery), _input);
|
|
|
|
if (result.success)
|
|
|
|
return result.responseOrErrorMessage;
|
2017-10-13 13:19:53 +00:00
|
|
|
}
|
2019-09-17 14:06:43 +00:00
|
|
|
m_unhandledQueries.push_back(_input);
|
|
|
|
return "unknown\n";
|
2017-07-13 19:06:29 +00:00
|
|
|
}
|