[llvm-branch-commits] [libc] [llvm] [libc] Use a syscall wrapper for pread (PR #240329)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Nov 9 03:17:35 PST 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 pread behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.
---
Full diff: https://github.com/llvm/llvm-project/pull/240329.diff
5 Files Affected:
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+17)
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/pread.h (+51)
- (modified) libc/src/unistd/linux/CMakeLists.txt (+5-5)
- (modified) libc/src/unistd/linux/pread.cpp (+11-22)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+21-2)
``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 10cb0173ed9a0..e7450be872876 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1881,6 +1881,23 @@ add_header_library(
libc.include.sys_syscall
)
+add_header_library(
+ pread
+ HDRS
+ pread.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.hdr.types.off_t
+ libc.hdr.types.size_t
+ libc.hdr.types.ssize_t
+ libc.include.sys_syscall
+ libc.src.__support.CPP.bit
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
add_header_library(
rt_sigaction
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/pread.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/pread.h
new file mode 100644
index 0000000000000..a3b9b8330c133
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/pread.h
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 pread.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PREAD_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PREAD_H
+
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/off_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.h"
+#include "src/__support/CPP/bit.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> pread(int fd, void *buf, size_t count,
+ off_t offset) {
+ if constexpr (sizeof(long) == sizeof(uint32_t) &&
+ sizeof(off_t) == sizeof(uint64_t)) {
+ // This is a 32-bit system with a 64-bit offset, offset must be split.
+ const uint64_t bits = cpp::bit_cast<uint64_t>(offset);
+ const uint32_t lo = bits & UINT32_MAX;
+ const uint32_t hi = bits >> 32;
+ const long offset_low = cpp::bit_cast<long>(static_cast<long>(lo));
+ const long offset_high = cpp::bit_cast<long>(static_cast<long>(hi));
+ return syscall_checked<ssize_t>(SYS_pread64, fd, buf, count, offset_low,
+ offset_high);
+ } else {
+ return syscall_checked<ssize_t>(SYS_pread64, fd, buf, count, offset);
+ }
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PREAD_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index c8a7f3d32cf43..a1ba556250469 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -754,11 +754,11 @@ add_entrypoint_object(
libc.hdr.types.off_t
libc.hdr.types.size_t
libc.hdr.types.ssize_t
- libc.hdr.fcntl_macros
- libc.hdr.stdint_proxy
- libc.include.unistd
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
+ libc.hdr.unistd_macros
+ libc.src.__support.OSUtil.linux.syscall_wrappers.pread
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
libc.src.__support.macros.sanitizer
libc.src.errno.errno
)
diff --git a/libc/src/unistd/linux/pread.cpp b/libc/src/unistd/linux/pread.cpp
index 782b4c2eb8a0d..25497706e995c 100644
--- a/libc/src/unistd/linux/pread.cpp
+++ b/libc/src/unistd/linux/pread.cpp
@@ -1,48 +1,37 @@
-//===-- Linux implementation of pread -------------------------------------===//
+//===----------------------------------------------------------------------===//
//
// 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
+/// Linux implementation of pread.
+///
+//===----------------------------------------------------------------------===//
#include "src/unistd/pread.h"
-#include "hdr/stdint_proxy.h" // For uint64_t.
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/pread.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/sanitizer.h" // for LIBC_MSAN_UNPOISON
-#include <sys/syscall.h> // For syscall numbers.
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(ssize_t, pread,
(int fd, void *buf, size_t count, off_t offset)) {
- ssize_t ret;
- if constexpr (sizeof(long) == sizeof(uint32_t) &&
- sizeof(off_t) == sizeof(uint64_t)) {
- // This is a 32-bit system with a 64-bit offset, offset must be split.
- const uint64_t bits = cpp::bit_cast<uint64_t>(offset);
- const uint32_t lo = bits & UINT32_MAX;
- const uint32_t hi = bits >> 32;
- const long offset_low = cpp::bit_cast<long>(static_cast<long>(lo));
- const long offset_high = cpp::bit_cast<long>(static_cast<long>(hi));
- ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_pread64, fd, buf, count,
- offset_low, offset_high);
- } else {
- ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_pread64, fd, buf, count,
- offset);
- }
+ auto result = linux_syscalls::pread(fd, buf, count, offset);
// The cast is important since there is a check that dereferences the pointer
// which fails on void*.
LIBC_MSAN_UNPOISON(reinterpret_cast<char *>(buf), count);
- if (ret < 0) {
- libc_errno = static_cast<int>(-ret);
+ if (!result) {
+ libc_errno = result.error();
return -1;
}
- return 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 16139b31b3fbe..bf9804e080d69 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3438,6 +3438,26 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_osutil_linux_syscall_wrappers_pread",
+ hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/pread.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ ":__support_common",
+ ":__support_cpp_bit",
+ ":__support_error_or",
+ ":__support_macros_config",
+ ":__support_osutil_syscall",
+ ":hdr_stdint_proxy",
+ ":types_off_t",
+ ":types_size_t",
+ ":types_ssize_t",
+ ],
+)
+
libc_support_library(
name = "__support_osutil_linux_syscall_wrappers_prlimit",
hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/prlimit.h"],
@@ -18986,9 +19006,8 @@ libc_function(
":__support_libc_errno",
":__support_macros_config",
":__support_macros_sanitizer",
- ":__support_osutil_syscall",
+ ":__support_osutil_linux_syscall_wrappers_pread",
":errno",
- ":hdr_stdint_proxy",
":hdr_unistd_macros",
":types_off_t",
":types_size_t",
``````````
</details>
https://github.com/llvm/llvm-project/pull/240329
More information about the llvm-branch-commits
mailing list