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 a1c2224
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/unittests/evm_memory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,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 a1c2224

Please sign in to comment.