From b3d97482475eee5a9e9eb55c6ce92dfb4d1ca38f Mon Sep 17 00:00:00 2001 From: Andrei Maiboroda Date: Wed, 29 Sep 2021 20:34:43 +0200 Subject: [PATCH] Make EOF header validation more generic (supporting any sections) --- lib/evmone/eof.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/evmone/eof.cpp b/lib/evmone/eof.cpp index a6601e22a2..e05c4a1357 100644 --- a/lib/evmone/eof.cpp +++ b/lib/evmone/eof.cpp @@ -16,8 +16,11 @@ constexpr uint8_t MAGIC = 0x00; constexpr uint8_t TERMINATOR = 0x00; constexpr uint8_t CODE_SECTION = 0x01; constexpr uint8_t DATA_SECTION = 0x02; +constexpr uint8_t MAX_SECTION = DATA_SECTION; -std::pair validate_eof_headers(bytes_view code) noexcept +using EOFSectionHeaders = std::array; + +std::pair validate_eof_headers(bytes_view code) noexcept { enum class State { @@ -28,7 +31,7 @@ std::pair validate_eof_headers(bytes_view code) auto state = State::section_id; uint8_t section_id = 0; - size_t section_sizes[3] = {0, 0, 0}; + EOFSectionHeaders section_headers{}; const auto code_end = code.end(); auto it = code.begin() + sizeof(MAGIC) + 2; // FORMAT + MAGIC + VERSION while (it != code_end && state != State::terminated) @@ -41,19 +44,19 @@ std::pair validate_eof_headers(bytes_view code) switch (section_id) { case TERMINATOR: - if (section_sizes[CODE_SECTION] == 0) + if (section_headers[CODE_SECTION] == 0) return {{}, EOFValidationErrror::code_section_missing}; state = State::terminated; break; case DATA_SECTION: - if (section_sizes[CODE_SECTION] == 0) + if (section_headers[CODE_SECTION] == 0) return {{}, EOFValidationErrror::code_section_missing}; - if (section_sizes[DATA_SECTION] != 0) + if (section_headers[DATA_SECTION] != 0) return {{}, EOFValidationErrror::multiple_data_sections}; state = State::section_size; break; case CODE_SECTION: - if (section_sizes[CODE_SECTION] != 0) + if (section_headers[CODE_SECTION] != 0) return {{}, EOFValidationErrror::multiple_code_sections}; state = State::section_size; break; @@ -73,7 +76,7 @@ std::pair validate_eof_headers(bytes_view code) if (section_size == 0) return {{}, EOFValidationErrror::zero_section_size}; - section_sizes[section_id] = section_size; + section_headers[section_id] = section_size; state = State::section_id; break; } @@ -87,17 +90,22 @@ std::pair validate_eof_headers(bytes_view code) if (state != State::terminated) return {{}, EOFValidationErrror::section_headers_not_terminated}; - const auto section_bodies_size = section_sizes[CODE_SECTION] + section_sizes[DATA_SECTION]; + const auto section_bodies_size = section_headers[CODE_SECTION] + section_headers[DATA_SECTION]; const auto remaining_code_size = static_cast(code_end - it); if (section_bodies_size != remaining_code_size) return {{}, EOFValidationErrror::invalid_section_bodies_size}; - return {{section_sizes[0], section_sizes[1]}, EOFValidationErrror::success}; + return {section_headers, EOFValidationErrror::success}; } std::pair validate_eof1(bytes_view code) noexcept { - return validate_eof_headers(code); + const auto [section_headers, error] = validate_eof_headers(code); + if (error != EOFValidationErrror::success) + return {{}, error}; + + const EOF1Header header{section_headers[CODE_SECTION], section_headers[DATA_SECTION]}; + return {header, EOFValidationErrror::success}; } } // namespace