dotnet/runtime

ILVerification fails unexpected on delegate construction with instance method

Open

#96,695 opened on 2024年1月9日

GitHub で見る
 (4 comments) (0 reactions) (0 assignees)C# (5,445 forks)batch import
area-Tools-ILVerificationhelp wanted

Repository metrics

Stars
 (17,886 stars)
PR merge metrics
 (平均マージ 12d 11h) (30d で 661 merged PRs)

説明

As part of rolsyn PR https://github.com/dotnet/roslyn/pull/71544, we're updating to ILVerify version 8.0.0-rtm.23523.3 (from 7.0.0-alpha.1.22060.1).

The test below shows the verification failure. The error is "[Main]: Unrecognized arguments for delegate .ctor. { Offset = 0xe }".

As far as I can tell the problem is in IsDelegateAssignable which ends up doing an IsAssignable check for assigning an int32 (which comes from the firstArg parameter and represents the stack value) to object.

Please let us know if the problem turns out to be with the code generated by roslyn (see IL captured in test below).

        [Fact]
        public void StructDelegate()
        {
            var source = @"
using System;
class Program
{
    static void Main()
    {
        int x = 42;
        Func<string> f = x.ToString;
        Console.Write(f.Invoke());
    }
}";
            var verifier = CompileAndVerify(source, expectedOutput: "42",
                verify: Verification.FailsILVerify with { ILVerifyMessage = "[Main]: Unrecognized arguments for delegate .ctor. { Offset = 0xe }" });

            verifier.VerifyIL("Program.Main", """
{
  // Code size       30 (0x1e)
  .maxstack  2
  IL_0000:  ldc.i4.s   42
  IL_0002:  box        "int"
  IL_0007:  dup
  IL_0008:  ldvirtftn  "string object.ToString()"
  IL_000e:  newobj     "System.Func<string>..ctor(object, System.IntPtr)"
  IL_0013:  callvirt   "string System.Func<string>.Invoke()"
  IL_0018:  call       "void System.Console.Write(string)"
  IL_001d:  ret
}
""");
        }

コントリビューターガイド