[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for readv (PR #237181)

via libc-commits libc-commits at lists.llvm.org
Fri Oct 30 04:36:36 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Connector Switch (c8ef)

<details>
<summary>Changes</summary>

This PR move readv behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.

---

<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>

---
Full diff: https://github.com/llvm/llvm-project/pull/237181.diff


5 Files Affected:

- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+14) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/readv.h (+36) 
- (modified) libc/src/sys/uio/linux/CMakeLists.txt (+9-8) 
- (modified) libc/src/sys/uio/linux/readv.cpp (+6-8) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+38) 


``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 8f3515b972cc7a..ee334a1f314dc2 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1479,6 +1479,20 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  readv
+  HDRS
+    readv.h
+  DEPENDS
+    libc.hdr.types.ssize_t
+    libc.hdr.types.struct_iovec
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   rename
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/readv.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/readv.h
new file mode 100644
index 00000000000000..0c09a015303f0e
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/readv.h
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for readv.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_READV_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_READV_H
+
+#include "hdr/types/ssize_t.h"
+#include "hdr/types/struct_iovec.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<ssize_t> readv(int fd, const struct iovec *iov,
+                                   int iovcnt) {
+  return syscall_checked<ssize_t>(SYS_readv, fd, iov, iovcnt);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_READV_H
diff --git a/libc/src/sys/uio/linux/CMakeLists.txt b/libc/src/sys/uio/linux/CMakeLists.txt
index db4fe68e4ca9c5..95177ba0424003 100644
--- a/libc/src/sys/uio/linux/CMakeLists.txt
+++ b/libc/src/sys/uio/linux/CMakeLists.txt
@@ -1,24 +1,25 @@
 add_entrypoint_object(
-  writev
+  readv
   SRCS
-    writev.cpp
+    readv.cpp
   HDRS
-    ../writev.h
+    ../readv.h
   DEPENDS
     libc.hdr.types.ssize_t
     libc.hdr.types.struct_iovec
-    libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
+    libc.src.__support.OSUtil.linux.syscall_wrappers.readv
     libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
     libc.src.errno.errno
 )
 
 add_entrypoint_object(
-  readv
+  writev
   SRCS
-    readv.cpp
+    writev.cpp
   HDRS
-    ../readv.h
+    ../writev.h
   DEPENDS
     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
index c9d8d87ddc72b8..a9cb74c07014d4 100644
--- a/libc/src/sys/uio/linux/readv.cpp
+++ b/libc/src/sys/uio/linux/readv.cpp
@@ -8,22 +8,20 @@
 #include "src/sys/uio/readv.h"
 #include "hdr/types/ssize_t.h"
 #include "hdr/types/struct_iovec.h"
-#include "src/__support/OSUtil/syscall.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/readv.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
-#include <sys/syscall.h>
+#include "src/__support/macros/config.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);
+  auto result = linux_syscalls::readv(fd, iov, iovcnt);
+  if (!result) {
+    libc_errno = result.error();
     return -1;
   }
-  // On success, return number of bytes read.
-  return static_cast<ssize_t>(ret);
+  return result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 2f8018ae18d5f9..331d297113b5c8 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2877,6 +2877,23 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_osutil_linux_syscall_wrappers_readv",
+    hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/readv.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_error_or",
+        ":__support_macros_config",
+        ":__support_osutil_syscall",
+        ":types_ssize_t",
+        ":types_struct_iovec",
+    ],
+)
+
 libc_support_library(
     name = "__support_osutil_linux_syscall_wrappers_recvfrom",
     hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/recvfrom.h"],
@@ -20015,6 +20032,27 @@ libc_function(
     ],
 )
 
+############################### sys/uio targets ################################
+
+libc_function(
+    name = "readv",
+    srcs = ["src/sys/uio/linux/readv.cpp"],
+    hdrs = ["src/sys/uio/readv.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_libc_errno",
+        ":__support_macros_config",
+        ":__support_osutil_linux_syscall_wrappers_readv",
+        ":errno",
+        ":types_ssize_t",
+        ":types_struct_iovec",
+    ],
+)
+
 ############################## sys/epoll targets ###############################
 
 libc_function(

``````````

</details>


https://github.com/llvm/llvm-project/pull/237181


More information about the libc-commits mailing list