Skip to content

std lib test suite: fix some illegal behavior found by valgrind #24906

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/std/math/big/int.zig
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ pub const Mutable = struct {

@memset(rma.limbs[0..req_limbs], 0);

llmulacc(.add, allocator, rma.limbs, a_limbs, b_limbs);
llmulacc(.add, allocator, rma.limbs[0..req_limbs], a_limbs, b_limbs);
rma.normalize(@min(req_limbs, a.limbs.len + b.limbs.len));
rma.positive = (a.positive == b.positive);
rma.truncate(rma.toConst(), signedness, bit_count);
Expand Down
18 changes: 12 additions & 6 deletions lib/std/os/linux/IoUring.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,9 @@ pub const BufferGroup = struct {
pub fn get(self: *BufferGroup, cqe: linux.io_uring_cqe) ![]u8 {
const buffer_id = try cqe.buffer_id();
const used_len = @as(usize, @intCast(cqe.res));
return self.get_by_id(buffer_id)[0..used_len];
const buf = self.get_by_id(buffer_id)[0..used_len];
std.valgrind.memcheck.makeMemDefinedIfAddressable(buf);
return buf;
}

// Release buffer from CQE to the kernel.
Expand Down Expand Up @@ -2178,7 +2180,7 @@ test "write_fixed/read_fixed" {
defer file.close();
const fd = file.handle;

var raw_buffers: [2][11]u8 = undefined;
var raw_buffers: [2][11]u8 = @splat(@splat(0));
// First buffer will be written to the file.
@memset(&raw_buffers[0], 'z');
raw_buffers[0][0.."foobar".len].* = "foobar".*;
Expand Down Expand Up @@ -2704,6 +2706,8 @@ test "statx" {
.BADF => return error.SkipZigTest,
else => |errno| std.debug.panic("unhandled errno: {}", .{errno}),
}
// buf is initialized after a successful copy_cqe
std.valgrind.memcheck.makeMemDefinedIfAddressable(std.mem.sliceAsBytes(std.mem.asBytes(&buf)));
try testing.expectEqual(linux.io_uring_cqe{
.user_data = 0xaaaaaaaa,
.res = 0,
Expand Down Expand Up @@ -3206,6 +3210,7 @@ test "provide_buffers: read" {
const buffer_len = 128;

var buffers: [4][buffer_len]u8 = undefined;
std.valgrind.memcheck.makeMemDefinedIfAddressable(std.mem.sliceAsBytes(&buffers));

// Provide 4 buffers

Expand Down Expand Up @@ -3338,6 +3343,7 @@ test "remove_buffers" {
const buffer_len = 128;

var buffers: [4][buffer_len]u8 = undefined;
std.valgrind.memcheck.makeMemDefinedIfAddressable(std.mem.sliceAsBytes(&buffers));
Copy link
Author

@CorruptedVor CorruptedVor Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I move this to around 3392, after ring.copy_cqe()? that is when it should be defined, not right here

(if that was not the case the check for a zero-initialized later would fail)


// Provide 4 buffers

Expand Down Expand Up @@ -4052,7 +4058,7 @@ test "waitid" {
posix.exit(7);
}

var siginfo: posix.siginfo_t = undefined;
var siginfo = std.mem.zeroes(posix.siginfo_t);
_ = try ring.waitid(0, .PID, pid, &siginfo, posix.W.EXITED, 0);

try testing.expectEqual(1, try ring.submit());
Expand Down Expand Up @@ -4416,9 +4422,7 @@ fn expect_buf_grp_cqe(
try testing.expectEqual(posix.E.SUCCESS, cqe.err());

// get buffer from pool
const buffer_id = try cqe.buffer_id();
const len = @as(usize, @intCast(cqe.res));
const buf = buf_grp.get_by_id(buffer_id)[0..len];
const buf = try buf_grp.get(cqe);
try testing.expectEqualSlices(u8, expected, buf);

return cqe;
Expand Down Expand Up @@ -4599,6 +4603,8 @@ fn testSendRecv(ring: *IoUring, send_fd: posix.socket_t, recv_fd: posix.socket_t
try testing.expectEqual(posix.E.SUCCESS, cqe.err());
recv_len += @intCast(cqe.res);
}
// if this is not the case, the check fails
std.valgrind.memcheck.makeMemDefinedIfAddressable(&buffer_recv);

// inspect recv buffer
try testing.expectEqualSlices(u8, buffer_send, buffer_recv[0..buffer_send.len]);
Expand Down
5 changes: 1 addition & 4 deletions lib/std/os/linux/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ test "fadvise" {
var file = try tmp.dir.createFile(tmp_file_name, .{});
defer file.close();

var buf: [2048]u8 = undefined;
try file.writeAll(&buf);

const ret = linux.fadvise(file.handle, 0, 0, linux.POSIX_FADV.SEQUENTIAL);
try expectEqual(@as(usize, 0), ret);
}
Expand Down Expand Up @@ -244,7 +241,7 @@ test "futex v1" {
try expectEqual(0, rc);

// CMP_REQUEUE - val3 mismatch
rc = linux.futex(&lock.raw, .{ .cmd = .CMP_REQUEUE, .private = true }, 2, .{ .val2 = 0 }, null, 99);
rc = linux.futex(&lock.raw, .{ .cmd = .CMP_REQUEUE, .private = true }, 2, .{ .val2 = 0 }, &lock.raw, 99);
try expectEqual(.AGAIN, linux.E.init(rc));

// CMP_REQUEUE - requeue (but no waiters, so ... not much)
Expand Down