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

Raise validation error on invalid constant expression #352

Merged
merged 4 commits into from
May 27, 2020
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
12 changes: 6 additions & 6 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions lib/fizzy/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ inline parser_result<ConstantExpression> 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 constant expression: " + std::to_string(*(pos - 1))};

case Instr::end:
break;
Expand Down
25 changes: 21 additions & 4 deletions test/unittests/parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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), parser_error,
"unexpected instruction in the global initializer expression: 0");
// valtype=i32 mutability=0 expression=<memory_size>
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)
Expand Down Expand Up @@ -873,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=<memory_size> 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 =
Expand Down Expand Up @@ -1246,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=<memory_size> 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}));
Expand Down