2019-02-18 18:52:06 -05:00
|
|
|
using System;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace ChocolArm64.Memory
|
|
|
|
{
|
2019-02-24 02:24:35 -05:00
|
|
|
public static class MemoryManagement
|
2019-02-18 18:52:06 -05:00
|
|
|
{
|
|
|
|
public static bool HasWriteWatchSupport => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
|
|
|
|
|
|
|
public static IntPtr Allocate(ulong size)
|
|
|
|
{
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
{
|
|
|
|
IntPtr sizeNint = new IntPtr((long)size);
|
|
|
|
|
2019-02-24 02:24:35 -05:00
|
|
|
return MemoryManagementWindows.Allocate(sizeNint);
|
2019-02-18 18:52:06 -05:00
|
|
|
}
|
|
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
|
|
|
|
RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
|
|
{
|
2019-02-24 02:24:35 -05:00
|
|
|
return MemoryManagementUnix.Allocate(size);
|
2019-02-18 18:52:06 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IntPtr AllocateWriteTracked(ulong size)
|
|
|
|
{
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
{
|
|
|
|
IntPtr sizeNint = new IntPtr((long)size);
|
|
|
|
|
2019-02-24 02:24:35 -05:00
|
|
|
return MemoryManagementWindows.AllocateWriteTracked(sizeNint);
|
2019-02-18 18:52:06 -05:00
|
|
|
}
|
|
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
|
|
|
|
RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
|
|
{
|
2019-02-24 02:24:35 -05:00
|
|
|
return MemoryManagementUnix.Allocate(size);
|
2019-02-18 18:52:06 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Reprotect(IntPtr address, ulong size, MemoryProtection permission)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
{
|
|
|
|
IntPtr sizeNint = new IntPtr((long)size);
|
|
|
|
|
2019-02-24 02:24:35 -05:00
|
|
|
result = MemoryManagementWindows.Reprotect(address, sizeNint, permission);
|
2019-02-18 18:52:06 -05:00
|
|
|
}
|
|
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
|
|
|
|
RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
|
|
{
|
2019-02-24 02:24:35 -05:00
|
|
|
result = MemoryManagementUnix.Reprotect(address, size, permission);
|
2019-02-18 18:52:06 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
throw new MemoryProtectionException(permission);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool Free(IntPtr address)
|
|
|
|
{
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
{
|
2019-02-24 02:24:35 -05:00
|
|
|
return MemoryManagementWindows.Free(address);
|
2019-02-18 18:52:06 -05:00
|
|
|
}
|
|
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
|
|
|
|
RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
|
|
{
|
2019-02-24 02:24:35 -05:00
|
|
|
return MemoryManagementUnix.Free(address);
|
2019-02-18 18:52:06 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static bool GetModifiedPages(
|
|
|
|
IntPtr address,
|
|
|
|
IntPtr size,
|
|
|
|
IntPtr[] addresses,
|
|
|
|
out ulong count)
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// This is only supported on windows, but returning
|
|
|
|
// false (failed) is also valid for platforms without
|
|
|
|
// write tracking support on the OS.
|
2019-02-18 18:52:06 -05:00
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
{
|
2019-02-24 02:24:35 -05:00
|
|
|
return MemoryManagementWindows.GetModifiedPages(address, size, addresses, out count);
|
2019-02-18 18:52:06 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|