Skip to content

Commit

Permalink
Remove Helper Method Frames (HMF) from Reflection (dotnet#110377)
Browse files Browse the repository at this point in the history
Remove HMF from RuntimeFieldHandle.GetLoaderAllocator() and RuntimeMethodHandle.GetLoaderAllocator().
Convert RuntimeTypeHandle.IsEquivalentTo() to QCall.
Convert RuntimeTypeHandle.GetMethodAt() to QCall.
Remove HMF from Signature.GetParameterOffset().
Clean up TypeEquivalence feature defines.
Convert RuntimeTypeHandle.GetArgumentTypesFromFunctionPointer() to QCall.
---------
Co-authored-by: Jan Kotas <[email protected]>
  • Loading branch information
AaronRobinsonMSFT authored Dec 5, 2024
1 parent e99557b commit 223dea5
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ internal static void ThrowInvalidCastException(object fromType, void* toTypeHnd)
mt = mt->ParentMethodTable;
}

#if FEATURE_TYPEEQUIVALENCE
// this helper is not supposed to be used with type-equivalent "to" type.
Debug.Assert(!((MethodTable*)toTypeHnd)->HasTypeEquivalence);
#endif // FEATURE_TYPEEQUIVALENCE

obj = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,9 @@ internal unsafe struct MethodTable
private const uint enum_flag_ContainsGCPointers = 0x01000000;
private const uint enum_flag_ContainsGenericVariables = 0x20000000;
private const uint enum_flag_HasComponentSize = 0x80000000;
#if FEATURE_TYPEEQUIVALENCE
private const uint enum_flag_HasTypeEquivalence = 0x02000000;
#endif // FEATURE_TYPEEQUIVALENCE
private const uint enum_flag_HasFinalizer = 0x00100000;
private const uint enum_flag_Category_Mask = 0x000F0000;
private const uint enum_flag_Category_ValueType = 0x00040000;
Expand Down
74 changes: 65 additions & 9 deletions src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,35 @@ public ModuleHandle GetModuleHandle()
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern int GetToken(RuntimeType type);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern RuntimeMethodHandleInternal GetMethodAt(RuntimeType type, int slot);
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeTypeHandle_GetMethodAt")]
private static unsafe partial IntPtr GetMethodAt(MethodTable* pMT, int slot);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Type[] GetArgumentTypesFromFunctionPointer(RuntimeType type);
internal static RuntimeMethodHandleInternal GetMethodAt(RuntimeType type, int slot)
{
TypeHandle typeHandle = type.GetNativeTypeHandle();
if (typeHandle.IsTypeDesc)
{
throw new ArgumentException(SR.Arg_InvalidHandle);
}

if (slot < 0)
{
throw new ArgumentException(SR.Arg_ArgumentOutOfRangeException);
}

return new RuntimeMethodHandleInternal(GetMethodAt(typeHandle.AsMethodTable(), slot));
}

internal static Type[] GetArgumentTypesFromFunctionPointer(RuntimeType type)
{
Debug.Assert(type.IsFunctionPointer);
Type[]? argTypes = null;
GetArgumentTypesFromFunctionPointer(new QCallTypeHandle(ref type), ObjectHandleOnStack.Create(ref argTypes));
return argTypes!;
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeTypeHandle_GetArgumentTypesFromFunctionPointer")]
private static partial void GetArgumentTypesFromFunctionPointer(QCallTypeHandle type, ObjectHandleOnStack argTypes);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool IsUnmanagedFunctionPointer(RuntimeType type);
Expand Down Expand Up @@ -796,8 +820,11 @@ public void GetObjectData(SerializationInfo info, StreamingContext context)
}

#if FEATURE_TYPEEQUIVALENCE
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool IsEquivalentTo(RuntimeType rtType1, RuntimeType rtType2);
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeTypeHandle_IsEquivalentTo")]
private static partial Interop.BOOL IsEquivalentTo(QCallTypeHandle rtType1, QCallTypeHandle rtType2);

internal static bool IsEquivalentTo(RuntimeType rtType1, RuntimeType rtType2)
=> IsEquivalentTo(new QCallTypeHandle(ref rtType1), new QCallTypeHandle(ref rtType2)) == Interop.BOOL.TRUE;
#endif // FEATURE_TYPEEQUIVALENCE
}

Expand Down Expand Up @@ -1213,7 +1240,17 @@ internal static IRuntimeMethodInfo StripMethodInstantiation(IRuntimeMethodInfo m
internal static extern bool IsConstructor(RuntimeMethodHandleInternal method);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern LoaderAllocator GetLoaderAllocator(RuntimeMethodHandleInternal method);
private static extern LoaderAllocator GetLoaderAllocatorInternal(RuntimeMethodHandleInternal method);

internal static LoaderAllocator GetLoaderAllocator(RuntimeMethodHandleInternal method)
{
if (method.IsNullHandle())
{
throw new ArgumentNullException(SR.Arg_InvalidHandle);
}

return GetLoaderAllocatorInternal(method);
}
}

// This type is used to remove the expense of having a managed reference object that is dynamically
Expand Down Expand Up @@ -1519,7 +1556,17 @@ internal static void SetValueDirect(RtFieldInfo field, RuntimeType fieldType, Ty
internal static extern bool AcquiresContextFromThis(RuntimeFieldHandleInternal field);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern LoaderAllocator GetLoaderAllocator(RuntimeFieldHandleInternal method);
private static extern LoaderAllocator GetLoaderAllocatorInternal(RuntimeFieldHandleInternal field);

internal static LoaderAllocator GetLoaderAllocator(RuntimeFieldHandleInternal field)
{
if (field.IsNullHandle())
{
throw new ArgumentNullException(SR.Arg_InvalidHandle);
}

return GetLoaderAllocatorInternal(field);
}

// ISerializable interface
[Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
Expand Down Expand Up @@ -1883,7 +1930,16 @@ internal Type[] GetCustomModifiers(int parameterIndex, bool required) =>
GetCustomModifiersAtOffset(GetParameterOffset(parameterIndex), required);

[MethodImpl(MethodImplOptions.InternalCall)]
internal extern int GetParameterOffset(int parameterIndex);
private static extern unsafe int GetParameterOffsetInternal(void* sig, int csig, int parameterIndex);

internal int GetParameterOffset(int parameterIndex)
{
int offsetMaybe = GetParameterOffsetInternal(m_sig, m_csig, parameterIndex);
// If the result is negative, it is an error code.
if (offsetMaybe < 0)
Marshal.ThrowExceptionForHR(offsetMaybe, new IntPtr(-1));
return offsetMaybe;
}

[MethodImpl(MethodImplOptions.InternalCall)]
internal extern int GetTypeParameterOffset(int offset, int index);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/clr.featuredefines.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<FeatureCoreCLR>true</FeatureCoreCLR>
<FeatureEventTrace>true</FeatureEventTrace>
<FeaturePerfTracing>true</FeaturePerfTracing>
<FeatureTypeEquivalence>true</FeatureTypeEquivalence>
<ProfilingSupportedBuild>true</ProfilingSupportedBuild>
</PropertyGroup>

Expand All @@ -16,6 +15,7 @@
<FeatureComWrappers>true</FeatureComWrappers>
<FeatureCominterop>true</FeatureCominterop>
<FeatureCominteropApartmentSupport>true</FeatureCominteropApartmentSupport>
<FeatureTypeEquivalence>true</FeatureTypeEquivalence>
<FeatureIjw>true</FeatureIjw>
</PropertyGroup>

Expand Down
7 changes: 4 additions & 3 deletions src/coreclr/clrdefinitions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ if(FEATURE_COMWRAPPERS)
add_compile_definitions(FEATURE_COMWRAPPERS)
endif(FEATURE_COMWRAPPERS)

if(FEATURE_TYPEEQUIVALENCE)
add_compile_definitions(FEATURE_TYPEEQUIVALENCE)
endif(FEATURE_TYPEEQUIVALENCE)

if(FEATURE_OBJCMARSHAL)
add_compile_definitions(FEATURE_OBJCMARSHAL)
endif()
Expand All @@ -180,9 +184,6 @@ if (CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARG
add_compile_definitions(FEATURE_ON_STACK_REPLACEMENT)
endif (CLR_CMAKE_TARGET_ARCH_AMD64 OR CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_LOONGARCH64 OR CLR_CMAKE_TARGET_ARCH_RISCV64)
add_compile_definitions(FEATURE_PGO)
if (CLR_CMAKE_TARGET_WIN32)
add_definitions(-DFEATURE_TYPEEQUIVALENCE)
endif(CLR_CMAKE_TARGET_WIN32)
if (CLR_CMAKE_TARGET_ARCH_AMD64)
# Enable the AMD64 Unix struct passing JIT-EE interface for all AMD64 platforms, to enable altjit.
add_definitions(-DUNIX_AMD64_ABI_ITF)
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/clrfeatures.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ endif()
if (CLR_CMAKE_TARGET_APPLE)
set(FEATURE_OBJCMARSHAL 1)
endif()

if (CLR_CMAKE_TARGET_WIN32)
set(FEATURE_TYPEEQUIVALENCE 1)
endif(CLR_CMAKE_TARGET_WIN32)
2 changes: 1 addition & 1 deletion src/coreclr/utilcode/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void CHECK::Trigger(LPCSTR reason)
pMessage->AppendASCII((m_message != (LPCSTR)1) ? m_message : "<runtime check failure>");

#if _DEBUG
pMessage->AppendASCII("FAILED: ");
pMessage->AppendASCII("\nFAILED: ");
pMessage->AppendASCII(m_condition);
#endif

Expand Down
9 changes: 3 additions & 6 deletions src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ FCFuncStart(gCOMTypeHandleFuncs)
FCFuncElement("GetArrayRank", RuntimeTypeHandle::GetArrayRank)
FCFuncElement("GetToken", RuntimeTypeHandle::GetToken)
FCFuncElement("GetUtf8NameInternal", RuntimeTypeHandle::GetUtf8Name)
FCFuncElement("GetMethodAt", RuntimeTypeHandle::GetMethodAt)
FCFuncElement("GetFields", RuntimeTypeHandle::GetFields)
FCFuncElement("GetInterfaces", RuntimeTypeHandle::GetInterfaces)
FCFuncElement("GetAttributes", RuntimeTypeHandle::GetAttributes)
Expand All @@ -101,10 +100,8 @@ FCFuncStart(gCOMTypeHandleFuncs)
FCFuncElement("IsGenericVariable", RuntimeTypeHandle::IsGenericVariable)
FCFuncElement("ContainsGenericVariables", RuntimeTypeHandle::ContainsGenericVariables)
FCFuncElement("SatisfiesConstraints", RuntimeTypeHandle::SatisfiesConstraints)
FCFuncElement("GetArgumentTypesFromFunctionPointer", RuntimeTypeHandle::GetArgumentTypesFromFunctionPointer)
FCFuncElement("IsUnmanagedFunctionPointer", RuntimeTypeHandle::IsUnmanagedFunctionPointer)
FCFuncElement("CompareCanonicalHandles", RuntimeTypeHandle::CompareCanonicalHandles)
FCFuncElement("IsEquivalentTo", RuntimeTypeHandle::IsEquivalentTo)
FCFuncElement("GetRuntimeTypeFromHandleIfExists", RuntimeTypeHandle::GetRuntimeTypeFromHandleIfExists)
FCFuncEnd()

Expand Down Expand Up @@ -139,7 +136,7 @@ FCFuncEnd()
FCFuncStart(gSignatureNative)
FCFuncElement("GetSignature", SignatureNative::GetSignature)
FCFuncElement("CompareSig", SignatureNative::CompareSig)
FCFuncElement("GetParameterOffset", SignatureNative::GetParameterOffset)
FCFuncElement("GetParameterOffsetInternal", SignatureNative::GetParameterOffsetInternal)
FCFuncElement("GetTypeParameterOffset", SignatureNative::GetTypeParameterOffset)
FCFuncElement("GetCustomModifiersAtOffset", SignatureNative::GetCustomModifiersAtOffset)
FCFuncElement("GetCallingConventionFromFunctionPointerAtOffset", SignatureNative::GetCallingConventionFromFunctionPointerAtOffset)
Expand All @@ -162,7 +159,7 @@ FCFuncStart(gRuntimeMethodHandle)
FCFuncElement("GetMethodBody", RuntimeMethodHandle::GetMethodBody)
FCFuncElement("IsConstructor", RuntimeMethodHandle::IsConstructor)
FCFuncElement("GetResolver", RuntimeMethodHandle::GetResolver)
FCFuncElement("GetLoaderAllocator", RuntimeMethodHandle::GetLoaderAllocator)
FCFuncElement("GetLoaderAllocatorInternal", RuntimeMethodHandle::GetLoaderAllocatorInternal)
FCFuncEnd()

FCFuncStart(gCOMFieldHandleNewFuncs)
Expand All @@ -172,7 +169,7 @@ FCFuncStart(gCOMFieldHandleNewFuncs)
FCFuncElement("GetToken", RuntimeFieldHandle::GetToken)
FCFuncElement("GetStaticFieldForGenericType", RuntimeFieldHandle::GetStaticFieldForGenericType)
FCFuncElement("AcquiresContextFromThis", RuntimeFieldHandle::AcquiresContextFromThis)
FCFuncElement("GetLoaderAllocator", RuntimeFieldHandle::GetLoaderAllocator)
FCFuncElement("GetLoaderAllocatorInternal", RuntimeFieldHandle::GetLoaderAllocatorInternal)
FCFuncElement("IsFastPathSupported", RuntimeFieldHandle::IsFastPathSupported)
FCFuncElement("GetInstanceFieldOffset", RuntimeFieldHandle::GetInstanceFieldOffset)
FCFuncElement("GetStaticFieldAddress", RuntimeFieldHandle::GetStaticFieldAddress)
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/vm/qcallentrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ static const Entry s_QCall[] =
DllImportEntry(RuntimeTypeHandle_MakeArray)
DllImportEntry(RuntimeTypeHandle_IsCollectible)
DllImportEntry(RuntimeTypeHandle_GetConstraints)
DllImportEntry(RuntimeTypeHandle_GetArgumentTypesFromFunctionPointer)
DllImportEntry(RuntimeTypeHandle_GetAssemblySlow)
DllImportEntry(RuntimeTypeHandle_GetModuleSlow)
DllImportEntry(RuntimeTypeHandle_GetNumVirtualsAndStaticVirtuals)
DllImportEntry(RuntimeTypeHandle_GetMethodAt)
DllImportEntry(RuntimeTypeHandle_VerifyInterfaceIsImplemented)
DllImportEntry(RuntimeTypeHandle_GetInterfaceMethodImplementation)
DllImportEntry(RuntimeTypeHandle_GetDeclaringTypeHandleForGenericVariable)
Expand All @@ -137,6 +139,9 @@ static const Entry s_QCall[] =
DllImportEntry(RuntimeTypeHandle_AllocateComObject)
#endif // FEATURE_COMINTEROP
DllImportEntry(RuntimeTypeHandle_GetRuntimeTypeFromHandleSlow)
#ifdef FEATURE_TYPEEQUIVALENCE
DllImportEntry(RuntimeTypeHandle_IsEquivalentTo)
#endif // FEATURE_TYPEEQUIVALENCE
DllImportEntry(RuntimeTypeHandle_CreateInstanceForAnotherGenericParameter)
DllImportEntry(RuntimeTypeHandle_InternalAlloc)
DllImportEntry(RuntimeTypeHandle_InternalAllocNoChecks)
Expand Down
Loading

0 comments on commit 223dea5

Please sign in to comment.