[libc-commits] [libc] [libc] implement `sys/uio/writev` (PR #124718)

via libc-commits libc-commits at lists.llvm.org
Tue Jan 28 00:04:36 PST 2025


https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/124718

Closes #124694.

>From 34e161c73820630ca8bc60836f17cc71a56475ed Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Tue, 28 Jan 2025 08:03:38 +0000
Subject: [PATCH] impl readv

---
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/include/sys/uio.yaml                 |  8 ++++++
 libc/src/sys/uio/CMakeLists.txt           |  7 ++++++
 libc/src/sys/uio/linux/CMakeLists.txt     | 15 ++++++++++++
 libc/src/sys/uio/linux/readv.cpp          | 27 ++++++++++++++++++++
 libc/src/sys/uio/readv.h                  | 22 +++++++++++++++++
 libc/test/src/sys/uio/CMakeLists.txt      | 18 +++++++++++++-
 libc/test/src/sys/uio/readv_test.cpp      | 30 +++++++++++++++++++++++
 9 files changed, 128 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/sys/uio/linux/readv.cpp
 create mode 100644 libc/src/sys/uio/readv.h
 create mode 100644 libc/test/src/sys/uio/readv_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 09eb51a3f8fc62..99b1187b43e11b 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -355,6 +355,7 @@ set(TARGET_LIBC_ENTRYPOINTS
 
     # sys/uio.h entrypoints
     libc.src.sys.uio.writev
+    libc.src.sys.uio.readv
 )
 
 if(LLVM_LIBC_INCLUDE_SCUDO)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 366e4d34294d15..df85f87140b88b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -355,6 +355,7 @@ set(TARGET_LIBC_ENTRYPOINTS
 
     # sys/uio.h entrypoints
     libc.src.sys.uio.writev
+    libc.src.sys.uio.readv
 )
 
 if(LLVM_LIBC_INCLUDE_SCUDO)
diff --git a/libc/include/sys/uio.yaml b/libc/include/sys/uio.yaml
index 87c5bdff48245c..6d3f336b2b5204 100644
--- a/libc/include/sys/uio.yaml
+++ b/libc/include/sys/uio.yaml
@@ -15,3 +15,11 @@ functions:
       - type: int
       - type: const struct iovec *
       - type: int
+  - name: readv
+    standards:
+      - POSIX
+    return_type: ssize_t
+    arguments:
+      - type: int
+      - type: const struct iovec *
+      - type: int
diff --git a/libc/src/sys/uio/CMakeLists.txt b/libc/src/sys/uio/CMakeLists.txt
index 6298f86cd937da..c6a64297904103 100644
--- a/libc/src/sys/uio/CMakeLists.txt
+++ b/libc/src/sys/uio/CMakeLists.txt
@@ -8,3 +8,10 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.writev
 )
+
+add_entrypoint_object(
+  readv
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.readv
+)
diff --git a/libc/src/sys/uio/linux/CMakeLists.txt b/libc/src/sys/uio/linux/CMakeLists.txt
index 85a7a3ae4d5c21..7901c741f69a5a 100644
--- a/libc/src/sys/uio/linux/CMakeLists.txt
+++ b/libc/src/sys/uio/linux/CMakeLists.txt
@@ -12,3 +12,18 @@ add_entrypoint_object(
     libc.hdr.types.ssize_t
     libc.hdr.types.struct_iovec
 )
+
+add_entrypoint_object(
+  readv
+  SRCS
+    readv.cpp
+  HDRS
+    ../readv.h
+  DEPENDS
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.errno.errno
+    libc.hdr.types.ssize_t
+    libc.hdr.types.struct_iovec
+)
diff --git a/libc/src/sys/uio/linux/readv.cpp b/libc/src/sys/uio/linux/readv.cpp
new file mode 100644
index 00000000000000..3812f4dec150b8
--- /dev/null
+++ b/libc/src/sys/uio/linux/readv.cpp
@@ -0,0 +1,27 @@
+//===-- Implementation file for readv -------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include "src/sys/uio/readv.h"
+#include "src/__support/OSUtil/syscall.h"
+#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
+#include <sys/syscall.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(ssize_t, readv, (int fd, const iovec *iov, int iovcnt)) {
+  long ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_readv, fd, iov, iovcnt);
+  // On failure, return -1 and set errno.
+  if (ret < 0) {
+    libc_errno = static_cast<int>(-ret);
+    return -1;
+  }
+  // On success, return number of bytes read.
+  return static_cast<ssize_t>(ret);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/uio/readv.h b/libc/src/sys/uio/readv.h
new file mode 100644
index 00000000000000..135b1e6fd1b5ac
--- /dev/null
+++ b/libc/src/sys/uio/readv.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for readv -----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_UIO_READV_H
+#define LLVM_LIBC_SRC_SYS_UIO_READV_H
+
+#include "hdr/types/ssize_t.h"
+#include "hdr/types/struct_iovec.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+ssize_t readv(int fd, const iovec *iov, int iovcnt);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_UIO_READV_H
diff --git a/libc/test/src/sys/uio/CMakeLists.txt b/libc/test/src/sys/uio/CMakeLists.txt
index 45f8d14c161792..a58337ec5f01a5 100644
--- a/libc/test/src/sys/uio/CMakeLists.txt
+++ b/libc/test/src/sys/uio/CMakeLists.txt
@@ -1,8 +1,9 @@
 add_custom_target(libc_sys_uio_unittests)
+
 add_libc_unittest(
   writev_test
   SUITE
-  libc_sys_uio_unittests
+    libc_sys_uio_unittests
   SRCS
     writev_test.cpp
   DEPENDS
@@ -13,3 +14,18 @@ add_libc_unittest(
     libc.src.fcntl.open
     libc.test.UnitTest.ErrnoSetterMatcher
 )
+
+add_libc_unittest(
+  readv_test
+  SUITE
+    libc_sys_uio_unittests
+  SRCS
+    readv_test.cpp
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.__support.common
+    libc.src.sys.uio.readv
+    libc.src.unistd.close
+    libc.src.fcntl.open
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/sys/uio/readv_test.cpp b/libc/test/src/sys/uio/readv_test.cpp
new file mode 100644
index 00000000000000..cf551d364653bc
--- /dev/null
+++ b/libc/test/src/sys/uio/readv_test.cpp
@@ -0,0 +1,30 @@
+//===-- Unittests for readv -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/fcntl/open.h"
+#include "src/sys/uio/readv.h"
+#include "src/unistd/close.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+TEST(LlvmLibcSysUioReadvTest, SmokeTest) {
+  int fd = LIBC_NAMESPACE::open("/dev/urandom", O_RDONLY);
+  ASSERT_THAT(fd, returns(GT(0)).with_errno(EQ(0)));
+  char buf0[2];
+  char buf1[3];
+  struct iovec iov[2];
+  iov[0].iov_base = buf0;
+  iov[0].iov_len = 1;
+  iov[1].iov_base = buf1;
+  iov[1].iov_len = 2;
+  ASSERT_THAT(LIBC_NAMESPACE::readv(fd, iov, 2),
+              returns(EQ(3)).with_errno(EQ(0)));
+  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
+}



More information about the libc-commits mailing list