Skip to content

Commit

Permalink
noexcept
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Oct 21, 2020
1 parent 298750a commit 1a34274
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/fizzy/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ inline fizzy::ExecutionResult unwrap(const FizzyExecutionResult& result) noexcep
}

fizzy::ExecutionResult unwrapped_external_function(
void* context, fizzy::Instance& instance, const fizzy::Value* args, int depth)
void* context, fizzy::Instance& instance, const fizzy::Value* args, int depth) noexcept
{
const auto* c_external_function = static_cast<const FizzyExternalFunction*>(context);
return unwrap(c_external_function->function(
Expand Down
4 changes: 2 additions & 2 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ void branch(const Code& code, OperandStack& stack, const Instr*& pc, const uint8
}

inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instance& instance,
OperandStack& stack, int depth)
OperandStack& stack, int depth) noexcept
{
const auto num_args = func_type.inputs.size();
assert(stack.size() >= num_args);
Expand All @@ -507,7 +507,7 @@ inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instan

} // namespace

ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, int depth)
ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, int depth) noexcept
{
assert(depth >= 0);
if (depth > CallStackLimit)
Expand Down
5 changes: 3 additions & 2 deletions lib/fizzy/execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ constexpr ExecutionResult Void{true};
constexpr ExecutionResult Trap{false};

// Execute a function on an instance.
ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args, int depth = 0);
ExecutionResult execute(
Instance& instance, FuncIdx func_idx, const Value* args, int depth = 0) noexcept;

inline ExecutionResult execute(
Instance& instance, FuncIdx func_idx, std::initializer_list<Value> args)
Instance& instance, FuncIdx func_idx, std::initializer_list<Value> args) noexcept
{
assert(args.size() == instance.module->get_function_type(func_idx).inputs.size());
return execute(instance, func_idx, args.begin());
Expand Down
2 changes: 1 addition & 1 deletion lib/fizzy/instantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ std::optional<ExportedFunction> find_exported_function(Instance& instance, std::
return std::nullopt;

const auto idx = *opt_index;
auto func = [](void* context, fizzy::Instance&, const Value* args, int depth) {
auto func = [](void* context, fizzy::Instance&, const Value* args, int depth) noexcept {
auto* instance_and_idx = static_cast<std::pair<Instance*, FuncIdx>*>(context);
return execute(*instance_and_idx->first, instance_and_idx->second, args, depth);
};
Expand Down
3 changes: 2 additions & 1 deletion lib/fizzy/instantiate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ namespace fizzy
struct ExecutionResult;
struct Instance;

using ExternalFunctionPtr = ExecutionResult (*)(void* context, Instance&, const Value*, int depth);
using ExternalFunctionPtr = ExecutionResult (*)(
void* context, Instance&, const Value*, int depth) noexcept;

struct ExternalFunction
{
Expand Down
8 changes: 5 additions & 3 deletions test/unittests/api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ using namespace fizzy::test;
namespace
{
template <int N>
ExecutionResult function_returning_value(void*, Instance&, const Value*, int)
ExecutionResult function_returning_value(void*, Instance&, const Value*, int) noexcept
{
return Value{N};
}

ExecutionResult function_returning_void(void*, Instance&, const Value*, int)
ExecutionResult function_returning_void(void*, Instance&, const Value*, int) noexcept
{
return Void;
}
Expand Down Expand Up @@ -272,7 +272,9 @@ TEST(api, find_exported_function)
"0061736d010000000105016000017f021001087370656374657374036261720000040401700000050401010102"
"0606017f0041000b07170403666f6f000001670300037461620100036d656d0200");

auto bar = [](void*, Instance&, const Value*, int) -> ExecutionResult { return Value{42}; };
auto bar = [](void*, Instance&, const Value*, int) noexcept -> ExecutionResult {
return Value{42};
};
const auto bar_type = FuncType{{}, {ValType::i32}};

auto instance_reexported_function =
Expand Down
21 changes: 13 additions & 8 deletions test/unittests/execute_call_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ TEST(execute_call, imported_function_call)

const auto module = parse(wasm);

constexpr auto host_foo = [](void*, Instance&, const Value*, int) -> ExecutionResult {
constexpr auto host_foo = [](void*, Instance&, const Value*, int) noexcept -> ExecutionResult {
return Value{42};
};
const auto host_foo_type = module->typesec[0];
Expand All @@ -297,7 +297,7 @@ TEST(execute_call, imported_function_call_with_arguments)

const auto module = parse(wasm);

auto host_foo = [](void*, Instance&, const Value* args, int) -> ExecutionResult {
auto host_foo = [](void*, Instance&, const Value* args, int) noexcept -> ExecutionResult {
return Value{as_uint32(args[0]) * 2};
};
const auto host_foo_type = module->typesec[0];
Expand Down Expand Up @@ -341,11 +341,12 @@ TEST(execute_call, imported_functions_call_indirect)
ASSERT_EQ(module->importsec.size(), 2);
ASSERT_EQ(module->codesec.size(), 2);

constexpr auto sqr = [](void*, Instance&, const Value* args, int) -> ExecutionResult {
constexpr auto sqr = [](void*, Instance&, const Value* args, int) noexcept -> ExecutionResult {
const auto x = as_uint32(args[0]);
return Value{uint64_t{x} * uint64_t{x}};
};
constexpr auto isqrt = [](void*, Instance&, const Value* args, int) -> ExecutionResult {
constexpr auto isqrt = [](void*, Instance&, const Value* args,
int) noexcept -> ExecutionResult {
const auto x = as_uint32(args[0]);
return Value{(11 + uint64_t{x} / 11) / 2};
};
Expand Down Expand Up @@ -564,7 +565,8 @@ TEST(execute_call, call_imported_infinite_recursion)
const auto wasm = from_hex("0061736d010000000105016000017f020b01036d6f6403666f6f0000");

const auto module = parse(wasm);
auto host_foo = [](void*, Instance& instance, const Value* args, int depth) -> ExecutionResult {
auto host_foo = [](void*, Instance& instance, const Value* args,
int depth) noexcept -> ExecutionResult {
EXPECT_LE(depth, MaxDepth);
return execute(instance, 0, args, depth + 1);
};
Expand All @@ -587,7 +589,8 @@ TEST(execute_call, call_via_imported_infinite_recursion)
"0061736d010000000105016000017f020b01036d6f6403666f6f0000030201000a0601040010000b");

const auto module = parse(wasm);
auto host_foo = [](void*, Instance& instance, const Value* args, int depth) -> ExecutionResult {
auto host_foo = [](void*, Instance& instance, const Value* args,
int depth) noexcept -> ExecutionResult {
// Function $f will increase depth. This means each iteration goes 2 steps deeper.
EXPECT_LE(depth, MaxDepth - 1);
return execute(instance, 1, args, depth + 1);
Expand All @@ -607,7 +610,8 @@ TEST(execute_call, call_imported_max_depth_recursion)
const auto wasm = from_hex("0061736d010000000105016000017f020b01036d6f6403666f6f0000");

const auto module = parse(wasm);
auto host_foo = [](void*, Instance& instance, const Value* args, int depth) -> ExecutionResult {
auto host_foo = [](void*, Instance& instance, const Value* args,
int depth) noexcept -> ExecutionResult {
if (depth == MaxDepth)
return Value{uint32_t{1}}; // Terminate recursion on the max depth.
return execute(instance, 0, args, depth + 1);
Expand All @@ -631,7 +635,8 @@ TEST(execute_call, call_via_imported_max_depth_recursion)
"0061736d010000000105016000017f020b01036d6f6403666f6f0000030201000a0601040010000b");

const auto module = parse(wasm);
auto host_foo = [](void*, Instance& instance, const Value* args, int depth) -> ExecutionResult {
auto host_foo = [](void*, Instance& instance, const Value* args,
int depth) noexcept -> 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.
Expand Down
23 changes: 15 additions & 8 deletions test/unittests/execute_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,8 @@ TEST(execute, imported_function)
const auto module = parse(wasm);
ASSERT_EQ(module->typesec.size(), 1);

constexpr auto host_foo = [](void*, Instance&, const Value* args, int) -> ExecutionResult {
constexpr auto host_foo = [](void*, Instance&, const Value* args,
int) noexcept -> ExecutionResult {
return Value{as_uint32(args[0]) + as_uint32(args[1])};
};

Expand All @@ -794,10 +795,12 @@ TEST(execute, imported_two_functions)
const auto module = parse(wasm);
ASSERT_EQ(module->typesec.size(), 1);

constexpr auto host_foo1 = [](void*, Instance&, const Value* args, int) -> ExecutionResult {
constexpr auto host_foo1 = [](void*, Instance&, const Value* args,
int) noexcept -> ExecutionResult {
return Value{as_uint32(args[0]) + as_uint32(args[1])};
};
constexpr auto host_foo2 = [](void*, Instance&, const Value* args, int) -> ExecutionResult {
constexpr auto host_foo2 = [](void*, Instance&, const Value* args,
int) noexcept -> ExecutionResult {
return Value{as_uint32(args[0]) * as_uint32(args[1])};
};

Expand All @@ -821,10 +824,12 @@ TEST(execute, imported_functions_and_regular_one)
"0061736d0100000001070160027f7f017f021702036d6f6404666f6f310000036d6f6404666f6f320000030201"
"000a0901070041aa80a8010b");

constexpr auto host_foo1 = [](void*, Instance&, const Value* args, int) -> ExecutionResult {
constexpr auto host_foo1 = [](void*, Instance&, const Value* args,
int) noexcept -> ExecutionResult {
return Value{as_uint32(args[0]) + as_uint32(args[1])};
};
constexpr auto host_foo2 = [](void*, Instance&, const Value* args, int) -> ExecutionResult {
constexpr auto host_foo2 = [](void*, Instance&, const Value* args,
int) noexcept -> ExecutionResult {
return Value{as_uint32(args[0]) * as_uint32(args[1])};
};

Expand All @@ -851,10 +856,12 @@ TEST(execute, imported_two_functions_different_type)
"0061736d01000000010c0260027f7f017f60017e017e021702036d6f6404666f6f310000036d6f6404666f6f32"
"0001030201010a0901070042aa80a8010b");

constexpr auto host_foo1 = [](void*, Instance&, const Value* args, int) -> ExecutionResult {
constexpr auto host_foo1 = [](void*, Instance&, const Value* args,
int) noexcept -> ExecutionResult {
return Value{as_uint32(args[0]) + as_uint32(args[1])};
};
constexpr auto host_foo2 = [](void*, Instance&, const Value* args, int) -> ExecutionResult {
constexpr auto host_foo2 = [](void*, Instance&, const Value* args,
int) noexcept -> ExecutionResult {
return Value{args[0].i64 * args[0].i64};
};

Expand All @@ -875,7 +882,7 @@ TEST(execute, imported_function_traps)
*/
const auto wasm = from_hex("0061736d0100000001070160027f7f017f020b01036d6f6403666f6f0000");

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

Expand Down
8 changes: 4 additions & 4 deletions test/unittests/instantiate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TEST(instantiate, imported_functions)
const auto bin = from_hex("0061736d0100000001060160017f017f020b01036d6f6403666f6f0000");
const auto module = parse(bin);

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

ASSERT_EQ(instance->imported_functions.size(), 1);
Expand All @@ -53,8 +53,8 @@ TEST(instantiate, imported_functions_multiple)
"0061736d0100000001090260017f017f600000021702036d6f6404666f6f310000036d6f6404666f6f320001");
const auto module = parse(bin);

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

Expand Down Expand Up @@ -87,7 +87,7 @@ TEST(instantiate, imported_function_wrong_type)
*/
const auto bin = from_hex("0061736d0100000001060160017f017f020b01036d6f6403666f6f0000");

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

EXPECT_THROW_MESSAGE(instantiate(parse(bin), {{host_foo, nullptr, host_foo_type}}),
Expand Down
2 changes: 1 addition & 1 deletion test/utils/execute_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace fizzy::test
{
inline ExecutionResult execute(const std::unique_ptr<const Module>& module, FuncIdx func_idx,
std::initializer_list<Value> args)
std::initializer_list<Value> args) noexcept
{
auto instance = instantiate(*module);
return execute(*instance, func_idx, args);
Expand Down
2 changes: 1 addition & 1 deletion test/utils/fizzy_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace fizzy::test
{
namespace
{
fizzy::ExecutionResult env_adler32(void*, Instance& instance, const Value* args, int)
fizzy::ExecutionResult env_adler32(void*, Instance& instance, const Value* args, int) noexcept
{
assert(instance.memory != nullptr);
const auto ret = fizzy::test::adler32(
Expand Down
15 changes: 9 additions & 6 deletions tools/wasi/wasi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@ namespace
// and we are a single-run tool. This may change in the future and should reevaluate.
uvwasi_t state;

fizzy::ExecutionResult wasi_return_enosys(void*, fizzy::Instance&, const fizzy::Value*, int)
fizzy::ExecutionResult wasi_return_enosys(
void*, fizzy::Instance&, const fizzy::Value*, int) noexcept
{
return fizzy::Value{uint32_t{UVWASI_ENOSYS}};
}

fizzy::ExecutionResult wasi_proc_exit(void*, fizzy::Instance&, const fizzy::Value* args, int)
fizzy::ExecutionResult wasi_proc_exit(
void*, fizzy::Instance&, const fizzy::Value* args, int) noexcept
{
uvwasi_proc_exit(&state, static_cast<uvwasi_exitcode_t>(args[0].as<uint32_t>()));
// Should not reach this.
return fizzy::Trap;
}

fizzy::ExecutionResult wasi_fd_write(
void*, fizzy::Instance& instance, const fizzy::Value* args, int)
void*, fizzy::Instance& instance, const fizzy::Value* args, int) noexcept
{
const auto fd = args[0].as<uint32_t>();
const auto iov_ptr = args[1].as<uint32_t>();
Expand All @@ -52,7 +54,8 @@ fizzy::ExecutionResult wasi_fd_write(
return fizzy::Value{uint32_t{ret}};
}

fizzy::ExecutionResult wasi_fd_read(void*, fizzy::Instance& instance, const fizzy::Value* args, int)
fizzy::ExecutionResult wasi_fd_read(
void*, fizzy::Instance& instance, const fizzy::Value* args, int) noexcept
{
const auto fd = args[0].as<uint32_t>();
const auto iov_ptr = args[1].as<uint32_t>();
Expand All @@ -74,7 +77,7 @@ fizzy::ExecutionResult wasi_fd_read(void*, fizzy::Instance& instance, const fizz
}

fizzy::ExecutionResult wasi_fd_prestat_get(
void*, fizzy::Instance& instance, const fizzy::Value* args, int)
void*, fizzy::Instance& instance, const fizzy::Value* args, int) noexcept
{
const auto fd = args[0].as<uint32_t>();
const auto prestat_ptr = args[1].as<uint32_t>();
Expand All @@ -88,7 +91,7 @@ fizzy::ExecutionResult wasi_fd_prestat_get(
}

fizzy::ExecutionResult wasi_environ_sizes_get(
void*, fizzy::Instance& instance, const fizzy::Value* args, int)
void*, fizzy::Instance& instance, const fizzy::Value* args, int) noexcept
{
const auto environc = args[0].as<uint32_t>();
const auto environ_buf_size = args[1].as<uint32_t>();
Expand Down

0 comments on commit 1a34274

Please sign in to comment.