From 5d48cfdbc09553d01211b10a649e15c7fa8e644c Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 14 Jul 2025 14:06:23 +0900 Subject: [PATCH] introduce wasm_runtime_instantiate_ex2 (#4444) * introduce wasm_runtime_instantiate_ex2 at this point, just a slightly inefficiant functionality equivalent of wasm_runtime_instantiate_ex. however, unlike wasm_runtime_instantiate_ex, this one is designed to be extendable without breaking the user-visible ABI. because the definition of InstantiationArgs2 is not exposed to users, we can safely add new members to it. this commit also makes wasm_runtime_instantiate_ex a wrapper of wasm_runtime_instantiate_ex2. if this goes well, maybe it's a good idea to apply a similar pattern to RuntimeInitArgs, LoadArgs, SharedHeapInitArgs, etc. i started with InstantiationArgs just because i happen to have a need to extend it for wasi-nn. cf. https://github.com/bytecodealliance/wasm-micro-runtime/issues/4364 https://github.com/bytecodealliance/wasm-micro-runtime/issues/4331 * product-mini/platforms/posix: use wasm_runtime_instantiate_ex2 --- core/iwasm/common/wasm_runtime_common.c | 60 ++++++++++++++++++++++++- core/iwasm/common/wasm_runtime_common.h | 38 ++++++++++++++++ core/iwasm/include/wasm_export.h | 42 +++++++++++++++++ product-mini/platforms/posix/main.c | 17 +++++-- 4 files changed, 152 insertions(+), 5 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 07fd0e859..b32110272 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1654,14 +1654,70 @@ wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size, error_buf_size); } +static void +instantiation_args_set_defaults(struct InstantiationArgs2 *args) +{ + memset(args, 0, sizeof(*args)); +} + WASMModuleInstanceCommon * wasm_runtime_instantiate_ex(WASMModuleCommon *module, const InstantiationArgs *args, char *error_buf, uint32 error_buf_size) +{ + struct InstantiationArgs2 v2; + instantiation_args_set_defaults(&v2); + v2.v1 = *args; + return wasm_runtime_instantiate_ex2(module, &v2, error_buf, error_buf_size); +} + +bool +wasm_runtime_instantiation_args_create(struct InstantiationArgs2 **p) +{ + struct InstantiationArgs2 *args = wasm_runtime_malloc(sizeof(*args)); + if (args == NULL) { + return false; + } + instantiation_args_set_defaults(args); + *p = args; + return true; +} + +void +wasm_runtime_instantiation_args_destroy(struct InstantiationArgs2 *p) +{ + wasm_runtime_free(p); +} + +void +wasm_runtime_instantiation_args_set_default_stack_size( + struct InstantiationArgs2 *p, uint32 v) +{ + p->v1.default_stack_size = v; +} + +void +wasm_runtime_instantiation_args_set_host_managed_heap_size( + struct InstantiationArgs2 *p, uint32 v) +{ + p->v1.host_managed_heap_size = v; +} + +void +wasm_runtime_instantiation_args_set_max_memory_pages( + struct InstantiationArgs2 *p, uint32 v) +{ + p->v1.max_memory_pages = v; +} + +WASMModuleInstanceCommon * +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->default_stack_size, - args->host_managed_heap_size, args->max_memory_pages, error_buf, + module, NULL, NULL, args->v1.default_stack_size, + args->v1.host_managed_heap_size, args->v1.max_memory_pages, error_buf, error_buf_size); } diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index a40756a45..324620bef 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -612,6 +612,10 @@ WASMExecEnv * wasm_runtime_get_exec_env_tls(void); #endif +struct InstantiationArgs2 { + InstantiationArgs v1; +}; + /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN bool wasm_runtime_init(void); @@ -700,6 +704,40 @@ wasm_runtime_instantiate_ex(WASMModuleCommon *module, const InstantiationArgs *args, char *error_buf, uint32 error_buf_size); +/* See wasm_export.h for description */ +WASM_RUNTIME_API_EXTERN +bool +wasm_runtime_instantiation_args_create(struct InstantiationArgs2 **p); + +/* See wasm_export.h for description */ +WASM_RUNTIME_API_EXTERN +void +wasm_runtime_instantiation_args_destroy(struct InstantiationArgs2 *p); + +/* See wasm_export.h for description */ +WASM_RUNTIME_API_EXTERN +void +wasm_runtime_instantiation_args_set_default_stack_size( + struct InstantiationArgs2 *p, uint32 v); + +/* See wasm_export.h for description */ +WASM_RUNTIME_API_EXTERN +void +wasm_runtime_instantiation_args_set_host_managed_heap_size( + struct InstantiationArgs2 *p, uint32 v); + +/* See wasm_export.h for description */ +WASM_RUNTIME_API_EXTERN +void +wasm_runtime_instantiation_args_set_max_memory_pages( + struct InstantiationArgs2 *p, uint32 v); + +/* See wasm_export.h for description */ +WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon * +wasm_runtime_instantiate_ex2(WASMModuleCommon *module, + const struct InstantiationArgs2 *args, + char *error_buf, uint32 error_buf_size); + /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN bool wasm_runtime_set_running_mode(wasm_module_inst_t module_inst, diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 59f93dc78..81efb8f6f 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -289,6 +289,8 @@ typedef struct InstantiationArgs { } InstantiationArgs; #endif /* INSTANTIATION_ARGS_OPTION_DEFINED */ +struct InstantiationArgs2; + #ifndef WASM_VALKIND_T_DEFINED #define WASM_VALKIND_T_DEFINED typedef uint8_t wasm_valkind_t; @@ -733,6 +735,46 @@ wasm_runtime_instantiate_ex(const wasm_module_t module, const InstantiationArgs *args, char *error_buf, uint32_t error_buf_size); +/** + * Create an InstantiationArgs2 object with default parameters. + * + * @return true if success, false otherwise + */ +WASM_RUNTIME_API_EXTERN bool +wasm_runtime_instantiation_args_create(struct InstantiationArgs2 **p); + +/** + * Dispose an InstantiationArgs2 object. + */ +WASM_RUNTIME_API_EXTERN void +wasm_runtime_instantiation_args_destroy(struct InstantiationArgs2 *p); + +/** + * Setter functions for the InstantiationArgs2 object. + */ +WASM_RUNTIME_API_EXTERN void +wasm_runtime_instantiation_args_set_default_stack_size( + struct InstantiationArgs2 *p, uint32_t v); + +WASM_RUNTIME_API_EXTERN void +wasm_runtime_instantiation_args_set_host_managed_heap_size( + struct InstantiationArgs2 *p, uint32_t v); + +WASM_RUNTIME_API_EXTERN void +wasm_runtime_instantiation_args_set_max_memory_pages( + struct InstantiationArgs2 *p, uint32_t v); + +/** + * Instantiate a WASM module, with specified instantiation arguments + * + * Same as wasm_runtime_instantiate_ex, but this version takes + * InstantiationArgs2, which can be extended without breaking the ABI. + */ +WASM_RUNTIME_API_EXTERN wasm_module_inst_t +wasm_runtime_instantiate_ex2(const wasm_module_t module, + const struct InstantiationArgs2 *args, + char *error_buf, uint32_t error_buf_size); + /** * Set the running mode of a WASM module instance, override the * default running mode of the runtime. Note that it only makes sense when diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index 2790a12eb..f95b0d2d6 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -596,6 +596,7 @@ main(int argc, char *argv[]) wasm_module_inst_t wasm_module_inst = NULL; RunningMode running_mode = 0; RuntimeInitArgs init_args; + struct InstantiationArgs2 *inst_args; char error_buf[128] = { 0 }; #if WASM_ENABLE_LOG != 0 int log_verbose_level = 2; @@ -949,10 +950,20 @@ main(int argc, char *argv[]) libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx); #endif + if (!wasm_runtime_instantiation_args_create(&inst_args)) { + printf("failed to create instantiate args\n"); + goto fail3; + } + wasm_runtime_instantiation_args_set_default_stack_size(inst_args, + stack_size); + wasm_runtime_instantiation_args_set_host_managed_heap_size(inst_args, + heap_size); + /* instantiate the module */ - if (!(wasm_module_inst = - wasm_runtime_instantiate(wasm_module, stack_size, heap_size, - error_buf, sizeof(error_buf)))) { + wasm_module_inst = wasm_runtime_instantiate_ex2( + wasm_module, inst_args, error_buf, sizeof(error_buf)); + wasm_runtime_instantiation_args_destroy(inst_args); + if (!wasm_module_inst) { printf("%s\n", error_buf); goto fail3; }