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

via libc-commits libc-commits at lists.llvm.org
Mon Sep 14 10:46:26 PDT 2026


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

>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 1/3] [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);
+}

>From dbc01e9eb386cbd037f06cd9c88cb9d9df3bff17 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Mon, 14 Sep 2026 17:31:46 +0000
Subject: [PATCH 2/3] Address comments.

---
 libc/hdr/types/off64_t.h                         |  7 ++++++-
 .../linux/syscall_wrappers/posix_fadvise.h       | 10 +++++-----
 libc/test/src/fcntl/CMakeLists.txt               |  1 +
 libc/test/src/fcntl/posix_fadvise64_test.cpp     | 16 +++++++++-------
 4 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/libc/hdr/types/off64_t.h b/libc/hdr/types/off64_t.h
index 5c9e2ab529e56..bf9d13c891c6d 100644
--- a/libc/hdr/types/off64_t.h
+++ b/libc/hdr/types/off64_t.h
@@ -1,10 +1,15 @@
-//===-- 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy for off64_t.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_HDR_TYPES_OFF64_T_H
 #define LLVM_LIBC_HDR_TYPES_OFF64_T_H
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 b38a443b7e2bb..7049ab3e6fdbc 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h
@@ -35,18 +35,18 @@ LIBC_INLINE ErrorOr<int> posix_fadvise(int fd, int64_t offset, int64_t len,
     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)
+#if defined(SYS_arm_fadvise64_64) || defined(SYS_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)
+#if 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_64)
+    return syscall_checked<int>(SYS_fadvise64_64, fd, offset_low, offset_high,
+                                len_low, len_high, advice);
 #elif defined(SYS_fadvise64)
     if (len < 0 ||
         static_cast<uint64_t>(len) > cpp::numeric_limits<size_t>::max())
diff --git a/libc/test/src/fcntl/CMakeLists.txt b/libc/test/src/fcntl/CMakeLists.txt
index 570ef7b4f9e4b..2232e25589baf 100644
--- a/libc/test/src/fcntl/CMakeLists.txt
+++ b/libc/test/src/fcntl/CMakeLists.txt
@@ -96,4 +96,5 @@ add_libc_test(
     libc.src.unistd.unlink
     libc.src.__support.CPP.scope
     libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
 )
diff --git a/libc/test/src/fcntl/posix_fadvise64_test.cpp b/libc/test/src/fcntl/posix_fadvise64_test.cpp
index bc80606303cc9..b804aa34f2ffa 100644
--- a/libc/test/src/fcntl/posix_fadvise64_test.cpp
+++ b/libc/test/src/fcntl/posix_fadvise64_test.cpp
@@ -22,9 +22,11 @@
 #include "src/unistd/pipe.h"
 #include "src/unistd/unlink.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
 using LlvmLibcPosixFadvise64Test = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
 
 TEST_F(LlvmLibcPosixFadvise64Test, InvalidFileDescriptor) {
   EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(-1, 0, 0, POSIX_FADV_NORMAL),
@@ -34,12 +36,12 @@ TEST_F(LlvmLibcPosixFadvise64Test, InvalidFileDescriptor) {
 }
 
 TEST_F(LlvmLibcPosixFadvise64Test, ValidFile) {
-  constexpr const char *TEST_FILE = "testdata/posix_fadvise64.test";
+  auto TEST_FILE = libc_make_test_file_path("posix_fadvise64.test");
   int fd = LIBC_NAMESPACE::creat(TEST_FILE, S_IRWXU);
-  ASSERT_GT(fd, 0);
+  ASSERT_GE(fd, 0);
   LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
-    EXPECT_EQ(LIBC_NAMESPACE::close(fd), 0);
-    EXPECT_EQ(LIBC_NAMESPACE::unlink(TEST_FILE), 0);
+    EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+    EXPECT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
   });
 
   EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise64(fd, 0, 0, POSIX_FADV_NORMAL), 0);
@@ -70,10 +72,10 @@ TEST_F(LlvmLibcPosixFadvise64Test, ValidFile) {
 
 TEST_F(LlvmLibcPosixFadvise64Test, Pipe) {
   int pipefd[2];
-  ASSERT_EQ(LIBC_NAMESPACE::pipe(pipefd), 0);
+  ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds(0));
   LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
-    EXPECT_EQ(LIBC_NAMESPACE::close(pipefd[0]), 0);
-    EXPECT_EQ(LIBC_NAMESPACE::close(pipefd[1]), 0);
+    EXPECT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds(0));
+    EXPECT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds(0));
   });
 
   // fadvise on a pipe should return ESPIPE

>From 7c28db6b6d87d5c2b31c5452e0e401d894bbb81e Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Mon, 14 Sep 2026 17:46:04 +0000
Subject: [PATCH 3/3] Also update posix_fadvise_test.

---
 libc/test/src/fcntl/CMakeLists.txt         |  1 +
 libc/test/src/fcntl/posix_fadvise_test.cpp | 16 +++++++++-------
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/libc/test/src/fcntl/CMakeLists.txt b/libc/test/src/fcntl/CMakeLists.txt
index 2232e25589baf..6a2acd9578697 100644
--- a/libc/test/src/fcntl/CMakeLists.txt
+++ b/libc/test/src/fcntl/CMakeLists.txt
@@ -75,6 +75,7 @@ add_libc_test(
     libc.src.unistd.unlink
     libc.src.__support.CPP.scope
     libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
 )
 
 add_libc_test(
diff --git a/libc/test/src/fcntl/posix_fadvise_test.cpp b/libc/test/src/fcntl/posix_fadvise_test.cpp
index 8da7f185de106..2c9ad1767dbdc 100644
--- a/libc/test/src/fcntl/posix_fadvise_test.cpp
+++ b/libc/test/src/fcntl/posix_fadvise_test.cpp
@@ -21,9 +21,11 @@
 #include "src/unistd/pipe.h"
 #include "src/unistd/unlink.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
 using LlvmLibcPosixFadviseTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
 
 TEST_F(LlvmLibcPosixFadviseTest, InvalidFileDescriptor) {
   EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(-1, 0, 0, POSIX_FADV_NORMAL), EBADF);
@@ -32,12 +34,12 @@ TEST_F(LlvmLibcPosixFadviseTest, InvalidFileDescriptor) {
 }
 
 TEST_F(LlvmLibcPosixFadviseTest, ValidFile) {
-  constexpr const char *TEST_FILE = "testdata/posix_fadvise.test";
+  auto TEST_FILE = libc_make_test_file_path("posix_fadvise.test");
   int fd = LIBC_NAMESPACE::creat(TEST_FILE, S_IRWXU);
-  ASSERT_GT(fd, 0);
+  ASSERT_GE(fd, 0);
   LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
-    EXPECT_EQ(LIBC_NAMESPACE::close(fd), 0);
-    EXPECT_EQ(LIBC_NAMESPACE::unlink(TEST_FILE), 0);
+    EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+    EXPECT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
   });
 
   EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_NORMAL), 0);
@@ -60,10 +62,10 @@ TEST_F(LlvmLibcPosixFadviseTest, ValidFile) {
 
 TEST_F(LlvmLibcPosixFadviseTest, Pipe) {
   int pipefd[2];
-  ASSERT_EQ(LIBC_NAMESPACE::pipe(pipefd), 0);
+  ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds(0));
   LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
-    EXPECT_EQ(LIBC_NAMESPACE::close(pipefd[0]), 0);
-    EXPECT_EQ(LIBC_NAMESPACE::close(pipefd[1]), 0);
+    EXPECT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds(0));
+    EXPECT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds(0));
   });
 
   // fadvise on a pipe should return ESPIPE



More information about the libc-commits mailing list