Skip to content

Commit

Permalink
test: Avoid dead host functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Dec 1, 2020
1 parent 217c084 commit 2570ac8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
27 changes: 10 additions & 17 deletions test/unittests/capi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

using namespace fizzy::test;

/// Represents an invalid/mocked pointer to a host function for tests without execution.
static constexpr FizzyExternalFn NullFn = nullptr;

TEST(capi, validate)
{
uint8_t wasm_prefix[]{0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00};
Expand Down Expand Up @@ -314,11 +317,7 @@ TEST(capi, instantiate_imported_function)
module = fizzy_parse(wasm.data(), wasm.size());
ASSERT_NE(module, nullptr);

FizzyExternalFunction host_funcs[] = {{{FizzyValueTypeI32, nullptr, 0},
[](void*, FizzyInstance*, const FizzyValue*, int) {
return FizzyExecutionResult{false, true, {42}};
},
nullptr}};
FizzyExternalFunction host_funcs[] = {{{FizzyValueTypeI32, nullptr, 0}, NullFn, nullptr}};

auto instance = fizzy_instantiate(module, host_funcs, 1, nullptr, nullptr, nullptr, 0);
EXPECT_NE(instance, nullptr);
Expand Down Expand Up @@ -415,10 +414,8 @@ TEST(capi, resolve_instantiate_no_imports)
module = fizzy_parse(wasm.data(), wasm.size());
ASSERT_NE(module, nullptr);

FizzyImportedFunction host_funcs[] = {{"mod", "foo",
{{FizzyValueTypeVoid, nullptr, 0},
[](void*, FizzyInstance*, const FizzyValue*, int) { return FizzyExecutionResult{}; },
nullptr}}};
FizzyImportedFunction host_funcs[] = {
{"mod", "foo", {{FizzyValueTypeVoid, nullptr, 0}, NullFn, nullptr}}};

instance = fizzy_resolve_instantiate(module, host_funcs, 1, nullptr, nullptr, nullptr, 0);
EXPECT_NE(instance, nullptr);
Expand All @@ -445,20 +442,16 @@ TEST(capi, resolve_instantiate)
module = fizzy_parse(wasm.data(), wasm.size());
ASSERT_NE(module, nullptr);

FizzyExternalFn host_fn = [](void* context, FizzyInstance*, const FizzyValue*, int) {
return FizzyExecutionResult{true, false, *static_cast<FizzyValue*>(context)};
};

const FizzyValueType input_type = FizzyValueTypeI32;
FizzyValue result_int{42};
FizzyExternalFunction mod1foo1 = {{FizzyValueTypeI32, &input_type, 1}, host_fn, &result_int};
FizzyExternalFunction mod1foo2 = {{FizzyValueTypeI64, &input_type, 1}, host_fn, &result_int};
FizzyExternalFunction mod1foo1 = {{FizzyValueTypeI32, &input_type, 1}, NullFn, &result_int};
FizzyExternalFunction mod1foo2 = {{FizzyValueTypeI64, &input_type, 1}, NullFn, &result_int};
FizzyValue result_f32;
result_f32.f32 = 42;
FizzyExternalFunction mod2foo1 = {{FizzyValueTypeF32, &input_type, 1}, host_fn, &result_f32};
FizzyExternalFunction mod2foo1 = {{FizzyValueTypeF32, &input_type, 1}, NullFn, &result_f32};
FizzyValue result_f64;
result_f64.f64 = 42;
FizzyExternalFunction mod2foo2 = {{FizzyValueTypeF64, &input_type, 1}, host_fn, &result_f64};
FizzyExternalFunction mod2foo2 = {{FizzyValueTypeF64, &input_type, 1}, NullFn, &result_f64};

FizzyImportedFunction host_funcs[] = {{"mod1", "foo1", mod1foo1}, {"mod1", "foo2", mod1foo2},
{"mod2", "foo1", mod2foo1}, {"mod2", "foo2", mod2foo2}};
Expand Down
5 changes: 1 addition & 4 deletions test/unittests/execute_control_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,11 +664,8 @@ TEST(execute_control, br_1_out_of_function_and_imported_function)
"0061736d010000000108026000006000017f02150108696d706f727465640866756e6374696f6e000003020101"
"0a0d010b00034041010c010b41000b");

constexpr auto fake_imported_function = [](Instance&, const Value*,
int) noexcept -> ExecutionResult { return Void; };

const auto module = parse(bin);
auto instance = instantiate(*module, {{fake_imported_function, module->typesec[0]}});
auto instance = instantiate(*module, {{nullptr, module->typesec[0]}});
EXPECT_THAT(execute(*instance, 1, {}), Result(1));
}

Expand Down
36 changes: 24 additions & 12 deletions test/unittests/instantiate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ using namespace fizzy::test;

namespace
{
ExecutionResult host_fn_1(Instance&, const Value*, int) noexcept
{
return Trap;
}

ExecutionResult host_fn_2(Instance&, const Value*, int) noexcept
{
return Trap;
}

uint32_t call_table_func(Instance& instance, size_t idx)
{
const auto& elem = (*instance.table)[idx];
Expand All @@ -24,6 +34,13 @@ uint32_t call_table_func(Instance& instance, size_t idx)
}
} // namespace

TEST(instantiate, check_test_host_functions)
{
Instance instance{{}, {nullptr, nullptr}, {}, {}, {nullptr, nullptr}, {}, {}, {}, {}};
EXPECT_THAT(host_fn_1(instance, nullptr, 0), Traps());
EXPECT_THAT(host_fn_2(instance, nullptr, 0), Traps());
}

TEST(instantiate, imported_functions)
{
/* wat2wasm
Expand All @@ -32,11 +49,10 @@ TEST(instantiate, imported_functions)
const auto bin = from_hex("0061736d0100000001060160017f017f020b01036d6f6403666f6f0000");
const auto module = parse(bin);

auto host_foo = [](Instance&, const Value*, int) { return Trap; };
auto instance = instantiate(*module, {{host_foo, module->typesec[0]}});
auto instance = instantiate(*module, {{host_fn_1, module->typesec[0]}});

ASSERT_EQ(instance->imported_functions.size(), 1);
EXPECT_EQ(*instance->imported_functions[0].function.target<decltype(host_foo)>(), host_foo);
EXPECT_EQ(*instance->imported_functions[0].function.target<decltype(&host_fn_1)>(), host_fn_1);
ASSERT_EQ(instance->imported_functions[0].type.inputs.size(), 1);
EXPECT_EQ(instance->imported_functions[0].type.inputs[0], ValType::i32);
ASSERT_EQ(instance->imported_functions[0].type.outputs.size(), 1);
Expand All @@ -53,18 +69,16 @@ TEST(instantiate, imported_functions_multiple)
"0061736d0100000001090260017f017f600000021702036d6f6404666f6f310000036d6f6404666f6f320001");
const auto module = parse(bin);

auto host_foo1 = [](Instance&, const Value*, int) { return Trap; };
auto host_foo2 = [](Instance&, const Value*, int) { return Trap; };
auto instance =
instantiate(*module, {{host_foo1, module->typesec[0]}, {host_foo2, module->typesec[1]}});
instantiate(*module, {{host_fn_1, module->typesec[0]}, {host_fn_2, module->typesec[1]}});

ASSERT_EQ(instance->imported_functions.size(), 2);
EXPECT_EQ(*instance->imported_functions[0].function.target<decltype(host_foo1)>(), host_foo1);
EXPECT_EQ(*instance->imported_functions[0].function.target<decltype(&host_fn_1)>(), host_fn_1);
ASSERT_EQ(instance->imported_functions[0].type.inputs.size(), 1);
EXPECT_EQ(instance->imported_functions[0].type.inputs[0], ValType::i32);
ASSERT_EQ(instance->imported_functions[0].type.outputs.size(), 1);
EXPECT_EQ(instance->imported_functions[0].type.outputs[0], ValType::i32);
EXPECT_EQ(*instance->imported_functions[1].function.target<decltype(host_foo2)>(), host_foo2);
EXPECT_EQ(*instance->imported_functions[1].function.target<decltype(&host_fn_2)>(), host_fn_2);
EXPECT_TRUE(instance->imported_functions[1].type.inputs.empty());
EXPECT_TRUE(instance->imported_functions[1].type.outputs.empty());
}
Expand All @@ -87,10 +101,8 @@ TEST(instantiate, imported_function_wrong_type)
*/
const auto bin = from_hex("0061736d0100000001060160017f017f020b01036d6f6403666f6f0000");

auto host_foo = [](Instance&, const Value*, int) { return Trap; };
const auto host_foo_type = FuncType{{}, {}};

EXPECT_THROW_MESSAGE(instantiate(parse(bin), {{host_foo, host_foo_type}}), instantiate_error,
const auto host_fn_type = FuncType{{}, {}};
EXPECT_THROW_MESSAGE(instantiate(parse(bin), {{host_fn_1, host_fn_type}}), instantiate_error,
"function 0 type doesn't match module's imported function type");
}

Expand Down

0 comments on commit 2570ac8

Please sign in to comment.