From 9e17cddef6169e3374defefb84631fab59b0917c Mon Sep 17 00:00:00 2001 From: fardragon Date: Sun, 18 Aug 2024 22:18:39 +0200 Subject: [PATCH 1/7] Correct layout of IntInfo according to https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-int --- lib/std/os/linux/bpf/btf.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/std/os/linux/bpf/btf.zig b/lib/std/os/linux/bpf/btf.zig index 39d25014dac3..3988fce349c9 100644 --- a/lib/std/os/linux/bpf/btf.zig +++ b/lib/std/os/linux/bpf/btf.zig @@ -83,13 +83,14 @@ pub const Kind = enum(u5) { /// int kind is followed by this struct pub const IntInfo = packed struct(u32) { bits: u8, - unused: u8, + reserved_1: u8, offset: u8, encoding: enum(u4) { signed = 1 << 0, char = 1 << 1, boolean = 1 << 2, }, + reserved_2: u4, }; test "IntInfo is 32 bits" { From 92f624a479567a68ca16995f8dd8c332605887bb Mon Sep 17 00:00:00 2001 From: fardragon Date: Sun, 18 Aug 2024 22:38:27 +0200 Subject: [PATCH 2/7] Fix VFS errors --- lib/std/os/linux.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index c35059663e2b..1f28477f3a27 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -5095,7 +5095,7 @@ pub const epoll_event = extern struct { pub const VFS_CAP_REVISION_MASK = 0xFF000000; pub const VFS_CAP_REVISION_SHIFT = 24; -pub const VFS_CAP_FLAGS_MASK = ~VFS_CAP_REVISION_MASK; +pub const VFS_CAP_FLAGS_MASK = ~@as(u32, VFS_CAP_REVISION_MASK); pub const VFS_CAP_FLAGS_EFFECTIVE = 0x000001; pub const VFS_CAP_REVISION_1 = 0x01000000; @@ -5113,7 +5113,7 @@ pub const VFS_CAP_REVISION = VFS_CAP_REVISION_2; pub const vfs_cap_data = extern struct { //all of these are mandated as little endian //when on disk. - const Data = struct { + const Data = extern struct { permitted: u32, inheritable: u32, }; From fd698db2a60faf066b0375bc189fcf879bca3002 Mon Sep 17 00:00:00 2001 From: fardragon Date: Sun, 18 Aug 2024 22:46:49 +0200 Subject: [PATCH 3/7] Fix std.os.linux.sendmmsg --- lib/std/os/linux.zig | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 1f28477f3a27..511f6792cc08 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -1740,31 +1740,31 @@ pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize var next_unsent: usize = 0; for (msgvec[0..kvlen], 0..) |*msg, i| { var size: i32 = 0; - const msg_iovlen = @as(usize, @intCast(msg.msg_hdr.msg_iovlen)); // kernel side this is treated as unsigned - for (msg.msg_hdr.msg_iov[0..msg_iovlen]) |iov| { + const msg_iovlen = @as(usize, @intCast(msg.hdr.iovlen)); // kernel side this is treated as unsigned + for (msg.hdr.iov[0..msg_iovlen]) |iov| { if (iov.len > std.math.maxInt(i32) or @addWithOverflow(size, @as(i32, @intCast(iov.len)))[1] != 0) { // batch-send all messages up to the current message if (next_unsent < i) { const batch_size = i - next_unsent; const r = syscall4(.sendmmsg, @as(usize, @bitCast(@as(isize, fd))), @intFromPtr(&msgvec[next_unsent]), batch_size, flags); - if (E.init(r) != 0) return next_unsent; + if (E.init(r) != .SUCCESS) return next_unsent; if (r < batch_size) return next_unsent + r; } // send current message as own packet - const r = sendmsg(fd, &msg.msg_hdr, flags); - if (E.init(r) != 0) return r; + const r = sendmsg(fd, &msg.hdr, flags); + if (E.init(r) != .SUCCESS) return r; // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe. - msg.msg_len = @as(u32, @intCast(r)); + msg.len = @as(u32, @intCast(r)); next_unsent = i + 1; break; } - size += iov.len; + size += @intCast(iov.len); } } if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG.EOR) const batch_size = kvlen - next_unsent; const r = syscall4(.sendmmsg, @as(usize, @bitCast(@as(isize, fd))), @intFromPtr(&msgvec[next_unsent]), batch_size, flags); - if (E.init(r) != 0) return r; + if (E.init(r) != .SUCCESS) return r; return next_unsent + r; } return kvlen; From 9b59d523dbc25173b15a263f0baf463a107c6209 Mon Sep 17 00:00:00 2001 From: fardragon Date: Sun, 18 Aug 2024 23:34:02 +0200 Subject: [PATCH 4/7] Fix std.os.linux.sigismember. Add tests --- lib/std/os/linux.zig | 2 +- lib/std/os/linux/test.zig | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 511f6792cc08..00a2fc40560a 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -1683,7 +1683,7 @@ pub fn sigaddset(set: *sigset_t, sig: u6) void { pub fn sigismember(set: *const sigset_t, sig: u6) bool { const s = sig - 1; - return ((set.*)[@as(usize, @intCast(s)) / usize_bits] & (@as(usize, @intCast(1)) << (s & (usize_bits - 1)))) != 0; + return ((set.*)[@as(usize, @intCast(s)) / usize_bits] & (@as(usize, @intCast(1)) << @intCast(s & (usize_bits - 1)))) != 0; } pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { diff --git a/lib/std/os/linux/test.zig b/lib/std/os/linux/test.zig index 8562d4be8e21..a8ebec47a5ed 100644 --- a/lib/std/os/linux/test.zig +++ b/lib/std/os/linux/test.zig @@ -125,6 +125,23 @@ test "fadvise" { try expectEqual(@as(usize, 0), ret); } +test "sigset_t" { + var sigset = linux.empty_sigset; + + try expectEqual(linux.sigismember(&sigset, linux.SIG.USR1), false); + try expectEqual(linux.sigismember(&sigset, linux.SIG.USR2), false); + + linux.sigaddset(&sigset, linux.SIG.USR1); + + try expectEqual(linux.sigismember(&sigset, linux.SIG.USR1), true); + try expectEqual(linux.sigismember(&sigset, linux.SIG.USR2), false); + + linux.sigaddset(&sigset, linux.SIG.USR2); + + try expectEqual(linux.sigismember(&sigset, linux.SIG.USR1), true); + try expectEqual(linux.sigismember(&sigset, linux.SIG.USR2), true); +} + test { _ = linux.IoUring; } From 3abaa32177dbb228ec95832b06b1d3af93d173da Mon Sep 17 00:00:00 2001 From: fardragon Date: Mon, 19 Aug 2024 18:09:26 +0200 Subject: [PATCH 5/7] Fix futex2 functions --- lib/std/os/linux.zig | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 00a2fc40560a..0ccf04454b8b 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -630,12 +630,12 @@ pub fn futex2_waitv( nr_futexes, flags, @intFromPtr(timeout), - @bitCast(@as(isize, clockid)), + @bitCast(@as(isize, @intFromEnum(clockid))), ); } /// Wait on a futex. -/// Identical to `FUTEX.WAIT`, except it is part of the futex2 family of calls. +/// Identical to `FUTEX.FUTEX_WAIT_BITSET`, except it is part of the futex2 family of calls. pub fn futex2_wait( /// Address of the futex to wait on. uaddr: *const anyopaque, @@ -646,7 +646,7 @@ pub fn futex2_wait( /// `FUTEX2` flags. flags: u32, /// Optional absolute timeout. - timeout: *const timespec, + timeout: ?*const timespec, /// Clock to be used for the timeout, realtime or monotonic. clockid: clockid_t, ) usize { @@ -657,15 +657,15 @@ pub fn futex2_wait( mask, flags, @intFromPtr(timeout), - @bitCast(@as(isize, clockid)), + @bitCast(@as(isize, @intFromEnum(clockid))), ); } -/// Wake a number of futexes. -/// Identical to `FUTEX.WAKE`, except it is part of the futex2 family of calls. +/// Wake a number of waiters. +/// Identical to `FUTEX.FUTEX_WAKE_BITSET`, except it is part of the futex2 family of calls. pub fn futex2_wake( /// Address of the futex(es) to wake. - uaddr: [*]const anyopaque, + uaddr: *const anyopaque, /// Bitmask mask: usize, /// Number of the futexes to wake. From 7569c83360e8cc2bfa56e7820c90db53365ab485 Mon Sep 17 00:00:00 2001 From: fardragon Date: Mon, 19 Aug 2024 18:26:09 +0200 Subject: [PATCH 6/7] Fix comments --- lib/std/os/linux.zig | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 0ccf04454b8b..6af4c670a244 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -635,7 +635,8 @@ pub fn futex2_waitv( } /// Wait on a futex. -/// Identical to `FUTEX.FUTEX_WAIT_BITSET`, except it is part of the futex2 family of calls. +/// Identical to the traditional FUTEX_WAIT_BITSET op, except it is part of the +/// futex2 familiy of calls. pub fn futex2_wait( /// Address of the futex to wait on. uaddr: *const anyopaque, @@ -661,8 +662,9 @@ pub fn futex2_wait( ); } -/// Wake a number of waiters. -/// Identical to `FUTEX.FUTEX_WAKE_BITSET`, except it is part of the futex2 family of calls. +/// Wake a number of futexes. +/// Identical to the traditional FUTEX_WAKE_BITSET op, except it is part of the +/// futex2 family of calls. pub fn futex2_wake( /// Address of the futex(es) to wake. uaddr: *const anyopaque, From a5bd5b21194600b56e9e1b1ab71ec9ffcf3eac95 Mon Sep 17 00:00:00 2001 From: fardragon Date: Wed, 21 Aug 2024 19:51:21 +0200 Subject: [PATCH 7/7] Restore backticks --- lib/std/os/linux.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 6af4c670a244..a29b381c401c 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -635,7 +635,7 @@ pub fn futex2_waitv( } /// Wait on a futex. -/// Identical to the traditional FUTEX_WAIT_BITSET op, except it is part of the +/// Identical to the traditional `FUTEX.FUTEX_WAIT_BITSET` op, except it is part of the /// futex2 familiy of calls. pub fn futex2_wait( /// Address of the futex to wait on. @@ -663,7 +663,7 @@ pub fn futex2_wait( } /// Wake a number of futexes. -/// Identical to the traditional FUTEX_WAKE_BITSET op, except it is part of the +/// Identical to the traditional `FUTEX.FUTEX_WAIT_BITSET` op, except it is part of the /// futex2 family of calls. pub fn futex2_wake( /// Address of the futex(es) to wake.