Skip to content

Delegates closed over null are broken in Mono #126284

Closed

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.

Activity

  1. added
    untriaged
    on Mar 29, 2026
  2. added
    runtime-mono
    and removed on Mar 29, 2026
  3. dotnet-policy-service commented on Mar 29, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @steveisok, @vitek-karas
    See info in area-owners.md if you want to be subscribed.

  4. linked a pull request that will close this issueFix delegates closed over null in Mono #126285on Mar 29, 2026
  5. MichalPetryka commented on Jun 12, 2026

    @MichalPetryka
    ContributorAuthor

    This regressed in 8.0, probably in #83705, cc @BrzVlad since you approved it and know the code better than me (and cause Zoltan left MS afair).

  6. removed their assignment
    on Jun 26, 2026
  7. removed
    untriaged
    on Jul 10, 2026
  8. steveisok commented on Jul 10, 2026

    @steveisok
    Member

    @MichalPetryka given we are moving mono configurations towards coreclr, I don't think this is worth addressing.

  9. locked and limited conversation to collaborators on Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Codegen-JIT-monoruntime-mono

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions