Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix handling of IDynamicInterfaceCastable wrt CastCache #108328

Merged
merged 1 commit into from
Sep 28, 2024
Merged
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
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
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
22 changes: 22 additions & 0 deletions src/tests/nativeaot/SmokeTests/UnitTests/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static int Run()
TestVariantInterfaceOptimizations.Run();
TestSharedInterfaceMethods.Run();
TestGenericAnalysis.Run();
TestRuntime108229Regression.Run();
TestCovariantReturns.Run();
TestDynamicInterfaceCastable.Run();
TestStaticInterfaceMethodsAnalysis.Run();
Expand Down Expand Up @@ -705,6 +706,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
Expand Down