Skip to content

Commit

Permalink
test: Do not bump depth in host functions
Browse files Browse the repository at this point in the history
This removes the test cases where host functions dump the wasm call
depth. This is still possible (two cases doing that remain) but not
recommended implementation.
  • Loading branch information
chfast committed Mar 15, 2021
1 parent 7dd5e66 commit dff0752
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
9 changes: 5 additions & 4 deletions test/unittests/execute_call_depth_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ TEST(execute_call_depth, execute_imported_host_function_calling_wasm_function)
static int recorded_depth;
constexpr auto host_f = [](std::any&, Instance& instance, const Value*, int depth) noexcept {
recorded_depth = depth;
return fizzy::execute(instance, 1, {}, depth + 1);
return fizzy::execute(instance, 1, {}, depth);
};

const auto module = parse(wasm);
Expand All @@ -111,8 +111,9 @@ TEST(execute_call_depth, execute_imported_host_function_calling_wasm_function)
EXPECT_THAT(execute(*instance, 0, {}, DepthLimit - 2), Result(1_u32));
EXPECT_EQ(recorded_depth, DepthLimit - 2);

// Host function is not included in the depth limit, so 1 slot is enough.
recorded_depth = -1000;
EXPECT_THAT(execute(*instance, 0, {}, DepthLimit - 1), Traps());
EXPECT_THAT(execute(*instance, 0, {}, DepthLimit - 1), Result(1_u32));
EXPECT_EQ(recorded_depth, DepthLimit - 1);

recorded_depth = -1000;
Expand Down Expand Up @@ -210,7 +211,7 @@ TEST(execute_call_depth, call_host_function_calling_wasm_function)
static int recorded_depth;
constexpr auto host_f = [](std::any&, Instance& instance, const Value*, int depth) noexcept {
recorded_depth = depth;
return fizzy::execute(instance, 2, {}, depth + 1);
return fizzy::execute(instance, 2, {}, depth);
};

const auto module = parse(wasm);
Expand All @@ -225,7 +226,7 @@ TEST(execute_call_depth, call_host_function_calling_wasm_function)
EXPECT_EQ(recorded_depth, DepthLimit - 2);

recorded_depth = -1000;
EXPECT_THAT(execute(*instance, 1, {}, DepthLimit - 2), Traps());
EXPECT_THAT(execute(*instance, 1, {}, DepthLimit - 2), Result(1_u32));
EXPECT_EQ(recorded_depth, DepthLimit - 1);

recorded_depth = -1000;
Expand Down
18 changes: 11 additions & 7 deletions test/unittests/execute_call_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,16 +895,19 @@ TEST(execute_call, imported_function_from_another_module_max_depth)
constexpr auto sub = [](std::any& host_context, Instance&, const Value* args,
int depth) noexcept {
auto [inst1, idx] = *std::any_cast<std::pair<Instance*, FuncIdx>>(&host_context);
return fizzy::execute(*inst1, idx, args, depth + 1);
return fizzy::execute(*inst1, idx, args, depth);
};

auto host_context = std::make_any<std::pair<Instance*, FuncIdx>>(instance1.get(), *func_idx);

auto instance2 = instantiate(
std::move(module2), {{{sub, std::move(host_context)}, instance1->module->typesec[0]}});

EXPECT_THAT(execute(*instance2, 2, {}, TestCallStackLimit - 1 - 1), Traps());
EXPECT_THAT(execute(*instance2, 3, {}, TestCallStackLimit - 1 - 1), Result());
EXPECT_THAT(execute(*instance2, 2, {}, TestCallStackLimit - 2), Result());
EXPECT_THAT(execute(*instance2, 2, {}, TestCallStackLimit - 1), Traps());

EXPECT_THAT(execute(*instance2, 3, {}, TestCallStackLimit - 2), Result());
EXPECT_THAT(execute(*instance2, 3, {}, TestCallStackLimit - 1), Traps());
}

TEST(execute_call, count_calls_to_imported_function)
Expand Down Expand Up @@ -976,6 +979,7 @@ TEST(execute_call, call_imported_infinite_recursion)
constexpr auto host_foo = [](std::any&, Instance& instance, const Value* args,
int depth) noexcept {
++counter;
// Host functions may use Fizzy depth limit to protect themselves.
return execute(instance, 0, args, depth + 1);
};
const auto host_foo_type = module->typesec[0];
Expand Down Expand Up @@ -1009,7 +1013,7 @@ TEST(execute_call, call_imported_interleaved_infinite_recursion)
// Function $f will increase depth. This means each iteration goes 2 steps deeper.
EXPECT_LT(depth, CallStackLimit);
++counter;
return execute(instance, 1, args, depth + 1);
return execute(instance, 1, args, depth);
};
const auto host_foo_type = module->typesec[0];

Expand All @@ -1018,12 +1022,12 @@ TEST(execute_call, call_imported_interleaved_infinite_recursion)
// Start with the imported host function.
counter = 0;
EXPECT_THAT(execute(*instance, 0, {}), Traps());
EXPECT_EQ(counter, CallStackLimit / 2);
EXPECT_EQ(counter, CallStackLimit);

// Start with the wasm function.
counter = 0;
EXPECT_THAT(execute(*instance, 1, {}), Traps());
EXPECT_EQ(counter, CallStackLimit / 2);
EXPECT_EQ(counter, CallStackLimit - 1);
}

TEST(execute_call, call_imported_max_depth_recursion)
Expand Down Expand Up @@ -1064,7 +1068,7 @@ TEST(execute_call, call_via_imported_max_depth_recursion)
// Function $f will increase depth. This means each iteration goes 2 steps deeper.
if (depth == TestCallStackLimit - 1)
return Value{uint32_t{1}}; // Terminate recursion on the max depth.
return execute(instance, 1, args, depth + 1);
return execute(instance, 1, args, depth);
};
const auto host_foo_type = module->typesec[0];

Expand Down

0 comments on commit dff0752

Please sign in to comment.