From b59b8ac943007b12e9d92464bd811488b483849a Mon Sep 17 00:00:00 2001
From: LotP1 <68976644+LotP1@users.noreply.github.com>
Date: Fri, 29 Nov 2024 23:32:55 +0100
Subject: [PATCH] JIT Sparse Function Table random crash fix (#319)

A couple of games have random crashing with the JIT Sparse Ftable changes, and it seems to have been caused by an insufficient int size returned by `AddressTableLevel#GetValue(ulong address)`.
It was 32 bits (Int32), but the GiantBlock (which is the current address table impl) uses potentially 36 bits for the first level.
---
 src/ARMeilleure/Common/AddressTableLevel.cs | 4 ++--
 src/ARMeilleure/Translation/PTC/Ptc.cs      | 2 +-
 src/Ryujinx.Cpu/AddressTable.cs             | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/ARMeilleure/Common/AddressTableLevel.cs b/src/ARMeilleure/Common/AddressTableLevel.cs
index 6107726ee..af3b9b99f 100644
--- a/src/ARMeilleure/Common/AddressTableLevel.cs
+++ b/src/ARMeilleure/Common/AddressTableLevel.cs
@@ -36,9 +36,9 @@ namespace ARMeilleure.Common
         /// </summary>
         /// <param name="address">Guest address</param>
         /// <returns>Value of the <see cref="AddressTableLevel"/> from the specified guest <paramref name="address"/></returns>
-        public int GetValue(ulong address)
+        public long GetValue(ulong address)
         {
-            return (int)((address & Mask) >> Index);
+            return (long)((address & Mask) >> Index);
         }
     }
 }
diff --git a/src/ARMeilleure/Translation/PTC/Ptc.cs b/src/ARMeilleure/Translation/PTC/Ptc.cs
index c722ce6be..841e5fefa 100644
--- a/src/ARMeilleure/Translation/PTC/Ptc.cs
+++ b/src/ARMeilleure/Translation/PTC/Ptc.cs
@@ -30,7 +30,7 @@ namespace ARMeilleure.Translation.PTC
         private const string OuterHeaderMagicString = "PTCohd\0\0";
         private const string InnerHeaderMagicString = "PTCihd\0\0";
 
-        private const uint InternalVersion = 6992; //! To be incremented manually for each change to the ARMeilleure project.
+        private const uint InternalVersion = 6997; //! To be incremented manually for each change to the ARMeilleure project.
 
         private const string ActualDir = "0";
         private const string BackupDir = "1";
diff --git a/src/Ryujinx.Cpu/AddressTable.cs b/src/Ryujinx.Cpu/AddressTable.cs
index d87b12ab0..038a2009c 100644
--- a/src/Ryujinx.Cpu/AddressTable.cs
+++ b/src/Ryujinx.Cpu/AddressTable.cs
@@ -238,7 +238,7 @@ namespace ARMeilleure.Common
             {
                 TEntry* page = GetPage(address);
 
-                int index = Levels[^1].GetValue(address);
+                long index = Levels[^1].GetValue(address);
 
                 EnsureMapped((IntPtr)(page + index));