Skip to content

Commit c3eef35

Browse files
committed
Fix code sizes in unit tests
1 parent 5568c28 commit c3eef35

File tree

3 files changed

+30
-75
lines changed

3 files changed

+30
-75
lines changed

lib/evmone/eof.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#include <limits>
1414
#include <numeric>
1515
#include <ostream>
16-
#include <unordered_set>
1716
#include <span>
1817
#include <stack>
18+
#include <unordered_set>
1919
#include <variant>
2020
#include <vector>
2121

@@ -215,7 +215,8 @@ std::variant<std::vector<EOFCodeType>, EOFValidationError> validate_types(
215215
}
216216

217217
EOFValidationError validate_instructions(evmc_revision rev, const EOF1Header& header,
218-
size_t code_idx, bytes_view container, std::unordered_set<uint16_t>& accessed_code_sections) noexcept
218+
size_t code_idx, bytes_view container,
219+
std::unordered_set<uint16_t>& accessed_code_sections) noexcept
219220
{
220221
const bytes_view code{header.get_code(container, code_idx)};
221222
assert(!code.empty()); // guaranteed by EOF headers validation

test/unittests/eof_test.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ TEST(eof, read_valid_eof1_header)
3636
};
3737

3838
std::string code_sections_256;
39-
for (int i = 0; i < 256; ++i)
40-
if (i < 255)
41-
code_sections_256 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
42-
else
43-
code_sections_256 += "5B5B00";
39+
for (int i = 0; i < 255; ++i)
40+
code_sections_256 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
41+
code_sections_256 += "5B5B00";
4442

4543
const TestCase test_cases[] = {
4644
{"EF00 01 010004 0200010001 040000 00 00800000 00", 4, 0, {1}},
@@ -52,13 +50,13 @@ TEST(eof, read_valid_eof1_header)
5250
12, 0, {3, 3, 3}},
5351
{"EF00 01 01000C 020003000300030003 040004 00 008000000080000000800000 E50001 E50002 5B5B00"
5452
"FFFFFFFF",
55-
12, 4, {1, 2, 3}},
53+
12, 4, {3, 3, 3}},
5654
{"EF00 01 010004 0200010100 041000 00 00800000" + hex(255 * bytecode("5B")) + "00" +
5755
std::string(8192, 'F'),
5856
4, 4096, {256}},
59-
{"EF00 01 010400 020100" + hex(256 * bytecode("0001")) + " 041000 00 " +
57+
{"EF00 01 010400 020100" + hex(256 * bytecode("0003")) + " 041000 00 " +
6058
hex(256 * bytecode("00800000")) + code_sections_256 + std::string(8192, 'F'),
61-
4 * 256, 4096, std::vector<uint16_t>(256, 1)},
59+
4 * 256, 4096, std::vector<uint16_t>(256, 3)},
6260
};
6361

6462
for (const auto& test_case : test_cases)

test/unittests/eof_validation_test.cpp

+21-65
Original file line numberDiff line numberDiff line change
@@ -311,22 +311,12 @@ TEST(eof_validation, EOF1_invalid_section_0_type)
311311

312312
TEST(eof_validation, EOF1_too_many_code_sections)
313313
{
314-
std::string code_sections_1024;
315-
std::string code_sections_1025;
316-
for (int i = 0; i < 1025; ++i)
317-
if (i < 1023)
318-
{
319-
code_sections_1024 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
320-
code_sections_1025 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
321-
}
322-
else if (i == 1023)
323-
{
324-
code_sections_1024 += "5B5B00";
325-
code_sections_1025 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
326-
}
327-
else
328-
code_sections_1025 += "5B5B00";
314+
std::string cs_calling_next;
315+
for (int i = 0; i < 1023; ++i)
316+
cs_calling_next += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
329317

318+
const std::string code_sections_1024 = cs_calling_next + "5B5B00";
319+
const std::string code_sections_1025 = cs_calling_next + "E504005B5B00";
330320

331321
const auto valid = "EF0001 011000" + bytecode{"020400"} + 0x400 * bytecode{"0003"} +
332322
"040000 00" + 0x400 * bytecode{"00800000"} + code_sections_1024;
@@ -873,11 +863,9 @@ TEST(eof_validation, multiple_code_sections_headers)
873863
TEST(eof_validation, many_code_sections_1023)
874864
{
875865
std::string code_sections_1023;
876-
for (auto i = 0; i < 1023; ++i)
877-
if (i < 1022)
878-
code_sections_1023 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
879-
else
880-
code_sections_1023 += "5B5B00";
866+
for (auto i = 0; i < 1022; ++i)
867+
code_sections_1023 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
868+
code_sections_1023 += "5B5B00";
881869

882870
const auto code = "EF0001 010FFC 0203FF " + 1023 * bytecode{"0003"} + "040000 00" +
883871
1023 * bytecode{"00800000"} + code_sections_1023;
@@ -888,11 +876,9 @@ TEST(eof_validation, many_code_sections_1023)
888876
TEST(eof_validation, many_code_sections_1024)
889877
{
890878
std::string code_sections_1024;
891-
for (auto i = 0; i < 1024; ++i)
892-
if (i < 1023)
893-
code_sections_1024 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
894-
else
895-
code_sections_1024 += "5B5B00";
879+
for (auto i = 0; i < 1023; ++i)
880+
code_sections_1024 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
881+
code_sections_1024 += "5B5B00";
896882

897883
const auto code = "EF0001 011000 020400 " + 1024 * bytecode{"0003"} + "040000 00" +
898884
1024 * bytecode{"00800000"} + code_sections_1024;
@@ -1248,54 +1234,24 @@ TEST(eof_validation, unreachable_code_sections)
12481234
}
12491235

12501236
{
1251-
std::string section_size_256;
1252-
std::string section_types_256;
1253-
std::string code_sections_256_err_001;
1254-
std::string code_sections_256_err_254;
1255-
for (int i = 0; i < 256; ++i)
1237+
auto code_sections_256_err_001 = eof_bytecode(bytecode{OP_JUMPF} + "0001").code(bytecode{OP_JUMPF} + "0001", 0, 0x80, 0);
1238+
auto code_sections_256_err_254 = eof_bytecode(bytecode{OP_JUMPF} + "0001").code(bytecode{OP_JUMPF} + "0002", 0, 0x80, 0);
1239+
for (int i = 2; i < 254; ++i)
12561240
{
1257-
section_size_256 += "0003";
1258-
section_types_256 += "00800000";
1259-
if (i < 255)
1260-
if (i == 1)
1261-
{
1262-
code_sections_256_err_001 += "E5" + hex(big_endian(static_cast<uint16_t>(i)));
1263-
code_sections_256_err_254 +=
1264-
"E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
1265-
}
1266-
else if (i == 254)
1267-
{
1268-
code_sections_256_err_001 +=
1269-
"E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
1270-
code_sections_256_err_254 += "E5" + hex(big_endian(static_cast<uint16_t>(i)));
1271-
}
1272-
else
1273-
{
1274-
code_sections_256_err_001 +=
1275-
"E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
1276-
code_sections_256_err_254 +=
1277-
"E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
1278-
}
1279-
else
1280-
{
1281-
code_sections_256_err_001 += "5B5B00";
1282-
code_sections_256_err_254 += "5B5B00";
1283-
}
1241+
code_sections_256_err_001.code(bytecode{OP_JUMPF} + hex(big_endian(static_cast<uint16_t>(i + 1))), 0, 0x80, 0);
1242+
code_sections_256_err_254.code(bytecode{OP_JUMPF} + hex(big_endian(static_cast<uint16_t>(i + 1))), 0, 0x80, 0);;
12841243
}
12851244

1245+
code_sections_256_err_001.code(bytecode{OP_JUMPF} + "00FF", 0, 0x80, 0).code(3 * bytecode{"5B"} + OP_STOP, 0, 0x80, 0);
1246+
code_sections_256_err_254.code(bytecode{OP_JUMPF} + "00FE", 0, 0x80, 0).code(3 * bytecode{"5B"} + OP_STOP, 0, 0x80, 0);
1247+
12861248
// Code Section 1 calls itself instead of code section 2, leaving code section 2 unreachable
1287-
EXPECT_EQ(
1288-
validate_eof(from_spaced_hex("EF00 01 010400 020100" + section_size_256 + "040000 00" +
1289-
section_types_256 + code_sections_256_err_001)
1290-
.value()),
1249+
EXPECT_EQ(validate_eof(code_sections_256_err_001),
12911250
EOFValidationError::unreachable_code_sections);
12921251

12931252
// Code Section 254 calls itself instead of code section 255, leaving code section 255
12941253
// unreachable
1295-
EXPECT_EQ(
1296-
validate_eof(from_spaced_hex("EF00 01 010400 020100" + section_size_256 + "040000 00" +
1297-
section_types_256 + code_sections_256_err_254)
1298-
.value()),
1254+
EXPECT_EQ(validate_eof(code_sections_256_err_254),
12991255
EOFValidationError::unreachable_code_sections);
13001256
}
13011257
}

0 commit comments

Comments
 (0)