Skip to content

Commit

Permalink
Move EOF support to Cancun (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet authored Feb 13, 2023
1 parent 9a5d8f3 commit 791b42b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/evmone/advanced_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ evmc_result execute(evmc_vm* /*unused*/, const evmc_host_interface* host, evmc_h
const bytes_view container = {code, code_size};
if (is_eof_code(container))
{
if (rev >= EVMC_SHANGHAI)
if (rev >= EVMC_CANCUN)
{
const auto eof1_header = read_valid_eof1_header(container.begin());
analysis = analyze(rev, {&container[eof1_header.code_begin()], eof1_header.code_size});
Expand Down
2 changes: 1 addition & 1 deletion lib/evmone/baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ CodeAnalysis analyze_eof1(bytes_view eof_container, const EOF1Header& header)

CodeAnalysis analyze(evmc_revision rev, bytes_view code)
{
if (rev < EVMC_SHANGHAI || !is_eof_code(code))
if (rev < EVMC_CANCUN || !is_eof_code(code))
return analyze_legacy(code);

const auto eof1_header = read_valid_eof1_header(code.begin());
Expand Down
2 changes: 1 addition & 1 deletion lib/evmone/eof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ EOFValidationError validate_eof(evmc_revision rev, bytes_view container) noexcep

if (version == 1)
{
if (rev < EVMC_SHANGHAI)
if (rev < EVMC_CANCUN)
return EOFValidationError::eof_version_unknown;
return validate_eof1(rev, container).second;
}
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/analysis_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ TEST(analysis, example1_eof1)
const auto code = eof1_bytecode(
push(0x2a) + push(0x1e) + OP_MSTORE8 + OP_MSIZE + push(0) + OP_SSTORE, "deadbeef");
const auto header = evmone::read_valid_eof1_header(bytes_view(code).begin());
const auto analysis = analyze(EVMC_SHANGHAI, {&code[header.code_begin()], header.code_size});
const auto analysis = analyze(EVMC_CANCUN, {&code[header.code_begin()], header.code_size});

ASSERT_EQ(analysis.instrs.size(), 8);

Expand Down
8 changes: 5 additions & 3 deletions test/unittests/eof_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace
{
// Can be called as validate_eof(string_view hex, rev) or validate_eof(bytes_view cont, rev).
inline EOFValidationError validate_eof(
const bytecode& container, evmc_revision rev = EVMC_SHANGHAI) noexcept
const bytecode& container, evmc_revision rev = EVMC_CANCUN) noexcept
{
return evmone::validate_eof(rev, container);
}
Expand Down Expand Up @@ -164,13 +164,15 @@ TEST(eof_validation, EOF1_undefined_opcodes)
{
auto cont = "EF0001 010002 00 0000"_hex;

const auto& gas_table = evmone::instr::gas_costs[EVMC_SHANGHAI];
const auto& gas_table = evmone::instr::gas_costs[EVMC_CANCUN];

for (uint16_t opcode = 0; opcode <= 0xff; ++opcode)
{
// PUSH* require immediate argument to be valid, checked in a separate test
// PUSH*, DUPN, SWAPN require immediate argument to be valid, checked in a separate test
if (opcode >= OP_PUSH1 && opcode <= OP_PUSH32)
continue;
if (opcode == OP_DUPN || opcode == OP_SWAPN)
continue;

cont[cont.size() - 2] = static_cast<uint8_t>(opcode);

Expand Down
30 changes: 15 additions & 15 deletions test/unittests/evm_eof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ TEST_P(evm, eof1_execution)
{
const auto code = eof1_bytecode(OP_STOP);

rev = EVMC_PARIS;
rev = EVMC_SHANGHAI;
execute(code);
EXPECT_STATUS(EVMC_UNDEFINED_INSTRUCTION);

rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
}

TEST_P(evm, eof1_execution_with_data_section)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
// data section contains ret(0, 1)
const auto code = eof1_bytecode(mstore8(0, 1) + OP_STOP, ret(0, 1));

Expand All @@ -32,7 +32,7 @@ TEST_P(evm, eof1_execution_with_data_section)

TEST_P(evm, eof1_pc)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
auto code = eof1_bytecode(OP_PC + mstore8(0) + ret(0, 1));

execute(code);
Expand All @@ -50,7 +50,7 @@ TEST_P(evm, eof1_pc)

TEST_P(evm, eof1_jump_inside_code_section)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
auto code = eof1_bytecode(jump(4) + OP_INVALID + OP_JUMPDEST + mstore8(0, 1) + ret(0, 1));

execute(code);
Expand All @@ -69,7 +69,7 @@ TEST_P(evm, eof1_jump_inside_code_section)

TEST_P(evm, eof1_jumpi_inside_code_section)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
auto code = eof1_bytecode(jumpi(6, 1) + OP_INVALID + OP_JUMPDEST + mstore8(0, 1) + ret(0, 1));

execute(code);
Expand All @@ -88,7 +88,7 @@ TEST_P(evm, eof1_jumpi_inside_code_section)

TEST_P(evm, eof1_jump_into_data_section)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
// data section contains OP_JUMPDEST + mstore8(0, 1) + ret(0, 1)
const auto code = eof1_bytecode(jump(4) + OP_STOP, OP_JUMPDEST + mstore8(0, 1) + ret(0, 1));

Expand All @@ -98,7 +98,7 @@ TEST_P(evm, eof1_jump_into_data_section)

TEST_P(evm, eof1_jumpi_into_data_section)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
// data section contains OP_JUMPDEST + mstore8(0, 1) + ret(0, 1)
const auto code = eof1_bytecode(jumpi(6, 1) + OP_STOP, OP_JUMPDEST + mstore8(0, 1) + ret(0, 1));

Expand All @@ -108,7 +108,7 @@ TEST_P(evm, eof1_jumpi_into_data_section)

TEST_P(evm, eof1_push_byte_in_header)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
// data section is 0x65 bytes long, so header contains 0x65 (PUSH6) byte,
// but it must not affect jumpdest analysis (OP_JUMPDEST stays valid)
auto code = eof1_bytecode(
Expand All @@ -122,7 +122,7 @@ TEST_P(evm, eof1_push_byte_in_header)

TEST_P(evm, eof1_codesize)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
auto code = eof1_bytecode(mstore8(0, OP_CODESIZE) + ret(0, 1));

execute(code);
Expand All @@ -140,7 +140,7 @@ TEST_P(evm, eof1_codesize)

TEST_P(evm, eof1_codecopy_full)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
auto code = eof1_bytecode(bytecode{19} + 0 + 0 + OP_CODECOPY + ret(0, 19));

execute(code);
Expand All @@ -158,7 +158,7 @@ TEST_P(evm, eof1_codecopy_full)

TEST_P(evm, eof1_codecopy_header)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
auto code = eof1_bytecode(bytecode{7} + 0 + 0 + OP_CODECOPY + ret(0, 7));

execute(code);
Expand All @@ -174,7 +174,7 @@ TEST_P(evm, eof1_codecopy_header)

TEST_P(evm, eof1_codecopy_code)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
auto code = eof1_bytecode(bytecode{12} + 7 + 0 + OP_CODECOPY + ret(0, 12));

execute(code);
Expand All @@ -190,7 +190,7 @@ TEST_P(evm, eof1_codecopy_code)

TEST_P(evm, eof1_codecopy_data)
{
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;

const auto code = eof1_bytecode(bytecode{4} + 22 + 0 + OP_CODECOPY + ret(0, 4), "deadbeef");

Expand All @@ -202,7 +202,7 @@ TEST_P(evm, eof1_codecopy_data)
TEST_P(evm, eof1_codecopy_out_of_bounds)
{
// 4 bytes out of container bounds - result is implicitly 0-padded
rev = EVMC_SHANGHAI;
rev = EVMC_CANCUN;
auto code = eof1_bytecode(bytecode{23} + 0 + 0 + OP_CODECOPY + ret(0, 23));

execute(code);
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/tracing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ TEST_F(tracing, trace_eof)
vm.add_tracer(evmone::create_instruction_tracer(trace_stream));

trace_stream << '\n';
EXPECT_EQ(trace(eof1_bytecode(add(2, 3) + OP_STOP), 0, 0, EVMC_SHANGHAI), R"(
{"depth":0,"rev":"Shanghai","static":false}
EXPECT_EQ(trace(eof1_bytecode(add(2, 3) + OP_STOP), 0, 0, EVMC_CANCUN), R"(
{"depth":0,"rev":"Cancun","static":false}
{"pc":0,"op":96,"opName":"PUSH1","gas":1000000,"stack":[],"memorySize":0}
{"pc":2,"op":96,"opName":"PUSH1","gas":999997,"stack":["0x3"],"memorySize":0}
{"pc":4,"op":1,"opName":"ADD","gas":999994,"stack":["0x3","0x2"],"memorySize":0}
Expand Down

0 comments on commit 791b42b

Please sign in to comment.