diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index ef89d5599a..a5e05182b0 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -7742,3 +7742,92 @@ wasm_runtime_is_underlying_binary_freeable(WASMModuleCommon *const module) return true; } + +void +wasm_runtime_release_imports(struct WasmExternInstance *extern_inst) +{ + if (extern_inst == NULL) + return; + + wasm_runtime_free(extern_inst); +} + +static struct WasmExternInstance * +wasm_runtime_create_imports(WASMModuleCommon *const module) +{ + int32_t import_count = wasm_runtime_get_import_count(module); + struct WasmExternInstance *imports = NULL; + + imports = + wasm_runtime_malloc(sizeof(struct WasmExternInstance) * import_count); + if (!imports) { + LOG_ERROR("allocate memory failed: %s", strerror(errno)); + return NULL; + } + + for (int32_t i = 0; i < import_count; i++) { + wasm_import_t import_type = { 0 }; + wasm_runtime_get_import_type(module, i, &import_type); + + struct WasmExternInstance *extern_instance = imports + i; + extern_instance->module_name = import_type.module_name; + extern_instance->field_name = import_type.name; + extern_instance->kind = import_type.kind; + + if (import_type.kind == WASM_IMPORT_EXPORT_KIND_MEMORY) { + extern_instance->u.memory = + wasm_runtime_create_memory(NULL, import_type.u.memory_type, 0); + } + else { + LOG_DEBUG("unimplemented import(%s,%s) kind %d\n", + import_type.module_name, import_type.name, + import_type.kind); + } + } + + return imports; +} + +#if WASM_ENABLE_SPEC_TEST != 0 +struct WasmExternInstance * +wasm_runtime_create_spec_test_imports(WASMModuleCommon *const module) +{ + LOG_DEBUG("create spec test imports\n"); +#ifndef NDEBUG + int32_t import_count = wasm_runtime_get_import_count(module); + for (int32_t i = 0; i < import_count; i++) { + + wasm_import_t import_type = { 0 }; + wasm_runtime_get_import_type(module, i, &import_type); + if (strncmp(import_type.module_name, "spectest", 8) != 0) { + LOG_WARNING("import module name %s is not spectest\n", + import_type.module_name); + } + } +#endif + return wasm_runtime_create_imports(module); +} +#endif + +#if WASM_ENABLE_WASI_TEST != 0 +struct WasmExternInstance * +wasm_runtime_create_wasi_test_imports(WASMModuleCommon *const module) +{ + LOG_DEBUG("create wasi test imports\n"); +#ifndef NDEBUG + int32_t import_count = wasm_runtime_get_import_count(module); + for (int32_t i = 0; i < import_count; i++) { + + wasm_import_t import_type = { 0 }; + wasm_runtime_get_import_type(module, i, &import_type); + if (strncmp(import_type.module_name, "foo", 3) != 0 + && strncmp(import_type.module_name, "env", 3) != 0) { + LOG_WARNING("import module name %s is not expected(foo or env) in " + "wasitest\n", + import_type.module_name); + } + } +#endif + return wasm_runtime_create_imports(module); +} +#endif diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index 97c0b5b601..c8f6da82b3 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -1217,6 +1217,19 @@ void wasm_runtime_set_linux_perf(bool flag); #endif +#if WASM_ENABLE_SPEC_TEST != 0 +struct WasmExternInstance * +wasm_runtime_create_spec_test_imports(WASMModuleCommon *const); +#endif + +#if WASM_ENABLE_WASI_TEST != 0 +struct WasmExternInstance * +wasm_runtime_create_wasi_test_imports(WASMModuleCommon *const); +#endif + +void +wasm_runtime_release_imports(struct WasmExternInstance *); + #ifdef __cplusplus } #endif diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 9cb37ac659..0c9857e081 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -521,7 +521,6 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, uint32 max_memory_pages, uint8 *aux_heap_base_global_data, char *error_buf, uint32 error_buf_size) { - WASMImport *import; uint32 mem_index = 0, i, memory_count = module->import_memory_count + module->memory_count; uint64 total_size; @@ -536,9 +535,9 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, memory = module_inst->global_table_data.memory_instances; /* instantiate memories from import section */ - import = module->import_memories; - for (i = 0; i < module->import_memory_count; i++, import++, memory++) { #if WASM_ENABLE_MULTI_MODULE != 0 + WASMImport *import = module->import_memories; + for (i = 0; i < module->import_memory_count; i++, import++, memory++) { // TODO: ? make sure import->u.memory.import_module is set properly if (import->u.memory.import_module != NULL) { WASMModuleInstance *module_inst_linked; @@ -557,10 +556,9 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, return NULL; } } - else -#endif - { -#if WASM_ENABLE_MULTI_MODULE != 0 + else { + // TODO: Although it is for inherited memory, it misses a situation + // where the memory is imported from host or other modules uint32 num_bytes_per_page = import->u.memory.mem_type.num_bytes_per_page; uint32 init_page_count = import->u.memory.mem_type.init_page_count; @@ -578,12 +576,20 @@ memories_instantiate(const WASMModule *module, WASMModuleInstance *module_inst, return NULL; } mem_index++; -#endif } } - /* instantiate memories from memory section */ + bh_assert(mem_index == module->import_memory_count); + bh_assert(memory + == module_inst->global_table_data.memory_instances + + module->import_memory_count); +#else mem_index = module->import_memory_count; + memory = module_inst->global_table_data.memory_instances + + module->import_memory_count; +#endif /* WASM_ENABLE_MULTI_MODULE != 0 */ + + /* instantiate memories from memory section */ for (i = 0; i < module->memory_count; i++, memory++) { uint32 max_page_count = wasm_runtime_get_max_mem( max_memory_pages, module->memories[i].init_page_count, @@ -1930,6 +1936,12 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env, return ret; } +/* + * all imported WASMXXXInstance shall be linked and NOT NULL + * + * TODO: not sure if MULTI_MODULE can be used under the same conditions + * for checking. + */ static bool check_linked_symbol(WASMModuleInstance *module_inst, char *error_buf, uint32 error_buf_size) @@ -5112,7 +5124,11 @@ wasm_get_module_name(WASMModule *module) } #if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0 -bool +/* + * The function is used to create a new struct WasmExternInstance list + * for a spawned thread. + */ +int32 wasm_runtime_inherit_imports(WASMModule *module, WASMModuleInstance *inst, struct WasmExternInstance *out, int32_t out_len) { @@ -5120,7 +5136,7 @@ wasm_runtime_inherit_imports(WASMModule *module, WASMModuleInstance *inst, if (spawned_import_count > out_len) { LOG_WARNING("The number of imported functions is more than the " "length of provided buffer "); - return false; + return -1; } for (int32_t i = 0, import_memory_index = 0; i < spawned_import_count; @@ -5136,7 +5152,9 @@ wasm_runtime_inherit_imports(WASMModule *module, WASMModuleInstance *inst, if (import_type.kind == WASM_IMPORT_EXPORT_KIND_MEMORY) { out[i].u.memory = inst->memories[import_memory_index]; +#if WASM_ENABLE_SHARED_MEMORY != 0 shared_memory_inc_reference(inst->memories[import_memory_index]); +#endif import_memory_index++; } else { @@ -5146,6 +5164,6 @@ wasm_runtime_inherit_imports(WASMModule *module, WASMModuleInstance *inst, } } - return true; + return 0; } -#endif \ No newline at end of file +#endif diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index d9f59acffc..4962dc269b 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -897,7 +897,7 @@ const char * wasm_get_module_name(WASMModule *module); #if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0 -bool +int32 wasm_runtime_inherit_imports(WASMModule *module, WASMModuleInstance *inst, struct WasmExternInstance *out, int32_t out_len); #endif diff --git a/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c b/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c index 806150a1f8..5983b5a4af 100644 --- a/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c +++ b/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c @@ -561,6 +561,8 @@ pthread_create_wrapper(wasm_exec_env_t exec_env, uint32 aux_stack_size; uint64 aux_stack_start = 0; int32 ret = -1; + int32_t spawned_import_count = ((WASMModule *)module)->import_count; + struct WasmExternInstance *spawned_imports = NULL; bh_assert(module); bh_assert(module_inst); @@ -579,12 +581,37 @@ pthread_create_wrapper(wasm_exec_env_t exec_env, } #endif + /* + * build a imports list(struct WASMExternInstance[]) from parent's imports + */ + spawned_imports = wasm_runtime_malloc(sizeof(struct WasmExternInstance) + * spawned_import_count); + if (spawned_imports == NULL) { + LOG_ERROR("Failed to allocate memory for imports"); + goto fail; + } + +#if WASM_ENABLE_INTERP != 0 + ret = wasm_runtime_inherit_imports((WASMModule *)module, + (WASMModuleInstance *)module_inst, + spawned_imports, spawned_import_count); + if (ret != 0) { + LOG_ERROR("Failed to inherit imports"); + goto fail; + } +#endif + +#if WASM_ENABLE_AOT != 0 + // TODO: + // bh_assert(false && "Unsupported operation"); +#endif + if (!(new_module_inst = wasm_runtime_instantiate_internal( module, module_inst, exec_env, stack_size, - 0, // heap_size - 0, // max_memory_pages - 0, // import_count - NULL, // imports + 0, // heap_size + 0, // max_memory_pages + spawned_import_count, // import_count + spawned_imports, // imports NULL, 0))) return -1; @@ -646,9 +673,12 @@ pthread_create_wrapper(wasm_exec_env_t exec_env, if (thread) *thread = thread_handle; + wasm_runtime_free(spawned_imports); return 0; fail: + if (spawned_imports) + wasm_runtime_free(spawned_imports); if (new_module_inst) wasm_runtime_deinstantiate_internal(new_module_inst, true); if (info_node) diff --git a/core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c b/core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c index 16ed8e86e5..cf71492b4b 100644 --- a/core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c +++ b/core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c @@ -77,9 +77,11 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg) wasm_module_inst_t new_module_inst = NULL; ThreadStartArg *thread_start_arg = NULL; wasm_function_inst_t start_func; - int32 thread_id; + int32 thread_id = -1; uint32 stack_size = 8192; int32 ret = -1; + int32_t spawned_import_count = ((WASMModule *)module)->import_count; + struct WasmExternInstance *spawned_imports = NULL; bh_assert(module); bh_assert(module_inst); @@ -89,18 +91,26 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg) /* * build a imports list(struct WASMExternInstance[]) from parent's imports */ - int32_t spawned_import_count = ((WASMModule *)module)->import_count; - struct WasmExternInstance *spawned_imports = wasm_runtime_malloc( - sizeof(struct WasmExternInstance) * spawned_import_count); + spawned_imports = wasm_runtime_malloc(sizeof(struct WasmExternInstance) + * spawned_import_count); if (spawned_imports == NULL) { LOG_ERROR("Failed to allocate memory for imports"); return -1; } #if WASM_ENABLE_INTERP != 0 - wasm_runtime_inherit_imports((WASMModule *)module, - (WASMModuleInstance *)module_inst, - spawned_imports, spawned_import_count); + ret = wasm_runtime_inherit_imports((WASMModule *)module, + (WASMModuleInstance *)module_inst, + spawned_imports, spawned_import_count); + if (ret != 0) { + LOG_ERROR("Failed to inherit imports"); + goto free_imports; + } +#endif + +#if WASM_ENABLE_AOT != 0 + // TODO: + // bh_assert(false && "Unsupported operation"); #endif new_module_inst = wasm_runtime_instantiate_internal( @@ -110,9 +120,9 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg) spawned_import_count, // import_count spawned_imports, // imports NULL, 0); - wasm_runtime_free(spawned_imports); if (new_module_inst == NULL) { - return -1; + LOG_ERROR("Failed to instantiate new module"); + goto free_imports; } wasm_runtime_set_custom_data_internal( @@ -151,6 +161,7 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg) goto thread_spawn_fail; } + wasm_runtime_free(spawned_imports); return thread_id; thread_spawn_fail: @@ -160,6 +171,8 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg) wasm_runtime_deinstantiate_internal(new_module_inst, true); if (thread_start_arg) wasm_runtime_free(thread_start_arg); +free_imports: + wasm_runtime_free(spawned_imports); return -1; } diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index c7ccf11bfe..f265428b69 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -500,17 +500,47 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) uint32 aux_stack_size; uint64 aux_stack_start; uint32 stack_size = 8192; + int32 ret = -1; + int32_t spawned_import_count = 0; + struct WasmExternInstance *spawned_imports = NULL; if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) { return NULL; } + /* + * build a imports list(struct WASMExternInstance[]) from parent's imports + */ + spawned_import_count = ((WASMModule *)module)->import_count; + spawned_imports = wasm_runtime_malloc(sizeof(struct WasmExternInstance) + * spawned_import_count); + if (spawned_imports == NULL) { + LOG_ERROR("Failed to allocate memory for imports"); + goto fail; + } + +#if WASM_ENABLE_INTERP != 0 + ret = wasm_runtime_inherit_imports((WASMModule *)module, + (WASMModuleInstance *)module_inst, + spawned_imports, spawned_import_count); + if (ret != 0) { + LOG_ERROR("Failed to inherit imports"); + goto fail; + } +#endif + +#if WASM_ENABLE_AOT != 0 + // TODO: + // bh_assert(false && "Unsupported operation"); + (void)ret; +#endif + if (!(new_module_inst = wasm_runtime_instantiate_internal( module, module_inst, exec_env, stack_size, - 0, // heap_size - 0, // max_memory_pages - 0, // import_count - NULL, // imports + 0, // heap_size + 0, // max_memory_pages + spawned_import_count, // import_count + spawned_imports, // imports NULL, 0))) { return NULL; } @@ -575,6 +605,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) os_mutex_unlock(&cluster->lock); + wasm_runtime_free(spawned_imports); return new_exec_env; fail3: @@ -585,6 +616,8 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) wasm_cluster_free_aux_stack(exec_env, aux_stack_start); fail1: wasm_runtime_deinstantiate_internal(new_module_inst, true); +fail: + wasm_runtime_free(spawned_imports); return NULL; } 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 91f03a979e..9b2c63bfcb 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp @@ -16,6 +16,9 @@ #include #endif +/*TODO: take me out when have a linker */ +#include "wasm_runtime_common.h" + extern "C" { typedef int (*os_print_function_t)(const char *message); extern void @@ -763,6 +766,8 @@ 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; /* avoid duplicated init */ if (runtime_inited) { @@ -797,13 +802,59 @@ 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); + struct WasmExternInstance *imports = NULL; + +#if WASM_ENABLE_SPEC_TEST != 0 + imports = wasm_runtime_create_spec_test_imports(wasm_module); + if (!imports) { + enclave_print("Failed to create spec test imports."); + enclave_print("\n"); + goto fail2; + } +#endif +#if WASM_ENABLE_WASI_TEST != 0 + imports = wasm_runtime_create_wasi_test_imports(wasm_module); + if (!imports) { + enclave_print("Failed to create wasi test imports."); + enclave_print("\n"); + goto fail2; + } +#endif + + if (!imports) { + enclave_print("Need to provide necessary imported objects"); + enclave_print("\n"); + } + + InstantiationArgs inst_args = { + .default_stack_size = stack_size, + .host_managed_heap_size = heap_size, + .max_memory_pages = 0, // ? + .import_count = import_count, + .imports = imports, + }; + + wasm_module_inst = wasm_runtime_instantiate_ex( + wasm_module, &inst_args, error_buf, sizeof(error_buf)); + wasm_runtime_release_imports(imports); + if (!wasm_module_inst) { + enclave_print(error_buf); + enclave_print("\n"); + goto fail2; + } + } +#else if (!(wasm_module_inst = - wasm_runtime_instantiate(wasm_module, 16 * 1024, 16 * 1024, + wasm_runtime_instantiate(wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf)))) { enclave_print(error_buf); enclave_print("\n"); goto fail2; } +#endif /* execute the main function of wasm app */ wasm_application_execute_main(wasm_module_inst, 0, NULL); diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index 39671193da..3d2048e1e4 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -22,6 +22,9 @@ #include #endif +/*TODO: take me out when have a linker */ +#include "wasm_runtime_common.h" + static int app_argc; static char **app_argv; @@ -942,41 +945,28 @@ main(int argc, char *argv[]) #endif /* instantiate the module */ -#if (WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0) \ - && WASM_ENABLE_MULTI_MODULE == 0 +#if WASM_ENABLE_MULTI_MODULE == 0 { int32_t import_count = wasm_runtime_get_import_count(wasm_module); - struct WasmExternInstance *imports = wasm_runtime_malloc( - sizeof(struct WasmExternInstance) * import_count); + struct WasmExternInstance *imports = NULL; + +#if WASM_ENABLE_SPEC_TEST != 0 + imports = wasm_runtime_create_spec_test_imports(wasm_module); if (!imports) { - printf("Failed to allocate memory for imports\n"); + printf("Failed to create spec test imports\n"); goto fail3; } +#endif +#if WASM_ENABLE_WASI_TEST != 0 + imports = wasm_runtime_create_wasi_test_imports(wasm_module); + if (!imports) { + printf("Failed to create wasi test imports\n"); + goto fail3; + } +#endif - for (int32_t i = 0; i < import_count; i++) { - wasm_import_t import_type = { 0 }; - wasm_runtime_get_import_type(wasm_module, i, &import_type); - if (strncmp(import_type.module_name, "spectest", 8) != 0 - && strncmp(import_type.module_name, "foo", 3) != 0 - && strncmp(import_type.module_name, "env", 3) != 0) { - continue; - } - - struct WasmExternInstance *extern_instance = imports + i; - extern_instance->module_name = import_type.module_name; - extern_instance->field_name = import_type.name; - extern_instance->kind = import_type.kind; - - // create external instance for spectest.XXX - if (import_type.kind == WASM_IMPORT_EXPORT_KIND_MEMORY) { - extern_instance->u.memory = wasm_runtime_create_memory( - NULL, import_type.u.memory_type, 0); - } - else { - LOG_WARNING("unimplemented import(%s,%s) kind %d\n", - import_type.module_name, import_type.name, - import_type.kind); - } + if (import_count) { + printf("Need to provide %d imported objects:\n", import_count); } InstantiationArgs inst_args = { @@ -989,6 +979,7 @@ main(int argc, char *argv[]) wasm_module_inst = wasm_runtime_instantiate_ex( wasm_module, &inst_args, error_buf, sizeof(error_buf)); + wasm_runtime_release_imports(imports); if (!wasm_module_inst) { printf("%s\n", error_buf); goto fail3; diff --git a/product-mini/platforms/windows/main.c b/product-mini/platforms/windows/main.c index d8b3a3bb7d..8101b3d08b 100644 --- a/product-mini/platforms/windows/main.c +++ b/product-mini/platforms/windows/main.c @@ -14,6 +14,9 @@ #include "../common/libc_wasi.c" #endif +/*TODO: take me out when have a linker */ +#include "wasm_runtime_common.h" + static int app_argc; static char **app_argv; @@ -522,39 +525,28 @@ main(int argc, char *argv[]) #endif /* instantiate the module */ -#if (WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_WASI_TEST != 0) \ - && WASM_ENABLE_MULTI_MODULE == 0 +#if WASM_ENABLE_MULTI_MODULE == 0 { int32_t import_count = wasm_runtime_get_import_count(wasm_module); - struct WasmExternInstance *imports = wasm_runtime_malloc( - sizeof(struct WasmExternInstance) * import_count); + struct WasmExternInstance *imports = NULL; + +#if WASM_ENABLE_SPEC_TEST != 0 + imports = wasm_runtime_create_spec_test_imports(wasm_module); if (!imports) { - printf("Failed to allocate memory for imports\n"); + printf("Failed to create spec test imports\n"); goto fail3; } +#endif +#if WASM_ENABLE_WASI_TEST != 0 + imports = wasm_runtime_create_wasi_test_imports(wasm_module); + if (!imports) { + printf("Failed to create wasi test imports\n"); + goto fail3; + } +#endif - for (int32_t i = 0; i < import_count; i++) { - wasm_import_t import_type = { 0 }; - wasm_runtime_get_import_type(wasm_module, i, &import_type); - if (strncmp(import_type.module_name, "spectest", 8) != 0 - && strncmp(import_type.module_name, "foo", 3) != 0 - && strncmp(import_type.module_name, "env", 3) != 0) { - continue; - } - - struct WasmExternInstance *extern_instance = imports + i; - extern_instance->module_name = import_type.module_name; - extern_instance->field_name = import_type.name; - extern_instance->kind = import_type.kind; - - // create external instance for spectest.XXX - if (import_type.kind == WASM_IMPORT_EXPORT_KIND_MEMORY) { - extern_instance->u.memory = wasm_runtime_create_memory( - NULL, import_type.u.memory_type, 0); - } - else { - LOG_WARNING("unimplemented import kind %d\n", import_type.kind); - } + if (import_count) { + printf("Need to provide %d imported objects:\n", import_count); } InstantiationArgs inst_args = { @@ -567,6 +559,7 @@ main(int argc, char *argv[]) wasm_module_inst = wasm_runtime_instantiate_ex( wasm_module, &inst_args, error_buf, sizeof(error_buf)); + wasm_runtime_release_imports(imports); if (!wasm_module_inst) { printf("%s\n", error_buf); goto fail3; diff --git a/samples/linking/raw/main.c b/samples/linking/raw/main.c index 452bc2e5a4..4675a7e2c1 100644 --- a/samples/linking/raw/main.c +++ b/samples/linking/raw/main.c @@ -33,7 +33,7 @@ main(int argc, char *argv_main[]) error_buf, sizeof(error_buf)); if (!module) { printf("Load wasm file failed: %s\n", error_buf); - goto destroy_runtime; + goto release_file_buffer; } /* import type */ @@ -63,7 +63,7 @@ main(int argc, char *argv_main[]) } /* import list */ - wasm_external_inst_t import_list[10] = { 0 }; + wasm_extern_inst_t import_list[10] = { 0 }; import_list[import_memory_index].module_name = "env"; import_list[import_memory_index].field_name = "memory"; import_list[import_memory_index].kind = WASM_IMPORT_EXPORT_KIND_MEMORY; @@ -110,6 +110,8 @@ main(int argc, char *argv_main[]) wasm_runtime_destroy_memory(memory); unload_module: wasm_runtime_unload(module); +release_file_buffer: + wasm_runtime_free(buffer); destroy_runtime: wasm_runtime_destroy();