Skip to content

Commit

Permalink
Change DATALOADN immediate argument to mean offset in bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed May 16, 2023
1 parent 19bc929 commit f31a835
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/evmone/eof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ EOFValidationError validate_instructions(
else if (op == OP_DATALOADN)
{
const auto index = read_uint16_be(&code[i + 1]);
if (index >= header.data_size / 32)
if (header.data_size < 32 || index > header.data_size - 32)
return EOFValidationError::invalid_dataloadn_index;
i += 2;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/evmone/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -946,8 +946,7 @@ inline code_iterator dataloadn(StackTop stack, ExecutionState& state, code_itera
{
const auto index = read_uint16_be(&pos[1]);

const auto begin = static_cast<size_t>(index * 32);
stack.push(intx::be::unsafe::load<uint256>(&state.data[begin]));
stack.push(intx::be::unsafe::load<uint256>(&state.data[index]));
return pos + 3;
}

Expand Down
19 changes: 14 additions & 5 deletions test/unittests/eof_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,12 @@ TEST(eof_validation, dataloadn)
EOFValidationError::success);

// DATALOADN{1}
EXPECT_EQ(validate_eof("EF0001 010004 0200010005 030040 00 00000001 b900015000"
EXPECT_EQ(validate_eof("EF0001 010004 0200010005 030021 00 00000001 b900015000"
"000000000000000011111111111111112222222222222222333333333333333344"),
EOFValidationError::success);

// DATALOADN{32}
EXPECT_EQ(validate_eof("EF0001 010004 0200010005 030040 00 00000001 b900205000"
"0000000000000000111111111111111122222222222222223333333333333333"
"0000000000000000111111111111111122222222222222223333333333333333"),
EOFValidationError::success);
Expand All @@ -1266,7 +1271,12 @@ TEST(eof_validation, dataloadn)
EOFValidationError::invalid_dataloadn_index);

// DATALOADN{1} - out of data section bounds
EXPECT_EQ(validate_eof("EF0001 010004 0200010005 030020 00 00000001 b900015000"
EXPECT_EQ(validate_eof("EF0001 010004 0200010005 030001 00 00000001 b900015000"
"00"),
EOFValidationError::invalid_dataloadn_index);

// DATALOADN{32} - out of data section bounds
EXPECT_EQ(validate_eof("EF0001 010004 0200010005 030020 00 00000001 b900205000"
"0000000000000000111111111111111122222222222222223333333333333333"),
EOFValidationError::invalid_dataloadn_index);

Expand All @@ -1275,9 +1285,8 @@ TEST(eof_validation, dataloadn)
"0000000000000000111111111111111122222222222222223333333333333333"),
EOFValidationError::invalid_dataloadn_index);

// DATALOADN{2} - truncated word
EXPECT_EQ(validate_eof("EF0001 010004 0200010005 03005f 00 00000001 b900025000"
"0000000000000000111111111111111122222222222222223333333333333333"
// DATALOADN{32} - truncated word
EXPECT_EQ(validate_eof("EF0001 010004 0200010005 03003F 00 00000001 b900205000"
"0000000000000000111111111111111122222222222222223333333333333333"
"00000000000000001111111111111111222222222222222233333333333333"),
EOFValidationError::invalid_dataloadn_index);
Expand Down
7 changes: 7 additions & 0 deletions test/unittests/evm_eof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ TEST_P(evm, eof1_dataloadn)
code = eof1_bytecode(bytecode(OP_DATALOADN) + "0001" + ret_top(), 2, data);
execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size),
"00000000000000111111111111111122222222222222223333333333333333aa"_hex);

// DATALOADN{32}
code = eof1_bytecode(bytecode(OP_DATALOADN) + "0020" + ret_top(), 2, data);
execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size),
"aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccccccccccccccccdddddddddddddddd"_hex);
}
Expand Down

0 comments on commit f31a835

Please sign in to comment.