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

Fix polluted CompareState when comparing element types in a signature #98198

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 6 additions & 6 deletions src/coreclr/vm/siginfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4201,8 +4201,6 @@ MetaSig::CompareTypeDefsUnderSubstitutions(
SigPointer inst1 = pSubst1->GetInst();
SigPointer inst2 = pSubst2->GetInst();

TokenPairList visited { pVisited };
CompareState state{ &visited };
for (DWORD i = 0; i < pTypeDef1->GetNumGenericArgs(); i++)
{
PCCOR_SIGNATURE startInst1 = inst1.GetPtr();
Expand All @@ -4211,6 +4209,8 @@ MetaSig::CompareTypeDefsUnderSubstitutions(
PCCOR_SIGNATURE startInst2 = inst2.GetPtr();
IfFailThrow(inst2.SkipExactlyOne());
PCCOR_SIGNATURE endInst2ptr = inst2.GetPtr();
TokenPairList visited{ pVisited };
CompareState state{ &visited };
if (!CompareElementType(
startInst1,
startInst2,
Expand Down Expand Up @@ -4379,8 +4379,6 @@ MetaSig::CompareMethodSigs(
IfFailThrow(CorSigUncompressData_EndPtr(pSig1, pEndSig1, &ArgCount1));
IfFailThrow(CorSigUncompressData_EndPtr(pSig2, pEndSig2, &ArgCount2));

TokenPairList visited{ pVisited };

if (ArgCount1 != ArgCount2)
{
if ((callConv & IMAGE_CEE_CS_CALLCONV_MASK) != IMAGE_CEE_CS_CALLCONV_VARARG)
Expand All @@ -4402,7 +4400,6 @@ MetaSig::CompareMethodSigs(
// to correctly handle overloads, where there are a number of varargs methods
// to pick from, like m1(int,...) and m2(int,int,...), etc.

CompareState state{ &visited };
// <= because we want to include a check of the return value!
for (i = 0; i <= ArgCount1; i++)
{
Expand Down Expand Up @@ -4434,6 +4431,8 @@ MetaSig::CompareMethodSigs(
else
{
// We are in bounds on both sides. Compare the element.
TokenPairList visited{ pVisited };
CompareState state{ &visited };
if (!CompareElementType(
pSig1,
pSig2,
Expand All @@ -4458,7 +4457,6 @@ MetaSig::CompareMethodSigs(
}

// do return type as well
CompareState state{ &visited };
for (i = 0; i <= ArgCount1; i++)
{
if (i == 0 && skipReturnTypeSig)
Expand All @@ -4473,6 +4471,8 @@ MetaSig::CompareMethodSigs(
}
else
{
TokenPairList visited{ pVisited };
CompareState state{ &visited };
if (!CompareElementType(
pSig1,
pSig2,
Expand Down
12 changes: 12 additions & 0 deletions src/tests/baseservices/typeequivalence/impl/Impls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,15 @@ public static class TestsExactTypeOptimizationsHelper
{
public static TestValueType[] s_arrayInstance;
}

public static class MethodCall
{
// Include a generic type in the method signature before the type using type equivalence to ensure that
// processing of the generic type does not affect subsequent type processing during signature comparison.
public static System.Collections.Generic.List<int> InterfaceAfterGeneric(IEmptyType t) => null;
public static System.Collections.Generic.List<int> ValueTypeAfterGeneric(TestValueType t) => null;

// Generic type after the type using type equivalence should also not affect processing.
public static void InterfaceBeforeGeneric(IEmptyType t, System.Collections.Generic.List<int> l) { }
public static void ValueTypeBeforeGeneric(TestValueType t, System.Collections.Generic.List<int> l) { }
}
20 changes: 19 additions & 1 deletion src/tests/baseservices/typeequivalence/simple/Simple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private static unsafe void TestTypeEquivalenceWithTypePunning()
}

[MethodImpl (MethodImplOptions.NoInlining)]
private static void TestLoadingValueTypesWithMethod()
private static void TestLoadingValueTypesWithMethod()
{
Console.WriteLine($"{nameof(TestLoadingValueTypesWithMethod)}");
Console.WriteLine($"-- {typeof(ValueTypeWithStaticMethod).Name}");
Expand Down Expand Up @@ -293,6 +293,23 @@ private static void TestsExactTypeOptimizations()
Assert.True(typeof(TestValueType[]) == TestsExactTypeOptimizationsHelper.s_arrayInstance.GetType());
}

private static void MethodCallSignature()
{
Console.WriteLine($"{nameof(MethodCallSignature)}");

Console.WriteLine($"-- {nameof(MethodCall.InterfaceAfterGeneric)}");
MethodCall.InterfaceAfterGeneric((IEmptyType)EmptyType2.Create());

Console.WriteLine($"-- {nameof(MethodCall.ValueTypeAfterGeneric)}");
MethodCall.ValueTypeAfterGeneric(new TestValueType());

Console.WriteLine($"-- {nameof(MethodCall.InterfaceBeforeGeneric)}");
MethodCall.InterfaceBeforeGeneric((IEmptyType)EmptyType2.Create(), null);

Console.WriteLine($"-- {nameof(MethodCall.ValueTypeBeforeGeneric)}");
MethodCall.ValueTypeBeforeGeneric(new TestValueType(), null);
}

[Fact]
public static int TestEntryPoint()
{
Expand All @@ -314,6 +331,7 @@ public static int TestEntryPoint()
TestLoadingValueTypesWithMethod();
TestCastsOptimizations();
TestsExactTypeOptimizations();
MethodCallSignature();
}
catch (Exception e)
{
Expand Down
Loading