Skip to content

Commit

Permalink
Consider method accessibility in interface resolution (#81409)
Browse files Browse the repository at this point in the history
Fixes dotnet/corert#1986 (yep, all the way to CoreRT repo).

We finally found an instance where this matters - in MAUI. Non-public methods never implement an interface by name+sig combo. We got the `ProtectedDerived` case in the newly added test wrong.
  • Loading branch information
MichalStrehovsky authored Feb 2, 2023
1 parent bab1596 commit 60fa29b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ public override bool IsFinal
}
}

public override bool IsPublic
{
get
{
return _methodDef.IsPublic;
}
}

public override bool HasCustomAttribute(string attributeNamespace, string attributeName)
{
return _methodDef.HasCustomAttribute(attributeNamespace, attributeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private static MethodDesc FindMatchingVirtualMethodOnTypeByNameAndSigWithSlotChe
return FindMatchingVirtualMethodOnTypeByNameAndSig(method, currentType, reverseMethodSearch, nameSigMatchMethodIsValidCandidate: s_VerifyMethodsHaveTheSameVirtualSlot);
}

private static Func<MethodDesc, MethodDesc, bool> s_VerifyMethodsHaveTheSameVirtualSlot = VerifyMethodsHaveTheSameVirtualSlot;
private static readonly Func<MethodDesc, MethodDesc, bool> s_VerifyMethodsHaveTheSameVirtualSlot = VerifyMethodsHaveTheSameVirtualSlot;

// Return true if the slot that defines methodToVerify matches slotDefiningMethod
private static bool VerifyMethodsHaveTheSameVirtualSlot(MethodDesc slotDefiningMethod, MethodDesc methodToVerify)
Expand All @@ -424,6 +424,14 @@ private static bool VerifyMethodsHaveTheSameVirtualSlot(MethodDesc slotDefiningM
return slotDefiningMethodOfMethodToVerify == slotDefiningMethod;
}

private static readonly Func<MethodDesc, MethodDesc, bool> s_VerifyMethodIsPublic = VerifyMethodIsPublic;

// Return true if the method to verify is public
private static bool VerifyMethodIsPublic(MethodDesc slotDefiningMethod, MethodDesc methodToVerify)
{
return methodToVerify.IsPublic;
}

private static void FindBaseUnificationGroup(MetadataType currentType, UnificationGroup unificationGroup)
{
MethodDesc originalDefiningMethod = unificationGroup.DefiningMethod;
Expand Down Expand Up @@ -615,7 +623,7 @@ private static MethodDesc ResolveInterfaceMethodToVirtualMethodOnType(MethodDesc
{
MethodDesc foundOnCurrentType = FindMatchingVirtualMethodOnTypeByNameAndSig(interfaceMethod, currentType,
reverseMethodSearch: false, /* When searching for name/sig overrides on a type that explicitly defines an interface, search through the type in the forward direction*/
nameSigMatchMethodIsValidCandidate :null);
nameSigMatchMethodIsValidCandidate: s_VerifyMethodIsPublic);
foundOnCurrentType = FindSlotDefiningMethodForVirtualMethod(foundOnCurrentType);

if (baseType == null)
Expand Down Expand Up @@ -653,7 +661,7 @@ private static MethodDesc ResolveInterfaceMethodToVirtualMethodOnType(MethodDesc
{
MethodDesc foundOnCurrentType = FindMatchingVirtualMethodOnTypeByNameAndSig(interfaceMethod, currentType,
reverseMethodSearch: false, /* When searching for name/sig overrides on a type that is the first type in the hierarchy to require the interface, search through the type in the forward direction*/
nameSigMatchMethodIsValidCandidate: null);
nameSigMatchMethodIsValidCandidate: s_VerifyMethodIsPublic);

foundOnCurrentType = FindSlotDefiningMethodForVirtualMethod(foundOnCurrentType);

Expand Down Expand Up @@ -729,7 +737,7 @@ private static MethodDesc FindNameSigOverrideForInterfaceMethodRecursive(MethodD

MethodDesc nameSigOverride = FindMatchingVirtualMethodOnTypeByNameAndSig(interfaceMethod, currentType,
reverseMethodSearch: true, /* When searching for a name sig match for an interface on parent types search in reverse order of declaration */
nameSigMatchMethodIsValidCandidate:null);
nameSigMatchMethodIsValidCandidate: s_VerifyMethodIsPublic);

if (nameSigOverride != null)
{
Expand Down
8 changes: 8 additions & 0 deletions src/coreclr/tools/Common/TypeSystem/Common/MethodDesc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,14 @@ public virtual bool IsFinal
}
}

public virtual bool IsPublic
{
get
{
return false;
}
}

public abstract bool HasCustomAttribute(string attributeNamespace, string attributeName);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ public override bool IsFinal
}
}

public override bool IsPublic
{
get
{
return _typicalMethodDef.IsPublic;
}
}

public override bool HasCustomAttribute(string attributeNamespace, string attributeName)
{
return _typicalMethodDef.HasCustomAttribute(attributeNamespace, attributeName);
Expand Down
8 changes: 8 additions & 0 deletions src/coreclr/tools/Common/TypeSystem/Ecma/EcmaMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,14 @@ public override bool IsDefaultConstructor
}
}

public override bool IsPublic
{
get
{
return Attributes.IsPublic();
}
}

public override bool IsStaticConstructor
{
get
Expand Down
41 changes: 41 additions & 0 deletions src/tests/nativeaot/SmokeTests/UnitTests/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static int Run()
if (TestIterfaceCallOptimization() == Fail)
return Fail;

TestPublicAndNonpublicDifference.Run();
TestDefaultInterfaceMethods.Run();
TestDefaultInterfaceVariance.Run();
TestVariantInterfaceOptimizations.Run();
Expand Down Expand Up @@ -477,6 +478,46 @@ private static int TestIterfaceCallOptimization()

#endregion

class TestPublicAndNonpublicDifference
{
interface IFrobber
{
string Frob();
}
class ProtectedBase : IFrobber
{
string IFrobber.Frob() => "IFrobber.Frob";
protected virtual string Frob() => "Base.Frob";
}

class ProtectedDerived : ProtectedBase, IFrobber
{
protected override string Frob() => "Derived.Frob";
}

class PublicBase : IFrobber
{
string IFrobber.Frob() => "IFrobber.Frob";
public virtual string Frob() => "Base.Frob";
}

class PublicDerived : PublicBase, IFrobber
{
public override string Frob() => "Derived.Frob";
}

public static void Run()
{
IFrobber f1 = new PublicDerived();
if (f1.Frob() != "Derived.Frob")
throw new Exception();

IFrobber f2 = new ProtectedDerived();
if (f2.Frob() != "IFrobber.Frob")
throw new Exception();
}
}

class TestDefaultInterfaceMethods
{
interface IFoo
Expand Down

0 comments on commit 60fa29b

Please sign in to comment.