This commit is contained in:
YAMAMOTO Takashi 2025-09-05 10:15:42 +09:00 committed by GitHub
commit 86325e76bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 61 additions and 46 deletions

View File

@ -1889,8 +1889,9 @@ check_linked_symbol(AOTModule *module, char *error_buf, uint32 error_buf_size)
AOTModuleInstance * AOTModuleInstance *
aot_instantiate(AOTModule *module, AOTModuleInstance *parent, aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
WASMExecEnv *exec_env_main, uint32 stack_size, uint32 heap_size, WASMExecEnv *exec_env_main,
uint32 max_memory_pages, char *error_buf, uint32 error_buf_size) const struct InstantiationArgs2 *args, char *error_buf,
uint32 error_buf_size)
{ {
AOTModuleInstance *module_inst; AOTModuleInstance *module_inst;
#if WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_REF_TYPES != 0 #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 #if WASM_ENABLE_MULTI_MODULE != 0
bool ret = false; bool ret = false;
#endif #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 */ /* Align and validate heap size */
heap_size = align_uint(heap_size, 8); heap_size = align_uint(heap_size, 8);
@ -1989,7 +1993,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
ret = wasm_runtime_sub_module_instantiate( ret = wasm_runtime_sub_module_instantiate(
(WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst, (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) { if (!ret) {
LOG_DEBUG("build a sub module list failed"); LOG_DEBUG("build a sub module list failed");
goto fail; goto fail;

View File

@ -544,10 +544,7 @@ aot_resolve_import_func(AOTModule *module, AOTImportFunc *import_func);
* *
* @param module the AOT module to instantiate * @param module the AOT module to instantiate
* @param parent the parent module instance * @param parent the parent module instance
* @param heap_size the default heap size of the module instance, a heap will * @param args the instantiation parameters
* 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 error_buf buffer to output the error info if failed * @param error_buf buffer to output the error info if failed
* @param error_buf_size the size of the error buffer * @param error_buf_size the size of the error buffer
* *
@ -555,8 +552,8 @@ aot_resolve_import_func(AOTModule *module, AOTImportFunc *import_func);
*/ */
AOTModuleInstance * AOTModuleInstance *
aot_instantiate(AOTModule *module, AOTModuleInstance *parent, aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
WASMExecEnv *exec_env_main, uint32 stack_size, uint32 heap_size, WASMExecEnv *exec_env_main,
uint32 max_memory_pages, char *error_buf, const struct InstantiationArgs2 *args, char *error_buf,
uint32 error_buf_size); uint32 error_buf_size);
/** /**

View File

@ -1623,41 +1623,45 @@ wasm_runtime_get_max_mem(uint32 max_memory_pages, uint32 module_init_page_count,
WASMModuleInstanceCommon * WASMModuleInstanceCommon *
wasm_runtime_instantiate_internal(WASMModuleCommon *module, wasm_runtime_instantiate_internal(WASMModuleCommon *module,
WASMModuleInstanceCommon *parent, WASMModuleInstanceCommon *parent,
WASMExecEnv *exec_env_main, uint32 stack_size, WASMExecEnv *exec_env_main,
uint32 heap_size, uint32 max_memory_pages, const struct InstantiationArgs2 *args,
char *error_buf, uint32 error_buf_size) char *error_buf, uint32 error_buf_size)
{ {
#if WASM_ENABLE_INTERP != 0 #if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode) if (module->module_type == Wasm_Module_Bytecode)
return (WASMModuleInstanceCommon *)wasm_instantiate( return (WASMModuleInstanceCommon *)wasm_instantiate(
(WASMModule *)module, (WASMModuleInstance *)parent, exec_env_main, (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 #endif
#if WASM_ENABLE_AOT != 0 #if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT) if (module->module_type == Wasm_Module_AoT)
return (WASMModuleInstanceCommon *)aot_instantiate( return (WASMModuleInstanceCommon *)aot_instantiate(
(AOTModule *)module, (AOTModuleInstance *)parent, exec_env_main, (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 #endif
set_error_buf(error_buf, error_buf_size, set_error_buf(error_buf, error_buf_size,
"Instantiate module failed, invalid module type"); "Instantiate module failed, invalid module type");
return NULL; return NULL;
} }
void
wasm_runtime_instantiation_args_set_defaults(struct InstantiationArgs2 *args)
{
memset(args, 0, sizeof(*args));
}
WASMModuleInstanceCommon * WASMModuleInstanceCommon *
wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size, wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
uint32 heap_size, char *error_buf, uint32 heap_size, char *error_buf,
uint32 error_buf_size) uint32 error_buf_size)
{ {
return wasm_runtime_instantiate_internal(module, NULL, NULL, stack_size, struct InstantiationArgs2 args;
heap_size, 0, error_buf, wasm_runtime_instantiation_args_set_defaults(&args);
error_buf_size); wasm_runtime_instantiation_args_set_default_stack_size(&args, stack_size);
} wasm_runtime_instantiation_args_set_host_managed_heap_size(&args,
heap_size);
static void return wasm_runtime_instantiate_internal(module, NULL, NULL, &args,
instantiation_args_set_defaults(struct InstantiationArgs2 *args) error_buf, error_buf_size);
{
memset(args, 0, sizeof(*args));
} }
WASMModuleInstanceCommon * WASMModuleInstanceCommon *
@ -1666,7 +1670,7 @@ wasm_runtime_instantiate_ex(WASMModuleCommon *module,
uint32 error_buf_size) uint32 error_buf_size)
{ {
struct InstantiationArgs2 v2; struct InstantiationArgs2 v2;
instantiation_args_set_defaults(&v2); wasm_runtime_instantiation_args_set_defaults(&v2);
v2.v1 = *args; v2.v1 = *args;
return wasm_runtime_instantiate_ex2(module, &v2, error_buf, error_buf_size); 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) { if (args == NULL) {
return false; return false;
} }
instantiation_args_set_defaults(args); wasm_runtime_instantiation_args_set_defaults(args);
*p = args; *p = args;
return true; return true;
} }
@ -1715,10 +1719,8 @@ wasm_runtime_instantiate_ex2(WASMModuleCommon *module,
const struct InstantiationArgs2 *args, const struct InstantiationArgs2 *args,
char *error_buf, uint32 error_buf_size) char *error_buf, uint32 error_buf_size)
{ {
return wasm_runtime_instantiate_internal( return wasm_runtime_instantiate_internal(module, NULL, NULL, args,
module, NULL, NULL, args->v1.default_stack_size, error_buf, error_buf_size);
args->v1.host_managed_heap_size, args->v1.max_memory_pages, error_buf,
error_buf_size);
} }
void void
@ -7666,9 +7668,8 @@ delete_loading_module:
bool bool
wasm_runtime_sub_module_instantiate(WASMModuleCommon *module, wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
WASMModuleInstanceCommon *module_inst, WASMModuleInstanceCommon *module_inst,
uint32 stack_size, uint32 heap_size, const struct InstantiationArgs2 *args,
uint32 max_memory_pages, char *error_buf, char *error_buf, uint32 error_buf_size)
uint32 error_buf_size)
{ {
bh_list *sub_module_inst_list = NULL; bh_list *sub_module_inst_list = NULL;
WASMRegisteredModule *sub_module_list_node = 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; WASMModuleCommon *sub_module = sub_module_list_node->module;
WASMModuleInstanceCommon *sub_module_inst = NULL; WASMModuleInstanceCommon *sub_module_inst = NULL;
sub_module_inst = wasm_runtime_instantiate_internal( sub_module_inst = wasm_runtime_instantiate_internal(
sub_module, NULL, NULL, stack_size, heap_size, max_memory_pages, sub_module, NULL, NULL, args, error_buf, error_buf_size);
error_buf, error_buf_size);
if (!sub_module_inst) { if (!sub_module_inst) {
LOG_DEBUG("instantiate %s failed", LOG_DEBUG("instantiate %s failed",
sub_module_list_node->module_name); sub_module_list_node->module_name);

View File

@ -616,6 +616,9 @@ struct InstantiationArgs2 {
InstantiationArgs v1; InstantiationArgs v1;
}; };
void
wasm_runtime_instantiation_args_set_defaults(struct InstantiationArgs2 *args);
/* See wasm_export.h for description */ /* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN bool WASM_RUNTIME_API_EXTERN bool
wasm_runtime_init(void); wasm_runtime_init(void);
@ -683,8 +686,8 @@ wasm_runtime_get_max_mem(uint32 max_memory_pages, uint32 module_init_page_count,
WASMModuleInstanceCommon * WASMModuleInstanceCommon *
wasm_runtime_instantiate_internal(WASMModuleCommon *module, wasm_runtime_instantiate_internal(WASMModuleCommon *module,
WASMModuleInstanceCommon *parent, WASMModuleInstanceCommon *parent,
WASMExecEnv *exec_env_main, uint32 stack_size, WASMExecEnv *exec_env_main,
uint32 heap_size, uint32 max_memory_pages, const struct InstantiationArgs2 *args,
char *error_buf, uint32 error_buf_size); char *error_buf, uint32 error_buf_size);
/* Internal API */ /* Internal API */
@ -1064,9 +1067,8 @@ wasm_runtime_load_depended_module(const WASMModuleCommon *parent_module,
bool bool
wasm_runtime_sub_module_instantiate(WASMModuleCommon *module, wasm_runtime_sub_module_instantiate(WASMModuleCommon *module,
WASMModuleInstanceCommon *module_inst, WASMModuleInstanceCommon *module_inst,
uint32 stack_size, uint32 heap_size, const struct InstantiationArgs2 *args,
uint32 max_memory_pages, char *error_buf, char *error_buf, uint32 error_buf_size);
uint32 error_buf_size);
void void
wasm_runtime_sub_module_deinstantiate(WASMModuleInstanceCommon *module_inst); wasm_runtime_sub_module_deinstantiate(WASMModuleInstanceCommon *module_inst);
#endif #endif

View File

@ -2421,8 +2421,8 @@ wasm_set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode)
*/ */
WASMModuleInstance * WASMModuleInstance *
wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
WASMExecEnv *exec_env_main, uint32 stack_size, WASMExecEnv *exec_env_main,
uint32 heap_size, uint32 max_memory_pages, char *error_buf, const struct InstantiationArgs2 *args, char *error_buf,
uint32 error_buf_size) uint32 error_buf_size)
{ {
WASMModuleInstance *module_inst; WASMModuleInstance *module_inst;
@ -2440,6 +2440,9 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
bool ret = false; bool ret = false;
#endif #endif
const bool is_sub_inst = parent != NULL; 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) if (!module)
return NULL; return NULL;
@ -2515,7 +2518,7 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
&module_inst->e->sub_module_inst_list_head; &module_inst->e->sub_module_inst_list_head;
ret = wasm_runtime_sub_module_instantiate( ret = wasm_runtime_sub_module_instantiate(
(WASMModuleCommon *)module, (WASMModuleInstanceCommon *)module_inst, (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) { if (!ret) {
LOG_DEBUG("build a sub module list failed"); LOG_DEBUG("build a sub module list failed");
goto fail; goto fail;

View File

@ -553,8 +553,8 @@ wasm_resolve_import_func(const WASMModule *module,
WASMModuleInstance * WASMModuleInstance *
wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
WASMExecEnv *exec_env_main, uint32 stack_size, WASMExecEnv *exec_env_main,
uint32 heap_size, uint32 max_memory_pages, char *error_buf, const struct InstantiationArgs2 *args, char *error_buf,
uint32 error_buf_size); uint32 error_buf_size);
void void

View File

@ -561,6 +561,7 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
uint32 aux_stack_size; uint32 aux_stack_size;
uint64 aux_stack_start = 0; uint64 aux_stack_start = 0;
int32 ret = -1; int32 ret = -1;
struct InstantiationArgs2 args;
bh_assert(module); bh_assert(module);
bh_assert(module_inst); bh_assert(module_inst);
@ -579,8 +580,10 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
} }
#endif #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( 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; return -1;
/* Set custom_data to new module instance */ /* Set custom_data to new module instance */

View File

@ -80,14 +80,17 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
int32 thread_id; int32 thread_id;
uint32 stack_size = 8192; uint32 stack_size = 8192;
int32 ret = -1; int32 ret = -1;
struct InstantiationArgs2 args;
bh_assert(module); bh_assert(module);
bh_assert(module_inst); bh_assert(module_inst);
stack_size = ((WASMModuleInstance *)module_inst)->default_wasm_stack_size; 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( 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; return -1;
wasm_runtime_set_custom_data_internal( wasm_runtime_set_custom_data_internal(

View File

@ -501,13 +501,16 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
uint32 aux_stack_size; uint32 aux_stack_size;
uint64 aux_stack_start; uint64 aux_stack_start;
uint32 stack_size = 8192; uint32 stack_size = 8192;
struct InstantiationArgs2 args;
if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) { if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) {
return NULL; 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( 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; return NULL;
} }