From 715b566176db04fc33a6e2e23c941e5b4ee80972 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sat, 28 Nov 2020 00:11:15 +0000 Subject: [PATCH 1/8] Remove internal redirection for RuntimeTypeHandle --- .../src/System/Reflection/RuntimeModule.cs | 39 ++++++----- .../src/System/RuntimeHandles.cs | 65 ++++++++++++------- .../src/System/RuntimeType.CoreCLR.cs | 6 +- src/coreclr/src/vm/runtimehandles.cpp | 2 - 4 files changed, 65 insertions(+), 47 deletions(-) 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..b57f9fabc8d9e6 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 @@ -226,29 +226,32 @@ 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); - if (t == null) + 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)); - return t; + RuntimeTypeHandle[]? typeArgs = null; + RuntimeTypeHandle[]? methodArgs = null; + if (genericTypeArguments is not null && genericTypeArguments.Length > 0) + { + typeArgs = ConvertToTypeHandleArray(genericTypeArguments); + } + if (genericMethodArguments is not null && genericMethodArguments.Length > 0) + { + methodArgs = ConvertToTypeHandleArray(genericMethodArguments); + } + + return GetModuleHandleImpl().ResolveTypeHandle(metadataToken, typeArgs, methodArgs).GetRuntimeType(); } catch (BadImageFormatException e) { 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..97c6c8f14c6005 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 is not null) + { + typeInstantiationContext = (RuntimeTypeHandle[]?)typeInstantiationContext.Clone(); + typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out typeInstCount); + } + if (methodInstantiationContext is not null) + { + 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; + } } } 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..056d0cfe9e4b50 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) { diff --git a/src/coreclr/src/vm/runtimehandles.cpp b/src/coreclr/src/vm/runtimehandles.cpp index 14787a7faefc07..7ae3778413c9cd 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, From 101d7b7274635b1c1af09d072654936e8ee4c228 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sat, 28 Nov 2020 20:35:10 +0000 Subject: [PATCH 2/8] Remove internal redirection for RuntimeFieldHandle --- .../src/System/Reflection/RuntimeModule.cs | 37 +++++++++----- .../src/System/RuntimeHandles.cs | 50 ++++++++++++------- src/coreclr/src/vm/runtimehandles.cpp | 2 - 3 files changed, 56 insertions(+), 33 deletions(-) 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 b57f9fabc8d9e6..702d9ee4565358 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 @@ -171,19 +171,27 @@ 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); + try + { + MetadataToken tk = new (metadataToken); - if (!MetadataImport.IsValidToken(tk)) - throw new ArgumentOutOfRangeException(nameof(metadataToken), - SR.Format(SR.Argument_InvalidToken, tk, this)); + if (!MetadataImport.IsValidToken(tk)) + throw new ArgumentOutOfRangeException(nameof(metadataToken), + SR.Format(SR.Argument_InvalidToken, tk, this)); - RuntimeTypeHandle[]? typeArgs = ConvertToTypeHandleArray(genericTypeArguments); - RuntimeTypeHandle[]? methodArgs = ConvertToTypeHandleArray(genericMethodArguments); + RuntimeTypeHandle[]? typeArgs = null; + RuntimeTypeHandle[]? methodArgs = null; + if (genericTypeArguments is not null && genericTypeArguments.Length > 0) + { + typeArgs = ConvertToTypeHandleArray(genericTypeArguments); + } + if (genericMethodArguments is not null && genericMethodArguments.Length > 0) + { + methodArgs = ConvertToTypeHandleArray(genericMethodArguments); + } - try - { IRuntimeFieldInfo fieldHandle; - + ModuleHandle moduleHandle = new (GetNativeHandle()); if (!tk.IsFieldDef) { if (!tk.IsMemberRef) @@ -199,10 +207,13 @@ public override byte[] ResolveSignature(int metadataToken) nameof(metadataToken)); } - fieldHandle = ModuleHandle.ResolveFieldHandleInternal(GetNativeHandle(), tk, typeArgs, methodArgs); + fieldHandle = moduleHandle.ResolveFieldHandle(tk, typeArgs, methodArgs).GetRuntimeFieldInfo(); + } + else + { + fieldHandle = moduleHandle.ResolveFieldHandle(metadataToken, typeArgs, methodArgs).GetRuntimeFieldInfo(); } - fieldHandle = ModuleHandle.ResolveFieldHandleInternal(GetNativeHandle(), metadataToken, typeArgs, methodArgs); 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(new MetadataToken(metadataToken), genericTypeArguments, genericMethodArguments); } catch (BadImageFormatException e) { 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 97c6c8f14c6005..53ccfcb9df4f56 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs @@ -1386,32 +1386,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 is not null) + { + typeInstantiationContext = (RuntimeTypeHandle[]?)typeInstantiationContext.Clone(); + typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out typeInstCount); + } + if (methodInstantiationContext is not null) + { + 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/vm/runtimehandles.cpp b/src/coreclr/src/vm/runtimehandles.cpp index 7ae3778413c9cd..9c46d10a421110 100644 --- a/src/coreclr/src/vm/runtimehandles.cpp +++ b/src/coreclr/src/vm/runtimehandles.cpp @@ -2963,8 +2963,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(); From 7257ded9efed315169704a5ae1e91a36dd767b1d Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sat, 28 Nov 2020 21:25:52 +0000 Subject: [PATCH 3/8] Remove internal redirection for RuntimeMethodHandle --- .../src/System/Reflection/Associates.cs | 2 +- .../src/System/Reflection/CustomAttribute.cs | 2 +- .../src/System/Reflection/RuntimeModule.cs | 27 +++++++++------- .../src/System/RuntimeHandles.cs | 32 ++++++++++--------- .../src/System/RuntimeType.CoreCLR.cs | 2 +- src/coreclr/src/vm/runtimehandles.cpp | 2 -- 6 files changed, 36 insertions(+), 31 deletions(-) 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..5ef6e0018508a1 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.GetNativeHandle()).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 702d9ee4565358..b1ded10fb26b0c 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); if (!tk.IsMethodDef && !tk.IsMethodSpec) { if (!tk.IsMemberRef) @@ -118,7 +110,20 @@ public override byte[] ResolveSignature(int metadataToken) } } - IRuntimeMethodInfo methodHandle = ModuleHandle.ResolveMethodHandleInternal(GetNativeHandle(), tk, typeArgs, methodArgs); + RuntimeTypeHandle[]? typeArgs = null; + RuntimeTypeHandle[]? methodArgs = null; + if (genericTypeArguments is not null && genericTypeArguments.Length > 0) + { + typeArgs = ConvertToTypeHandleArray(genericTypeArguments); + } + if (genericMethodArguments is not null && genericMethodArguments.Length > 0) + { + methodArgs = ConvertToTypeHandleArray(genericMethodArguments); + } + + ModuleHandle moduleHandle = new (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) { 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 53ccfcb9df4f56..3218c56d039e99 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs @@ -1340,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(); @@ -1356,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; } } 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 056d0cfe9e4b50..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 @@ -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 9c46d10a421110..4c76c62f2b8828 100644 --- a/src/coreclr/src/vm/runtimehandles.cpp +++ b/src/coreclr/src/vm/runtimehandles.cpp @@ -2940,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)); From 8ecc156df8efd6ececb12420845b6be74c1b7d18 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sun, 29 Nov 2020 05:30:24 +0000 Subject: [PATCH 4/8] Feedback --- .../src/System/Reflection/CustomAttribute.cs | 2 +- .../src/System/Reflection/RuntimeModule.cs | 13 ++++--------- .../src/System/RuntimeHandles.cs | 8 ++++---- 3 files changed, 9 insertions(+), 14 deletions(-) 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 5ef6e0018508a1..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 = new ModuleHandle(decoratedModule.GetNativeHandle()).ResolveMethodHandle(caCtorToken).GetMethodInfo(); + 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 b1ded10fb26b0c..dc2186fb338b46 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 @@ -195,8 +195,7 @@ public override byte[] ResolveSignature(int metadataToken) methodArgs = ConvertToTypeHandleArray(genericMethodArguments); } - IRuntimeFieldInfo fieldHandle; - ModuleHandle moduleHandle = new (GetNativeHandle()); + ModuleHandle moduleHandle = new (this); if (!tk.IsFieldDef) { if (!tk.IsMemberRef) @@ -207,18 +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.ResolveFieldHandle(tk, typeArgs, methodArgs).GetRuntimeFieldInfo(); - } - else - { - fieldHandle = moduleHandle.ResolveFieldHandle(metadataToken, typeArgs, methodArgs).GetRuntimeFieldInfo(); } + IRuntimeFieldInfo fieldHandle = moduleHandle.ResolveFieldHandle(metadataToken, typeArgs, methodArgs).GetRuntimeFieldInfo(); + RuntimeType declaringType = RuntimeFieldHandle.GetApproxDeclaringType(fieldHandle.Value); if (declaringType.IsGenericType || declaringType.IsArray) 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 3218c56d039e99..74d0e9530794f5 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs @@ -1298,12 +1298,12 @@ public RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[]? t int methodInstCount = 0; // defensive copy of user-provided array, per CopyRuntimeTypeHandles contract - if (typeInstantiationContext is not null) + if (typeInstantiationContext is not null && typeInstantiationContext.Length > 0) { typeInstantiationContext = (RuntimeTypeHandle[]?)typeInstantiationContext.Clone(); typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out typeInstCount); } - if (methodInstantiationContext is not null) + if (methodInstantiationContext is not null && methodInstantiationContext.Length > 0) { methodInstantiationContext = (RuntimeTypeHandle[]?)methodInstantiationContext.Clone(); methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out methodInstCount); @@ -1400,12 +1400,12 @@ public RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[] int methodInstCount = 0; // defensive copy of user-provided array, per CopyRuntimeTypeHandles contract - if (typeInstantiationContext is not null) + if (typeInstantiationContext is not null && typeInstantiationContext.Length > 0) { typeInstantiationContext = (RuntimeTypeHandle[]?)typeInstantiationContext.Clone(); typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out typeInstCount); } - if (methodInstantiationContext is not null) + if (methodInstantiationContext is not null && methodInstantiationContext.Length > 0) { methodInstantiationContext = (RuntimeTypeHandle[]?)methodInstantiationContext.Clone(); methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out methodInstCount); From 444f99c341404a86b7903edb1e39e1eb9672adde Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sun, 29 Nov 2020 17:33:40 +0000 Subject: [PATCH 5/8] Add null token tests --- .../tests/System/Reflection/ModuleTests.cs | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) 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 From 80e96562dd405435eb613aef7c4a10be9f133607 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Mon, 30 Nov 2020 22:21:10 +0000 Subject: [PATCH 6/8] Apply suggestions from code review Co-authored-by: Jan Kotas --- .../src/System/Reflection/RuntimeModule.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) 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 dc2186fb338b46..406ec748231079 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 @@ -226,7 +226,7 @@ public override byte[] ResolveSignature(int metadataToken) } catch (MissingFieldException) { - return ResolveLiteralField(new MetadataToken(metadataToken), genericTypeArguments, genericMethodArguments); + return ResolveLiteralField(metadataToken, genericTypeArguments, genericMethodArguments); } catch (BadImageFormatException e) { @@ -244,10 +244,6 @@ public override Type ResolveType(int metadataToken, Type[]? genericTypeArguments 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)); From 357f67aaeb658073995f232113b965789cf6fdfc Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Mon, 30 Nov 2020 22:24:02 +0000 Subject: [PATCH 7/8] Remove .ToPointer() --- .../src/System/Reflection/RuntimeModule.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 406ec748231079..4718b33ee9aaf3 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 @@ -104,7 +104,7 @@ 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)); } @@ -296,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); } From 68da9b01c3a51cfe5e72fa2afa64fc7b01c971d4 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 1 Dec 2020 22:17:21 +0000 Subject: [PATCH 8/8] Feedback --- .../src/System/Reflection/RuntimeModule.cs | 22 +++++++++---------- .../src/System/RuntimeHandles.cs | 8 +++---- 2 files changed, 15 insertions(+), 15 deletions(-) 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 4718b33ee9aaf3..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 @@ -93,7 +93,7 @@ public override byte[] ResolveSignature(int metadataToken) { try { - MetadataToken tk = new (metadataToken); + MetadataToken tk = new MetadataToken(metadataToken); if (!tk.IsMethodDef && !tk.IsMethodSpec) { if (!tk.IsMemberRef) @@ -112,16 +112,16 @@ public override byte[] ResolveSignature(int metadataToken) RuntimeTypeHandle[]? typeArgs = null; RuntimeTypeHandle[]? methodArgs = null; - if (genericTypeArguments is not null && genericTypeArguments.Length > 0) + if (genericTypeArguments?.Length > 0) { typeArgs = ConvertToTypeHandleArray(genericTypeArguments); } - if (genericMethodArguments is not null && genericMethodArguments.Length > 0) + if (genericMethodArguments?.Length > 0) { methodArgs = ConvertToTypeHandleArray(genericMethodArguments); } - ModuleHandle moduleHandle = new (GetNativeHandle()); + ModuleHandle moduleHandle = new ModuleHandle(GetNativeHandle()); IRuntimeMethodInfo methodHandle = moduleHandle.ResolveMethodHandle(tk, typeArgs, methodArgs).GetMethodInfo(); Type declaringType = RuntimeMethodHandle.GetDeclaringType(methodHandle); @@ -178,7 +178,7 @@ public override byte[] ResolveSignature(int metadataToken) { try { - MetadataToken tk = new (metadataToken); + MetadataToken tk = new MetadataToken(metadataToken); if (!MetadataImport.IsValidToken(tk)) throw new ArgumentOutOfRangeException(nameof(metadataToken), @@ -186,16 +186,16 @@ public override byte[] ResolveSignature(int metadataToken) RuntimeTypeHandle[]? typeArgs = null; RuntimeTypeHandle[]? methodArgs = null; - if (genericTypeArguments is not null && genericTypeArguments.Length > 0) + if (genericTypeArguments?.Length > 0) { typeArgs = ConvertToTypeHandleArray(genericTypeArguments); } - if (genericMethodArguments is not null && genericMethodArguments.Length > 0) + if (genericMethodArguments?.Length > 0) { methodArgs = ConvertToTypeHandleArray(genericMethodArguments); } - ModuleHandle moduleHandle = new (this); + ModuleHandle moduleHandle = new ModuleHandle(this); if (!tk.IsFieldDef) { if (!tk.IsMemberRef) @@ -239,7 +239,7 @@ public override Type ResolveType(int metadataToken, Type[]? genericTypeArguments { try { - MetadataToken tk = new (metadataToken); + MetadataToken tk = new MetadataToken(metadataToken); if (tk.IsGlobalTypeDefToken) throw new ArgumentException(SR.Format(SR.Argument_ResolveModuleType, tk), nameof(metadataToken)); @@ -249,11 +249,11 @@ public override Type ResolveType(int metadataToken, Type[]? genericTypeArguments RuntimeTypeHandle[]? typeArgs = null; RuntimeTypeHandle[]? methodArgs = null; - if (genericTypeArguments is not null && genericTypeArguments.Length > 0) + if (genericTypeArguments?.Length > 0) { typeArgs = ConvertToTypeHandleArray(genericTypeArguments); } - if (genericMethodArguments is not null && genericMethodArguments.Length > 0) + if (genericMethodArguments?.Length > 0) { methodArgs = ConvertToTypeHandleArray(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 74d0e9530794f5..42db402066a4ba 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeHandles.cs @@ -1298,12 +1298,12 @@ public RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[]? t int methodInstCount = 0; // defensive copy of user-provided array, per CopyRuntimeTypeHandles contract - if (typeInstantiationContext is not null && typeInstantiationContext.Length > 0) + if (typeInstantiationContext?.Length > 0) { typeInstantiationContext = (RuntimeTypeHandle[]?)typeInstantiationContext.Clone(); typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out typeInstCount); } - if (methodInstantiationContext is not null && methodInstantiationContext.Length > 0) + if (methodInstantiationContext?.Length > 0) { methodInstantiationContext = (RuntimeTypeHandle[]?)methodInstantiationContext.Clone(); methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out methodInstCount); @@ -1400,12 +1400,12 @@ public RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[] int methodInstCount = 0; // defensive copy of user-provided array, per CopyRuntimeTypeHandles contract - if (typeInstantiationContext is not null && typeInstantiationContext.Length > 0) + if (typeInstantiationContext?.Length > 0) { typeInstantiationContext = (RuntimeTypeHandle[]?)typeInstantiationContext.Clone(); typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out typeInstCount); } - if (methodInstantiationContext is not null && methodInstantiationContext.Length > 0) + if (methodInstantiationContext?.Length > 0) { methodInstantiationContext = (RuntimeTypeHandle[]?)methodInstantiationContext.Clone(); methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out methodInstCount);