WindowsPageAllocator.cs 3.5 KB

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