Skip to content

Commit

Permalink
Make EOF header validation more generic (supporting any sections)
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Mar 22, 2022
1 parent f463096 commit b3d9748
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions lib/evmone/eof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<EOF1Header, EOFValidationErrror> validate_eof_headers(bytes_view code) noexcept
using EOFSectionHeaders = std::array<size_t, MAX_SECTION + 1>;

std::pair<EOFSectionHeaders, EOFValidationErrror> validate_eof_headers(bytes_view code) noexcept
{
enum class State
{
Expand All @@ -28,7 +31,7 @@ std::pair<EOF1Header, EOFValidationErrror> 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)
Expand All @@ -41,19 +44,19 @@ std::pair<EOF1Header, EOFValidationErrror> 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;
Expand All @@ -73,7 +76,7 @@ std::pair<EOF1Header, EOFValidationErrror> 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;
}
Expand All @@ -87,17 +90,22 @@ std::pair<EOF1Header, EOFValidationErrror> 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<size_t>(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<EOF1Header, EOFValidationErrror> 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

Expand Down

0 comments on commit b3d9748

Please sign in to comment.