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 2, 2020
1 parent 8b3c2b8 commit 9295c81
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/unittests/execute_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,37 @@ TEST(execute, memory_grow)
EXPECT_THAT(execute(module, 0, {0xffffffe}), Result(-1));
}

TEST(execute, memory_grow_custom_hard_limit)
{
/* wat2wasm
(memory 1)
(func (param i32) (result i32)
get_local 0
memory.grow
)
*/
const auto wasm =
from_hex("0061736d0100000001060160017f017f0302010005030100010a08010600200040000b");

const auto instance1 = instantiate(parse(wasm), {}, {}, {}, {}, 16);
EXPECT_THAT(execute(*instance1, 0, {0}), Result(1));

const auto instance2 = instantiate(parse(wasm), {}, {}, {}, {}, 16);
EXPECT_THAT(execute(*instance2, 0, {1}), Result(1));

// 1 MB memory.
const auto instance3 = instantiate(parse(wasm), {}, {}, {}, {}, 16);
EXPECT_THAT(execute(*instance3, 0, {15}), Result(1));

// >1 MB memory.
const auto instance4 = instantiate(parse(wasm), {}, {}, {}, {}, 16);
EXPECT_THAT(execute(*instance4, 0, {16}), Result(-1));

// Way too high (but still within bounds)
const auto instance5 = instantiate(parse(wasm), {}, {}, {}, {}, 16);
EXPECT_THAT(execute(*instance5, 0, {0xffffffe}), Result(-1));
}

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

TEST(instantiate, memory_single_custom_hard_limit)
{
/* wat2wasm
(memory 32 64)
*/
const auto bin = from_hex("0061736d01000000050401012040");
const auto module = parse(bin);

EXPECT_THROW_MESSAGE(instantiate(module, {}, {}, {}, {}, 8), instantiate_error,
"cannot exceed hard memory limit of 524288 bytes");
EXPECT_THROW_MESSAGE(instantiate(module, {}, {}, {}, {}, 48), instantiate_error,
"cannot exceed hard memory limit of 3145728 bytes");

EXPECT_NO_THROW(instantiate(module, {}, {}, {}, {}, 64));
EXPECT_NO_THROW(instantiate(module, {}, {}, {}, {}, 128));
}

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

0 comments on commit 9295c81

Please sign in to comment.