Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/9.0-staging] Fix obtaining type handles of IDynamicInterfaceCastableImplementation #109909

Open
wants to merge 2 commits into
base: release/9.0-staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,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 @@ -57,6 +58,7 @@ public static int Run()
TestDefaultDynamicStaticNonGeneric.Run();
TestDefaultDynamicStaticGeneric.Run();
TestDynamicStaticGenericVirtualMethods.Run();
TestRuntime109496Regression.Run();

return Pass;
}
Expand Down Expand Up @@ -1703,4 +1705,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();
}
}
}
Loading