Skip to content

Commit d4fc644

Browse files
authored
Partly Implement Berkeley Socket API for libc-wasi (#836)
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. And add the sample.
1 parent 5bdab7f commit d4fc644

File tree

25 files changed

+1964
-207
lines changed

25 files changed

+1964
-207
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
10181018
module->wasi_args.dir_list, module->wasi_args.dir_count,
10191019
module->wasi_args.map_dir_list, module->wasi_args.map_dir_count,
10201020
module->wasi_args.env, module->wasi_args.env_count,
1021+
module->wasi_args.addr_pool, module->wasi_args.addr_count,
10211022
module->wasi_args.argv, module->wasi_args.argc,
10221023
module->wasi_args.stdio[0], module->wasi_args.stdio[1],
10231024
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
@@ -1969,14 +1969,36 @@ wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
19691969
argc, -1, -1, -1);
19701970
}
19711971

1972+
void
1973+
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
1974+
uint32 addr_pool_size)
1975+
{
1976+
WASIArguments *wasi_args = NULL;
1977+
1978+
#if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
1979+
if (module->module_type == Wasm_Module_Bytecode)
1980+
wasi_args = &((WASMModule *)module)->wasi_args;
1981+
#endif
1982+
#if WASM_ENABLE_AOT != 0
1983+
if (module->module_type == Wasm_Module_AoT)
1984+
wasi_args = &((AOTModule *)module)->wasi_args;
1985+
#endif
1986+
1987+
if (wasi_args) {
1988+
wasi_args->addr_pool = addr_pool;
1989+
wasi_args->addr_count = addr_pool_size;
1990+
}
1991+
}
1992+
19721993
#if WASM_ENABLE_UVWASI == 0
19731994
bool
19741995
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
19751996
const char *dir_list[], uint32 dir_count,
19761997
const char *map_dir_list[], uint32 map_dir_count,
1977-
const char *env[], uint32 env_count, char *argv[],
1978-
uint32 argc, int stdinfd, int stdoutfd, int stderrfd,
1979-
char *error_buf, uint32 error_buf_size)
1998+
const char *env[], uint32 env_count,
1999+
const char *addr_pool[], uint32 addr_pool_size,
2000+
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
2001+
int stderrfd, char *error_buf, uint32 error_buf_size)
19802002
{
19812003
WASIContext *wasi_ctx;
19822004
char *argv_buf = NULL;
@@ -1988,8 +2010,10 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
19882010
struct fd_table *curfds = NULL;
19892011
struct fd_prestats *prestats = NULL;
19902012
struct argv_environ_values *argv_environ = NULL;
2013+
struct addr_pool *apool = NULL;
19912014
bool fd_table_inited = false, fd_prestats_inited = false;
19922015
bool argv_environ_inited = false;
2016+
bool addr_pool_inited = false;
19932017
__wasi_fd_t wasm_fd = 3;
19942018
int32 raw_fd;
19952019
char *path, resolved_path[PATH_MAX];
@@ -2063,7 +2087,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
20632087
if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
20642088
|| !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
20652089
|| !(argv_environ =
2066-
wasm_runtime_malloc(sizeof(struct argv_environ_values)))) {
2090+
wasm_runtime_malloc(sizeof(struct argv_environ_values)))
2091+
|| !(apool = wasm_runtime_malloc(sizeof(struct addr_pool)))) {
20672092
set_error_buf(error_buf, error_buf_size,
20682093
"Init wasi environment failed: allocate memory failed");
20692094
goto fail;
@@ -2094,6 +2119,14 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
20942119
}
20952120
argv_environ_inited = true;
20962121

2122+
if (!addr_pool_init(apool)) {
2123+
set_error_buf(error_buf, error_buf_size,
2124+
"Init wasi environment failed: "
2125+
"init the address pool failed");
2126+
goto fail;
2127+
}
2128+
addr_pool_inited = true;
2129+
20972130
/* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
20982131
if (!fd_table_insert_existing(curfds, 0, (stdinfd != -1) ? stdinfd : 0)
20992132
|| !fd_table_insert_existing(curfds, 1, (stdoutfd != -1) ? stdoutfd : 1)
@@ -2128,9 +2161,34 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
21282161
fd_prestats_insert(prestats, dir_list[i], wasm_fd);
21292162
}
21302163

2164+
/* addr_pool(textual) -> apool */
2165+
for (i = 0; i < addr_pool_size; i++) {
2166+
char *cp, *address, *mask;
2167+
bool ret = false;
2168+
2169+
cp = bh_strdup(addr_pool[i]);
2170+
if (!cp) {
2171+
set_error_buf(error_buf, error_buf_size,
2172+
"Init wasi environment failed: copy address failed");
2173+
goto fail;
2174+
}
2175+
2176+
address = strtok(cp, "/");
2177+
mask = strtok(NULL, "/");
2178+
2179+
ret = addr_pool_insert(apool, address, (uint8)(atoi(mask)));
2180+
wasm_runtime_free(cp);
2181+
if (!ret) {
2182+
set_error_buf(error_buf, error_buf_size,
2183+
"Init wasi environment failed: store address failed");
2184+
goto fail;
2185+
}
2186+
}
2187+
21312188
wasi_ctx->curfds = curfds;
21322189
wasi_ctx->prestats = prestats;
21332190
wasi_ctx->argv_environ = argv_environ;
2191+
wasi_ctx->addr_pool = apool;
21342192
wasi_ctx->argv_buf = argv_buf;
21352193
wasi_ctx->argv_list = argv_list;
21362194
wasi_ctx->env_buf = env_buf;
@@ -2145,12 +2203,16 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
21452203
fd_prestats_destroy(prestats);
21462204
if (fd_table_inited)
21472205
fd_table_destroy(curfds);
2206+
if (addr_pool_inited)
2207+
addr_pool_destroy(apool);
21482208
if (curfds)
21492209
wasm_runtime_free(curfds);
21502210
if (prestats)
21512211
wasm_runtime_free(prestats);
21522212
if (argv_environ)
21532213
wasm_runtime_free(argv_environ);
2214+
if (apool)
2215+
wasm_runtime_free(apool);
21542216
if (argv_buf)
21552217
wasm_runtime_free(argv_buf);
21562218
if (argv_list)
@@ -2208,9 +2270,10 @@ bool
22082270
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
22092271
const char *dir_list[], uint32 dir_count,
22102272
const char *map_dir_list[], uint32 map_dir_count,
2211-
const char *env[], uint32 env_count, char *argv[],
2212-
uint32 argc, int stdinfd, int stdoutfd, int stderrfd,
2213-
char *error_buf, uint32 error_buf_size)
2273+
const char *env[], uint32 env_count,
2274+
const char *addr_pool[], uint32 addr_pool_size,
2275+
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
2276+
int stderrfd, char *error_buf, uint32 error_buf_size)
22142277
{
22152278
uvwasi_t *uvwasi = NULL;
22162279
uvwasi_options_t init_options;
@@ -2373,6 +2436,10 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
23732436
fd_prestats_destroy(wasi_ctx->prestats);
23742437
wasm_runtime_free(wasi_ctx->prestats);
23752438
}
2439+
if (wasi_ctx->addr_pool) {
2440+
addr_pool_destroy(wasi_ctx->addr_pool);
2441+
wasm_runtime_free(wasi_ctx->addr_pool);
2442+
}
23762443
if (wasi_ctx->argv_buf)
23772444
wasm_runtime_free(wasi_ctx->argv_buf);
23782445
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;
@@ -717,9 +718,10 @@ bool
717718
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
718719
const char *dir_list[], uint32 dir_count,
719720
const char *map_dir_list[], uint32 map_dir_count,
720-
const char *env[], uint32 env_count, char *argv[],
721-
uint32 argc, int stdinfd, int stdoutfd, int stderrfd,
722-
char *error_buf, uint32 error_buf_size);
721+
const char *env[], uint32 env_count,
722+
const char *addr_pool[], uint32 addr_pool_size,
723+
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
724+
int stderrfd, char *error_buf, uint32 error_buf_size);
723725

724726
void
725727
wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
@@ -731,6 +733,9 @@ wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
731733
WASIContext *
732734
wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);
733735

736+
WASM_RUNTIME_API_EXTERN void
737+
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
738+
uint32 addr_pool_size);
734739
#endif /* end of WASM_ENABLE_LIBC_WASI */
735740

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

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

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)