diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs index 95bcf0652648ab..1bf9c90c514357 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs @@ -727,7 +727,16 @@ static RuntimeModule GetManifestModuleWorker(RuntimeAssembly assembly) private static partial void GetManifestModuleSlow(ObjectHandleOnStack assembly, ObjectHandleOnStack module); [MethodImpl(MethodImplOptions.InternalCall)] - internal static extern int GetToken(RuntimeAssembly assembly); + private static extern int GetTokenInternal(RuntimeAssembly assembly); + + internal static int GetToken(RuntimeAssembly assembly) + { + int tokenMaybe = GetTokenInternal(assembly); + // If the result is negative, it is an error code. + if (tokenMaybe < 0) + Marshal.ThrowExceptionForHR(tokenMaybe, new IntPtr(-1)); + return tokenMaybe; + } [RequiresUnreferencedCode("Types might be removed")] public sealed override Type[] GetForwardedTypes() diff --git a/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs b/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs index 3246c90a35f472..4bc9b32f20ca38 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs @@ -1258,7 +1258,7 @@ internal static bool IsGenericMethodDefinition(IRuntimeMethodInfo method) } [MethodImpl(MethodImplOptions.InternalCall)] - internal static extern bool IsTypicalMethodDefinition(IRuntimeMethodInfo method); + private static extern bool IsTypicalMethodDefinition(IRuntimeMethodInfo method); [LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeMethodHandle_GetTypicalMethodDefinition")] private static partial void GetTypicalMethodDefinition(RuntimeMethodHandleInternal method, ObjectHandleOnStack outMethod); @@ -1626,7 +1626,14 @@ internal static void SetValueDirect(RtFieldInfo field, RuntimeType fieldType, Ty } [MethodImpl(MethodImplOptions.InternalCall)] - internal static extern RuntimeFieldHandleInternal GetStaticFieldForGenericType(RuntimeFieldHandleInternal field, RuntimeType declaringType); + private static extern unsafe RuntimeFieldHandleInternal GetStaticFieldForGenericType(RuntimeFieldHandleInternal field, MethodTable* pMT); + + internal static RuntimeFieldHandleInternal GetStaticFieldForGenericType(RuntimeFieldHandleInternal field, RuntimeType declaringType) + { + TypeHandle th = declaringType.GetNativeTypeHandle(); + Debug.Assert(!th.IsTypeDesc); + return GetStaticFieldForGenericType(field, th.AsMethodTable()); + } [MethodImpl(MethodImplOptions.InternalCall)] internal static extern bool AcquiresContextFromThis(RuntimeFieldHandleInternal field); diff --git a/src/coreclr/vm/ecalllist.h b/src/coreclr/vm/ecalllist.h index a782ae05d2d41f..d6fd867a53ff0a 100644 --- a/src/coreclr/vm/ecalllist.h +++ b/src/coreclr/vm/ecalllist.h @@ -169,7 +169,7 @@ FCFuncEnd() FCFuncStart(gRuntimeAssemblyFuncs) FCFuncElement("GetIsDynamic", AssemblyNative::GetIsDynamic) FCFuncElement("GetManifestModule", AssemblyHandle::GetManifestModule) - FCFuncElement("GetToken", AssemblyHandle::GetToken) + FCFuncElement("GetTokenInternal", AssemblyHandle::GetTokenInternal) FCFuncEnd() FCFuncStart(gAssemblyLoadContextFuncs) diff --git a/src/coreclr/vm/fcall.cpp b/src/coreclr/vm/fcall.cpp index 80f163c19b3756..fc2e16f98a49b3 100644 --- a/src/coreclr/vm/fcall.cpp +++ b/src/coreclr/vm/fcall.cpp @@ -65,53 +65,6 @@ NOINLINE LPVOID __FCThrow(LPVOID __me, RuntimeExceptionKind reKind, UINT resID, return NULL; } -NOINLINE LPVOID __FCThrowArgument(LPVOID __me, RuntimeExceptionKind reKind, LPCWSTR argName, LPCWSTR resourceName) -{ - STATIC_CONTRACT_THROWS; - // This isn't strictly true... But the guarantee that we make here is - // that we won't trigger without having setup a frame. - // STATIC_CONTRACT_TRIGGER - STATIC_CONTRACT_GC_NOTRIGGER; - - // side effect the compiler can't remove - if (FC_NO_TAILCALL != 1) - return (LPVOID)(SIZE_T)(FC_NO_TAILCALL + 1); - - FC_CAN_TRIGGER_GC(); - INCONTRACT(FCallCheck __fCallCheck(__FILE__, __LINE__)); - FC_GC_POLL_NOT_NEEDED(); // throws always open up for GC - - HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_NOPOLL(Frame::FRAME_ATTR_CAPTURE_DEPTH_2); - - switch (reKind) { - case kArgumentNullException: - if (resourceName) { - COMPlusThrowArgumentNull(argName, resourceName); - } else { - COMPlusThrowArgumentNull(argName); - } - break; - - case kArgumentOutOfRangeException: - COMPlusThrowArgumentOutOfRange(argName, resourceName); - break; - - case kArgumentException: - COMPlusThrowArgumentException(argName, resourceName); - break; - - default: - // If you see this assert, add a case for your exception kind above. - _ASSERTE(argName == NULL); - COMPlusThrow(reKind, resourceName); - } - - HELPER_METHOD_FRAME_END(); - FC_CAN_TRIGGER_GC_END(); - _ASSERTE(!"Throw returned"); - return NULL; -} - /**************************************************************************************/ /* erect a frame in the FCALL and then poll the GC, objToProtect will be protected during the poll and the updated object returned. */ diff --git a/src/coreclr/vm/fcall.h b/src/coreclr/vm/fcall.h index 8c56fd8c55f794..3a5bcf5e0ee159 100644 --- a/src/coreclr/vm/fcall.h +++ b/src/coreclr/vm/fcall.h @@ -362,11 +362,9 @@ class CompletedFCallTransitionState //============================================================================================== // This is where FCThrow ultimately ends up. Never call this directly. -// Use the FCThrow() macros. __FCThrowArgument is the helper to throw ArgumentExceptions -// with a resource taken from the managed resource manager. +// Use the FCThrow() macro. //============================================================================================== LPVOID __FCThrow(LPVOID me, enum RuntimeExceptionKind reKind, UINT resID, LPCWSTR arg1, LPCWSTR arg2, LPCWSTR arg3); -LPVOID __FCThrowArgument(LPVOID me, enum RuntimeExceptionKind reKind, LPCWSTR argumentName, LPCWSTR resourceName); //============================================================================================== // FDECLn: A set of macros for generating header declarations for FC targets. @@ -1236,15 +1234,6 @@ struct FCSigCheck { return 0; \ } -// Use FCThrowRes to throw an exception with a localized error message from the -// ResourceManager in managed code. -#define FCThrowRes(reKind, resourceName) \ - { \ - while (NULL == \ - __FCThrowArgument(__me, reKind, NULL, resourceName)) {}; \ - return 0; \ - } - // The managed calling convention expects returned small types (e.g. bool) to be // widened to 32-bit on return. The C/C++ calling convention does not guarantee returned // small types to be widened on most platforms. The small types have to be artificially diff --git a/src/coreclr/vm/runtimehandles.cpp b/src/coreclr/vm/runtimehandles.cpp index b016f70ba60dc8..c656784427fb9c 100644 --- a/src/coreclr/vm/runtimehandles.cpp +++ b/src/coreclr/vm/runtimehandles.cpp @@ -185,8 +185,10 @@ extern "C" BOOL QCALLTYPE RuntimeTypeHandle_IsEquivalentTo(QCall::TypeHandle rtT } #endif // FEATURE_TYPEEQUIVALENCE -FCIMPL1(MethodDesc *, RuntimeTypeHandle::GetFirstIntroducedMethod, ReflectClassBaseObject *pTypeUNSAFE) { - CONTRACTL { +FCIMPL1(MethodDesc *, RuntimeTypeHandle::GetFirstIntroducedMethod, ReflectClassBaseObject *pTypeUNSAFE) +{ + CONTRACTL + { FCALL_CHECK; PRECONDITION(CheckPointer(pTypeUNSAFE)); } @@ -194,18 +196,11 @@ FCIMPL1(MethodDesc *, RuntimeTypeHandle::GetFirstIntroducedMethod, ReflectClassB REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE); TypeHandle typeHandle = refType->GetType(); - - if (typeHandle.IsGenericVariable()) - FCThrowRes(kArgumentException, W("Arg_InvalidHandle")); - - if (typeHandle.IsTypeDesc()) { + _ASSERTE(!typeHandle.IsGenericVariable()); + if (typeHandle.IsTypeDesc()) return NULL; - } MethodTable* pMT = typeHandle.AsMethodTable(); - if (pMT == NULL) - return NULL; - MethodDesc* pMethod = MethodTable::IntroducedMethodIterator::GetFirst(pMT); return pMethod; } @@ -622,22 +617,14 @@ extern "C" void QCALLTYPE RuntimeTypeHandle_GetConstraints(QCall::TypeHandle pTy return; } -FCIMPL1(INT32, RuntimeTypeHandle::GetAttributes, ReflectClassBaseObject *pTypeUNSAFE) { - CONTRACTL { - FCALL_CHECK; - } - CONTRACTL_END; +FCIMPL1(INT32, RuntimeTypeHandle::GetAttributes, ReflectClassBaseObject *pTypeUNSAFE) +{ + FCALL_CONTRACT; REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE); - - if (refType == NULL) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); - TypeHandle typeHandle = refType->GetType(); - - if (typeHandle.IsTypeDesc()) { + if (typeHandle.IsTypeDesc()) return tdPublic; - } #ifdef FEATURE_COMINTEROP // __ComObject types are always public. @@ -757,19 +744,14 @@ FCIMPL1(LPCUTF8, RuntimeTypeHandle::GetUtf8Name, MethodTable* pMT) } FCIMPLEND -FCIMPL1(INT32, RuntimeTypeHandle::GetToken, ReflectClassBaseObject *pTypeUNSAFE) { - CONTRACTL { - FCALL_CHECK; - } - CONTRACTL_END; +FCIMPL1(INT32, RuntimeTypeHandle::GetToken, ReflectClassBaseObject *pTypeUNSAFE) +{ + FCALL_CONTRACT; REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE); - - if (refType == NULL) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); + _ASSERTE(refType != NULL); TypeHandle typeHandle = refType->GetType(); - if (typeHandle.IsTypeDesc() || typeHandle.IsArray()) { if (typeHandle.IsGenericVariable()) @@ -1161,10 +1143,6 @@ FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::CompareCanonicalHandles, ReflectClassBas REFLECTCLASSBASEREF refLeft = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pLeftUNSAFE); REFLECTCLASSBASEREF refRight = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pRightUNSAFE); - - if ((refLeft == NULL) || (refRight == NULL)) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); - FC_RETURN_BOOL(refLeft->GetType().GetCanonicalMethodTable() == refRight->GetType().GetCanonicalMethodTable()); } FCIMPLEND @@ -1174,10 +1152,6 @@ FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsGenericVariable, PTR_ReflectClassBaseO FCALL_CONTRACT; REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE); - - if (refType == NULL) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); - FC_RETURN_BOOL(refType->GetType().IsGenericVariable()); } FCIMPLEND @@ -1187,10 +1161,6 @@ FCIMPL1(INT32, RuntimeTypeHandle::GetGenericVariableIndex, PTR_ReflectClassBaseO FCALL_CONTRACT; REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE); - - if (refType == NULL) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); - return (INT32)refType->GetType().AsGenericVariable()->GetIndex(); } FCIMPLEND @@ -1200,10 +1170,6 @@ FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::ContainsGenericVariables, PTR_ReflectCla FCALL_CONTRACT; REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE); - - if (refType == NULL) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); - FC_RETURN_BOOL(refType->GetType().ContainsGenericVariables()); } FCIMPLEND @@ -1304,33 +1270,23 @@ FCIMPL1(LPCUTF8, RuntimeMethodHandle::GetUtf8Name, MethodDesc* pMethod) } FCIMPLEND -FCIMPL1(INT32, RuntimeMethodHandle::GetAttributes, MethodDesc *pMethod) { - CONTRACTL { - FCALL_CHECK; - } - CONTRACTL_END; - - if (!pMethod) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); - +FCIMPL1(INT32, RuntimeMethodHandle::GetAttributes, MethodDesc *pMethod) +{ + FCALL_CONTRACT; + _ASSERTE(pMethod != NULL); return (INT32)pMethod->GetAttrs(); } FCIMPLEND -FCIMPL1(INT32, RuntimeMethodHandle::GetImplAttributes, ReflectMethodObject *pMethodUNSAFE) { - CONTRACTL { - FCALL_CHECK; - } - CONTRACTL_END; - - if (!pMethodUNSAFE) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); +FCIMPL1(INT32, RuntimeMethodHandle::GetImplAttributes, ReflectMethodObject *pMethodUNSAFE) +{ + FCALL_CONTRACT; + _ASSERTE(pMethodUNSAFE != NULL); MethodDesc* pMethod = pMethodUNSAFE->GetMethod(); - INT32 attributes = 0; if (IsNilToken(pMethod->GetMemberDef())) - return attributes; + return 0; return (INT32)pMethod->GetImplAttrs(); } @@ -1338,26 +1294,16 @@ FCIMPLEND FCIMPL1(MethodTable*, RuntimeMethodHandle::GetMethodTable, MethodDesc *pMethod) { - CONTRACTL - { - FCALL_CHECK; - PRECONDITION(pMethod != NULL); - } - CONTRACTL_END; - + FCALL_CONTRACT; + _ASSERTE(pMethod != NULL); return pMethod->GetMethodTable(); } FCIMPLEND -FCIMPL1(INT32, RuntimeMethodHandle::GetSlot, MethodDesc *pMethod) { - CONTRACTL { - FCALL_CHECK; - } - CONTRACTL_END; - - if (!pMethod) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); - +FCIMPL1(INT32, RuntimeMethodHandle::GetSlot, MethodDesc *pMethod) +{ + FCALL_CONTRACT; + _ASSERTE(pMethod != NULL); return (INT32)pMethod->GetSlot(); } FCIMPLEND @@ -1759,9 +1705,7 @@ FCIMPLEND FCIMPL1(Object*, RuntimeMethodHandle::GetResolver, MethodDesc * pMethod) { FCALL_CONTRACT; - - if (!pMethod) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); + _ASSERTE(pMethod != NULL); OBJECTREF resolver = NULL; if (pMethod->IsLCGMethod()) @@ -1803,12 +1747,7 @@ extern "C" void QCALLTYPE RuntimeMethodHandle_Destroy(MethodDesc * pMethod) FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsTypicalMethodDefinition, ReflectMethodObject *pMethodUNSAFE) { FCALL_CONTRACT; - - if (!pMethodUNSAFE) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); - MethodDesc* pMethod = pMethodUNSAFE->GetMethod(); - FC_RETURN_BOOL(pMethod->IsTypicalMethodDefinition()); } FCIMPLEND @@ -2178,13 +2117,8 @@ FCIMPLEND FCIMPL1(INT32, RuntimeFieldHandle::GetAttributes, FieldDesc *pField) { - CONTRACTL - { - FCALL_CHECK; - PRECONDITION(pField != NULL); - } - CONTRACTL_END; - + FCALL_CONTRACT; + _ASSERTE(pField != NULL); return (INT32)pField->GetAttributes(); } FCIMPLEND @@ -2217,26 +2151,11 @@ FCIMPL1(INT32, RuntimeFieldHandle::GetToken, FieldDesc* pField) } FCIMPLEND -FCIMPL2(FieldDesc*, RuntimeFieldHandle::GetStaticFieldForGenericType, FieldDesc *pField, ReflectClassBaseObject *pDeclaringTypeUNSAFE) +FCIMPL2(FieldDesc*, RuntimeFieldHandle::GetStaticFieldForGenericType, FieldDesc* pField, MethodTable* pMT) { - CONTRACTL { - FCALL_CHECK; - } - CONTRACTL_END; - - REFLECTCLASSBASEREF refDeclaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE); - - if ((refDeclaringType == NULL) || (pField == NULL)) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); - - TypeHandle declaringType = refDeclaringType->GetType(); - - if (!pField) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); - if (declaringType.IsTypeDesc() || declaringType.IsArray()) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); - MethodTable *pMT = declaringType.AsMethodTable(); + FCALL_CONTRACT; + _ASSERTE ((pField != NULL) && (pMT != NULL)); _ASSERTE(pField->IsStatic()); if (pMT->HasGenericsStaticsInfo()) pField = pMT->GetFieldDescByIndex(pField->GetApproxEnclosingMethodTable()->GetIndexForFieldDesc(pField)); @@ -2252,8 +2171,7 @@ FCIMPL1(ReflectModuleBaseObject*, AssemblyHandle::GetManifestModule, AssemblyBas FCALL_CONTRACT; ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE); - if (refAssembly == NULL) - return NULL; + _ASSERTE(refAssembly != NULL); Module* pModule = refAssembly->GetAssembly()->GetModule(); OBJECTREF refModule = pModule->GetExposedObjectIfExists(); @@ -2261,24 +2179,18 @@ FCIMPL1(ReflectModuleBaseObject*, AssemblyHandle::GetManifestModule, AssemblyBas } FCIMPLEND -FCIMPL1(INT32, AssemblyHandle::GetToken, AssemblyBaseObject* pAssemblyUNSAFE) { +FCIMPL1(INT32, AssemblyHandle::GetTokenInternal, AssemblyBaseObject* pAssemblyUNSAFE) +{ FCALL_CONTRACT; ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE); - - if (refAssembly == NULL) - FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle")); + _ASSERTE(refAssembly != NULL); mdAssembly token = mdAssemblyNil; - IMDInternalImport *mdImport = refAssembly->GetAssembly()->GetMDImport(); - if (mdImport != 0) - { - if (FAILED(mdImport->GetAssemblyFromScope(&token))) - { - FCThrow(kBadImageFormatException); - } - } + IMDInternalImport* mdImport = refAssembly->GetAssembly()->GetMDImport(); + if (FAILED(mdImport->GetAssemblyFromScope(&token))) + return COR_E_BADIMAGEFORMAT; return token; } diff --git a/src/coreclr/vm/runtimehandles.h b/src/coreclr/vm/runtimehandles.h index dd549987bb9fe3..914b549495933a 100644 --- a/src/coreclr/vm/runtimehandles.h +++ b/src/coreclr/vm/runtimehandles.h @@ -261,7 +261,7 @@ class RuntimeFieldHandle static FCDECL1(INT32, GetAttributes, FieldDesc *pField); static FCDECL1(MethodTable*, GetApproxDeclaringMethodTable, FieldDesc *pField); static FCDECL1(INT32, GetToken, FieldDesc* pField); - static FCDECL2(FieldDesc*, GetStaticFieldForGenericType, FieldDesc *pField, ReflectClassBaseObject *pDeclaringType); + static FCDECL2(FieldDesc*, GetStaticFieldForGenericType, FieldDesc *pField, MethodTable *pMT); static FCDECL1(FC_BOOL_RET, AcquiresContextFromThis, FieldDesc *pField); static FCDECL1(Object*, GetLoaderAllocatorInternal, FieldDesc *pField); }; @@ -292,7 +292,7 @@ class AssemblyHandle { public: static FCDECL1(ReflectModuleBaseObject*, GetManifestModule, AssemblyBaseObject *pAssemblyUNSAFE); - static FCDECL1(INT32, GetToken, AssemblyBaseObject *pAssemblyUNSAFE); + static FCDECL1(INT32, GetTokenInternal, AssemblyBaseObject *pAssemblyUNSAFE); }; extern "C" void QCALLTYPE AssemblyHandle_GetManifestModuleSlow(QCall::ObjectHandleOnStack assembly, QCall::ObjectHandleOnStack module);