1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-11-10 22:36:40 +00:00

Fix potential issue with partial unmap

We must also do the unmap operation with the RWLock, otherwise faults on the unmapped region will cause crashes and the whole thing becomes pointless
This commit is contained in:
gdk 2022-06-23 13:59:43 -03:00 committed by Mary-nyan
parent 96d4ad952c
commit 93dd6d525a

View file

@ -293,18 +293,14 @@ namespace Ryujinx.Memory.WindowsShared
if (IsMapped(overlap.Value)) if (IsMapped(overlap.Value))
{ {
ulong overlapStart = overlap.Start;
ulong overlapEnd = overlap.End;
ulong overlapValue = overlap.Value;
lock (_mappings) lock (_mappings)
{ {
_mappings.Remove(overlap); _mappings.Remove(overlap);
_mappings.Add(new RangeNode<ulong>(overlapStart, overlapEnd, ulong.MaxValue)); _mappings.Add(new RangeNode<ulong>(overlap.Start, overlap.End, ulong.MaxValue));
} }
bool overlapStartsBefore = overlapStart < startAddress; bool overlapStartsBefore = overlap.Start < startAddress;
bool overlapEndsAfter = overlapEnd > endAddress; bool overlapEndsAfter = overlap.End > endAddress;
if (overlapStartsBefore || overlapEndsAfter) if (overlapStartsBefore || overlapEndsAfter)
{ {
@ -321,25 +317,25 @@ namespace Ryujinx.Memory.WindowsShared
{ {
partialUnmapState.PartialUnmapsCount++; partialUnmapState.PartialUnmapsCount++;
if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlapStart, 2)) if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2))
{ {
throw new WindowsApiException("UnmapViewOfFile2"); throw new WindowsApiException("UnmapViewOfFile2");
} }
if (overlapStartsBefore) if (overlapStartsBefore)
{ {
ulong remapSize = startAddress - overlapStart; ulong remapSize = startAddress - overlap.Start;
MapViewInternal(sharedMemory, overlapValue, (IntPtr)overlapStart, (IntPtr)remapSize); MapViewInternal(sharedMemory, overlap.Value, (IntPtr)overlap.Start, (IntPtr)remapSize);
RestoreRangeProtection(overlapStart, remapSize); RestoreRangeProtection(overlap.Start, remapSize);
} }
if (overlapEndsAfter) if (overlapEndsAfter)
{ {
ulong overlappedSize = endAddress - overlapStart; ulong overlappedSize = endAddress - overlap.Start;
ulong remapBackingOffset = overlapValue + overlappedSize; ulong remapBackingOffset = overlap.Value + overlappedSize;
ulong remapAddress = overlapStart + overlappedSize; ulong remapAddress = overlap.Start + overlappedSize;
ulong remapSize = overlapEnd - endAddress; ulong remapSize = overlap.End - endAddress;
MapViewInternal(sharedMemory, remapBackingOffset, (IntPtr)remapAddress, (IntPtr)remapSize); MapViewInternal(sharedMemory, remapBackingOffset, (IntPtr)remapAddress, (IntPtr)remapSize);
RestoreRangeProtection(remapAddress, remapSize); RestoreRangeProtection(remapAddress, remapSize);
@ -350,7 +346,7 @@ namespace Ryujinx.Memory.WindowsShared
partialUnmapLock.DowngradeFromWriterLock(); partialUnmapLock.DowngradeFromWriterLock();
} }
} }
else if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlapStart, 2)) else if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2))
{ {
throw new WindowsApiException("UnmapViewOfFile2"); throw new WindowsApiException("UnmapViewOfFile2");
} }