Runtime: [Bug ?] Bad binary signature for invoke GetDynamicILInfo() without do anything !?

Created on 9 Sep 2020  路  8Comments  路  Source: dotnet/runtime

Description

I try to write il code via DynamicILInfo but never success at once so I test call it and got error for just invoke GetDynamicILInfo.

Configuration

Windows 10 1909
.Net core 3.1
VS 2019 16.7.3

Regression?

https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AbEA3YAfASQFsAHaAFwGcACAZQE9LyYiBYAKGLKiustmoBeOo2ZEAdACUYAMwwww5AJYQAduICiRJeQ5cKNCCSEimLKbPmKV6rTvEB5EgGEIAExiUOHAAoBXYAwlMGoAWXc/eWpnDmo46n9A4LoAsIBDJVUACgBKWPiC6gB1HQALagA5GAB3PlhxABF6VTTtMFCYclL3LIAiXrRKiC7MgHNBgG8AcU6AFXoSGCyCVXIAZgAmHIBfPPZCg+Ky6nEZ8gIAGRnVGCg08mh8w8PNbXIso3ELtzSoUYB9AAMe2ez1eOg+JHEPiMINBB3B70+0nIcPhGlUbiOXSeoIaSlgimcaSYWXEzlg9xgDRg8lGVKyZ3miyyAEFrGosg4ZNQVustjkcoN2cpOdzeatNoLxCtsBAANZLACcSrRhQxWJKOPYuIKWvKVVq/BgjWarWCHS6PX6gwqw1KY0mTIWSz5Ut2usO+pOZyaLTalxWMgg3n28OoGuxpU9B29p06l2ut3ujzD4fiiMhXx+fyBavTmc+MJI+fDhahKNLB0j+pjhXxhPIxNJ5MpzBpdIZzpZIpsXJ5boFQuovbFA8lAplqjliqyKqrEcxUdxkdoAQ4kfCbkiMCAA==

Imports System
Imports sre = System.Reflection.Emit
Imports op = System.Reflection.Emit.OpCodes

Public Module C
    Public Sub Main()
' Work fine.
         With New sre.DynamicMethod("", Nothing, {GetType(Int32)})
            With .GetILGenerator
                .Emit(op.Ldarg_0)
                .Emit(op.Pop)
                .Emit(op.Ret)
            End With
            DirectCast(.CreateDelegate(GetType(Action(Of Int32))), Action(Of Int32)).Invoke(99)
        End With

' Got an System.BadImageFormatException: 'Bad binary signature
        With New sre.DynamicMethod("", Nothing, {GetType(Int32)})
            With .GetDynamicILInfo

            End With
            With .GetILGenerator
                .Emit(op.Ldarg_0)
                .Emit(op.Pop)
                .Emit(op.Ret)
            End With
            DirectCast(.CreateDelegate(GetType(Action(Of Int32))), Action(Of Int32)).Invoke(99)
        End With
    End Sub
End Module

Other information

Is this a bug ? I didn't see any info inform me why it error also can anyone please give me an example to create method via DynamicILInfo, I try simple code like .SetCode({2, 38, 42}, 1) but it still error; I want to edit method on runtime like copy il code via array byte from method body to new dynamic method.

area-System.Reflection.Emit question

Most helpful comment

The format is described in EMCA 335 搂II.25.4.5.

It can't really have a better error message because then it'd have to actually verify the signature which would be a sizable performance hit. Better documentation would be nice but that'd probably be a lot of work when most of the folks who would opt for this API are already familiar with the format (or at least where to find it).

All 8 comments

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

You need to set the local signature and exception handling regions as well, e.g

info.SetLocalSignature(new byte[] { 7, 0 });
info.SetExceptions(new byte[] { 1, 4, 0, 0 });

Thank you very much @SeeminglyScience,
I successful compile simple code like return 5; but when I try to calling other method it won't compile anyway, could you please point where did I missing or wrong again.

    Sub twrite(Input As Int64)
        Console.WriteLine(Input)
    End Sub

A il code from above method is "00-02-28-55-00-00-0A-00-2A" but I got invalid error from this, I try to apply signal from SignatureHelper.GetMethodSigHelper (with not much hope if I understand correctly it use with calli) "00-01-01-0A" to "00-02-28-55-01-01-0A-00-2A" and I still got the same error (still use {7, 0} and {1, 4, 0, 0} for local and exception).

You won't be able to direct copy the IL from another method as the metadata tokens won't be valid from the context of the dynamic method. You'd need to analyze the IL, pick out the tokens, and swap in your own from DynamicILInfo.GetTokenFor. You'd also need to do the same for the source method's locals signature and exception regions.

Thank you very much @SeeminglyScience ,
I can compile most of what I plan to do even I can't find anything about how to manage exception region(no help from MSDN, no helpful error message what's ever I input to SetExceptions just FormatException).

The format is described in EMCA 335 搂II.25.4.5.

It can't really have a better error message because then it'd have to actually verify the signature which would be a sizable performance hit. Better documentation would be nice but that'd probably be a lot of work when most of the folks who would opt for this API are already familiar with the format (or at least where to find it).

Thank you very much once again @SeeminglyScience , so that why googleing isn't much help for a newbie like me.

Closing; no actionable issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nalywa picture nalywa  路  3Comments

Timovzl picture Timovzl  路  3Comments

yahorsi picture yahorsi  路  3Comments

EgorBo picture EgorBo  路  3Comments

omariom picture omariom  路  3Comments