diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 876e4b1f0..2705b6175 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1889,8 +1889,9 @@ check_linked_symbol(AOTModule *module, char *error_buf, uint32 error_buf_size) AOTModuleInstance * aot_instantiate(AOTModule *module, AOTModuleInstance *parent, - WASMExecEnv *exec_env_main, uint32 stack_size, uint32 heap_size, - uint32 max_memory_pages, char *error_buf, uint32 error_buf_size) + WASMExecEnv *exec_env_main, + const struct InstantiationArgs2 *args, char *error_buf, + uint32 error_buf_size) { AOTModuleInstance *module_inst; #if WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_REF_TYPES != 0 @@ -1908,6 +1909,9 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent, #if WASM_ENABLE_MULTI_MODULE != 0 bool ret = false; #endif + uint32 stack_size = args->v1.default_stack_size; + uint32 heap_size = args->v1.host_managed_heap_size; + uint32 max_memory_pages = args->v1.max_memory_pages; /* Align and validate heap size */ heap_size = align_uint(heap_size, 8); @@ -1989,7 +1993,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent, ret = wasm_runtime_sub_module_instantiate( (WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst, - stack_size, heap_size, max_memory_pages, error_buf, error_buf_size); + args, error_buf, error_buf_size); if (!ret) { LOG_DEBUG("build a sub module list failed"); goto fail; diff --git a/core/iwasm/aot/aot_runtime.h b/core/iwasm/aot/aot_runtime.h index d06cd1081..687c75e14 100644 --- a/core/iwasm/aot/aot_runtime.h +++ b/core/iwasm/aot/aot_runtime.h @@ -544,10 +544,7 @@ aot_resolve_import_func(AOTModule *module, AOTImportFunc *import_func); * * @param module the AOT module to instantiate * @param parent the parent module instance - * @param heap_size the default heap size of the module instance, a heap will - * be created besides the app memory space. Both wasm app and native - * function can allocate memory from the heap. If heap_size is 0, the - * default heap size will be used. + * @param args the instantiation parameters * @param error_buf buffer to output the error info if failed * @param error_buf_size the size of the error buffer * @@ -555,8 +552,8 @@ aot_resolve_import_func(AOTModule *module, AOTImportFunc *import_func); */ AOTModuleInstance * aot_instantiate(AOTModule *module, AOTModuleInstance *parent, - WASMExecEnv *exec_env_main, uint32 stack_size, uint32 heap_size, - uint32 max_memory_pages, char *error_buf, + WASMExecEnv *exec_env_main, + const struct InstantiationArgs2 *args, char *error_buf, uint32 error_buf_size); /** diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 26283b3f8..5c832a64e 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1623,41 +1623,45 @@ wasm_runtime_get_max_mem(uint32 max_memory_pages, uint32 module_init_page_count, WASMModuleInstanceCommon * wasm_runtime_instantiate_internal(WASMModuleCommon *module, WASMModuleInstanceCommon *parent, - WASMExecEnv *exec_env_main, uint32 stack_size, - uint32 heap_size, uint32 max_memory_pages, + WASMExecEnv *exec_env_main, + const struct InstantiationArgs2 *args, char *error_buf, uint32 error_buf_size) { #if WASM_ENABLE_INTERP != 0 if (module->module_type == Wasm_Module_Bytecode) return (WASMModuleInstanceCommon *)wasm_instantiate( (WASMModule *)module, (WASMModuleInstance *)parent, exec_env_main, - stack_size, heap_size, max_memory_pages, error_buf, error_buf_size); + args, error_buf, error_buf_size); #endif #if WASM_ENABLE_AOT != 0 if (module->module_type == Wasm_Module_AoT) return (WASMModuleInstanceCommon *)aot_instantiate( (AOTModule *)module, (AOTModuleInstance *)parent, exec_env_main, - stack_size, heap_size, max_memory_pages, error_buf, error_buf_size); + args, error_buf, error_buf_size); #endif set_error_buf(error_buf, error_buf_size, "Instantiate module failed, invalid module type"); return NULL; } +void +wasm_runtime_instantiation_args_set_defaults(struct InstantiationArgs2 *args) +{ + memset(args, 0, sizeof(*args)); +} + WASMModuleInstanceCommon * wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size, uint32 heap_size, char *error_buf, uint32 error_buf_size) { - return wasm_runtime_instantiate_internal(module, NULL, NULL, stack_size, - heap_size, 0, error_buf, - error_buf_size); -} - -static void -instantiation_args_set_defaults(struct InstantiationArgs2 *args) -{ - memset(args, 0, sizeof(*args)); + struct InstantiationArgs2 args; + wasm_runtime_instantiation_args_set_defaults(&args); + wasm_runtime_instantiation_args_set_default_stack_size(&args, stack_size); + wasm_runtime_instantiation_args_set_host_managed_heap_size(&args, + heap_size); + return wasm_runtime_instantiate_internal(module, NULL, NULL, &args, + error_buf, error_buf_size); } WASMModuleInstanceCommon * @@ -1666,7 +1670,7 @@ wasm_runtime_instantiate_ex(WASMModuleCommon *module, uint32 error_buf_size) { struct InstantiationArgs2 v2; - instantiation_args_set_defaults(&v2); + wasm_runtime_instantiation_args_set_defaults(&v2); v2.v1 = *args; return wasm_runtime_instantiate_ex2(module, &v2, error_buf, error_buf_size); } @@ -1678,7 +1682,7 @@ wasm_runtime_instantiation_args_create(struct InstantiationArgs2 **p) if (args == NULL) { return false; } - instantiation_args_set_defaults(args); + wasm_runtime_instantiation_args_set_defaults(args); *p = args; return true; } @@ -1715,10 +1719,8 @@ wasm_runtime_instantiate_ex2(WASMModuleCommon *module, const struct InstantiationArgs2 *args, char *error_buf, uint32 error_buf_size) { - return wasm_runtime_instantiate_internal( - module, NULL, NULL, args->v1.default_stack_size, - args->v1.host_managed_heap_size, args->v1.max_memory_pages, error_buf, - error_buf_size); + return wasm_runtime_instantiate_internal(module, NULL, NULL, args, + error_buf, error_buf_size); } void @@ -7666,9 +7668,8 @@ delete_loading_module: bool wasm_runtime_sub_module_instantiate(WASMModuleCommon *module, WASMModuleInstanceCommon *module_inst, - uint32 stack_size, uint32 heap_size, - uint32 max_memory_pages, char *error_buf, - uint32 error_buf_size) + const struct InstantiationArgs2 *args, + char *error_buf, uint32 error_buf_size) { bh_list *sub_module_inst_list = NULL; WASMRegisteredModule *sub_module_list_node = NULL; @@ -7696,8 +7697,7 @@ wasm_runtime_sub_module_instantiate(WASMModuleCommon *module, WASMModuleCommon *sub_module = sub_module_list_node->module; WASMModuleInstanceCommon *sub_module_inst = NULL; sub_module_inst = wasm_runtime_instantiate_internal( - sub_module, NULL, NULL, stack_size, heap_size, max_memory_pages, - error_buf, error_buf_size); + sub_module, NULL, NULL, args, error_buf, error_buf_size); if (!sub_module_inst) { LOG_DEBUG("instantiate %s failed", sub_module_list_node->module_name); diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index a315c75da..88af68744 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -616,6 +616,9 @@ struct InstantiationArgs2 { InstantiationArgs v1; }; +void +wasm_runtime_instantiation_args_set_defaults(struct InstantiationArgs2 *args); + /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN bool wasm_runtime_init(void); @@ -683,8 +686,8 @@ wasm_runtime_get_max_mem(uint32 max_memory_pages, uint32 module_init_page_count, WASMModuleInstanceCommon * wasm_runtime_instantiate_internal(WASMModuleCommon *module, WASMModuleInstanceCommon *parent, - WASMExecEnv *exec_env_main, uint32 stack_size, - uint32 heap_size, uint32 max_memory_pages, + WASMExecEnv *exec_env_main, + const struct InstantiationArgs2 *args, char *error_buf, uint32 error_buf_size); /* Internal API */ @@ -1064,9 +1067,8 @@ wasm_runtime_load_depended_module(const WASMModuleCommon *parent_module, bool wasm_runtime_sub_module_instantiate(WASMModuleCommon *module, WASMModuleInstanceCommon *module_inst, - uint32 stack_size, uint32 heap_size, - uint32 max_memory_pages, char *error_buf, - uint32 error_buf_size); + const struct InstantiationArgs2 *args, + char *error_buf, uint32 error_buf_size); void wasm_runtime_sub_module_deinstantiate(WASMModuleInstanceCommon *module_inst); #endif diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 55e65142a..331c55f22 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -2421,8 +2421,8 @@ wasm_set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode) */ WASMModuleInstance * wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, - WASMExecEnv *exec_env_main, uint32 stack_size, - uint32 heap_size, uint32 max_memory_pages, char *error_buf, + WASMExecEnv *exec_env_main, + const struct InstantiationArgs2 *args, char *error_buf, uint32 error_buf_size) { WASMModuleInstance *module_inst; @@ -2440,6 +2440,9 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, bool ret = false; #endif const bool is_sub_inst = parent != NULL; + uint32 stack_size = args->v1.default_stack_size; + uint32 heap_size = args->v1.host_managed_heap_size; + uint32 max_memory_pages = args->v1.max_memory_pages; if (!module) return NULL; @@ -2515,7 +2518,7 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, &module_inst->e->sub_module_inst_list_head; ret = wasm_runtime_sub_module_instantiate( (WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst, - stack_size, heap_size, max_memory_pages, error_buf, error_buf_size); + args, error_buf, error_buf_size); if (!ret) { LOG_DEBUG("build a sub module list failed"); goto fail; diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index 16c670f0f..0ba2049af 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -553,8 +553,8 @@ wasm_resolve_import_func(const WASMModule *module, WASMModuleInstance * wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, - WASMExecEnv *exec_env_main, uint32 stack_size, - uint32 heap_size, uint32 max_memory_pages, char *error_buf, + WASMExecEnv *exec_env_main, + const struct InstantiationArgs2 *args, char *error_buf, uint32 error_buf_size); void diff --git a/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c b/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c index b3fa57d72..d7dca2f1d 100644 --- a/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c +++ b/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c @@ -561,6 +561,7 @@ pthread_create_wrapper(wasm_exec_env_t exec_env, uint32 aux_stack_size; uint64 aux_stack_start = 0; int32 ret = -1; + struct InstantiationArgs2 args; bh_assert(module); bh_assert(module_inst); @@ -579,8 +580,10 @@ pthread_create_wrapper(wasm_exec_env_t exec_env, } #endif + wasm_runtime_instantiation_args_set_defaults(&args); + wasm_runtime_instantiation_args_set_default_stack_size(&args, stack_size); if (!(new_module_inst = wasm_runtime_instantiate_internal( - module, module_inst, exec_env, stack_size, 0, 0, NULL, 0))) + module, module_inst, exec_env, &args, NULL, 0))) return -1; /* Set custom_data to new module instance */ 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 c9512fb43..65b75ac28 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 @@ -80,14 +80,17 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg) int32 thread_id; uint32 stack_size = 8192; int32 ret = -1; + struct InstantiationArgs2 args; bh_assert(module); bh_assert(module_inst); stack_size = ((WASMModuleInstance *)module_inst)->default_wasm_stack_size; + wasm_runtime_instantiation_args_set_defaults(&args); + wasm_runtime_instantiation_args_set_default_stack_size(&args, stack_size); if (!(new_module_inst = wasm_runtime_instantiate_internal( - module, module_inst, exec_env, stack_size, 0, 0, NULL, 0))) + module, module_inst, exec_env, &args, NULL, 0))) return -1; wasm_runtime_set_custom_data_internal( diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index 8f3a6317b..5a647a9b6 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -501,13 +501,16 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) uint32 aux_stack_size; uint64 aux_stack_start; uint32 stack_size = 8192; + struct InstantiationArgs2 args; if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) { return NULL; } + wasm_runtime_instantiation_args_set_defaults(&args); + wasm_runtime_instantiation_args_set_default_stack_size(&args, stack_size); if (!(new_module_inst = wasm_runtime_instantiate_internal( - module, module_inst, exec_env, stack_size, 0, 0, NULL, 0))) { + module, module_inst, exec_env, &args, NULL, 0))) { return NULL; }