From 811303ea54f3e423e8aaf1130df14b5b86fdca3d Mon Sep 17 00:00:00 2001 From: Steveice10 <1269164+Steveice10@users.noreply.github.com> Date: Sat, 30 Dec 2023 15:36:12 -0800 Subject: [PATCH] kernel: Fix freeing shared memory with wrong region. (#7301) --- src/core/hle/kernel/shared_memory.cpp | 7 ++++--- src/core/hle/kernel/shared_memory.h | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/core/hle/kernel/shared_memory.cpp b/src/core/hle/kernel/shared_memory.cpp index 46e216551..30526923c 100644 --- a/src/core/hle/kernel/shared_memory.cpp +++ b/src/core/hle/kernel/shared_memory.cpp @@ -18,8 +18,7 @@ SharedMemory::SharedMemory(KernelSystem& kernel) : Object(kernel), kernel(kernel SharedMemory::~SharedMemory() { for (const auto& interval : holding_memory) { - kernel.GetMemoryRegion(MemoryRegion::SYSTEM) - ->Free(interval.lower(), interval.upper() - interval.lower()); + memory_region->Free(interval.lower(), interval.upper() - interval.lower()); } auto process = owner_process.lock(); @@ -39,17 +38,18 @@ ResultVal> KernelSystem::CreateSharedMemory( std::shared_ptr owner_process, u32 size, MemoryPermission permissions, MemoryPermission other_permissions, VAddr address, MemoryRegion region, std::string name) { + auto memory_region = GetMemoryRegion(region); auto shared_memory = std::make_shared(*this); shared_memory->owner_process = owner_process; shared_memory->name = std::move(name); shared_memory->size = size; + shared_memory->memory_region = memory_region; shared_memory->permissions = permissions; shared_memory->other_permissions = other_permissions; if (address == 0) { // We need to allocate a block from the Linear Heap ourselves. // We'll manually allocate some memory from the linear heap in the specified region. - auto memory_region = GetMemoryRegion(region); auto offset = memory_region->LinearAllocate(size); ASSERT_MSG(offset, "Not enough space in region to allocate shared memory!"); @@ -93,6 +93,7 @@ std::shared_ptr KernelSystem::CreateSharedMemoryForApplet( shared_memory->owner_process = std::weak_ptr(); shared_memory->name = std::move(name); shared_memory->size = size; + shared_memory->memory_region = memory_region; shared_memory->permissions = permissions; shared_memory->other_permissions = other_permissions; for (const auto& interval : backing_blocks) { diff --git a/src/core/hle/kernel/shared_memory.h b/src/core/hle/kernel/shared_memory.h index d5cec7678..b308399d1 100644 --- a/src/core/hle/kernel/shared_memory.h +++ b/src/core/hle/kernel/shared_memory.h @@ -94,6 +94,8 @@ private: std::vector> backing_blocks; /// Size of the memory block. Page-aligned. u32 size = 0; + /// Region of memory this block exists in. + std::shared_ptr memory_region = nullptr; /// Permission restrictions applied to the process which created the block. MemoryPermission permissions{}; /// Permission restrictions applied to other processes mapping the block. @@ -116,6 +118,7 @@ private: ar& linear_heap_phys_offset; ar& backing_blocks; ar& size; + ar& memory_region; ar& permissions; ar& other_permissions; ar& owner_process;