浏览代码

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
gdkchan 3 年之前
父节点
当前提交
7d26e4ac7b
共有 2 个文件被更改,包括 27 次插入1 次删除
  1. 26 0
      Ryujinx.Memory.Tests/Tests.cs
  2. 1 1
      Ryujinx.Memory/MemoryManagementUnix.cs

+ 26 - 0
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<int>(0));
+
+                toAlias.UnmapView(backing, offset, pageSize);
+            }
+        }
     }
 }

+ 1 - 1
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);
         }
     }
 }