Skip to content

Commit

Permalink
Change RJUMPV immediate argument to mean max_index
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed May 8, 2023
1 parent 550f0a2 commit 892025a
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 20 deletions.
8 changes: 2 additions & 6 deletions lib/evmone/eof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,8 @@ EOFValidationError validate_instructions(evmc_revision rev, bytes_view code) noe
if (i + 1 >= code.size())
return EOFValidationError::truncated_instruction;

const auto count = code[i + 1];
if (count < 1)
return EOFValidationError::invalid_rjumpv_count;
i += static_cast<size_t>(1 /* count */ + count * 2 /* tbl */);
const auto count = code[i + 1] + 1;
i += static_cast<size_t>(1 /* max_index */ + count * 2 /* tbl */);
}
else
i += instr::traits[op].immediate_size;
Expand Down Expand Up @@ -577,8 +575,6 @@ std::string_view get_error_message(EOFValidationError err) noexcept
return "undefined_instruction";
case EOFValidationError::truncated_instruction:
return "truncated_instruction";
case EOFValidationError::invalid_rjumpv_count:
return "invalid_rjumpv_count";
case EOFValidationError::invalid_rjump_destination:
return "invalid_rjump_destination";
case EOFValidationError::too_many_code_sections:
Expand Down
1 change: 0 additions & 1 deletion lib/evmone/eof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ enum class EOFValidationError
invalid_section_bodies_size,
undefined_instruction,
truncated_instruction,
invalid_rjumpv_count,
invalid_rjump_destination,
too_many_code_sections,
invalid_type_section_size,
Expand Down
6 changes: 3 additions & 3 deletions lib/evmone/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,10 +728,10 @@ inline code_iterator rjumpv(StackTop stack, ExecutionState& /*state*/, code_iter
constexpr auto REL_OFFSET_SIZE = sizeof(int16_t);
const auto case_ = stack.pop();

const auto count = pc[1];
const auto pc_post = pc + 1 + 1 /* count */ + count * REL_OFFSET_SIZE /* tbl */;
const auto max_index = pc[1];
const auto pc_post = pc + 1 + 1 /* max_index */ + (max_index + 1) * REL_OFFSET_SIZE /* tbl */;

if (case_ >= count)
if (case_ > max_index)
{
return pc_post;
}
Expand Down
11 changes: 2 additions & 9 deletions test/unittests/eof_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,6 @@ TEST(eof_validation, EOF1_rjumpv_truncated)
EOFValidationError::truncated_instruction);
}

TEST(eof_validation, EOF1_rjumpv_0_count)
{
auto code = eof1_bytecode(rjumpv({}, 0) + OP_STOP, 1);

EXPECT_EQ(validate_eof(code), EOFValidationError::invalid_rjumpv_count);
}

TEST(eof_validation, EOF1_rjump_invalid_destination)
{
// Into header (offset = -5)
Expand Down Expand Up @@ -662,7 +655,7 @@ TEST(eof_valication, max_arguments_count)
}
}

TEST(eof_valication, max_stack_heigh)
TEST(eof_valication, max_stack_height)
{
{
auto code = "EF0001 010008 02000200010BFE 030000 00 00000000 000003FF B1" +
Expand Down Expand Up @@ -719,7 +712,7 @@ TEST(eof_valication, max_stack_heigh)
}

{
auto code = eof1_bytecode(rjumpv({-4}, 0) + OP_RETF, 1);
auto code = eof1_bytecode(OP_PUSH0 + rjumpv({-4}, 0) + OP_RETF, 1);

EXPECT_EQ(validate_eof(code), EOFValidationError::stack_height_mismatch);
}
Expand Down
3 changes: 2 additions & 1 deletion test/utils/bytecode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ inline bytecode rjumpi(int16_t offset, bytecode condition)

inline bytecode rjumpv(const std::initializer_list<int16_t> offsets, bytecode condition)
{
bytecode ret = condition + OP_RJUMPV + static_cast<Opcode>(offsets.size());
assert(offsets.size() > 0);
bytecode ret = condition + OP_RJUMPV + static_cast<Opcode>(offsets.size() - 1);
for (const auto offset : offsets)
ret += bytecode{big_endian(offset)};
return ret;
Expand Down

0 comments on commit 892025a

Please sign in to comment.