2018-04-20 14:56:10 +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
|
2018-04-20 14:56:10 +00:00
|
|
|
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/SMTPortfolio.h>
|
2018-04-20 14:56:10 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_Z3
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/Z3Interface.h>
|
2018-04-20 14:56:10 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_CVC4
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/CVC4Interface.h>
|
2018-04-20 14:56:10 +00:00
|
|
|
#endif
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/SMTLib2Interface.h>
|
2018-04-20 14:56:10 +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;
|
2018-04-20 14:56:10 +00:00
|
|
|
|
2019-09-17 14:06:43 +00:00
|
|
|
SMTPortfolio::SMTPortfolio(
|
2023-07-12 12:09:19 +00:00
|
|
|
std::map<h256, std::string> _smtlib2Responses,
|
2020-05-11 17:56:29 +00:00
|
|
|
frontend::ReadCallback::Callback _smtCallback,
|
2020-11-02 20:20:20 +00:00
|
|
|
[[maybe_unused]] SMTSolverChoice _enabledSolvers,
|
2023-07-12 12:09:19 +00:00
|
|
|
std::optional<unsigned> _queryTimeout,
|
2023-04-26 10:50:36 +00:00
|
|
|
bool _printQuery
|
2020-11-02 20:20:20 +00:00
|
|
|
):
|
|
|
|
SolverInterface(_queryTimeout)
|
2018-04-20 14:56:10 +00:00
|
|
|
{
|
2023-04-26 10:50:36 +00:00
|
|
|
solAssert(!_printQuery || _enabledSolvers == smtutil::SMTSolverChoice::SMTLIB2(), "Only SMTLib2 solver can be enabled to print queries");
|
2021-05-19 15:35:19 +00:00
|
|
|
if (_enabledSolvers.smtlib2)
|
2023-07-12 12:09:19 +00:00
|
|
|
m_solvers.emplace_back(std::make_unique<SMTLib2Interface>(std::move(_smtlib2Responses), std::move(_smtCallback), m_queryTimeout));
|
2018-04-20 14:56:10 +00:00
|
|
|
#ifdef HAVE_Z3
|
2020-12-03 01:06:30 +00:00
|
|
|
if (_enabledSolvers.z3 && Z3Interface::available())
|
2023-07-12 12:09:19 +00:00
|
|
|
m_solvers.emplace_back(std::make_unique<Z3Interface>(m_queryTimeout));
|
2018-04-20 14:56:10 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_CVC4
|
2019-12-03 13:39:47 +00:00
|
|
|
if (_enabledSolvers.cvc4)
|
2023-07-12 12:09:19 +00:00
|
|
|
m_solvers.emplace_back(std::make_unique<CVC4Interface>(m_queryTimeout));
|
2018-04-20 14:56:10 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTPortfolio::reset()
|
|
|
|
{
|
2019-05-09 08:56:58 +00:00
|
|
|
for (auto const& s: m_solvers)
|
2018-04-20 14:56:10 +00:00
|
|
|
s->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTPortfolio::push()
|
|
|
|
{
|
2019-05-09 08:56:58 +00:00
|
|
|
for (auto const& s: m_solvers)
|
2018-04-20 14:56:10 +00:00
|
|
|
s->push();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTPortfolio::pop()
|
|
|
|
{
|
2019-05-09 08:56:58 +00:00
|
|
|
for (auto const& s: m_solvers)
|
2018-04-20 14:56:10 +00:00
|
|
|
s->pop();
|
|
|
|
}
|
|
|
|
|
2023-07-12 12:09:19 +00:00
|
|
|
void SMTPortfolio::declareVariable(std::string const& _name, SortPointer const& _sort)
|
2018-04-20 14:56:10 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_sort, "");
|
2019-05-09 08:56:58 +00:00
|
|
|
for (auto const& s: m_solvers)
|
2018-11-21 15:57:02 +00:00
|
|
|
s->declareVariable(_name, _sort);
|
2018-04-20 14:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
void SMTPortfolio::addAssertion(Expression const& _expr)
|
2018-04-20 14:56:10 +00:00
|
|
|
{
|
2019-05-09 08:56:58 +00:00
|
|
|
for (auto const& s: m_solvers)
|
2018-04-20 14:56:10 +00:00
|
|
|
s->addAssertion(_expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Broadcasts the SMT query to all solvers and returns a single result.
|
|
|
|
* This comment explains how this result is decided.
|
|
|
|
*
|
|
|
|
* When a solver is queried, there are four possible answers:
|
2018-06-26 10:41:26 +00:00
|
|
|
* SATISFIABLE (SAT), UNSATISFIABLE (UNSAT), UNKNOWN, CONFLICTING, ERROR
|
2018-04-20 14:56:10 +00:00
|
|
|
* We say that a solver _answered_ the query if it returns either:
|
|
|
|
* SAT or UNSAT
|
|
|
|
* A solver did not answer the query if it returns either:
|
|
|
|
* UNKNOWN (it tried but couldn't solve it) or ERROR (crash, internal error, API error, etc).
|
|
|
|
*
|
|
|
|
* Ideally all solvers answer the query and agree on what the answer is
|
|
|
|
* (all say SAT or all say UNSAT).
|
|
|
|
*
|
|
|
|
* The actual logic as as follows:
|
|
|
|
* 1) If at least one solver answers the query, all the non-answer results are ignored.
|
|
|
|
* Here SAT/UNSAT is preferred over UNKNOWN since it's an actual answer, and over ERROR
|
|
|
|
* because one buggy solver/integration shouldn't break the portfolio.
|
|
|
|
*
|
|
|
|
* 2) If at least one solver answers SAT and at least one answers UNSAT, at least one of them is buggy
|
2018-06-26 10:41:26 +00:00
|
|
|
* and the result is CONFLICTING.
|
2018-04-20 14:56:10 +00:00
|
|
|
* In the future if we have more than 2 solvers enabled we could go with the majority.
|
|
|
|
*
|
|
|
|
* 3) If NO solver answers the query:
|
|
|
|
* If at least one solver returned UNKNOWN (where the rest returned ERROR), the result is UNKNOWN.
|
|
|
|
* This is preferred over ERROR since the SMTChecker might decide to abstract the query
|
|
|
|
* when it is told that this is a hard query to solve.
|
|
|
|
*
|
|
|
|
* If all solvers return ERROR, the result is ERROR.
|
|
|
|
*/
|
2023-07-12 12:09:19 +00:00
|
|
|
std::pair<CheckResult, std::vector<std::string>> SMTPortfolio::check(std::vector<Expression> const& _expressionsToEvaluate)
|
2018-04-20 14:56:10 +00:00
|
|
|
{
|
|
|
|
CheckResult lastResult = CheckResult::ERROR;
|
2023-07-12 12:09:19 +00:00
|
|
|
std::vector<std::string> finalValues;
|
2019-05-09 08:56:58 +00:00
|
|
|
for (auto const& s: m_solvers)
|
2018-04-20 14:56:10 +00:00
|
|
|
{
|
|
|
|
CheckResult result;
|
2023-07-12 12:09:19 +00:00
|
|
|
std::vector<std::string> values;
|
2018-04-20 14:56:10 +00:00
|
|
|
tie(result, values) = s->check(_expressionsToEvaluate);
|
|
|
|
if (solverAnswered(result))
|
|
|
|
{
|
|
|
|
if (!solverAnswered(lastResult))
|
|
|
|
{
|
|
|
|
lastResult = result;
|
|
|
|
finalValues = std::move(values);
|
|
|
|
}
|
|
|
|
else if (lastResult != result)
|
|
|
|
{
|
2018-06-26 10:41:26 +00:00
|
|
|
lastResult = CheckResult::CONFLICTING;
|
|
|
|
break;
|
2018-04-20 14:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (result == CheckResult::UNKNOWN && lastResult == CheckResult::ERROR)
|
|
|
|
lastResult = result;
|
|
|
|
}
|
2023-07-12 12:09:19 +00:00
|
|
|
return std::make_pair(lastResult, finalValues);
|
2018-04-20 14:56:10 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 12:09:19 +00:00
|
|
|
std::vector<std::string> SMTPortfolio::unhandledQueries()
|
2019-01-23 10:22:38 +00:00
|
|
|
{
|
|
|
|
// This code assumes that the constructor guarantees that
|
2021-05-19 15:35:19 +00:00
|
|
|
// SmtLib2Interface is in position 0, if enabled.
|
|
|
|
if (!m_solvers.empty())
|
|
|
|
if (auto smtlib2 = dynamic_cast<SMTLib2Interface*>(m_solvers.front().get()))
|
|
|
|
return smtlib2->unhandledQueries();
|
|
|
|
return {};
|
2019-01-23 10:22:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 14:56:10 +00:00
|
|
|
bool SMTPortfolio::solverAnswered(CheckResult result)
|
|
|
|
{
|
|
|
|
return result == CheckResult::SATISFIABLE || result == CheckResult::UNSATISFIABLE;
|
|
|
|
}
|
2023-04-26 10:50:36 +00:00
|
|
|
|
2023-07-12 12:09:19 +00:00
|
|
|
std::string SMTPortfolio::dumpQuery(std::vector<Expression> const& _expressionsToEvaluate)
|
2023-04-26 10:50:36 +00:00
|
|
|
{
|
|
|
|
// This code assumes that the constructor guarantees that
|
|
|
|
// SmtLib2Interface is in position 0, if enabled.
|
|
|
|
auto smtlib2 = dynamic_cast<SMTLib2Interface*>(m_solvers.front().get());
|
|
|
|
solAssert(smtlib2, "Must use SMTLib2 solver to dump queries");
|
|
|
|
return smtlib2->dumpQuery(_expressionsToEvaluate);
|
|
|
|
}
|