mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-11-27 10:00:59 +00:00
add a set of apis to configure wasi via InstantiationArgs2 (#4707)
* track if WASIArguments is configured by user i plan to use this to decide which wasi arguments (the one from module or the one from InstantiationArgs2) to use. * add WASIArguments to InstantiationArgs2 * use wasi configuration from InstantiationArgs2 if any fallback to the via-module configuration for now. * add a few api to configure wasi via InstantiationArgs2 * configure wasi via InstantiationArgs2 for platforms using libc_wasi.c * rt-thread: migrate to libc_wasi_set_init_args * common/libc_wasi.c: retire libc_wasi_init * fix build without wasi
This commit is contained in:
parent
0cefefab1e
commit
912c2a6e31
|
|
@ -2081,17 +2081,25 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
|
|||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
if (!is_sub_inst) {
|
||||
const WASIArguments *wasi_args = &args->wasi;
|
||||
if (module->wasi_args.set_by_user) {
|
||||
if (wasi_args->set_by_user) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"WASI configuration was given via both of module "
|
||||
"and InstantiationArgs2");
|
||||
goto fail;
|
||||
}
|
||||
wasi_args = &module->wasi_args;
|
||||
}
|
||||
if (!wasm_runtime_init_wasi(
|
||||
(WASMModuleInstanceCommon *)module_inst,
|
||||
module->wasi_args.dir_list, module->wasi_args.dir_count,
|
||||
module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
|
||||
module->wasi_args.env, module->wasi_args.env_count,
|
||||
module->wasi_args.addr_pool, module->wasi_args.addr_count,
|
||||
module->wasi_args.ns_lookup_pool,
|
||||
module->wasi_args.ns_lookup_count, module->wasi_args.argv,
|
||||
module->wasi_args.argc, module->wasi_args.stdio[0],
|
||||
module->wasi_args.stdio[1], module->wasi_args.stdio[2],
|
||||
error_buf, error_buf_size))
|
||||
(WASMModuleInstanceCommon *)module_inst, wasi_args->dir_list,
|
||||
wasi_args->dir_count, wasi_args->map_dir_list,
|
||||
wasi_args->map_dir_count, wasi_args->env, wasi_args->env_count,
|
||||
wasi_args->addr_pool, wasi_args->addr_count,
|
||||
wasi_args->ns_lookup_pool, wasi_args->ns_lookup_count,
|
||||
wasi_args->argv, wasi_args->argc, wasi_args->stdio[0],
|
||||
wasi_args->stdio[1], wasi_args->stdio[2], error_buf,
|
||||
error_buf_size))
|
||||
goto fail;
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1648,6 +1648,9 @@ void
|
|||
wasm_runtime_instantiation_args_set_defaults(struct InstantiationArgs2 *args)
|
||||
{
|
||||
memset(args, 0, sizeof(*args));
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
wasi_args_set_defaults(&args->wasi);
|
||||
#endif
|
||||
}
|
||||
|
||||
WASMModuleInstanceCommon *
|
||||
|
|
@ -1714,6 +1717,84 @@ wasm_runtime_instantiation_args_set_max_memory_pages(
|
|||
p->v1.max_memory_pages = v;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
void
|
||||
wasm_runtime_instantiation_args_set_wasi_arg(struct InstantiationArgs2 *p,
|
||||
char *argv[], int argc)
|
||||
{
|
||||
WASIArguments *wasi_args = &p->wasi;
|
||||
|
||||
wasi_args->argv = argv;
|
||||
wasi_args->argc = (uint32)argc;
|
||||
wasi_args->set_by_user = true;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_instantiation_args_set_wasi_env(struct InstantiationArgs2 *p,
|
||||
const char *env[],
|
||||
uint32 env_count)
|
||||
{
|
||||
WASIArguments *wasi_args = &p->wasi;
|
||||
|
||||
wasi_args->env = env;
|
||||
wasi_args->env_count = env_count;
|
||||
wasi_args->set_by_user = true;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_instantiation_args_set_wasi_dir(struct InstantiationArgs2 *p,
|
||||
const char *dir_list[],
|
||||
uint32 dir_count,
|
||||
const char *map_dir_list[],
|
||||
uint32 map_dir_count)
|
||||
{
|
||||
WASIArguments *wasi_args = &p->wasi;
|
||||
|
||||
wasi_args->dir_list = dir_list;
|
||||
wasi_args->dir_count = dir_count;
|
||||
wasi_args->map_dir_list = map_dir_list;
|
||||
wasi_args->map_dir_count = map_dir_count;
|
||||
wasi_args->set_by_user = true;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_instantiation_args_set_wasi_stdio(struct InstantiationArgs2 *p,
|
||||
int64 stdinfd, int64 stdoutfd,
|
||||
int64 stderrfd)
|
||||
{
|
||||
WASIArguments *wasi_args = &p->wasi;
|
||||
|
||||
wasi_args->stdio[0] = (os_raw_file_handle)stdinfd;
|
||||
wasi_args->stdio[1] = (os_raw_file_handle)stdoutfd;
|
||||
wasi_args->stdio[2] = (os_raw_file_handle)stderrfd;
|
||||
wasi_args->set_by_user = true;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_instantiation_args_set_wasi_addr_pool(struct InstantiationArgs2 *p,
|
||||
const char *addr_pool[],
|
||||
uint32 addr_pool_size)
|
||||
{
|
||||
WASIArguments *wasi_args = &p->wasi;
|
||||
|
||||
wasi_args->addr_pool = addr_pool;
|
||||
wasi_args->addr_count = addr_pool_size;
|
||||
wasi_args->set_by_user = true;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_instantiation_args_set_wasi_ns_lookup_pool(
|
||||
struct InstantiationArgs2 *p, const char *ns_lookup_pool[],
|
||||
uint32 ns_lookup_pool_size)
|
||||
{
|
||||
WASIArguments *wasi_args = &p->wasi;
|
||||
|
||||
wasi_args->ns_lookup_pool = ns_lookup_pool;
|
||||
wasi_args->ns_lookup_count = ns_lookup_pool_size;
|
||||
wasi_args->set_by_user = true;
|
||||
}
|
||||
#endif /* WASM_ENABLE_LIBC_WASI != 0 */
|
||||
|
||||
WASMModuleInstanceCommon *
|
||||
wasm_runtime_instantiate_ex2(WASMModuleCommon *module,
|
||||
const struct InstantiationArgs2 *args,
|
||||
|
|
@ -3494,6 +3575,7 @@ wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
|
|||
wasi_args->stdio[0] = (os_raw_file_handle)stdinfd;
|
||||
wasi_args->stdio[1] = (os_raw_file_handle)stdoutfd;
|
||||
wasi_args->stdio[2] = (os_raw_file_handle)stderrfd;
|
||||
wasi_args->set_by_user = true;
|
||||
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
|
|
@ -3524,6 +3606,7 @@ wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
|
|||
if (wasi_args) {
|
||||
wasi_args->addr_pool = addr_pool;
|
||||
wasi_args->addr_count = addr_pool_size;
|
||||
wasi_args->set_by_user = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -3537,6 +3620,7 @@ wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
|
|||
if (wasi_args) {
|
||||
wasi_args->ns_lookup_pool = ns_lookup_pool;
|
||||
wasi_args->ns_lookup_count = ns_lookup_pool_size;
|
||||
wasi_args->set_by_user = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -614,6 +614,9 @@ wasm_runtime_get_exec_env_tls(void);
|
|||
|
||||
struct InstantiationArgs2 {
|
||||
InstantiationArgs v1;
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
WASIArguments wasi;
|
||||
#endif
|
||||
};
|
||||
|
||||
void
|
||||
|
|
@ -735,6 +738,43 @@ void
|
|||
wasm_runtime_instantiation_args_set_max_memory_pages(
|
||||
struct InstantiationArgs2 *p, uint32 v);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_arg(struct InstantiationArgs2 *p,
|
||||
char *argv[], int argc);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_env(struct InstantiationArgs2 *p,
|
||||
const char *env[],
|
||||
uint32 env_count);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_dir(struct InstantiationArgs2 *p,
|
||||
const char *dir_list[],
|
||||
uint32 dir_count,
|
||||
const char *map_dir_list[],
|
||||
uint32 map_dir_count);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_stdio(struct InstantiationArgs2 *p,
|
||||
int64 stdinfd, int64 stdoutfd,
|
||||
int64 stderrfd);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_addr_pool(struct InstantiationArgs2 *p,
|
||||
const char *addr_pool[],
|
||||
uint32 addr_pool_size);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_ns_lookup_pool(
|
||||
struct InstantiationArgs2 *p, const char *ns_lookup_pool[],
|
||||
uint32 ns_lookup_pool_size);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon *
|
||||
wasm_runtime_instantiate_ex2(WASMModuleCommon *module,
|
||||
|
|
|
|||
|
|
@ -764,6 +764,38 @@ WASM_RUNTIME_API_EXTERN void
|
|||
wasm_runtime_instantiation_args_set_max_memory_pages(
|
||||
struct InstantiationArgs2 *p, uint32_t v);
|
||||
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_arg(struct InstantiationArgs2 *p,
|
||||
char *argv[], int argc);
|
||||
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_env(struct InstantiationArgs2 *p,
|
||||
const char *env[],
|
||||
uint32_t env_count);
|
||||
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_dir(struct InstantiationArgs2 *p,
|
||||
const char *dir_list[],
|
||||
uint32_t dir_count,
|
||||
const char *map_dir_list[],
|
||||
uint32_t map_dir_count);
|
||||
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_stdio(struct InstantiationArgs2 *p,
|
||||
int64_t stdinfd,
|
||||
int64_t stdoutfd,
|
||||
int64_t stderrfd);
|
||||
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_addr_pool(struct InstantiationArgs2 *p,
|
||||
const char *addr_pool[],
|
||||
uint32_t addr_pool_size);
|
||||
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_instantiation_args_set_wasi_ns_lookup_pool(
|
||||
struct InstantiationArgs2 *p, const char *ns_lookup_pool[],
|
||||
uint32_t ns_lookup_pool_size);
|
||||
|
||||
/**
|
||||
* Instantiate a WASM module, with specified instantiation arguments
|
||||
*
|
||||
|
|
|
|||
|
|
@ -848,6 +848,7 @@ typedef struct WASIArguments {
|
|||
char **argv;
|
||||
uint32 argc;
|
||||
os_raw_file_handle stdio[3];
|
||||
bool set_by_user;
|
||||
} WASIArguments;
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -3276,17 +3276,25 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
|
|||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
/* The sub-instance will get the wasi_ctx from main-instance */
|
||||
if (!is_sub_inst) {
|
||||
const WASIArguments *wasi_args = &args->wasi;
|
||||
if (module->wasi_args.set_by_user) {
|
||||
if (wasi_args->set_by_user) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"WASI configuration was given via both of module "
|
||||
"and InstantiationArgs2");
|
||||
goto fail;
|
||||
}
|
||||
wasi_args = &module->wasi_args;
|
||||
}
|
||||
if (!wasm_runtime_init_wasi(
|
||||
(WASMModuleInstanceCommon *)module_inst,
|
||||
module->wasi_args.dir_list, module->wasi_args.dir_count,
|
||||
module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
|
||||
module->wasi_args.env, module->wasi_args.env_count,
|
||||
module->wasi_args.addr_pool, module->wasi_args.addr_count,
|
||||
module->wasi_args.ns_lookup_pool,
|
||||
module->wasi_args.ns_lookup_count, module->wasi_args.argv,
|
||||
module->wasi_args.argc, module->wasi_args.stdio[0],
|
||||
module->wasi_args.stdio[1], module->wasi_args.stdio[2],
|
||||
error_buf, error_buf_size)) {
|
||||
(WASMModuleInstanceCommon *)module_inst, wasi_args->dir_list,
|
||||
wasi_args->dir_count, wasi_args->map_dir_list,
|
||||
wasi_args->map_dir_count, wasi_args->env, wasi_args->env_count,
|
||||
wasi_args->addr_pool, wasi_args->addr_count,
|
||||
wasi_args->ns_lookup_pool, wasi_args->ns_lookup_count,
|
||||
wasi_args->argv, wasi_args->argc, wasi_args->stdio[0],
|
||||
wasi_args->stdio[1], wasi_args->stdio[2], error_buf,
|
||||
error_buf_size)) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -162,16 +162,18 @@ libc_wasi_parse(char *arg, libc_wasi_parse_context_t *ctx)
|
|||
return LIBC_WASI_PARSE_RESULT_OK;
|
||||
}
|
||||
|
||||
void
|
||||
libc_wasi_init(wasm_module_t wasm_module, int argc, char **argv,
|
||||
libc_wasi_parse_context_t *ctx)
|
||||
static void
|
||||
libc_wasi_set_init_args(struct InstantiationArgs2 *args, int argc, char **argv,
|
||||
libc_wasi_parse_context_t *ctx)
|
||||
{
|
||||
wasm_runtime_set_wasi_args(wasm_module, ctx->dir_list, ctx->dir_list_size,
|
||||
ctx->map_dir_list, ctx->map_dir_list_size,
|
||||
ctx->env_list, ctx->env_list_size, argv, argc);
|
||||
|
||||
wasm_runtime_set_wasi_addr_pool(wasm_module, ctx->addr_pool,
|
||||
ctx->addr_pool_size);
|
||||
wasm_runtime_set_wasi_ns_lookup_pool(wasm_module, ctx->ns_lookup_pool,
|
||||
ctx->ns_lookup_pool_size);
|
||||
wasm_runtime_instantiation_args_set_wasi_arg(args, argv, argc);
|
||||
wasm_runtime_instantiation_args_set_wasi_env(args, ctx->env_list,
|
||||
ctx->env_list_size);
|
||||
wasm_runtime_instantiation_args_set_wasi_dir(
|
||||
args, ctx->dir_list, ctx->dir_list_size, ctx->map_dir_list,
|
||||
ctx->map_dir_list_size);
|
||||
wasm_runtime_instantiation_args_set_wasi_addr_pool(args, ctx->addr_pool,
|
||||
ctx->addr_pool_size);
|
||||
wasm_runtime_instantiation_args_set_wasi_ns_lookup_pool(
|
||||
args, ctx->ns_lookup_pool, ctx->ns_lookup_pool_size);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -962,10 +962,6 @@ main(int argc, char *argv[])
|
|||
}
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
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;
|
||||
|
|
@ -974,6 +970,9 @@ main(int argc, char *argv[])
|
|||
stack_size);
|
||||
wasm_runtime_instantiation_args_set_host_managed_heap_size(inst_args,
|
||||
heap_size);
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
libc_wasi_set_init_args(inst_args, argc, argv, &wasi_parse_ctx);
|
||||
#endif
|
||||
|
||||
/* instantiate the module */
|
||||
wasm_module_inst = wasm_runtime_instantiate_ex2(
|
||||
|
|
|
|||
|
|
@ -380,7 +380,7 @@ iwasm(int argc, char **argv)
|
|||
wasm_runtime_instantiation_args_set_host_managed_heap_size(inst_args,
|
||||
heap_size);
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
|
||||
libc_wasi_set_init_args(wasm_module, argc, argv, &wasi_parse_ctx);
|
||||
#endif
|
||||
|
||||
rt_memset(error_buf, 0x00, sizeof(error_buf));
|
||||
|
|
|
|||
|
|
@ -598,10 +598,6 @@ main(int argc, char *argv[])
|
|||
goto fail2;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
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;
|
||||
|
|
@ -610,6 +606,9 @@ main(int argc, char *argv[])
|
|||
stack_size);
|
||||
wasm_runtime_instantiation_args_set_host_managed_heap_size(inst_args,
|
||||
heap_size);
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
libc_wasi_set_init_args(inst_args, argc, argv, &wasi_parse_ctx);
|
||||
#endif
|
||||
|
||||
/* instantiate the module */
|
||||
wasm_module_inst = wasm_runtime_instantiate_ex2(
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user