Skip to content

Commit d2b482b

Browse files
authored
[libc] (reland #117503) Implement process_mrelease (#117851)
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.
1 parent 65339e4 commit d2b482b

File tree

12 files changed

+188
-1
lines changed

12 files changed

+188
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,23 @@ 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
202+
libc.test.UnitTest.ErrnoSetterMatcher
203+
)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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/ErrnoSetterMatcher.h"
18+
#include "test/UnitTest/LibcTest.h"
19+
20+
#include <sys/syscall.h>
21+
22+
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
23+
24+
int pidfd_open(pid_t pid, unsigned int flags) {
25+
return LIBC_NAMESPACE::syscall_impl(SYS_pidfd_open, pid, flags);
26+
}
27+
28+
TEST(LlvmLibcProcessMReleaseTest, NoError) {
29+
pid_t child_pid = fork();
30+
EXPECT_GE(child_pid, 0);
31+
32+
if (child_pid == 0) {
33+
// Child process: wait a bit then exit gracefully.
34+
LIBC_NAMESPACE::sleep_briefly();
35+
LIBC_NAMESPACE::exit(0);
36+
} else {
37+
// Parent process: wait a bit and then kill the child.
38+
// Give child process some time to start.
39+
LIBC_NAMESPACE::sleep_briefly();
40+
int pidfd = pidfd_open(child_pid, 0);
41+
EXPECT_GE(pidfd, 0);
42+
43+
// Send SIGKILL to child process
44+
LIBC_NAMESPACE::kill(child_pid, SIGKILL);
45+
46+
EXPECT_THAT(LIBC_NAMESPACE::process_mrelease(pidfd, 0), Succeeds());
47+
48+
LIBC_NAMESPACE::close(pidfd);
49+
}
50+
}
51+
52+
TEST(LlvmLibcProcessMReleaseTest, ErrorNotKilled) {
53+
pid_t child_pid = fork();
54+
EXPECT_GE(child_pid, 0);
55+
56+
if (child_pid == 0) {
57+
// Child process: wait a bit then exit gracefully.
58+
LIBC_NAMESPACE::sleep_briefly();
59+
LIBC_NAMESPACE::exit(0);
60+
} else {
61+
// Give child process some time to start.
62+
LIBC_NAMESPACE::sleep_briefly();
63+
int pidfd = pidfd_open(child_pid, 0);
64+
EXPECT_GE(pidfd, 0);
65+
66+
EXPECT_THAT(LIBC_NAMESPACE::process_mrelease(pidfd, 0), Fails(EINVAL));
67+
68+
LIBC_NAMESPACE::close(pidfd);
69+
}
70+
}
71+
72+
TEST(LlvmLibcProcessMReleaseTest, ErrorNonExistingPidfd) {
73+
EXPECT_THAT(LIBC_NAMESPACE::process_mrelease(-1, 0), Fails(EBADF));
74+
}

0 commit comments

Comments
 (0)