[libc-commits] [libc] [libc] Implement posix_fadvise64. (PR #223294)

via libc-commits libc-commits at lists.llvm.org
Sun Sep 13 20:26:33 PDT 2026


https://github.com/lntue created https://github.com/llvm/llvm-project/pull/223294

None

>From a21fdd4120f18dcc4deefd1fa3b5e8455c60c456 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Mon, 14 Sep 2026 03:24:58 +0000
Subject: [PATCH] [libc] Implement posix_fadvise64.

---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/hdr/types/CMakeLists.txt                 | 11 +++
 libc/hdr/types/off64_t.h                      | 22 +++++
 libc/include/CMakeLists.txt                   |  1 +
 libc/include/fcntl.yaml                       | 10 +++
 .../linux/syscall_wrappers/CMakeLists.txt     |  1 -
 .../linux/syscall_wrappers/posix_fadvise.h    |  6 +-
 libc/src/fcntl/CMakeLists.txt                 |  7 ++
 libc/src/fcntl/linux/CMakeLists.txt           | 12 +++
 libc/src/fcntl/linux/posix_fadvise64.cpp      | 31 +++++++
 libc/src/fcntl/posix_fadvise64.h              | 26 ++++++
 libc/test/src/fcntl/CMakeLists.txt            | 21 +++++
 libc/test/src/fcntl/posix_fadvise64_test.cpp  | 84 +++++++++++++++++++
 15 files changed, 230 insertions(+), 5 deletions(-)
 create mode 100644 libc/hdr/types/off64_t.h
 create mode 100644 libc/src/fcntl/linux/posix_fadvise64.cpp
 create mode 100644 libc/src/fcntl/posix_fadvise64.h
 create mode 100644 libc/test/src/fcntl/posix_fadvise64_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index abf928394e591..e17e69b3a53b7 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -42,6 +42,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.fcntl.open
     libc.src.fcntl.openat
     libc.src.fcntl.posix_fadvise
+    libc.src.fcntl.posix_fadvise64
 
     # poll.h entrypoints
     libc.src.poll.poll
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index c05f58413eac3..7fc8cb3ffa5b7 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -53,6 +53,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.fcntl.open
     libc.src.fcntl.openat
     libc.src.fcntl.posix_fadvise
+    libc.src.fcntl.posix_fadvise64
 
     # net/if.h entrypoints
     libc.src.net.if_indextoname
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5cc68f9ef507b..a8a3150877f45 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -53,6 +53,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.fcntl.open
     libc.src.fcntl.openat
     libc.src.fcntl.posix_fadvise
+    libc.src.fcntl.posix_fadvise64
 
     # net/if.h entrypoints
     libc.src.net.if_indextoname
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 999aaa2b72b06..18f9173d501e1 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -481,6 +481,17 @@ add_proxy_header_library(
     libc.include.stdio
 )
 
+add_proxy_header_library(
+  off64_t
+  HDRS
+    off64_t.h
+  DEPENDS
+    libc.hdr.fcntl_overlay
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.off64_t
+    libc.include.fcntl
+)
+
 add_proxy_header_library(
   cookie_io_functions_t
   HDRS
diff --git a/libc/hdr/types/off64_t.h b/libc/hdr/types/off64_t.h
new file mode 100644
index 0000000000000..5c9e2ab529e56
--- /dev/null
+++ b/libc/hdr/types/off64_t.h
@@ -0,0 +1,22 @@
+//===-- Proxy for off64_t -------------------------------------------------===//
+//
+// 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_HDR_TYPES_OFF64_T_H
+#define LLVM_LIBC_HDR_TYPES_OFF64_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/off64_t.h"
+
+#else // Overlay mode
+
+#include "hdr/fcntl_overlay.h"
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_OFF64_T_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index b6a059e67c355..4a1158401d6ed 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -69,6 +69,7 @@ add_header_macro(
     .llvm-libc-types.struct_flock64
     .llvm-libc-types.pid_t
     .llvm-libc-types.off_t
+    .llvm-libc-types.off64_t
     .llvm_libc_common_h
 )
 
diff --git a/libc/include/fcntl.yaml b/libc/include/fcntl.yaml
index e2f174447b6ba..2b73c8da1e989 100644
--- a/libc/include/fcntl.yaml
+++ b/libc/include/fcntl.yaml
@@ -25,6 +25,7 @@ macros:
 types:
   - type_name: mode_t
   - type_name: off_t
+  - type_name: off64_t
   - type_name: pid_t
   - type_name: struct_f_owner_ex
   - type_name: struct_flock
@@ -73,3 +74,12 @@ functions:
       - type: off_t
       - type: off_t
       - type: int
+  - name: posix_fadvise64
+    standards:
+      - gnu
+    return_type: int
+    arguments:
+      - type: int
+      - type: off64_t
+      - type: off64_t
+      - type: int
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 4cd2dccb2f6fe..3857c736086dc 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -466,7 +466,6 @@ add_header_library(
   DEPENDS
     libc.hdr.errno_macros
     libc.hdr.stdint_proxy
-    libc.hdr.types.off_t
     libc.include.sys_syscall
     libc.src.__support.CPP.bit
     libc.src.__support.CPP.limits
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h
index 5469f5ae788bb..b38a443b7e2bb 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h
@@ -16,7 +16,6 @@
 
 #include "hdr/errno_macros.h"
 #include "hdr/stdint_proxy.h"
-#include "hdr/types/off_t.h"
 #include "src/__support/CPP/bit.h"
 #include "src/__support/CPP/limits.h"
 #include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
@@ -29,10 +28,9 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
-LIBC_INLINE ErrorOr<int> posix_fadvise(int fd, off_t offset, off_t len,
+LIBC_INLINE ErrorOr<int> posix_fadvise(int fd, int64_t offset, int64_t len,
                                        int advice) {
-  if constexpr (sizeof(long) == sizeof(uint32_t) &&
-                sizeof(off_t) == sizeof(uint64_t)) {
+  if constexpr (sizeof(long) == sizeof(uint32_t)) {
     uint64_t offset_bits = cpp::bit_cast<uint64_t>(offset);
     long offset_low = static_cast<long>(offset_bits & UINT32_MAX);
     long offset_high = static_cast<long>(offset_bits >> 32);
diff --git a/libc/src/fcntl/CMakeLists.txt b/libc/src/fcntl/CMakeLists.txt
index 1d6c31cf98978..09afab57b7aa5 100644
--- a/libc/src/fcntl/CMakeLists.txt
+++ b/libc/src/fcntl/CMakeLists.txt
@@ -36,3 +36,10 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.posix_fadvise
 )
+
+add_entrypoint_object(
+  posix_fadvise64
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.posix_fadvise64
+)
diff --git a/libc/src/fcntl/linux/CMakeLists.txt b/libc/src/fcntl/linux/CMakeLists.txt
index bba33928da17a..0ce44e551dae0 100644
--- a/libc/src/fcntl/linux/CMakeLists.txt
+++ b/libc/src/fcntl/linux/CMakeLists.txt
@@ -60,3 +60,15 @@ add_entrypoint_object(
     libc.src.__support.OSUtil.linux.syscall_wrappers.posix_fadvise
     libc.src.__support.common
 )
+
+add_entrypoint_object(
+  posix_fadvise64
+  SRCS
+    posix_fadvise64.cpp
+  HDRS
+    ../posix_fadvise64.h
+  DEPENDS
+    libc.hdr.types.off64_t
+    libc.src.__support.OSUtil.linux.syscall_wrappers.posix_fadvise
+    libc.src.__support.common
+)
diff --git a/libc/src/fcntl/linux/posix_fadvise64.cpp b/libc/src/fcntl/linux/posix_fadvise64.cpp
new file mode 100644
index 0000000000000..ba7a61f341dfc
--- /dev/null
+++ b/libc/src/fcntl/linux/posix_fadvise64.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 posix_fadvise64.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/fcntl/posix_fadvise64.h"
+
+#include "hdr/types/off64_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, posix_fadvise64,
+                   (int fd, off64_t offset, off64_t len, int advice)) {
+  auto result = linux_syscalls::posix_fadvise(fd, offset, len, advice);
+  if (!result)
+    return result.error();
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/fcntl/posix_fadvise64.h b/libc/src/fcntl/posix_fadvise64.h
new file mode 100644
index 0000000000000..d17004203f898
--- /dev/null
+++ b/libc/src/fcntl/posix_fadvise64.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Implementation header for posix_fadvise64.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_FCNTL_POSIX_FADVISE64_H
+#define LLVM_LIBC_SRC_FCNTL_POSIX_FADVISE64_H
+
+#include "hdr/types/off64_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int posix_fadvise64(int fd, off64_t offset, off64_t len, int advice);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_FCNTL_POSIX_FADVISE64_H
diff --git a/libc/test/src/fcntl/CMakeLists.txt b/libc/test/src/fcntl/CMakeLists.txt
index bcce22406692b..570ef7b4f9e4b 100644
--- a/libc/test/src/fcntl/CMakeLists.txt
+++ b/libc/test/src/fcntl/CMakeLists.txt
@@ -76,3 +76,24 @@ add_libc_test(
     libc.src.__support.CPP.scope
     libc.test.UnitTest.ErrnoCheckingTest
 )
+
+add_libc_test(
+  posix_fadvise64_test
+  SUITE
+    libc_fcntl_unittests
+  SRCS
+    posix_fadvise64_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.fcntl_macros
+    libc.hdr.sys_stat_macros
+    libc.hdr.types.off64_t
+    libc.src.errno.errno
+    libc.src.fcntl.creat
+    libc.src.fcntl.posix_fadvise64
+    libc.src.unistd.close
+    libc.src.unistd.pipe
+    libc.src.unistd.unlink
+    libc.src.__support.CPP.scope
+    libc.test.UnitTest.ErrnoCheckingTest
+)
diff --git a/libc/test/src/fcntl/posix_fadvise64_test.cpp b/libc/test/src/fcntl/posix_fadvise64_test.cpp
new file mode 100644
index 0000000000000..bc80606303cc9
--- /dev/null
+++ b/libc/test/src/fcntl/posix_fadvise64_test.cpp
@@ -0,0 +1,84 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 posix_fadvise64.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "hdr/types/off64_t.h"
+#include "src/__support/CPP/scope.h"
+#include "src/fcntl/creat.h"
+#include "src/fcntl/posix_fadvise64.h"
+#include "src/unistd/close.h"
+#include "src/unistd/pipe.h"
+#include "src/unistd/unlink.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcPosixFadvise64Test = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcPosixFadvise64Test, InvalidFileDescriptor) {
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(-1, 0, 0, POSIX_FADV_NORMAL),
+            EBADF);
+  // posix_fadvise64 must return the error directly and not set errno.
+  ASSERT_ERRNO_SUCCESS();
+}
+
+TEST_F(LlvmLibcPosixFadvise64Test, ValidFile) {
+  constexpr const char *TEST_FILE = "testdata/posix_fadvise64.test";
+  int fd = LIBC_NAMESPACE::creat(TEST_FILE, S_IRWXU);
+  ASSERT_GT(fd, 0);
+  LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
+    EXPECT_EQ(LIBC_NAMESPACE::close(fd), 0);
+    EXPECT_EQ(LIBC_NAMESPACE::unlink(TEST_FILE), 0);
+  });
+
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(fd, 0, 0, POSIX_FADV_NORMAL), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(fd, 0, 0, POSIX_FADV_RANDOM), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(fd, 0, 0, POSIX_FADV_SEQUENTIAL),
+            0);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(fd, 0, 0, POSIX_FADV_WILLNEED), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(fd, 0, 0, POSIX_FADV_DONTNEED), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(fd, 0, 0, POSIX_FADV_NOREUSE), 0);
+
+  // Non-zero offset and length
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(fd, 10, 20, POSIX_FADV_NORMAL), 0);
+
+  // 64-bit offset and length (> 4 GiB)
+  constexpr off64_t LARGE_OFFSET = static_cast<off64_t>(1) << 33;
+  constexpr off64_t LARGE_LEN = static_cast<off64_t>(1) << 32;
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(fd, LARGE_OFFSET, LARGE_LEN,
+                                            POSIX_FADV_NORMAL),
+            0);
+
+  // Invalid advice
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(fd, 0, 0, -1), EINVAL);
+
+  // Negative len
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(fd, 0, -1, POSIX_FADV_NORMAL),
+            EINVAL);
+}
+
+TEST_F(LlvmLibcPosixFadvise64Test, Pipe) {
+  int pipefd[2];
+  ASSERT_EQ(LIBC_NAMESPACE::pipe(pipefd), 0);
+  LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
+    EXPECT_EQ(LIBC_NAMESPACE::close(pipefd[0]), 0);
+    EXPECT_EQ(LIBC_NAMESPACE::close(pipefd[1]), 0);
+  });
+
+  // fadvise on a pipe should return ESPIPE
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(pipefd[0], 0, 0, POSIX_FADV_NORMAL),
+            ESPIPE);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(pipefd[1], 0, 0, POSIX_FADV_NORMAL),
+            ESPIPE);
+}



More information about the libc-commits mailing list