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/BMC.h>
|
|
|
|
|
|
|
|
#include <libsolidity/formal/SymbolicTypes.h>
|
|
|
|
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/SMTPortfolio.h>
|
|
|
|
|
2021-07-01 15:28:06 +00:00
|
|
|
#include <liblangutil/CharStream.h>
|
|
|
|
#include <liblangutil/CharStreamProvider.h>
|
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2020-12-03 01:06:30 +00:00
|
|
|
#ifdef HAVE_Z3_DLOPEN
|
|
|
|
#include <z3_version.h>
|
|
|
|
#endif
|
|
|
|
|
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;
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2019-09-17 14:06:43 +00:00
|
|
|
BMC::BMC(
|
|
|
|
smt::EncodingContext& _context,
|
2021-08-27 09:40:20 +00:00
|
|
|
UniqueErrorReporter& _errorReporter,
|
2023-03-06 13:19:58 +00:00
|
|
|
UniqueErrorReporter& _unsupportedErrorReporter,
|
2019-12-03 13:39:47 +00:00
|
|
|
map<h256, string> const& _smtlib2Responses,
|
2019-12-05 15:44:26 +00:00
|
|
|
ReadCallback::Callback const& _smtCallback,
|
2022-05-15 15:45:43 +00:00
|
|
|
ModelCheckerSettings _settings,
|
2021-07-01 15:28:06 +00:00
|
|
|
CharStreamProvider const& _charStreamProvider
|
2019-09-17 14:06:43 +00:00
|
|
|
):
|
2023-03-06 13:19:58 +00:00
|
|
|
SMTEncoder(_context, _settings, _errorReporter, _unsupportedErrorReporter, _charStreamProvider),
|
2023-04-26 10:50:36 +00:00
|
|
|
m_interface(make_unique<smtutil::SMTPortfolio>(
|
|
|
|
_smtlib2Responses, _smtCallback, _settings.solvers, _settings.timeout, _settings.printQuery
|
|
|
|
))
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2023-04-26 10:50:36 +00:00
|
|
|
solAssert(!_settings.printQuery || _settings.solvers == smtutil::SMTSolverChoice::SMTLIB2(), "Only SMTLib2 solver can be enabled to print queries");
|
2019-06-25 10:46:17 +00:00
|
|
|
#if defined (HAVE_Z3) || defined (HAVE_CVC4)
|
2021-05-19 15:35:19 +00:00
|
|
|
if (m_settings.solvers.cvc4 || m_settings.solvers.z3)
|
2019-12-03 13:39:47 +00:00
|
|
|
if (!_smtlib2Responses.empty())
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
5622_error,
|
2019-12-03 13:39:47 +00:00
|
|
|
"SMT-LIB2 query responses were given in the auxiliary input, "
|
|
|
|
"but this Solidity binary uses an SMT solver (Z3/CVC4) directly."
|
|
|
|
"These responses will be ignored."
|
|
|
|
"Consider disabling Z3/CVC4 at compilation time in order to use SMT-LIB2 responses."
|
|
|
|
);
|
2019-06-25 10:46:17 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-02 12:42:53 +00:00
|
|
|
void BMC::analyze(SourceUnit const& _source, map<ASTNode const*, set<VerificationTargetType>, smt::EncodingContext::IdCompare> _solvedTargets)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2022-05-15 15:45:43 +00:00
|
|
|
// At this point every enabled solver is available.
|
|
|
|
if (!m_settings.solvers.cvc4 && !m_settings.solvers.smtlib2 && !m_settings.solvers.z3)
|
2021-05-19 15:35:19 +00:00
|
|
|
{
|
2022-05-10 17:04:37 +00:00
|
|
|
m_errorReporter.warning(
|
|
|
|
7710_error,
|
|
|
|
SourceLocation(),
|
|
|
|
"BMC analysis was not possible since no SMT solver was found and enabled."
|
2022-05-15 15:45:43 +00:00
|
|
|
" The accepted solvers for BMC are cvc4 and z3."
|
2022-05-10 17:04:37 +00:00
|
|
|
);
|
2021-05-19 15:35:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-27 14:45:41 +00:00
|
|
|
SMTEncoder::resetSourceAnalysis();
|
2019-07-08 09:21:23 +00:00
|
|
|
|
2023-06-30 13:02:06 +00:00
|
|
|
state().prepareForSourceUnit(_source, false);
|
2022-08-23 17:28:45 +00:00
|
|
|
m_solvedTargets = std::move(_solvedTargets);
|
2021-08-27 14:45:41 +00:00
|
|
|
m_context.setSolver(m_interface.get());
|
|
|
|
m_context.reset();
|
|
|
|
m_context.setAssertionAccumulation(true);
|
|
|
|
m_variableUsage.setFunctionInlining(shouldInlineFunctionCall);
|
|
|
|
createFreeConstants(sourceDependencies(_source));
|
|
|
|
m_unprovedAmt = 0;
|
2021-07-02 12:42:53 +00:00
|
|
|
|
2021-08-27 14:45:41 +00:00
|
|
|
_source.accept(*this);
|
|
|
|
|
|
|
|
if (m_unprovedAmt > 0 && !m_settings.showUnproved)
|
|
|
|
m_errorReporter.warning(
|
|
|
|
2788_error,
|
|
|
|
{},
|
|
|
|
"BMC: " +
|
|
|
|
to_string(m_unprovedAmt) +
|
|
|
|
" verification condition(s) could not be proved." +
|
|
|
|
" Enable the model checker option \"show unproved\" to see all of them." +
|
|
|
|
" Consider choosing a specific contract to be verified in order to reduce the solving problems." +
|
|
|
|
" Consider increasing the timeout per query."
|
|
|
|
);
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2023-02-09 16:07:13 +00:00
|
|
|
if (!m_settings.showProvedSafe && !m_safeTargets.empty())
|
|
|
|
m_errorReporter.info(
|
|
|
|
6002_error,
|
|
|
|
"BMC: " +
|
|
|
|
to_string(m_safeTargets.size()) +
|
|
|
|
" verification condition(s) proved safe!" +
|
|
|
|
" Enable the model checker option \"show proved safe\" to see all of them."
|
|
|
|
);
|
|
|
|
else if (m_settings.showProvedSafe)
|
|
|
|
for (auto const& [node, targets]: m_safeTargets)
|
|
|
|
for (auto const& target: targets)
|
|
|
|
m_errorReporter.info(
|
|
|
|
2961_error,
|
|
|
|
node->location(),
|
|
|
|
"BMC: " +
|
|
|
|
targetDescription(target) +
|
|
|
|
" check is safe!"
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
// If this check is true, Z3 and CVC4 are not available
|
|
|
|
// and the query answers were not provided, since SMTPortfolio
|
2021-05-19 15:35:19 +00:00
|
|
|
// guarantees that SmtLib2Interface is the first solver, if enabled.
|
|
|
|
if (
|
|
|
|
!m_interface->unhandledQueries().empty() &&
|
|
|
|
m_interface->solvers() == 1 &&
|
|
|
|
m_settings.solvers.smtlib2
|
|
|
|
)
|
2022-05-10 17:04:37 +00:00
|
|
|
m_errorReporter.warning(
|
|
|
|
8084_error,
|
|
|
|
SourceLocation(),
|
|
|
|
"BMC analysis was not possible. No SMT solver (Z3 or CVC4) was available."
|
|
|
|
" None of the installed solvers was enabled."
|
2020-12-03 01:06:30 +00:00
|
|
|
#ifdef HAVE_Z3_DLOPEN
|
2022-05-10 17:04:37 +00:00
|
|
|
" Install libz3.so." + to_string(Z3_MAJOR_VERSION) + "." + to_string(Z3_MINOR_VERSION) + " to enable Z3."
|
2020-12-03 01:06:30 +00:00
|
|
|
#endif
|
2022-05-10 17:04:37 +00:00
|
|
|
);
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 17:17:56 +00:00
|
|
|
bool BMC::shouldInlineFunctionCall(
|
|
|
|
FunctionCall const& _funCall,
|
|
|
|
ContractDefinition const* _scopeContract,
|
|
|
|
ContractDefinition const* _contextContract
|
|
|
|
)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2021-01-25 17:17:56 +00:00
|
|
|
auto funDef = functionCallToDefinition(_funCall, _scopeContract, _contextContract);
|
2019-07-17 15:54:48 +00:00
|
|
|
if (!funDef || !funDef->isImplemented())
|
|
|
|
return false;
|
2019-06-25 10:46:17 +00:00
|
|
|
|
|
|
|
FunctionType const& funType = dynamic_cast<FunctionType const&>(*_funCall.expression().annotation().type);
|
|
|
|
if (funType.kind() == FunctionType::Kind::External)
|
2021-10-12 09:12:18 +00:00
|
|
|
return isExternalCallToThis(&_funCall.expression());
|
2019-06-25 10:46:17 +00:00
|
|
|
else if (funType.kind() != FunctionType::Kind::Internal)
|
2019-07-17 15:54:48 +00:00
|
|
|
return false;
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2019-07-17 15:54:48 +00:00
|
|
|
return true;
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// AST visitors.
|
|
|
|
|
|
|
|
bool BMC::visit(ContractDefinition const& _contract)
|
|
|
|
{
|
2019-09-28 19:04:49 +00:00
|
|
|
initContract(_contract);
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2019-09-28 19:04:49 +00:00
|
|
|
SMTEncoder::visit(_contract);
|
|
|
|
|
|
|
|
return false;
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 16:36:26 +00:00
|
|
|
void BMC::endVisit(ContractDefinition const& _contract)
|
|
|
|
{
|
2019-09-26 14:12:27 +00:00
|
|
|
if (auto constructor = _contract.constructor())
|
|
|
|
constructor->accept(*this);
|
|
|
|
else
|
|
|
|
{
|
2020-11-16 16:52:06 +00:00
|
|
|
/// Visiting implicit constructor - we need a dummy callstack frame
|
|
|
|
pushCallStack({nullptr, nullptr});
|
2019-09-26 14:12:27 +00:00
|
|
|
inlineConstructorHierarchy(_contract);
|
2020-11-16 16:52:06 +00:00
|
|
|
popCallStack();
|
2019-09-26 14:12:27 +00:00
|
|
|
/// Check targets created by state variable initialization.
|
2020-12-16 17:32:34 +00:00
|
|
|
checkVerificationTargets();
|
2019-09-26 14:12:27 +00:00
|
|
|
m_verificationTargets.clear();
|
|
|
|
}
|
|
|
|
|
2019-08-02 16:36:26 +00:00
|
|
|
SMTEncoder::endVisit(_contract);
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool BMC::visit(FunctionDefinition const& _function)
|
|
|
|
{
|
2021-04-07 12:52:56 +00:00
|
|
|
// Free functions need to be visited in the context of a contract.
|
|
|
|
if (!m_currentContract)
|
|
|
|
return false;
|
|
|
|
|
2019-08-02 16:36:26 +00:00
|
|
|
auto contract = dynamic_cast<ContractDefinition const*>(_function.scope());
|
|
|
|
auto const& hierarchy = m_currentContract->annotation().linearizedBaseContracts;
|
2021-04-07 12:52:56 +00:00
|
|
|
if (contract && find(hierarchy.begin(), hierarchy.end(), contract) == hierarchy.end())
|
2019-09-26 14:12:27 +00:00
|
|
|
createStateVariables(*contract);
|
2019-08-02 16:36:26 +00:00
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
if (m_callStack.empty())
|
2019-09-26 14:12:27 +00:00
|
|
|
{
|
2019-06-25 10:46:17 +00:00
|
|
|
reset();
|
2019-09-26 14:12:27 +00:00
|
|
|
initFunction(_function);
|
2021-09-06 22:14:28 +00:00
|
|
|
if (_function.isConstructor() || _function.isPublic())
|
|
|
|
m_context.addAssertion(state().txTypeConstraints() && state().txFunctionConstraints(_function));
|
2019-09-26 14:12:27 +00:00
|
|
|
resetStateVariables();
|
|
|
|
}
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2020-11-09 15:37:08 +00:00
|
|
|
if (_function.isConstructor())
|
2021-04-07 12:52:56 +00:00
|
|
|
{
|
|
|
|
solAssert(contract, "");
|
|
|
|
inlineConstructorHierarchy(*contract);
|
|
|
|
}
|
2020-11-09 15:37:08 +00:00
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
/// Already visits the children.
|
|
|
|
SMTEncoder::visit(_function);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BMC::endVisit(FunctionDefinition const& _function)
|
|
|
|
{
|
2021-04-07 12:52:56 +00:00
|
|
|
// Free functions need to be visited in the context of a contract.
|
|
|
|
if (!m_currentContract)
|
|
|
|
return;
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
if (isRootFunction())
|
|
|
|
{
|
2020-12-16 17:32:34 +00:00
|
|
|
checkVerificationTargets();
|
2019-06-25 10:46:17 +00:00
|
|
|
m_verificationTargets.clear();
|
2020-11-14 14:42:32 +00:00
|
|
|
m_pathConditions.clear();
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SMTEncoder::endVisit(_function);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BMC::visit(IfStatement const& _node)
|
|
|
|
{
|
2023-05-11 14:43:12 +00:00
|
|
|
auto indicesBeforePush = copyVariableIndices();
|
2019-06-25 10:46:17 +00:00
|
|
|
// This check needs to be done in its own context otherwise
|
|
|
|
// constraints from the If body might influence it.
|
|
|
|
m_context.pushSolver();
|
|
|
|
_node.condition().accept(*this);
|
|
|
|
|
|
|
|
// We ignore called functions here because they have
|
|
|
|
// specific input values.
|
2023-02-23 18:22:46 +00:00
|
|
|
if (isRootFunction() && !isInsideLoop())
|
2019-06-25 10:46:17 +00:00
|
|
|
addVerificationTarget(
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType::ConstantCondition,
|
2019-06-25 10:46:17 +00:00
|
|
|
expr(_node.condition()),
|
|
|
|
&_node.condition()
|
|
|
|
);
|
|
|
|
m_context.popSolver();
|
2023-05-11 14:43:12 +00:00
|
|
|
resetVariableIndices(std::move(indicesBeforePush));
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2020-11-14 14:42:32 +00:00
|
|
|
_node.condition().accept(*this);
|
|
|
|
auto conditionExpr = expr(_node.condition());
|
|
|
|
// visit true branch
|
|
|
|
auto [indicesEndTrue, trueEndPathCondition] = visitBranch(&_node.trueStatement(), conditionExpr);
|
|
|
|
|
|
|
|
// visit false branch
|
|
|
|
decltype(indicesEndTrue) indicesEndFalse;
|
|
|
|
auto falseEndPathCondition = currentPathConditions() && !conditionExpr;
|
|
|
|
if (_node.falseStatement())
|
|
|
|
std::tie(indicesEndFalse, falseEndPathCondition) = visitBranch(_node.falseStatement(), !conditionExpr);
|
|
|
|
else
|
|
|
|
indicesEndFalse = copyVariableIndices();
|
|
|
|
|
|
|
|
// merge the information from branches
|
|
|
|
setPathCondition(trueEndPathCondition || falseEndPathCondition);
|
2021-03-29 12:44:33 +00:00
|
|
|
mergeVariables(expr(_node.condition()), indicesEndTrue, indicesEndFalse);
|
2019-06-25 10:46:17 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-18 17:25:36 +00:00
|
|
|
bool BMC::visit(Conditional const& _op)
|
|
|
|
{
|
2023-05-11 14:43:12 +00:00
|
|
|
auto indicesBeforePush = copyVariableIndices();
|
2020-08-18 17:25:36 +00:00
|
|
|
m_context.pushSolver();
|
|
|
|
_op.condition().accept(*this);
|
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
if (isRootFunction() && !isInsideLoop())
|
2020-08-18 17:25:36 +00:00
|
|
|
addVerificationTarget(
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType::ConstantCondition,
|
2020-08-18 17:25:36 +00:00
|
|
|
expr(_op.condition()),
|
|
|
|
&_op.condition()
|
|
|
|
);
|
|
|
|
m_context.popSolver();
|
2023-05-11 14:43:12 +00:00
|
|
|
resetVariableIndices(std::move(indicesBeforePush));
|
2020-08-18 17:25:36 +00:00
|
|
|
|
|
|
|
SMTEncoder::visit(_op);
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
// Unrolls while or do-while loop
|
2019-06-25 10:46:17 +00:00
|
|
|
bool BMC::visit(WhileStatement const& _node)
|
|
|
|
{
|
2023-02-23 18:22:46 +00:00
|
|
|
unsigned int bmcLoopIterations = m_settings.bmcLoopIterations.value_or(1);
|
|
|
|
smtutil::Expression broke(false);
|
|
|
|
smtutil::Expression loopCondition(true);
|
2019-06-25 10:46:17 +00:00
|
|
|
if (_node.isDoWhile())
|
|
|
|
{
|
2023-02-23 18:22:46 +00:00
|
|
|
for (unsigned int i = 0; i < bmcLoopIterations; ++i)
|
|
|
|
{
|
|
|
|
m_loopCheckpoints.emplace();
|
|
|
|
|
|
|
|
auto indicesBefore = copyVariableIndices();
|
|
|
|
_node.body().accept(*this);
|
|
|
|
|
|
|
|
auto [continues, brokeInCurrentIteration] = mergeVariablesFromLoopCheckpoints();
|
|
|
|
|
|
|
|
auto indicesBreak = copyVariableIndices();
|
|
|
|
_node.condition().accept(*this);
|
|
|
|
mergeVariables(
|
|
|
|
!brokeInCurrentIteration,
|
|
|
|
copyVariableIndices(),
|
|
|
|
indicesBreak
|
2019-06-25 10:46:17 +00:00
|
|
|
);
|
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
mergeVariables(
|
|
|
|
broke || !loopCondition,
|
|
|
|
indicesBefore,
|
|
|
|
copyVariableIndices()
|
|
|
|
);
|
|
|
|
loopCondition = expr(_node.condition());
|
|
|
|
broke = broke || brokeInCurrentIteration;
|
|
|
|
m_loopCheckpoints.pop();
|
|
|
|
}
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
2023-02-23 18:22:46 +00:00
|
|
|
else {
|
|
|
|
smtutil::Expression loopConditionOnPreviousIteration(true);
|
|
|
|
for (unsigned int i = 0; i < bmcLoopIterations; ++i)
|
|
|
|
{
|
|
|
|
m_loopCheckpoints.emplace();
|
|
|
|
auto indicesBefore = copyVariableIndices();
|
|
|
|
_node.condition().accept(*this);
|
|
|
|
loopCondition = expr(_node.condition());
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
auto indicesAfterCondition = copyVariableIndices();
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
pushPathCondition(loopCondition);
|
|
|
|
_node.body().accept(*this);
|
|
|
|
popPathCondition();
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
auto [continues, brokeInCurrentIteration] = mergeVariablesFromLoopCheckpoints();
|
|
|
|
|
|
|
|
// merges indices modified when accepting loop condition that no longer holds
|
|
|
|
mergeVariables(
|
|
|
|
!loopCondition,
|
|
|
|
indicesAfterCondition,
|
|
|
|
copyVariableIndices()
|
|
|
|
);
|
|
|
|
|
|
|
|
// handles breaks in previous iterations
|
|
|
|
// breaks in current iterations are handled when traversing loop checkpoints
|
|
|
|
// handles case when the loop condition no longer holds but bmc loop iterations still unrolls the loop
|
|
|
|
mergeVariables(
|
|
|
|
broke || !loopConditionOnPreviousIteration,
|
|
|
|
indicesBefore,
|
|
|
|
copyVariableIndices()
|
|
|
|
);
|
|
|
|
m_loopCheckpoints.pop();
|
|
|
|
broke = broke || brokeInCurrentIteration;
|
|
|
|
loopConditionOnPreviousIteration = loopCondition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bmcLoopIterations > 0)
|
|
|
|
m_context.addAssertion(not(loopCondition) || broke);
|
2019-06-25 10:46:17 +00:00
|
|
|
m_loopExecutionHappened = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
// Unrolls for loop
|
2019-06-25 10:46:17 +00:00
|
|
|
bool BMC::visit(ForStatement const& _node)
|
|
|
|
{
|
|
|
|
if (_node.initializationExpression())
|
|
|
|
_node.initializationExpression()->accept(*this);
|
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
smtutil::Expression broke(false);
|
|
|
|
smtutil::Expression forCondition(true);
|
|
|
|
smtutil::Expression forConditionOnPreviousIteration(true);
|
|
|
|
unsigned int bmcLoopIterations = m_settings.bmcLoopIterations.value_or(1);
|
|
|
|
for (unsigned int i = 0; i < bmcLoopIterations; ++i)
|
|
|
|
{
|
|
|
|
auto indicesBefore = copyVariableIndices();
|
|
|
|
if (_node.condition())
|
|
|
|
{
|
|
|
|
_node.condition()->accept(*this);
|
|
|
|
// values in loop condition might change during loop iteration
|
|
|
|
forCondition = expr(*_node.condition());
|
|
|
|
}
|
|
|
|
m_loopCheckpoints.emplace();
|
|
|
|
auto indicesAfterCondition = copyVariableIndices();
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
pushPathCondition(forCondition);
|
|
|
|
_node.body().accept(*this);
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
auto [continues, brokeInCurrentIteration] = mergeVariablesFromLoopCheckpoints();
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
// accept loop expression if there was no break
|
|
|
|
if (_node.loopExpression())
|
|
|
|
{
|
|
|
|
auto indicesBreak = copyVariableIndices();
|
|
|
|
_node.loopExpression()->accept(*this);
|
|
|
|
mergeVariables(
|
|
|
|
!brokeInCurrentIteration,
|
|
|
|
copyVariableIndices(),
|
|
|
|
indicesBreak
|
2019-06-25 10:46:17 +00:00
|
|
|
);
|
2023-02-23 18:22:46 +00:00
|
|
|
}
|
|
|
|
popPathCondition();
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
// merges indices modified when accepting loop condition that does no longer hold
|
|
|
|
mergeVariables(
|
|
|
|
!forCondition,
|
|
|
|
indicesAfterCondition,
|
|
|
|
copyVariableIndices()
|
|
|
|
);
|
2019-06-25 10:46:17 +00:00
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
// handles breaks in previous iterations
|
|
|
|
// breaks in current iterations are handled when traversing loop checkpoints
|
|
|
|
// handles case when the loop condition no longer holds but bmc loop iterations still unrolls the loop
|
|
|
|
mergeVariables(
|
|
|
|
broke || !forConditionOnPreviousIteration,
|
|
|
|
indicesBefore,
|
|
|
|
copyVariableIndices()
|
|
|
|
);
|
|
|
|
m_loopCheckpoints.pop();
|
|
|
|
broke = broke || brokeInCurrentIteration;
|
|
|
|
forConditionOnPreviousIteration = forCondition;
|
|
|
|
}
|
|
|
|
if (bmcLoopIterations > 0)
|
|
|
|
m_context.addAssertion(not(forCondition) || broke);
|
2019-06-25 10:46:17 +00:00
|
|
|
m_loopExecutionHappened = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
std::tuple<smtutil::Expression, smtutil::Expression> BMC::mergeVariablesFromLoopCheckpoints()
|
|
|
|
{
|
|
|
|
smtutil::Expression continues(false);
|
|
|
|
smtutil::Expression brokeInCurrentIteration(false);
|
|
|
|
for (auto const& loopControl: m_loopCheckpoints.top())
|
|
|
|
{
|
|
|
|
// use SSAs associated with this break statement only if
|
|
|
|
// loop didn't break or continue earlier in the iteration
|
|
|
|
// loop condition is included in break path conditions
|
|
|
|
mergeVariables(
|
|
|
|
!brokeInCurrentIteration && !continues && loopControl.pathConditions,
|
|
|
|
loopControl.variableIndices,
|
|
|
|
copyVariableIndices()
|
|
|
|
);
|
|
|
|
if (loopControl.kind == LoopControlKind::Break)
|
|
|
|
brokeInCurrentIteration =
|
|
|
|
brokeInCurrentIteration || loopControl.pathConditions;
|
|
|
|
else if (loopControl.kind == LoopControlKind::Continue)
|
|
|
|
continues = continues || loopControl.pathConditions;
|
|
|
|
}
|
|
|
|
return std::pair(continues, brokeInCurrentIteration);
|
|
|
|
}
|
|
|
|
|
2021-01-07 16:20:13 +00:00
|
|
|
bool BMC::visit(TryStatement const& _tryStatement)
|
|
|
|
{
|
|
|
|
FunctionCall const* externalCall = dynamic_cast<FunctionCall const*>(&_tryStatement.externalCall());
|
|
|
|
solAssert(externalCall && externalCall->annotation().tryCall, "");
|
|
|
|
|
|
|
|
externalCall->accept(*this);
|
2021-01-09 15:35:21 +00:00
|
|
|
if (_tryStatement.successClause()->parameters())
|
2021-01-12 14:07:30 +00:00
|
|
|
expressionToTupleAssignment(_tryStatement.successClause()->parameters()->parameters(), *externalCall);
|
2021-01-07 16:20:13 +00:00
|
|
|
|
2021-01-09 15:35:21 +00:00
|
|
|
smtutil::Expression clauseId = m_context.newVariable("clause_choice_" + to_string(m_context.newUniqueId()), smtutil::SortProvider::uintSort);
|
2021-01-07 16:20:13 +00:00
|
|
|
auto const& clauses = _tryStatement.clauses();
|
2021-01-09 15:35:21 +00:00
|
|
|
m_context.addAssertion(clauseId >= 0 && clauseId < clauses.size());
|
2021-01-07 16:20:13 +00:00
|
|
|
solAssert(clauses[0].get() == _tryStatement.successClause(), "First clause of TryStatement should be the success clause");
|
|
|
|
vector<pair<VariableIndices, smtutil::Expression>> clausesVisitResults;
|
|
|
|
for (size_t i = 0; i < clauses.size(); ++i)
|
2021-01-09 15:35:21 +00:00
|
|
|
clausesVisitResults.push_back(visitBranch(clauses[i].get()));
|
2021-01-07 16:20:13 +00:00
|
|
|
|
|
|
|
// merge the information from all clauses
|
|
|
|
smtutil::Expression pathCondition = clausesVisitResults.front().second;
|
2021-01-09 15:35:21 +00:00
|
|
|
auto currentIndices = clausesVisitResults[0].first;
|
2021-01-07 16:20:13 +00:00
|
|
|
for (size_t i = 1; i < clauses.size(); ++i)
|
|
|
|
{
|
2021-03-29 12:44:33 +00:00
|
|
|
mergeVariables(clauseId == i, clausesVisitResults[i].first, currentIndices);
|
2021-01-09 15:35:21 +00:00
|
|
|
currentIndices = copyVariableIndices();
|
2021-01-07 16:20:13 +00:00
|
|
|
pathCondition = pathCondition || clausesVisitResults[i].second;
|
|
|
|
}
|
|
|
|
setPathCondition(pathCondition);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
bool BMC::visit(Break const&)
|
|
|
|
{
|
|
|
|
LoopControl control = {
|
|
|
|
LoopControlKind::Break,
|
|
|
|
currentPathConditions(),
|
|
|
|
copyVariableIndices()
|
|
|
|
};
|
|
|
|
m_loopCheckpoints.top().emplace_back(control);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BMC::visit(Continue const&)
|
|
|
|
{
|
|
|
|
LoopControl control = {
|
|
|
|
LoopControlKind::Continue,
|
|
|
|
currentPathConditions(),
|
|
|
|
copyVariableIndices()
|
|
|
|
};
|
|
|
|
m_loopCheckpoints.top().emplace_back(control);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void BMC::endVisit(UnaryOperation const& _op)
|
|
|
|
{
|
|
|
|
SMTEncoder::endVisit(_op);
|
|
|
|
|
2020-04-22 17:57:00 +00:00
|
|
|
if (
|
|
|
|
_op.annotation().type->category() == Type::Category::RationalNumber ||
|
|
|
|
_op.annotation().type->category() == Type::Category::FixedPoint
|
|
|
|
)
|
2019-06-25 10:46:17 +00:00
|
|
|
return;
|
|
|
|
|
2020-12-10 15:51:10 +00:00
|
|
|
if (_op.getOperator() == Token::Sub && smt::isInteger(*_op.annotation().type))
|
2019-06-25 10:46:17 +00:00
|
|
|
addVerificationTarget(
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType::UnderOverflow,
|
2019-06-25 10:46:17 +00:00
|
|
|
expr(_op),
|
|
|
|
&_op
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BMC::endVisit(FunctionCall const& _funCall)
|
|
|
|
{
|
2020-04-08 17:38:30 +00:00
|
|
|
auto functionCallKind = *_funCall.annotation().kind;
|
|
|
|
|
|
|
|
if (functionCallKind != FunctionCallKind::FunctionCall)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
|
|
|
SMTEncoder::endVisit(_funCall);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionType const& funType = dynamic_cast<FunctionType const&>(*_funCall.expression().annotation().type);
|
|
|
|
switch (funType.kind())
|
|
|
|
{
|
|
|
|
case FunctionType::Kind::Assert:
|
|
|
|
visitAssert(_funCall);
|
|
|
|
SMTEncoder::endVisit(_funCall);
|
|
|
|
break;
|
|
|
|
case FunctionType::Kind::Require:
|
|
|
|
visitRequire(_funCall);
|
|
|
|
SMTEncoder::endVisit(_funCall);
|
|
|
|
break;
|
|
|
|
case FunctionType::Kind::Internal:
|
|
|
|
case FunctionType::Kind::External:
|
|
|
|
case FunctionType::Kind::DelegateCall:
|
|
|
|
case FunctionType::Kind::BareCall:
|
|
|
|
case FunctionType::Kind::BareCallCode:
|
|
|
|
case FunctionType::Kind::BareDelegateCall:
|
|
|
|
case FunctionType::Kind::BareStaticCall:
|
|
|
|
case FunctionType::Kind::Creation:
|
|
|
|
SMTEncoder::endVisit(_funCall);
|
|
|
|
internalOrExternalFunctionCall(_funCall);
|
|
|
|
break;
|
|
|
|
case FunctionType::Kind::Send:
|
|
|
|
case FunctionType::Kind::Transfer:
|
|
|
|
{
|
|
|
|
auto value = _funCall.arguments().front();
|
|
|
|
solAssert(value, "");
|
2021-02-03 10:31:17 +00:00
|
|
|
smtutil::Expression thisBalance = state().balance();
|
2019-06-25 10:46:17 +00:00
|
|
|
|
|
|
|
addVerificationTarget(
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType::Balance,
|
2019-06-25 10:46:17 +00:00
|
|
|
thisBalance < expr(*value),
|
|
|
|
&_funCall
|
|
|
|
);
|
2020-09-18 16:55:23 +00:00
|
|
|
|
|
|
|
SMTEncoder::endVisit(_funCall);
|
2019-06-25 10:46:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-10-13 16:00:26 +00:00
|
|
|
case FunctionType::Kind::KECCAK256:
|
|
|
|
case FunctionType::Kind::ECRecover:
|
|
|
|
case FunctionType::Kind::SHA256:
|
|
|
|
case FunctionType::Kind::RIPEMD160:
|
2020-09-29 08:36:20 +00:00
|
|
|
case FunctionType::Kind::BlockHash:
|
2020-09-28 10:09:40 +00:00
|
|
|
case FunctionType::Kind::AddMod:
|
|
|
|
case FunctionType::Kind::MulMod:
|
2021-09-17 19:28:07 +00:00
|
|
|
case FunctionType::Kind::Unwrap:
|
|
|
|
case FunctionType::Kind::Wrap:
|
2020-09-28 10:09:40 +00:00
|
|
|
[[fallthrough]];
|
2019-06-25 10:46:17 +00:00
|
|
|
default:
|
|
|
|
SMTEncoder::endVisit(_funCall);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 14:42:32 +00:00
|
|
|
void BMC::endVisit(Return const& _return)
|
|
|
|
{
|
|
|
|
SMTEncoder::endVisit(_return);
|
|
|
|
setPathCondition(smtutil::Expression(false));
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
/// Visitor helpers.
|
|
|
|
|
|
|
|
void BMC::visitAssert(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
auto const& args = _funCall.arguments();
|
|
|
|
solAssert(args.size() == 1, "");
|
|
|
|
solAssert(args.front()->annotation().type->category() == Type::Category::Bool, "");
|
|
|
|
addVerificationTarget(
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType::Assert,
|
2019-06-25 10:46:17 +00:00
|
|
|
expr(*args.front()),
|
|
|
|
&_funCall
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BMC::visitRequire(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
auto const& args = _funCall.arguments();
|
2019-07-01 14:11:29 +00:00
|
|
|
solAssert(args.size() >= 1, "");
|
2019-06-25 10:46:17 +00:00
|
|
|
solAssert(args.front()->annotation().type->category() == Type::Category::Bool, "");
|
2023-02-23 18:22:46 +00:00
|
|
|
if (isRootFunction() && !isInsideLoop())
|
2019-06-25 10:46:17 +00:00
|
|
|
addVerificationTarget(
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType::ConstantCondition,
|
2019-06-25 10:46:17 +00:00
|
|
|
expr(*args.front()),
|
|
|
|
args.front().get()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-28 10:09:40 +00:00
|
|
|
void BMC::visitAddMulMod(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
solAssert(_funCall.arguments().at(2), "");
|
|
|
|
addVerificationTarget(
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType::DivByZero,
|
2020-09-28 10:09:40 +00:00
|
|
|
expr(*_funCall.arguments().at(2)),
|
|
|
|
&_funCall
|
|
|
|
);
|
|
|
|
|
|
|
|
SMTEncoder::visitAddMulMod(_funCall);
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void BMC::inlineFunctionCall(FunctionCall const& _funCall)
|
|
|
|
{
|
2021-01-25 17:17:56 +00:00
|
|
|
solAssert(shouldInlineFunctionCall(_funCall, currentScopeContract(), m_currentContract), "");
|
|
|
|
auto funDef = functionCallToDefinition(_funCall, currentScopeContract(), m_currentContract);
|
2019-06-25 10:46:17 +00:00
|
|
|
solAssert(funDef, "");
|
2019-08-09 13:26:47 +00:00
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
if (visitedFunction(funDef))
|
2019-08-09 13:26:47 +00:00
|
|
|
{
|
|
|
|
auto const& returnParams = funDef->returnParameters();
|
|
|
|
for (auto param: returnParams)
|
|
|
|
{
|
|
|
|
m_context.newValue(*param);
|
|
|
|
m_context.setUnknownValue(*param);
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 10:46:17 +00:00
|
|
|
else
|
|
|
|
{
|
2020-12-22 19:25:31 +00:00
|
|
|
initializeFunctionCallParameters(*funDef, symbolicArguments(_funCall, m_currentContract));
|
2019-06-25 10:46:17 +00:00
|
|
|
|
|
|
|
// The reason why we need to pushCallStack here instead of visit(FunctionDefinition)
|
|
|
|
// is that there we don't have `_funCall`.
|
|
|
|
pushCallStack({funDef, &_funCall});
|
2020-11-14 14:42:32 +00:00
|
|
|
pushPathCondition(currentPathConditions());
|
2021-01-15 09:49:38 +00:00
|
|
|
auto oldChecked = std::exchange(m_checked, true);
|
2019-06-25 10:46:17 +00:00
|
|
|
funDef->accept(*this);
|
2021-01-15 09:49:38 +00:00
|
|
|
m_checked = oldChecked;
|
2020-11-14 14:42:32 +00:00
|
|
|
popPathCondition();
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
2019-08-09 13:26:47 +00:00
|
|
|
|
2020-12-22 19:25:31 +00:00
|
|
|
createReturnedExpressions(_funCall, m_currentContract);
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BMC::internalOrExternalFunctionCall(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
auto const& funType = dynamic_cast<FunctionType const&>(*_funCall.expression().annotation().type);
|
2021-01-25 17:17:56 +00:00
|
|
|
if (shouldInlineFunctionCall(_funCall, currentScopeContract(), m_currentContract))
|
2019-06-25 10:46:17 +00:00
|
|
|
inlineFunctionCall(_funCall);
|
2021-10-12 09:12:18 +00:00
|
|
|
else if (publicGetter(_funCall.expression()))
|
2020-11-26 16:14:58 +00:00
|
|
|
{
|
|
|
|
// Do nothing here.
|
|
|
|
// The processing happens in SMT Encoder, but we need to prevent the resetting of the state variables.
|
|
|
|
}
|
2019-06-25 10:46:17 +00:00
|
|
|
else if (funType.kind() == FunctionType::Kind::Internal)
|
2023-03-06 13:19:58 +00:00
|
|
|
m_unsupportedErrors.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
5729_error,
|
2019-06-25 10:46:17 +00:00
|
|
|
_funCall.location(),
|
2020-11-05 12:30:42 +00:00
|
|
|
"BMC does not yet implement this type of function call."
|
2019-06-25 10:46:17 +00:00
|
|
|
);
|
2021-08-25 12:09:46 +00:00
|
|
|
else if (funType.kind() == FunctionType::Kind::BareStaticCall)
|
|
|
|
{
|
|
|
|
// Do nothing here.
|
|
|
|
// Neither storage nor balances should be modified.
|
|
|
|
}
|
2019-06-25 10:46:17 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_externalFunctionCallHappened = true;
|
2021-02-03 10:42:20 +00:00
|
|
|
resetStorageVariables();
|
2021-08-25 09:29:42 +00:00
|
|
|
resetBalances();
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
pair<smtutil::Expression, smtutil::Expression> BMC::arithmeticOperation(
|
2019-06-25 10:46:17 +00:00
|
|
|
Token _op,
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression const& _left,
|
|
|
|
smtutil::Expression const& _right,
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* _commonType,
|
2019-06-25 10:46:17 +00:00
|
|
|
Expression const& _expression
|
|
|
|
)
|
|
|
|
{
|
2020-12-16 17:32:34 +00:00
|
|
|
// Unchecked does not disable div by 0 checks.
|
2019-06-25 10:46:17 +00:00
|
|
|
if (_op == Token::Div || _op == Token::Mod)
|
|
|
|
addVerificationTarget(
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType::DivByZero,
|
2019-06-25 10:46:17 +00:00
|
|
|
_right,
|
|
|
|
&_expression
|
|
|
|
);
|
|
|
|
|
|
|
|
auto values = SMTEncoder::arithmeticOperation(_op, _left, _right, _commonType, _expression);
|
|
|
|
|
2020-12-16 17:32:34 +00:00
|
|
|
if (!m_checked)
|
|
|
|
return values;
|
|
|
|
|
2020-08-11 10:01:45 +00:00
|
|
|
auto const* intType = dynamic_cast<IntegerType const*>(_commonType);
|
|
|
|
if (!intType)
|
|
|
|
intType = TypeProvider::uint256();
|
|
|
|
|
|
|
|
// Mod does not need underflow/overflow checks.
|
|
|
|
if (_op == Token::Mod)
|
|
|
|
return values;
|
|
|
|
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType type;
|
2020-08-11 10:01:45 +00:00
|
|
|
// The order matters here:
|
|
|
|
// If _op is Div and intType is signed, we only care about overflow.
|
|
|
|
if (_op == Token::Div)
|
|
|
|
{
|
|
|
|
if (intType->isSigned())
|
|
|
|
// Signed division can only overflow.
|
2021-01-19 11:56:22 +00:00
|
|
|
type = VerificationTargetType::Overflow;
|
2020-08-11 10:01:45 +00:00
|
|
|
else
|
|
|
|
// Unsigned division cannot underflow/overflow.
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
else if (intType->isSigned())
|
2021-01-19 11:56:22 +00:00
|
|
|
type = VerificationTargetType::UnderOverflow;
|
2020-08-11 10:01:45 +00:00
|
|
|
else if (_op == Token::Sub)
|
2021-01-19 11:56:22 +00:00
|
|
|
type = VerificationTargetType::Underflow;
|
2020-08-11 10:01:45 +00:00
|
|
|
else if (_op == Token::Add || _op == Token::Mul)
|
2021-01-19 11:56:22 +00:00
|
|
|
type = VerificationTargetType::Overflow;
|
2020-08-11 10:01:45 +00:00
|
|
|
else
|
|
|
|
solAssert(false, "");
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
addVerificationTarget(
|
2020-08-11 10:01:45 +00:00
|
|
|
type,
|
2019-06-25 10:46:17 +00:00
|
|
|
values.second,
|
|
|
|
&_expression
|
|
|
|
);
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BMC::reset()
|
|
|
|
{
|
|
|
|
m_externalFunctionCallHappened = false;
|
|
|
|
m_loopExecutionHappened = false;
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
pair<vector<smtutil::Expression>, vector<string>> BMC::modelExpressions()
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2020-05-19 12:14:46 +00:00
|
|
|
vector<smtutil::Expression> expressionsToEvaluate;
|
2019-06-25 10:46:17 +00:00
|
|
|
vector<string> expressionNames;
|
|
|
|
for (auto const& var: m_context.variables())
|
|
|
|
if (var.first->type()->isValueType())
|
|
|
|
{
|
|
|
|
expressionsToEvaluate.emplace_back(currentValue(*var.first));
|
|
|
|
expressionNames.push_back(var.first->name());
|
|
|
|
}
|
|
|
|
for (auto const& var: m_context.globalSymbols())
|
|
|
|
{
|
|
|
|
auto const& type = var.second->type();
|
|
|
|
if (
|
|
|
|
type->isValueType() &&
|
2020-08-27 13:16:02 +00:00
|
|
|
smt::smtKind(*type) != smtutil::Kind::Function
|
2019-06-25 10:46:17 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
expressionsToEvaluate.emplace_back(var.second->currentValue());
|
|
|
|
expressionNames.push_back(var.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto const& uf: m_uninterpretedTerms)
|
|
|
|
if (uf->annotation().type->isValueType())
|
|
|
|
{
|
|
|
|
expressionsToEvaluate.emplace_back(expr(*uf));
|
2021-07-01 15:28:06 +00:00
|
|
|
string expressionName;
|
|
|
|
if (uf->location().hasText())
|
|
|
|
expressionName = m_charStreamProvider.charStream(*uf->location().sourceName).text(
|
|
|
|
uf->location()
|
|
|
|
);
|
2022-08-23 17:28:45 +00:00
|
|
|
expressionNames.push_back(std::move(expressionName));
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {expressionsToEvaluate, expressionNames};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verification targets.
|
|
|
|
|
2023-02-09 16:07:13 +00:00
|
|
|
string BMC::targetDescription(BMCVerificationTarget const& _target)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
_target.type == VerificationTargetType::Underflow ||
|
|
|
|
_target.type == VerificationTargetType::Overflow
|
|
|
|
)
|
|
|
|
{
|
|
|
|
auto const* intType = dynamic_cast<IntegerType const*>(_target.expression->annotation().type);
|
|
|
|
if (!intType)
|
|
|
|
intType = TypeProvider::uint256();
|
|
|
|
|
|
|
|
if (_target.type == VerificationTargetType::Underflow)
|
|
|
|
return "Underflow (resulting value less than " + formatNumberReadable(intType->minValue()) + ")";
|
|
|
|
return "Overflow (resulting value larger than " + formatNumberReadable(intType->maxValue()) + ")";
|
|
|
|
}
|
|
|
|
else if (_target.type == VerificationTargetType::DivByZero)
|
|
|
|
return "Division by zero";
|
|
|
|
else if (_target.type == VerificationTargetType::Assert)
|
|
|
|
return "Assertion violation";
|
|
|
|
else if (_target.type == VerificationTargetType::Balance)
|
|
|
|
return "Insufficient funds";
|
|
|
|
solAssert(false);
|
|
|
|
}
|
|
|
|
|
2020-12-16 17:32:34 +00:00
|
|
|
void BMC::checkVerificationTargets()
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
|
|
|
for (auto& target: m_verificationTargets)
|
2020-12-16 17:32:34 +00:00
|
|
|
checkVerificationTarget(target);
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 17:32:34 +00:00
|
|
|
void BMC::checkVerificationTarget(BMCVerificationTarget& _target)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
|
|
|
switch (_target.type)
|
|
|
|
{
|
2021-01-19 11:56:22 +00:00
|
|
|
case VerificationTargetType::ConstantCondition:
|
2019-06-25 10:46:17 +00:00
|
|
|
checkConstantCondition(_target);
|
|
|
|
break;
|
2021-01-19 11:56:22 +00:00
|
|
|
case VerificationTargetType::Underflow:
|
2020-12-16 17:32:34 +00:00
|
|
|
checkUnderflow(_target);
|
2019-06-25 10:46:17 +00:00
|
|
|
break;
|
2021-01-19 11:56:22 +00:00
|
|
|
case VerificationTargetType::Overflow:
|
2020-12-16 17:32:34 +00:00
|
|
|
checkOverflow(_target);
|
2019-06-25 10:46:17 +00:00
|
|
|
break;
|
2021-01-19 11:56:22 +00:00
|
|
|
case VerificationTargetType::UnderOverflow:
|
2020-12-16 17:32:34 +00:00
|
|
|
checkUnderflow(_target);
|
|
|
|
checkOverflow(_target);
|
2019-06-25 10:46:17 +00:00
|
|
|
break;
|
2021-01-19 11:56:22 +00:00
|
|
|
case VerificationTargetType::DivByZero:
|
2019-06-25 10:46:17 +00:00
|
|
|
checkDivByZero(_target);
|
|
|
|
break;
|
2021-01-19 11:56:22 +00:00
|
|
|
case VerificationTargetType::Balance:
|
2019-06-25 10:46:17 +00:00
|
|
|
checkBalance(_target);
|
|
|
|
break;
|
2021-01-19 11:56:22 +00:00
|
|
|
case VerificationTargetType::Assert:
|
2019-06-25 10:46:17 +00:00
|
|
|
checkAssert(_target);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
solAssert(false, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:09:45 +00:00
|
|
|
void BMC::checkConstantCondition(BMCVerificationTarget& _target)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
|
|
|
checkBooleanNotConstant(
|
|
|
|
*_target.expression,
|
|
|
|
_target.constraints,
|
|
|
|
_target.value,
|
2020-05-12 00:29:54 +00:00
|
|
|
_target.callStack
|
2019-06-25 10:46:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-16 17:32:34 +00:00
|
|
|
void BMC::checkUnderflow(BMCVerificationTarget& _target)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
|
|
|
solAssert(
|
2021-01-19 11:56:22 +00:00
|
|
|
_target.type == VerificationTargetType::Underflow ||
|
|
|
|
_target.type == VerificationTargetType::UnderOverflow,
|
2019-06-25 10:46:17 +00:00
|
|
|
""
|
|
|
|
);
|
2020-08-11 10:01:45 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
m_solvedTargets.count(_target.expression) && (
|
2021-01-19 11:56:22 +00:00
|
|
|
m_solvedTargets.at(_target.expression).count(VerificationTargetType::Underflow) ||
|
|
|
|
m_solvedTargets.at(_target.expression).count(VerificationTargetType::UnderOverflow)
|
2020-08-11 10:01:45 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto const* intType = dynamic_cast<IntegerType const*>(_target.expression->annotation().type);
|
|
|
|
if (!intType)
|
2020-05-29 16:13:03 +00:00
|
|
|
intType = TypeProvider::uint256();
|
2020-08-11 10:01:45 +00:00
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
checkCondition(
|
2023-02-09 16:07:13 +00:00
|
|
|
_target,
|
2020-12-16 17:32:34 +00:00
|
|
|
_target.constraints && _target.value < smt::minValue(*intType),
|
2019-06-25 10:46:17 +00:00
|
|
|
_target.callStack,
|
|
|
|
_target.modelExpressions,
|
|
|
|
_target.expression->location(),
|
2020-05-12 00:29:54 +00:00
|
|
|
4144_error,
|
|
|
|
8312_error,
|
2019-06-25 10:46:17 +00:00
|
|
|
"<result>",
|
|
|
|
&_target.value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-16 17:32:34 +00:00
|
|
|
void BMC::checkOverflow(BMCVerificationTarget& _target)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
|
|
|
solAssert(
|
2021-01-19 11:56:22 +00:00
|
|
|
_target.type == VerificationTargetType::Overflow ||
|
|
|
|
_target.type == VerificationTargetType::UnderOverflow,
|
2019-06-25 10:46:17 +00:00
|
|
|
""
|
|
|
|
);
|
2020-08-11 10:01:45 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
m_solvedTargets.count(_target.expression) && (
|
2021-01-19 11:56:22 +00:00
|
|
|
m_solvedTargets.at(_target.expression).count(VerificationTargetType::Overflow) ||
|
|
|
|
m_solvedTargets.at(_target.expression).count(VerificationTargetType::UnderOverflow)
|
2020-08-11 10:01:45 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto const* intType = dynamic_cast<IntegerType const*>(_target.expression->annotation().type);
|
|
|
|
if (!intType)
|
2020-05-29 16:13:03 +00:00
|
|
|
intType = TypeProvider::uint256();
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
checkCondition(
|
2023-02-09 16:07:13 +00:00
|
|
|
_target,
|
2020-12-16 17:32:34 +00:00
|
|
|
_target.constraints && _target.value > smt::maxValue(*intType),
|
2019-06-25 10:46:17 +00:00
|
|
|
_target.callStack,
|
|
|
|
_target.modelExpressions,
|
|
|
|
_target.expression->location(),
|
2020-05-12 00:29:54 +00:00
|
|
|
2661_error,
|
|
|
|
8065_error,
|
2019-06-25 10:46:17 +00:00
|
|
|
"<result>",
|
|
|
|
&_target.value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:09:45 +00:00
|
|
|
void BMC::checkDivByZero(BMCVerificationTarget& _target)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2021-01-19 11:56:22 +00:00
|
|
|
solAssert(_target.type == VerificationTargetType::DivByZero, "");
|
2020-10-19 16:54:42 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
m_solvedTargets.count(_target.expression) &&
|
2021-01-19 11:56:22 +00:00
|
|
|
m_solvedTargets.at(_target.expression).count(VerificationTargetType::DivByZero)
|
2020-10-19 16:54:42 +00:00
|
|
|
)
|
|
|
|
return;
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
checkCondition(
|
2023-02-09 16:07:13 +00:00
|
|
|
_target,
|
2019-06-25 10:46:17 +00:00
|
|
|
_target.constraints && (_target.value == 0),
|
|
|
|
_target.callStack,
|
|
|
|
_target.modelExpressions,
|
|
|
|
_target.expression->location(),
|
2020-05-12 00:29:54 +00:00
|
|
|
3046_error,
|
|
|
|
5272_error,
|
2019-06-25 10:46:17 +00:00
|
|
|
"<result>",
|
|
|
|
&_target.value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:09:45 +00:00
|
|
|
void BMC::checkBalance(BMCVerificationTarget& _target)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2021-01-19 11:56:22 +00:00
|
|
|
solAssert(_target.type == VerificationTargetType::Balance, "");
|
2019-06-25 10:46:17 +00:00
|
|
|
checkCondition(
|
2023-02-09 16:07:13 +00:00
|
|
|
_target,
|
2019-06-25 10:46:17 +00:00
|
|
|
_target.constraints && _target.value,
|
|
|
|
_target.callStack,
|
|
|
|
_target.modelExpressions,
|
|
|
|
_target.expression->location(),
|
2020-05-12 00:29:54 +00:00
|
|
|
1236_error,
|
|
|
|
4010_error,
|
2019-06-25 10:46:17 +00:00
|
|
|
"address(this).balance"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:09:45 +00:00
|
|
|
void BMC::checkAssert(BMCVerificationTarget& _target)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2021-01-19 11:56:22 +00:00
|
|
|
solAssert(_target.type == VerificationTargetType::Assert, "");
|
2020-07-10 08:28:49 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
m_solvedTargets.count(_target.expression) &&
|
|
|
|
m_solvedTargets.at(_target.expression).count(_target.type)
|
|
|
|
)
|
|
|
|
return;
|
|
|
|
|
|
|
|
checkCondition(
|
2023-02-09 16:07:13 +00:00
|
|
|
_target,
|
2020-07-10 08:28:49 +00:00
|
|
|
_target.constraints && !_target.value,
|
|
|
|
_target.callStack,
|
|
|
|
_target.modelExpressions,
|
|
|
|
_target.expression->location(),
|
|
|
|
4661_error,
|
2023-02-09 16:07:13 +00:00
|
|
|
7812_error
|
2020-07-10 08:28:49 +00:00
|
|
|
);
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BMC::addVerificationTarget(
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType _type,
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression const& _value,
|
2019-06-25 10:46:17 +00:00
|
|
|
Expression const* _expression
|
|
|
|
)
|
|
|
|
{
|
2021-04-15 10:31:45 +00:00
|
|
|
if (!m_settings.targets.has(_type) || (m_currentContract && !shouldAnalyze(*m_currentContract)))
|
2021-01-19 11:56:22 +00:00
|
|
|
return;
|
|
|
|
|
2020-02-12 01:09:45 +00:00
|
|
|
BMCVerificationTarget target{
|
|
|
|
{
|
|
|
|
_type,
|
|
|
|
_value,
|
|
|
|
currentPathConditions() && m_context.assertions()
|
|
|
|
},
|
2019-06-25 10:46:17 +00:00
|
|
|
_expression,
|
|
|
|
m_callStack,
|
|
|
|
modelExpressions()
|
|
|
|
};
|
2021-01-19 11:56:22 +00:00
|
|
|
if (_type == VerificationTargetType::ConstantCondition)
|
2019-06-25 10:46:17 +00:00
|
|
|
checkVerificationTarget(target);
|
|
|
|
else
|
2022-08-23 17:28:45 +00:00
|
|
|
m_verificationTargets.emplace_back(std::move(target));
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Solving.
|
|
|
|
|
|
|
|
void BMC::checkCondition(
|
2023-02-09 16:07:13 +00:00
|
|
|
BMCVerificationTarget const& _target,
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression _condition,
|
2020-05-12 00:29:54 +00:00
|
|
|
vector<SMTEncoder::CallStackEntry> const& _callStack,
|
2020-05-19 12:14:46 +00:00
|
|
|
pair<vector<smtutil::Expression>, vector<string>> const& _modelExpressions,
|
2019-06-25 10:46:17 +00:00
|
|
|
SourceLocation const& _location,
|
2020-05-12 00:29:54 +00:00
|
|
|
ErrorId _errorHappens,
|
|
|
|
ErrorId _errorMightHappen,
|
2019-06-25 10:46:17 +00:00
|
|
|
string const& _additionalValueName,
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression const* _additionalValue
|
2019-06-25 10:46:17 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
m_interface->push();
|
|
|
|
m_interface->addAssertion(_condition);
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
vector<smtutil::Expression> expressionsToEvaluate;
|
2019-06-25 10:46:17 +00:00
|
|
|
vector<string> expressionNames;
|
|
|
|
tie(expressionsToEvaluate, expressionNames) = _modelExpressions;
|
2023-02-23 18:22:46 +00:00
|
|
|
if (!_callStack.empty())
|
2019-06-25 10:46:17 +00:00
|
|
|
if (_additionalValue)
|
|
|
|
{
|
|
|
|
expressionsToEvaluate.emplace_back(*_additionalValue);
|
|
|
|
expressionNames.push_back(_additionalValueName);
|
|
|
|
}
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::CheckResult result;
|
2019-06-25 10:46:17 +00:00
|
|
|
vector<string> values;
|
|
|
|
tie(result, values) = checkSatisfiableAndGenerateModel(expressionsToEvaluate);
|
|
|
|
|
|
|
|
string extraComment = SMTEncoder::extraComment();
|
|
|
|
if (m_loopExecutionHappened)
|
|
|
|
extraComment +=
|
2023-02-23 18:22:46 +00:00
|
|
|
"False negatives are possible when unrolling loops.\n"
|
|
|
|
"This is due to the possibility that the BMC loop iteration setting is"
|
|
|
|
" smaller than the actual number of iterations needed to complete a loop.";
|
2019-06-25 10:46:17 +00:00
|
|
|
if (m_externalFunctionCallHappened)
|
2020-10-14 15:44:40 +00:00
|
|
|
extraComment +=
|
2019-06-25 10:46:17 +00:00
|
|
|
"\nNote that external function calls are not inlined,"
|
|
|
|
" even if the source code of the function is available."
|
|
|
|
" This is due to the possibility that the actual called contract"
|
|
|
|
" has the same ABI but implements the function differently.";
|
|
|
|
|
|
|
|
SecondarySourceLocation secondaryLocation{};
|
|
|
|
secondaryLocation.append(extraComment, SourceLocation{});
|
|
|
|
|
|
|
|
switch (result)
|
|
|
|
{
|
2020-05-19 12:14:46 +00:00
|
|
|
case smtutil::CheckResult::SATISFIABLE:
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2020-11-16 16:52:06 +00:00
|
|
|
solAssert(!_callStack.empty(), "");
|
2019-06-25 10:46:17 +00:00
|
|
|
std::ostringstream message;
|
2023-02-09 16:07:13 +00:00
|
|
|
message << "BMC: " << targetDescription(_target) << " happens here.";
|
2021-05-19 15:35:19 +00:00
|
|
|
|
2020-11-16 16:52:06 +00:00
|
|
|
std::ostringstream modelMessage;
|
2021-05-19 15:35:19 +00:00
|
|
|
// Sometimes models have complex smtlib2 expressions that SMTLib2Interface fails to parse.
|
|
|
|
if (values.size() == expressionNames.size())
|
|
|
|
{
|
|
|
|
modelMessage << "Counterexample:\n";
|
|
|
|
map<string, string> sortedModel;
|
|
|
|
for (size_t i = 0; i < values.size(); ++i)
|
|
|
|
if (expressionsToEvaluate.at(i).name != values.at(i))
|
|
|
|
sortedModel[expressionNames.at(i)] = values.at(i);
|
|
|
|
|
|
|
|
for (auto const& eval: sortedModel)
|
|
|
|
modelMessage << " " << eval.first << " = " << eval.second << "\n";
|
|
|
|
}
|
2020-10-14 15:44:40 +00:00
|
|
|
|
2020-11-16 16:52:06 +00:00
|
|
|
m_errorReporter.warning(
|
|
|
|
_errorHappens,
|
|
|
|
_location,
|
|
|
|
message.str(),
|
|
|
|
SecondarySourceLocation().append(modelMessage.str(), SourceLocation{})
|
|
|
|
.append(SMTEncoder::callStackMessage(_callStack))
|
2022-08-23 17:28:45 +00:00
|
|
|
.append(std::move(secondaryLocation))
|
2020-11-16 16:52:06 +00:00
|
|
|
);
|
2019-06-25 10:46:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-19 12:14:46 +00:00
|
|
|
case smtutil::CheckResult::UNSATISFIABLE:
|
2023-02-09 16:07:13 +00:00
|
|
|
{
|
|
|
|
m_safeTargets[_target.expression].insert(_target);
|
2019-06-25 10:46:17 +00:00
|
|
|
break;
|
2023-02-09 16:07:13 +00:00
|
|
|
}
|
2020-05-19 12:14:46 +00:00
|
|
|
case smtutil::CheckResult::UNKNOWN:
|
2021-07-02 12:42:53 +00:00
|
|
|
{
|
|
|
|
++m_unprovedAmt;
|
|
|
|
if (m_settings.showUnproved)
|
2023-02-09 16:07:13 +00:00
|
|
|
m_errorReporter.warning(_errorMightHappen, _location, "BMC: " + targetDescription(_target) + " might happen here.", secondaryLocation);
|
2019-06-25 10:46:17 +00:00
|
|
|
break;
|
2021-07-02 12:42:53 +00:00
|
|
|
}
|
2020-05-19 12:14:46 +00:00
|
|
|
case smtutil::CheckResult::CONFLICTING:
|
2020-09-25 17:09:06 +00:00
|
|
|
m_errorReporter.warning(1584_error, _location, "BMC: At least two SMT solvers provided conflicting answers. Results might not be sound.");
|
2019-06-25 10:46:17 +00:00
|
|
|
break;
|
2020-05-19 12:14:46 +00:00
|
|
|
case smtutil::CheckResult::ERROR:
|
2020-09-25 17:09:06 +00:00
|
|
|
m_errorReporter.warning(1823_error, _location, "BMC: Error trying to invoke SMT solver.");
|
2019-06-25 10:46:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_interface->pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BMC::checkBooleanNotConstant(
|
|
|
|
Expression const& _condition,
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression const& _constraints,
|
|
|
|
smtutil::Expression const& _value,
|
2020-05-12 00:29:54 +00:00
|
|
|
vector<SMTEncoder::CallStackEntry> const& _callStack
|
2019-06-25 10:46:17 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
// Do not check for const-ness if this is a constant.
|
|
|
|
if (dynamic_cast<Literal const*>(&_condition))
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_interface->push();
|
|
|
|
m_interface->addAssertion(_constraints && _value);
|
|
|
|
auto positiveResult = checkSatisfiable();
|
|
|
|
m_interface->pop();
|
|
|
|
|
|
|
|
m_interface->push();
|
|
|
|
m_interface->addAssertion(_constraints && !_value);
|
|
|
|
auto negatedResult = checkSatisfiable();
|
|
|
|
m_interface->pop();
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
if (positiveResult == smtutil::CheckResult::ERROR || negatedResult == smtutil::CheckResult::ERROR)
|
2020-11-05 12:30:42 +00:00
|
|
|
m_errorReporter.warning(8592_error, _condition.location(), "BMC: Error trying to invoke SMT solver.");
|
2020-05-19 12:14:46 +00:00
|
|
|
else if (positiveResult == smtutil::CheckResult::CONFLICTING || negatedResult == smtutil::CheckResult::CONFLICTING)
|
2020-11-05 12:30:42 +00:00
|
|
|
m_errorReporter.warning(3356_error, _condition.location(), "BMC: At least two SMT solvers provided conflicting answers. Results might not be sound.");
|
2020-05-19 12:14:46 +00:00
|
|
|
else if (positiveResult == smtutil::CheckResult::SATISFIABLE && negatedResult == smtutil::CheckResult::SATISFIABLE)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
|
|
|
// everything fine.
|
|
|
|
}
|
2020-05-19 12:14:46 +00:00
|
|
|
else if (positiveResult == smtutil::CheckResult::UNKNOWN || negatedResult == smtutil::CheckResult::UNKNOWN)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
|
|
|
// can't do anything.
|
|
|
|
}
|
2020-05-19 12:14:46 +00:00
|
|
|
else if (positiveResult == smtutil::CheckResult::UNSATISFIABLE && negatedResult == smtutil::CheckResult::UNSATISFIABLE)
|
2020-11-05 12:30:42 +00:00
|
|
|
m_errorReporter.warning(2512_error, _condition.location(), "BMC: Condition unreachable.", SMTEncoder::callStackMessage(_callStack));
|
2019-06-25 10:46:17 +00:00
|
|
|
else
|
|
|
|
{
|
2020-05-12 00:29:54 +00:00
|
|
|
string description;
|
2020-05-19 12:14:46 +00:00
|
|
|
if (positiveResult == smtutil::CheckResult::SATISFIABLE)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2020-05-19 12:14:46 +00:00
|
|
|
solAssert(negatedResult == smtutil::CheckResult::UNSATISFIABLE, "");
|
2020-09-25 17:09:06 +00:00
|
|
|
description = "BMC: Condition is always true.";
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-19 12:14:46 +00:00
|
|
|
solAssert(positiveResult == smtutil::CheckResult::UNSATISFIABLE, "");
|
|
|
|
solAssert(negatedResult == smtutil::CheckResult::SATISFIABLE, "");
|
2020-09-25 17:09:06 +00:00
|
|
|
description = "BMC: Condition is always false.";
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
6838_error,
|
2019-06-25 10:46:17 +00:00
|
|
|
_condition.location(),
|
2020-05-12 00:29:54 +00:00
|
|
|
description,
|
2019-06-25 10:46:17 +00:00
|
|
|
SMTEncoder::callStackMessage(_callStack)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
pair<smtutil::CheckResult, vector<string>>
|
|
|
|
BMC::checkSatisfiableAndGenerateModel(vector<smtutil::Expression> const& _expressionsToEvaluate)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::CheckResult result;
|
2019-06-25 10:46:17 +00:00
|
|
|
vector<string> values;
|
|
|
|
try
|
|
|
|
{
|
2023-04-26 10:50:36 +00:00
|
|
|
if (m_settings.printQuery)
|
|
|
|
{
|
|
|
|
auto portfolio = dynamic_cast<smtutil::SMTPortfolio*>(m_interface.get());
|
|
|
|
string smtlibCode = portfolio->dumpQuery(_expressionsToEvaluate);
|
|
|
|
m_errorReporter.info(
|
|
|
|
6240_error,
|
|
|
|
"BMC: Requested query:\n" + smtlibCode
|
|
|
|
);
|
|
|
|
}
|
2019-06-25 10:46:17 +00:00
|
|
|
tie(result, values) = m_interface->check(_expressionsToEvaluate);
|
|
|
|
}
|
2020-05-19 12:14:46 +00:00
|
|
|
catch (smtutil::SolverError const& _e)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
2020-11-05 12:30:42 +00:00
|
|
|
string description("BMC: Error querying SMT solver");
|
2019-06-25 10:46:17 +00:00
|
|
|
if (_e.comment())
|
|
|
|
description += ": " + *_e.comment();
|
2020-05-05 22:38:28 +00:00
|
|
|
m_errorReporter.warning(8140_error, description);
|
2020-05-19 12:14:46 +00:00
|
|
|
result = smtutil::CheckResult::ERROR;
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (string& value: values)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Parse and re-format nicely
|
|
|
|
value = formatNumberReadable(bigint(value));
|
|
|
|
}
|
|
|
|
catch (...) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_pair(result, values);
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::CheckResult BMC::checkSatisfiable()
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
|
|
|
return checkSatisfiableAndGenerateModel({}).first;
|
|
|
|
}
|
|
|
|
|
2020-11-14 14:42:32 +00:00
|
|
|
void BMC::assignment(smt::SymbolicVariable& _symVar, smtutil::Expression const& _value)
|
|
|
|
{
|
|
|
|
auto oldVar = _symVar.currentValue();
|
|
|
|
auto newVar = _symVar.increaseIndex();
|
|
|
|
m_context.addAssertion(smtutil::Expression::ite(
|
|
|
|
currentPathConditions(),
|
|
|
|
newVar == _value,
|
|
|
|
newVar == oldVar
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2023-02-23 18:22:46 +00:00
|
|
|
bool BMC::isInsideLoop() const
|
|
|
|
{
|
|
|
|
return !m_loopCheckpoints.empty();
|
|
|
|
}
|