Skip to content

Commit

Permalink
Partly Implement Berkeley Socket API for libc-wasi (#836)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lum1n0us authored Feb 23, 2022
1 parent 5bdab7f commit d4fc644
Show file tree
Hide file tree
Showing 25 changed files with 1,964 additions and 207 deletions.
1 change: 1 addition & 0 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
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.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))
Expand Down
81 changes: 74 additions & 7 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1969,14 +1969,36 @@ wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
argc, -1, -1, -1);
}

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

if (wasi_args) {
wasi_args->addr_pool = addr_pool;
wasi_args->addr_count = addr_pool_size;
}
}

#if WASM_ENABLE_UVWASI == 0
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, char *argv[],
uint32 argc, int stdinfd, int stdoutfd, int stderrfd,
char *error_buf, uint32 error_buf_size)
const char *env[], uint32 env_count,
const char *addr_pool[], uint32 addr_pool_size,
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
int stderrfd, char *error_buf, uint32 error_buf_size)
{
WASIContext *wasi_ctx;
char *argv_buf = NULL;
Expand All @@ -1988,8 +2010,10 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
struct fd_table *curfds = NULL;
struct fd_prestats *prestats = NULL;
struct argv_environ_values *argv_environ = NULL;
struct addr_pool *apool = NULL;
bool fd_table_inited = false, fd_prestats_inited = false;
bool argv_environ_inited = false;
bool addr_pool_inited = false;
__wasi_fd_t wasm_fd = 3;
int32 raw_fd;
char *path, resolved_path[PATH_MAX];
Expand Down Expand Up @@ -2063,7 +2087,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
|| !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
|| !(argv_environ =
wasm_runtime_malloc(sizeof(struct argv_environ_values)))) {
wasm_runtime_malloc(sizeof(struct argv_environ_values)))
|| !(apool = wasm_runtime_malloc(sizeof(struct addr_pool)))) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: allocate memory failed");
goto fail;
Expand Down Expand Up @@ -2094,6 +2119,14 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
}
argv_environ_inited = true;

if (!addr_pool_init(apool)) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: "
"init the address pool failed");
goto fail;
}
addr_pool_inited = true;

/* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
if (!fd_table_insert_existing(curfds, 0, (stdinfd != -1) ? stdinfd : 0)
|| !fd_table_insert_existing(curfds, 1, (stdoutfd != -1) ? stdoutfd : 1)
Expand Down Expand Up @@ -2128,9 +2161,34 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
fd_prestats_insert(prestats, dir_list[i], wasm_fd);
}

/* addr_pool(textual) -> apool */
for (i = 0; i < addr_pool_size; i++) {
char *cp, *address, *mask;
bool ret = false;

cp = bh_strdup(addr_pool[i]);
if (!cp) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: copy address failed");
goto fail;
}

address = strtok(cp, "/");
mask = strtok(NULL, "/");

ret = addr_pool_insert(apool, address, (uint8)(atoi(mask)));
wasm_runtime_free(cp);
if (!ret) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: store address failed");
goto fail;
}
}

wasi_ctx->curfds = curfds;
wasi_ctx->prestats = prestats;
wasi_ctx->argv_environ = argv_environ;
wasi_ctx->addr_pool = apool;
wasi_ctx->argv_buf = argv_buf;
wasi_ctx->argv_list = argv_list;
wasi_ctx->env_buf = env_buf;
Expand All @@ -2145,12 +2203,16 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
fd_prestats_destroy(prestats);
if (fd_table_inited)
fd_table_destroy(curfds);
if (addr_pool_inited)
addr_pool_destroy(apool);
if (curfds)
wasm_runtime_free(curfds);
if (prestats)
wasm_runtime_free(prestats);
if (argv_environ)
wasm_runtime_free(argv_environ);
if (apool)
wasm_runtime_free(apool);
if (argv_buf)
wasm_runtime_free(argv_buf);
if (argv_list)
Expand Down Expand Up @@ -2208,9 +2270,10 @@ 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, char *argv[],
uint32 argc, int stdinfd, int stdoutfd, int stderrfd,
char *error_buf, uint32 error_buf_size)
const char *env[], uint32 env_count,
const char *addr_pool[], uint32 addr_pool_size,
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
int stderrfd, char *error_buf, uint32 error_buf_size)
{
uvwasi_t *uvwasi = NULL;
uvwasi_options_t init_options;
Expand Down Expand Up @@ -2373,6 +2436,10 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
fd_prestats_destroy(wasi_ctx->prestats);
wasm_runtime_free(wasi_ctx->prestats);
}
if (wasi_ctx->addr_pool) {
addr_pool_destroy(wasi_ctx->addr_pool);
wasm_runtime_free(wasi_ctx->addr_pool);
}
if (wasi_ctx->argv_buf)
wasm_runtime_free(wasi_ctx->argv_buf);
if (wasi_ctx->argv_list)
Expand Down
11 changes: 8 additions & 3 deletions core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ typedef struct WASIContext {
struct fd_table *curfds;
struct fd_prestats *prestats;
struct argv_environ_values *argv_environ;
struct addr_pool *addr_pool;
char *argv_buf;
char **argv_list;
char *env_buf;
Expand Down Expand Up @@ -717,9 +718,10 @@ 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, char *argv[],
uint32 argc, int stdinfd, int stdoutfd, int stderrfd,
char *error_buf, uint32 error_buf_size);
const char *env[], uint32 env_count,
const char *addr_pool[], uint32 addr_pool_size,
char *argv[], uint32 argc, int stdinfd, int stdoutfd,
int stderrfd, char *error_buf, uint32 error_buf_size);

void
wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
Expand All @@ -731,6 +733,9 @@ wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
WASIContext *
wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst);

WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
uint32 addr_pool_size);
#endif /* end of WASM_ENABLE_LIBC_WASI */

#if WASM_ENABLE_REF_TYPES != 0
Expand Down
4 changes: 4 additions & 0 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ wasm_runtime_set_wasi_args(wasm_module_t module,
const char *env[], uint32_t env_count,
char *argv[], int argc);

WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_wasi_addr_pool(wasm_module_t module, const char *addr_pool[],
uint32_t addr_pool_size);

/**
* Instantiate a WASM module.
*
Expand Down
3 changes: 3 additions & 0 deletions core/iwasm/interpreter/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ typedef struct WASIArguments {
uint32 map_dir_count;
const char **env;
uint32 env_count;
/* in CIDR noation */
const char **addr_pool;
uint32 addr_count;
char **argv;
uint32 argc;
int stdio[3];
Expand Down
1 change: 1 addition & 0 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
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.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)) {
Expand Down
Loading

0 comments on commit d4fc644

Please sign in to comment.