solidity/libsmtutil/SMTLib2Interface.cpp

334 lines
9.8 KiB
C++
Raw Normal View History

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/>.
*/
// 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
#include <libsolutil/Keccak256.h>
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
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
SMTLib2Interface::SMTLib2Interface(
std::map<h256, std::string> _queryResponses,
2020-11-02 20:20:20 +00:00
ReadCallback::Callback _smtCallback,
2023-06-26 08:31:02 +00:00
SMTSolverChoice _enabledSolvers,
std::optional<unsigned> _queryTimeout,
bool _dumpQuery
):
2020-11-02 20:20:20 +00:00
SolverInterface(_queryTimeout),
2022-08-23 17:28:45 +00:00
m_queryResponses(std::move(_queryResponses)),
2023-06-26 08:31:02 +00:00
m_smtCallback(std::move(_smtCallback)),
m_enabledSolvers(_enabledSolvers),
m_dumpQuery(_dumpQuery)
2017-07-10 19:21:11 +00:00
{
reset();
}
void SMTLib2Interface::reset()
{
m_accumulatedOutput.clear();
m_accumulatedOutput.emplace_back();
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 " + std::to_string(*m_queryTimeout) + ")");
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();
}
void SMTLib2Interface::declareVariable(std::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)
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) + ')');
}
}
void SMTLib2Interface::declareFunction(std::string const& _name, SortPointer const& _sort)
{
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
if (!m_variables.count(_name))
2018-04-17 21:46:53 +00:00
{
auto const& fSort = std::dynamic_pointer_cast<FunctionSort>(_sort);
std::string domain = toSmtLibSort(fSort->domain);
std::string codomain = toSmtLibSort(*fSort->codomain);
2019-09-24 15:35:31 +00:00
m_variables.emplace(_name, _sort);
2018-04-17 21:46:53 +00:00
write(
"(declare-fun |" +
_name +
"| " +
domain +
" " +
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
}
std::pair<CheckResult, std::vector<std::string>> SMTLib2Interface::check(std::vector<Expression> const& _expressionsToEvaluate)
2017-07-10 19:21:11 +00:00
{
std::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
std::vector<std::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 std::make_pair(result, values);
2017-07-10 19:21:11 +00:00
}
std::string SMTLib2Interface::toSExpr(Expression const& _expr)
2017-07-13 16:22:51 +00:00
{
if (_expr.arguments.empty())
return _expr.name;
std::string sexpr = "(";
2020-05-13 11:08:48 +00:00
if (_expr.name == "int2bv")
{
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 " + std::to_string(size) + ")";
2020-05-13 11:08:48 +00:00
// Some solvers treat all BVs as unsigned, so we need to manually apply 2's complement if needed.
sexpr += std::string("ite ") +
2020-05-13 11:08:48 +00:00
"(>= " + arg + " 0) " +
"(" + int2bv + " " + arg + ") " +
"(bvneg (" + int2bv + " (- " + arg + ")))";
}
else if (_expr.name == "bv2int")
{
auto intSort = std::dynamic_pointer_cast<IntSort>(_expr.sort);
2020-05-13 11:08:48 +00:00
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);
2020-05-13 11:08:48 +00:00
smtAssert(bvSort, "");
auto size = std::to_string(bvSort->size);
auto pos = std::to_string(bvSort->size - 1);
2020-05-13 11:08:48 +00:00
// Some solvers treat all BVs as unsigned, so we need to manually apply 2's complement if needed.
sexpr += std::string("ite ") +
2020-05-13 11:08:48 +00:00
"(= ((_ 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")
{
2020-05-19 12:52:31 +00:00
smtAssert(_expr.arguments.size() == 2, "");
auto sortSort = std::dynamic_pointer_cast<SortSort>(_expr.arguments.at(0).sort);
2020-05-19 12:52:31 +00:00
smtAssert(sortSort, "");
auto arraySort = std::dynamic_pointer_cast<ArraySort>(sortSort->inner);
2020-05-19 12:52:31 +00:00
smtAssert(arraySort, "");
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, "");
auto tupleSort = std::dynamic_pointer_cast<TupleSort>(_expr.arguments.at(0).sort);
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 = std::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
}
else
{
sexpr += _expr.name;
for (auto const& arg: _expr.arguments)
sexpr += " " + toSExpr(arg);
}
2017-07-13 16:22:51 +00:00
sexpr += ")";
return sexpr;
}
std::string SMTLib2Interface::toSmtLibSort(Sort const& _sort)
{
switch (_sort.kind)
{
case Kind::Int:
return "Int";
case Kind::Bool:
return "Bool";
2020-05-11 17:56:29 +00:00
case Kind::BitVector:
return "(_ BitVec " + std::to_string(dynamic_cast<BitVectorSort const&>(_sort).size) + ")";
case Kind::Array:
{
auto const& arraySort = dynamic_cast<ArraySort const&>(_sort);
2020-05-19 12:52:31 +00:00
smtAssert(arraySort.domain && arraySort.range, "");
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);
std::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
{
std::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
}
default:
2020-05-19 12:52:31 +00:00
smtAssert(false, "Invalid SMT sort");
}
}
std::string SMTLib2Interface::toSmtLibSort(std::vector<SortPointer> const& _sorts)
{
std::string ssort("(");
for (auto const& sort: _sorts)
ssort += toSmtLibSort(*sort) + " ";
ssort += ")";
return ssort;
}
void SMTLib2Interface::write(std::string _data)
2017-07-10 19:21:11 +00:00
{
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
}
std::string SMTLib2Interface::checkSatAndGetValuesCommand(std::vector<Expression> const& _expressionsToEvaluate)
2017-07-10 19:21:11 +00:00
{
std::string command;
2017-07-11 11:26:43 +00:00
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.");
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";
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_" + std::to_string(i) + "| ";
2017-07-11 11:26:43 +00:00
command += "))\n";
}
return command;
2017-07-10 19:21:11 +00:00
}
std::vector<std::string> SMTLib2Interface::parseValues(std::string::const_iterator _start, std::string::const_iterator _end)
2017-07-10 19:21:11 +00:00
{
std::vector<std::string> values;
2017-07-10 19:21:11 +00:00
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
std::string SMTLib2Interface::querySolver(std::string const& _input)
2017-07-13 19:06:29 +00:00
{
2019-12-11 16:31:36 +00:00
h256 inputHash = keccak256(_input);
if (m_queryResponses.count(inputHash))
return m_queryResponses.at(inputHash);
if (m_smtCallback)
{
auto result = m_smtCallback(ReadCallback::kindString(ReadCallback::Kind::SMTQuery), _input);
if (result.success)
return result.responseOrErrorMessage;
}
m_unhandledQueries.push_back(_input);
return "unknown\n";
2017-07-13 19:06:29 +00:00
}
std::string SMTLib2Interface::dumpQuery(std::vector<Expression> const& _expressionsToEvaluate)
{
return boost::algorithm::join(m_accumulatedOutput, "\n") +
checkSatAndGetValuesCommand(_expressionsToEvaluate);
}