solidity/test/tools/yulInterpreter/EwasmBuiltinInterpreter.cpp

583 lines
15 KiB
C++
Raw Normal View History

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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2019-11-12 16:08:23 +00:00
/**
* Yul interpreter module that evaluates Ewasm builtins.
2019-11-12 16:08:23 +00:00
*/
#include <test/tools/yulInterpreter/EwasmBuiltinInterpreter.h>
2019-11-12 16:08:23 +00:00
#include <test/tools/yulInterpreter/Interpreter.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/AsmData.h>
#include <libevmasm/Instruction.h>
#include <libsolutil/Keccak256.h>
2019-11-12 16:08:23 +00:00
using namespace std;
using namespace solidity;
using namespace solidity::yul;
using namespace solidity::yul::test;
using solidity::util::h256;
2019-11-12 16:08:23 +00:00
namespace
{
/// Copy @a _size bytes of @a _source at offset @a _sourceOffset to
/// @a _target at offset @a _targetOffset. Behaves as if @a _source would
/// continue with an infinite sequence of zero bytes beyond its end.
void copyZeroExtended(
map<u256, uint8_t>& _target, bytes const& _source,
size_t _targetOffset, size_t _sourceOffset, size_t _size
)
{
for (size_t i = 0; i < _size; ++i)
_target[_targetOffset + i] = _sourceOffset + i < _source.size() ? _source[_sourceOffset + i] : 0;
}
/// Count leading zeros for uint64. Following WebAssembly rules, it returns 64 for @a _v being zero.
/// NOTE: the clz builtin of the compiler may or may not do this
uint64_t clz64(uint64_t _v)
{
if (_v == 0)
return 64;
uint64_t r = 0;
while (!(_v & 0x8000000000000000))
{
r += 1;
_v = _v << 1;
}
return r;
}
2020-07-08 22:16:43 +00:00
/// Count trailing zeros for uint32. Following WebAssembly rules, it returns 32 for @a _v being zero.
/// NOTE: the ctz builtin of the compiler may or may not do this
uint32_t ctz32(uint32_t _v)
{
if (_v == 0)
return 32;
uint32_t r = 0;
while (!(_v & 1))
{
r++;
_v >>= 1;
}
return r;
}
/// Count trailing zeros for uint64. Following WebAssembly rules, it returns 64 for @a _v being zero.
/// NOTE: the ctz builtin of the compiler may or may not do this
uint64_t ctz64(uint64_t _v)
{
if (_v == 0)
return 64;
uint64_t r = 0;
while (!(_v & 1))
{
r++;
_v >>= 1;
}
return r;
}
/// Count number of bits set for uint64
uint64_t popcnt(uint64_t _v)
{
uint64_t r = 0;
while (_v)
{
r += (_v & 1);
_v >>= 1;
}
return r;
}
2019-11-12 16:08:23 +00:00
}
using u512 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<512, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
2020-07-27 14:22:05 +00:00
u256 EwasmBuiltinInterpreter::evalBuiltin(
YulString _functionName,
vector<Expression> const& _arguments,
vector<u256> const& _evaluatedArguments
)
2019-11-12 16:08:23 +00:00
{
vector<uint64_t> arg;
2020-07-27 14:22:05 +00:00
for (u256 const& a: _evaluatedArguments)
2019-11-12 16:08:23 +00:00
arg.emplace_back(uint64_t(a & uint64_t(-1)));
2020-07-27 14:22:05 +00:00
string const fun = _functionName.str();
if (fun == "datasize" || fun == "dataoffset")
{
string arg = std::get<Literal>(_arguments.at(0)).value.str();
if (arg.length() < 32)
arg.resize(32, 0);
if (fun == "datasize")
return u256(util::keccak256(arg)) & 0xfff;
else if (fun == "dataoffset")
{
arg[31] += 2;
return u256(util::keccak256(arg)) & 0xfff;
}
}
2020-07-09 12:56:34 +00:00
else if (fun == "datacopy")
2019-11-12 16:08:23 +00:00
{
// This is identical to codecopy.
2020-07-27 14:22:05 +00:00
if (accessMemory(_evaluatedArguments.at(0), _evaluatedArguments.at(2)))
2019-11-12 16:08:23 +00:00
copyZeroExtended(
m_state.memory,
m_state.code,
2020-07-27 14:22:05 +00:00
static_cast<size_t>(_evaluatedArguments.at(0)),
static_cast<size_t>(_evaluatedArguments.at(1) & numeric_limits<size_t>::max()),
static_cast<size_t>(_evaluatedArguments.at(2))
2019-11-12 16:08:23 +00:00
);
return 0;
}
2020-07-09 12:56:34 +00:00
else if (fun == "i32.drop" || fun == "i64.drop" || fun == "nop")
2019-11-12 16:08:23 +00:00
return {};
2020-07-09 12:56:34 +00:00
else if (fun == "i32.wrap_i64")
return arg.at(0) & uint32_t(-1);
2020-07-09 12:56:34 +00:00
else if (fun == "i64.extend_i32_u")
// Return the same as above because everything is u256 anyway.
return arg.at(0) & uint32_t(-1);
2020-07-09 12:56:34 +00:00
else if (fun == "unreachable")
{
2019-12-11 16:31:36 +00:00
logTrace(evmasm::Instruction::INVALID, {});
2019-11-12 16:08:23 +00:00
throw ExplicitlyTerminated();
}
2020-07-09 12:56:34 +00:00
else if (fun == "i64.store")
{
accessMemory(arg[0], 8);
writeMemoryWord(arg[0], arg[1]);
return 0;
}
2020-07-09 12:56:34 +00:00
else if (fun == "i64.store8" || fun == "i32.store8")
{
accessMemory(arg[0], 1);
writeMemoryByte(arg[0], static_cast<uint8_t>(arg[1] & 0xff));
return 0;
}
2020-07-09 12:56:34 +00:00
else if (fun == "i64.load")
{
accessMemory(arg[0], 8);
return readMemoryWord(arg[0]);
}
2020-07-09 12:56:34 +00:00
else if (fun == "i32.store")
{
accessMemory(arg[0], 4);
writeMemoryHalfWord(arg[0], arg[1]);
return 0;
}
2020-07-09 12:56:34 +00:00
else if (fun == "i32.load")
{
accessMemory(arg[0], 4);
return readMemoryHalfWord(arg[0]);
}
2020-07-27 14:22:05 +00:00
else if (fun == "i32.clz")
// NOTE: the clz implementation assumes 64-bit inputs, hence the adjustment
return clz64(arg[0] & uint32_t(-1)) - 32;
2020-07-27 14:22:05 +00:00
else if (fun == "i64.clz")
return clz64(arg[0]);
2020-07-27 14:22:05 +00:00
else if (fun == "i32.ctz")
2020-07-08 22:16:43 +00:00
return ctz32(uint32_t(arg[0] & uint32_t(-1)));
2020-07-27 14:22:05 +00:00
else if (fun == "i64.ctz")
2020-07-08 22:16:43 +00:00
return ctz64(arg[0]);
2020-07-09 12:56:34 +00:00
string prefix = fun;
string suffix;
auto dot = prefix.find(".");
if (dot != string::npos)
{
suffix = prefix.substr(dot + 1);
prefix.resize(dot);
}
if (prefix == "i32")
{
vector<uint32_t> halfWordArgs;
for (uint64_t a: arg)
halfWordArgs.push_back(uint32_t(a & uint32_t(-1)));
return evalWasmBuiltin(suffix, halfWordArgs);
}
else if (prefix == "i64")
return evalWasmBuiltin(suffix, arg);
else if (prefix == "eth")
return evalEthBuiltin(suffix, arg);
2020-07-27 14:22:05 +00:00
yulAssert(false, "Unknown builtin: " + fun + " (or implementation did not return)");
return 0;
}
template <typename Word>
u256 EwasmBuiltinInterpreter::evalWasmBuiltin(string const& _fun, vector<Word> const& _arguments)
{
vector<Word> const& arg = _arguments;
if (_fun == "add")
2019-11-12 16:08:23 +00:00
return arg[0] + arg[1];
else if (_fun == "sub")
2019-11-12 16:08:23 +00:00
return arg[0] - arg[1];
else if (_fun == "mul")
2019-11-12 16:08:23 +00:00
return arg[0] * arg[1];
else if (_fun == "div_u")
2019-11-12 16:08:23 +00:00
{
if (arg[1] == 0)
throw ExplicitlyTerminated();
else
return arg[0] / arg[1];
}
else if (_fun == "rem_u")
2019-11-12 16:08:23 +00:00
{
if (arg[1] == 0)
throw ExplicitlyTerminated();
else
return arg[0] % arg[1];
}
else if (_fun == "and")
2019-11-12 16:08:23 +00:00
return arg[0] & arg[1];
else if (_fun == "or")
2019-11-12 16:08:23 +00:00
return arg[0] | arg[1];
else if (_fun == "xor")
2019-11-12 16:08:23 +00:00
return arg[0] ^ arg[1];
else if (_fun == "shl")
2019-11-12 16:08:23 +00:00
return arg[0] << arg[1];
else if (_fun == "shr_u")
2019-11-12 16:08:23 +00:00
return arg[0] >> arg[1];
else if (_fun == "eq")
2019-11-12 16:08:23 +00:00
return arg[0] == arg[1] ? 1 : 0;
else if (_fun == "ne")
2019-11-12 16:08:23 +00:00
return arg[0] != arg[1] ? 1 : 0;
else if (_fun == "eqz")
2019-11-12 16:08:23 +00:00
return arg[0] == 0 ? 1 : 0;
2020-07-08 22:16:43 +00:00
else if (_fun == "popcnt")
return popcnt(arg[0]);
else if (_fun == "lt_u")
2019-11-12 16:08:23 +00:00
return arg[0] < arg[1] ? 1 : 0;
else if (_fun == "gt_u")
2019-11-12 16:08:23 +00:00
return arg[0] > arg[1] ? 1 : 0;
else if (_fun == "le_u")
2019-11-12 16:08:23 +00:00
return arg[0] <= arg[1] ? 1 : 0;
else if (_fun == "ge_u")
2019-11-12 16:08:23 +00:00
return arg[0] >= arg[1] ? 1 : 0;
yulAssert(false, "Unknown builtin: " + _fun + " (or implementation did not return)");
return 0;
}
u256 EwasmBuiltinInterpreter::evalEthBuiltin(string const& _fun, vector<uint64_t> const& _arguments)
{
vector<uint64_t> const& arg = _arguments;
if (_fun == "getAddress")
{
writeAddress(arg[0], m_state.address);
return 0;
}
else if (_fun == "getExternalBalance")
{
2019-11-12 16:08:23 +00:00
// TODO this does not read the address, but is consistent with
// EVM interpreter implementation.
// If we take the address into account, this needs to use readAddress.
writeU128(arg[1], m_state.balance);
return 0;
}
else if (_fun == "getBlockHash")
2019-11-12 16:08:23 +00:00
{
if (arg[0] >= m_state.blockNumber || arg[0] + 256 < m_state.blockNumber)
return 1;
else
{
writeU256(arg[1], 0xaaaaaaaa + u256(arg[0] - m_state.blockNumber - 256));
return 0;
}
2019-11-12 16:08:23 +00:00
}
else if (_fun == "call")
2019-11-12 16:08:23 +00:00
{
// TODO read args from memory
// TODO use readAddress to read address.
2019-12-11 16:31:36 +00:00
logTrace(evmasm::Instruction::CALL, {});
2019-11-12 16:08:23 +00:00
return arg[0] & 1;
}
else if (_fun == "callDataCopy")
2019-11-12 16:08:23 +00:00
{
if (arg[1] + arg[2] < arg[1] || arg[1] + arg[2] > m_state.calldata.size())
throw ExplicitlyTerminated();
if (accessMemory(arg[0], arg[2]))
copyZeroExtended(
m_state.memory, m_state.calldata,
size_t(arg[0]), size_t(arg[1]), size_t(arg[2])
);
return {};
}
else if (_fun == "getCallDataSize")
2019-11-12 16:08:23 +00:00
return m_state.calldata.size();
else if (_fun == "callCode")
2019-11-12 16:08:23 +00:00
{
// TODO read args from memory
// TODO use readAddress to read address.
2019-12-11 16:31:36 +00:00
logTrace(evmasm::Instruction::CALLCODE, {});
2019-11-12 16:08:23 +00:00
return arg[0] & 1;
}
else if (_fun == "callDelegate")
2019-11-12 16:08:23 +00:00
{
// TODO read args from memory
// TODO use readAddress to read address.
2019-12-11 16:31:36 +00:00
logTrace(evmasm::Instruction::DELEGATECALL, {});
2019-11-12 16:08:23 +00:00
return arg[0] & 1;
}
else if (_fun == "callStatic")
2019-11-12 16:08:23 +00:00
{
// TODO read args from memory
// TODO use readAddress to read address.
2019-12-11 16:31:36 +00:00
logTrace(evmasm::Instruction::STATICCALL, {});
2019-11-12 16:08:23 +00:00
return arg[0] & 1;
}
else if (_fun == "storageStore")
2019-11-12 16:08:23 +00:00
{
m_state.storage[h256(readU256(arg[0]))] = readU256((arg[1]));
return 0;
}
else if (_fun == "storageLoad")
{
writeU256(arg[1], m_state.storage[h256(readU256(arg[0]))]);
return 0;
}
else if (_fun == "getCaller")
{
2019-11-12 16:08:23 +00:00
// TODO should this only write 20 bytes?
writeAddress(arg[0], m_state.caller);
return 0;
}
else if (_fun == "getCallValue")
{
writeU128(arg[0], m_state.callvalue);
return 0;
}
else if (_fun == "codeCopy")
2019-11-12 16:08:23 +00:00
{
if (accessMemory(arg[0], arg[2]))
copyZeroExtended(
m_state.memory, m_state.code,
size_t(arg[0]), size_t(arg[1]), size_t(arg[2])
);
return 0;
}
else if (_fun == "getCodeSize")
return m_state.code.size();
else if (_fun == "getBlockCoinbase")
{
writeAddress(arg[0], m_state.coinbase);
return 0;
}
else if (_fun == "create")
2019-11-12 16:08:23 +00:00
{
// TODO access memory
// TODO use writeAddress to store resulting address
2019-12-11 16:31:36 +00:00
logTrace(evmasm::Instruction::CREATE, {});
2019-11-12 16:08:23 +00:00
return 0xcccccc + arg[1];
}
else if (_fun == "getBlockDifficulty")
{
writeU256(arg[0], m_state.difficulty);
return 0;
}
else if (_fun == "externalCodeCopy")
2019-11-12 16:08:23 +00:00
{
// TODO use readAddress to read address.
if (accessMemory(arg[1], arg[3]))
// TODO this way extcodecopy and codecopy do the same thing.
copyZeroExtended(
m_state.memory, m_state.code,
size_t(arg[1]), size_t(arg[2]), size_t(arg[3])
);
return 0;
}
else if (_fun == "getExternalCodeSize")
// Generate "random" code length. Make sure it fits the page size.
return u256(keccak256(h256(readAddress(arg[0])))) & 0xfff;
else if (_fun == "getGasLeft")
2019-11-12 16:08:23 +00:00
return 0x99;
else if (_fun == "getBlockGasLimit")
2019-11-12 16:08:23 +00:00
return uint64_t(m_state.gaslimit);
else if (_fun == "getTxGasPrice")
{
writeU128(arg[0], m_state.gasprice);
return 0;
}
else if (_fun == "log")
2019-11-12 16:08:23 +00:00
{
uint64_t numberOfTopics = arg[2];
if (numberOfTopics > 4)
throw ExplicitlyTerminated();
2019-12-11 16:31:36 +00:00
logTrace(evmasm::logInstruction(numberOfTopics), {});
2019-11-12 16:08:23 +00:00
return 0;
}
else if (_fun == "getBlockNumber")
2019-11-12 16:08:23 +00:00
return m_state.blockNumber;
else if (_fun == "getTxOrigin")
{
writeAddress(arg[0], m_state.origin);
return 0;
}
else if (_fun == "finish")
2019-11-12 16:08:23 +00:00
{
bytes data;
if (accessMemory(arg[0], arg[1]))
data = readMemory(arg[0], arg[1]);
2019-12-11 16:31:36 +00:00
logTrace(evmasm::Instruction::RETURN, {}, data);
2019-11-12 16:08:23 +00:00
throw ExplicitlyTerminated();
}
else if (_fun == "revert")
2019-11-12 16:08:23 +00:00
{
bytes data;
if (accessMemory(arg[0], arg[1]))
data = readMemory(arg[0], arg[1]);
2019-12-11 16:31:36 +00:00
logTrace(evmasm::Instruction::REVERT, {}, data);
2019-11-12 16:08:23 +00:00
throw ExplicitlyTerminated();
}
else if (_fun == "getReturnDataSize")
2019-11-12 16:08:23 +00:00
return m_state.returndata.size();
else if (_fun == "returnDataCopy")
2019-11-12 16:08:23 +00:00
{
if (arg[1] + arg[2] < arg[1] || arg[1] + arg[2] > m_state.returndata.size())
throw ExplicitlyTerminated();
if (accessMemory(arg[0], arg[2]))
copyZeroExtended(
m_state.memory, m_state.calldata,
size_t(arg[0]), size_t(arg[1]), size_t(arg[2])
);
return {};
}
else if (_fun == "selfDestruct")
2019-11-12 16:08:23 +00:00
{
// TODO use readAddress to read address.
2019-12-11 16:31:36 +00:00
logTrace(evmasm::Instruction::SELFDESTRUCT, {});
2019-11-12 16:08:23 +00:00
throw ExplicitlyTerminated();
}
else if (_fun == "getBlockTimestamp")
2019-11-12 16:08:23 +00:00
return m_state.timestamp;
yulAssert(false, "Unknown builtin: " + _fun + " (or implementation did not return)");
2019-11-12 16:08:23 +00:00
return 0;
}
bool EwasmBuiltinInterpreter::accessMemory(u256 const& _offset, u256 const& _size)
2019-11-12 16:08:23 +00:00
{
if (((_offset + _size) >= _offset) && ((_offset + _size + 0x1f) >= (_offset + _size)))
{
u256 newSize = (_offset + _size + 0x1f) & ~u256(0x1f);
m_state.msize = max(m_state.msize, newSize);
return _size <= 0xffff;
}
else
m_state.msize = u256(-1);
return false;
}
bytes EwasmBuiltinInterpreter::readMemory(uint64_t _offset, uint64_t _size)
2019-11-12 16:08:23 +00:00
{
yulAssert(_size <= 0xffff, "Too large read.");
bytes data(size_t(_size), uint8_t(0));
for (size_t i = 0; i < data.size(); ++i)
data[i] = m_state.memory[_offset + i];
return data;
}
uint64_t EwasmBuiltinInterpreter::readMemoryWord(uint64_t _offset)
2019-11-12 16:08:23 +00:00
{
uint64_t r = 0;
for (size_t i = 0; i < 8; i++)
r |= uint64_t(m_state.memory[_offset + i]) << (i * 8);
return r;
}
uint32_t EwasmBuiltinInterpreter::readMemoryHalfWord(uint64_t _offset)
{
uint32_t r = 0;
for (size_t i = 0; i < 4; i++)
r |= uint64_t(m_state.memory[_offset + i]) << (i * 8);
return r;
}
void EwasmBuiltinInterpreter::writeMemoryWord(uint64_t _offset, uint64_t _value)
2019-11-12 16:08:23 +00:00
{
for (size_t i = 0; i < 8; i++)
m_state.memory[_offset + i] = uint8_t((_value >> (i * 8)) & 0xff);
}
void EwasmBuiltinInterpreter::writeMemoryHalfWord(uint64_t _offset, uint32_t _value)
{
for (size_t i = 0; i < 4; i++)
m_state.memory[_offset + i] = uint8_t((_value >> (i * 8)) & 0xff);
}
void EwasmBuiltinInterpreter::writeMemoryByte(uint64_t _offset, uint8_t _value)
{
m_state.memory[_offset] = _value;
}
void EwasmBuiltinInterpreter::writeU256(uint64_t _offset, u256 _value, size_t _croppedTo)
2019-11-12 16:08:23 +00:00
{
accessMemory(_offset, _croppedTo);
for (size_t i = 0; i < _croppedTo; i++)
{
m_state.memory[_offset + _croppedTo - 1 - i] = uint8_t(_value & 0xff);
_value >>= 8;
}
}
u256 EwasmBuiltinInterpreter::readU256(uint64_t _offset, size_t _croppedTo)
2019-11-12 16:08:23 +00:00
{
accessMemory(_offset, _croppedTo);
u256 value;
for (size_t i = 0; i < _croppedTo; i++)
value = (value << 8) | m_state.memory[_offset + i];
return value;
}
2019-12-11 16:31:36 +00:00
void EwasmBuiltinInterpreter::logTrace(evmasm::Instruction _instruction, std::vector<u256> const& _arguments, bytes const& _data)
2019-11-12 16:08:23 +00:00
{
2019-12-11 16:31:36 +00:00
logTrace(evmasm::instructionInfo(_instruction).name, _arguments, _data);
2019-11-12 16:08:23 +00:00
}
void EwasmBuiltinInterpreter::logTrace(std::string const& _pseudoInstruction, std::vector<u256> const& _arguments, bytes const& _data)
2019-11-12 16:08:23 +00:00
{
string message = _pseudoInstruction + "(";
for (size_t i = 0; i < _arguments.size(); ++i)
message += (i > 0 ? ", " : "") + util::formatNumber(_arguments[i]);
2019-11-12 16:08:23 +00:00
message += ")";
if (!_data.empty())
message += " [" + util::toHex(_data) + "]";
2019-11-12 16:08:23 +00:00
m_state.trace.emplace_back(std::move(message));
if (m_state.maxTraceSize > 0 && m_state.trace.size() >= m_state.maxTraceSize)
{
m_state.trace.emplace_back("Trace size limit reached.");
throw TraceLimitReached();
}
}