Merge dev/socket into main (#1393)
Implement more socket APIs, refer to #1336 and below PRs: - Implement wasi_addr_resolve function (#1319) - Fix socket-api byte order issue when host/network order are the same (#1327) - Enhance sock_addr_local syscall (#1320) - Implement sock_addr_remote syscall (#1360) - Add support for IPv6 in WAMR (#1411) - Implement ns lookup allowlist (#1420) - Implement sock_send_to and sock_recv_from system calls (#1457) - Added http downloader and multicast socket options (#1467) - Fix `bind()` calls to receive the correct size of `sockaddr` structure (#1490) - Assert on correct parameters (#1505) - Copy only received bytes from socket recv buffer into the app buffer (#1497) Co-authored-by: Marcin Kolny <mkolny@amazon.com> Co-authored-by: Marcin Kolny <marcin.kolny@gmail.com> Co-authored-by: Callum Macmillan <callumimacmillan@gmail.com>
This commit is contained in:
@ -2328,12 +2328,8 @@ wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
|
||||
void
|
||||
wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
|
||||
uint32 dir_count, const char *map_dir_list[],
|
||||
uint32 map_dir_count, const char *env_list[],
|
||||
uint32 env_count, char *argv[], int argc,
|
||||
int stdinfd, int stdoutfd, int stderrfd)
|
||||
static WASIArguments *
|
||||
get_wasi_args_from_module(wasm_module_t module)
|
||||
{
|
||||
WASIArguments *wasi_args = NULL;
|
||||
|
||||
@ -2346,6 +2342,18 @@ wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
|
||||
wasi_args = &((AOTModule *)module)->wasi_args;
|
||||
#endif
|
||||
|
||||
return wasi_args;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
|
||||
uint32 dir_count, const char *map_dir_list[],
|
||||
uint32 map_dir_count, const char *env_list[],
|
||||
uint32 env_count, char *argv[], int argc,
|
||||
int stdinfd, int stdoutfd, int stderrfd)
|
||||
{
|
||||
WASIArguments *wasi_args = get_wasi_args_from_module(module);
|
||||
|
||||
if (wasi_args) {
|
||||
wasi_args->dir_list = dir_list;
|
||||
wasi_args->dir_count = dir_count;
|
||||
@ -2376,16 +2384,7 @@ void
|
||||
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
|
||||
uint32 addr_pool_size)
|
||||
{
|
||||
WASIArguments *wasi_args = NULL;
|
||||
|
||||
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
|
||||
if (module->module_type == Wasm_Module_Bytecode)
|
||||
wasi_args = &((WASMModule *)module)->wasi_args;
|
||||
#endif
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module->module_type == Wasm_Module_AoT)
|
||||
wasi_args = &((AOTModule *)module)->wasi_args;
|
||||
#endif
|
||||
WASIArguments *wasi_args = get_wasi_args_from_module(module);
|
||||
|
||||
if (wasi_args) {
|
||||
wasi_args->addr_pool = addr_pool;
|
||||
@ -2393,13 +2392,67 @@ wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
|
||||
const char *ns_lookup_pool[],
|
||||
uint32 ns_lookup_pool_size)
|
||||
{
|
||||
WASIArguments *wasi_args = get_wasi_args_from_module(module);
|
||||
|
||||
if (wasi_args) {
|
||||
wasi_args->ns_lookup_pool = ns_lookup_pool;
|
||||
wasi_args->ns_lookup_count = ns_lookup_pool_size;
|
||||
}
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_UVWASI == 0
|
||||
static bool
|
||||
copy_string_array(const char *array[], uint32 array_size, char **buf_ptr,
|
||||
char ***list_ptr, uint64 *out_buf_size)
|
||||
{
|
||||
uint64 buf_size = 0, total_size;
|
||||
uint32 buf_offset = 0, i;
|
||||
char *buf = NULL, **list = NULL;
|
||||
|
||||
for (i = 0; i < array_size; i++)
|
||||
buf_size += strlen(array[i]) + 1;
|
||||
|
||||
/* We add +1 to generate null-terminated array of strings */
|
||||
total_size = sizeof(char *) * (uint64)array_size + 1;
|
||||
if (total_size >= UINT32_MAX
|
||||
|| (total_size > 0 && !(list = wasm_runtime_malloc((uint32)total_size)))
|
||||
|| buf_size >= UINT32_MAX
|
||||
|| (buf_size > 0 && !(buf = wasm_runtime_malloc((uint32)buf_size)))) {
|
||||
|
||||
if (buf)
|
||||
wasm_runtime_free(buf);
|
||||
if (list)
|
||||
wasm_runtime_free(list);
|
||||
return false;
|
||||
}
|
||||
|
||||
for (i = 0; i < array_size; i++) {
|
||||
list[i] = buf + buf_offset;
|
||||
bh_strcpy_s(buf + buf_offset, (uint32)buf_size - buf_offset, array[i]);
|
||||
buf_offset += (uint32)(strlen(array[i]) + 1);
|
||||
}
|
||||
list[array_size] = NULL;
|
||||
|
||||
*list_ptr = list;
|
||||
*buf_ptr = buf;
|
||||
if (out_buf_size)
|
||||
*out_buf_size = buf_size;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
|
||||
const char *dir_list[], uint32 dir_count,
|
||||
const char *map_dir_list[], uint32 map_dir_count,
|
||||
const char *env[], uint32 env_count,
|
||||
const char *addr_pool[], uint32 addr_pool_size,
|
||||
const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
|
||||
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
|
||||
int stderrfd, char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
@ -2408,8 +2461,9 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
|
||||
char **argv_list = NULL;
|
||||
char *env_buf = NULL;
|
||||
char **env_list = NULL;
|
||||
uint64 argv_buf_size = 0, env_buf_size = 0, total_size;
|
||||
uint32 argv_buf_offset = 0, env_buf_offset = 0;
|
||||
char *ns_lookup_buf = NULL;
|
||||
char **ns_lookup_list = NULL;
|
||||
uint64 argv_buf_size = 0, env_buf_size = 0;
|
||||
struct fd_table *curfds = NULL;
|
||||
struct fd_prestats *prestats = NULL;
|
||||
struct argv_environ_values *argv_environ = NULL;
|
||||
@ -2443,50 +2497,20 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
|
||||
#endif
|
||||
|
||||
/* process argv[0], trip the path and suffix, only keep the program name */
|
||||
for (i = 0; i < argc; i++)
|
||||
argv_buf_size += strlen(argv[i]) + 1;
|
||||
|
||||
total_size = sizeof(char *) * (uint64)argc;
|
||||
if (total_size >= UINT32_MAX
|
||||
|| (total_size > 0
|
||||
&& !(argv_list = wasm_runtime_malloc((uint32)total_size)))
|
||||
|| argv_buf_size >= UINT32_MAX
|
||||
|| (argv_buf_size > 0
|
||||
&& !(argv_buf = wasm_runtime_malloc((uint32)argv_buf_size)))) {
|
||||
if (!copy_string_array((const char **)argv, argc, &argv_buf, &argv_list,
|
||||
&argv_buf_size)) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"Init wasi environment failed: allocate memory failed");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
argv_list[i] = argv_buf + argv_buf_offset;
|
||||
bh_strcpy_s(argv_buf + argv_buf_offset,
|
||||
(uint32)argv_buf_size - argv_buf_offset, argv[i]);
|
||||
argv_buf_offset += (uint32)(strlen(argv[i]) + 1);
|
||||
}
|
||||
|
||||
for (i = 0; i < env_count; i++)
|
||||
env_buf_size += strlen(env[i]) + 1;
|
||||
|
||||
total_size = sizeof(char *) * (uint64)env_count;
|
||||
if (total_size >= UINT32_MAX
|
||||
|| (total_size > 0
|
||||
&& !(env_list = wasm_runtime_malloc((uint32)total_size)))
|
||||
|| env_buf_size >= UINT32_MAX
|
||||
|| (env_buf_size > 0
|
||||
&& !(env_buf = wasm_runtime_malloc((uint32)env_buf_size)))) {
|
||||
if (!copy_string_array(env, env_count, &env_buf, &env_list,
|
||||
&env_buf_size)) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"Init wasi environment failed: allocate memory failed");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
for (i = 0; i < env_count; i++) {
|
||||
env_list[i] = env_buf + env_buf_offset;
|
||||
bh_strcpy_s(env_buf + env_buf_offset,
|
||||
(uint32)env_buf_size - env_buf_offset, env[i]);
|
||||
env_buf_offset += (uint32)(strlen(env[i]) + 1);
|
||||
}
|
||||
|
||||
if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
|
||||
|| !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
|
||||
|| !(argv_environ =
|
||||
@ -2588,6 +2612,13 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
|
||||
}
|
||||
}
|
||||
|
||||
if (!copy_string_array(ns_lookup_pool, ns_lookup_pool_size, &ns_lookup_buf,
|
||||
&ns_lookup_list, NULL)) {
|
||||
set_error_buf(error_buf, error_buf_size,
|
||||
"Init wasi environment failed: allocate memory failed");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
wasi_ctx->curfds = curfds;
|
||||
wasi_ctx->prestats = prestats;
|
||||
wasi_ctx->argv_environ = argv_environ;
|
||||
@ -2596,6 +2627,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
|
||||
wasi_ctx->argv_list = argv_list;
|
||||
wasi_ctx->env_buf = env_buf;
|
||||
wasi_ctx->env_list = env_list;
|
||||
wasi_ctx->ns_lookup_buf = ns_lookup_buf;
|
||||
wasi_ctx->ns_lookup_list = ns_lookup_list;
|
||||
|
||||
return true;
|
||||
|
||||
@ -2624,6 +2657,10 @@ fail:
|
||||
wasm_runtime_free(env_buf);
|
||||
if (env_list)
|
||||
wasm_runtime_free(env_list);
|
||||
if (ns_lookup_buf)
|
||||
wasm_runtime_free(ns_lookup_buf);
|
||||
if (ns_lookup_list)
|
||||
wasm_runtime_free(ns_lookup_list);
|
||||
return false;
|
||||
}
|
||||
#else /* else of WASM_ENABLE_UVWASI == 0 */
|
||||
@ -2675,6 +2712,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
|
||||
const char *map_dir_list[], uint32 map_dir_count,
|
||||
const char *env[], uint32 env_count,
|
||||
const char *addr_pool[], uint32 addr_pool_size,
|
||||
const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
|
||||
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
|
||||
int stderrfd, char *error_buf, uint32 error_buf_size)
|
||||
{
|
||||
@ -2851,6 +2889,11 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
|
||||
wasm_runtime_free(wasi_ctx->env_buf);
|
||||
if (wasi_ctx->env_list)
|
||||
wasm_runtime_free(wasi_ctx->env_list);
|
||||
if (wasi_ctx->ns_lookup_buf)
|
||||
wasm_runtime_free(wasi_ctx->ns_lookup_buf);
|
||||
if (wasi_ctx->ns_lookup_list)
|
||||
wasm_runtime_free(wasi_ctx->ns_lookup_list);
|
||||
|
||||
wasm_runtime_free(wasi_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user