solidity/libsmtutil/CHCSmtLib2Interface.cpp

231 lines
6.0 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>
2021-05-20 16:07:40 +00:00
#include <range/v3/view.hpp>
2019-09-24 15:35:31 +00:00
#include <array>
#include <fstream>
#include <iostream>
#include <memory>
#include <stdexcept>
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(
std::map<h256, std::string> const& _queryResponses,
2020-11-02 20:20:20 +00:00
ReadCallback::Callback _smtCallback,
SMTSolverChoice _enabledSolvers,
std::optional<unsigned> _queryTimeout
):
2020-11-02 20:20:20 +00:00
CHCSolverInterface(_queryTimeout),
2023-06-26 08:31:02 +00:00
m_smtlib2(std::make_unique<SMTLib2Interface>(_queryResponses, _smtCallback, _enabledSolvers, m_queryTimeout)),
2022-08-23 17:28:45 +00:00
m_queryResponses(std::move(_queryResponses)),
m_smtCallback(_smtCallback),
m_enabledSolvers(_enabledSolvers)
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();
2021-05-18 17:12:06 +00:00
m_sortNames.clear();
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
{
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 = std::dynamic_pointer_cast<FunctionSort>(_expr.sort);
std::string domain = toSmtLibSort(fSort->domain);
2019-09-24 15:35:31 +00:00
// Relations are predicates which have implicit codomain Bool.
m_variables.insert(_expr.name);
write(
2021-05-18 17:12:06 +00:00
"(declare-fun |" +
2019-09-24 15:35:31 +00:00
_expr.name +
"| " +
domain +
2021-05-18 17:12:06 +00:00
" Bool)"
2019-09-24 15:35:31 +00:00
);
}
}
2021-05-18 17:12:06 +00:00
void CHCSmtLib2Interface::addRule(Expression const& _expr, std::string const& /*_name*/)
2019-09-24 15:35:31 +00:00
{
write(
2021-05-18 17:12:06 +00:00
"(assert\n(forall " + forall() + "\n" +
2019-09-24 15:35:31 +00:00
m_smtlib2->toSExpr(_expr) +
2021-05-18 17:12:06 +00:00
"))\n\n"
2019-09-24 15:35:31 +00:00
);
}
std::tuple<CheckResult, Expression, CHCSolverInterface::CexGraph> CHCSmtLib2Interface::query(Expression const& _block)
2019-09-24 15:35:31 +00:00
{
std::string query = dumpQuery(_block);
std::string response = querySolver(query);
2019-09-24 15:35:31 +00:00
CheckResult result;
// TODO proper parsing
2021-05-18 17:12:06 +00:00
if (boost::starts_with(response, "sat"))
2019-09-24 15:35:31 +00:00
result = CheckResult::UNSATISFIABLE;
2021-05-18 17:12:06 +00:00
else if (boost::starts_with(response, "unsat"))
result = CheckResult::SATISFIABLE;
else if (boost::starts_with(response, "unknown"))
2019-09-24 15:35:31 +00:00
result = CheckResult::UNKNOWN;
else
result = CheckResult::ERROR;
2021-10-06 09:44:33 +00:00
return {result, Expression(true), {}};
2019-09-24 15:35:31 +00:00
}
void CHCSmtLib2Interface::declareVariable(std::string const& _name, SortPointer const& _sort)
2019-09-24 15:35: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);
2021-05-18 17:12:06 +00:00
write("(declare-var |" + _name + "| " + toSmtLibSort(*_sort) + ')');
}
}
std::string CHCSmtLib2Interface::toSmtLibSort(Sort const& _sort)
2021-05-18 17:12:06 +00:00
{
if (!m_sortNames.count(&_sort))
m_sortNames[&_sort] = m_smtlib2->toSmtLibSort(_sort);
return m_sortNames.at(&_sort);
}
std::string CHCSmtLib2Interface::toSmtLibSort(std::vector<SortPointer> const& _sorts)
2021-05-18 17:12:06 +00:00
{
std::string ssort("(");
2021-05-18 17:12:06 +00:00
for (auto const& sort: _sorts)
ssort += toSmtLibSort(*sort) + " ";
ssort += ")";
return ssort;
}
std::string CHCSmtLib2Interface::forall()
2021-05-18 17:12:06 +00:00
{
std::string vars("(");
2021-05-18 17:12:06 +00:00
for (auto const& [name, sort]: m_smtlib2->variables())
{
solAssert(sort, "");
if (sort->kind != Kind::Function)
vars += " (" + name + " " + toSmtLibSort(*sort) + ")";
2019-09-24 15:35:31 +00:00
}
2021-05-18 17:12:06 +00:00
vars += ")";
return vars;
2019-09-24 15:35:31 +00:00
}
void CHCSmtLib2Interface::declareFunction(std::string const& _name, SortPointer const& _sort)
2019-09-24 15:35: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 = std::dynamic_pointer_cast<FunctionSort>(_sort);
smtAssert(fSort->codomain);
std::string domain = toSmtLibSort(fSort->domain);
std::string codomain = toSmtLibSort(*fSort->codomain);
2019-09-24 15:35:31 +00:00
m_variables.insert(_name);
write(
"(declare-fun |" +
_name +
"| " +
domain +
" " +
codomain +
")"
);
}
}
void CHCSmtLib2Interface::write(std::string _data)
2019-09-24 15:35:31 +00:00
{
2022-08-23 17:28:45 +00:00
m_accumulatedOutput += std::move(_data) + "\n";
2019-09-24 15:35:31 +00:00
}
std::string CHCSmtLib2Interface::querySolver(std::string const& _input)
2019-09-24 15:35:31 +00:00
{
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);
2023-06-26 11:41:17 +00:00
smtAssert(m_enabledSolvers.eld || m_enabledSolvers.z3);
smtAssert(m_smtCallback, "Callback must be set!");
std::string solverBinary = [&](){
if (m_enabledSolvers.eld)
return "eld";
if (m_enabledSolvers.z3)
2023-06-27 09:49:20 +00:00
return "z3 rlimit=1000000";
return "";
}();
auto result = m_smtCallback(ReadCallback::kindString(ReadCallback::Kind::SMTQuery) + " " + solverBinary, _input);
if (result.success)
return result.responseOrErrorMessage;
m_unhandledQueries.push_back(_input);
return "unknown\n";
2019-09-24 15:35:31 +00:00
}
std::string CHCSmtLib2Interface::dumpQuery(Expression const& _expr)
{
std::stringstream s;
s
<< createHeaderAndDeclarations()
<< m_accumulatedOutput << std::endl
<< createQueryAssertion(_expr.name) << std::endl
<< "(check-sat)" << std::endl;
return s.str();
}
std::string CHCSmtLib2Interface::createHeaderAndDeclarations() {
std::stringstream s;
if (m_queryTimeout)
s << "(set-option :timeout " + std::to_string(*m_queryTimeout) + ")\n";
s << "(set-logic HORN)" << std::endl;
for (auto const& decl: m_smtlib2->userSorts() | ranges::views::values)
s << decl << std::endl;
return s.str();
}
std::string CHCSmtLib2Interface::createQueryAssertion(std::string name) {
return "(assert\n(forall " + forall() + "\n" + "(=> " + name + " false)))";
}