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

Validate constant expressions for data offset initialization #393

Merged
merged 3 commits into from
Jun 26, 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
3 changes: 3 additions & 0 deletions lib/fizzy/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ Module parse(bytes_view input)
if (!module.datasec.empty() && !module.has_memory())
throw validation_error{"data section encountered without a memory section"};

for (const auto& data : module.datasec)
validate_constant_expression(data.offset, module);

if (module.imported_table_types.size() > 1)
throw validation_error{"too many imported tables (at most one is allowed)"};

Expand Down
13 changes: 0 additions & 13 deletions test/unittests/instantiate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,19 +677,6 @@ TEST(instantiate, data_section_offset_from_imported_global)
EXPECT_EQ(instance->memory->substr(42, 2), "aaff"_bytes);
}

TEST(instantiate, data_section_offset_from_mutable_global)
{
Module module;
module.memorysec.emplace_back(Memory{{1, 1}});
module.globalsec.emplace_back(
Global{{ValType::i32, true}, {ConstantExpression::Kind::Constant, {42}}});
// Memory contents: 0, 0xaa, 0xff, 0, ...
module.datasec.emplace_back(Data{{ConstantExpression::Kind::GlobalGet, {0}}, {0xaa, 0xff}});

EXPECT_THROW_MESSAGE(instantiate(module), instantiate_error,
"constant expression can use global_get only for const globals");
}

TEST(instantiate, data_section_offset_too_large)
{
Module module;
Expand Down
16 changes: 11 additions & 5 deletions test/unittests/parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,12 +1263,18 @@ TEST(parser, data_section_empty)

TEST(parser, data_section)
{
const auto section_contents =
make_vec({"0041010b02aaff"_bytes, "0041020b025555"_bytes, "0023000b022424"_bytes});
const auto bin = bytes{wasm_prefix} + make_section(5, make_vec({"0000"_bytes})) +
make_section(11, section_contents);

/* wat2wasm
(global (import "m" "g") i32)
(memory 0)
(data (i32.const 1) "\aa\ff")
(data (i32.const 2) "\55\55")
(data (global.get 0) "\24\24")
*/
const auto bin = from_hex(
"0061736d01000000020801016d0167037f0005030100000b16030041010b02aaff0041020b0255550023000b02"
"2424");
const auto module = parse(bin);

ASSERT_EQ(module.datasec.size(), 3);
EXPECT_EQ(module.datasec[0].offset.kind, ConstantExpression::Kind::Constant);
EXPECT_EQ(module.datasec[0].offset.value.constant, 1);
Expand Down
22 changes: 22 additions & 0 deletions test/unittests/validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ TEST(validation, table_multi_min_limit)
parse(bin), validation_error, "too many table sections (at most one is allowed)");
}

TEST(validation, data_section_invalid_offset_expression)
{
/* wat2wasm --no-check
(memory 1 1)
(data (global.get 0) "\aa\ff")
*/
const auto bin1 = from_hex("0061736d010000000504010101010b08010023000b02aaff");

EXPECT_THROW_MESSAGE(
parse(bin1), validation_error, "invalid global index in constant expression");

/* wat2wasm --no-check
(global (mut i32) (i32.const 42))
(memory 1 1)
(data (global.get 0) "\aa\ff")
*/
const auto bin2 = from_hex("0061736d010000000504010101010606017f01412a0b0b08010023000b02aaff");

EXPECT_THROW_MESSAGE(parse(bin2), validation_error,
"constant expression can use global.get only for const globals");
}

TEST(validation, i32_store_no_memory)
{
/* wat2wasm --no-check
Expand Down