diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Associates.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Associates.cs index 6d90ebf7b99391..1b24ecd5e0a633 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Associates.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Associates.cs @@ -63,7 +63,7 @@ internal static bool IncludeAccessor(MethodInfo? associate, bool nonPublic) } } - RuntimeMethodHandleInternal associateMethodHandle = ModuleHandle.ResolveMethodHandleInternalCore(RuntimeTypeHandle.GetModule(declaredType), tkMethod, genericArgumentHandles, genericArgumentCount, null, 0); + RuntimeMethodHandleInternal associateMethodHandle = ModuleHandle.ResolveMethodHandleInternal(RuntimeTypeHandle.GetModule(declaredType), tkMethod, genericArgumentHandles, genericArgumentCount, null, 0); Debug.Assert(!associateMethodHandle.IsNullHandle(), "Failed to resolve associateRecord methodDef token"); if (isInherited) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs index 33df3dfc35af89..613bb64f448cce 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs @@ -1374,7 +1374,7 @@ private static bool FilterCustomAttributeRecord( } else { - ctorWithParameters = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), caCtorToken); + ctorWithParameters = new ModuleHandle(decoratedModule).ResolveMethodHandle(caCtorToken).GetMethodInfo(); } } diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs index 990b52854a05fb..1c31175cf164ef 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs @@ -91,17 +91,9 @@ public override byte[] ResolveSignature(int metadataToken) [RequiresUnreferencedCode("Trimming changes metadata tokens")] public override MethodBase? ResolveMethod(int metadataToken, Type[]? genericTypeArguments, Type[]? genericMethodArguments) { - MetadataToken tk = new MetadataToken(metadataToken); - - if (!MetadataImport.IsValidToken(tk)) - throw new ArgumentOutOfRangeException(nameof(metadataToken), - SR.Format(SR.Argument_InvalidToken, tk, this)); - - RuntimeTypeHandle[]? typeArgs = ConvertToTypeHandleArray(genericTypeArguments); - RuntimeTypeHandle[]? methodArgs = ConvertToTypeHandleArray(genericMethodArguments); - try { + MetadataToken tk = new MetadataToken(metadataToken); if (!tk.IsMethodDef && !tk.IsMethodSpec) { if (!tk.IsMemberRef) @@ -112,13 +104,26 @@ public override byte[] ResolveSignature(int metadataToken) { ConstArray sig = MetadataImport.GetMemberRefProps(tk); - if (*(MdSigCallingConvention*)sig.Signature.ToPointer() == MdSigCallingConvention.Field) + if (*(MdSigCallingConvention*)sig.Signature == MdSigCallingConvention.Field) throw new ArgumentException(SR.Format(SR.Argument_ResolveMethod, tk, this), nameof(metadataToken)); } } - IRuntimeMethodInfo methodHandle = ModuleHandle.ResolveMethodHandleInternal(GetNativeHandle(), tk, typeArgs, methodArgs); + RuntimeTypeHandle[]? typeArgs = null; + RuntimeTypeHandle[]? methodArgs = null; + if (genericTypeArguments?.Length > 0) + { + typeArgs = ConvertToTypeHandleArray(genericTypeArguments); + } + if (genericMethodArguments?.Length > 0) + { + methodArgs = ConvertToTypeHandleArray(genericMethodArguments); + } + + ModuleHandle moduleHandle = new ModuleHandle(GetNativeHandle()); + IRuntimeMethodInfo methodHandle = moduleHandle.ResolveMethodHandle(tk, typeArgs, methodArgs).GetMethodInfo(); + Type declaringType = RuntimeMethodHandle.GetDeclaringType(methodHandle); if (declaringType.IsGenericType || declaringType.IsArray) @@ -131,7 +136,7 @@ public override byte[] ResolveSignature(int metadataToken) declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments); } - return System.RuntimeType.GetMethodBase(declaringType as RuntimeType, methodHandle); + return RuntimeType.GetMethodBase(declaringType as RuntimeType, methodHandle); } catch (BadImageFormatException e) { @@ -171,19 +176,26 @@ public override byte[] ResolveSignature(int metadataToken) [RequiresUnreferencedCode("Trimming changes metadata tokens")] public override FieldInfo? ResolveField(int metadataToken, Type[]? genericTypeArguments, Type[]? genericMethodArguments) { - MetadataToken tk = new MetadataToken(metadataToken); - - if (!MetadataImport.IsValidToken(tk)) - throw new ArgumentOutOfRangeException(nameof(metadataToken), - SR.Format(SR.Argument_InvalidToken, tk, this)); - - RuntimeTypeHandle[]? typeArgs = ConvertToTypeHandleArray(genericTypeArguments); - RuntimeTypeHandle[]? methodArgs = ConvertToTypeHandleArray(genericMethodArguments); - try { - IRuntimeFieldInfo fieldHandle; + MetadataToken tk = new MetadataToken(metadataToken); + + if (!MetadataImport.IsValidToken(tk)) + throw new ArgumentOutOfRangeException(nameof(metadataToken), + SR.Format(SR.Argument_InvalidToken, tk, this)); + + RuntimeTypeHandle[]? typeArgs = null; + RuntimeTypeHandle[]? methodArgs = null; + if (genericTypeArguments?.Length > 0) + { + typeArgs = ConvertToTypeHandleArray(genericTypeArguments); + } + if (genericMethodArguments?.Length > 0) + { + methodArgs = ConvertToTypeHandleArray(genericMethodArguments); + } + ModuleHandle moduleHandle = new ModuleHandle(this); if (!tk.IsFieldDef) { if (!tk.IsMemberRef) @@ -194,15 +206,14 @@ public override byte[] ResolveSignature(int metadataToken) { ConstArray sig = MetadataImport.GetMemberRefProps(tk); - if (*(MdSigCallingConvention*)sig.Signature.ToPointer() != MdSigCallingConvention.Field) + if (*(MdSigCallingConvention*)sig.Signature != MdSigCallingConvention.Field) throw new ArgumentException(SR.Format(SR.Argument_ResolveField, tk, this), nameof(metadataToken)); } - - fieldHandle = ModuleHandle.ResolveFieldHandleInternal(GetNativeHandle(), tk, typeArgs, methodArgs); } - fieldHandle = ModuleHandle.ResolveFieldHandleInternal(GetNativeHandle(), metadataToken, typeArgs, methodArgs); + IRuntimeFieldInfo fieldHandle = moduleHandle.ResolveFieldHandle(metadataToken, typeArgs, methodArgs).GetRuntimeFieldInfo(); + RuntimeType declaringType = RuntimeFieldHandle.GetApproxDeclaringType(fieldHandle.Value); if (declaringType.IsGenericType || declaringType.IsArray) @@ -211,11 +222,11 @@ public override byte[] ResolveSignature(int metadataToken) declaringType = (RuntimeType)ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments); } - return System.RuntimeType.GetFieldInfo(declaringType, fieldHandle); + return RuntimeType.GetFieldInfo(declaringType, fieldHandle); } catch (MissingFieldException) { - return ResolveLiteralField(tk, genericTypeArguments, genericMethodArguments); + return ResolveLiteralField(metadataToken, genericTypeArguments, genericMethodArguments); } catch (BadImageFormatException e) { @@ -226,29 +237,28 @@ public override byte[] ResolveSignature(int metadataToken) [RequiresUnreferencedCode("Trimming changes metadata tokens")] public override Type ResolveType(int metadataToken, Type[]? genericTypeArguments, Type[]? genericMethodArguments) { - MetadataToken tk = new MetadataToken(metadataToken); - - if (tk.IsGlobalTypeDefToken) - throw new ArgumentException(SR.Format(SR.Argument_ResolveModuleType, tk), nameof(metadataToken)); - - if (!MetadataImport.IsValidToken(tk)) - throw new ArgumentOutOfRangeException(nameof(metadataToken), - SR.Format(SR.Argument_InvalidToken, tk, this)); - - if (!tk.IsTypeDef && !tk.IsTypeSpec && !tk.IsTypeRef) - throw new ArgumentException(SR.Format(SR.Argument_ResolveType, tk, this), nameof(metadataToken)); - - RuntimeTypeHandle[]? typeArgs = ConvertToTypeHandleArray(genericTypeArguments); - RuntimeTypeHandle[]? methodArgs = ConvertToTypeHandleArray(genericMethodArguments); - try { - Type t = GetModuleHandleImpl().ResolveTypeHandle(metadataToken, typeArgs, methodArgs).GetRuntimeType(); + MetadataToken tk = new MetadataToken(metadataToken); + + if (tk.IsGlobalTypeDefToken) + throw new ArgumentException(SR.Format(SR.Argument_ResolveModuleType, tk), nameof(metadataToken)); - if (t == null) + if (!tk.IsTypeDef && !tk.IsTypeSpec && !tk.IsTypeRef) throw new ArgumentException(SR.Format(SR.Argument_ResolveType, tk, this), nameof(metadataToken)); - return t; + RuntimeTypeHandle[]? typeArgs = null; + RuntimeTypeHandle[]? methodArgs = null; + if (genericTypeArguments?.Length > 0) + { + typeArgs = ConvertToTypeHandleArray(genericTypeArguments); + } + if (genericMethodArguments?.Length > 0) + { + methodArgs = ConvertToTypeHandleArray(genericMethodArguments); + } + + return GetModuleHandleImpl().ResolveTypeHandle(metadataToken, typeArgs, methodArgs).GetRuntimeType(); } catch (BadImageFormatException e) { @@ -286,7 +296,7 @@ public override Type ResolveType(int metadataToken, Type[]? genericTypeArguments unsafe { - if (*(MdSigCallingConvention*)sig.Signature.ToPointer() == MdSigCallingConvention.Field) + if (*(MdSigCallingConvention*)sig.Signature == MdSigCallingConvention.Field) { return ResolveField(tk, genericTypeArguments, genericMethodArguments); } diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs index fbca319a20c229..42db402066a4ba 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs @@ -1273,42 +1273,59 @@ public bool Equals(ModuleHandle handle) private static void ValidateModulePointer(RuntimeModule module) { // Make sure we have a valid Module to resolve against. - if (module == null) - throw new InvalidOperationException(SR.InvalidOperation_NullModuleHandle); + if (module is null) + { + // Local function to allow inlining of simple null check. + ThrowInvalidOperationException(); + } + + [StackTraceHidden] + [DoesNotReturn] + static void ThrowInvalidOperationException() => throw new InvalidOperationException(SR.InvalidOperation_NullModuleHandle); } // SQL-CLR LKG9 Compiler dependency public RuntimeTypeHandle GetRuntimeTypeHandleFromMetadataToken(int typeToken) { return ResolveTypeHandle(typeToken); } - public RuntimeTypeHandle ResolveTypeHandle(int typeToken) - { - return new RuntimeTypeHandle(ResolveTypeHandleInternal(GetRuntimeModule(), typeToken, null, null)); - } + public RuntimeTypeHandle ResolveTypeHandle(int typeToken) => ResolveTypeHandle(typeToken, null, null); public RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext) { - return new RuntimeTypeHandle(ModuleHandle.ResolveTypeHandleInternal(GetRuntimeModule(), typeToken, typeInstantiationContext, methodInstantiationContext)); - } - - internal static RuntimeType ResolveTypeHandleInternal(RuntimeModule module, int typeToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext) - { + RuntimeModule module = GetRuntimeModule(); ValidateModulePointer(module); - if (!ModuleHandle.GetMetadataImport(module).IsValidToken(typeToken)) - throw new ArgumentOutOfRangeException(nameof(typeToken), - SR.Format(SR.Argument_InvalidToken, typeToken, new ModuleHandle(module))); - // defensive copy of user-provided array, per CopyRuntimeTypeHandles contract - typeInstantiationContext = (RuntimeTypeHandle[]?)typeInstantiationContext?.Clone(); - methodInstantiationContext = (RuntimeTypeHandle[]?)methodInstantiationContext?.Clone(); + IntPtr[]? typeInstantiationContextHandles = null; + int typeInstCount = 0; + IntPtr[]? methodInstantiationContextHandles = null; + int methodInstCount = 0; - IntPtr[]? typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out int typeInstCount); - IntPtr[]? methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out int methodInstCount); + // defensive copy of user-provided array, per CopyRuntimeTypeHandles contract + if (typeInstantiationContext?.Length > 0) + { + typeInstantiationContext = (RuntimeTypeHandle[]?)typeInstantiationContext.Clone(); + typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out typeInstCount); + } + if (methodInstantiationContext?.Length > 0) + { + methodInstantiationContext = (RuntimeTypeHandle[]?)methodInstantiationContext.Clone(); + methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out methodInstCount); + } fixed (IntPtr* typeInstArgs = typeInstantiationContextHandles, methodInstArgs = methodInstantiationContextHandles) { - RuntimeType? type = null; - ResolveType(new QCallModule(ref module), typeToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount, ObjectHandleOnStack.Create(ref type)); - GC.KeepAlive(typeInstantiationContext); - GC.KeepAlive(methodInstantiationContext); - return type!; + try + { + RuntimeType? type = null; + ResolveType(new QCallModule(ref module), typeToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount, ObjectHandleOnStack.Create(ref type)); + GC.KeepAlive(typeInstantiationContext); + GC.KeepAlive(methodInstantiationContext); + return new RuntimeTypeHandle(type!); + } + catch (Exception) + { + if (!GetMetadataImport(module).IsValidToken(typeToken)) + throw new ArgumentOutOfRangeException(nameof(typeToken), + SR.Format(SR.Argument_InvalidToken, typeToken, new ModuleHandle(module))); + throw; + } } } @@ -1323,15 +1340,10 @@ private static extern void ResolveType(QCallModule module, // SQL-CLR LKG9 Compiler dependency public RuntimeMethodHandle GetRuntimeMethodHandleFromMetadataToken(int methodToken) { return ResolveMethodHandle(methodToken); } - public RuntimeMethodHandle ResolveMethodHandle(int methodToken) { return ResolveMethodHandle(methodToken, null, null); } - internal static IRuntimeMethodInfo ResolveMethodHandleInternal(RuntimeModule module, int methodToken) { return ModuleHandle.ResolveMethodHandleInternal(module, methodToken, null, null); } + public RuntimeMethodHandle ResolveMethodHandle(int methodToken) => ResolveMethodHandle(methodToken, null, null); public RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext) { - return new RuntimeMethodHandle(ResolveMethodHandleInternal(GetRuntimeModule(), methodToken, typeInstantiationContext, methodInstantiationContext)); - } - - internal static IRuntimeMethodInfo ResolveMethodHandleInternal(RuntimeModule module, int methodToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext) - { + RuntimeModule module = GetRuntimeModule(); // defensive copy of user-provided array, per CopyRuntimeTypeHandles contract typeInstantiationContext = (RuntimeTypeHandle[]?)typeInstantiationContext?.Clone(); methodInstantiationContext = (RuntimeTypeHandle[]?)methodInstantiationContext?.Clone(); @@ -1339,23 +1351,30 @@ internal static IRuntimeMethodInfo ResolveMethodHandleInternal(RuntimeModule mod IntPtr[]? typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out int typeInstCount); IntPtr[]? methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out int methodInstCount); - RuntimeMethodHandleInternal handle = ResolveMethodHandleInternalCore(module, methodToken, typeInstantiationContextHandles, typeInstCount, methodInstantiationContextHandles, methodInstCount); + RuntimeMethodHandleInternal handle = ResolveMethodHandleInternal(module, methodToken, typeInstantiationContextHandles, typeInstCount, methodInstantiationContextHandles, methodInstCount); IRuntimeMethodInfo retVal = new RuntimeMethodInfoStub(handle, RuntimeMethodHandle.GetLoaderAllocator(handle)); GC.KeepAlive(typeInstantiationContext); GC.KeepAlive(methodInstantiationContext); - return retVal; + return new RuntimeMethodHandle(retVal); } - internal static RuntimeMethodHandleInternal ResolveMethodHandleInternalCore(RuntimeModule module, int methodToken, IntPtr[]? typeInstantiationContext, int typeInstCount, IntPtr[]? methodInstantiationContext, int methodInstCount) + internal static RuntimeMethodHandleInternal ResolveMethodHandleInternal(RuntimeModule module, int methodToken, IntPtr[]? typeInstantiationContext, int typeInstCount, IntPtr[]? methodInstantiationContext, int methodInstCount) { ValidateModulePointer(module); - if (!ModuleHandle.GetMetadataImport(module.GetNativeHandle()).IsValidToken(methodToken)) - throw new ArgumentOutOfRangeException(nameof(methodToken), - SR.Format(SR.Argument_InvalidToken, methodToken, new ModuleHandle(module))); - fixed (IntPtr* typeInstArgs = typeInstantiationContext, methodInstArgs = methodInstantiationContext) + try + { + fixed (IntPtr* typeInstArgs = typeInstantiationContext, methodInstArgs = methodInstantiationContext) + { + return ResolveMethod(new QCallModule(ref module), methodToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount); + } + } + catch (Exception) { - return ResolveMethod(new QCallModule(ref module), methodToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount); + if (!ModuleHandle.GetMetadataImport(module.GetNativeHandle()).IsValidToken(methodToken)) + throw new ArgumentOutOfRangeException(nameof(methodToken), + SR.Format(SR.Argument_InvalidToken, methodToken, new ModuleHandle(module))); + throw; } } @@ -1369,32 +1388,46 @@ private static extern RuntimeMethodHandleInternal ResolveMethod(QCallModule modu // SQL-CLR LKG9 Compiler dependency public RuntimeFieldHandle GetRuntimeFieldHandleFromMetadataToken(int fieldToken) { return ResolveFieldHandle(fieldToken); } - public RuntimeFieldHandle ResolveFieldHandle(int fieldToken) { return new RuntimeFieldHandle(ResolveFieldHandleInternal(GetRuntimeModule(), fieldToken, null, null)); } + public RuntimeFieldHandle ResolveFieldHandle(int fieldToken) => ResolveFieldHandle(fieldToken, null, null); public RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext) - { return new RuntimeFieldHandle(ResolveFieldHandleInternal(GetRuntimeModule(), fieldToken, typeInstantiationContext, methodInstantiationContext)); } - - internal static IRuntimeFieldInfo ResolveFieldHandleInternal(RuntimeModule module, int fieldToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext) { + RuntimeModule module = GetRuntimeModule(); ValidateModulePointer(module); - if (!ModuleHandle.GetMetadataImport(module.GetNativeHandle()).IsValidToken(fieldToken)) - throw new ArgumentOutOfRangeException(nameof(fieldToken), - SR.Format(SR.Argument_InvalidToken, fieldToken, new ModuleHandle(module))); - // defensive copy of user-provided array, per CopyRuntimeTypeHandles contract - typeInstantiationContext = (RuntimeTypeHandle[]?)typeInstantiationContext?.Clone(); - methodInstantiationContext = (RuntimeTypeHandle[]?)methodInstantiationContext?.Clone(); + IntPtr[]? typeInstantiationContextHandles = null; + int typeInstCount = 0; + IntPtr[]? methodInstantiationContextHandles = null; + int methodInstCount = 0; - // defensive copy to be sure array is not mutated from the outside during processing - IntPtr[]? typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out int typeInstCount); - IntPtr[]? methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out int methodInstCount); + // defensive copy of user-provided array, per CopyRuntimeTypeHandles contract + if (typeInstantiationContext?.Length > 0) + { + typeInstantiationContext = (RuntimeTypeHandle[]?)typeInstantiationContext.Clone(); + typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out typeInstCount); + } + if (methodInstantiationContext?.Length > 0) + { + methodInstantiationContext = (RuntimeTypeHandle[]?)methodInstantiationContext.Clone(); + methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out methodInstCount); + } fixed (IntPtr* typeInstArgs = typeInstantiationContextHandles, methodInstArgs = methodInstantiationContextHandles) { - IRuntimeFieldInfo? field = null; - ResolveField(new QCallModule(ref module), fieldToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount, ObjectHandleOnStack.Create(ref field)); - GC.KeepAlive(typeInstantiationContext); - GC.KeepAlive(methodInstantiationContext); - return field!; + try + { + IRuntimeFieldInfo? field = null; + ResolveField(new QCallModule(ref module), fieldToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount, ObjectHandleOnStack.Create(ref field)); + GC.KeepAlive(typeInstantiationContext); + GC.KeepAlive(methodInstantiationContext); + return new RuntimeFieldHandle(field!); + } + catch (Exception) + { + if (!GetMetadataImport(module.GetNativeHandle()).IsValidToken(fieldToken)) + throw new ArgumentOutOfRangeException(nameof(fieldToken), + SR.Format(SR.Argument_InvalidToken, fieldToken, new ModuleHandle(module))); + throw; + } } } diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs index 6e2c218e31bfa0..e08e9ac5f0b6e3 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs @@ -1094,8 +1094,8 @@ private RuntimeType[] PopulateNestedClasses(Filter filter) ListBuilder list = default; - RuntimeModule moduleHandle = RuntimeTypeHandle.GetModule(declaringType); - MetadataImport scope = ModuleHandle.GetMetadataImport(moduleHandle); + ModuleHandle moduleHandle = new ModuleHandle(RuntimeTypeHandle.GetModule(declaringType)); + MetadataImport scope = ModuleHandle.GetMetadataImport(moduleHandle.GetRuntimeModule()); scope.EnumNestedTypes(tkEnclosingType, out MetadataEnumResult tkNestedClasses); @@ -1105,7 +1105,7 @@ private RuntimeType[] PopulateNestedClasses(Filter filter) try { - nestedType = ModuleHandle.ResolveTypeHandleInternal(moduleHandle, tkNestedClasses[i], null, null); + nestedType = moduleHandle.ResolveTypeHandle(tkNestedClasses[i]).GetRuntimeType(); } catch (System.TypeLoadException) { @@ -1723,7 +1723,7 @@ internal FieldInfo GetField(RuntimeFieldHandleInternal field) internal static MethodBase? GetMethodBase(RuntimeModule scope, int typeMetadataToken) { - return GetMethodBase(ModuleHandle.ResolveMethodHandleInternal(scope, typeMetadataToken)); + return GetMethodBase(new ModuleHandle(scope).ResolveMethodHandle(typeMetadataToken).GetMethodInfo()); } internal static MethodBase? GetMethodBase(IRuntimeMethodInfo methodHandle) diff --git a/src/coreclr/src/vm/runtimehandles.cpp b/src/coreclr/src/vm/runtimehandles.cpp index 14787a7faefc07..4c76c62f2b8828 100644 --- a/src/coreclr/src/vm/runtimehandles.cpp +++ b/src/coreclr/src/vm/runtimehandles.cpp @@ -2919,8 +2919,6 @@ void QCALLTYPE ModuleHandle::ResolveType(QCall::ModuleHandle pModule, INT32 tkTy BEGIN_QCALL; - _ASSERTE(!IsNilToken(tkType)); - SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount)); typeHandle = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, tkType, &typeContext, ClassLoader::ThrowIfNotFound, @@ -2942,8 +2940,6 @@ MethodDesc *QCALLTYPE ModuleHandle::ResolveMethod(QCall::ModuleHandle pModule, I BEGIN_QCALL; - _ASSERTE(!IsNilToken(tkMemberRef)); - BOOL strictMetadataChecks = (TypeFromToken(tkMemberRef) == mdtMethodSpec); SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount)); @@ -2965,8 +2961,6 @@ void QCALLTYPE ModuleHandle::ResolveField(QCall::ModuleHandle pModule, INT32 tkM BEGIN_QCALL; - _ASSERTE(!IsNilToken(tkMemberRef)); - SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount)); pField = MemberLoader::GetFieldDescFromMemberDefOrRef(pModule, tkMemberRef, &typeContext, FALSE); GCX_COOP(); diff --git a/src/libraries/System.Runtime/tests/System/Reflection/ModuleTests.cs b/src/libraries/System.Runtime/tests/System/Reflection/ModuleTests.cs index b5494718b7be91..9368a6184d0856 100644 --- a/src/libraries/System.Runtime/tests/System/Reflection/ModuleTests.cs +++ b/src/libraries/System.Runtime/tests/System/Reflection/ModuleTests.cs @@ -191,7 +191,8 @@ public void ResolveType(Type t) { new object[] { 1234 }, new object[] { typeof(ModuleTests).GetMethod("ResolveType").MetadataToken }, - }; + } + .Union(NullTokens); [Theory] [MemberData(nameof(BadResolveTypes))] @@ -219,7 +220,8 @@ public void ResolveMethod(MethodInfo t) new object[] { 1234 }, new object[] { typeof(ModuleTests).MetadataToken }, new object[] { typeof(ModuleTests).MetadataToken + 1000 }, - }; + } + .Union(NullTokens); [Theory] [MemberData(nameof(BadResolveMethods))] @@ -247,7 +249,8 @@ public void ResolveField(FieldInfo t) new object[] { 1234 }, new object[] { typeof(ModuleTests).MetadataToken }, new object[] { typeof(ModuleTests).MetadataToken + 1000 }, - }; + } + .Union(NullTokens); [Theory] [MemberData(nameof(BadResolveFields))] @@ -265,7 +268,8 @@ public void ResolveFieldFail(int token) new object[] { 1234 }, new object[] { typeof(ModuleTests).MetadataToken }, new object[] { typeof(ModuleTests).MetadataToken + 1000 }, - }; + } + .Union(NullTokens); [Theory] [MemberData(nameof(BadResolveStrings))] @@ -303,6 +307,38 @@ public void GetTypes() Assert.Equal(1, types.Count); Assert.Equal("System.Reflection.TestModule.Dummy, System.Reflection.TestModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", types[0].AssemblyQualifiedName); } + + private static object[][] NullTokens => + new[] + { + new object[] { 0x00000000 }, // mdtModule + new object[] { 0x01000000 }, // mdtTypeRef + new object[] { 0x02000000 }, // mdtTypeDef + new object[] { 0x04000000 }, // mdtFieldDef + new object[] { 0x06000000 }, // mdtMethodDef + new object[] { 0x08000000 }, // mdtParamDef + new object[] { 0x09000000 }, // mdtInterfaceImpl + new object[] { 0x0a000000 }, // mdtMemberRef + new object[] { 0x0c000000 }, // mdtCustomAttribute + new object[] { 0x0e000000 }, // mdtPermission + new object[] { 0x11000000 }, // mdtSignature + new object[] { 0x14000000 }, // mdtEvent + new object[] { 0x17000000 }, // mdtProperty + new object[] { 0x19000000 }, // mdtMethodImpl + new object[] { 0x1a000000 }, // mdtModuleRef + new object[] { 0x1b000000 }, // mdtTypeSpec + new object[] { 0x20000000 }, // mdtAssembly + new object[] { 0x23000000 }, // mdtAssemblyRef + new object[] { 0x26000000 }, // mdtFile + new object[] { 0x27000000 }, // mdtExportedType + new object[] { 0x28000000 }, // mdtManifestResource + new object[] { 0x2a000000 }, // mdtGenericParam + new object[] { 0x2b000000 }, // mdtMethodSpec + new object[] { 0x2c000000 }, // mdtGenericParamConstraint + new object[] { 0x70000000 }, // mdtString + new object[] { 0x71000000 }, // mdtName + new object[] { 0x72000000 } // mdtBaseType + }; } public class Foo