From d8d8b5a8472eb3f547832106b9ef82746e6c158d Mon Sep 17 00:00:00 2001 From: Alex Sanchez-Stern Date: Mon, 9 Sep 2024 18:20:07 -0700 Subject: [PATCH 1/2] Don't unlink shared memory on Unix Essentially adds the funcitonality from PR #6091 to Unix platforms. This allows third party tools like DME and JuniorsToolbox to access the shared memory region allowing them to manipulate game memory for various functionality. To do this, keep the shared memory object mapped to a name so that the other process can access it, instead of unlinking it from the name right after creation. This might leak files into the filesystem, like mentioned in https://github.com/dolphin-emu/dolphin/pull/9834#discussion_r803123299, but will only be one virtual file per dolphin so I think it's fine. --- Source/Core/Common/MemArenaUnix.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Core/Common/MemArenaUnix.cpp b/Source/Core/Common/MemArenaUnix.cpp index 83026f76b8..1afa1a913f 100644 --- a/Source/Core/Common/MemArenaUnix.cpp +++ b/Source/Core/Common/MemArenaUnix.cpp @@ -37,7 +37,6 @@ void MemArena::GrabSHMSegment(size_t size, std::string_view base_name) ERROR_LOG_FMT(MEMMAP, "shm_open failed: {}", strerror(errno)); return; } - shm_unlink(file_name.c_str()); if (ftruncate(m_shm_fd, size) < 0) ERROR_LOG_FMT(MEMMAP, "Failed to allocate low memory space"); } From 1252eba1cceb4820247ca8f953dae0833fc4dee8 Mon Sep 17 00:00:00 2001 From: Alex Sanchez-Stern Date: Sun, 22 Sep 2024 22:11:31 -0700 Subject: [PATCH 2/2] Unlink shm file on close --- Source/Core/Common/MemArena.h | 3 +++ Source/Core/Common/MemArenaUnix.cpp | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Source/Core/Common/MemArena.h b/Source/Core/Common/MemArena.h index 4f414afa27..709dd6b2e4 100644 --- a/Source/Core/Common/MemArena.h +++ b/Source/Core/Common/MemArena.h @@ -124,6 +124,9 @@ private: int m_shm_fd = 0; void* m_reserved_region = nullptr; std::size_t m_reserved_region_size = 0; +#ifndef ANDROID + std::string m_seg_name; +#endif #endif }; diff --git a/Source/Core/Common/MemArenaUnix.cpp b/Source/Core/Common/MemArenaUnix.cpp index 1afa1a913f..e7b6ae9c30 100644 --- a/Source/Core/Common/MemArenaUnix.cpp +++ b/Source/Core/Common/MemArenaUnix.cpp @@ -30,8 +30,8 @@ MemArena::~MemArena() = default; void MemArena::GrabSHMSegment(size_t size, std::string_view base_name) { - const std::string file_name = fmt::format("/{}.{}", base_name, getpid()); - m_shm_fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600); + m_seg_name = fmt::format("/{}.{}", base_name, getpid()); + m_shm_fd = shm_open(m_seg_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600); if (m_shm_fd == -1) { ERROR_LOG_FMT(MEMMAP, "shm_open failed: {}", strerror(errno)); @@ -43,6 +43,7 @@ void MemArena::GrabSHMSegment(size_t size, std::string_view base_name) void MemArena::ReleaseSHMSegment() { + shm_unlink(m_seg_name.c_str()); close(m_shm_fd); }