[libc-commits] [libc] [llvm] [libc] Convert readv, writev, and read_write tests to use pipes (PR #210725)

via libc-commits libc-commits at lists.llvm.org
Mon Jul 20 07:42:49 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Pavel Labath (labath)

<details>
<summary>Changes</summary>

Using a pipe avoids races between "hermetic" and "unit" versions of the same test and avoids leaving filesystem artifacts around if the test fails. If I'm successful, we won't have the worry about the first problem anymore, but I think it's still a nice cleanup.

Assisted by Gemini.

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


6 Files Affected:

- (modified) libc/test/src/sys/uio/CMakeLists.txt (+5-6) 
- (modified) libc/test/src/sys/uio/readv_test.cpp (+8-13) 
- (modified) libc/test/src/sys/uio/writev_test.cpp (+14-9) 
- (modified) libc/test/src/unistd/CMakeLists.txt (+2-5) 
- (modified) libc/test/src/unistd/read_write_test.cpp (+7-19) 
- (modified) utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel (+1-4) 


``````````diff
diff --git a/libc/test/src/sys/uio/CMakeLists.txt b/libc/test/src/sys/uio/CMakeLists.txt
index 7ba02be5d1cc4..9d8beec1fe5be 100644
--- a/libc/test/src/sys/uio/CMakeLists.txt
+++ b/libc/test/src/sys/uio/CMakeLists.txt
@@ -1,6 +1,6 @@
 add_custom_target(libc_sys_uio_unittests)
 
-add_libc_unittest(
+add_libc_test(
   writev_test
   SUITE
     libc_sys_uio_unittests
@@ -10,14 +10,14 @@ add_libc_unittest(
     libc.hdr.types.struct_iovec
     libc.src.__support.common
     libc.src.errno.errno
-    libc.src.fcntl.open
     libc.src.sys.uio.writev
     libc.src.unistd.close
-    libc.src.unistd.unlink
+    libc.src.unistd.pipe
+    libc.src.unistd.read
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
-add_libc_unittest(
+add_libc_test(
   readv_test
   SUITE
     libc_sys_uio_unittests
@@ -27,10 +27,9 @@ add_libc_unittest(
     libc.hdr.types.struct_iovec
     libc.src.__support.common
     libc.src.errno.errno
-    libc.src.fcntl.open
     libc.src.sys.uio.readv
     libc.src.unistd.close
-    libc.src.unistd.unlink
+    libc.src.unistd.pipe
     libc.src.unistd.write
     libc.test.UnitTest.ErrnoSetterMatcher
 )
diff --git a/libc/test/src/sys/uio/readv_test.cpp b/libc/test/src/sys/uio/readv_test.cpp
index 7cbaa3397d887..b25afc9754688 100644
--- a/libc/test/src/sys/uio/readv_test.cpp
+++ b/libc/test/src/sys/uio/readv_test.cpp
@@ -7,10 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "hdr/types/struct_iovec.h"
-#include "src/fcntl/open.h"
 #include "src/sys/uio/readv.h"
 #include "src/unistd/close.h"
-#include "src/unistd/unlink.h"
+#include "src/unistd/pipe.h"
 #include "src/unistd/write.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
@@ -18,16 +17,14 @@
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
 
 TEST(LlvmLibcSysUioReadvTest, SmokeTest) {
-  const char *filename = "./LlvmLibcSysUioReadvTest";
-  int fd = LIBC_NAMESPACE::open(filename, O_WRONLY | O_CREAT, 0644);
-  ASSERT_THAT(fd, returns(GT(0)).with_errno(EQ(0)));
+  int pipefd[2];
+  ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds());
+
   const char data[] = "Hello, World!\n";
-  ASSERT_THAT(LIBC_NAMESPACE::write(fd, data, sizeof(data)),
+  ASSERT_THAT(LIBC_NAMESPACE::write(pipefd[1], data, sizeof(data)),
               returns(EQ(sizeof(data))).with_errno(EQ(0)));
-  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
+  ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds());
 
-  fd = LIBC_NAMESPACE::open(filename, O_RDONLY);
-  ASSERT_THAT(fd, returns(GT(0)).with_errno(EQ(0)));
   char buf0[2];
   char buf1[3];
   struct iovec iov[2];
@@ -35,9 +32,7 @@ TEST(LlvmLibcSysUioReadvTest, SmokeTest) {
   iov[0].iov_len = 1;
   iov[1].iov_base = buf1;
   iov[1].iov_len = 2;
-  ASSERT_THAT(LIBC_NAMESPACE::readv(fd, iov, 2),
+  ASSERT_THAT(LIBC_NAMESPACE::readv(pipefd[0], iov, 2),
               returns(EQ(ssize_t(3))).with_errno(EQ(0)));
-  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
-  ASSERT_THAT(LIBC_NAMESPACE::unlink(filename),
-              returns(EQ(ssize_t(0))).with_errno(EQ(0)));
+  ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds());
 }
diff --git a/libc/test/src/sys/uio/writev_test.cpp b/libc/test/src/sys/uio/writev_test.cpp
index 69db0de47141c..bf6b04fee0bda 100644
--- a/libc/test/src/sys/uio/writev_test.cpp
+++ b/libc/test/src/sys/uio/writev_test.cpp
@@ -7,28 +7,33 @@
 //===----------------------------------------------------------------------===//
 
 #include "hdr/types/struct_iovec.h"
-#include "src/fcntl/open.h"
 #include "src/sys/uio/writev.h"
 #include "src/unistd/close.h"
-#include "src/unistd/unlink.h"
+#include "src/unistd/pipe.h"
+#include "src/unistd/read.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
 
 TEST(LlvmLibcSysUioWritevTest, SmokeTest) {
-  const char *filename = "./LlvmLibcSysUioWritevTest";
-  int fd = LIBC_NAMESPACE::open(filename, O_WRONLY | O_CREAT, 0644);
-  ASSERT_THAT(fd, returns(GT(0)).with_errno(EQ(0)));
+  int pipefd[2];
+  ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds());
+
   const char *data = "Hello, World!\n";
   struct iovec iov[2];
   iov[0].iov_base = const_cast<char *>(data);
   iov[0].iov_len = 7;
   iov[1].iov_base = const_cast<char *>(data + 7);
   iov[1].iov_len = 8;
-  ASSERT_THAT(LIBC_NAMESPACE::writev(fd, iov, 2),
+  ASSERT_THAT(LIBC_NAMESPACE::writev(pipefd[1], iov, 2),
+              returns(EQ(ssize_t(15))).with_errno(EQ(0)));
+  ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds());
+
+  char buf[16];
+  ASSERT_THAT(LIBC_NAMESPACE::read(pipefd[0], buf, 15),
               returns(EQ(ssize_t(15))).with_errno(EQ(0)));
-  ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
-  ASSERT_THAT(LIBC_NAMESPACE::unlink(filename),
-              returns(EQ(ssize_t(0))).with_errno(EQ(0)));
+  buf[15] = '\0';
+  EXPECT_STREQ(buf, data);
+  ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds());
 }
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 070c25f24e61a..a25e3a29af95c 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -230,22 +230,19 @@ add_libc_unittest(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
-add_libc_unittest(
+add_libc_test(
   read_write_test
   SUITE
     libc_unistd_unittests
   SRCS
     read_write_test.cpp
   DEPENDS
-    libc.hdr.sys_stat_macros
     libc.include.unistd
     libc.src.errno.errno
-    libc.src.fcntl.open
     libc.src.unistd.close
-    libc.src.unistd.fsync
+    libc.src.unistd.pipe
     libc.src.unistd.read
     libc.src.unistd.write
-    libc.src.stdio.remove
     libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
diff --git a/libc/test/src/unistd/read_write_test.cpp b/libc/test/src/unistd/read_write_test.cpp
index 17cd4f6ce8b44..7b700154d2f30 100644
--- a/libc/test/src/unistd/read_write_test.cpp
+++ b/libc/test/src/unistd/read_write_test.cpp
@@ -6,11 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "hdr/sys_stat_macros.h"
-#include "src/fcntl/open.h"
-#include "src/stdio/remove.h"
 #include "src/unistd/close.h"
-#include "src/unistd/fsync.h"
+#include "src/unistd/pipe.h"
 #include "src/unistd/read.h"
 #include "src/unistd/write.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
@@ -21,29 +18,20 @@ using LlvmLibcUniStd = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
 TEST_F(LlvmLibcUniStd, WriteAndReadBackTest) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
-  constexpr const char *FILENAME = "__unistd_read_write.test";
-  auto TEST_FILE = libc_make_test_file_path(FILENAME);
+  int pipefd[2];
+  ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds(0));
 
-  int write_fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
-  ASSERT_ERRNO_SUCCESS();
-  ASSERT_GT(write_fd, 0);
   constexpr const char HELLO[] = "hello";
   constexpr ssize_t HELLO_SIZE = sizeof(HELLO);
-  ASSERT_THAT(LIBC_NAMESPACE::write(write_fd, HELLO, HELLO_SIZE),
+  ASSERT_THAT(LIBC_NAMESPACE::write(pipefd[1], HELLO, HELLO_SIZE),
               Succeeds(HELLO_SIZE));
-  ASSERT_THAT(LIBC_NAMESPACE::fsync(write_fd), Succeeds(0));
-  ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds(0));
 
-  int read_fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
-  ASSERT_ERRNO_SUCCESS();
-  ASSERT_GT(read_fd, 0);
   char read_buf[10];
-  ASSERT_THAT(LIBC_NAMESPACE::read(read_fd, read_buf, HELLO_SIZE),
+  ASSERT_THAT(LIBC_NAMESPACE::read(pipefd[0], read_buf, HELLO_SIZE),
               Succeeds(HELLO_SIZE));
   EXPECT_STREQ(read_buf, HELLO);
-  ASSERT_THAT(LIBC_NAMESPACE::close(read_fd), Succeeds(0));
-
-  ASSERT_THAT(LIBC_NAMESPACE::remove(TEST_FILE), Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds(0));
 }
 
 TEST_F(LlvmLibcUniStd, WriteFails) {
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel
index 54006159cee10..6507820ea5679 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel
@@ -107,11 +107,8 @@ libc_test(
     srcs = ["read_write_test.cpp"],
     deps = [
         "//libc:close",
-        "//libc:fsync",
-        "//libc:hdr_sys_stat_macros",
-        "//libc:open",
+        "//libc:pipe",
         "//libc:read",
-        "//libc:remove",
         "//libc:write",
     ],
 )

``````````

</details>


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


More information about the libc-commits mailing list