-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] pipe(2) linux syscall wrapper and unittest #85514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
6a75de8
a8afc5c
174676f
c6fab70
30d2385
53c43e3
6100033
7b87696
863c2ab
63d9955
4cf1101
95f743d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| //===-- Linux implementation of pipe2 | ||
muffpy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //---------------------------------------===// | ||
| // | ||
| // 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" | ||
muffpy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #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) | ||
muffpy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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; | ||
muffpy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| //===-- Implementation header for pipe2 ---------------------------*- C++ | ||
muffpy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //-*-===// | ||
| // | ||
| // 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> | ||
muffpy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| namespace LIBC_NAMESPACE { | ||
|
|
||
| int pipe2(int pipefd[2], int flags); | ||
|
|
||
| } // namespace LIBC_NAMESPACE | ||
|
|
||
| #endif // LLVM_LIBC_SRC_UNISTD_PIPE2_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| //===-- Unittests for pipe2 | ||
muffpy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //-------------------------------------------------===// | ||
| // | ||
| // 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/unistd/close.h" | ||
| #include "src/unistd/pipe2.h" | ||
| #include "src/unistd/read.h" | ||
| #include "src/unistd/write.h" | ||
| #include "test/UnitTest/ErrnoSetterMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| #include <fcntl.h> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please include include/llvm-libc-macros/linux/fcntl-macros.h instead.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and make sure to remove <fcntl.h>. If you get a compile time failure as a result, let me know what it is.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need <fcntl.h> for this subtest in Pipe2CreationTest. |
||
|
|
||
| TEST(LlvmLibcPipe2Test, ReadAndWriteViaPipe2) { | ||
| int pipefd[2]; | ||
| int flags; | ||
|
|
||
| LIBC_NAMESPACE::libc_errno = 0; | ||
| using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; | ||
|
|
||
| // Create pipe(2) with flags set to 0 | ||
| flags = 0; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In terms of flags we define, it looks like we only define
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. And created a new uapi header for O_NOTIFICATION_PIPE. May I ask if we are currently not categorizing headers into things like asm-generic and uapi? |
||
| ASSERT_NE(LIBC_NAMESPACE::pipe2(pipefd, flags), -1); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
|
|
||
| // Write something via the pipe and read from other end | ||
| constexpr char MESSAGE[] = "Hello from the write end!"; | ||
| constexpr size_t MESSAGE_SIZE = sizeof(MESSAGE); | ||
| char buf[MESSAGE_SIZE]; | ||
| ASSERT_EQ(ssize_t(MESSAGE_SIZE), | ||
| LIBC_NAMESPACE::write(pipefd[1], MESSAGE, MESSAGE_SIZE)); | ||
| ASSERT_EQ(ssize_t(MESSAGE_SIZE), | ||
| LIBC_NAMESPACE::read(pipefd[0], buf, MESSAGE_SIZE)); | ||
| ASSERT_STREQ(buf, MESSAGE); | ||
|
|
||
| // Close the pipe file descriptors | ||
| ASSERT_NE(LIBC_NAMESPACE::close(pipefd[0]), -1); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| ASSERT_NE(LIBC_NAMESPACE::close(pipefd[1]), -1); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
| } | ||
|
|
||
| TEST(LlvmLibcPipe2Test, Pipe2InvalidFlags) { | ||
| int invalidflags = O_CREAT | O_PATH | O_SYNC; | ||
| int pipefd[2]; | ||
|
|
||
| using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; | ||
| ASSERT_THAT(LIBC_NAMESPACE::pipe2(pipefd, invalidflags), Fails(EINVAL)); | ||
muffpy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| TEST(LlvmLibcPipe2Test, Pipe2InvalidPipeFD) { | ||
| int flags = 0; | ||
| int invalidpipefd[1]; | ||
|
|
||
| using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; | ||
| ASSERT_THAT(LIBC_NAMESPACE::pipe2(invalidpipefd, flags), Fails(EFAULT)); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.