diff --git a/include/fizzy/fizzy.h b/include/fizzy/fizzy.h index 8e53cc1fb..e71b5c347 100644 --- a/include/fizzy/fizzy.h +++ b/include/fizzy/fizzy.h @@ -10,6 +10,13 @@ #include #include +/// Safe way of marking a function with `noexcept` C++ specifier. +#ifdef __cplusplus +#define FIZZY_NOEXCEPT noexcept +#else +#define FIZZY_NOEXCEPT +#endif + #ifdef __cplusplus extern "C" { #endif @@ -81,8 +88,8 @@ typedef struct FizzyExecutionContext FizzyExecutionContext; /// @note /// External functions implemented in C++ must be non-throwing, i.e. the effect of any exception /// escaping the function is std::terminate being called. -typedef FizzyExecutionResult (*FizzyExternalFn)( - void* host_ctx, FizzyInstance* instance, const FizzyValue* args, FizzyExecutionContext* ctx); +typedef FizzyExecutionResult (*FizzyExternalFn)(void* host_ctx, FizzyInstance* instance, + const FizzyValue* args, FizzyExecutionContext* ctx) FIZZY_NOEXCEPT; /// Value type. typedef uint8_t FizzyValueType; @@ -243,7 +250,8 @@ typedef struct FizzyImportedGlobal /// @param error Pointer to store detailed error information at. Can be NULL if error /// information is not required. /// @return true if module is valid, false otherwise. -bool fizzy_validate(const uint8_t* wasm_binary, size_t wasm_binary_size, FizzyError* error); +bool fizzy_validate( + const uint8_t* wasm_binary, size_t wasm_binary_size, FizzyError* error) FIZZY_NOEXCEPT; /// Parse binary module. /// @@ -253,7 +261,7 @@ bool fizzy_validate(const uint8_t* wasm_binary, size_t wasm_binary_size, FizzyEr /// information is not required. /// @return non-NULL pointer to module in case of success, NULL otherwise. const FizzyModule* fizzy_parse( - const uint8_t* wasm_binary, size_t wasm_binary_size, FizzyError* error); + const uint8_t* wasm_binary, size_t wasm_binary_size, FizzyError* error) FIZZY_NOEXCEPT; /// Free resources associated with the module. /// @@ -261,7 +269,7 @@ const FizzyModule* fizzy_parse( /// /// @note /// Should be called unless @p module was passed to fizzy_instantiate(). -void fizzy_free_module(const FizzyModule* module); +void fizzy_free_module(const FizzyModule* module) FIZZY_NOEXCEPT; /// Make a copy of a module. /// @@ -272,13 +280,13 @@ void fizzy_free_module(const FizzyModule* module); /// @note Creating a copy is needed if more than single instance of a module is required, because /// instantiation takes ownership of a module, and the same module cannot be instantiated twice. /// @note Input module is not modified neither in success nor in failure case. -const FizzyModule* fizzy_clone_module(const FizzyModule* module); +const FizzyModule* fizzy_clone_module(const FizzyModule* module) FIZZY_NOEXCEPT; /// Get number of types defined in the module. /// /// @param module Pointer to module. Cannot be NULL. /// @return Number of type in the module. -uint32_t fizzy_get_type_count(const FizzyModule* module); +uint32_t fizzy_get_type_count(const FizzyModule* module) FIZZY_NOEXCEPT; /// Get type defined in the module. /// @@ -286,13 +294,13 @@ uint32_t fizzy_get_type_count(const FizzyModule* module); /// @param type_idx Type index. Behaviour is undefined if index is not valid according /// to module definition. /// @return Type corresponding to the index. -FizzyFunctionType fizzy_get_type(const FizzyModule* module, uint32_t type_idx); +FizzyFunctionType fizzy_get_type(const FizzyModule* module, uint32_t type_idx) FIZZY_NOEXCEPT; /// Get number of imports defined in the module. /// /// @param module Pointer to module. Cannot be NULL. /// @return Number of imports in the module. -uint32_t fizzy_get_import_count(const FizzyModule* module); +uint32_t fizzy_get_import_count(const FizzyModule* module) FIZZY_NOEXCEPT; /// Get the import description defined in the module. /// @@ -303,7 +311,8 @@ uint32_t fizzy_get_import_count(const FizzyModule* module); /// FizzyImportDescription::module and FizzyImportDescription::name fields /// point to the string stored inside the module and are valid as long as /// module is alive (including after successful instantiation.) -FizzyImportDescription fizzy_get_import_description(const FizzyModule* module, uint32_t import_idx); +FizzyImportDescription fizzy_get_import_description( + const FizzyModule* module, uint32_t import_idx) FIZZY_NOEXCEPT; /// Get type of the function defined in the module. /// @@ -314,25 +323,26 @@ FizzyImportDescription fizzy_get_import_description(const FizzyModule* module, u /// @return Type of the function corresponding to the index. /// /// @note All module function indices are greater than all imported function indices. -FizzyFunctionType fizzy_get_function_type(const FizzyModule* module, uint32_t func_idx); +FizzyFunctionType fizzy_get_function_type( + const FizzyModule* module, uint32_t func_idx) FIZZY_NOEXCEPT; /// Check whether module has a table. /// /// @param module Pointer to module. Cannot be NULL. /// @return true if module has a table definition, false otherwise. -bool fizzy_module_has_table(const FizzyModule* module); +bool fizzy_module_has_table(const FizzyModule* module) FIZZY_NOEXCEPT; /// Check whether module has a memory. /// /// @param module Pointer to module. Cannot be NULL. /// @return true if module has a memory definition, false otherwise. -bool fizzy_module_has_memory(const FizzyModule* module); +bool fizzy_module_has_memory(const FizzyModule* module) FIZZY_NOEXCEPT; /// Get number of globals defined in the module. /// /// @param module Pointer to module. Cannot be NULL. /// @return Number of globals in the module. -uint32_t fizzy_get_global_count(const FizzyModule* module); +uint32_t fizzy_get_global_count(const FizzyModule* module) FIZZY_NOEXCEPT; /// Get type of a given global defined in the module. /// @@ -343,13 +353,14 @@ uint32_t fizzy_get_global_count(const FizzyModule* module); /// @return Type of the global corresponding to the index. /// /// @note All module global indices are greater than all imported global indices. -FizzyGlobalType fizzy_get_global_type(const FizzyModule* module, uint32_t global_idx); +FizzyGlobalType fizzy_get_global_type( + const FizzyModule* module, uint32_t global_idx) FIZZY_NOEXCEPT; /// Get number of exports defined in the module. /// /// @param module Pointer to module. Cannot be NULL. /// @return Number of exports in the module. -uint32_t fizzy_get_export_count(const FizzyModule* module); +uint32_t fizzy_get_export_count(const FizzyModule* module) FIZZY_NOEXCEPT; /// Get the export description defined in the module. /// @@ -360,7 +371,8 @@ uint32_t fizzy_get_export_count(const FizzyModule* module); /// FizzyExportDescription::name field points to the string stored inside the /// module and is valid as long as module is alive (including after successful /// instantiation.) -FizzyExportDescription fizzy_get_export_description(const FizzyModule* module, uint32_t export_idx); +FizzyExportDescription fizzy_get_export_description( + const FizzyModule* module, uint32_t export_idx) FIZZY_NOEXCEPT; /// Find index of exported function by name. /// @@ -369,13 +381,13 @@ FizzyExportDescription fizzy_get_export_description(const FizzyModule* module, u /// @param out_func_idx Pointer to output where function index will be stored. Cannot be NULL. /// @return true if function was found, false otherwise. bool fizzy_find_exported_function_index( - const FizzyModule* module, const char* name, uint32_t* out_func_idx); + const FizzyModule* module, const char* name, uint32_t* out_func_idx) FIZZY_NOEXCEPT; /// Check whether module has a start function. /// /// @param module Pointer to module. Cannot be NULL. /// @return true if module has a start function, false otherwise. -bool fizzy_module_has_start_function(const FizzyModule* module); +bool fizzy_module_has_start_function(const FizzyModule* module) FIZZY_NOEXCEPT; /// Instantiate a module. /// @@ -417,7 +429,8 @@ bool fizzy_module_has_start_function(const FizzyModule* module); FizzyInstance* fizzy_instantiate(const FizzyModule* module, const FizzyExternalFunction* imported_functions, size_t imported_functions_size, const FizzyExternalTable* imported_table, const FizzyExternalMemory* imported_memory, - const FizzyExternalGlobal* imported_globals, size_t imported_globals_size, FizzyError* error); + const FizzyExternalGlobal* imported_globals, size_t imported_globals_size, + FizzyError* error) FIZZY_NOEXCEPT; /// Instantiate a module resolving imported functions. /// @@ -464,12 +477,13 @@ FizzyInstance* fizzy_instantiate(const FizzyModule* module, FizzyInstance* fizzy_resolve_instantiate(const FizzyModule* module, const FizzyImportedFunction* imported_functions, size_t imported_functions_size, const FizzyExternalTable* imported_table, const FizzyExternalMemory* imported_memory, - const FizzyImportedGlobal* imported_globals, size_t imported_globals_size, FizzyError* error); + const FizzyImportedGlobal* imported_globals, size_t imported_globals_size, + FizzyError* error) FIZZY_NOEXCEPT; /// Free resources associated with the instance. /// /// @param instance Pointer to instance. If NULL is passed, function has no effect. -void fizzy_free_instance(FizzyInstance* instance); +void fizzy_free_instance(FizzyInstance* instance) FIZZY_NOEXCEPT; /// Get pointer to module of an instance. /// @@ -478,7 +492,7 @@ void fizzy_free_instance(FizzyInstance* instance); /// /// @note The returned pointer represents non-owning, "view"-access to the module and must not be /// passed to fizzy_free_module(). -const FizzyModule* fizzy_get_instance_module(FizzyInstance* instance); +const FizzyModule* fizzy_get_instance_module(FizzyInstance* instance) FIZZY_NOEXCEPT; /// Get pointer to memory of an instance. /// @@ -488,7 +502,7 @@ const FizzyModule* fizzy_get_instance_module(FizzyInstance* instance); /// @note Function returns pointer to memory regardless of whether memory is exported or not. /// @note For instances of the modules defined with memory of size 0 the returned pointer is not /// NULL. -uint8_t* fizzy_get_instance_memory_data(FizzyInstance* instance); +uint8_t* fizzy_get_instance_memory_data(FizzyInstance* instance) FIZZY_NOEXCEPT; /// Get size of memory of an instance. /// @@ -496,7 +510,7 @@ uint8_t* fizzy_get_instance_memory_data(FizzyInstance* instance); /// @return Size of memory in bytes or 0 in case instance doesn't have any memory. /// /// @note Function returns memory size regardless of whether memory is exported or not. -size_t fizzy_get_instance_memory_size(FizzyInstance* instance); +size_t fizzy_get_instance_memory_size(FizzyInstance* instance) FIZZY_NOEXCEPT; /// Find exported function by name. /// @@ -510,7 +524,7 @@ size_t fizzy_get_instance_memory_size(FizzyInstance* instance); /// modified, and fizzy_free_exported_function() must not be called. /// @return true if function was found, false otherwise. bool fizzy_find_exported_function( - FizzyInstance* instance, const char* name, FizzyExternalFunction* out_function); + FizzyInstance* instance, const char* name, FizzyExternalFunction* out_function) FIZZY_NOEXCEPT; /// Free resources associated with exported function. /// @@ -519,7 +533,7 @@ bool fizzy_find_exported_function( /// /// @note This function may not be called with external function, which was not returned from /// fizzy_find_exported_function(). -void fizzy_free_exported_function(FizzyExternalFunction* external_function); +void fizzy_free_exported_function(FizzyExternalFunction* external_function) FIZZY_NOEXCEPT; /// Find exported table by name. /// @@ -530,7 +544,7 @@ void fizzy_free_exported_function(FizzyExternalFunction* external_function); /// /// @note WebAssembly 1.0 spec allows at most one table in a module. bool fizzy_find_exported_table( - FizzyInstance* instance, const char* name, FizzyExternalTable* out_table); + FizzyInstance* instance, const char* name, FizzyExternalTable* out_table) FIZZY_NOEXCEPT; /// Find exported memory by name. /// @@ -541,7 +555,7 @@ bool fizzy_find_exported_table( /// /// @note WebAssembly 1.0 spec allows at most one memory in a module. bool fizzy_find_exported_memory( - FizzyInstance* instance, const char* name, FizzyExternalMemory* out_memory); + FizzyInstance* instance, const char* name, FizzyExternalMemory* out_memory) FIZZY_NOEXCEPT; /// Find exported global by name. /// @@ -550,7 +564,7 @@ bool fizzy_find_exported_memory( /// @param out_global Pointer to output struct to store found global. Cannot be NULL. /// @return true if global was found, false otherwise. bool fizzy_find_exported_global( - FizzyInstance* instance, const char* name, FizzyExternalGlobal* out_global); + FizzyInstance* instance, const char* name, FizzyExternalGlobal* out_global) FIZZY_NOEXCEPT; /// Execute module function. /// @@ -563,7 +577,7 @@ bool fizzy_find_exported_global( /// When number of passed arguments or their types are different from the ones defined by the /// function type, behaviour is undefined. FizzyExecutionResult fizzy_execute( - FizzyInstance* instance, uint32_t func_idx, const FizzyValue* args); + FizzyInstance* instance, uint32_t func_idx, const FizzyValue* args) FIZZY_NOEXCEPT; #ifdef __cplusplus } diff --git a/lib/fizzy/capi.cpp b/lib/fizzy/capi.cpp index 52a603aba..b6d5aeb7a 100644 --- a/lib/fizzy/capi.cpp +++ b/lib/fizzy/capi.cpp @@ -205,7 +205,7 @@ inline FizzyExternalFunction wrap(fizzy::ExternalFunction external_func) { static constexpr FizzyExternalFn c_function = [](void* host_ctx, FizzyInstance* instance, const FizzyValue* args, - FizzyExecutionContext* c_ctx) -> FizzyExecutionResult { + FizzyExecutionContext* c_ctx) noexcept -> FizzyExecutionResult { // If execution context not provided, allocate new one. // It must be allocated on heap otherwise the stack will explode in recursive calls. std::unique_ptr new_ctx; @@ -428,7 +428,8 @@ inline FizzyExportDescription wrap(const fizzy::Export& exp) noexcept } // namespace extern "C" { -bool fizzy_validate(const uint8_t* wasm_binary, size_t wasm_binary_size, FizzyError* error) + +bool fizzy_validate(const uint8_t* wasm_binary, size_t wasm_binary_size, FizzyError* error) noexcept { try { @@ -444,7 +445,7 @@ bool fizzy_validate(const uint8_t* wasm_binary, size_t wasm_binary_size, FizzyEr } const FizzyModule* fizzy_parse( - const uint8_t* wasm_binary, size_t wasm_binary_size, FizzyError* error) + const uint8_t* wasm_binary, size_t wasm_binary_size, FizzyError* error) noexcept { try { @@ -459,12 +460,12 @@ const FizzyModule* fizzy_parse( } } -void fizzy_free_module(const FizzyModule* module) +void fizzy_free_module(const FizzyModule* module) noexcept { delete unwrap(module); } -const FizzyModule* fizzy_clone_module(const FizzyModule* module) +const FizzyModule* fizzy_clone_module(const FizzyModule* module) noexcept { try { @@ -477,65 +478,66 @@ const FizzyModule* fizzy_clone_module(const FizzyModule* module) } } -uint32_t fizzy_get_type_count(const FizzyModule* module) +uint32_t fizzy_get_type_count(const FizzyModule* module) noexcept { return static_cast(unwrap(module)->typesec.size()); } -FizzyFunctionType fizzy_get_type(const FizzyModule* module, uint32_t type_idx) +FizzyFunctionType fizzy_get_type(const FizzyModule* module, uint32_t type_idx) noexcept { return wrap(unwrap(module)->typesec[type_idx]); } -uint32_t fizzy_get_import_count(const FizzyModule* module) +uint32_t fizzy_get_import_count(const FizzyModule* module) noexcept { return static_cast(unwrap(module)->importsec.size()); } FizzyImportDescription fizzy_get_import_description( - const FizzyModule* c_module, uint32_t import_idx) + const FizzyModule* c_module, uint32_t import_idx) noexcept { const auto* module = unwrap(c_module); return wrap(module->importsec[import_idx], *module); } -FizzyFunctionType fizzy_get_function_type(const FizzyModule* module, uint32_t func_idx) +FizzyFunctionType fizzy_get_function_type(const FizzyModule* module, uint32_t func_idx) noexcept { return wrap(unwrap(module)->get_function_type(func_idx)); } -bool fizzy_module_has_table(const FizzyModule* module) +bool fizzy_module_has_table(const FizzyModule* module) noexcept { return unwrap(module)->has_table(); } -bool fizzy_module_has_memory(const FizzyModule* module) +bool fizzy_module_has_memory(const FizzyModule* module) noexcept { return unwrap(module)->has_memory(); } -uint32_t fizzy_get_global_count(const FizzyModule* module) +uint32_t fizzy_get_global_count(const FizzyModule* module) noexcept { return static_cast(unwrap(module)->get_global_count()); } -FizzyGlobalType fizzy_get_global_type(const FizzyModule* module, uint32_t global_idx) +FizzyGlobalType fizzy_get_global_type(const FizzyModule* module, uint32_t global_idx) noexcept { return wrap(unwrap(module)->get_global_type(global_idx)); } -uint32_t fizzy_get_export_count(const FizzyModule* module) +uint32_t fizzy_get_export_count(const FizzyModule* module) noexcept { return static_cast(unwrap(module)->exportsec.size()); } -FizzyExportDescription fizzy_get_export_description(const FizzyModule* module, uint32_t export_idx) +FizzyExportDescription fizzy_get_export_description( + const FizzyModule* module, uint32_t export_idx) noexcept { return wrap(unwrap(module)->exportsec[export_idx]); } bool fizzy_find_exported_function_index( - const FizzyModule* module, const char* name, uint32_t* out_func_idx) + const FizzyModule* module, const char* name, uint32_t* out_func_idx) noexcept { const auto optional_func_idx = fizzy::find_exported_function_index(*unwrap(module), name); if (!optional_func_idx) @@ -546,23 +548,30 @@ bool fizzy_find_exported_function_index( } bool fizzy_find_exported_function( - FizzyInstance* instance, const char* name, FizzyExternalFunction* out_function) + FizzyInstance* instance, const char* name, FizzyExternalFunction* out_function) noexcept { auto optional_func = fizzy::find_exported_function(*unwrap(instance), name); if (!optional_func) return false; - *out_function = wrap(std::move(*optional_func)); - return true; + try + { + *out_function = wrap(std::move(*optional_func)); + return true; + } + catch (...) + { + return false; + } } -void fizzy_free_exported_function(FizzyExternalFunction* external_function) +void fizzy_free_exported_function(FizzyExternalFunction* external_function) noexcept { delete static_cast(external_function->context); } bool fizzy_find_exported_table( - FizzyInstance* instance, const char* name, FizzyExternalTable* out_table) + FizzyInstance* instance, const char* name, FizzyExternalTable* out_table) noexcept { const auto optional_external_table = fizzy::find_exported_table(*unwrap(instance), name); if (!optional_external_table) @@ -573,7 +582,7 @@ bool fizzy_find_exported_table( } bool fizzy_find_exported_memory( - FizzyInstance* instance, const char* name, FizzyExternalMemory* out_memory) + FizzyInstance* instance, const char* name, FizzyExternalMemory* out_memory) noexcept { const auto optional_external_memory = fizzy::find_exported_memory(*unwrap(instance), name); if (!optional_external_memory) @@ -584,7 +593,7 @@ bool fizzy_find_exported_memory( } bool fizzy_find_exported_global( - FizzyInstance* instance, const char* name, FizzyExternalGlobal* out_global) + FizzyInstance* instance, const char* name, FizzyExternalGlobal* out_global) noexcept { const auto optional_external_global = fizzy::find_exported_global(*unwrap(instance), name); if (!optional_external_global) @@ -594,7 +603,7 @@ bool fizzy_find_exported_global( return true; } -bool fizzy_module_has_start_function(const FizzyModule* module) +bool fizzy_module_has_start_function(const FizzyModule* module) noexcept { return unwrap(module)->startfunc.has_value(); } @@ -602,7 +611,8 @@ bool fizzy_module_has_start_function(const FizzyModule* module) FizzyInstance* fizzy_instantiate(const FizzyModule* module, const FizzyExternalFunction* imported_functions, size_t imported_functions_size, const FizzyExternalTable* imported_table, const FizzyExternalMemory* imported_memory, - const FizzyExternalGlobal* imported_globals, size_t imported_globals_size, FizzyError* error) + const FizzyExternalGlobal* imported_globals, size_t imported_globals_size, + FizzyError* error) noexcept { try { @@ -627,7 +637,8 @@ FizzyInstance* fizzy_instantiate(const FizzyModule* module, FizzyInstance* fizzy_resolve_instantiate(const FizzyModule* c_module, const FizzyImportedFunction* c_imported_functions, size_t imported_functions_size, const FizzyExternalTable* imported_table, const FizzyExternalMemory* imported_memory, - const FizzyImportedGlobal* c_imported_globals, size_t imported_globals_size, FizzyError* error) + const FizzyImportedGlobal* c_imported_globals, size_t imported_globals_size, + FizzyError* error) noexcept { try { @@ -653,17 +664,17 @@ FizzyInstance* fizzy_resolve_instantiate(const FizzyModule* c_module, } } -void fizzy_free_instance(FizzyInstance* instance) +void fizzy_free_instance(FizzyInstance* instance) noexcept { delete unwrap(instance); } -const FizzyModule* fizzy_get_instance_module(FizzyInstance* instance) +const FizzyModule* fizzy_get_instance_module(FizzyInstance* instance) noexcept { return wrap(unwrap(instance)->module.get()); } -uint8_t* fizzy_get_instance_memory_data(FizzyInstance* instance) +uint8_t* fizzy_get_instance_memory_data(FizzyInstance* instance) noexcept { auto& memory = unwrap(instance)->memory; if (!memory) @@ -672,7 +683,7 @@ uint8_t* fizzy_get_instance_memory_data(FizzyInstance* instance) return memory->data(); } -size_t fizzy_get_instance_memory_size(FizzyInstance* instance) +size_t fizzy_get_instance_memory_size(FizzyInstance* instance) noexcept { auto& memory = unwrap(instance)->memory; if (!memory) @@ -682,9 +693,10 @@ size_t fizzy_get_instance_memory_size(FizzyInstance* instance) } FizzyExecutionResult fizzy_execute( - FizzyInstance* instance, uint32_t func_idx, const FizzyValue* args) + FizzyInstance* instance, uint32_t func_idx, const FizzyValue* args) noexcept { const auto result = fizzy::execute(*unwrap(instance), func_idx, unwrap(args)); return wrap(result); } -} + +} // extern "C" diff --git a/test/unittests/capi_test.cpp b/test/unittests/capi_test.cpp index d7d4e71ed..f34c5fbb7 100644 --- a/test/unittests/capi_test.cpp +++ b/test/unittests/capi_test.cpp @@ -909,7 +909,7 @@ TEST(capi, resolve_instantiate_functions) ASSERT_NE(module, nullptr); FizzyExternalFn host_fn = [](void* context, FizzyInstance*, const FizzyValue*, - FizzyExecutionContext*) { + FizzyExecutionContext*) noexcept { return FizzyExecutionResult{false, true, *static_cast(context)}; }; @@ -982,7 +982,8 @@ TEST(capi, resolve_instantiate_function_duplicate) auto module = fizzy_parse(wasm.data(), wasm.size(), nullptr); ASSERT_NE(module, nullptr); - FizzyExternalFn host_fn = [](void*, FizzyInstance*, const FizzyValue*, FizzyExecutionContext*) { + FizzyExternalFn host_fn = [](void*, FizzyInstance*, const FizzyValue*, + FizzyExecutionContext*) noexcept { return FizzyExecutionResult{false, true, FizzyValue{42}}; }; @@ -1028,7 +1029,8 @@ TEST(capi, resolve_instantiate_globals) module = fizzy_parse(wasm.data(), wasm.size(), nullptr); ASSERT_NE(module, nullptr); - FizzyExternalFn host_fn = [](void*, FizzyInstance*, const FizzyValue*, FizzyExecutionContext*) { + FizzyExternalFn host_fn = [](void*, FizzyInstance*, const FizzyValue*, + FizzyExecutionContext*) noexcept { return FizzyExecutionResult{true, false, {0}}; }; FizzyImportedFunction mod1foo1 = { @@ -1323,12 +1325,12 @@ TEST(capi, execute_with_host_function) FizzyExternalFunction host_funcs[] = { {{FizzyValueTypeI32, nullptr, 0}, - [](void*, FizzyInstance*, const FizzyValue*, FizzyExecutionContext*) { + [](void*, FizzyInstance*, const FizzyValue*, FizzyExecutionContext*) noexcept { return FizzyExecutionResult{false, true, {42}}; }, nullptr}, {{FizzyValueTypeI32, &inputs[0], 2}, - [](void*, FizzyInstance*, const FizzyValue* args, FizzyExecutionContext*) { + [](void*, FizzyInstance*, const FizzyValue* args, FizzyExecutionContext*) noexcept { FizzyValue v; v.i32 = args[0].i32 / args[1].i32; return FizzyExecutionResult{false, true, {v}}; @@ -1360,7 +1362,7 @@ TEST(capi, imported_function_traps) ASSERT_NE(module, nullptr); FizzyExternalFunction host_funcs[] = {{{FizzyValueTypeI32, nullptr, 0}, - [](void*, FizzyInstance*, const FizzyValue*, FizzyExecutionContext*) { + [](void*, FizzyInstance*, const FizzyValue*, FizzyExecutionContext*) noexcept { return FizzyExecutionResult{true, false, {}}; }, nullptr}}; @@ -1388,7 +1390,7 @@ TEST(capi, imported_function_void) bool called = false; FizzyExternalFunction host_funcs[] = {{{}, - [](void* context, FizzyInstance*, const FizzyValue*, FizzyExecutionContext*) { + [](void* context, FizzyInstance*, const FizzyValue*, FizzyExecutionContext*) noexcept { *static_cast(context) = true; return FizzyExecutionResult{false, false, {}}; }, diff --git a/test/utils/fizzy_c_engine.cpp b/test/utils/fizzy_c_engine.cpp index 675e38337..11ac327df 100644 --- a/test/utils/fizzy_c_engine.cpp +++ b/test/utils/fizzy_c_engine.cpp @@ -29,15 +29,22 @@ class FizzyCEngine final : public WasmEngine namespace { FizzyExecutionResult env_adler32( - void*, FizzyInstance* instance, const FizzyValue* args, FizzyExecutionContext*) + void*, FizzyInstance* instance, const FizzyValue* args, FizzyExecutionContext*) noexcept { - auto* memory = fizzy_get_instance_memory_data(instance); - assert(memory != nullptr); - const auto size = fizzy_get_instance_memory_size(instance); - const auto ret = fizzy::test::adler32( - bytes_view(memory, size) - .substr(static_cast(args[0].i64), static_cast(args[1].i64))); - return {false, true, {ret}}; + try + { + auto* memory = fizzy_get_instance_memory_data(instance); + assert(memory != nullptr); + const auto size = fizzy_get_instance_memory_size(instance); + const auto ret = fizzy::test::adler32( + bytes_view(memory, size) + .substr(static_cast(args[0].i64), static_cast(args[1].i64))); + return {false, true, {ret}}; + } + catch (...) + { + return {true, false, {}}; + } } } // namespace diff --git a/test/utils/fizzy_engine.cpp b/test/utils/fizzy_engine.cpp index 6c43a1e4b..e00188c87 100644 --- a/test/utils/fizzy_engine.cpp +++ b/test/utils/fizzy_engine.cpp @@ -31,10 +31,17 @@ namespace fizzy::ExecutionResult env_adler32(std::any&, fizzy::Instance& instance, const fizzy::Value* args, fizzy::ExecutionContext&) noexcept { - assert(instance.memory != nullptr); - const auto ret = fizzy::test::adler32( - bytes_view{*instance.memory}.substr(args[0].as(), args[1].as())); - return Value{ret}; + try + { + assert(instance.memory != nullptr); + const auto ret = fizzy::test::adler32( + bytes_view{*instance.memory}.substr(args[0].as(), args[1].as())); + return Value{ret}; + } + catch (...) + { + return Trap; + } } } // namespace