2019-06-25 10:46:17 +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
|
2019-06-25 10:46:17 +00:00
|
|
|
|
|
|
|
#include <libsolidity/formal/ModelChecker.h>
|
2020-12-03 01:06:30 +00:00
|
|
|
#ifdef HAVE_Z3
|
|
|
|
#include <libsmtutil/Z3Interface.h>
|
|
|
|
#endif
|
2022-05-15 15:45:43 +00:00
|
|
|
#ifdef HAVE_Z3_DLOPEN
|
|
|
|
#include <z3_version.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__linux) || defined(__APPLE__)
|
|
|
|
#include <boost/process.hpp>
|
|
|
|
#endif
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2021-03-31 15:09:37 +00:00
|
|
|
#include <range/v3/algorithm/any_of.hpp>
|
2021-04-15 10:31:45 +00:00
|
|
|
#include <range/v3/view.hpp>
|
2021-03-31 15:09:37 +00:00
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::frontend;
|
2022-05-15 15:45:43 +00:00
|
|
|
using namespace solidity::smtutil;
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2019-09-17 14:06:43 +00:00
|
|
|
ModelChecker::ModelChecker(
|
|
|
|
ErrorReporter& _errorReporter,
|
2021-07-01 15:28:06 +00:00
|
|
|
langutil::CharStreamProvider const& _charStreamProvider,
|
2019-09-17 14:06:43 +00:00
|
|
|
map<h256, string> const& _smtlib2Responses,
|
2020-11-02 20:20:20 +00:00
|
|
|
ModelCheckerSettings _settings,
|
2021-05-19 15:35:19 +00:00
|
|
|
ReadCallback::Callback const& _smtCallback
|
2019-09-17 14:06:43 +00:00
|
|
|
):
|
2021-03-31 15:09:37 +00:00
|
|
|
m_errorReporter(_errorReporter),
|
2022-08-23 17:28:45 +00:00
|
|
|
m_settings(std::move(_settings)),
|
2020-02-12 01:52:35 +00:00
|
|
|
m_context(),
|
2023-03-06 13:19:58 +00:00
|
|
|
m_bmc(m_context, m_uniqueErrorReporter, m_unsupportedErrorReporter, _smtlib2Responses, _smtCallback, m_settings, _charStreamProvider),
|
|
|
|
m_chc(m_context, m_uniqueErrorReporter, m_unsupportedErrorReporter, _smtlib2Responses, _smtCallback, m_settings, _charStreamProvider)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:09:37 +00:00
|
|
|
// TODO This should be removed for 0.9.0.
|
2022-05-15 15:45:43 +00:00
|
|
|
bool ModelChecker::isPragmaPresent(vector<shared_ptr<SourceUnit>> const& _sources)
|
2021-03-31 15:09:37 +00:00
|
|
|
{
|
2022-05-15 15:45:43 +00:00
|
|
|
return ranges::any_of(_sources, [](auto _source) {
|
2021-03-31 15:09:37 +00:00
|
|
|
return _source && _source->annotation().experimentalFeatures.count(ExperimentalFeature::SMTChecker);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-15 10:31:45 +00:00
|
|
|
void ModelChecker::checkRequestedSourcesAndContracts(vector<shared_ptr<SourceUnit>> const& _sources)
|
|
|
|
{
|
|
|
|
map<string, set<string>> exist;
|
|
|
|
for (auto const& source: _sources)
|
|
|
|
for (auto node: source->nodes())
|
|
|
|
if (auto contract = dynamic_pointer_cast<ContractDefinition>(node))
|
|
|
|
exist[contract->sourceUnitName()].insert(contract->name());
|
|
|
|
|
|
|
|
// Requested sources
|
|
|
|
for (auto const& sourceName: m_settings.contracts.contracts | ranges::views::keys)
|
|
|
|
{
|
|
|
|
if (!exist.count(sourceName))
|
|
|
|
{
|
2021-08-27 09:40:20 +00:00
|
|
|
m_uniqueErrorReporter.warning(
|
2021-04-15 10:31:45 +00:00
|
|
|
9134_error,
|
|
|
|
SourceLocation(),
|
|
|
|
"Requested source \"" + sourceName + "\" does not exist."
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto const& source = exist.at(sourceName);
|
|
|
|
// Requested contracts in source `s`.
|
|
|
|
for (auto const& contract: m_settings.contracts.contracts.at(sourceName))
|
|
|
|
if (!source.count(contract))
|
2021-08-27 09:40:20 +00:00
|
|
|
m_uniqueErrorReporter.warning(
|
2021-04-15 10:31:45 +00:00
|
|
|
7400_error,
|
|
|
|
SourceLocation(),
|
|
|
|
"Requested contract \"" + contract + "\" does not exist in source \"" + sourceName + "\"."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 14:02:11 +00:00
|
|
|
void ModelChecker::analyze(SourceUnit const& _source)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2021-03-31 15:09:37 +00:00
|
|
|
// TODO This should be removed for 0.9.0.
|
|
|
|
if (_source.annotation().experimentalFeatures.count(ExperimentalFeature::SMTChecker))
|
|
|
|
{
|
|
|
|
PragmaDirective const* smtPragma = nullptr;
|
|
|
|
for (auto node: _source.nodes())
|
|
|
|
if (auto pragma = dynamic_pointer_cast<PragmaDirective>(node))
|
|
|
|
if (
|
|
|
|
pragma->literals().size() >= 2 &&
|
|
|
|
pragma->literals().at(1) == "SMTChecker"
|
|
|
|
)
|
|
|
|
{
|
|
|
|
smtPragma = pragma.get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
solAssert(smtPragma, "");
|
2021-08-27 09:40:20 +00:00
|
|
|
m_uniqueErrorReporter.warning(
|
2021-03-31 15:09:37 +00:00
|
|
|
5523_error,
|
|
|
|
smtPragma->location(),
|
|
|
|
"The SMTChecker pragma has been deprecated and will be removed in the future. "
|
|
|
|
"Please use the \"model checker engine\" compiler setting to activate the SMTChecker instead. "
|
|
|
|
"If the pragma is enabled, all engines will be used."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_settings.engine.none())
|
2019-06-25 10:46:17 +00:00
|
|
|
return;
|
|
|
|
|
2020-11-02 20:20:20 +00:00
|
|
|
if (m_settings.engine.chc)
|
2020-10-14 15:44:40 +00:00
|
|
|
m_chc.analyze(_source);
|
2020-07-10 08:28:49 +00:00
|
|
|
|
2023-02-09 16:07:13 +00:00
|
|
|
map<ASTNode const*, set<VerificationTargetType>, smt::EncodingContext::IdCompare> solvedTargets;
|
|
|
|
|
|
|
|
for (auto const& [node, targets]: m_chc.safeTargets())
|
|
|
|
for (auto const& target: targets)
|
|
|
|
solvedTargets[node].insert(target.type);
|
|
|
|
|
2021-07-02 12:42:53 +00:00
|
|
|
for (auto const& [node, targets]: m_chc.unsafeTargets())
|
|
|
|
solvedTargets[node] += targets | ranges::views::keys;
|
2020-07-10 08:28:49 +00:00
|
|
|
|
2020-11-02 20:20:20 +00:00
|
|
|
if (m_settings.engine.bmc)
|
2020-10-14 15:44:40 +00:00
|
|
|
m_bmc.analyze(_source, solvedTargets);
|
2021-08-27 09:40:20 +00:00
|
|
|
|
2023-03-06 13:19:58 +00:00
|
|
|
if (m_settings.showUnsupported)
|
|
|
|
{
|
|
|
|
m_errorReporter.append(m_unsupportedErrorReporter.errors());
|
|
|
|
m_unsupportedErrorReporter.clear();
|
|
|
|
}
|
|
|
|
else if (!m_unsupportedErrorReporter.errors().empty())
|
|
|
|
m_errorReporter.warning(
|
|
|
|
5724_error,
|
|
|
|
{},
|
|
|
|
"SMTChecker: " +
|
|
|
|
to_string(m_unsupportedErrorReporter.errors().size()) +
|
|
|
|
" unsupported language feature(s)."
|
|
|
|
" Enable the model checker option \"show unsupported\" to see all of them."
|
|
|
|
);
|
|
|
|
|
2021-08-27 09:40:20 +00:00
|
|
|
m_errorReporter.append(m_uniqueErrorReporter.errors());
|
|
|
|
m_uniqueErrorReporter.clear();
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> ModelChecker::unhandledQueries()
|
|
|
|
{
|
2019-09-24 15:35:31 +00:00
|
|
|
return m_bmc.unhandledQueries() + m_chc.unhandledQueries();
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
2019-12-03 15:50:28 +00:00
|
|
|
|
2022-05-15 15:45:43 +00:00
|
|
|
SMTSolverChoice ModelChecker::availableSolvers()
|
2019-12-03 15:50:28 +00:00
|
|
|
{
|
2021-05-19 15:35:19 +00:00
|
|
|
smtutil::SMTSolverChoice available = smtutil::SMTSolverChoice::SMTLIB2();
|
2022-05-15 15:45:43 +00:00
|
|
|
#if defined(__linux) || defined(__APPLE__)
|
|
|
|
available.eld = !boost::process::search_path("eld").empty();
|
|
|
|
#endif
|
2019-12-03 15:50:28 +00:00
|
|
|
#ifdef HAVE_Z3
|
2020-12-03 01:06:30 +00:00
|
|
|
available.z3 = solidity::smtutil::Z3Interface::available();
|
2019-12-03 15:50:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_CVC4
|
|
|
|
available.cvc4 = true;
|
|
|
|
#endif
|
|
|
|
return available;
|
|
|
|
}
|
2022-05-15 15:45:43 +00:00
|
|
|
|
|
|
|
SMTSolverChoice ModelChecker::checkRequestedSolvers(SMTSolverChoice _enabled, ErrorReporter& _errorReporter)
|
|
|
|
{
|
|
|
|
SMTSolverChoice availableSolvers{ModelChecker::availableSolvers()};
|
|
|
|
|
|
|
|
if (_enabled.cvc4 && !availableSolvers.cvc4)
|
|
|
|
{
|
|
|
|
_enabled.cvc4 = false;
|
|
|
|
_errorReporter.warning(
|
|
|
|
4902_error,
|
|
|
|
SourceLocation(),
|
|
|
|
"Solver CVC4 was selected for SMTChecker but it is not available."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_enabled.eld && !availableSolvers.eld)
|
|
|
|
{
|
|
|
|
_enabled.eld = false;
|
|
|
|
_errorReporter.warning(
|
|
|
|
4458_error,
|
|
|
|
SourceLocation(),
|
|
|
|
#if defined(__linux) || defined(__APPLE__)
|
|
|
|
"Solver Eldarica was selected for SMTChecker but it was not found in the system."
|
|
|
|
#else
|
|
|
|
"Solver Eldarica was selected for SMTChecker but it is only supported on Linux and MacOS."
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_enabled.z3 && !availableSolvers.z3)
|
|
|
|
{
|
|
|
|
_enabled.z3 = false;
|
|
|
|
_errorReporter.warning(
|
|
|
|
8158_error,
|
|
|
|
SourceLocation(),
|
|
|
|
"Solver z3 was selected for SMTChecker but it is not available."
|
|
|
|
#ifdef HAVE_Z3_DLOPEN
|
|
|
|
" libz3.so." + to_string(Z3_MAJOR_VERSION) + "." + to_string(Z3_MINOR_VERSION) + " was not found."
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _enabled;
|
|
|
|
}
|