Skip to content

[release/9.0-staging] Fix handling of IDynamicInterfaceCastable wrt CastCache #110007

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

Merged
Merged
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
31 changes: 15 additions & 16 deletions src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/TypeCast.cs
Original file line number Diff line number Diff line change
@@ -1066,10 +1066,14 @@ private static unsafe bool CacheMiss(MethodTable* pSourceType, MethodTable* pTar
bool result = TypeCast.AreTypesAssignableInternalUncached(pSourceType, pTargetType, variation, &newList);

//
// Update the cache
// Update the cache. We only consider type-based conversion rules here.
// Therefore a negative result cannot rule out convertibility for IDynamicInterfaceCastable.
//
nuint sourceAndVariation = (nuint)pSourceType + (uint)variation;
s_castCache.TrySet(sourceAndVariation, (nuint)pTargetType, result);
if (result || !(pTargetType->IsInterface && pSourceType->IsIDynamicInterfaceCastable))
{
nuint sourceAndVariation = (nuint)pSourceType + (uint)variation;
s_castCache.TrySet(sourceAndVariation, (nuint)pTargetType, result);
}

return result;
}
@@ -1252,13 +1256,11 @@ internal static unsafe bool AreTypesAssignableInternalUncached(MethodTable* pSou
}

//
// Update the cache
// Update the cache. We only consider type-based conversion rules here.
// Therefore a negative result cannot rule out convertibility for IDynamicInterfaceCastable.
//
if (!pSourceType->IsIDynamicInterfaceCastable)
if (retObj != null || !(pTargetType->IsInterface && pSourceType->IsIDynamicInterfaceCastable))
{
//
// Update the cache
//
nuint sourceAndVariation = (nuint)pSourceType + (uint)AssignmentVariation.BoxedSource;
s_castCache.TrySet(sourceAndVariation, (nuint)pTargetType, retObj != null);
}
@@ -1293,14 +1295,11 @@ private static unsafe object CheckCastAny_NoCacheLookup(MethodTable* pTargetType
obj = CheckCastClass(pTargetType, obj);
}

if (!pSourceType->IsIDynamicInterfaceCastable)
{
//
// Update the cache
//
nuint sourceAndVariation = (nuint)pSourceType + (uint)AssignmentVariation.BoxedSource;
s_castCache.TrySet(sourceAndVariation, (nuint)pTargetType, true);
}
//
// Update the cache
//
nuint sourceAndVariation = (nuint)pSourceType + (uint)AssignmentVariation.BoxedSource;
s_castCache.TrySet(sourceAndVariation, (nuint)pTargetType, true);

return obj;
}
22 changes: 22 additions & 0 deletions src/tests/nativeaot/SmokeTests/UnitTests/Interfaces.cs
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ public static int Run()
TestVariantInterfaceOptimizations.Run();
TestSharedInterfaceMethods.Run();
TestGenericAnalysis.Run();
TestRuntime108229Regression.Run();
TestCovariantReturns.Run();
TestDynamicInterfaceCastable.Run();
TestStaticInterfaceMethodsAnalysis.Run();
@@ -738,6 +739,27 @@ public static void Run()
}
}

class TestRuntime108229Regression
{
class Shapeshifter : IDynamicInterfaceCastable
{
public RuntimeTypeHandle GetInterfaceImplementation(RuntimeTypeHandle interfaceType) => throw new NotImplementedException();
public bool IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented) => true;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static bool Is(object o) => o is IEnumerable<object>;

public static void Run()
{
object o = new Shapeshifter();

// Call multiple times in case we just flushed the cast cache (when we flush we don't store).
if (!Is(o) || !Is(o) || !Is(o))
throw new Exception();
}
}

class TestCovariantReturns
{
interface IFoo