Skip to content

Commit

Permalink
eof: Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiazet committed Dec 27, 2022
1 parent bcc15ac commit e844a66
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 36 deletions.
10 changes: 3 additions & 7 deletions lib/evmone/eof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// SPDX-License-Identifier: Apache-2.0

#include "eof.hpp"
#include "instructions_traits.hpp"
#include "baseline_instruction_table.hpp"
#include "instructions_traits.hpp"

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -359,9 +359,7 @@ std::pair<EOFValidationError, int32_t> validate_max_stack_height(
return {EOFValidationError::no_terminating_instruction, -1};

auto beg = stack_heights.begin() + static_cast<int32_t>(i) + 1;
auto end = beg + count * 2 + 1;
for (auto it = beg; it < end; ++it)
*it = -2;
std::fill_n(beg, count * 2 + 1, -2);

successors.push_back(next);

Expand All @@ -378,9 +376,7 @@ std::pair<EOFValidationError, int32_t> validate_max_stack_height(
else
{
auto beg = stack_heights.begin() + static_cast<int32_t>(i) + 1;
auto end = beg + instr::traits[opcode].immediate_size;
for (auto it = beg; it < end; ++it)
*it = -2;
std::fill_n(beg, instr::traits[opcode].immediate_size, -2);
}

stack_height += stack_height_change;
Expand Down
25 changes: 17 additions & 8 deletions test/unittests/eof_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,26 @@ TEST(eof_validation, EOF1_undefined_opcodes)
// They're all valid in Shanghai and checked in other tests below.
if (opcode >= OP_PUSH1 && opcode <= OP_PUSH32)
continue;
if (opcode == OP_RJUMP || opcode == OP_RJUMPI || opcode == OP_RJUMPV ||
opcode == OP_CALLF || opcode == OP_RETF || opcode == OP_INVALID || opcode == OP_STOP ||
opcode == OP_RETURN || opcode == OP_REVERT || opcode == OP_SELFDESTRUCT)
if (opcode == OP_RJUMP || opcode == OP_RJUMPI || opcode == OP_RJUMPV || opcode == OP_CALLF)
continue;
if (opcode == OP_JUMP || opcode == OP_JUMPI || opcode == OP_PC || opcode == OP_CALLCODE ||
opcode == OP_SELFDESTRUCT)
continue;

cont += static_cast<uint8_t>(opcode);
if (!instr::traits[opcode].is_terminating)
cont += "00"_hex;
if (opcode == OP_RETF)
{
cont += "5050505050505050505050505050505050"_hex;
cont += static_cast<uint8_t>(opcode);
cont[10] = 0x24;
}
else
cont[10] = 0x13;

{
cont += static_cast<uint8_t>(opcode);
if (!instr::traits[opcode].is_terminating)
cont += "00"_hex;
else
cont[10] = 0x13;
}

auto op_stack_change = instr::traits[opcode].stack_height_change;
cont[15] = static_cast<uint8_t>(op_stack_change <= 0 ? 17 : 17 + op_stack_change);
Expand Down
40 changes: 20 additions & 20 deletions test/unittests/evm_eof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ TEST_P(evm, DISABLED_eof1_push_byte_in_header)
TEST_P(evm, eof1_codesize)
{
rev = EVMC_SHANGHAI;
auto code = eof1_bytecode(mstore8(0, OP_CODESIZE) + ret(0, 1));
auto code = eof1_bytecode(mstore8(0, OP_CODESIZE) + ret(0, 1), 2);

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
Expand All @@ -143,19 +143,19 @@ TEST_P(evm, eof1_codesize)
TEST_P(evm, eof1_codecopy_full)
{
rev = EVMC_SHANGHAI;
auto code = eof1_bytecode(bytecode{31} + 0 + 0 + OP_CODECOPY + ret(0, 31));
auto code = eof1_bytecode(bytecode{31} + 0 + 0 + OP_CODECOPY + ret(0, 31), 3);

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size),
"ef0001010004020001000c0300000000000000601f6000600039601f6000f3"_hex);
"ef0001010004020001000c0300000000000003601f6000600039601f6000f3"_hex);

code = eof1_bytecode(bytecode{35} + 0 + 0 + OP_CODECOPY + ret(0, 35), 3, "deadbeef");

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size),
"ef0001010004020001000c03000400000000006023600060003960236000f3deadbeef"_hex);
"ef0001010004020001000c03000400000000036023600060003960236000f3deadbeef"_hex);
}

TEST_P(evm, eof1_codecopy_header)
Expand Down Expand Up @@ -207,19 +207,19 @@ TEST_P(evm, eof1_codecopy_out_of_bounds)
{
// 4 bytes out of container bounds - result is implicitly 0-padded
rev = EVMC_SHANGHAI;
auto code = eof1_bytecode(bytecode{35} + 0 + 0 + OP_CODECOPY + ret(0, 35));
auto code = eof1_bytecode(bytecode{35} + 0 + 0 + OP_CODECOPY + ret(0, 35), 3);

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size),
"ef0001010004020001000c03000000000000006023600060003960236000f300000000"_hex);
"ef0001010004020001000c03000000000000036023600060003960236000f300000000"_hex);

code = eof1_bytecode(bytecode{39} + 0 + 0 + OP_CODECOPY + ret(0, 39), 3, "deadbeef");

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size),
"ef0001010004020001000c03000400000000006027600060003960276000f3deadbeef00000000"_hex);
"ef0001010004020001000c03000400000000036027600060003960276000f3deadbeef00000000"_hex);
}

TEST_P(evm, eof2_rjump)
Expand All @@ -229,14 +229,15 @@ TEST_P(evm, eof2_rjump)
return;

rev = EVMC_SHANGHAI;
auto code = eof1_bytecode(rjump(1) + OP_INVALID + mstore8(0, 1) + ret(0, 1));
auto code = eof1_bytecode(rjumpi(3, 0) + rjump(1) + OP_INVALID + mstore8(0, 1) + ret(0, 1), 2);

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
ASSERT_EQ(result.output_size, 1);
EXPECT_EQ(result.output_data[0], 1);

code = eof1_bytecode(rjump(1) + OP_INVALID + mstore8(0, 1) + ret(0, 1), 2, "deadbeef");
code = eof1_bytecode(
rjumpi(3, 0) + rjump(1) + OP_INVALID + mstore8(0, 1) + ret(0, 1), 2, "deadbeef");

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
Expand All @@ -251,16 +252,14 @@ TEST_P(evm, eof2_rjump_backward)
return;

rev = EVMC_SHANGHAI;
auto code =
eof1_bytecode(rjump(11) + OP_INVALID + mstore8(0, 1) + ret(0, 1) + rjump(-13) + OP_STOP);
auto code = eof1_bytecode(rjump(10) + mstore8(0, 1) + ret(0, 1) + rjump(-13), 2);

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
ASSERT_EQ(result.output_size, 1);
EXPECT_EQ(result.output_data[0], 1);

code = eof1_bytecode(
rjump(11) + OP_INVALID + mstore8(0, 1) + ret(0, 1) + rjump(-13) + OP_STOP, 2, "deadbeef");
code = eof1_bytecode(rjump(10) + mstore8(0, 1) + ret(0, 1) + rjump(-13), 2, "deadbeef");

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
Expand All @@ -275,7 +274,7 @@ TEST_P(evm, eof2_rjump_0_offset)
return;

rev = EVMC_SHANGHAI;
auto code = eof1_bytecode(rjump(0) + mstore8(0, 1) + ret(0, 1));
auto code = eof1_bytecode(rjump(0) + mstore8(0, 1) + ret(0, 1), 2);

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
Expand All @@ -291,7 +290,7 @@ TEST_P(evm, eof2_rjumpi)

rev = EVMC_SHANGHAI;
auto code = eof1_bytecode(
rjumpi(10, calldataload(0)) + mstore8(0, 2) + ret(0, 1) + mstore8(0, 1) + ret(0, 1));
rjumpi(10, calldataload(0)) + mstore8(0, 2) + ret(0, 1) + mstore8(0, 1) + ret(0, 1), 2);

// RJUMPI condition is true
execute(code, "01"_hex);
Expand All @@ -313,8 +312,9 @@ TEST_P(evm, eof2_rjumpi_backwards)
return;

rev = EVMC_SHANGHAI;
auto code = eof1_bytecode(rjump(11) + OP_INVALID + mstore8(0, 1) + ret(0, 1) +
rjumpi(-16, calldataload(0)) + mstore8(0, 2) + ret(0, 1));
auto code = eof1_bytecode(rjump(10) + mstore8(0, 1) + ret(0, 1) + rjumpi(-16, calldataload(0)) +
mstore8(0, 2) + ret(0, 1),
2);

// RJUMPI condition is true
execute(code, "01"_hex);
Expand All @@ -336,7 +336,7 @@ TEST_P(evm, eof2_rjumpi_0_offset)
return;

rev = EVMC_SHANGHAI;
auto code = eof1_bytecode(rjumpi(0, calldataload(0)) + mstore8(0, 1) + ret(0, 1));
auto code = eof1_bytecode(rjumpi(0, calldataload(0)) + mstore8(0, 1) + ret(0, 1), 2);

// RJUMPI condition is true
execute(code, "01"_hex);
Expand Down Expand Up @@ -375,7 +375,7 @@ TEST_P(evm, eof_function_example1)
rev = EVMC_SHANGHAI;
const auto code =
"EF00 01 010008 020002 000f 0002 00"
"00000005 02010000"
"00000002 02010002"
"6001 6008 b00001 " +
ret_top() + "03b1";

Expand All @@ -394,7 +394,7 @@ TEST_P(evm, eof_function_example2)

rev = EVMC_SHANGHAI;
const auto code =
"ef0001 01000c 020003 003b 0017 001d 00 00000400 01010400 01010400"
"ef0001 01000c 020003 003b 0017 001d 00 00000004 01010003 01010004"
"60043560003560e01c63c766526781145d001c63c6c2ea1781145d00065050600080fd50b00002600052602060"
"00f350b0000160005260206000f3"
"600181115d0004506001b160018103b0000181029050b1"
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/tracing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ 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"(
EXPECT_EQ(trace(eof1_bytecode(add(2, 3) + OP_STOP, 2), 0, 0, EVMC_SHANGHAI), R"(
{"depth":0,"rev":"Shanghai","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}
Expand Down

0 comments on commit e844a66

Please sign in to comment.