Open squeek502 opened 3 months ago
I've got a change with a bunch of WASI with-or-without-libc fixes coming soon, I'll see if I can fold this in.
I think the problem is that AT_SYMLINK_NOFOLLOW
should be 0x1 on wasi-with-libc, but its 0x100 in Zig? I'm not entirely sure where wasi-libc defines these constants, but I see an 0x1 in here: https://github.com/WebAssembly/wasi-libc/blob/230d4be6c54bec93181050f9e25c87150506bdd0/expected/wasm32-wasip1/predefined-macros.txt#L52 (I see the 0x100 over here, but I think that's a vanilla musl header?)
My test show the 0x1 works, which bolsters my case, but I'm curious to know what the actual source of truth for these constants is. ..
This might be relevant if Zig is calling into the "bottom half" of wasi-libc:
https://github.com/WebAssembly/wasi-libc/blob/main/libc-bottom-half/headers/public/__header_fcntl.h#L55 https://github.com/WebAssembly/wasi-libc/tree/main/libc-bottom-half
I believe I've found the header that defines the constants that wasi-libc expects its clients to use: lib/libc/include/wasm-wasi-musl/__header_fcntl.h. (This header is included by fcntl.h
in the same directory and via a bunch of indirect pre-processing, overrides the default constants defined in fcntl.h, so someone who just glances at fcntl.h might see the wrong constants.)
The directory is included by wasm_libc.zig as the sysroot (the triple
is wasm-wasi-musl):
"-iwithsysroot",
try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libc", "include", triple }),
The "bottom-half" headers I linked to earlier, as I understand it, are for the wasi-libc implementation to talk to WASI APIs, so its not directly relevant here -- though it seems like many of the constants are defined to match across the two layers.
So, according to this header AT_SYMLINK_{FOLLOW,NOFOLLOW}
should be 0x2 and 0x1 respectively.
I have fixes for this in #20991 (along with some testcases that exercise these values).
If you have a pure C compilation environment set up for WASI, you might trying printf
ing these various constants to see which values they map to?
Zig Version
0.14.0-dev.653+91c17979f
Steps to Reproduce and Observed Behavior
This test passes normally and when targeting
wasm32-wasi
and not linking libc:But fails when linking libc (meaning
wasi-libc
):(that mode is
IFREG
aka a file)This is not a problem with the parameters being passed to
fstatat
: I confirmed that theAT_SYMLINK_NOFOLLOW
is being passed to thefstatat_sym
call here:https://github.com/ziglang/zig/blob/059856acfc9f87d723a90af6a4214e128b8cae2e/lib/std/posix.zig#L4382
But
strace
shows that theNOFOLLOW
flag is being dropped when going throughwasmtime
(theopenat2
call should haveO_NOFOLLOW
, which it does when not linkingwasi-libc
):My first thought was that this is a
wasi-libc
/wasmtime
bug, similar to https://github.com/ziglang/zig/issues/20747, but I have been unable to find a reproduction when building an intended-to-be-equivalent C version viawasi-sdk
. Here's the program I'm trying (I'm usingwasi-sdk 23.0
andwasmtime 23.0.1
):Built with:
And run with:
(0xA000 is S_IFLNK aka symlink)
Expected Behavior
The test to pass when linking
wasi-libc