Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Internal compiler error when .NET Native compiles __makeref, __refvalue, TypedReference #7372

Closed

Description

@verelpode

When I put the following C# code in a UWP app in VS 2019 16.0.2, it works well when compiled for Debug. However, when it is compiled for Release (.NET Native), then error ILT0005 and "Internal compiler error" occur.

class MyTestTypedReference
{
	protected int fTestField0 = 123;
	protected int fTestField1 = 456;
	protected string fTestField2 = "TestStr";
	protected int fTestField3 = 0;

	public unsafe void TestMakeTrArray()
	{
		System.TypedReference* trArray = stackalloc System.TypedReference[4];
		trArray[0] = __makeref(fTestField0);
		trArray[1] = __makeref(fTestField1);
		trArray[2] = __makeref(fTestField2);
		trArray[3] = __makeref(fTestField3);
		TestReceiveTrArray(trArray);
	}

	public unsafe void TestReceiveTrArray(System.TypedReference* inParams)
	{
		RealMethod1(__refvalue(inParams[0], int), __refvalue(inParams[1], int), __refvalue(inParams[2], string), out __refvalue(inParams[3], int));
	}

	public void RealMethod1(int inA, int inB, string inC, out int outD)
	{
		outD = 6000;
	}
}

public unsafe delegate void TestDelegateWithTrArray(object inInstance, System.TypedReference* inParams);

The error is:

Internal compiler error: Object reference not set to an instance of an object.

If you comment out TestReceiveTrArray and compile the rest (meaning compile the __makeref stuff but not the __refvalue stuff), then the error is:

Error ILT0005: 'C:\Users\XXXX\.nuget\packages\runtime.win10-x64.microsoft.net.native.compiler\2.1.8\tools\x64\ilc\Tools\nutc_driver.exe 
@"C:\Users\XXXX\Documents\TestUwpApp1\obj\x64\Release\ilc\intermediate\MDIL\TestUwpApp1.rsp"' 
returned exit code -2147483645

System.TypedReference is low-level but quite useful in certain scenarios such as dynamic programming, Reflection, late-binding / dynamic binding at runtime.

System.Delegate.DynamicInvoke(params object[]) and System.Reflection.MethodBase.Invoke(object, object[]) are useful but costly, especially when executed many times in a loop. The above code snippet is an example of how TypedReference can be used to dynamically execute a method without the overhead of boxing all of the parameters. It works great when compiled to CIL, but not when compiled with .NET Native 2.1.8.

In some circumstances, an alternative to TypedReference is the DLR, but if I remember correctly, it was @jkotas who wrote somewhere something like the following comment about the DLR or DLR with .NAT Native: "This stuff is super fragile". I feel that it is too risky to switch over to a solution that relies upon the DLR. The DLR is unnecessary in this particular case/project because TypedReference is a much simpler solution that works well. Some projects do need the full functionality of the DLR, but other projects can get away with TypedReference.

I very much hope that .NET Native will fully support TypedReference. Given the choice between .NET Native supporting TypedReference or the DLR, then obviously TypedReference is far far easier to support and maintain than the big complex DLR.

Activity

  1. MichalStrehovsky commented on Apr 29, 2019

    @MichalStrehovsky
    Member

    We don't have plans to add these instructions to .NET Native at this time. This can change in the future, but since mkrefany/refanyval/refanytype are only accessible through undocumented C# keywords, this was scoped out in the 2.0 release.

    We were supposed to error out on this with a proper message, but seems like the bug that was tracking it was missed an is still open. For internal reference, the bug is 404381.

  2. verelpode commented on Apr 29, 2019

    @verelpode
    Author

    @MichalStrehovsky That's a pity. Does .NET Native support any other way of doing the equivalent of System.Delegate.DynamicInvoke(params object[]) without boxing each parameter?

    I guess this also means that __arglist and System.ArgIterator are likewise not in the plan for .NET Native.

  3. hc4 commented on Apr 30, 2019

    @hc4
    Contributor

    @MichalStrehovsky That's a pity. Does .NET Native support any other way of doing the equivalent of System.Delegate.DynamicInvoke(params object[]) without boxing each parameter?

    Recently I've done it with help of new ref variables from C# 7.3, but it requires all args of equal size and to create one struct and method overload for each args count:

    public struct Args1
    {
    public Arg Arg1;
    }
    
    public struct Args2
    {
    public Arg Arg1;
    public Arg Arg2;
    }
    
    [StructLayout(LayoutKind.Explicit, Size = 8)]
    public struct Arg
    {
        [FieldOffset(0)] public int Int;
        [FieldOffset(0)] public string String;
    }
    
    public void Method(ref Args1 args) ...
    public void Method(ref Args2 args) ...
    

    Also you can use Unsafe.As to wrap/unwrap args

  4. MichalStrehovsky commented on Apr 30, 2019

    @MichalStrehovsky
    Member
    [StructLayout(LayoutKind.Explicit, Size = 8)]
    public struct Arg
    {
        [FieldOffset(0)] public int Int;
        [FieldOffset(0)] public string String;
    }

    This is not legal. .NET Native misses the validation for this, but CoreCLR and CLR will fail to load such type. You cannot overlap reference type and non-reference type fields. This will cause memory corruptions.

    High performance reflection APIs are discussed here: dotnet/corefx#14021, but we don't have anything concrete yet.

  5. hc4 commented on Apr 30, 2019

    @hc4
    Contributor

    You cannot overlap reference type and non-reference type fields

    Sure, my mistake.
    For reference type one could use GCHandle.Alloc and pass it as IntPtr:

    [StructLayout(LayoutKind.Explicit, Size = 8)]
    public struct Arg
    {
        [FieldOffset(0)] public int Int;
        [FieldOffset(0)] public IntPtr Handle;
    }
    
    var stringHandle = GCHandle.Alloc(str);
    var arg = new Arg { Handle =  GCHandle.ToIntPtr(stringHandle) }

    But it won't work with external API :)
    We need more details, but I almost sure it could be solved without undocumented features.

  6. verelpode commented on Apr 30, 2019

    @verelpode
    Author

    @MichalStrehovsky OK, thanks, you've helped me decide the path forward. In order to be compatible with .NET Native, and to avoid taking the risk of relying on the DLR, I think I'll go with a solution that boxes all the parameters to a mutable "box" type instead of System.Object, and then pool and recycle the "mutable boxes", so that the late-binding cost is the same for 10 iterations of a loop as for a million iterations.

    By the way, the problem with the DLR seems to be that it is a combination of useful and counterproductive functionality. For example: Useful example: Late-binding is useful in certain cases. Counterproductive example: If every object can change its type at runtime, then this seems to be a case of claiming "That's not a bug. That's a feature!" and then trying really, really, damn hard to believe it. For example, TypeScript attempts to solve the problem of the "runtime type morphism feature/defect" in JavaScript. The lesson learned from TypeScript and other examples is that dynamic languages tend to be a mixed bag of helpful + counterproductive ideas rolled into one. Microsoft already abandoned IronRuby and IronPython and I find that to be an understandable decision.

    Thus the DLR poses a problem for .NET Native / CoreRT because the DLR itself is a problem. Thus it would be good if CoreRT provides alternatives. I can see value in supporting __makeref but yes I can also understand if it's decided that it's not the best solution.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions