Skip to content

Commit

Permalink
eof: Improve RJUMPV tests (#755)
Browse files Browse the repository at this point in the history
- Replace CODECOPY with DATACOPY
- Use calldata argument instead of modifying code byte
- Split long jumps case into separate cases for min negative and max
positive jumps
  • Loading branch information
gumb0 authored Dec 21, 2023
2 parents 1ac539f + 7fdc926 commit b2337d4
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions test/unittests/evm_eof_rjump_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ TEST_P(evm, eof1_rjumpv_single_offset)
return;

rev = EVMC_PRAGUE;
auto code = eof_bytecode(rjumpv({3}, 0) + OP_JUMPDEST + OP_JUMPDEST + OP_STOP + 20 + 40 + 0 +
OP_CODECOPY + ret(0, 20),
auto code = eof_bytecode(rjumpv({3}, 0) + OP_JUMPDEST + OP_JUMPDEST + OP_STOP + 20 + 0 + 0 +
OP_DATACOPY + ret(0, 20),
3)
.data("ef000101000402000100010300000000000000fe");

Expand All @@ -163,34 +163,29 @@ TEST_P(evm, eof1_rjumpv_multiple_offsets)
return;

rev = EVMC_PRAGUE;
bytecode code = eof_bytecode(rjump(12) + 10 + 68 + 0 + OP_CODECOPY + ret(0, 10) +
rjumpv({12, -22, 0}, 1) + 10 + 78 + 0 + OP_CODECOPY +
ret(0, 10) + 20 + 68 + 0 + OP_CODECOPY + ret(0, 20),
const auto code = eof_bytecode(
rjump(12) + 10 + 0 + 0 + OP_DATACOPY + ret(0, 10) + rjumpv({12, -23, 0}, calldataload(0)) +
10 + 10 + 0 + OP_DATACOPY + ret(0, 10) + 20 + 0 + 0 + OP_DATACOPY + ret(0, 20),
3)
.data("ef000101000402000100010300000000000000fe");
.data("ef000101000402000100010300000000000000fe");

execute(code);
execute(code, bytes(31, 0) + "01"_hex);
EXPECT_STATUS(EVMC_SUCCESS);
ASSERT_EQ(result.output_size, 10);
EXPECT_EQ(bytes_view(result.output_data, result.output_size), "ef000101000402000100"_hex);

auto& rjumpv_cond = code[35];

rjumpv_cond = 2;
execute(code);
execute(code, bytes(31, 0) + "02"_hex);
EXPECT_STATUS(EVMC_SUCCESS);
ASSERT_EQ(result.output_size, 10);
EXPECT_EQ(bytes_view(result.output_data, result.output_size), "010300000000000000fe"_hex);

rjumpv_cond = 0;
execute(code);
execute(code, "00"_hex);
EXPECT_STATUS(EVMC_SUCCESS);
ASSERT_EQ(result.output_size, 20);
EXPECT_EQ(bytes_view(result.output_data, result.output_size),
"ef000101000402000100010300000000000000fe"_hex);

rjumpv_cond = 12; // case >= count, same behaviour as for case == 2
execute(code);
execute(code, bytes(31, 0) + "12"_hex); // case >= count, same behaviour as for case == 2
EXPECT_STATUS(EVMC_SUCCESS);
ASSERT_EQ(result.output_size, 10);
EXPECT_EQ(bytes_view(result.output_data, result.output_size), "010300000000000000fe"_hex);
Expand All @@ -203,24 +198,32 @@ TEST_P(evm, eof1_rjumpv_long_jumps)
return;

rev = EVMC_PRAGUE;
auto code =
rjump(0x7fff - 3 - 5) + (0x7fff - 3 - 2 - 8 - 5) * bytecode{OP_JUMPDEST} + 7 + ret_top();

code += rjumpv({-0x7FFF, 0x7FFF - 8 - 2 - 8}, 0) +
(0x7fff - 8 - 2 - 8) * bytecode{OP_JUMPDEST} + 5 + ret_top();
const auto code_ret_7 = push(7) + ret_top();
// code_ret_7 and jumpdests together make up 0x7fff bytes
const auto jumpdests = (0x7fff - static_cast<int>(code_ret_7.size())) * bytecode{OP_JUMPDEST};

code = eof_bytecode(code, 2);
auto& rjumpv_cond = code[0x7fff - 3 - 5 + 3 + 1 + 19];
auto code = eof_bytecode(rjumpi(0x7fff, 1) + jumpdests + code_ret_7 +
rjumpv({-0x7fff}, calldataload(0)) + 5 + ret_top(),
2);

execute(code);
execute(code, "00"_hex);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_OUTPUT_INT(7);

rjumpv_cond = 1;
execute(code, "01"_hex);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_OUTPUT_INT(5);

execute(code);
code =
eof_bytecode(rjumpv({0x7FFF}, calldataload(0)) + jumpdests + code_ret_7 + 5 + ret_top(), 2);

execute(code, bytes(31, 0) + "00"_hex);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_OUTPUT_INT(5);

execute(code, bytes(31, 0) + "01"_hex);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_OUTPUT_INT(7);
}

TEST_P(evm, rjumps_undefined_in_legacy)
Expand Down

0 comments on commit b2337d4

Please sign in to comment.