Skip to content

Commit 8987432

Browse files
authored
libc-wasi: Conditionally support SYNC flags (#2581)
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.
1 parent 28ebd57 commit 8987432

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c

+22-12
Original file line numberDiff line numberDiff line change
@@ -1282,18 +1282,20 @@ wasmtime_ssp_fd_fdstat_get(wasm_exec_env_t exec_env, struct fd_table *curfds,
12821282

12831283
if ((ret & O_APPEND) != 0)
12841284
buf->fs_flags |= __WASI_FDFLAG_APPEND;
1285-
#ifdef O_DSYNC
1285+
#ifdef CONFIG_HAS_O_DSYNC
12861286
if ((ret & O_DSYNC) != 0)
12871287
buf->fs_flags |= __WASI_FDFLAG_DSYNC;
12881288
#endif
12891289
if ((ret & O_NONBLOCK) != 0)
12901290
buf->fs_flags |= __WASI_FDFLAG_NONBLOCK;
1291-
#ifdef O_RSYNC
1291+
#ifdef CONFIG_HAS_O_RSYNC
12921292
if ((ret & O_RSYNC) != 0)
12931293
buf->fs_flags |= __WASI_FDFLAG_RSYNC;
12941294
#endif
1295+
#ifdef CONFIG_HAS_O_SYNC
12951296
if ((ret & O_SYNC) != 0)
12961297
buf->fs_flags |= __WASI_FDFLAG_SYNC;
1298+
#endif
12971299
return 0;
12981300
}
12991301

@@ -1306,21 +1308,25 @@ wasmtime_ssp_fd_fdstat_set_flags(wasm_exec_env_t exec_env,
13061308
if ((fs_flags & __WASI_FDFLAG_APPEND) != 0)
13071309
noflags |= O_APPEND;
13081310
if ((fs_flags & __WASI_FDFLAG_DSYNC) != 0)
1309-
#ifdef O_DSYNC
1311+
#ifdef CONFIG_HAS_O_DSYNC
13101312
noflags |= O_DSYNC;
13111313
#else
1312-
noflags |= O_SYNC;
1314+
return __WASI_ENOTSUP;
13131315
#endif
13141316
if ((fs_flags & __WASI_FDFLAG_NONBLOCK) != 0)
13151317
noflags |= O_NONBLOCK;
13161318
if ((fs_flags & __WASI_FDFLAG_RSYNC) != 0)
1317-
#ifdef O_RSYNC
1319+
#ifdef CONFIG_HAS_O_RSYNC
13181320
noflags |= O_RSYNC;
13191321
#else
1320-
noflags |= O_SYNC;
1322+
return __WASI_ENOTSUP;
13211323
#endif
13221324
if ((fs_flags & __WASI_FDFLAG_SYNC) != 0)
1325+
#ifdef CONFIG_HAS_O_SYNC
13231326
noflags |= O_SYNC;
1327+
#else
1328+
return __WASI_ENOTSUP;
1329+
#endif
13241330

13251331
struct fd_object *fo;
13261332
__wasi_errno_t error =
@@ -1971,26 +1977,30 @@ wasmtime_ssp_path_open(wasm_exec_env_t exec_env, struct fd_table *curfds,
19711977
if ((fs_flags & __WASI_FDFLAG_APPEND) != 0)
19721978
noflags |= O_APPEND;
19731979
if ((fs_flags & __WASI_FDFLAG_DSYNC) != 0) {
1974-
#ifdef O_DSYNC
1980+
#ifdef CONFIG_HAS_O_DSYNC
19751981
noflags |= O_DSYNC;
1982+
needed_inheriting |= __WASI_RIGHT_FD_DATASYNC;
19761983
#else
1977-
noflags |= O_SYNC;
1984+
return __WASI_ENOTSUP;
19781985
#endif
1979-
needed_inheriting |= __WASI_RIGHT_FD_DATASYNC;
19801986
}
19811987
if ((fs_flags & __WASI_FDFLAG_NONBLOCK) != 0)
19821988
noflags |= O_NONBLOCK;
19831989
if ((fs_flags & __WASI_FDFLAG_RSYNC) != 0) {
1984-
#ifdef O_RSYNC
1990+
#ifdef CONFIG_HAS_O_RSYNC
19851991
noflags |= O_RSYNC;
1992+
needed_inheriting |= __WASI_RIGHT_FD_SYNC;
19861993
#else
1987-
noflags |= O_SYNC;
1994+
return __WASI_ENOTSUP;
19881995
#endif
1989-
needed_inheriting |= __WASI_RIGHT_FD_SYNC;
19901996
}
19911997
if ((fs_flags & __WASI_FDFLAG_SYNC) != 0) {
1998+
#ifdef CONFIG_HAS_O_SYNC
19921999
noflags |= O_SYNC;
19932000
needed_inheriting |= __WASI_RIGHT_FD_SYNC;
2001+
#else
2002+
return __WASI_ENOTSUP;
2003+
#endif
19942004
}
19952005
if (write && (noflags & (O_APPEND | O_TRUNC)) == 0)
19962006
needed_inheriting |= __WASI_RIGHT_FD_SEEK;

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#ifndef SSP_CONFIG_H
1515
#define SSP_CONFIG_H
1616

17+
#include "bh_platform.h"
1718
#include "gnuc.h"
18-
#include <stdlib.h>
1919

2020
#if defined(__FreeBSD__) || defined(__APPLE__) \
2121
|| (defined(ANDROID) && __ANDROID_API__ < 28)
@@ -101,6 +101,20 @@
101101
#define st_mtim st_mtimespec
102102
#endif
103103

104+
#if defined(O_DSYNC)
105+
#define CONFIG_HAS_O_DSYNC
106+
#endif
107+
108+
// POSIX requires O_RSYNC to be defined, but Linux explicitly doesn't support
109+
// it.
110+
#if defined(O_RSYNC) && !defined(__linux__)
111+
#define CONFIG_HAS_O_RSYNC
112+
#endif
113+
114+
#if defined(O_SYNC)
115+
#define CONFIG_HAS_O_SYNC
116+
#endif
117+
104118
#if !defined(BH_PLATFORM_LINUX_SGX)
105119
/* Clang's __GNUC_PREREQ macro has a different meaning than GCC one,
106120
so we have to handle this case specially */

0 commit comments

Comments
 (0)