Skip to content

Commit

Permalink
Fix code sizes in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hugo-dc committed Jan 17, 2024
1 parent 5568c28 commit 89434de
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 75 deletions.
5 changes: 3 additions & 2 deletions lib/evmone/eof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#include <limits>
#include <numeric>
#include <ostream>
#include <unordered_set>
#include <span>
#include <stack>
#include <unordered_set>
#include <variant>
#include <vector>

Expand Down Expand Up @@ -215,7 +215,8 @@ std::variant<std::vector<EOFCodeType>, EOFValidationError> validate_types(
}

EOFValidationError validate_instructions(evmc_revision rev, const EOF1Header& header,
size_t code_idx, bytes_view container, std::unordered_set<uint16_t>& accessed_code_sections) noexcept
size_t code_idx, bytes_view container,
std::unordered_set<uint16_t>& accessed_code_sections) noexcept
{
const bytes_view code{header.get_code(container, code_idx)};
assert(!code.empty()); // guaranteed by EOF headers validation
Expand Down
14 changes: 6 additions & 8 deletions test/unittests/eof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>(i + 1)));
else
code_sections_256 += "5B5B00";
for (int i = 0; i < 255; ++i)
code_sections_256 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
code_sections_256 += "5B5B00";

const TestCase test_cases[] = {
{"EF00 01 010004 0200010001 040000 00 00800000 00", 4, 0, {1}},
Expand All @@ -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<uint16_t>(256, 1)},
4 * 256, 4096, std::vector<uint16_t>(256, 3)},

Check warning on line 59 in test/unittests/eof_test.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/eof_test.cpp#L59

Added line #L59 was not covered by tests
};

for (const auto& test_case : test_cases)
Expand Down
93 changes: 28 additions & 65 deletions test/unittests/eof_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>(i + 1)));
code_sections_1025 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
}
else if (i == 1023)
{
code_sections_1024 += "5B5B00";
code_sections_1025 += "E5" + hex(big_endian(static_cast<uint16_t>(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<uint16_t>(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;
Expand Down Expand Up @@ -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<uint16_t>(i + 1)));
else
code_sections_1023 += "5B5B00";
for (auto i = 0; i < 1022; ++i)
code_sections_1023 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
code_sections_1023 += "5B5B00";

const auto code = "EF0001 010FFC 0203FF " + 1023 * bytecode{"0003"} + "040000 00" +
1023 * bytecode{"00800000"} + code_sections_1023;
Expand All @@ -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<uint16_t>(i + 1)));
else
code_sections_1024 += "5B5B00";
for (auto i = 0; i < 1023; ++i)
code_sections_1024 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
code_sections_1024 += "5B5B00";

const auto code = "EF0001 011000 020400 " + 1024 * bytecode{"0003"} + "040000 00" +
1024 * bytecode{"00800000"} + code_sections_1024;
Expand Down Expand Up @@ -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<uint16_t>(i)));
code_sections_256_err_254 +=
"E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
}
else if (i == 254)
{
code_sections_256_err_001 +=
"E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
code_sections_256_err_254 += "E5" + hex(big_endian(static_cast<uint16_t>(i)));
}
else
{
code_sections_256_err_001 +=
"E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
code_sections_256_err_254 +=
"E5" + hex(big_endian(static_cast<uint16_t>(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<uint16_t>(i + 1))), 0, 0x80, 0);
code_sections_256_err_254.code(
bytecode{OP_JUMPF} + hex(big_endian(static_cast<uint16_t>(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);
}
}

0 comments on commit 89434de

Please sign in to comment.