From 40164573f5d806df98ef34e0c040e5979828cae7 Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Fri, 1 Nov 2024 15:28:38 +0000 Subject: [PATCH] Refactor wasm_runtime_instantiate() The main goal is to let existed code run without modification --- core/iwasm/aot/aot_runtime.c | 6 +- core/iwasm/common/wasm_runtime_common.c | 81 +++++++++++++---- core/iwasm/include/wasm_export.h | 21 +++-- core/iwasm/interpreter/wasm_runtime.c | 6 +- .../enclave-sample/Enclave/Enclave.cpp | 88 +------------------ product-mini/platforms/posix/main.c | 52 ++--------- product-mini/platforms/windows/main.c | 40 +-------- samples/file/src/main.c | 39 +------- 8 files changed, 97 insertions(+), 236 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index a496eaef4b..9616c7a345 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1899,13 +1899,9 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent, module->import_func_count + module->import_global_count + module->import_memory_count + module->import_table_count; if (total_import_count > 0 && !imports) { - /* - * TODO: might be too strict - * might wasm_runtime_create_imports_with_builtin() here by default - */ set_error_buf(error_buf, error_buf_size, "imports is NULL while module has imports"); - // return NULL; + return NULL; } #endif diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 5e88d50517..d5bee98348 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1651,12 +1651,21 @@ wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size, uint32 heap_size, char *error_buf, uint32 error_buf_size) { +#if WASM_ENABLE_MULTI_MODULE == 0 + /* + * TODO: if wasm_runtime_instantiate_with_builtin_linker is not in this + * library what should we do? _weak? + */ + return wasm_runtime_instantiate_with_builtin_linker( + module, stack_size, heap_size, error_buf, error_buf_size); +#else return wasm_runtime_instantiate_internal(module, NULL, NULL, stack_size, heap_size, 0, // max_memory_pages NULL, // imports 0, // import_count error_buf, error_buf_size); +#endif } WASMModuleInstanceCommon * @@ -7751,6 +7760,45 @@ wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module) } /*TODO: take us(below) out when have a linker */ +WASMModuleInstanceCommon * +wasm_runtime_instantiate_with_builtin_linker(WASMModuleCommon *module, + uint32 stack_size, + uint32 heap_size, char *error_buf, + uint32 error_buf_size) +{ + int32_t import_count = 0; + WASMExternInstance *imports = NULL; + + import_count = wasm_runtime_get_import_count(module); + if (import_count) { + imports = runtime_malloc(sizeof(WASMExternInstance) * import_count, + NULL, error_buf, error_buf_size); + if (!imports) { + set_error_buf(error_buf, error_buf_size, "allocate imports failed"); + return NULL; + } + + if (!wasm_runtime_create_imports_with_builtin(module, imports, + import_count)) { + set_error_buf(error_buf, error_buf_size, + "initialize imports failed"); + wasm_runtime_free(imports); + return NULL; + } + } + + WASMModuleInstanceCommon *inst = wasm_runtime_instantiate_internal( + module, NULL, NULL, stack_size, heap_size, + 0, // max_memory_pages + imports, import_count, error_buf, error_buf_size); + + if (imports) { + wasm_runtime_free(imports); + } + + return inst; +} + bool wasm_runtime_create_extern_inst(WASMModuleCommon *module, wasm_import_t import_type, @@ -7853,22 +7901,19 @@ wasm_runtime_destroy_imports(WASMModuleCommon *module, wasm_runtime_free(extern_inst_list); } -WASMExternInstance * +bool wasm_runtime_create_imports(WASMModuleCommon *module, - bool (*module_name_filter)(const char *)) + bool (*module_name_filter)(const char *), + WASMExternInstance *out, int32 out_len) { int32 import_count = wasm_runtime_get_import_count(module); - WASMExternInstance *imports = NULL; if (import_count == 0) - return NULL; + return true; - imports = runtime_malloc(sizeof(WASMExternInstance) * import_count, - NULL, // module_inst - NULL, 0); - if (!imports) { - LOG_ERROR("allocate memory failed"); - return NULL; + if (!out || out_len < import_count) { + LOG_ERROR("invalid arguments"); + return false; } for (int32 i = 0; i < import_count; i++) { @@ -7882,23 +7927,25 @@ wasm_runtime_create_imports(WASMModuleCommon *module, continue; } - WASMExternInstance *extern_instance = imports + i; + WASMExternInstance *extern_instance = out + i; if (!wasm_runtime_create_extern_inst(module, import_type, extern_instance)) { - wasm_runtime_destroy_imports(module, imports); + wasm_runtime_destroy_imports(module, out); LOG_ERROR("create import failed"); - return NULL; + return false; } } - return imports; + return true; } -WASMExternInstance * -wasm_runtime_create_imports_with_builtin(WASMModuleCommon *module) +bool +wasm_runtime_create_imports_with_builtin(WASMModuleCommon *module, + WASMExternInstance *out, int32 out_len) { LOG_DEBUG("create imports with builtin"); - return wasm_runtime_create_imports(module, wasm_runtime_is_built_in_module); + return wasm_runtime_create_imports(module, wasm_runtime_is_built_in_module, + out, out_len); } #if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0 diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index ffcd3ece69..11590a7409 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -2331,18 +2331,29 @@ WASM_RUNTIME_API_EXTERN void wasm_runtime_shared_heap_free(wasm_module_inst_t module_inst, uint64_t ptr); /*TODO: take me out when have a linker */ +WASM_RUNTIME_API_EXTERN wasm_module_inst_t +wasm_runtime_instantiate_with_builtin_linker(wasm_module_t module, + uint32_t stack_size, + uint32_t heap_size, + char *error_buf, + uint32_t error_buf_size); + /** * @return NULL if failed and if there is no import */ -WASM_RUNTIME_API_EXTERN wasm_extern_inst_t -wasm_runtime_create_imports_with_builtin(wasm_module_t module); +WASM_RUNTIME_API_EXTERN bool +wasm_runtime_create_imports_with_builtin(wasm_module_t module, + wasm_extern_inst_t out, + int32_t out_len); -WASM_RUNTIME_API_EXTERN void +WASM_RUNTIME_API_EXTERN +void wasm_runtime_destroy_imports(wasm_module_t module, wasm_extern_inst_t imports); -WASM_RUNTIME_API_EXTERN wasm_extern_inst_t +WASM_RUNTIME_API_EXTERN bool wasm_runtime_create_imports(wasm_module_t module, - bool (*module_name_filter)(const char *)); + bool (*module_name_filter)(const char *), + wasm_extern_inst_t out, int32_t out_len); #ifdef __cplusplus } diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index ce6f3d8b01..7e8478f6db 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -2502,13 +2502,9 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, #if WASM_ENABLE_MULTI_MODULE == 0 if (module->import_count > 0 && !imports) { - /* - * TODO: might be too strict - * might wasm_runtime_create_imports_with_builtin() here by default - */ set_error_buf(error_buf, error_buf_size, "argument imports is NULL while module has imports"); - // return NULL; + return NULL; } #endif diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp index b17f0997ab..91f03a979e 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp @@ -377,56 +377,17 @@ handle_cmd_instantiate_module(uint64 *args, uint32 argc) bh_assert(argc == 5); - *(void **)args_org = NULL; - if (!runtime_inited) { + *(void **)args_org = NULL; return; } -#if WASM_ENABLE_MULTI_MODULE == 0 - { - int32_t import_count = - wasm_runtime_get_import_count(enclave_module->module); - WASMExternInstance *imports = NULL; - -#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \ - || WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0 - imports = - wasm_runtime_create_imports_with_builtin(enclave_module->module); -#endif - if (import_count > 0 && imports == NULL) { - LOG_WARNING("Need to provide necessary imported objects"); - return; - } - - InstantiationArgs inst_args = { - .default_stack_size = stack_size, - .host_managed_heap_size = heap_size, - .max_memory_pages = 0, - .imports = imports, - .import_count = (uint32_t)import_count, - }; - - module_inst = wasm_runtime_instantiate_ex( - enclave_module->module, &inst_args, error_buf, error_buf_size); - if (!module_inst) { - wasm_runtime_destroy_imports(enclave_module->module, imports); - return; - } - - /* - * FIXME: how to relese imports. - * if there will be spawned thread, need to release when the thread is - * done. - */ - } -#else /* WASM_ENABLE_MULTI_MODULE == 0 */ if (!(module_inst = wasm_runtime_instantiate(enclave_module->module, stack_size, heap_size, error_buf, error_buf_size))) { + *(void **)args_org = NULL; return; } -#endif /* WASM_ENABLE_MULTI_MODULE == 0 */ *(wasm_module_inst_t *)args_org = module_inst; @@ -802,11 +763,6 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size) RuntimeInitArgs init_args; char error_buf[128]; const char *exception; - uint32 stack_size = 16 * 1024; - uint32 heap_size = 16 * 1024; -#if WASM_ENABLE_MULTI_MODULE == 0 - WASMExternInstance *imports = NULL; -#endif /* avoid duplicated init */ if (runtime_inited) { @@ -841,45 +797,13 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size) } /* instantiate the module */ -#if WASM_ENABLE_MULTI_MODULE == 0 - { - int32_t import_count = wasm_runtime_get_import_count(wasm_module); - -#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \ - || WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0 - imports = wasm_runtime_create_imports_with_builtin(wasm_module); -#endif - if (import_count > 0 && imports == NULL) { - enclave_print("Need to provide necessary imported objects"); - enclave_print("\n"); - goto fail2; - } - - InstantiationArgs inst_args = { - .default_stack_size = stack_size, - .host_managed_heap_size = heap_size, - .max_memory_pages = 0, - .imports = imports, - .import_count = (uint32_t)import_count, - }; - - wasm_module_inst = wasm_runtime_instantiate_ex( - wasm_module, &inst_args, error_buf, sizeof(error_buf)); - if (!wasm_module_inst) { - enclave_print(error_buf); - enclave_print("\n"); - goto fail3; - } - } -#else /* WASM_ENABLE_MULTI_MODULE == 0 */ if (!(wasm_module_inst = - wasm_runtime_instantiate(wasm_module, stack_size, heap_size, + wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024, error_buf, sizeof(error_buf)))) { enclave_print(error_buf); enclave_print("\n"); goto fail2; } -#endif /* WASM_ENABLE_MULTI_MODULE == 0 */ /* execute the main function of wasm app */ wasm_application_execute_main(wasm_module_inst, 0, NULL); @@ -891,12 +815,6 @@ ecall_iwasm_main(uint8_t *wasm_file_buf, uint32_t wasm_file_size) /* destroy the module instance */ wasm_runtime_deinstantiate(wasm_module_inst); -#if WASM_ENABLE_MULTI_MODULE == 0 -fail3: - /* destory imports */ - wasm_runtime_destroy_imports(wasm_module, imports); -#endif /* WASM_ENABLE_MULTI_MODULE == 0*/ - fail2: /* unload the module */ wasm_runtime_unload(wasm_module); diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index 6f7c95e14b..af50223a4f 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -885,7 +885,7 @@ main(int argc, char *argv[]) /* load WASM byte buffer from WASM bin file */ if (!(wasm_file_buf = (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size))) - goto unregister_native; + goto fail1; #if WASM_ENABLE_AOT != 0 if (wasm_runtime_is_xip_file(wasm_file_buf, wasm_file_size)) { @@ -898,7 +898,7 @@ main(int argc, char *argv[]) map_flags, os_get_invalid_handle()))) { printf("mmap memory failed\n"); wasm_runtime_free(wasm_file_buf); - goto unregister_native; + goto fail1; } #if (WASM_MEM_DUAL_BUS_MIRROR != 0) @@ -925,7 +925,7 @@ main(int argc, char *argv[]) if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf, sizeof(error_buf)))) { printf("%s\n", error_buf); - goto unmap_file; + goto fail2; } #if WASM_ENABLE_DYNAMIC_AOT_DEBUG != 0 @@ -933,7 +933,7 @@ main(int argc, char *argv[]) sizeof(error_buf))) { printf("set aot module name failed in dynamic aot debug mode, %s\n", error_buf); - goto unload_module; + goto fail3; } #endif @@ -942,42 +942,12 @@ main(int argc, char *argv[]) #endif /* instantiate the module */ -#if WASM_ENABLE_MULTI_MODULE == 0 - int32_t import_count = wasm_runtime_get_import_count(wasm_module); - WASMExternInstance *imports = NULL; - -#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \ - || WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0 - imports = wasm_runtime_create_imports_with_builtin(wasm_module); -#endif - - if (import_count > 0 && imports == NULL) { - printf("Need to provide %" PRId32 " imported objects:\n", import_count); - goto unload_module; - } - - InstantiationArgs inst_args = { - .default_stack_size = stack_size, - .host_managed_heap_size = heap_size, - .max_memory_pages = 0, - .imports = imports, - .import_count = import_count, - }; - - wasm_module_inst = wasm_runtime_instantiate_ex( - wasm_module, &inst_args, error_buf, sizeof(error_buf)); - if (!wasm_module_inst) { - printf("%s\n", error_buf); - goto destroy_imports; - } -#else if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf)))) { printf("%s\n", error_buf); - goto unload_module; + goto fail3; } -#endif /* WASM_ENABLE_MULTI_MODULE == 0 */ #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0 if (disable_bounds_checks) { @@ -1067,27 +1037,21 @@ main(int argc, char *argv[]) #if WASM_ENABLE_DEBUG_INTERP != 0 fail4: #endif - /* destroy the module instance */ wasm_runtime_deinstantiate(wasm_module_inst); -#if WASM_ENABLE_MULTI_MODULE == 0 -destroy_imports: - wasm_runtime_destroy_imports(wasm_module, imports); -#endif - -unload_module: +fail3: /* unload the module */ wasm_runtime_unload(wasm_module); -unmap_file: +fail2: /* free the file buffer */ if (!is_xip_file) wasm_runtime_free(wasm_file_buf); else os_munmap(wasm_file_buf, wasm_file_size); -unregister_native: +fail1: #if BH_HAS_DLFCN /* unload the native libraries */ unregister_and_unload_native_libs(native_lib_loaded_count, diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index e932787e97..e85e869c28 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -540,41 +540,12 @@ main(int argc, char *argv[]) #endif /* instantiate the module */ -#if WASM_ENABLE_MULTI_MODULE == 0 - int32_t import_count = wasm_runtime_get_import_count(wasm_module); - WASMExternInstance *imports = NULL; - -#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \ - || WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0 - imports = wasm_runtime_create_imports_with_builtin(wasm_module); -#endif - if (import_count > 0 && imports == NULL) { - printf("Need to provide %d imported objects:\n", import_count); - goto fail3; - } - - InstantiationArgs inst_args = { - .default_stack_size = stack_size, - .host_managed_heap_size = heap_size, - .max_memory_pages = 0, - .imports = imports, - .import_count = import_count, - }; - - wasm_module_inst = wasm_runtime_instantiate_ex( - wasm_module, &inst_args, error_buf, sizeof(error_buf)); - if (!wasm_module_inst) { - printf("%s\n", error_buf); - goto fail4; - } -#else if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf)))) { printf("%s\n", error_buf); goto fail3; } -#endif /* WASM_ENABLE_MULTI_MODULE == 0 */ #if WASM_ENABLE_DEBUG_INTERP != 0 if (ip_addr != NULL) { @@ -583,12 +554,12 @@ main(int argc, char *argv[]) uint32_t debug_port; if (exec_env == NULL) { printf("%s\n", wasm_runtime_get_exception(wasm_module_inst)); - goto fail5; + goto fail4; } debug_port = wasm_runtime_start_debug_instance(exec_env); if (debug_port == 0) { printf("Failed to start debug instance\n"); - goto fail5; + goto fail4; } } #endif @@ -624,16 +595,11 @@ main(int argc, char *argv[]) printf("%s\n", exception); #if WASM_ENABLE_DEBUG_INTERP != 0 -fail5: +fail4: #endif /* destroy the module instance */ wasm_runtime_deinstantiate(wasm_module_inst); -#if WASM_ENABLE_MULTI_MODULE == 0 -fail4: - wasm_runtime_destroy_imports(wasm_module, imports); -#endif - fail3: /* unload the module */ wasm_runtime_unload(wasm_module); diff --git a/samples/file/src/main.c b/samples/file/src/main.c index f7b1da898d..3675ee0574 100644 --- a/samples/file/src/main.c +++ b/samples/file/src/main.c @@ -18,7 +18,7 @@ int main(int argc, char *argv_main[]) { static char global_heap_buf[512 * 1024]; - char *buffer, error_buf[128] = { 0 }; + char *buffer, error_buf[128]; const char *wasm_path = NULL, *wasi_dir = NULL; int opt, main_result = 1; @@ -27,10 +27,6 @@ main(int argc, char *argv_main[]) wasm_exec_env_t exec_env = NULL; uint32 buf_size, stack_size = 8092, heap_size = 8092; -#if WASM_ENABLE_MULTI_MODULE == 0 - WASMExternInstance *imports = NULL; -#endif - RuntimeInitArgs init_args; memset(&init_args, 0, sizeof(RuntimeInitArgs)); @@ -80,37 +76,8 @@ main(int argc, char *argv_main[]) wasm_runtime_set_wasi_args_ex(module, &wasi_dir, 1, NULL, 0, NULL, 0, NULL, 0, 0, 1, 2); -#if WASM_ENABLE_MULTI_MODULE == 0 - int32_t import_count = wasm_runtime_get_import_count(module); - -#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0 \ - || WASM_ENABLE_LIBC_BUILTIN != 0 || WASM_ENABLE_LIBC_WASI != 0 - imports = wasm_runtime_create_imports_with_builtin(module); -#endif - - if (import_count > 0 && imports == NULL) { - printf("Need to provide %" PRId32 " imported objects:\n", import_count); - goto fail; - } - - InstantiationArgs inst_args = { - .default_stack_size = stack_size, - .host_managed_heap_size = heap_size, - .max_memory_pages = 0, - .imports = imports, - .import_count = import_count, - }; - - module_inst = wasm_runtime_instantiate_ex(module, &inst_args, error_buf, - sizeof(error_buf)); - if (!module_inst) { - printf("%s\n", error_buf); - goto fail; - } -#else module_inst = wasm_runtime_instantiate(module, stack_size, heap_size, error_buf, sizeof(error_buf)); -#endif if (!module_inst) { printf("Instantiate wasm module failed. error: %s\n", error_buf); @@ -137,10 +104,6 @@ main(int argc, char *argv_main[]) wasm_runtime_destroy_exec_env(exec_env); if (module_inst) wasm_runtime_deinstantiate(module_inst); -#if WASM_ENABLE_MULTI_MODULE == 0 - if (imports) - wasm_runtime_destroy_imports(module, imports); -#endif if (module) wasm_runtime_unload(module); if (buffer)