Skip to content

Commit 9c87a1e

Browse files
authored
Implement part of Berkeley Socket API for libc-wasi (bytecodealliance#1036)
Refer to [Networking API design](WebAssembly/WASI#370) and [feat(socket): berkeley socket API v2](WebAssembly/WASI#459): - Support the socket API of synchronous mode, including `socket/bind/listen/accept/send/recv/close/shutdown`, the asynchronous mode isn't supported yet. - Support adding `--addr-pool=<pool1,pool2,..>` argument for command line to identify the valid ip address range - Add socket-api sample and update the document
1 parent 0065743 commit 9c87a1e

File tree

28 files changed

+2204
-207
lines changed

28 files changed

+2204
-207
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ iwasm VM core
4242
- [Source debugging support](./doc/source_debugging.md), ref to [document](./doc/source_debugging.md)
4343
- [WAMR-IDE (Experimental)](./test-tools/wamr-ide) to develop WebAssembly applications with build, run and debug support, ref to [document](./test-tools/wamr-ide)
4444
- [XIP (Execution In Place) support](./doc/xip.md), ref to [document](./doc/xip.md)
45+
- [Berkeley/Posix Socket support](./doc/socket_api.md), ref to [document](./doc/socket_api.md) and [sample](./samples/socket-api)
4546

4647
### WASM post-MVP features
4748
- [wasm-c-api](https://github.com/WebAssembly/wasm-c-api), ref to [document](doc/wasm_c_api.md) and [sample](samples/wasm-c-api)
@@ -153,6 +154,7 @@ The WAMR [samples](./samples) integrate the iwasm VM core, application manager a
153154
- **[multi-module](./samples/multi-module)**: Demonstrating the [multiple modules as dependencies](./doc/multi_module.md) feature which implements the [load-time dynamic linking](https://webassembly.org/docs/dynamic-linking/).
154155
- **[ref-types](./samples/ref-types)**: Demonstrating how to call wasm functions with argument of externref type introduced by [reference types proposal](https://github.com/WebAssembly/reference-types).
155156
- **[wasm-c-api](./samples/wasm-c-api/README.md)**: Demonstrating how to run some samples from [wasm-c-api proposal](https://github.com/WebAssembly/wasm-c-api) and showing the supported API's.
157+
- **[socket-api](./samples/socket-api/README.md)**: Demonstrating how to run wasm tcp server and tcp client applications, and how they communicate with each other.
156158
- **[workload](./samples/workload/README.md)**: Demonstrating how to build and run some complex workloads, e.g. tensorflow-lite, XNNPACK, wasm-av1, meshoptimizer and bwa.
157159

158160

core/iwasm/aot/aot_runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
10201020
module->wasi_args.dir_list, module->wasi_args.dir_count,
10211021
module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
10221022
module->wasi_args.env, module->wasi_args.env_count,
1023+
module->wasi_args.addr_pool, module->wasi_args.addr_count,
10231024
module->wasi_args.argv, module->wasi_args.argc,
10241025
module->wasi_args.stdio[0], module->wasi_args.stdio[1],
10251026
module->wasi_args.stdio[2], error_buf, error_buf_size))

core/iwasm/common/wasm_runtime_common.c

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,14 +2191,36 @@ wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
21912191
argc, -1, -1, -1);
21922192
}
21932193

2194+
void
2195+
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
2196+
uint32 addr_pool_size)
2197+
{
2198+
WASIArguments *wasi_args = NULL;
2199+
2200+
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
2201+
if (module->module_type == Wasm_Module_Bytecode)
2202+
wasi_args = &((WASMModule *)module)->wasi_args;
2203+
#endif
2204+
#if WASM_ENABLE_AOT != 0
2205+
if (module->module_type == Wasm_Module_AoT)
2206+
wasi_args = &((AOTModule *)module)->wasi_args;
2207+
#endif
2208+
2209+
if (wasi_args) {
2210+
wasi_args->addr_pool = addr_pool;
2211+
wasi_args->addr_count = addr_pool_size;
2212+
}
2213+
}
2214+
21942215
#if WASM_ENABLE_UVWASI == 0
21952216
bool
21962217
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
21972218
const char *dir_list[], uint32 dir_count,
21982219
const char *map_dir_list[], uint32 map_dir_count,
2199-
const char *env[], uint32 env_count, char *argv[],
2200-
uint32 argc, int stdinfd, int stdoutfd, int stderrfd,
2201-
char *error_buf, uint32 error_buf_size)
2220+
const char *env[], uint32 env_count,
2221+
const char *addr_pool[], uint32 addr_pool_size,
2222+
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
2223+
int stderrfd, char *error_buf, uint32 error_buf_size)
22022224
{
22032225
WASIContext *wasi_ctx;
22042226
char *argv_buf = NULL;
@@ -2210,8 +2232,10 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
22102232
struct fd_table *curfds = NULL;
22112233
struct fd_prestats *prestats = NULL;
22122234
struct argv_environ_values *argv_environ = NULL;
2235+
struct addr_pool *apool = NULL;
22132236
bool fd_table_inited = false, fd_prestats_inited = false;
22142237
bool argv_environ_inited = false;
2238+
bool addr_pool_inited = false;
22152239
__wasi_fd_t wasm_fd = 3;
22162240
int32 raw_fd;
22172241
char *path, resolved_path[PATH_MAX];
@@ -2285,7 +2309,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
22852309
if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
22862310
|| !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
22872311
|| !(argv_environ =
2288-
wasm_runtime_malloc(sizeof(struct argv_environ_values)))) {
2312+
wasm_runtime_malloc(sizeof(struct argv_environ_values)))
2313+
|| !(apool = wasm_runtime_malloc(sizeof(struct addr_pool)))) {
22892314
set_error_buf(error_buf, error_buf_size,
22902315
"Init wasi environment failed: allocate memory failed");
22912316
goto fail;
@@ -2316,6 +2341,14 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
23162341
}
23172342
argv_environ_inited = true;
23182343

2344+
if (!addr_pool_init(apool)) {
2345+
set_error_buf(error_buf, error_buf_size,
2346+
"Init wasi environment failed: "
2347+
"init the address pool failed");
2348+
goto fail;
2349+
}
2350+
addr_pool_inited = true;
2351+
23192352
/* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
23202353
if (!fd_table_insert_existing(curfds, 0, (stdinfd != -1) ? stdinfd : 0)
23212354
|| !fd_table_insert_existing(curfds, 1, (stdoutfd != -1) ? stdoutfd : 1)
@@ -2350,9 +2383,34 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
23502383
fd_prestats_insert(prestats, dir_list[i], wasm_fd);
23512384
}
23522385

2386+
/* addr_pool(textual) -> apool */
2387+
for (i = 0; i < addr_pool_size; i++) {
2388+
char *cp, *address, *mask;
2389+
bool ret = false;
2390+
2391+
cp = bh_strdup(addr_pool[i]);
2392+
if (!cp) {
2393+
set_error_buf(error_buf, error_buf_size,
2394+
"Init wasi environment failed: copy address failed");
2395+
goto fail;
2396+
}
2397+
2398+
address = strtok(cp, "/");
2399+
mask = strtok(NULL, "/");
2400+
2401+
ret = addr_pool_insert(apool, address, (uint8)(atoi(mask)));
2402+
wasm_runtime_free(cp);
2403+
if (!ret) {
2404+
set_error_buf(error_buf, error_buf_size,
2405+
"Init wasi environment failed: store address failed");
2406+
goto fail;
2407+
}
2408+
}
2409+
23532410
wasi_ctx->curfds = curfds;
23542411
wasi_ctx->prestats = prestats;
23552412
wasi_ctx->argv_environ = argv_environ;
2413+
wasi_ctx->addr_pool = apool;
23562414
wasi_ctx->argv_buf = argv_buf;
23572415
wasi_ctx->argv_list = argv_list;
23582416
wasi_ctx->env_buf = env_buf;
@@ -2367,12 +2425,16 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
23672425
fd_prestats_destroy(prestats);
23682426
if (fd_table_inited)
23692427
fd_table_destroy(curfds);
2428+
if (addr_pool_inited)
2429+
addr_pool_destroy(apool);
23702430
if (curfds)
23712431
wasm_runtime_free(curfds);
23722432
if (prestats)
23732433
wasm_runtime_free(prestats);
23742434
if (argv_environ)
23752435
wasm_runtime_free(argv_environ);
2436+
if (apool)
2437+
wasm_runtime_free(apool);
23762438
if (argv_buf)
23772439
wasm_runtime_free(argv_buf);
23782440
if (argv_list)
@@ -2430,9 +2492,10 @@ bool
24302492
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
24312493
const char *dir_list[], uint32 dir_count,
24322494
const char *map_dir_list[], uint32 map_dir_count,
2433-
const char *env[], uint32 env_count, char *argv[],
2434-
uint32 argc, int stdinfd, int stdoutfd, int stderrfd,
2435-
char *error_buf, uint32 error_buf_size)
2495+
const char *env[], uint32 env_count,
2496+
const char *addr_pool[], uint32 addr_pool_size,
2497+
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
2498+
int stderrfd, char *error_buf, uint32 error_buf_size)
24362499
{
24372500
uvwasi_t *uvwasi = NULL;
24382501
uvwasi_options_t init_options;
@@ -2595,6 +2658,10 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
25952658
fd_prestats_destroy(wasi_ctx->prestats);
25962659
wasm_runtime_free(wasi_ctx->prestats);
25972660
}
2661+
if (wasi_ctx->addr_pool) {
2662+
addr_pool_destroy(wasi_ctx->addr_pool);
2663+
wasm_runtime_free(wasi_ctx->addr_pool);
2664+
}
25982665
if (wasi_ctx->argv_buf)
25992666
wasm_runtime_free(wasi_ctx->argv_buf);
26002667
if (wasi_ctx->argv_list)

core/iwasm/common/wasm_runtime_common.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ typedef struct WASIContext {
352352
struct fd_table *curfds;
353353
struct fd_prestats *prestats;
354354
struct argv_environ_values *argv_environ;
355+
struct addr_pool *addr_pool;
355356
char *argv_buf;
356357
char **argv_list;
357358
char *env_buf;
@@ -712,9 +713,10 @@ bool
712713
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
713714
const char *dir_list[], uint32 dir_count,
714715
const char *map_dir_list[], uint32 map_dir_count,
715-
const char *env[], uint32 env_count, char *argv[],
716-
uint32 argc, int stdinfd, int stdoutfd, int stderrfd,
717-
char *error_buf, uint32 error_buf_size);
716+
const char *env[], uint32 env_count,
717+
const char *addr_pool[], uint32 addr_pool_size,
718+
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
719+
int stderrfd, char *error_buf, uint32 error_buf_size);
718720

719721
void
720722
wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
@@ -726,6 +728,9 @@ wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
726728
WASIContext *
727729
wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
728730

731+
WASM_RUNTIME_API_EXTERN void
732+
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
733+
uint32 addr_pool_size);
729734
#endif /* end of WASM_ENABLE_LIBC_WASI */
730735

731736
#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
@@ -348,6 +348,10 @@ wasm_runtime_set_wasi_args(wasm_module_t module,
348348
const char *env[], uint32_t env_count,
349349
char *argv[], int argc);
350350

351+
WASM_RUNTIME_API_EXTERN void
352+
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
353+
uint32_t addr_pool_size);
354+
351355
/**
352356
* Instantiate a WASM module.
353357
*

core/iwasm/interpreter/wasm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ typedef struct WASIArguments {
305305
uint32 map_dir_count;
306306
const char **env;
307307
uint32 env_count;
308+
/* in CIDR noation */
309+
const char **addr_pool;
310+
uint32 addr_count;
308311
char **argv;
309312
uint32 argc;
310313
int stdio[3];

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
15221522
module->wasi_args.dir_list, module->wasi_args.dir_count,
15231523
module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
15241524
module->wasi_args.env, module->wasi_args.env_count,
1525+
module->wasi_args.addr_pool, module->wasi_args.addr_count,
15251526
module->wasi_args.argv, module->wasi_args.argc,
15261527
module->wasi_args.stdio[0], module->wasi_args.stdio[1],
15271528
module->wasi_args.stdio[2], error_buf, error_buf_size)) {

0 commit comments

Comments
 (0)