Skip to content

Commit aca18aa

Browse files
Ensure type loader templates for interface dispatch cells (#134774)
Fixes #134698. The `TemplateConstructableTypes` was done on the canonicalized version of the method, so `IEnumerable<Foo<T__Canon>>` would turn into `IEnumerable<__Canon>` and we would forget a type loader template for `Foo<T__Canon>>` is needed. This bug is old, .NET Native (Project X one) likely had it too. It requires a bit of bad luck to hit because normally the template would show up elsewhere. The repro hit this due to interaction with interface variance.
1 parent 29be631 commit aca18aa

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

‎src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NativeLayoutVertexNode.cs‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,16 +1252,13 @@ public sealed override IEnumerable<DependencyListEntry> GetStaticDependencies(No
12521252
{
12531253
yield return new DependencyListEntry(_signature, "TypeSignature");
12541254

1255-
MethodDesc method = _method;
1256-
if (method.IsRuntimeDeterminedExactMethod)
1257-
method = method.GetCanonMethodTarget(CanonicalFormKind.Specific);
1258-
1259-
if (!factory.VTable(method.OwningType).HasKnownVirtualMethodUse)
1255+
MethodDesc canonMethod = _method.GetCanonMethodTarget(CanonicalFormKind.Specific);
1256+
if (!factory.VTable(canonMethod.OwningType).HasKnownVirtualMethodUse)
12601257
{
1261-
yield return new DependencyListEntry(factory.VirtualMethodUse(method), "Slot number");
1258+
yield return new DependencyListEntry(factory.VirtualMethodUse(canonMethod), "Slot number");
12621259
}
12631260

1264-
foreach (var dependency in factory.NativeLayout.TemplateConstructableTypes(method.OwningType))
1261+
foreach (var dependency in factory.NativeLayout.TemplateConstructableTypes(_method.OwningType))
12651262
{
12661263
yield return new DependencyListEntry(dependency, "template construction dependency");
12671264
}

‎src/tests/nativeaot/SmokeTests/UnitTests/Generics.cs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ internal static int Run()
7878
TestVariantDispatchUnconstructedTypes.Run();
7979
TestMDArrayAddressMethod.Run();
8080
TestNativeLayoutGeneration.Run();
81+
TestInterfaceDispatchTemplateDependencies.Run();
8182
TestByRefLikeVTables.Run();
8283
TestFunctionPointerLoading.Run();
8384

@@ -2426,6 +2427,49 @@ public static void Run()
24262427
}
24272428
}
24282429

2430+
class TestInterfaceDispatchTemplateDependencies
2431+
{
2432+
static Type s_atomType = typeof(Atom);
2433+
2434+
class Atom { }
2435+
class Bar<T> { }
2436+
2437+
interface IFoo<in T>
2438+
{
2439+
int Method();
2440+
}
2441+
2442+
class Foo : IFoo<object>
2443+
{
2444+
public int Method() => 42;
2445+
}
2446+
2447+
interface ITest
2448+
{
2449+
int Method();
2450+
}
2451+
2452+
class Gen<T> : ITest
2453+
{
2454+
public int Method()
2455+
{
2456+
// Use variance so only the interface dispatch cell requires the Bar<T> template.
2457+
IFoo<Bar<T>> foo = GetFoo();
2458+
return foo.Method();
2459+
}
2460+
}
2461+
2462+
[MethodImpl(MethodImplOptions.NoInlining)]
2463+
static IFoo<object> GetFoo() => new Foo();
2464+
2465+
public static void Run()
2466+
{
2467+
var instance = (ITest)Activator.CreateInstance(typeof(Gen<>).MakeGenericType(s_atomType));
2468+
if (instance.Method() != 42)
2469+
throw new Exception("Unexpected interface dispatch result.");
2470+
}
2471+
}
2472+
24292473
class TestInterfaceVTableTracking
24302474
{
24312475
class Gen<T> { }

0 commit comments

Comments
 (0)