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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Yul interpreter module that evaluates EVM instructions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libyul/AsmDataForward.h>
|
|
|
|
|
|
|
|
#include <libdevcore/CommonData.h>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
2019-03-28 11:47:21 +00:00
|
|
|
namespace eth
|
2018-02-06 09:57:16 +00:00
|
|
|
{
|
|
|
|
enum class Instruction: uint8_t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace yul
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
|
|
|
struct InterpreterState;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interprets EVM instructions based on the current state and logs instructions with
|
|
|
|
* side-effects.
|
|
|
|
*
|
2019-02-18 14:16:14 +00:00
|
|
|
* Since this is mainly meant to be used for differential fuzz testing, it is focused
|
|
|
|
* on a single contract only, does not do any gas counting and differs from the correct
|
|
|
|
* implementation in many ways:
|
2018-02-06 09:57:16 +00:00
|
|
|
*
|
|
|
|
* - If memory access to a "large" memory position is performed, a deterministic
|
2019-02-18 14:16:14 +00:00
|
|
|
* value is returned. Data that is stored in a "large" memory position is not
|
|
|
|
* retained.
|
2018-02-06 09:57:16 +00:00
|
|
|
* - The blockhash instruction returns a fixed value if the argument is in range.
|
|
|
|
* - Extcodesize returns a deterministic value depending on the address.
|
|
|
|
* - Extcodecopy copies a deterministic value depending on the address.
|
|
|
|
* - And many other things
|
|
|
|
*
|
2019-02-18 14:16:14 +00:00
|
|
|
* The main focus is that the generated execution trace is the same for equivalent executions
|
2019-02-25 10:46:53 +00:00
|
|
|
* and likely to be different for non-equivalent executions.
|
2018-02-06 09:57:16 +00:00
|
|
|
*/
|
|
|
|
class EVMInstructionInterpreter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit EVMInstructionInterpreter(InterpreterState& _state):
|
|
|
|
m_state(_state)
|
|
|
|
{}
|
2019-03-28 11:47:21 +00:00
|
|
|
dev::u256 eval(dev::eth::Instruction _instruction, std::vector<dev::u256> const& _arguments);
|
2018-02-06 09:57:16 +00:00
|
|
|
|
|
|
|
private:
|
2019-02-25 10:46:53 +00:00
|
|
|
/// Record a memory read in the trace. Also updates m_state.msize
|
2018-02-06 09:57:16 +00:00
|
|
|
/// @returns true if m_state.memory can be used at that offset.
|
|
|
|
bool logMemoryRead(dev::u256 const& _offset, dev::u256 const& _size = 32);
|
2019-02-25 10:46:53 +00:00
|
|
|
/// Record a memory write in the trace. Also updates m_state.msize
|
2018-02-06 09:57:16 +00:00
|
|
|
/// @returns true if m_state.memory can be used at that offset.
|
|
|
|
bool logMemoryWrite(dev::u256 const& _offset, dev::u256 const& _size = 32, dev::bytes const& _data = {});
|
|
|
|
|
|
|
|
bool logMemory(bool _write, dev::u256 const& _offset, dev::u256 const& _size = 32, dev::bytes const& _data = {});
|
|
|
|
|
2019-03-28 11:47:21 +00:00
|
|
|
void logTrace(dev::eth::Instruction _instruction, std::vector<dev::u256> const& _arguments = {}, dev::bytes const& _data = {});
|
2018-02-06 09:57:16 +00:00
|
|
|
/// Appends a log to the trace representing an instruction or similar operation by string,
|
|
|
|
/// with arguments and auxiliary data (if nonempty).
|
|
|
|
void logTrace(std::string const& _pseudoInstruction, std::vector<dev::u256> const& _arguments = {}, dev::bytes const& _data = {});
|
|
|
|
|
|
|
|
InterpreterState& m_state;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|