HookUtils_OSX.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #if (UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX)
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using UnityEngine;
  8. namespace MonoHook
  9. {
  10. public static unsafe class HookUtils
  11. {
  12. static bool jit_write_protect_supported;
  13. private static readonly long _Pagesize;
  14. static HookUtils()
  15. {
  16. try
  17. {
  18. jit_write_protect_supported = pthread_jit_write_protect_supported_np() != 0;
  19. }
  20. catch { }
  21. PropertyInfo p_SystemPageSize = typeof(Environment).GetProperty("SystemPageSize");
  22. if (p_SystemPageSize == null)
  23. throw new NotSupportedException("Unsupported runtime");
  24. _Pagesize = (int)p_SystemPageSize.GetValue(null, new object[0]);
  25. }
  26. public static void MemCpy(void* pDst, void* pSrc, int len)
  27. {
  28. byte* pDst_ = (byte*)pDst;
  29. byte* pSrc_ = (byte*)pSrc;
  30. for (int i = 0; i < len; i++)
  31. *pDst_++ = *pSrc_++;
  32. }
  33. public static void MemCpy_Jit(void* pDst, byte[] src)
  34. {
  35. if (!jit_write_protect_supported)
  36. {
  37. fixed(void * pSrc = &src[0])
  38. {
  39. MemCpy(pDst, pSrc, src.Length);
  40. }
  41. return;
  42. }
  43. fixed(void * p = &src[0])
  44. {
  45. memcpy_jit(new IntPtr(pDst), new IntPtr(p), src.Length);
  46. }
  47. }
  48. /// <summary>
  49. /// set flags of address to `read write execute`
  50. /// </summary>
  51. public static void SetAddrFlagsToRWX(IntPtr ptr, int size) { }
  52. public static void FlushICache(void* code, int size) { }
  53. public static KeyValuePair<long, long> GetPageAlignedAddr(long code, int size)
  54. {
  55. long pagesize = _Pagesize;
  56. long startPage = (code) & ~(pagesize - 1);
  57. long endPage = (code + size + pagesize - 1) & ~(pagesize - 1);
  58. return new KeyValuePair<long, long>(startPage, endPage);
  59. }
  60. const int PRINT_SPLIT = 4;
  61. const int PRINT_COL_SIZE = PRINT_SPLIT * 4;
  62. public static string HexToString(void* ptr, int size, int offset = 0)
  63. {
  64. Func<IntPtr, string> formatAddr = (IntPtr addr__) => IntPtr.Size == 4 ? $"0x{(uint)addr__:x}" : $"0x{(ulong)addr__:x}";
  65. byte* addr = (byte*)ptr;
  66. StringBuilder sb = new StringBuilder(1024);
  67. sb.AppendLine($"addr:{formatAddr(new IntPtr(addr))}");
  68. addr += offset;
  69. size += Math.Abs(offset);
  70. int count = 0;
  71. while (true)
  72. {
  73. sb.Append($"\r\n{formatAddr(new IntPtr(addr + count))}: ");
  74. for (int i = 1; i < PRINT_COL_SIZE + 1; i++)
  75. {
  76. if (count >= size)
  77. goto END;
  78. sb.Append($"{*(addr + count):x2}");
  79. if (i % PRINT_SPLIT == 0)
  80. sb.Append(" ");
  81. count++;
  82. }
  83. }
  84. END:;
  85. return sb.ToString();
  86. }
  87. [DllImport("pthread", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
  88. private static extern int pthread_jit_write_protect_supported_np();
  89. [DllImport("libMonoHookUtils_OSX", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
  90. private static extern IntPtr memcpy_jit(IntPtr dst, IntPtr src, int len);
  91. }
  92. }
  93. #endif