WindowsPageAllocator.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.InteropServices;
  4. namespace BepInEx.IL2CPP.Allocator
  5. {
  6. /// <summary>
  7. /// Based on https://github.com/kubo/funchook
  8. /// </summary>
  9. internal class WindowsPageAllocator : PageAllocator
  10. {
  11. protected override IntPtr AllocateChunk(IntPtr hint)
  12. {
  13. while (true)
  14. {
  15. var mbi = new WinApi.MEMORY_BASIC_INFORMATION();
  16. if (WinApi.VirtualQuery(hint, ref mbi, Marshal.SizeOf<WinApi.MEMORY_BASIC_INFORMATION>()) == 0)
  17. throw new Win32Exception(Marshal.GetLastWin32Error());
  18. if (mbi.State == WinApi.PageState.MEM_FREE)
  19. {
  20. long nextAddress = RoundUp(mbi.BaseAddress.ToInt64(), ALLOCATION_UNIT);
  21. long d = nextAddress - mbi.BaseAddress.ToInt64();
  22. if (d >= 0 && mbi.RegionSize.ToInt64() - d >= ALLOCATION_UNIT)
  23. {
  24. hint = (IntPtr)nextAddress;
  25. break;
  26. }
  27. }
  28. hint = (IntPtr)(mbi.BaseAddress.ToInt64() + mbi.RegionSize.ToInt64());
  29. }
  30. var chunk = WinApi.VirtualAlloc(hint, (UIntPtr)ALLOCATION_UNIT, WinApi.AllocationType.MEM_RESERVE, WinApi.ProtectConstant.PAGE_NOACCESS);
  31. if (chunk == null)
  32. throw new Win32Exception(Marshal.GetLastWin32Error());
  33. var addr = WinApi.VirtualAlloc(chunk, (UIntPtr)PAGE_SIZE, WinApi.AllocationType.MEM_COMMIT, WinApi.ProtectConstant.PAGE_READWRITE);
  34. if (addr == IntPtr.Zero)
  35. {
  36. int error = Marshal.GetLastWin32Error();
  37. WinApi.VirtualFree(chunk, UIntPtr.Zero, WinApi.FreeType.MEM_RELEASE);
  38. throw new Win32Exception(error);
  39. }
  40. return chunk;
  41. }
  42. public override IntPtr Allocate(IntPtr hint)
  43. {
  44. var pageAddress = base.Allocate(hint);
  45. if (WinApi.VirtualAlloc(pageAddress, (UIntPtr)PAGE_SIZE, WinApi.AllocationType.MEM_COMMIT, WinApi.ProtectConstant.PAGE_READWRITE) == IntPtr.Zero)
  46. throw new Win32Exception(Marshal.GetLastWin32Error());
  47. return pageAddress;
  48. }
  49. private static class WinApi
  50. {
  51. [Flags]
  52. public enum AllocationType : uint
  53. {
  54. // ReSharper disable InconsistentNaming
  55. MEM_COMMIT = 0x00001000,
  56. MEM_RESERVE = 0x00002000
  57. // ReSharper restore InconsistentNaming
  58. }
  59. [Flags]
  60. public enum FreeType : uint
  61. {
  62. // ReSharper disable InconsistentNaming
  63. MEM_RELEASE = 0x00008000
  64. // ReSharper restore InconsistentNaming
  65. }
  66. public enum PageState : uint
  67. {
  68. // ReSharper disable InconsistentNaming
  69. MEM_FREE = 0x10000
  70. // ReSharper restore InconsistentNaming
  71. }
  72. [Flags]
  73. public enum ProtectConstant : uint
  74. {
  75. // ReSharper disable InconsistentNaming
  76. PAGE_NOACCESS = 0x01,
  77. PAGE_READWRITE = 0x04
  78. // ReSharper restore InconsistentNaming
  79. }
  80. [DllImport("kernel32", SetLastError = true)]
  81. public static extern int VirtualQuery(IntPtr lpAddress, ref MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);
  82. [DllImport("kernel32", SetLastError = true)]
  83. public static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, AllocationType flAllocationType, ProtectConstant flProtect);
  84. [DllImport("kernel32", SetLastError = true)]
  85. public static extern bool VirtualFree(IntPtr lpAddress, UIntPtr dwSize, FreeType dwFreeType);
  86. [StructLayout(LayoutKind.Sequential)]
  87. // ReSharper disable once InconsistentNaming
  88. public struct MEMORY_BASIC_INFORMATION
  89. {
  90. public readonly IntPtr BaseAddress;
  91. public readonly IntPtr AllocationBase;
  92. public readonly uint AllocationProtect;
  93. public readonly IntPtr RegionSize;
  94. public readonly PageState State;
  95. public readonly uint Protect;
  96. public readonly uint Type;
  97. }
  98. }
  99. }
  100. }