2019-12-03 15:50:28 +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-12-03 15:50:28 +00:00
|
|
|
|
|
|
|
#include <test/libsolidity/SMTCheckerTest.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2019-12-03 15:50:28 +00:00
|
|
|
|
2021-10-06 09:51:55 +00:00
|
|
|
#include <range/v3/action/remove_if.hpp>
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::frontend::test;
|
2019-12-03 15:50:28 +00:00
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
SMTCheckerTest::SMTCheckerTest(std::string const& _filename): SyntaxTest(_filename, EVMVersion{})
|
2019-12-03 15:50:28 +00:00
|
|
|
{
|
2022-11-28 10:00:13 +00:00
|
|
|
auto contract = m_reader.stringSetting("SMTContract", "");
|
|
|
|
if (!contract.empty())
|
|
|
|
m_modelCheckerSettings.contracts.contracts[""] = {contract};
|
|
|
|
|
2021-10-12 09:12:18 +00:00
|
|
|
auto extCallsMode = ModelCheckerExtCalls::fromString(m_reader.stringSetting("SMTExtCalls", "untrusted"));
|
|
|
|
if (extCallsMode)
|
|
|
|
m_modelCheckerSettings.externalCalls = *extCallsMode;
|
|
|
|
else
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid SMT external calls mode."));
|
2021-10-12 09:12:18 +00:00
|
|
|
|
2021-07-02 13:05:24 +00:00
|
|
|
auto const& showUnproved = m_reader.stringSetting("SMTShowUnproved", "yes");
|
|
|
|
if (showUnproved == "no")
|
|
|
|
m_modelCheckerSettings.showUnproved = false;
|
|
|
|
else if (showUnproved == "yes")
|
|
|
|
m_modelCheckerSettings.showUnproved = true;
|
|
|
|
else
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid SMT \"show unproved\" choice."));
|
2021-07-02 13:05:24 +00:00
|
|
|
|
2023-03-06 13:19:58 +00:00
|
|
|
auto const& showUnsupported = m_reader.stringSetting("SMTShowUnsupported", "yes");
|
|
|
|
if (showUnsupported == "no")
|
|
|
|
m_modelCheckerSettings.showUnsupported = false;
|
|
|
|
else if (showUnsupported == "yes")
|
|
|
|
m_modelCheckerSettings.showUnsupported = true;
|
|
|
|
else
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid SMT \"show unsupported\" choice."));
|
2023-03-06 13:19:58 +00:00
|
|
|
|
2021-09-06 20:23:24 +00:00
|
|
|
m_modelCheckerSettings.solvers = smtutil::SMTSolverChoice::None();
|
2022-11-23 15:51:06 +00:00
|
|
|
auto const& choice = m_reader.stringSetting("SMTSolvers", "z3");
|
|
|
|
if (choice == "none")
|
2021-05-19 15:35:19 +00:00
|
|
|
m_modelCheckerSettings.solvers = smtutil::SMTSolverChoice::None();
|
|
|
|
else if (!m_modelCheckerSettings.solvers.setSolver(choice))
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid SMT solver choice."));
|
2020-02-18 16:13:13 +00:00
|
|
|
|
2021-05-19 15:35:19 +00:00
|
|
|
m_modelCheckerSettings.solvers &= ModelChecker::availableSolvers();
|
2020-02-18 16:13:13 +00:00
|
|
|
|
2021-07-07 10:31:09 +00:00
|
|
|
/// Underflow and Overflow are not enabled by default for Solidity >=0.8.7,
|
2021-10-12 09:12:18 +00:00
|
|
|
/// so we explicitly enable all targets for the tests,
|
|
|
|
/// if the targets were not explicitly set by the test.
|
|
|
|
auto targets = ModelCheckerTargets::fromString(m_reader.stringSetting("SMTTargets", "all"));
|
|
|
|
if (targets)
|
|
|
|
m_modelCheckerSettings.targets = *targets;
|
|
|
|
else
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid SMT targets."));
|
2021-07-07 10:31:09 +00:00
|
|
|
|
2020-11-05 12:30:42 +00:00
|
|
|
auto engine = ModelCheckerEngine::fromString(m_reader.stringSetting("SMTEngine", "all"));
|
|
|
|
if (engine)
|
|
|
|
m_modelCheckerSettings.engine = *engine;
|
|
|
|
else
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid SMT engine choice."));
|
2020-11-05 12:30:42 +00:00
|
|
|
|
2021-05-19 15:35:19 +00:00
|
|
|
if (m_modelCheckerSettings.solvers.none() || m_modelCheckerSettings.engine.none())
|
2020-02-18 16:13:13 +00:00
|
|
|
m_shouldRun = false;
|
2020-12-02 17:40:48 +00:00
|
|
|
|
2022-11-28 17:59:51 +00:00
|
|
|
auto const& ignoreCex = m_reader.stringSetting("SMTIgnoreCex", "yes");
|
2020-12-02 17:40:48 +00:00
|
|
|
if (ignoreCex == "no")
|
|
|
|
m_ignoreCex = false;
|
|
|
|
else if (ignoreCex == "yes")
|
|
|
|
m_ignoreCex = true;
|
|
|
|
else
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid SMT counterexample choice."));
|
2021-09-27 15:45:34 +00:00
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
static auto removeInv = [](std::vector<SyntaxTestError>&& errors) {
|
|
|
|
std::vector<SyntaxTestError> filtered;
|
2022-05-03 08:43:19 +00:00
|
|
|
for (auto&& e: errors)
|
|
|
|
if (e.errorId != 1180_error)
|
|
|
|
filtered.emplace_back(e);
|
|
|
|
return filtered;
|
|
|
|
};
|
|
|
|
if (m_modelCheckerSettings.invariants.invariants.empty())
|
2022-08-23 17:28:45 +00:00
|
|
|
m_expectations = removeInv(std::move(m_expectations));
|
2022-05-03 08:43:19 +00:00
|
|
|
|
|
|
|
auto const& ignoreInv = m_reader.stringSetting("SMTIgnoreInv", "yes");
|
2021-10-06 09:51:55 +00:00
|
|
|
if (ignoreInv == "no")
|
|
|
|
m_modelCheckerSettings.invariants = ModelCheckerInvariants::All();
|
|
|
|
else if (ignoreInv == "yes")
|
|
|
|
m_modelCheckerSettings.invariants = ModelCheckerInvariants::None();
|
|
|
|
else
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid SMT invariant choice."));
|
2021-10-06 09:51:55 +00:00
|
|
|
|
2021-09-27 15:45:34 +00:00
|
|
|
auto const& ignoreOSSetting = m_reader.stringSetting("SMTIgnoreOS", "none");
|
2023-08-30 00:08:06 +00:00
|
|
|
for (std::string const& os: ignoreOSSetting | ranges::views::split(',') | ranges::to<std::vector<std::string>>())
|
2021-09-27 15:45:34 +00:00
|
|
|
{
|
|
|
|
#ifdef __APPLE__
|
|
|
|
if (os == "macos")
|
|
|
|
m_shouldRun = false;
|
|
|
|
#elif _WIN32
|
|
|
|
if (os == "windows")
|
|
|
|
m_shouldRun = false;
|
|
|
|
#elif __linux__
|
|
|
|
if (os == "linux")
|
|
|
|
m_shouldRun = false;
|
|
|
|
#endif
|
|
|
|
}
|
2023-02-23 18:22:46 +00:00
|
|
|
|
|
|
|
auto const& bmcLoopIterations = m_reader.sizetSetting("BMCLoopIterations", 1);
|
|
|
|
m_modelCheckerSettings.bmcLoopIterations = std::optional<unsigned>{bmcLoopIterations};
|
2019-12-03 15:50:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 11:00:08 +00:00
|
|
|
void SMTCheckerTest::setupCompiler(CompilerStack& _compiler)
|
2019-12-03 15:50:28 +00:00
|
|
|
{
|
2023-08-07 11:00:08 +00:00
|
|
|
SyntaxTest::setupCompiler(_compiler);
|
2019-12-03 15:50:28 +00:00
|
|
|
|
2023-08-07 11:00:08 +00:00
|
|
|
_compiler.setModelCheckerSettings(m_modelCheckerSettings);
|
2019-12-03 15:50:28 +00:00
|
|
|
}
|
2020-12-02 17:40:48 +00:00
|
|
|
|
|
|
|
void SMTCheckerTest::filterObtainedErrors()
|
|
|
|
{
|
|
|
|
SyntaxTest::filterObtainedErrors();
|
2023-07-24 15:01:12 +00:00
|
|
|
m_unfilteredErrorList = m_errorList;
|
2020-12-02 17:40:48 +00:00
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
static auto removeCex = [](std::vector<SyntaxTestError>& errors) {
|
2020-12-02 17:40:48 +00:00
|
|
|
for (auto& e: errors)
|
|
|
|
if (
|
|
|
|
auto cexPos = e.message.find("\\nCounterexample");
|
2023-08-30 00:08:06 +00:00
|
|
|
cexPos != std::string::npos
|
2020-12-02 17:40:48 +00:00
|
|
|
)
|
|
|
|
e.message = e.message.substr(0, cexPos);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (m_ignoreCex)
|
2022-11-28 17:59:51 +00:00
|
|
|
{
|
|
|
|
removeCex(m_expectations);
|
2020-12-02 17:40:48 +00:00
|
|
|
removeCex(m_errorList);
|
2022-11-28 17:59:51 +00:00
|
|
|
}
|
2020-12-02 17:40:48 +00:00
|
|
|
}
|
2023-07-24 15:01:12 +00:00
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
void SMTCheckerTest::printUpdatedExpectations(std::ostream &_stream, const std::string &_linePrefix) const {
|
2023-07-24 15:01:12 +00:00
|
|
|
if (!m_unfilteredErrorList.empty())
|
|
|
|
printErrorList(_stream, m_unfilteredErrorList, _linePrefix, false);
|
|
|
|
else
|
|
|
|
CommonSyntaxTest::printUpdatedExpectations(_stream, _linePrefix);
|
|
|
|
}
|