From 356e480bf55d9a1497fcf54e9395d9b8ab72b815 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Wed, 14 Sep 2022 12:46:37 -0300 Subject: [PATCH] Fix partial unmap reprotection on Windows (#3702) --- .../WindowsShared/PlaceholderManager.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Ryujinx.Memory/WindowsShared/PlaceholderManager.cs b/Ryujinx.Memory/WindowsShared/PlaceholderManager.cs index a17fea5e3b..3022b66165 100644 --- a/Ryujinx.Memory/WindowsShared/PlaceholderManager.cs +++ b/Ryujinx.Memory/WindowsShared/PlaceholderManager.cs @@ -73,7 +73,7 @@ namespace Ryujinx.Memory.WindowsShared lock (_protections) { - _protections.Add(new RangeNode(address, size, MemoryPermission.None)); + _protections.Add(new RangeNode(address, address + size, MemoryPermission.None)); } } @@ -132,7 +132,7 @@ namespace Ryujinx.Memory.WindowsShared try { UnmapViewInternal(sharedMemory, location, size, owner, updateProtection: false); - MapViewInternal(sharedMemory, srcOffset, location, size); + MapViewInternal(sharedMemory, srcOffset, location, size, updateProtection: true); } finally { @@ -147,8 +147,9 @@ namespace Ryujinx.Memory.WindowsShared /// Offset in the shared memory to map /// Address to map the view into /// Size of the view in bytes + /// Indicates if the memory protections should be updated after the map /// Thrown when the Windows API returns an error mapping the memory - private void MapViewInternal(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size) + private void MapViewInternal(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size, bool updateProtection) { SplitForMap((ulong)location, (ulong)size, srcOffset); @@ -168,7 +169,10 @@ namespace Ryujinx.Memory.WindowsShared throw new WindowsApiException("MapViewOfFile3"); } - UpdateProtection((ulong)location, (ulong)size, MemoryPermission.ReadAndWrite); + if (updateProtection) + { + UpdateProtection((ulong)location, (ulong)size, MemoryPermission.ReadAndWrite); + } } /// @@ -330,7 +334,7 @@ namespace Ryujinx.Memory.WindowsShared { ulong remapSize = startAddress - overlap.Start; - MapViewInternal(sharedMemory, overlap.Value, (IntPtr)overlap.Start, (IntPtr)remapSize); + MapViewInternal(sharedMemory, overlap.Value, (IntPtr)overlap.Start, (IntPtr)remapSize, updateProtection: false); RestoreRangeProtection(overlap.Start, remapSize); } @@ -341,7 +345,7 @@ namespace Ryujinx.Memory.WindowsShared ulong remapAddress = overlap.Start + overlappedSize; ulong remapSize = overlap.End - endAddress; - MapViewInternal(sharedMemory, remapBackingOffset, (IntPtr)remapAddress, (IntPtr)remapSize); + MapViewInternal(sharedMemory, remapBackingOffset, (IntPtr)remapAddress, (IntPtr)remapSize, updateProtection: false); RestoreRangeProtection(remapAddress, remapSize); } } @@ -606,7 +610,7 @@ namespace Ryujinx.Memory.WindowsShared _protections.Remove(protection); - if (protection.Value == permission) + if (protPermission == permission) { if (startAddress > protAddress) {