Skip to content

[libc] Implement process_mrelease #117851

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

Merged
merged 9 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions libc/include/sys/syscall.h.def
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions libc/newhdrgen/yaml/sys/mman.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions libc/spec/linux.td
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ def Linux : StandardSpec<"Linux"> {
ArgSpec<IntType>,
ArgSpec<SizeTType>,
ArgSpec<IntType>,
FunctionSpec<
"process_mrelease",
RetValSpec<IntType>,
[
ArgSpec<IntType>,
ArgSpec<UnsignedIntType>
]
>,
FunctionSpec<
Expand Down
6 changes: 6 additions & 0 deletions libc/src/sys/mman/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,9 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.mremap
)

add_entrypoint_object(
process_mrelease
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.process_mrelease)
12 changes: 11 additions & 1 deletion libc/src/sys/mman/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ add_entrypoint_object(
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
munmap
SRCS
Expand Down Expand Up @@ -214,3 +213,14 @@ 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.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno)
33 changes: 33 additions & 0 deletions libc/src/sys/mman/linux/process_mrelease.cpp
Original file line number Diff line number Diff line change
@@ -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 -1;
}

return 0;
}

} // namespace LIBC_NAMESPACE_DECL
21 changes: 21 additions & 0 deletions libc/src/sys/mman/process_mrelease.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- 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
20 changes: 20 additions & 0 deletions libc/test/src/sys/mman/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,23 @@ 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.signal.kill
libc.include.signal
libc.src.stdlib.exit
libc.src.__support.OSUtil.osutil
libc.src.__support.threads.sleep
libc.test.UnitTest.ErrnoSetterMatcher
)
74 changes: 74 additions & 0 deletions libc/test/src/sys/mman/linux/process_mrelease_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//===-- 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/ErrnoSetterMatcher.h"
#include "test/UnitTest/LibcTest.h"

#include <sys/syscall.h>

using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;

int pidfd_open(pid_t pid, unsigned int flags) {
return LIBC_NAMESPACE::syscall_impl(SYS_pidfd_open, pid, flags);
}

TEST(LlvmLibcProcessMReleaseTest, 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_THAT(LIBC_NAMESPACE::process_mrelease(pidfd, 0), Succeeds());

LIBC_NAMESPACE::close(pidfd);
}
}

TEST(LlvmLibcProcessMReleaseTest, 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);

EXPECT_THAT(LIBC_NAMESPACE::process_mrelease(pidfd, 0), Fails(EINVAL));

LIBC_NAMESPACE::close(pidfd);
}
}

TEST(LlvmLibcProcessMReleaseTest, ErrorNonExistingPidfd) {
EXPECT_THAT(LIBC_NAMESPACE::process_mrelease(-1, 0), Fails(EBADF));
}