From 1cf51c50ca95d7994c333b784e22d527de7eb669 Mon Sep 17 00:00:00 2001 From: Zoraaver Singh Date: Thu, 21 Sep 2023 14:34:42 +0100 Subject: [PATCH] Don't support fd sync flags by default It's not yet clear whether these flags should be added to the standard so disallow use of them by default for the time being. See https://github.com/WebAssembly/wasi-filesystem/issues/98 for more details. These changes are also necessary to ensure the rust WASI tests pass since they assert that path_open returns ENOTSUP when passing O_DSYNC/O_RSYNC/O_SYNC. --- build-scripts/config_common.cmake | 5 +++++ .../iwasm/libraries/libc-wasi/libc_wasi.cmake | 6 +++++ .../sandboxed-system-primitives/src/posix.c | 22 ++++++++++++++----- doc/build_wamr.md | 2 ++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 210f2d7887..451ff7feea 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -201,6 +201,11 @@ elseif (WAMR_BUILD_LIBC_WASI EQUAL 1) else () message (" Libc WASI disabled") endif () +if (WAMR_BUILD_ENABLE_LIBC_WASI_SYNC_FLAGS EQUAL 1) + message (" Libc WASI file descriptor sync flags enabled") +else() + message (" Libc WASI file descriptor sync flags disabled") +endif() if ((WAMR_BUILD_FAST_INTERP EQUAL 1) AND (WAMR_BUILD_INTERP EQUAL 1)) add_definitions (-DWASM_ENABLE_FAST_INTERP=1) message (" Fast interpreter enabled") diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi.cmake b/core/iwasm/libraries/libc-wasi/libc_wasi.cmake index d72c42a063..64c7b5faf8 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi.cmake +++ b/core/iwasm/libraries/libc-wasi/libc_wasi.cmake @@ -5,6 +5,12 @@ set (LIBC_WASI_DIR ${CMAKE_CURRENT_LIST_DIR}) add_definitions (-DWASM_ENABLE_LIBC_WASI=1) +if (WAMR_BUILD_ENABLE_LIBC_WASI_SYNC_FLAGS EQUAL 1) + add_definitions (-DWASM_ENABLE_LIBC_WASI_SYNC_FLAGS=1) +else() + add_definitions (-DWASM_ENABLE_LIBC_WASI_SYNC_FLAGS=0) +endif() + include_directories(${LIBC_WASI_DIR}/sandboxed-system-primitives/include ${LIBC_WASI_DIR}/sandboxed-system-primitives/src) diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c index eda65b8da4..240b9ce28c 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c @@ -1970,6 +1970,11 @@ wasmtime_ssp_path_open(wasm_exec_env_t exec_env, struct fd_table *curfds, // Convert file descriptor flags. if ((fs_flags & __WASI_FDFLAG_APPEND) != 0) noflags |= O_APPEND; + if ((fs_flags & __WASI_FDFLAG_NONBLOCK) != 0) + noflags |= O_NONBLOCK; + if (write && (noflags & (O_APPEND | O_TRUNC)) == 0) + needed_inheriting |= __WASI_RIGHT_FD_SEEK; +#if WASM_ENABLE_LIBC_WASI_SYNC_FLAGS == 1 if ((fs_flags & __WASI_FDFLAG_DSYNC) != 0) { #ifdef O_DSYNC noflags |= O_DSYNC; @@ -1978,8 +1983,6 @@ wasmtime_ssp_path_open(wasm_exec_env_t exec_env, struct fd_table *curfds, #endif needed_inheriting |= __WASI_RIGHT_FD_DATASYNC; } - if ((fs_flags & __WASI_FDFLAG_NONBLOCK) != 0) - noflags |= O_NONBLOCK; if ((fs_flags & __WASI_FDFLAG_RSYNC) != 0) { #ifdef O_RSYNC noflags |= O_RSYNC; @@ -1992,8 +1995,16 @@ wasmtime_ssp_path_open(wasm_exec_env_t exec_env, struct fd_table *curfds, noflags |= O_SYNC; needed_inheriting |= __WASI_RIGHT_FD_SYNC; } - if (write && (noflags & (O_APPEND | O_TRUNC)) == 0) - needed_inheriting |= __WASI_RIGHT_FD_SEEK; +#else + // It's not clear whether we want to support these flags in the standard + // yet: https://github.com/WebAssembly/wasi-filesystem/issues/98 so disable + // them by default in the meanwhile. + if (((fs_flags & __WASI_FDFLAG_DSYNC) != 0) + || ((fs_flags & __WASI_FDFLAG_RSYNC) != 0) + || ((fs_flags & __WASI_FDFLAG_SYNC) != 0)) { + return __WASI_ENOTSUP; + } +#endif /* end of WASM_ENABLE_LIBC_WASI_SYNC_FLAGS */ struct path_access pa; __wasi_errno_t error = @@ -3490,7 +3501,8 @@ argv_environ_init(struct argv_environ_values *argv_environ, char *argv_buf, void argv_environ_destroy(struct argv_environ_values *argv_environ) -{} +{ +} void fd_table_destroy(struct fd_table *ft) diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 9938eadcbe..b98e5894a7 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -55,6 +55,8 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM - **WAMR_BUILD_LIBC_WASI**=1/0, build the [WASI](https://github.com/WebAssembly/WASI) libc subset for WASM app, default to enable if not set +- **WAMR_BUILD_LIBC_WASI_ENABLE_SYNC_FLAGS**=1/0, enable support for the file descriptor flags [`FDFLAGS_DSYNC`](https://docs.rs/wasi/latest/wasi/constant.FDFLAGS_DSYNC.html), [`FDFLAGS_RSYNC`](https://docs.rs/wasi/latest/wasi/constant.FDFLAGS_RSYNC.html) and [`FDFLAGS_DSYNC`](https://docs.rs/wasi/latest/wasi/constant.FDFLAGS_DSYNC.html), default to disable if not set. If enabled, support is dependent on the host OS. + - **WAMR_BUILD_LIBC_UVWASI**=1/0 (Experiment), build the [WASI](https://github.com/WebAssembly/WASI) libc subset for WASM app based on [uvwasi](https://github.com/nodejs/uvwasi) implementation, default to disable if not set > Note: for platform which doesn't support **WAMR_BUILD_LIBC_WASI**, e.g. Windows, developer can try using **WAMR_BUILD_LIBC_UVWASI**.