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>
|
|
|
|
|
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,
|
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,
|
2021-07-01 15:28:06 +00:00
|
|
|
ModelCheckerSettings const& _settings,
|
|
|
|
CharStreamProvider const& _charStreamProvider
|
2019-09-17 14:06:43 +00:00
|
|
|
):
|
2021-08-27 09:40:20 +00:00
|
|
|
SMTEncoder(_context, _settings, _errorReporter, _charStreamProvider),
|
|
|
|
m_interface(make_unique<smtutil::SMTPortfolio>(_smtlib2Responses, _smtCallback, _settings.solvers, _settings.timeout))
|
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
|
|
|
{
|
2021-05-19 15:35:19 +00:00
|
|
|
if (m_interface->solvers() == 0)
|
|
|
|
{
|
|
|
|
if (!m_noSolverWarning)
|
|
|
|
{
|
|
|
|
m_noSolverWarning = true;
|
2021-08-27 09:40:20 +00:00
|
|
|
m_errorReporter.warning(
|
2021-05-19 15:35:19 +00:00
|
|
|
7710_error,
|
|
|
|
SourceLocation(),
|
|
|
|
"BMC analysis was not possible since no SMT solver was found and enabled."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-27 14:45:41 +00:00
|
|
|
SMTEncoder::resetSourceAnalysis();
|
2019-07-08 09:21:23 +00:00
|
|
|
|
2021-08-27 14:45:41 +00:00
|
|
|
m_solvedTargets = move(_solvedTargets);
|
|
|
|
m_context.setSolver(m_interface.get());
|
|
|
|
m_context.reset();
|
|
|
|
m_context.setAssertionAccumulation(true);
|
|
|
|
m_variableUsage.setFunctionInlining(shouldInlineFunctionCall);
|
|
|
|
createFreeConstants(sourceDependencies(_source));
|
|
|
|
state().prepareForSourceUnit(_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
|
|
|
|
|
|
|
// 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
|
|
|
|
)
|
2019-06-25 10:46:17 +00:00
|
|
|
{
|
|
|
|
if (!m_noSolverWarning)
|
|
|
|
{
|
|
|
|
m_noSolverWarning = true;
|
2021-08-27 09:40:20 +00:00
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
8084_error,
|
2019-06-25 10:46:17 +00:00
|
|
|
SourceLocation(),
|
2021-05-19 15:35:19 +00:00
|
|
|
"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
|
|
|
|
" Install libz3.so." + to_string(Z3_MAJOR_VERSION) + "." + to_string(Z3_MINOR_VERSION) + " to enable Z3."
|
|
|
|
#endif
|
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)
|
2020-11-09 13:50:41 +00:00
|
|
|
return isTrustedExternalCall(&_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)
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
if (isRootFunction())
|
|
|
|
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();
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
m_context.pushSolver();
|
|
|
|
_op.condition().accept(*this);
|
|
|
|
|
|
|
|
if (isRootFunction())
|
|
|
|
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();
|
|
|
|
|
|
|
|
SMTEncoder::visit(_op);
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we consider the execution of two branches:
|
|
|
|
// Branch 1 assumes the loop condition to be true and executes the loop once,
|
|
|
|
// after resetting touched variables.
|
|
|
|
// Branch 2 assumes the loop condition to be false and skips the loop after
|
|
|
|
// visiting the condition (it might contain side-effects, they need to be considered)
|
|
|
|
// and does not erase knowledge.
|
|
|
|
// If the loop is a do-while, condition side-effects are lost since the body,
|
|
|
|
// executed once before the condition, might reassign variables.
|
|
|
|
// Variables touched by the loop are merged with Branch 2.
|
|
|
|
bool BMC::visit(WhileStatement const& _node)
|
|
|
|
{
|
|
|
|
auto indicesBeforeLoop = copyVariableIndices();
|
2021-03-29 12:44:33 +00:00
|
|
|
m_context.resetVariables(touchedVariables(_node));
|
2019-06-25 10:46:17 +00:00
|
|
|
decltype(indicesBeforeLoop) indicesAfterLoop;
|
|
|
|
if (_node.isDoWhile())
|
|
|
|
{
|
2020-11-14 14:42:32 +00:00
|
|
|
indicesAfterLoop = visitBranch(&_node.body()).first;
|
2019-06-25 10:46:17 +00:00
|
|
|
// TODO the assertions generated in the body should still be active in the condition
|
|
|
|
_node.condition().accept(*this);
|
|
|
|
if (isRootFunction())
|
|
|
|
addVerificationTarget(
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType::ConstantCondition,
|
2019-06-25 10:46:17 +00:00
|
|
|
expr(_node.condition()),
|
|
|
|
&_node.condition()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_node.condition().accept(*this);
|
|
|
|
if (isRootFunction())
|
|
|
|
addVerificationTarget(
|
2021-01-19 11:56:22 +00:00
|
|
|
VerificationTargetType::ConstantCondition,
|
2019-06-25 10:46:17 +00:00
|
|
|
expr(_node.condition()),
|
|
|
|
&_node.condition()
|
|
|
|
);
|
|
|
|
|
2020-11-14 14:42:32 +00:00
|
|
|
indicesAfterLoop = visitBranch(&_node.body(), expr(_node.condition())).first;
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We reset the execution to before the loop
|
|
|
|
// and visit the condition in case it's not a do-while.
|
|
|
|
// A do-while's body might have non-precise information
|
|
|
|
// in its first run about variables that are touched.
|
|
|
|
resetVariableIndices(indicesBeforeLoop);
|
|
|
|
if (!_node.isDoWhile())
|
|
|
|
_node.condition().accept(*this);
|
|
|
|
|
2021-03-29 12:44:33 +00:00
|
|
|
mergeVariables(expr(_node.condition()), indicesAfterLoop, copyVariableIndices());
|
2019-06-25 10:46:17 +00:00
|
|
|
|
|
|
|
m_loopExecutionHappened = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we consider the execution of two branches similar to WhileStatement.
|
|
|
|
bool BMC::visit(ForStatement const& _node)
|
|
|
|
{
|
|
|
|
if (_node.initializationExpression())
|
|
|
|
_node.initializationExpression()->accept(*this);
|
|
|
|
|
|
|
|
auto indicesBeforeLoop = copyVariableIndices();
|
|
|
|
|
|
|
|
// Do not reset the init expression part.
|
|
|
|
auto touchedVars = touchedVariables(_node.body());
|
|
|
|
if (_node.condition())
|
|
|
|
touchedVars += touchedVariables(*_node.condition());
|
|
|
|
if (_node.loopExpression())
|
|
|
|
touchedVars += touchedVariables(*_node.loopExpression());
|
|
|
|
|
|
|
|
m_context.resetVariables(touchedVars);
|
|
|
|
|
|
|
|
if (_node.condition())
|
|
|
|
{
|
|
|
|
_node.condition()->accept(*this);
|
|
|
|
if (isRootFunction())
|
|
|
|
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.pushSolver();
|
|
|
|
if (_node.condition())
|
|
|
|
m_context.addAssertion(expr(*_node.condition()));
|
|
|
|
_node.body().accept(*this);
|
|
|
|
if (_node.loopExpression())
|
|
|
|
_node.loopExpression()->accept(*this);
|
|
|
|
m_context.popSolver();
|
|
|
|
|
|
|
|
auto indicesAfterLoop = copyVariableIndices();
|
|
|
|
// We reset the execution to before the loop
|
|
|
|
// and visit the condition.
|
|
|
|
resetVariableIndices(indicesBeforeLoop);
|
|
|
|
if (_node.condition())
|
|
|
|
_node.condition()->accept(*this);
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
auto forCondition = _node.condition() ? expr(*_node.condition()) : smtutil::Expression(true);
|
2021-03-29 12:44:33 +00:00
|
|
|
mergeVariables(forCondition, indicesAfterLoop, copyVariableIndices());
|
2019-06-25 10:46:17 +00:00
|
|
|
|
|
|
|
m_loopExecutionHappened = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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, "");
|
|
|
|
if (isRootFunction())
|
|
|
|
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);
|
2020-11-26 16:14:58 +00:00
|
|
|
else if (isPublicGetter(_funCall.expression()))
|
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
m_errorReporter.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()
|
|
|
|
);
|
|
|
|
expressionNames.push_back(move(expressionName));
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {expressionsToEvaluate, expressionNames};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verification targets.
|
|
|
|
|
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(
|
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
|
|
|
"Underflow (resulting value less than " + formatNumberReadable(intType->minValue()) + ")",
|
|
|
|
"<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(
|
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
|
|
|
"Overflow (resulting value larger than " + formatNumberReadable(intType->maxValue()) + ")",
|
|
|
|
"<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(
|
|
|
|
_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
|
|
|
"Division by zero",
|
|
|
|
"<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(
|
|
|
|
_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
|
|
|
"Insufficient funds",
|
|
|
|
"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(
|
|
|
|
_target.constraints && !_target.value,
|
|
|
|
_target.callStack,
|
|
|
|
_target.modelExpressions,
|
|
|
|
_target.expression->location(),
|
|
|
|
4661_error,
|
|
|
|
7812_error,
|
|
|
|
"Assertion violation"
|
|
|
|
);
|
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
|
|
|
|
m_verificationTargets.emplace_back(move(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Solving.
|
|
|
|
|
|
|
|
void BMC::checkCondition(
|
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& _description,
|
|
|
|
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;
|
2020-05-12 00:29:54 +00:00
|
|
|
if (_callStack.size())
|
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 +=
|
|
|
|
"\nNote that some information is erased after the execution of loops.\n"
|
|
|
|
"You can re-introduce information using require().";
|
|
|
|
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;
|
2020-09-25 17:09:06 +00:00
|
|
|
message << "BMC: " << _description << " 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))
|
|
|
|
.append(move(secondaryLocation))
|
|
|
|
);
|
2019-06-25 10:46:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-19 12:14:46 +00:00
|
|
|
case smtutil::CheckResult::UNSATISFIABLE:
|
2019-06-25 10:46:17 +00:00
|
|
|
break;
|
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)
|
|
|
|
m_errorReporter.warning(_errorMightHappen, _location, "BMC: " + _description + " 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
|
|
|
|
{
|
|
|
|
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
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|