Resolving the tokens found in the IL from a dynamic method
Asked Answered
B

1

6

Thanks to Hans Passant answering my question here: How do I get an IL bytearray from a DynamicMethod?

I was able to get up and running. I am now trying to resolve the Metadata tokens found in the IL emitted, to see what methods are being called, or what not. I am able to resolve that the next token in the method body is a call. I'm using some code from Mono.Reflection's MethodBodyReader.

static byte[] GetILByteArray(Delegate @delegate){
   // does stuff mentioned in other thread
}
...
Expression<Action> foo = () => Console.WriteLine(0);
var compiled = foo.Compile();
var bytes = GetILByteArray(compiled);
int index =Array.FindIndex(bytes,b=>GetOpCode(b).OperandType == OperandType.InlineMethod);
var token = BitConverter.ToInt32(bytes,index+1);
compiled.Method.Module.ResolveMember(token);

Throws an exception saying that the token is non-resolvable in that domain. Anyone have a trick here? Should I try passing in the delegates generic parameters or are they totally useless?

I'm currently toying around with the idea of writing a decompiler for delegates to expression trees and I'd really like to be able to use expression trees that I compile myself as test cases as I can always go back to the original and compare.

Bathyal answered 10/11, 2010 at 19:31 Comment(6)
Would a project like this help you: codeproject.com/KB/cs/… It seems to be working along the same lines, so its source might contain what you need.Heliotaxis
So you are using it? I definitely don't remember getting an answer mark for it. Help me get back to my job and I'll help you, hope that makes sense.Machicolate
Modified version of your answer. It's mostly correct, I'll mark you as the answer but I think the baked version is the one that is needed.Bathyal
Did you ever figure it out? I also ran into this.Lanni
I have a solution but it's so reflection-messy it makes me want to cry. You have to construct a RuntimeTypeHandle and RuntimeMethodHandle etc from IntPtrs. The Reference Source indicates that this is the only real way to solve it. Posting soon.Lanni
Now that your question is answered, I also have to let you know that Array.FindIndex is a terrible way to locate IL instructions. Since the instructions are variable length, you need to parse each instruction starting from the top of the array. Otherwise, Array.FindIndex will "find" your InlineMethod opcode in the middle of some random integer or floating point literal or member token.Lanni
L
11

The answer is that you must use the DynamicMethod.m_resolver to resolve tokens for dynamic methods rather than using Module. This makes sense because DynamicMethod.m_resolver.m_code is where you should be getting the IL byte array from.

This is difficult because DynamicResolver.ResolveToken returns IntPtr outs and turning them back into RuntimeTypeHandle and RuntimeMethodHandle etc. requires quite a lot of reflection. This solution is not likely to break in the .NET 4.x runtime but keep an eye out on any major version changes.

There's just no concise way to put this.

Define and use this interface instead of Module for resolving tokens:

public interface ITokenResolver
{
    MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
    Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
    FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
    MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
    byte[] ResolveSignature(int metadataToken);
    string ResolveString(int metadataToken);
}

For non-dynamic methods:

public sealed class ModuleTokenResolver : ITokenResolver
{
    private readonly Module module;

    public ModuleTokenResolver(Module module)
    {
        this.module = module;
    }

    public MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) =>
        module.ResolveMember(metadataToken, genericTypeArguments, genericMethodArguments);

    public Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) =>
        module.ResolveType(metadataToken, genericTypeArguments, genericMethodArguments);

    public FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) =>
        module.ResolveField(metadataToken, genericTypeArguments, genericMethodArguments);

    public MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) =>
        module.ResolveMethod(metadataToken, genericTypeArguments, genericMethodArguments);

    public byte[] ResolveSignature(int metadataToken) =>
        module.ResolveSignature(metadataToken);

    public string ResolveString(int metadataToken) =>
        module.ResolveString(metadataToken);
}

For dynamic methods:

public sealed class DynamicMethodTokenResolver : ITokenResolver
{
    private delegate void TokenResolver(int token, out IntPtr typeHandle, out IntPtr methodHandle, out IntPtr fieldHandle);
    private delegate string StringResolver(int token);
    private delegate byte[] SignatureResolver(int token, int fromMethod);
    private delegate Type GetTypeFromHandleUnsafe(IntPtr handle);

    private readonly TokenResolver tokenResolver;
    private readonly StringResolver stringResolver;
    private readonly SignatureResolver signatureResolver;
    private readonly GetTypeFromHandleUnsafe getTypeFromHandleUnsafe;
    private readonly MethodInfo getMethodBase;
    private readonly ConstructorInfo runtimeMethodHandleInternalCtor;
    private readonly ConstructorInfo runtimeFieldHandleStubCtor;
    private readonly MethodInfo getFieldInfo;

    public DynamicMethodTokenResolver(DynamicMethod dynamicMethod)
    {
        var resolver = typeof(DynamicMethod).GetField("m_resolver", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(dynamicMethod);
        if (resolver == null) throw new ArgumentException("The dynamic method's IL has not been finalized.");

        tokenResolver = (TokenResolver)resolver.GetType().GetMethod("ResolveToken", BindingFlags.Instance | BindingFlags.NonPublic).CreateDelegate(typeof(TokenResolver), resolver);
        stringResolver = (StringResolver)resolver.GetType().GetMethod("GetStringLiteral", BindingFlags.Instance | BindingFlags.NonPublic).CreateDelegate(typeof(StringResolver), resolver);
        signatureResolver = (SignatureResolver)resolver.GetType().GetMethod("ResolveSignature", BindingFlags.Instance | BindingFlags.NonPublic).CreateDelegate(typeof(SignatureResolver), resolver);

        getTypeFromHandleUnsafe = (GetTypeFromHandleUnsafe)typeof(Type).GetMethod("GetTypeFromHandleUnsafe", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(IntPtr) }, null).CreateDelegate(typeof(GetTypeFromHandleUnsafe), null);
        var runtimeType = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeType");

        var runtimeMethodHandleInternal = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeMethodHandleInternal");
        getMethodBase = runtimeType.GetMethod("GetMethodBase", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { runtimeType, runtimeMethodHandleInternal }, null);
        runtimeMethodHandleInternalCtor = runtimeMethodHandleInternal.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(IntPtr) }, null);

        var runtimeFieldInfoStub = typeof(RuntimeTypeHandle).Assembly.GetType("System.RuntimeFieldInfoStub");
        runtimeFieldHandleStubCtor = runtimeFieldInfoStub.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, new[] { typeof(IntPtr), typeof(object) }, null);
        getFieldInfo = runtimeType.GetMethod("GetFieldInfo", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { runtimeType, typeof(RuntimeTypeHandle).Assembly.GetType("System.IRuntimeFieldInfo") }, null);
    }

    public Type ResolveType(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
    {
        IntPtr typeHandle, methodHandle, fieldHandle;
        tokenResolver.Invoke(metadataToken, out typeHandle, out methodHandle, out fieldHandle);

        return getTypeFromHandleUnsafe.Invoke(typeHandle);
    }

    public MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
    {
        IntPtr typeHandle, methodHandle, fieldHandle;
        tokenResolver.Invoke(metadataToken, out typeHandle, out methodHandle, out fieldHandle);

        return (MethodBase)getMethodBase.Invoke(null, new[]
        {
            typeHandle == IntPtr.Zero ? null : getTypeFromHandleUnsafe.Invoke(typeHandle),
            runtimeMethodHandleInternalCtor.Invoke(new object[] { methodHandle })
        });
    }

    public FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
    {
        IntPtr typeHandle, methodHandle, fieldHandle;
        tokenResolver.Invoke(metadataToken, out typeHandle, out methodHandle, out fieldHandle);

        return (FieldInfo)getFieldInfo.Invoke(null, new[]
        {
            typeHandle == IntPtr.Zero ? null : getTypeFromHandleUnsafe.Invoke(typeHandle),
            runtimeFieldHandleStubCtor.Invoke(new object[] { fieldHandle, null })
        });
    }

    public MemberInfo ResolveMember(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
    {
        IntPtr typeHandle, methodHandle, fieldHandle;
        tokenResolver.Invoke(metadataToken, out typeHandle, out methodHandle, out fieldHandle);

        if (methodHandle != IntPtr.Zero)
        {
            return (MethodBase)getMethodBase.Invoke(null, new[]
            {
                typeHandle == IntPtr.Zero ? null : getTypeFromHandleUnsafe.Invoke(typeHandle),
                runtimeMethodHandleInternalCtor.Invoke(new object[] { methodHandle })
            });
        }

        if (fieldHandle != IntPtr.Zero)
        {
            return (FieldInfo)getFieldInfo.Invoke(null, new[]
            {
                typeHandle == IntPtr.Zero ? null : getTypeFromHandleUnsafe.Invoke(typeHandle),
                runtimeFieldHandleStubCtor.Invoke(new object[] { fieldHandle, null })
            });
        }

        if (typeHandle != IntPtr.Zero)
        {
            return getTypeFromHandleUnsafe.Invoke(typeHandle);
        }

        throw new NotImplementedException("DynamicMethods are not able to reference members by token other than types, methods and fields.");
    }

    public byte[] ResolveSignature(int metadataToken)
    {
        return signatureResolver.Invoke(metadataToken, 0);
    }

    public string ResolveString(int metadataToken)
    {
        return stringResolver.Invoke(metadataToken);
    }
}

This is how to detect dynamic methods, and some helper methods:

public static class ReflectionExtensions
{
    public static bool IsLightweightMethod(this MethodBase method)
    {
        return method is DynamicMethod || typeof(DynamicMethod).GetNestedType("RTDynamicMethod", BindingFlags.NonPublic).IsInstanceOfType(method);
    }

    public static ITokenResolver GetTokenResolver(this MethodBase method)
    {
        var dynamicMethod = TryGetDynamicMethod(method as MethodInfo) ?? method as DynamicMethod;
        return dynamicMethod != null
            ? new DynamicMethodTokenResolver(dynamicMethod)
            : (ITokenResolver)new ModuleTokenResolver(method.Module);
    }

    public static byte[] GetILBytes(this MethodBase method)
    {
        var dynamicMethod = TryGetDynamicMethod(method as MethodInfo) ?? method as DynamicMethod;
        return dynamicMethod != null
            ? GetILBytes(dynamicMethod)
            : method.GetMethodBody()?.GetILAsByteArray();
    }

    public static byte[] GetILBytes(DynamicMethod dynamicMethod)
    {
        var resolver = typeof(DynamicMethod).GetField("m_resolver", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(dynamicMethod);
        if (resolver == null) throw new ArgumentException("The dynamic method's IL has not been finalized.");
        return (byte[])resolver.GetType().GetField("m_code", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(resolver);
    }

    public static DynamicMethod TryGetDynamicMethod(MethodInfo rtDynamicMethod)
    {
        var typeRTDynamicMethod = typeof(DynamicMethod).GetNestedType("RTDynamicMethod", BindingFlags.NonPublic);
        return typeRTDynamicMethod.IsInstanceOfType(rtDynamicMethod)
            ? (DynamicMethod)typeRTDynamicMethod.GetField("m_owner", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(rtDynamicMethod)
            : null;
    }
}
Lanni answered 29/2, 2016 at 22:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.