MacOsPageAllocator.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 static readonly IntPtr TaskSelf;
  27. public const int VM_REGION_BASIC_INFO_64 = 9;
  28. public const int KERN_SUCCESS = 0;
  29. [StructLayout(LayoutKind.Sequential)]
  30. public struct vm_region_basic_info_64
  31. {
  32. public int protection;
  33. public int max_protection;
  34. public uint inheritance;
  35. [MarshalAs(UnmanagedType.I4)]
  36. public bool shared;
  37. [MarshalAs(UnmanagedType.I4)]
  38. public bool reserved;
  39. public ulong offset;
  40. public int behavior;
  41. public ushort user_wired_count;
  42. }
  43. 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);
  44. [DynDllImport("libSystem")]
  45. public static vm_region_64Delegate vm_region_64;
  46. static LibSystem()
  47. {
  48. typeof(LibSystem).ResolveDynDllImports(new Dictionary<string, List<DynDllMapping>>
  49. {
  50. ["libSystem"] = new List<DynDllMapping>
  51. {
  52. "/usr/lib/libSystem.dylib" // OSX POSIX
  53. }
  54. });
  55. var libsystem = DynDll.OpenLibrary("/usr/lib/libSystem.dylib");
  56. TaskSelf = libsystem.GetFunction("mach_task_self_"); // This isn't a function but rather an exported symbol
  57. }
  58. }
  59. }
  60. }