From e7777c9b3def8cb9243fbf710a9c6d869fb19c57 Mon Sep 17 00:00:00 2001 From: Ezekiel Date: Fri, 4 Apr 2025 16:33:23 -0700 Subject: [PATCH] feat: wasi-sdk dynamic library support --- ecsact/si/wasmer/detail/guest_imports.hh | 2 +- ecsact/si/wasmer/detail/guest_imports/env.hh | 82 ++++++++++++++------ ecsact/si/wasmer/detail/minst/minst.cc | 22 ++++++ ecsact/si/wasmer/detail/minst/minst.hh | 28 ++++++- ecsact/si/wasmer/detail/wasm.cc | 6 +- 5 files changed, 110 insertions(+), 30 deletions(-) diff --git a/ecsact/si/wasmer/detail/guest_imports.hh b/ecsact/si/wasmer/detail/guest_imports.hh index 02a975d..a9ffc18 100644 --- a/ecsact/si/wasmer/detail/guest_imports.hh +++ b/ecsact/si/wasmer/detail/guest_imports.hh @@ -9,7 +9,7 @@ namespace ecsact::wasm::detail { using allowed_guest_imports_t = std::unordered_map< std::string_view, // Function name - std::function>; + std::function>; using allowed_guest_modules_t = std::unordered_map< std::string_view, // Module name diff --git a/ecsact/si/wasmer/detail/guest_imports/env.hh b/ecsact/si/wasmer/detail/guest_imports/env.hh index 7482ebd..2030e01 100644 --- a/ecsact/si/wasmer/detail/guest_imports/env.hh +++ b/ecsact/si/wasmer/detail/guest_imports/env.hh @@ -7,10 +7,44 @@ namespace ecsact::wasm::detail { const auto guest_env_module_imports = allowed_guest_imports_t{ + { + "memory", + [](const minst_import imp) -> minst_import_resolve_t { + return minst_import_resolve_memory{ + .memory_limits = wasm_limits_t{1, 10}, + }; + }, + }, + { + "__indirect_function_table", + [](const minst_import imp) -> minst_import_resolve_t { + return minst_import_resolve_table{ + .table_type = imp.as_tabletype(), + }; + }, + }, + { + "__stack_pointer", + [](const minst_import imp) -> minst_import_resolve_t { + return minst_import_resolve_global{}; + }, + }, + { + "__memory_base", + [](const minst_import imp) -> minst_import_resolve_t { + return minst_import_resolve_global{}; + }, + }, + { + "__table_base", + [](const minst_import imp) -> minst_import_resolve_t { + return minst_import_resolve_global{}; + }, + }, { "ecsact_system_execution_context_action", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_2_0( wasm_valtype_new(WASM_I32), // context wasm_valtype_new(WASM_I32) // out_action_data @@ -21,8 +55,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{ }, { "ecsact_system_execution_context_parent", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_1_1( wasm_valtype_new(WASM_I32), // context wasm_valtype_new(WASM_I32) // parent context (return) @@ -33,8 +67,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{ }, { "ecsact_system_execution_context_same", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_2_1( wasm_valtype_new(WASM_I32), // context a wasm_valtype_new(WASM_I32), // context b @@ -46,8 +80,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{ }, { "ecsact_system_execution_context_get", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_4_0( wasm_valtype_new(WASM_I32), // context wasm_valtype_new(WASM_I32), // component_id @@ -60,8 +94,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{ }, { "ecsact_system_execution_context_update", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_4_0( wasm_valtype_new(WASM_I32), // context wasm_valtype_new(WASM_I32), // component_id @@ -74,8 +108,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{ }, { "ecsact_system_execution_context_has", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_3_0( wasm_valtype_new(WASM_I32), // context wasm_valtype_new(WASM_I32), // component_id @@ -87,8 +121,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{ }, { "ecsact_system_execution_context_generate", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_4_0( wasm_valtype_new(WASM_I32), // context wasm_valtype_new(WASM_I32), // component_count @@ -101,8 +135,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{ }, { "ecsact_system_execution_context_add", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_3_0( wasm_valtype_new(WASM_I32), // context wasm_valtype_new(WASM_I32), // component_id @@ -114,8 +148,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{ }, { "ecsact_system_execution_context_remove", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_3_0( wasm_valtype_new(WASM_I32), // context wasm_valtype_new(WASM_I32), // component_id @@ -127,8 +161,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{ }, { "ecsact_system_execution_context_other", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_2_1( wasm_valtype_new(WASM_I32), // context wasm_valtype_new(WASM_I32), // entity_id @@ -140,8 +174,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{ }, { "ecsact_system_execution_context_entity", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_1_1( wasm_valtype_new(WASM_I32), // context wasm_valtype_new(WASM_I32) // entity 9return) @@ -152,8 +186,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{ }, { "ecsact_system_execution_context_stream_toggle", - []() -> minst_import_resolve_func { - return { + [](const minst_import) -> minst_import_resolve_t { + return minst_import_resolve_func{ wasm_functype_new_4_0( wasm_valtype_new(WASM_I32), // context wasm_valtype_new(WASM_I32), // component_id diff --git a/ecsact/si/wasmer/detail/minst/minst.cc b/ecsact/si/wasmer/detail/minst/minst.cc index 961397f..1296332 100644 --- a/ecsact/si/wasmer/detail/minst/minst.cc +++ b/ecsact/si/wasmer/detail/minst/minst.cc @@ -10,6 +10,9 @@ using ecsact::wasm::detail::minst; using ecsact::wasm::detail::minst_export; using ecsact::wasm::detail::minst_import; using ecsact::wasm::detail::minst_import_resolve_func; +using ecsact::wasm::detail::minst_import_resolve_global; +using ecsact::wasm::detail::minst_import_resolve_memory; +using ecsact::wasm::detail::minst_import_resolve_table; using ecsact::wasm::detail::minst_trap; namespace { @@ -43,6 +46,10 @@ auto minst_import::kind() const -> wasm_externkind_enum { ); } +auto minst_import::as_tabletype() const -> const wasm_tabletype_t* { + return wasm_externtype_as_tabletype_const(wasm_importtype_type(import_type)); +} + auto minst_export::name() const -> std::string_view { auto name = wasm_exporttype_name(export_type); return std::string_view{ @@ -108,6 +115,21 @@ auto minst_import_resolve_func::as_extern( // return wasm_func_as_extern(func); } +auto minst_import_resolve_memory::as_extern( // + wasm_store_t* store +) -> wasm_extern_t* { + auto memory_type = wasm_memorytype_new(&memory_limits); + auto memory = wasm_memory_new(store, memory_type); + return wasm_memory_as_extern(memory); +} + +auto minst_import_resolve_table::as_extern( // + wasm_store_t* store +) -> wasm_extern_t* { + auto* table = wasm_table_new(store, table_type, nullptr); + return wasm_table_as_extern(table); +} + auto minst_trap::message() const -> std::string { auto trap_msg = wasm_message_t{}; wasm_trap_message(trap, &trap_msg); diff --git a/ecsact/si/wasmer/detail/minst/minst.hh b/ecsact/si/wasmer/detail/minst/minst.hh index 035d6a6..97cf23c 100644 --- a/ecsact/si/wasmer/detail/minst/minst.hh +++ b/ecsact/si/wasmer/detail/minst/minst.hh @@ -44,8 +44,29 @@ struct minst_import_resolve_func { auto as_extern(wasm_store_t* store) -> wasm_extern_t*; }; -using minst_import_resolve_t = - std::optional>; +struct minst_import_resolve_memory { + wasm_limits_t memory_limits; + + auto as_extern(wasm_store_t* store) -> wasm_extern_t*; +}; + +struct minst_import_resolve_table { + const wasm_tabletype_t* table_type; + + auto as_extern(wasm_store_t* store) -> wasm_extern_t*; +}; + +struct minst_import_resolve_global { + // TODO + + auto as_extern(wasm_store_t* store) -> wasm_extern_t*; +}; + +using minst_import_resolve_t = std::optional>; struct minst_import { wasm_importtype_t* import_type; @@ -53,6 +74,9 @@ struct minst_import { auto name() const -> std::string_view; auto module() const -> std::string_view; auto kind() const -> wasm_externkind_enum; + + // only valid when kind() == WASM_EXTERN_TABLE + auto as_tabletype() const -> const wasm_tabletype_t*; }; struct minst_export { diff --git a/ecsact/si/wasmer/detail/wasm.cc b/ecsact/si/wasmer/detail/wasm.cc index 7af6c69..061e573 100644 --- a/ecsact/si/wasmer/detail/wasm.cc +++ b/ecsact/si/wasmer/detail/wasm.cc @@ -175,15 +175,15 @@ ecsact_si_wasm_error ecsact_si_wasm_load( auto module_name = imp.module(); auto method_name = imp.name(); - if(imp.module() == "env") { + if(module_name == "env") { auto itr = guest_env_module_imports.find(method_name); if(itr == guest_env_module_imports.end()) { return std::nullopt; } - return itr->second(); + return itr->second(imp); } - if(imp.module() == "wasi_snapshot_preview1") { + if(module_name == "wasi_snapshot_preview1") { auto itr = guest_wasi_module_imports.find(method_name); if(itr == guest_wasi_module_imports.end()) { return std::nullopt;