public bool Hook(Data.CompileMethodDel 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.CompileMethodDel) Marshal.GetDelegateForFunctionPointer(Marshal.ReadIntPtr(pCompileMethod), typeof(Data.CompileMethodDel)); // 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 32 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="hookedCompileMethod32">The CompileMethod to call instead of the original.</param> public HookHelper32(Assembly asm, Data.CompileMethodDel hookedCompileMethod32) { LoadedAssembly = asm; ModuleScope = LoadedAssembly.ManifestModule.GetScope(); // .NET 4.0+ Hook = new JITHook <ClrjitAddrProvider>(hookedCompileMethod32); Original = Hook.OriginalCompileMethod32; }
/// <summary> /// Initialize a new 32 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="hookedCompileMethod32">The CompileMethod to call instead of the original.</param> public HookHelper32(Assembly asm, Data.CompileMethodDel hookedCompileMethod32) { LoadedAssembly = asm; ModuleScope = LoadedAssembly.ManifestModule.GetScope(); if (LoadedAssembly.ImageRuntimeVersion == "v2.0.50727") { // .NET 2.0-3.5 Hook = new JITHook <MscorjitAddrProvider>(hookedCompileMethod32); } else { // .NET 4.0+ Hook = new JITHook <ClrjitAddrProvider>(hookedCompileMethod32); } Original = Hook.OriginalCompileMethod32; }