Updated ptr guard so windows have max on 4gb again.

This commit is contained in:
cake
2026-01-22 02:03:39 +01:00
parent 995e11371a
commit d4640328aa
4 changed files with 33 additions and 34 deletions

View File

@@ -127,7 +127,7 @@ public class PlayerDataFactory
{ {
nint basePtr = playerPointer; nint basePtr = playerPointer;
if (!PtrGuard.LooksLikePtr(basePtr)) if (!PtrGuard.LooksLikePtr(basePtr, _dalamudUtil.IsWine))
return true; return true;
nint drawObjAddr = basePtr + _drawObjectOffset; nint drawObjAddr = basePtr + _drawObjectOffset;
@@ -135,10 +135,10 @@ public class PlayerDataFactory
if (!PtrGuard.IsReadable(drawObjAddr, (nuint)IntPtr.Size)) if (!PtrGuard.IsReadable(drawObjAddr, (nuint)IntPtr.Size))
return true; return true;
if (!PtrGuard.TryReadIntPtr(drawObjAddr, out var drawObj)) if (!PtrGuard.TryReadIntPtr(drawObjAddr, _dalamudUtil.IsWine, out var drawObj))
return true; return true;
if (drawObj != 0 && !PtrGuard.LooksLikePtr(drawObj)) if (drawObj != 0 && !PtrGuard.LooksLikePtr(drawObj, _dalamudUtil.IsWine))
return true; return true;
return drawObj == 0; return drawObj == 0;

View File

@@ -180,16 +180,14 @@ public sealed class GameObjectHandler : DisposableMediatorSubscriberBase, IHighP
var nextAddr = _getAddress(); var nextAddr = _getAddress();
if (nextAddr != IntPtr.Zero && !PtrGuard.LooksLikePtr(nextAddr)) if (nextAddr != IntPtr.Zero && !PtrGuard.LooksLikePtr(nextAddr, _dalamudUtil.IsWine))
{ {
Logger.LogWarning("[{this}] _getAddress returned non-pointer: 0x{addr:X}", this, (ulong)nextAddr);
nextAddr = IntPtr.Zero; nextAddr = IntPtr.Zero;
} }
if (nextAddr != IntPtr.Zero && if (nextAddr != IntPtr.Zero &&
!PtrGuard.IsReadable(nextAddr, (nuint)sizeof(FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject))) !PtrGuard.IsReadable(nextAddr, (nuint)sizeof(FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject)))
{ {
Logger.LogWarning("[{this}] Address not readable: 0x{addr:X}", this, (ulong)nextAddr);
nextAddr = IntPtr.Zero; nextAddr = IntPtr.Zero;
} }
@@ -201,7 +199,7 @@ public sealed class GameObjectHandler : DisposableMediatorSubscriberBase, IHighP
var draw = (nint)gameObject->DrawObject; var draw = (nint)gameObject->DrawObject;
if (!PtrGuard.LooksLikePtr(draw) || !PtrGuard.IsReadable(draw, (nuint)sizeof(DrawObject))) if (!PtrGuard.LooksLikePtr(draw, _dalamudUtil.IsWine) || !PtrGuard.IsReadable(draw, (nuint)sizeof(DrawObject)))
draw = 0; draw = 0;
DrawObjectAddress = draw; DrawObjectAddress = draw;
@@ -363,7 +361,7 @@ public sealed class GameObjectHandler : DisposableMediatorSubscriberBase, IHighP
private unsafe bool CompareAndUpdateMainHand(Weapon* weapon) private unsafe bool CompareAndUpdateMainHand(Weapon* weapon)
{ {
var p = (nint)weapon; var p = (nint)weapon;
if (!PtrGuard.LooksLikePtr(p) || !PtrGuard.IsReadable(p, (nuint)sizeof(Weapon))) if (!PtrGuard.LooksLikePtr(p, _dalamudUtil.IsWine) || !PtrGuard.IsReadable(p, (nuint)sizeof(Weapon)))
return false; return false;
bool hasChanges = false; bool hasChanges = false;
@@ -379,7 +377,7 @@ public sealed class GameObjectHandler : DisposableMediatorSubscriberBase, IHighP
private unsafe bool CompareAndUpdateOffHand(Weapon* weapon) private unsafe bool CompareAndUpdateOffHand(Weapon* weapon)
{ {
var p = (nint)weapon; var p = (nint)weapon;
if (!PtrGuard.LooksLikePtr(p) || !PtrGuard.IsReadable(p, (nuint)sizeof(Weapon))) if (!PtrGuard.LooksLikePtr(p, _dalamudUtil.IsWine) || !PtrGuard.IsReadable(p, (nuint)sizeof(Weapon)))
return false; return false;
bool hasChanges = false; bool hasChanges = false;

View File

@@ -5,40 +5,41 @@ namespace LightlessSync.Utils
{ {
public static partial class PtrGuard public static partial class PtrGuard
{ {
private const ulong _aligmentPtr = 0x7UL; private static readonly nuint _hardMinWindows =
private static readonly nuint _minAppAddr = (nuint)GetMinAppAddr(); (nuint)(IntPtr.Size == 8 ? 0x0000000100000000UL : 0x0000000000010000UL);
private static readonly nuint _maxAppAddr = (nuint)GetMaxAppAddr(); private static readonly nuint _hardMaxWindows =
(nuint)(IntPtr.Size == 8 ? 0x00007FFFFFFFFFFFUL : 0x7FFFFFFFUL);
private const nuint _alignmentPtr = 0x7;
private static nint GetMinAppAddr() private static readonly (nuint min, nuint max) _sysRange = GetSysRange();
private static (nuint min, nuint max) GetSysRange()
{ {
GetSystemInfo(out var si); GetSystemInfo(out var si);
return si.lpMinimumApplicationAddress; return ((nuint)si.lpMinimumApplicationAddress, (nuint)si.lpMaximumApplicationAddress);
} }
private static nint GetMaxAppAddr() private static nuint GetMinAppAddr(bool isWine) => isWine ? _sysRange.min : _hardMinWindows;
{ private static nuint GetMaxAppAddr(bool isWine) => isWine ? _sysRange.max : _hardMaxWindows;
GetSystemInfo(out var si);
return si.lpMaximumApplicationAddress;
}
public static bool LooksLikePtr(nint p) public static bool LooksLikePtr(nint p, bool isWine = false)
{ {
if (p == 0) return false; if (p == 0) return false;
nuint u = (nuint)p; nuint u = (nuint)p;
if (u < _minAppAddr) return false; if (u < GetMinAppAddr(isWine)) return false;
if (u > _maxAppAddr) return false; if (u > GetMaxAppAddr(isWine)) return false;
if ((u & _aligmentPtr) != 0) return false; if ((u & _alignmentPtr) != 0) return false;
if ((uint)u == 0x12345679u) return false; if ((uint)u == 0x12345679u) return false;
return true; return true;
} }
public static bool TryReadIntPtr(nint addr, out nint value) public static bool TryReadIntPtr(nint addr, bool isWine, out nint value)
{ {
value = 0; value = 0;
if (!LooksLikePtr(addr)) if (!LooksLikePtr(addr, isWine))
return false; return false;
return ReadProcessMemory(GetCurrentProcess(), addr, out value, (nuint)IntPtr.Size, out nuint bytesRead) return ReadProcessMemory(GetCurrentProcess(), addr, out value, (nuint)IntPtr.Size, out nuint bytesRead)

View File

@@ -34,10 +34,10 @@ namespace LightlessSync.Utils
internal static extern nint GetCurrentProcess(); internal static extern nint GetCurrentProcess();
[DllImport("kernel32.dll")] [DllImport("kernel32.dll")]
internal static extern void GetSystemInfo(out SYSTEM_INFO lpSystemInfo); internal static extern void GetSystemInfo(out SystemInfo lpSystemInfo);
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
internal struct SYSTEM_INFO internal struct SystemInfo
{ {
public ushort wProcessorArchitecture; public ushort wProcessorArchitecture;
public ushort wReserved; public ushort wReserved;