diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 210f2d7887..43510c5fde 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -201,6 +201,9 @@ 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") +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..86b56edd63 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi.cmake +++ b/core/iwasm/libraries/libc-wasi/libc_wasi.cmake @@ -5,6 +5,10 @@ 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 (-DCONFIG_HAS_LIBC_WASI_SYNC_FLAGS=1) +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..5cb1dea7ae 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 CONFIG_HAS_LIBC_WASI_SYNC_FLAGS != 0 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 CONFIG_HAS_LIBC_WASI_SYNC_FLAGS */ struct path_access pa; __wasi_errno_t error = diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h index f5e130a9ee..8519662655 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h @@ -134,4 +134,8 @@ not implemented until 4.9. See #define CONFIG_HAS_D_INO 0 #endif +#ifndef CONFIG_HAS_LIBC_WASI_SYNC_FLAGS +#define CONFIG_HAS_LIBC_WASI_SYNC_FLAGS 0 +#endif + #endif 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**. diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index 04a6db91fd..10b0b214df 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -246,6 +246,9 @@ 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 +ifeq ($(CONFIG_INTERPRETERS_WAMR_LIBC_WASI_SYNC_FLAGS),y) +CFLAGS += -DCONFIG_HAS_LIBC_WASI_SYNC_FLAGS=1 +endif CSRCS += blocking_op.c CSRCS += posix_socket.c CSRCS += libc_wasi_wrapper.c