Simple JIT compiler hook in C#.
It supports both x64 and x86 applications, and supports .NET version from 2.0 to 4.5
- Add a reference to either SJITHook or SJITHook.Compact
- Determine what .NET version the target assembly is using, and pass the according VTableAddrProvider to JITHook:
// .NET 2.0->3.5
var hook = new JITHook<MscorjitAddrProvider>();
// .NET 4.0->4.5
var hook = new JITHook<ClrjitAddrProvider>();
- Install the hook, and pass your custom CompileMethod delegate:
if (hook.Hook(HookedCompileMethod))
Console.WriteLine("Successfully installed hook!");
private static unsafe int HookedCompileMethod(IntPtr thisPtr, [In] IntPtr corJitInfo,
[In] Data.CorMethodInfo* methodInfo, Data.CorJitFlag flags,
[Out] IntPtr nativeEntry, [Out] IntPtr nativeSizeOfCode)
{
/*
Do whatever you want with the parameters here
*/
return hook.OriginalCompileMethod(thisPtr, corJitInfo, methodInfo, flags, nativeEntry, nativeSizeOfCode);
}
- Finally uninstall the hook when you're done using it:
if (hook.UnHook())
Console.WriteLine("Successfully uninstalled hook!");
- SJITHook is simply replacing the entry in CILJit's VTable, and not writing any instructions in the actual compileMethod function.
- You need to enable Unsafe code in your project in order to use SJITHook.