From 32d06615131f7c03d93f555749865d6398818965 Mon Sep 17 00:00:00 2001 From: Andrei Tatar Date: Thu, 6 Feb 2025 13:49:13 +0100 Subject: [PATCH] lib/ukfile: API: Change iovec length to size_t Previously the ukfile API would take the size of a passed iovec as a signed int, a design oversight copied over from the binary syscall API. Negative iovec lengths make no sense and should not be exposed by our internal API. This change makes iovec lengths unsigned size_t for ukfiles. The posix-socket socket ops API is similarly changed to use size_t. External socket implementations will need updating. Signed-off-by: Andrei Tatar Approved-by: Sergiu Moga Approved-by: Razvan Deaconescu Reviewed-by: Sergiu Moga GitHub-Closes: #1580 --- lib/posix-eventfd/eventfd.c | 4 ++-- lib/posix-fdio/fdio.c | 12 ++++++------ lib/posix-pipe/pipe.c | 8 ++++---- lib/posix-socket/include/uk/socket_driver.h | 8 ++++---- lib/posix-socket/socket.c | 4 ++-- lib/posix-timerfd/timerfd.c | 2 +- lib/posix-tty/pseudo.c | 14 ++++++++------ lib/posix-tty/serial.c | 8 ++++---- lib/posix-unixsocket/unixsock.c | 4 ++-- lib/ukfile/file-nops.c | 6 ++++-- lib/ukfile/include/uk/file.h | 6 +++--- lib/ukfile/include/uk/file/iovutil.h | 18 +++++++++--------- lib/ukfile/include/uk/file/nops.h | 4 ++-- 13 files changed, 51 insertions(+), 47 deletions(-) diff --git a/lib/posix-eventfd/eventfd.c b/lib/posix-eventfd/eventfd.c index 078590bf4..ef97acfa2 100644 --- a/lib/posix-eventfd/eventfd.c +++ b/lib/posix-eventfd/eventfd.c @@ -35,7 +35,7 @@ struct evfd_alloc { static ssize_t evfd_read(const struct uk_file *f, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags __unused) { int semaphore; @@ -75,7 +75,7 @@ static ssize_t evfd_read(const struct uk_file *f, static ssize_t evfd_write(const struct uk_file *f, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags __unused) { uint64_t add; diff --git a/lib/posix-fdio/fdio.c b/lib/posix-fdio/fdio.c index 912221564..dc8b2a673 100644 --- a/lib/posix-fdio/fdio.c +++ b/lib/posix-fdio/fdio.c @@ -77,7 +77,7 @@ ssize_t uk_sys_preadv(struct uk_ofile *of, const struct iovec *iov, int iovcnt, for (;;) { if (iolock) uk_file_rlock(f); - r = uk_file_read(f, iov, iovcnt, offset, flags); + r = uk_file_read(f, iov, (size_t)iovcnt, offset, flags); if (iolock) uk_file_runlock(f); if (!_SHOULD_BLOCK(r, mode)) @@ -124,7 +124,7 @@ ssize_t uk_sys_readv(struct uk_ofile *of, const struct iovec *iov, int iovcnt) if (iolock) uk_file_rlock(f); - r = uk_file_read(f, iov, iovcnt, off, flags); + r = uk_file_read(f, iov, (size_t)iovcnt, off, flags); if (iolock) uk_file_runlock(f); if (!_SHOULD_BLOCK(r, mode)) @@ -192,7 +192,7 @@ ssize_t uk_sys_preadv2(struct uk_ofile *of, const struct iovec *iov, int iovcnt, if (iolock) uk_file_rlock(f); - r = uk_file_read(f, iov, iovcnt, off, xflags); + r = uk_file_read(f, iov, (size_t)iovcnt, off, xflags); if (iolock) uk_file_runlock(f); if (!_SHOULD_BLOCK(r, mode)) @@ -237,7 +237,7 @@ ssize_t uk_sys_pwritev(struct uk_ofile *of, const struct iovec *iov, int iovcnt, for (;;) { if (iolock) uk_file_wlock(f); - r = uk_file_write(f, iov, iovcnt, offset, flags); + r = uk_file_write(f, iov, (size_t)iovcnt, offset, flags); if (iolock) uk_file_wunlock(f); if (!_SHOULD_BLOCK(r, mode)) @@ -292,7 +292,7 @@ ssize_t uk_sys_writev(struct uk_ofile *of, const struct iovec *iov, int iovcnt) } if (likely(off >= 0)) - r = uk_file_write(f, iov, iovcnt, off, flags); + r = uk_file_write(f, iov, (size_t)iovcnt, off, flags); else r = off; @@ -372,7 +372,7 @@ ssize_t uk_sys_pwritev2(struct uk_ofile *of, const struct iovec *iov, } if (likely(off >= 0)) - r = uk_file_write(f, iov, iovcnt, off, xflags); + r = uk_file_write(f, iov, (size_t)iovcnt, off, xflags); else r = off; diff --git a/lib/posix-pipe/pipe.c b/lib/posix-pipe/pipe.c index 3b06bc970..dc8006600 100644 --- a/lib/posix-pipe/pipe.c +++ b/lib/posix-pipe/pipe.c @@ -127,11 +127,11 @@ static void pipebuf_iovwrite(char *buf, pipeidx head, _pipebuf_write(buf, head, (const char *)iov[i].iov_base, n); } -static ssize_t _iovsz(const struct iovec *iov, int iovcnt) +static ssize_t _iovsz(const struct iovec *iov, size_t iovcnt) { size_t ret = 0; - for (int i = 0; i < iovcnt; i++) + for (size_t i = 0; i < iovcnt; i++) if (iov[i].iov_len) { if (likely(iov[i].iov_base)) ret += iov[i].iov_len; @@ -142,7 +142,7 @@ static ssize_t _iovsz(const struct iovec *iov, int iovcnt) } static ssize_t pipe_read(const struct uk_file *f, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags __unused) { ssize_t toread; @@ -230,7 +230,7 @@ static ssize_t pipe_read(const struct uk_file *f, } static ssize_t pipe_write(const struct uk_file *f, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags) { struct pipe_node *d; diff --git a/lib/posix-socket/include/uk/socket_driver.h b/lib/posix-socket/include/uk/socket_driver.h index 878e68bcb..b712b6d36 100644 --- a/lib/posix-socket/include/uk/socket_driver.h +++ b/lib/posix-socket/include/uk/socket_driver.h @@ -398,7 +398,7 @@ typedef void (*posix_socket_socketpair_post_func_t)( * @return The number of bytes written on success, -errno otherwise */ typedef ssize_t (*posix_socket_write_func_t)(posix_sock *sock, - const struct iovec *iov, int iovcnt); + const struct iovec *iov, size_t iovcnt); /** * Read from a socket file descriptor. @@ -411,7 +411,7 @@ typedef ssize_t (*posix_socket_write_func_t)(posix_sock *sock, * @return The number of bytes read on success, -errno otherwise */ typedef ssize_t (*posix_socket_read_func_t)(posix_sock *sock, - const struct iovec *iov, int iovcnt); + const struct iovec *iov, size_t iovcnt); /** * Close the socket. @@ -640,7 +640,7 @@ posix_socket_socketpair_post(struct posix_socket_driver *d, static inline ssize_t posix_socket_write(posix_sock *sock, const struct iovec *iov, - int iovcnt) + size_t iovcnt) { struct posix_socket_driver *d = posix_sock_get_driver(sock); @@ -650,7 +650,7 @@ posix_socket_write(posix_sock *sock, const struct iovec *iov, static inline ssize_t posix_socket_read(posix_sock *sock, const struct iovec *iov, - int iovcnt) + size_t iovcnt) { struct posix_socket_driver *d = posix_sock_get_driver(sock); diff --git a/lib/posix-socket/socket.c b/lib/posix-socket/socket.c index ccd43e0f0..641f88834 100644 --- a/lib/posix-socket/socket.c +++ b/lib/posix-socket/socket.c @@ -93,7 +93,7 @@ static struct uk_ofile *socketfd_get(int fd) static ssize_t socket_read(const struct uk_file *sock, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags __unused) { ssize_t ret; @@ -125,7 +125,7 @@ socket_read(const struct uk_file *sock, static ssize_t socket_write(const struct uk_file *sock, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags __unused) { ssize_t ret; diff --git a/lib/posix-timerfd/timerfd.c b/lib/posix-timerfd/timerfd.c index 464a29dcb..e360c7fad 100644 --- a/lib/posix-timerfd/timerfd.c +++ b/lib/posix-timerfd/timerfd.c @@ -129,7 +129,7 @@ static void _timerfd_set(struct timerfd_node *d, const struct itimerspec *set) /* Ops */ static ssize_t timerfd_read(const struct uk_file *f, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags __unused) { struct timerfd_node *d; diff --git a/lib/posix-tty/pseudo.c b/lib/posix-tty/pseudo.c index d351e3f8c..29b19baf9 100644 --- a/lib/posix-tty/pseudo.c +++ b/lib/posix-tty/pseudo.c @@ -22,7 +22,8 @@ static const char VOID_VOLID[] = "void_vol"; static const char ZERO_VOLID[] = "zero_vol"; static ssize_t null_read(const struct uk_file *f __maybe_unused, - const struct iovec *iov __unused, int iovcnt __unused, + const struct iovec *iov __unused, + size_t iovcnt __unused, size_t off __unused, long flags __unused) { UK_ASSERT(f->vol == NULL_VOLID); @@ -30,7 +31,8 @@ static ssize_t null_read(const struct uk_file *f __maybe_unused, } static ssize_t void_read(const struct uk_file *f __maybe_unused, - const struct iovec *iov __unused, int iovcnt __unused, + const struct iovec *iov __unused, + size_t iovcnt __unused, size_t off __unused, long flags __unused) { UK_ASSERT(f->vol == VOID_VOLID); @@ -38,14 +40,14 @@ static ssize_t void_read(const struct uk_file *f __maybe_unused, } static ssize_t zero_read(const struct uk_file *f __maybe_unused, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off __unused, long flags __unused) { ssize_t total = 0; UK_ASSERT(f->vol == ZERO_VOLID); - for (int i = 0; i < iovcnt; i++) { + for (size_t i = 0; i < iovcnt; i++) { if (unlikely(!iov[i].iov_base && iov[i].iov_len)) return -EFAULT; memset(iov[i].iov_base, 0, iov[i].iov_len); @@ -55,7 +57,7 @@ static ssize_t zero_read(const struct uk_file *f __maybe_unused, } static ssize_t null_write(const struct uk_file *f __maybe_unused, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off __unused, long flags __unused) { ssize_t total = 0; @@ -63,7 +65,7 @@ static ssize_t null_write(const struct uk_file *f __maybe_unused, UK_ASSERT(f->vol == NULL_VOLID || f->vol == ZERO_VOLID || f->vol == VOID_VOLID); - for (int i = 0; i < iovcnt; i++) + for (size_t i = 0; i < iovcnt; i++) total += iov[i].iov_len; return total; } diff --git a/lib/posix-tty/serial.c b/lib/posix-tty/serial.c index 00820ce4d..adc230738 100644 --- a/lib/posix-tty/serial.c +++ b/lib/posix-tty/serial.c @@ -88,7 +88,7 @@ static inline __ssz _console_out(const char *buf, __sz len) } static ssize_t serial_read(const struct uk_file *f, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags __unused) { ssize_t total = 0; @@ -100,7 +100,7 @@ static ssize_t serial_read(const struct uk_file *f, if (!uk_file_poll_immediate(f, UKFD_POLLIN)) return 0; - for (int i = 0; i < iovcnt; i++) { + for (size_t i = 0; i < iovcnt; i++) { char *buf = iov[i].iov_base; size_t len = iov[i].iov_len; char *last; @@ -137,7 +137,7 @@ static ssize_t serial_read(const struct uk_file *f, } static ssize_t serial_write(const struct uk_file *f __maybe_unused, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags __unused) { ssize_t total = 0; @@ -146,7 +146,7 @@ static ssize_t serial_write(const struct uk_file *f __maybe_unused, if (unlikely(off)) return -ESPIPE; - for (int i = 0; i < iovcnt; i++) { + for (size_t i = 0; i < iovcnt; i++) { char *buf = iov[i].iov_base; size_t len = iov[i].iov_len; int bytes_written; diff --git a/lib/posix-unixsocket/unixsock.c b/lib/posix-unixsocket/unixsock.c index dd33bf13d..91d3a8113 100644 --- a/lib/posix-unixsocket/unixsock.c +++ b/lib/posix-unixsocket/unixsock.c @@ -887,7 +887,7 @@ ssize_t unix_socket_sendto(posix_sock *file, const void *buf, static ssize_t unix_socket_read(posix_sock *file, - const struct iovec *iov, int iovcnt) + const struct iovec *iov, size_t iovcnt) { struct msghdr msg = { .msg_name = NULL, @@ -903,7 +903,7 @@ ssize_t unix_socket_read(posix_sock *file, static ssize_t unix_socket_write(posix_sock *file, - const struct iovec *iov, int iovcnt) + const struct iovec *iov, size_t iovcnt) { struct msghdr msg = { .msg_name = NULL, diff --git a/lib/ukfile/file-nops.c b/lib/ukfile/file-nops.c index 1109e7a39..2fbbd161a 100644 --- a/lib/ukfile/file-nops.c +++ b/lib/ukfile/file-nops.c @@ -10,14 +10,16 @@ ssize_t uk_file_nop_read(const struct uk_file *f __unused, - const struct iovec *iov __unused, int iovcnt __unused, + const struct iovec *iov __unused, + size_t iovcnt __unused, size_t off __unused, long flags __unused) { return -ENOSYS; } ssize_t uk_file_nop_write(const struct uk_file *f __unused, - const struct iovec *iov __unused, int iovcnt __unused, + const struct iovec *iov __unused, + size_t iovcnt __unused, size_t off __unused, long flags __unused) { return -ENOSYS; diff --git a/lib/ukfile/include/uk/file.h b/lib/ukfile/include/uk/file.h index 1c7255dc8..c4e6cab64 100644 --- a/lib/ukfile/include/uk/file.h +++ b/lib/ukfile/include/uk/file.h @@ -36,7 +36,7 @@ struct uk_file; /* I/O */ typedef ssize_t (*uk_file_io_func)(const struct uk_file *f, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags); /* Info (stat-like & chXXX-like) */ @@ -221,7 +221,7 @@ struct uk_file { /* Operations inlines */ static inline ssize_t uk_file_read(const struct uk_file *f, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags) { return f->ops->read(f, iov, iovcnt, off, flags); @@ -229,7 +229,7 @@ ssize_t uk_file_read(const struct uk_file *f, static inline ssize_t uk_file_write(const struct uk_file *f, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags) { return f->ops->write(f, iov, iovcnt, off, flags); diff --git a/lib/ukfile/include/uk/file/iovutil.h b/lib/ukfile/include/uk/file/iovutil.h index b7b0b1b68..3a8b805b5 100644 --- a/lib/ukfile/include/uk/file/iovutil.h +++ b/lib/ukfile/include/uk/file/iovutil.h @@ -24,11 +24,11 @@ * @return Number of bytes zeroed */ static inline -size_t uk_iov_zero(const struct iovec *iov, int iovcnt, size_t len, - int *iovip, size_t *curp) +size_t uk_iov_zero(const struct iovec *iov, size_t iovcnt, size_t len, + size_t *iovip, size_t *curp) { size_t ret = 0; - int i = *iovip; + size_t i = *iovip; size_t cur = *curp; UK_ASSERT(i < iovcnt); @@ -74,11 +74,11 @@ size_t uk_iov_zero(const struct iovec *iov, int iovcnt, size_t len, * @return Number of bytes copied */ static inline -size_t uk_iov_scatter(const struct iovec *iov, int iovcnt, const char *buf, - size_t len, int *iovip, size_t *curp) +size_t uk_iov_scatter(const struct iovec *iov, size_t iovcnt, const char *buf, + size_t len, size_t *iovip, size_t *curp) { size_t ret = 0; - int i = *iovip; + size_t i = *iovip; size_t cur = *curp; UK_ASSERT(i < iovcnt); @@ -126,11 +126,11 @@ size_t uk_iov_scatter(const struct iovec *iov, int iovcnt, const char *buf, * @return Number of bytes copied */ static inline -size_t uk_iov_gather(char *buf, const struct iovec *iov, int iovcnt, - size_t len, int *iovip, size_t *curp) +size_t uk_iov_gather(char *buf, const struct iovec *iov, size_t iovcnt, + size_t len, size_t *iovip, size_t *curp) { size_t ret = 0; - int i = *iovip; + size_t i = *iovip; size_t cur = *curp; UK_ASSERT(i < iovcnt); diff --git a/lib/ukfile/include/uk/file/nops.h b/lib/ukfile/include/uk/file/nops.h index 82644f516..7c0fe1d93 100644 --- a/lib/ukfile/include/uk/file/nops.h +++ b/lib/ukfile/include/uk/file/nops.h @@ -14,11 +14,11 @@ extern const struct uk_file_ops uk_file_nops; ssize_t uk_file_nop_read(const struct uk_file *f, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags); ssize_t uk_file_nop_write(const struct uk_file *f, - const struct iovec *iov, int iovcnt, + const struct iovec *iov, size_t iovcnt, size_t off, long flags); int uk_file_nop_getstat(const struct uk_file *f, -- 2.39.5