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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/formal/SMTLib2Interface.h>
|
|
|
|
|
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-11 11:26:43 +00:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2017-07-10 19:21:11 +00:00
|
|
|
|
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>
|
|
|
|
|
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;
|
|
|
|
using namespace solidity::frontend::smt;
|
2017-07-10 16:13:38 +00:00
|
|
|
|
2019-09-17 14:06:43 +00:00
|
|
|
SMTLib2Interface::SMTLib2Interface(
|
|
|
|
map<h256, string> const& _queryResponses,
|
|
|
|
ReadCallback::Callback const& _smtCallback
|
|
|
|
):
|
|
|
|
m_queryResponses(_queryResponses),
|
|
|
|
m_smtCallback(_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();
|
2017-07-11 11:26:43 +00:00
|
|
|
write("(set-option :produce-models true)");
|
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()
|
|
|
|
{
|
|
|
|
solAssert(!m_accumulatedOutput.empty(), "");
|
|
|
|
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
|
|
|
{
|
2019-09-24 15:35:31 +00:00
|
|
|
solAssert(_sort, "");
|
|
|
|
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
|
|
|
{
|
2019-09-24 15:35:31 +00:00
|
|
|
solAssert(_sort, "");
|
|
|
|
solAssert(_sort->kind == smt::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
|
|
|
}
|
|
|
|
|
2019-09-02 12:27:35 +00:00
|
|
|
void SMTLib2Interface::addAssertion(smt::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
|
|
|
}
|
|
|
|
|
2019-09-02 12:27:35 +00:00
|
|
|
pair<CheckResult, vector<string>> SMTLib2Interface::check(vector<smt::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;
|
2018-07-27 12:13:22 +00:00
|
|
|
if (result == CheckResult::SATISFIABLE && result != CheckResult::ERROR)
|
2017-07-10 19:21:11 +00:00
|
|
|
values = parseValues(find(response.cbegin(), response.cend(), '\n'), response.cend());
|
|
|
|
return make_pair(result, values);
|
|
|
|
}
|
|
|
|
|
2019-09-02 12:27:35 +00:00
|
|
|
string SMTLib2Interface::toSExpr(smt::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 = "(";
|
|
|
|
if (_expr.name == "const_array")
|
|
|
|
{
|
|
|
|
solAssert(_expr.arguments.size() == 2, "");
|
|
|
|
auto sortSort = std::dynamic_pointer_cast<SortSort>(_expr.arguments.at(0).sort);
|
|
|
|
solAssert(sortSort, "");
|
|
|
|
auto arraySort = dynamic_pointer_cast<ArraySort>(sortSort->inner);
|
|
|
|
solAssert(arraySort, "");
|
|
|
|
sexpr += "(as const " + toSmtLibSort(*arraySort) + ") ";
|
|
|
|
sexpr += toSExpr(_expr.arguments.at(1));
|
|
|
|
}
|
|
|
|
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";
|
2018-11-22 10:24:12 +00:00
|
|
|
case Kind::Array:
|
|
|
|
{
|
|
|
|
auto const& arraySort = dynamic_cast<ArraySort const&>(_sort);
|
2019-09-24 15:35:31 +00:00
|
|
|
solAssert(arraySort.domain && arraySort.range, "");
|
2018-11-22 10:24:12 +00:00
|
|
|
return "(Array " + toSmtLibSort(*arraySort.domain) + ' ' + toSmtLibSort(*arraySort.range) + ')';
|
|
|
|
}
|
2018-10-25 14:00:09 +00:00
|
|
|
default:
|
|
|
|
solAssert(false, "Invalid SMT sort");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
solAssert(!m_accumulatedOutput.empty(), "");
|
2017-07-11 11:26:43 +00:00
|
|
|
m_accumulatedOutput.back() += move(_data) + "\n";
|
2017-07-10 19:21:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 12:27:35 +00:00
|
|
|
string SMTLib2Interface::checkSatAndGetValuesCommand(vector<smt::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);
|
2018-11-21 14:13:50 +00:00
|
|
|
solAssert(e.sort->kind == Kind::Int || e.sort->kind == Kind::Bool, "Invalid sort for expression to evaluate.");
|
|
|
|
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
|
|
|
}
|