From f3c412b183455fdfe9aa98588d4dafec00875612 Mon Sep 17 00:00:00 2001 From: Ioannis Assiouras Date: Tue, 29 Nov 2022 10:16:54 +0000 Subject: [PATCH] SWDEV-369548 - Disallow opening an ipc mem handle in the originating process. Change-Id: Icce5e41a220566141bd7d92b7199d477f60bed8e [ROCm/clr commit: 30e740023ba794465b998db24209a8287a1c93f2] --- projects/clr/hipamd/src/hip_event_ipc.cpp | 4 ++-- projects/clr/hipamd/src/hip_internal.hpp | 7 ++----- projects/clr/hipamd/src/hip_memory.cpp | 5 +++++ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/projects/clr/hipamd/src/hip_event_ipc.cpp b/projects/clr/hipamd/src/hip_event_ipc.cpp index 9a01f27b55..7385566b8e 100644 --- a/projects/clr/hipamd/src/hip_event_ipc.cpp +++ b/projects/clr/hipamd/src/hip_event_ipc.cpp @@ -192,7 +192,7 @@ hipError_t IPCEvent::GetHandle(ihipIpcEventHandle_t* handle) { return hipErrorInvalidValue; } ipc_evt_.ipc_shmem_->owners_device_id = deviceId(); - ipc_evt_.ipc_shmem_->owners_process_id = getpid(); + ipc_evt_.ipc_shmem_->owners_process_id = amd::Os::getProcessId(); memset(handle->shmem_name, 0, HIP_IPC_HANDLE_SIZE); ipc_evt_.ipc_name_.copy(handle->shmem_name, std::string::npos); return hipSuccess; @@ -206,7 +206,7 @@ hipError_t IPCEvent::OpenHandle(ihipIpcEventHandle_t* handle) { return hipErrorInvalidValue; } - if (getpid() == ipc_evt_.ipc_shmem_->owners_process_id.load()) { + if (amd::Os::getProcessId() == ipc_evt_.ipc_shmem_->owners_process_id.load()) { // If this is in the same process, return error. return hipErrorInvalidContext; } diff --git a/projects/clr/hipamd/src/hip_internal.hpp b/projects/clr/hipamd/src/hip_internal.hpp index 6d009b2e50..7d9e9dbaaf 100644 --- a/projects/clr/hipamd/src/hip_internal.hpp +++ b/projects/clr/hipamd/src/hip_internal.hpp @@ -50,12 +50,13 @@ /*! IHIP IPC MEMORY Structure */ #define IHIP_IPC_MEM_HANDLE_SIZE 32 -#define IHIP_IPC_MEM_RESERVED_SIZE LP64_SWITCH(24,16) +#define IHIP_IPC_MEM_RESERVED_SIZE LP64_SWITCH(20,12) typedef struct ihipIpcMemHandle_st { char ipc_handle[IHIP_IPC_MEM_HANDLE_SIZE]; ///< ipc memory handle on ROCr size_t psize; size_t poffset; + int owners_process_id; char reserved[IHIP_IPC_MEM_RESERVED_SIZE]; } ihipIpcMemHandle_t; @@ -68,10 +69,6 @@ typedef struct ihipIpcEventHandle_st { char shmem_name[IHIP_IPC_EVENT_HANDLE_SIZE]; }ihipIpcEventHandle_t; -#ifdef _WIN32 - inline int getpid() { return _getpid(); } -#endif - const char* ihipGetErrorName(hipError_t hip_error); static amd::Monitor g_hipInitlock{"hipInit lock"}; diff --git a/projects/clr/hipamd/src/hip_memory.cpp b/projects/clr/hipamd/src/hip_memory.cpp index e5310428c3..00264a6589 100644 --- a/projects/clr/hipamd/src/hip_memory.cpp +++ b/projects/clr/hipamd/src/hip_memory.cpp @@ -2943,6 +2943,7 @@ hipError_t hipIpcGetMemHandle(hipIpcMemHandle_t* handle, void* dev_ptr) { LogPrintfError("IPC memory creation failed for memory: 0x%x", dev_ptr); HIP_RETURN(hipErrorInvalidValue); } + ihandle->owners_process_id = amd::Os::getProcessId(); HIP_RETURN(hipSuccess); } @@ -2966,6 +2967,10 @@ hipError_t hipIpcOpenMemHandle(void** dev_ptr, hipIpcMemHandle_t handle, unsigne HIP_RETURN(hipErrorInvalidValue); } + if (ihandle->owners_process_id == amd::Os::getProcessId()) { + HIP_RETURN(hipErrorInvalidContext); + } + if(!device->IpcAttach(&(ihandle->ipc_handle), ihandle->psize, ihandle->poffset, flags, dev_ptr)) { LogPrintfError("Cannot attach ipc_handle: with ipc_size: %u"