[libc-commits] [libc] [llvm] Process mrelease (PR #119147)

Omar Hossam via libc-commits libc-commits at lists.llvm.org
Sun Dec 8 12:39:27 PST 2024


https://github.com/moar55 updated https://github.com/llvm/llvm-project/pull/119147

>From c4bf6b97975ad7a79392717e0315cf2940bd477a Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 8 Dec 2024 11:34:29 -0800
Subject: [PATCH 1/2] [memprof] Drop memprof:: in unit tests (NFC) (#119113)

This patch replaces memprof::Foo with Foo if we have corresponding:

  using llvm::memprof::Foo;

 #110124: Add support for process_mrelease syscall

reformat

fix cmakelist

add missing target

address comments

fix license

check against errno

fix formatting
---
 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   |  20 ++
 .../sys/mman/linux/process_mrelease_test.cpp  |  74 ++++
 llvm/unittests/ProfileData/InstrProfTest.cpp  |   4 +-
 llvm/unittests/ProfileData/MemProfTest.cpp    |  56 ++-
 14 files changed, 544 insertions(+), 33 deletions(-)
 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..6021ccd8c89a82a 100644
--- a/libc/test/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/test/src/sys/mman/linux/CMakeLists.txt
@@ -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
+    )
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..1c3c549aa9bebdc
--- /dev/null
+++ b/libc/test/src/sys/mman/linux/process_mrelease_test.cpp
@@ -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(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_THAT(LIBC_NAMESPACE::process_mrelease(pidfd, 0), Succeeds());
+
+    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);
+
+    EXPECT_THAT(LIBC_NAMESPACE::process_mrelease(pidfd, 0), Fails(EINVAL));
+
+    LIBC_NAMESPACE::close(pidfd);
+  }
+}
+
+TEST(LlvmLibcMProcessMReleaseTest, ErrorNonExistingPidfd) {
+  EXPECT_THAT(LIBC_NAMESPACE::process_mrelease(-1, 0), Fails(EBADF));
+}
diff --git a/llvm/unittests/ProfileData/InstrProfTest.cpp b/llvm/unittests/ProfileData/InstrProfTest.cpp
index 6df2ec1efcd81cc..ac872d8235622b3 100644
--- a/llvm/unittests/ProfileData/InstrProfTest.cpp
+++ b/llvm/unittests/ProfileData/InstrProfTest.cpp
@@ -393,7 +393,7 @@ IndexedMemProfRecord
 makeRecordV2(std::initializer_list<::llvm::memprof::CallStackId> AllocFrames,
              std::initializer_list<::llvm::memprof::CallStackId> CallSiteFrames,
              const MemInfoBlock &Block, const memprof::MemProfSchema &Schema) {
-  llvm::memprof::IndexedMemProfRecord MR;
+  IndexedMemProfRecord MR;
   for (const auto &CSId : AllocFrames)
     MR.AllocSites.emplace_back(CSId, Block, Schema);
   for (const auto &CSId : CallSiteFrames)
@@ -533,7 +533,7 @@ TEST_F(InstrProfTest, test_caller_callee_pairs) {
       /*AllocFrames=*/{0x111, 0x222},
       /*CallSiteFrames=*/{}, MIB, memprof::getHotColdSchema());
 
-  memprof::IndexedMemProfData MemProfData;
+  IndexedMemProfData MemProfData;
   MemProfData.Frames.try_emplace(0, 0x123, 1, 2, false);
   MemProfData.Frames.try_emplace(1, 0x234, 3, 4, true);
   MemProfData.Frames.try_emplace(2, 0x123, 5, 6, false);
diff --git a/llvm/unittests/ProfileData/MemProfTest.cpp b/llvm/unittests/ProfileData/MemProfTest.cpp
index 55feb1c3f60b453..4edd5d449072cfc 100644
--- a/llvm/unittests/ProfileData/MemProfTest.cpp
+++ b/llvm/unittests/ProfileData/MemProfTest.cpp
@@ -277,9 +277,9 @@ TEST(MemProf, RecordSerializationRoundTripVerion2) {
                     /*dealloc_timestamp=*/2000, /*alloc_cpu=*/3,
                     /*dealloc_cpu=*/4, /*Histogram=*/0, /*HistogramSize=*/0);
 
-  llvm::SmallVector<llvm::memprof::CallStackId> CallStackIds = {0x123, 0x456};
+  llvm::SmallVector<CallStackId> CallStackIds = {0x123, 0x456};
 
-  llvm::SmallVector<llvm::memprof::CallStackId> CallSiteIds = {0x333, 0x444};
+  llvm::SmallVector<CallStackId> CallSiteIds = {0x333, 0x444};
 
   IndexedMemProfRecord Record;
   for (const auto &CSId : CallStackIds) {
@@ -308,9 +308,9 @@ TEST(MemProf, RecordSerializationRoundTripVersion2HotColdSchema) {
   Info.TotalLifetime = 33;
   Info.TotalLifetimeAccessDensity = 44;
 
-  llvm::SmallVector<llvm::memprof::CallStackId> CallStackIds = {0x123, 0x456};
+  llvm::SmallVector<CallStackId> CallStackIds = {0x123, 0x456};
 
-  llvm::SmallVector<llvm::memprof::CallStackId> CallSiteIds = {0x333, 0x444};
+  llvm::SmallVector<CallStackId> CallSiteIds = {0x333, 0x444};
 
   IndexedMemProfRecord Record;
   for (const auto &CSId : CallStackIds) {
@@ -422,7 +422,7 @@ TEST(MemProf, SymbolizationFilter) {
 }
 
 TEST(MemProf, BaseMemProfReader) {
-  llvm::memprof::IndexedMemProfData MemProfData;
+  IndexedMemProfData MemProfData;
   Frame F1(/*Hash=*/IndexedMemProfRecord::getGUID("foo"), /*LineOffset=*/20,
            /*Column=*/5, /*IsInlineFrame=*/true);
   Frame F2(/*Hash=*/IndexedMemProfRecord::getGUID("bar"), /*LineOffset=*/10,
@@ -455,7 +455,7 @@ TEST(MemProf, BaseMemProfReader) {
 }
 
 TEST(MemProf, BaseMemProfReaderWithCSIdMap) {
-  llvm::memprof::IndexedMemProfData MemProfData;
+  IndexedMemProfData MemProfData;
   Frame F1(/*Hash=*/IndexedMemProfRecord::getGUID("foo"), /*LineOffset=*/20,
            /*Column=*/5, /*IsInlineFrame=*/true);
   Frame F2(/*Hash=*/IndexedMemProfRecord::getGUID("bar"), /*LineOffset=*/10,
@@ -553,8 +553,8 @@ MemInfoBlock makePartialMIB() {
 TEST(MemProf, MissingCallStackId) {
   // Use a non-existent CallStackId to trigger a mapping error in
   // toMemProfRecord.
-  llvm::memprof::IndexedAllocationInfo AI(0xdeadbeefU, makePartialMIB(),
-                                          llvm::memprof::getHotColdSchema());
+  IndexedAllocationInfo AI(0xdeadbeefU, makePartialMIB(),
+                           llvm::memprof::getHotColdSchema());
 
   IndexedMemProfRecord IndexedMR;
   IndexedMR.AllocSites.push_back(AI);
@@ -575,8 +575,8 @@ TEST(MemProf, MissingCallStackId) {
 }
 
 TEST(MemProf, MissingFrameId) {
-  llvm::memprof::IndexedAllocationInfo AI(0x222, makePartialMIB(),
-                                          llvm::memprof::getHotColdSchema());
+  IndexedAllocationInfo AI(0x222, makePartialMIB(),
+                           llvm::memprof::getHotColdSchema());
 
   IndexedMemProfRecord IndexedMR;
   IndexedMR.AllocSites.push_back(AI);
@@ -602,9 +602,8 @@ TEST(MemProf, MissingFrameId) {
 TEST(MemProf, RadixTreeBuilderEmpty) {
   llvm::DenseMap<FrameId, llvm::memprof::LinearFrameId> MemProfFrameIndexes;
   llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> MemProfCallStackData;
-  llvm::DenseMap<llvm::memprof::FrameId, llvm::memprof::FrameStat>
-      FrameHistogram =
-          llvm::memprof::computeFrameHistogram<FrameId>(MemProfCallStackData);
+  llvm::DenseMap<FrameId, llvm::memprof::FrameStat> FrameHistogram =
+      llvm::memprof::computeFrameHistogram<FrameId>(MemProfCallStackData);
   llvm::memprof::CallStackRadixTreeBuilder<FrameId> Builder;
   Builder.build(std::move(MemProfCallStackData), &MemProfFrameIndexes,
                 FrameHistogram);
@@ -617,12 +616,11 @@ TEST(MemProf, RadixTreeBuilderEmpty) {
 TEST(MemProf, RadixTreeBuilderOne) {
   llvm::DenseMap<FrameId, llvm::memprof::LinearFrameId> MemProfFrameIndexes = {
       {11, 1}, {12, 2}, {13, 3}};
-  llvm::SmallVector<llvm::memprof::FrameId> CS1 = {13, 12, 11};
+  llvm::SmallVector<FrameId> CS1 = {13, 12, 11};
   llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> MemProfCallStackData;
   MemProfCallStackData.insert({hashCallStack(CS1), CS1});
-  llvm::DenseMap<llvm::memprof::FrameId, llvm::memprof::FrameStat>
-      FrameHistogram =
-          llvm::memprof::computeFrameHistogram<FrameId>(MemProfCallStackData);
+  llvm::DenseMap<FrameId, llvm::memprof::FrameStat> FrameHistogram =
+      llvm::memprof::computeFrameHistogram<FrameId>(MemProfCallStackData);
   llvm::memprof::CallStackRadixTreeBuilder<FrameId> Builder;
   Builder.build(std::move(MemProfCallStackData), &MemProfFrameIndexes,
                 FrameHistogram);
@@ -640,14 +638,13 @@ TEST(MemProf, RadixTreeBuilderOne) {
 TEST(MemProf, RadixTreeBuilderTwo) {
   llvm::DenseMap<FrameId, llvm::memprof::LinearFrameId> MemProfFrameIndexes = {
       {11, 1}, {12, 2}, {13, 3}};
-  llvm::SmallVector<llvm::memprof::FrameId> CS1 = {12, 11};
-  llvm::SmallVector<llvm::memprof::FrameId> CS2 = {13, 12, 11};
+  llvm::SmallVector<FrameId> CS1 = {12, 11};
+  llvm::SmallVector<FrameId> CS2 = {13, 12, 11};
   llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> MemProfCallStackData;
   MemProfCallStackData.insert({hashCallStack(CS1), CS1});
   MemProfCallStackData.insert({hashCallStack(CS2), CS2});
-  llvm::DenseMap<llvm::memprof::FrameId, llvm::memprof::FrameStat>
-      FrameHistogram =
-          llvm::memprof::computeFrameHistogram<FrameId>(MemProfCallStackData);
+  llvm::DenseMap<FrameId, llvm::memprof::FrameStat> FrameHistogram =
+      llvm::memprof::computeFrameHistogram<FrameId>(MemProfCallStackData);
   llvm::memprof::CallStackRadixTreeBuilder<FrameId> Builder;
   Builder.build(std::move(MemProfCallStackData), &MemProfFrameIndexes,
                 FrameHistogram);
@@ -670,18 +667,17 @@ TEST(MemProf, RadixTreeBuilderSuccessiveJumps) {
   llvm::DenseMap<FrameId, llvm::memprof::LinearFrameId> MemProfFrameIndexes = {
       {11, 1}, {12, 2}, {13, 3}, {14, 4}, {15, 5}, {16, 6}, {17, 7}, {18, 8},
   };
-  llvm::SmallVector<llvm::memprof::FrameId> CS1 = {14, 13, 12, 11};
-  llvm::SmallVector<llvm::memprof::FrameId> CS2 = {15, 13, 12, 11};
-  llvm::SmallVector<llvm::memprof::FrameId> CS3 = {17, 16, 12, 11};
-  llvm::SmallVector<llvm::memprof::FrameId> CS4 = {18, 16, 12, 11};
+  llvm::SmallVector<FrameId> CS1 = {14, 13, 12, 11};
+  llvm::SmallVector<FrameId> CS2 = {15, 13, 12, 11};
+  llvm::SmallVector<FrameId> CS3 = {17, 16, 12, 11};
+  llvm::SmallVector<FrameId> CS4 = {18, 16, 12, 11};
   llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> MemProfCallStackData;
   MemProfCallStackData.insert({hashCallStack(CS1), CS1});
   MemProfCallStackData.insert({hashCallStack(CS2), CS2});
   MemProfCallStackData.insert({hashCallStack(CS3), CS3});
   MemProfCallStackData.insert({hashCallStack(CS4), CS4});
-  llvm::DenseMap<llvm::memprof::FrameId, llvm::memprof::FrameStat>
-      FrameHistogram =
-          llvm::memprof::computeFrameHistogram<FrameId>(MemProfCallStackData);
+  llvm::DenseMap<FrameId, llvm::memprof::FrameStat> FrameHistogram =
+      llvm::memprof::computeFrameHistogram<FrameId>(MemProfCallStackData);
   llvm::memprof::CallStackRadixTreeBuilder<FrameId> Builder;
   Builder.build(std::move(MemProfCallStackData), &MemProfFrameIndexes,
                 FrameHistogram);
@@ -737,7 +733,7 @@ TEST(MemProf, YAMLParser) {
 
   llvm::memprof::YAMLMemProfReader YAMLReader;
   YAMLReader.parse(YAMLData);
-  llvm::memprof::IndexedMemProfData MemProfData = YAMLReader.takeMemProfData();
+  IndexedMemProfData MemProfData = YAMLReader.takeMemProfData();
 
   Frame F1(0x100, 11, 10, true);
   Frame F2(0x200, 22, 20, false);

>From 0eab2e23042c3477adfcb6edf5fd142e7b4e06f2 Mon Sep 17 00:00:00 2001
From: moar55 <moar.ahmed at gmail.com>
Date: Sun, 8 Dec 2024 21:24:19 +0100
Subject: [PATCH 2/2] [libc] Implement process_mrelease #117851

---
 libc/test/src/sys/mman/linux/CMakeLists.txt   |  8 +--
 .../sys/mman/linux/process_mrelease_test.cpp  | 59 +------------------
 2 files changed, 4 insertions(+), 63 deletions(-)

diff --git a/libc/test/src/sys/mman/linux/CMakeLists.txt b/libc/test/src/sys/mman/linux/CMakeLists.txt
index 6021ccd8c89a82a..9c7c06d7c4869fa 100644
--- a/libc/test/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/test/src/sys/mman/linux/CMakeLists.txt
@@ -193,11 +193,5 @@ add_libc_unittest(
     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
-    )
+)
diff --git a/libc/test/src/sys/mman/linux/process_mrelease_test.cpp b/libc/test/src/sys/mman/linux/process_mrelease_test.cpp
index 1c3c549aa9bebdc..d369c92031a7f09 100644
--- a/libc/test/src/sys/mman/linux/process_mrelease_test.cpp
+++ b/libc/test/src/sys/mman/linux/process_mrelease_test.cpp
@@ -6,14 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#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"
 
@@ -21,54 +14,8 @@
 
 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(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_THAT(LIBC_NAMESPACE::process_mrelease(pidfd, 0), Succeeds());
-
-    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);
-
-    EXPECT_THAT(LIBC_NAMESPACE::process_mrelease(pidfd, 0), Fails(EINVAL));
-
-    LIBC_NAMESPACE::close(pidfd);
-  }
-}
-
-TEST(LlvmLibcMProcessMReleaseTest, ErrorNonExistingPidfd) {
+#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