1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 09:17:35 +00:00

LibWasm: Fix memory limits validator discrepancy

The spec allows the maximum size of the linear memory to be 2^16.
However, we previously only allowed 2^16-1, which caused a spec
compliance issue.
This commit is contained in:
Diego 2024-05-30 21:51:26 -07:00 committed by Ali Mohammad Pur
parent a9fdd819c3
commit cf6aa77816
2 changed files with 6 additions and 6 deletions

View File

@ -282,12 +282,12 @@ ErrorOr<void, ValidationError> Validator::validate(CodeSection const& section)
ErrorOr<void, ValidationError> Validator::validate(TableType const& type) ErrorOr<void, ValidationError> Validator::validate(TableType const& type)
{ {
return validate(type.limits(), 32); return validate(type.limits(), (1ull << 32) - 1);
} }
ErrorOr<void, ValidationError> Validator::validate(MemoryType const& type) ErrorOr<void, ValidationError> Validator::validate(MemoryType const& type)
{ {
return validate(type.limits(), 16); return validate(type.limits(), 1 << 16);
} }
ErrorOr<FunctionType, ValidationError> Validator::validate(BlockType const& type) ErrorOr<FunctionType, ValidationError> Validator::validate(BlockType const& type)
@ -309,9 +309,8 @@ ErrorOr<FunctionType, ValidationError> Validator::validate(BlockType const& type
return Errors::invalid("BlockType"sv); return Errors::invalid("BlockType"sv);
} }
ErrorOr<void, ValidationError> Validator::validate(Limits const& limits, size_t k) ErrorOr<void, ValidationError> Validator::validate(Limits const& limits, u64 bound)
{ {
auto bound = (1ull << k) - 1;
auto check_bound = [bound](auto value) { auto check_bound = [bound](auto value) {
return static_cast<u64>(value) <= bound; return static_cast<u64>(value) <= bound;
}; };
@ -319,8 +318,9 @@ ErrorOr<void, ValidationError> Validator::validate(Limits const& limits, size_t
if (!check_bound(limits.min())) if (!check_bound(limits.min()))
return Errors::out_of_bounds("limit minimum"sv, limits.min(), 0, bound); return Errors::out_of_bounds("limit minimum"sv, limits.min(), 0, bound);
if (limits.max().has_value() && (limits.max().value() < limits.min() || !check_bound(*limits.max()))) if (limits.max().has_value() && (limits.max().value() < limits.min() || !check_bound(*limits.max()))) {
return Errors::out_of_bounds("limit maximum"sv, limits.max().value(), limits.min(), bound); return Errors::out_of_bounds("limit maximum"sv, limits.max().value(), limits.min(), bound);
}
return {}; return {};
} }

View File

@ -254,7 +254,7 @@ public:
ErrorOr<void, ValidationError> validate_instruction(Instruction const&, Stack& stack, bool& is_constant); ErrorOr<void, ValidationError> validate_instruction(Instruction const&, Stack& stack, bool& is_constant);
// Types // Types
ErrorOr<void, ValidationError> validate(Limits const&, size_t k); // n <= 2^k-1 && m? <= 2^k-1 ErrorOr<void, ValidationError> validate(Limits const&, u64 bound); // n <= bound && m? <= bound
ErrorOr<FunctionType, ValidationError> validate(BlockType const&); ErrorOr<FunctionType, ValidationError> validate(BlockType const&);
ErrorOr<void, ValidationError> validate(FunctionType const&) { return {}; } ErrorOr<void, ValidationError> validate(FunctionType const&) { return {}; }
ErrorOr<void, ValidationError> validate(TableType const&); ErrorOr<void, ValidationError> validate(TableType const&);