Skip to content

Commit 804d730

Browse files
jkotasstephentoub
andauthored
Convert several FCalls to QCalls, plus misc cleanup (#45258)
* Convert several FCalls to QCalls, plus misc cleanup Co-authored-by: Stephen Toub <[email protected]>
1 parent bf1b48d commit 804d730

File tree

16 files changed

+152
-190
lines changed

16 files changed

+152
-190
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ private static void AddCustomAttributes(
12281228
}
12291229
else
12301230
{
1231-
attribute = attributeType.CreateInstanceDefaultCtor(publicOnly: false, skipCheckThis: true, fillCache: true, wrapExceptions: false)!;
1231+
attribute = attributeType.CreateInstanceDefaultCtor(publicOnly: false, wrapExceptions: false)!;
12321232

12331233
// It is allowed by the ECMA spec to have an empty signature blob
12341234
int blobLen = (int)((byte*)blobEnd - (byte*)blobStart);

src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public sealed override Delegate CreateDelegate(Type delegateType)
346346
// Compile the method since accessibility checks are done as part of compilation.
347347
GetMethodDescriptor();
348348
IRuntimeMethodInfo? methodHandle = m_methodHandle;
349-
System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(methodHandle != null ? methodHandle.Value : RuntimeMethodHandleInternal.EmptyHandle);
349+
System.Runtime.CompilerServices.RuntimeHelpers.CompileMethod(methodHandle != null ? methodHandle.Value : RuntimeMethodHandleInternal.EmptyHandle);
350350
GC.KeepAlive(methodHandle);
351351
}
352352

@@ -363,7 +363,7 @@ public sealed override Delegate CreateDelegate(Type delegateType, object? target
363363
// Compile the method since accessibility checks are done as part of compilation
364364
GetMethodDescriptor();
365365
IRuntimeMethodInfo? methodHandle = m_methodHandle;
366-
System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(methodHandle != null ? methodHandle.Value : RuntimeMethodHandleInternal.EmptyHandle);
366+
System.Runtime.CompilerServices.RuntimeHelpers.CompileMethod(methodHandle != null ? methodHandle.Value : RuntimeMethodHandleInternal.EmptyHandle);
367367
GC.KeepAlive(methodHandle);
368368
}
369369

src/coreclr/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.Reflection;
7+
using System.Runtime.CompilerServices;
68
using System.Runtime.InteropServices;
9+
using System.Runtime.Serialization;
710
using Internal.Runtime.CompilerServices;
811

912
namespace System.Runtime.CompilerServices
@@ -39,12 +42,16 @@ public static partial class RuntimeHelpers
3942
// This call will generate an exception if the specified class constructor threw an
4043
// exception when it ran.
4144

42-
[MethodImpl(MethodImplOptions.InternalCall)]
43-
private static extern void _RunClassConstructor(RuntimeType type);
45+
[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
46+
private static extern void RunClassConstructor(QCallTypeHandle type);
4447

4548
public static void RunClassConstructor(RuntimeTypeHandle type)
4649
{
47-
_RunClassConstructor(type.GetRuntimeType());
50+
RuntimeType rt = type.GetRuntimeType();
51+
if (rt is null)
52+
throw new ArgumentException(SR.InvalidOperation_HandleIsNotInitialized, nameof(type));
53+
54+
RunClassConstructor(new QCallTypeHandle(ref rt));
4855
}
4956

5057
// RunModuleConstructor causes the module constructor for the given type to be triggered
@@ -55,42 +62,41 @@ public static void RunClassConstructor(RuntimeTypeHandle type)
5562
// This call will generate an exception if the specified module constructor threw an
5663
// exception when it ran.
5764

58-
[MethodImpl(MethodImplOptions.InternalCall)]
59-
private static extern void _RunModuleConstructor(System.Reflection.RuntimeModule module);
65+
[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
66+
private static extern void RunModuleConstructor(QCallModule module);
6067

6168
public static void RunModuleConstructor(ModuleHandle module)
6269
{
63-
_RunModuleConstructor(module.GetRuntimeModule());
70+
RuntimeModule rm = module.GetRuntimeModule();
71+
if (rm is null)
72+
throw new ArgumentException(SR.InvalidOperation_HandleIsNotInitialized, nameof(module));
73+
74+
RunModuleConstructor(new QCallModule(ref rm));
6475
}
6576

77+
[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
78+
internal static extern void CompileMethod(RuntimeMethodHandleInternal method);
6679

6780
[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
68-
internal static extern void _CompileMethod(RuntimeMethodHandleInternal method);
81+
private static extern unsafe void PrepareMethod(RuntimeMethodHandleInternal method, IntPtr* pInstantiation, int cInstantiation);
6982

70-
[MethodImpl(MethodImplOptions.InternalCall)]
71-
private static extern unsafe void _PrepareMethod(IRuntimeMethodInfo method, IntPtr* pInstantiation, int cInstantiation);
83+
public static void PrepareMethod(RuntimeMethodHandle method) => PrepareMethod(method, null);
7284

73-
public static void PrepareMethod(RuntimeMethodHandle method)
85+
public static unsafe void PrepareMethod(RuntimeMethodHandle method, RuntimeTypeHandle[]? instantiation)
7486
{
75-
unsafe
76-
{
77-
_PrepareMethod(method.GetMethodInfo(), null, 0);
78-
}
79-
}
87+
IRuntimeMethodInfo methodInfo = method.GetMethodInfo();
88+
if (methodInfo == null)
89+
throw new ArgumentException(SR.InvalidOperation_HandleIsNotInitialized, nameof(method));
8090

81-
public static void PrepareMethod(RuntimeMethodHandle method, RuntimeTypeHandle[]? instantiation)
82-
{
8391
// defensive copy of user-provided array, per CopyRuntimeTypeHandles contract
8492
instantiation = (RuntimeTypeHandle[]?)instantiation?.Clone();
8593

86-
unsafe
94+
IntPtr[]? instantiationHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(instantiation, out int length);
95+
fixed (IntPtr* pInstantiation = instantiationHandles)
8796
{
88-
IntPtr[]? instantiationHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(instantiation, out int length);
89-
fixed (IntPtr* pInstantiation = instantiationHandles)
90-
{
91-
_PrepareMethod(method.GetMethodInfo(), pInstantiation, length);
92-
GC.KeepAlive(instantiation);
93-
}
97+
PrepareMethod(methodInfo.Value, pInstantiation, length);
98+
GC.KeepAlive(instantiation);
99+
GC.KeepAlive(methodInfo);
94100
}
95101
}
96102

@@ -138,15 +144,32 @@ public static int OffsetToStringData
138144
[MethodImpl(MethodImplOptions.InternalCall)]
139145
public static extern bool TryEnsureSufficientExecutionStack();
140146

141-
[MethodImpl(MethodImplOptions.InternalCall)]
142-
private static extern object GetUninitializedObjectInternal(
147+
public static object GetUninitializedObject(
143148
// This API doesn't call any constructors, but the type needs to be seen as constructed.
144149
// A type is seen as constructed if a constructor is kept.
145150
// This obviously won't cover a type with no constructor. Reference types with no
146151
// constructor are an academic problem. Valuetypes with no constructors are a problem,
147152
// but IL Linker currently treats them as always implicitly boxed.
148153
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
149-
Type type);
154+
Type type)
155+
{
156+
if (type is not RuntimeType rt)
157+
{
158+
if (type is null)
159+
{
160+
throw new ArgumentNullException(nameof(type), SR.ArgumentNull_Type);
161+
}
162+
163+
throw new SerializationException(SR.Format(SR.Serialization_InvalidType, type));
164+
}
165+
166+
object? obj = null;
167+
GetUninitializedObject(new QCallTypeHandle(ref rt), ObjectHandleOnStack.Create(ref obj));
168+
return obj!;
169+
}
170+
171+
[DllImport(RuntimeHelpers.QCall)]
172+
private static extern void GetUninitializedObject(QCallTypeHandle type, ObjectHandleOnStack retObject);
150173

151174
[MethodImpl(MethodImplOptions.InternalCall)]
152175
internal static extern object AllocateUninitializedClone(object obj);
@@ -280,11 +303,11 @@ public static IntPtr AllocateTypeAssociatedMemory(Type type, int size)
280303
if (size < 0)
281304
throw new ArgumentOutOfRangeException(nameof(size));
282305

283-
return AllocateTypeAssociatedMemoryInternal(new QCallTypeHandle(ref rt), (uint)size);
306+
return AllocateTypeAssociatedMemory(new QCallTypeHandle(ref rt), (uint)size);
284307
}
285308

286309
[DllImport(RuntimeHelpers.QCall)]
287-
private static extern IntPtr AllocateTypeAssociatedMemoryInternal(QCallTypeHandle type, uint size);
310+
private static extern IntPtr AllocateTypeAssociatedMemory(QCallTypeHandle type, uint size);
288311

289312
[MethodImpl(MethodImplOptions.InternalCall)]
290313
private static extern IntPtr AllocTailCallArgBuffer(int size, IntPtr gcDesc);

src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private static void PrelinkCore(MethodInfo m)
200200
private static object PtrToStructureHelper(IntPtr ptr, Type structureType)
201201
{
202202
var rt = (RuntimeType)structureType;
203-
object structure = rt.CreateInstanceDefaultCtor(publicOnly: false, skipCheckThis: false, fillCache: false, wrapExceptions: true)!;
203+
object structure = rt.CreateInstanceDefaultCtor(publicOnly: false, wrapExceptions: true)!;
204204
PtrToStructureHelper(ptr, structure, allowValueClasses: true);
205205
return structure;
206206
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,16 +1218,10 @@ public void GetObjectData(SerializationInfo info, StreamingContext context)
12181218

12191219
public unsafe struct ModuleHandle
12201220
{
1221-
// Returns handle for interop with EE. The handle is guaranteed to be non-null.
12221221
#region Public Static Members
1223-
public static readonly ModuleHandle EmptyHandle = GetEmptyMH();
1222+
public static readonly ModuleHandle EmptyHandle;
12241223
#endregion
12251224

1226-
private static ModuleHandle GetEmptyMH()
1227-
{
1228-
return default;
1229-
}
1230-
12311225
#region Private Data Members
12321226
private readonly RuntimeModule m_ptr;
12331227
#endregion

src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3895,7 +3895,7 @@ private void CreateInstanceCheckThis()
38953895
if (args.Length == 0 && (bindingAttr & BindingFlags.Public) != 0 && (bindingAttr & BindingFlags.Instance) != 0
38963896
&& (IsGenericCOMObjectImpl() || IsValueType))
38973897
{
3898-
instance = CreateInstanceDefaultCtor(publicOnly, skipCheckThis: false, fillCache: true, wrapExceptions);
3898+
instance = CreateInstanceDefaultCtor(publicOnly, wrapExceptions);
38993899
}
39003900
else
39013901
{
@@ -3967,7 +3967,7 @@ private void CreateInstanceCheckThis()
39673967
/// </summary>
39683968
[DebuggerStepThrough]
39693969
[DebuggerHidden]
3970-
internal object? CreateInstanceDefaultCtor(bool publicOnly, bool skipCheckThis, bool fillCache, bool wrapExceptions)
3970+
internal object? CreateInstanceDefaultCtor(bool publicOnly, bool wrapExceptions)
39713971
{
39723972
// Get or create the cached factory. Creating the cache will fail if one
39733973
// of our invariant checks fails; e.g., no appropriate ctor found.

src/coreclr/src/vm/corelib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ DEFINE_METHOD(RUNTIME_HELPERS, IS_BITWISE_EQUATABLE, IsBitwiseEquatable,
700700
DEFINE_METHOD(RUNTIME_HELPERS, GET_METHOD_TABLE, GetMethodTable, NoSig)
701701
DEFINE_METHOD(RUNTIME_HELPERS, GET_RAW_DATA, GetRawData, NoSig)
702702
DEFINE_METHOD(RUNTIME_HELPERS, GET_RAW_ARRAY_DATA, GetRawArrayData, NoSig)
703-
DEFINE_METHOD(RUNTIME_HELPERS, GET_UNINITIALIZED_OBJECT, GetUninitializedObject, NoSig)
703+
DEFINE_METHOD(RUNTIME_HELPERS, GET_UNINITIALIZED_OBJECT, GetUninitializedObject, SM_Type_RetObj)
704704
DEFINE_METHOD(RUNTIME_HELPERS, ENUM_EQUALS, EnumEquals, NoSig)
705705
DEFINE_METHOD(RUNTIME_HELPERS, ENUM_COMPARE_TO, EnumCompareTo, NoSig)
706706
DEFINE_METHOD(RUNTIME_HELPERS, ALLOC_TAILCALL_ARG_BUFFER, AllocTailCallArgBuffer, SM_Int_IntPtr_RetIntPtr)

src/coreclr/src/vm/ecalllist.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -871,18 +871,18 @@ FCFuncEnd()
871871
FCFuncStart(gRuntimeHelpers)
872872
FCFuncElement("GetObjectValue", ObjectNative::GetObjectValue)
873873
FCIntrinsic("InitializeArray", ArrayNative::InitializeArray, CORINFO_INTRINSIC_InitializeArray)
874-
FCFuncElement("_RunClassConstructor", ReflectionInvocation::RunClassConstructor)
875-
FCFuncElement("_RunModuleConstructor", ReflectionInvocation::RunModuleConstructor)
876-
QCFuncElement("_CompileMethod", ReflectionInvocation::CompileMethod)
877-
FCFuncElement("_PrepareMethod", ReflectionInvocation::PrepareMethod)
874+
QCFuncElement("RunClassConstructor", ReflectionInvocation::RunClassConstructor)
875+
QCFuncElement("RunModuleConstructor", ReflectionInvocation::RunModuleConstructor)
876+
QCFuncElement("CompileMethod", ReflectionInvocation::CompileMethod)
877+
QCFuncElement("PrepareMethod", ReflectionInvocation::PrepareMethod)
878878
FCFuncElement("PrepareDelegate", ReflectionInvocation::PrepareDelegate)
879879
FCFuncElement("GetHashCode", ObjectNative::GetHashCode)
880880
FCFuncElement("Equals", ObjectNative::Equals)
881881
FCFuncElement("AllocateUninitializedClone", ObjectNative::AllocateUninitializedClone)
882882
FCFuncElement("EnsureSufficientExecutionStack", ReflectionInvocation::EnsureSufficientExecutionStack)
883883
FCFuncElement("TryEnsureSufficientExecutionStack", ReflectionInvocation::TryEnsureSufficientExecutionStack)
884-
FCFuncElement("GetUninitializedObjectInternal", ReflectionSerialization::GetUninitializedObject)
885-
QCFuncElement("AllocateTypeAssociatedMemoryInternal", RuntimeTypeHandle::AllocateTypeAssociatedMemory)
884+
QCFuncElement("GetUninitializedObject", ReflectionSerialization::GetUninitializedObject)
885+
QCFuncElement("AllocateTypeAssociatedMemory", RuntimeTypeHandle::AllocateTypeAssociatedMemory)
886886
FCFuncElement("AllocTailCallArgBuffer", TailCallHelp::AllocTailCallArgBuffer)
887887
FCFuncElement("GetTailCallInfo", TailCallHelp::GetTailCallInfo)
888888
FCFuncElement("GetILBytesJitted", GetJittedBytes)

src/coreclr/src/vm/metasig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ DEFINE_METASIG(SM(Int_Str_IntPtr_Int_RetVoid, i s I i, v))
352352
DEFINE_METASIG(SM(Str_IntPtr_RetIntPtr, s I, I))
353353
DEFINE_METASIG(SM(Str_Bool_Int_RetV, s F i, v))
354354

355+
DEFINE_METASIG_T(SM(Type_RetObj, C(TYPE), j))
355356
DEFINE_METASIG_T(SM(Type_RetInt, C(TYPE), i))
356357
DEFINE_METASIG(SM(ArrByte_RetObj, a(b), j))
357358
DEFINE_METASIG(SM(ArrByte_Bool_RetObj, a(b) F, j))

0 commit comments

Comments
 (0)