Skip to content

Commit

Permalink
test: custom memory hard limit
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Sep 3, 2020
1 parent 1d35c96 commit ca23b2f
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
109 changes: 109 additions & 0 deletions test/unittests/execute_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,115 @@ TEST(execute, memory_grow)
EXPECT_THAT(execute(module, 0, {0xffffffe}), Result(-1));
}

TEST(execute, memory_grow_custom_hard_limit)
{
constexpr std::pair<uint32_t, uint32_t> test_cases[]{
{0, 1},
{1, 1},
{15, 1},
{16, -1},
{0xffffffff, -1},
};

/* wat2wasm
(memory 1)
(func (param i32) (result i32)
get_local 0
memory.grow
)
*/
const auto wasm =
from_hex("0061736d0100000001060160017f017f0302010005030100010a08010600200040000b");
const auto module = parse(wasm);

for (const auto& test_case : test_cases)
{
const auto instance = instantiate(module, {}, {}, {}, {}, 16);
EXPECT_THAT(execute(*instance, 0, {test_case.first}), Result(test_case.second));
}

/* wat2wasm
(memory 1 16)
(func (param i32) (result i32)
get_local 0
memory.grow
)
*/
const auto wasm_max_limit =
from_hex("0061736d0100000001060160017f017f030201000504010101100a08010600200040000b");
const auto module_max_limit = parse(wasm_max_limit);

for (const auto& test_case : test_cases)
{
const auto instance = instantiate(module_max_limit, {}, {}, {}, {}, 32);
EXPECT_THAT(execute(*instance, 0, {test_case.first}), Result(test_case.second));
}

/* wat2wasm
(memory (import "mod" "mem") 1)
(func (param i32) (result i32)
get_local 0
memory.grow
)
*/
const auto wasm_imported = from_hex(
"0061736d0100000001060160017f017f020c01036d6f64036d656d020001030201000a08010600200040000b");
const auto module_imported = parse(wasm_imported);

for (const auto& test_case : test_cases)
{
bytes memory(PageSize, 0);
const auto instance =
instantiate(module_imported, {}, {}, {{&memory, {1, std::nullopt}}}, {}, 16);
EXPECT_THAT(execute(*instance, 0, {test_case.first}), Result(test_case.second));

bytes memory_max_limit(PageSize, 0);
const auto instance_max_limit =
instantiate(module_imported, {}, {}, {{&memory_max_limit, {1, 16}}}, {}, 32);
EXPECT_THAT(execute(*instance_max_limit, 0, {test_case.first}), Result(test_case.second));
}

/* wat2wasm
(memory (import "mod" "mem") 1 16)
(func (param i32) (result i32)
get_local 0
memory.grow
)
*/
const auto wasm_imported_max_limit = from_hex(
"0061736d0100000001060160017f017f020d01036d6f64036d656d02010110030201000a08010600200040000"
"b");
const auto module_imported_max_limit = parse(wasm_imported_max_limit);

for (const auto& test_case : test_cases)
{
bytes memory(PageSize, 0);
const auto instance =
instantiate(module_imported_max_limit, {}, {}, {{&memory, {1, 16}}}, {}, 32);
EXPECT_THAT(execute(*instance, 0, {test_case.first}), Result(test_case.second));
}

/* wat2wasm
(memory (import "mod" "mem") 1 32)
(func (param i32) (result i32)
get_local 0
memory.grow
)
*/
const auto wasm_imported_max_limit_narrowing = from_hex(
"0061736d0100000001060160017f017f020d01036d6f64036d656d02010120030201000a08010600200040000"
"b");
const auto module_imported_max_limit_narrowing = parse(wasm_imported_max_limit_narrowing);

for (const auto& test_case : test_cases)
{
bytes memory(PageSize, 0);
const auto instance =
instantiate(module_imported_max_limit_narrowing, {}, {}, {{&memory, {1, 16}}}, {}, 32);
EXPECT_THAT(execute(*instance, 0, {test_case.first}), Result(test_case.second));
}
}

TEST(execute, start_section)
{
// In this test the start function (index 1) writes a i32 value to the memory
Expand Down
59 changes: 59 additions & 0 deletions test/unittests/instantiate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,65 @@ TEST(instantiate, memory_single_large_maximum)
"cannot exceed hard memory limit of 268435456 bytes");
}

TEST(instantiate, memory_single_custom_hard_limit)
{
/* wat2wasm
(memory 2 4)
*/
const auto bin = from_hex("0061736d01000000050401010204");
const auto module = parse(bin);

EXPECT_THROW_MESSAGE(instantiate(module, {}, {}, {}, {}, 1), instantiate_error,
"cannot exceed hard memory limit of 65536 bytes");
EXPECT_THROW_MESSAGE(instantiate(module, {}, {}, {}, {}, 3), instantiate_error,
"cannot exceed hard memory limit of 196608 bytes");

EXPECT_NO_THROW(instantiate(module, {}, {}, {}, {}, 4));
EXPECT_NO_THROW(instantiate(module, {}, {}, {}, {}, 8));
}

TEST(instantiate, imported_memory_custom_hard_limit)
{
/* wat2wasm
(memory (import "mod" "mem") 2)
*/
const auto bin = from_hex("0061736d01000000020c01036d6f64036d656d020002");
const auto module = parse(bin);

bytes memory(PageSize * 3, 0);

EXPECT_THROW_MESSAGE(instantiate(module, {}, {}, {{&memory, {3, 4}}}, {}, 1), instantiate_error,
"imported memory limits cannot exceed hard memory limit of 65536 bytes");
EXPECT_THROW_MESSAGE(instantiate(module, {}, {}, {{&memory, {3, 4}}}, {}, 2), instantiate_error,
"imported memory limits cannot exceed hard memory limit of 131072 bytes");
EXPECT_THROW_MESSAGE(instantiate(module, {}, {}, {{&memory, {3, 4}}}, {}, 3), instantiate_error,
"imported memory limits cannot exceed hard memory limit of 196608 bytes");

EXPECT_NO_THROW(instantiate(module, {}, {}, {{&memory, {3, std::nullopt}}}, {}, 3));
EXPECT_NO_THROW(instantiate(module, {}, {}, {{&memory, {3, std::nullopt}}}, {}, 4));
EXPECT_NO_THROW(instantiate(module, {}, {}, {{&memory, {3, 4}}}, {}, 4));
EXPECT_NO_THROW(instantiate(module, {}, {}, {{&memory, {3, 4}}}, {}, 8));

/* wat2wasm
(memory (import "mod" "mem") 2 6)
*/
const auto bin_max_limit = from_hex("0061736d01000000020d01036d6f64036d656d02010206");
const auto module_max_limit = parse(bin_max_limit);

EXPECT_THROW_MESSAGE(instantiate(module_max_limit, {}, {}, {{&memory, {3, 4}}}, {}, 1),
instantiate_error, "imported memory limits cannot exceed hard memory limit of 65536 bytes");
EXPECT_THROW_MESSAGE(instantiate(module_max_limit, {}, {}, {{&memory, {3, 4}}}, {}, 2),
instantiate_error,
"imported memory limits cannot exceed hard memory limit of 131072 bytes");
EXPECT_THROW_MESSAGE(instantiate(module_max_limit, {}, {}, {{&memory, {3, 4}}}, {}, 3),
instantiate_error,
"imported memory limits cannot exceed hard memory limit of 196608 bytes");

EXPECT_NO_THROW(instantiate(module_max_limit, {}, {}, {{&memory, {3, 4}}}, {}, 4));
EXPECT_NO_THROW(instantiate(module_max_limit, {}, {}, {{&memory, {3, 6}}}, {}, 6));
EXPECT_NO_THROW(instantiate(module_max_limit, {}, {}, {{&memory, {3, 6}}}, {}, 8));
}

TEST(instantiate, element_section)
{
/* wat2wasm
Expand Down

0 comments on commit ca23b2f

Please sign in to comment.