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 generic dictionary lookups for param types and function pointers #85369

Merged
merged 3 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix generic dictionary lookups for param types and function pointers
Fixes #85240
  • Loading branch information
jkotas committed Apr 26, 2023
commit 59e1a926bc654838deff39eac3506d7edf8efaa9
44 changes: 34 additions & 10 deletions src/coreclr/vm/typedesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ PTR_Module TypeDesc::GetLoaderModule()
}
}

BOOL TypeDesc::IsSharedByGenericInstantiations()
{
LIMITED_METHOD_DAC_CONTRACT;

if (HasTypeParam())
{
return GetRootTypeParam().IsCanonicalSubtype();
}

if (IsFnPtr())
{
return dac_cast<PTR_FnPtrTypeDesc>(this)->IsSharedByGenericInstantiations();
}

return FALSE;
}

PTR_BaseDomain TypeDesc::GetDomain()
{
CONTRACTL
Expand Down Expand Up @@ -1634,18 +1651,26 @@ OBJECTREF TypeVarTypeDesc::GetManagedClassObject()
TypeHandle *
FnPtrTypeDesc::GetRetAndArgTypes()
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;

LIMITED_METHOD_CONTRACT;

return m_RetAndArgTypes;
} // FnPtrTypeDesc::GetRetAndArgTypes

BOOL
FnPtrTypeDesc::IsSharedByGenericInstantiations()
{
LIMITED_METHOD_DAC_CONTRACT;

for (DWORD i = 0; i <= m_NumArgs; i++)
{
if (m_RetAndArgTypes[i].IsCanonicalSubtype())
{
return TRUE;
}
}
return FALSE;
} // FnPtrTypeDesc::IsSharedByGenericInstantiations

#ifndef DACCESS_COMPILE

// Returns TRUE if all return and argument types are externally visible.
Expand All @@ -1660,10 +1685,9 @@ FnPtrTypeDesc::IsExternallyVisible() const
}
CONTRACTL_END;

const TypeHandle * rgRetAndArgTypes = GetRetAndArgTypes();
for (DWORD i = 0; i <= m_NumArgs; i++)
{
if (!rgRetAndArgTypes[i].IsExternallyVisible())
if (!m_RetAndArgTypes[i].IsExternallyVisible())
{
return FALSE;
}
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/vm/typedesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ class TypeDesc
return GetLoaderModule()->GetLoaderAllocator();
}

BOOL IsSharedByGenericInstantiations();

protected:
// See methodtable.h for details of the flags with the same name there
enum
Expand Down Expand Up @@ -524,6 +526,8 @@ class FnPtrTypeDesc : public TypeDesc
return PTR_TypeHandle(m_RetAndArgTypes);
}

BOOL IsSharedByGenericInstantiations();

#ifndef DACCESS_COMPILE
// Returns TRUE if all return and argument types are externally visible.
BOOL IsExternallyVisible() const;
Expand Down
9 changes: 6 additions & 3 deletions src/coreclr/vm/typehandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,15 @@ BOOL TypeHandle::IsSharedByGenericInstantiations() const
{
LIMITED_METHOD_DAC_CONTRACT;

if (IsArray())
if (IsTypeDesc())
{
return GetArrayElementTypeHandle().IsCanonicalSubtype();
return AsTypeDesc()->IsSharedByGenericInstantiations();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see - the previous code just ignored IsTypeDesc()

}
else if (!IsTypeDesc())
else
{
if (IsArray())
return GetArrayElementTypeHandle().IsCanonicalSubtype();

return AsMethodTable()->IsSharedByGenericInstantiations();
}

Expand Down
33 changes: 33 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_85240/test85240.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

#pragma warning disable 8500

public unsafe class Program
{
static void AssertEqual<T>(T actual, T expected)
{
if (!actual.Equals(expected))
throw new Exception($"Failed Scenario. Actual = {actual}. Expected = {expected}");
}

public static Type GrabArray<T>() => typeof(T[]);
public static Type GrabPtr<T>() => typeof(T*);
public static Type GrabFnptr<T>() => typeof(delegate*<T>);

public static int Main()
{
AssertEqual(GrabArray<int>().GetElementType(), typeof(int));
AssertEqual(GrabArray<string>().GetElementType(), typeof(string));

AssertEqual(GrabPtr<uint>().GetElementType(), typeof(uint));
AssertEqual(GrabPtr<object>().GetElementType(), typeof(object));

AssertEqual(GrabFnptr<DateTime>().GetFunctionPointerReturnType(), typeof(DateTime));
AssertEqual(GrabFnptr<Action>().GetFunctionPointerReturnType(), typeof(Action));

return 100;
}
}
8 changes: 8 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_85240/test85240.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="test85240.cs" />
</ItemGroup>
</Project>