Skip to content

Commit 945d6c4

Browse files
committed
Change validation messages for unknown memory
Use validation error message "invalid memory index N" for both N != 0 and data segments without any memory in the module. Inspired by messages in wasm reference interpreter.
1 parent d5109a9 commit 945d6c4

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/fizzy/parser.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,14 @@ inline parser_result<Data> parse(const uint8_t* pos, const uint8_t* end)
410410
{
411411
MemIdx memory_index;
412412
std::tie(memory_index, pos) = leb128u_decode<uint32_t>(pos, end);
413+
414+
// TODO: The check should be memory_index < num_of_memories (0 or 1),
415+
// but access to the memory section of the module is needed.
413416
if (memory_index != 0)
414-
throw validation_error{"unexpected memidx value " + std::to_string(memory_index)};
417+
{
418+
throw validation_error{
419+
"invalid memory index " + std::to_string(memory_index) + " (only memory 0 is allowed)"};
420+
}
415421

416422
ConstantExpression offset;
417423
// Offset expression is required to have i32 result value
@@ -560,7 +566,10 @@ std::unique_ptr<const Module> parse(bytes_view input)
560566
}
561567

562568
if (!module->datasec.empty() && !module->has_memory())
563-
throw validation_error{"data section encountered without a memory section"};
569+
{
570+
throw validation_error{
571+
"invalid memory index 0 (data section encountered without a memory section)"};
572+
}
564573

565574
for (const auto& data : module->datasec)
566575
{

test/unittests/parser_test.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,8 @@ TEST(parser, data_section_memidx_nonzero)
13241324
{
13251325
const auto section_contents = make_vec({"0141010b0100"_bytes});
13261326
const auto bin = bytes{wasm_prefix} + make_section(11, section_contents);
1327-
EXPECT_THROW_MESSAGE(parse(bin), parser_error, "unexpected memidx value 1");
1327+
EXPECT_THROW_MESSAGE(
1328+
parse(bin), validation_error, "invalid memory index 1 (only memory 0 is allowed)");
13281329
}
13291330

13301331
TEST(parser, data_section_invalid_initializer)
@@ -1338,15 +1339,15 @@ TEST(parser, data_section_invalid_initializer)
13381339
TEST(parser, data_section_empty_vector_without_memory)
13391340
{
13401341
const auto bin = bytes{wasm_prefix} + make_section(11, make_vec({"0041010b00"_bytes}));
1341-
EXPECT_THROW_MESSAGE(
1342-
parse(bin), validation_error, "data section encountered without a memory section");
1342+
EXPECT_THROW_MESSAGE(parse(bin), validation_error,
1343+
"invalid memory index 0 (data section encountered without a memory section)");
13431344
}
13441345

13451346
TEST(parser, data_section_without_memory)
13461347
{
13471348
const auto bin = bytes{wasm_prefix} + make_section(11, make_vec({"0041010b02aaff"_bytes}));
1348-
EXPECT_THROW_MESSAGE(
1349-
parse(bin), validation_error, "data section encountered without a memory section");
1349+
EXPECT_THROW_MESSAGE(parse(bin), validation_error,
1350+
"invalid memory index 0 (data section encountered without a memory section)");
13501351
}
13511352

13521353
TEST(parser, data_section_out_of_bounds)

0 commit comments

Comments
 (0)