[libc-commits] [libc] [libc] pipe(2) linux syscall impl. draft (PR #85402)

via libc-commits libc-commits at lists.llvm.org
Fri Mar 15 07:08:46 PDT 2024


https://github.com/muffpy created https://github.com/llvm/llvm-project/pull/85402

None

>From 049c0c67ef9d2fafb7499a6e7b4ca0245943b1d8 Mon Sep 17 00:00:00 2001
From: muffpy <rahulathreya at yahoo.com>
Date: Fri, 15 Mar 2024 13:52:17 +0000
Subject: [PATCH] [libc] pipe(2) linux syscall impl. draft

---
 libc/config/linux/x86_64/entrypoints.txt |  1 +
 libc/spec/linux.td                       | 17 +++++++
 libc/src/unistd/CMakeLists.txt           |  7 +++
 libc/src/unistd/linux/CMakeLists.txt     | 13 +++++
 libc/src/unistd/linux/pipe2.cpp          | 35 ++++++++++++++
 libc/src/unistd/pipe2.h                  | 20 ++++++++
 libc/test/src/unistd/pipe2_test.cpp      | 61 ++++++++++++++++++++++++
 7 files changed, 154 insertions(+)
 create mode 100644 libc/src/unistd/linux/pipe2.cpp
 create mode 100644 libc/src/unistd/pipe2.h
 create mode 100644 libc/test/src/unistd/pipe2_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 4fb31c593b9dc7..702894b8a21d26 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -297,6 +297,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.link
     libc.src.unistd.linkat
     libc.src.unistd.lseek
+    libc.src.unistd.pipe2
     libc.src.unistd.pread
     libc.src.unistd.pwrite
     libc.src.unistd.read
diff --git a/libc/spec/linux.td b/libc/spec/linux.td
index f91f55ddac7846..80d3e860ed6c46 100644
--- a/libc/spec/linux.td
+++ b/libc/spec/linux.td
@@ -3,6 +3,8 @@ def StructEpollEventPtr : PtrType<StructEpollEvent>;
 
 def StructEpollData : NamedType<"struct epoll_data">;
 
+def Pipe2fdArrayT : NamedType<"__pipe2fd_array_t">;
+
 def Linux : StandardSpec<"Linux"> {
   HeaderSpec Errno = HeaderSpec<
       "errno.h",
@@ -264,6 +266,20 @@ def Linux : StandardSpec<"Linux"> {
       ]
   >;
 
+  HeaderSpec UniStd = HeaderSpec<
+    "unistd.h",
+    [], // Macros
+    [Pipe2fdArrayT], // Types
+    [], // Enumerations
+    [
+        FunctionSpec<
+          "pipe2",
+          RetValSpec<IntType>,
+          [ArgSpec<Pipe2fdArrayT>, ArgSpec<IntType>]
+        >,
+    ]
+  >;
+
   let Headers = [
     Errno,
     SysEpoll,
@@ -272,5 +288,6 @@ def Linux : StandardSpec<"Linux"> {
     SysRandom,
     SysTime,
     Signal,
+    UniStd
   ];
 }
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index e22b0e1872caa1..c63f170df29b96 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -149,6 +149,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.lseek
 )
 
+add_entrypoint_object(
+  pipe2
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.pipe2
+)
+
 add_entrypoint_object(
   pread
   ALIAS
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index df85d44e9e9edc..3496d32a18f2e4 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -273,6 +273,19 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  pipe2
+  SRCS
+    pipe2.cpp
+  HDRS
+    ../pipe2.h
+  DEPENDS
+    libc.include.unistd
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   pread
   SRCS
diff --git a/libc/src/unistd/linux/pipe2.cpp b/libc/src/unistd/linux/pipe2.cpp
new file mode 100644
index 00000000000000..9f2e5390104703
--- /dev/null
+++ b/libc/src/unistd/linux/pipe2.cpp
@@ -0,0 +1,35 @@
+//===-- Linux implementation of pipe2 ---------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/pipe2.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include "src/errno/libc_errno.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, pipe2, (int pipefd[2], int flags)) {
+    int ret;
+#ifdef SYS_pipe2
+  ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pipe2, pipefd, flags);
+#elif defined (SYS_pipe)
+  ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pipe, pipefd);
+#else 
+#error "pipe and pipe2 not available."
+#endif
+  if (ret == -1) {
+    libc_errno = -ret;
+    return -1;
+  }
+  return ret;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/unistd/pipe2.h b/libc/src/unistd/pipe2.h
new file mode 100644
index 00000000000000..3cf5d81f1f280e
--- /dev/null
+++ b/libc/src/unistd/pipe2.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for pipe2 ---------------------------*- C++ -*-===//
+//
+// 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_SRC_UNISTD_PIPE2_H
+#define LLVM_LIBC_SRC_UNISTD_PIPE2_H
+
+#include <unistd.h>
+
+namespace LIBC_NAMESPACE {
+
+int pipe2(int pipefd[2], int flags);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_UNISTD_PIPE2_H
diff --git a/libc/test/src/unistd/pipe2_test.cpp b/libc/test/src/unistd/pipe2_test.cpp
new file mode 100644
index 00000000000000..da6607fe68dda6
--- /dev/null
+++ b/libc/test/src/unistd/pipe2_test.cpp
@@ -0,0 +1,61 @@
+//===-- Unittests for pipe2 -------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/errno/libc_errno.h"
+#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "src/unistd/dup.h"
+#include "src/unistd/read.h"
+#include "src/unistd/unlink.h"
+#include "src/unistd/write.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include <sys/stat.h>
+
+TEST(LlvmLibcpipe2Test, ReadAndWriteViaPipe2) {
+  LIBC_NAMESPACE::libc_errno = 0;
+  using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+  constexpr const char *FILENAME = "pipe2.test";
+  auto TEST_FILE = libc_make_test_file_path(FILENAME);
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(fd, 0);
+  int dupfd = LIBC_NAMESPACE::dup(fd);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(dupfd, 0);
+
+  // Write something via the dup
+  constexpr char WRITE_DATA[] = "Hello, dup!";
+  constexpr size_t WRITE_SIZE = sizeof(WRITE_DATA);
+  ASSERT_EQ(ssize_t(WRITE_SIZE),
+            LIBC_NAMESPACE::write(dupfd, WRITE_DATA, WRITE_SIZE));
+  ASSERT_THAT(LIBC_NAMESPACE::close(dupfd), Succeeds(0));
+
+  // Reopen the file for reading and create a dup.
+  fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(fd, 0);
+  dupfd = LIBC_NAMESPACE::dup(fd);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(dupfd, 0);
+
+  // Read the file content via the dup.
+  char buf[WRITE_SIZE];
+  ASSERT_THAT(LIBC_NAMESPACE::read(dupfd, buf, WRITE_SIZE),
+              Succeeds(WRITE_SIZE));
+  ASSERT_STREQ(buf, WRITE_DATA);
+
+  ASSERT_THAT(LIBC_NAMESPACE::close(dupfd), Succeeds(0));
+  ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
+}
+
+TEST(LlvmLibcdupTest, DupBadFD) {
+  using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+  ASSERT_THAT(LIBC_NAMESPACE::dup(-1), Fails(EBADF));
+}



More information about the libc-commits mailing list