-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc] Implement process_mrelease. #117503
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-libc Author: Omar Hossam (moar55) ChangesThis PR addresses #110124. Full diff: https://github.com/llvm/llvm-project/pull/117503.diff 12 Files Affected:
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 74ca3742977a5f..08b5072499da69 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -252,6 +252,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sys.mman.munlockall
libc.src.sys.mman.munmap
libc.src.sys.mman.remap_file_pages
+ libc.src.sys.mman.process_mrelease
libc.src.sys.mman.posix_madvise
libc.src.sys.mman.shm_open
libc.src.sys.mman.shm_unlink
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 5419462d4f5b3b..4ea65f76d7948d 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -251,6 +251,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sys.mman.munmap
libc.src.sys.mman.remap_file_pages
libc.src.sys.mman.posix_madvise
+ libc.src.sys.mman.process_mrelease
libc.src.sys.mman.shm_open
libc.src.sys.mman.shm_unlink
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 957e28bd66cc4c..d0651c06b930ad 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -252,6 +252,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sys.mman.munmap
libc.src.sys.mman.remap_file_pages
libc.src.sys.mman.posix_madvise
+ libc.src.sys.mman.process_mrelease
libc.src.sys.mman.shm_open
libc.src.sys.mman.shm_unlink
diff --git a/libc/include/sys/syscall.h.def b/libc/include/sys/syscall.h.def
index 03c19eb0885ed6..11758ea8336ddf 100644
--- a/libc/include/sys/syscall.h.def
+++ b/libc/include/sys/syscall.h.def
@@ -2349,5 +2349,12 @@
#define SYS_writev __NR_writev
#endif
+#ifdef __NR_process_mrelease
+#define SYS_process_mrelease __NR_process_mrelease
+#endif
+
+#ifdef __NR_pidfd_open
+#define SYS_pidfd_open __NR_pidfd_open
+#endif
#endif // LLVM_LIBC_SYS_SYSCALL_H
diff --git a/libc/newhdrgen/yaml/sys/mman.yaml b/libc/newhdrgen/yaml/sys/mman.yaml
index 962ca3591917f7..dd53eb60d1ec57 100644
--- a/libc/newhdrgen/yaml/sys/mman.yaml
+++ b/libc/newhdrgen/yaml/sys/mman.yaml
@@ -132,3 +132,10 @@ functions:
return_type: int
arguments:
- type: const char *
+ - name: process_mrelease
+ standards:
+ - Linux
+ return_type: int
+ arguments:
+ - type: int
+ - type: unsigned int
diff --git a/libc/spec/linux.td b/libc/spec/linux.td
index 9b5dc8e30c95e4..99e0949a592dfa 100644
--- a/libc/spec/linux.td
+++ b/libc/spec/linux.td
@@ -112,6 +112,12 @@ def Linux : StandardSpec<"Linux"> {
ArgSpec<IntType>,
ArgSpec<SizeTType>,
ArgSpec<IntType>,
+ FunctionSpec<
+ "process_mrelease",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<IntType>,
+ ArgSpec<UnsignedIntType>
]
>,
FunctionSpec<
diff --git a/libc/src/sys/mman/CMakeLists.txt b/libc/src/sys/mman/CMakeLists.txt
index 4d4c2ad376050e..7d71f14ff8837f 100644
--- a/libc/src/sys/mman/CMakeLists.txt
+++ b/libc/src/sys/mman/CMakeLists.txt
@@ -113,3 +113,8 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.mremap
)
+add_entrypoint_object(
+ process_mrelease
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.process_mrelease)
diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt
index 89a0ad1527a065..23a8486a9e12dd 100644
--- a/libc/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/src/sys/mman/linux/CMakeLists.txt
@@ -36,7 +36,6 @@ add_entrypoint_object(
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
-
add_entrypoint_object(
munmap
SRCS
@@ -214,3 +213,16 @@ add_entrypoint_object(
libc.src.unistd.unlink
.shm_common
)
+
+add_entrypoint_object(
+ process_mrelease
+ SRCS
+ process_mrelease.cpp
+ HDRS
+ ../process_mrelease.h
+ DEPENDS
+ libc.include.signal
+ libc.src.signal.kill
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.errno.errno)
diff --git a/libc/src/sys/mman/linux/process_mrelease.cpp b/libc/src/sys/mman/linux/process_mrelease.cpp
new file mode 100644
index 00000000000000..d71b1289c3e928
--- /dev/null
+++ b/libc/src/sys/mman/linux/process_mrelease.cpp
@@ -0,0 +1,33 @@
+//===---------- Linux implementation of the mrelease function -----------===//
+//
+// 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/sys/mman/process_mrelease.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include "src/__support/macros/config.h"
+#include "src/errno/libc_errno.h"
+#include <linux/param.h> // For EXEC_PAGESIZE.
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, process_mrelease, (int pidfd, unsigned int flags)) {
+ long ret =
+ LIBC_NAMESPACE::syscall_impl<int>(SYS_process_mrelease, pidfd, flags);
+
+ if (ret < 0) {
+ libc_errno = static_cast<int>(-ret);
+ return libc_errno;
+ }
+
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/mman/process_mrelease.h b/libc/src/sys/mman/process_mrelease.h
new file mode 100644
index 00000000000000..13a9ba57eae19b
--- /dev/null
+++ b/libc/src/sys/mman/process_mrelease.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for process_mrelease function -----------------*-
+// 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_SYS_MMAN_PROCESS_MRELEASE_H
+#define LLVM_LIBC_SRC_SYS_MMAN_PROCESS_MRELEASE_H
+
+#include "src/__support/macros/config.h"
+#include <sys/mman.h> // For size_t and off_t
+
+namespace LIBC_NAMESPACE_DECL {
+
+int process_mrelease(int pidfd, unsigned int flags);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_MMAN_PROCESS_MRELEASE_H
diff --git a/libc/test/src/sys/mman/linux/CMakeLists.txt b/libc/test/src/sys/mman/linux/CMakeLists.txt
index 44ed11aadfe8b7..87fcc8a25812b9 100644
--- a/libc/test/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/test/src/sys/mman/linux/CMakeLists.txt
@@ -1,5 +1,3 @@
-add_custom_target(libc_sys_mman_unittests)
-
add_libc_unittest(
mmap_test
SUITE
@@ -181,3 +179,19 @@ add_libc_unittest(
libc.hdr.fcntl_macros
libc.test.UnitTest.ErrnoSetterMatcher
)
+
+add_libc_unittest(
+ process_mrelease_test
+ SUITE
+ libc_sys_mman_unittests
+ SRCS
+ process_mrelease_test.cpp
+ DEPENDS
+ libc.include.sys_mman
+ libc.include.sys_syscall
+ libc.src.errno.errno
+ libc.src.sys.mman.process_mrelease
+ libc.src.unistd.close
+ libc.src.stdlib.exit
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.threads.sleep)
diff --git a/libc/test/src/sys/mman/linux/process_mrelease_test.cpp b/libc/test/src/sys/mman/linux/process_mrelease_test.cpp
new file mode 100644
index 00000000000000..aa47b1ebf9fdcf
--- /dev/null
+++ b/libc/test/src/sys/mman/linux/process_mrelease_test.cpp
@@ -0,0 +1,72 @@
+//===-- Unittests for process_mrelease
+//-------------------------------------===//
+//
+// 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/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/threads/sleep.h"
+#include "src/errno/libc_errno.h"
+#include "src/signal/kill.h"
+#include "src/stdlib/exit.h"
+#include "src/sys/mman/process_mrelease.h"
+#include "src/unistd/close.h"
+#include "src/unistd/fork.h"
+#include "test/UnitTest/LibcTest.h"
+
+#include <sys/syscall.h>
+
+int pidfd_open(pid_t pid, unsigned int flags) {
+ return LIBC_NAMESPACE::syscall_impl(SYS_pidfd_open, pid, flags);
+}
+
+TEST(LlvmLibcMProcessMReleaseTest, NoError) {
+ pid_t child_pid = fork();
+ EXPECT_GE(child_pid, 0);
+
+ if (child_pid == 0) {
+ // Child process: wait a bit then exit gracefully.
+ LIBC_NAMESPACE::sleep_briefly();
+ LIBC_NAMESPACE::exit(0);
+ } else {
+ // Parent process: wait a bit and then kill the child.
+ // Give child process some time to start.
+ LIBC_NAMESPACE::sleep_briefly();
+ int pidfd = pidfd_open(child_pid, 0);
+ EXPECT_GE(pidfd, 0);
+
+ // Send SIGKILL to child process
+ LIBC_NAMESPACE::kill(child_pid, SIGKILL);
+
+ EXPECT_EQ(LIBC_NAMESPACE::process_mrelease(pidfd, 0), 0);
+
+ LIBC_NAMESPACE::close(pidfd);
+ }
+}
+
+TEST(LlvmLibcMProcessMReleaseTest, ErrorNotKilled) {
+ pid_t child_pid = fork();
+ EXPECT_GE(child_pid, 0);
+
+ if (child_pid == 0) {
+ // Child process: wait a bit then exit gracefully.
+ LIBC_NAMESPACE::sleep_briefly();
+ LIBC_NAMESPACE::exit(0);
+ } else {
+ // Give child process some time to start.
+ LIBC_NAMESPACE::sleep_briefly();
+ int pidfd = pidfd_open(child_pid, 0);
+ EXPECT_GE(pidfd, 0);
+
+ ASSERT_EQ(LIBC_NAMESPACE::process_mrelease(pidfd, 0), EINVAL);
+
+ LIBC_NAMESPACE::close(pidfd);
+ }
+}
+
+TEST(LlvmLibcMProcessMReleaseTest, ErrorNonExistingPidfd) {
+
+ ASSERT_EQ(LIBC_NAMESPACE::process_mrelease(12345, 0), EBADF);
+}
|
f582249
to
069e615
Compare
Hey @SchrodingerZhu can you assign a CO(if you aren't already) to view my PR :) |
e6a9fca
to
a8fcbfd
Compare
64dad6b
to
0194da2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thank you for the patch!
@moar55 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/104/builds/11392 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/71/builds/11380 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/179/builds/11109 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/171/builds/11127 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/131/builds/11220 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/188/builds/7315 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/196/builds/1580 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/183/builds/6781 Here is the relevant piece of the build log for the reference
|
This PR implements process_mrelease. A previous PR was merged #117503, but failed on merge due to an issue in the tests. Namely the failing tests were comparing against return type as opposed to errno. This is fixed in this PR.
…s_mrelease." (#117807) Reverts llvm/llvm-project#117503 GitOrigin-RevId: 2c9d9f39d26417fce2ccf5e87fcd84ed0f379ecd Original-Revision: dc76a123ac1d7575e8a8fc51750f35d1fe434b29 Roller-URL: https://cr-buildbucket.appspot.com/build/8730138437189786929 CQ-Do-Not-Cancel-Tryjobs: true Change-Id: I9157f41ab9fca634b8b3358830c02a8a859a4f37 Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1164328
…se." (#117807) Reverts llvm/llvm-project#117503 GitOrigin-RevId: 2c9d9f39d26417fce2ccf5e87fcd84ed0f379ecd Original-Revision: dc76a123ac1d7575e8a8fc51750f35d1fe434b29 Change-Id: I82333f9f3f8b90dea71bd897d6f976739ecaee86
…bc] Implement process_mrelease." (#117807) Reverts llvm/llvm-project#117503 GitOrigin-RevId: 1f191d4b5f44d69b98a4037dfb31fd1311ac16b2 Original-Revision: dc76a123ac1d7575e8a8fc51750f35d1fe434b29 Original-Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1164328 Original-Revision: a86fba66b677cd19c357b4fb0090c4b63de56ab8 Change-Id: Ic83eff136003aff4cd359510d7ca38f75494536b
This PR addresses #110124.