2018-02-06 09:57:16 +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
|
2018-02-06 09:57:16 +00:00
|
|
|
/**
|
|
|
|
* Yul interpreter.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/tools/yulInterpreter/Interpreter.h>
|
|
|
|
|
|
|
|
#include <test/tools/yulInterpreter/EVMInstructionInterpreter.h>
|
2019-12-12 12:57:07 +00:00
|
|
|
#include <test/tools/yulInterpreter/EwasmBuiltinInterpreter.h>
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
#include <libyul/AsmData.h>
|
2019-04-30 13:02:52 +00:00
|
|
|
#include <libyul/Dialect.h>
|
2018-02-06 09:57:16 +00:00
|
|
|
#include <libyul/Utilities.h>
|
2019-04-30 13:02:52 +00:00
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
2019-11-12 16:08:23 +00:00
|
|
|
#include <libyul/backends/wasm/WasmDialect.h>
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/FixedHash.h>
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
2019-05-21 09:46:37 +00:00
|
|
|
#include <boost/algorithm/cxx11/all_of.hpp>
|
|
|
|
|
|
|
|
#include <ostream>
|
2019-11-19 15:42:49 +00:00
|
|
|
#include <variant>
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::yul::test;
|
|
|
|
|
|
|
|
using solidity::util::h256;
|
2018-02-06 09:57:16 +00:00
|
|
|
|
2019-05-21 09:46:37 +00:00
|
|
|
void InterpreterState::dumpTraceAndState(ostream& _out) const
|
|
|
|
{
|
|
|
|
_out << "Trace:" << endl;
|
|
|
|
for (auto const& line: trace)
|
|
|
|
_out << " " << line << endl;
|
|
|
|
_out << "Memory dump:\n";
|
2019-09-02 10:06:29 +00:00
|
|
|
map<u256, u256> words;
|
|
|
|
for (auto const& [offset, value]: memory)
|
2020-06-02 13:46:37 +00:00
|
|
|
words[(offset / 0x20) * 0x20] |= u256(uint32_t(value)) << (256 - 8 - 8 * static_cast<size_t>(offset % 0x20));
|
2019-09-02 10:06:29 +00:00
|
|
|
for (auto const& [offset, value]: words)
|
|
|
|
if (value != 0)
|
2019-10-24 14:37:18 +00:00
|
|
|
_out << " " << std::uppercase << std::hex << std::setw(4) << offset << ": " << h256(value).hex() << endl;
|
2019-05-21 09:46:37 +00:00
|
|
|
_out << "Storage dump:" << endl;
|
|
|
|
for (auto const& slot: storage)
|
2019-12-14 13:38:44 +00:00
|
|
|
if (slot.second != h256{})
|
2019-05-21 09:46:37 +00:00
|
|
|
_out << " " << slot.first.hex() << ": " << slot.second.hex() << endl;
|
|
|
|
}
|
2018-02-06 09:57:16 +00:00
|
|
|
|
2020-07-09 12:24:49 +00:00
|
|
|
void Interpreter::run(InterpreterState& _state, Dialect const& _dialect, Block const& _ast)
|
|
|
|
{
|
|
|
|
Scope scope;
|
|
|
|
Interpreter{_state, _dialect, scope}(_ast);
|
|
|
|
}
|
|
|
|
|
2018-02-06 09:57:16 +00:00
|
|
|
void Interpreter::operator()(ExpressionStatement const& _expressionStatement)
|
|
|
|
{
|
|
|
|
evaluateMulti(_expressionStatement.expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::operator()(Assignment const& _assignment)
|
|
|
|
{
|
|
|
|
solAssert(_assignment.value, "");
|
|
|
|
vector<u256> values = evaluateMulti(*_assignment.value);
|
|
|
|
solAssert(values.size() == _assignment.variableNames.size(), "");
|
|
|
|
for (size_t i = 0; i < values.size(); ++i)
|
|
|
|
{
|
|
|
|
YulString varName = _assignment.variableNames.at(i).name;
|
|
|
|
solAssert(m_variables.count(varName), "");
|
|
|
|
m_variables[varName] = values.at(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::operator()(VariableDeclaration const& _declaration)
|
|
|
|
{
|
|
|
|
vector<u256> values(_declaration.variables.size(), 0);
|
|
|
|
if (_declaration.value)
|
|
|
|
values = evaluateMulti(*_declaration.value);
|
|
|
|
|
|
|
|
solAssert(values.size() == _declaration.variables.size(), "");
|
|
|
|
for (size_t i = 0; i < values.size(); ++i)
|
|
|
|
{
|
|
|
|
YulString varName = _declaration.variables.at(i).name;
|
|
|
|
solAssert(!m_variables.count(varName), "");
|
|
|
|
m_variables[varName] = values.at(i);
|
2020-07-09 12:24:49 +00:00
|
|
|
m_scope->names.emplace(varName, nullptr);
|
2018-02-06 09:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::operator()(If const& _if)
|
|
|
|
{
|
|
|
|
solAssert(_if.condition, "");
|
|
|
|
if (evaluate(*_if.condition) != 0)
|
|
|
|
(*this)(_if.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::operator()(Switch const& _switch)
|
|
|
|
{
|
|
|
|
solAssert(_switch.expression, "");
|
|
|
|
u256 val = evaluate(*_switch.expression);
|
|
|
|
solAssert(!_switch.cases.empty(), "");
|
|
|
|
for (auto const& c: _switch.cases)
|
|
|
|
// Default case has to be last.
|
|
|
|
if (!c.value || evaluate(*c.value) == val)
|
|
|
|
{
|
|
|
|
(*this)(c.body);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::operator()(FunctionDefinition const&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::operator()(ForLoop const& _forLoop)
|
|
|
|
{
|
|
|
|
solAssert(_forLoop.condition, "");
|
|
|
|
|
2020-07-09 12:24:49 +00:00
|
|
|
enterScope(_forLoop.pre);
|
|
|
|
ScopeGuard g([this]{ leaveScope(); });
|
2019-10-28 14:25:02 +00:00
|
|
|
|
2018-02-06 09:57:16 +00:00
|
|
|
for (auto const& statement: _forLoop.pre.statements)
|
2019-10-28 14:25:02 +00:00
|
|
|
{
|
2018-02-06 09:57:16 +00:00
|
|
|
visit(statement);
|
2019-10-28 14:25:02 +00:00
|
|
|
if (m_state.controlFlowState == ControlFlowState::Leave)
|
|
|
|
return;
|
|
|
|
}
|
2018-02-06 09:57:16 +00:00
|
|
|
while (evaluate(*_forLoop.condition) != 0)
|
|
|
|
{
|
2020-07-21 12:37:22 +00:00
|
|
|
// Increment step for each loop iteration for loops with
|
|
|
|
// an empty body and post blocks to prevent a deadlock.
|
|
|
|
if (_forLoop.body.statements.size() == 0 && _forLoop.post.statements.size() == 0)
|
|
|
|
incrementStep();
|
|
|
|
|
2019-10-28 14:25:02 +00:00
|
|
|
m_state.controlFlowState = ControlFlowState::Default;
|
2018-02-06 09:57:16 +00:00
|
|
|
(*this)(_forLoop.body);
|
2019-10-28 14:25:02 +00:00
|
|
|
if (m_state.controlFlowState == ControlFlowState::Break || m_state.controlFlowState == ControlFlowState::Leave)
|
2019-03-04 14:38:05 +00:00
|
|
|
break;
|
|
|
|
|
2019-10-28 14:25:02 +00:00
|
|
|
m_state.controlFlowState = ControlFlowState::Default;
|
2018-02-06 09:57:16 +00:00
|
|
|
(*this)(_forLoop.post);
|
2019-10-28 14:25:02 +00:00
|
|
|
if (m_state.controlFlowState == ControlFlowState::Leave)
|
|
|
|
break;
|
2018-02-06 09:57:16 +00:00
|
|
|
}
|
2019-10-28 14:25:02 +00:00
|
|
|
if (m_state.controlFlowState != ControlFlowState::Leave)
|
|
|
|
m_state.controlFlowState = ControlFlowState::Default;
|
2018-02-06 09:57:16 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 14:38:05 +00:00
|
|
|
void Interpreter::operator()(Break const&)
|
|
|
|
{
|
2019-10-28 14:25:02 +00:00
|
|
|
m_state.controlFlowState = ControlFlowState::Break;
|
2019-03-04 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::operator()(Continue const&)
|
|
|
|
{
|
2019-10-28 14:25:02 +00:00
|
|
|
m_state.controlFlowState = ControlFlowState::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::operator()(Leave const&)
|
|
|
|
{
|
|
|
|
m_state.controlFlowState = ControlFlowState::Leave;
|
2019-03-04 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 09:57:16 +00:00
|
|
|
void Interpreter::operator()(Block const& _block)
|
|
|
|
{
|
2020-07-09 12:24:49 +00:00
|
|
|
enterScope(_block);
|
2018-02-06 09:57:16 +00:00
|
|
|
// Register functions.
|
|
|
|
for (auto const& statement: _block.statements)
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<FunctionDefinition>(statement))
|
2018-02-06 09:57:16 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
FunctionDefinition const& funDef = std::get<FunctionDefinition>(statement);
|
2020-07-09 12:24:49 +00:00
|
|
|
m_scope->names.emplace(funDef.name, &funDef);
|
2018-02-06 09:57:16 +00:00
|
|
|
}
|
2019-03-04 14:38:05 +00:00
|
|
|
|
|
|
|
for (auto const& statement: _block.statements)
|
|
|
|
{
|
2020-07-21 12:37:22 +00:00
|
|
|
incrementStep();
|
2019-03-04 14:38:05 +00:00
|
|
|
visit(statement);
|
2019-10-28 14:25:02 +00:00
|
|
|
if (m_state.controlFlowState != ControlFlowState::Default)
|
2019-03-04 14:38:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-09 12:24:49 +00:00
|
|
|
leaveScope();
|
2018-02-06 09:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u256 Interpreter::evaluate(Expression const& _expression)
|
|
|
|
{
|
2020-07-09 12:24:49 +00:00
|
|
|
ExpressionEvaluator ev(m_state, m_dialect, *m_scope, m_variables);
|
2018-02-06 09:57:16 +00:00
|
|
|
ev.visit(_expression);
|
|
|
|
return ev.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<u256> Interpreter::evaluateMulti(Expression const& _expression)
|
|
|
|
{
|
2020-07-09 12:24:49 +00:00
|
|
|
ExpressionEvaluator ev(m_state, m_dialect, *m_scope, m_variables);
|
2018-02-06 09:57:16 +00:00
|
|
|
ev.visit(_expression);
|
|
|
|
return ev.values();
|
|
|
|
}
|
|
|
|
|
2020-07-09 12:24:49 +00:00
|
|
|
void Interpreter::enterScope(Block const& _block)
|
|
|
|
{
|
|
|
|
if (!m_scope->subScopes.count(&_block))
|
|
|
|
m_scope->subScopes[&_block] = make_unique<Scope>(Scope{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
m_scope
|
|
|
|
});
|
|
|
|
m_scope = m_scope->subScopes[&_block].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::leaveScope()
|
2018-02-06 09:57:16 +00:00
|
|
|
{
|
2020-07-09 12:24:49 +00:00
|
|
|
for (auto const& [var, funDeclaration]: m_scope->names)
|
2019-08-28 14:18:05 +00:00
|
|
|
if (!funDeclaration)
|
2020-07-09 12:24:49 +00:00
|
|
|
m_variables.erase(var);
|
|
|
|
m_scope = m_scope->parent;
|
|
|
|
yulAssert(m_scope, "");
|
2018-02-06 09:57:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 12:37:22 +00:00
|
|
|
void Interpreter::incrementStep()
|
|
|
|
{
|
|
|
|
m_state.numSteps++;
|
|
|
|
if (m_state.maxSteps > 0 && m_state.numSteps >= m_state.maxSteps)
|
|
|
|
{
|
|
|
|
m_state.trace.emplace_back("Interpreter execution step limit reached.");
|
|
|
|
throw StepLimitReached();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 09:57:16 +00:00
|
|
|
void ExpressionEvaluator::operator()(Literal const& _literal)
|
|
|
|
{
|
|
|
|
static YulString const trueString("true");
|
|
|
|
static YulString const falseString("false");
|
|
|
|
|
2019-03-05 16:26:28 +00:00
|
|
|
setValue(valueOfLiteral(_literal));
|
2018-02-06 09:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionEvaluator::operator()(Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
solAssert(m_variables.count(_identifier.name), "");
|
|
|
|
setValue(m_variables.at(_identifier.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionEvaluator::operator()(FunctionCall const& _funCall)
|
|
|
|
{
|
2020-07-27 14:22:05 +00:00
|
|
|
vector<optional<LiteralKind>> const* literalArguments = nullptr;
|
|
|
|
if (BuiltinFunction const* builtin = m_dialect.builtin(_funCall.functionName.name))
|
|
|
|
if (!builtin->literalArguments.empty())
|
|
|
|
literalArguments = &builtin->literalArguments;
|
|
|
|
evaluateArgs(_funCall.arguments, literalArguments);
|
2018-02-06 09:57:16 +00:00
|
|
|
|
2019-05-20 12:30:32 +00:00
|
|
|
if (EVMDialect const* dialect = dynamic_cast<EVMDialect const*>(&m_dialect))
|
2019-11-12 16:08:23 +00:00
|
|
|
{
|
2019-05-20 12:30:32 +00:00
|
|
|
if (BuiltinFunctionForEVM const* fun = dialect->builtin(_funCall.functionName.name))
|
|
|
|
{
|
|
|
|
EVMInstructionInterpreter interpreter(m_state);
|
2020-07-27 14:22:05 +00:00
|
|
|
setValue(interpreter.evalBuiltin(*fun, _funCall.arguments, values()));
|
2019-05-20 12:30:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-12 16:08:23 +00:00
|
|
|
}
|
|
|
|
else if (WasmDialect const* dialect = dynamic_cast<WasmDialect const*>(&m_dialect))
|
|
|
|
if (dialect->builtin(_funCall.functionName.name))
|
|
|
|
{
|
2019-12-12 12:57:07 +00:00
|
|
|
EwasmBuiltinInterpreter interpreter(m_state);
|
2020-07-27 14:22:05 +00:00
|
|
|
setValue(interpreter.evalBuiltin(_funCall.functionName.name, _funCall.arguments, values()));
|
2019-11-12 16:08:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-04-30 13:02:52 +00:00
|
|
|
|
2020-07-09 12:24:49 +00:00
|
|
|
Scope* scope = &m_scope;
|
|
|
|
for (; scope; scope = scope->parent)
|
|
|
|
if (scope->names.count(_funCall.functionName.name))
|
|
|
|
break;
|
|
|
|
yulAssert(scope, "");
|
2019-08-28 14:18:05 +00:00
|
|
|
|
2020-07-09 12:24:49 +00:00
|
|
|
FunctionDefinition const* fun = scope->names.at(_funCall.functionName.name);
|
|
|
|
yulAssert(fun, "Function not found.");
|
|
|
|
yulAssert(m_values.size() == fun->parameters.size(), "");
|
2018-02-06 09:57:16 +00:00
|
|
|
map<YulString, u256> variables;
|
2019-08-28 14:18:05 +00:00
|
|
|
for (size_t i = 0; i < fun->parameters.size(); ++i)
|
|
|
|
variables[fun->parameters.at(i).name] = m_values.at(i);
|
|
|
|
for (size_t i = 0; i < fun->returnVariables.size(); ++i)
|
|
|
|
variables[fun->returnVariables.at(i).name] = 0;
|
2018-02-06 09:57:16 +00:00
|
|
|
|
2019-10-28 14:25:02 +00:00
|
|
|
m_state.controlFlowState = ControlFlowState::Default;
|
2020-07-09 12:24:49 +00:00
|
|
|
Interpreter interpreter(m_state, m_dialect, *scope, std::move(variables));
|
2019-08-28 14:18:05 +00:00
|
|
|
interpreter(fun->body);
|
2019-10-28 14:25:02 +00:00
|
|
|
m_state.controlFlowState = ControlFlowState::Default;
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
m_values.clear();
|
2019-08-28 14:18:05 +00:00
|
|
|
for (auto const& retVar: fun->returnVariables)
|
2018-02-06 09:57:16 +00:00
|
|
|
m_values.emplace_back(interpreter.valueOfVariable(retVar.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
u256 ExpressionEvaluator::value() const
|
|
|
|
{
|
|
|
|
solAssert(m_values.size() == 1, "");
|
|
|
|
return m_values.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionEvaluator::setValue(u256 _value)
|
|
|
|
{
|
|
|
|
m_values.clear();
|
|
|
|
m_values.emplace_back(std::move(_value));
|
|
|
|
}
|
|
|
|
|
2020-07-27 14:22:05 +00:00
|
|
|
void ExpressionEvaluator::evaluateArgs(
|
|
|
|
vector<Expression> const& _expr,
|
|
|
|
vector<optional<LiteralKind>> const* _literalArguments
|
|
|
|
)
|
2018-02-06 09:57:16 +00:00
|
|
|
{
|
|
|
|
vector<u256> values;
|
2020-07-27 14:22:05 +00:00
|
|
|
size_t i = 0;
|
2018-02-06 09:57:16 +00:00
|
|
|
/// Function arguments are evaluated in reverse.
|
|
|
|
for (auto const& expr: _expr | boost::adaptors::reversed)
|
|
|
|
{
|
2020-07-27 14:22:05 +00:00
|
|
|
if (!_literalArguments || !_literalArguments->at(_expr.size() - i - 1))
|
|
|
|
visit(expr);
|
|
|
|
else
|
|
|
|
m_values = {0};
|
2018-02-06 09:57:16 +00:00
|
|
|
values.push_back(value());
|
2020-07-27 14:22:05 +00:00
|
|
|
++i;
|
2018-02-06 09:57:16 +00:00
|
|
|
}
|
|
|
|
m_values = std::move(values);
|
|
|
|
std::reverse(m_values.begin(), m_values.end());
|
|
|
|
}
|