Skip to content

Commit

Permalink
add stubs for statvfs, chmod, etc. (#463)
Browse files Browse the repository at this point in the history
Per WebAssembly/wasi-sdk#373, LLVM's libc++ no longer
allows us to enable `<fstream>` and `<filesystem>` separately -- it's both or
neither.  Consequently, we either need to patch libc++ to not use `statvfs`,
`chmod`, etc. or add stub functions for those features to `wasi-libc`.  Since
we're planning to eventually support those features with WASI Preview 2 and
beyond, it makes sense to do the latter.

Note that since libc++ uses `DT_SOCK`, I've added a definition for it -- even
though WASI Preview 1 does not define it.  No Preview 1 file will ever have that
type, so code that handles that type will never be reached, but defining it
allows us to avoid WASI-specific patches to libc++.

Related to `DT_SOCK`, I had to change the `S_IFIFO` value so it does not
conflict with `S_IFSOCK`, thereby avoiding ambiguity in `__wasilibc_iftodt`.

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej authored Jan 11, 2024
1 parent 47b9db6 commit cc62fa8
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 11 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ MUSL_OMIT_HEADERS += \
"bits/shm.h" "bits/msg.h" "bits/ipc.h" "bits/ptrace.h" \
"bits/statfs.h" \
"sys/vfs.h" \
"sys/statvfs.h" \
"syslog.h" "sys/syslog.h" \
"wait.h" "sys/wait.h" \
"ucontext.h" "sys/ucontext.h" \
Expand Down
5 changes: 5 additions & 0 deletions expected/wasm32-wasi-preview2/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ cexp
cexpf
cexpl
chdir
chmod
cimag
cimagf
cimagl
Expand Down Expand Up @@ -550,6 +551,8 @@ fabs
fabsf
fabsl
faccessat
fchmod
fchmodat
fclose
fcntl
fcvt
Expand Down Expand Up @@ -701,6 +704,7 @@ fsetpos
fsetpos64
fstat
fstatat
fstatvfs
fsync
ftell
ftello
Expand Down Expand Up @@ -1121,6 +1125,7 @@ srand48
srandom
sscanf
stat
statvfs
stderr
stderr_get_stderr
stdin
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi-preview2/include-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/stropts.h>
#include <sys/syscall.h>
#include <sys/sysinfo.h>
Expand Down
18 changes: 17 additions & 1 deletion expected/wasm32-wasi-preview2/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
#define DT_FIFO __WASI_FILETYPE_SOCKET_STREAM
#define DT_LNK __WASI_FILETYPE_SYMBOLIC_LINK
#define DT_REG __WASI_FILETYPE_REGULAR_FILE
#define DT_SOCK 20
#define DT_UNKNOWN __WASI_FILETYPE_UNKNOWN
#define D_FMT 0x20029
#define D_T_FMT 0x20028
Expand Down Expand Up @@ -1731,6 +1732,18 @@
#define STRU_F 1
#define STRU_P 3
#define STRU_R 2
#define ST_APPEND 256
#define ST_IMMUTABLE 512
#define ST_MANDLOCK 64
#define ST_NOATIME 1024
#define ST_NODEV 4
#define ST_NODIRATIME 2048
#define ST_NOEXEC 8
#define ST_NOSUID 2
#define ST_RDONLY 1
#define ST_RELATIME 4096
#define ST_SYNCHRONOUS 16
#define ST_WRITE 128
#define SUN_LEN(s) (2+strlen((s)->sun_path))
#define SUSP 237
#define SYMLOOP_MAX 40
Expand All @@ -1745,7 +1758,7 @@
#define S_IFBLK (0x6000)
#define S_IFCHR (0x2000)
#define S_IFDIR (0x4000)
#define S_IFIFO (0xc000)
#define S_IFIFO (0x1000)
#define S_IFLNK (0xa000)
#define S_IFMT (S_IFBLK | S_IFCHR | S_IFDIR | S_IFIFO | S_IFLNK | S_IFREG | S_IFSOCK)
#define S_IFREG (0x8000)
Expand Down Expand Up @@ -2420,6 +2433,7 @@
#define _SYS_REG_H
#define _SYS_SELECT_H
#define _SYS_SOCKET_H
#define _SYS_STATVFS_H
#define _SYS_STAT_H
#define _SYS_SYSCALL_H
#define _SYS_SYSINFO_H
Expand Down Expand Up @@ -3244,6 +3258,7 @@
#define fsfilcnt64_t fsfilcnt_t
#define fstat64 fstat
#define fstatat64 fstatat
#define fstatvfs64 fstatvfs
#define ftello64 ftello
#define ftruncate64 ftruncate
#define getdents64 getdents
Expand Down Expand Up @@ -3427,6 +3442,7 @@
#define st_mtime st_mtim.tv_sec
#define stat64 stat
#define static_assert _Static_assert
#define statvfs64 statvfs
#define stderr (stderr)
#define stdin (stdin)
#define stdout (stdout)
Expand Down
5 changes: 5 additions & 0 deletions expected/wasm32-wasi-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ cexp
cexpf
cexpl
chdir
chmod
cimag
cimagf
cimagl
Expand Down Expand Up @@ -607,6 +608,8 @@ fabs
fabsf
fabsl
faccessat
fchmod
fchmodat
fclose
fcntl
fcvt
Expand Down Expand Up @@ -702,6 +705,7 @@ fsetpos
fsetpos64
fstat
fstatat
fstatvfs
fsync
ftell
ftello
Expand Down Expand Up @@ -1162,6 +1166,7 @@ srand48
srandom
sscanf
stat
statvfs
stderr
stdin
stdout
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi-threads/include-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/stropts.h>
#include <sys/syscall.h>
#include <sys/sysinfo.h>
Expand Down
20 changes: 18 additions & 2 deletions expected/wasm32-wasi-threads/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
#define DT_FIFO __WASI_FILETYPE_SOCKET_STREAM
#define DT_LNK __WASI_FILETYPE_SYMBOLIC_LINK
#define DT_REG __WASI_FILETYPE_REGULAR_FILE
#define DT_SOCK 20
#define DT_UNKNOWN __WASI_FILETYPE_UNKNOWN
#define D_FMT 0x20029
#define D_T_FMT 0x20028
Expand Down Expand Up @@ -1671,6 +1672,18 @@
#define STRU_F 1
#define STRU_P 3
#define STRU_R 2
#define ST_APPEND 256
#define ST_IMMUTABLE 512
#define ST_MANDLOCK 64
#define ST_NOATIME 1024
#define ST_NODEV 4
#define ST_NODIRATIME 2048
#define ST_NOEXEC 8
#define ST_NOSUID 2
#define ST_RDONLY 1
#define ST_RELATIME 4096
#define ST_SYNCHRONOUS 16
#define ST_WRITE 128
#define SUN_LEN(s) (2+strlen((s)->sun_path))
#define SUSP 237
#define SYMLOOP_MAX 40
Expand All @@ -1685,7 +1698,7 @@
#define S_IFBLK (0x6000)
#define S_IFCHR (0x2000)
#define S_IFDIR (0x4000)
#define S_IFIFO (0xc000)
#define S_IFIFO (0x1000)
#define S_IFLNK (0xa000)
#define S_IFMT (S_IFBLK | S_IFCHR | S_IFDIR | S_IFIFO | S_IFLNK | S_IFREG | S_IFSOCK)
#define S_IFREG (0x8000)
Expand Down Expand Up @@ -2052,7 +2065,7 @@
#define _Complex_I (0.0f+1.0fi)
#define _DIRENT_H
#define _DIRENT_HAVE_D_TYPE
#define _DLFCN_H
#define _DLFCN_H
#define _ENDIAN_H
#define _ERRNO_H
#define _ERR_H
Expand Down Expand Up @@ -2360,6 +2373,7 @@
#define _SYS_REG_H
#define _SYS_SELECT_H
#define _SYS_SOCKET_H
#define _SYS_STATVFS_H
#define _SYS_STAT_H
#define _SYS_SYSCALL_H
#define _SYS_SYSINFO_H
Expand Down Expand Up @@ -3185,6 +3199,7 @@
#define fsfilcnt64_t fsfilcnt_t
#define fstat64 fstat
#define fstatat64 fstatat
#define fstatvfs64 fstatvfs
#define ftello64 ftello
#define ftruncate64 ftruncate
#define getdents64 getdents
Expand Down Expand Up @@ -3371,6 +3386,7 @@
#define st_mtime st_mtim.tv_sec
#define stat64 stat
#define static_assert _Static_assert
#define statvfs64 statvfs
#define stderr (stderr)
#define stdin (stdin)
#define stdout (stdout)
Expand Down
5 changes: 5 additions & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ cexp
cexpf
cexpl
chdir
chmod
cimag
cimagf
cimagl
Expand Down Expand Up @@ -543,6 +544,8 @@ fabs
fabsf
fabsl
faccessat
fchmod
fchmodat
fclose
fcntl
fcvt
Expand Down Expand Up @@ -637,6 +640,7 @@ fsetpos
fsetpos64
fstat
fstatat
fstatvfs
fsync
ftell
ftello
Expand Down Expand Up @@ -1012,6 +1016,7 @@ srand48
srandom
sscanf
stat
statvfs
stderr
stdin
stdout
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi/include-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/stropts.h>
#include <sys/syscall.h>
#include <sys/sysinfo.h>
Expand Down
20 changes: 18 additions & 2 deletions expected/wasm32-wasi/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
#define DT_FIFO __WASI_FILETYPE_SOCKET_STREAM
#define DT_LNK __WASI_FILETYPE_SYMBOLIC_LINK
#define DT_REG __WASI_FILETYPE_REGULAR_FILE
#define DT_SOCK 20
#define DT_UNKNOWN __WASI_FILETYPE_UNKNOWN
#define D_FMT 0x20029
#define D_T_FMT 0x20028
Expand Down Expand Up @@ -1637,6 +1638,18 @@
#define STRU_F 1
#define STRU_P 3
#define STRU_R 2
#define ST_APPEND 256
#define ST_IMMUTABLE 512
#define ST_MANDLOCK 64
#define ST_NOATIME 1024
#define ST_NODEV 4
#define ST_NODIRATIME 2048
#define ST_NOEXEC 8
#define ST_NOSUID 2
#define ST_RDONLY 1
#define ST_RELATIME 4096
#define ST_SYNCHRONOUS 16
#define ST_WRITE 128
#define SUN_LEN(s) (2+strlen((s)->sun_path))
#define SUSP 237
#define SYMLOOP_MAX 40
Expand All @@ -1651,7 +1664,7 @@
#define S_IFBLK (0x6000)
#define S_IFCHR (0x2000)
#define S_IFDIR (0x4000)
#define S_IFIFO (0xc000)
#define S_IFIFO (0x1000)
#define S_IFLNK (0xa000)
#define S_IFMT (S_IFBLK | S_IFCHR | S_IFDIR | S_IFIFO | S_IFLNK | S_IFREG | S_IFSOCK)
#define S_IFREG (0x8000)
Expand Down Expand Up @@ -2018,7 +2031,7 @@
#define _Complex_I (0.0f+1.0fi)
#define _DIRENT_H
#define _DIRENT_HAVE_D_TYPE
#define _DLFCN_H
#define _DLFCN_H
#define _ENDIAN_H
#define _ERRNO_H
#define _ERR_H
Expand Down Expand Up @@ -2323,6 +2336,7 @@
#define _SYS_REG_H
#define _SYS_SELECT_H
#define _SYS_SOCKET_H
#define _SYS_STATVFS_H
#define _SYS_STAT_H
#define _SYS_SYSCALL_H
#define _SYS_SYSINFO_H
Expand Down Expand Up @@ -3146,6 +3160,7 @@
#define fsfilcnt64_t fsfilcnt_t
#define fstat64 fstat
#define fstatat64 fstatat
#define fstatvfs64 fstatvfs
#define ftello64 ftello
#define ftruncate64 ftruncate
#define getdents64 getdents
Expand Down Expand Up @@ -3329,6 +3344,7 @@
#define st_mtime st_mtim.tv_sec
#define stat64 stat
#define static_assert _Static_assert
#define statvfs64 statvfs
#define stderr (stderr)
#define stdin (stdin)
#define stdout (stdout)
Expand Down
7 changes: 7 additions & 0 deletions libc-bottom-half/headers/public/__header_dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
#define DT_REG __WASI_FILETYPE_REGULAR_FILE
#define DT_UNKNOWN __WASI_FILETYPE_UNKNOWN

// DT_SOCK is not supported in WASI Preview 1 (but will be in Preview 2). We
// define it regardless so that libc++'s `<filesystem>` implementation builds.
// The exact value is mostly arbitrary, but chosen so it doesn't conflict with
// any of the existing `__WASI_FILETYPE_*` flags. We do not expect any new
// flags to be added to WASI Preview 1, so that should be sufficient.
#define DT_SOCK 20

#define IFTODT(x) (__wasilibc_iftodt(x))
#define DTTOIF(x) (__wasilibc_dttoif(x))

Expand Down
2 changes: 1 addition & 1 deletion libc-bottom-half/headers/public/__mode_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#define S_IFLNK (0xa000)
#define S_IFREG (0x8000)
#define S_IFSOCK (0xc000)
#define S_IFIFO (0xc000)
#define S_IFIFO (0x1000)

#define S_ISBLK(m) (((m)&S_IFMT) == S_IFBLK)
#define S_ISCHR(m) (((m)&S_IFMT) == S_IFCHR)
Expand Down
Loading

0 comments on commit cc62fa8

Please sign in to comment.