Skip to content

Commit c2eac3f

Browse files
committed
wasi c.zig: Update comments around wasi-libc constants
Point to the correct header files where the wasi-libc constants are defined that the Zig lib/std/c.zig is replicating. Zig's current copy of wasi-libc has a bug in the file-mode constants. Both S_IFIFO and S_IFSOCK are 0xc000, but S_IFIFO should be 0x1000. See WebAssembly/wasi-libc#463.
1 parent 516be96 commit c2eac3f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lib/std/c.zig

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ pub const F = switch (native_os) {
672672
.linux => linux.F,
673673
.emscripten => emscripten.F,
674674
.wasi => struct {
675+
// Match F_* constants from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
675676
pub const GETFD = 1;
676677
pub const SETFD = 2;
677678
pub const GETFL = 3;
@@ -1703,14 +1704,20 @@ pub const S = switch (native_os) {
17031704
.linux => linux.S,
17041705
.emscripten => emscripten.S,
17051706
.wasi => struct {
1706-
// Match wasi-libc's libc-bottom-half/headers/public/__mode_t.h
1707+
// Match S_* constants from lib/libc/include/wasm-wasi-musl/__mode_t.h
1708+
//
1709+
// Note, a bug in wasi-libc means both IFIFO and IFSOCK have the same value (0xc000).
1710+
// IFIFO should be 0x1000 (see https://github.com/WebAssembly/wasi-libc/pull/463), and
1711+
// 0x1000 is used by the wasi-libc bottom-half. So we use 0x1000 here. But the actual bit
1712+
// values we get back from a wasi-libc may get masked with the wrong values, or may get
1713+
// mistranslated. So the FIFO and Socket file-type bits are not trustworthy.
17071714
pub const IFBLK = 0x6000;
17081715
pub const IFCHR = 0x2000;
17091716
pub const IFDIR = 0x4000;
1710-
pub const IFIFO = 0x1000;
1717+
pub const IFIFO = 0x1000; // buggy
17111718
pub const IFLNK = 0xa000;
17121719
pub const IFREG = 0x8000;
1713-
pub const IFSOCK = 0xc000;
1720+
pub const IFSOCK = 0xc000; // buggy
17141721
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;
17151722

17161723
pub fn ISBLK(m: u32) bool {
@@ -6389,7 +6396,7 @@ pub const Stat = switch (native_os) {
63896396
},
63906397
.emscripten => emscripten.Stat,
63916398
.wasi => extern struct {
6392-
// Matches wasi-libc's libc-bottom-half/headers/public/__struct_stat.h
6399+
// Matches wasi-libc's struct stat in lib/libc/include/wasm-wasi-musl/__struct_stat.h
63936400
dev: dev_t,
63946401
ino: ino_t,
63956402
nlink: nlink_t,
@@ -7089,7 +7096,7 @@ pub const AT = switch (native_os) {
70897096
pub const RECURSIVE = 0x8000;
70907097
},
70917098
.wasi => struct {
7092-
// Match AT_* constants in wasi-libc libc-bottom-half/headers/public/__header_fcntl.h
7099+
// Match AT_* constants in lib/libc/include/wasm-wasi-musl/__header_fcntl.h
70937100
pub const FDCWD = -2;
70947101
pub const EACCESS = 0x0;
70957102
pub const SYMLINK_NOFOLLOW = 0x1;
@@ -7124,7 +7131,7 @@ pub const O = switch (native_os) {
71247131
_: u9 = 0,
71257132
},
71267133
.wasi => packed struct(u32) {
7127-
// Match layout from wasi-libc libc-bottom-half/headers/public/__header_fcntl.h
7134+
// Match O_* bits from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
71287135
APPEND: bool = false,
71297136
DSYNC: bool = false,
71307137
NONBLOCK: bool = false,

0 commit comments

Comments
 (0)