[llvm] [z/OS] Add call to shmctl() to release shared memory on z/OS (PR #130163)

Zibi Sarbinowski via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 6 11:29:30 PST 2025


https://github.com/zibi2 created https://github.com/llvm/llvm-project/pull/130163

This PR will solve the issue with leaking shared memory we have after running llvm lit test on z/OS.
In particular llvm/unittests/ExecutionEngine/Orc/SharedMemoryMapperTest.cpp was causing the leak.

>From 24ac1fbcb2b21d7d74a2d81628e46923a997aaa6 Mon Sep 17 00:00:00 2001
From: Zibi Sarbinowski <zibi at ca.ibm.com>
Date: Thu, 6 Mar 2025 19:41:40 +0000
Subject: [PATCH] Add call to shmctl() to release shared memory on z/OS

---
 llvm/include/llvm/ExecutionEngine/Orc/MemoryMapper.h |  1 +
 llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp        | 11 +++++++----
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/ExecutionEngine/Orc/MemoryMapper.h b/llvm/include/llvm/ExecutionEngine/Orc/MemoryMapper.h
index 9f9ff06b2c2f6..8815777f35e07 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/MemoryMapper.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/MemoryMapper.h
@@ -157,6 +157,7 @@ class SharedMemoryMapper final : public MemoryMapper {
   struct Reservation {
     void *LocalAddr;
     size_t Size;
+    int SharedMemoryId;
   };
 
   ExecutorProcessControl &EPC;
diff --git a/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp b/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp
index 1989d8ca101e1..0b84541258ac1 100644
--- a/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp
@@ -220,10 +220,11 @@ void SharedMemoryMapper::reserve(size_t NumBytes,
                                  OnReservedFunction OnReserved) {
 #if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
 
+  int SharedMemoryId = -1;
   EPC.callSPSWrapperAsync<
       rt::SPSExecutorSharedMemoryMapperServiceReserveSignature>(
       SAs.Reserve,
-      [this, NumBytes, OnReserved = std::move(OnReserved)](
+      [this, NumBytes, OnReserved = std::move(OnReserved), SharedMemoryId](
           Error SerializationErr,
           Expected<std::pair<ExecutorAddr, std::string>> Result) mutable {
         if (SerializationErr) {
@@ -248,7 +249,7 @@ void SharedMemoryMapper::reserve(size_t NumBytes,
             SharedMemoryName.size());
         auto HashedName = BLAKE3::hash<sizeof(key_t)>(Data);
         key_t Key = *reinterpret_cast<key_t *>(HashedName.data());
-        int SharedMemoryId =
+        SharedMemoryId =
             shmget(Key, NumBytes, IPC_CREAT | __IPC_SHAREAS | 0700);
         if (SharedMemoryId < 0) {
           return OnReserved(errorCodeToError(
@@ -298,7 +299,8 @@ void SharedMemoryMapper::reserve(size_t NumBytes,
 #endif
         {
           std::lock_guard<std::mutex> Lock(Mutex);
-          Reservations.insert({RemoteAddr, {LocalAddr, NumBytes}});
+          Reservations.insert(
+              {RemoteAddr, {LocalAddr, NumBytes, SharedMemoryId}});
         }
 
         OnReserved(ExecutorAddrRange(RemoteAddr, NumBytes));
@@ -396,7 +398,8 @@ void SharedMemoryMapper::release(ArrayRef<ExecutorAddr> Bases,
 #if defined(LLVM_ON_UNIX)
 
 #if defined(__MVS__)
-      if (shmdt(Reservations[Base].LocalAddr) < 0)
+      if (shmdt(Reservations[Base].LocalAddr) < 0 ||
+          shmctl(Reservations[Base].SharedMemoryId, IPC_RMID, NULL) < 0)
         Err = joinErrors(std::move(Err), errorCodeToError(errnoAsErrorCode()));
 #else
       if (munmap(Reservations[Base].LocalAddr, Reservations[Base].Size) != 0)



More information about the llvm-commits mailing list