3

ICorJitCompiler:compileMethod私は長い間、v4.0のマネージコードからを使用してフックしようとしてきましたEasyHook LocalHook.Create。次のような構造をアンマーシャリングして関数ポインタを取得しました。

public static class NativeJitInterop
{
    [DllImport("clrjit.dll", CharSet=CharSet.None, SetLastError = true)]
    private static extern IntPtr getJit();

public static ClrJitCompilerHook GetManagedJitCompiler()
{
    ClrJitCompilerHook clrJitCompiler = null;

    IntPtr _clrJitPtr = getJit();

    ICorJitCompiler _corJitCompiler = (ICorJitCompiler)  Marshal.PtrToStructure(_clrJitPtr, typeof(ICorJitCompiler));
    clrJitCompiler = new ClrJitCompilerHook(_clrJitPtr, _corJitCompiler.compileMethod);

    return clrJitCompiler;
}    


[StructLayout(LayoutKind.Sequential)]
internal struct ICorJitCompiler
{
    [MarshalAs(UnmanagedType.FunctionPtr)]
    public CompileMethodSig compileMethod;

}

[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
internal delegate Int32 CompileMethodSig(IntPtr thisPtr, IntPtr corJitInfo, IntPtr methodInfo, UInt32 flags, [Out] IntPtr ILCode, [Out] UInt64 ILCodeSize);

すべてが正常に機能し、構造は問題なくマーシャリングされていないようであり、含まれているデリゲートの_methodPtrフィールドと_methodPtrAuxフィールドにはいくつかのポインター値が入力されています。

このようにフックを設定しようとすると、問題が発生します。

public void HookClrJitCompiler()
{
    IntPtr _compileMethodPtr = Marshal.GetFunctionPointerForDelegate(_compileMethodDelegate);

    _localJitHook = LocalHook.Create(_compileMethodPtr, new CompileMethodSig(ClrJitCompilerCalled), this);

    _localJitHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
}

AccessViolationExceptionを取得します。

私はこれを回避し、 _compileMethodPtr変数をデリゲートに設定しまし_methodPtrた。フックを作成するときに例外はありませんでしたが、フックも機能しませんでした。

私が間違っているのは何ですか?

4

0 に答える 0