2019-11-12 16:08:23 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-11-12 16:08:23 +00:00
|
|
|
/**
|
2019-12-12 12:57:07 +00:00
|
|
|
* Yul interpreter module that evaluates Ewasm builtins.
|
2019-11-12 16:08:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/ASTForward.h>
|
2019-11-12 16:08:23 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2020-10-27 19:26:00 +00:00
|
|
|
#include <libsolutil/FixedHash.h>
|
2019-11-12 16:08:23 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::evmasm
|
2019-11-12 16:08:23 +00:00
|
|
|
{
|
|
|
|
enum class Instruction: uint8_t;
|
|
|
|
}
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::yul
|
2019-11-12 16:08:23 +00:00
|
|
|
{
|
|
|
|
class YulString;
|
|
|
|
struct BuiltinFunctionForEVM;
|
2019-12-23 15:50:30 +00:00
|
|
|
}
|
2019-11-12 16:08:23 +00:00
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::yul::test
|
2019-11-12 16:08:23 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
struct InterpreterState;
|
|
|
|
|
|
|
|
/**
|
2019-12-12 12:57:07 +00:00
|
|
|
* Interprets Ewasm builtins based on the current state and logs instructions with
|
2019-11-12 16:08:23 +00:00
|
|
|
* side-effects.
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* - If memory access to a "large" memory position is performed, a deterministic
|
|
|
|
* value is returned. Data that is stored in a "large" memory position is not
|
|
|
|
* retained.
|
|
|
|
* - 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
|
|
|
|
*
|
|
|
|
* The main focus is that the generated execution trace is the same for equivalent executions
|
|
|
|
* and likely to be different for non-equivalent executions.
|
2020-10-27 19:26:00 +00:00
|
|
|
*
|
|
|
|
* The type names are following the Ewasm specification (https://github.com/ewasm/design/blob/master/eth_interface.md).
|
2019-11-12 16:08:23 +00:00
|
|
|
*/
|
2019-12-12 12:57:07 +00:00
|
|
|
class EwasmBuiltinInterpreter
|
2019-11-12 16:08:23 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-12-12 12:57:07 +00:00
|
|
|
explicit EwasmBuiltinInterpreter(InterpreterState& _state):
|
2019-11-12 16:08:23 +00:00
|
|
|
m_state(_state)
|
|
|
|
{}
|
|
|
|
/// Evaluate builtin function
|
2020-07-27 14:22:05 +00:00
|
|
|
u256 evalBuiltin(
|
|
|
|
YulString _functionName,
|
|
|
|
std::vector<Expression> const& _arguments,
|
|
|
|
std::vector<u256> const& _evaluatedArguments
|
|
|
|
);
|
2019-11-12 16:08:23 +00:00
|
|
|
|
|
|
|
private:
|
2020-02-03 22:35:19 +00:00
|
|
|
template <typename Word>
|
|
|
|
u256 evalWasmBuiltin(
|
|
|
|
std::string const& _fun,
|
|
|
|
std::vector<Word> const& _arguments
|
|
|
|
);
|
|
|
|
u256 evalEthBuiltin(
|
|
|
|
std::string const& _fun,
|
|
|
|
std::vector<uint64_t> const& _arguments
|
|
|
|
);
|
|
|
|
|
2019-11-12 16:08:23 +00:00
|
|
|
/// Checks if the memory access is not too large for the interpreter and adjusts
|
|
|
|
/// msize accordingly.
|
2020-10-28 14:31:49 +00:00
|
|
|
void accessMemory(u256 const& _offset, u256 const& _size = 32);
|
2019-11-12 16:08:23 +00:00
|
|
|
/// @returns the memory contents at the provided address.
|
|
|
|
/// Does not adjust msize, use @a accessMemory for that
|
2019-12-23 15:50:30 +00:00
|
|
|
bytes readMemory(uint64_t _offset, uint64_t _size = 32);
|
2020-02-03 22:35:19 +00:00
|
|
|
/// @returns the memory contents (8 bytes) at the provided address (little-endian).
|
2019-11-12 16:08:23 +00:00
|
|
|
/// Does not adjust msize, use @a accessMemory for that
|
|
|
|
uint64_t readMemoryWord(uint64_t _offset);
|
2020-02-03 22:35:19 +00:00
|
|
|
/// @returns the memory contents (4 bytes) at the provided address (little-endian).
|
|
|
|
/// Does not adjust msize, use @a accessMemory for that
|
|
|
|
uint32_t readMemoryHalfWord(uint64_t _offset);
|
2020-10-27 19:26:00 +00:00
|
|
|
/// Writes bytes to memory.
|
|
|
|
/// Does not adjust msize, use @a accessMemory for that
|
|
|
|
void writeMemory(uint64_t _offset, bytes const& _value);
|
2019-11-12 16:08:23 +00:00
|
|
|
/// Writes a word to memory (little-endian)
|
|
|
|
/// Does not adjust msize, use @a accessMemory for that
|
|
|
|
void writeMemoryWord(uint64_t _offset, uint64_t _value);
|
2020-02-03 22:35:19 +00:00
|
|
|
/// Writes a 4-byte value to memory (little-endian)
|
|
|
|
/// Does not adjust msize, use @a accessMemory for that
|
|
|
|
void writeMemoryHalfWord(uint64_t _offset, uint32_t _value);
|
2019-12-18 18:48:29 +00:00
|
|
|
/// Writes a byte to memory
|
|
|
|
/// Does not adjust msize, use @a accessMemory for that
|
|
|
|
void writeMemoryByte(uint64_t _offset, uint8_t _value);
|
2019-11-12 16:08:23 +00:00
|
|
|
|
2020-10-27 19:26:00 +00:00
|
|
|
/// Helper for eth.* builtins. Writes to memory (little-endian) and always returns zero.
|
2019-12-23 15:50:30 +00:00
|
|
|
void writeU256(uint64_t _offset, u256 _value, size_t _croppedTo = 32);
|
|
|
|
void writeU128(uint64_t _offset, u256 _value) { writeU256(_offset, std::move(_value), 16); }
|
2020-10-27 19:26:00 +00:00
|
|
|
/// Helper for eth.* builtins. Writes to memory (as a byte string).
|
|
|
|
void writeBytes32(uint64_t _offset, util::h256 _value) { accessMemory(_offset, 32); writeMemory(_offset, _value.asBytes()); }
|
|
|
|
void writeAddress(uint64_t _offset, util::h160 _value) { accessMemory(_offset, 20); writeMemory(_offset, _value.asBytes()); }
|
|
|
|
/// Helper for eth.* builtins. Reads from memory (little-endian) and returns the value.
|
2019-12-23 15:50:30 +00:00
|
|
|
u256 readU256(uint64_t _offset, size_t _croppedTo = 32);
|
|
|
|
u256 readU128(uint64_t _offset) { return readU256(_offset, 16); }
|
2020-10-27 19:26:00 +00:00
|
|
|
/// Helper for eth.* builtins. Reads from memory (as a byte string).
|
|
|
|
util::h256 readBytes32(uint64_t _offset) { accessMemory(_offset, 32); return util::h256(readMemory(_offset, 32)); }
|
|
|
|
util::h160 readAddress(uint64_t _offset) { accessMemory(_offset, 20); return util::h160(readMemory(_offset, 20)); }
|
2019-11-12 16:08:23 +00:00
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
void logTrace(evmasm::Instruction _instruction, std::vector<u256> const& _arguments = {}, bytes const& _data = {});
|
2019-11-12 16:08:23 +00:00
|
|
|
/// Appends a log to the trace representing an instruction or similar operation by string,
|
|
|
|
/// with arguments and auxiliary data (if nonempty).
|
2019-12-23 15:50:30 +00:00
|
|
|
void logTrace(std::string const& _pseudoInstruction, std::vector<u256> const& _arguments = {}, bytes const& _data = {});
|
2019-11-12 16:08:23 +00:00
|
|
|
|
|
|
|
InterpreterState& m_state;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|