private void GenerateTrampolineInner(out int trampolineLength, out int jmpLength) { if (TrampolinePtr != IntPtr.Zero) { trampolineLength = TrampolineSize; jmpLength = TrampolineJmpSize; return; } var instructionBuffer = new byte[32]; Marshal.Copy(OriginalFunctionPtr, instructionBuffer, 0, 32); var trampolineAlloc = PageAllocator.Instance.Allocate(OriginalFunctionPtr); logger.Log(LogLevel.Debug, $"Original: {OriginalFunctionPtr.ToInt64():X}, Trampoline: {trampolineAlloc:X}, diff: {Math.Abs(OriginalFunctionPtr.ToInt64() - trampolineAlloc):X}; is within +-1GB range: {PageAllocator.IsInRelJmpRange(OriginalFunctionPtr, trampolineAlloc)}"); DetourHelper.Native.MakeWritable(trampolineAlloc, PageAllocator.PAGE_SIZE); var arch = IntPtr.Size == 8 ? Architecture.X64 : Architecture.X86; DetourGenerator.CreateTrampolineFromFunction(instructionBuffer, OriginalFunctionPtr, trampolineAlloc, DetourGenerator.GetDetourLength(arch), arch, out trampolineLength, out jmpLength); DetourHelper.Native.MakeExecutable(trampolineAlloc, PageAllocator.PAGE_SIZE); TrampolinePtr = trampolineAlloc; TrampolineSize = trampolineLength; TrampolineJmpSize = jmpLength; }
private void GenerateTrampolineInner(out int trampolineLength, out int jmpLength) { if (TrampolinePtr != IntPtr.Zero) { trampolineLength = TrampolineSize; jmpLength = TrampolineJmpSize; return; } byte[] instructionBuffer = new byte[32]; Marshal.Copy(OriginalFunctionPtr, instructionBuffer, 0, 32); var trampolineAlloc = DetourHelper.Native.MemAlloc(80); DetourHelper.Native.MakeWritable(trampolineAlloc, 80); var arch = IntPtr.Size == 8 ? Architecture.X64 : Architecture.X86; DetourGenerator.CreateTrampolineFromFunction(instructionBuffer, OriginalFunctionPtr, trampolineAlloc, DetourGenerator.GetDetourLength(arch), arch, out trampolineLength, out jmpLength); DetourHelper.Native.MakeExecutable(trampolineAlloc, 80); TrampolinePtr = trampolineAlloc; TrampolineSize = trampolineLength; TrampolineJmpSize = jmpLength; }