LinuxPageAllocator.cs 948 B

12345678910111213141516171819202122232425262728
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. namespace BepInEx.IL2CPP.Allocator
  6. {
  7. internal class LinuxPageAllocator : UnixPageAllocator
  8. {
  9. protected override IEnumerable<(IntPtr, IntPtr)> MapMemoryAreas()
  10. {
  11. // Each row of /proc/self/maps is as follows:
  12. // <start_address>-<end_address> <perms> <offset> <dev> <inode> <owner_name>
  13. // More info: https://stackoverflow.com/a/1401595
  14. using var procMap = new StreamReader(File.OpenRead("/proc/self/maps"));
  15. string line;
  16. while ((line = procMap.ReadLine()) != null)
  17. {
  18. int startIndex = line.IndexOf('-');
  19. int endIndex = line.IndexOf(' ');
  20. long startAddr = long.Parse(line.Substring(0, startIndex), NumberStyles.HexNumber);
  21. long endAddr = long.Parse(line.Substring(startIndex + 1, endIndex - startIndex - 1), NumberStyles.HexNumber);
  22. yield return (new IntPtr(startAddr), new IntPtr(endAddr));
  23. }
  24. }
  25. }
  26. }