Skip to content

ILVerification fails unexpectedly on delegate construction #65010

Closed

Description

@jcouv

Description

Assignments of an extension method to Action trips ILVerification.

Reproduction Steps

Compile the following C# code and verify it.

The verification fails with:
Unrecognized arguments for delegate .ctor. { Offset = 21 } // that's 0x15
Unrecognized arguments for delegate .ctor. { Offset = 59 } // that's 0x3B

using System;
static class Program
{
    static void Main()
    {
        "ABC".Goo();
        Action a = "123".Goo;
        a();
        a = new Action(a);
        a();
        a = new Action("xyz".Goo);
        a();
    }
    static void Goo(this string x)
    {
        Console.WriteLine(x);
    }
}
// ILVerify:
//     Unrecognized arguments for delegate .ctor. { Offset = 21 } // that's 0x15
//     Unrecognized arguments for delegate .ctor. { Offset = 59 } // that's 0x3B
.class private auto ansi abstract sealed beforefieldinit Program
	extends [netstandard]System.Object
{
	.custom instance void [netstandard]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = (
		01 00 00 00
	)
	// Methods
	.method private hidebysig static 
		void Main () cil managed 
	{
		// Method begins at RVA 0x2050
		// Code size 70 (0x46)
		.maxstack 2
		.entrypoint
		IL_0000: ldstr "ABC"
		IL_0005: call void Program::Goo(string)
		IL_000a: ldstr "123"
		IL_000f: ldftn void Program::Goo(string)
		IL_0015: newobj instance void [netstandard]System.Action::.ctor(object, native int)
		IL_001a: dup
		IL_001b: callvirt instance void [netstandard]System.Action::Invoke()
		IL_0020: ldftn instance void [netstandard]System.Action::Invoke()
		IL_0026: newobj instance void [netstandard]System.Action::.ctor(object, native int)
		IL_002b: callvirt instance void [netstandard]System.Action::Invoke()
		IL_0030: ldstr "xyz"
		IL_0035: ldftn void Program::Goo(string)
		IL_003b: newobj instance void [netstandard]System.Action::.ctor(object, native int)
		IL_0040: callvirt instance void [netstandard]System.Action::Invoke()
		IL_0045: ret
	} // end of method Program::Main
	.method private hidebysig static 
		void Goo (
			string x
		) cil managed 
	{
		.custom instance void [netstandard]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = (
			01 00 00 00
		)
		// Method begins at RVA 0x20a2
		// Code size 7 (0x7)
		.maxstack 8
		IL_0000: ldarg.0
		IL_0001: call void [netstandard]System.Console::WriteLine(string)
		IL_0006: ret
	} // end of method Program::Goo
} // end of class Program

This was found in Roslyn, test: ExtensionMethodTests.ExtensionMethodInTheSameClass. Discussion

Expected behavior

No error (ie. same behavior as PEVerify)

Actual behavior

Unrecognized arguments for delegate .ctor. { Offset = 21 }
Unrecognized arguments for delegate .ctor. { Offset = 59 }

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. added
    area-Tools-ILVerification
    on Feb 8, 2022
  2. ghost added
    untriaged
    on Feb 8, 2022
  3. added
    help wanted
    on Feb 9, 2022
  4. added a commit that references this issue on Feb 16, 2022
    8dce7f8
  5. ghost added
    in-pr
    on Feb 16, 2022
  6. added a commit that references this issue on Feb 17, 2022
    385fc88
  7. ghost removed
    in-pr
    on Feb 17, 2022
  8. ghost locked as resolved and limited conversation to collaborators on Mar 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILVerificationhelp wanteduntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions