[libc-commits] [libc] Process mrelease (PR #119147)
Omar Hossam via libc-commits
libc-commits at lists.llvm.org
Sun Dec 8 12:45:11 PST 2024
https://github.com/moar55 updated https://github.com/llvm/llvm-project/pull/119147
>From aa63b908ca7db8d5aeb34a310c2a775f9a052e19 Mon Sep 17 00:00:00 2001
From: moar55 <moar.ahmed at gmail.com>
Date: Tue, 5 Nov 2024 20:57:03 +0100
Subject: [PATCH] [libc] Implement process_mrelease #117851
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/hdrgen/yaml/sys/mman.yaml | 7 +
libc/include/sys/syscall.h.def | 7 +
libc/spec/linux.td | 334 ++++++++++++++++++
libc/src/sys/mman/CMakeLists.txt | 6 +
libc/src/sys/mman/linux/CMakeLists.txt | 12 +-
libc/src/sys/mman/linux/process_mrelease.cpp | 33 ++
libc/src/sys/mman/process_mrelease.h | 21 ++
libc/test/src/sys/mman/linux/CMakeLists.txt | 14 +
.../sys/mman/linux/process_mrelease_test.cpp | 21 ++
12 files changed, 457 insertions(+), 1 deletion(-)
create mode 100644 libc/spec/linux.td
create mode 100644 libc/src/sys/mman/linux/process_mrelease.cpp
create mode 100644 libc/src/sys/mman/process_mrelease.h
create mode 100644 libc/test/src/sys/mman/linux/process_mrelease_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index effa5b12d87ac4a..aa0b8ba0490e98b 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 5a48baf104159f4..7bdb7f4d1ec61b6 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 1bedc25a9d0291f..5e9cc71279ab16d 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/hdrgen/yaml/sys/mman.yaml b/libc/hdrgen/yaml/sys/mman.yaml
index 962ca3591917f74..dd53eb60d1ec574 100644
--- a/libc/hdrgen/yaml/sys/mman.yaml
+++ b/libc/hdrgen/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/include/sys/syscall.h.def b/libc/include/sys/syscall.h.def
index 03c19eb0885ed6a..11758ea8336ddf6 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/spec/linux.td b/libc/spec/linux.td
new file mode 100644
index 000000000000000..99e0949a592dfa8
--- /dev/null
+++ b/libc/spec/linux.td
@@ -0,0 +1,334 @@
+def StructEpollEvent : NamedType<"struct epoll_event">;
+def StructEpollEventPtr : PtrType<StructEpollEvent>;
+
+def StructEpollData : NamedType<"struct epoll_data">;
+
+def Linux : StandardSpec<"Linux"> {
+ HeaderSpec Errno = HeaderSpec<
+ "errno.h",
+ [
+ Macro<"ENOMEDIUM">,
+ Macro<"ENOTBLK">,
+ Macro<"EMEDIUMTYPE">,
+ Macro<"EBADSLT">,
+ Macro<"ECHRNG">,
+ Macro<"ERFKILL">,
+ Macro<"EUSERS">,
+ Macro<"EBADR">,
+ Macro<"EL3HLT">,
+ Macro<"ENOTUNIQ">,
+ Macro<"EXFULL">,
+ Macro<"EHOSTDOWN">,
+ Macro<"EL3RST">,
+ Macro<"ENOPKG">,
+ Macro<"ENOCSI">,
+ Macro<"EUNATCH">,
+ Macro<"EREMCHG">,
+ Macro<"ETOOMANYREFS">,
+ Macro<"EL2HLT">,
+ Macro<"EBADFD">,
+ Macro<"EREMOTEIO">,
+ Macro<"ENAVAIL">,
+ Macro<"ELIBEXEC">,
+ Macro<"ESHUTDOWN">,
+ Macro<"ENOKEY">,
+ Macro<"ESTRPIPE">,
+ Macro<"EKEYREJECTED">,
+ Macro<"ESRMNT">,
+ Macro<"EKEYREVOKED">,
+ Macro<"EBADE">,
+ Macro<"ELIBBAD">,
+ Macro<"EISNAM">,
+ Macro<"EBFONT">,
+ Macro<"EPFNOSUPPORT">,
+ Macro<"EREMOTE">,
+ Macro<"EDEADLOCK">,
+ Macro<"ENONET">,
+ Macro<"EDOTDOT">,
+ Macro<"EKEYEXPIRED">,
+ Macro<"ELIBSCN">,
+ Macro<"ERESTART">,
+ Macro<"EBADRQC">,
+ Macro<"EUCLEAN">,
+ Macro<"ENOANO">,
+ Macro<"ELIBACC">,
+ Macro<"EHWPOISON">,
+ Macro<"ELIBMAX">,
+ Macro<"ESOCKTNOSUPPORT">,
+ Macro<"ENOTNAM">,
+ Macro<"ELNRNG">,
+ Macro<"EL2NSYNC">,
+ Macro<"EADV">,
+ Macro<"ECOMM">,
+ ]
+ >;
+
+ HeaderSpec Sched = HeaderSpec<
+ "sched.h",
+ [
+ Macro<"SCHED_OTHER">,
+ Macro<"SCHED_FIFO">,
+ Macro<"SCHED_RR">,
+ Macro<"SCHED_BATCH">,
+ Macro<"SCHED_ISO">,
+ Macro<"SCHED_IDLE">,
+ Macro<"SCHED_DEADLINE">,
+ ],
+ [], // Types
+ [], // Enumerations
+ [] // Functions
+ >;
+
+ HeaderSpec SysMMan = HeaderSpec<
+ "sys/mman.h",
+ [Macro<"MAP_ANONYMOUS">],
+ [], // Types
+ [], // Enumerations
+ [
+ FunctionSpec<
+ "mincore",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<VoidPtr>,
+ ArgSpec<SizeTType>,
+ ArgSpec<UnsignedCharPtr>,
+ ]
+ >,
+ FunctionSpec<
+ "mlock2",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<VoidPtr>,
+ ArgSpec<SizeTType>,
+ ArgSpec<UnsignedIntType>,
+ ]
+ >,
+ FunctionSpec<
+ "remap_file_pages",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<VoidPtr>,
+ ArgSpec<SizeTType>,
+ ArgSpec<IntType>,
+ ArgSpec<SizeTType>,
+ ArgSpec<IntType>,
+ FunctionSpec<
+ "process_mrelease",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<IntType>,
+ ArgSpec<UnsignedIntType>
+ ]
+ >,
+ FunctionSpec<
+ "mremap",
+ RetValSpec<VoidPtr>,
+ [
+ ArgSpec<VoidPtr>,
+ ArgSpec<SizeTType>,
+ ArgSpec<SizeTType>,
+ ArgSpec<IntType>,
+ ArgSpec<VarArgType>,
+ ]
+ >,
+ ] // Functions
+ >;
+
+
+ HeaderSpec SysPrctl = HeaderSpec<
+ "sys/prctl.h",
+ [], // Macros
+ [], // Types
+ [], // Enumerations
+ [
+ FunctionSpec<
+ "prctl",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<IntType>,
+ ArgSpec<UnsignedLongType>,
+ ArgSpec<UnsignedLongType>,
+ ArgSpec<UnsignedLongType>,
+ ArgSpec<UnsignedLongType>,
+ ]
+ >,
+ ] // Functions
+ >;
+
+ HeaderSpec SysRandom = HeaderSpec<
+ "sys/random.h",
+ [
+ Macro<"GRND_RANDOM">,
+ Macro<"GRND_NONBLOCK">,
+ Macro<"GRND_INSECURE">,
+ ],
+ [SizeTType, SSizeTType], // Types
+ [], // Enumerations
+ [
+ FunctionSpec<
+ "getrandom",
+ RetValSpec<SSizeTType>,
+ [
+ ArgSpec<VoidPtr>,
+ ArgSpec<SizeTType>,
+ ArgSpec<UnsignedIntType>
+ ]
+ >,
+ ]
+ >;
+
+ HeaderSpec SysTime = HeaderSpec<
+ "sys/time.h",
+ [
+ Macro<"timeradd">,
+ Macro<"timersub">,
+ Macro<"timerclear">,
+ Macro<"timerisset">,
+ Macro<"timercmp">,
+ ],
+ [StructTimevalType], // Types
+ [], // Enumerations
+ [] // Functions
+ >;
+
+
+ HeaderSpec SysEpoll = HeaderSpec<
+ "sys/epoll.h",
+ [], // Macros
+ [
+ StructEpollEvent,
+ StructEpollData,
+ SigSetType,
+ StructTimeSpec,
+ ], // Types
+ [], // Enumerations
+ [
+ FunctionSpec<
+ "epoll_create",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<IntType>
+ ]
+ >,
+ FunctionSpec<
+ "epoll_create1",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<IntType>
+ ]
+ >,
+ FunctionSpec<
+ "epoll_ctl",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<IntType>,
+ ArgSpec<IntType>,
+ ArgSpec<IntType>,
+ ArgSpec<StructEpollEventPtr>
+ ]
+ >,
+ FunctionSpec<
+ "epoll_wait",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<IntType>,
+ ArgSpec<StructEpollEventPtr>,
+ ArgSpec<IntType>,
+ ArgSpec<IntType>
+ ]
+ >,
+ FunctionSpec<
+ "epoll_pwait",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<IntType>,
+ ArgSpec<StructEpollEventPtr>,
+ ArgSpec<IntType>,
+ ArgSpec<IntType>,
+ ArgSpec<ConstSigSetPtrType>
+ ]
+ >,
+ FunctionSpec<
+ "epoll_pwait2",
+ RetValSpec<IntType>,
+ [
+ ArgSpec<IntType>,
+ ArgSpec<StructEpollEventPtr>,
+ ArgSpec<IntType>,
+ ArgSpec<ConstStructTimeSpecPtr>,
+ ArgSpec<ConstSigSetPtrType>
+ ]
+ >,
+ ] // Functions
+ >;
+
+ HeaderSpec Signal = HeaderSpec<
+ "signal.h",
+ [
+ Macro<"NSIG">,
+
+ Macro<"SIGHUP">,
+ Macro<"SIGINT">,
+ Macro<"SIGQUIT">,
+ Macro<"SIGILL">,
+ Macro<"SIGTRAP">,
+ Macro<"SIGABRT">,
+ Macro<"SIGIOT">,
+ Macro<"SIGBUS">,
+ Macro<"SIGFPE">,
+ Macro<"SIGKILL">,
+ Macro<"SIGUSR1">,
+ Macro<"SIGSEGV">,
+ Macro<"SIGUSR2">,
+ Macro<"SIGPIPE">,
+ Macro<"SIGALRM">,
+ Macro<"SIGTERM">,
+ Macro<"SIGSTKFLT">,
+ Macro<"SIGCHLD">,
+ Macro<"SIGCONT">,
+ Macro<"SIGSTOP">,
+ Macro<"SIGTSTP">,
+ Macro<"SIGTTIN">,
+ Macro<"SIGTTOU">,
+ Macro<"SIGURG">,
+ Macro<"SIGXCPU">,
+ Macro<"SIGXFSZ">,
+ Macro<"SIGVTALRM">,
+ Macro<"SIGPROF">,
+ Macro<"SIGWINCH">,
+ Macro<"SIGIO">,
+ Macro<"SIGPOLL">,
+ Macro<"SIGPWR">,
+ Macro<"SIGSYS">,
+ Macro<"SIGUNUSED">,
+ ]
+ >;
+
+
+ HeaderSpec UniStd = HeaderSpec<
+ "unistd.h",
+ [], // Macros
+ [],
+ [], // Enumerations
+ [
+ FunctionSpec<
+ "pipe2",
+ RetValSpec<IntType>,
+ [ArgSpec<IntPtr>, ArgSpec<IntType>] //TODO: make this int[2]
+ >,
+ ],
+ []
+ >;
+
+
+ let Headers = [
+ Errno,
+ SysEpoll,
+ SysMMan,
+ SysPrctl,
+ SysRandom,
+ SysTime,
+ Signal,
+ UniStd,
+ ];
+}
diff --git a/libc/src/sys/mman/CMakeLists.txt b/libc/src/sys/mman/CMakeLists.txt
index 4d4c2ad376050ec..281efc0ffcdf200 100644
--- a/libc/src/sys/mman/CMakeLists.txt
+++ b/libc/src/sys/mman/CMakeLists.txt
@@ -113,3 +113,9 @@ 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 89a0ad1527a0650..aa2ca4b160181a5 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,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)
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 000000000000000..e86bbec1b1b6612
--- /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 -1;
+ }
+
+ 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 000000000000000..6c800f2d0eab868
--- /dev/null
+++ b/libc/src/sys/mman/process_mrelease.h
@@ -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
diff --git a/libc/test/src/sys/mman/linux/CMakeLists.txt b/libc/test/src/sys/mman/linux/CMakeLists.txt
index 44ed11aadfe8b74..9c7c06d7c4869fa 100644
--- a/libc/test/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/test/src/sys/mman/linux/CMakeLists.txt
@@ -181,3 +181,17 @@ 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.test.UnitTest.ErrnoSetterMatcher
+)
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 000000000000000..d369c92031a7f09
--- /dev/null
+++ b/libc/test/src/sys/mman/linux/process_mrelease_test.cpp
@@ -0,0 +1,21 @@
+//===-- 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/sys/mman/process_mrelease.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/LibcTest.h"
+
+#include <sys/syscall.h>
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+#if defined(SYS_process_mrelease)
+TEST(LlvmLibcProcessMReleaseTest, ErrorNonExistingPidfd) {
+ EXPECT_THAT(LIBC_NAMESPACE::process_mrelease(-1, 0), Fails(EBADF));
+}
+#endif
More information about the libc-commits
mailing list