authorgravatar for joran@ronomon.comJoran Dirk Greef <joran@ronomon.com> 2020-09-20 14:59:40+02:00
committergravatar for joran@ronomon.comJoran Dirk Greef <joran@ronomon.com> 2020-09-20 14:59:40+02:00
logb672dc7abfe318623e88afd2e9ccaffbf38eb401
tree7195a870ff9ac6863d2d9d83413c6255c935ddae
parentabebacda322074c040778aaca5347c8cd714362e

Use os.fd_t instead of i32 and assert against c_int for syscall safety


1 files changed, 8 insertions(+), 7 deletions(-)

lib/std/os/linux/io_uring.zig+8-7
...@@ -15,7 +15,7 @@ const io_uring_sqe = linux.io_uring_sqe;...@@ -15,7 +15,7 @@ const io_uring_sqe = linux.io_uring_sqe;
15const io_uring_cqe = linux.io_uring_cqe;15const io_uring_cqe = linux.io_uring_cqe;
1616
17pub const IO_Uring = struct {17pub const IO_Uring = struct {
18 fd: i32 = -1,18 fd: os.fd_t = -1,
19 sq: SubmissionQueue,19 sq: SubmissionQueue,
20 cq: CompletionQueue,20 cq: CompletionQueue,
21 flags: u32,21 flags: u32,
...@@ -76,7 +76,7 @@ pub const IO_Uring = struct {...@@ -76,7 +76,7 @@ pub const IO_Uring = struct {
76 linux.ENOSYS => return error.SystemOutdated,76 linux.ENOSYS => return error.SystemOutdated,
77 else => |errno| return os.unexpectedErrno(errno)77 else => |errno| return os.unexpectedErrno(errno)
78 }78 }
79 const fd = @intCast(i32, res);79 const fd = @intCast(os.fd_t, res);
80 assert(fd >= 0);80 assert(fd >= 0);
81 errdefer os.close(fd);81 errdefer os.close(fd);
8282
...@@ -510,8 +510,9 @@ pub const IO_Uring = struct {...@@ -510,8 +510,9 @@ pub const IO_Uring = struct {
510 /// Registering file descriptors will wait for the ring to idle.510 /// Registering file descriptors will wait for the ring to idle.
511 /// Files are automatically unregistered by the kernel when the ring is torn down.511 /// Files are automatically unregistered by the kernel when the ring is torn down.
512 /// An application need unregister only if it wants to register a new array of file descriptors.512 /// An application need unregister only if it wants to register a new array of file descriptors.
513 pub fn register_files(self: *IO_Uring, fds: []const i32) !void {513 pub fn register_files(self: *IO_Uring, fds: []const os.fd_t) !void {
514 assert(self.fd >= 0);514 assert(self.fd >= 0);
515 comptime assert(@sizeOf(os.fd_t) == @sizeOf(c_int));
515 const res = linux.io_uring_register(516 const res = linux.io_uring_register(
516 self.fd,517 self.fd,
517 .REGISTER_FILES,518 .REGISTER_FILES,
...@@ -575,7 +576,7 @@ pub const SubmissionQueue = struct {...@@ -575,7 +576,7 @@ pub const SubmissionQueue = struct {
575 sqe_head: u32 = 0,576 sqe_head: u32 = 0,
576 sqe_tail: u32 = 0,577 sqe_tail: u32 = 0,
577 578
578 pub fn init(fd: i32, p: io_uring_params) !SubmissionQueue {579 pub fn init(fd: os.fd_t, p: io_uring_params) !SubmissionQueue {
579 assert(fd >= 0);580 assert(fd >= 0);
580 assert((p.features & linux.IORING_FEAT_SINGLE_MMAP) != 0);581 assert((p.features & linux.IORING_FEAT_SINGLE_MMAP) != 0);
581 const size = std.math.max(582 const size = std.math.max(
...@@ -641,7 +642,7 @@ pub const CompletionQueue = struct {...@@ -641,7 +642,7 @@ pub const CompletionQueue = struct {
641 overflow: *u32,642 overflow: *u32,
642 cqes: []io_uring_cqe,643 cqes: []io_uring_cqe,
643644
644 pub fn init(fd: i32, p: io_uring_params, sq: SubmissionQueue) !CompletionQueue {645 pub fn init(fd: os.fd_t, p: io_uring_params, sq: SubmissionQueue) !CompletionQueue {
645 assert(fd >= 0);646 assert(fd >= 0);
646 assert((p.features & linux.IORING_FEAT_SINGLE_MMAP) != 0);647 assert((p.features & linux.IORING_FEAT_SINGLE_MMAP) != 0);
647 const mmap = sq.mmap;648 const mmap = sq.mmap;
...@@ -678,7 +679,7 @@ test "queue_nop" {...@@ -678,7 +679,7 @@ test "queue_nop" {
678 };679 };
679 defer {680 defer {
680 ring.deinit();681 ring.deinit();
681 testing.expectEqual(@as(i32, -1), ring.fd);682 testing.expectEqual(@as(os.fd_t, -1), ring.fd);
682 }683 }
683684
684 var sqe = try ring.queue_nop(@intCast(u64, 0xaaaaaaaa));685 var sqe = try ring.queue_nop(@intCast(u64, 0xaaaaaaaa));
...@@ -754,7 +755,7 @@ test "queue_readv" {...@@ -754,7 +755,7 @@ test "queue_readv" {
754 // https://github.com/torvalds/linux/blob/v5.4/fs/io_uring.c#L3119-L3124 vs755 // https://github.com/torvalds/linux/blob/v5.4/fs/io_uring.c#L3119-L3124 vs
755 // https://github.com/torvalds/linux/blob/v5.8/fs/io_uring.c#L6687-L6691756 // https://github.com/torvalds/linux/blob/v5.8/fs/io_uring.c#L6687-L6691
756 // We therefore avoid stressing sparse fd sets here:757 // We therefore avoid stressing sparse fd sets here:
757 var registered_fds = [_]i32{0} ** 1;758 var registered_fds = [_]os.fd_t{0} ** 1;
758 const fd_index = 0;759 const fd_index = 0;
759 registered_fds[fd_index] = fd;760 registered_fds[fd_index] = fd;
760 try ring.register_files(registered_fds[0..]);761 try ring.register_files(registered_fds[0..]);