Skip to content

Commit 78b5c5b

Browse files
wenyonghloganekcimacmillan
authored
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 <[email protected]> Co-authored-by: Marcin Kolny <[email protected]> Co-authored-by: Callum Macmillan <[email protected]>
1 parent 32d2d16 commit 78b5c5b

File tree

31 files changed

+6380
-473
lines changed

31 files changed

+6380
-473
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,9 +1061,11 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
10611061
module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
10621062
module->wasi_args.env, module->wasi_args.env_count,
10631063
module->wasi_args.addr_pool, module->wasi_args.addr_count,
1064-
module->wasi_args.argv, module->wasi_args.argc,
1065-
module->wasi_args.stdio[0], module->wasi_args.stdio[1],
1066-
module->wasi_args.stdio[2], error_buf, error_buf_size))
1064+
module->wasi_args.ns_lookup_pool,
1065+
module->wasi_args.ns_lookup_count, module->wasi_args.argv,
1066+
module->wasi_args.argc, module->wasi_args.stdio[0],
1067+
module->wasi_args.stdio[1], module->wasi_args.stdio[2],
1068+
error_buf, error_buf_size))
10671069
goto fail;
10681070
}
10691071
#endif

core/iwasm/common/wasm_runtime_common.c

Lines changed: 95 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,12 +2328,8 @@ wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
23282328

23292329
#if WASM_ENABLE_LIBC_WASI != 0
23302330

2331-
void
2332-
wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
2333-
uint32 dir_count, const char *map_dir_list[],
2334-
uint32 map_dir_count, const char *env_list[],
2335-
uint32 env_count, char *argv[], int argc,
2336-
int stdinfd, int stdoutfd, int stderrfd)
2331+
static WASIArguments *
2332+
get_wasi_args_from_module(wasm_module_t module)
23372333
{
23382334
WASIArguments *wasi_args = NULL;
23392335

@@ -2346,6 +2342,18 @@ wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
23462342
wasi_args = &((AOTModule *)module)->wasi_args;
23472343
#endif
23482344

2345+
return wasi_args;
2346+
}
2347+
2348+
void
2349+
wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
2350+
uint32 dir_count, const char *map_dir_list[],
2351+
uint32 map_dir_count, const char *env_list[],
2352+
uint32 env_count, char *argv[], int argc,
2353+
int stdinfd, int stdoutfd, int stderrfd)
2354+
{
2355+
WASIArguments *wasi_args = get_wasi_args_from_module(module);
2356+
23492357
if (wasi_args) {
23502358
wasi_args->dir_list = dir_list;
23512359
wasi_args->dir_count = dir_count;
@@ -2376,30 +2384,75 @@ void
23762384
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
23772385
uint32 addr_pool_size)
23782386
{
2379-
WASIArguments *wasi_args = NULL;
2380-
2381-
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
2382-
if (module->module_type == Wasm_Module_Bytecode)
2383-
wasi_args = &((WASMModule *)module)->wasi_args;
2384-
#endif
2385-
#if WASM_ENABLE_AOT != 0
2386-
if (module->module_type == Wasm_Module_AoT)
2387-
wasi_args = &((AOTModule *)module)->wasi_args;
2388-
#endif
2387+
WASIArguments *wasi_args = get_wasi_args_from_module(module);
23892388

23902389
if (wasi_args) {
23912390
wasi_args->addr_pool = addr_pool;
23922391
wasi_args->addr_count = addr_pool_size;
23932392
}
23942393
}
23952394

2395+
void
2396+
wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
2397+
const char *ns_lookup_pool[],
2398+
uint32 ns_lookup_pool_size)
2399+
{
2400+
WASIArguments *wasi_args = get_wasi_args_from_module(module);
2401+
2402+
if (wasi_args) {
2403+
wasi_args->ns_lookup_pool = ns_lookup_pool;
2404+
wasi_args->ns_lookup_count = ns_lookup_pool_size;
2405+
}
2406+
}
2407+
23962408
#if WASM_ENABLE_UVWASI == 0
2409+
static bool
2410+
copy_string_array(const char *array[], uint32 array_size, char **buf_ptr,
2411+
char ***list_ptr, uint64 *out_buf_size)
2412+
{
2413+
uint64 buf_size = 0, total_size;
2414+
uint32 buf_offset = 0, i;
2415+
char *buf = NULL, **list = NULL;
2416+
2417+
for (i = 0; i < array_size; i++)
2418+
buf_size += strlen(array[i]) + 1;
2419+
2420+
/* We add +1 to generate null-terminated array of strings */
2421+
total_size = sizeof(char *) * (uint64)array_size + 1;
2422+
if (total_size >= UINT32_MAX
2423+
|| (total_size > 0 && !(list = wasm_runtime_malloc((uint32)total_size)))
2424+
|| buf_size >= UINT32_MAX
2425+
|| (buf_size > 0 && !(buf = wasm_runtime_malloc((uint32)buf_size)))) {
2426+
2427+
if (buf)
2428+
wasm_runtime_free(buf);
2429+
if (list)
2430+
wasm_runtime_free(list);
2431+
return false;
2432+
}
2433+
2434+
for (i = 0; i < array_size; i++) {
2435+
list[i] = buf + buf_offset;
2436+
bh_strcpy_s(buf + buf_offset, (uint32)buf_size - buf_offset, array[i]);
2437+
buf_offset += (uint32)(strlen(array[i]) + 1);
2438+
}
2439+
list[array_size] = NULL;
2440+
2441+
*list_ptr = list;
2442+
*buf_ptr = buf;
2443+
if (out_buf_size)
2444+
*out_buf_size = buf_size;
2445+
2446+
return true;
2447+
}
2448+
23972449
bool
23982450
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
23992451
const char *dir_list[], uint32 dir_count,
24002452
const char *map_dir_list[], uint32 map_dir_count,
24012453
const char *env[], uint32 env_count,
24022454
const char *addr_pool[], uint32 addr_pool_size,
2455+
const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
24032456
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
24042457
int stderrfd, char *error_buf, uint32 error_buf_size)
24052458
{
@@ -2408,8 +2461,9 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
24082461
char **argv_list = NULL;
24092462
char *env_buf = NULL;
24102463
char **env_list = NULL;
2411-
uint64 argv_buf_size = 0, env_buf_size = 0, total_size;
2412-
uint32 argv_buf_offset = 0, env_buf_offset = 0;
2464+
char *ns_lookup_buf = NULL;
2465+
char **ns_lookup_list = NULL;
2466+
uint64 argv_buf_size = 0, env_buf_size = 0;
24132467
struct fd_table *curfds = NULL;
24142468
struct fd_prestats *prestats = NULL;
24152469
struct argv_environ_values *argv_environ = NULL;
@@ -2443,50 +2497,20 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
24432497
#endif
24442498

24452499
/* process argv[0], trip the path and suffix, only keep the program name */
2446-
for (i = 0; i < argc; i++)
2447-
argv_buf_size += strlen(argv[i]) + 1;
2448-
2449-
total_size = sizeof(char *) * (uint64)argc;
2450-
if (total_size >= UINT32_MAX
2451-
|| (total_size > 0
2452-
&& !(argv_list = wasm_runtime_malloc((uint32)total_size)))
2453-
|| argv_buf_size >= UINT32_MAX
2454-
|| (argv_buf_size > 0
2455-
&& !(argv_buf = wasm_runtime_malloc((uint32)argv_buf_size)))) {
2500+
if (!copy_string_array((const char **)argv, argc, &argv_buf, &argv_list,
2501+
&argv_buf_size)) {
24562502
set_error_buf(error_buf, error_buf_size,
24572503
"Init wasi environment failed: allocate memory failed");
24582504
goto fail;
24592505
}
24602506

2461-
for (i = 0; i < argc; i++) {
2462-
argv_list[i] = argv_buf + argv_buf_offset;
2463-
bh_strcpy_s(argv_buf + argv_buf_offset,
2464-
(uint32)argv_buf_size - argv_buf_offset, argv[i]);
2465-
argv_buf_offset += (uint32)(strlen(argv[i]) + 1);
2466-
}
2467-
2468-
for (i = 0; i < env_count; i++)
2469-
env_buf_size += strlen(env[i]) + 1;
2470-
2471-
total_size = sizeof(char *) * (uint64)env_count;
2472-
if (total_size >= UINT32_MAX
2473-
|| (total_size > 0
2474-
&& !(env_list = wasm_runtime_malloc((uint32)total_size)))
2475-
|| env_buf_size >= UINT32_MAX
2476-
|| (env_buf_size > 0
2477-
&& !(env_buf = wasm_runtime_malloc((uint32)env_buf_size)))) {
2507+
if (!copy_string_array(env, env_count, &env_buf, &env_list,
2508+
&env_buf_size)) {
24782509
set_error_buf(error_buf, error_buf_size,
24792510
"Init wasi environment failed: allocate memory failed");
24802511
goto fail;
24812512
}
24822513

2483-
for (i = 0; i < env_count; i++) {
2484-
env_list[i] = env_buf + env_buf_offset;
2485-
bh_strcpy_s(env_buf + env_buf_offset,
2486-
(uint32)env_buf_size - env_buf_offset, env[i]);
2487-
env_buf_offset += (uint32)(strlen(env[i]) + 1);
2488-
}
2489-
24902514
if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
24912515
|| !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
24922516
|| !(argv_environ =
@@ -2588,6 +2612,13 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
25882612
}
25892613
}
25902614

2615+
if (!copy_string_array(ns_lookup_pool, ns_lookup_pool_size, &ns_lookup_buf,
2616+
&ns_lookup_list, NULL)) {
2617+
set_error_buf(error_buf, error_buf_size,
2618+
"Init wasi environment failed: allocate memory failed");
2619+
goto fail;
2620+
}
2621+
25912622
wasi_ctx->curfds = curfds;
25922623
wasi_ctx->prestats = prestats;
25932624
wasi_ctx->argv_environ = argv_environ;
@@ -2596,6 +2627,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
25962627
wasi_ctx->argv_list = argv_list;
25972628
wasi_ctx->env_buf = env_buf;
25982629
wasi_ctx->env_list = env_list;
2630+
wasi_ctx->ns_lookup_buf = ns_lookup_buf;
2631+
wasi_ctx->ns_lookup_list = ns_lookup_list;
25992632

26002633
return true;
26012634

@@ -2624,6 +2657,10 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
26242657
wasm_runtime_free(env_buf);
26252658
if (env_list)
26262659
wasm_runtime_free(env_list);
2660+
if (ns_lookup_buf)
2661+
wasm_runtime_free(ns_lookup_buf);
2662+
if (ns_lookup_list)
2663+
wasm_runtime_free(ns_lookup_list);
26272664
return false;
26282665
}
26292666
#else /* else of WASM_ENABLE_UVWASI == 0 */
@@ -2675,6 +2712,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
26752712
const char *map_dir_list[], uint32 map_dir_count,
26762713
const char *env[], uint32 env_count,
26772714
const char *addr_pool[], uint32 addr_pool_size,
2715+
const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
26782716
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
26792717
int stderrfd, char *error_buf, uint32 error_buf_size)
26802718
{
@@ -2851,6 +2889,11 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
28512889
wasm_runtime_free(wasi_ctx->env_buf);
28522890
if (wasi_ctx->env_list)
28532891
wasm_runtime_free(wasi_ctx->env_list);
2892+
if (wasi_ctx->ns_lookup_buf)
2893+
wasm_runtime_free(wasi_ctx->ns_lookup_buf);
2894+
if (wasi_ctx->ns_lookup_list)
2895+
wasm_runtime_free(wasi_ctx->ns_lookup_list);
2896+
28542897
wasm_runtime_free(wasi_ctx);
28552898
}
28562899
}

core/iwasm/common/wasm_runtime_common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ typedef struct WASIContext {
363363
struct fd_prestats *prestats;
364364
struct argv_environ_values *argv_environ;
365365
struct addr_pool *addr_pool;
366+
char *ns_lookup_buf;
367+
char **ns_lookup_list;
366368
char *argv_buf;
367369
char **argv_list;
368370
char *env_buf;
@@ -775,6 +777,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
775777
const char *map_dir_list[], uint32 map_dir_count,
776778
const char *env[], uint32 env_count,
777779
const char *addr_pool[], uint32 addr_pool_size,
780+
const char *ns_lookup_pool[], uint32 ns_lookup_pool_size,
778781
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
779782
int stderrfd, char *error_buf, uint32 error_buf_size);
780783

@@ -791,6 +794,11 @@ wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
791794
WASM_RUNTIME_API_EXTERN void
792795
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
793796
uint32 addr_pool_size);
797+
798+
WASM_RUNTIME_API_EXTERN void
799+
wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module,
800+
const char *ns_lookup_pool[],
801+
uint32 ns_lookup_pool_size);
794802
#endif /* end of WASM_ENABLE_LIBC_WASI */
795803

796804
#if WASM_ENABLE_REF_TYPES != 0

core/iwasm/include/wasm_export.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ WASM_RUNTIME_API_EXTERN void
376376
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
377377
uint32_t addr_pool_size);
378378

379+
WASM_RUNTIME_API_EXTERN void
380+
wasm_runtime_set_wasi_ns_lookup_pool(wasm_module_t module, const char *ns_lookup_pool[],
381+
uint32_t ns_lookup_pool_size);
382+
379383
/**
380384
* Instantiate a WASM module.
381385
*

core/iwasm/interpreter/wasm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ typedef struct WASIArguments {
321321
/* in CIDR noation */
322322
const char **addr_pool;
323323
uint32 addr_count;
324+
const char **ns_lookup_pool;
325+
uint32 ns_lookup_count;
324326
char **argv;
325327
uint32 argc;
326328
int stdio[3];

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,9 +1701,11 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
17011701
module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
17021702
module->wasi_args.env, module->wasi_args.env_count,
17031703
module->wasi_args.addr_pool, module->wasi_args.addr_count,
1704-
module->wasi_args.argv, module->wasi_args.argc,
1705-
module->wasi_args.stdio[0], module->wasi_args.stdio[1],
1706-
module->wasi_args.stdio[2], error_buf, error_buf_size)) {
1704+
module->wasi_args.ns_lookup_pool,
1705+
module->wasi_args.ns_lookup_count, module->wasi_args.argv,
1706+
module->wasi_args.argc, module->wasi_args.stdio[0],
1707+
module->wasi_args.stdio[1], module->wasi_args.stdio[2],
1708+
error_buf, error_buf_size)) {
17071709
goto fail;
17081710
}
17091711
}

core/iwasm/libraries/debug-engine/gdbserver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ wasm_create_gdbserver(const char *host, int32 *port)
5959

6060
memset(server->receive_ctx, 0, sizeof(rsp_recv_context_t));
6161

62-
if (0 != os_socket_create(&listen_fd, 1)) {
62+
if (0 != os_socket_create(&listen_fd, true, true)) {
6363
LOG_ERROR("wasm gdb server error: create socket failed");
6464
goto fail;
6565
}

0 commit comments

Comments
 (0)