Skip to content

Commit 3be1ed0

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 46e72c1 commit 3be1ed0

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 {
@@ -6513,7 +6520,7 @@ pub const Stat = switch (native_os) {
65136520
},
65146521
.emscripten => emscripten.Stat,
65156522
.wasi => extern struct {
6516-
// Matches wasi-libc's libc-bottom-half/headers/public/__struct_stat.h
6523+
// Matches wasi-libc's struct stat in lib/libc/include/wasm-wasi-musl/__struct_stat.h
65176524
dev: dev_t,
65186525
ino: ino_t,
65196526
nlink: nlink_t,
@@ -7213,7 +7220,7 @@ pub const AT = switch (native_os) {
72137220
pub const RECURSIVE = 0x8000;
72147221
},
72157222
.wasi => struct {
7216-
// Match AT_* constants in wasi-libc libc-bottom-half/headers/public/__header_fcntl.h
7223+
// Match AT_* constants in lib/libc/include/wasm-wasi-musl/__header_fcntl.h
72177224
pub const FDCWD = -2;
72187225
pub const EACCESS = 0x0;
72197226
pub const SYMLINK_NOFOLLOW = 0x1;
@@ -7248,7 +7255,7 @@ pub const O = switch (native_os) {
72487255
_: u9 = 0,
72497256
},
72507257
.wasi => packed struct(u32) {
7251-
// Match layout from wasi-libc libc-bottom-half/headers/public/__header_fcntl.h
7258+
// Match O_* bits from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
72527259
APPEND: bool = false,
72537260
DSYNC: bool = false,
72547261
NONBLOCK: bool = false,

0 commit comments

Comments
 (0)