@@ -31,7 +31,7 @@ constexpr auto CODE_SECTION_SIZE_SIZE = sizeof(uint16_t);
31
31
constexpr auto CONTAINER_SECTION_SIZE_SIZE = sizeof (uint32_t );
32
32
constexpr auto CODE_SECTION_NUMBER_LIMIT = 1024 ;
33
33
constexpr auto CONTAINER_SECTION_NUMBER_LIMIT = 256 ;
34
- constexpr auto MAX_STACK_HEIGHT = 0x03FF ;
34
+ constexpr auto MAX_STACK_INCREASE_LIMIT = 0x03FF ;
35
35
constexpr auto OUTPUTS_INPUTS_NUMBER_LIMIT = 0x7F ;
36
36
constexpr auto REL_OFFSET_SIZE = sizeof (int16_t );
37
37
constexpr auto STACK_SIZE_LIMIT = 1024 ;
@@ -237,7 +237,7 @@ EOFValidationError validate_types(bytes_view container, const EOF1Header& header
237
237
{
238
238
for (size_t i = 0 ; i < header.get_type_count (); ++i)
239
239
{
240
- const auto [inputs, outputs, max_stack_height ] = header.get_type (container, i);
240
+ const auto [inputs, outputs, max_stack_increase ] = header.get_type (container, i);
241
241
242
242
// First type should be (0, 0x80)
243
243
if (i == 0 && (inputs != 0 || outputs != NON_RETURNING_FUNCTION))
@@ -247,8 +247,8 @@ EOFValidationError validate_types(bytes_view container, const EOF1Header& header
247
247
inputs > OUTPUTS_INPUTS_NUMBER_LIMIT)
248
248
return EOFValidationError::inputs_outputs_num_above_limit;
249
249
250
- if (max_stack_height > MAX_STACK_HEIGHT )
251
- return EOFValidationError::max_stack_height_above_limit ;
250
+ if (max_stack_increase > MAX_STACK_INCREASE_LIMIT )
251
+ return EOFValidationError::max_stack_increase_above_limit ;
252
252
}
253
253
254
254
return EOFValidationError::success;
@@ -423,8 +423,11 @@ bool validate_rjump_destinations(bytes_view code) noexcept
423
423
return true ;
424
424
}
425
425
426
+ // / Validates stack height of the function.
427
+ // /
426
428
// / Requires that the input is validated against truncation.
427
- std::variant<EOFValidationError, int32_t > validate_max_stack_height (
429
+ // / Returns computed max stack increase or the validation error.
430
+ std::variant<int32_t , EOFValidationError> validate_stack_height (
428
431
bytes_view code, size_t func_index, const EOF1Header& header, bytes_view container)
429
432
{
430
433
// Special value used for detecting errors.
@@ -467,8 +470,7 @@ std::variant<EOFValidationError, int32_t> validate_max_stack_height(
467
470
const auto callee_type = header.get_type (container, fid);
468
471
stack_height_required = callee_type.inputs ;
469
472
470
- if (stack_height.max + callee_type.max_stack_height - stack_height_required >
471
- STACK_SIZE_LIMIT)
473
+ if (stack_height.max + callee_type.max_stack_increase > STACK_SIZE_LIMIT)
472
474
return EOFValidationError::stack_overflow;
473
475
474
476
// Instruction validation ensures target function is returning
@@ -480,8 +482,7 @@ std::variant<EOFValidationError, int32_t> validate_max_stack_height(
480
482
const auto fid = read_uint16_be (&code[i + 1 ]);
481
483
const auto callee_type = header.get_type (container, fid);
482
484
483
- if (stack_height.max + callee_type.max_stack_height - callee_type.inputs >
484
- STACK_SIZE_LIMIT)
485
+ if (stack_height.max + callee_type.max_stack_increase > STACK_SIZE_LIMIT)
485
486
return EOFValidationError::stack_overflow;
486
487
487
488
if (callee_type.outputs == NON_RETURNING_FUNCTION)
@@ -601,7 +602,8 @@ std::variant<EOFValidationError, int32_t> validate_max_stack_height(
601
602
602
603
const auto max_stack_height_it = std::ranges::max_element (stack_heights,
603
604
[](StackHeightRange lhs, StackHeightRange rhs) noexcept { return lhs.max < rhs.max ; });
604
- return max_stack_height_it->max ;
605
+ const auto max_stack_increase = max_stack_height_it->max - type.inputs ;
606
+ return max_stack_increase;
605
607
}
606
608
607
609
EOFValidationError validate_eof1 (
@@ -684,16 +686,16 @@ EOFValidationError validate_eof1(
684
686
return EOFValidationError::invalid_rjump_destination;
685
687
686
688
// Validate stack
687
- auto msh_or_error = validate_max_stack_height (
689
+ const auto shi_or_error = validate_stack_height (
688
690
header.get_code (container, code_idx), code_idx, header, container);
689
- if (const auto * error = std::get_if<EOFValidationError>(&msh_or_error ))
691
+ if (const auto * error = std::get_if<EOFValidationError>(&shi_or_error ))
690
692
return *error;
691
693
// TODO(clang-tidy): Too restrictive, see
692
694
// https://github.com/llvm/llvm-project/issues/120867.
693
695
// NOLINTNEXTLINE(modernize-use-integer-sign-comparison)
694
- if (std::get<int32_t >(msh_or_error ) !=
695
- header.get_type (container, code_idx).max_stack_height )
696
- return EOFValidationError::invalid_max_stack_height ;
696
+ if (std::get<int32_t >(shi_or_error ) !=
697
+ header.get_type (container, code_idx).max_stack_increase )
698
+ return EOFValidationError::invalid_max_stack_increase ;
697
699
}
698
700
699
701
if (std::ranges::find (visited_code_sections, false ) != visited_code_sections.end ())
@@ -975,10 +977,10 @@ std::string_view get_error_message(EOFValidationError err) noexcept
975
977
return " invalid_type_section_size" ;
976
978
case EOFValidationError::invalid_first_section_type:
977
979
return " invalid_first_section_type" ;
978
- case EOFValidationError::invalid_max_stack_height :
979
- return " invalid_max_stack_height " ;
980
- case EOFValidationError::max_stack_height_above_limit :
981
- return " max_stack_height_above_limit " ;
980
+ case EOFValidationError::invalid_max_stack_increase :
981
+ return " invalid_max_stack_increase " ;
982
+ case EOFValidationError::max_stack_increase_above_limit :
983
+ return " max_stack_increase_above_limit " ;
982
984
case EOFValidationError::inputs_outputs_num_above_limit:
983
985
return " inputs_outputs_num_above_limit" ;
984
986
case EOFValidationError::no_terminating_instruction:
0 commit comments