Skip to content
Closed
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
11 changes: 10 additions & 1 deletion src/coreclr/vm/genmeth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,19 @@ InstantiatedMethodDesc::NewInstantiatedMethodDesc(MethodTable *pExactMT,
// The canonical instantiation is exempt from constraint checks. It's used as the basis
// for all other reference instantiations so we can't not load it. The Canon type is
// not visible to users so it can't be abused.
//
// Instantiations containing generic variables (type parameters) are also exempt.
// Such instantiations arise during JIT access checks when the caller is a generic
// method and the EE resolves method specs using the caller's formal type parameters.
// Constraint checking for these instantiations would unnecessarily load types
// instantiated over the type variables (e.g. IList<TMethod>). The actual constraint
// check will happen when the method is instantiated with concrete types.

BOOL fExempt =
TypeHandle::IsCanonicalSubtypeInstantiation(methodInst) ||
TypeHandle::IsCanonicalSubtypeInstantiation(pNewMD->GetClassInstantiation());
TypeHandle::IsCanonicalSubtypeInstantiation(pNewMD->GetClassInstantiation()) ||
MethodTable::ComputeContainsGenericVariables(methodInst) ||
MethodTable::ComputeContainsGenericVariables(pNewMD->GetClassInstantiation());

if (!fExempt)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Regression test: access checks triggered by the JIT for generic methods with
// constraints should not unnecessarily load types instantiated over type variables.
// The JIT uses the typical method definition (with formal type params) when doing
// security checks for generic callers, which was triggering constraint checks that
// loaded types like IList<TMethod> where TMethod is a type variable.

using System;
using System.Collections.Generic;
using Xunit;

public class TypeLoadWithGenericVars
{
static void Method<TMethod1_1, TMethod1_2>() where TMethod1_1 : IList<TMethod1_2>
{
Method2<TMethod1_1, TMethod1_2>();
}

static void Method2<TMethod2_1, TMethod2_2>() where TMethod2_1 : IList<TMethod2_2>
{
}

static void MethodA<T1, T2, T3>()
where T1 : IList<T2>
where T2 : IList<T3>
{
MethodB<T1, T2, T3>();
}

static void MethodB<U1, U2, U3>()
where U1 : IList<U2>
where U2 : IList<U3>
{
}

[Fact]
public static void TestEntryPoint()
{
// Call with concrete types that satisfy the constraints.
// The JIT will use the typical method definition for access checks,
// which previously caused unnecessary loading of IList<TMethod1_2>, etc.
Method<List<int>, int>();
Method<int[], int>();

// Test with chained generic constraints
MethodA<List<List<int>>, List<int>, int>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<CLRTestPriority>1</CLRTestPriority>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(TestLibraryProjectPath)" />
</ItemGroup>
</Project>
Loading