Skip to content

Commit 36a46d8

Browse files
authored
[libc] Implement process_mrelease. (#117503)
This PR addresses #110124.
1 parent 5add295 commit 36a46d8

File tree

12 files changed

+184
-1
lines changed

12 files changed

+184
-1
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ set(TARGET_LIBC_ENTRYPOINTS
252252
libc.src.sys.mman.munlockall
253253
libc.src.sys.mman.munmap
254254
libc.src.sys.mman.remap_file_pages
255+
libc.src.sys.mman.process_mrelease
255256
libc.src.sys.mman.posix_madvise
256257
libc.src.sys.mman.shm_open
257258
libc.src.sys.mman.shm_unlink

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ set(TARGET_LIBC_ENTRYPOINTS
251251
libc.src.sys.mman.munmap
252252
libc.src.sys.mman.remap_file_pages
253253
libc.src.sys.mman.posix_madvise
254+
libc.src.sys.mman.process_mrelease
254255
libc.src.sys.mman.shm_open
255256
libc.src.sys.mman.shm_unlink
256257

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ set(TARGET_LIBC_ENTRYPOINTS
252252
libc.src.sys.mman.munmap
253253
libc.src.sys.mman.remap_file_pages
254254
libc.src.sys.mman.posix_madvise
255+
libc.src.sys.mman.process_mrelease
255256
libc.src.sys.mman.shm_open
256257
libc.src.sys.mman.shm_unlink
257258

libc/include/sys/syscall.h.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,5 +2349,12 @@
23492349
#define SYS_writev __NR_writev
23502350
#endif
23512351

2352+
#ifdef __NR_process_mrelease
2353+
#define SYS_process_mrelease __NR_process_mrelease
2354+
#endif
2355+
2356+
#ifdef __NR_pidfd_open
2357+
#define SYS_pidfd_open __NR_pidfd_open
2358+
#endif
23522359

23532360
#endif // LLVM_LIBC_SYS_SYSCALL_H

libc/newhdrgen/yaml/sys/mman.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,10 @@ functions:
132132
return_type: int
133133
arguments:
134134
- type: const char *
135+
- name: process_mrelease
136+
standards:
137+
- Linux
138+
return_type: int
139+
arguments:
140+
- type: int
141+
- type: unsigned int

libc/spec/linux.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ def Linux : StandardSpec<"Linux"> {
112112
ArgSpec<IntType>,
113113
ArgSpec<SizeTType>,
114114
ArgSpec<IntType>,
115+
FunctionSpec<
116+
"process_mrelease",
117+
RetValSpec<IntType>,
118+
[
119+
ArgSpec<IntType>,
120+
ArgSpec<UnsignedIntType>
115121
]
116122
>,
117123
FunctionSpec<

libc/src/sys/mman/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,9 @@ add_entrypoint_object(
113113
DEPENDS
114114
.${LIBC_TARGET_OS}.mremap
115115
)
116+
117+
add_entrypoint_object(
118+
process_mrelease
119+
ALIAS
120+
DEPENDS
121+
.${LIBC_TARGET_OS}.process_mrelease)

libc/src/sys/mman/linux/CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ add_entrypoint_object(
3636
libc.src.__support.OSUtil.osutil
3737
libc.src.errno.errno
3838
)
39-
4039
add_entrypoint_object(
4140
munmap
4241
SRCS
@@ -214,3 +213,14 @@ add_entrypoint_object(
214213
libc.src.unistd.unlink
215214
.shm_common
216215
)
216+
217+
add_entrypoint_object(
218+
process_mrelease
219+
SRCS
220+
process_mrelease.cpp
221+
HDRS
222+
../process_mrelease.h
223+
DEPENDS
224+
libc.include.sys_syscall
225+
libc.src.__support.OSUtil.osutil
226+
libc.src.errno.errno)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===---------- Linux implementation of the mrelease function -----------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/sys/mman/process_mrelease.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
#include "src/__support/common.h"
13+
14+
#include "src/__support/macros/config.h"
15+
#include "src/errno/libc_errno.h"
16+
#include <linux/param.h> // For EXEC_PAGESIZE.
17+
#include <sys/syscall.h> // For syscall numbers.
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
LLVM_LIBC_FUNCTION(int, process_mrelease, (int pidfd, unsigned int flags)) {
22+
long ret =
23+
LIBC_NAMESPACE::syscall_impl<int>(SYS_process_mrelease, pidfd, flags);
24+
25+
if (ret < 0) {
26+
libc_errno = static_cast<int>(-ret);
27+
return -1;
28+
}
29+
30+
return 0;
31+
}
32+
33+
} // namespace LIBC_NAMESPACE_DECL

libc/src/sys/mman/process_mrelease.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for process_mrelease function --------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===-------------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SYS_MMAN_PROCESS_MRELEASE_H
10+
#define LLVM_LIBC_SRC_SYS_MMAN_PROCESS_MRELEASE_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include <sys/mman.h> // For size_t and off_t
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
int process_mrelease(int pidfd, unsigned int flags);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_SYS_MMAN_PROCESS_MRELEASE_H

libc/test/src/sys/mman/linux/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,21 @@ add_libc_unittest(
181181
libc.hdr.fcntl_macros
182182
libc.test.UnitTest.ErrnoSetterMatcher
183183
)
184+
185+
add_libc_unittest(
186+
process_mrelease_test
187+
SUITE
188+
libc_sys_mman_unittests
189+
SRCS
190+
process_mrelease_test.cpp
191+
DEPENDS
192+
libc.include.sys_mman
193+
libc.include.sys_syscall
194+
libc.src.errno.errno
195+
libc.src.sys.mman.process_mrelease
196+
libc.src.unistd.close
197+
libc.src.signal.kill
198+
libc.include.signal
199+
libc.src.stdlib.exit
200+
libc.src.__support.OSUtil.osutil
201+
libc.src.__support.threads.sleep)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===-- Unittests for process_mrelease ------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
10+
#include "src/__support/threads/sleep.h"
11+
#include "src/errno/libc_errno.h"
12+
#include "src/signal/kill.h"
13+
#include "src/stdlib/exit.h"
14+
#include "src/sys/mman/process_mrelease.h"
15+
#include "src/unistd/close.h"
16+
#include "src/unistd/fork.h"
17+
#include "test/UnitTest/LibcTest.h"
18+
19+
#include <sys/syscall.h>
20+
21+
int pidfd_open(pid_t pid, unsigned int flags) {
22+
return LIBC_NAMESPACE::syscall_impl(SYS_pidfd_open, pid, flags);
23+
}
24+
25+
TEST(LlvmLibcMProcessMReleaseTest, NoError) {
26+
pid_t child_pid = fork();
27+
EXPECT_GE(child_pid, 0);
28+
29+
if (child_pid == 0) {
30+
// Child process: wait a bit then exit gracefully.
31+
LIBC_NAMESPACE::sleep_briefly();
32+
LIBC_NAMESPACE::exit(0);
33+
} else {
34+
// Parent process: wait a bit and then kill the child.
35+
// Give child process some time to start.
36+
LIBC_NAMESPACE::sleep_briefly();
37+
int pidfd = pidfd_open(child_pid, 0);
38+
EXPECT_GE(pidfd, 0);
39+
40+
// Send SIGKILL to child process
41+
LIBC_NAMESPACE::kill(child_pid, SIGKILL);
42+
43+
EXPECT_EQ(LIBC_NAMESPACE::process_mrelease(pidfd, 0), 0);
44+
45+
LIBC_NAMESPACE::close(pidfd);
46+
}
47+
}
48+
49+
TEST(LlvmLibcMProcessMReleaseTest, ErrorNotKilled) {
50+
pid_t child_pid = fork();
51+
EXPECT_GE(child_pid, 0);
52+
53+
if (child_pid == 0) {
54+
// Child process: wait a bit then exit gracefully.
55+
LIBC_NAMESPACE::sleep_briefly();
56+
LIBC_NAMESPACE::exit(0);
57+
} else {
58+
// Give child process some time to start.
59+
LIBC_NAMESPACE::sleep_briefly();
60+
int pidfd = pidfd_open(child_pid, 0);
61+
EXPECT_GE(pidfd, 0);
62+
63+
ASSERT_EQ(LIBC_NAMESPACE::process_mrelease(pidfd, 0), EINVAL);
64+
65+
LIBC_NAMESPACE::close(pidfd);
66+
}
67+
}
68+
69+
TEST(LlvmLibcMProcessMReleaseTest, ErrorNonExistingPidfd) {
70+
71+
ASSERT_EQ(LIBC_NAMESPACE::process_mrelease(-1, 0), EBADF);
72+
}

0 commit comments

Comments
 (0)