From 14ce9e15672d03cb6fc067316f90d81471398ebc Mon Sep 17 00:00:00 2001
From: riperiperi <rhy3756547@hotmail.com>
Date: Sat, 30 Jul 2022 00:16:29 +0200
Subject: Move partial unmap handler to the native signal handler (#3437)

* Initial commit with a lot of testing stuff.

* Partial Unmap Cleanup Part 1

* Fix some minor issues, hopefully windows tests.

* Disable partial unmap tests on macos for now

Weird issue.

* Goodbye magic number

* Add COMPlus_EnableAlternateStackCheck for tests

`COMPlus_EnableAlternateStackCheck` is needed for NullReferenceException handling to work on linux after registering the signal handler, due to how dotnet registers its own signal handler.

* Address some feedback

* Force retry when memory is mapped in memory tracking

This case existed before, but returning `false` no longer retries, so it would crash immediately after unprotecting the memory... Now, we return `true` to deliberately retry.

This case existed before (was just broken by this change) and I don't really want to look into fixing the issue right now. Technically, this means that on guest code partial unmaps will retry _due to this_ rather than hitting the handler. I don't expect this to cause any issues.

This should fix random crashes in Xenoblade Chronicles 2.

* Use IsRangeMapped

* Suppress MockMemoryManager.UnmapEvent warning

This event is not signalled by the mock memory manager.

* Remove 4kb mapping
---
 Ryujinx.Memory/Tracking/MemoryTracking.cs | 30 ++++--------------------------
 1 file changed, 4 insertions(+), 26 deletions(-)

(limited to 'Ryujinx.Memory/Tracking/MemoryTracking.cs')

diff --git a/Ryujinx.Memory/Tracking/MemoryTracking.cs b/Ryujinx.Memory/Tracking/MemoryTracking.cs
index c5abb576..ec75e3d0 100644
--- a/Ryujinx.Memory/Tracking/MemoryTracking.cs
+++ b/Ryujinx.Memory/Tracking/MemoryTracking.cs
@@ -188,30 +188,6 @@ namespace Ryujinx.Memory.Tracking
             return VirtualMemoryEvent(address, 1, write);
         }
 
-        /// <summary>
-        /// Signal that a virtual memory event happened at the given location.
-        /// This is similar VirtualMemoryEvent, but on Windows, it might also return true after a partial unmap.
-        /// This should only be called from the exception handler.
-        /// </summary>
-        /// <param name="address">Virtual address accessed</param>
-        /// <param name="size">Size of the region affected in bytes</param>
-        /// <param name="write">Whether the region was written to or read</param>
-        /// <param name="precise">True if the access is precise, false otherwise</param>
-        /// <returns>True if the event triggered any tracking regions, false otherwise</returns>
-        public bool VirtualMemoryEventEh(ulong address, ulong size, bool write, bool precise = false)
-        {
-            // Windows has a limitation, it can't do partial unmaps.
-            // For this reason, we need to unmap the whole range and then remap the sub-ranges.
-            // When this happens, we might have caused a undesirable access violation from the time that the range was unmapped.
-            // In this case, try again as the memory might be mapped now.
-            if (OperatingSystem.IsWindows() && MemoryManagementWindows.RetryFromAccessViolation())
-            {
-                return true;
-            }
-
-            return VirtualMemoryEvent(address, size, write, precise);
-        }
-
         /// <summary>
         /// Signal that a virtual memory event happened at the given location.
         /// This can be flagged as a precise event, which will avoid reprotection and call special handlers if possible.
@@ -237,10 +213,12 @@ namespace Ryujinx.Memory.Tracking
 
                 if (count == 0 && !precise)
                 {
-                    if (_memoryManager.IsMapped(address))
+                    if (_memoryManager.IsRangeMapped(address, size))
                     {
+                        // TODO: There is currently the possibility that a page can be protected after its virtual region is removed.
+                        // This code handles that case when it happens, but it would be better to find out how this happens.
                         _memoryManager.TrackingReprotect(address & ~(ulong)(_pageSize - 1), (ulong)_pageSize, MemoryPermission.ReadAndWrite);
-                        return false; // We can't handle this - it's probably a real invalid access.
+                        return true; // This memory _should_ be mapped, so we need to try again.
                     }
                     else
                     {
-- 
cgit v1.2.3-70-g09d2