Skip to content

Commit

Permalink
Abstract POSIX filesystem functions
Browse files Browse the repository at this point in the history
To allow non-POSIX platforms such as Windows to support WASI libc
filesystem functionality, create a set of wrapper functions which
provide a platform-agnostic interface to interact with the host
filesystem. For now, the Windows implementation is stubbed but this will
be implemented properly in a future PR. There are no functional changes
in this change, just a reorganization of code to move any direct POSIX
references out of posix.c in the libc implementation into posix_file.c
under the shared POSIX sources.

See #2495 for a
more detailed overview of the plan to port the WASI libc filesystem to Windows.
  • Loading branch information
zoraaver committed Sep 25, 2023
1 parent b7a9da1 commit fd8398a
Show file tree
Hide file tree
Showing 29 changed files with 2,254 additions and 927 deletions.
60 changes: 25 additions & 35 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2745,7 +2745,7 @@ 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)
uint64 stdinfd, uint64 stdoutfd, uint64 stderrfd)
{
WASIArguments *wasi_args = get_wasi_args_from_module(module);

Expand All @@ -2759,9 +2759,9 @@ wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module, const char *dir_list[],
wasi_args->env_count = env_count;
wasi_args->argv = argv;
wasi_args->argc = (uint32)argc;
wasi_args->stdio[0] = stdinfd;
wasi_args->stdio[1] = stdoutfd;
wasi_args->stdio[2] = stderrfd;
wasi_args->stdio[0] = (os_raw_handle)stdinfd;
wasi_args->stdio[1] = (os_raw_handle)stdoutfd;
wasi_args->stdio[2] = (os_raw_handle)stderrfd;

#if WASM_ENABLE_MULTI_MODULE != 0
#if WASM_ENABLE_INTERP != 0
Expand All @@ -2780,7 +2780,9 @@ wasm_runtime_set_wasi_args(WASMModuleCommon *module, const char *dir_list[],
{
wasm_runtime_set_wasi_args_ex(module, dir_list, dir_count, map_dir_list,
map_dir_count, env_list, env_count, argv,
argc, -1, -1, -1);
argc, (uint64)WASI_LIBC_INVALID_RAW_HANDLE,
(uint64)WASI_LIBC_INVALID_RAW_HANDLE,
(uint64)WASI_LIBC_INVALID_RAW_HANDLE);
}

void
Expand Down Expand Up @@ -2856,8 +2858,9 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
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)
char *argv[], uint32 argc, os_raw_handle stdinfd,
os_raw_handle stdoutfd, os_raw_handle stderrfd,
char *error_buf, uint32 error_buf_size)
{
WASIContext *wasi_ctx;
char *argv_buf = NULL;
Expand All @@ -2875,7 +2878,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
bool argv_environ_inited = false;
bool addr_pool_inited = false;
__wasi_fd_t wasm_fd = 3;
int32 raw_fd;
os_handle raw_handle;
char *path, resolved_path[PATH_MAX];
uint32 i;

Expand Down Expand Up @@ -2945,52 +2948,38 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
}
addr_pool_inited = true;

/* Prepopulate curfds with stdin, stdout, and stderr file descriptors.
*
* If -1 is given, use STDIN_FILENO (0), STDOUT_FILENO (1),
* STDERR_FILENO (2) respectively.
*/
if (!fd_table_insert_existing(curfds, 0, (stdinfd != -1) ? stdinfd : 0)
|| !fd_table_insert_existing(curfds, 1, (stdoutfd != -1) ? stdoutfd : 1)
// Prepopulate curfds with stdin, stdout, and stderr file descriptors.
if (!fd_table_insert_existing(curfds, 0, os_get_stdin_handle(stdinfd))
|| !fd_table_insert_existing(curfds, 1, os_get_stdout_handle(stdoutfd))
|| !fd_table_insert_existing(curfds, 2,
(stderrfd != -1) ? stderrfd : 2)) {
os_get_stderr_handle(stderrfd))) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: init fd table failed");
goto fail;
}

wasm_fd = 3;
for (i = 0; i < dir_count; i++, wasm_fd++) {
#ifdef BH_PLATFORM_WINDOWS
path = _fullpath(resolved_path, dir_list[i], PATH_MAX);
#else
path = realpath(dir_list[i], resolved_path);
#endif
path = os_realpath(dir_list[i], resolved_path);
if (!path) {
if (error_buf)
snprintf(error_buf, error_buf_size,
"error while pre-opening directory %s: %d\n",
dir_list[i], errno);
goto fail;
}
#ifdef BH_PLATFORM_WINDOWS
if (error_buf)
snprintf(
error_buf, error_buf_size,
"pre-opening directory is not supported on windows platforms");
goto fail;
#else
raw_fd = open(path, O_RDONLY | O_DIRECTORY, 0);
#endif
if (raw_fd == -1) {

__wasi_errno_t error = os_open_preopendir(path, &raw_handle);

if (error != __WASI_ESUCCESS) {
if (error_buf)
snprintf(error_buf, error_buf_size,
"error while pre-opening directory %s: %d\n",
dir_list[i], errno);
dir_list[i], error);
goto fail;
}

fd_table_insert_existing(curfds, wasm_fd, raw_fd);
fd_table_insert_existing(curfds, wasm_fd, raw_handle);
fd_prestats_insert(prestats, dir_list[i], wasm_fd);
}

Expand Down Expand Up @@ -3119,8 +3108,9 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
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)
char *argv[], uint32 argc, os_raw_handle stdinfd,
os_raw_handle stdoutfd, os_raw_handle stderrfd,
char *error_buf, uint32 error_buf_size)
{
WASIContext *ctx;
uvwasi_t *uvwasi;
Expand Down
8 changes: 5 additions & 3 deletions core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "bh_platform.h"
#include "bh_common.h"
#include "platform_common.h"
#include "wasm_exec_env.h"
#include "wasm_native.h"
#include "../include/wasm_export.h"
Expand Down Expand Up @@ -817,7 +818,7 @@ 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);
uint64 stdinfd, uint64 stdoutfd, uint64 stderrfd);

/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN void
Expand Down Expand Up @@ -845,8 +846,9 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
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);
char *argv[], uint32 argc, os_raw_handle stdinfd,
os_raw_handle stdoutfd, os_raw_handle stderrfd,
char *error_buf, uint32 error_buf_size);

void
wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);
Expand Down
23 changes: 14 additions & 9 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,26 +431,31 @@ wasm_runtime_get_module_hash(wasm_module_t module);
* @param env_count The number of elements in env.
* @param argv The list of command line arguments.
* @param argc The number of elements in argv.
* @param stdinfd The host file descriptor to back WASI STDIN_FILENO.
* If -1 is specified, STDIN_FILENO is used.
* @param stdoutfd The host file descriptor to back WASI STDOUT_FILENO.
* If -1 is specified, STDOUT_FILENO is used.
* @param stderrfd The host file descriptor to back WASI STDERR_FILENO.
* If -1 is specified, STDERR_FILENO is used.
* @param stdin_handle The raw host handle to back WASI STDIN_FILENO.
* If an invalid handle is specified (e.g. -1 on POSIX,
* INVALID_HANDLE_VALUE on Windows), the platform default
* for STDIN is used.
* @param stdoutfd The raw host handle to back WASI STDOUT_FILENO.
* If an invalid handle is specified (e.g. -1 on POSIX,
* INVALID_HANDLE_VALUE on Windows), the platform default
* for STDOUT is used.
* @param stderrfd The raw host handle to back WASI STDERR_FILENO.
* If an invalid handle is specified (e.g. -1 on POSIX,
* INVALID_HANDLE_VALUE on Windows), the platform default
* for STDERR is used.
*/
WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_wasi_args_ex(wasm_module_t module,
const char *dir_list[], uint32_t dir_count,
const char *map_dir_list[], uint32_t map_dir_count,
const char *env[], uint32_t env_count,
char *argv[], int argc,
int stdinfd, int stdoutfd, int stderrfd);
uint64_t stdinfd, uint64_t stdoutfd, uint64_t stderrfd);

/**
* Set WASI parameters.
*
* Same as wasm_runtime_set_wasi_args_ex with stdinfd = -1, stdoutfd = -1,
* stderrfd = -1.
* Same as wasm_runtime_set_wasi_args_ex but with default stdio handles
*/
WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_wasi_args(wasm_module_t module,
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/interpreter/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ typedef struct WASIArguments {
uint32 ns_lookup_count;
char **argv;
uint32 argc;
int stdio[3];
os_raw_handle stdio[3];
} WASIArguments;
#endif

Expand Down
Loading

0 comments on commit fd8398a

Please sign in to comment.