From 7d26e4ac7b61c4b6a860f6fd9aabdc8e653bc2d7 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Tue, 18 Oct 2022 22:02:45 -0300 Subject: [PATCH] Fix mapping leaks caused by UnmapView not working on Linux (#3650) * Add test for UnmapView mapping leaks * Throw when UnmapView fails on Linux * Fix UnmapView * Remove throw --- Ryujinx.Memory.Tests/Tests.cs | 26 ++++++++++++++++++++++++++ Ryujinx.Memory/MemoryManagementUnix.cs | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Ryujinx.Memory.Tests/Tests.cs b/Ryujinx.Memory.Tests/Tests.cs index 45d00e51d..c5a7842ec 100644 --- a/Ryujinx.Memory.Tests/Tests.cs +++ b/Ryujinx.Memory.Tests/Tests.cs @@ -92,5 +92,31 @@ namespace Ryujinx.Memory.Tests } } } + + [Test] + public void Test_AliasMapLeak() + { + if (OperatingSystem.IsMacOS()) + { + // Memory aliasing tests fail on CI at the moment. + return; + } + + ulong pageSize = 4096; + ulong size = 100000 * pageSize; // The mappings limit on Linux is usually around 65K, so let's make sure we are above that. + + using MemoryBlock backing = new MemoryBlock(pageSize, MemoryAllocationFlags.Mirrorable); + using MemoryBlock toAlias = new MemoryBlock(size, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible); + + for (ulong offset = 0; offset < size; offset += pageSize) + { + toAlias.MapView(backing, 0, offset, pageSize); + + toAlias.Write(offset, 0xbadc0de); + Assert.AreEqual(0xbadc0de, backing.Read(0)); + + toAlias.UnmapView(backing, offset, pageSize); + } + } } } \ No newline at end of file diff --git a/Ryujinx.Memory/MemoryManagementUnix.cs b/Ryujinx.Memory/MemoryManagementUnix.cs index db7539f03..df3fcea91 100644 --- a/Ryujinx.Memory/MemoryManagementUnix.cs +++ b/Ryujinx.Memory/MemoryManagementUnix.cs @@ -177,7 +177,7 @@ namespace Ryujinx.Memory public static void UnmapView(IntPtr location, ulong size) { - mmap(location, size, MmapProts.PROT_NONE, MmapFlags.MAP_FIXED, -1, 0); + mmap(location, size, MmapProts.PROT_NONE, MmapFlags.MAP_FIXED | MmapFlags.MAP_PRIVATE | MmapFlags.MAP_ANONYMOUS | MmapFlags.MAP_NORESERVE, -1, 0); } } } \ No newline at end of file