Skip to content

Delegates closed over null are broken in Mono #126284

@MichalPetryka

Description

@MichalPetryka

Description

Creating and calling a delegate closed over null in Mono leads to runtime errors due to invalid stubs being used, see godbolt for an example.

Reproduction Steps

using System;

internal class Program
{
    public static void Main()
    {
        Console.WriteLine(Nul());
    }

	public static bool Nul()
	{
		return new Func<bool>(new C().A).Method.CreateDelegate<Func<bool>>(null)();
	}

	internal class C
	{
		public bool A()
		{
			return this is null;
		}
	}
}

Expected behavior

Prints true.

Actual behavior

Unhandled Exception:
System.InvalidProgramException: Invalid IL code in (wrapper delegate-invoke) System.Func`1<bool>:invoke_callvirt_TResult (): IL_0066: ldarga.s  1


   at Program.Nul()
   at Program.Main()

Regression?

No idea.

Known Workarounds

Don't use Mono

Configuration

Mono main windows/linux x64

Other information

One issue I found was that Mono was incorrectly using target being null to decide the stub but even after I fixed that here, the issue was still broken.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions