Skip to content

Commit

Permalink
Unit tests for MCOPY
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed May 3, 2023
1 parent 37b2d30 commit de3e232
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
40 changes: 39 additions & 1 deletion test/unittests/evm_memory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ memory_access_opcode memory_access_opcodes[] = {
{OP_MLOAD, 0, -1},
{OP_MSTORE, 0, -1},
{OP_MSTORE8, 0, -1},
{OP_MCOPY, 0, 2},
{OP_MCOPY, 1, 2},
{OP_EXTCODECOPY, 1, 3},
{OP_RETURNDATACOPY, 0, 2},
{OP_LOG0, 0, 1},
Expand Down Expand Up @@ -176,7 +178,7 @@ TEST_P(evm, memory_access)
{
// This test checks if instructions accessing memory properly respond with out-of-gas
// error for combinations of memory offset and memory size arguments.
rev = EVMC_CONSTANTINOPLE;
rev = EVMC_CANCUN;

for (const auto& p : memory_access_test_cases)
{
Expand Down Expand Up @@ -238,3 +240,39 @@ TEST_P(evm, memory_access)
}
}
}

TEST_P(evm, mcopy)
{
rev = EVMC_CANCUN;
bytecode s;
s += mstore(0, push(0x0123456789abcdef000000000000000000000000000000000000000000000000_u256)) +
mcopy(32, 0, 8) + ret(32, 8);
execute(s);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
ASSERT_EQ(result.output_size, 8);
EXPECT_EQ(bytes_view(&result.output_data[0], 8), "0123456789abcdef"_hex);

// copy uninitialized memory
s = mstore(0, push(0x0000000000000000000000000000000000000000000000000123456789abcdef_u256)) +
mcopy(64, 24, 16) + ret(64, 16);
execute(s);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
ASSERT_EQ(result.output_size, 16);
EXPECT_EQ(bytes_view(&result.output_data[0], 16), "0123456789abcdef0000000000000000"_hex);

// overlapped
s = mstore(0, push(0x0123456789abcdef000000000000000000000000000000000000000000000000_u256)) +
mcopy(4, 0, 8) + ret(0, 16);
execute(s);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
ASSERT_EQ(result.output_size, 16);
EXPECT_EQ(bytes_view(&result.output_data[0], 16), "012345670123456789abcdef00000000"_hex);
}

TEST_P(evm, mcopy_memory_cost)
{
rev = EVMC_CANCUN;
const auto code = mcopy(0, 0, 1);
execute(18, code);
EXPECT_GAS_USED(EVMC_SUCCESS, 18);
}
5 changes: 5 additions & 0 deletions test/utils/bytecode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ inline bytecode mstore8(bytecode index, bytecode value)
return value + index + OP_MSTORE8;
}

inline bytecode mcopy(bytecode dst, bytecode src, bytecode length)
{
return length + src + dst + OP_MCOPY;
}

inline bytecode jump(bytecode target)
{
return target + OP_JUMP;
Expand Down

0 comments on commit de3e232

Please sign in to comment.