diff --git a/LightlessSync/Utils/PtrGuard.cs b/LightlessSync/Utils/PtrGuard.cs index 2b08169..430a317 100644 --- a/LightlessSync/Utils/PtrGuard.cs +++ b/LightlessSync/Utils/PtrGuard.cs @@ -5,19 +5,35 @@ namespace LightlessSync.Utils { public static partial class PtrGuard { - private const ulong _minLikelyPtr = 0x0000_0001_0000_0000UL; - private const ulong _maxUserPtr = 0x0000_7FFF_FFFF_FFFFUL; private const ulong _aligmentPtr = 0x7UL; + private static readonly nuint _minAppAddr = (nuint)GetMinAppAddr(); + private static readonly nuint _maxAppAddr = (nuint)GetMaxAppAddr(); - public static bool LooksLikePtr(nint p) + private static nint GetMinAppAddr() { - if (p == 0) return false; - var u = (ulong)p; - if (u < _minLikelyPtr) return false; - if (u > _maxUserPtr) return false; - if ((u & _aligmentPtr) != 0) return false; - return true; + GetSystemInfo(out var si); + return si.lpMinimumApplicationAddress; } + + private static nint GetMaxAppAddr() + { + GetSystemInfo(out var si); + return si.lpMaximumApplicationAddress; + } + + public static bool LooksLikePtr(nint p) + { + if (p == 0) return false; + nuint u = (nuint)p; + + if (u < _minAppAddr) return false; + if (u > _maxAppAddr) return false; + if ((u & _aligmentPtr) != 0) return false; + if ((uint)u == 0x12345679u) return false; + + return true; + } + public static bool TryReadIntPtr(nint addr, out nint value) { value = 0; diff --git a/LightlessSync/Utils/PtrGuardMemory.cs b/LightlessSync/Utils/PtrGuardMemory.cs index ff29c4f..ccb5b79 100644 --- a/LightlessSync/Utils/PtrGuardMemory.cs +++ b/LightlessSync/Utils/PtrGuardMemory.cs @@ -32,5 +32,24 @@ namespace LightlessSync.Utils [DllImport("kernel32.dll")] internal static extern nint GetCurrentProcess(); + + [DllImport("kernel32.dll")] + internal static extern void GetSystemInfo(out SYSTEM_INFO lpSystemInfo); + + [StructLayout(LayoutKind.Sequential)] + internal struct SYSTEM_INFO + { + public ushort wProcessorArchitecture; + public ushort wReserved; + public uint dwPageSize; + public nint lpMinimumApplicationAddress; + public nint lpMaximumApplicationAddress; + public nint dwActiveProcessorMask; + public uint dwNumberOfProcessors; + public uint dwProcessorType; + public uint dwAllocationGranularity; + public ushort wProcessorLevel; + public ushort wProcessorRevision; + } } } \ No newline at end of file