Compare commits

...

3 Commits

Author SHA1 Message Date
YAMAMOTO Takashi
86325e76bf
Merge 1a28133517 into 6253bd1b52 2025-09-05 10:15:42 +09:00
YAMAMOTO Takashi
6253bd1b52
Reduce the code duplication a bit (#4596)
Some checks failed
compilation on macos / build_iwasm (${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}, -DWAMR_DISABLE_HW_BOUND_CHECK=1, $FAST_INTERP_BUILD_OPTIONS, macos-13, darwin) (push) Has been cancelled
compilation on macos / build_iwasm (${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}, -DWAMR_DISABLE_HW_BOUND_CHECK=1, $LLVM_EAGER_JIT_BUILD_OPTIONS, macos-13, darwin) (push) Has been cancelled
compilation on macos / build_iwasm (${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}, -DWAMR_DISABLE_HW_BOUND_CHECK=1, $LLVM_LAZY_JIT_BUILD_OPTIONS, macos-13, darwin) (push) Has been cancelled
compilation on macos / build_samples_wasm_c_api (${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}, $AOT_BUILD_OPTIONS, macos-13) (push) Has been cancelled
compilation on macos / build_samples_wasm_c_api (${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}, $CLASSIC_INTERP_BUILD_OPTIONS, macos-13) (push) Has been cancelled
compilation on macos / build_samples_wasm_c_api (${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}, $FAST_INTERP_BUILD_OPTIONS, macos-13) (push) Has been cancelled
compilation on macos / build_samples_wasm_c_api (${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}, $FAST_JIT_BUILD_OPTIONS, macos-13) (push) Has been cancelled
compilation on macos / build_samples_wasm_c_api (${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}, $LLVM_EAGER_JIT_BUILD_OPTIONS, macos-13) (push) Has been cancelled
compilation on macos / build_samples_wasm_c_api (${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}, $LLVM_LAZY_JIT_BUILD_OPTIONS, macos-13) (push) Has been cancelled
compilation on macos / build_samples_wasm_c_api (${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}, $MULTI_TIER_JIT_BUILD_OPTIONS, macos-13) (push) Has been cancelled
compilation on macos / build_samples_others (${{ needs.build_llvm_libraries_on_arm_macos.outputs.cache_key }}, macos-14) (push) Has been cancelled
compilation on macos / build_samples_others (${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}, macos-13) (push) Has been cancelled
compilation on SGX / run_samples_file (-DWAMR_BUILD_SGX_IPFS=1, $AOT_BUILD_OPTIONS, ${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, linux-sgx) (push) Has been cancelled
compilation on SGX / run_samples_file (-DWAMR_BUILD_SGX_IPFS=1, $CLASSIC_INTERP_BUILD_OPTIONS, ${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, linux-sgx) (push) Has been cancelled
compilation on SGX / run_samples_file (-DWAMR_BUILD_SGX_IPFS=1, $FAST_INTERP_BUILD_OPTIONS, ${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, linux-sgx) (push) Has been cancelled
compilation on SGX / run_samples_file (-DWAMR_BUILD_SGX_IPFS=1, $FAST_JIT_BUILD_OPTIONS, ${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, linux-sgx) (push) Has been cancelled
compilation on SGX / spec_test_default (${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, aot, $DEFAULT_TEST_OPTIONS) (push) Has been cancelled
compilation on SGX / spec_test_default (${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, aot, $SIMD_TEST_OPTIONS) (push) Has been cancelled
compilation on SGX / spec_test_default (${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, aot, $XIP_TEST_OPTIONS) (push) Has been cancelled
compilation on SGX / spec_test_default (${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, classic-interp, $DEFAULT_TEST_OPTIONS) (push) Has been cancelled
compilation on SGX / spec_test_default (${{ needs.build_llvm_libraries.outputs.cache_key }}, ubuntu-22.04, fast-jit, $DEFAULT_TEST_OPTIONS) (push) Has been cancelled
compilation on windows-latest / build_wamrc (${{ needs.build_llvm_libraries_on_windows.outputs.cache_key }}, windows-latest) (push) Has been cancelled
compilation on windows-latest / test (classic-interp, $DEFAULT_TEST_OPTIONS) (push) Has been cancelled
compilation on windows-latest / test (classic-interp, $MULTI_MODULES_TEST_OPTIONS) (push) Has been cancelled
compilation on windows-latest / test (classic-interp, $THREADS_TEST_OPTIONS) (push) Has been cancelled
compilation on windows-latest / test (classic-interp, $WASI_TEST_OPTIONS) (push) Has been cancelled
compilation on windows-latest / test (fast-interp, $DEFAULT_TEST_OPTIONS) (push) Has been cancelled
compilation on windows-latest / test (fast-interp, $MULTI_MODULES_TEST_OPTIONS) (push) Has been cancelled
compilation on windows-latest / test (fast-interp, $THREADS_TEST_OPTIONS) (push) Has been cancelled
compilation on windows-latest / test (fast-interp, $WASI_TEST_OPTIONS) (push) Has been cancelled
This is a preparation for
https://github.com/bytecodealliance/wasm-micro-runtime/issues/4364

No functional changes are intended.
2025-09-04 10:58:09 +08:00
YAMAMOTO Takashi
1a28133517 Pass InstantiationArgs2 down to aot_instantiate/wasm_instantiate
This is a preparation for
https://github.com/bytecodealliance/wasm-micro-runtime/issues/4364

No functional changes are intended.
2025-09-02 13:56:10 +09:00
12 changed files with 82 additions and 73 deletions

View File

@ -4226,15 +4226,7 @@ create_module(char *name, char *error_buf, uint32 error_buf_size)
#endif
#if WASM_ENABLE_LIBC_WASI != 0
#if WASM_ENABLE_UVWASI == 0
module->wasi_args.stdio[0] = os_invalid_raw_handle();
module->wasi_args.stdio[1] = os_invalid_raw_handle();
module->wasi_args.stdio[2] = os_invalid_raw_handle();
#else
module->wasi_args.stdio[0] = os_get_invalid_handle();
module->wasi_args.stdio[1] = os_get_invalid_handle();
module->wasi_args.stdio[2] = os_get_invalid_handle();
#endif /* WASM_ENABLE_UVWASI == 0 */
wasi_args_set_defaults(&module->wasi_args);
#endif /* WASM_ENABLE_LIBC_WASI != 0 */
return module;

View File

@ -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;

View File

@ -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);
/**

View File

@ -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
@ -3438,6 +3440,21 @@ wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
#if WASM_ENABLE_LIBC_WASI != 0
void
wasi_args_set_defaults(WASIArguments *args)
{
memset(args, 0, sizeof(*args));
#if WASM_ENABLE_UVWASI == 0
args->stdio[0] = os_invalid_raw_handle();
args->stdio[1] = os_invalid_raw_handle();
args->stdio[2] = os_invalid_raw_handle();
#else
args->stdio[0] = os_get_invalid_handle();
args->stdio[1] = os_get_invalid_handle();
args->stdio[2] = os_get_invalid_handle();
#endif /* WASM_ENABLE_UVWASI == 0 */
}
static WASIArguments *
get_wasi_args_from_module(wasm_module_t module)
{
@ -7651,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;
@ -7681,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);

View File

@ -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
@ -1118,6 +1120,9 @@ wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst);
WASM_RUNTIME_API_EXTERN uint32_t
wasm_runtime_get_wasi_exit_code(WASMModuleInstanceCommon *module_inst);
void
wasi_args_set_defaults(WASIArguments *args);
bool
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
const char *dir_list[], uint32 dir_count,

View File

@ -6712,15 +6712,7 @@ create_module(char *name, char *error_buf, uint32 error_buf_size)
#endif
#if WASM_ENABLE_LIBC_WASI != 0
#if WASM_ENABLE_UVWASI == 0
module->wasi_args.stdio[0] = os_invalid_raw_handle();
module->wasi_args.stdio[1] = os_invalid_raw_handle();
module->wasi_args.stdio[2] = os_invalid_raw_handle();
#else
module->wasi_args.stdio[0] = os_get_invalid_handle();
module->wasi_args.stdio[1] = os_get_invalid_handle();
module->wasi_args.stdio[2] = os_get_invalid_handle();
#endif /* WASM_ENABLE_UVWASI == 0 */
wasi_args_set_defaults(&module->wasi_args);
#endif /* WASM_ENABLE_LIBC_WASI != 0 */
(void)ret;

View File

@ -3316,15 +3316,7 @@ create_module(char *name, char *error_buf, uint32 error_buf_size)
#endif
#if WASM_ENABLE_LIBC_WASI != 0
#if WASM_ENABLE_LIBC_UVWASI == 0
module->wasi_args.stdio[0] = os_invalid_raw_handle();
module->wasi_args.stdio[1] = os_invalid_raw_handle();
module->wasi_args.stdio[2] = os_invalid_raw_handle();
#else
module->wasi_args.stdio[0] = os_get_invalid_handle();
module->wasi_args.stdio[1] = os_get_invalid_handle();
module->wasi_args.stdio[2] = os_get_invalid_handle();
#endif /* WASM_ENABLE_UVWASI == 0 */
wasi_args_set_defaults(&module->wasi_args);
#endif /* WASM_ENABLE_LIBC_WASI != 0 */
(void)ret;

View File

@ -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;

View File

@ -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

View File

@ -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 */

View File

@ -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(

View File

@ -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;
}