Skip to content

Commit

Permalink
Fix obtaining type handles of IDynamicInterfaceCastableImplementation (
Browse files Browse the repository at this point in the history
…dotnet#109875)

Fixes dotnet#109496.

We try to optimize away type handles (MethodTables) of types that are only needed due to metadata. Trying to grab type handle of such type throws. This fixes it by unconditionally generating type handles of IDynamicInterfaceCastableImplementation.
  • Loading branch information
MichalStrehovsky authored and mikelle-rogers committed Dec 4, 2024
1 parent a5b9847 commit b74596d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ public static void GetMetadataDependencies(ref DependencyList dependencies, Node
default:
Debug.Assert(type.IsDefType);

// We generally postpone creating MethodTables until absolutely needed.
// IDynamicInterfaceCastableImplementation is special in the sense that just obtaining a System.Type
// (by e.g. browsing custom attribute metadata) gives the user enough to pass this to runtime APIs
// that need a MethodTable. We don't have a legitimate type handle without the MethodTable. Other
// kinds of APIs that expect a MethodTable have enough dataflow annotation to trigger warnings.
// There's no dataflow annotations on the IDynamicInterfaceCastable.GetInterfaceImplementation API.
if (type.IsInterface && ((MetadataType)type).IsDynamicInterfaceCastableImplementation())
{
dependencies ??= new DependencyList();
dependencies.Add(nodeFactory.ReflectedType(type), "Reflected IDynamicInterfaceCastableImplementation");
}

TypeDesc typeDefinition = type.GetTypeDefinition();
if (typeDefinition != type)
{
Expand Down
55 changes: 55 additions & 0 deletions src/tests/nativeaot/SmokeTests/UnitTests/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -60,6 +61,7 @@ public static int Run()
TestDefaultDynamicStaticNonGeneric.Run();
TestDefaultDynamicStaticGeneric.Run();
TestDynamicStaticGenericVirtualMethods.Run();
TestRuntime109496Regression.Run();

return Pass;
}
Expand Down Expand Up @@ -1890,4 +1892,57 @@ public static void Run()
Console.WriteLine(s_entry.Enter1<SimpleCallStruct<object>>("One"));
}
}

class TestRuntime109496Regression
{
class CastableThing : IDynamicInterfaceCastable
{
RuntimeTypeHandle IDynamicInterfaceCastable.GetInterfaceImplementation(RuntimeTypeHandle interfaceType)
=> Type.GetTypeFromHandle(interfaceType).GetCustomAttribute<TypeAttribute>().TheType.TypeHandle;
bool IDynamicInterfaceCastable.IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented)
=> Type.GetTypeFromHandle(interfaceType).IsDefined(typeof(TypeAttribute));
}

[Type(typeof(IMyInterfaceImpl))]
interface IMyInterface
{
int Method();
}

[DynamicInterfaceCastableImplementation]
interface IMyInterfaceImpl : IMyInterface
{
int IMyInterface.Method() => 42;
}

[Type(typeof(IMyGenericInterfaceImpl<int>))]
interface IMyGenericInterface
{
int Method();
}

[DynamicInterfaceCastableImplementation]
interface IMyGenericInterfaceImpl<T> : IMyGenericInterface
{
int IMyGenericInterface.Method() => typeof(T).Name.Length;
}

class TypeAttribute : Attribute
{
public Type TheType { get; }

public TypeAttribute(Type t) => TheType = t;
}

public static void Run()
{
object o = new CastableThing();

if (((IMyInterface)o).Method() != 42)
throw new Exception();

if (((IMyGenericInterface)o).Method() != 5)
throw new Exception();
}
}
}

0 comments on commit b74596d

Please sign in to comment.