2017-07-06 09:05:05 +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
|
2017-07-06 09:05:05 +00:00
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
#include <libsolidity/formal/SMTEncoder.h>
|
2017-07-06 09:05:05 +00:00
|
|
|
|
2019-04-15 13:33:39 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2020-03-24 19:15:15 +00:00
|
|
|
#include <libsolidity/formal/SymbolicState.h>
|
2018-10-15 15:32:17 +00:00
|
|
|
#include <libsolidity/formal/SymbolicTypes.h>
|
2017-09-28 13:24:24 +00:00
|
|
|
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/SMTPortfolio.h>
|
2020-05-11 17:56:29 +00:00
|
|
|
#include <libsmtutil/Helpers.h>
|
2020-05-18 15:42:24 +00:00
|
|
|
|
2019-03-18 12:04:08 +00:00
|
|
|
#include <boost/range/adaptors.hpp>
|
2019-09-26 14:12:27 +00:00
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
2017-07-14 09:49:27 +00:00
|
|
|
|
2017-07-06 09:05:05 +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;
|
2017-07-06 09:05:05 +00:00
|
|
|
|
2019-07-02 10:06:52 +00:00
|
|
|
SMTEncoder::SMTEncoder(smt::EncodingContext& _context):
|
2019-04-17 13:55:46 +00:00
|
|
|
m_errorReporter(m_smtErrors),
|
2019-06-25 10:46:17 +00:00
|
|
|
m_context(_context)
|
2017-07-06 09:05:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::visit(ContractDefinition const& _contract)
|
2018-04-30 14:30:41 +00:00
|
|
|
{
|
2019-09-28 19:04:49 +00:00
|
|
|
solAssert(m_currentContract, "");
|
2019-08-02 16:36:26 +00:00
|
|
|
|
2019-09-28 19:04:49 +00:00
|
|
|
for (auto const& node: _contract.subNodes())
|
2019-09-26 14:12:27 +00:00
|
|
|
if (
|
|
|
|
!dynamic_pointer_cast<FunctionDefinition>(node) &&
|
|
|
|
!dynamic_pointer_cast<VariableDeclaration>(node)
|
|
|
|
)
|
2019-09-28 19:04:49 +00:00
|
|
|
node->accept(*this);
|
2019-07-09 14:39:30 +00:00
|
|
|
|
2019-09-28 19:04:49 +00:00
|
|
|
vector<FunctionDefinition const*> resolvedFunctions = _contract.definedFunctions();
|
|
|
|
for (auto const& base: _contract.annotation().linearizedBaseContracts)
|
2019-09-26 14:12:27 +00:00
|
|
|
{
|
|
|
|
// Look for all the constructor invocations bottom up.
|
|
|
|
if (auto const& constructor = base->constructor())
|
|
|
|
for (auto const& invocation: constructor->modifiers())
|
|
|
|
{
|
|
|
|
auto refDecl = invocation->name()->annotation().referencedDeclaration;
|
|
|
|
if (auto const& baseContract = dynamic_cast<ContractDefinition const*>(refDecl))
|
|
|
|
{
|
|
|
|
solAssert(!m_baseConstructorCalls.count(baseContract), "");
|
|
|
|
m_baseConstructorCalls[baseContract] = invocation.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for function overrides.
|
2019-09-28 19:04:49 +00:00
|
|
|
for (auto const& baseFunction: base->definedFunctions())
|
|
|
|
{
|
|
|
|
if (baseFunction->isConstructor())
|
|
|
|
continue;
|
|
|
|
bool overridden = false;
|
|
|
|
for (auto const& function: resolvedFunctions)
|
|
|
|
if (
|
|
|
|
function->name() == baseFunction->name() &&
|
2019-11-05 16:55:05 +00:00
|
|
|
function->kind() == baseFunction->kind() &&
|
2020-05-26 08:48:17 +00:00
|
|
|
FunctionType(*function).asExternallyCallableFunction(false)->
|
|
|
|
hasEqualParameterTypes(*FunctionType(*baseFunction).asExternallyCallableFunction(false))
|
2019-09-28 19:04:49 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
overridden = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!overridden)
|
|
|
|
resolvedFunctions.push_back(baseFunction);
|
|
|
|
}
|
2019-09-26 14:12:27 +00:00
|
|
|
}
|
2019-09-28 19:04:49 +00:00
|
|
|
|
2019-09-26 14:12:27 +00:00
|
|
|
// Functions are visited first since they might be used
|
|
|
|
// for state variable initialization which is part of
|
|
|
|
// the constructor.
|
|
|
|
// Constructors are visited as part of the constructor
|
|
|
|
// hierarchy inlining.
|
2019-09-28 19:04:49 +00:00
|
|
|
for (auto const& function: resolvedFunctions)
|
2019-09-26 14:12:27 +00:00
|
|
|
if (!function->isConstructor())
|
|
|
|
function->accept(*this);
|
|
|
|
|
|
|
|
// Constructors need to be handled by the engines separately.
|
2019-09-28 19:04:49 +00:00
|
|
|
|
|
|
|
return false;
|
2018-04-30 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 16:36:26 +00:00
|
|
|
void SMTEncoder::endVisit(ContractDefinition const& _contract)
|
2018-04-30 14:30:41 +00:00
|
|
|
{
|
2019-05-10 14:28:29 +00:00
|
|
|
m_context.resetAllVariables();
|
2019-08-02 16:36:26 +00:00
|
|
|
|
2019-09-26 14:12:27 +00:00
|
|
|
m_baseConstructorCalls.clear();
|
|
|
|
|
2019-08-02 16:36:26 +00:00
|
|
|
solAssert(m_currentContract == &_contract, "");
|
|
|
|
m_currentContract = nullptr;
|
2020-04-14 21:25:56 +00:00
|
|
|
|
|
|
|
if (m_callStack.empty())
|
|
|
|
m_context.popSolver();
|
2018-04-30 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(VariableDeclaration const& _varDecl)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-09-26 14:12:27 +00:00
|
|
|
// State variables are handled by the constructor.
|
|
|
|
if (_varDecl.isLocalVariable() &&_varDecl.value())
|
2019-06-24 15:58:56 +00:00
|
|
|
assignment(_varDecl, *_varDecl.value());
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::visit(ModifierDefinition const&)
|
2019-03-11 20:06:28 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::visit(FunctionDefinition const& _function)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-05-06 18:03:11 +00:00
|
|
|
m_modifierDepthStack.push_back(-1);
|
2019-09-26 14:12:27 +00:00
|
|
|
|
2019-05-06 18:03:11 +00:00
|
|
|
if (_function.isConstructor())
|
2019-09-26 14:12:27 +00:00
|
|
|
inlineConstructorHierarchy(dynamic_cast<ContractDefinition const&>(*_function.scope()));
|
|
|
|
|
2020-09-02 08:45:47 +00:00
|
|
|
initializeLocalVariables(_function);
|
2019-09-26 14:12:27 +00:00
|
|
|
|
|
|
|
_function.parameterList().accept(*this);
|
|
|
|
if (_function.returnParameterList())
|
|
|
|
_function.returnParameterList()->accept(*this);
|
|
|
|
|
|
|
|
visitFunctionOrModifier();
|
|
|
|
|
2019-03-11 20:06:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::visitFunctionOrModifier()
|
2019-03-11 20:06:28 +00:00
|
|
|
{
|
2019-05-06 18:03:11 +00:00
|
|
|
solAssert(!m_callStack.empty(), "");
|
2019-03-11 20:06:28 +00:00
|
|
|
solAssert(!m_modifierDepthStack.empty(), "");
|
|
|
|
|
|
|
|
++m_modifierDepthStack.back();
|
2019-05-06 18:03:11 +00:00
|
|
|
FunctionDefinition const& function = dynamic_cast<FunctionDefinition const&>(*m_callStack.back().first);
|
2019-03-11 20:06:28 +00:00
|
|
|
|
2020-06-02 13:42:46 +00:00
|
|
|
if (m_modifierDepthStack.back() == static_cast<int>(function.modifiers().size()))
|
2019-03-11 20:06:28 +00:00
|
|
|
{
|
|
|
|
if (function.isImplemented())
|
|
|
|
function.body().accept(*this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-02 13:42:46 +00:00
|
|
|
solAssert(m_modifierDepthStack.back() < static_cast<int>(function.modifiers().size()), "");
|
|
|
|
ASTPointer<ModifierInvocation> const& modifierInvocation =
|
|
|
|
function.modifiers()[static_cast<size_t>(m_modifierDepthStack.back())];
|
2019-03-11 20:06:28 +00:00
|
|
|
solAssert(modifierInvocation, "");
|
2019-09-26 14:12:27 +00:00
|
|
|
auto refDecl = modifierInvocation->name()->annotation().referencedDeclaration;
|
|
|
|
if (dynamic_cast<ContractDefinition const*>(refDecl))
|
|
|
|
visitFunctionOrModifier();
|
|
|
|
else if (auto modifierDef = dynamic_cast<ModifierDefinition const*>(refDecl))
|
|
|
|
inlineModifierInvocation(modifierInvocation.get(), modifierDef);
|
|
|
|
else
|
|
|
|
solAssert(false, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
--m_modifierDepthStack.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTEncoder::inlineModifierInvocation(ModifierInvocation const* _invocation, CallableDeclaration const* _definition)
|
|
|
|
{
|
|
|
|
solAssert(_invocation, "");
|
|
|
|
_invocation->accept(*this);
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
vector<smtutil::Expression> args;
|
2019-09-26 14:12:27 +00:00
|
|
|
if (auto const* arguments = _invocation->arguments())
|
|
|
|
{
|
|
|
|
auto const& modifierParams = _definition->parameters();
|
|
|
|
solAssert(modifierParams.size() == arguments->size(), "");
|
|
|
|
for (unsigned i = 0; i < arguments->size(); ++i)
|
|
|
|
args.push_back(expr(*arguments->at(i), modifierParams.at(i)->type()));
|
|
|
|
}
|
|
|
|
|
|
|
|
initializeFunctionCallParameters(*_definition, args);
|
|
|
|
|
|
|
|
pushCallStack({_definition, _invocation});
|
|
|
|
if (auto modifier = dynamic_cast<ModifierDefinition const*>(_definition))
|
|
|
|
{
|
2020-04-15 10:42:15 +00:00
|
|
|
if (modifier->isImplemented())
|
|
|
|
modifier->body().accept(*this);
|
2019-09-26 14:12:27 +00:00
|
|
|
popCallStack();
|
|
|
|
}
|
|
|
|
else if (auto function = dynamic_cast<FunctionDefinition const*>(_definition))
|
|
|
|
{
|
|
|
|
if (function->isImplemented())
|
|
|
|
function->accept(*this);
|
|
|
|
// Functions are popped from the callstack in endVisit(FunctionDefinition)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTEncoder::inlineConstructorHierarchy(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
auto const& hierarchy = m_currentContract->annotation().linearizedBaseContracts;
|
|
|
|
auto it = find(begin(hierarchy), end(hierarchy), &_contract);
|
|
|
|
solAssert(it != end(hierarchy), "");
|
|
|
|
|
|
|
|
auto nextBase = it + 1;
|
|
|
|
// Initialize the base contracts here as long as their constructors are implicit,
|
|
|
|
// stop when the first explicit constructor is found.
|
|
|
|
while (nextBase != end(hierarchy))
|
|
|
|
{
|
|
|
|
if (auto baseConstructor = (*nextBase)->constructor())
|
2019-11-13 11:21:37 +00:00
|
|
|
{
|
2019-09-26 14:12:27 +00:00
|
|
|
createLocalVariables(*baseConstructor);
|
|
|
|
// If any subcontract explicitly called baseConstructor, use those arguments.
|
|
|
|
if (m_baseConstructorCalls.count(*nextBase))
|
|
|
|
inlineModifierInvocation(m_baseConstructorCalls.at(*nextBase), baseConstructor);
|
|
|
|
else if (baseConstructor->isImplemented())
|
|
|
|
{
|
|
|
|
// The first constructor found is handled like a function
|
|
|
|
// and its pushed into the callstack there.
|
|
|
|
// This if avoids duplication in the callstack.
|
|
|
|
if (!m_callStack.empty())
|
|
|
|
pushCallStack({baseConstructor, nullptr});
|
|
|
|
baseConstructor->accept(*this);
|
|
|
|
// popped by endVisit(FunctionDefinition)
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
initializeStateVariables(**nextBase);
|
|
|
|
++nextBase;
|
2019-11-13 11:21:37 +00:00
|
|
|
}
|
2019-03-11 20:06:28 +00:00
|
|
|
}
|
2018-10-10 12:31:49 +00:00
|
|
|
|
2019-09-26 14:12:27 +00:00
|
|
|
initializeStateVariables(_contract);
|
2019-03-11 20:06:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::visit(PlaceholderStatement const&)
|
2019-03-11 20:06:28 +00:00
|
|
|
{
|
2019-05-06 18:03:11 +00:00
|
|
|
solAssert(!m_callStack.empty(), "");
|
|
|
|
auto lastCall = popCallStack();
|
2019-03-11 20:06:28 +00:00
|
|
|
visitFunctionOrModifier();
|
|
|
|
pushCallStack(lastCall);
|
2017-07-11 11:26:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(FunctionDefinition const&)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-06-24 15:37:03 +00:00
|
|
|
popCallStack();
|
2019-05-06 18:03:11 +00:00
|
|
|
solAssert(m_modifierDepthStack.back() == -1, "");
|
|
|
|
m_modifierDepthStack.pop_back();
|
|
|
|
if (m_callStack.empty())
|
2019-06-24 15:37:03 +00:00
|
|
|
m_context.popSolver();
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::visit(InlineAssembly const& _inlineAsm)
|
2019-04-05 14:41:05 +00:00
|
|
|
{
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
7737_error,
|
2019-04-05 14:41:05 +00:00
|
|
|
_inlineAsm.location(),
|
|
|
|
"Assertion checker does not support inline assembly."
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:00:40 +00:00
|
|
|
bool SMTEncoder::visit(TryCatchClause const& _clause)
|
|
|
|
{
|
|
|
|
if (auto params = _clause.parameters())
|
|
|
|
for (auto const& var: params->parameters())
|
|
|
|
createVariable(*var);
|
|
|
|
|
|
|
|
m_errorReporter.warning(
|
|
|
|
7645_error,
|
|
|
|
_clause.location(),
|
|
|
|
"Assertion checker does not support try/catch clauses."
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::visit(IfStatement const& _node)
|
2017-07-14 09:49:27 +00:00
|
|
|
{
|
|
|
|
_node.condition().accept(*this);
|
|
|
|
|
2019-03-22 16:05:58 +00:00
|
|
|
auto indicesEndTrue = visitBranch(&_node.trueStatement(), expr(_node.condition()));
|
2019-04-01 09:10:28 +00:00
|
|
|
auto touchedVars = touchedVariables(_node.trueStatement());
|
2018-10-17 09:32:01 +00:00
|
|
|
decltype(indicesEndTrue) indicesEndFalse;
|
2017-07-14 09:49:27 +00:00
|
|
|
if (_node.falseStatement())
|
2017-09-28 13:24:24 +00:00
|
|
|
{
|
2019-03-22 16:05:58 +00:00
|
|
|
indicesEndFalse = visitBranch(_node.falseStatement(), !expr(_node.condition()));
|
2019-04-01 09:10:28 +00:00
|
|
|
touchedVars += touchedVariables(*_node.falseStatement());
|
2017-09-28 13:24:24 +00:00
|
|
|
}
|
2018-01-17 20:02:23 +00:00
|
|
|
else
|
2018-10-17 09:32:01 +00:00
|
|
|
indicesEndFalse = copyVariableIndices();
|
2017-07-14 09:49:27 +00:00
|
|
|
|
2019-04-01 09:10:28 +00:00
|
|
|
mergeVariables(touchedVars, expr(_node.condition()), indicesEndTrue, indicesEndFalse);
|
2017-07-14 09:49:27 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(VariableDeclarationStatement const& _varDecl)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
|
|
|
if (_varDecl.declarations().size() != 1)
|
2019-04-30 14:02:01 +00:00
|
|
|
{
|
|
|
|
if (auto init = _varDecl.initialValue())
|
|
|
|
{
|
2019-05-13 14:53:38 +00:00
|
|
|
auto symbTuple = dynamic_pointer_cast<smt::SymbolicTupleVariable>(m_context.expression(*init));
|
|
|
|
solAssert(symbTuple, "");
|
2020-04-03 13:10:16 +00:00
|
|
|
auto const& symbComponents = symbTuple->components();
|
|
|
|
|
|
|
|
auto tupleType = dynamic_cast<TupleType const*>(init->annotation().type);
|
|
|
|
solAssert(tupleType, "");
|
|
|
|
solAssert(tupleType->components().size() == symbTuple->components().size(), "");
|
|
|
|
auto const& components = tupleType->components();
|
|
|
|
|
2019-05-13 14:53:38 +00:00
|
|
|
auto const& declarations = _varDecl.declarations();
|
2020-04-03 13:10:16 +00:00
|
|
|
solAssert(symbComponents.size() == declarations.size(), "");
|
2019-11-15 13:48:11 +00:00
|
|
|
for (unsigned i = 0; i < declarations.size(); ++i)
|
|
|
|
if (
|
|
|
|
components.at(i) &&
|
|
|
|
declarations.at(i) &&
|
|
|
|
m_context.knownVariable(*declarations.at(i))
|
|
|
|
)
|
2020-04-03 13:10:16 +00:00
|
|
|
assignment(*declarations.at(i), symbTuple->component(i, components.at(i), declarations.at(i)->type()));
|
2019-04-30 14:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-10 14:28:29 +00:00
|
|
|
else if (m_context.knownVariable(*_varDecl.declarations().front()))
|
2017-07-14 09:49:27 +00:00
|
|
|
{
|
|
|
|
if (_varDecl.initialValue())
|
2019-06-24 15:58:56 +00:00
|
|
|
assignment(*_varDecl.declarations().front(), *_varDecl.initialValue());
|
2017-07-14 09:49:27 +00:00
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
7186_error,
|
2017-07-11 11:26:43 +00:00
|
|
|
_varDecl.location(),
|
|
|
|
"Assertion checker does not yet implement such variable declarations."
|
|
|
|
);
|
2019-04-30 14:02:01 +00:00
|
|
|
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(Assignment const& _assignment)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-07-01 14:25:33 +00:00
|
|
|
createExpr(_assignment);
|
|
|
|
|
2019-03-28 11:43:20 +00:00
|
|
|
Token op = _assignment.assignmentOperator();
|
2020-09-22 09:15:56 +00:00
|
|
|
solAssert(TokenTraits::isAssignmentOp(op), "");
|
2019-08-02 17:46:06 +00:00
|
|
|
|
2020-09-22 09:15:56 +00:00
|
|
|
if (!smt::isSupportedType(*_assignment.annotation().type))
|
2019-05-08 12:05:23 +00:00
|
|
|
{
|
|
|
|
// Give it a new index anyway to keep the SSA scheme sound.
|
2020-05-28 09:26:32 +00:00
|
|
|
|
|
|
|
Expression const* base = &_assignment.leftHandSide();
|
|
|
|
if (auto const* indexAccess = dynamic_cast<IndexAccess const*>(base))
|
|
|
|
base = leftmostBase(*indexAccess);
|
|
|
|
|
|
|
|
if (auto varDecl = identifierToVariable(*base))
|
2019-05-10 14:28:29 +00:00
|
|
|
m_context.newValue(*varDecl);
|
2019-05-08 12:05:23 +00:00
|
|
|
}
|
2019-04-29 09:39:24 +00:00
|
|
|
else
|
2018-11-09 16:06:30 +00:00
|
|
|
{
|
2020-05-27 17:24:48 +00:00
|
|
|
if (dynamic_cast<TupleType const*>(_assignment.rightHandSide().annotation().type))
|
|
|
|
tupleAssignment(_assignment.leftHandSide(), _assignment.rightHandSide());
|
2019-04-29 11:02:27 +00:00
|
|
|
else
|
|
|
|
{
|
2020-05-27 17:24:48 +00:00
|
|
|
auto const& type = _assignment.annotation().type;
|
2020-09-22 09:15:56 +00:00
|
|
|
auto rightHandSide = op == Token::Assign ?
|
|
|
|
expr(_assignment.rightHandSide(), type) :
|
|
|
|
compoundAssignment(_assignment);
|
2019-04-29 11:02:27 +00:00
|
|
|
defineExpr(_assignment, rightHandSide);
|
2020-05-27 17:24:48 +00:00
|
|
|
assignment(
|
|
|
|
_assignment.leftHandSide(),
|
|
|
|
expr(_assignment, type),
|
2020-08-25 17:28:31 +00:00
|
|
|
type
|
2020-05-27 17:24:48 +00:00
|
|
|
);
|
2019-04-29 11:02:27 +00:00
|
|
|
}
|
2019-04-29 09:39:24 +00:00
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(TupleExpression const& _tuple)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-07-01 14:25:33 +00:00
|
|
|
createExpr(_tuple);
|
|
|
|
|
2019-04-26 12:57:29 +00:00
|
|
|
if (_tuple.isInlineArray())
|
2017-07-11 11:26:43 +00:00
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
2177_error,
|
2017-07-11 11:26:43 +00:00
|
|
|
_tuple.location(),
|
2019-04-26 12:57:29 +00:00
|
|
|
"Assertion checker does not yet implement inline arrays."
|
2017-07-11 11:26:43 +00:00
|
|
|
);
|
2020-08-31 14:45:52 +00:00
|
|
|
else if (_tuple.components().size() == 1)
|
|
|
|
defineExpr(_tuple, expr(*_tuple.components().front()));
|
|
|
|
else
|
2019-04-26 12:57:29 +00:00
|
|
|
{
|
2020-08-31 14:45:52 +00:00
|
|
|
solAssert(_tuple.annotation().type->category() == Type::Category::Tuple, "");
|
2019-05-13 14:53:38 +00:00
|
|
|
auto const& symbTuple = dynamic_pointer_cast<smt::SymbolicTupleVariable>(m_context.expression(_tuple));
|
2019-04-26 12:57:29 +00:00
|
|
|
solAssert(symbTuple, "");
|
2019-11-15 13:48:11 +00:00
|
|
|
auto const& symbComponents = symbTuple->components();
|
2020-07-15 16:56:28 +00:00
|
|
|
auto const* tuple = dynamic_cast<TupleExpression const*>(innermostTuple(_tuple));
|
|
|
|
solAssert(tuple, "");
|
|
|
|
auto const& tupleComponents = tuple->components();
|
|
|
|
solAssert(symbComponents.size() == tupleComponents.size(), "");
|
2019-11-15 13:48:11 +00:00
|
|
|
for (unsigned i = 0; i < symbComponents.size(); ++i)
|
2019-07-26 14:28:19 +00:00
|
|
|
{
|
2020-07-15 16:56:28 +00:00
|
|
|
auto tComponent = tupleComponents.at(i);
|
2020-04-03 13:10:16 +00:00
|
|
|
if (tComponent)
|
2019-11-15 13:48:11 +00:00
|
|
|
{
|
|
|
|
if (auto varDecl = identifierToVariable(*tComponent))
|
2020-04-03 13:10:16 +00:00
|
|
|
m_context.addAssertion(symbTuple->component(i) == currentValue(*varDecl));
|
2019-11-15 13:48:11 +00:00
|
|
|
else
|
2019-07-26 14:28:19 +00:00
|
|
|
{
|
2020-03-02 21:10:15 +00:00
|
|
|
if (!m_context.knownExpression(*tComponent))
|
|
|
|
createExpr(*tComponent);
|
2020-04-03 13:10:16 +00:00
|
|
|
m_context.addAssertion(symbTuple->component(i) == expr(*tComponent));
|
2019-07-26 14:28:19 +00:00
|
|
|
}
|
2019-11-15 13:48:11 +00:00
|
|
|
}
|
2019-07-26 14:28:19 +00:00
|
|
|
}
|
2019-04-26 12:57:29 +00:00
|
|
|
}
|
2017-10-05 13:23:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(UnaryOperation const& _op)
|
2017-10-05 13:23:25 +00:00
|
|
|
{
|
2020-09-01 09:35:25 +00:00
|
|
|
/// We need to shortcut here due to potentially unknown
|
|
|
|
/// rational number sizes.
|
2019-03-11 10:56:08 +00:00
|
|
|
if (_op.annotation().type->category() == Type::Category::RationalNumber)
|
|
|
|
return;
|
|
|
|
|
2020-09-01 09:35:25 +00:00
|
|
|
if (TokenTraits::isBitOp(_op.getOperator()))
|
|
|
|
return bitwiseNotOperation(_op);
|
|
|
|
|
2019-07-01 14:25:33 +00:00
|
|
|
createExpr(_op);
|
|
|
|
|
2020-08-31 10:59:11 +00:00
|
|
|
auto const* subExpr = innermostTuple(_op.subExpression());
|
2020-08-27 13:16:02 +00:00
|
|
|
auto type = _op.annotation().type;
|
2017-10-05 13:23:25 +00:00
|
|
|
switch (_op.getOperator())
|
|
|
|
{
|
|
|
|
case Token::Not: // !
|
|
|
|
{
|
2020-08-27 13:16:02 +00:00
|
|
|
solAssert(smt::isBool(*type), "");
|
2020-08-31 10:59:11 +00:00
|
|
|
defineExpr(_op, !expr(*subExpr));
|
2017-10-05 13:23:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::Inc: // ++ (pre- or postfix)
|
|
|
|
case Token::Dec: // -- (pre- or postfix)
|
|
|
|
{
|
2020-08-27 13:16:02 +00:00
|
|
|
solAssert(smt::isInteger(*type) || smt::isFixedPoint(*type), "");
|
2020-08-31 10:59:11 +00:00
|
|
|
solAssert(subExpr->annotation().willBeWrittenTo, "");
|
|
|
|
if (auto identifier = dynamic_cast<Identifier const*>(subExpr))
|
2017-10-05 13:23:25 +00:00
|
|
|
{
|
2019-04-29 09:44:32 +00:00
|
|
|
auto decl = identifierToVariable(*identifier);
|
2019-04-02 10:59:19 +00:00
|
|
|
solAssert(decl, "");
|
|
|
|
auto innerValue = currentValue(*decl);
|
|
|
|
auto newValue = _op.getOperator() == Token::Inc ? innerValue + 1 : innerValue - 1;
|
|
|
|
defineExpr(_op, _op.isPrefixOperation() ? newValue : innerValue);
|
2019-06-24 15:58:56 +00:00
|
|
|
assignment(*decl, newValue);
|
2019-04-02 10:59:19 +00:00
|
|
|
}
|
2020-08-27 13:16:02 +00:00
|
|
|
else if (
|
|
|
|
dynamic_cast<IndexAccess const*>(&_op.subExpression()) ||
|
|
|
|
dynamic_cast<MemberAccess const*>(&_op.subExpression())
|
|
|
|
)
|
2019-04-02 10:59:19 +00:00
|
|
|
{
|
2020-08-31 10:59:11 +00:00
|
|
|
auto innerValue = expr(*subExpr);
|
2019-04-02 10:59:19 +00:00
|
|
|
auto newValue = _op.getOperator() == Token::Inc ? innerValue + 1 : innerValue - 1;
|
|
|
|
defineExpr(_op, _op.isPrefixOperation() ? newValue : innerValue);
|
2020-08-25 14:58:09 +00:00
|
|
|
indexOrMemberAssignment(_op.subExpression(), newValue);
|
2017-10-05 13:23:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
1950_error,
|
2017-10-05 13:23:25 +00:00
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement such increments / decrements."
|
|
|
|
);
|
2019-06-24 15:58:56 +00:00
|
|
|
|
2017-10-05 13:23:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::Sub: // -
|
|
|
|
{
|
2020-08-31 10:59:11 +00:00
|
|
|
defineExpr(_op, 0 - expr(*subExpr));
|
2017-10-05 13:23:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-05-06 16:24:15 +00:00
|
|
|
case Token::Delete:
|
|
|
|
{
|
2020-08-31 10:59:11 +00:00
|
|
|
if (auto decl = identifierToVariable(*subExpr))
|
2019-05-06 16:24:15 +00:00
|
|
|
{
|
2019-05-10 14:28:29 +00:00
|
|
|
m_context.newValue(*decl);
|
|
|
|
m_context.setZeroValue(*decl);
|
2019-05-06 16:24:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-31 10:59:11 +00:00
|
|
|
solAssert(m_context.knownExpression(*subExpr), "");
|
|
|
|
auto const& symbVar = m_context.expression(*subExpr);
|
2019-05-06 16:24:15 +00:00
|
|
|
symbVar->increaseIndex();
|
2019-05-10 14:28:29 +00:00
|
|
|
m_context.setZeroValue(*symbVar);
|
2020-08-25 14:58:09 +00:00
|
|
|
if (
|
|
|
|
dynamic_cast<IndexAccess const*>(&_op.subExpression()) ||
|
|
|
|
dynamic_cast<MemberAccess const*>(&_op.subExpression())
|
|
|
|
)
|
|
|
|
indexOrMemberAssignment(_op.subExpression(), symbVar->currentValue());
|
2019-05-06 16:24:15 +00:00
|
|
|
else
|
2020-08-25 17:28:31 +00:00
|
|
|
solAssert(false, "");
|
2019-05-06 16:24:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-10-05 13:23:25 +00:00
|
|
|
default:
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
3682_error,
|
2017-10-05 13:23:25 +00:00
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement this operator."
|
|
|
|
);
|
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::visit(UnaryOperation const& _op)
|
2019-03-11 10:56:08 +00:00
|
|
|
{
|
|
|
|
return !shortcutRationalNumber(_op);
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::visit(BinaryOperation const& _op)
|
2019-03-11 10:56:08 +00:00
|
|
|
{
|
2019-03-22 16:05:58 +00:00
|
|
|
if (shortcutRationalNumber(_op))
|
|
|
|
return false;
|
|
|
|
if (TokenTraits::isBooleanOp(_op.getOperator()))
|
|
|
|
{
|
|
|
|
booleanOperation(_op);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-03-11 10:56:08 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(BinaryOperation const& _op)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-03-11 10:56:08 +00:00
|
|
|
if (_op.annotation().type->category() == Type::Category::RationalNumber)
|
|
|
|
return;
|
2019-03-22 16:05:58 +00:00
|
|
|
if (TokenTraits::isBooleanOp(_op.getOperator()))
|
|
|
|
return;
|
2019-03-11 10:56:08 +00:00
|
|
|
|
2019-07-01 14:25:33 +00:00
|
|
|
createExpr(_op);
|
|
|
|
|
2018-10-22 14:48:21 +00:00
|
|
|
if (TokenTraits::isArithmeticOp(_op.getOperator()))
|
2017-07-11 11:26:43 +00:00
|
|
|
arithmeticOperation(_op);
|
2018-10-22 14:48:21 +00:00
|
|
|
else if (TokenTraits::isCompareOp(_op.getOperator()))
|
2017-07-11 11:26:43 +00:00
|
|
|
compareOperation(_op);
|
2020-09-04 14:55:45 +00:00
|
|
|
else if (TokenTraits::isBitOp(_op.getOperator()) || TokenTraits::isShiftOp(_op.getOperator()))
|
2020-05-13 11:08:48 +00:00
|
|
|
bitwiseOperation(_op);
|
2017-07-11 11:26:43 +00:00
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
3876_error,
|
2017-07-11 11:26:43 +00:00
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement this operator."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-18 17:25:36 +00:00
|
|
|
bool SMTEncoder::visit(Conditional const& _op)
|
|
|
|
{
|
|
|
|
_op.condition().accept(*this);
|
|
|
|
|
|
|
|
auto indicesEndTrue = visitBranch(&_op.trueExpression(), expr(_op.condition()));
|
|
|
|
auto touchedVars = touchedVariables(_op.trueExpression());
|
|
|
|
|
|
|
|
auto indicesEndFalse = visitBranch(&_op.falseExpression(), !expr(_op.condition()));
|
|
|
|
touchedVars += touchedVariables(_op.falseExpression());
|
|
|
|
|
|
|
|
mergeVariables(touchedVars, expr(_op.condition()), indicesEndTrue, indicesEndFalse);
|
|
|
|
|
|
|
|
defineExpr(_op, smtutil::Expression::ite(
|
|
|
|
expr(_op.condition()),
|
|
|
|
expr(_op.trueExpression()),
|
|
|
|
expr(_op.falseExpression())
|
|
|
|
));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(FunctionCall const& _funCall)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2020-04-08 17:38:30 +00:00
|
|
|
auto functionCallKind = *_funCall.annotation().kind;
|
|
|
|
|
2019-05-14 21:10:56 +00:00
|
|
|
createExpr(_funCall);
|
2020-04-08 17:38:30 +00:00
|
|
|
if (functionCallKind == FunctionCallKind::StructConstructorCall)
|
2017-10-04 11:18:40 +00:00
|
|
|
{
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
4639_error,
|
2017-10-04 11:18:40 +00:00
|
|
|
_funCall.location(),
|
|
|
|
"Assertion checker does not yet implement this expression."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-08 17:38:30 +00:00
|
|
|
if (functionCallKind == FunctionCallKind::TypeConversion)
|
2018-12-20 12:20:07 +00:00
|
|
|
{
|
|
|
|
visitTypeConversion(_funCall);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-11 11:26:43 +00:00
|
|
|
FunctionType const& funType = dynamic_cast<FunctionType const&>(*_funCall.expression().annotation().type);
|
|
|
|
|
|
|
|
std::vector<ASTPointer<Expression const>> const args = _funCall.arguments();
|
2018-12-10 16:23:36 +00:00
|
|
|
switch (funType.kind())
|
|
|
|
{
|
|
|
|
case FunctionType::Kind::Assert:
|
2018-10-10 12:31:49 +00:00
|
|
|
visitAssert(_funCall);
|
2018-12-10 16:23:36 +00:00
|
|
|
break;
|
|
|
|
case FunctionType::Kind::Require:
|
2018-10-10 12:31:49 +00:00
|
|
|
visitRequire(_funCall);
|
2018-12-10 16:23:36 +00:00
|
|
|
break;
|
2020-09-11 19:49:04 +00:00
|
|
|
case FunctionType::Kind::Revert:
|
|
|
|
// Revert is a special case of require and equals to `require(false)`
|
|
|
|
addPathImpliedExpression(smtutil::Expression(false));
|
|
|
|
break;
|
2018-12-10 16:23:36 +00:00
|
|
|
case FunctionType::Kind::GasLeft:
|
2018-10-18 13:03:52 +00:00
|
|
|
visitGasLeft(_funCall);
|
2018-12-10 16:23:36 +00:00
|
|
|
break;
|
|
|
|
case FunctionType::Kind::Internal:
|
2018-12-20 17:11:20 +00:00
|
|
|
case FunctionType::Kind::External:
|
2019-04-11 16:14:48 +00:00
|
|
|
case FunctionType::Kind::DelegateCall:
|
|
|
|
case FunctionType::Kind::BareCall:
|
|
|
|
case FunctionType::Kind::BareCallCode:
|
|
|
|
case FunctionType::Kind::BareDelegateCall:
|
|
|
|
case FunctionType::Kind::BareStaticCall:
|
|
|
|
case FunctionType::Kind::Creation:
|
2018-12-20 17:11:20 +00:00
|
|
|
break;
|
2018-12-10 16:23:36 +00:00
|
|
|
case FunctionType::Kind::KECCAK256:
|
|
|
|
case FunctionType::Kind::ECRecover:
|
|
|
|
case FunctionType::Kind::SHA256:
|
|
|
|
case FunctionType::Kind::RIPEMD160:
|
|
|
|
case FunctionType::Kind::BlockHash:
|
|
|
|
case FunctionType::Kind::AddMod:
|
|
|
|
case FunctionType::Kind::MulMod:
|
|
|
|
break;
|
2019-04-11 16:14:48 +00:00
|
|
|
case FunctionType::Kind::Send:
|
|
|
|
case FunctionType::Kind::Transfer:
|
|
|
|
{
|
|
|
|
auto const& memberAccess = dynamic_cast<MemberAccess const&>(_funCall.expression());
|
|
|
|
auto const& address = memberAccess.expression();
|
2019-05-09 11:17:00 +00:00
|
|
|
auto const& value = args.front();
|
2019-04-11 16:14:48 +00:00
|
|
|
solAssert(value, "");
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression thisBalance = m_context.state().balance();
|
2019-07-03 14:05:56 +00:00
|
|
|
setSymbolicUnknownValue(thisBalance, TypeProvider::uint256(), m_context);
|
2019-06-24 15:37:03 +00:00
|
|
|
|
2020-03-24 19:15:15 +00:00
|
|
|
m_context.state().transfer(m_context.state().thisAddress(), expr(address), expr(*value));
|
2019-04-11 16:14:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-11 22:39:00 +00:00
|
|
|
case FunctionType::Kind::ArrayPush:
|
|
|
|
arrayPush(_funCall);
|
|
|
|
break;
|
|
|
|
case FunctionType::Kind::ArrayPop:
|
|
|
|
arrayPop(_funCall);
|
|
|
|
break;
|
2020-09-12 11:22:03 +00:00
|
|
|
case FunctionType::Kind::Log0:
|
|
|
|
case FunctionType::Kind::Log1:
|
|
|
|
case FunctionType::Kind::Log2:
|
|
|
|
case FunctionType::Kind::Log3:
|
|
|
|
case FunctionType::Kind::Log4:
|
|
|
|
case FunctionType::Kind::Event:
|
|
|
|
// These can be safely ignored.
|
|
|
|
break;
|
2020-09-25 12:00:32 +00:00
|
|
|
case FunctionType::Kind::ObjectCreation:
|
|
|
|
visitObjectCreation(_funCall);
|
|
|
|
return;
|
2018-12-10 16:23:36 +00:00
|
|
|
default:
|
2018-10-10 12:31:49 +00:00
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
4588_error,
|
2018-10-10 12:31:49 +00:00
|
|
|
_funCall.location(),
|
|
|
|
"Assertion checker does not yet implement this type of function call."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 21:34:33 +00:00
|
|
|
bool SMTEncoder::visit(ModifierInvocation const& _node)
|
|
|
|
{
|
|
|
|
if (auto const* args = _node.arguments())
|
|
|
|
for (auto const& arg: *args)
|
|
|
|
if (arg)
|
|
|
|
arg->accept(*this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-28 19:04:49 +00:00
|
|
|
void SMTEncoder::initContract(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
solAssert(m_currentContract == nullptr, "");
|
|
|
|
m_currentContract = &_contract;
|
|
|
|
|
2019-09-26 14:12:27 +00:00
|
|
|
m_context.reset();
|
|
|
|
m_context.pushSolver();
|
|
|
|
createStateVariables(_contract);
|
|
|
|
clearIndices(m_currentContract, nullptr);
|
2019-09-28 19:04:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::initFunction(FunctionDefinition const& _function)
|
|
|
|
{
|
|
|
|
solAssert(m_callStack.empty(), "");
|
2019-09-26 14:12:27 +00:00
|
|
|
solAssert(m_currentContract, "");
|
2019-06-25 10:46:17 +00:00
|
|
|
m_context.pushSolver();
|
|
|
|
m_pathConditions.clear();
|
|
|
|
pushCallStack({&_function, nullptr});
|
|
|
|
m_uninterpretedTerms.clear();
|
2019-09-26 14:12:27 +00:00
|
|
|
createStateVariables(*m_currentContract);
|
|
|
|
createLocalVariables(_function);
|
2019-06-25 10:46:17 +00:00
|
|
|
m_arrayAssignmentHappened = false;
|
2019-09-26 14:12:27 +00:00
|
|
|
clearIndices(m_currentContract, &_function);
|
2019-06-25 10:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SMTEncoder::visitAssert(FunctionCall const& _funCall)
|
2018-10-10 12:31:49 +00:00
|
|
|
{
|
|
|
|
auto const& args = _funCall.arguments();
|
|
|
|
solAssert(args.size() == 1, "");
|
2019-07-01 14:11:29 +00:00
|
|
|
solAssert(args.front()->annotation().type->category() == Type::Category::Bool, "");
|
2018-10-10 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::visitRequire(FunctionCall const& _funCall)
|
2018-10-10 12:31:49 +00:00
|
|
|
{
|
|
|
|
auto const& args = _funCall.arguments();
|
2019-07-01 14:11:29 +00:00
|
|
|
solAssert(args.size() >= 1, "");
|
|
|
|
solAssert(args.front()->annotation().type->category() == Type::Category::Bool, "");
|
|
|
|
addPathImpliedExpression(expr(*args.front()));
|
2018-10-10 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::visitGasLeft(FunctionCall const& _funCall)
|
2018-10-18 13:03:52 +00:00
|
|
|
{
|
2018-10-22 16:19:11 +00:00
|
|
|
string gasLeft = "gasleft()";
|
2018-10-18 13:03:52 +00:00
|
|
|
// We increase the variable index since gasleft changes
|
|
|
|
// inside a tx.
|
2018-12-10 16:23:36 +00:00
|
|
|
defineGlobalVariable(gasLeft, _funCall, true);
|
2019-05-14 16:57:34 +00:00
|
|
|
auto const& symbolicVar = m_context.globalSymbol(gasLeft);
|
2018-10-22 16:19:11 +00:00
|
|
|
unsigned index = symbolicVar->index();
|
|
|
|
// We set the current value to unknown anyway to add type constraints.
|
2019-05-10 14:28:29 +00:00
|
|
|
m_context.setUnknownValue(*symbolicVar);
|
2018-10-22 16:19:11 +00:00
|
|
|
if (index > 0)
|
2019-06-24 15:37:03 +00:00
|
|
|
m_context.addAssertion(symbolicVar->currentValue() <= symbolicVar->valueAtIndex(index - 1));
|
2018-10-18 13:03:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 12:00:32 +00:00
|
|
|
void SMTEncoder::visitObjectCreation(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
auto const& args = _funCall.arguments();
|
|
|
|
solAssert(args.size() >= 1, "");
|
|
|
|
auto argType = args.front()->annotation().type->category();
|
|
|
|
solAssert(argType == Type::Category::Integer || argType == Type::Category::RationalNumber, "");
|
|
|
|
|
|
|
|
smtutil::Expression arraySize = expr(*args.front());
|
|
|
|
setSymbolicUnknownValue(arraySize, TypeProvider::uint256(), m_context);
|
|
|
|
|
|
|
|
auto symbArray = dynamic_pointer_cast<smt::SymbolicArrayVariable>(m_context.expression(_funCall));
|
|
|
|
solAssert(symbArray, "");
|
|
|
|
smt::setSymbolicZeroValue(*symbArray, m_context);
|
|
|
|
auto zeroElements = symbArray->elements();
|
|
|
|
symbArray->increaseIndex();
|
|
|
|
m_context.addAssertion(symbArray->length() == arraySize);
|
|
|
|
m_context.addAssertion(symbArray->elements() == zeroElements);
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(Identifier const& _identifier)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2020-07-15 16:56:28 +00:00
|
|
|
if (auto decl = identifierToVariable(_identifier))
|
2019-07-01 14:25:33 +00:00
|
|
|
defineExpr(_identifier, currentValue(*decl));
|
2019-11-10 17:58:29 +00:00
|
|
|
else if (_identifier.annotation().type->category() == Type::Category::Function)
|
|
|
|
visitFunctionIdentifier(_identifier);
|
2019-07-01 14:25:33 +00:00
|
|
|
else if (_identifier.name() == "now")
|
|
|
|
defineGlobalVariable(_identifier.name(), _identifier);
|
|
|
|
else if (_identifier.name() == "this")
|
2018-06-12 08:58:24 +00:00
|
|
|
{
|
2020-03-24 19:15:15 +00:00
|
|
|
defineExpr(_identifier, m_context.state().thisAddress());
|
2019-07-01 14:25:33 +00:00
|
|
|
m_uninterpretedTerms.insert(&_identifier);
|
2018-06-12 08:58:24 +00:00
|
|
|
}
|
2020-09-24 09:32:56 +00:00
|
|
|
// Ignore the builtin abi, it is handled in FunctionCall.
|
|
|
|
// TODO: ignore MagicType in general (abi, block, msg, tx, type)
|
|
|
|
else if (auto magicType = dynamic_cast<MagicType const*>(_identifier.annotation().type); magicType && magicType->kind() == MagicType::Kind::ABI)
|
|
|
|
{
|
|
|
|
solAssert(_identifier.name() == "abi", "");
|
|
|
|
return;
|
|
|
|
}
|
2019-11-27 21:34:33 +00:00
|
|
|
else
|
2019-11-15 13:48:11 +00:00
|
|
|
createExpr(_identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTEncoder::endVisit(ElementaryTypeNameExpression const& _typeName)
|
|
|
|
{
|
|
|
|
auto const& typeType = dynamic_cast<TypeType const&>(*_typeName.annotation().type);
|
|
|
|
auto result = smt::newSymbolicVariable(
|
|
|
|
*TypeProvider::uint256(),
|
|
|
|
typeType.actualType()->toString(false),
|
|
|
|
m_context
|
|
|
|
);
|
|
|
|
solAssert(!result.first && result.second, "");
|
|
|
|
m_context.createExpression(_typeName, result.second);
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::visitTypeConversion(FunctionCall const& _funCall)
|
2018-12-20 12:20:07 +00:00
|
|
|
{
|
2020-04-08 17:38:30 +00:00
|
|
|
solAssert(*_funCall.annotation().kind == FunctionCallKind::TypeConversion, "");
|
2018-12-20 12:20:07 +00:00
|
|
|
solAssert(_funCall.arguments().size() == 1, "");
|
2019-05-09 11:17:00 +00:00
|
|
|
auto argument = _funCall.arguments().front();
|
2018-12-20 12:20:07 +00:00
|
|
|
unsigned argSize = argument->annotation().type->storageBytes();
|
|
|
|
unsigned castSize = _funCall.annotation().type->storageBytes();
|
2020-09-14 19:22:50 +00:00
|
|
|
auto const& funCallCategory = _funCall.annotation().type->category();
|
|
|
|
// Allow casting number literals to address.
|
|
|
|
// TODO: remove the isNegative() check once the type checker disallows this
|
|
|
|
if (
|
|
|
|
auto const* numberType = dynamic_cast<RationalNumberType const*>(argument->annotation().type);
|
|
|
|
numberType && !numberType->isNegative() && (funCallCategory == Type::Category::Address)
|
|
|
|
)
|
|
|
|
defineExpr(_funCall, numberType->literalValue(nullptr));
|
|
|
|
else if (argSize == castSize)
|
2018-12-20 12:20:07 +00:00
|
|
|
defineExpr(_funCall, expr(*argument));
|
|
|
|
else
|
|
|
|
{
|
2019-05-13 14:53:38 +00:00
|
|
|
m_context.setUnknownValue(*m_context.expression(_funCall));
|
2018-12-20 12:20:07 +00:00
|
|
|
// TODO: truncating and bytesX needs a different approach because of right padding.
|
|
|
|
if (funCallCategory == Type::Category::Integer || funCallCategory == Type::Category::Address)
|
|
|
|
{
|
|
|
|
if (argSize < castSize)
|
|
|
|
defineExpr(_funCall, expr(*argument));
|
|
|
|
else
|
|
|
|
{
|
2019-05-13 14:53:38 +00:00
|
|
|
auto const& intType = dynamic_cast<IntegerType const&>(*m_context.expression(_funCall)->type());
|
2020-05-19 12:14:46 +00:00
|
|
|
defineExpr(_funCall, smtutil::Expression::ite(
|
2019-05-09 10:16:52 +00:00
|
|
|
expr(*argument) >= smt::minValue(intType) && expr(*argument) <= smt::maxValue(intType),
|
2018-12-20 12:20:07 +00:00
|
|
|
expr(*argument),
|
|
|
|
expr(_funCall)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
5084_error,
|
2018-12-20 12:20:07 +00:00
|
|
|
_funCall.location(),
|
|
|
|
"Type conversion is not yet fully supported and might yield false positives."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::visitFunctionIdentifier(Identifier const& _identifier)
|
2018-12-10 16:23:36 +00:00
|
|
|
{
|
|
|
|
auto const& fType = dynamic_cast<FunctionType const&>(*_identifier.annotation().type);
|
2019-04-30 06:58:08 +00:00
|
|
|
if (fType.returnParameterTypes().size() == 1)
|
2018-12-10 16:23:36 +00:00
|
|
|
{
|
2019-09-18 20:19:35 +00:00
|
|
|
defineGlobalVariable(fType.identifier(), _identifier);
|
|
|
|
m_context.createExpression(_identifier, m_context.globalSymbol(fType.identifier()));
|
2018-12-10 16:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(Literal const& _literal)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-02-27 10:55:59 +00:00
|
|
|
solAssert(_literal.annotation().type, "Expected type for AST node");
|
2017-07-11 11:26:43 +00:00
|
|
|
Type const& type = *_literal.annotation().type;
|
2020-08-27 13:16:02 +00:00
|
|
|
if (smt::isNumber(type))
|
2020-05-19 12:14:46 +00:00
|
|
|
defineExpr(_literal, smtutil::Expression(type.literalValue(&_literal)));
|
2020-08-27 13:16:02 +00:00
|
|
|
else if (smt::isBool(type))
|
2020-05-19 12:14:46 +00:00
|
|
|
defineExpr(_literal, smtutil::Expression(_literal.token() == Token::TrueLiteral ? true : false));
|
2020-08-27 13:16:02 +00:00
|
|
|
else if (smt::isStringLiteral(type))
|
2020-09-24 11:16:38 +00:00
|
|
|
{
|
2019-08-07 08:48:09 +00:00
|
|
|
createExpr(_literal);
|
2020-09-24 11:16:38 +00:00
|
|
|
|
|
|
|
// Add constraints for the length and values as it is known.
|
|
|
|
auto symbArray = dynamic_pointer_cast<smt::SymbolicArrayVariable>(m_context.expression(_literal));
|
|
|
|
solAssert(symbArray, "");
|
|
|
|
|
|
|
|
auto value = _literal.value();
|
|
|
|
m_context.addAssertion(symbArray->length() == value.length());
|
|
|
|
for (size_t i = 0; i < value.length(); i++)
|
|
|
|
m_context.addAssertion(
|
|
|
|
smtutil::Expression::select(symbArray->elements(), i) == size_t(value[i])
|
|
|
|
);
|
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
else
|
2019-02-20 15:42:15 +00:00
|
|
|
{
|
2017-07-11 11:26:43 +00:00
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
7885_error,
|
2017-07-11 11:26:43 +00:00
|
|
|
_literal.location(),
|
2017-09-28 13:24:24 +00:00
|
|
|
"Assertion checker does not yet support the type of this literal (" +
|
2017-07-11 11:26:43 +00:00
|
|
|
_literal.annotation().type->toString() +
|
|
|
|
")."
|
|
|
|
);
|
2019-02-20 15:42:15 +00:00
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(Return const& _return)
|
2018-10-10 12:31:49 +00:00
|
|
|
{
|
2019-05-13 14:53:38 +00:00
|
|
|
if (_return.expression() && m_context.knownExpression(*_return.expression()))
|
2018-10-10 12:31:49 +00:00
|
|
|
{
|
2019-05-06 18:03:11 +00:00
|
|
|
auto returnParams = m_callStack.back().first->returnParameters();
|
2018-10-10 12:31:49 +00:00
|
|
|
if (returnParams.size() > 1)
|
2019-04-30 06:58:08 +00:00
|
|
|
{
|
2019-08-02 18:43:54 +00:00
|
|
|
auto const& symbTuple = dynamic_pointer_cast<smt::SymbolicTupleVariable>(m_context.expression(*_return.expression()));
|
|
|
|
solAssert(symbTuple, "");
|
2020-04-03 13:10:16 +00:00
|
|
|
solAssert(symbTuple->components().size() == returnParams.size(), "");
|
|
|
|
|
|
|
|
auto const* tupleType = dynamic_cast<TupleType const*>(_return.expression()->annotation().type);
|
|
|
|
solAssert(tupleType, "");
|
|
|
|
auto const& types = tupleType->components();
|
|
|
|
solAssert(types.size() == returnParams.size(), "");
|
|
|
|
|
2019-04-30 06:58:08 +00:00
|
|
|
for (unsigned i = 0; i < returnParams.size(); ++i)
|
2020-04-03 13:10:16 +00:00
|
|
|
m_context.addAssertion(symbTuple->component(i, types.at(i), returnParams.at(i)->type()) == m_context.newValue(*returnParams.at(i)));
|
2019-04-30 06:58:08 +00:00
|
|
|
}
|
2018-10-10 12:31:49 +00:00
|
|
|
else if (returnParams.size() == 1)
|
2019-11-13 11:21:37 +00:00
|
|
|
m_context.addAssertion(expr(*_return.expression(), returnParams.front()->type()) == m_context.newValue(*returnParams.front()));
|
2018-10-10 12:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::visit(MemberAccess const& _memberAccess)
|
2018-10-18 13:03:52 +00:00
|
|
|
{
|
2018-10-25 15:50:31 +00:00
|
|
|
auto const& accessType = _memberAccess.annotation().type;
|
|
|
|
if (accessType->category() == Type::Category::Function)
|
|
|
|
return true;
|
|
|
|
|
2019-07-01 14:25:33 +00:00
|
|
|
createExpr(_memberAccess);
|
|
|
|
|
2018-10-18 13:03:52 +00:00
|
|
|
auto const& exprType = _memberAccess.expression().annotation().type;
|
|
|
|
solAssert(exprType, "");
|
2019-03-06 00:10:43 +00:00
|
|
|
auto identifier = dynamic_cast<Identifier const*>(&_memberAccess.expression());
|
2018-10-18 13:03:52 +00:00
|
|
|
if (exprType->category() == Type::Category::Magic)
|
|
|
|
{
|
2018-10-22 16:19:11 +00:00
|
|
|
if (identifier)
|
2020-09-11 18:28:01 +00:00
|
|
|
defineGlobalVariable(identifier->name() + "." + _memberAccess.memberName(), _memberAccess);
|
|
|
|
else if (auto magicType = dynamic_cast<MagicType const*>(exprType); magicType->kind() == MagicType::Kind::MetaType)
|
|
|
|
{
|
|
|
|
auto const& memberName = _memberAccess.memberName();
|
|
|
|
if (memberName == "min" || memberName == "max")
|
|
|
|
{
|
|
|
|
IntegerType const& integerType = dynamic_cast<IntegerType const&>(*magicType->typeArgument());
|
|
|
|
defineExpr(_memberAccess, memberName == "min" ? integerType.minValue() : integerType.maxValue());
|
|
|
|
}
|
2020-09-11 18:37:34 +00:00
|
|
|
else if (memberName == "interfaceId")
|
|
|
|
{
|
|
|
|
ContractDefinition const& contract = dynamic_cast<ContractType const&>(*magicType->typeArgument()).contractDefinition();
|
2020-09-14 10:39:38 +00:00
|
|
|
defineExpr(_memberAccess, contract.interfaceId());
|
2020-09-11 18:37:34 +00:00
|
|
|
}
|
2020-09-11 18:28:01 +00:00
|
|
|
else
|
|
|
|
// NOTE: supporting name, creationCode, runtimeCode would be easy enough, but the bytes/string they return are not
|
|
|
|
// at all useable in the SMT checker currently
|
|
|
|
m_errorReporter.warning(
|
|
|
|
7507_error,
|
|
|
|
_memberAccess.location(),
|
|
|
|
"Assertion checker does not yet support this expression."
|
|
|
|
);
|
|
|
|
}
|
2018-10-22 16:19:11 +00:00
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
9551_error,
|
2018-10-22 16:19:11 +00:00
|
|
|
_memberAccess.location(),
|
|
|
|
"Assertion checker does not yet support this expression."
|
|
|
|
);
|
2018-10-18 13:03:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-27 13:16:02 +00:00
|
|
|
else if (smt::isNonRecursiveStruct(*exprType))
|
2020-08-25 14:58:09 +00:00
|
|
|
{
|
|
|
|
_memberAccess.expression().accept(*this);
|
|
|
|
auto const& symbStruct = dynamic_pointer_cast<smt::SymbolicStructVariable>(m_context.expression(_memberAccess.expression()));
|
|
|
|
defineExpr(_memberAccess, symbStruct->member(_memberAccess.memberName()));
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-06 00:10:43 +00:00
|
|
|
else if (exprType->category() == Type::Category::TypeType)
|
|
|
|
{
|
|
|
|
if (identifier && dynamic_cast<EnumDefinition const*>(identifier->annotation().referencedDeclaration))
|
|
|
|
{
|
2019-04-15 13:33:39 +00:00
|
|
|
auto enumType = dynamic_cast<EnumType const*>(accessType);
|
2019-03-06 00:10:43 +00:00
|
|
|
solAssert(enumType, "");
|
|
|
|
defineExpr(_memberAccess, enumType->memberValue(_memberAccess.memberName()));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-11 16:14:48 +00:00
|
|
|
else if (exprType->category() == Type::Category::Address)
|
|
|
|
{
|
|
|
|
_memberAccess.expression().accept(*this);
|
|
|
|
if (_memberAccess.memberName() == "balance")
|
|
|
|
{
|
2020-03-24 19:15:15 +00:00
|
|
|
defineExpr(_memberAccess, m_context.state().balance(expr(_memberAccess.expression())));
|
2019-07-03 14:05:56 +00:00
|
|
|
setSymbolicUnknownValue(*m_context.expression(_memberAccess), m_context);
|
2019-04-11 16:14:48 +00:00
|
|
|
m_uninterpretedTerms.insert(&_memberAccess);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 09:09:38 +00:00
|
|
|
else if (exprType->category() == Type::Category::Array)
|
|
|
|
{
|
|
|
|
_memberAccess.expression().accept(*this);
|
|
|
|
if (_memberAccess.memberName() == "length")
|
|
|
|
{
|
|
|
|
auto symbArray = dynamic_pointer_cast<smt::SymbolicArrayVariable>(m_context.expression(_memberAccess.expression()));
|
|
|
|
solAssert(symbArray, "");
|
|
|
|
defineExpr(_memberAccess, symbArray->length());
|
|
|
|
m_uninterpretedTerms.insert(&_memberAccess);
|
|
|
|
setSymbolicUnknownValue(
|
|
|
|
expr(_memberAccess),
|
|
|
|
_memberAccess.annotation().type,
|
|
|
|
m_context
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-18 13:03:52 +00:00
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
7650_error,
|
2018-10-18 13:03:52 +00:00
|
|
|
_memberAccess.location(),
|
|
|
|
"Assertion checker does not yet support this expression."
|
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::endVisit(IndexAccess const& _indexAccess)
|
2018-11-09 16:06:30 +00:00
|
|
|
{
|
2019-07-01 14:25:33 +00:00
|
|
|
createExpr(_indexAccess);
|
|
|
|
|
2019-12-02 23:00:13 +00:00
|
|
|
if (_indexAccess.annotation().type->category() == Type::Category::TypeType)
|
|
|
|
return;
|
2020-09-24 13:27:56 +00:00
|
|
|
if (auto const* type = dynamic_cast<FixedBytesType const*>(_indexAccess.baseExpression().annotation().type))
|
2020-07-27 10:11:10 +00:00
|
|
|
{
|
2020-09-24 13:27:56 +00:00
|
|
|
smtutil::Expression base = expr(_indexAccess.baseExpression());
|
|
|
|
|
|
|
|
if (type->numBytes() == 1)
|
|
|
|
defineExpr(_indexAccess, base);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto [bvSize, isSigned] = smt::typeBvSizeAndSignedness(_indexAccess.baseExpression().annotation().type);
|
|
|
|
solAssert(!isSigned, "");
|
|
|
|
solAssert(bvSize >= 16, "");
|
|
|
|
solAssert(bvSize % 8 == 0, "");
|
|
|
|
|
|
|
|
smtutil::Expression idx = expr(*_indexAccess.indexExpression());
|
|
|
|
|
|
|
|
auto bvBase = smtutil::Expression::int2bv(base, bvSize);
|
|
|
|
auto bvShl = smtutil::Expression::int2bv(idx * 8, bvSize);
|
|
|
|
auto bvShr = smtutil::Expression::int2bv(bvSize - 8, bvSize);
|
|
|
|
auto result = (bvBase << bvShl) >> bvShr;
|
|
|
|
|
|
|
|
auto anyValue = expr(_indexAccess);
|
|
|
|
m_context.expression(_indexAccess)->increaseIndex();
|
|
|
|
unsigned numBytes = bvSize / 8;
|
|
|
|
auto withBound = smtutil::Expression::ite(
|
|
|
|
idx < numBytes,
|
|
|
|
smtutil::Expression::bv2int(result, false),
|
|
|
|
anyValue
|
|
|
|
);
|
|
|
|
defineExpr(_indexAccess, withBound);
|
|
|
|
}
|
2020-07-27 10:11:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-12-02 23:00:13 +00:00
|
|
|
|
2019-05-09 10:16:52 +00:00
|
|
|
shared_ptr<smt::SymbolicVariable> array;
|
2019-12-02 23:00:13 +00:00
|
|
|
if (auto const* id = dynamic_cast<Identifier const*>(&_indexAccess.baseExpression()))
|
2018-11-09 16:06:30 +00:00
|
|
|
{
|
2019-04-29 09:44:32 +00:00
|
|
|
auto varDecl = identifierToVariable(*id);
|
|
|
|
solAssert(varDecl, "");
|
2019-05-10 14:28:29 +00:00
|
|
|
array = m_context.variable(*varDecl);
|
2018-11-09 16:06:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-25 14:58:09 +00:00
|
|
|
solAssert(m_context.knownExpression(_indexAccess.baseExpression()), "");
|
|
|
|
array = m_context.expression(_indexAccess.baseExpression());
|
2018-11-09 16:06:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 09:09:38 +00:00
|
|
|
auto arrayVar = dynamic_pointer_cast<smt::SymbolicArrayVariable>(array);
|
|
|
|
solAssert(arrayVar, "");
|
2020-05-19 12:14:46 +00:00
|
|
|
defineExpr(_indexAccess, smtutil::Expression::select(
|
2020-04-14 09:09:38 +00:00
|
|
|
arrayVar->elements(),
|
2018-11-09 16:06:30 +00:00
|
|
|
expr(*_indexAccess.indexExpression())
|
|
|
|
));
|
|
|
|
setSymbolicUnknownValue(
|
|
|
|
expr(_indexAccess),
|
|
|
|
_indexAccess.annotation().type,
|
2019-07-03 14:05:56 +00:00
|
|
|
m_context
|
2018-11-09 16:06:30 +00:00
|
|
|
);
|
|
|
|
m_uninterpretedTerms.insert(&_indexAccess);
|
|
|
|
}
|
|
|
|
|
2019-09-03 16:30:00 +00:00
|
|
|
void SMTEncoder::endVisit(IndexRangeAccess const& _indexRangeAccess)
|
|
|
|
{
|
|
|
|
createExpr(_indexRangeAccess);
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
2923_error,
|
2019-09-03 16:30:00 +00:00
|
|
|
_indexRangeAccess.location(),
|
|
|
|
"Assertion checker does not yet implement this expression."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::arrayAssignment()
|
2018-12-14 11:21:43 +00:00
|
|
|
{
|
|
|
|
m_arrayAssignmentHappened = true;
|
|
|
|
}
|
|
|
|
|
2020-08-25 14:58:09 +00:00
|
|
|
void SMTEncoder::indexOrMemberAssignment(Expression const& _expr, smtutil::Expression const& _rightHandSide)
|
2018-11-09 16:06:30 +00:00
|
|
|
{
|
2019-10-28 16:27:39 +00:00
|
|
|
auto toStore = _rightHandSide;
|
2020-08-25 14:58:09 +00:00
|
|
|
auto const* lastExpr = &_expr;
|
2019-10-28 16:27:39 +00:00
|
|
|
while (true)
|
2018-11-09 16:06:30 +00:00
|
|
|
{
|
2020-08-25 14:58:09 +00:00
|
|
|
if (auto const* indexAccess = dynamic_cast<IndexAccess const*>(lastExpr))
|
2019-10-28 16:27:39 +00:00
|
|
|
{
|
2020-08-25 14:58:09 +00:00
|
|
|
auto const& base = indexAccess->baseExpression();
|
|
|
|
if (dynamic_cast<Identifier const*>(&base))
|
|
|
|
base.accept(*this);
|
|
|
|
auto symbArray = dynamic_pointer_cast<smt::SymbolicArrayVariable>(m_context.expression(base));
|
|
|
|
solAssert(symbArray, "");
|
|
|
|
auto baseType = symbArray->type();
|
|
|
|
toStore = smtutil::Expression::tuple_constructor(
|
|
|
|
smtutil::Expression(make_shared<smtutil::SortSort>(smt::smtSort(*baseType)), baseType->toString(true)),
|
|
|
|
{smtutil::Expression::store(symbArray->elements(), expr(*indexAccess->indexExpression()), toStore), symbArray->length()}
|
2019-10-28 16:27:39 +00:00
|
|
|
);
|
2020-08-25 14:58:09 +00:00
|
|
|
m_context.expression(*indexAccess)->increaseIndex();
|
2020-05-19 12:14:46 +00:00
|
|
|
defineExpr(*indexAccess, smtutil::Expression::select(
|
2020-04-14 09:09:38 +00:00
|
|
|
symbArray->elements(),
|
2019-10-28 16:27:39 +00:00
|
|
|
expr(*indexAccess->indexExpression())
|
|
|
|
));
|
2020-08-25 14:58:09 +00:00
|
|
|
lastExpr = &indexAccess->baseExpression();
|
2019-10-28 16:27:39 +00:00
|
|
|
}
|
2020-08-25 14:58:09 +00:00
|
|
|
else if (auto const* memberAccess = dynamic_cast<MemberAccess const*>(lastExpr))
|
2019-05-09 14:06:13 +00:00
|
|
|
{
|
2020-08-25 14:58:09 +00:00
|
|
|
auto const& base = memberAccess->expression();
|
|
|
|
if (dynamic_cast<Identifier const*>(&base))
|
|
|
|
base.accept(*this);
|
2020-08-27 13:16:02 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
auto const* structType = dynamic_cast<StructType const*>(base.annotation().type);
|
|
|
|
structType && structType->recursive()
|
|
|
|
)
|
|
|
|
{
|
|
|
|
m_errorReporter.warning(
|
|
|
|
4375_error,
|
|
|
|
memberAccess->location(),
|
|
|
|
"Assertion checker does not support recursive structs."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-25 14:58:09 +00:00
|
|
|
auto symbStruct = dynamic_pointer_cast<smt::SymbolicStructVariable>(m_context.expression(base));
|
|
|
|
solAssert(symbStruct, "");
|
|
|
|
symbStruct->assignMember(memberAccess->memberName(), toStore);
|
|
|
|
toStore = symbStruct->currentValue();
|
|
|
|
defineExpr(*memberAccess, symbStruct->member(memberAccess->memberName()));
|
|
|
|
lastExpr = &memberAccess->expression();
|
|
|
|
}
|
|
|
|
else if (auto const& id = dynamic_cast<Identifier const*>(lastExpr))
|
|
|
|
{
|
|
|
|
auto varDecl = identifierToVariable(*id);
|
|
|
|
solAssert(varDecl, "");
|
|
|
|
|
|
|
|
if (varDecl->hasReferenceOrMappingType())
|
|
|
|
resetReferences(*varDecl);
|
|
|
|
|
|
|
|
m_context.addAssertion(m_context.newValue(*varDecl) == toStore);
|
|
|
|
m_context.expression(*id)->increaseIndex();
|
|
|
|
defineExpr(*id,currentValue(*varDecl));
|
|
|
|
break;
|
2019-10-28 16:27:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-25 17:28:31 +00:00
|
|
|
auto type = lastExpr->annotation().type;
|
|
|
|
if (
|
|
|
|
dynamic_cast<ReferenceType const*>(type) ||
|
|
|
|
dynamic_cast<MappingType const*>(type)
|
|
|
|
)
|
|
|
|
resetReferences(type);
|
|
|
|
|
|
|
|
m_context.expression(*lastExpr)->increaseIndex();
|
|
|
|
m_context.addAssertion(expr(*lastExpr) == toStore);
|
2019-10-28 16:27:39 +00:00
|
|
|
break;
|
2019-05-09 14:06:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-09 16:06:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 22:39:00 +00:00
|
|
|
void SMTEncoder::arrayPush(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
auto memberAccess = dynamic_cast<MemberAccess const*>(&_funCall.expression());
|
|
|
|
solAssert(memberAccess, "");
|
|
|
|
auto symbArray = dynamic_pointer_cast<smt::SymbolicArrayVariable>(m_context.expression(memberAccess->expression()));
|
|
|
|
solAssert(symbArray, "");
|
|
|
|
auto oldLength = symbArray->length();
|
2020-05-17 18:20:17 +00:00
|
|
|
m_context.addAssertion(oldLength >= 0);
|
|
|
|
// Real world assumption: the array length is assumed to not overflow.
|
|
|
|
// This assertion guarantees that both the current and updated lengths have the above property.
|
|
|
|
m_context.addAssertion(oldLength + 1 < (smt::maxValue(*TypeProvider::uint256()) - 1));
|
|
|
|
|
2020-05-11 22:39:00 +00:00
|
|
|
auto const& arguments = _funCall.arguments();
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression element = arguments.empty() ?
|
2020-05-11 22:39:00 +00:00
|
|
|
smt::zeroValue(_funCall.annotation().type) :
|
|
|
|
expr(*arguments.front());
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression store = smtutil::Expression::store(
|
2020-05-11 22:39:00 +00:00
|
|
|
symbArray->elements(),
|
|
|
|
oldLength,
|
|
|
|
element
|
|
|
|
);
|
|
|
|
symbArray->increaseIndex();
|
|
|
|
m_context.addAssertion(symbArray->elements() == store);
|
2020-05-17 18:20:17 +00:00
|
|
|
m_context.addAssertion(symbArray->length() == oldLength + 1);
|
2020-05-11 22:39:00 +00:00
|
|
|
|
|
|
|
if (arguments.empty())
|
2020-07-17 08:00:25 +00:00
|
|
|
defineExpr(_funCall, smtutil::Expression::select(symbArray->elements(), oldLength));
|
2020-05-11 22:39:00 +00:00
|
|
|
|
|
|
|
arrayPushPopAssign(memberAccess->expression(), symbArray->currentValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTEncoder::arrayPop(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
auto memberAccess = dynamic_cast<MemberAccess const*>(&_funCall.expression());
|
|
|
|
solAssert(memberAccess, "");
|
|
|
|
auto symbArray = dynamic_pointer_cast<smt::SymbolicArrayVariable>(m_context.expression(memberAccess->expression()));
|
|
|
|
solAssert(symbArray, "");
|
2020-05-17 21:21:08 +00:00
|
|
|
|
|
|
|
makeArrayPopVerificationTarget(_funCall);
|
|
|
|
|
2020-05-11 22:39:00 +00:00
|
|
|
auto oldElements = symbArray->elements();
|
|
|
|
auto oldLength = symbArray->length();
|
2020-05-17 21:21:08 +00:00
|
|
|
|
2020-05-11 22:39:00 +00:00
|
|
|
symbArray->increaseIndex();
|
|
|
|
m_context.addAssertion(symbArray->elements() == oldElements);
|
2020-05-19 12:14:46 +00:00
|
|
|
auto newLength = smtutil::Expression::ite(
|
2020-08-28 09:59:15 +00:00
|
|
|
oldLength > 0,
|
|
|
|
oldLength - 1,
|
|
|
|
0
|
2020-05-11 22:39:00 +00:00
|
|
|
);
|
2020-08-28 09:59:15 +00:00
|
|
|
m_context.addAssertion(symbArray->length() == newLength);
|
2020-05-11 22:39:00 +00:00
|
|
|
|
|
|
|
arrayPushPopAssign(memberAccess->expression(), symbArray->currentValue());
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
void SMTEncoder::arrayPushPopAssign(Expression const& _expr, smtutil::Expression const& _array)
|
2020-05-11 22:39:00 +00:00
|
|
|
{
|
2020-08-31 10:59:11 +00:00
|
|
|
Expression const* expr = innermostTuple(_expr);
|
2020-07-27 15:14:59 +00:00
|
|
|
|
|
|
|
if (auto const* id = dynamic_cast<Identifier const*>(expr))
|
2020-05-11 22:39:00 +00:00
|
|
|
{
|
|
|
|
auto varDecl = identifierToVariable(*id);
|
|
|
|
solAssert(varDecl, "");
|
2020-06-05 12:54:52 +00:00
|
|
|
if (varDecl->hasReferenceOrMappingType())
|
|
|
|
resetReferences(*varDecl);
|
2020-05-11 22:39:00 +00:00
|
|
|
m_context.addAssertion(m_context.newValue(*varDecl) == _array);
|
2020-08-25 14:58:09 +00:00
|
|
|
m_context.expression(*id)->increaseIndex();
|
|
|
|
defineExpr(*id,currentValue(*varDecl));
|
2020-05-11 22:39:00 +00:00
|
|
|
}
|
2020-08-25 14:58:09 +00:00
|
|
|
else if (
|
|
|
|
dynamic_cast<IndexAccess const*>(expr) ||
|
|
|
|
dynamic_cast<MemberAccess const*>(expr)
|
|
|
|
)
|
|
|
|
indexOrMemberAssignment(_expr, _array);
|
2020-07-27 15:14:59 +00:00
|
|
|
else if (auto const* funCall = dynamic_cast<FunctionCall const*>(expr))
|
2020-07-17 08:00:25 +00:00
|
|
|
{
|
|
|
|
FunctionType const& funType = dynamic_cast<FunctionType const&>(*funCall->expression().annotation().type);
|
|
|
|
if (funType.kind() == FunctionType::Kind::ArrayPush)
|
|
|
|
{
|
|
|
|
auto memberAccess = dynamic_cast<MemberAccess const*>(&funCall->expression());
|
|
|
|
solAssert(memberAccess, "");
|
|
|
|
auto symbArray = dynamic_pointer_cast<smt::SymbolicArrayVariable>(m_context.expression(memberAccess->expression()));
|
|
|
|
solAssert(symbArray, "");
|
|
|
|
|
|
|
|
auto oldLength = symbArray->length();
|
|
|
|
auto store = smtutil::Expression::store(
|
|
|
|
symbArray->elements(),
|
|
|
|
symbArray->length() - 1,
|
|
|
|
_array
|
|
|
|
);
|
|
|
|
symbArray->increaseIndex();
|
|
|
|
m_context.addAssertion(symbArray->elements() == store);
|
|
|
|
m_context.addAssertion(symbArray->length() == oldLength);
|
|
|
|
arrayPushPopAssign(memberAccess->expression(), symbArray->currentValue());
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 22:39:00 +00:00
|
|
|
else
|
|
|
|
solAssert(false, "");
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::defineGlobalVariable(string const& _name, Expression const& _expr, bool _increaseIndex)
|
2018-10-18 13:03:52 +00:00
|
|
|
{
|
2019-05-14 16:57:34 +00:00
|
|
|
if (!m_context.knownGlobalSymbol(_name))
|
2018-10-18 13:03:52 +00:00
|
|
|
{
|
2019-05-14 16:57:34 +00:00
|
|
|
bool abstract = m_context.createGlobalSymbol(_name, _expr);
|
|
|
|
if (abstract)
|
2018-10-18 13:03:52 +00:00
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
1695_error,
|
2018-10-18 13:03:52 +00:00
|
|
|
_expr.location(),
|
2018-12-10 16:23:36 +00:00
|
|
|
"Assertion checker does not yet support this global variable."
|
2018-10-18 13:03:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (_increaseIndex)
|
2019-05-14 16:57:34 +00:00
|
|
|
m_context.globalSymbol(_name)->increaseIndex();
|
2018-10-18 13:03:52 +00:00
|
|
|
// The default behavior is not to increase the index since
|
2018-12-10 16:23:36 +00:00
|
|
|
// most of the global values stay the same throughout a tx.
|
2020-08-27 13:16:02 +00:00
|
|
|
if (smt::isSupportedType(*_expr.annotation().type))
|
2019-05-14 16:57:34 +00:00
|
|
|
defineExpr(_expr, m_context.globalSymbol(_name)->currentValue());
|
2018-10-25 14:00:09 +00:00
|
|
|
}
|
2018-10-18 13:03:52 +00:00
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::shortcutRationalNumber(Expression const& _expr)
|
2019-03-11 10:56:08 +00:00
|
|
|
{
|
|
|
|
if (_expr.annotation().type->category() == Type::Category::RationalNumber)
|
|
|
|
{
|
2019-04-15 13:33:39 +00:00
|
|
|
auto rationalType = dynamic_cast<RationalNumberType const*>(_expr.annotation().type);
|
2019-03-11 10:56:08 +00:00
|
|
|
solAssert(rationalType, "");
|
|
|
|
if (rationalType->isNegative())
|
2020-05-19 12:14:46 +00:00
|
|
|
defineExpr(_expr, smtutil::Expression(u2s(rationalType->literalValue(nullptr))));
|
2019-03-11 10:56:08 +00:00
|
|
|
else
|
2020-05-19 12:14:46 +00:00
|
|
|
defineExpr(_expr, smtutil::Expression(rationalType->literalValue(nullptr)));
|
2019-03-11 10:56:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::arithmeticOperation(BinaryOperation const& _op)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-04-30 15:45:28 +00:00
|
|
|
auto type = _op.annotation().commonType;
|
|
|
|
solAssert(type, "");
|
2020-05-29 16:13:03 +00:00
|
|
|
if (type->category() == Type::Category::Integer || type->category() == Type::Category::FixedPoint)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-04-30 15:45:28 +00:00
|
|
|
switch (_op.getOperator())
|
|
|
|
{
|
|
|
|
case Token::Add:
|
|
|
|
case Token::Sub:
|
|
|
|
case Token::Mul:
|
|
|
|
case Token::Div:
|
|
|
|
case Token::Mod:
|
|
|
|
{
|
2019-06-25 10:46:17 +00:00
|
|
|
auto values = arithmeticOperation(
|
2019-04-30 15:45:28 +00:00
|
|
|
_op.getOperator(),
|
|
|
|
expr(_op.leftExpression()),
|
|
|
|
expr(_op.rightExpression()),
|
|
|
|
_op.annotation().commonType,
|
2019-06-24 15:37:03 +00:00
|
|
|
_op
|
2019-06-25 10:46:17 +00:00
|
|
|
);
|
|
|
|
defineExpr(_op, values.first);
|
2019-04-30 15:45:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
5188_error,
|
2019-04-30 15:45:28 +00:00
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement this operator."
|
|
|
|
);
|
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
2019-04-30 15:45:28 +00:00
|
|
|
else
|
2017-07-11 11:26:43 +00:00
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
9011_error,
|
2017-07-11 11:26:43 +00:00
|
|
|
_op.location(),
|
2019-04-30 15:45:28 +00:00
|
|
|
"Assertion checker does not yet implement this operator for type " + type->richIdentifier() + "."
|
2017-07-11 11:26:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
pair<smtutil::Expression, smtutil::Expression> SMTEncoder::arithmeticOperation(
|
2019-03-28 11:42:32 +00:00
|
|
|
Token _op,
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression const& _left,
|
|
|
|
smtutil::Expression const& _right,
|
2019-03-28 11:42:32 +00:00
|
|
|
TypePointer const& _commonType,
|
2020-06-09 11:07:40 +00:00
|
|
|
Expression const& _operation
|
2019-03-28 11:42:32 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
static set<Token> validOperators{
|
|
|
|
Token::Add,
|
|
|
|
Token::Sub,
|
|
|
|
Token::Mul,
|
2019-04-05 15:07:08 +00:00
|
|
|
Token::Div,
|
|
|
|
Token::Mod
|
2019-03-28 11:42:32 +00:00
|
|
|
};
|
|
|
|
solAssert(validOperators.count(_op), "");
|
|
|
|
solAssert(_commonType, "");
|
2020-05-29 16:13:03 +00:00
|
|
|
solAssert(
|
|
|
|
_commonType->category() == Type::Category::Integer || _commonType->category() == Type::Category::FixedPoint,
|
|
|
|
""
|
|
|
|
);
|
|
|
|
|
|
|
|
IntegerType const* intType = nullptr;
|
|
|
|
if (auto type = dynamic_cast<IntegerType const*>(_commonType))
|
|
|
|
intType = type;
|
|
|
|
else
|
|
|
|
intType = TypeProvider::uint256();
|
2019-03-28 11:42:32 +00:00
|
|
|
|
2020-06-09 11:07:40 +00:00
|
|
|
auto valueUnbounded = [&]() -> smtutil::Expression {
|
|
|
|
switch (_op)
|
|
|
|
{
|
|
|
|
case Token::Add: return _left + _right;
|
|
|
|
case Token::Sub: return _left - _right;
|
|
|
|
case Token::Mul: return _left * _right;
|
|
|
|
case Token::Div: return division(_left, _right, *intType);
|
|
|
|
case Token::Mod: return _left % _right;
|
|
|
|
default: solAssert(false, "");
|
|
|
|
}
|
|
|
|
}();
|
2019-03-28 11:42:32 +00:00
|
|
|
|
2019-04-05 15:07:08 +00:00
|
|
|
if (_op == Token::Div || _op == Token::Mod)
|
2020-06-09 11:07:40 +00:00
|
|
|
{
|
2019-06-24 15:37:03 +00:00
|
|
|
m_context.addAssertion(_right != 0);
|
2019-03-28 11:42:32 +00:00
|
|
|
|
2020-06-09 11:07:40 +00:00
|
|
|
// mod and unsigned division never underflow/overflow
|
|
|
|
if (_op == Token::Mod || !intType->isSigned())
|
|
|
|
return {valueUnbounded, valueUnbounded};
|
|
|
|
|
|
|
|
// The only case where division overflows is
|
|
|
|
// - type is signed
|
|
|
|
// - LHS is type.min
|
|
|
|
// - RHS is -1
|
|
|
|
// the result is then -(type.min), which wraps back to type.min
|
|
|
|
smtutil::Expression maxLeft = _left == smt::minValue(*intType);
|
2020-06-15 10:51:58 +00:00
|
|
|
smtutil::Expression minusOneRight = _right == numeric_limits<size_t >::max();
|
2020-06-09 11:07:40 +00:00
|
|
|
smtutil::Expression wrap = smtutil::Expression::ite(maxLeft && minusOneRight, smt::minValue(*intType), valueUnbounded);
|
|
|
|
return {wrap, valueUnbounded};
|
|
|
|
}
|
|
|
|
|
2020-05-29 16:13:03 +00:00
|
|
|
auto symbMin = smt::minValue(*intType);
|
|
|
|
auto symbMax = smt::maxValue(*intType);
|
|
|
|
|
|
|
|
smtutil::Expression intValueRange = (0 - symbMin) + symbMax + 1;
|
2020-07-10 08:28:49 +00:00
|
|
|
string suffix = to_string(_operation.id()) + "_" + to_string(m_context.newUniqueId());
|
2020-06-09 11:07:40 +00:00
|
|
|
smt::SymbolicIntVariable k(intType, intType, "k_" + suffix, m_context);
|
|
|
|
smt::SymbolicIntVariable m(intType, intType, "m_" + suffix, m_context);
|
|
|
|
|
|
|
|
// To wrap around valueUnbounded in case of overflow or underflow, we replace it with a k, given:
|
|
|
|
// 1. k + m * intValueRange = valueUnbounded
|
|
|
|
// 2. k is in range of the desired integer type
|
|
|
|
auto wrap = k.currentValue();
|
|
|
|
m_context.addAssertion(valueUnbounded == (k.currentValue() + intValueRange * m.currentValue()));
|
|
|
|
m_context.addAssertion(k.currentValue() >= symbMin);
|
|
|
|
m_context.addAssertion(k.currentValue() <= symbMax);
|
|
|
|
|
|
|
|
// TODO this could be refined:
|
|
|
|
// for unsigned types it's enough to check only the upper bound.
|
2020-05-19 12:14:46 +00:00
|
|
|
auto value = smtutil::Expression::ite(
|
2020-06-09 11:07:40 +00:00
|
|
|
valueUnbounded > symbMax,
|
|
|
|
wrap,
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression::ite(
|
2020-06-09 11:07:40 +00:00
|
|
|
valueUnbounded < symbMin,
|
|
|
|
wrap,
|
|
|
|
valueUnbounded
|
2019-08-20 13:03:45 +00:00
|
|
|
)
|
2019-03-28 11:42:32 +00:00
|
|
|
);
|
2019-08-20 13:03:45 +00:00
|
|
|
|
2020-06-09 11:07:40 +00:00
|
|
|
return {value, valueUnbounded};
|
2019-03-28 11:42:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 14:54:58 +00:00
|
|
|
smtutil::Expression SMTEncoder::bitwiseOperation(
|
|
|
|
Token _op,
|
|
|
|
smtutil::Expression const& _left,
|
|
|
|
smtutil::Expression const& _right,
|
|
|
|
TypePointer const& _commonType
|
|
|
|
)
|
|
|
|
{
|
|
|
|
static set<Token> validOperators{
|
|
|
|
Token::BitAnd,
|
|
|
|
Token::BitOr,
|
|
|
|
Token::BitXor,
|
|
|
|
Token::SHL,
|
|
|
|
Token::SHR,
|
|
|
|
Token::SAR
|
|
|
|
};
|
|
|
|
solAssert(validOperators.count(_op), "");
|
|
|
|
solAssert(_commonType, "");
|
|
|
|
|
|
|
|
auto [bvSize, isSigned] = smt::typeBvSizeAndSignedness(_commonType);
|
|
|
|
|
|
|
|
auto bvLeft = smtutil::Expression::int2bv(_left, bvSize);
|
|
|
|
auto bvRight = smtutil::Expression::int2bv(_right, bvSize);
|
|
|
|
|
|
|
|
optional<smtutil::Expression> result;
|
|
|
|
switch (_op)
|
|
|
|
{
|
|
|
|
case Token::BitAnd:
|
|
|
|
result = bvLeft & bvRight;
|
|
|
|
break;
|
|
|
|
case Token::BitOr:
|
|
|
|
result = bvLeft | bvRight;
|
|
|
|
break;
|
|
|
|
case Token::BitXor:
|
|
|
|
result = bvLeft ^ bvRight;
|
|
|
|
break;
|
|
|
|
case Token::SHL:
|
|
|
|
result = bvLeft << bvRight;
|
|
|
|
break;
|
|
|
|
case Token::SHR:
|
|
|
|
solAssert(false, "");
|
|
|
|
case Token::SAR:
|
|
|
|
result = isSigned ?
|
|
|
|
smtutil::Expression::ashr(bvLeft, bvRight) :
|
|
|
|
bvLeft >> bvRight;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
solAssert(false, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
solAssert(result.has_value(), "");
|
|
|
|
return smtutil::Expression::bv2int(*result, isSigned);
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::compareOperation(BinaryOperation const& _op)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-08-07 08:48:09 +00:00
|
|
|
auto const& commonType = _op.annotation().commonType;
|
|
|
|
solAssert(commonType, "");
|
2020-08-27 13:16:02 +00:00
|
|
|
if (smt::isSupportedType(*commonType))
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression left(expr(_op.leftExpression(), commonType));
|
|
|
|
smtutil::Expression right(expr(_op.rightExpression(), commonType));
|
2018-10-22 14:48:21 +00:00
|
|
|
Token op = _op.getOperator();
|
2020-05-19 12:14:46 +00:00
|
|
|
shared_ptr<smtutil::Expression> value;
|
2020-08-27 13:16:02 +00:00
|
|
|
if (smt::isNumber(*commonType))
|
2018-03-09 15:19:03 +00:00
|
|
|
{
|
2020-05-19 12:14:46 +00:00
|
|
|
value = make_shared<smtutil::Expression>(
|
2018-03-09 15:19:03 +00:00
|
|
|
op == Token::Equal ? (left == right) :
|
|
|
|
op == Token::NotEqual ? (left != right) :
|
|
|
|
op == Token::LessThan ? (left < right) :
|
|
|
|
op == Token::LessThanOrEqual ? (left <= right) :
|
|
|
|
op == Token::GreaterThan ? (left > right) :
|
|
|
|
/*op == Token::GreaterThanOrEqual*/ (left >= right)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else // Bool
|
|
|
|
{
|
2020-08-27 13:16:02 +00:00
|
|
|
solUnimplementedAssert(smt::isBool(*commonType), "Operation not yet supported");
|
2020-05-19 12:14:46 +00:00
|
|
|
value = make_shared<smtutil::Expression>(
|
2018-03-09 15:19:03 +00:00
|
|
|
op == Token::Equal ? (left == right) :
|
2018-04-27 09:35:58 +00:00
|
|
|
/*op == Token::NotEqual*/ (left != right)
|
2018-03-09 15:19:03 +00:00
|
|
|
);
|
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
// TODO: check that other values for op are not possible.
|
2018-03-09 15:19:03 +00:00
|
|
|
defineExpr(_op, *value);
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
7229_error,
|
2017-07-11 11:26:43 +00:00
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement the type " + _op.annotation().commonType->toString() + " for comparisons"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::booleanOperation(BinaryOperation const& _op)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
|
|
|
solAssert(_op.getOperator() == Token::And || _op.getOperator() == Token::Or, "");
|
|
|
|
solAssert(_op.annotation().commonType, "");
|
|
|
|
if (_op.annotation().commonType->category() == Type::Category::Bool)
|
|
|
|
{
|
2017-09-28 11:44:56 +00:00
|
|
|
// @TODO check that both of them are not constant
|
2019-03-22 16:05:58 +00:00
|
|
|
_op.leftExpression().accept(*this);
|
2017-07-11 11:26:43 +00:00
|
|
|
if (_op.getOperator() == Token::And)
|
2019-03-22 16:05:58 +00:00
|
|
|
{
|
|
|
|
auto indicesAfterSecond = visitBranch(&_op.rightExpression(), expr(_op.leftExpression()));
|
2019-05-03 04:15:00 +00:00
|
|
|
mergeVariables(touchedVariables(_op.rightExpression()), !expr(_op.leftExpression()), copyVariableIndices(), indicesAfterSecond);
|
2017-10-05 13:23:25 +00:00
|
|
|
defineExpr(_op, expr(_op.leftExpression()) && expr(_op.rightExpression()));
|
2019-03-22 16:05:58 +00:00
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
else
|
2019-03-22 16:05:58 +00:00
|
|
|
{
|
|
|
|
auto indicesAfterSecond = visitBranch(&_op.rightExpression(), !expr(_op.leftExpression()));
|
2019-05-03 04:15:00 +00:00
|
|
|
mergeVariables(touchedVariables(_op.rightExpression()), expr(_op.leftExpression()), copyVariableIndices(), indicesAfterSecond);
|
2017-10-05 13:23:25 +00:00
|
|
|
defineExpr(_op, expr(_op.leftExpression()) || expr(_op.rightExpression()));
|
2019-03-22 16:05:58 +00:00
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
3263_error,
|
2017-07-11 11:26:43 +00:00
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement the type " + _op.annotation().commonType->toString() + " for boolean operations"
|
2019-03-22 16:05:58 +00:00
|
|
|
);
|
2017-09-28 11:44:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 11:08:48 +00:00
|
|
|
void SMTEncoder::bitwiseOperation(BinaryOperation const& _op)
|
|
|
|
{
|
2020-09-04 14:55:45 +00:00
|
|
|
auto op = _op.getOperator();
|
|
|
|
solAssert(TokenTraits::isBitOp(op) || TokenTraits::isShiftOp(op), "");
|
2020-05-13 11:08:48 +00:00
|
|
|
auto commonType = _op.annotation().commonType;
|
|
|
|
solAssert(commonType, "");
|
|
|
|
|
2020-09-21 14:54:58 +00:00
|
|
|
defineExpr(_op, bitwiseOperation(
|
|
|
|
_op.getOperator(),
|
|
|
|
expr(_op.leftExpression(), commonType),
|
|
|
|
expr(_op.rightExpression(), commonType),
|
|
|
|
commonType
|
|
|
|
));
|
2020-05-13 11:08:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 19:21:45 +00:00
|
|
|
void SMTEncoder::bitwiseNotOperation(UnaryOperation const& _op)
|
|
|
|
{
|
|
|
|
solAssert(_op.getOperator() == Token::BitNot, "");
|
|
|
|
|
|
|
|
auto [bvSize, isSigned] = smt::typeBvSizeAndSignedness(_op.annotation().type);
|
|
|
|
|
|
|
|
auto bvOperand = smtutil::Expression::int2bv(expr(_op.subExpression(), _op.annotation().type), bvSize);
|
|
|
|
defineExpr(_op, smtutil::Expression::bv2int(~bvOperand, isSigned));
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression SMTEncoder::division(smtutil::Expression _left, smtutil::Expression _right, IntegerType const& _type)
|
2017-10-05 17:20:46 +00:00
|
|
|
{
|
|
|
|
// Signed division in SMTLIB2 rounds differently for negative division.
|
|
|
|
if (_type.isSigned())
|
2020-09-16 16:08:54 +00:00
|
|
|
return signedDivisionEVM(_left, _right);
|
2017-10-05 17:20:46 +00:00
|
|
|
else
|
|
|
|
return _left / _right;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::assignment(
|
2019-04-29 09:39:24 +00:00
|
|
|
Expression const& _left,
|
2020-05-27 17:24:48 +00:00
|
|
|
smtutil::Expression const& _right,
|
2020-08-25 17:28:31 +00:00
|
|
|
TypePointer const& _type
|
2019-04-29 09:39:24 +00:00
|
|
|
)
|
|
|
|
{
|
2020-05-27 17:24:48 +00:00
|
|
|
solAssert(
|
|
|
|
_left.annotation().type->category() != Type::Category::Tuple,
|
|
|
|
"Tuple assignments should be handled by tupleAssignment."
|
|
|
|
);
|
|
|
|
|
2020-08-31 10:59:11 +00:00
|
|
|
Expression const* left = innermostTuple(_left);
|
2020-07-15 16:56:28 +00:00
|
|
|
|
2020-08-27 13:16:02 +00:00
|
|
|
if (!smt::isSupportedType(*_type))
|
2019-08-02 17:46:06 +00:00
|
|
|
{
|
|
|
|
// Give it a new index anyway to keep the SSA scheme sound.
|
2020-07-15 16:56:28 +00:00
|
|
|
if (auto varDecl = identifierToVariable(*left))
|
2019-08-02 17:46:06 +00:00
|
|
|
m_context.newValue(*varDecl);
|
|
|
|
}
|
2020-07-15 16:56:28 +00:00
|
|
|
else if (auto varDecl = identifierToVariable(*left))
|
2020-05-27 17:24:48 +00:00
|
|
|
assignment(*varDecl, _right);
|
2020-08-25 14:58:09 +00:00
|
|
|
else if (
|
|
|
|
dynamic_cast<IndexAccess const*>(left) ||
|
|
|
|
dynamic_cast<MemberAccess const*>(left)
|
|
|
|
)
|
|
|
|
indexOrMemberAssignment(*left, _right);
|
2019-04-29 09:39:24 +00:00
|
|
|
else
|
2020-08-25 17:28:31 +00:00
|
|
|
solAssert(false, "");
|
2019-04-29 09:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 17:24:48 +00:00
|
|
|
void SMTEncoder::tupleAssignment(Expression const& _left, Expression const& _right)
|
|
|
|
{
|
2020-08-31 10:59:11 +00:00
|
|
|
auto lTuple = dynamic_cast<TupleExpression const*>(innermostTuple(_left));
|
2020-05-27 17:24:48 +00:00
|
|
|
solAssert(lTuple, "");
|
2020-08-31 14:12:38 +00:00
|
|
|
Expression const* right = innermostTuple(_right);
|
2020-05-27 17:24:48 +00:00
|
|
|
|
|
|
|
auto const& lComponents = lTuple->components();
|
|
|
|
|
|
|
|
// If both sides are tuple expressions, we individually and potentially
|
|
|
|
// recursively assign each pair of components.
|
|
|
|
// This is because of potential type conversion.
|
2020-08-31 14:12:38 +00:00
|
|
|
if (auto rTuple = dynamic_cast<TupleExpression const*>(right))
|
2020-05-27 17:24:48 +00:00
|
|
|
{
|
|
|
|
auto const& rComponents = rTuple->components();
|
|
|
|
solAssert(lComponents.size() == rComponents.size(), "");
|
|
|
|
for (unsigned i = 0; i < lComponents.size(); ++i)
|
|
|
|
{
|
|
|
|
if (!lComponents.at(i) || !rComponents.at(i))
|
|
|
|
continue;
|
|
|
|
auto const& lExpr = *lComponents.at(i);
|
|
|
|
auto const& rExpr = *rComponents.at(i);
|
|
|
|
if (lExpr.annotation().type->category() == Type::Category::Tuple)
|
|
|
|
tupleAssignment(lExpr, rExpr);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto type = lExpr.annotation().type;
|
2020-08-25 17:28:31 +00:00
|
|
|
assignment(lExpr, expr(rExpr, type), type);
|
2020-05-27 17:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-31 14:12:38 +00:00
|
|
|
auto rType = dynamic_cast<TupleType const*>(right->annotation().type);
|
2020-05-27 17:24:48 +00:00
|
|
|
solAssert(rType, "");
|
|
|
|
|
|
|
|
auto const& rComponents = rType->components();
|
|
|
|
solAssert(lComponents.size() == rComponents.size(), "");
|
|
|
|
|
2020-08-31 14:12:38 +00:00
|
|
|
auto symbRight = expr(*right);
|
2020-05-27 17:24:48 +00:00
|
|
|
solAssert(symbRight.sort->kind == smtutil::Kind::Tuple, "");
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < lComponents.size(); ++i)
|
|
|
|
if (auto component = lComponents.at(i); component && rComponents.at(i))
|
2020-08-25 17:28:31 +00:00
|
|
|
assignment(*component, smtutil::Expression::tuple_get(symbRight, i), component->annotation().type);
|
2020-05-27 17:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression SMTEncoder::compoundAssignment(Assignment const& _assignment)
|
2019-04-29 09:39:24 +00:00
|
|
|
{
|
|
|
|
static map<Token, Token> const compoundToArithmetic{
|
|
|
|
{Token::AssignAdd, Token::Add},
|
|
|
|
{Token::AssignSub, Token::Sub},
|
|
|
|
{Token::AssignMul, Token::Mul},
|
|
|
|
{Token::AssignDiv, Token::Div},
|
|
|
|
{Token::AssignMod, Token::Mod}
|
|
|
|
};
|
2020-09-21 14:54:58 +00:00
|
|
|
static map<Token, Token> const compoundToBitwise{
|
|
|
|
{Token::AssignBitAnd, Token::BitAnd},
|
|
|
|
{Token::AssignBitOr, Token::BitOr},
|
2020-09-22 09:15:56 +00:00
|
|
|
{Token::AssignBitXor, Token::BitXor},
|
|
|
|
{Token::AssignShl, Token::SHL},
|
|
|
|
{Token::AssignShr, Token::SHR},
|
|
|
|
{Token::AssignSar, Token::SAR}
|
2020-09-21 14:54:58 +00:00
|
|
|
};
|
2019-04-29 09:39:24 +00:00
|
|
|
Token op = _assignment.assignmentOperator();
|
2020-09-21 14:54:58 +00:00
|
|
|
solAssert(compoundToArithmetic.count(op) || compoundToBitwise.count(op), "");
|
|
|
|
|
2019-04-29 09:39:24 +00:00
|
|
|
auto decl = identifierToVariable(_assignment.leftHandSide());
|
2020-09-21 14:54:58 +00:00
|
|
|
|
|
|
|
if (compoundToBitwise.count(op))
|
|
|
|
return bitwiseOperation(
|
|
|
|
compoundToBitwise.at(op),
|
|
|
|
decl ? currentValue(*decl) : expr(_assignment.leftHandSide()),
|
|
|
|
expr(_assignment.rightHandSide()),
|
|
|
|
_assignment.annotation().type
|
|
|
|
);
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
auto values = arithmeticOperation(
|
2019-04-29 09:39:24 +00:00
|
|
|
compoundToArithmetic.at(op),
|
|
|
|
decl ? currentValue(*decl) : expr(_assignment.leftHandSide()),
|
|
|
|
expr(_assignment.rightHandSide()),
|
|
|
|
_assignment.annotation().type,
|
2019-06-24 15:37:03 +00:00
|
|
|
_assignment
|
2019-04-29 09:39:24 +00:00
|
|
|
);
|
2019-06-25 10:46:17 +00:00
|
|
|
return values.first;
|
2019-04-29 09:39:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::assignment(VariableDeclaration const& _variable, Expression const& _value)
|
2017-09-28 11:44:56 +00:00
|
|
|
{
|
2020-04-14 09:09:38 +00:00
|
|
|
// In general, at this point, the SMT sorts of _variable and _value are the same,
|
|
|
|
// even if there is implicit conversion.
|
|
|
|
// This is a special case where the SMT sorts are different.
|
|
|
|
// For now we are unaware of other cases where this happens, but if they do appear
|
|
|
|
// we should extract this into an `implicitConversion` function.
|
2020-09-25 09:34:18 +00:00
|
|
|
assignment(_variable, expr(_value, _variable.type()));
|
2017-10-05 13:23:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
void SMTEncoder::assignment(VariableDeclaration const& _variable, smtutil::Expression const& _value)
|
2017-10-05 13:23:25 +00:00
|
|
|
{
|
|
|
|
TypePointer type = _variable.type();
|
2019-06-24 15:58:56 +00:00
|
|
|
if (type->category() == Type::Category::Mapping)
|
2018-12-14 11:21:43 +00:00
|
|
|
arrayAssignment();
|
2019-06-24 15:37:03 +00:00
|
|
|
m_context.addAssertion(m_context.newValue(_variable) == _value);
|
2017-09-28 11:44:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
SMTEncoder::VariableIndices SMTEncoder::visitBranch(ASTNode const* _statement, smtutil::Expression _condition)
|
2017-09-28 13:24:24 +00:00
|
|
|
{
|
2017-12-18 18:43:15 +00:00
|
|
|
return visitBranch(_statement, &_condition);
|
2017-09-28 13:24:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
SMTEncoder::VariableIndices SMTEncoder::visitBranch(ASTNode const* _statement, smtutil::Expression const* _condition)
|
2017-09-28 11:44:56 +00:00
|
|
|
{
|
2018-10-17 09:32:01 +00:00
|
|
|
auto indicesBeforeBranch = copyVariableIndices();
|
2017-09-28 11:44:56 +00:00
|
|
|
if (_condition)
|
2017-12-11 19:09:17 +00:00
|
|
|
pushPathCondition(*_condition);
|
2019-03-22 16:05:58 +00:00
|
|
|
_statement->accept(*this);
|
2017-12-11 19:09:17 +00:00
|
|
|
if (_condition)
|
|
|
|
popPathCondition();
|
2018-10-17 09:32:01 +00:00
|
|
|
auto indicesAfterBranch = copyVariableIndices();
|
|
|
|
resetVariableIndices(indicesBeforeBranch);
|
|
|
|
return indicesAfterBranch;
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
void SMTEncoder::initializeFunctionCallParameters(CallableDeclaration const& _function, vector<smtutil::Expression> const& _callArgs)
|
2018-10-10 12:31:49 +00:00
|
|
|
{
|
|
|
|
auto const& funParams = _function.parameters();
|
|
|
|
solAssert(funParams.size() == _callArgs.size(), "");
|
|
|
|
for (unsigned i = 0; i < funParams.size(); ++i)
|
|
|
|
if (createVariable(*funParams[i]))
|
2018-12-14 11:21:43 +00:00
|
|
|
{
|
2019-06-24 15:37:03 +00:00
|
|
|
m_context.addAssertion(_callArgs[i] == m_context.newValue(*funParams[i]));
|
2018-12-14 11:21:43 +00:00
|
|
|
if (funParams[i]->annotation().type->category() == Type::Category::Mapping)
|
|
|
|
m_arrayAssignmentHappened = true;
|
|
|
|
}
|
2018-10-10 12:31:49 +00:00
|
|
|
|
|
|
|
for (auto const& variable: _function.localVariables())
|
|
|
|
if (createVariable(*variable))
|
|
|
|
{
|
2019-05-10 14:28:29 +00:00
|
|
|
m_context.newValue(*variable);
|
|
|
|
m_context.setZeroValue(*variable);
|
2018-10-10 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_function.returnParameterList())
|
|
|
|
for (auto const& retParam: _function.returnParameters())
|
|
|
|
if (createVariable(*retParam))
|
|
|
|
{
|
2019-05-10 14:28:29 +00:00
|
|
|
m_context.newValue(*retParam);
|
|
|
|
m_context.setZeroValue(*retParam);
|
2018-10-10 12:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 14:12:27 +00:00
|
|
|
void SMTEncoder::createStateVariables(ContractDefinition const& _contract)
|
2019-08-02 16:36:26 +00:00
|
|
|
{
|
|
|
|
for (auto var: _contract.stateVariablesIncludingInherited())
|
|
|
|
createVariable(*var);
|
|
|
|
}
|
|
|
|
|
2019-09-26 14:12:27 +00:00
|
|
|
void SMTEncoder::initializeStateVariables(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
for (auto var: _contract.stateVariables())
|
|
|
|
{
|
|
|
|
solAssert(m_context.knownVariable(*var), "");
|
|
|
|
m_context.setZeroValue(*var);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto var: _contract.stateVariables())
|
|
|
|
if (var->value())
|
|
|
|
{
|
|
|
|
var->value()->accept(*this);
|
|
|
|
assignment(*var, *var->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTEncoder::createLocalVariables(FunctionDefinition const& _function)
|
|
|
|
{
|
|
|
|
for (auto const& variable: _function.localVariables())
|
|
|
|
createVariable(*variable);
|
|
|
|
|
|
|
|
for (auto const& param: _function.parameters())
|
|
|
|
createVariable(*param);
|
|
|
|
|
|
|
|
if (_function.returnParameterList())
|
|
|
|
for (auto const& retParam: _function.returnParameters())
|
|
|
|
createVariable(*retParam);
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::initializeLocalVariables(FunctionDefinition const& _function)
|
2017-09-28 13:24:24 +00:00
|
|
|
{
|
|
|
|
for (auto const& variable: _function.localVariables())
|
2019-09-26 14:12:27 +00:00
|
|
|
{
|
|
|
|
solAssert(m_context.knownVariable(*variable), "");
|
|
|
|
m_context.setZeroValue(*variable);
|
|
|
|
}
|
2017-10-05 17:31:17 +00:00
|
|
|
|
2017-09-28 13:24:24 +00:00
|
|
|
for (auto const& param: _function.parameters())
|
2019-09-26 14:12:27 +00:00
|
|
|
{
|
|
|
|
solAssert(m_context.knownVariable(*param), "");
|
|
|
|
m_context.setUnknownValue(*param);
|
|
|
|
}
|
2017-10-05 17:31:17 +00:00
|
|
|
|
2017-09-28 13:24:24 +00:00
|
|
|
if (_function.returnParameterList())
|
|
|
|
for (auto const& retParam: _function.returnParameters())
|
2019-09-26 14:12:27 +00:00
|
|
|
{
|
|
|
|
solAssert(m_context.knownVariable(*retParam), "");
|
|
|
|
m_context.setZeroValue(*retParam);
|
|
|
|
}
|
2018-12-20 17:11:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::resetStateVariables()
|
2018-04-18 21:14:45 +00:00
|
|
|
{
|
2019-05-10 14:28:29 +00:00
|
|
|
m_context.resetVariables([&](VariableDeclaration const& _variable) { return _variable.isStateVariable(); });
|
2018-12-20 17:11:20 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 12:54:52 +00:00
|
|
|
void SMTEncoder::resetReferences(VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
m_context.resetVariables([&](VariableDeclaration const& _var) {
|
|
|
|
if (_var == _varDecl)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If both are state variables no need to clear knowledge.
|
|
|
|
if (_var.isStateVariable() && _varDecl.isStateVariable())
|
|
|
|
return false;
|
|
|
|
|
2020-08-25 17:28:31 +00:00
|
|
|
return sameTypeOrSubtype(_var.type(), _varDecl.type());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTEncoder::resetReferences(TypePointer _type)
|
|
|
|
{
|
|
|
|
m_context.resetVariables([&](VariableDeclaration const& _var) {
|
|
|
|
return sameTypeOrSubtype(_var.type(), _type);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SMTEncoder::sameTypeOrSubtype(TypePointer _a, TypePointer _b)
|
|
|
|
{
|
|
|
|
TypePointer prefix = _a;
|
|
|
|
while (
|
|
|
|
prefix->category() == Type::Category::Mapping ||
|
|
|
|
prefix->category() == Type::Category::Array
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (*typeWithoutPointer(_b) == *typeWithoutPointer(prefix))
|
|
|
|
return true;
|
|
|
|
if (prefix->category() == Type::Category::Mapping)
|
2020-06-05 12:54:52 +00:00
|
|
|
{
|
2020-08-25 17:28:31 +00:00
|
|
|
auto mapPrefix = dynamic_cast<MappingType const*>(prefix);
|
|
|
|
solAssert(mapPrefix, "");
|
|
|
|
prefix = mapPrefix->valueType();
|
2020-06-05 12:54:52 +00:00
|
|
|
}
|
2020-08-25 17:28:31 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto arrayPrefix = dynamic_cast<ArrayType const*>(prefix);
|
|
|
|
solAssert(arrayPrefix, "");
|
|
|
|
prefix = arrayPrefix->baseType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2020-06-05 12:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
TypePointer SMTEncoder::typeWithoutPointer(TypePointer const& _type)
|
2019-02-21 12:01:58 +00:00
|
|
|
{
|
2019-04-15 13:33:39 +00:00
|
|
|
if (auto refType = dynamic_cast<ReferenceType const*>(_type))
|
2019-04-17 11:25:03 +00:00
|
|
|
return TypeProvider::withLocationIfReference(refType->location(), _type);
|
2019-02-21 12:01:58 +00:00
|
|
|
return _type;
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
void SMTEncoder::mergeVariables(set<VariableDeclaration const*> const& _variables, smtutil::Expression const& _condition, VariableIndices const& _indicesEndTrue, VariableIndices const& _indicesEndFalse)
|
2019-03-22 16:05:58 +00:00
|
|
|
{
|
2019-05-08 15:33:18 +00:00
|
|
|
auto cmp = [] (VariableDeclaration const* var1, VariableDeclaration const* var2) {
|
|
|
|
return var1->id() < var2->id();
|
|
|
|
};
|
|
|
|
set<VariableDeclaration const*, decltype(cmp)> sortedVars(begin(_variables), end(_variables), cmp);
|
2019-07-30 10:18:03 +00:00
|
|
|
|
|
|
|
/// Knowledge about references is erased if a reference is assigned,
|
|
|
|
/// so those also need their SSA's merged.
|
|
|
|
/// This does not cause scope harm since the symbolic variables
|
|
|
|
/// are kept alive.
|
|
|
|
for (auto const& var: _indicesEndTrue)
|
|
|
|
{
|
|
|
|
solAssert(_indicesEndFalse.count(var.first), "");
|
|
|
|
if (
|
|
|
|
_indicesEndFalse.at(var.first) != var.second &&
|
|
|
|
!sortedVars.count(var.first)
|
|
|
|
)
|
|
|
|
sortedVars.insert(var.first);
|
|
|
|
}
|
|
|
|
|
2019-05-08 15:33:18 +00:00
|
|
|
for (auto const* decl: sortedVars)
|
2017-12-18 18:43:15 +00:00
|
|
|
{
|
2018-10-17 09:32:01 +00:00
|
|
|
solAssert(_indicesEndTrue.count(decl) && _indicesEndFalse.count(decl), "");
|
2020-06-02 13:42:46 +00:00
|
|
|
auto trueIndex = static_cast<unsigned>(_indicesEndTrue.at(decl));
|
|
|
|
auto falseIndex = static_cast<unsigned>(_indicesEndFalse.at(decl));
|
2018-10-17 09:32:01 +00:00
|
|
|
solAssert(trueIndex != falseIndex, "");
|
2020-05-19 12:14:46 +00:00
|
|
|
m_context.addAssertion(m_context.newValue(*decl) == smtutil::Expression::ite(
|
2017-12-18 18:43:15 +00:00
|
|
|
_condition,
|
2018-10-17 09:32:01 +00:00
|
|
|
valueAtIndex(*decl, trueIndex),
|
|
|
|
valueAtIndex(*decl, falseIndex))
|
2017-12-18 18:43:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression SMTEncoder::currentValue(VariableDeclaration const& _decl)
|
2019-05-10 14:28:29 +00:00
|
|
|
{
|
|
|
|
solAssert(m_context.knownVariable(_decl), "");
|
|
|
|
return m_context.variable(_decl)->currentValue();
|
|
|
|
}
|
|
|
|
|
2020-06-02 13:42:46 +00:00
|
|
|
smtutil::Expression SMTEncoder::valueAtIndex(VariableDeclaration const& _decl, unsigned _index)
|
2019-05-10 14:28:29 +00:00
|
|
|
{
|
|
|
|
solAssert(m_context.knownVariable(_decl), "");
|
|
|
|
return m_context.variable(_decl)->valueAtIndex(_index);
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::createVariable(VariableDeclaration const& _varDecl)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-05-10 14:28:29 +00:00
|
|
|
if (m_context.knownVariable(_varDecl))
|
2018-10-10 12:31:49 +00:00
|
|
|
return true;
|
2019-05-10 14:28:29 +00:00
|
|
|
bool abstract = m_context.createVariable(_varDecl);
|
|
|
|
if (abstract)
|
2017-10-05 17:31:17 +00:00
|
|
|
{
|
2017-07-11 11:26:43 +00:00
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
8115_error,
|
2017-07-11 11:26:43 +00:00
|
|
|
_varDecl.location(),
|
|
|
|
"Assertion checker does not yet support the type of this variable."
|
|
|
|
);
|
2017-10-05 17:31:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-17 16:00:42 +00:00
|
|
|
return true;
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression SMTEncoder::expr(Expression const& _e, TypePointer _targetType)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2019-05-13 14:53:38 +00:00
|
|
|
if (!m_context.knownExpression(_e))
|
2017-10-05 17:39:29 +00:00
|
|
|
{
|
2020-05-05 22:38:28 +00:00
|
|
|
m_errorReporter.warning(6031_error, _e.location(), "Internal error: Expression undefined for SMT solver." );
|
2017-10-05 17:39:29 +00:00
|
|
|
createExpr(_e);
|
|
|
|
}
|
2019-08-07 08:48:09 +00:00
|
|
|
|
|
|
|
return m_context.expression(_e)->currentValue(_targetType);
|
2018-10-10 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::createExpr(Expression const& _e)
|
2017-10-05 13:23:25 +00:00
|
|
|
{
|
2019-05-13 14:53:38 +00:00
|
|
|
bool abstract = m_context.createExpression(_e);
|
|
|
|
if (abstract)
|
|
|
|
m_errorReporter.warning(
|
2020-05-05 22:38:28 +00:00
|
|
|
8364_error,
|
2019-05-13 14:53:38 +00:00
|
|
|
_e.location(),
|
2019-07-01 14:25:33 +00:00
|
|
|
"Assertion checker does not yet implement type " + _e.annotation().type->toString()
|
2019-05-13 14:53:38 +00:00
|
|
|
);
|
2017-10-05 13:23:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
void SMTEncoder::defineExpr(Expression const& _e, smtutil::Expression _value)
|
2017-10-05 13:23:25 +00:00
|
|
|
{
|
|
|
|
createExpr(_e);
|
2020-05-19 12:14:46 +00:00
|
|
|
solAssert(_value.sort->kind != smtutil::Kind::Function, "Equality operator applied to type that is not fully supported");
|
2019-06-24 15:37:03 +00:00
|
|
|
m_context.addAssertion(expr(_e) == _value);
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::popPathCondition()
|
2017-12-11 19:09:17 +00:00
|
|
|
{
|
|
|
|
solAssert(m_pathConditions.size() > 0, "Cannot pop path condition, empty.");
|
|
|
|
m_pathConditions.pop_back();
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
void SMTEncoder::pushPathCondition(smtutil::Expression const& _e)
|
2017-12-11 19:09:17 +00:00
|
|
|
{
|
|
|
|
m_pathConditions.push_back(currentPathConditions() && _e);
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression SMTEncoder::currentPathConditions()
|
2017-12-11 19:09:17 +00:00
|
|
|
{
|
2018-10-09 03:29:37 +00:00
|
|
|
if (m_pathConditions.empty())
|
2020-05-19 12:14:46 +00:00
|
|
|
return smtutil::Expression(true);
|
2017-12-11 19:09:17 +00:00
|
|
|
return m_pathConditions.back();
|
|
|
|
}
|
2017-12-13 16:59:36 +00:00
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
SecondarySourceLocation SMTEncoder::callStackMessage(vector<CallStackEntry> const& _callStack)
|
2019-03-18 12:04:08 +00:00
|
|
|
{
|
|
|
|
SecondarySourceLocation callStackLocation;
|
2019-06-24 15:37:03 +00:00
|
|
|
solAssert(!_callStack.empty(), "");
|
2019-03-18 12:04:08 +00:00
|
|
|
callStackLocation.append("Callstack: ", SourceLocation());
|
2019-06-24 15:37:03 +00:00
|
|
|
for (auto const& call: _callStack | boost::adaptors::reversed)
|
2019-05-06 18:03:11 +00:00
|
|
|
if (call.second)
|
|
|
|
callStackLocation.append("", call.second->location());
|
2019-03-18 12:04:08 +00:00
|
|
|
return callStackLocation;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
pair<CallableDeclaration const*, ASTNode const*> SMTEncoder::popCallStack()
|
2019-03-11 20:06:28 +00:00
|
|
|
{
|
|
|
|
solAssert(!m_callStack.empty(), "");
|
2019-05-06 18:03:11 +00:00
|
|
|
auto lastCalled = m_callStack.back();
|
2019-03-11 20:06:28 +00:00
|
|
|
m_callStack.pop_back();
|
|
|
|
return lastCalled;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::pushCallStack(CallStackEntry _entry)
|
2019-03-11 20:06:28 +00:00
|
|
|
{
|
2019-05-06 18:03:11 +00:00
|
|
|
m_callStack.push_back(_entry);
|
2019-03-11 20:06:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
void SMTEncoder::addPathImpliedExpression(smtutil::Expression const& _e)
|
2017-12-13 16:59:36 +00:00
|
|
|
{
|
2020-05-19 12:14:46 +00:00
|
|
|
m_context.addAssertion(smtutil::Expression::implies(currentPathConditions(), _e));
|
2017-12-13 16:59:36 +00:00
|
|
|
}
|
2018-06-05 09:46:21 +00:00
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::isRootFunction()
|
2018-06-05 09:46:21 +00:00
|
|
|
{
|
2019-05-06 18:03:11 +00:00
|
|
|
return m_callStack.size() == 1;
|
2018-10-10 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
bool SMTEncoder::visitedFunction(FunctionDefinition const* _funDef)
|
2018-10-10 12:31:49 +00:00
|
|
|
{
|
2019-05-06 18:03:11 +00:00
|
|
|
for (auto const& call: m_callStack)
|
|
|
|
if (call.first == _funDef)
|
|
|
|
return true;
|
|
|
|
return false;
|
2018-06-05 09:46:21 +00:00
|
|
|
}
|
2018-10-15 15:32:17 +00:00
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
SMTEncoder::VariableIndices SMTEncoder::copyVariableIndices()
|
2018-10-15 15:32:17 +00:00
|
|
|
{
|
2018-10-17 09:32:01 +00:00
|
|
|
VariableIndices indices;
|
2019-05-10 14:28:29 +00:00
|
|
|
for (auto const& var: m_context.variables())
|
2018-10-17 09:32:01 +00:00
|
|
|
indices.emplace(var.first, var.second->index());
|
|
|
|
return indices;
|
2018-10-15 15:32:17 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
void SMTEncoder::resetVariableIndices(VariableIndices const& _indices)
|
2018-10-15 15:32:17 +00:00
|
|
|
{
|
2018-10-17 09:32:01 +00:00
|
|
|
for (auto const& var: _indices)
|
2020-06-02 13:42:46 +00:00
|
|
|
m_context.variable(*var.first)->setIndex(static_cast<unsigned>(var.second));
|
2018-10-15 15:32:17 +00:00
|
|
|
}
|
2019-03-25 12:58:22 +00:00
|
|
|
|
2019-09-26 14:12:27 +00:00
|
|
|
void SMTEncoder::clearIndices(ContractDefinition const* _contract, FunctionDefinition const* _function)
|
|
|
|
{
|
|
|
|
solAssert(_contract, "");
|
|
|
|
for (auto var: _contract->stateVariablesIncludingInherited())
|
|
|
|
m_context.variable(*var)->resetIndex();
|
|
|
|
if (_function)
|
|
|
|
{
|
|
|
|
for (auto const& var: _function->parameters() + _function->returnParameters())
|
|
|
|
m_context.variable(*var)->resetIndex();
|
|
|
|
for (auto const& var: _function->localVariables())
|
|
|
|
m_context.variable(*var)->resetIndex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
Expression const* SMTEncoder::leftmostBase(IndexAccess const& _indexAccess)
|
2019-05-09 14:06:13 +00:00
|
|
|
{
|
|
|
|
Expression const* base = &_indexAccess.baseExpression();
|
|
|
|
while (auto access = dynamic_cast<IndexAccess const*>(base))
|
|
|
|
base = &access->baseExpression();
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2020-08-31 10:59:11 +00:00
|
|
|
Expression const* SMTEncoder::innermostTuple(Expression const& _expr)
|
2020-07-15 16:56:28 +00:00
|
|
|
{
|
2020-08-31 10:59:11 +00:00
|
|
|
auto const* tuple = dynamic_cast<TupleExpression const*>(&_expr);
|
|
|
|
if (!tuple || tuple->isInlineArray())
|
|
|
|
return &_expr;
|
|
|
|
|
2020-07-15 16:56:28 +00:00
|
|
|
Expression const* expr = tuple;
|
|
|
|
while (tuple && !tuple->isInlineArray() && tuple->components().size() == 1)
|
|
|
|
{
|
|
|
|
expr = tuple->components().front().get();
|
|
|
|
tuple = dynamic_cast<TupleExpression const*>(expr);
|
|
|
|
}
|
|
|
|
solAssert(expr, "");
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
set<VariableDeclaration const*> SMTEncoder::touchedVariables(ASTNode const& _node)
|
2019-04-01 09:10:28 +00:00
|
|
|
{
|
2019-05-06 18:03:11 +00:00
|
|
|
vector<CallableDeclaration const*> callStack;
|
|
|
|
for (auto const& call: m_callStack)
|
|
|
|
callStack.push_back(call.first);
|
|
|
|
return m_variableUsage.touchedVariables(_node, callStack);
|
2019-04-01 09:10:28 +00:00
|
|
|
}
|
2019-04-29 09:39:24 +00:00
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
VariableDeclaration const* SMTEncoder::identifierToVariable(Expression const& _expr)
|
2019-04-29 09:39:24 +00:00
|
|
|
{
|
|
|
|
if (auto identifier = dynamic_cast<Identifier const*>(&_expr))
|
|
|
|
{
|
|
|
|
if (auto decl = dynamic_cast<VariableDeclaration const*>(identifier->annotation().referencedDeclaration))
|
|
|
|
{
|
2019-05-10 14:28:29 +00:00
|
|
|
solAssert(m_context.knownVariable(*decl), "");
|
2019-04-29 09:39:24 +00:00
|
|
|
return decl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-06-25 10:46:17 +00:00
|
|
|
|
|
|
|
string SMTEncoder::extraComment()
|
|
|
|
{
|
|
|
|
string extra;
|
|
|
|
if (m_arrayAssignmentHappened)
|
|
|
|
extra +=
|
|
|
|
"\nNote that array aliasing is not supported,"
|
|
|
|
" therefore all mapping information is erased after"
|
|
|
|
" a mapping local variable/parameter is assigned.\n"
|
|
|
|
"You can re-introduce information using require().";
|
|
|
|
return extra;
|
|
|
|
}
|
2019-07-17 15:54:48 +00:00
|
|
|
|
|
|
|
FunctionDefinition const* SMTEncoder::functionCallToDefinition(FunctionCall const& _funCall)
|
|
|
|
{
|
2020-04-08 17:38:30 +00:00
|
|
|
if (*_funCall.annotation().kind != FunctionCallKind::FunctionCall)
|
2019-07-17 15:54:48 +00:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
FunctionDefinition const* funDef = nullptr;
|
|
|
|
Expression const* calledExpr = &_funCall.expression();
|
|
|
|
|
|
|
|
if (TupleExpression const* fun = dynamic_cast<TupleExpression const*>(&_funCall.expression()))
|
|
|
|
{
|
|
|
|
solAssert(fun->components().size() == 1, "");
|
|
|
|
calledExpr = fun->components().front().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Identifier const* fun = dynamic_cast<Identifier const*>(calledExpr))
|
|
|
|
funDef = dynamic_cast<FunctionDefinition const*>(fun->annotation().referencedDeclaration);
|
|
|
|
else if (MemberAccess const* fun = dynamic_cast<MemberAccess const*>(calledExpr))
|
|
|
|
funDef = dynamic_cast<FunctionDefinition const*>(fun->annotation().referencedDeclaration);
|
|
|
|
|
|
|
|
return funDef;
|
|
|
|
}
|
2019-08-02 18:43:54 +00:00
|
|
|
|
2020-08-19 11:51:58 +00:00
|
|
|
vector<VariableDeclaration const*> SMTEncoder::stateVariablesIncludingInheritedAndPrivate(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
return fold(
|
|
|
|
_contract.annotation().linearizedBaseContracts,
|
|
|
|
vector<VariableDeclaration const*>{},
|
|
|
|
[](auto&& _acc, auto _contract) { return _acc + _contract->stateVariables(); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<VariableDeclaration const*> SMTEncoder::stateVariablesIncludingInheritedAndPrivate(FunctionDefinition const& _function)
|
|
|
|
{
|
|
|
|
return stateVariablesIncludingInheritedAndPrivate(dynamic_cast<ContractDefinition const&>(*_function.scope()));
|
|
|
|
}
|
|
|
|
|
2020-09-02 08:45:47 +00:00
|
|
|
SourceUnit const* SMTEncoder::sourceUnitContaining(Scopable const& _scopable)
|
|
|
|
{
|
|
|
|
for (auto const* s = &_scopable; s; s = dynamic_cast<Scopable const*>(s->scope()))
|
|
|
|
if (auto const* source = dynamic_cast<SourceUnit const*>(s->scope()))
|
|
|
|
return source;
|
|
|
|
solAssert(false, "");
|
|
|
|
}
|
|
|
|
|
2019-08-02 18:43:54 +00:00
|
|
|
void SMTEncoder::createReturnedExpressions(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
FunctionDefinition const* funDef = functionCallToDefinition(_funCall);
|
|
|
|
if (!funDef)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto const& returnParams = funDef->returnParameters();
|
|
|
|
for (auto param: returnParams)
|
|
|
|
createVariable(*param);
|
|
|
|
|
|
|
|
if (returnParams.size() > 1)
|
|
|
|
{
|
|
|
|
auto const& symbTuple = dynamic_pointer_cast<smt::SymbolicTupleVariable>(m_context.expression(_funCall));
|
|
|
|
solAssert(symbTuple, "");
|
2019-11-15 13:48:11 +00:00
|
|
|
auto const& symbComponents = symbTuple->components();
|
|
|
|
solAssert(symbComponents.size() == returnParams.size(), "");
|
|
|
|
for (unsigned i = 0; i < symbComponents.size(); ++i)
|
2019-08-02 18:43:54 +00:00
|
|
|
{
|
2019-11-15 13:48:11 +00:00
|
|
|
auto param = returnParams.at(i);
|
|
|
|
solAssert(param, "");
|
2020-04-03 13:10:16 +00:00
|
|
|
solAssert(m_context.knownVariable(*param), "");
|
|
|
|
m_context.addAssertion(symbTuple->component(i) == currentValue(*param));
|
2019-08-02 18:43:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (returnParams.size() == 1)
|
|
|
|
defineExpr(_funCall, currentValue(*returnParams.front()));
|
|
|
|
}
|
2020-02-12 01:11:28 +00:00
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
vector<smtutil::Expression> SMTEncoder::symbolicArguments(FunctionCall const& _funCall)
|
2020-02-12 01:11:28 +00:00
|
|
|
{
|
|
|
|
auto const* function = functionCallToDefinition(_funCall);
|
|
|
|
solAssert(function, "");
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
vector<smtutil::Expression> args;
|
2020-02-12 01:11:28 +00:00
|
|
|
Expression const* calledExpr = &_funCall.expression();
|
|
|
|
auto const& funType = dynamic_cast<FunctionType const*>(calledExpr->annotation().type);
|
|
|
|
solAssert(funType, "");
|
|
|
|
|
|
|
|
auto const& functionParams = function->parameters();
|
|
|
|
auto const& arguments = _funCall.arguments();
|
|
|
|
unsigned firstParam = 0;
|
|
|
|
if (funType->bound())
|
|
|
|
{
|
|
|
|
auto const& boundFunction = dynamic_cast<MemberAccess const*>(calledExpr);
|
|
|
|
solAssert(boundFunction, "");
|
|
|
|
args.push_back(expr(boundFunction->expression(), functionParams.front()->type()));
|
|
|
|
firstParam = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
solAssert((arguments.size() + firstParam) == functionParams.size(), "");
|
|
|
|
for (unsigned i = 0; i < arguments.size(); ++i)
|
|
|
|
args.push_back(expr(*arguments.at(i), functionParams.at(i + firstParam)->type()));
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|