Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Check argument types in fizzy::test::execute()
Browse files Browse the repository at this point in the history
chfast committed Dec 28, 2020
1 parent dc09315 commit b96047f
Showing 4 changed files with 87 additions and 5 deletions.
1 change: 1 addition & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -296,6 +296,7 @@ jobs:
./wat2wasm4cpp.py test/unittests/execute_call_test.cpp
./wat2wasm4cpp.py test/unittests/execute_control_test.cpp
./wat2wasm4cpp.py test/unittests/execute_floating_point_test.cpp
./wat2wasm4cpp.py test/unittests/execute_invalid_test.cpp
./wat2wasm4cpp.py test/unittests/execute_numeric_test.cpp
./wat2wasm4cpp.py test/unittests/execute_test.cpp
./wat2wasm4cpp.py test/unittests/instantiate_test.cpp
1 change: 1 addition & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ target_sources(
execute_floating_point_conversion_test.cpp
execute_floating_point_test.cpp
execute_floating_point_test.hpp
execute_invalid_test.cpp
execute_numeric_test.cpp
execute_test.cpp
floating_point_utils_test.cpp
63 changes: 63 additions & 0 deletions test/unittests/execute_invalid_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Fizzy: A fast WebAssembly interpreter
// Copyright 2020 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

#include "parser.hpp"
#include <gtest/gtest.h>
#include <test/utils/asserts.hpp>
#include <test/utils/execute_helpers.hpp>
#include <test/utils/hex.hpp>

using namespace fizzy;
using namespace fizzy::test;

TEST(execute_invalid, invalid_number_of_arguments)
{
/* wat2wasm
(func)
(func (param i32))
(func (param f32 f32))
*/
const auto wasm = from_hex(
"0061736d01000000010d0360000060017f0060027d7d000304030001020a0a0302000b02000b02000b");

auto instance = instantiate(parse(wasm));

EXPECT_THROW_MESSAGE(
execute(*instance, 0, {1_u32}), std::invalid_argument, "too many arguments");

EXPECT_THROW_MESSAGE(execute(*instance, 1, {}), std::invalid_argument, "too few arguments");
EXPECT_THROW_MESSAGE(
execute(*instance, 1, {1_u32, 2_u32}), std::invalid_argument, "too many arguments");

EXPECT_THROW_MESSAGE(execute(*instance, 2, {}), std::invalid_argument, "too few arguments");
EXPECT_THROW_MESSAGE(execute(*instance, 2, {0.0f}), std::invalid_argument, "too few arguments");
EXPECT_THROW_MESSAGE(
execute(*instance, 2, {0.0f, 0.0f, 0.0f}), std::invalid_argument, "too many arguments");
}

TEST(execute_invalid, wrong_argument_types)
{
/* wat2wasm
(func (param i32))
(func (param f32 f32))
*/
const auto wasm =
from_hex("0061736d01000000010a0260017f0060027d7d0003030200010a070202000b02000b");

auto instance = instantiate(parse(wasm));

EXPECT_THROW_MESSAGE(
execute(*instance, 0, {0_u64}), std::invalid_argument, "invalid type of the argument 0");
EXPECT_THROW_MESSAGE(
execute(*instance, 0, {0.0}), std::invalid_argument, "invalid type of the argument 0");
EXPECT_THROW_MESSAGE(
execute(*instance, 0, {0.0f}), std::invalid_argument, "invalid type of the argument 0");

EXPECT_THROW_MESSAGE(
execute(*instance, 1, {0, 0}), std::invalid_argument, "invalid type of the argument 0");
EXPECT_THROW_MESSAGE(execute(*instance, 1, {0.0f, 0.0}), std::invalid_argument,
"invalid type of the argument 1");
EXPECT_THROW_MESSAGE(
execute(*instance, 1, {0, 0.0f}), std::invalid_argument, "invalid type of the argument 0");
}
27 changes: 22 additions & 5 deletions test/utils/execute_helpers.hpp
Original file line number Diff line number Diff line change
@@ -6,21 +6,38 @@

#include "execute.hpp"
#include <test/utils/instantiate_helpers.hpp>
#include <test/utils/typed_value.hpp>
#include <algorithm>
#include <initializer_list>

namespace fizzy::test
{
inline ExecutionResult execute(
Instance& instance, FuncIdx func_idx, std::initializer_list<Value> args)
Instance& instance, FuncIdx func_idx, std::initializer_list<TypedValue> typed_args)
{
assert(args.size() == instance.module->get_function_type(func_idx).inputs.size());
return fizzy::execute(instance, func_idx, args.begin());
const auto& func_type = instance.module->get_function_type(func_idx);
const auto [typed_arg_it, type_it] = std::mismatch(std::cbegin(typed_args),
std::cend(typed_args), std::cbegin(func_type.inputs), std::cend(func_type.inputs),
[](const auto& typed_arg, auto type) { return typed_arg.type == type; });
if (typed_arg_it != std::cend(typed_args) && type_it != std::cend(func_type.inputs))
throw std::invalid_argument{"invalid type of the argument " +
std::to_string(typed_arg_it - std::cbegin(typed_args))};
else if (typed_arg_it != std::cend(typed_args))
throw std::invalid_argument{"too many arguments"};
else if (type_it != std::cend(func_type.inputs))
throw std::invalid_argument{"too few arguments"};

std::vector<fizzy::Value> args(typed_args.size());
std::transform(std::cbegin(typed_args), std::cend(typed_args), std::begin(args),
[](const auto& typed_arg) { return typed_arg.value; });

return fizzy::execute(instance, func_idx, args.data());
}

inline ExecutionResult execute(const std::unique_ptr<const Module>& module, FuncIdx func_idx,
std::initializer_list<Value> args)
std::initializer_list<TypedValue> typed_args)
{
auto instance = instantiate(*module);
return test::execute(*instance, func_idx, args);
return test::execute(*instance, func_idx, typed_args);
}
} // namespace fizzy::test

0 comments on commit b96047f

Please sign in to comment.