Skip to content

Commit

Permalink
libc-wasi: Conditionally support SYNC flags (bytecodealliance#2581)
Browse files Browse the repository at this point in the history
To make it clearer to users when synchronization behaviour is not
supported, return ENOTSUP when O_RSYNC, O_DSYNC or O_SYNC are
respectively not defined. Linux also doesn't support O_RSYNC despite the
O_RSYNC flag being defined.
  • Loading branch information
zoraaver authored Oct 4, 2023
1 parent 1cd7341 commit d65c0e0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1282,18 +1282,20 @@ wasmtime_ssp_fd_fdstat_get(wasm_exec_env_t exec_env, struct fd_table *curfds,

if ((ret & O_APPEND) != 0)
buf->fs_flags |= __WASI_FDFLAG_APPEND;
#ifdef O_DSYNC
#ifdef CONFIG_HAS_O_DSYNC
if ((ret & O_DSYNC) != 0)
buf->fs_flags |= __WASI_FDFLAG_DSYNC;
#endif
if ((ret & O_NONBLOCK) != 0)
buf->fs_flags |= __WASI_FDFLAG_NONBLOCK;
#ifdef O_RSYNC
#ifdef CONFIG_HAS_O_RSYNC
if ((ret & O_RSYNC) != 0)
buf->fs_flags |= __WASI_FDFLAG_RSYNC;
#endif
#ifdef CONFIG_HAS_O_SYNC
if ((ret & O_SYNC) != 0)
buf->fs_flags |= __WASI_FDFLAG_SYNC;
#endif
return 0;
}

Expand All @@ -1306,21 +1308,25 @@ wasmtime_ssp_fd_fdstat_set_flags(wasm_exec_env_t exec_env,
if ((fs_flags & __WASI_FDFLAG_APPEND) != 0)
noflags |= O_APPEND;
if ((fs_flags & __WASI_FDFLAG_DSYNC) != 0)
#ifdef O_DSYNC
#ifdef CONFIG_HAS_O_DSYNC
noflags |= O_DSYNC;
#else
noflags |= O_SYNC;
return __WASI_ENOTSUP;
#endif
if ((fs_flags & __WASI_FDFLAG_NONBLOCK) != 0)
noflags |= O_NONBLOCK;
if ((fs_flags & __WASI_FDFLAG_RSYNC) != 0)
#ifdef O_RSYNC
#ifdef CONFIG_HAS_O_RSYNC
noflags |= O_RSYNC;
#else
noflags |= O_SYNC;
return __WASI_ENOTSUP;
#endif
if ((fs_flags & __WASI_FDFLAG_SYNC) != 0)
#ifdef CONFIG_HAS_O_SYNC
noflags |= O_SYNC;
#else
return __WASI_ENOTSUP;
#endif

struct fd_object *fo;
__wasi_errno_t error =
Expand Down Expand Up @@ -1971,26 +1977,30 @@ wasmtime_ssp_path_open(wasm_exec_env_t exec_env, struct fd_table *curfds,
if ((fs_flags & __WASI_FDFLAG_APPEND) != 0)
noflags |= O_APPEND;
if ((fs_flags & __WASI_FDFLAG_DSYNC) != 0) {
#ifdef O_DSYNC
#ifdef CONFIG_HAS_O_DSYNC
noflags |= O_DSYNC;
needed_inheriting |= __WASI_RIGHT_FD_DATASYNC;
#else
noflags |= O_SYNC;
return __WASI_ENOTSUP;
#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
#ifdef CONFIG_HAS_O_RSYNC
noflags |= O_RSYNC;
needed_inheriting |= __WASI_RIGHT_FD_SYNC;
#else
noflags |= O_SYNC;
return __WASI_ENOTSUP;
#endif
needed_inheriting |= __WASI_RIGHT_FD_SYNC;
}
if ((fs_flags & __WASI_FDFLAG_SYNC) != 0) {
#ifdef CONFIG_HAS_O_SYNC
noflags |= O_SYNC;
needed_inheriting |= __WASI_RIGHT_FD_SYNC;
#else
return __WASI_ENOTSUP;
#endif
}
if (write && (noflags & (O_APPEND | O_TRUNC)) == 0)
needed_inheriting |= __WASI_RIGHT_FD_SEEK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#ifndef SSP_CONFIG_H
#define SSP_CONFIG_H

#include "bh_platform.h"
#include "gnuc.h"
#include <stdlib.h>

#if defined(__FreeBSD__) || defined(__APPLE__) \
|| (defined(ANDROID) && __ANDROID_API__ < 28)
Expand Down Expand Up @@ -101,6 +101,20 @@
#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 */
Expand Down

0 comments on commit d65c0e0

Please sign in to comment.