From c3691b7bdb30fa67c6e56398771ca0ccea680dd5 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 27 May 2020 17:13:20 +0100 Subject: [PATCH 1/4] Raise validation error on invalid constant expression --- lib/fizzy/parser.cpp | 4 ++-- test/unittests/parser_test.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fizzy/parser.cpp b/lib/fizzy/parser.cpp index 9c23cc117..756040bcc 100644 --- a/lib/fizzy/parser.cpp +++ b/lib/fizzy/parser.cpp @@ -114,8 +114,8 @@ inline parser_result parse_constant_expression( switch (instr) { default: - throw parser_error{"unexpected instruction in the global initializer expression: " + - std::to_string(*(pos - 1))}; + throw validation_error{"unexpected instruction in the global initializer expression: " + + std::to_string(*(pos - 1))}; case Instr::end: break; diff --git a/test/unittests/parser_test.cpp b/test/unittests/parser_test.cpp index 8c5ffbfeb..bb4258423 100644 --- a/test/unittests/parser_test.cpp +++ b/test/unittests/parser_test.cpp @@ -643,7 +643,7 @@ TEST(parser, global_invalid_mutability) TEST(parser, global_initializer_expression_invalid_instruction) { const auto wasm = bytes{wasm_prefix} + make_section(6, make_vec({"7f0000"_bytes})); - EXPECT_THROW_MESSAGE(parse(wasm), parser_error, + EXPECT_THROW_MESSAGE(parse(wasm), validation_error, "unexpected instruction in the global initializer expression: 0"); } From 7984200a0267c09c2d4e26634d16c80c8e8fbdf5 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 27 May 2020 18:18:06 +0100 Subject: [PATCH 2/4] ci: update spectest expectations --- circle.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/circle.yml b/circle.yml index 131cfaa64..6f785d036 100644 --- a/circle.yml +++ b/circle.yml @@ -272,8 +272,8 @@ jobs: - benchmark: min_time: "0.01" - spectest: - expected_passed: 4859 - expected_failed: 573 + expected_passed: 4874 + expected_failed: 558 expected_skipped: 6381 sanitizers-macos: @@ -290,8 +290,8 @@ jobs: - benchmark: min_time: "0.01" - spectest: - expected_passed: 4859 - expected_failed: 573 + expected_passed: 4874 + expected_failed: 558 expected_skipped: 6381 benchmark: @@ -401,8 +401,8 @@ jobs: expected_failed: 8 expected_skipped: 7323 - spectest: - expected_passed: 4859 - expected_failed: 573 + expected_passed: 4874 + expected_failed: 558 expected_skipped: 6381 workflows: From 9e1a5d566af5076521ce097c0fdc4cb7364aac8a Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 27 May 2020 18:19:06 +0100 Subject: [PATCH 3/4] test: improve constant expression test case for globals --- lib/fizzy/parser.cpp | 4 ++-- test/unittests/parser_test.cpp | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/fizzy/parser.cpp b/lib/fizzy/parser.cpp index 756040bcc..249fc78a1 100644 --- a/lib/fizzy/parser.cpp +++ b/lib/fizzy/parser.cpp @@ -114,8 +114,8 @@ inline parser_result parse_constant_expression( switch (instr) { default: - throw validation_error{"unexpected instruction in the global initializer expression: " + - std::to_string(*(pos - 1))}; + throw validation_error{ + "unexpected instruction in the constant expression: " + std::to_string(*(pos - 1))}; case Instr::end: break; diff --git a/test/unittests/parser_test.cpp b/test/unittests/parser_test.cpp index bb4258423..373b77679 100644 --- a/test/unittests/parser_test.cpp +++ b/test/unittests/parser_test.cpp @@ -640,11 +640,12 @@ TEST(parser, global_invalid_mutability) "unexpected byte value 2, expected 0x00 or 0x01 for global mutability"); } -TEST(parser, global_initializer_expression_invalid_instruction) +TEST(parser, global_invalid_initializer) { - const auto wasm = bytes{wasm_prefix} + make_section(6, make_vec({"7f0000"_bytes})); - EXPECT_THROW_MESSAGE(parse(wasm), validation_error, - "unexpected instruction in the global initializer expression: 0"); + // valtype=i32 mutability=0 expression= + const auto wasm = bytes{wasm_prefix} + make_section(6, make_vec({"7f003f"_bytes})); + EXPECT_THROW_MESSAGE( + parse(wasm), validation_error, "unexpected instruction in the constant expression: 63"); } TEST(parser, global_valtype_out_of_bounds) From 2250df03348f26d3aca836f6b40279594de3f3d0 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 27 May 2020 18:54:41 +0100 Subject: [PATCH 4/4] test: add test for invalid initialiser in element and data sections --- test/unittests/parser_test.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/unittests/parser_test.cpp b/test/unittests/parser_test.cpp index 373b77679..e610a81f8 100644 --- a/test/unittests/parser_test.cpp +++ b/test/unittests/parser_test.cpp @@ -874,6 +874,14 @@ TEST(parser, element_section_tableidx_nonzero) EXPECT_THROW_MESSAGE(parse(bin), parser_error, "unexpected tableidx value 1"); } +TEST(parser, element_section_invalid_initializer) +{ + // tableidx=0 expression= items=0 + const auto wasm = bytes{wasm_prefix} + make_section(9, make_vec({"003f00"_bytes})); + EXPECT_THROW_MESSAGE( + parse(wasm), validation_error, "unexpected instruction in the constant expression: 63"); +} + TEST(parser, element_section_no_table_section) { const auto wasm = @@ -1247,6 +1255,14 @@ TEST(parser, data_section_memidx_nonzero) EXPECT_THROW_MESSAGE(parse(bin), parser_error, "unexpected memidx value 1"); } +TEST(parser, data_section_invalid_initializer) +{ + // memidx=0 expression= items=0 + const auto wasm = bytes{wasm_prefix} + make_section(9, make_vec({"003f00"_bytes})); + EXPECT_THROW_MESSAGE( + parse(wasm), validation_error, "unexpected instruction in the constant expression: 63"); +} + TEST(parser, data_section_empty_vector_without_memory) { const auto bin = bytes{wasm_prefix} + make_section(11, make_vec({"0041010b00"_bytes}));