diff --git a/lib/evmone/eof.cpp b/lib/evmone/eof.cpp index 4bc3365214..640c197caa 100644 --- a/lib/evmone/eof.cpp +++ b/lib/evmone/eof.cpp @@ -13,9 +13,9 @@ #include #include #include -#include #include #include +#include #include #include @@ -215,7 +215,8 @@ std::variant, EOFValidationError> validate_types( } EOFValidationError validate_instructions(evmc_revision rev, const EOF1Header& header, - size_t code_idx, bytes_view container, std::unordered_set& accessed_code_sections) noexcept + size_t code_idx, bytes_view container, + std::unordered_set& accessed_code_sections) noexcept { const bytes_view code{header.get_code(container, code_idx)}; assert(!code.empty()); // guaranteed by EOF headers validation diff --git a/test/unittests/eof_test.cpp b/test/unittests/eof_test.cpp index 258a05ec2a..4030692233 100644 --- a/test/unittests/eof_test.cpp +++ b/test/unittests/eof_test.cpp @@ -36,11 +36,9 @@ TEST(eof, read_valid_eof1_header) }; std::string code_sections_256; - for (int i = 0; i < 256; ++i) - if (i < 255) - code_sections_256 += "E5" + hex(big_endian(static_cast(i + 1))); - else - code_sections_256 += "5B5B00"; + for (int i = 0; i < 255; ++i) + code_sections_256 += "E5" + hex(big_endian(static_cast(i + 1))); + code_sections_256 += "5B5B00"; const TestCase test_cases[] = { {"EF00 01 010004 0200010001 040000 00 00800000 00", 4, 0, {1}}, @@ -52,13 +50,13 @@ TEST(eof, read_valid_eof1_header) 12, 0, {3, 3, 3}}, {"EF00 01 01000C 020003000300030003 040004 00 008000000080000000800000 E50001 E50002 5B5B00" "FFFFFFFF", - 12, 4, {1, 2, 3}}, + 12, 4, {3, 3, 3}}, {"EF00 01 010004 0200010100 041000 00 00800000" + hex(255 * bytecode("5B")) + "00" + std::string(8192, 'F'), 4, 4096, {256}}, - {"EF00 01 010400 020100" + hex(256 * bytecode("0001")) + " 041000 00 " + + {"EF00 01 010400 020100" + hex(256 * bytecode("0003")) + " 041000 00 " + hex(256 * bytecode("00800000")) + code_sections_256 + std::string(8192, 'F'), - 4 * 256, 4096, std::vector(256, 1)}, + 4 * 256, 4096, std::vector(256, 3)}, }; for (const auto& test_case : test_cases) diff --git a/test/unittests/eof_validation_test.cpp b/test/unittests/eof_validation_test.cpp index a34aeb78ed..88b6d24d31 100644 --- a/test/unittests/eof_validation_test.cpp +++ b/test/unittests/eof_validation_test.cpp @@ -311,22 +311,12 @@ TEST(eof_validation, EOF1_invalid_section_0_type) TEST(eof_validation, EOF1_too_many_code_sections) { - std::string code_sections_1024; - std::string code_sections_1025; - for (int i = 0; i < 1025; ++i) - if (i < 1023) - { - code_sections_1024 += "E5" + hex(big_endian(static_cast(i + 1))); - code_sections_1025 += "E5" + hex(big_endian(static_cast(i + 1))); - } - else if (i == 1023) - { - code_sections_1024 += "5B5B00"; - code_sections_1025 += "E5" + hex(big_endian(static_cast(i + 1))); - } - else - code_sections_1025 += "5B5B00"; + std::string cs_calling_next; + for (int i = 0; i < 1023; ++i) + cs_calling_next += "E5" + hex(big_endian(static_cast(i + 1))); + const std::string code_sections_1024 = cs_calling_next + "5B5B00"; + const std::string code_sections_1025 = cs_calling_next + "E504005B5B00"; const auto valid = "EF0001 011000" + bytecode{"020400"} + 0x400 * bytecode{"0003"} + "040000 00" + 0x400 * bytecode{"00800000"} + code_sections_1024; @@ -873,11 +863,9 @@ TEST(eof_validation, multiple_code_sections_headers) TEST(eof_validation, many_code_sections_1023) { std::string code_sections_1023; - for (auto i = 0; i < 1023; ++i) - if (i < 1022) - code_sections_1023 += "E5" + hex(big_endian(static_cast(i + 1))); - else - code_sections_1023 += "5B5B00"; + for (auto i = 0; i < 1022; ++i) + code_sections_1023 += "E5" + hex(big_endian(static_cast(i + 1))); + code_sections_1023 += "5B5B00"; const auto code = "EF0001 010FFC 0203FF " + 1023 * bytecode{"0003"} + "040000 00" + 1023 * bytecode{"00800000"} + code_sections_1023; @@ -888,11 +876,9 @@ TEST(eof_validation, many_code_sections_1023) TEST(eof_validation, many_code_sections_1024) { std::string code_sections_1024; - for (auto i = 0; i < 1024; ++i) - if (i < 1023) - code_sections_1024 += "E5" + hex(big_endian(static_cast(i + 1))); - else - code_sections_1024 += "5B5B00"; + for (auto i = 0; i < 1023; ++i) + code_sections_1024 += "E5" + hex(big_endian(static_cast(i + 1))); + code_sections_1024 += "5B5B00"; const auto code = "EF0001 011000 020400 " + 1024 * bytecode{"0003"} + "040000 00" + 1024 * bytecode{"00800000"} + code_sections_1024; @@ -1248,54 +1234,31 @@ TEST(eof_validation, unreachable_code_sections) } { - std::string section_size_256; - std::string section_types_256; - std::string code_sections_256_err_001; - std::string code_sections_256_err_254; - for (int i = 0; i < 256; ++i) + auto code_sections_256_err_001 = + eof_bytecode(bytecode{OP_JUMPF} + "0001").code(bytecode{OP_JUMPF} + "0001", 0, 0x80, 0); + auto code_sections_256_err_254 = + eof_bytecode(bytecode{OP_JUMPF} + "0001").code(bytecode{OP_JUMPF} + "0002", 0, 0x80, 0); + for (int i = 2; i < 254; ++i) { - section_size_256 += "0003"; - section_types_256 += "00800000"; - if (i < 255) - if (i == 1) - { - code_sections_256_err_001 += "E5" + hex(big_endian(static_cast(i))); - code_sections_256_err_254 += - "E5" + hex(big_endian(static_cast(i + 1))); - } - else if (i == 254) - { - code_sections_256_err_001 += - "E5" + hex(big_endian(static_cast(i + 1))); - code_sections_256_err_254 += "E5" + hex(big_endian(static_cast(i))); - } - else - { - code_sections_256_err_001 += - "E5" + hex(big_endian(static_cast(i + 1))); - code_sections_256_err_254 += - "E5" + hex(big_endian(static_cast(i + 1))); - } - else - { - code_sections_256_err_001 += "5B5B00"; - code_sections_256_err_254 += "5B5B00"; - } + code_sections_256_err_001.code( + bytecode{OP_JUMPF} + hex(big_endian(static_cast(i + 1))), 0, 0x80, 0); + code_sections_256_err_254.code( + bytecode{OP_JUMPF} + hex(big_endian(static_cast(i + 1))), 0, 0x80, 0); + ; } + code_sections_256_err_001.code(bytecode{OP_JUMPF} + "00FF", 0, 0x80, 0) + .code(3 * bytecode{"5B"} + OP_STOP, 0, 0x80, 0); + code_sections_256_err_254.code(bytecode{OP_JUMPF} + "00FE", 0, 0x80, 0) + .code(3 * bytecode{"5B"} + OP_STOP, 0, 0x80, 0); + // Code Section 1 calls itself instead of code section 2, leaving code section 2 unreachable EXPECT_EQ( - validate_eof(from_spaced_hex("EF00 01 010400 020100" + section_size_256 + "040000 00" + - section_types_256 + code_sections_256_err_001) - .value()), - EOFValidationError::unreachable_code_sections); + validate_eof(code_sections_256_err_001), EOFValidationError::unreachable_code_sections); // Code Section 254 calls itself instead of code section 255, leaving code section 255 // unreachable EXPECT_EQ( - validate_eof(from_spaced_hex("EF00 01 010400 020100" + section_size_256 + "040000 00" + - section_types_256 + code_sections_256_err_254) - .value()), - EOFValidationError::unreachable_code_sections); + validate_eof(code_sections_256_err_254), EOFValidationError::unreachable_code_sections); } }