Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions llvm/lib/ExecutionEngine/Orc/MemoryMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#if defined(LLVM_ON_UNIX) && !defined(__ANDROID__)
#include <fcntl.h>
#include <sys/mman.h>
#if defined(__MVS__)
#include "llvm/Support/BLAKE3.h"
#include <sys/shm.h>
#endif
#include <unistd.h>
#elif defined(_WIN32)
#include <windows.h>
Expand Down Expand Up @@ -239,6 +243,24 @@ void SharedMemoryMapper::reserve(size_t NumBytes,

#if defined(LLVM_ON_UNIX)

#if defined(__MVS__)
ArrayRef<uint8_t> Data(
reinterpret_cast<const uint8_t *>(SharedMemoryName.c_str()),
SharedMemoryName.size());
auto HashedName = BLAKE3::hash<sizeof(key_t)>(Data);
key_t Key = *reinterpret_cast<key_t *>(HashedName.data());
int SharedMemoryId =
shmget(Key, NumBytes, IPC_CREAT | __IPC_SHAREAS | 0700);
if (SharedMemoryId < 0) {
return OnReserved(errorCodeToError(
std::error_code(errno, std::generic_category())));
}
LocalAddr = shmat(SharedMemoryId, nullptr, 0);
if (LocalAddr == reinterpret_cast<void *>(-1)) {
return OnReserved(errorCodeToError(
std::error_code(errno, std::generic_category())));
}
#else
int SharedMemoryFile = shm_open(SharedMemoryName.c_str(), O_RDWR, 0700);
if (SharedMemoryFile < 0) {
return OnReserved(errorCodeToError(errnoAsErrorCode()));
Expand All @@ -254,6 +276,7 @@ void SharedMemoryMapper::reserve(size_t NumBytes,
}

close(SharedMemoryFile);
#endif

#elif defined(_WIN32)

Expand Down Expand Up @@ -373,8 +396,13 @@ void SharedMemoryMapper::release(ArrayRef<ExecutorAddr> Bases,

#if defined(LLVM_ON_UNIX)

#if defined(__MVS__)
if (shmdt(Reservations[Base].LocalAddr) < 0)
Err = joinErrors(std::move(Err), errorCodeToError(errnoAsErrorCode()));
#else
if (munmap(Reservations[Base].LocalAddr, Reservations[Base].Size) != 0)
Err = joinErrors(std::move(Err), errorCodeToError(errnoAsErrorCode()));
#endif

#elif defined(_WIN32)

Expand Down Expand Up @@ -415,7 +443,11 @@ SharedMemoryMapper::~SharedMemoryMapper() {

#if defined(LLVM_ON_UNIX) && !defined(__ANDROID__)

#if defined(__MVS__)
shmdt(R.second.LocalAddr);
#else
munmap(R.second.LocalAddr, R.second.Size);
#endif

#elif defined(_WIN32)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#if defined(__MVS__)
#include "llvm/Support/BLAKE3.h"
#include <sys/shm.h>
#endif
#include <unistd.h>
#endif

Expand Down Expand Up @@ -59,6 +63,21 @@ ExecutorSharedMemoryMapperService::reserve(uint64_t Size) {
SharedMemoryName = SharedMemoryNameStream.str();
}

#if defined(__MVS__)
ArrayRef<uint8_t> Data(
reinterpret_cast<const uint8_t *>(SharedMemoryName.c_str()),
SharedMemoryName.size());
auto HashedName = BLAKE3::hash<sizeof(key_t)>(Data);
key_t Key = *reinterpret_cast<key_t *>(HashedName.data());
int SharedMemoryId =
shmget(Key, Size, IPC_CREAT | IPC_EXCL | __IPC_SHAREAS | 0700);
if (SharedMemoryId < 0)
return errorCodeToError(errnoAsErrorCode());

void *Addr = shmat(SharedMemoryId, nullptr, 0);
if (Addr == reinterpret_cast<void *>(-1))
return errorCodeToError(errnoAsErrorCode());
#else
int SharedMemoryFile =
shm_open(SharedMemoryName.c_str(), O_RDWR | O_CREAT | O_EXCL, 0700);
if (SharedMemoryFile < 0)
Expand All @@ -73,6 +92,7 @@ ExecutorSharedMemoryMapperService::reserve(uint64_t Size) {
return errorCodeToError(errnoAsErrorCode());

close(SharedMemoryFile);
#endif

#elif defined(_WIN32)

Expand Down Expand Up @@ -131,6 +151,9 @@ Expected<ExecutorAddr> ExecutorSharedMemoryMapperService::initialize(

#if defined(LLVM_ON_UNIX)

#if defined(__MVS__)
// TODO Is it possible to change the protection level?
#else
int NativeProt = 0;
if ((Segment.RAG.Prot & MemProt::Read) == MemProt::Read)
NativeProt |= PROT_READ;
Expand All @@ -141,6 +164,7 @@ Expected<ExecutorAddr> ExecutorSharedMemoryMapperService::initialize(

if (mprotect(Segment.Addr.toPtr<void *>(), Segment.Size, NativeProt))
return errorCodeToError(errnoAsErrorCode());
#endif

#elif defined(_WIN32)

Expand Down Expand Up @@ -239,8 +263,15 @@ Error ExecutorSharedMemoryMapperService::release(

#if defined(LLVM_ON_UNIX)

#if defined(__MVS__)
(void)Size;

if (shmdt(Base.toPtr<void *>()) < 0)
Err = joinErrors(std::move(Err), errorCodeToError(errnoAsErrorCode()));
#else
if (munmap(Base.toPtr<void *>(), Size) != 0)
Err = joinErrors(std::move(Err), errorCodeToError(errnoAsErrorCode()));
#endif

#elif defined(_WIN32)
(void)Size;
Expand Down