solidity/test/tools/yulInterpreter/EVMInstructionInterpreter.cpp

510 lines
14 KiB
C++
Raw Normal View History

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.
*/
#include <test/tools/yulInterpreter/EVMInstructionInterpreter.h>
#include <test/tools/yulInterpreter/Interpreter.h>
2019-05-20 12:30:32 +00:00
#include <libyul/backends/evm/EVMDialect.h>
2018-02-06 09:57:16 +00:00
#include <libyul/AsmData.h>
#include <libevmasm/Instruction.h>
#include <libsolutil/Keccak256.h>
2018-02-06 09:57:16 +00:00
using namespace std;
using namespace solidity;
using namespace solidity::yul;
using namespace solidity::yul::test;
using solidity::util::h256;
using solidity::util::keccak256;
2018-02-06 09:57:16 +00:00
namespace
{
2019-02-18 14:16:14 +00:00
/// Reads 32 bytes from @a _data at position @a _offset bytes while
/// interpreting @a _data to be padded with an infinite number of zero
/// bytes beyond its end.
2018-02-06 09:57:16 +00:00
u256 readZeroExtended(bytes const& _data, u256 const& _offset)
{
if (_offset >= _data.size())
return 0;
else if (_offset + 32 <= _data.size())
return *reinterpret_cast<h256 const*>(_data.data() + size_t(_offset));
else
{
size_t off = size_t(_offset);
u256 val;
for (size_t i = 0; i < 32; ++i)
{
val <<= 8;
if (off + i < _data.size())
val += _data[off + i];
}
return val;
}
}
2019-02-18 14:16:14 +00:00
/// 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.
2018-02-06 09:57:16 +00:00
void copyZeroExtended(
map<u256, uint8_t>& _target, bytes const& _source,
2018-02-06 09:57:16 +00:00
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;
}
}
using u512 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<512, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
u256 EVMInstructionInterpreter::eval(
2019-12-11 16:31:36 +00:00
evmasm::Instruction _instruction,
2018-02-06 09:57:16 +00:00
vector<u256> const& _arguments
)
{
using namespace solidity::evmasm;
2019-12-11 16:31:36 +00:00
using evmasm::Instruction;
2018-02-06 09:57:16 +00:00
auto info = instructionInfo(_instruction);
2018-02-06 09:57:16 +00:00
yulAssert(size_t(info.args) == _arguments.size(), "");
auto const& arg = _arguments;
switch (_instruction)
{
case Instruction::STOP:
throw ExplicitlyTerminated();
2018-02-06 09:57:16 +00:00
// --------------- arithmetic ---------------
case Instruction::ADD:
return arg[0] + arg[1];
case Instruction::MUL:
return arg[0] * arg[1];
case Instruction::SUB:
return arg[0] - arg[1];
case Instruction::DIV:
return arg[1] == 0 ? 0 : arg[0] / arg[1];
case Instruction::SDIV:
return arg[1] == 0 ? 0 : s2u(u2s(arg[0]) / u2s(arg[1]));
case Instruction::MOD:
return arg[1] == 0 ? 0 : arg[0] % arg[1];
case Instruction::SMOD:
return arg[1] == 0 ? 0 : s2u(u2s(arg[0]) % u2s(arg[1]));
case Instruction::EXP:
return exp256(arg[0], arg[1]);
case Instruction::NOT:
return ~arg[0];
case Instruction::LT:
return arg[0] < arg[1] ? 1 : 0;
case Instruction::GT:
return arg[0] > arg[1] ? 1 : 0;
case Instruction::SLT:
return u2s(arg[0]) < u2s(arg[1]) ? 1 : 0;
case Instruction::SGT:
2019-02-25 10:46:53 +00:00
return u2s(arg[0]) > u2s(arg[1]) ? 1 : 0;
2018-02-06 09:57:16 +00:00
case Instruction::EQ:
return arg[0] == arg[1] ? 1 : 0;
case Instruction::ISZERO:
return arg[0] == 0 ? 1 : 0;
case Instruction::AND:
return arg[0] & arg[1];
case Instruction::OR:
return arg[0] | arg[1];
case Instruction::XOR:
return arg[0] ^ arg[1];
case Instruction::BYTE:
2019-02-18 14:16:14 +00:00
return arg[0] >= 32 ? 0 : (arg[1] >> unsigned(8 * (31 - arg[0]))) & 0xff;
2018-02-06 09:57:16 +00:00
case Instruction::SHL:
2019-02-18 14:16:14 +00:00
return arg[0] > 255 ? 0 : (arg[1] << unsigned(arg[0]));
2018-02-06 09:57:16 +00:00
case Instruction::SHR:
2019-02-18 14:16:14 +00:00
return arg[0] > 255 ? 0 : (arg[1] >> unsigned(arg[0]));
2018-02-06 09:57:16 +00:00
case Instruction::SAR:
{
static u256 const hibit = u256(1) << 255;
if (arg[0] >= 256)
return arg[1] & hibit ? u256(-1) : 0;
else
{
2019-02-18 14:16:14 +00:00
unsigned amount = unsigned(arg[0]);
2018-02-06 09:57:16 +00:00
u256 v = arg[1] >> amount;
if (arg[1] & hibit)
v |= u256(-1) << (256 - amount);
return v;
}
}
case Instruction::ADDMOD:
return arg[2] == 0 ? 0 : u256((u512(arg[0]) + u512(arg[1])) % arg[2]);
case Instruction::MULMOD:
return arg[2] == 0 ? 0 : u256((u512(arg[0]) * u512(arg[1])) % arg[2]);
case Instruction::SIGNEXTEND:
if (arg[0] >= 31)
2019-02-26 16:55:30 +00:00
return arg[1];
2018-02-06 09:57:16 +00:00
else
{
2019-02-18 14:16:14 +00:00
unsigned testBit = unsigned(arg[0]) * 8 + 7;
2018-02-06 09:57:16 +00:00
u256 ret = arg[1];
u256 mask = ((u256(1) << testBit) - 1);
if (boost::multiprecision::bit_test(ret, testBit))
ret |= ~mask;
else
ret &= mask;
return ret;
}
// --------------- blockchain stuff ---------------
case Instruction::KECCAK256:
{
2019-05-23 09:35:12 +00:00
if (!accessMemory(arg[0], arg[1]))
2018-02-06 09:57:16 +00:00
return u256("0x1234cafe1234cafe1234cafe") + arg[0];
uint64_t offset = uint64_t(arg[0] & uint64_t(-1));
uint64_t size = uint64_t(arg[1] & uint64_t(-1));
return u256(keccak256(readMemory(offset, size)));
2018-02-06 09:57:16 +00:00
}
case Instruction::ADDRESS:
return m_state.address;
case Instruction::BALANCE:
return m_state.balance;
2019-09-02 13:23:45 +00:00
case Instruction::SELFBALANCE:
return m_state.selfbalance;
2018-02-06 09:57:16 +00:00
case Instruction::ORIGIN:
return m_state.origin;
case Instruction::CALLER:
return m_state.caller;
case Instruction::CALLVALUE:
return m_state.callvalue;
case Instruction::CALLDATALOAD:
return readZeroExtended(m_state.calldata, arg[0]);
case Instruction::CALLDATASIZE:
return m_state.calldata.size();
case Instruction::CALLDATACOPY:
2019-05-23 09:35:12 +00:00
if (accessMemory(arg[0], arg[2]))
2018-02-06 09:57:16 +00:00
copyZeroExtended(
m_state.memory, m_state.calldata,
size_t(arg[0]), size_t(arg[1]), size_t(arg[2])
);
return 0;
case Instruction::CODESIZE:
return m_state.code.size();
case Instruction::CODECOPY:
2019-05-23 09:35:12 +00:00
if (accessMemory(arg[0], arg[2]))
2018-02-06 09:57:16 +00:00
copyZeroExtended(
m_state.memory, m_state.code,
size_t(arg[0]), size_t(arg[1]), size_t(arg[2])
);
return 0;
case Instruction::GASPRICE:
return m_state.gasprice;
2019-09-02 13:23:45 +00:00
case Instruction::CHAINID:
return m_state.chainid;
2018-02-06 09:57:16 +00:00
case Instruction::EXTCODESIZE:
return u256(keccak256(h256(arg[0]))) & 0xffffff;
case Instruction::EXTCODEHASH:
return u256(keccak256(h256(arg[0] + 1)));
case Instruction::EXTCODECOPY:
logTrace(_instruction, arg);
2019-05-23 09:35:12 +00:00
if (accessMemory(arg[1], arg[3]))
2018-02-06 09:57:16 +00:00
// 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;
case Instruction::RETURNDATASIZE:
return m_state.returndata.size();
case Instruction::RETURNDATACOPY:
logTrace(_instruction, arg);
2019-05-23 09:35:12 +00:00
if (accessMemory(arg[0], arg[2]))
2018-02-06 09:57:16 +00:00
copyZeroExtended(
m_state.memory, m_state.returndata,
size_t(arg[0]), size_t(arg[1]), size_t(arg[2])
);
return 0;
case Instruction::BLOCKHASH:
if (arg[0] >= m_state.blockNumber || arg[0] + 256 < m_state.blockNumber)
return 0;
else
return 0xaaaaaaaa + (arg[0] - m_state.blockNumber - 256);
case Instruction::COINBASE:
return m_state.coinbase;
case Instruction::TIMESTAMP:
return m_state.timestamp;
case Instruction::NUMBER:
return m_state.blockNumber;
case Instruction::DIFFICULTY:
return m_state.difficulty;
case Instruction::GASLIMIT:
return m_state.gaslimit;
// --------------- memory / storage / logs ---------------
case Instruction::MLOAD:
accessMemory(arg[0], 0x20);
return readMemoryWord(arg[0]);
2018-02-06 09:57:16 +00:00
case Instruction::MSTORE:
accessMemory(arg[0], 0x20);
writeMemoryWord(arg[0], arg[1]);
2018-02-06 09:57:16 +00:00
return 0;
case Instruction::MSTORE8:
accessMemory(arg[0], 1);
m_state.memory[arg[0]] = uint8_t(arg[1] & 0xff);
2018-02-06 09:57:16 +00:00
return 0;
case Instruction::SLOAD:
return m_state.storage[h256(arg[0])];
case Instruction::SSTORE:
m_state.storage[h256(arg[0])] = h256(arg[1]);
return 0;
case Instruction::PC:
return 0x77;
case Instruction::MSIZE:
return m_state.msize;
case Instruction::GAS:
return 0x99;
case Instruction::LOG0:
2019-05-23 09:35:12 +00:00
accessMemory(arg[0], arg[1]);
2018-02-06 09:57:16 +00:00
logTrace(_instruction, arg);
return 0;
case Instruction::LOG1:
2019-05-23 09:35:12 +00:00
accessMemory(arg[0], arg[1]);
2018-02-06 09:57:16 +00:00
logTrace(_instruction, arg);
return 0;
case Instruction::LOG2:
2019-05-23 09:35:12 +00:00
accessMemory(arg[0], arg[1]);
2018-02-06 09:57:16 +00:00
logTrace(_instruction, arg);
return 0;
case Instruction::LOG3:
2019-05-23 09:35:12 +00:00
accessMemory(arg[0], arg[1]);
2018-02-06 09:57:16 +00:00
logTrace(_instruction, arg);
return 0;
case Instruction::LOG4:
2019-05-23 09:35:12 +00:00
accessMemory(arg[0], arg[1]);
2018-02-06 09:57:16 +00:00
logTrace(_instruction, arg);
return 0;
// --------------- calls ---------------
case Instruction::CREATE:
2019-05-23 09:35:12 +00:00
accessMemory(arg[1], arg[2]);
2018-02-06 09:57:16 +00:00
logTrace(_instruction, arg);
return 0xcccccc + arg[1];
case Instruction::CREATE2:
2019-05-23 09:35:12 +00:00
accessMemory(arg[2], arg[3]);
2018-02-06 09:57:16 +00:00
logTrace(_instruction, arg);
return 0xdddddd + arg[1];
case Instruction::CALL:
case Instruction::CALLCODE:
// TODO assign returndata
2019-05-23 09:35:12 +00:00
accessMemory(arg[3], arg[4]);
accessMemory(arg[5], arg[6]);
2018-02-06 09:57:16 +00:00
logTrace(_instruction, arg);
return arg[0] & 1;
case Instruction::DELEGATECALL:
case Instruction::STATICCALL:
2019-05-23 09:35:12 +00:00
accessMemory(arg[2], arg[3]);
accessMemory(arg[4], arg[5]);
2018-02-06 09:57:16 +00:00
logTrace(_instruction, arg);
return 0;
case Instruction::RETURN:
{
bytes data;
2019-05-23 09:35:12 +00:00
if (accessMemory(arg[0], arg[1]))
data = readMemory(arg[0], arg[1]);
2018-02-06 09:57:16 +00:00
logTrace(_instruction, arg, data);
throw ExplicitlyTerminated();
2018-02-06 09:57:16 +00:00
}
case Instruction::REVERT:
2019-05-23 09:35:12 +00:00
accessMemory(arg[0], arg[1]);
2018-02-06 09:57:16 +00:00
logTrace(_instruction, arg);
throw ExplicitlyTerminated();
2018-02-06 09:57:16 +00:00
case Instruction::INVALID:
logTrace(_instruction);
throw ExplicitlyTerminated();
2018-02-06 09:57:16 +00:00
case Instruction::SELFDESTRUCT:
logTrace(_instruction, arg);
throw ExplicitlyTerminated();
2019-02-18 14:19:14 +00:00
case Instruction::POP:
break;
2018-02-06 09:57:16 +00:00
// --------------- invalid in strict assembly ---------------
case Instruction::JUMP:
case Instruction::JUMPI:
case Instruction::JUMPDEST:
case Instruction::PUSH1:
case Instruction::PUSH2:
case Instruction::PUSH3:
case Instruction::PUSH4:
case Instruction::PUSH5:
case Instruction::PUSH6:
case Instruction::PUSH7:
case Instruction::PUSH8:
case Instruction::PUSH9:
case Instruction::PUSH10:
case Instruction::PUSH11:
case Instruction::PUSH12:
case Instruction::PUSH13:
case Instruction::PUSH14:
case Instruction::PUSH15:
case Instruction::PUSH16:
case Instruction::PUSH17:
case Instruction::PUSH18:
case Instruction::PUSH19:
case Instruction::PUSH20:
case Instruction::PUSH21:
case Instruction::PUSH22:
case Instruction::PUSH23:
case Instruction::PUSH24:
case Instruction::PUSH25:
case Instruction::PUSH26:
case Instruction::PUSH27:
case Instruction::PUSH28:
case Instruction::PUSH29:
case Instruction::PUSH30:
case Instruction::PUSH31:
case Instruction::PUSH32:
case Instruction::DUP1:
case Instruction::DUP2:
case Instruction::DUP3:
case Instruction::DUP4:
case Instruction::DUP5:
case Instruction::DUP6:
case Instruction::DUP7:
case Instruction::DUP8:
case Instruction::DUP9:
case Instruction::DUP10:
case Instruction::DUP11:
case Instruction::DUP12:
case Instruction::DUP13:
case Instruction::DUP14:
case Instruction::DUP15:
case Instruction::DUP16:
case Instruction::SWAP1:
case Instruction::SWAP2:
case Instruction::SWAP3:
case Instruction::SWAP4:
case Instruction::SWAP5:
case Instruction::SWAP6:
case Instruction::SWAP7:
case Instruction::SWAP8:
case Instruction::SWAP9:
case Instruction::SWAP10:
case Instruction::SWAP11:
case Instruction::SWAP12:
case Instruction::SWAP13:
case Instruction::SWAP14:
case Instruction::SWAP15:
case Instruction::SWAP16:
// --------------- EVM 2.0 ---------------
case Instruction::JUMPTO:
case Instruction::JUMPIF:
case Instruction::JUMPV:
case Instruction::JUMPSUB:
case Instruction::JUMPSUBV:
case Instruction::BEGINSUB:
case Instruction::BEGINDATA:
case Instruction::RETURNSUB:
case Instruction::PUTLOCAL:
case Instruction::GETLOCAL:
{
yulAssert(false, "");
return 0;
}
}
return 0;
}
2019-05-20 12:30:32 +00:00
u256 EVMInstructionInterpreter::evalBuiltin(BuiltinFunctionForEVM const& _fun, const std::vector<u256>& _arguments)
{
2019-05-20 12:30:32 +00:00
if (_fun.instruction)
return eval(*_fun.instruction, _arguments);
else if (_fun.name == "datasize"_yulstring)
return u256(keccak256(h256(_arguments.at(0)))) & 0xfff;
2019-05-20 12:30:32 +00:00
else if (_fun.name == "dataoffset"_yulstring)
return u256(keccak256(h256(_arguments.at(0) + 2))) & 0xfff;
2019-05-20 12:30:32 +00:00
else if (_fun.name == "datacopy"_yulstring)
{
// This is identical to codecopy.
2019-05-23 09:35:12 +00:00
if (accessMemory(_arguments.at(0), _arguments.at(2)))
copyZeroExtended(
m_state.memory,
m_state.code,
size_t(_arguments.at(0)),
size_t(_arguments.at(1) & size_t(-1)),
size_t(_arguments.at(2))
);
}
else
2019-05-20 12:30:32 +00:00
yulAssert(false, "Unknown builtin: " + _fun.name.str());
return 0;
}
2018-02-06 09:57:16 +00:00
2019-05-23 09:35:12 +00:00
bool EVMInstructionInterpreter::accessMemory(u256 const& _offset, u256 const& _size)
2018-02-06 09:57:16 +00:00
{
if (((_offset + _size) >= _offset) && ((_offset + _size + 0x1f) >= (_offset + _size)))
2018-02-06 09:57:16 +00:00
{
u256 newSize = (_offset + _size + 0x1f) & ~u256(0x1f);
2018-02-06 09:57:16 +00:00
m_state.msize = max(m_state.msize, newSize);
return _size <= 0xffff;
2018-02-06 09:57:16 +00:00
}
else
m_state.msize = u256(-1);
return false;
}
bytes EVMInstructionInterpreter::readMemory(u256 const& _offset, u256 const& _size)
{
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;
}
u256 EVMInstructionInterpreter::readMemoryWord(u256 const& _offset)
{
return u256(h256(readMemory(_offset, 32)));
}
void EVMInstructionInterpreter::writeMemoryWord(u256 const& _offset, u256 const& _value)
{
for (size_t i = 0; i < 32; i++)
m_state.memory[_offset + i] = uint8_t((_value >> (8 * (31 - i))) & 0xff);
}
2019-12-11 16:31:36 +00:00
void EVMInstructionInterpreter::logTrace(evmasm::Instruction _instruction, std::vector<u256> const& _arguments, bytes const& _data)
2018-02-06 09:57:16 +00:00
{
2019-12-11 16:31:36 +00:00
logTrace(evmasm::instructionInfo(_instruction).name, _arguments, _data);
2018-02-06 09:57:16 +00:00
}
void EVMInstructionInterpreter::logTrace(std::string const& _pseudoInstruction, std::vector<u256> const& _arguments, bytes const& _data)
{
string message = _pseudoInstruction + "(";
for (size_t i = 0; i < _arguments.size(); ++i)
message += (i > 0 ? ", " : "") + util::formatNumber(_arguments[i]);
2018-02-06 09:57:16 +00:00
message += ")";
if (!_data.empty())
message += " [" + util::toHex(_data) + "]";
2018-02-06 09:57:16 +00:00
m_state.trace.emplace_back(std::move(message));
if (m_state.maxTraceSize > 0 && m_state.trace.size() >= m_state.maxTraceSize)
2018-02-07 20:56:14 +00:00
{
m_state.trace.emplace_back("Trace size limit reached.");
throw TraceLimitReached();
2018-02-07 20:56:14 +00:00
}
2018-02-06 09:57:16 +00:00
}