Skip to content

Commit a5d13e2

Browse files
Delete more code around reflection blocking (#86139)
We had a bunch of code to carefully compute names in situations where the metadata/names might not be available. Such situations no longer exist.
1 parent b05b4e4 commit a5d13e2

File tree

18 files changed

+31
-250
lines changed

18 files changed

+31
-250
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionDomain.cs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -117,28 +117,19 @@ public Type GetNamedTypeForHandle(RuntimeTypeHandle typeHandle)
117117

118118
public Type GetArrayTypeForHandle(RuntimeTypeHandle typeHandle)
119119
{
120-
RuntimeTypeHandle elementTypeHandle;
121-
if (!ExecutionEnvironment.TryGetArrayTypeElementType(typeHandle, out elementTypeHandle))
122-
throw CreateMissingMetadataException((Type?)null);
123-
120+
RuntimeTypeHandle elementTypeHandle = ExecutionEnvironment.GetArrayTypeElementType(typeHandle);
124121
return elementTypeHandle.GetTypeForRuntimeTypeHandle().GetArrayType(typeHandle);
125122
}
126123

127124
public Type GetMdArrayTypeForHandle(RuntimeTypeHandle typeHandle, int rank)
128125
{
129-
RuntimeTypeHandle elementTypeHandle;
130-
if (!ExecutionEnvironment.TryGetArrayTypeElementType(typeHandle, out elementTypeHandle))
131-
throw CreateMissingMetadataException((Type?)null);
132-
126+
RuntimeTypeHandle elementTypeHandle = ExecutionEnvironment.GetArrayTypeElementType(typeHandle);
133127
return elementTypeHandle.GetTypeForRuntimeTypeHandle().GetMultiDimArrayType(rank, typeHandle);
134128
}
135129

136130
public Type GetPointerTypeForHandle(RuntimeTypeHandle typeHandle)
137131
{
138-
RuntimeTypeHandle targetTypeHandle;
139-
if (!ExecutionEnvironment.TryGetPointerTypeTargetType(typeHandle, out targetTypeHandle))
140-
throw CreateMissingMetadataException((Type?)null);
141-
132+
RuntimeTypeHandle targetTypeHandle = ExecutionEnvironment.GetPointerTypeTargetType(typeHandle);
142133
return targetTypeHandle.GetTypeForRuntimeTypeHandle().GetPointerType(typeHandle);
143134
}
144135

@@ -161,10 +152,7 @@ public Type GetFunctionPointerTypeForHandle(RuntimeTypeHandle typeHandle)
161152

162153
public Type GetByRefTypeForHandle(RuntimeTypeHandle typeHandle)
163154
{
164-
RuntimeTypeHandle targetTypeHandle;
165-
if (!ExecutionEnvironment.TryGetByRefTypeTargetType(typeHandle, out targetTypeHandle))
166-
throw CreateMissingMetadataException((Type?)null);
167-
155+
RuntimeTypeHandle targetTypeHandle = ExecutionEnvironment.GetByRefTypeTargetType(typeHandle);
168156
return targetTypeHandle.GetTypeForRuntimeTypeHandle().GetByRefType(typeHandle);
169157
}
170158

@@ -187,7 +175,7 @@ public Type GetConstructedGenericTypeForHandle(RuntimeTypeHandle typeHandle)
187175
//=======================================================================================
188176
// Missing metadata exceptions.
189177
//=======================================================================================
190-
public Exception CreateMissingMetadataException(Type? pertainant)
178+
public Exception CreateMissingMetadataException(Type pertainant)
191179
{
192180
return this.ReflectionDomainSetup.CreateMissingMetadataException(pertainant);
193181
}
@@ -197,16 +185,6 @@ public Exception CreateNonInvokabilityException(MemberInfo pertainant)
197185
return this.ReflectionDomainSetup.CreateNonInvokabilityException(pertainant);
198186
}
199187

200-
public Exception CreateMissingArrayTypeException(Type elementType, bool isMultiDim, int rank)
201-
{
202-
return ReflectionDomainSetup.CreateMissingArrayTypeException(elementType, isMultiDim, rank);
203-
}
204-
205-
public Exception CreateMissingConstructedGenericTypeException(Type genericTypeDefinition, Type[] genericTypeArguments)
206-
{
207-
return ReflectionDomainSetup.CreateMissingConstructedGenericTypeException(genericTypeDefinition, genericTypeArguments);
208-
}
209-
210188
//=======================================================================================
211189
// Miscellaneous.
212190
//=======================================================================================

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionEnvironment.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public abstract class ExecutionEnvironment
4646
public abstract IEnumerable<RuntimeTypeHandle> TryGetImplementedInterfaces(RuntimeTypeHandle typeHandle);
4747
public abstract void VerifyInterfaceIsImplemented(RuntimeTypeHandle typeHandle, RuntimeTypeHandle ifaceHandle);
4848
public abstract void GetInterfaceMap(Type instanceType, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType, out MethodInfo[] interfaceMethods, out MethodInfo[] targetMethods);
49-
public abstract string GetLastResortString(RuntimeTypeHandle typeHandle);
5049

5150
//==============================================================================================
5251
// Reflection Mapping Tables
@@ -55,18 +54,18 @@ public abstract class ExecutionEnvironment
5554
public abstract bool TryGetNamedTypeForMetadata(QTypeDefinition qTypeDefinition, out RuntimeTypeHandle runtimeTypeHandle);
5655

5756
public abstract bool TryGetArrayTypeForElementType(RuntimeTypeHandle elementTypeHandle, out RuntimeTypeHandle arrayTypeHandle);
58-
public abstract bool TryGetArrayTypeElementType(RuntimeTypeHandle arrayTypeHandle, out RuntimeTypeHandle elementTypeHandle);
57+
public abstract RuntimeTypeHandle GetArrayTypeElementType(RuntimeTypeHandle arrayTypeHandle);
5958

6059
public abstract bool TryGetMultiDimArrayTypeForElementType(RuntimeTypeHandle elementTypeHandle, int rank, out RuntimeTypeHandle arrayTypeHandle);
6160

6261
public abstract bool TryGetFunctionPointerTypeForComponents(RuntimeTypeHandle returnTypeHandle, RuntimeTypeHandle[] parameterHandles, bool isUnmanaged, out RuntimeTypeHandle functionPointerTypeHandle);
6362
public abstract void GetFunctionPointerTypeComponents(RuntimeTypeHandle functionPointerHandle, out RuntimeTypeHandle returnTypeHandle, out RuntimeTypeHandle[] parameterHandles, out bool isUnmanaged);
6463

6564
public abstract bool TryGetPointerTypeForTargetType(RuntimeTypeHandle targetTypeHandle, out RuntimeTypeHandle pointerTypeHandle);
66-
public abstract bool TryGetPointerTypeTargetType(RuntimeTypeHandle pointerTypeHandle, out RuntimeTypeHandle targetTypeHandle);
65+
public abstract RuntimeTypeHandle GetPointerTypeTargetType(RuntimeTypeHandle pointerTypeHandle);
6766

6867
public abstract bool TryGetByRefTypeForTargetType(RuntimeTypeHandle targetTypeHandle, out RuntimeTypeHandle byRefTypeHandle);
69-
public abstract bool TryGetByRefTypeTargetType(RuntimeTypeHandle byRefTypeHandle, out RuntimeTypeHandle targetTypeHandle);
68+
public abstract RuntimeTypeHandle GetByRefTypeTargetType(RuntimeTypeHandle byRefTypeHandle);
7069

7170
public abstract bool TryGetConstructedGenericTypeForComponents(RuntimeTypeHandle genericTypeDefinitionHandle, RuntimeTypeHandle[] genericTypeArgumentHandles, out RuntimeTypeHandle runtimeTypeHandle);
7271
public abstract bool TryGetConstructedGenericTypeForComponentsNoConstraintCheck(RuntimeTypeHandle genericTypeDefinitionHandle, RuntimeTypeHandle[] genericTypeArgumentHandles, out RuntimeTypeHandle runtimeTypeHandle);

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/ReflectionDomainSetup.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ public abstract class ReflectionDomainSetup
1313
{
1414
protected ReflectionDomainSetup() { }
1515
public abstract AssemblyBinder AssemblyBinder { get; }
16-
public abstract Exception CreateMissingMetadataException(Type? pertainant);
16+
public abstract Exception CreateMissingMetadataException(Type pertainant);
1717
public abstract Exception CreateNonInvokabilityException(MemberInfo pertainant);
18-
public abstract Exception CreateMissingArrayTypeException(Type elementType, bool isMultiDim, int rank);
19-
public abstract Exception CreateMissingConstructedGenericTypeException(Type genericTypeDefinition, Type[] genericTypeArguments);
2018
}
2119
}

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/ReflectionExecutionDomainCallbacks.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public abstract class ReflectionExecutionDomainCallbacks
4141
// Flotsam and jetsam.
4242
public abstract Exception CreateMissingMetadataException(Type typeWithMissingMetadata);
4343

44-
public abstract string GetBetterDiagnosticInfoIfAvailable(RuntimeTypeHandle runtimeTypeHandle);
4544
public abstract MethodBase GetMethodBaseFromStartAddressIfAvailable(IntPtr methodStartAddress);
4645
public abstract Assembly GetAssemblyForHandle(RuntimeTypeHandle typeHandle);
4746

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -914,11 +914,6 @@ public static Delegate CreateObjectArrayDelegate(Type delegateType, Func<object?
914914
return Delegate.CreateObjectArrayDelegate(delegateType, invoker);
915915
}
916916

917-
public static string GetLastResortString(RuntimeTypeHandle typeHandle)
918-
{
919-
return typeHandle.LastResortToString;
920-
}
921-
922917
public static IntPtr RhHandleAlloc(object value, GCHandleType type)
923918
{
924919
return RuntimeImports.RhHandleAlloc(value, type);

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/RuntimeInteropData.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ public static uint GetStructFieldOffset(RuntimeTypeHandle structureTypeHandle, s
2020
return offset;
2121
}
2222

23+
Type structureType = Type.GetTypeFromHandle(structureTypeHandle)!;
24+
2325
// if we can find the struct but couldn't find its field, throw Argument Exception
2426
if (structExists)
2527
{
26-
throw new ArgumentException(SR.Format(SR.Argument_OffsetOfFieldNotFound, RuntimeAugments.GetLastResortString(structureTypeHandle)), nameof(fieldName));
28+
throw new ArgumentException(SR.Format(SR.Argument_OffsetOfFieldNotFound, structureType), nameof(fieldName));
2729
}
2830

29-
throw new NotSupportedException(SR.Format(SR.StructMarshalling_MissingInteropData, Type.GetTypeFromHandle(structureTypeHandle)));
31+
throw new NotSupportedException(SR.Format(SR.StructMarshalling_MissingInteropData, structureType));
3032
}
3133

3234
public static int GetStructUnsafeStructSize(RuntimeTypeHandle structureTypeHandle)

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/Helpers.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ public static RuntimeTypeInfo[] ToRuntimeTypeInfoArray(this Type[] types)
6262
return typeInfos;
6363
}
6464

65-
public static string LastResortString(this RuntimeTypeHandle typeHandle)
66-
{
67-
return ReflectionCoreExecution.ExecutionEnvironment.GetLastResortString(typeHandle);
68-
}
69-
7065
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7166
public static RuntimeNamedTypeInfo CastToRuntimeNamedTypeInfo(this Type type)
7267
{

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/TypeUnifier.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private static RuntimeArrayTypeInfo WithVerifiedTypeHandle(this RuntimeArrayType
7070
&& !(elementType is RuntimeCLSIDTypeInfo)
7171
#endif
7272
)
73-
throw ReflectionCoreExecution.ExecutionDomain.CreateMissingArrayTypeException(elementType, isMultiDim: false, 1);
73+
throw ReflectionCoreExecution.ExecutionDomain.CreateMissingMetadataException(arrayType);
7474

7575
return arrayType;
7676
}
@@ -109,7 +109,7 @@ private static RuntimeConstructedGenericTypeInfo WithVerifiedTypeHandle(this Run
109109
atLeastOneOpenType = true;
110110
}
111111
if (!atLeastOneOpenType)
112-
throw ReflectionCoreExecution.ExecutionDomain.CreateMissingConstructedGenericTypeException(genericType.GetGenericTypeDefinition(), genericTypeArguments.CloneTypeArray());
112+
throw ReflectionCoreExecution.ExecutionDomain.CreateMissingMetadataException(genericType);
113113
}
114114

115115
return genericType;

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/RuntimeConstructedGenericTypeInfo.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,7 @@ internal sealed override RuntimeNamedTypeInfo AnchoringTypeDefinitionForDeclared
213213
{
214214
get
215215
{
216-
RuntimeTypeInfo genericTypeDefinition = this.GenericTypeDefinitionTypeInfo;
217-
if (!(genericTypeDefinition is RuntimeNamedTypeInfo genericTypeDefinitionNamedTypeInfo))
218-
throw ReflectionCoreExecution.ExecutionDomain.CreateMissingMetadataException(genericTypeDefinition);
219-
return genericTypeDefinitionNamedTypeInfo;
216+
return (RuntimeNamedTypeInfo)GenericTypeDefinitionTypeInfo;
220217
}
221218
}
222219

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NativeAot.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public static unsafe void DestroyStructure(IntPtr ptr, Type structuretype)
117117
structureTypeHandle.IsInterface() ||
118118
InteropExtensions.AreTypesAssignable(typeof(Delegate).TypeHandle, structureTypeHandle))
119119
{
120-
throw new ArgumentException(SR.Format(SR.Argument_MustHaveLayoutOrBeBlittable, structureTypeHandle.LastResortToString));
120+
throw new ArgumentException(SR.Format(SR.Argument_MustHaveLayoutOrBeBlittable, structuretype));
121121
}
122122

123123
if (structureTypeHandle.IsBlittable())
@@ -128,7 +128,7 @@ public static unsafe void DestroyStructure(IntPtr ptr, Type structuretype)
128128

129129
IntPtr destroyStructureStub = RuntimeInteropData.GetDestroyStructureStub(structureTypeHandle, out bool hasInvalidLayout);
130130
if (hasInvalidLayout)
131-
throw new ArgumentException(SR.Format(SR.Argument_MustHaveLayoutOrBeBlittable, structureTypeHandle.LastResortToString));
131+
throw new ArgumentException(SR.Format(SR.Argument_MustHaveLayoutOrBeBlittable, structuretype));
132132
// DestroyStructureStub == IntPtr.Zero means its fields don't need to be destroyed
133133
if (destroyStructureStub != IntPtr.Zero)
134134
{

0 commit comments

Comments
 (0)