Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearer separation of WASI POSIX support with (or without) wasi-libc #20991

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ fn runCommand(
// the `--` before the module name. This appears to work for both old and
// new Wasmtime versions.
try interp_argv.append(bin_name);
try interp_argv.append("--dir=.");
try interp_argv.append("--dir=."); // Preopen '.' at file descriptor 3 for cwd
try interp_argv.append("--");
try interp_argv.append(argv[0]);
try interp_argv.appendSlice(argv[1..]);
Expand Down
86 changes: 63 additions & 23 deletions lib/std/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ pub const dev_t = switch (native_os) {
pub const mode_t = switch (native_os) {
.linux => linux.mode_t,
.emscripten => emscripten.mode_t,
.openbsd, .haiku, .netbsd, .solaris, .illumos, .wasi => u32,
.openbsd, .haiku, .netbsd, .solaris, .illumos => u32,
.freebsd, .macos, .ios, .tvos, .watchos, .visionos => u16,
else => u0,
.wasi, .windows => void,
else => void,
};

pub const nlink_t = switch (native_os) {
Expand Down Expand Up @@ -671,6 +672,7 @@ pub const F = switch (native_os) {
.linux => linux.F,
.emscripten => emscripten.F,
.wasi => struct {
// Match F_* constants from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const GETFD = 1;
pub const SETFD = 2;
pub const GETFL = 3;
Expand Down Expand Up @@ -1702,17 +1704,49 @@ pub const S = switch (native_os) {
.linux => linux.S,
.emscripten => emscripten.S,
.wasi => struct {
pub const IEXEC = @compileError("TODO audit this");
// Match S_* constants from lib/libc/include/wasm-wasi-musl/__mode_t.h
//
// Note, a bug in wasi-libc means both IFIFO and IFSOCK have the same value (0xc000).
// IFIFO should be 0x1000 (see https://github.com/WebAssembly/wasi-libc/pull/463), and
// 0x1000 is used by the wasi-libc bottom-half. So we use 0x1000 here. But the actual bit
// values we get back from a wasi-libc may get masked with the wrong values, or may get
// mistranslated. So the FIFO and Socket file-type bits are not trustworthy.
pub const IFBLK = 0x6000;
pub const IFCHR = 0x2000;
pub const IFDIR = 0x4000;
pub const IFIFO = 0xc000;
pub const IFIFO = 0x1000; // buggy
pub const IFLNK = 0xa000;
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;
pub const IFREG = 0x8000;
/// There's no concept of UNIX domain socket but we define this value here
/// in order to line with other OSes.
pub const IFSOCK = 0x1;
pub const IFSOCK = 0xc000; // buggy
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;

pub fn ISBLK(m: u32) bool {
return m & IFMT == IFBLK;
}

pub fn ISCHR(m: u32) bool {
return m & IFMT == IFCHR;
}

pub fn ISDIR(m: u32) bool {
return m & IFMT == IFDIR;
}

pub fn ISFIFO(m: u32) bool {
return m & IFMT == IFIFO;
}

pub fn ISLNK(m: u32) bool {
return m & IFMT == IFLNK;
}

pub fn ISREG(m: u32) bool {
return m & IFMT == IFREG;
}

pub fn ISSOCK(m: u32) bool {
return m & IFMT == IFSOCK;
}
},
.macos, .ios, .tvos, .watchos, .visionos => struct {
pub const IFMT = 0o170000;
Expand Down Expand Up @@ -6486,10 +6520,11 @@ pub const Stat = switch (native_os) {
},
.emscripten => emscripten.Stat,
.wasi => extern struct {
// Matches wasi-libc's struct stat in lib/libc/include/wasm-wasi-musl/__struct_stat.h
dev: dev_t,
ino: ino_t,
nlink: nlink_t,
mode: mode_t,
mode: c_uint, // only the file-type bits (S.IFMT), no permission bits
uid: uid_t,
gid: gid_t,
__pad0: c_uint = 0,
Expand Down Expand Up @@ -7185,17 +7220,13 @@ pub const AT = switch (native_os) {
pub const RECURSIVE = 0x8000;
},
.wasi => struct {
pub const SYMLINK_NOFOLLOW = 0x100;
pub const SYMLINK_FOLLOW = 0x400;
pub const REMOVEDIR: u32 = 0x4;
/// When linking libc, we follow their convention and use -2 for current working directory.
/// However, without libc, Zig does a different convention: it assumes the
/// current working directory is the first preopen. This behavior can be
/// overridden with a public function called `wasi_cwd` in the root source
/// file.
pub const FDCWD: fd_t = if (builtin.link_libc) -2 else 3;
// Match AT_* constants in lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const FDCWD = -2;
pub const EACCESS = 0x0;
pub const SYMLINK_NOFOLLOW = 0x1;
pub const SYMLINK_FOLLOW = 0x2;
pub const REMOVEDIR = 0x4;
},

else => void,
};

Expand Down Expand Up @@ -7224,6 +7255,7 @@ pub const O = switch (native_os) {
_: u9 = 0,
},
.wasi => packed struct(u32) {
// Match O_* bits from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
APPEND: bool = false,
DSYNC: bool = false,
NONBLOCK: bool = false,
Expand All @@ -7236,10 +7268,17 @@ pub const O = switch (native_os) {
TRUNC: bool = false,
_16: u8 = 0,
NOFOLLOW: bool = false,

// Logical O_ACCMODE is the following 4 bits, note O_SEARCH is between RD and WR,
// so can't easily reuse std.posix.ACCMODE in this struct.
EXEC: bool = false,
read: bool = false,
RDONLY: bool = true, // equivalent to "ACCMODE = .RDONLY" default
SEARCH: bool = false,
write: bool = false,
WRONLY: bool = false,

// CLOEXEC, TTY_ININT, NOCTTY are mapped 0, so they're silently
// ignored in C code.

_: u3 = 0,
},
.solaris, .illumos => packed struct(u32) {
Expand Down Expand Up @@ -9069,6 +9108,7 @@ pub const gettimeofday = switch (native_os) {

pub const msync = switch (native_os) {
.netbsd => private.__msync13,
.wasi => {},
else => private.msync,
};

Expand Down Expand Up @@ -9211,8 +9251,8 @@ pub const fork = switch (native_os) {
pub extern "c" fn access(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn faccessat(dirfd: fd_t, path: [*:0]const u8, mode: c_uint, flags: c_uint) c_int;
pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
pub extern "c" fn symlinkat(oldpath: [*:0]const u8, newdirfd: fd_t, newpath: [*:0]const u8) c_int;
pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;
Expand Down
19 changes: 12 additions & 7 deletions lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1017,12 +1017,12 @@ test printLineFromFileAnyOs {

var test_dir = std.testing.tmpDir(.{});
defer test_dir.cleanup();
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths.
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths relative to cwd.
const test_dir_path = try join(allocator, &.{ ".zig-cache", "tmp", test_dir.sub_path[0..] });
defer allocator.free(test_dir_path);

// Cases
{
// no newlines in file
const path = try join(allocator, &.{ test_dir_path, "one_line.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{ .sub_path = "one_line.zig", .data = "no new lines in this file, but one is printed anyway" });
Expand All @@ -1034,7 +1034,8 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
const path = try fs.path.join(allocator, &.{ test_dir_path, "three_lines.zig" });
// print 1 & 3 of 3-line file
const path = try join(allocator, &.{ test_dir_path, "three_lines.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{
.sub_path = "three_lines.zig",
Expand All @@ -1054,9 +1055,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size boundary crossing line
const file = try test_dir.dir.createFile("line_overlaps_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
defer allocator.free(path);

const overlap = 10;
Expand All @@ -1070,9 +1072,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// ends on mem.page_size boundary
const file = try test_dir.dir.createFile("file_ends_on_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All @@ -1083,9 +1086,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// multi-mem.page_size "line"
const file = try test_dir.dir.createFile("very_long_first_line_spanning_multiple_pages.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
const path = try join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All @@ -1108,9 +1112,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size pages of newlines
const file = try test_dir.dir.createFile("file_of_newlines.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand Down
Loading