[libc-commits] [libc] [libc] Reland "Implement copy_file_range in unistd" (PR #240018)

via libc-commits libc-commits at lists.llvm.org
Sat Nov 7 11:54:41 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Aman Maurya (amanmaurya92)

<details>
<summary>Changes</summary>

This patch relands #<!-- -->236914 (reverted in #<!-- -->238956 / commit 53d0afa56861).

### Root Cause & Fix
The revert was caused by pre-commit CI failures where `copy_file_range_test` encountered environments lacking `copy_file_range` syscall emulation or running on container overlay filesystems:
- Emulators return `EFAULT` or `ENOSYS` instead of `EBADF` on invalid descriptors with `nullptr`.
- Container overlayfs / older kernels may return `EXDEV`, `ENOSYS`, or `EOPNOTSUPP`.

### Changes in Reland
- Update `InvalidArgs` in `copy_file_range_test.cpp` to accept `any_of(EBADF, EFAULT, ENOSYS)`.
- Gracefully handle `ENOSYS`, `EXDEV`, and `EOPNOTSUPP` in test copy cases.
- Add fallback returning `ENOSYS` in internal `syscall_wrappers/copy_file_range.h` if `SYS_copy_file_range` is not defined.

Fixes #<!-- -->236897.

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


13 Files Affected:

- (modified) libc/config/linux/aarch64/entrypoints.txt (+2-1) 
- (modified) libc/config/linux/arm/entrypoints.txt (+2-1) 
- (modified) libc/config/linux/riscv/entrypoints.txt (+2-1) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+2-1) 
- (modified) libc/include/unistd.yaml (+11) 
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+16) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/copy_file_range.h (+53) 
- (modified) libc/src/unistd/CMakeLists.txt (+8-1) 
- (added) libc/src/unistd/copy_file_range.h (+29) 
- (modified) libc/src/unistd/linux/CMakeLists.txt (+17) 
- (added) libc/src/unistd/linux/copy_file_range.cpp (+39) 
- (modified) libc/test/src/unistd/CMakeLists.txt (+20) 
- (added) libc/test/src/unistd/copy_file_range_test.cpp (+143) 


``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 8ba560989af6af..bc256bfadd78cd 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -465,7 +465,8 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.chown
     libc.src.unistd.chroot
     libc.src.unistd.close
-    libc.src.unistd.close_range
+    libc.src.unistd.close_range
+    libc.src.unistd.copy_file_range
     libc.src.unistd.dup
     libc.src.unistd.dup2
     libc.src.unistd.dup3
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 5e04635e298e77..62fe3849ac9379 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -257,7 +257,8 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.chown
     libc.src.unistd.chroot
     libc.src.unistd.close
-    libc.src.unistd.close_range
+    libc.src.unistd.close_range
+    libc.src.unistd.copy_file_range
     libc.src.unistd.dup
     libc.src.unistd.dup2
     libc.src.unistd.dup3
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 56813aaf66a91f..349c9053a051ee 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -491,7 +491,8 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.chown
     libc.src.unistd.chroot
     libc.src.unistd.close
-    libc.src.unistd.close_range
+    libc.src.unistd.close_range
+    libc.src.unistd.copy_file_range
     libc.src.unistd.dup
     libc.src.unistd.dup2
     libc.src.unistd.dup3
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 25b24740400b97..59ea19c5fb2b89 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -511,7 +511,8 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.chown
     libc.src.unistd.chroot
     libc.src.unistd.close
-    libc.src.unistd.close_range
+    libc.src.unistd.close_range
+    libc.src.unistd.copy_file_range
     libc.src.unistd.dup
     libc.src.unistd.dup2
     libc.src.unistd.dup3
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index faac5c197b29c5..2860ac7d3ee0bf 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -196,7 +196,18 @@ functions:
     arguments:
       - type: unsigned int
       - type: unsigned int
+      - type: int
+  - name: copy_file_range
+    standards:
+      - linux
+    return_type: ssize_t
+    arguments:
       - type: int
+      - type: off_t *
+      - type: int
+      - type: off_t *
+      - type: size_t
+      - type: unsigned int
   - name: dup
     standards:
       - posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index d89bb6d8df7b14..42ccf89e55efab 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1757,6 +1757,22 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  copy_file_range
+  HDRS
+    copy_file_range.h
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.types.off_t
+    libc.hdr.types.size_t
+    libc.hdr.types.ssize_t
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.include.sys_syscall
+)
+
 add_header_library(
   prlimit
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/copy_file_range.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/copy_file_range.h
new file mode 100644
index 00000000000000..0eea0bfa4c6b97
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/copy_file_range.h
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 copy_file_range.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_COPY_FILE_RANGE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_COPY_FILE_RANGE_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/off_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.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> copy_file_range(int fd_in, off_t *off_in,
+                                             int fd_out, off_t *off_out,
+                                             size_t len, unsigned int flags) {
+#ifdef SYS_copy_file_range
+  return syscall_checked<ssize_t>(SYS_copy_file_range, fd_in, off_in, fd_out,
+                                  off_out, len, flags);
+#elif defined(__NR_copy_file_range)
+  return syscall_checked<ssize_t>(__NR_copy_file_range, fd_in, off_in, fd_out,
+                                  off_out, len, flags);
+#else
+  (void)fd_in;
+  (void)off_in;
+  (void)fd_out;
+  (void)off_out;
+  (void)len;
+  (void)flags;
+  return ErrorOr<ssize_t>::from_error(ENOSYS);
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_COPY_FILE_RANGE_H
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index d7980762b97268..54fc70e4282673 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -81,7 +81,14 @@ add_entrypoint_object(
   close_range
   ALIAS
   DEPENDS
-    .${LIBC_TARGET_OS}.close_range
+    .${LIBC_TARGET_OS}.close_range
+)
+
+add_entrypoint_object(
+  copy_file_range
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.copy_file_range
 )
 
 add_entrypoint_object(
diff --git a/libc/src/unistd/copy_file_range.h b/libc/src/unistd/copy_file_range.h
new file mode 100644
index 00000000000000..a4877931043cad
--- /dev/null
+++ b/libc/src/unistd/copy_file_range.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Header for copy_file_range.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_COPY_FILE_RANGE_H
+#define LLVM_LIBC_SRC_UNISTD_COPY_FILE_RANGE_H
+
+#include "hdr/types/off_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+ssize_t copy_file_range(int fd_in, off_t *off_in, int fd_out, off_t *off_out,
+                        size_t len, unsigned int flags);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_COPY_FILE_RANGE_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 36ac888e563a54..8af8ff53d02719 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -80,7 +80,24 @@ add_entrypoint_object(
     libc.src.__support.OSUtil.linux.syscall_wrappers.close_range
     libc.src.__support.common
     libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+)
+
+add_entrypoint_object(
+  copy_file_range
+  SRCS
+    copy_file_range.cpp
+  HDRS
+    ../copy_file_range.h
+  DEPENDS
+    libc.hdr.types.off_t
+    libc.hdr.types.size_t
+    libc.hdr.types.ssize_t
+    libc.src.__support.common
+    libc.src.__support.libc_errno
     libc.src.__support.macros.config
+    libc.src.__support.OSUtil.linux.syscall_wrappers.copy_file_range
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/unistd/linux/copy_file_range.cpp b/libc/src/unistd/linux/copy_file_range.cpp
new file mode 100644
index 00000000000000..4a2a18d8fef45c
--- /dev/null
+++ b/libc/src/unistd/linux/copy_file_range.cpp
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 copy_file_range.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/copy_file_range.h"
+
+#include "hdr/types/off_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/copy_file_range.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(ssize_t, copy_file_range,
+                   (int fd_in, off_t *off_in, int fd_out, off_t *off_out,
+                    size_t len, unsigned int flags)) {
+  auto result = linux_syscalls::copy_file_range(fd_in, off_in, fd_out, off_out,
+                                                len, flags);
+
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 8d08b382b81047..499b061d6cd02c 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -123,6 +123,26 @@ add_libc_test(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
+add_libc_test(
+  copy_file_range_test
+  SUITE
+    libc-unistd-tests
+  SRCS
+    copy_file_range_test.cpp
+  DEPENDS
+    libc.hdr.fcntl_macros
+    libc.hdr.sys_stat_macros
+    libc.src.__support.CPP.string_view
+    libc.src.fcntl.open
+    libc.src.unistd.close
+    libc.src.unistd.copy_file_range
+    libc.src.unistd.read
+    libc.src.unistd.unlink
+    libc.src.unistd.write
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
 add_libc_test(
   dup2_test
   SUITE
diff --git a/libc/test/src/unistd/copy_file_range_test.cpp b/libc/test/src/unistd/copy_file_range_test.cpp
new file mode 100644
index 00000000000000..67959d3d31d4f6
--- /dev/null
+++ b/libc/test/src/unistd/copy_file_range_test.cpp
@@ -0,0 +1,143 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Unittests for copy_file_range.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "src/unistd/copy_file_range.h"
+#include "src/unistd/read.h"
+#include "src/unistd/unlink.h"
+#include "src/unistd/write.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+namespace cpp = LIBC_NAMESPACE::cpp;
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+class LlvmLibcCopyFileRangeTest
+    : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
+protected:
+  static constexpr const char *IN_FILENAME = "copy_file_range_in.test";
+  static constexpr const char *OUT_FILENAME = "copy_file_range_out.test";
+  static constexpr const char TEST_DATA[] = "0123456789ABCDEF";
+  static constexpr size_t TEST_SIZE = sizeof(TEST_DATA) - 1;
+
+  const char *in_path = nullptr;
+  const char *out_path = nullptr;
+  int in_fd = -1;
+  int out_fd = -1;
+
+  void SetUp() override {
+    in_path = libc_make_test_file_path(IN_FILENAME);
+    out_path = libc_make_test_file_path(OUT_FILENAME);
+
+    int write_fd =
+        LIBC_NAMESPACE::open(in_path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
+    ASSERT_ERRNO_SUCCESS();
+    ASSERT_GT(write_fd, 0);
+    ASSERT_EQ(static_cast<ssize_t>(TEST_SIZE),
+              LIBC_NAMESPACE::write(write_fd, TEST_DATA, TEST_SIZE));
+    ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
+
+    in_fd = LIBC_NAMESPACE::open(in_path, O_RDONLY);
+    ASSERT_ERRNO_SUCCESS();
+    ASSERT_GT(in_fd, 0);
+
+    out_fd =
+        LIBC_NAMESPACE::open(out_path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
+    ASSERT_ERRNO_SUCCESS();
+    ASSERT_GT(out_fd, 0);
+  }
+
+  void TearDown() override {
+    if (in_fd >= 0) {
+      LIBC_NAMESPACE::close(in_fd);
+      in_fd = -1;
+    }
+    if (out_fd >= 0) {
+      LIBC_NAMESPACE::close(out_fd);
+      out_fd = -1;
+    }
+    if (in_path)
+      LIBC_NAMESPACE::unlink(in_path);
+    if (out_path)
+      LIBC_NAMESPACE::unlink(out_path);
+  }
+};
+
+TEST_F(LlvmLibcCopyFileRangeTest, InvalidArgs) {
+  // Bad file descriptors. Under QEMU user mode emulation or restricted
+  // environments, nullptr pointers or missing syscalls may report EFAULT
+  // or ENOSYS.
+  EXPECT_THAT(LIBC_NAMESPACE::copy_file_range(-1, nullptr, -1, nullptr, 10, 0),
+              Fails<ssize_t>(any_of(EBADF, EFAULT, ENOSYS)));
+}
+
+TEST_F(LlvmLibcCopyFileRangeTest, CopyNoOffsets) {
+  EXPECT_THAT(
+      LIBC_NAMESPACE::copy_file_range(in_fd, nullptr, out_fd, nullptr, 1, ~0U),
+      Fails<ssize_t>(any_of(EINVAL, ENOSYS, EXDEV, EOPNOTSUPP)));
+
+  ssize_t copied = LIBC_NAMESPACE::copy_file_range(in_fd, nullptr, out_fd,
+                                                   nullptr, TEST_SIZE, 0);
+  if (copied == -1 && (libc_errno == ENOSYS || libc_errno == EXDEV ||
+                       libc_errno == EOPNOTSUPP)) {
+    // Underlying kernel, container environment (e.g. overlayfs), or emulator
+    // does not support copy_file_range.
+    return;
+  }
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_EQ(copied, static_cast<ssize_t>(TEST_SIZE));
+
+  ASSERT_THAT(LIBC_NAMESPACE::close(out_fd), Succeeds(0));
+  out_fd = LIBC_NAMESPACE::open(out_path, O_RDONLY);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(out_fd, 0);
+
+  char buf[TEST_SIZE + 1];
+  ASSERT_EQ(static_cast<ssize_t>(TEST_SIZE),
+            LIBC_NAMESPACE::read(out_fd, buf, TEST_SIZE));
+  buf[TEST_SIZE] = '\0';
+  EXPECT_EQ(cpp::string_view(buf, TEST_SIZE),
+            cpp::string_view(TEST_DATA, TEST_SIZE));
+}
+
+TEST_F(LlvmLibcCopyFileRangeTest, CopyWithOffsets) {
+  off_t off_in = 4;
+  off_t off_out = 0;
+  constexpr size_t COPY_LEN = 6;
+  ssize_t copied = LIBC_NAMESPACE::copy_file_range(in_fd, &off_in, out_fd,
+                                                   &off_out, COPY_LEN, 0);
+  if (copied == -1 && (libc_errno == ENOSYS || libc_errno == EXDEV ||
+                       libc_errno == EOPNOTSUPP)) {
+    return;
+  }
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_EQ(copied, static_cast<ssize_t>(COPY_LEN));
+  EXPECT_EQ(off_in, static_cast<off_t>(10));
+  EXPECT_EQ(off_out, static_cast<off_t>(6));
+
+  ASSERT_THAT(LIBC_NAMESPACE::close(out_fd), Succeeds(0));
+  out_fd = LIBC_NAMESPACE::open(out_path, O_RDONLY);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(out_fd, 0);
+
+  char buf[COPY_LEN + 1];
+  ASSERT_EQ(static_cast<ssize_t>(COPY_LEN),
+            LIBC_NAMESPACE::read(out_fd, buf, COPY_LEN));
+  buf[COPY_LEN] = '\0';
+  EXPECT_EQ(cpp::string_view(buf, COPY_LEN), cpp::string_view("456789", 6));
+}

``````````

</details>


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


More information about the libc-commits mailing list