authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-01 14:30:00-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-02 16:30:59-07:00
log9354036dc2e547f52ec3c445b83f4f5a06c1a132
treec7dba6bd7a37175ed67393c4809c80d927d74ae1
parent47f18ee6a058966bbe06e83e26a5b6ed67f368cb

std.os.linux: remove sendmmsg workaround

This "fix" is too opinionated to belong here. Better instead to document the pitfalls.

1 files changed, 9 insertions(+), 42 deletions(-)

lib/std/os/linux.zig+9-42
......@@ -1,10 +1,8 @@
11//! This file provides the system interface functions for Linux matching those
22//! that are provided by libc, whether or not libc is linked. The following
33//! abstractions are made:
4//! * Work around kernel bugs and limitations. For example, see sendmmsg.
54//! * Implement all the syscalls in the same way that libc functions will
65//! provide `rename` when only the `renameat` syscall exists.
7//! * Does not support POSIX thread cancellation.
86const std = @import("../std.zig");
97const builtin = @import("builtin");
108const assert = std.debug.assert;
......@@ -1768,7 +1766,7 @@ pub fn seteuid(euid: uid_t) usize {
17681766 // id will not be changed. Since uid_t is unsigned, this wraps around to the
17691767 // max value in C.
17701768 comptime assert(@typeInfo(uid_t) == .int and @typeInfo(uid_t).int.signedness == .unsigned);
1771 return setresuid(std.math.maxInt(uid_t), euid, std.math.maxInt(uid_t));
1769 return setresuid(maxInt(uid_t), euid, maxInt(uid_t));
17721770}
17731771
17741772pub fn setegid(egid: gid_t) usize {
......@@ -1779,7 +1777,7 @@ pub fn setegid(egid: gid_t) usize {
17791777 // id will not be changed. Since gid_t is unsigned, this wraps around to the
17801778 // max value in C.
17811779 comptime assert(@typeInfo(uid_t) == .int and @typeInfo(uid_t).int.signedness == .unsigned);
1782 return setresgid(std.math.maxInt(gid_t), egid, std.math.maxInt(gid_t));
1780 return setresgid(maxInt(gid_t), egid, maxInt(gid_t));
17831781}
17841782
17851783pub fn getresuid(ruid: *uid_t, euid: *uid_t, suid: *uid_t) usize {
......@@ -2013,44 +2011,13 @@ pub fn sendmsg(fd: i32, msg: *const msghdr_const, flags: u32) usize {
20132011 }
20142012}
20152013
2014/// Warning: libc is defined to have incompatible integer types with the
2015/// corresponding kernel data structures for this syscall.
2016///
2017/// Warning: on 64-bit systems, if any message length would exceed `maxInt(i32)`,
2018/// number of bytes sent cannot be determined, because the kernel uses `ssize_t`
2019/// for `sendmsg` return value but `int` for the corresponding values here.
20162020pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize {
2017 if (@typeInfo(usize).int.bits > @typeInfo(@typeInfo(mmsghdr).@"struct".fields[1].type).int.bits) {
2018 // workaround kernel brokenness:
2019 // if adding up all iov_len overflows a i32 then split into multiple calls
2020 // see https://www.openwall.com/lists/musl/2014/06/07/5
2021 const kvlen = if (vlen > IOV_MAX) IOV_MAX else vlen; // matches kernel
2022 var next_unsent: usize = 0;
2023 for (msgvec[0..kvlen], 0..) |*msg, i| {
2024 var size: i32 = 0;
2025 const msg_iovlen = @as(usize, @intCast(msg.hdr.iovlen)); // kernel side this is treated as unsigned
2026 for (msg.hdr.iov[0..msg_iovlen]) |iov| {
2027 if (iov.len > std.math.maxInt(i32) or @addWithOverflow(size, @as(i32, @intCast(iov.len)))[1] != 0) {
2028 // batch-send all messages up to the current message
2029 if (next_unsent < i) {
2030 const batch_size = i - next_unsent;
2031 const r = syscall4(.sendmmsg, @as(usize, @bitCast(@as(isize, fd))), @intFromPtr(&msgvec[next_unsent]), batch_size, flags);
2032 if (E.init(r) != .SUCCESS) return next_unsent;
2033 if (r < batch_size) return next_unsent + r;
2034 }
2035 // send current message as own packet
2036 const r = sendmsg(fd, &msg.hdr, flags);
2037 if (E.init(r) != .SUCCESS) return r;
2038 // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe.
2039 msg.len = @as(u32, @intCast(r));
2040 next_unsent = i + 1;
2041 break;
2042 }
2043 size += @intCast(iov.len);
2044 }
2045 }
2046 if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG.EOR)
2047 const batch_size = kvlen - next_unsent;
2048 const r = syscall4(.sendmmsg, @as(usize, @bitCast(@as(isize, fd))), @intFromPtr(&msgvec[next_unsent]), batch_size, flags);
2049 if (E.init(r) != .SUCCESS) return r;
2050 return next_unsent + r;
2051 }
2052 return kvlen;
2053 }
20542021 return syscall4(.sendmmsg, @as(usize, @bitCast(@as(isize, fd))), @intFromPtr(msgvec), vlen, flags);
20552022}
20562023
......@@ -8613,7 +8580,7 @@ pub const PR = enum(i32) {
86138580 pub const SET_MM_MAP = 14;
86148581 pub const SET_MM_MAP_SIZE = 15;
86158582
8616 pub const SET_PTRACER_ANY = std.math.maxInt(c_ulong);
8583 pub const SET_PTRACER_ANY = maxInt(c_ulong);
86178584
86188585 pub const FP_MODE_FR = 1 << 0;
86198586 pub const FP_MODE_FRE = 1 << 1;