solidity/libsmtutil/CHCSmtLib2Interface.cpp

174 lines
4.5 KiB
C++
Raw Normal View History

2019-09-24 15:35:31 +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
2019-09-24 15:35:31 +00:00
2020-05-18 15:42:24 +00:00
#include <libsmtutil/CHCSmtLib2Interface.h>
2019-09-24 15:35:31 +00:00
#include <libsolutil/Keccak256.h>
2019-09-24 15:35:31 +00:00
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <array>
#include <fstream>
#include <iostream>
#include <memory>
#include <stdexcept>
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;
2019-09-24 15:35:31 +00:00
CHCSmtLib2Interface::CHCSmtLib2Interface(
2020-12-03 21:40:30 +00:00
map<h256, string> const& _queryResponses,
2020-11-02 20:20:20 +00:00
ReadCallback::Callback _smtCallback,
optional<unsigned> _queryTimeout
):
2020-11-02 20:20:20 +00:00
CHCSolverInterface(_queryTimeout),
m_smtlib2(make_unique<SMTLib2Interface>(_queryResponses, _smtCallback, m_queryTimeout)),
m_queryResponses(move(_queryResponses)),
m_smtCallback(_smtCallback)
2019-09-24 15:35:31 +00:00
{
reset();
}
void CHCSmtLib2Interface::reset()
{
m_accumulatedOutput.clear();
m_variables.clear();
2020-09-02 08:45:47 +00:00
m_unhandledQueries.clear();
2020-11-02 20:20:20 +00:00
if (m_queryTimeout)
write("(set-option :timeout " + to_string(*m_queryTimeout) + ")");
2019-09-24 15:35:31 +00:00
}
2020-05-19 12:14:46 +00:00
void CHCSmtLib2Interface::registerRelation(Expression const& _expr)
2019-09-24 15:35:31 +00:00
{
2020-05-19 12:52:31 +00:00
smtAssert(_expr.sort, "");
smtAssert(_expr.sort->kind == Kind::Function, "");
2019-09-24 15:35:31 +00:00
if (!m_variables.count(_expr.name))
{
auto fSort = dynamic_pointer_cast<FunctionSort>(_expr.sort);
string domain = m_smtlib2->toSmtLibSort(fSort->domain);
// Relations are predicates which have implicit codomain Bool.
m_variables.insert(_expr.name);
write(
"(declare-rel |" +
_expr.name +
"| " +
domain +
")"
);
}
}
2020-05-19 12:14:46 +00:00
void CHCSmtLib2Interface::addRule(Expression const& _expr, std::string const& _name)
2019-09-24 15:35:31 +00:00
{
write(
"(rule (! " +
m_smtlib2->toSExpr(_expr) +
" :named " +
_name +
"))"
);
}
2020-07-07 14:22:51 +00:00
pair<CheckResult, CHCSolverInterface::CexGraph> CHCSmtLib2Interface::query(Expression const& _block)
2019-09-24 15:35:31 +00:00
{
string accumulated{};
swap(m_accumulatedOutput, accumulated);
for (auto const& var: m_smtlib2->variables())
declareVariable(var.first, var.second);
m_accumulatedOutput += accumulated;
string response = querySolver(
m_accumulatedOutput +
"\n(query " + _block.name + " :print-certificate true)"
);
CheckResult result;
// TODO proper parsing
if (boost::starts_with(response, "sat\n"))
result = CheckResult::SATISFIABLE;
else if (boost::starts_with(response, "unsat\n"))
result = CheckResult::UNSATISFIABLE;
else if (boost::starts_with(response, "unknown\n"))
result = CheckResult::UNKNOWN;
else
result = CheckResult::ERROR;
// TODO collect invariants or counterexamples.
2020-07-07 14:22:51 +00:00
return {result, {}};
2019-09-24 15:35:31 +00:00
}
void CHCSmtLib2Interface::declareVariable(string const& _name, SortPointer const& _sort)
{
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))
{
m_variables.insert(_name);
write("(declare-var |" + _name + "| " + m_smtlib2->toSmtLibSort(*_sort) + ')');
}
}
void CHCSmtLib2Interface::declareFunction(string const& _name, SortPointer const& _sort)
{
2020-05-19 12:52:31 +00:00
smtAssert(_sort, "");
smtAssert(_sort->kind == Kind::Function, "");
2019-09-24 15:35:31 +00:00
// TODO Use domain and codomain as key as well
if (!m_variables.count(_name))
{
auto fSort = dynamic_pointer_cast<FunctionSort>(_sort);
2020-05-19 12:52:31 +00:00
smtAssert(fSort->codomain, "");
2019-09-24 15:35:31 +00:00
string domain = m_smtlib2->toSmtLibSort(fSort->domain);
string codomain = m_smtlib2->toSmtLibSort(*fSort->codomain);
m_variables.insert(_name);
write(
"(declare-fun |" +
_name +
"| " +
domain +
" " +
codomain +
")"
);
}
}
void CHCSmtLib2Interface::write(string _data)
{
m_accumulatedOutput += move(_data) + "\n";
}
string CHCSmtLib2Interface::querySolver(string const& _input)
{
2019-12-11 16:31:36 +00:00
util::h256 inputHash = util::keccak256(_input);
2019-09-24 15:35:31 +00:00
if (m_queryResponses.count(inputHash))
return m_queryResponses.at(inputHash);
if (m_smtCallback)
2019-09-24 15:35:31 +00:00
{
auto result = m_smtCallback(ReadCallback::kindString(ReadCallback::Kind::SMTQuery), _input);
if (result.success)
return result.responseOrErrorMessage;
2019-09-24 15:35:31 +00:00
}
m_unhandledQueries.push_back(_input);
return "unknown\n";
2019-09-24 15:35:31 +00:00
}