You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I modified the FileMon example to hook the compileMethod of a program.
The full code is here.
The code looks like this
Get compileMethod addr
// use getJit() method get compileMethod of jit// source code https://github.com/dotnet/coreclr/blob/release/3.1/src/jit/ee_il_dll.hpppublicstatic IntPtr GetCompileMethodPtr(){// get CILJit() obj// source code https://github.com/dotnet/coreclr/blob/release/3.1/src/jit/ee_il_dll.cppIntPtrpJit= GetJit();if(pJit==null)return IntPtr.Zero;// self define CorJit struct.// Although the structure of each .net version will be different, compileMethod is always the first member.compiler= Marshal.PtrToStructure<CorJitCompilerNative>(Marshal.ReadIntPtr(pJit));// get the origin compileMethod methodoriginCompile= compiler.CompileMethod;// get the ptr of compileMethodoriginCompilePtr= Marshal.GetFunctionPointerForDelegate(originCompile);returnoriginCompilePtr;}
The delegate method is
// [source code](https://github.com/dotnet/coreclr/blob/release/3.1/src/jit/ee_il_dll.cpp)internalstatic CorJitCompiler.CorJitResult compileMethodDel(IntPtrthisPtr,[In]IntPtrcorJitInfoPtr,[In]CorInfo*methodInfo,CorJitFlagflags,[Out]IntPtrnativeEntry,[Out]IntPtrnativeSizeOfCode){
Interface.Info("["+ RemoteHooking.GetCurrentProcessId()+":"+
RemoteHooking.GetCurrentThreadId()+"]: \""+" ############# My This compile Method called ##############"+"\"");return originCompile(thisPtr, corJitInfoPtr, methodInfo, flags, nativeEntry, nativeSizeOfCode);}
Usage: FileMon %PID%
or: FileMon PathToExecutable
Please enter a process Id or path to executable: F:\extraIncome\testpro\ConsoleApp5\TargetProcess\WindowsFormsApp2.exe
Created and injected process 26336
<Press any key to exit>
The info is origiCompilePtr is 140711675387888
FileMon has been installed in target 26336.
The info is [26336:35612]: " ############# My This compile Method called ##############"
The info is [26336:35612]: " ############# My This compile Method called ##############"
The info is [26336:35612]: " ############# My This compile Method called ##############"
The info is [26336:35612]: " ############# My This compile Method called ##############"
The info is [26336:35612]: " ############# My This compile Method called ##############"
The info is [26336:35612]: " ############# My This compile Method called ##############"
The info is [26336:35612]: " ############# My This compile Method called ##############"
The info is [26336:35612]: " ############# My This compile Method called ##############"
The info is [26336:35612]: " ############# My This compile Method called ##############"
The info is [26336:35612]: " ############# My This compile Method called ##############"
The info is [26336:35612]: " ############# My This compile Method called ##############"
The text was updated successfully, but these errors were encountered:
Not sure if this is the root cause but you should avoid calling Interface.xyz directly in your hook handler. See how FileMon example added messages to a queue and processes them on the entrypoint thread instead.
Generally speaking the hook handler should be as lightweight as possible, and definitely avoid any thread related activities.
The link you provided is invalid for full example so I can’t really comment further.
I modified the FileMon example to hook the compileMethod of a program.
The full code is here.
The code looks like this
Get compileMethod addr
The delegate method is
The hook code.
It can print some logs, but it still crashes.
output
The text was updated successfully, but these errors were encountered: