Skip to content

Commit 8edcc31

Browse files
Remove FCThrows from Reflection APIs. (#110766)
Co-authored-by: Jan Kotas <[email protected]>
1 parent e654570 commit 8edcc31

File tree

7 files changed

+65
-195
lines changed

7 files changed

+65
-195
lines changed

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,16 @@ static RuntimeModule GetManifestModuleWorker(RuntimeAssembly assembly)
727727
private static partial void GetManifestModuleSlow(ObjectHandleOnStack assembly, ObjectHandleOnStack module);
728728

729729
[MethodImpl(MethodImplOptions.InternalCall)]
730-
internal static extern int GetToken(RuntimeAssembly assembly);
730+
private static extern int GetTokenInternal(RuntimeAssembly assembly);
731+
732+
internal static int GetToken(RuntimeAssembly assembly)
733+
{
734+
int tokenMaybe = GetTokenInternal(assembly);
735+
// If the result is negative, it is an error code.
736+
if (tokenMaybe < 0)
737+
Marshal.ThrowExceptionForHR(tokenMaybe, new IntPtr(-1));
738+
return tokenMaybe;
739+
}
731740

732741
[RequiresUnreferencedCode("Types might be removed")]
733742
public sealed override Type[] GetForwardedTypes()

src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ internal static bool IsGenericMethodDefinition(IRuntimeMethodInfo method)
12581258
}
12591259

12601260
[MethodImpl(MethodImplOptions.InternalCall)]
1261-
internal static extern bool IsTypicalMethodDefinition(IRuntimeMethodInfo method);
1261+
private static extern bool IsTypicalMethodDefinition(IRuntimeMethodInfo method);
12621262

12631263
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeMethodHandle_GetTypicalMethodDefinition")]
12641264
private static partial void GetTypicalMethodDefinition(RuntimeMethodHandleInternal method, ObjectHandleOnStack outMethod);
@@ -1626,7 +1626,14 @@ internal static void SetValueDirect(RtFieldInfo field, RuntimeType fieldType, Ty
16261626
}
16271627

16281628
[MethodImpl(MethodImplOptions.InternalCall)]
1629-
internal static extern RuntimeFieldHandleInternal GetStaticFieldForGenericType(RuntimeFieldHandleInternal field, RuntimeType declaringType);
1629+
private static extern unsafe RuntimeFieldHandleInternal GetStaticFieldForGenericType(RuntimeFieldHandleInternal field, MethodTable* pMT);
1630+
1631+
internal static RuntimeFieldHandleInternal GetStaticFieldForGenericType(RuntimeFieldHandleInternal field, RuntimeType declaringType)
1632+
{
1633+
TypeHandle th = declaringType.GetNativeTypeHandle();
1634+
Debug.Assert(!th.IsTypeDesc);
1635+
return GetStaticFieldForGenericType(field, th.AsMethodTable());
1636+
}
16301637

16311638
[MethodImpl(MethodImplOptions.InternalCall)]
16321639
internal static extern bool AcquiresContextFromThis(RuntimeFieldHandleInternal field);

src/coreclr/vm/ecalllist.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ FCFuncEnd()
169169
FCFuncStart(gRuntimeAssemblyFuncs)
170170
FCFuncElement("GetIsDynamic", AssemblyNative::GetIsDynamic)
171171
FCFuncElement("GetManifestModule", AssemblyHandle::GetManifestModule)
172-
FCFuncElement("GetToken", AssemblyHandle::GetToken)
172+
FCFuncElement("GetTokenInternal", AssemblyHandle::GetTokenInternal)
173173
FCFuncEnd()
174174

175175
FCFuncStart(gAssemblyLoadContextFuncs)

src/coreclr/vm/fcall.cpp

-47
Original file line numberDiff line numberDiff line change
@@ -65,53 +65,6 @@ NOINLINE LPVOID __FCThrow(LPVOID __me, RuntimeExceptionKind reKind, UINT resID,
6565
return NULL;
6666
}
6767

68-
NOINLINE LPVOID __FCThrowArgument(LPVOID __me, RuntimeExceptionKind reKind, LPCWSTR argName, LPCWSTR resourceName)
69-
{
70-
STATIC_CONTRACT_THROWS;
71-
// This isn't strictly true... But the guarantee that we make here is
72-
// that we won't trigger without having setup a frame.
73-
// STATIC_CONTRACT_TRIGGER
74-
STATIC_CONTRACT_GC_NOTRIGGER;
75-
76-
// side effect the compiler can't remove
77-
if (FC_NO_TAILCALL != 1)
78-
return (LPVOID)(SIZE_T)(FC_NO_TAILCALL + 1);
79-
80-
FC_CAN_TRIGGER_GC();
81-
INCONTRACT(FCallCheck __fCallCheck(__FILE__, __LINE__));
82-
FC_GC_POLL_NOT_NEEDED(); // throws always open up for GC
83-
84-
HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_NOPOLL(Frame::FRAME_ATTR_CAPTURE_DEPTH_2);
85-
86-
switch (reKind) {
87-
case kArgumentNullException:
88-
if (resourceName) {
89-
COMPlusThrowArgumentNull(argName, resourceName);
90-
} else {
91-
COMPlusThrowArgumentNull(argName);
92-
}
93-
break;
94-
95-
case kArgumentOutOfRangeException:
96-
COMPlusThrowArgumentOutOfRange(argName, resourceName);
97-
break;
98-
99-
case kArgumentException:
100-
COMPlusThrowArgumentException(argName, resourceName);
101-
break;
102-
103-
default:
104-
// If you see this assert, add a case for your exception kind above.
105-
_ASSERTE(argName == NULL);
106-
COMPlusThrow(reKind, resourceName);
107-
}
108-
109-
HELPER_METHOD_FRAME_END();
110-
FC_CAN_TRIGGER_GC_END();
111-
_ASSERTE(!"Throw returned");
112-
return NULL;
113-
}
114-
11568
/**************************************************************************************/
11669
/* erect a frame in the FCALL and then poll the GC, objToProtect will be protected
11770
during the poll and the updated object returned. */

src/coreclr/vm/fcall.h

+1-12
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,9 @@ class CompletedFCallTransitionState
362362

363363
//==============================================================================================
364364
// This is where FCThrow ultimately ends up. Never call this directly.
365-
// Use the FCThrow() macros. __FCThrowArgument is the helper to throw ArgumentExceptions
366-
// with a resource taken from the managed resource manager.
365+
// Use the FCThrow() macro.
367366
//==============================================================================================
368367
LPVOID __FCThrow(LPVOID me, enum RuntimeExceptionKind reKind, UINT resID, LPCWSTR arg1, LPCWSTR arg2, LPCWSTR arg3);
369-
LPVOID __FCThrowArgument(LPVOID me, enum RuntimeExceptionKind reKind, LPCWSTR argumentName, LPCWSTR resourceName);
370368

371369
//==============================================================================================
372370
// FDECLn: A set of macros for generating header declarations for FC targets.
@@ -1236,15 +1234,6 @@ struct FCSigCheck {
12361234
return 0; \
12371235
}
12381236

1239-
// Use FCThrowRes to throw an exception with a localized error message from the
1240-
// ResourceManager in managed code.
1241-
#define FCThrowRes(reKind, resourceName) \
1242-
{ \
1243-
while (NULL == \
1244-
__FCThrowArgument(__me, reKind, NULL, resourceName)) {}; \
1245-
return 0; \
1246-
}
1247-
12481237
// The managed calling convention expects returned small types (e.g. bool) to be
12491238
// widened to 32-bit on return. The C/C++ calling convention does not guarantee returned
12501239
// small types to be widened on most platforms. The small types have to be artificially

0 commit comments

Comments
 (0)