From 64fba47ecacc4c270a5d3b033408ed28bf16cb52 Mon Sep 17 00:00:00 2001 From: Andrei Maiboroda Date: Wed, 21 Oct 2020 11:33:03 +0200 Subject: [PATCH] noexcept --- lib/fizzy/capi.cpp | 2 +- lib/fizzy/execute.cpp | 4 ++-- lib/fizzy/execute.hpp | 5 +++-- lib/fizzy/instantiate.cpp | 2 +- lib/fizzy/instantiate.hpp | 3 ++- test/unittests/api_test.cpp | 8 +++++--- test/unittests/execute_call_test.cpp | 21 +++++++++++++-------- test/unittests/execute_test.cpp | 23 +++++++++++++++-------- test/unittests/instantiate_test.cpp | 8 ++++---- test/utils/execute_helpers.hpp | 2 +- test/utils/fizzy_engine.cpp | 2 +- tools/wasi/wasi.cpp | 15 +++++++++------ 12 files changed, 57 insertions(+), 38 deletions(-) diff --git a/lib/fizzy/capi.cpp b/lib/fizzy/capi.cpp index 41966e7c5..82c126109 100644 --- a/lib/fizzy/capi.cpp +++ b/lib/fizzy/capi.cpp @@ -112,7 +112,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(context); return unwrap(c_external_function->function( diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index 4aecd44ef..bd78c7e36 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -475,7 +475,7 @@ void branch(const Code& code, OperandStack& stack, const uint8_t*& pc, uint32_t } 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); @@ -501,7 +501,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) diff --git a/lib/fizzy/execute.hpp b/lib/fizzy/execute.hpp index 6e955eab2..6c1f2c219 100644 --- a/lib/fizzy/execute.hpp +++ b/lib/fizzy/execute.hpp @@ -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 args) + Instance& instance, FuncIdx func_idx, std::initializer_list args) noexcept { assert(args.size() == instance.module->get_function_type(func_idx).inputs.size()); return execute(instance, func_idx, args.begin()); diff --git a/lib/fizzy/instantiate.cpp b/lib/fizzy/instantiate.cpp index 94cd4dfac..02f8aa884 100644 --- a/lib/fizzy/instantiate.cpp +++ b/lib/fizzy/instantiate.cpp @@ -427,7 +427,7 @@ std::optional 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*>(context); return execute(*instance_and_idx->first, instance_and_idx->second, args, depth); }; diff --git a/lib/fizzy/instantiate.hpp b/lib/fizzy/instantiate.hpp index 323113a55..377b37dff 100644 --- a/lib/fizzy/instantiate.hpp +++ b/lib/fizzy/instantiate.hpp @@ -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 { diff --git a/test/unittests/api_test.cpp b/test/unittests/api_test.cpp index 5764347b3..f9ed805ef 100644 --- a/test/unittests/api_test.cpp +++ b/test/unittests/api_test.cpp @@ -16,12 +16,12 @@ using namespace fizzy::test; namespace { template -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; } @@ -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 = diff --git a/test/unittests/execute_call_test.cpp b/test/unittests/execute_call_test.cpp index b02cd82f3..624d44792 100644 --- a/test/unittests/execute_call_test.cpp +++ b/test/unittests/execute_call_test.cpp @@ -277,7 +277,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]; @@ -304,7 +304,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]; @@ -348,11 +348,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}; }; @@ -571,7 +572,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); }; @@ -594,7 +596,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); @@ -614,7 +617,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); @@ -638,7 +642,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. diff --git a/test/unittests/execute_test.cpp b/test/unittests/execute_test.cpp index a67019586..22d929fdc 100644 --- a/test/unittests/execute_test.cpp +++ b/test/unittests/execute_test.cpp @@ -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])}; }; @@ -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])}; }; @@ -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])}; }; @@ -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}; }; @@ -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; }; diff --git a/test/unittests/instantiate_test.cpp b/test/unittests/instantiate_test.cpp index 45c8260a2..02f3c8588 100644 --- a/test/unittests/instantiate_test.cpp +++ b/test/unittests/instantiate_test.cpp @@ -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); @@ -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]}}); @@ -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}}), diff --git a/test/utils/execute_helpers.hpp b/test/utils/execute_helpers.hpp index 0a76374c2..04cbb9e4f 100644 --- a/test/utils/execute_helpers.hpp +++ b/test/utils/execute_helpers.hpp @@ -11,7 +11,7 @@ namespace fizzy::test { inline ExecutionResult execute(const std::unique_ptr& module, FuncIdx func_idx, - std::initializer_list args) + std::initializer_list args) noexcept { auto instance = instantiate(*module); return execute(*instance, func_idx, args); diff --git a/test/utils/fizzy_engine.cpp b/test/utils/fizzy_engine.cpp index c1a29964d..1b9e8c9cb 100644 --- a/test/utils/fizzy_engine.cpp +++ b/test/utils/fizzy_engine.cpp @@ -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( diff --git a/tools/wasi/wasi.cpp b/tools/wasi/wasi.cpp index 04bf56f78..d406f5d46 100644 --- a/tools/wasi/wasi.cpp +++ b/tools/wasi/wasi.cpp @@ -18,12 +18,14 @@ 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(args[0].as())); // Should not reach this. @@ -31,7 +33,7 @@ fizzy::ExecutionResult wasi_proc_exit(void*, fizzy::Instance&, const fizzy::Valu } 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(); const auto iov_ptr = args[1].as(); @@ -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(); const auto iov_ptr = args[1].as(); @@ -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(); const auto prestat_ptr = args[1].as(); @@ -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(); const auto environ_buf_size = args[1].as();