authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-13 21:56:32+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-08-13 21:56:32+02:00
log1373df4c34436b18b570f5b0f1be14da63966557
treefb53a6071543007e904ff8a186e0c1bd3a029377
parentc53423f8aac8de49e700ba6eb89ccac462ed2212
parent68617c9fb05ad7e92315edb0dec7c5fad56b6265
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9227 from mathetake/libc-wasi-test

WASI,libc: fix libstd with wasi-libc linkage, and enable tests.

16 files changed, 223 insertions(+), 86 deletions(-)

lib/std/Thread.zig+1-1
......@@ -24,7 +24,7 @@ pub const Condition = @import("Thread/Condition.zig");
2424
2525pub const spinLoopHint = @compileError("deprecated: use std.atomic.spinLoopHint");
2626
27pub const use_pthreads = target.os.tag != .windows and std.builtin.link_libc;
27pub const use_pthreads = target.os.tag != .windows and std.Target.current.os.tag != .wasi and std.builtin.link_libc;
2828
2929const Thread = @This();
3030const Impl = if (target.os.tag == .windows)
lib/std/c.zig+1
......@@ -31,6 +31,7 @@ pub usingnamespace switch (std.Target.current.os.tag) {
3131 .fuchsia => @import("c/fuchsia.zig"),
3232 .minix => @import("c/minix.zig"),
3333 .emscripten => @import("c/emscripten.zig"),
34 .wasi => @import("c/wasi.zig"),
3435 else => struct {},
3536};
3637
lib/std/c/wasi.zig created+60
......@@ -0,0 +1,60 @@
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2015-2021 Zig Contributors
3// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.
6usingnamespace @import("../os/bits.zig");
7
8extern threadlocal var errno: c_int;
9
10pub fn _errno() *c_int {
11 return &errno;
12}
13
14pub const pid_t = c_int;
15pub const uid_t = u32;
16pub const gid_t = u32;
17pub const off_t = i64;
18
19pub const libc_stat = extern struct {
20 dev: i32,
21 ino: ino_t,
22 nlink: u64,
23
24 mode: mode_t,
25 uid: uid_t,
26 gid: gid_t,
27 __pad0: isize,
28 rdev: i32,
29 size: off_t,
30 blksize: i32,
31 blocks: i64,
32
33 atimesec: time_t,
34 atimensec: isize,
35 mtimesec: time_t,
36 mtimensec: isize,
37 ctimesec: time_t,
38 ctimensec: isize,
39
40 pub fn atime(self: @This()) timespec {
41 return timespec{
42 .tv_sec = self.atimesec,
43 .tv_nsec = self.atimensec,
44 };
45 }
46
47 pub fn mtime(self: @This()) timespec {
48 return timespec{
49 .tv_sec = self.mtimesec,
50 .tv_nsec = self.mtimensec,
51 };
52 }
53
54 pub fn ctime(self: @This()) timespec {
55 return timespec{
56 .tv_sec = self.ctimesec,
57 .tv_nsec = self.ctimensec,
58 };
59 }
60};
lib/std/fs.zig+35-24
......@@ -647,6 +647,9 @@ pub const Dir = struct {
647647 /// Memory such as file names referenced in this returned entry becomes invalid
648648 /// with subsequent calls to `next`, as well as when this `Dir` is deinitialized.
649649 pub fn next(self: *Self) Error!?Entry {
650 // We intentinally use fd_readdir even when linked with libc,
651 // since its implementation is exactly the same as below,
652 // and we avoid the code complexity here.
650653 const w = os.wasi;
651654 start_over: while (true) {
652655 if (self.index >= self.end_index) {
......@@ -858,7 +861,7 @@ pub const Dir = struct {
858861 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
859862 return self.openFileW(path_w.span(), flags);
860863 }
861 if (builtin.os.tag == .wasi) {
864 if (builtin.os.tag == .wasi and !builtin.link_libc) {
862865 return self.openFileWasi(sub_path, flags);
863866 }
864867 const path_c = try os.toPosixPath(sub_path);
......@@ -934,14 +937,18 @@ pub const Dir = struct {
934937 try os.openatZ(self.fd, sub_path, os_flags, 0);
935938 errdefer os.close(fd);
936939
937 if (!has_flock_open_flags and flags.lock != .None) {
938 // TODO: integrate async I/O
939 const lock_nonblocking = if (flags.lock_nonblocking) os.LOCK_NB else @as(i32, 0);
940 try os.flock(fd, switch (flags.lock) {
941 .None => unreachable,
942 .Shared => os.LOCK_SH | lock_nonblocking,
943 .Exclusive => os.LOCK_EX | lock_nonblocking,
944 });
940 // WASI doesn't have os.flock so we intetinally check OS prior to the inner if block
941 // since it is not compiltime-known and we need to avoid undefined symbol in Wasm.
942 if (builtin.target.os.tag != .wasi) {
943 if (!has_flock_open_flags and flags.lock != .None) {
944 // TODO: integrate async I/O
945 const lock_nonblocking = if (flags.lock_nonblocking) os.LOCK_NB else @as(i32, 0);
946 try os.flock(fd, switch (flags.lock) {
947 .None => unreachable,
948 .Shared => os.LOCK_SH | lock_nonblocking,
949 .Exclusive => os.LOCK_EX | lock_nonblocking,
950 });
951 }
945952 }
946953
947954 if (has_flock_open_flags and flags.lock_nonblocking) {
......@@ -1014,7 +1021,7 @@ pub const Dir = struct {
10141021 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
10151022 return self.createFileW(path_w.span(), flags);
10161023 }
1017 if (builtin.os.tag == .wasi) {
1024 if (builtin.os.tag == .wasi and !builtin.link_libc) {
10181025 return self.createFileWasi(sub_path, flags);
10191026 }
10201027 const path_c = try os.toPosixPath(sub_path);
......@@ -1084,14 +1091,18 @@ pub const Dir = struct {
10841091 try os.openatZ(self.fd, sub_path_c, os_flags, flags.mode);
10851092 errdefer os.close(fd);
10861093
1087 if (!has_flock_open_flags and flags.lock != .None) {
1088 // TODO: integrate async I/O
1089 const lock_nonblocking = if (flags.lock_nonblocking) os.LOCK_NB else @as(i32, 0);
1090 try os.flock(fd, switch (flags.lock) {
1091 .None => unreachable,
1092 .Shared => os.LOCK_SH | lock_nonblocking,
1093 .Exclusive => os.LOCK_EX | lock_nonblocking,
1094 });
1094 // WASI doesn't have os.flock so we intetinally check OS prior to the inner if block
1095 // since it is not compiltime-known and we need to avoid undefined symbol in Wasm.
1096 if (builtin.target.os.tag != .wasi) {
1097 if (!has_flock_open_flags and flags.lock != .None) {
1098 // TODO: integrate async I/O
1099 const lock_nonblocking = if (flags.lock_nonblocking) os.LOCK_NB else @as(i32, 0);
1100 try os.flock(fd, switch (flags.lock) {
1101 .None => unreachable,
1102 .Shared => os.LOCK_SH | lock_nonblocking,
1103 .Exclusive => os.LOCK_EX | lock_nonblocking,
1104 });
1105 }
10951106 }
10961107
10971108 if (has_flock_open_flags and flags.lock_nonblocking) {
......@@ -1372,7 +1383,7 @@ pub const Dir = struct {
13721383 if (builtin.os.tag == .windows) {
13731384 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
13741385 return self.openDirW(sub_path_w.span().ptr, args);
1375 } else if (builtin.os.tag == .wasi) {
1386 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
13761387 return self.openDirWasi(sub_path, args);
13771388 } else {
13781389 const sub_path_c = try os.toPosixPath(sub_path);
......@@ -1519,7 +1530,7 @@ pub const Dir = struct {
15191530 if (builtin.os.tag == .windows) {
15201531 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
15211532 return self.deleteFileW(sub_path_w.span());
1522 } else if (builtin.os.tag == .wasi) {
1533 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
15231534 os.unlinkatWasi(self.fd, sub_path, 0) catch |err| switch (err) {
15241535 error.DirNotEmpty => unreachable, // not passing AT_REMOVEDIR
15251536 else => |e| return e,
......@@ -1582,7 +1593,7 @@ pub const Dir = struct {
15821593 if (builtin.os.tag == .windows) {
15831594 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
15841595 return self.deleteDirW(sub_path_w.span());
1585 } else if (builtin.os.tag == .wasi) {
1596 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
15861597 os.unlinkat(self.fd, sub_path, os.AT_REMOVEDIR) catch |err| switch (err) {
15871598 error.IsDir => unreachable, // not possible since we pass AT_REMOVEDIR
15881599 else => |e| return e,
......@@ -1641,7 +1652,7 @@ pub const Dir = struct {
16411652 sym_link_path: []const u8,
16421653 flags: SymLinkFlags,
16431654 ) !void {
1644 if (builtin.os.tag == .wasi) {
1655 if (builtin.os.tag == .wasi and !builtin.link_libc) {
16451656 return self.symLinkWasi(target_path, sym_link_path, flags);
16461657 }
16471658 if (builtin.os.tag == .windows) {
......@@ -1694,7 +1705,7 @@ pub const Dir = struct {
16941705 /// The return value is a slice of `buffer`, from index `0`.
16951706 /// Asserts that the path parameter has no null bytes.
16961707 pub fn readLink(self: Dir, sub_path: []const u8, buffer: []u8) ![]u8 {
1697 if (builtin.os.tag == .wasi) {
1708 if (builtin.os.tag == .wasi and !builtin.link_libc) {
16981709 return self.readLinkWasi(sub_path, buffer);
16991710 }
17001711 if (builtin.os.tag == .windows) {
......@@ -2113,7 +2124,7 @@ pub const Dir = struct {
21132124pub fn cwd() Dir {
21142125 if (builtin.os.tag == .windows) {
21152126 return Dir{ .fd = os.windows.peb().ProcessParameters.CurrentDirectory.Handle };
2116 } else if (builtin.os.tag == .wasi) {
2127 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
21172128 @compileError("WASI doesn't have a concept of cwd(); use std.fs.wasi.PreopenList to get available Dir handles instead");
21182129 } else {
21192130 return Dir{ .fd = os.AT_FDCWD };
lib/std/fs/file.zig+17-20
......@@ -326,26 +326,23 @@ pub const File = struct {
326326 .inode = st.ino,
327327 .size = @bitCast(u64, st.size),
328328 .mode = st.mode,
329 .kind = switch (builtin.os.tag) {
330 .wasi => switch (st.filetype) {
331 os.FILETYPE_BLOCK_DEVICE => Kind.BlockDevice,
332 os.FILETYPE_CHARACTER_DEVICE => Kind.CharacterDevice,
333 os.FILETYPE_DIRECTORY => Kind.Directory,
334 os.FILETYPE_SYMBOLIC_LINK => Kind.SymLink,
335 os.FILETYPE_REGULAR_FILE => Kind.File,
336 os.FILETYPE_SOCKET_STREAM, os.FILETYPE_SOCKET_DGRAM => Kind.UnixDomainSocket,
337 else => Kind.Unknown,
338 },
339 else => switch (st.mode & os.S_IFMT) {
340 os.S_IFBLK => Kind.BlockDevice,
341 os.S_IFCHR => Kind.CharacterDevice,
342 os.S_IFDIR => Kind.Directory,
343 os.S_IFIFO => Kind.NamedPipe,
344 os.S_IFLNK => Kind.SymLink,
345 os.S_IFREG => Kind.File,
346 os.S_IFSOCK => Kind.UnixDomainSocket,
347 else => Kind.Unknown,
348 },
329 .kind = if (builtin.os.tag == .wasi and !builtin.link_libc) switch (st.filetype) {
330 os.FILETYPE_BLOCK_DEVICE => Kind.BlockDevice,
331 os.FILETYPE_CHARACTER_DEVICE => Kind.CharacterDevice,
332 os.FILETYPE_DIRECTORY => Kind.Directory,
333 os.FILETYPE_SYMBOLIC_LINK => Kind.SymLink,
334 os.FILETYPE_REGULAR_FILE => Kind.File,
335 os.FILETYPE_SOCKET_STREAM, os.FILETYPE_SOCKET_DGRAM => Kind.UnixDomainSocket,
336 else => Kind.Unknown,
337 } else switch (st.mode & os.S_IFMT) {
338 os.S_IFBLK => Kind.BlockDevice,
339 os.S_IFCHR => Kind.CharacterDevice,
340 os.S_IFDIR => Kind.Directory,
341 os.S_IFIFO => Kind.NamedPipe,
342 os.S_IFLNK => Kind.SymLink,
343 os.S_IFREG => Kind.File,
344 os.S_IFSOCK => Kind.UnixDomainSocket,
345 else => Kind.Unknown,
349346 },
350347 .atime = @as(i128, atime.tv_sec) * std.time.ns_per_s + atime.tv_nsec,
351348 .mtime = @as(i128, mtime.tv_sec) * std.time.ns_per_s + mtime.tv_nsec,
lib/std/fs/wasi.zig+1-1
......@@ -169,7 +169,7 @@ pub const PreopenList = struct {
169169};
170170
171171test "extracting WASI preopens" {
172 if (std.builtin.os.tag != .wasi) return error.SkipZigTest;
172 if (std.builtin.os.tag != .wasi or std.builtin.link_libc) return error.SkipZigTest;
173173
174174 var preopens = PreopenList.init(std.testing.allocator);
175175 defer preopens.deinit();
lib/std/io/c_writer.zig+1-1
......@@ -35,7 +35,7 @@ fn cWriterWrite(c_file: *std.c.FILE, bytes: []const u8) std.fs.File.WriteError!u
3535}
3636
3737test {
38 if (!builtin.link_libc) return error.SkipZigTest;
38 if (!builtin.link_libc or builtin.os.tag == .wasi) return error.SkipZigTest;
3939
4040 const filename = "tmp_io_test_file.txt";
4141 const out_file = std.c.fopen(filename, "w") orelse return error.UnableToOpenTestFile;
lib/std/os.zig+25-22
......@@ -404,7 +404,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
404404 const first = iov[0];
405405 return read(fd, first.iov_base[0..first.iov_len]);
406406 }
407 if (builtin.os.tag == .wasi) {
407 if (builtin.os.tag == .wasi and !builtin.link_libc) {
408408 var nread: usize = undefined;
409409 switch (wasi.fd_read(fd, iov.ptr, iov.len, &nread)) {
410410 wasi.ESUCCESS => return nread,
......@@ -461,7 +461,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
461461 if (builtin.os.tag == .windows) {
462462 return windows.ReadFile(fd, buf, offset, std.io.default_mode);
463463 }
464 if (builtin.os.tag == .wasi) {
464 if (builtin.os.tag == .wasi and !builtin.link_libc) {
465465 const iovs = [1]iovec{iovec{
466466 .iov_base = buf.ptr,
467467 .iov_len = buf.len,
......@@ -556,7 +556,7 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
556556 else => return windows.unexpectedStatus(rc),
557557 }
558558 }
559 if (std.Target.current.os.tag == .wasi) {
559 if (std.Target.current.os.tag == .wasi and !builtin.link_libc) {
560560 switch (wasi.fd_filestat_set_size(fd, length)) {
561561 wasi.ESUCCESS => return,
562562 wasi.EINTR => unreachable,
......@@ -617,7 +617,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
617617 const first = iov[0];
618618 return pread(fd, first.iov_base[0..first.iov_len], offset);
619619 }
620 if (builtin.os.tag == .wasi) {
620 if (builtin.os.tag == .wasi and !builtin.link_libc) {
621621 var nread: usize = undefined;
622622 switch (wasi.fd_pread(fd, iov.ptr, iov.len, offset, &nread)) {
623623 wasi.ESUCCESS => return nread,
......@@ -795,7 +795,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
795795 const first = iov[0];
796796 return write(fd, first.iov_base[0..first.iov_len]);
797797 }
798 if (builtin.os.tag == .wasi) {
798 if (builtin.os.tag == .wasi and !builtin.link_libc) {
799799 var nwritten: usize = undefined;
800800 switch (wasi.fd_write(fd, iov.ptr, iov.len, &nwritten)) {
801801 wasi.ESUCCESS => return nwritten,
......@@ -867,7 +867,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
867867 if (std.Target.current.os.tag == .windows) {
868868 return windows.WriteFile(fd, bytes, offset, std.io.default_mode);
869869 }
870 if (builtin.os.tag == .wasi) {
870 if (builtin.os.tag == .wasi and !builtin.link_libc) {
871871 const ciovs = [1]iovec_const{iovec_const{
872872 .iov_base = bytes.ptr,
873873 .iov_len = bytes.len,
......@@ -968,7 +968,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
968968 const first = iov[0];
969969 return pwrite(fd, first.iov_base[0..first.iov_len], offset);
970970 }
971 if (builtin.os.tag == .wasi) {
971 if (builtin.os.tag == .wasi and !builtin.link_libc) {
972972 var nwritten: usize = undefined;
973973 switch (wasi.fd_pwrite(fd, iov.ptr, iov.len, offset, &nwritten)) {
974974 wasi.ESUCCESS => return nwritten,
......@@ -1627,7 +1627,7 @@ pub fn symlinkZ(target_path: [*:0]const u8, sym_link_path: [*:0]const u8) SymLin
16271627/// If `sym_link_path` exists, it will not be overwritten.
16281628/// See also `symlinkatWasi`, `symlinkatZ` and `symlinkatW`.
16291629pub fn symlinkat(target_path: []const u8, newdirfd: fd_t, sym_link_path: []const u8) SymLinkError!void {
1630 if (builtin.os.tag == .wasi) {
1630 if (builtin.os.tag == .wasi and !builtin.link_libc) {
16311631 return symlinkatWasi(target_path, newdirfd, sym_link_path);
16321632 }
16331633 if (builtin.os.tag == .windows) {
......@@ -1856,7 +1856,7 @@ pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!vo
18561856 if (builtin.os.tag == .windows) {
18571857 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
18581858 return unlinkatW(dirfd, file_path_w.span(), flags);
1859 } else if (builtin.os.tag == .wasi) {
1859 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
18601860 return unlinkatWasi(dirfd, file_path, flags);
18611861 } else {
18621862 const file_path_c = try toPosixPath(file_path);
......@@ -2023,7 +2023,7 @@ pub fn renameat(
20232023 const old_path_w = try windows.sliceToPrefixedFileW(old_path);
20242024 const new_path_w = try windows.sliceToPrefixedFileW(new_path);
20252025 return renameatW(old_dir_fd, old_path_w.span(), new_dir_fd, new_path_w.span(), windows.TRUE);
2026 } else if (builtin.os.tag == .wasi) {
2026 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
20272027 return renameatWasi(old_dir_fd, old_path, new_dir_fd, new_path);
20282028 } else {
20292029 const old_path_c = try toPosixPath(old_path);
......@@ -2159,7 +2159,7 @@ pub fn mkdirat(dir_fd: fd_t, sub_dir_path: []const u8, mode: u32) MakeDirError!v
21592159 if (builtin.os.tag == .windows) {
21602160 const sub_dir_path_w = try windows.sliceToPrefixedFileW(sub_dir_path);
21612161 return mkdiratW(dir_fd, sub_dir_path_w.span(), mode);
2162 } else if (builtin.os.tag == .wasi) {
2162 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
21632163 return mkdiratWasi(dir_fd, sub_dir_path, mode);
21642164 } else {
21652165 const sub_dir_path_c = try toPosixPath(sub_dir_path);
......@@ -2519,7 +2519,7 @@ pub fn readlinkZ(file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8
25192519/// The return value is a slice of `out_buffer` from index 0.
25202520/// See also `readlinkatWasi`, `realinkatZ` and `realinkatW`.
25212521pub fn readlinkat(dirfd: fd_t, file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
2522 if (builtin.os.tag == .wasi) {
2522 if (builtin.os.tag == .wasi and !builtin.link_libc) {
25232523 return readlinkatWasi(dirfd, file_path, out_buffer);
25242524 }
25252525 if (builtin.os.tag == .windows) {
......@@ -3481,7 +3481,7 @@ pub const FStatError = error{
34813481
34823482/// Return information about a file descriptor.
34833483pub fn fstat(fd: fd_t) FStatError!Stat {
3484 if (builtin.os.tag == .wasi) {
3484 if (builtin.os.tag == .wasi and !builtin.link_libc) {
34853485 var stat: wasi.filestat_t = undefined;
34863486 switch (wasi.fd_filestat_get(fd, &stat)) {
34873487 wasi.ESUCCESS => return Stat.fromFilestat(stat),
......@@ -3519,7 +3519,7 @@ pub const FStatAtError = FStatError || error{ NameTooLong, FileNotFound, SymLink
35193519/// which is relative to `dirfd` handle.
35203520/// See also `fstatatZ` and `fstatatWasi`.
35213521pub fn fstatat(dirfd: fd_t, pathname: []const u8, flags: u32) FStatAtError!Stat {
3522 if (builtin.os.tag == .wasi) {
3522 if (builtin.os.tag == .wasi and !builtin.link_libc) {
35233523 return fstatatWasi(dirfd, pathname, flags);
35243524 } else if (builtin.os.tag == .windows) {
35253525 @compileError("fstatat is not yet implemented on Windows");
......@@ -4123,7 +4123,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
41234123 if (builtin.os.tag == .windows) {
41244124 return windows.SetFilePointerEx_BEGIN(fd, offset);
41254125 }
4126 if (builtin.os.tag == .wasi) {
4126 if (builtin.os.tag == .wasi and !builtin.link_libc) {
41274127 var new_offset: wasi.filesize_t = undefined;
41284128 switch (wasi.fd_seek(fd, @bitCast(wasi.filedelta_t, offset), wasi.WHENCE_SET, &new_offset)) {
41294129 wasi.ESUCCESS => return,
......@@ -4171,7 +4171,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
41714171 if (builtin.os.tag == .windows) {
41724172 return windows.SetFilePointerEx_CURRENT(fd, offset);
41734173 }
4174 if (builtin.os.tag == .wasi) {
4174 if (builtin.os.tag == .wasi and !builtin.link_libc) {
41754175 var new_offset: wasi.filesize_t = undefined;
41764176 switch (wasi.fd_seek(fd, offset, wasi.WHENCE_CUR, &new_offset)) {
41774177 wasi.ESUCCESS => return,
......@@ -4218,7 +4218,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
42184218 if (builtin.os.tag == .windows) {
42194219 return windows.SetFilePointerEx_END(fd, offset);
42204220 }
4221 if (builtin.os.tag == .wasi) {
4221 if (builtin.os.tag == .wasi and !builtin.link_libc) {
42224222 var new_offset: wasi.filesize_t = undefined;
42234223 switch (wasi.fd_seek(fd, offset, wasi.WHENCE_END, &new_offset)) {
42244224 wasi.ESUCCESS => return,
......@@ -4265,7 +4265,7 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
42654265 if (builtin.os.tag == .windows) {
42664266 return windows.SetFilePointerEx_CURRENT_get(fd);
42674267 }
4268 if (builtin.os.tag == .wasi) {
4268 if (builtin.os.tag == .wasi and !builtin.link_libc) {
42694269 var new_offset: wasi.filesize_t = undefined;
42704270 switch (wasi.fd_seek(fd, 0, wasi.WHENCE_CUR, &new_offset)) {
42714271 wasi.ESUCCESS => return new_offset,
......@@ -4665,7 +4665,7 @@ pub const ClockGetTimeError = error{UnsupportedClock} || UnexpectedError;
46654665/// TODO: change this to return the timespec as a return value
46664666/// TODO: look into making clk_id an enum
46674667pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
4668 if (std.Target.current.os.tag == .wasi) {
4668 if (std.Target.current.os.tag == .wasi and !builtin.link_libc) {
46694669 var ts: timestamp_t = undefined;
46704670 switch (system.clock_time_get(@bitCast(u32, clk_id), 1, &ts)) {
46714671 0 => {
......@@ -4706,7 +4706,7 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
47064706}
47074707
47084708pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {
4709 if (std.Target.current.os.tag == .wasi) {
4709 if (std.Target.current.os.tag == .wasi and !builtin.link_libc) {
47104710 var ts: timestamp_t = undefined;
47114711 switch (system.clock_res_get(@bitCast(u32, clk_id), &ts)) {
47124712 0 => res.* = .{
......@@ -4834,7 +4834,7 @@ pub const FutimensError = error{
48344834} || UnexpectedError;
48354835
48364836pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void {
4837 if (builtin.os.tag == .wasi) {
4837 if (builtin.os.tag == .wasi and !builtin.link_libc) {
48384838 // TODO WASI encodes `wasi.fstflags` to signify magic values
48394839 // similar to UTIME_NOW and UTIME_OMIT. Currently, we ignore
48404840 // this here, but we should really handle it somehow.
......@@ -5581,7 +5581,10 @@ var has_copy_file_range_syscall = std.atomic.Atomic(bool).init(true);
55815581///
55825582/// Maximum offsets on Linux are `math.maxInt(i64)`.
55835583pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len: usize, flags: u32) CopyFileRangeError!usize {
5584 const call_cfr = comptime if (builtin.link_libc)
5584 const call_cfr = comptime if (std.Target.current.os.tag == .wasi)
5585 // WASI-libc doesn't have copy_file_range.
5586 false
5587 else if (builtin.link_libc)
55855588 std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok
55865589 else
55875590 std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true;
lib/std/os/bits/wasi.zig+55-8
......@@ -4,6 +4,7 @@
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
66// Convenience types and consts used by std.os module
7const builtin = @import("builtin");
78const posix = @import("posix.zig");
89pub const iovec = posix.iovec;
910pub const iovec_const = posix.iovec_const;
......@@ -12,7 +13,7 @@ pub const STDIN_FILENO = 0;
1213pub const STDOUT_FILENO = 1;
1314pub const STDERR_FILENO = 2;
1415
15pub const mode_t = u0;
16pub const mode_t = u32;
1617
1718pub const time_t = i64; // match https://github.com/CraneStation/wasi-libc
1819
......@@ -75,7 +76,8 @@ pub const kernel_stat = struct {
7576 }
7677};
7778
78pub const AT_REMOVEDIR: u32 = 1; // there's no AT_REMOVEDIR in WASI, but we simulate here to match other OSes
79pub const AT_REMOVEDIR: u32 = 0x4;
80pub const AT_FDCWD: fd_t = -2;
7981
8082// As defined in the wasi_snapshot_preview1 spec file:
8183// https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/witx/typenames.witx
......@@ -115,6 +117,7 @@ pub const EADDRINUSE: errno_t = 3;
115117pub const EADDRNOTAVAIL: errno_t = 4;
116118pub const EAFNOSUPPORT: errno_t = 5;
117119pub const EAGAIN: errno_t = 6;
120pub const EWOULDBLOCK = EAGAIN;
118121pub const EALREADY: errno_t = 7;
119122pub const EBADF: errno_t = 8;
120123pub const EBADMSG: errno_t = 9;
......@@ -167,6 +170,7 @@ pub const ENOTEMPTY: errno_t = 55;
167170pub const ENOTRECOVERABLE: errno_t = 56;
168171pub const ENOTSOCK: errno_t = 57;
169172pub const ENOTSUP: errno_t = 58;
173pub const EOPNOTSUPP = ENOTSUP;
170174pub const ENOTTY: errno_t = 59;
171175pub const ENXIO: errno_t = 60;
172176pub const EOVERFLOW: errno_t = 61;
......@@ -208,7 +212,7 @@ pub const EVENTTYPE_FD_WRITE: eventtype_t = 2;
208212
209213pub const exitcode_t = u32;
210214
211pub const fd_t = u32;
215pub const fd_t = if (builtin.link_libc) c_int else u32;
212216
213217pub const fdflags_t = u16;
214218pub const FDFLAG_APPEND: fdflags_t = 0x0001;
......@@ -275,11 +279,34 @@ pub const linkcount_t = u64;
275279pub const lookupflags_t = u32;
276280pub const LOOKUP_SYMLINK_FOLLOW: lookupflags_t = 0x00000001;
277281
278pub const oflags_t = u16;
279pub const O_CREAT: oflags_t = 0x0001;
280pub const O_DIRECTORY: oflags_t = 0x0002;
281pub const O_EXCL: oflags_t = 0x0004;
282pub const O_TRUNC: oflags_t = 0x0008;
282pub usingnamespace if (builtin.link_libc) struct {
283 // Derived from https://github.com/WebAssembly/wasi-libc/blob/main/expected/wasm32-wasi/predefined-macros.txt
284 pub const O_ACCMODE = (O_EXEC | O_RDWR | O_SEARCH);
285 pub const O_APPEND = FDFLAG_APPEND;
286 pub const O_CLOEXEC = (0);
287 pub const O_CREAT = ((1 << 0) << 12); // = __WASI_OFLAGS_CREAT << 12
288 pub const O_DIRECTORY = ((1 << 1) << 12); // = __WASI_OFLAGS_DIRECTORY << 12
289 pub const O_DSYNC = FDFLAG_DSYNC;
290 pub const O_EXCL = ((1 << 2) << 12); // = __WASI_OFLAGS_EXCL << 12
291 pub const O_EXEC = (0x02000000);
292 pub const O_NOCTTY = (0);
293 pub const O_NOFOLLOW = (0x01000000);
294 pub const O_NONBLOCK = (1 << FDFLAG_NONBLOCK);
295 pub const O_RDONLY = (0x04000000);
296 pub const O_RDWR = (O_RDONLY | O_WRONLY);
297 pub const O_RSYNC = (1 << FDFLAG_RSYNC);
298 pub const O_SEARCH = (0x08000000);
299 pub const O_SYNC = (1 << FDFLAG_SYNC);
300 pub const O_TRUNC = ((1 << 3) << 12); // = __WASI_OFLAGS_TRUNC << 12
301 pub const O_TTY_INIT = (0);
302 pub const O_WRONLY = (0x10000000);
303} else struct {
304 pub const oflags_t = u16;
305 pub const O_CREAT: oflags_t = 0x0001;
306 pub const O_DIRECTORY: oflags_t = 0x0002;
307 pub const O_EXCL: oflags_t = 0x0004;
308 pub const O_TRUNC: oflags_t = 0x0008;
309};
283310
284311pub const preopentype_t = u8;
285312pub const PREOPENTYPE_DIR: preopentype_t = 0;
......@@ -441,3 +468,23 @@ pub const whence_t = u8;
441468pub const WHENCE_SET: whence_t = 0;
442469pub const WHENCE_CUR: whence_t = 1;
443470pub const WHENCE_END: whence_t = 2;
471
472pub const S_IEXEC = S_IXUSR;
473pub const S_IFBLK = 0x6000;
474pub const S_IFCHR = 0x2000;
475pub const S_IFDIR = 0x4000;
476pub const S_IFIFO = 0xc000;
477pub const S_IFLNK = 0xa000;
478pub const S_IFMT = S_IFBLK | S_IFCHR | S_IFDIR | S_IFIFO | S_IFLNK | S_IFREG | S_IFSOCK;
479pub const S_IFREG = 0x8000;
480// There's no concept of UNIX domain socket but we define this value here in order to line with other OSes.
481pub const S_IFSOCK = 0x1;
482
483pub const SEEK_SET = WHENCE_SET;
484pub const SEEK_CUR = WHENCE_CUR;
485pub const SEEK_END = WHENCE_END;
486
487pub const LOCK_SH = 0x1;
488pub const LOCK_EX = 0x2;
489pub const LOCK_NB = 0x4;
490pub const LOCK_UN = 0x8;
lib/std/process.zig+4-4
......@@ -88,7 +88,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
8888 try result.putMove(key, value);
8989 }
9090 return result;
91 } else if (builtin.os.tag == .wasi) {
91 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
9292 var environ_count: usize = undefined;
9393 var environ_buf_size: usize = undefined;
9494
......@@ -450,7 +450,7 @@ pub const ArgIteratorWindows = struct {
450450pub const ArgIterator = struct {
451451 const InnerType = switch (builtin.os.tag) {
452452 .windows => ArgIteratorWindows,
453 .wasi => ArgIteratorWasi,
453 .wasi => if (builtin.link_libc) ArgIteratorPosix else ArgIteratorWasi,
454454 else => ArgIteratorPosix,
455455 };
456456
......@@ -469,7 +469,7 @@ pub const ArgIterator = struct {
469469
470470 /// You must deinitialize iterator's internal buffers by calling `deinit` when done.
471471 pub fn initWithAllocator(allocator: *mem.Allocator) InitError!ArgIterator {
472 if (builtin.os.tag == .wasi) {
472 if (builtin.os.tag == .wasi and !builtin.link_libc) {
473473 return ArgIterator{ .inner = try InnerType.init(allocator) };
474474 }
475475
......@@ -507,7 +507,7 @@ pub const ArgIterator = struct {
507507 /// was created with `initWithAllocator` function.
508508 pub fn deinit(self: *ArgIterator) void {
509509 // Unless we're targeting WASI, this is a no-op.
510 if (builtin.os.tag == .wasi) {
510 if (builtin.os.tag == .wasi and !builtin.link_libc) {
511511 self.inner.deinit();
512512 }
513513 }
lib/std/start.zig+8-1
......@@ -46,7 +46,9 @@ comptime {
4646 }
4747 } else if (builtin.output_mode == .Exe or @hasDecl(root, "main")) {
4848 if (builtin.link_libc and @hasDecl(root, "main")) {
49 if (@typeInfo(@TypeOf(root.main)).Fn.calling_convention != .C) {
49 if (native_arch.isWasm()) {
50 @export(mainWithoutEnv, .{ .name = "main" });
51 } else if (@typeInfo(@TypeOf(root.main)).Fn.calling_convention != .C) {
5052 @export(main, .{ .name = "main" });
5153 }
5254 } else if (native_os == .windows) {
......@@ -420,6 +422,11 @@ fn main(c_argc: i32, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) callconv(.C)
420422 return @call(.{ .modifier = .always_inline }, callMainWithArgs, .{ @intCast(usize, c_argc), c_argv, envp });
421423}
422424
425fn mainWithoutEnv(c_argc: i32, c_argv: [*][*:0]u8) callconv(.C) usize {
426 std.os.argv = c_argv[0..@intCast(usize, c_argc)];
427 return @call(.{ .modifier = .always_inline }, callMain, .{});
428}
429
423430// General error message for a malformed return type
424431const bad_main_ret = "expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'";
425432
lib/std/testing.zig+2-1
......@@ -4,6 +4,7 @@
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
66const std = @import("std.zig");
7
78const math = std.math;
89const print = std.debug.print;
910
......@@ -326,7 +327,7 @@ pub const TmpDir = struct {
326327};
327328
328329fn getCwdOrWasiPreopen() std.fs.Dir {
329 if (std.builtin.os.tag == .wasi) {
330 if (std.builtin.os.tag == .wasi and !std.builtin.link_libc) {
330331 var preopens = std.fs.wasi.PreopenList.init(allocator);
331332 defer preopens.deinit();
332333 preopens.populate() catch
src/stage1/codegen.cpp+3-1
......@@ -582,7 +582,9 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {
582582
583583 bool want_ssp_attrs = g->build_mode != BuildModeFastRelease &&
584584 g->build_mode != BuildModeSmallRelease &&
585 g->link_libc;
585 g->link_libc &&
586 // WASI-libc does not support stack-protector yet.
587 !target_is_wasm(g->zig_target);
586588 if (want_ssp_attrs) {
587589 addLLVMFnAttr(llvm_fn, "sspstrong");
588590 addLLVMFnAttrStr(llvm_fn, "stack-protector-buffer-size", "4");
test/cases.zig+1-1
......@@ -26,7 +26,7 @@ pub fn addCases(ctx: *TestContext) !void {
2626 var case = ctx.exe("hello world with updates", linux_x64);
2727
2828 case.addError("", &[_][]const u8{
29 ":93:9: error: struct 'tmp.tmp' has no member named 'main'",
29 ":95:9: error: struct 'tmp.tmp' has no member named 'main'",
3030 });
3131
3232 // Incorrect return type
test/stage2/darwin.zig+1-1
......@@ -14,7 +14,7 @@ pub fn addCases(ctx: *TestContext) !void {
1414 {
1515 var case = ctx.exe("hello world with updates", target);
1616 case.addError("", &[_][]const u8{
17 ":93:9: error: struct 'tmp.tmp' has no member named 'main'",
17 ":95:9: error: struct 'tmp.tmp' has no member named 'main'",
1818 });
1919
2020 // Incorrect return type
test/tests.zig+8
......@@ -57,6 +57,14 @@ const test_targets = blk: {
5757 .link_libc = false,
5858 .single_threaded = true,
5959 },
60 TestTarget{
61 .target = .{
62 .cpu_arch = .wasm32,
63 .os_tag = .wasi,
64 },
65 .link_libc = true,
66 .single_threaded = true,
67 },
6068
6169 TestTarget{
6270 .target = .{