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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/ASTForward.h>
|
2018-02-06 09:57:16 +00:00
|
|
|
#include <libyul/optimiser/ASTWalker.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/FixedHash.h>
|
|
|
|
#include <libsolutil/CommonData.h>
|
2018-02-06 09:57:16 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Exceptions.h>
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::yul
|
2018-02-06 09:57:16 +00:00
|
|
|
{
|
2019-04-30 13:02:52 +00:00
|
|
|
struct Dialect;
|
2019-12-23 15:50:30 +00:00
|
|
|
}
|
2019-04-30 13:02:52 +00:00
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::yul::test
|
2018-02-06 09:57:16 +00:00
|
|
|
{
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
class InterpreterTerminatedGeneric: public util::Exception
|
2019-03-27 11:05:54 +00:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
class ExplicitlyTerminated: public InterpreterTerminatedGeneric
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
class StepLimitReached: public InterpreterTerminatedGeneric
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
class TraceLimitReached: public InterpreterTerminatedGeneric
|
2018-02-06 09:57:16 +00:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2020-07-23 09:58:10 +00:00
|
|
|
class ExpressionNestingLimitReached: public InterpreterTerminatedGeneric
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2019-10-28 14:25:02 +00:00
|
|
|
enum class ControlFlowState
|
2019-03-04 14:38:05 +00:00
|
|
|
{
|
|
|
|
Default,
|
|
|
|
Continue,
|
|
|
|
Break,
|
2019-10-28 14:25:02 +00:00
|
|
|
Leave
|
2019-03-04 14:38:05 +00:00
|
|
|
};
|
|
|
|
|
2018-02-06 09:57:16 +00:00
|
|
|
struct InterpreterState
|
|
|
|
{
|
2019-12-23 15:50:30 +00:00
|
|
|
bytes calldata;
|
|
|
|
bytes returndata;
|
|
|
|
std::map<u256, uint8_t> memory;
|
2018-02-06 09:57:16 +00:00
|
|
|
/// This is different than memory.size() because we ignore gas.
|
2019-12-23 15:50:30 +00:00
|
|
|
u256 msize;
|
|
|
|
std::map<util::h256, util::h256> storage;
|
2020-12-04 18:39:42 +00:00
|
|
|
util::h160 address = util::h160("0x0000000000000000000000000000000011111111");
|
2019-12-23 15:50:30 +00:00
|
|
|
u256 balance = 0x22222222;
|
|
|
|
u256 selfbalance = 0x22223333;
|
2020-12-04 18:39:42 +00:00
|
|
|
util::h160 origin = util::h160("0x0000000000000000000000000000000033333333");
|
|
|
|
util::h160 caller = util::h160("0x0000000000000000000000000000000044444444");
|
2019-12-23 15:50:30 +00:00
|
|
|
u256 callvalue = 0x55555555;
|
2018-02-06 09:57:16 +00:00
|
|
|
/// Deployed code
|
2019-12-23 15:50:30 +00:00
|
|
|
bytes code = util::asBytes("codecodecodecodecode");
|
|
|
|
u256 gasprice = 0x66666666;
|
2020-12-04 18:39:42 +00:00
|
|
|
util::h160 coinbase = util::h160("0x0000000000000000000000000000000077777777");
|
2019-12-23 15:50:30 +00:00
|
|
|
u256 timestamp = 0x88888888;
|
|
|
|
u256 blockNumber = 1024;
|
|
|
|
u256 difficulty = 0x9999999;
|
|
|
|
u256 gaslimit = 4000000;
|
|
|
|
u256 chainid = 0x01;
|
2021-07-12 14:49:53 +00:00
|
|
|
/// The minimum value of basefee: 7 wei.
|
|
|
|
u256 basefee = 0x07;
|
2018-02-06 09:57:16 +00:00
|
|
|
/// Log of changes / effects. Sholud be structured data in the future.
|
|
|
|
std::vector<std::string> trace;
|
|
|
|
/// This is actually an input parameter that more or less limits the runtime.
|
|
|
|
size_t maxTraceSize = 0;
|
2019-03-27 11:05:54 +00:00
|
|
|
size_t maxSteps = 0;
|
|
|
|
size_t numSteps = 0;
|
2020-07-23 09:58:10 +00:00
|
|
|
size_t maxExprNesting = 0;
|
2019-10-28 14:25:02 +00:00
|
|
|
ControlFlowState controlFlowState = ControlFlowState::Default;
|
2019-05-21 09:46:37 +00:00
|
|
|
|
2021-04-12 07:34:58 +00:00
|
|
|
/// Prints execution trace and non-zero storage to @param _out.
|
2019-05-21 09:46:37 +00:00
|
|
|
void dumpTraceAndState(std::ostream& _out) const;
|
2021-04-12 07:34:58 +00:00
|
|
|
/// Prints non-zero storage to @param _out.
|
|
|
|
void dumpStorage(std::ostream& _out) const;
|
2018-02-06 09:57:16 +00:00
|
|
|
};
|
|
|
|
|
2020-07-09 12:24:49 +00:00
|
|
|
/**
|
|
|
|
* Scope structure built and maintained during execution.
|
|
|
|
*/
|
|
|
|
struct Scope
|
|
|
|
{
|
|
|
|
/// Used for variables and functions. Value is nullptr for variables.
|
|
|
|
std::map<YulString, FunctionDefinition const*> names;
|
|
|
|
std::map<Block const*, std::unique_ptr<Scope>> subScopes;
|
|
|
|
Scope* parent = nullptr;
|
|
|
|
};
|
|
|
|
|
2018-02-06 09:57:16 +00:00
|
|
|
/**
|
|
|
|
* Yul interpreter.
|
|
|
|
*/
|
|
|
|
class Interpreter: public ASTWalker
|
|
|
|
{
|
|
|
|
public:
|
2020-07-09 12:24:49 +00:00
|
|
|
static void run(InterpreterState& _state, Dialect const& _dialect, Block const& _ast);
|
|
|
|
|
2018-02-06 09:57:16 +00:00
|
|
|
Interpreter(
|
|
|
|
InterpreterState& _state,
|
2019-04-30 13:02:52 +00:00
|
|
|
Dialect const& _dialect,
|
2020-07-09 12:24:49 +00:00
|
|
|
Scope& _scope,
|
|
|
|
std::map<YulString, u256> _variables = {}
|
2018-02-06 09:57:16 +00:00
|
|
|
):
|
2019-04-30 13:02:52 +00:00
|
|
|
m_dialect(_dialect),
|
2018-02-06 09:57:16 +00:00
|
|
|
m_state(_state),
|
|
|
|
m_variables(std::move(_variables)),
|
2020-07-09 12:24:49 +00:00
|
|
|
m_scope(&_scope)
|
|
|
|
{
|
|
|
|
}
|
2018-02-06 09:57:16 +00:00
|
|
|
|
2019-02-25 10:46:53 +00:00
|
|
|
void operator()(ExpressionStatement const& _statement) override;
|
|
|
|
void operator()(Assignment const& _assignment) override;
|
|
|
|
void operator()(VariableDeclaration const& _varDecl) override;
|
|
|
|
void operator()(If const& _if) override;
|
|
|
|
void operator()(Switch const& _switch) override;
|
|
|
|
void operator()(FunctionDefinition const&) override;
|
|
|
|
void operator()(ForLoop const&) override;
|
2019-03-04 14:38:05 +00:00
|
|
|
void operator()(Break const&) override;
|
|
|
|
void operator()(Continue const&) override;
|
2019-10-28 14:25:02 +00:00
|
|
|
void operator()(Leave const&) override;
|
2019-02-25 10:46:53 +00:00
|
|
|
void operator()(Block const& _block) override;
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
std::vector<std::string> const& trace() const { return m_state.trace; }
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
u256 valueOfVariable(YulString _name) const { return m_variables.at(_name); }
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Asserts that the expression evaluates to exactly one value and returns it.
|
2019-12-23 15:50:30 +00:00
|
|
|
u256 evaluate(Expression const& _expression);
|
2018-02-06 09:57:16 +00:00
|
|
|
/// Evaluates the expression and returns its value.
|
2019-12-23 15:50:30 +00:00
|
|
|
std::vector<u256> evaluateMulti(Expression const& _expression);
|
2018-02-06 09:57:16 +00:00
|
|
|
|
2020-07-09 12:24:49 +00:00
|
|
|
void enterScope(Block const& _block);
|
|
|
|
void leaveScope();
|
2018-02-06 09:57:16 +00:00
|
|
|
|
2020-07-21 12:37:22 +00:00
|
|
|
/// Increment interpreter step count, throwing exception if step limit
|
|
|
|
/// is reached.
|
|
|
|
void incrementStep();
|
|
|
|
|
2019-04-30 13:02:52 +00:00
|
|
|
Dialect const& m_dialect;
|
2018-02-06 09:57:16 +00:00
|
|
|
InterpreterState& m_state;
|
|
|
|
/// Values of variables.
|
2019-12-23 15:50:30 +00:00
|
|
|
std::map<YulString, u256> m_variables;
|
2020-07-09 12:24:49 +00:00
|
|
|
Scope* m_scope;
|
2018-02-06 09:57:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Yul expression evaluator.
|
|
|
|
*/
|
|
|
|
class ExpressionEvaluator: public ASTWalker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ExpressionEvaluator(
|
|
|
|
InterpreterState& _state,
|
2019-04-30 13:02:52 +00:00
|
|
|
Dialect const& _dialect,
|
2020-07-09 12:24:49 +00:00
|
|
|
Scope& _scope,
|
|
|
|
std::map<YulString, u256> const& _variables
|
2018-02-06 09:57:16 +00:00
|
|
|
):
|
|
|
|
m_state(_state),
|
2019-04-30 13:02:52 +00:00
|
|
|
m_dialect(_dialect),
|
2018-02-06 09:57:16 +00:00
|
|
|
m_variables(_variables),
|
2020-07-09 12:24:49 +00:00
|
|
|
m_scope(_scope)
|
2018-02-06 09:57:16 +00:00
|
|
|
{}
|
|
|
|
|
2019-02-25 10:46:53 +00:00
|
|
|
void operator()(Literal const&) override;
|
|
|
|
void operator()(Identifier const&) override;
|
|
|
|
void operator()(FunctionCall const& _funCall) override;
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
/// Asserts that the expression has exactly one value and returns it.
|
2019-12-23 15:50:30 +00:00
|
|
|
u256 value() const;
|
2018-02-06 09:57:16 +00:00
|
|
|
/// Returns the list of values of the expression.
|
2019-12-23 15:50:30 +00:00
|
|
|
std::vector<u256> values() const { return m_values; }
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
private:
|
2019-12-23 15:50:30 +00:00
|
|
|
void setValue(u256 _value);
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
/// Evaluates the given expression from right to left and
|
|
|
|
/// stores it in m_value.
|
2020-07-27 14:22:05 +00:00
|
|
|
void evaluateArgs(
|
|
|
|
std::vector<Expression> const& _expr,
|
|
|
|
std::vector<std::optional<LiteralKind>> const* _literalArguments
|
|
|
|
);
|
2018-02-06 09:57:16 +00:00
|
|
|
|
2020-07-23 09:58:10 +00:00
|
|
|
/// Increment evaluation count, throwing exception if the
|
|
|
|
/// nesting level is beyond the upper bound configured in
|
|
|
|
/// the interpreter state.
|
|
|
|
void incrementStep();
|
|
|
|
|
2018-02-06 09:57:16 +00:00
|
|
|
InterpreterState& m_state;
|
2019-04-30 13:02:52 +00:00
|
|
|
Dialect const& m_dialect;
|
2018-02-06 09:57:16 +00:00
|
|
|
/// Values of variables.
|
2019-12-23 15:50:30 +00:00
|
|
|
std::map<YulString, u256> const& m_variables;
|
2020-07-09 12:24:49 +00:00
|
|
|
Scope& m_scope;
|
2018-02-06 09:57:16 +00:00
|
|
|
/// Current value of the expression
|
2019-12-23 15:50:30 +00:00
|
|
|
std::vector<u256> m_values;
|
2020-07-23 09:58:10 +00:00
|
|
|
/// Current expression nesting level
|
|
|
|
unsigned m_nestingLevel = 0;
|
2018-02-06 09:57:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|