From de3e232beb4312a2cf1573761ca4b04fe9681095 Mon Sep 17 00:00:00 2001 From: Andrei Maiboroda Date: Wed, 3 May 2023 14:10:43 +0200 Subject: [PATCH] Unit tests for MCOPY --- test/unittests/evm_memory_test.cpp | 40 +++++++++++++++++++++++++++++- test/utils/bytecode.hpp | 5 ++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/test/unittests/evm_memory_test.cpp b/test/unittests/evm_memory_test.cpp index 2ef008555e..7887fd0542 100644 --- a/test/unittests/evm_memory_test.cpp +++ b/test/unittests/evm_memory_test.cpp @@ -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}, @@ -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) { @@ -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); +} diff --git a/test/utils/bytecode.hpp b/test/utils/bytecode.hpp index cab98883b5..471386f421 100644 --- a/test/utils/bytecode.hpp +++ b/test/utils/bytecode.hpp @@ -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;