Skip to content

Commit

Permalink
test: Extend call depth tests
Browse files Browse the repository at this point in the history
  • Loading branch information
axic authored and chfast committed Oct 7, 2020
1 parent e8eae59 commit d2d6241
Showing 1 changed file with 73 additions and 4 deletions.
77 changes: 73 additions & 4 deletions test/unittests/execute_call_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ TEST(execute_call, call_indirect_infinite_recursion)
EXPECT_TRUE(execute(module, 0, {}).trapped);
}

constexpr int MaxDepth = 2048;
static_assert(MaxDepth == CallStackLimit);

TEST(execute_call, call_max_depth)
{
/* wat2wasm
Expand All @@ -530,8 +533,8 @@ TEST(execute_call, call_max_depth)
const auto module = parse(bin);
auto instance = instantiate(module);

EXPECT_THAT(execute(*instance, 0, {}, 2048), Result(42));
EXPECT_THAT(execute(*instance, 1, {}, 2048), Traps());
EXPECT_THAT(execute(*instance, 0, {}, MaxDepth), Result(42));
EXPECT_THAT(execute(*instance, 1, {}, MaxDepth), Traps());
}

// A regression test for incorrect number of arguments passed to a call.
Expand Down Expand Up @@ -561,7 +564,26 @@ TEST(execute_call, call_imported_infinite_recursion)
{
/* wat2wasm
(import "mod" "foo" (func (result i32)))
(func (result i32)
*/
const auto wasm = from_hex("0061736d010000000105016000017f020b01036d6f6403666f6f0000");

const auto module = parse(wasm);
auto host_foo = [](Instance& instance, span<const Value>, int depth) -> ExecutionResult {
EXPECT_LE(depth, MaxDepth);
return execute(instance, 0, {}, depth + 1);
};
const auto host_foo_type = module.typesec[0];

auto instance = instantiate(module, {{host_foo, host_foo_type}});

EXPECT_THAT(execute(*instance, 0, {}), Traps());
}

TEST(execute_call, call_via_imported_infinite_recursion)
{
/* wat2wasm
(import "mod" "foo" (func (result i32)))
(func $f (result i32)
call 0
)
*/
Expand All @@ -570,13 +592,60 @@ TEST(execute_call, call_imported_infinite_recursion)

const auto module = parse(wasm);
auto host_foo = [](Instance& instance, span<const Value>, int depth) -> ExecutionResult {
// Function $f will increase depth. This means each iteration goes 2 steps deeper.
EXPECT_LE(depth, MaxDepth - 1);
return execute(instance, 1, {}, depth + 1);
};
const auto host_foo_type = module.typesec[0];

auto instance = instantiate(module, {{host_foo, host_foo_type}});

EXPECT_THAT(execute(*instance, 1, {}), Traps());
}

TEST(execute_call, call_imported_max_depth_recursion)
{
/* wat2wasm
(import "mod" "foo" (func (result i32)))
*/
const auto wasm = from_hex("0061736d010000000105016000017f020b01036d6f6403666f6f0000");

const auto module = parse(wasm);
auto host_foo = [](Instance& instance, span<const Value>, int depth) -> ExecutionResult {
if (depth == MaxDepth)
return Value{uint32_t{1}}; // Terminate recursion on the max depth.
return execute(instance, 0, {}, depth + 1);
};
const auto host_foo_type = module.typesec[0];

auto instance = instantiate(module, {{host_foo, host_foo_type}});

EXPECT_THAT(execute(*instance, 0, {}), Traps());
EXPECT_THAT(execute(*instance, 0, {}), Result(uint32_t{1}));
}

TEST(execute_call, call_via_imported_max_depth_recursion)
{
/* wat2wasm
(import "mod" "foo" (func (result i32)))
(func $f (result i32)
call 0
)
*/
const auto wasm = from_hex(
"0061736d010000000105016000017f020b01036d6f6403666f6f0000030201000a0601040010000b");

const auto module = parse(wasm);
auto host_foo = [](Instance& instance, span<const Value>, int depth) -> ExecutionResult {
// Function $f will increase depth. This means each iteration goes 2 steps deeper.
if (depth == (MaxDepth - 1))
return Value{uint32_t{1}}; // Terminate recursion on the max depth.
return execute(instance, 1, {}, depth + 1);
};
const auto host_foo_type = module.typesec[0];

auto instance = instantiate(module, {{host_foo, host_foo_type}});

EXPECT_THAT(execute(*instance, 1, {}), Result(uint32_t{1}));
}

TEST(execute_call, call_indirect_imported_table_infinite_recursion)
Expand Down

0 comments on commit d2d6241

Please sign in to comment.