[libc-commits] [libc] 49f13a5 - [libc][fcntl] Implement posix_fadvise function. (#220790)
via libc-commits
libc-commits at lists.llvm.org
Fri Sep 4 08:19:05 PDT 2026
Author: lntue
Date: 2026-09-04T11:18:59-04:00
New Revision: 49f13a5a8014591c4431c4c85e217c62c2705c65
URL: https://github.com/llvm/llvm-project/commit/49f13a5a8014591c4431c4c85e217c62c2705c65
DIFF: https://github.com/llvm/llvm-project/commit/49f13a5a8014591c4431c4c85e217c62c2705c65.diff
LOG: [libc][fcntl] Implement posix_fadvise function. (#220790)
Fixes https://github.com/llvm/llvm-project/issues/220765.
Assisted-by: Gemini
Added:
libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h
libc/src/fcntl/linux/posix_fadvise.cpp
libc/src/fcntl/posix_fadvise.h
libc/test/src/fcntl/posix_fadvise_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/include/fcntl.yaml
libc/include/llvm-libc-macros/linux/fcntl-macros.h
libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
libc/src/fcntl/CMakeLists.txt
libc/src/fcntl/linux/CMakeLists.txt
libc/test/src/fcntl/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index cc0cb0eb56d12..4c707cb00912b 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -41,6 +41,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.fcntl.fcntl
libc.src.fcntl.open
libc.src.fcntl.openat
+ libc.src.fcntl.posix_fadvise
# poll.h entrypoints
libc.src.poll.poll
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index f574a225c092f..49bd22356e988 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -52,6 +52,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.fcntl.fcntl
libc.src.fcntl.open
libc.src.fcntl.openat
+ libc.src.fcntl.posix_fadvise
# 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 4de6a291fef66..c645deea487c6 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -52,6 +52,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.fcntl.fcntl
libc.src.fcntl.open
libc.src.fcntl.openat
+ libc.src.fcntl.posix_fadvise
# net/if.h entrypoints
libc.src.net.if_indextoname
diff --git a/libc/include/fcntl.yaml b/libc/include/fcntl.yaml
index 43226b455051a..e2f174447b6ba 100644
--- a/libc/include/fcntl.yaml
+++ b/libc/include/fcntl.yaml
@@ -10,6 +10,18 @@ macros:
macro_header: fcntl-macros.h
- macro_name: AT_EACCESS
macro_header: fcntl-macros.h
+ - macro_name: POSIX_FADV_NORMAL
+ macro_header: fcntl-macros.h
+ - macro_name: POSIX_FADV_RANDOM
+ macro_header: fcntl-macros.h
+ - macro_name: POSIX_FADV_SEQUENTIAL
+ macro_header: fcntl-macros.h
+ - macro_name: POSIX_FADV_WILLNEED
+ macro_header: fcntl-macros.h
+ - macro_name: POSIX_FADV_DONTNEED
+ macro_header: fcntl-macros.h
+ - macro_name: POSIX_FADV_NOREUSE
+ macro_header: fcntl-macros.h
types:
- type_name: mode_t
- type_name: off_t
@@ -52,3 +64,12 @@ functions:
- type: const char *
- type: int
- type: '...'
+ - name: posix_fadvise
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: int
+ - type: off_t
+ - type: off_t
+ - type: int
diff --git a/libc/include/llvm-libc-macros/linux/fcntl-macros.h b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
index 8dcc177434cec..648c4a9ec00da 100644
--- a/libc/include/llvm-libc-macros/linux/fcntl-macros.h
+++ b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
@@ -105,4 +105,12 @@
#define F_SETLKW F_SETLKW64
#endif
+// Advice values for posix_fadvise.
+#define POSIX_FADV_NORMAL 0
+#define POSIX_FADV_RANDOM 1
+#define POSIX_FADV_SEQUENTIAL 2
+#define POSIX_FADV_WILLNEED 3
+#define POSIX_FADV_DONTNEED 4
+#define POSIX_FADV_NOREUSE 5
+
#endif // LLVM_LIBC_MACROS_LINUX_FCNTL_MACROS_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 8b2f9b075a706..9512528e9e5e2 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -457,6 +457,23 @@ add_header_library(
libc.include.sys_syscall
)
+add_header_library(
+ posix_fadvise
+ HDRS
+ posix_fadvise.h
+ 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
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
add_header_library(
ptrace
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h
new file mode 100644
index 0000000000000..5469f5ae788bb
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h
@@ -0,0 +1,75 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// ErrorOr-returning syscall wrapper for posix_fadvise.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_POSIX_FADVISE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_POSIX_FADVISE_H
+
+#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
+#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<int> posix_fadvise(int fd, off_t offset, off_t len,
+ int advice) {
+ if constexpr (sizeof(long) == sizeof(uint32_t) &&
+ sizeof(off_t) == sizeof(uint64_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);
+
+#if defined(SYS_fadvise64_64) || defined(SYS_arm_fadvise64_64)
+ uint64_t len_bits = cpp::bit_cast<uint64_t>(len);
+ long len_low = static_cast<long>(len_bits & UINT32_MAX);
+ long len_high = static_cast<long>(len_bits >> 32);
+#endif
+
+#if defined(SYS_fadvise64_64)
+ return syscall_checked<int>(SYS_fadvise64_64, fd, offset_low, offset_high,
+ len_low, len_high, advice);
+#elif defined(SYS_arm_fadvise64_64)
+ return syscall_checked<int>(SYS_arm_fadvise64_64, fd, advice, offset_low,
+ offset_high, len_low, len_high);
+#elif defined(SYS_fadvise64)
+ if (len < 0 ||
+ static_cast<uint64_t>(len) > cpp::numeric_limits<size_t>::max())
+ return Error(EINVAL);
+ return syscall_checked<int>(SYS_fadvise64, fd, offset_low, offset_high,
+ static_cast<size_t>(len), advice);
+#else
+#error "fadvise64 syscall not available."
+#endif
+ } else {
+#if defined(SYS_fadvise64)
+ return syscall_checked<int>(SYS_fadvise64, fd, offset, len, advice);
+#elif defined(SYS_fadvise64_64)
+ return syscall_checked<int>(SYS_fadvise64_64, fd, offset, len, advice);
+#else
+#error "fadvise64 syscall not available."
+#endif
+ }
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_POSIX_FADVISE_H
diff --git a/libc/src/fcntl/CMakeLists.txt b/libc/src/fcntl/CMakeLists.txt
index 77400e9050d08..1d6c31cf98978 100644
--- a/libc/src/fcntl/CMakeLists.txt
+++ b/libc/src/fcntl/CMakeLists.txt
@@ -29,3 +29,10 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.openat
)
+
+add_entrypoint_object(
+ posix_fadvise
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.posix_fadvise
+)
diff --git a/libc/src/fcntl/linux/CMakeLists.txt b/libc/src/fcntl/linux/CMakeLists.txt
index a235a99272636..bba33928da17a 100644
--- a/libc/src/fcntl/linux/CMakeLists.txt
+++ b/libc/src/fcntl/linux/CMakeLists.txt
@@ -48,3 +48,15 @@ add_entrypoint_object(
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
+
+add_entrypoint_object(
+ posix_fadvise
+ SRCS
+ posix_fadvise.cpp
+ HDRS
+ ../posix_fadvise.h
+ DEPENDS
+ libc.hdr.types.off_t
+ libc.src.__support.OSUtil.linux.syscall_wrappers.posix_fadvise
+ libc.src.__support.common
+)
diff --git a/libc/src/fcntl/linux/posix_fadvise.cpp b/libc/src/fcntl/linux/posix_fadvise.cpp
new file mode 100644
index 0000000000000..ed0a539204d0a
--- /dev/null
+++ b/libc/src/fcntl/linux/posix_fadvise.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_fadvise.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/fcntl/posix_fadvise.h"
+
+#include "hdr/types/off_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_fadvise,
+ (int fd, off_t offset, off_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_fadvise.h b/libc/src/fcntl/posix_fadvise.h
new file mode 100644
index 0000000000000..1a93521846a92
--- /dev/null
+++ b/libc/src/fcntl/posix_fadvise.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_fadvise.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_FCNTL_POSIX_FADVISE_H
+#define LLVM_LIBC_SRC_FCNTL_POSIX_FADVISE_H
+
+#include "hdr/types/off_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int posix_fadvise(int fd, off_t offset, off_t len, int advice);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_FCNTL_POSIX_FADVISE_H
diff --git a/libc/test/src/fcntl/CMakeLists.txt b/libc/test/src/fcntl/CMakeLists.txt
index dd2b473d954c5..bcce22406692b 100644
--- a/libc/test/src/fcntl/CMakeLists.txt
+++ b/libc/test/src/fcntl/CMakeLists.txt
@@ -56,3 +56,23 @@ add_libc_test(
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
+
+add_libc_test(
+ posix_fadvise_test
+ SUITE
+ libc_fcntl_unittests
+ SRCS
+ posix_fadvise_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fcntl_macros
+ libc.hdr.sys_stat_macros
+ libc.src.errno.errno
+ libc.src.fcntl.creat
+ libc.src.fcntl.posix_fadvise
+ 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_fadvise_test.cpp b/libc/test/src/fcntl/posix_fadvise_test.cpp
new file mode 100644
index 0000000000000..8da7f185de106
--- /dev/null
+++ b/libc/test/src/fcntl/posix_fadvise_test.cpp
@@ -0,0 +1,74 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_fadvise.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/fcntl/creat.h"
+#include "src/fcntl/posix_fadvise.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 LlvmLibcPosixFadviseTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcPosixFadviseTest, InvalidFileDescriptor) {
+ EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(-1, 0, 0, POSIX_FADV_NORMAL), EBADF);
+ // posix_fadvise must return the error directly and not set errno.
+ ASSERT_ERRNO_SUCCESS();
+}
+
+TEST_F(LlvmLibcPosixFadviseTest, ValidFile) {
+ constexpr const char *TEST_FILE = "testdata/posix_fadvise.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_fadvise(fd, 0, 0, POSIX_FADV_NORMAL), 0);
+ EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM), 0);
+ EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL), 0);
+ EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED), 0);
+ EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED), 0);
+ EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE), 0);
+
+ // Non-zero offset and length
+ EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 10, 20, POSIX_FADV_NORMAL), 0);
+
+ // Invalid advice
+ EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, -1), EINVAL);
+
+ // Negative len
+ EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, -1, POSIX_FADV_NORMAL),
+ EINVAL);
+}
+
+TEST_F(LlvmLibcPosixFadviseTest, 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_fadvise(pipefd[0], 0, 0, POSIX_FADV_NORMAL),
+ ESPIPE);
+ EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(pipefd[1], 0, 0, POSIX_FADV_NORMAL),
+ ESPIPE);
+}
More information about the libc-commits
mailing list