FastNativeDetour.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Reflection;
  3. using System.Runtime.InteropServices;
  4. using BepInEx.Logging;
  5. using MonoMod.RuntimeDetour;
  6. using MonoMod.Utils;
  7. namespace BepInEx.IL2CPP.Hook
  8. {
  9. public class FastNativeDetour : IDetour
  10. {
  11. protected byte[] BackupBytes { get; set; }
  12. public bool IsValid { get; protected set; } = true;
  13. public bool IsApplied { get; protected set; }
  14. public IntPtr OriginalFunctionPtr { get; protected set; }
  15. public IntPtr DetourFunctionPtr { get; protected set; }
  16. public IntPtr TrampolinePtr { get; protected set; } = IntPtr.Zero;
  17. public int TrampolineSize { get; protected set; } = 0;
  18. protected int TrampolineJmpSize { get; set; } = 0;
  19. protected MethodInfo TrampolineMethod { get; set; }
  20. public FastNativeDetour(IntPtr originalFunctionPtr, IntPtr detourFunctionPtr)
  21. {
  22. OriginalFunctionPtr = originalFunctionPtr;
  23. DetourFunctionPtr = detourFunctionPtr;
  24. // TODO: This may not be safe during undo if the method is smaller than 20 bytes
  25. BackupBytes = new byte[20];
  26. Marshal.Copy(originalFunctionPtr, BackupBytes, 0, 20);
  27. }
  28. public void Apply()
  29. {
  30. Apply(null);
  31. }
  32. public void Apply(ManualLogSource debuggerLogSource)
  33. {
  34. if (IsApplied)
  35. return;
  36. DetourHelper.Native.MakeWritable(OriginalFunctionPtr, 32);
  37. if (debuggerLogSource != null)
  38. {
  39. debuggerLogSource.LogDebug($"Detouring 0x{OriginalFunctionPtr.ToString("X")} -> 0x{DetourFunctionPtr.ToString("X")}");
  40. debuggerLogSource.LogDebug("Original (32) asm");
  41. DetourGenerator.Disassemble(debuggerLogSource, OriginalFunctionPtr, 32);
  42. }
  43. var arch = IntPtr.Size == 8 ? Architecture.X64 : Architecture.X86;
  44. GenerateTrampolineInner(out int trampolineLength, out int jmpLength);
  45. DetourGenerator.ApplyDetour(OriginalFunctionPtr, DetourFunctionPtr, arch, trampolineLength - jmpLength);
  46. if (debuggerLogSource != null)
  47. {
  48. debuggerLogSource.LogDebug($"Trampoline allocation: 0x{TrampolinePtr.ToString("X")}");
  49. debuggerLogSource.LogDebug("Modified (32) asm");
  50. DetourGenerator.Disassemble(debuggerLogSource, OriginalFunctionPtr, 32);
  51. debuggerLogSource.LogDebug($"Trampoline ({trampolineLength}) asm");
  52. DetourGenerator.Disassemble(debuggerLogSource, TrampolinePtr, trampolineLength);
  53. }
  54. DetourHelper.Native.MakeExecutable(OriginalFunctionPtr, 32);
  55. IsApplied = true;
  56. }
  57. private void GenerateTrampolineInner(out int trampolineLength, out int jmpLength)
  58. {
  59. if (TrampolinePtr != IntPtr.Zero)
  60. {
  61. trampolineLength = TrampolineSize;
  62. jmpLength = TrampolineJmpSize;
  63. return;
  64. }
  65. byte[] instructionBuffer = new byte[32];
  66. Marshal.Copy(OriginalFunctionPtr, instructionBuffer, 0, 32);
  67. var trampolineAlloc = DetourHelper.Native.MemAlloc(80);
  68. DetourHelper.Native.MakeWritable(trampolineAlloc, 80);
  69. var arch = IntPtr.Size == 8 ? Architecture.X64 : Architecture.X86;
  70. DetourGenerator.CreateTrampolineFromFunction(instructionBuffer, OriginalFunctionPtr, trampolineAlloc,
  71. DetourGenerator.GetDetourLength(arch), arch, out trampolineLength, out jmpLength);
  72. DetourHelper.Native.MakeExecutable(trampolineAlloc, 80);
  73. TrampolinePtr = trampolineAlloc;
  74. TrampolineSize = trampolineLength;
  75. TrampolineJmpSize = jmpLength;
  76. }
  77. public void Undo()
  78. {
  79. if (!IsApplied)
  80. return;
  81. Marshal.Copy(BackupBytes, 0, OriginalFunctionPtr, BackupBytes.Length);
  82. DetourHelper.Native.MemFree(TrampolinePtr);
  83. TrampolinePtr = IntPtr.Zero;
  84. TrampolineSize = 0;
  85. IsApplied = false;
  86. }
  87. public void Free()
  88. {
  89. IsValid = false;
  90. }
  91. public MethodBase GenerateTrampoline(MethodBase signature = null)
  92. {
  93. if (TrampolineMethod == null)
  94. {
  95. // Generate trampoline without applying the detour
  96. GenerateTrampolineInner(out _, out _);
  97. if (TrampolinePtr == IntPtr.Zero)
  98. throw new InvalidOperationException("Trampoline pointer is not available");
  99. TrampolineMethod = DetourHelper.GenerateNativeProxy(TrampolinePtr, signature);
  100. }
  101. return TrampolineMethod;
  102. }
  103. public T GenerateTrampoline<T>() where T : Delegate
  104. {
  105. if (!typeof(Delegate).IsAssignableFrom(typeof(T)))
  106. throw new InvalidOperationException($"Type {typeof(T)} not a delegate type.");
  107. return GenerateTrampoline(typeof(T).GetMethod("Invoke")).CreateDelegate(typeof(T)) as T;
  108. }
  109. public void Dispose()
  110. {
  111. if (!IsValid)
  112. return;
  113. Undo();
  114. Free();
  115. }
  116. }
  117. }