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 start function index #353

Merged
merged 4 commits into from
May 30, 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: 4896
expected_failed: 536
expected_passed: 4899
expected_failed: 533
expected_skipped: 6381

sanitizers-macos:
Expand All @@ -290,8 +290,8 @@ jobs:
- benchmark:
min_time: "0.01"
- spectest:
expected_passed: 4896
expected_failed: 536
expected_passed: 4899
expected_failed: 533
expected_skipped: 6381

benchmark:
Expand Down Expand Up @@ -401,8 +401,8 @@ jobs:
expected_failed: 8
expected_skipped: 7323
- spectest:
expected_passed: 4896
expected_failed: 536
expected_passed: 4899
expected_failed: 533
expected_skipped: 6381

workflows:
Expand Down
11 changes: 9 additions & 2 deletions lib/fizzy/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,15 @@ Module parse(bytes_view input)
throw validation_error("duplicate export name " + export_.name);
}

if (module.startfunc && *module.startfunc >= total_func_count)
throw parser_error{"invalid start function index"};
if (module.startfunc)
{
if (*module.startfunc >= total_func_count)
throw validation_error{"invalid start function index"};

const auto& func_type = module.get_function_type(*module.startfunc);
if (!func_type.inputs.empty() || !func_type.outputs.empty())
throw validation_error{"invalid start function type"};
}

// Process code. TODO: This can be done lazily.
module.codesec.reserve(code_binaries.size());
Expand Down
6 changes: 3 additions & 3 deletions test/unittests/parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,15 +794,15 @@ TEST(parser, start_invalid_index)
make_section(3, func_section) + make_section(8, start_section) +
make_section(10, code_section);

EXPECT_THROW_MESSAGE(parse(bin), parser_error, "invalid start function index");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests should better be moved to validation_test.cpp now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I did that quite a few times. Perhaps we should merge these small changes as is and then do a sweep of moving things across?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's coordinate with moving bigger code pieces.

EXPECT_THROW_MESSAGE(parse(bin), validation_error, "invalid start function index");
}

TEST(parser, start_missing_funcsec)
{
const auto start_section = "01"_bytes;
const auto bin = bytes{wasm_prefix} + make_section(8, start_section);

EXPECT_THROW_MESSAGE(parse(bin), parser_error, "invalid start function index");
EXPECT_THROW_MESSAGE(parse(bin), validation_error, "invalid start function index");
}

TEST(parser, start_module_with_imports)
Expand Down Expand Up @@ -834,7 +834,7 @@ TEST(parser, start_module_with_imports_invalid_index)
make_section(2, import_section) + make_section(3, func_section) +
make_section(8, start_section) + make_section(10, code_section);

EXPECT_THROW_MESSAGE(parse(bin), parser_error, "invalid start function index");
EXPECT_THROW_MESSAGE(parse(bin), validation_error, "invalid start function index");
}

TEST(parser, start_index_decode_out_of_bounds)
Expand Down
28 changes: 28 additions & 0 deletions test/unittests/validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,31 @@ TEST(validation, export_duplicate_name)
"00010b");
EXPECT_THROW_MESSAGE(parse(wasm_func_glob), validation_error, "duplicate export name foo");
}

TEST(validation, start_function_type)
{
/* wat2wasm --no-check
(func $start (param i32))
(start $start)
*/
const auto wasm_func_param =
from_hex("0061736d0100000001050160017f00030201000801000a040102000b");
EXPECT_THROW_MESSAGE(parse(wasm_func_param), validation_error, "invalid start function type");

/* wat2wasm --no-check
(func $start (param i32) (result i32) (return (i32.const 0)))
(start $start)
*/
const auto wasm_func_param_result =
from_hex("0061736d0100000001060160017f017f030201000801000a0701050041000f0b");
EXPECT_THROW_MESSAGE(
parse(wasm_func_param_result), validation_error, "invalid start function type");

/* wat2wasm --no-check
(func $start (result i32) (return (i32.const 0)))
(start $start)
*/
const auto wasm_func_result =
from_hex("0061736d010000000105016000017f030201000801000a0701050041000f0b");
EXPECT_THROW_MESSAGE(parse(wasm_func_result), validation_error, "invalid start function type");
}