Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize EOF by reading types on demand #1034

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 57 additions & 55 deletions lib/evmone/eof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,43 +205,43 @@ std::variant<EOFSectionHeaders, EOFValidationError> validate_section_headers(byt
if (remaining_container_size < section_bodies_without_data)
return EOFValidationError::invalid_section_bodies_size;

if (section_headers[TYPE_SECTION][0] != section_headers[CODE_SECTION].size() * 4)
if (section_headers[TYPE_SECTION][0] !=
section_headers[CODE_SECTION].size() * EOF1Header::TYPE_ENTRY_SIZE)
return EOFValidationError::invalid_type_section_size;

return section_headers;
}

std::variant<std::vector<EOFCodeType>, EOFValidationError> validate_types(
bytes_view container, size_t header_size, uint16_t type_section_size) noexcept
EOFValidationError validate_types(
bytes_view container, size_t type_section_offset, uint16_t type_section_size) noexcept
{
assert(!container.empty()); // guaranteed by EOF headers validation

std::vector<EOFCodeType> types;

// guaranteed by EOF headers validation
assert(header_size + type_section_size < container.size());
assert(type_section_offset + type_section_size < container.size());

for (auto offset = header_size; offset < header_size + type_section_size; offset += 4)
const auto num_types = type_section_size / EOF1Header::TYPE_ENTRY_SIZE;
for (size_t i = 0; i < num_types; ++i)
{
types.emplace_back(
container[offset], container[offset + 1], read_uint16_be(&container[offset + 2]));
}
const auto offset = type_section_offset + (i * EOF1Header::TYPE_ENTRY_SIZE);
const auto inputs = container[offset];
const auto outputs = container[offset + 1];
const auto max_stack_height = read_uint16_be(&container[offset + 2]);

// check 1st section is (0, 0x80)
if (types[0].inputs != 0 || types[0].outputs != NON_RETURNING_FUNCTION)
return EOFValidationError::invalid_first_section_type;
// First type should be (0, 0x80)
if (i == 0 && (inputs != 0 || outputs != NON_RETURNING_FUNCTION))
return EOFValidationError::invalid_first_section_type;

for (const auto& t : types)
{
if ((t.outputs > OUTPUTS_INPUTS_NUMBER_LIMIT && t.outputs != NON_RETURNING_FUNCTION) ||
t.inputs > OUTPUTS_INPUTS_NUMBER_LIMIT)
if ((outputs > OUTPUTS_INPUTS_NUMBER_LIMIT && outputs != NON_RETURNING_FUNCTION) ||
inputs > OUTPUTS_INPUTS_NUMBER_LIMIT)
return EOFValidationError::inputs_outputs_num_above_limit;

if (t.max_stack_height > MAX_STACK_HEIGHT)
if (max_stack_height > MAX_STACK_HEIGHT)
return EOFValidationError::max_stack_height_above_limit;
}

return types;
return EOFValidationError::success;
}

/// Result of validating instructions in a code section.
Expand Down Expand Up @@ -286,9 +286,11 @@ std::variant<InstructionValidationResult, EOFValidationError> validate_instructi
else if (op == OP_CALLF)
{
const auto fid = read_uint16_be(&code[i + 1]);
if (fid >= header.types.size())
if (fid >= header.code_sizes.size())
return EOFValidationError::invalid_code_section_index;
if (header.types[fid].outputs == NON_RETURNING_FUNCTION)

const auto type = header.get_type(container, fid);
if (type.outputs == NON_RETURNING_FUNCTION)
return EOFValidationError::callf_to_non_returning_function;
if (code_idx != fid)
accessed_code_sections.insert(fid);
Expand All @@ -302,10 +304,12 @@ std::variant<InstructionValidationResult, EOFValidationError> validate_instructi
else if (op == OP_JUMPF)
{
const auto fid = read_uint16_be(&code[i + 1]);
if (fid >= header.types.size())
if (fid >= header.code_sizes.size())
return EOFValidationError::invalid_code_section_index;

const auto type = header.get_type(container, fid);
// JUMPF into returning function means current function is returning.
if (header.types[fid].outputs != NON_RETURNING_FUNCTION)
if (type.outputs != NON_RETURNING_FUNCTION)
is_returning = true;
if (code_idx != fid)
accessed_code_sections.insert(fid);
Expand Down Expand Up @@ -342,7 +346,8 @@ std::variant<InstructionValidationResult, EOFValidationError> validate_instructi
i += instr::traits[op].immediate_size;
}

const auto declared_returning = (header.types[code_idx].outputs != NON_RETURNING_FUNCTION);
const auto declared_returning =
header.get_type(container, code_idx).outputs != NON_RETURNING_FUNCTION;
if (is_returning != declared_returning)
return EOFValidationError::invalid_non_returning_flag;

Expand Down Expand Up @@ -410,7 +415,7 @@ bool validate_rjump_destinations(bytes_view code) noexcept

/// Requires that the input is validated against truncation.
std::variant<EOFValidationError, int32_t> validate_max_stack_height(
bytes_view code, size_t func_index, const std::vector<EOFCodeType>& code_types)
bytes_view code, size_t func_index, const EOF1Header& header, bytes_view container)
{
// Special value used for detecting errors.
static constexpr int32_t LOC_UNVISITED = -1; // Unvisited byte.
Expand All @@ -427,8 +432,9 @@ std::variant<EOFValidationError, int32_t> validate_max_stack_height(

assert(!code.empty());

const auto type = header.get_type(container, func_index);
std::vector<StackHeightRange> stack_heights(code.size());
stack_heights[0] = {code_types[func_index].inputs, code_types[func_index].inputs};
stack_heights[0] = {type.inputs, type.inputs};

for (size_t i = 0; i < code.size();)
{
Expand All @@ -448,37 +454,36 @@ std::variant<EOFValidationError, int32_t> validate_max_stack_height(
if (opcode == OP_CALLF)
{
const auto fid = read_uint16_be(&code[i + 1]);
const auto callee_type = header.get_type(container, fid);
stack_height_required = callee_type.inputs;

stack_height_required = code_types[fid].inputs;

if (stack_height.max + code_types[fid].max_stack_height - stack_height_required >
if (stack_height.max + callee_type.max_stack_height - stack_height_required >
STACK_SIZE_LIMIT)
return EOFValidationError::stack_overflow;

// Instruction validation ensures target function is returning
assert(code_types[fid].outputs != NON_RETURNING_FUNCTION);
stack_height_change =
static_cast<int8_t>(code_types[fid].outputs - stack_height_required);
assert(callee_type.outputs != NON_RETURNING_FUNCTION);
stack_height_change = static_cast<int8_t>(callee_type.outputs - stack_height_required);
}
else if (opcode == OP_JUMPF)
{
const auto fid = read_uint16_be(&code[i + 1]);
const auto callee_type = header.get_type(container, fid);

if (stack_height.max + code_types[fid].max_stack_height - code_types[fid].inputs >
if (stack_height.max + callee_type.max_stack_height - callee_type.inputs >
STACK_SIZE_LIMIT)
return EOFValidationError::stack_overflow;

if (code_types[fid].outputs == NON_RETURNING_FUNCTION)
if (callee_type.outputs == NON_RETURNING_FUNCTION)
{
stack_height_required = code_types[fid].inputs;
stack_height_required = callee_type.inputs;
}
else
{
if (code_types[func_index].outputs < code_types[fid].outputs)
if (type.outputs < callee_type.outputs)
return EOFValidationError::jumpf_destination_incompatible_outputs;

stack_height_required = code_types[func_index].outputs + code_types[fid].inputs -
code_types[fid].outputs;
stack_height_required = type.outputs + callee_type.inputs - callee_type.outputs;

// JUMPF to returning function requires exact number of stack items
// and is allowed only in constant stack segment.
Expand All @@ -488,7 +493,7 @@ std::variant<EOFValidationError, int32_t> validate_max_stack_height(
}
else if (opcode == OP_RETF)
{
stack_height_required = code_types[func_index].outputs;
stack_height_required = type.outputs;
// RETF allowed only in constant stack segment
if (stack_height.max > stack_height_required)
return EOFValidationError::stack_higher_than_outputs_required;
Expand Down Expand Up @@ -665,10 +670,11 @@ EOFValidationError validate_eof1(

// Validate stack
auto msh_or_error = validate_max_stack_height(
header.get_code(container, code_idx), code_idx, header.types);
header.get_code(container, code_idx), code_idx, header, container);
if (const auto* error = std::get_if<EOFValidationError>(&msh_or_error))
return *error;
if (std::get<int32_t>(msh_or_error) != header.types[code_idx].max_stack_height)
if (std::get<int32_t>(msh_or_error) !=
header.get_type(container, code_idx).max_stack_height)
return EOFValidationError::invalid_max_stack_height;
}

Expand Down Expand Up @@ -759,14 +765,17 @@ std::variant<EOF1Header, EOFValidationError> validate_header(

const auto header_size = eof_header_size(section_headers);

const auto types_or_error =
validate_types(container, header_size, section_headers[TYPE_SECTION].front());
if (const auto* error = std::get_if<EOFValidationError>(&types_or_error))
return *error;
const auto& types = std::get<std::vector<EOFCodeType>>(types_or_error);
const auto type_section_offset = header_size;
const auto type_section_size = section_headers[TYPE_SECTION].front();

if (type_section_size != code_sizes.size() * EOF1Header::TYPE_ENTRY_SIZE)
return EOFValidationError::invalid_type_section_size;

const auto validation_error = validate_types(container, type_section_offset, type_section_size);
if (validation_error != EOFValidationError::success)
return validation_error;

std::vector<uint16_t> code_offsets;
const auto type_section_size = section_headers[TYPE_SECTION][0];
auto offset = header_size + type_section_size;

for (const auto code_size : code_sizes)
Expand All @@ -790,13 +799,13 @@ std::variant<EOF1Header, EOFValidationError> validate_header(

return EOF1Header{
.version = container[2],
.type_section_offset = type_section_offset,
.code_sizes = code_sizes,
.code_offsets = code_offsets,
.data_size = data_size,
.data_offset = data_offset,
.container_sizes = container_sizes,
.container_offsets = container_offsets,
.types = types,
};
}

Expand Down Expand Up @@ -829,15 +838,8 @@ EOF1Header read_valid_eof1_header(bytes_view container)
const auto header_size = eof_header_size(section_headers);

EOF1Header header;

header.version = container[2];

for (auto type_offset = header_size;
type_offset < header_size + section_headers[TYPE_SECTION][0]; type_offset += 4)
{
header.types.emplace_back(container[type_offset], container[type_offset + 1],
read_uint16_be(&container[type_offset + 2]));
}
header.type_section_offset = header_size;

header.code_sizes = section_headers[CODE_SECTION];
auto code_offset = header_size + section_headers[TYPE_SECTION][0];
Expand Down
57 changes: 38 additions & 19 deletions lib/evmone/eof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@

namespace evmone
{
/// Loads big endian int16_t from data. Unsafe.
/// TODO: Move it to intx
inline int16_t read_int16_be(auto it) noexcept
{
const uint8_t h = *it++;
const uint8_t l = *it;
return static_cast<int16_t>((h << 8) | l);
}

/// Loads big endian uint16_t from data. Unsafe.
/// TODO: Move it to intx
inline uint16_t read_uint16_be(auto it) noexcept
{
const uint8_t h = *it++;
const uint8_t l = *it;
return static_cast<uint16_t>((h << 8) | l);
}

using evmc::bytes;
using evmc::bytes_view;
using namespace evmc::literals;
Expand All @@ -40,9 +58,15 @@ struct EOFCodeType

struct EOF1Header
{
/// Size of a type entry in bytes.
static constexpr size_t TYPE_ENTRY_SIZE = sizeof(EOFCodeType);

/// The EOF version, 0 means legacy code.
uint8_t version = 0;

/// Offset of the type section start.
size_t type_section_offset = 0;
chfast marked this conversation as resolved.
Show resolved Hide resolved

/// Size of every code section.
std::vector<uint16_t> code_sizes;

Expand All @@ -62,7 +86,20 @@ struct EOF1Header
/// Offset of every container section start;
std::vector<uint16_t> container_offsets;

std::vector<EOFCodeType> types;
/// A helper to extract reference to a specific type section.
[[nodiscard]] EOFCodeType get_type(bytes_view container, size_t type_idx) const noexcept
{
const auto offset = type_section_offset + type_idx * TYPE_ENTRY_SIZE;
// TODO: Make EOFCodeType aggregate type and use designated initializers.
return EOFCodeType{
container[offset], // inputs
container[offset + 1], // outputs
read_uint16_be(&container[offset + 2]) // max_stack_height
};
}

/// Returns the number of types in the type section.
[[nodiscard]] size_t get_type_count() const noexcept { return code_sizes.size(); }

/// A helper to extract reference to a specific code section.
[[nodiscard]] bytes_view get_code(bytes_view container, size_t code_idx) const noexcept
Expand Down Expand Up @@ -182,22 +219,4 @@ enum class ContainerKind : uint8_t
/// Output operator for EOFValidationError.
EVMC_EXPORT std::ostream& operator<<(std::ostream& os, EOFValidationError err) noexcept;

/// Loads big endian int16_t from data. Unsafe.
/// TODO: Move it to intx
inline int16_t read_int16_be(auto it) noexcept
{
const uint8_t h = *it++;
const uint8_t l = *it;
return static_cast<int16_t>((h << 8) | l);
}

/// Loads big endian uint16_t from data. Unsafe.
/// TODO: Move it to intx
inline uint16_t read_uint16_be(auto it) noexcept
{
const uint8_t h = *it++;
const uint8_t l = *it;
return static_cast<uint16_t>((h << 8) | l);
}

} // namespace evmone
10 changes: 4 additions & 6 deletions lib/evmone/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,8 @@ inline code_iterator callf(StackTop stack, ExecutionState& state, code_iterator
const auto index = read_uint16_be(&pos[1]);
const auto& header = state.analysis.baseline->eof_header();
const auto stack_size = &stack.top() - state.stack_space.bottom();

const auto callee_required_stack_size =
header.types[index].max_stack_height - header.types[index].inputs;
const auto callee_type = header.get_type(state.original_code, index);
const auto callee_required_stack_size = callee_type.max_stack_height - callee_type.inputs;
if (stack_size + callee_required_stack_size > StackSpace::limit)
{
state.status = EVMC_STACK_OVERFLOW;
Expand Down Expand Up @@ -1134,9 +1133,8 @@ inline code_iterator jumpf(StackTop stack, ExecutionState& state, code_iterator
const auto index = read_uint16_be(&pos[1]);
const auto& header = state.analysis.baseline->eof_header();
const auto stack_size = &stack.top() - state.stack_space.bottom();

const auto callee_required_stack_size =
header.types[index].max_stack_height - header.types[index].inputs;
const auto callee_type = header.get_type(state.original_code, index);
const auto callee_required_stack_size = callee_type.max_stack_height - callee_type.inputs;
if (stack_size + callee_required_stack_size > StackSpace::limit)
{
state.status = EVMC_STACK_OVERFLOW;
Expand Down
3 changes: 2 additions & 1 deletion test/unittests/eof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ TEST(eof, read_valid_eof1_header)
const auto header = read_valid_eof1_header(code);
EXPECT_EQ(header.code_sizes, test_case.code_sizes) << test_case.code;
EXPECT_EQ(header.data_size, test_case.data_size) << test_case.code;
EXPECT_EQ(header.types.size() * 4, test_case.types_size) << test_case.code;
EXPECT_EQ(header.get_type_count(), test_case.types_size / EOF1Header::TYPE_ENTRY_SIZE)
<< test_case.code;
EXPECT_EQ(header.container_sizes, test_case.container_sizes) << test_case.code;
}
}
Expand Down