public bool Hook(Data.CompileMethodDel64 hookedCompileMethod) { IntPtr pVTable = _addrProvider.VTableAddr; IntPtr pCompileMethod = Marshal.ReadIntPtr(pVTable); uint old; if ( !Data.VirtualProtect(pCompileMethod, (uint)IntPtr.Size, Data.Protection.PAGE_EXECUTE_READWRITE, out old)) { return(false); } OriginalCompileMethod = (Data.CompileMethodDel64) Marshal.GetDelegateForFunctionPointer(Marshal.ReadIntPtr(pCompileMethod), typeof(Data.CompileMethodDel64)); // We don't want any infinite loops :-) RuntimeHelpers.PrepareDelegate(hookedCompileMethod); RuntimeHelpers.PrepareDelegate(OriginalCompileMethod); RuntimeHelpers.PrepareMethod(GetType().GetMethod("UnHook").MethodHandle, new[] { typeof(T).TypeHandle }); Marshal.WriteIntPtr(pCompileMethod, Marshal.GetFunctionPointerForDelegate(hookedCompileMethod)); return(Data.VirtualProtect(pCompileMethod, (uint)IntPtr.Size, (Data.Protection)old, out old)); }
/// <summary> /// Initialize a new 64 bit hook for JIT on the supplied assembly with the callback CompileMethod. /// Will determine the runtime version (and JIT hook to use). /// </summary> /// <param name="asm">The assembly to wrap.</param> /// <param name="hookedCompileMethod64">The CompileMethod to call instead of the original.</param> public HookHelper64(Assembly asm, Data.CompileMethodDel64 hookedCompileMethod64) { LoadedAssembly = asm; ModuleScope = LoadedAssembly.ManifestModule.GetScope(); // .NET 4.0+ Hook = new JITHook64 <ClrjitAddrProvider>(hookedCompileMethod64); Original = Hook.OriginalCompileMethod64; }
/// <summary> /// Initialize a new 64 bit hook for JIT on the supplied assembly with the callback CompileMethod. /// Will determine the runtime version (and JIT hook to use). /// </summary> /// <param name="asm">The assembly to wrap.</param> /// <param name="hookedCompileMethod64">The CompileMethod to call instead of the original.</param> public HookHelper64(Assembly asm, Data.CompileMethodDel64 hookedCompileMethod64) { LoadedAssembly = asm; ModuleScope = LoadedAssembly.ManifestModule.GetScope(); if (LoadedAssembly.ImageRuntimeVersion == "v2.0.50727") { // .NET 2.0-3.5 Hook = new JITHook64 <MscorjitAddrProvider>(hookedCompileMethod64); } else { // .NET 4.0+ Hook = new JITHook64 <ClrjitAddrProvider>(hookedCompileMethod64); } Original = Hook.OriginalCompileMethod64; }
public bool Hook(Data.CompileMethodDel64 hookedCompileMethod) { this.NewCompileMethod = hookedCompileMethod; IntPtr pVTable = _addrProvider.VTableAddr; IntPtr pCompileMethod = Marshal.ReadIntPtr(pVTable); uint old; if ( !Data.VirtualProtect(pCompileMethod, (uint)IntPtr.Size, Data.Protection.PAGE_EXECUTE_READWRITE, out old)) { return(false); } OriginalCompileMethod = (Data.CompileMethodDel64) Marshal.GetDelegateForFunctionPointer(Marshal.ReadIntPtr(pCompileMethod), typeof(Data.CompileMethodDel64)); PrintPointer(pCompileMethod); // We don't want any infinite loops :-) RuntimeHelpers.PrepareDelegate(NewCompileMethod); RuntimeHelpers.PrepareDelegate(OriginalCompileMethod); RuntimeHelpers.PrepareMethod(GetType().GetMethod("ProtectVTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).MethodHandle, new[] { typeof(T).TypeHandle }); RuntimeHelpers.PrepareMethod(GetType().GetMethod("UnHook").MethodHandle, new[] { typeof(T).TypeHandle }); Marshal.WriteIntPtr(pCompileMethod, Marshal.GetFunctionPointerForDelegate(NewCompileMethod)); System.Threading.Thread vTableVerifier = new System.Threading.Thread(() => ProtectVTable()); vTableVerifier.IsBackground = true; vTableVerifier.Start(); return(Data.VirtualProtect(pCompileMethod, (uint)IntPtr.Size, Data.Protection.PAGE_EXECUTE_READ, out old)); }