authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-28 05:51:15-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-02-28 05:51:15-08:00
loga1b083b6667bbacc80db485ea97eceb6867cf600
tree2873fefc63d11e3fee3593459dcd9a16c3f7bceb
parent6e078883eebd0532928dba0c70e86a2eee0f246b
parentf446d8e8f9bbfc52e73649b1754dab88d4acfb07
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #19120 from jacobly0/os-cleanup

posix: fix socket fd leak

2 files changed, 163 insertions(+), 212 deletions(-)

lib/std/Target.zig+4-1
......@@ -392,7 +392,10 @@ pub const Os = struct {
392392
393393 /// Checks if system is guaranteed to be at least `version` or older than `version`.
394394 /// Returns `null` if a runtime check is required.
395 pub inline fn isAtLeast(self: Os, comptime tag: Tag, version: anytype) ?bool {
395 pub inline fn isAtLeast(self: Os, comptime tag: Tag, version: switch (tag) {
396 else => std.SemanticVersion,
397 .windows => WindowsVersion,
398 }) ?bool {
396399 if (self.tag != tag) return false;
397400
398401 return switch (tag) {
lib/std/os.zig+159-211
......@@ -302,7 +302,7 @@ pub fn close(fd: fd_t) void {
302302 _ = wasi.fd_close(fd);
303303 return;
304304 }
305 if (comptime builtin.target.isDarwin()) {
305 if (builtin.target.isDarwin()) {
306306 // This avoids the EINTR problem.
307307 switch (darwin.getErrno(darwin.@"close$NOCANCEL"(fd))) {
308308 .BADF => unreachable, // Always a race condition.
......@@ -476,7 +476,7 @@ fn fchmodat2(dirfd: fd_t, path: []const u8, mode: mode_t, flags: u32) FChmodAtEr
476476 const rc = system.openat(dirfd, &path_c, .{ .PATH = true, .NOFOLLOW = true, .CLOEXEC = true }, @as(mode_t, 0));
477477 switch (system.getErrno(rc)) {
478478 .SUCCESS => {
479 pathfd = @as(fd_t, @intCast(rc));
479 pathfd = @intCast(rc);
480480 break;
481481 },
482482 .INTR => continue,
......@@ -550,7 +550,7 @@ pub fn fchown(fd: fd_t, owner: ?uid_t, group: ?gid_t) FChownError!void {
550550 }
551551
552552 while (true) {
553 const res = system.fchown(fd, owner orelse @as(u32, 0) -% 1, group orelse @as(u32, 0) -% 1);
553 const res = system.fchown(fd, owner orelse ~@as(uid_t, 0), group orelse ~@as(gid_t, 0));
554554
555555 switch (system.getErrno(res)) {
556556 .SUCCESS => return,
......@@ -596,7 +596,7 @@ pub fn reboot(cmd: RebootCommand) RebootError!void {
596596 switch (system.getErrno(linux.reboot(
597597 .MAGIC1,
598598 .MAGIC2,
599 @as(linux.LINUX_REBOOT.CMD, cmd),
599 cmd,
600600 switch (cmd) {
601601 .RESTART2 => |s| s,
602602 else => null,
......@@ -639,27 +639,27 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
639639 std.c.versionCheck(std.SemanticVersion{ .major = 2, .minor = 25, .patch = 0 });
640640
641641 while (buf.len != 0) {
642 const res = if (use_c) blk: {
642 const num_read: usize, const err = if (use_c) res: {
643643 const rc = std.c.getrandom(buf.ptr, buf.len, 0);
644 break :blk .{
645 .num_read = @as(usize, @bitCast(rc)),
646 .err = std.c.getErrno(rc),
644 break :res .{
645 @bitCast(rc),
646 std.c.getErrno(rc),
647647 };
648 } else blk: {
648 } else res: {
649649 const rc = linux.getrandom(buf.ptr, buf.len, 0);
650 break :blk .{
651 .num_read = rc,
652 .err = linux.getErrno(rc),
650 break :res .{
651 rc,
652 linux.getErrno(rc),
653653 };
654654 };
655655
656 switch (res.err) {
657 .SUCCESS => buf = buf[res.num_read..],
656 switch (err) {
657 .SUCCESS => buf = buf[num_read..],
658658 .INVAL => unreachable,
659659 .FAULT => unreachable,
660660 .INTR => continue,
661661 .NOSYS => return getRandomBytesDevURandom(buf),
662 else => return unexpectedErrno(res.err),
662 else => return unexpectedErrno(err),
663663 }
664664 }
665665 return;
......@@ -818,10 +818,10 @@ pub fn exit(status: u8) noreturn {
818818 // exit() is only available if exitBootServices() has not been called yet.
819819 // This call to exit should not fail, so we don't care about its return value.
820820 if (uefi.system_table.boot_services) |bs| {
821 _ = bs.exit(uefi.handle, @as(uefi.Status, @enumFromInt(status)), 0, null);
821 _ = bs.exit(uefi.handle, @enumFromInt(status), 0, null);
822822 }
823823 // If we can't exit, reboot the system instead.
824 uefi.system_table.runtime_services.resetSystem(uefi.tables.ResetType.ResetCold, @as(uefi.Status, @enumFromInt(status)), 0, null);
824 uefi.system_table.runtime_services.resetSystem(.ResetCold, @enumFromInt(status), 0, null);
825825 }
826826 system.exit(status);
827827}
......@@ -893,12 +893,10 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
893893 .macos, .ios, .watchos, .tvos => math.maxInt(i32),
894894 else => math.maxInt(isize),
895895 };
896 const adjusted_len = @min(max_count, buf.len);
897
898896 while (true) {
899 const rc = system.read(fd, buf.ptr, adjusted_len);
897 const rc = system.read(fd, buf.ptr, @min(buf.len, max_count));
900898 switch (errno(rc)) {
901 .SUCCESS => return @as(usize, @intCast(rc)),
899 .SUCCESS => return @intCast(rc),
902900 .INTR => continue,
903901 .INVAL => unreachable,
904902 .FAULT => unreachable,
......@@ -932,7 +930,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
932930pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
933931 if (builtin.os.tag == .windows) {
934932 // TODO improve this to use ReadFileScatter
935 if (iov.len == 0) return @as(usize, 0);
933 if (iov.len == 0) return 0;
936934 const first = iov[0];
937935 return read(fd, first.iov_base[0..first.iov_len]);
938936 }
......@@ -956,12 +954,11 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
956954 else => |err| return unexpectedErrno(err),
957955 }
958956 }
959 const iov_count = math.cast(u31, iov.len) orelse math.maxInt(u31);
957
960958 while (true) {
961 // TODO handle the case when iov_len is too large and get rid of this @intCast
962 const rc = system.readv(fd, iov.ptr, iov_count);
959 const rc = system.readv(fd, iov.ptr, @min(iov.len, IOV_MAX));
963960 switch (errno(rc)) {
964 .SUCCESS => return @as(usize, @intCast(rc)),
961 .SUCCESS => return @intCast(rc),
965962 .INTR => continue,
966963 .INVAL => unreachable,
967964 .FAULT => unreachable,
......@@ -1035,15 +1032,12 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
10351032 .macos, .ios, .watchos, .tvos => math.maxInt(i32),
10361033 else => math.maxInt(isize),
10371034 };
1038 const adjusted_len = @min(max_count, buf.len);
10391035
10401036 const pread_sym = if (lfs64_abi) system.pread64 else system.pread;
1041
1042 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
10431037 while (true) {
1044 const rc = pread_sym(fd, buf.ptr, adjusted_len, ioffset);
1038 const rc = pread_sym(fd, buf.ptr, @min(buf.len, max_count), @bitCast(offset));
10451039 switch (errno(rc)) {
1046 .SUCCESS => return @as(usize, @intCast(rc)),
1040 .SUCCESS => return @intCast(rc),
10471041 .INTR => continue,
10481042 .INVAL => unreachable,
10491043 .FAULT => unreachable,
......@@ -1078,7 +1072,7 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
10781072 if (builtin.os.tag == .windows) {
10791073 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
10801074 var eof_info = windows.FILE_END_OF_FILE_INFORMATION{
1081 .EndOfFile = @as(windows.LARGE_INTEGER, @bitCast(length)),
1075 .EndOfFile = @bitCast(length),
10821076 };
10831077
10841078 const rc = windows.ntdll.NtSetInformationFile(
......@@ -1111,11 +1105,9 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
11111105 }
11121106 }
11131107
1108 const ftruncate_sym = if (lfs64_abi) system.ftruncate64 else system.ftruncate;
11141109 while (true) {
1115 const ftruncate_sym = if (lfs64_abi) system.ftruncate64 else system.ftruncate;
1116
1117 const ilen = @as(i64, @bitCast(length)); // the OS treats this as unsigned
1118 switch (errno(ftruncate_sym(fd, ilen))) {
1110 switch (errno(ftruncate_sym(fd, @bitCast(length)))) {
11191111 .SUCCESS => return,
11201112 .INTR => continue,
11211113 .FBIG => return error.FileTooBig,
......@@ -1178,15 +1170,11 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
11781170 }
11791171 }
11801172
1181 const iov_count = math.cast(u31, iov.len) orelse math.maxInt(u31);
1182
11831173 const preadv_sym = if (lfs64_abi) system.preadv64 else system.preadv;
1184
1185 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
11861174 while (true) {
1187 const rc = preadv_sym(fd, iov.ptr, iov_count, ioffset);
1175 const rc = preadv_sym(fd, iov.ptr, @min(iov.len, IOV_MAX), @bitCast(offset));
11881176 switch (errno(rc)) {
1189 .SUCCESS => return @as(usize, @bitCast(rc)),
1177 .SUCCESS => return @bitCast(rc),
11901178 .INTR => continue,
11911179 .INVAL => unreachable,
11921180 .FAULT => unreachable,
......@@ -1293,12 +1281,10 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
12931281 .macos, .ios, .watchos, .tvos => math.maxInt(i32),
12941282 else => math.maxInt(isize),
12951283 };
1296 const adjusted_len = @min(max_count, bytes.len);
1297
12981284 while (true) {
1299 const rc = system.write(fd, bytes.ptr, adjusted_len);
1285 const rc = system.write(fd, bytes.ptr, @min(bytes.len, max_count));
13001286 switch (errno(rc)) {
1301 .SUCCESS => return @as(usize, @intCast(rc)),
1287 .SUCCESS => return @intCast(rc),
13021288 .INTR => continue,
13031289 .INVAL => return error.InvalidArgument,
13041290 .FAULT => unreachable,
......@@ -1342,7 +1328,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
13421328pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
13431329 if (builtin.os.tag == .windows) {
13441330 // TODO improve this to use WriteFileScatter
1345 if (iov.len == 0) return @as(usize, 0);
1331 if (iov.len == 0) return 0;
13461332 const first = iov[0];
13471333 return write(fd, first.iov_base[0..first.iov_len]);
13481334 }
......@@ -1367,11 +1353,10 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
13671353 }
13681354 }
13691355
1370 const iov_count = if (iov.len > IOV_MAX) IOV_MAX else @as(u31, @intCast(iov.len));
13711356 while (true) {
1372 const rc = system.writev(fd, iov.ptr, iov_count);
1357 const rc = system.writev(fd, iov.ptr, @min(iov.len, IOV_MAX));
13731358 switch (errno(rc)) {
1374 .SUCCESS => return @as(usize, @intCast(rc)),
1359 .SUCCESS => return @intCast(rc),
13751360 .INTR => continue,
13761361 .INVAL => return error.InvalidArgument,
13771362 .FAULT => unreachable,
......@@ -1455,15 +1440,12 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
14551440 .macos, .ios, .watchos, .tvos => math.maxInt(i32),
14561441 else => math.maxInt(isize),
14571442 };
1458 const adjusted_len = @min(max_count, bytes.len);
14591443
14601444 const pwrite_sym = if (lfs64_abi) system.pwrite64 else system.pwrite;
1461
1462 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
14631445 while (true) {
1464 const rc = pwrite_sym(fd, bytes.ptr, adjusted_len, ioffset);
1446 const rc = pwrite_sym(fd, bytes.ptr, @min(bytes.len, max_count), @bitCast(offset));
14651447 switch (errno(rc)) {
1466 .SUCCESS => return @as(usize, @intCast(rc)),
1448 .SUCCESS => return @intCast(rc),
14671449 .INTR => continue,
14681450 .INVAL => return error.InvalidArgument,
14691451 .FAULT => unreachable,
......@@ -1515,7 +1497,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
15151497 if (have_pwrite_but_not_pwritev) {
15161498 // We could loop here; but proper usage of `pwritev` must handle partial writes anyway.
15171499 // So we simply write the first vector only.
1518 if (iov.len == 0) return @as(usize, 0);
1500 if (iov.len == 0) return 0;
15191501 const first = iov[0];
15201502 return pwrite(fd, first.iov_base[0..first.iov_len], offset);
15211503 }
......@@ -1544,13 +1526,10 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
15441526 }
15451527
15461528 const pwritev_sym = if (lfs64_abi) system.pwritev64 else system.pwritev;
1547
1548 const iov_count = if (iov.len > IOV_MAX) IOV_MAX else @as(u31, @intCast(iov.len));
1549 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
15501529 while (true) {
1551 const rc = pwritev_sym(fd, iov.ptr, iov_count, ioffset);
1530 const rc = pwritev_sym(fd, iov.ptr, @min(iov.len, IOV_MAX), @bitCast(offset));
15521531 switch (errno(rc)) {
1553 .SUCCESS => return @as(usize, @intCast(rc)),
1532 .SUCCESS => return @intCast(rc),
15541533 .INTR => continue,
15551534 .INVAL => return error.InvalidArgument,
15561535 .FAULT => unreachable,
......@@ -1666,11 +1645,10 @@ pub fn openZ(file_path: [*:0]const u8, flags: O, perm: mode_t) OpenError!fd_t {
16661645 }
16671646
16681647 const open_sym = if (lfs64_abi) system.open64 else system.open;
1669
16701648 while (true) {
16711649 const rc = open_sym(file_path, flags, perm);
16721650 switch (errno(rc)) {
1673 .SUCCESS => return @as(fd_t, @intCast(rc)),
1651 .SUCCESS => return @intCast(rc),
16741652 .INTR => continue,
16751653
16761654 .FAULT => unreachable,
......@@ -1863,11 +1841,10 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O
18631841 }
18641842
18651843 const openat_sym = if (lfs64_abi) system.openat64 else system.openat;
1866
18671844 while (true) {
18681845 const rc = openat_sym(dir_fd, file_path, flags, mode);
18691846 switch (errno(rc)) {
1870 .SUCCESS => return @as(fd_t, @intCast(rc)),
1847 .SUCCESS => return @intCast(rc),
18711848 .INTR => continue,
18721849
18731850 .FAULT => unreachable,
......@@ -1900,7 +1877,7 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O
19001877pub fn dup(old_fd: fd_t) !fd_t {
19011878 const rc = system.dup(old_fd);
19021879 return switch (errno(rc)) {
1903 .SUCCESS => return @as(fd_t, @intCast(rc)),
1880 .SUCCESS => return @intCast(rc),
19041881 .MFILE => error.ProcessFdQuotaExceeded,
19051882 .BADF => unreachable, // invalid file descriptor
19061883 else => |err| return unexpectedErrno(err),
......@@ -2144,11 +2121,11 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
21442121 return result;
21452122 }
21462123
2147 const err = if (builtin.link_libc) blk: {
2124 const err: E = if (builtin.link_libc) err: {
21482125 const c_err = if (std.c.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else std.c._errno().*;
2149 break :blk @as(E, @enumFromInt(c_err));
2150 } else blk: {
2151 break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len));
2126 break :err @enumFromInt(c_err);
2127 } else err: {
2128 break :err errno(system.getcwd(out_buffer.ptr, out_buffer.len));
21522129 };
21532130 switch (err) {
21542131 .SUCCESS => return mem.sliceTo(out_buffer, 0),
......@@ -2880,13 +2857,13 @@ pub fn renameatW(
28802857 // supported in order to avoid either (1) using a redundant call that we can know in advance will return
28812858 // STATUS_NOT_SUPPORTED or (2) only setting IGNORE_READONLY_ATTRIBUTE when >= rs5
28822859 // and therefore having different behavior when the Windows version is >= rs1 but < rs5.
2883 if (comptime builtin.target.os.version_range.windows.min.isAtLeast(.win10_rs5)) {
2860 if (builtin.target.os.isAtLeast(.windows, .win10_rs5) orelse false) {
28842861 const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) + (MAX_PATH_BYTES - 1);
28852862 var rename_info_buf: [struct_buf_len]u8 align(@alignOf(windows.FILE_RENAME_INFORMATION_EX)) = undefined;
28862863 const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) - 1 + new_path_w.len * 2;
28872864 if (struct_len > struct_buf_len) return error.NameTooLong;
28882865
2889 const rename_info = @as(*windows.FILE_RENAME_INFORMATION_EX, @ptrCast(&rename_info_buf));
2866 const rename_info: *windows.FILE_RENAME_INFORMATION_EX = @ptrCast(&rename_info_buf);
28902867 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
28912868
28922869 var flags: windows.ULONG = windows.FILE_RENAME_POSIX_SEMANTICS | windows.FILE_RENAME_IGNORE_READONLY_ATTRIBUTE;
......@@ -2897,7 +2874,7 @@ pub fn renameatW(
28972874 .FileNameLength = @intCast(new_path_w.len * 2), // already checked error.NameTooLong
28982875 .FileName = undefined,
28992876 };
2900 @memcpy(@as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w);
2877 @memcpy((&rename_info.FileName).ptr, new_path_w);
29012878 rc = windows.ntdll.NtSetInformationFile(
29022879 src_fd,
29032880 &io_status_block,
......@@ -2923,7 +2900,7 @@ pub fn renameatW(
29232900 const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION) - 1 + new_path_w.len * 2;
29242901 if (struct_len > struct_buf_len) return error.NameTooLong;
29252902
2926 const rename_info = @as(*windows.FILE_RENAME_INFORMATION, @ptrCast(&rename_info_buf));
2903 const rename_info: *windows.FILE_RENAME_INFORMATION = @ptrCast(&rename_info_buf);
29272904 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
29282905
29292906 rename_info.* = .{
......@@ -2932,7 +2909,7 @@ pub fn renameatW(
29322909 .FileNameLength = @intCast(new_path_w.len * 2), // already checked error.NameTooLong
29332910 .FileName = undefined,
29342911 };
2935 @memcpy(@as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w);
2912 @memcpy((&rename_info.FileName).ptr, new_path_w);
29362913
29372914 rc =
29382915 windows.ntdll.NtSetInformationFile(
......@@ -3380,7 +3357,7 @@ pub fn readlinkZ(file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8
33803357 }
33813358 const rc = system.readlink(file_path, out_buffer.ptr, out_buffer.len);
33823359 switch (errno(rc)) {
3383 .SUCCESS => return out_buffer[0..@as(usize, @bitCast(rc))],
3360 .SUCCESS => return out_buffer[0..@bitCast(rc)],
33843361 .ACCES => return error.AccessDenied,
33853362 .FAULT => unreachable,
33863363 .INVAL => return error.NotLink,
......@@ -3458,7 +3435,7 @@ pub fn readlinkatZ(dirfd: fd_t, file_path: [*:0]const u8, out_buffer: []u8) Read
34583435 }
34593436 const rc = system.readlinkat(dirfd, file_path, out_buffer.ptr, out_buffer.len);
34603437 switch (errno(rc)) {
3461 .SUCCESS => return out_buffer[0..@as(usize, @bitCast(rc))],
3438 .SUCCESS => return out_buffer[0..@bitCast(rc)],
34623439 .ACCES => return error.AccessDenied,
34633440 .FAULT => unreachable,
34643441 .INVAL => return error.NotLink,
......@@ -3570,7 +3547,7 @@ pub fn isatty(handle: fd_t) bool {
35703547 if (builtin.os.tag == .linux) {
35713548 while (true) {
35723549 var wsz: linux.winsize = undefined;
3573 const fd = @as(usize, @bitCast(@as(isize, handle)));
3550 const fd: usize = @bitCast(@as(isize, handle));
35743551 const rc = linux.syscall3(.ioctl, fd, linux.T.IOCGWINSZ, @intFromPtr(&wsz));
35753552 switch (linux.getErrno(rc)) {
35763553 .SUCCESS => return true,
......@@ -3614,15 +3591,15 @@ pub fn isCygwinPty(handle: fd_t) bool {
36143591 var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = [_]u8{0} ** (name_bytes_offset + num_name_bytes);
36153592
36163593 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
3617 const rc = windows.ntdll.NtQueryInformationFile(handle, &io_status_block, &name_info_bytes, @as(u32, @intCast(name_info_bytes.len)), .FileNameInformation);
3594 const rc = windows.ntdll.NtQueryInformationFile(handle, &io_status_block, &name_info_bytes, @intCast(name_info_bytes.len), .FileNameInformation);
36183595 switch (rc) {
36193596 .SUCCESS => {},
36203597 .INVALID_PARAMETER => unreachable,
36213598 else => return false,
36223599 }
36233600
3624 const name_info = @as(*const windows.FILE_NAME_INFO, @ptrCast(&name_info_bytes[0]));
3625 const name_bytes = name_info_bytes[name_bytes_offset .. name_bytes_offset + @as(usize, name_info.FileNameLength)];
3601 const name_info: *const windows.FILE_NAME_INFO = @ptrCast(&name_info_bytes);
3602 const name_bytes = name_info_bytes[name_bytes_offset .. name_bytes_offset + name_info.FileNameLength];
36263603 const name_wide = mem.bytesAsSlice(u16, name_bytes);
36273604 // Note: The name we get from NtQueryInformationFile will be prefixed with a '\', e.g. \msys-1888ae32e00d56aa-pty0-to-master
36283605 return (mem.startsWith(u16, name_wide, &[_]u16{ '\\', 'm', 's', 'y', 's', '-' }) or
......@@ -3668,9 +3645,9 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t
36683645 else
36693646 0;
36703647 const rc = try windows.WSASocketW(
3671 @as(i32, @bitCast(domain)),
3672 @as(i32, @bitCast(filtered_sock_type)),
3673 @as(i32, @bitCast(protocol)),
3648 @bitCast(domain),
3649 @bitCast(filtered_sock_type),
3650 @bitCast(protocol),
36743651 null,
36753652 0,
36763653 flags,
......@@ -3688,7 +3665,7 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t
36883665 return rc;
36893666 }
36903667
3691 const have_sock_flags = comptime !builtin.target.isDarwin();
3668 const have_sock_flags = !builtin.target.isDarwin();
36923669 const filtered_sock_type = if (!have_sock_flags)
36933670 socket_type & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC)
36943671 else
......@@ -3696,7 +3673,8 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t
36963673 const rc = system.socket(domain, filtered_sock_type, protocol);
36973674 switch (errno(rc)) {
36983675 .SUCCESS => {
3699 const fd = @as(fd_t, @intCast(rc));
3676 const fd: fd_t = @intCast(rc);
3677 errdefer close(fd);
37003678 if (!have_sock_flags) {
37013679 try setSockFlags(fd, socket_type);
37023680 }
......@@ -3983,10 +3961,10 @@ pub fn accept(
39833961 /// description of the `CLOEXEC` flag in `open` for reasons why this may be useful.
39843962 flags: u32,
39853963) AcceptError!socket_t {
3986 const have_accept4 = comptime !(builtin.target.isDarwin() or builtin.os.tag == .windows);
3964 const have_accept4 = !(builtin.target.isDarwin() or builtin.os.tag == .windows);
39873965 assert(0 == (flags & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC))); // Unsupported flag(s)
39883966
3989 const accepted_sock = while (true) {
3967 const accepted_sock: socket_t = while (true) {
39903968 const rc = if (have_accept4)
39913969 system.accept4(sock, addr, addr_size, flags)
39923970 else if (builtin.os.tag == .windows)
......@@ -4013,9 +3991,7 @@ pub fn accept(
40133991 }
40143992 } else {
40153993 switch (errno(rc)) {
4016 .SUCCESS => {
4017 break @as(socket_t, @intCast(rc));
4018 },
3994 .SUCCESS => break @intCast(rc),
40193995 .INTR => continue,
40203996 .AGAIN => return error.WouldBlock,
40213997 .BADF => unreachable, // always a race condition
......@@ -4035,6 +4011,10 @@ pub fn accept(
40354011 }
40364012 };
40374013
4014 errdefer switch (builtin.os.tag) {
4015 .windows => windows.closesocket(accepted_sock) catch unreachable,
4016 else => close(accepted_sock),
4017 };
40384018 if (!have_accept4) {
40394019 try setSockFlags(accepted_sock, flags);
40404020 }
......@@ -4058,7 +4038,7 @@ pub const EpollCreateError = error{
40584038pub fn epoll_create1(flags: u32) EpollCreateError!i32 {
40594039 const rc = system.epoll_create1(flags);
40604040 switch (errno(rc)) {
4061 .SUCCESS => return @as(i32, @intCast(rc)),
4041 .SUCCESS => return @intCast(rc),
40624042 else => |err| return unexpectedErrno(err),
40634043
40644044 .INVAL => unreachable,
......@@ -4117,9 +4097,9 @@ pub fn epoll_ctl(epfd: i32, op: u32, fd: i32, event: ?*linux.epoll_event) EpollC
41174097pub fn epoll_wait(epfd: i32, events: []linux.epoll_event, timeout: i32) usize {
41184098 while (true) {
41194099 // TODO get rid of the @intCast
4120 const rc = system.epoll_wait(epfd, events.ptr, @as(u32, @intCast(events.len)), timeout);
4100 const rc = system.epoll_wait(epfd, events.ptr, @intCast(events.len), timeout);
41214101 switch (errno(rc)) {
4122 .SUCCESS => return @as(usize, @intCast(rc)),
4102 .SUCCESS => return @intCast(rc),
41234103 .INTR => continue,
41244104 .BADF => unreachable,
41254105 .FAULT => unreachable,
......@@ -4138,7 +4118,7 @@ pub const EventFdError = error{
41384118pub fn eventfd(initval: u32, flags: u32) EventFdError!i32 {
41394119 const rc = system.eventfd(initval, flags);
41404120 switch (errno(rc)) {
4141 .SUCCESS => return @as(i32, @intCast(rc)),
4121 .SUCCESS => return @intCast(rc),
41424122 else => |err| return unexpectedErrno(err),
41434123
41444124 .INVAL => unreachable, // invalid parameters
......@@ -4272,7 +4252,7 @@ pub const ConnectError = error{
42724252/// return error.WouldBlock when EAGAIN or EINPROGRESS is received.
42734253pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) ConnectError!void {
42744254 if (builtin.os.tag == .windows) {
4275 const rc = windows.ws2_32.connect(sock, sock_addr, @as(i32, @intCast(len)));
4255 const rc = windows.ws2_32.connect(sock, sock_addr, @intCast(len));
42764256 if (rc == 0) return;
42774257 switch (windows.ws2_32.WSAGetLastError()) {
42784258 .WSAEADDRINUSE => return error.AddressInUse,
......@@ -4327,7 +4307,7 @@ pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) Conne
43274307pub fn getsockoptError(sockfd: fd_t) ConnectError!void {
43284308 var err_code: i32 = undefined;
43294309 var size: u32 = @sizeOf(u32);
4330 const rc = system.getsockopt(sockfd, SOL.SOCKET, SO.ERROR, @as([*]u8, @ptrCast(&err_code)), &size);
4310 const rc = system.getsockopt(sockfd, SOL.SOCKET, SO.ERROR, @ptrCast(&err_code), &size);
43314311 assert(size == 4);
43324312 switch (errno(rc)) {
43334313 .SUCCESS => switch (@as(E, @enumFromInt(err_code))) {
......@@ -4368,15 +4348,13 @@ pub const WaitPidResult = struct {
43684348/// Use this version of the `waitpid` wrapper if you spawned your child process using explicit
43694349/// `fork` and `execve` method.
43704350pub fn waitpid(pid: pid_t, flags: u32) WaitPidResult {
4371 const Status = if (builtin.link_libc) c_int else u32;
4372 var status: Status = undefined;
4373 const coerced_flags = if (builtin.link_libc) @as(c_int, @intCast(flags)) else flags;
4351 var status: if (builtin.link_libc) c_int else u32 = undefined;
43744352 while (true) {
4375 const rc = system.waitpid(pid, &status, coerced_flags);
4353 const rc = system.waitpid(pid, &status, @intCast(flags));
43764354 switch (errno(rc)) {
43774355 .SUCCESS => return .{
4378 .pid = @as(pid_t, @intCast(rc)),
4379 .status = @as(u32, @bitCast(status)),
4356 .pid = @intCast(rc),
4357 .status = @bitCast(status),
43804358 },
43814359 .INTR => continue,
43824360 .CHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error.
......@@ -4387,15 +4365,13 @@ pub fn waitpid(pid: pid_t, flags: u32) WaitPidResult {
43874365}
43884366
43894367pub fn wait4(pid: pid_t, flags: u32, ru: ?*rusage) WaitPidResult {
4390 const Status = if (builtin.link_libc) c_int else u32;
4391 var status: Status = undefined;
4392 const coerced_flags = if (builtin.link_libc) @as(c_int, @intCast(flags)) else flags;
4368 var status: if (builtin.link_libc) c_int else u32 = undefined;
43934369 while (true) {
4394 const rc = system.wait4(pid, &status, coerced_flags, ru);
4370 const rc = system.wait4(pid, &status, @intCast(flags), ru);
43954371 switch (errno(rc)) {
43964372 .SUCCESS => return .{
4397 .pid = @as(pid_t, @intCast(rc)),
4398 .status = @as(u32, @bitCast(status)),
4373 .pid = @intCast(rc),
4374 .status = @bitCast(status),
43994375 },
44004376 .INTR => continue,
44014377 .CHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error.
......@@ -4423,7 +4399,6 @@ pub fn fstat(fd: fd_t) FStatError!Stat {
44234399 }
44244400
44254401 const fstat_sym = if (lfs64_abi) system.fstat64 else system.fstat;
4426
44274402 var stat = mem.zeroes(Stat);
44284403 switch (errno(fstat_sym(fd, &stat))) {
44294404 .SUCCESS => return stat,
......@@ -4507,7 +4482,6 @@ pub fn fstatatZ(dirfd: fd_t, pathname: [*:0]const u8, flags: u32) FStatAtError!S
45074482 }
45084483
45094484 const fstatat_sym = if (lfs64_abi) system.fstatat64 else system.fstatat;
4510
45114485 var stat = mem.zeroes(Stat);
45124486 switch (errno(fstatat_sym(dirfd, pathname, &stat, flags))) {
45134487 .SUCCESS => return stat,
......@@ -4540,7 +4514,7 @@ pub const KQueueError = error{
45404514pub fn kqueue() KQueueError!i32 {
45414515 const rc = system.kqueue();
45424516 switch (errno(rc)) {
4543 .SUCCESS => return @as(i32, @intCast(rc)),
4517 .SUCCESS => return @intCast(rc),
45444518 .MFILE => return error.ProcessFdQuotaExceeded,
45454519 .NFILE => return error.SystemFdQuotaExceeded,
45464520 else => |err| return unexpectedErrno(err),
......@@ -4581,7 +4555,7 @@ pub fn kevent(
45814555 timeout,
45824556 );
45834557 switch (errno(rc)) {
4584 .SUCCESS => return @as(usize, @intCast(rc)),
4558 .SUCCESS => return @intCast(rc),
45854559 .ACCES => return error.AccessDenied,
45864560 .FAULT => unreachable,
45874561 .BADF => unreachable, // Always a race condition.
......@@ -4605,7 +4579,7 @@ pub const INotifyInitError = error{
46054579pub fn inotify_init1(flags: u32) INotifyInitError!i32 {
46064580 const rc = system.inotify_init1(flags);
46074581 switch (errno(rc)) {
4608 .SUCCESS => return @as(i32, @intCast(rc)),
4582 .SUCCESS => return @intCast(rc),
46094583 .INVAL => unreachable,
46104584 .MFILE => return error.ProcessFdQuotaExceeded,
46114585 .NFILE => return error.SystemFdQuotaExceeded,
......@@ -4634,7 +4608,7 @@ pub fn inotify_add_watch(inotify_fd: i32, pathname: []const u8, mask: u32) INoti
46344608pub fn inotify_add_watchZ(inotify_fd: i32, pathname: [*:0]const u8, mask: u32) INotifyAddWatchError!i32 {
46354609 const rc = system.inotify_add_watch(inotify_fd, pathname, mask);
46364610 switch (errno(rc)) {
4637 .SUCCESS => return @as(i32, @intCast(rc)),
4611 .SUCCESS => return @intCast(rc),
46384612 .ACCES => return error.AccessDenied,
46394613 .BADF => unreachable,
46404614 .FAULT => unreachable,
......@@ -4670,7 +4644,7 @@ pub const FanotifyInitError = error{
46704644pub fn fanotify_init(flags: u32, event_f_flags: u32) FanotifyInitError!i32 {
46714645 const rc = system.fanotify_init(flags, event_f_flags);
46724646 switch (errno(rc)) {
4673 .SUCCESS => return @as(i32, @intCast(rc)),
4647 .SUCCESS => return @intCast(rc),
46744648 .INVAL => unreachable,
46754649 .MFILE => return error.ProcessFdQuotaExceeded,
46764650 .NFILE => return error.SystemFdQuotaExceeded,
......@@ -4775,7 +4749,7 @@ pub const ForkError = error{SystemResources} || UnexpectedError;
47754749pub fn fork() ForkError!pid_t {
47764750 const rc = system.fork();
47774751 switch (errno(rc)) {
4778 .SUCCESS => return @as(pid_t, @intCast(rc)),
4752 .SUCCESS => return @intCast(rc),
47794753 .AGAIN => return error.SystemResources,
47804754 .NOMEM => return error.SystemResources,
47814755 else => |err| return unexpectedErrno(err),
......@@ -4815,12 +4789,10 @@ pub fn mmap(
48154789 offset: u64,
48164790) MMapError![]align(mem.page_size) u8 {
48174791 const mmap_sym = if (lfs64_abi) system.mmap64 else system.mmap;
4818
4819 const ioffset: i64 = @bitCast(offset); // the OS treats this as unsigned
4820 const rc = mmap_sym(ptr, length, prot, @bitCast(flags), fd, ioffset);
4821 const err = if (builtin.link_libc) blk: {
4792 const rc = mmap_sym(ptr, length, prot, @bitCast(flags), fd, @bitCast(offset));
4793 const err: E = if (builtin.link_libc) blk: {
48224794 if (rc != std.c.MAP_FAILED) return @as([*]align(mem.page_size) u8, @ptrCast(@alignCast(rc)))[0..length];
4823 break :blk @as(E, @enumFromInt(system._errno().*));
4795 break :blk @enumFromInt(system._errno().*);
48244796 } else blk: {
48254797 const err = errno(rc);
48264798 if (err == .SUCCESS) return @as([*]align(mem.page_size) u8, @ptrFromInt(rc))[0..length];
......@@ -5245,7 +5217,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
52455217 }
52465218 if (builtin.os.tag == .wasi and !builtin.link_libc) {
52475219 var new_offset: wasi.filesize_t = undefined;
5248 switch (wasi.fd_seek(fd, @as(wasi.filedelta_t, @bitCast(offset)), .SET, &new_offset)) {
5220 switch (wasi.fd_seek(fd, @bitCast(offset), .SET, &new_offset)) {
52495221 .SUCCESS => return,
52505222 .BADF => unreachable, // always a race condition
52515223 .INVAL => return error.Unseekable,
......@@ -5258,9 +5230,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
52585230 }
52595231
52605232 const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
5261
5262 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
5263 switch (errno(lseek_sym(fd, ioffset, SEEK.SET))) {
5233 switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.SET))) {
52645234 .SUCCESS => return,
52655235 .BADF => unreachable, // always a race condition
52665236 .INVAL => return error.Unseekable,
......@@ -5275,7 +5245,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
52755245pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
52765246 if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
52775247 var result: u64 = undefined;
5278 switch (errno(system.llseek(fd, @as(u64, @bitCast(offset)), &result, SEEK.CUR))) {
5248 switch (errno(system.llseek(fd, @bitCast(offset), &result, SEEK.CUR))) {
52795249 .SUCCESS => return,
52805250 .BADF => unreachable, // always a race condition
52815251 .INVAL => return error.Unseekable,
......@@ -5302,9 +5272,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
53025272 }
53035273 }
53045274 const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
5305
5306 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
5307 switch (errno(lseek_sym(fd, ioffset, SEEK.CUR))) {
5275 switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.CUR))) {
53085276 .SUCCESS => return,
53095277 .BADF => unreachable, // always a race condition
53105278 .INVAL => return error.Unseekable,
......@@ -5319,7 +5287,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
53195287pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
53205288 if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
53215289 var result: u64 = undefined;
5322 switch (errno(system.llseek(fd, @as(u64, @bitCast(offset)), &result, SEEK.END))) {
5290 switch (errno(system.llseek(fd, @bitCast(offset), &result, SEEK.END))) {
53235291 .SUCCESS => return,
53245292 .BADF => unreachable, // always a race condition
53255293 .INVAL => return error.Unseekable,
......@@ -5346,9 +5314,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
53465314 }
53475315 }
53485316 const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
5349
5350 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
5351 switch (errno(lseek_sym(fd, ioffset, SEEK.END))) {
5317 switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.END))) {
53525318 .SUCCESS => return,
53535319 .BADF => unreachable, // always a race condition
53545320 .INVAL => return error.Unseekable,
......@@ -5390,10 +5356,9 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
53905356 }
53915357 }
53925358 const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
5393
53945359 const rc = lseek_sym(fd, 0, SEEK.CUR);
53955360 switch (errno(rc)) {
5396 .SUCCESS => return @as(u64, @bitCast(rc)),
5361 .SUCCESS => return @bitCast(rc),
53975362 .BADF => unreachable, // always a race condition
53985363 .INVAL => return error.Unseekable,
53995364 .OVERFLOW => return error.Unseekable,
......@@ -5416,7 +5381,7 @@ pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) FcntlError!usize {
54165381 while (true) {
54175382 const rc = system.fcntl(fd, cmd, arg);
54185383 switch (errno(rc)) {
5419 .SUCCESS => return @as(usize, @intCast(rc)),
5384 .SUCCESS => return @intCast(rc),
54205385 .INTR => continue,
54215386 .AGAIN, .ACCES => return error.Locked,
54225387 .BADF => unreachable,
......@@ -5709,7 +5674,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
57095674 // errno values to expect when command is F.GETPATH...
57105675 else => |err| return unexpectedErrno(err),
57115676 }
5712 const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES;
5677 const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES;
57135678 return out_buffer[0..len];
57145679 },
57155680 .linux => {
......@@ -5741,7 +5706,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
57415706 return target;
57425707 },
57435708 .freebsd => {
5744 if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0, .patch = 0 }) == .gt) {
5709 if (comptime builtin.os.isAtLeast(.freebsd, .{ .major = 13, .minor = 0, .patch = 0 }) orelse false) {
57455710 var kfile: system.kinfo_file = undefined;
57465711 kfile.structsize = system.KINFO_FILE_SIZE;
57475712 switch (errno(system.fcntl(fd, system.F.KINFO, @intFromPtr(&kfile)))) {
......@@ -5780,7 +5745,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
57805745 };
57815746 var i: usize = 0;
57825747 while (i < len) {
5783 const kf: *align(1) system.kinfo_file = @as(*align(1) system.kinfo_file, @ptrCast(&buf[i]));
5748 const kf: *align(1) system.kinfo_file = @ptrCast(&buf[i]);
57845749 if (kf.fd == fd) {
57855750 len = mem.indexOfScalar(u8, &kf.path, 0) orelse MAX_PATH_BYTES;
57865751 if (len == 0) return error.NameTooLong;
......@@ -5788,7 +5753,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
57885753 @memcpy(result, kf.path[0..len]);
57895754 return result;
57905755 }
5791 i += @as(usize, @intCast(kf.structsize));
5756 i += @intCast(kf.structsize);
57925757 }
57935758 return error.FileNotFound;
57945759 }
......@@ -5801,7 +5766,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
58015766 .RANGE => return error.NameTooLong,
58025767 else => |err| return unexpectedErrno(err),
58035768 }
5804 const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES;
5769 const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES;
58055770 return out_buffer[0..len];
58065771 },
58075772 .netbsd => {
......@@ -5815,7 +5780,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
58155780 .RANGE => return error.NameTooLong,
58165781 else => |err| return unexpectedErrno(err),
58175782 }
5818 const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES;
5783 const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES;
58195784 return out_buffer[0..len];
58205785 },
58215786 else => unreachable, // made unreachable by isGetFdPathSupportedOnTarget above
......@@ -5866,14 +5831,14 @@ pub fn dl_iterate_phdr(
58665831 callback(info, size, context_ptr.*) catch |err| return @intFromError(err);
58675832 return 0;
58685833 }
5869 }.callbackC, @as(?*anyopaque, @ptrFromInt(@intFromPtr(&context))))) {
5834 }.callbackC, @ptrCast(@constCast(&context)))) {
58705835 0 => return,
58715836 else => |err| return @as(Error, @errorCast(@errorFromInt(@as(std.meta.Int(.unsigned, @bitSizeOf(anyerror)), @intCast(err))))),
58725837 }
58735838 }
58745839
58755840 const elf_base = std.process.getBaseAddress();
5876 const ehdr = @as(*elf.Ehdr, @ptrFromInt(elf_base));
5841 const ehdr: *elf.Ehdr = @ptrFromInt(elf_base);
58775842 // Make sure the base address points to an ELF image.
58785843 assert(mem.eql(u8, ehdr.e_ident[0..4], elf.MAGIC));
58795844 const n_phdr = ehdr.e_phnum;
......@@ -5911,12 +5876,12 @@ pub fn dl_iterate_phdr(
59115876 var dlpi_phnum: u16 = undefined;
59125877
59135878 if (entry.l_addr != 0) {
5914 const elf_header = @as(*elf.Ehdr, @ptrFromInt(entry.l_addr));
5915 dlpi_phdr = @as([*]elf.Phdr, @ptrFromInt(entry.l_addr + elf_header.e_phoff));
5879 const elf_header: *elf.Ehdr = @ptrFromInt(entry.l_addr);
5880 dlpi_phdr = @ptrFromInt(entry.l_addr + elf_header.e_phoff);
59165881 dlpi_phnum = elf_header.e_phnum;
59175882 } else {
59185883 // This is the running ELF image
5919 dlpi_phdr = @as([*]elf.Phdr, @ptrFromInt(elf_base + ehdr.e_phoff));
5884 dlpi_phdr = @ptrFromInt(elf_base + ehdr.e_phoff);
59205885 dlpi_phnum = ehdr.e_phnum;
59215886 }
59225887
......@@ -5938,11 +5903,11 @@ pub const ClockGetTimeError = error{UnsupportedClock} || UnexpectedError;
59385903pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
59395904 if (builtin.os.tag == .wasi and !builtin.link_libc) {
59405905 var ts: timestamp_t = undefined;
5941 switch (system.clock_time_get(@as(u32, @bitCast(clk_id)), 1, &ts)) {
5906 switch (system.clock_time_get(@bitCast(clk_id), 1, &ts)) {
59425907 .SUCCESS => {
59435908 tp.* = .{
5944 .tv_sec = @as(i64, @intCast(ts / std.time.ns_per_s)),
5945 .tv_nsec = @as(isize, @intCast(ts % std.time.ns_per_s)),
5909 .tv_sec = @intCast(ts / std.time.ns_per_s),
5910 .tv_nsec = @intCast(ts % std.time.ns_per_s),
59465911 };
59475912 },
59485913 .INVAL => return error.UnsupportedClock,
......@@ -5979,10 +5944,10 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
59795944pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {
59805945 if (builtin.os.tag == .wasi and !builtin.link_libc) {
59815946 var ts: timestamp_t = undefined;
5982 switch (system.clock_res_get(@as(u32, @bitCast(clk_id)), &ts)) {
5947 switch (system.clock_res_get(@bitCast(clk_id), &ts)) {
59835948 .SUCCESS => res.* = .{
5984 .tv_sec = @as(i64, @intCast(ts / std.time.ns_per_s)),
5985 .tv_nsec = @as(isize, @intCast(ts % std.time.ns_per_s)),
5949 .tv_sec = @intCast(ts / std.time.ns_per_s),
5950 .tv_nsec = @intCast(ts % std.time.ns_per_s),
59865951 },
59875952 .INVAL => return error.UnsupportedClock,
59885953 else => |err| return unexpectedErrno(err),
......@@ -6207,7 +6172,7 @@ pub fn res_mkquery(
62076172 // TODO determine the circumstances for this and whether or
62086173 // not this should be an error.
62096174 if (j - i - 1 > 62) unreachable;
6210 q[i - 1] = @as(u8, @intCast(j - i));
6175 q[i - 1] = @intCast(j - i);
62116176 }
62126177 q[i + 1] = ty;
62136178 q[i + 3] = class;
......@@ -6216,10 +6181,10 @@ pub fn res_mkquery(
62166181 var ts: timespec = undefined;
62176182 clock_gettime(CLOCK.REALTIME, &ts) catch {};
62186183 const UInt = std.meta.Int(.unsigned, @bitSizeOf(@TypeOf(ts.tv_nsec)));
6219 const unsec = @as(UInt, @bitCast(ts.tv_nsec));
6220 const id = @as(u32, @truncate(unsec + unsec / 65536));
6221 q[0] = @as(u8, @truncate(id / 256));
6222 q[1] = @as(u8, @truncate(id));
6184 const unsec: UInt = @bitCast(ts.tv_nsec);
6185 const id: u32 = @truncate(unsec + unsec / 65536);
6186 q[0] = @truncate(id / 256);
6187 q[1] = @truncate(id);
62236188
62246189 @memcpy(buf[0..n], q[0..n]);
62256190 return n;
......@@ -6325,11 +6290,11 @@ pub fn sendmsg(
63256290 else => |err| return windows.unexpectedWSAError(err),
63266291 }
63276292 } else {
6328 return @as(usize, @intCast(rc));
6293 return @intCast(rc);
63296294 }
63306295 } else {
63316296 switch (errno(rc)) {
6332 .SUCCESS => return @as(usize, @intCast(rc)),
6297 .SUCCESS => return @intCast(rc),
63336298
63346299 .ACCES => return error.AccessDenied,
63356300 .AGAIN => return error.WouldBlock,
......@@ -6425,13 +6390,13 @@ pub fn sendto(
64256390 .WSANOTINITIALISED => unreachable, // A successful WSAStartup call must occur before using this function.
64266391 else => |err| return windows.unexpectedWSAError(err),
64276392 },
6428 else => |rc| return @as(usize, @intCast(rc)),
6393 else => |rc| return @intCast(rc),
64296394 }
64306395 }
64316396 while (true) {
64326397 const rc = system.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen);
64336398 switch (errno(rc)) {
6434 .SUCCESS => return @as(usize, @intCast(rc)),
6399 .SUCCESS => return @intCast(rc),
64356400
64366401 .ACCES => return error.AccessDenied,
64376402 .AGAIN => return error.WouldBlock,
......@@ -6583,18 +6548,15 @@ pub fn sendfile(
65836548 }
65846549
65856550 // Here we match BSD behavior, making a zero count value send as many bytes as possible.
6586 const adjusted_count_tmp = if (in_len == 0) max_count else @min(in_len, @as(size_t, max_count));
6587 // TODO we should not need this cast; improve return type of @min
6588 const adjusted_count = @as(usize, @intCast(adjusted_count_tmp));
6551 const adjusted_count = if (in_len == 0) max_count else @min(in_len, max_count);
65896552
65906553 const sendfile_sym = if (lfs64_abi) system.sendfile64 else system.sendfile;
6591
65926554 while (true) {
6593 var offset: off_t = @as(off_t, @bitCast(in_offset));
6555 var offset: off_t = @bitCast(in_offset);
65946556 const rc = sendfile_sym(out_fd, in_fd, &offset, adjusted_count);
65956557 switch (errno(rc)) {
65966558 .SUCCESS => {
6597 const amt = @as(usize, @bitCast(rc));
6559 const amt: usize = @bitCast(rc);
65986560 total_written += amt;
65996561 if (in_len == 0 and amt == 0) {
66006562 // We have detected EOF from `in_fd`.
......@@ -6660,13 +6622,10 @@ pub fn sendfile(
66606622 hdtr = &hdtr_data;
66616623 }
66626624
6663 const adjusted_count = @min(in_len, max_count);
6664
66656625 while (true) {
66666626 var sbytes: off_t = undefined;
6667 const offset = @as(off_t, @bitCast(in_offset));
6668 const err = errno(system.sendfile(in_fd, out_fd, offset, adjusted_count, hdtr, &sbytes, flags));
6669 const amt = @as(usize, @bitCast(sbytes));
6627 const err = errno(system.sendfile(in_fd, out_fd, @bitCast(in_offset), @min(in_len, max_count), hdtr, &sbytes, flags));
6628 const amt: usize = @bitCast(sbytes);
66706629 switch (err) {
66716630 .SUCCESS => return amt,
66726631
......@@ -6733,15 +6692,10 @@ pub fn sendfile(
67336692 hdtr = &hdtr_data;
67346693 }
67356694
6736 const adjusted_count_temporary = @min(in_len, @as(u63, max_count));
6737 // TODO we should not need this int cast; improve the return type of `@min`
6738 const adjusted_count = @as(u63, @intCast(adjusted_count_temporary));
6739
67406695 while (true) {
6741 var sbytes: off_t = adjusted_count;
6742 const signed_offset = @as(i64, @bitCast(in_offset));
6743 const err = errno(system.sendfile(in_fd, out_fd, signed_offset, &sbytes, hdtr, flags));
6744 const amt = @as(usize, @bitCast(sbytes));
6696 var sbytes: off_t = @min(in_len, max_count);
6697 const err = errno(system.sendfile(in_fd, out_fd, @bitCast(in_offset), &sbytes, hdtr, flags));
6698 const amt: usize = @bitCast(sbytes);
67456699 switch (err) {
67466700 .SUCCESS => return amt,
67476701
......@@ -6786,9 +6740,7 @@ pub fn sendfile(
67866740 rw: {
67876741 var buf: [8 * 4096]u8 = undefined;
67886742 // Here we match BSD behavior, making a zero count value send as many bytes as possible.
6789 const adjusted_count_tmp = if (in_len == 0) buf.len else @min(buf.len, in_len);
6790 // TODO we should not need this cast; improve return type of @min
6791 const adjusted_count = @as(usize, @intCast(adjusted_count_tmp));
6743 const adjusted_count = if (in_len == 0) buf.len else @min(buf.len, in_len);
67926744 const amt_read = try pread(in_fd, buf[0..adjusted_count], in_offset);
67936745 if (amt_read == 0) {
67946746 if (in_len == 0) {
......@@ -6859,14 +6811,14 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
68596811 std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 })) and
68606812 has_copy_file_range_syscall.load(.Monotonic)))
68616813 {
6862 var off_in_copy = @as(i64, @bitCast(off_in));
6863 var off_out_copy = @as(i64, @bitCast(off_out));
6814 var off_in_copy: i64 = @bitCast(off_in);
6815 var off_out_copy: i64 = @bitCast(off_out);
68646816
68656817 while (true) {
68666818 const rc = system.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags);
68676819 if (builtin.os.tag == .freebsd) {
68686820 switch (system.getErrno(rc)) {
6869 .SUCCESS => return @as(usize, @intCast(rc)),
6821 .SUCCESS => return @intCast(rc),
68706822 .BADF => return error.FilesOpenedWithWrongFlags,
68716823 .FBIG => return error.FileTooBig,
68726824 .IO => return error.InputOutput,
......@@ -6879,7 +6831,7 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
68796831 }
68806832 } else { // assume linux
68816833 switch (system.getErrno(rc)) {
6882 .SUCCESS => return @as(usize, @intCast(rc)),
6834 .SUCCESS => return @intCast(rc),
68836835 .BADF => return error.FilesOpenedWithWrongFlags,
68846836 .FBIG => return error.FileTooBig,
68856837 .IO => return error.InputOutput,
......@@ -6902,11 +6854,8 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
69026854 }
69036855
69046856 var buf: [8 * 4096]u8 = undefined;
6905 const adjusted_count = @min(buf.len, len);
6906 const amt_read = try pread(fd_in, buf[0..adjusted_count], off_in);
6907 // TODO without @as the line below fails to compile for wasm32-wasi:
6908 // error: integer value 0 cannot be coerced to type 'os.PWriteError!usize'
6909 if (amt_read == 0) return @as(usize, 0);
6857 const amt_read = try pread(fd_in, buf[0..@min(buf.len, len)], off_in);
6858 if (amt_read == 0) return 0;
69106859 return pwrite(fd_out, buf[0..amt_read], off_out);
69116860}
69126861
......@@ -6932,11 +6881,11 @@ pub fn poll(fds: []pollfd, timeout: i32) PollError!usize {
69326881 else => |err| return windows.unexpectedWSAError(err),
69336882 }
69346883 } else {
6935 return @as(usize, @intCast(rc));
6884 return @intCast(rc);
69366885 }
69376886 } else {
69386887 switch (errno(rc)) {
6939 .SUCCESS => return @as(usize, @intCast(rc)),
6888 .SUCCESS => return @intCast(rc),
69406889 .FAULT => unreachable,
69416890 .INTR => continue,
69426891 .INVAL => unreachable,
......@@ -6966,7 +6915,7 @@ pub fn ppoll(fds: []pollfd, timeout: ?*const timespec, mask: ?*const sigset_t) P
69666915 const fds_count = math.cast(nfds_t, fds.len) orelse return error.SystemResources;
69676916 const rc = system.ppoll(fds.ptr, fds_count, ts_ptr, mask);
69686917 switch (errno(rc)) {
6969 .SUCCESS => return @as(usize, @intCast(rc)),
6918 .SUCCESS => return @intCast(rc),
69706919 .FAULT => unreachable,
69716920 .INTR => return error.SignalInterrupt,
69726921 .INVAL => unreachable,
......@@ -7033,11 +6982,11 @@ pub fn recvfrom(
70336982 else => |err| return windows.unexpectedWSAError(err),
70346983 }
70356984 } else {
7036 return @as(usize, @intCast(rc));
6985 return @intCast(rc);
70376986 }
70386987 } else {
70396988 switch (errno(rc)) {
7040 .SUCCESS => return @as(usize, @intCast(rc)),
6989 .SUCCESS => return @intCast(rc),
70416990 .BADF => unreachable, // always a race condition
70426991 .FAULT => unreachable,
70436992 .INVAL => unreachable,
......@@ -7076,7 +7025,7 @@ pub fn dn_expand(
70767025 // loop invariants: p<end, dest<dend
70777026 if ((p[0] & 0xc0) != 0) {
70787027 if (p + 1 == end) return error.InvalidDnsPacket;
7079 const j = ((p[0] & @as(usize, 0x3f)) << 8) | p[1];
7028 const j = @as(usize, p[0] & 0x3f) << 8 | p[1];
70807029 if (len == std.math.maxInt(usize)) len = @intFromPtr(p) + 2 - @intFromPtr(comp_dn.ptr);
70817030 if (j >= msg.len) return error.InvalidDnsPacket;
70827031 p = msg.ptr + j;
......@@ -7130,7 +7079,7 @@ pub const SetSockOptError = error{
71307079/// Set a socket's options.
71317080pub fn setsockopt(fd: socket_t, level: u32, optname: u32, opt: []const u8) SetSockOptError!void {
71327081 if (builtin.os.tag == .windows) {
7133 const rc = windows.ws2_32.setsockopt(fd, @as(i32, @intCast(level)), @as(i32, @intCast(optname)), opt.ptr, @as(i32, @intCast(opt.len)));
7082 const rc = windows.ws2_32.setsockopt(fd, @intCast(level), @intCast(optname), opt.ptr, @intCast(opt.len));
71347083 if (rc == windows.ws2_32.SOCKET_ERROR) {
71357084 switch (windows.ws2_32.WSAGetLastError()) {
71367085 .WSANOTINITIALISED => unreachable,
......@@ -7143,7 +7092,7 @@ pub fn setsockopt(fd: socket_t, level: u32, optname: u32, opt: []const u8) SetSo
71437092 }
71447093 return;
71457094 } else {
7146 switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @as(socklen_t, @intCast(opt.len))))) {
7095 switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @intCast(opt.len)))) {
71477096 .SUCCESS => {},
71487097 .BADF => unreachable, // always a race condition
71497098 .NOTSOCK => unreachable, // always a race condition
......@@ -7180,7 +7129,7 @@ pub fn memfd_createZ(name: [*:0]const u8, flags: u32) MemFdCreateError!fd_t {
71807129 const getErrno = if (use_c) std.c.getErrno else linux.getErrno;
71817130 const rc = sys.memfd_create(name, flags);
71827131 switch (getErrno(rc)) {
7183 .SUCCESS => return @as(fd_t, @intCast(rc)),
7132 .SUCCESS => return @intCast(rc),
71847133 .FAULT => unreachable, // name has invalid memory
71857134 .INVAL => unreachable, // name/flags are faulty
71867135 .NFILE => return error.SystemFdQuotaExceeded,
......@@ -7330,7 +7279,7 @@ pub fn ioctl_SIOCGIFINDEX(fd: fd_t, ifr: *ifreq) IoCtl_SIOCGIFINDEX_Error!void {
73307279pub fn signalfd(fd: fd_t, mask: *const sigset_t, flags: u32) !fd_t {
73317280 const rc = system.signalfd(fd, mask, flags);
73327281 switch (errno(rc)) {
7333 .SUCCESS => return @as(fd_t, @intCast(rc)),
7282 .SUCCESS => return @intCast(rc),
73347283 .BADF, .INVAL => unreachable,
73357284 .NFILE => return error.SystemFdQuotaExceeded,
73367285 .NOMEM => return error.SystemResources,
......@@ -7438,7 +7387,7 @@ pub fn prctl(option: PR, args: anytype) PrctlError!u31 {
74387387
74397388 const rc = system.prctl(@intFromEnum(option), buf[0], buf[1], buf[2], buf[3]);
74407389 switch (errno(rc)) {
7441 .SUCCESS => return @as(u31, @intCast(rc)),
7390 .SUCCESS => return @intCast(rc),
74427391 .ACCES => return error.AccessDenied,
74437392 .BADF => return error.InvalidFileDescriptor,
74447393 .FAULT => return error.InvalidAddress,
......@@ -7619,7 +7568,7 @@ pub fn perf_event_open(
76197568) PerfEventOpenError!fd_t {
76207569 const rc = linux.perf_event_open(attr, pid, cpu, group_fd, flags);
76217570 switch (errno(rc)) {
7622 .SUCCESS => return @as(fd_t, @intCast(rc)),
7571 .SUCCESS => return @intCast(rc),
76237572 .@"2BIG" => return error.TooBig,
76247573 .ACCES => return error.PermissionDenied,
76257574 .BADF => unreachable, // group_fd file descriptor is not valid.
......@@ -7654,7 +7603,7 @@ pub const TimerFdSetError = TimerFdGetError || error{Canceled};
76547603pub fn timerfd_create(clokid: i32, flags: linux.TFD) TimerFdCreateError!fd_t {
76557604 const rc = linux.timerfd_create(clokid, flags);
76567605 return switch (errno(rc)) {
7657 .SUCCESS => @as(fd_t, @intCast(rc)),
7606 .SUCCESS => @intCast(rc),
76587607 .INVAL => unreachable,
76597608 .MFILE => return error.ProcessFdQuotaExceeded,
76607609 .NFILE => return error.SystemFdQuotaExceeded,
......@@ -7697,7 +7646,6 @@ pub fn timerfd_gettime(fd: i32) TimerFdGetError!linux.itimerspec {
76977646pub const PtraceError = error{
76987647 DeviceBusy,
76997648 InputOutput,
7700 Overflow,
77017649 ProcessNotFound,
77027650 PermissionDenied,
77037651} || UnexpectedError;
......@@ -7719,10 +7667,10 @@ pub fn ptrace(request: u32, pid: pid_t, addr: usize, signal: usize) PtraceError!
77197667 },
77207668
77217669 .macos, .ios, .tvos, .watchos => switch (errno(darwin.ptrace(
7722 math.cast(i32, request) orelse return error.Overflow,
7670 @intCast(request),
77237671 pid,
7724 @as(?[*]u8, @ptrFromInt(addr)),
7725 math.cast(i32, signal) orelse return error.Overflow,
7672 @ptrFromInt(addr),
7673 @intCast(signal),
77267674 ))) {
77277675 .SUCCESS => {},
77287676 .SRCH => error.ProcessNotFound,