Abstract POSIX filesystem functions (#2585)

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 https://github.com/bytecodealliance/wasm-micro-runtime/issues/2495 for a
more detailed overview of the plan to port the WASI libc filesystem to Windows.
This commit is contained in:
zoraaver 2023-10-19 12:19:39 +01:00 committed by GitHub
parent 5fd530610a
commit fa5e9d72b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 3805 additions and 2566 deletions

View File

@ -2778,7 +2778,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)
int64 stdinfd, int64 stdoutfd, int64 stderrfd)
{
WASIArguments *wasi_args = get_wasi_args_from_module(module);
@ -2792,9 +2792,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_file_handle)stdinfd;
wasi_args->stdio[1] = (os_raw_file_handle)stdoutfd;
wasi_args->stdio[2] = (os_raw_file_handle)stderrfd;
#if WASM_ENABLE_MULTI_MODULE != 0
#if WASM_ENABLE_INTERP != 0
@ -2889,8 +2889,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_file_handle stdinfd,
os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
char *error_buf, uint32 error_buf_size)
{
WASIContext *wasi_ctx;
char *argv_buf = NULL;
@ -2908,7 +2909,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_file_handle file_handle;
char *path, resolved_path[PATH_MAX];
uint32 i;
@ -2978,15 +2979,19 @@ 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)
|| !fd_table_insert_existing(curfds, 2,
(stderrfd != -1) ? stderrfd : 2)) {
os_file_handle stdin_file_handle = os_convert_stdin_handle(stdinfd);
os_file_handle stdout_file_handle = os_convert_stdout_handle(stdoutfd);
os_file_handle stderr_file_handle = os_convert_stderr_handle(stderrfd);
if (!os_is_handle_valid(&stdin_file_handle)
|| !os_is_handle_valid(&stdout_file_handle)
|| !os_is_handle_valid(&stderr_file_handle))
goto fail;
/* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
if (!fd_table_insert_existing(curfds, 0, stdin_file_handle, true)
|| !fd_table_insert_existing(curfds, 1, stdout_file_handle, true)
|| !fd_table_insert_existing(curfds, 2, stderr_file_handle, true)) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: init fd table failed");
goto fail;
@ -2994,11 +2999,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
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,
@ -3006,25 +3007,34 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
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, &file_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_prestats_insert(prestats, dir_list[i], wasm_fd);
if (!fd_table_insert_existing(curfds, wasm_fd, file_handle, false)) {
if (error_buf)
snprintf(error_buf, error_buf_size,
"error inserting preopen fd %u (directory %s) into fd "
"table",
(unsigned int)wasm_fd, dir_list[i]);
goto fail;
}
if (!fd_prestats_insert(prestats, dir_list[i], wasm_fd)) {
if (error_buf)
snprintf(error_buf, error_buf_size,
"error inserting preopen fd %u (directory %s) into "
"prestats table",
(unsigned int)wasm_fd, dir_list[i]);
goto fail;
}
}
/* addr_pool(textual) -> apool */
@ -3152,8 +3162,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_file_handle stdinfd,
os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
char *error_buf, uint32 error_buf_size)
{
WASIContext *ctx;
uvwasi_t *uvwasi;

View File

@ -14,7 +14,6 @@
#include "../interpreter/wasm.h"
#if WASM_ENABLE_LIBC_WASI != 0
#if WASM_ENABLE_UVWASI == 0
#include "wasmtime_ssp.h"
#include "posix.h"
#else
#include "uvwasi.h"
@ -853,7 +852,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);
int64 stdinfd, int64 stdoutfd, int64 stderrfd);
/* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN void
@ -881,8 +880,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_file_handle stdinfd,
os_raw_file_handle stdoutfd, os_raw_file_handle stderrfd,
char *error_buf, uint32 error_buf_size);
void
wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst);

View File

@ -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);
char *argv[], int argc, int64_t stdinfd,
int64_t stdoutfd, int64_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,

View File

@ -350,7 +350,7 @@ typedef struct WASIArguments {
uint32 ns_lookup_count;
char **argv;
uint32 argc;
int stdio[3];
os_raw_file_handle stdio[3];
} WASIArguments;
#endif

View File

@ -7,6 +7,7 @@
#include "bh_platform.h"
#include "wasm_export.h"
#include "wasm_runtime_common.h"
#include "wasmtime_ssp.h"
#if WASM_ENABLE_THREAD_MGR != 0
#include "../../../thread-mgr/thread_manager.h"

View File

@ -6,7 +6,6 @@
#ifndef _LIBC_WASI_WRAPPER_H
#define _LIBC_WASI_WRAPPER_H
#include "wasmtime_ssp.h"
#include "posix.h"
#ifdef __cplusplus

View File

@ -8,119 +8,68 @@
#include "ssp_config.h"
#include "blocking_op.h"
int
blocking_op_close(wasm_exec_env_t exec_env, int fd)
__wasi_errno_t
blocking_op_close(wasm_exec_env_t exec_env, os_file_handle handle,
bool is_stdio)
{
if (!wasm_runtime_begin_blocking_op(exec_env)) {
errno = EINTR;
return -1;
return __WASI_EINTR;
}
int ret = close(fd);
__wasi_errno_t error = os_close(handle, is_stdio);
wasm_runtime_end_blocking_op(exec_env);
return error;
}
__wasi_errno_t
blocking_op_readv(wasm_exec_env_t exec_env, os_file_handle handle,
const struct __wasi_iovec_t *iov, int iovcnt, size_t *nread)
{
if (!wasm_runtime_begin_blocking_op(exec_env)) {
return __WASI_EINTR;
}
__wasi_errno_t error = os_readv(handle, iov, iovcnt, nread);
wasm_runtime_end_blocking_op(exec_env);
return error;
}
__wasi_errno_t
blocking_op_preadv(wasm_exec_env_t exec_env, os_file_handle handle,
const struct __wasi_iovec_t *iov, int iovcnt,
__wasi_filesize_t offset, size_t *nread)
{
if (!wasm_runtime_begin_blocking_op(exec_env)) {
return __WASI_EINTR;
}
__wasi_errno_t ret = os_preadv(handle, iov, iovcnt, offset, nread);
wasm_runtime_end_blocking_op(exec_env);
return ret;
}
ssize_t
blocking_op_readv(wasm_exec_env_t exec_env, int fd, const struct iovec *iov,
int iovcnt)
__wasi_errno_t
blocking_op_writev(wasm_exec_env_t exec_env, os_file_handle handle,
const struct __wasi_ciovec_t *iov, int iovcnt,
size_t *nwritten)
{
#ifdef BH_PLATFORM_WINDOWS
errno = ENOTSUP;
return -1;
#else
if (!wasm_runtime_begin_blocking_op(exec_env)) {
errno = EINTR;
return -1;
return __WASI_EINTR;
}
ssize_t ret = readv(fd, iov, iovcnt);
__wasi_errno_t error = os_writev(handle, iov, iovcnt, nwritten);
wasm_runtime_end_blocking_op(exec_env);
return ret;
#endif
return error;
}
#if CONFIG_HAS_PREADV
ssize_t
blocking_op_preadv(wasm_exec_env_t exec_env, int fd, const struct iovec *iov,
int iovcnt, off_t offset)
__wasi_errno_t
blocking_op_pwritev(wasm_exec_env_t exec_env, os_file_handle handle,
const struct __wasi_ciovec_t *iov, int iovcnt,
__wasi_filesize_t offset, size_t *nwritten)
{
if (!wasm_runtime_begin_blocking_op(exec_env)) {
errno = EINTR;
return -1;
return __WASI_EINTR;
}
ssize_t ret = preadv(fd, iov, iovcnt, offset);
__wasi_errno_t error = os_pwritev(handle, iov, iovcnt, offset, nwritten);
wasm_runtime_end_blocking_op(exec_env);
return ret;
return error;
}
#else /* CONFIG_HAS_PREADV */
ssize_t
blocking_op_pread(wasm_exec_env_t exec_env, int fd, void *p, size_t nb,
off_t offset)
{
#ifdef BH_PLATFORM_WINDOWS
errno = ENOTSUP;
return -1;
#else
if (!wasm_runtime_begin_blocking_op(exec_env)) {
errno = EINTR;
return -1;
}
ssize_t ret = pread(fd, p, nb, offset);
wasm_runtime_end_blocking_op(exec_env);
return ret;
#endif
}
#endif /* CONFIG_HAS_PREADV */
ssize_t
blocking_op_writev(wasm_exec_env_t exec_env, int fd, const struct iovec *iov,
int iovcnt)
{
#ifdef BH_PLATFORM_WINDOWS
errno = ENOTSUP;
return -1;
#else
if (!wasm_runtime_begin_blocking_op(exec_env)) {
errno = EINTR;
return -1;
}
ssize_t ret = writev(fd, iov, iovcnt);
wasm_runtime_end_blocking_op(exec_env);
return ret;
#endif
}
#if CONFIG_HAS_PWRITEV
ssize_t
blocking_op_pwritev(wasm_exec_env_t exec_env, int fd, const struct iovec *iov,
int iovcnt, off_t offset)
{
if (!wasm_runtime_begin_blocking_op(exec_env)) {
errno = EINTR;
return -1;
}
ssize_t ret = pwritev(fd, iov, iovcnt, offset);
wasm_runtime_end_blocking_op(exec_env);
return ret;
}
#else /* CONFIG_HAS_PWRITEV */
ssize_t
blocking_op_pwrite(wasm_exec_env_t exec_env, int fd, const void *p, size_t nb,
off_t offset)
{
#ifdef BH_PLATFORM_WINDOWS
errno = ENOTSUP;
return -1;
#else
if (!wasm_runtime_begin_blocking_op(exec_env)) {
errno = EINTR;
return -1;
}
ssize_t ret = pwrite(fd, p, nb, offset);
wasm_runtime_end_blocking_op(exec_env);
return ret;
#endif
}
#endif /* CONFIG_HAS_PWRITEV */
int
blocking_op_socket_accept(wasm_exec_env_t exec_env, bh_socket_t server_sock,
@ -207,20 +156,17 @@ blocking_op_socket_addr_resolve(wasm_exec_env_t exec_env, const char *host,
return ret;
}
int
blocking_op_openat(wasm_exec_env_t exec_env, int fd, const char *path,
int oflags, mode_t mode)
__wasi_errno_t
blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle,
const char *path, __wasi_oflags_t oflags,
__wasi_fdflags_t fd_flags, __wasi_lookupflags_t lookup_flags,
wasi_libc_file_access_mode access_mode, os_file_handle *out)
{
#ifdef BH_PLATFORM_WINDOWS
errno = ENOTSUP;
return -1;
#else
if (!wasm_runtime_begin_blocking_op(exec_env)) {
errno = EINTR;
return -1;
return __WASI_EINTR;
}
int ret = openat(fd, path, oflags, mode);
__wasi_errno_t error = os_openat(handle, path, oflags, fd_flags,
lookup_flags, access_mode, out);
wasm_runtime_end_blocking_op(exec_env);
return ret;
#endif
return error;
}

View File

@ -6,26 +6,24 @@
#include "bh_platform.h"
#include "wasm_export.h"
int
blocking_op_close(wasm_exec_env_t exec_env, int fd);
ssize_t
blocking_op_readv(wasm_exec_env_t exec_env, int fd, const struct iovec *iov,
int iovcnt);
ssize_t
blocking_op_preadv(wasm_exec_env_t exec_env, int fd, const struct iovec *iov,
int iovcnt, off_t offset);
ssize_t
blocking_op_pread(wasm_exec_env_t exec_env, int fd, void *p, size_t nb,
off_t offset);
ssize_t
blocking_op_writev(wasm_exec_env_t exec_env, int fd, const struct iovec *iov,
int iovcnt);
ssize_t
blocking_op_pwritev(wasm_exec_env_t exec_env, int fd, const struct iovec *iov,
int iovcnt, off_t offset);
ssize_t
blocking_op_pwrite(wasm_exec_env_t exec_env, int fd, const void *p, size_t nb,
off_t offset);
__wasi_errno_t
blocking_op_close(wasm_exec_env_t exec_env, os_file_handle handle,
bool is_stdio);
__wasi_errno_t
blocking_op_readv(wasm_exec_env_t exec_env, os_file_handle handle,
const struct __wasi_iovec_t *iov, int iovcnt, size_t *nread);
__wasi_errno_t
blocking_op_preadv(wasm_exec_env_t exec_env, os_file_handle handle,
const struct __wasi_iovec_t *iov, int iovcnt,
__wasi_filesize_t offset, size_t *nread);
__wasi_errno_t
blocking_op_writev(wasm_exec_env_t exec_env, os_file_handle handle,
const struct __wasi_ciovec_t *iov, int iovcnt,
size_t *nwritten);
__wasi_errno_t
blocking_op_pwritev(wasm_exec_env_t exec_env, os_file_handle handle,
const struct __wasi_ciovec_t *iov, int iovcnt,
__wasi_filesize_t offset, size_t *nwritten);
int
blocking_op_socket_accept(wasm_exec_env_t exec_env, bh_socket_t server_sock,
bh_socket_t *sockp, void *addr,
@ -48,10 +46,8 @@ blocking_op_socket_addr_resolve(wasm_exec_env_t exec_env, const char *host,
bh_addr_info_t *addr_info,
size_t addr_info_size, size_t *max_info_size);
#ifdef BH_PLATFORM_WINDOWS
/* TODO to be (re)moved as part of WASI on windows work */
typedef unsigned mode_t;
#endif
int
blocking_op_openat(wasm_exec_env_t exec_env, int fd, const char *path,
int oflags, mode_t mode);
__wasi_errno_t
blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle,
const char *path, __wasi_oflags_t oflags,
__wasi_fdflags_t fd_flags, __wasi_lookupflags_t lookup_flags,
wasi_libc_file_access_mode access_mode, os_file_handle *out);

View File

@ -60,7 +60,8 @@ struct addr_pool {
bool
fd_table_init(struct fd_table *);
bool
fd_table_insert_existing(struct fd_table *, __wasi_fd_t, int);
fd_table_insert_existing(struct fd_table *, __wasi_fd_t, os_file_handle,
bool is_stdio);
bool
fd_prestats_init(struct fd_prestats *);
bool

View File

@ -47,38 +47,6 @@
#define CONFIG_HAS_CLOCK_NANOSLEEP 0
#endif
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(ESP_PLATFORM) \
&& !defined(_WIN32)
#define CONFIG_HAS_FDATASYNC 1
#else
#define CONFIG_HAS_FDATASYNC 0
#endif
/*
* For NuttX, CONFIG_HAS_ISATTY is provided by its platform header.
* (platform_internal.h)
*/
#ifndef __NuttX__
#ifndef __CloudABI__
#define CONFIG_HAS_ISATTY 1
#else
#define CONFIG_HAS_ISATTY 0
#endif
#endif
#if !defined(__APPLE__) && !defined(ESP_PLATFORM) && !defined(_WIN32) \
&& !defined(__COSMOPOLITAN__)
#define CONFIG_HAS_POSIX_FALLOCATE 1
#else
#define CONFIG_HAS_POSIX_FALLOCATE 0
#endif
#if !defined(__APPLE__) && !defined(ESP_PLATFORM) && !defined(_WIN32)
#define CONFIG_HAS_PREADV 1
#else
#define CONFIG_HAS_PREADV 0
#endif
#if defined(__APPLE__) || defined(__CloudABI__)
#define CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP 1
#else
@ -92,32 +60,6 @@
#define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 0
#endif
#if !defined(__APPLE__) && !defined(ESP_PLATFORM) && !defined(_WIN32)
#define CONFIG_HAS_PWRITEV 1
#else
#define CONFIG_HAS_PWRITEV 0
#endif
#ifdef __APPLE__
#define st_atim st_atimespec
#define st_ctim st_ctimespec
#define st_mtim st_mtimespec
#endif
#if defined(O_DSYNC)
#define CONFIG_HAS_O_DSYNC
#endif
// POSIX requires O_RSYNC to be defined, but Linux explicitly doesn't support
// it.
#if defined(O_RSYNC) && !defined(__linux__)
#define CONFIG_HAS_O_RSYNC
#endif
#if defined(O_SYNC)
#define CONFIG_HAS_O_SYNC
#endif
#if !defined(BH_PLATFORM_LINUX_SGX)
/* Clang's __GNUC_PREREQ macro has a different meaning than GCC one,
so we have to handle this case specially */
@ -145,10 +87,4 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58016 */
#define CONFIG_HAS_STD_ATOMIC 0
#endif /* end of !defined(BH_PLATFORM_LINUX_SGX) */
#if !defined(__NuttX__)
#define CONFIG_HAS_D_INO 1
#else
#define CONFIG_HAS_D_INO 0
#endif
#endif

View File

@ -64,4 +64,8 @@ int signbit(double x);
int isnan(double x);
/* clang-format on */
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#endif /* end of _BH_PLATFORM_H */

View File

@ -146,6 +146,10 @@ preadv(int __fd, const struct iovec *__iov, int __count, off_t __offset);
ssize_t
pwritev(int __fd, const struct iovec *__iov, int __count, off_t __offset);
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,256 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "errno.h"
#include "libc_errno.h"
__wasi_errno_t
convert_errno(int error)
{
// The C standard library only requires EDOM, EILSEQ and ERANGE to be
// defined. Other error codes are POSIX-specific and hence may or may
// not be available on non-POSIX platforms.
__wasi_errno_t code = __WASI_ENOSYS;
#define X(v) \
case v: \
code = __WASI_##v; \
break;
switch (error) {
X(EDOM)
X(EILSEQ)
X(ERANGE)
#ifdef E2BIG
X(E2BIG)
#endif
#ifdef EACCES
X(EACCES)
#endif
#ifdef EADDRINUSE
X(EADDRINUSE)
#endif
#ifdef EADDRNOTAVAIL
X(EADDRNOTAVAIL)
#endif
#ifdef EAFNOSUPPORT
X(EAFNOSUPPORT)
#endif
#ifdef EAGAIN
X(EAGAIN)
#endif
#ifdef EALREADY
X(EALREADY)
#endif
#ifdef EBADF
X(EBADF)
#endif
#ifdef EBADMSG
X(EBADMSG)
#endif
#ifdef EBUSY
X(EBUSY)
#endif
#ifdef ECANCELED
X(ECANCELED)
#endif
#ifdef ECHILD
X(ECHILD)
#endif
#ifdef ECONNABORTED
X(ECONNABORTED)
#endif
#ifdef ECONNREFUSED
X(ECONNREFUSED)
#endif
#ifdef ECONNRESET
X(ECONNRESET)
#endif
#ifdef EDEADLK
X(EDEADLK)
#endif
#ifdef EDESTADDRREQ
X(EDESTADDRREQ)
#endif
#ifdef EDQUOT
X(EDQUOT)
#endif
#ifdef EEXIST
X(EEXIST)
#endif
#ifdef EFAULT
X(EFAULT)
#endif
#ifdef EFBIG
X(EFBIG)
#endif
#ifdef EHOSTUNREACH
X(EHOSTUNREACH)
#endif
#ifdef EIDRM
X(EIDRM)
#endif
#ifdef EINPROGRESS
X(EINPROGRESS)
#endif
#ifdef EINTR
X(EINTR)
#endif
#ifdef EINVAL
X(EINVAL)
#endif
#ifdef EIO
X(EIO)
#endif
#ifdef EISCONN
X(EISCONN)
#endif
#ifdef EISDIR
X(EISDIR)
#endif
#ifdef ELOOP
X(ELOOP)
#endif
#ifdef EMFILE
X(EMFILE)
#endif
#ifdef EMLINK
X(EMLINK)
#endif
#ifdef EMSGSIZE
X(EMSGSIZE)
#endif
#ifdef EMULTIHOP
X(EMULTIHOP)
#endif
#ifdef ENAMETOOLONG
X(ENAMETOOLONG)
#endif
#ifdef ENETDOWN
X(ENETDOWN)
#endif
#ifdef ENETRESET
X(ENETRESET)
#endif
#ifdef ENETUNREACH
X(ENETUNREACH)
#endif
#ifdef ENFILE
X(ENFILE)
#endif
#ifdef ENOBUFS
X(ENOBUFS)
#endif
#ifdef ENODEV
X(ENODEV)
#endif
#ifdef ENOENT
X(ENOENT)
#endif
#ifdef ENOEXEC
X(ENOEXEC)
#endif
#ifdef ENOLCK
X(ENOLCK)
#endif
#ifdef ENOLINK
X(ENOLINK)
#endif
#ifdef ENOMEM
X(ENOMEM)
#endif
#ifdef ENOMSG
X(ENOMSG)
#endif
#ifdef ENOPROTOOPT
X(ENOPROTOOPT)
#endif
#ifdef ENOSPC
X(ENOSPC)
#endif
#ifdef ENOSYS
X(ENOSYS)
#endif
#ifdef ENOTCAPABLE
X(ENOTCAPABLE)
#endif
#ifdef ENOTCONN
X(ENOTCONN)
#endif
#ifdef ENOTDIR
X(ENOTDIR)
#endif
#ifdef ENOTEMPTY
X(ENOTEMPTY)
#endif
#ifdef ENOTRECOVERABLE
X(ENOTRECOVERABLE)
#endif
#ifdef ENOTSOCK
X(ENOTSOCK)
#endif
#ifdef ENOTSUP
X(ENOTSUP)
#endif
#ifdef ENOTTY
X(ENOTTY)
#endif
#ifdef ENXIO
X(ENXIO)
#endif
#ifdef EOVERFLOW
X(EOVERFLOW)
#endif
#ifdef EOWNERDEAD
X(EOWNERDEAD)
#endif
#ifdef EPERM
X(EPERM)
#endif
#ifdef EPIPE
X(EPIPE)
#endif
#ifdef EPROTO
X(EPROTO)
#endif
#ifdef EPROTONOSUPPORT
X(EPROTONOSUPPORT)
#endif
#ifdef EPROTOTYPE
X(EPROTOTYPE)
#endif
#ifdef EROFS
X(EROFS)
#endif
#ifdef ESPIPE
X(ESPIPE)
#endif
#ifdef ESRCH
X(ESRCH)
#endif
#ifdef ESTALE
X(ESTALE)
#endif
#ifdef ETIMEDOUT
X(ETIMEDOUT)
#endif
#ifdef ETXTBSY
X(ETXTBSY)
#endif
#ifdef EXDEV
X(EXDEV)
#endif
default:
#ifdef EOPNOTSUPP
if (error == EOPNOTSUPP)
code = __WASI_ENOTSUP;
#endif
#ifdef EWOULDBLOCK
if (error == EWOULDBLOCK)
code = __WASI_EAGAIN;
#endif
break;
}
#undef X
return code;
}

View File

@ -0,0 +1,15 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef WASI_ERRNO_H
#define WASI_ERRNO_H
#include "platform_wasi.h"
// Converts an errno error code to a WASI error code.
__wasi_errno_t
convert_errno(int error);
#endif /* end of WASI_ERRNO_H */

View File

@ -0,0 +1,8 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (PLATFORM_COMMON_LIBC_UTIL_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${PLATFORM_COMMON_LIBC_UTIL_DIR})
file (GLOB_RECURSE PLATFORM_COMMON_LIBC_UTIL_SOURCE ${PLATFORM_COMMON_LIBC_UTIL_DIR}/*.c)

View File

@ -5,4 +5,11 @@ set (PLATFORM_COMMON_POSIX_DIR ${CMAKE_CURRENT_LIST_DIR})
file (GLOB_RECURSE source_all ${PLATFORM_COMMON_POSIX_DIR}/*.c)
if (NOT WAMR_BUILD_LIBC_WASI EQUAL 1)
list(REMOVE_ITEM source_all ${PLATFORM_COMMON_POSIX_DIR}/posix_file.c)
else()
include (${CMAKE_CURRENT_LIST_DIR}/../libc-util/platform_common_libc_util.cmake)
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
endif()
set (PLATFORM_COMMON_POSIX_SOURCE ${source_all} )

File diff suppressed because it is too large Load Diff

View File

@ -63,6 +63,10 @@ typedef sem_t korp_sem;
#define bh_socket_t int
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#if WASM_DISABLE_WRITE_GS_BASE == 0
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
#define os_writegsbase(base_addr) \

View File

@ -109,6 +109,10 @@ os_sigreturn();
void
os_set_signal_number_for_blocking_op(int signo);
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#ifdef __cplusplus
}
#endif

View File

@ -109,6 +109,10 @@ typedef unsigned int korp_sem;
#define DT_LNK DTYPE_LINK
#define DT_SOCK DTYPE_SOCK
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#ifdef __cplusplus
}
#endif

View File

@ -66,6 +66,10 @@ typedef sem_t korp_sem;
#define bh_socket_t int
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#if WASM_DISABLE_HW_BOUND_CHECK == 0
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \

View File

@ -7,6 +7,7 @@
#define PLATFORM_API_EXTENSION_H
#include "platform_common.h"
#include "platform_wasi.h"
/**
* The related data structures should be defined
* in platform_internal.h

File diff suppressed because it is too large Load Diff

View File

@ -69,6 +69,10 @@ strcpy(char *dest, const char *src);
#define os_memory_order_seq_cst __ATOMIC_SEQ_CST
#define os_atomic_thread_fence __atomic_thread_fence
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#ifdef __cplusplus
}
#endif

View File

@ -7,7 +7,6 @@
#define _LIBC_WASI_SGX_PFS_H
#include "bh_hashmap.h"
#include "wasmtime_ssp.h"
#ifdef __cplusplus
extern "C" {

View File

@ -20,16 +20,20 @@ if (NOT BUILD_UNTRUST_PART EQUAL 1)
${SGX_SDK_DIR}/include/libcxx)
endif ()
if (NOT WAMR_BUILD_LIBC_WASI EQUAL 1)
add_definitions(-DSGX_DISABLE_WASI)
endif ()
if (NOT WAMR_BUILD_THREAD_MGR EQUAL 1)
add_definitions(-DSGX_DISABLE_PTHREAD)
endif ()
file (GLOB source_all ${PLATFORM_SHARED_DIR}/*.c)
if (NOT WAMR_BUILD_LIBC_WASI EQUAL 1)
add_definitions(-DSGX_DISABLE_WASI)
else()
list(APPEND source_all ${PLATFORM_SHARED_DIR}/../common/posix/posix_file.c)
include (${CMAKE_CURRENT_LIST_DIR}/../common/libc-util/platform_common_libc_util.cmake)
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
endif()
file (GLOB source_all_untrusted ${PLATFORM_SHARED_DIR}/untrusted/*.c)
set (PLATFORM_SHARED_SOURCE ${source_all})

View File

@ -122,6 +122,10 @@ os_sigreturn();
void
os_set_signal_number_for_blocking_op(int signo);
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#ifdef __cplusplus
}
#endif

View File

@ -130,6 +130,10 @@ fdopendir(int fd);
void
os_set_signal_number_for_blocking_op(int signo);
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#ifdef __cplusplus
}
#endif

View File

@ -10,5 +10,11 @@ include_directories(${PLATFORM_SHARED_DIR}/../include)
file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c)
if (WAMR_BUILD_LIBC_WASI EQUAL 1)
list(APPEND source_all ${PLATFORM_SHARED_DIR}/../common/posix/posix_file.c)
include (${CMAKE_CURRENT_LIST_DIR}/../common/libc-util/platform_common_libc_util.cmake)
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
endif ()
set (PLATFORM_SHARED_SOURCE ${source_all} ${PLATFORM_COMMON_MATH_SOURCE})

View File

@ -52,6 +52,10 @@ typedef struct korp_cond {
#define os_printf printf
#define os_vprintf vprintf
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#if WA_MATH
/* clang-format off */
/* math functions which are not provided by os*/

View File

@ -45,4 +45,8 @@ typedef rt_int16_t int16_t;
typedef rt_uint64_t uint64_t;
typedef rt_int64_t int64_t;
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#endif /* RTTHREAD_PLATFORM_INTERNAL_H */

View File

@ -61,6 +61,10 @@ typedef sem_t korp_sem;
#define os_thread_local_attribute __thread
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#if WASM_DISABLE_HW_BOUND_CHECK == 0
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
|| defined(BUILD_TARGET_AARCH64)

View File

@ -137,6 +137,14 @@ bh_atomic_thread_fence(int mem_order);
#define os_atomic_thread_fence bh_atomic_thread_fence
typedef HANDLE os_file_handle;
typedef void *os_dir_stream;
#if WASM_ENABLE_UVWASI != 1
typedef HANDLE os_raw_file_handle;
#else
typedef uint32_t os_raw_file_handle;
#endif
#ifdef __cplusplus
}
#endif

View File

@ -13,6 +13,13 @@ include_directories(${PLATFORM_SHARED_DIR}/../include)
file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c
${PLATFORM_SHARED_DIR}/*.cpp)
if (NOT WAMR_BUILD_LIBC_WASI EQUAL 1)
list(REMOVE_ITEM source_all ${PLATFORM_SHARED_DIR}/win_file.c)
else()
include (${CMAKE_CURRENT_LIST_DIR}/../common/libc-util/platform_common_libc_util.cmake)
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
endif()
set (PLATFORM_SHARED_SOURCE ${source_all})
file (GLOB header ${PLATFORM_SHARED_DIR}/../include/*.h)

View File

@ -0,0 +1,267 @@
/*
* Copyright (C) 2023 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "platform_api_extension.h"
#include "platform_internal.h"
__wasi_errno_t
os_fstat(os_file_handle handle, struct __wasi_filestat_t *buf)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_fstatat(os_file_handle handle, const char *path,
struct __wasi_filestat_t *buf, __wasi_lookupflags_t lookup_flags)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_file_get_fdflags(os_file_handle handle, __wasi_fdflags_t *flags)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_file_set_fdflags(os_file_handle handle, __wasi_fdflags_t flags)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_file_get_access_mode(os_file_handle handle,
wasi_libc_file_access_mode *access_mode)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_fdatasync(os_file_handle handle)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_fsync(os_file_handle handle)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_open_preopendir(const char *path, os_file_handle *out)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_openat(os_file_handle handle, const char *path, __wasi_oflags_t oflags,
__wasi_fdflags_t fs_flags, __wasi_lookupflags_t lookup_flags,
wasi_libc_file_access_mode read_write_mode, os_file_handle *out)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_close(os_file_handle handle, bool is_stdio)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_preadv(os_file_handle handle, const struct __wasi_iovec_t *iov, int iovcnt,
__wasi_filesize_t offset, size_t *nread)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_pwritev(os_file_handle handle, const struct __wasi_ciovec_t *iov, int iovcnt,
__wasi_filesize_t offset, size_t *nwritten)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_readv(os_file_handle handle, const struct __wasi_iovec_t *iov, int iovcnt,
size_t *nread)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_writev(os_file_handle handle, const struct __wasi_ciovec_t *iov, int iovcnt,
size_t *nwritten)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_fallocate(os_file_handle handle, __wasi_filesize_t offset,
__wasi_filesize_t length)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_ftruncate(os_file_handle handle, __wasi_filesize_t size)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_futimens(os_file_handle handle, __wasi_timestamp_t access_time,
__wasi_timestamp_t modification_time, __wasi_fstflags_t fstflags)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_utimensat(os_file_handle handle, const char *path,
__wasi_timestamp_t access_time,
__wasi_timestamp_t modification_time, __wasi_fstflags_t fstflags,
__wasi_lookupflags_t lookup_flags)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_readlinkat(os_file_handle handle, const char *path, char *buf,
size_t bufsize, size_t *nread)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_linkat(os_file_handle from_handle, const char *from_path,
os_file_handle to_handle, const char *to_path,
__wasi_lookupflags_t lookup_flags)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_symlinkat(const char *old_path, os_file_handle handle, const char *new_path)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_mkdirat(os_file_handle handle, const char *path)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_renameat(os_file_handle old_handle, const char *old_path,
os_file_handle new_handle, const char *new_path)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_unlinkat(os_file_handle handle, const char *path, bool is_dir)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_lseek(os_file_handle handle, __wasi_filedelta_t offset,
__wasi_whence_t whence, __wasi_filesize_t *new_offset)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_fadvise(os_file_handle handle, __wasi_filesize_t offset,
__wasi_filesize_t length, __wasi_advice_t advice)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_isatty(os_file_handle handle)
{
return __WASI_ENOSYS;
}
os_file_handle
os_convert_stdin_handle(os_raw_file_handle raw_stdin)
{
return INVALID_HANDLE_VALUE;
}
os_file_handle
os_convert_stdout_handle(os_raw_file_handle raw_stdout)
{
return INVALID_HANDLE_VALUE;
}
os_file_handle
os_convert_stderr_handle(os_raw_file_handle raw_stderr)
{
return INVALID_HANDLE_VALUE;
}
__wasi_errno_t
os_fdopendir(os_file_handle handle, os_dir_stream *dir_stream)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_rewinddir(os_dir_stream dir_stream)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_seekdir(os_dir_stream dir_stream, __wasi_dircookie_t position)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_readdir(os_dir_stream dir_stream, __wasi_dirent_t *entry,
const char **d_name)
{
return __WASI_ENOSYS;
}
__wasi_errno_t
os_closedir(os_dir_stream dir_stream)
{
return __WASI_ENOSYS;
}
os_dir_stream
os_get_invalid_dir_stream()
{
return NULL;
}
bool
os_is_dir_stream_valid(os_dir_stream *dir_stream)
{
return false;
}
os_file_handle
os_get_invalid_handle()
{
return INVALID_HANDLE_VALUE;
}
bool
os_is_handle_valid(os_file_handle *handle)
{
return false;
}
char *
os_realpath(const char *path, char *resolved_path)
{
return NULL;
}

View File

@ -146,4 +146,8 @@ void
set_exec_mem_alloc_func(exec_mem_alloc_func_t alloc_func,
exec_mem_free_func_t free_func);
typedef int os_file_handle;
typedef DIR *os_dir_stream;
typedef int os_raw_file_handle;
#endif

View File

@ -246,8 +246,11 @@ ifeq ($(CONFIG_INTERPRETERS_WAMR_LIBC_WASI),y)
CFLAGS += -DWASM_ENABLE_LIBC_WASI=1
CFLAGS += -I$(IWASM_ROOT)/libraries/libc-wasi/sandboxed-system-primitives/src
CFLAGS += -I$(IWASM_ROOT)/libraries/libc-wasi/sandboxed-system-primitives/include
CFLAGS += -I${SHARED_ROOT}/platform/common/libc-util
CSRCS += blocking_op.c
CSRCS += posix_socket.c
CSRCS += posix_file.c
CSRCS += libc_errno.c
CSRCS += libc_wasi_wrapper.c
VPATH += $(IWASM_ROOT)/libraries/libc-wasi
CSRCS += posix.c
@ -387,6 +390,7 @@ ASRCS += $(INVOKE_NATIVE)
VPATH += $(SHARED_ROOT)/platform/nuttx
VPATH += $(SHARED_ROOT)/platform/common/posix
VPATH += $(SHARED_ROOT)/platform/common/libc-util
VPATH += $(SHARED_ROOT)/mem-alloc
VPATH += $(SHARED_ROOT)/mem-alloc/ems
VPATH += $(SHARED_ROOT)/utils

View File

@ -198,6 +198,12 @@ include_directories (${SHARED_DIR}/include
enable_language (ASM)
if (NOT MINGW AND NOT MSVC)
set(WAMR_BUILD_LIBC_WASI 1)
else()
set(WAMR_BUILD_LIBC_UVWASI 1)
endif()
include (${SHARED_DIR}/platform/${WAMR_BUILD_PLATFORM}/shared_platform.cmake)
include (${SHARED_DIR}/mem-alloc/mem_alloc.cmake)
include (${SHARED_DIR}/utils/shared_utils.cmake)