Skip to content

Commit

Permalink
Implement wasm_runtime_instantiate_with_inheritance function and refa…
Browse files Browse the repository at this point in the history
…ctor related import handling in thread management
  • Loading branch information
lum1n0us committed Nov 4, 2024
1 parent b6f7e3b commit 20cd0b2
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 109 deletions.
5 changes: 0 additions & 5 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -963,11 +963,6 @@ load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
static void
destroy_import_memories(AOTImportMemory *import_memories)
{
if (!import_memories)
return;

import_memories->module_name = NULL;
import_memories->memory_name = NULL;
wasm_runtime_free(import_memories);
}

Expand Down
40 changes: 39 additions & 1 deletion core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -7947,11 +7947,12 @@ wasm_runtime_create_imports_with_builtin(WASMModuleCommon *module,
}

#if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0

/*
* The function is used to create a new WASMExternInstance list
* for a spawned thread.
*/
int32
static int32
wasm_runtime_inherit_imports(WASMModuleCommon *module,
WASMModuleInstanceCommon *inst,
WASMExternInstance *out, int32 out_len)
Expand All @@ -7971,6 +7972,43 @@ wasm_runtime_inherit_imports(WASMModuleCommon *module,
LOG_ERROR("inherit imports failed, invalid module type");
return 0;
}

WASMModuleInstanceCommon *
wasm_runtime_instantiate_with_inheritance(
WASMModuleCommon *module, WASMModuleInstanceCommon *parent_inst,
WASMExecEnv *exec_env, uint32 stack_size, uint32 heap_size,
uint32 max_memory_pages, char *error_buf, uint32 error_buf_size)
{
int spawned_import_count = wasm_runtime_get_import_count(module);
WASMExternInstance *spawned_imports =
runtime_malloc(sizeof(WASMExternInstance) * spawned_import_count, NULL,
error_buf, error_buf_size);
if (spawned_imports == NULL) {
LOG_ERROR("Failed to allocate memory for imports");
return NULL;
}

int ret = wasm_runtime_inherit_imports(module, parent_inst, spawned_imports,
spawned_import_count);
if (ret != 0) {
LOG_ERROR("Failed to inherit imports");
wasm_runtime_free(spawned_imports);
return NULL;
}

wasm_module_inst_t child_inst = wasm_runtime_instantiate_internal(
module, parent_inst, exec_env, stack_size,
0, // heap_size
0, // max_memory_pages
spawned_imports, // imports
spawned_import_count, // import_count
error_buf, error_buf_size);

wasm_runtime_free(spawned_imports);

return child_inst;
}

#endif /* WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0 */

const WASMExternInstance *
Expand Down
13 changes: 5 additions & 8 deletions core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1218,14 +1218,11 @@ wasm_runtime_set_linux_perf(bool flag);
#endif

#if WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0
/*
* The function is used to create a new WASMExternInstance list
* for a spawned thread.
*/
int32
wasm_runtime_inherit_imports(WASMModuleCommon *module,
WASMModuleInstanceCommon *inst,
WASMExternInstance *out, int32 out_len);
WASMModuleInstanceCommon *
wasm_runtime_instantiate_with_inheritance(
WASMModuleCommon *module, WASMModuleInstanceCommon *parent_inst,
WASMExecEnv *exec_env, uint32 stack_size, uint32 heap_size,
uint32 max_memory_pages, char *error_buf, uint32 error_buf_size);
#endif /* WASM_ENABLE_LIB_WASI_THREADS != 0 || WASM_ENABLE_THREAD_MGR != 0 */

const WASMExternInstance *
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/compilation/aot.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ aot_create_import_memories(const WASMModule *module, uint32 import_memory_count)
}

import_memories = wasm_runtime_malloc((uint32)size);
if (!module->import_memories) {
if (!import_memories) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
Expand Down
34 changes: 4 additions & 30 deletions core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,6 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
uint32 aux_stack_size;
uint64 aux_stack_start = 0;
int32 ret = -1;
int32 spawned_import_count = 0;
WASMExternInstance *spawned_imports = NULL;

bh_assert(module);
bh_assert(module_inst);
Expand All @@ -581,32 +579,12 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
}
#endif

/*
* build a imports list(WASMExternInstance[]) from parent's imports
*/
spawned_import_count = wasm_runtime_get_import_count(module);
spawned_imports =
wasm_runtime_malloc(sizeof(WASMExternInstance) * spawned_import_count);
if (spawned_imports == NULL) {
LOG_ERROR("Failed to allocate memory for imports");
goto fail;
}

ret = wasm_runtime_inherit_imports(module, module_inst, spawned_imports,
spawned_import_count);
if (ret != 0) {
LOG_ERROR("Failed to inherit imports");
goto fail;
}

if (!(new_module_inst = wasm_runtime_instantiate_internal(
if (!(new_module_inst = wasm_runtime_instantiate_with_inheritance(
module, module_inst, exec_env, stack_size,
0, // heap_size
0, // max_memory_pages
spawned_imports, // imports
spawned_import_count, // import_count
0, // heap_size
0, // max_memory_pages
NULL, 0)))
goto fail;
return -1;

/* Set custom_data to new module instance */
wasm_runtime_set_custom_data_internal(
Expand Down Expand Up @@ -666,13 +644,9 @@ 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)
Expand Down
41 changes: 7 additions & 34 deletions core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,46 +77,21 @@ 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 = -1;
int32 thread_id;
uint32 stack_size = 8192;
int32 ret = -1;
int32 spawned_import_count = 0;
WASMExternInstance *spawned_imports = NULL;

bh_assert(module);
bh_assert(module_inst);

stack_size = ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;

/*
* build a imports list(WASMExternInstance[]) from parent's imports
*/
spawned_import_count = wasm_runtime_get_import_count(module);
spawned_imports =
wasm_runtime_malloc(sizeof(WASMExternInstance) * spawned_import_count);
if (spawned_imports == NULL) {
LOG_ERROR("Failed to allocate memory for imports");
if (!(new_module_inst = wasm_runtime_instantiate_with_inheritance(
module, module_inst, exec_env, stack_size,
0, // heap_size
0, // max_memory_pages
NULL, 0)))
return -1;
}

ret = wasm_runtime_inherit_imports(module, module_inst, spawned_imports,
spawned_import_count);
if (ret != 0) {
LOG_ERROR("Failed to inherit imports");
goto free_imports;
}

new_module_inst = wasm_runtime_instantiate_internal(
module, module_inst, exec_env, stack_size,
0, // heap_size
0, // max_memory_pages
spawned_imports, // imports
spawned_import_count, // import_count
NULL, 0);
if (new_module_inst == NULL) {
LOG_ERROR("Failed to instantiate new module");
goto free_imports;
}

wasm_runtime_set_custom_data_internal(
new_module_inst, wasm_runtime_get_custom_data(module_inst));
Expand Down Expand Up @@ -154,18 +129,16 @@ 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:
deallocate_thread_id(thread_id);

thread_preparation_fail:
if (new_module_inst)
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;
}
Expand Down
34 changes: 4 additions & 30 deletions core/iwasm/libraries/thread-mgr/thread_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,40 +500,17 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
uint32 aux_stack_size;
uint64 aux_stack_start;
uint32 stack_size = 8192;
int32 ret = -1;
int32 spawned_import_count = 0;
WASMExternInstance *spawned_imports = NULL;

if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) {
return NULL;
}

/*
* build a imports list(WASMExternInstance[]) from parent's imports
*/
spawned_import_count = ((WASMModule *)module)->import_count;
spawned_imports =
wasm_runtime_malloc(sizeof(WASMExternInstance) * spawned_import_count);
if (spawned_imports == NULL) {
LOG_ERROR("Failed to allocate memory for imports");
return NULL;
}

ret = wasm_runtime_inherit_imports(module, module_inst, spawned_imports,
spawned_import_count);
if (ret != 0) {
LOG_ERROR("Failed to inherit imports");
goto release_imports;
}

if (!(new_module_inst = wasm_runtime_instantiate_internal(
if (!(new_module_inst = wasm_runtime_instantiate_with_inheritance(
module, module_inst, exec_env, stack_size,
0, // heap_size
0, // max_memory_pages
spawned_imports, // imports
spawned_import_count, // import_count
0, // heap_size
0, // max_memory_pages
NULL, 0))) {
goto release_imports;
return NULL;
}

/* Set custom_data to new module instance */
Expand Down Expand Up @@ -596,7 +573,6 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)

os_mutex_unlock(&cluster->lock);

wasm_runtime_free(spawned_imports);
return new_exec_env;

fail3:
Expand All @@ -607,8 +583,6 @@ 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);
release_imports:
wasm_runtime_free(spawned_imports);

return NULL;
}
Expand Down

0 comments on commit 20cd0b2

Please sign in to comment.