Skip to content

Commit

Permalink
Remove Helper Method Frames from all StubHelpers (#106793)
Browse files Browse the repository at this point in the history
* Convert GetCOMIPFromRCW

* Convert AllocateInternal

* Convert GetHRExceptionObject and GetCOMHRExceptionObject.

* Convert MarshalToManagedVaListInternal and MarshalToUnmanagedVaListInternal.

* Convert ValidateObject and ValidateByref.
  • Loading branch information
AaronRobinsonMSFT authored Aug 22, 2024
1 parent 06ae1be commit b7baeda
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 268 deletions.
69 changes: 45 additions & 24 deletions src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,25 +1353,26 @@ internal static void DestroyCleanupList(ref CleanupWorkListElement? pCleanupWork

internal static Exception GetHRExceptionObject(int hr)
{
Exception ex = InternalGetHRExceptionObject(hr);
ex.InternalPreserveStackTrace();
return ex;
Exception? ex = null;
GetHRExceptionObject(hr, ObjectHandleOnStack.Create(ref ex));
ex!.InternalPreserveStackTrace();
return ex!;
}

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Exception InternalGetHRExceptionObject(int hr);
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "StubHelpers_GetHRExceptionObject")]
private static partial void GetHRExceptionObject(int hr, ObjectHandleOnStack throwable);

#if FEATURE_COMINTEROP
internal static Exception GetCOMHRExceptionObject(int hr, IntPtr pCPCMD, object pThis)
{
Exception ex = InternalGetCOMHRExceptionObject(hr, pCPCMD, pThis);
ex.InternalPreserveStackTrace();
return ex;
Exception? ex = null;
GetCOMHRExceptionObject(hr, pCPCMD, ObjectHandleOnStack.Create(ref pThis), ObjectHandleOnStack.Create(ref ex));
ex!.InternalPreserveStackTrace();
return ex!;
}

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Exception InternalGetCOMHRExceptionObject(int hr, IntPtr pCPCMD, object? pThis);

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "StubHelpers_GetCOMHRExceptionObject")]
private static partial void GetCOMHRExceptionObject(int hr, IntPtr pCPCMD, ObjectHandleOnStack pThis, ObjectHandleOnStack throwable);
#endif // FEATURE_COMINTEROP

[ThreadStatic]
Expand Down Expand Up @@ -1426,7 +1427,27 @@ internal static void SafeHandleRelease(SafeHandle pHandle)

#if FEATURE_COMINTEROP
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern IntPtr GetCOMIPFromRCW(object objSrc, IntPtr pCPCMD, out IntPtr ppTarget, out bool pfNeedsRelease);
private static extern IntPtr GetCOMIPFromRCW(object objSrc, IntPtr pCPCMD, out IntPtr ppTarget);

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "StubHelpers_GetCOMIPFromRCWSlow")]
private static partial IntPtr GetCOMIPFromRCWSlow(ObjectHandleOnStack objSrc, IntPtr pCPCMD, out IntPtr ppTarget);

internal static IntPtr GetCOMIPFromRCW(object objSrc, IntPtr pCPCMD, out IntPtr ppTarget, out bool pfNeedsRelease)
{
IntPtr rcw = GetCOMIPFromRCW(objSrc, pCPCMD, out ppTarget);
if (rcw == IntPtr.Zero)
{
// If we didn't find the COM interface pointer in the cache we need to release the pointer.
pfNeedsRelease = true;
return GetCOMIPFromRCWWorker(objSrc, pCPCMD, out ppTarget);
}
pfNeedsRelease = false;
return rcw;

[MethodImpl(MethodImplOptions.NoInlining)]
static IntPtr GetCOMIPFromRCWWorker(object objSrc, IntPtr pCPCMD, out IntPtr ppTarget)
=> GetCOMIPFromRCWSlow(ObjectHandleOnStack.Create(ref objSrc), pCPCMD, out ppTarget);
}
#endif // FEATURE_COMINTEROP

#if PROFILING_SUPPORTED
Expand Down Expand Up @@ -1522,26 +1543,26 @@ internal static unsafe void LayoutDestroyNativeInternal(object obj, byte* pNativ
}
}

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern object AllocateInternal(IntPtr typeHandle);
[LibraryImport(RuntimeHelpers.QCall, EntryPoint="StubHelpers_MarshalToManagedVaList")]
internal static partial void MarshalToManagedVaList(IntPtr va_list, IntPtr pArgIterator);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void MarshalToUnmanagedVaListInternal(IntPtr va_list, uint vaListSize, IntPtr pArgIterator);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void MarshalToManagedVaListInternal(IntPtr va_list, IntPtr pArgIterator);
[LibraryImport(RuntimeHelpers.QCall, EntryPoint="StubHelpers_MarshalToUnmanagedVaList")]
internal static partial void MarshalToUnmanagedVaList(IntPtr va_list, uint vaListSize, IntPtr pArgIterator);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern uint CalcVaListSize(IntPtr va_list);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void ValidateObject(object obj, IntPtr pMD, object pThis);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void LogPinnedArgument(IntPtr localDesc, IntPtr nativeArg);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void ValidateByref(IntPtr byref, IntPtr pMD, object pThis); // the byref is pinned so we can safely "cast" it to IntPtr
[LibraryImport(RuntimeHelpers.QCall, EntryPoint="StubHelpers_ValidateObject")]
private static partial void ValidateObject(ObjectHandleOnStack obj, IntPtr pMD);

internal static void ValidateObject(object obj, IntPtr pMD)
=> ValidateObject(ObjectHandleOnStack.Create(ref obj), pMD);

[LibraryImport(RuntimeHelpers.QCall, EntryPoint="StubHelpers_ValidateByref")]
internal static partial void ValidateByref(IntPtr byref, IntPtr pMD); // the byref is pinned so we can safely "cast" it to IntPtr

[Intrinsic]
[MethodImpl(MethodImplOptions.InternalCall)]
Expand Down
5 changes: 2 additions & 3 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -940,9 +940,8 @@ DEFINE_METHOD(STUBHELPERS, CHECK_STRING_LENGTH, CheckStringLength,
DEFINE_METHOD(STUBHELPERS, FMT_CLASS_UPDATE_NATIVE_INTERNAL, FmtClassUpdateNativeInternal, SM_Obj_PtrByte_RefCleanupWorkListElement_RetVoid)
DEFINE_METHOD(STUBHELPERS, FMT_CLASS_UPDATE_CLR_INTERNAL, FmtClassUpdateCLRInternal, SM_Obj_PtrByte_RetVoid)
DEFINE_METHOD(STUBHELPERS, LAYOUT_DESTROY_NATIVE_INTERNAL, LayoutDestroyNativeInternal, SM_Obj_PtrByte_RetVoid)
DEFINE_METHOD(STUBHELPERS, ALLOCATE_INTERNAL, AllocateInternal, SM_IntPtr_RetObj)
DEFINE_METHOD(STUBHELPERS, MARSHAL_TO_MANAGED_VA_LIST_INTERNAL,MarshalToManagedVaListInternal, SM_IntPtr_IntPtr_RetVoid)
DEFINE_METHOD(STUBHELPERS, MARSHAL_TO_UNMANAGED_VA_LIST_INTERNAL,MarshalToUnmanagedVaListInternal,SM_IntPtr_UInt_IntPtr_RetVoid)
DEFINE_METHOD(STUBHELPERS, MARSHAL_TO_MANAGED_VA_LIST, MarshalToManagedVaList, SM_IntPtr_IntPtr_RetVoid)
DEFINE_METHOD(STUBHELPERS, MARSHAL_TO_UNMANAGED_VA_LIST, MarshalToUnmanagedVaList, SM_IntPtr_UInt_IntPtr_RetVoid)
DEFINE_METHOD(STUBHELPERS, CALC_VA_LIST_SIZE, CalcVaListSize, SM_IntPtr_RetUInt)
DEFINE_METHOD(STUBHELPERS, VALIDATE_OBJECT, ValidateObject, SM_Obj_IntPtr_Obj_RetVoid)
DEFINE_METHOD(STUBHELPERS, VALIDATE_BYREF, ValidateByref, SM_IntPtr_IntPtr_Obj_RetVoid)
Expand Down
12 changes: 5 additions & 7 deletions src/coreclr/vm/dllimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2277,30 +2277,28 @@ void NDirectStubLinker::EmitValidateLocal(ILCodeStream* pcsEmit, DWORD dwLocalNu

if (SF_IsDelegateStub(dwStubFlags))
{
pcsEmit->EmitLoadNullPtr();
pcsEmit->EmitLoadThis();
pcsEmit->EmitCALL(METHOD__DELEGATE__GET_INVOKE_METHOD, 1, 1);
}
else if (SF_IsCALLIStub(dwStubFlags))
{
pcsEmit->EmitLoadNullPtr();
pcsEmit->EmitLDNULL();
}
else
{
// P/Invoke, CLR->COM
EmitLoadStubContext(pcsEmit, dwStubFlags);
pcsEmit->EmitLDNULL();
}

if (fIsByref)
{
// StubHelpers.ValidateByref(byref, pMD, pThis)
pcsEmit->EmitCALL(METHOD__STUBHELPERS__VALIDATE_BYREF, 3, 0);
// StubHelpers.ValidateByref(byref, pMD)
pcsEmit->EmitCALL(METHOD__STUBHELPERS__VALIDATE_BYREF, 2, 0);
}
else
{
// StubHelpers.ValidateObject(obj, pMD, pThis)
pcsEmit->EmitCALL(METHOD__STUBHELPERS__VALIDATE_OBJECT, 3, 0);
// StubHelpers.ValidateObject(obj, pMD)
pcsEmit->EmitCALL(METHOD__STUBHELPERS__VALIDATE_OBJECT, 2, 0);
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,10 @@ FCFuncStart(gStubHelperFuncs)
FCFuncElement("TryGetStringTrailByte", StubHelpers::TryGetStringTrailByte)
FCFuncElement("SetLastError", StubHelpers::SetLastError)
FCFuncElement("ClearLastError", StubHelpers::ClearLastError)
FCFuncElement("InternalGetHRExceptionObject", StubHelpers::GetHRExceptionObject)
#ifdef FEATURE_COMINTEROP
FCFuncElement("InternalGetCOMHRExceptionObject", StubHelpers::GetCOMHRExceptionObject)
FCFuncElement("GetCOMIPFromRCW", StubHelpers::GetCOMIPFromRCW)
#endif // FEATURE_COMINTEROP
FCFuncElement("AllocateInternal", StubHelpers::AllocateInternal)
FCFuncElement("MarshalToUnmanagedVaListInternal", StubHelpers::MarshalToUnmanagedVaListInternal)
FCFuncElement("MarshalToManagedVaListInternal", StubHelpers::MarshalToManagedVaListInternal)
FCFuncElement("CalcVaListSize", StubHelpers::CalcVaListSize)
FCFuncElement("ValidateObject", StubHelpers::ValidateObject)
FCFuncElement("ValidateByref", StubHelpers::ValidateByref)
FCFuncElement("LogPinnedArgument", StubHelpers::LogPinnedArgument)
FCFuncElement("GetStubContext", StubHelpers::GetStubContext)
FCFuncElement("MulticastDebuggerTraceHelper", StubHelpers::MulticastDebuggerTraceHelper)
Expand Down
13 changes: 7 additions & 6 deletions src/coreclr/vm/ilmarshalers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2233,9 +2233,10 @@ void ILLayoutClassPtrMarshalerBase::EmitConvertSpaceNativeToCLR(ILCodeStream* ps
pslILEmit->EmitBRFALSE(pNullRefLabel);

pslILEmit->EmitLDTOKEN(pslILEmit->GetToken(m_pargs->m_pMT));
pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__TO_INTPTR, 1, 1);
// static object AllocateInternal(IntPtr typeHandle);
pslILEmit->EmitCALL(METHOD__STUBHELPERS__ALLOCATE_INTERNAL, 1, 1);
// static Type Type.GetTypeFromHandle(RuntimeTypeHandle handle)
pslILEmit->EmitCALL(METHOD__TYPE__GET_TYPE_FROM_HANDLE, 1, 1);
// static object RuntimeHelpers.GetUninitializedObject(Type type)
pslILEmit->EmitCALL(METHOD__RUNTIME_HELPERS__GET_UNINITIALIZED_OBJECT, 1, 1);
EmitStoreManagedValue(pslILEmit);
pslILEmit->EmitLabel(pNullRefLabel);
}
Expand Down Expand Up @@ -3522,11 +3523,11 @@ void ILArgIteratorMarshaler::EmitConvertSpaceAndContentsCLRToNative(ILCodeStream
pslILEmit->EmitLOCALLOC();
EmitStoreNativeValue(pslILEmit);

// void MarshalToUnmanagedVaListInternal(va_list, uint vaListSize, VARARGS* data)
// void MarshalToUnmanagedVaList(va_list, uint vaListSize, VARARGS* data)
EmitLoadNativeValue(pslILEmit);
pslILEmit->EmitLDLOC(dwVaListSizeLocal);
EmitLoadManagedHomeAddr(pslILEmit);
pslILEmit->EmitCALL(METHOD__STUBHELPERS__MARSHAL_TO_UNMANAGED_VA_LIST_INTERNAL, 3, 0);
pslILEmit->EmitCALL(METHOD__STUBHELPERS__MARSHAL_TO_UNMANAGED_VA_LIST, 3, 0);
}

void ILArgIteratorMarshaler::EmitConvertContentsNativeToCLR(ILCodeStream* pslILEmit)
Expand All @@ -3535,7 +3536,7 @@ void ILArgIteratorMarshaler::EmitConvertContentsNativeToCLR(ILCodeStream* pslILE
EmitLoadManagedHomeAddr(pslILEmit);

// void MarshalToManagedVaList(va_list va, VARARGS *dataout)
pslILEmit->EmitCALL(METHOD__STUBHELPERS__MARSHAL_TO_MANAGED_VA_LIST_INTERNAL, 2, 0);
pslILEmit->EmitCALL(METHOD__STUBHELPERS__MARSHAL_TO_MANAGED_VA_LIST, 2, 0);
}

LocalDesc ILArrayWithOffsetMarshaler::GetNativeType()
Expand Down
7 changes: 7 additions & 0 deletions src/coreclr/vm/qcallentrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,18 @@ static const Entry s_QCall[] =
DllImportEntry(StubHelpers_CreateCustomMarshalerHelper)
DllImportEntry(StubHelpers_SetStringTrailByte)
DllImportEntry(StubHelpers_ThrowInteropParamException)
DllImportEntry(StubHelpers_MarshalToManagedVaList)
DllImportEntry(StubHelpers_MarshalToUnmanagedVaList)
DllImportEntry(StubHelpers_ValidateObject)
DllImportEntry(StubHelpers_ValidateByref)
#ifdef PROFILING_SUPPORTED
DllImportEntry(StubHelpers_ProfilerBeginTransitionCallback)
DllImportEntry(StubHelpers_ProfilerEndTransitionCallback)
#endif
DllImportEntry(StubHelpers_GetHRExceptionObject)
#if defined(FEATURE_COMINTEROP)
DllImportEntry(StubHelpers_GetCOMHRExceptionObject)
DllImportEntry(StubHelpers_GetCOMIPFromRCWSlow)
DllImportEntry(ObjectMarshaler_ConvertToNative)
DllImportEntry(ObjectMarshaler_ConvertToManaged)
DllImportEntry(InterfaceMarshaler_ConvertToNative)
Expand Down
Loading

0 comments on commit b7baeda

Please sign in to comment.