MacOsPageAllocator.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using MonoMod.Utils;
  5. namespace BepInEx.IL2CPP.Allocator
  6. {
  7. internal class MacOsPageAllocator : UnixPageAllocator
  8. {
  9. protected override IEnumerable<(IntPtr, IntPtr)> MapMemoryAreas()
  10. {
  11. var size = IntPtr.Zero;
  12. var info = new LibSystem.vm_region_basic_info_64();
  13. var infoCount = (uint)(Marshal.SizeOf<LibSystem.vm_region_basic_info_64>() / sizeof(int));
  14. var objectName = 0u;
  15. var address = IntPtr.Zero;
  16. while (LibSystem.vm_region_64(LibSystem.TaskSelf, ref address, ref size, LibSystem.VM_REGION_BASIC_INFO_64, ref info, ref infoCount, ref objectName) == LibSystem.KERN_SUCCESS)
  17. {
  18. var start = new IntPtr(address.ToInt64());
  19. var end = new IntPtr(address.ToInt64() + size.ToInt64());
  20. address = end;
  21. yield return (start, end);
  22. }
  23. }
  24. private static class LibSystem
  25. {
  26. public const int VM_REGION_BASIC_INFO_64 = 9;
  27. public const int KERN_SUCCESS = 0;
  28. public static readonly IntPtr TaskSelf;
  29. [DynDllImport("libSystem")]
  30. public static vm_region_64Delegate vm_region_64;
  31. public delegate int vm_region_64Delegate(IntPtr target_task, ref IntPtr address, ref IntPtr size, int flavor, ref vm_region_basic_info_64 info, ref uint infoCnt, ref uint object_name);
  32. static LibSystem()
  33. {
  34. typeof(LibSystem).ResolveDynDllImports(new Dictionary<string, List<DynDllMapping>>
  35. {
  36. ["libSystem"] = new List<DynDllMapping>
  37. {
  38. "/usr/lib/libSystem.dylib" // OSX POSIX
  39. }
  40. });
  41. var libsystem = DynDll.OpenLibrary("/usr/lib/libSystem.dylib");
  42. TaskSelf = libsystem.GetFunction("mach_task_self_"); // This isn't a function but rather an exported symbol
  43. }
  44. [StructLayout(LayoutKind.Sequential)]
  45. public struct vm_region_basic_info_64
  46. {
  47. public readonly int protection;
  48. public readonly int max_protection;
  49. public readonly uint inheritance;
  50. [MarshalAs(UnmanagedType.I4)]
  51. public readonly bool shared;
  52. [MarshalAs(UnmanagedType.I4)]
  53. public readonly bool reserved;
  54. public readonly ulong offset;
  55. public readonly int behavior;
  56. public readonly ushort user_wired_count;
  57. }
  58. }
  59. }
  60. }