Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ private LoaderAllocator()
m_scout = new LoaderAllocatorScout();
}

[UnmanagedCallersOnly]
private static unsafe void Create(object* pResult, Exception* pException)
{
try
{
*pResult = new LoaderAllocator();
}
catch (Exception ex)
{
*pException = ex;
}
}

#pragma warning disable CA1823, 414, 169
private LoaderAllocatorScout m_scout;
private object[] m_slots;
Expand Down
52 changes: 52 additions & 0 deletions src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2207,5 +2207,57 @@ internal struct CORINFO_EH_CLAUSE
internal abstract byte[]? ResolveSignature(int token, int fromMethod);
//
internal abstract MethodInfo GetDynamicMethod();

[UnmanagedCallersOnly]
internal static unsafe void GetJitContext(Resolver* pResolver, int* pSecurityControlFlags, RuntimeType* ppResult, Exception* pException)
{
try
{
*ppResult = pResolver->GetJitContext(out *pSecurityControlFlags);
}
catch (Exception ex)
{
*pException = ex;
}
}

[UnmanagedCallersOnly]
internal static unsafe void GetCodeInfo(Resolver* pResolver, int* pStackSize, int* pInitLocals, int* pEHCount, byte[]* ppResult, Exception* pException)
{
try
{
*ppResult = pResolver->GetCodeInfo(out *pStackSize, out *pInitLocals, out *pEHCount);
}
catch (Exception ex)
{
*pException = ex;
}
}

[UnmanagedCallersOnly]
internal static unsafe void GetLocalsSignature(Resolver* pResolver, byte[]* ppResult, Exception* pException)
{
try
{
*ppResult = pResolver->GetLocalsSignature();
}
catch (Exception ex)
{
*pException = ex;
}
}

[UnmanagedCallersOnly]
internal static unsafe void GetStringLiteral(Resolver* pResolver, int token, string* ppResult, Exception* pException)
{
try
{
*ppResult = pResolver->GetStringLiteral(token);
}
catch (Exception ex)
{
*pException = ex;
}
}
}
}
74 changes: 74 additions & 0 deletions src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,54 @@ internal static void MulticastDebuggerTraceHelper(object o, int count)
} // class StubHelpers

#if FEATURE_COMINTEROP
internal static class CultureInfoMarshaler
{
[UnmanagedCallersOnly]
internal static unsafe void GetCurrentCulture(bool bUICulture, object* pResult, Exception* pException)
{
try
{
*pResult = bUICulture
? Globalization.CultureInfo.CurrentUICulture
: Globalization.CultureInfo.CurrentCulture;
}
catch (Exception ex)
{
*pException = ex;
}
}

[UnmanagedCallersOnly]
internal static unsafe void SetCurrentCulture(bool bUICulture, Globalization.CultureInfo* pValue, Exception* pException)
{
try
{
if (bUICulture)
Globalization.CultureInfo.CurrentUICulture = *pValue;
else
Globalization.CultureInfo.CurrentCulture = *pValue;
}
catch (Exception ex)
{
*pException = ex;
}
}

[UnmanagedCallersOnly]
internal static unsafe void CreateCultureInfo(int culture, object* pResult, Exception* pException)
{
try
{
// Consider calling CultureInfo.GetCultureInfo that returns a cached instance to avoid this expensive creation.
*pResult = new Globalization.CultureInfo(culture);
}
catch (Exception ex)
{
*pException = ex;
}
}
}

internal static class ColorMarshaler
{
private static readonly MethodInvoker s_oleColorToDrawingColorMethod;
Expand Down Expand Up @@ -1657,6 +1705,32 @@ internal static int ConvertToNative(object? managedColor)
{
return (int)s_drawingColorToOleColorMethod.Invoke(null, managedColor)!;
}

[UnmanagedCallersOnly]
internal static unsafe void ConvertToManaged(int oleColor, object* pResult, Exception* pException)
{
try
{
*pResult = ConvertToManaged(oleColor);
}
catch (Exception ex)
{
*pException = ex;
}
}

[UnmanagedCallersOnly]
internal static unsafe void ConvertToNative(object* pSrcObj, int* pResult, Exception* pException)
{
try
{
*pResult = ConvertToNative(*pSrcObj);
}
catch (Exception ex)
{
*pException = ex;
}
}
}
#endif
}
4 changes: 4 additions & 0 deletions src/coreclr/vm/callhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,10 @@ void CallDefaultConstructor(OBJECTREF ref);
// from native code.
//

// Use CLR_BOOL_ARG to convert a BOOL value to a CLR_BOOL for passing to
// UnmanagedCallersOnlyCaller::InvokeThrowing when the managed parameter is bool.
#define CLR_BOOL_ARG(x) ((CLR_BOOL)(!!(x)))

// Helper class for calling managed methods marked with [UnmanagedCallersOnly].
// This provides a more efficient alternative to MethodDescCallSite for methods
// using the reverse P/Invoke infrastructure.
Expand Down
25 changes: 16 additions & 9 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -659,12 +659,12 @@ DEFINE_METHOD(PROPERTY, GET_GETTER, GetGetMethod,
DEFINE_CLASS(PROPERTY_INFO, Reflection, PropertyInfo)

DEFINE_CLASS(RESOLVER, System, Resolver)
DEFINE_METHOD(RESOLVER, GET_JIT_CONTEXT, GetJitContext, IM_RefInt_RetRuntimeType)
DEFINE_METHOD(RESOLVER, GET_CODE_INFO, GetCodeInfo, IM_RefInt_RefInt_RefInt_RetArrByte)
DEFINE_METHOD(RESOLVER, GET_LOCALS_SIGNATURE, GetLocalsSignature, IM_RetArrByte)
DEFINE_METHOD(RESOLVER, GET_JIT_CONTEXT, GetJitContext, SM_PtrResolver_PtrInt_PtrClass_PtrException_RetVoid)
DEFINE_METHOD(RESOLVER, GET_CODE_INFO, GetCodeInfo, SM_PtrResolver_PtrInt_PtrInt_PtrInt_PtrArrByte_PtrException_RetVoid)
DEFINE_METHOD(RESOLVER, GET_LOCALS_SIGNATURE, GetLocalsSignature, SM_PtrResolver_PtrArrByte_PtrException_RetVoid)
DEFINE_METHOD(RESOLVER, GET_EH_INFO, GetEHInfo, IM_Int_VoidPtr_RetVoid)
DEFINE_METHOD(RESOLVER, GET_RAW_EH_INFO, GetRawEHInfo, IM_RetArrByte)
DEFINE_METHOD(RESOLVER, GET_STRING_LITERAL, GetStringLiteral, IM_Int_RetStr)
DEFINE_METHOD(RESOLVER, GET_STRING_LITERAL, GetStringLiteral, SM_PtrResolver_Int_PtrStr_PtrException_RetVoid)
DEFINE_METHOD(RESOLVER, RESOLVE_TOKEN, ResolveToken, IM_Int_RefIntPtr_RefIntPtr_RefIntPtr_RetVoid)
DEFINE_METHOD(RESOLVER, RESOLVE_SIGNATURE, ResolveSignature, IM_IntInt_RetArrByte)

Expand Down Expand Up @@ -892,11 +892,11 @@ DEFINE_FIELD_U(rgiLastFrameFromForeignExceptionStackTrace, StackFrame
DEFINE_FIELD_U(iFrameCount, StackFrameHelper, iFrameCount)

DEFINE_CLASS(EVENT_SOURCE, Tracing, EventSource)
DEFINE_METHOD(EVENT_SOURCE, INITIALIZE_DEFAULT_EVENT_SOURCES, InitializeDefaultEventSources, SM_RetVoid)
DEFINE_METHOD(EVENT_SOURCE, INITIALIZE_DEFAULT_EVENT_SOURCES, InitializeDefaultEventSources, SM_PtrException_RetVoid)

DEFINE_CLASS(STARTUP_HOOK_PROVIDER, System, StartupHookProvider)
DEFINE_METHOD(STARTUP_HOOK_PROVIDER, MANAGED_STARTUP, ManagedStartup, SM_PtrChar_RetVoid)
DEFINE_METHOD(STARTUP_HOOK_PROVIDER, CALL_STARTUP_HOOK, CallStartupHook, SM_PtrChar_RetVoid)
DEFINE_METHOD(STARTUP_HOOK_PROVIDER, CALL_STARTUP_HOOK, CallStartupHook, SM_PtrChar_PtrException_RetVoid)

DEFINE_CLASS(STREAM, IO, Stream)
DEFINE_METHOD(STREAM, BEGIN_READ, BeginRead, IM_ArrByte_Int_Int_AsyncCallback_Object_RetIAsyncResult)
Expand Down Expand Up @@ -1124,9 +1124,16 @@ DEFINE_METHOD(MNGD_SAFE_ARRAY_MARSHALER, CONVERT_SPACE_TO_MANAGED, ConvertSpa
DEFINE_METHOD(MNGD_SAFE_ARRAY_MARSHALER, CONVERT_CONTENTS_TO_MANAGED, ConvertContentsToManaged, SM_IntPtr_RefObj_IntPtr_RetVoid)
DEFINE_METHOD(MNGD_SAFE_ARRAY_MARSHALER, CLEAR_NATIVE, ClearNative, SM_IntPtr_RefObj_IntPtr_RetVoid)

DEFINE_CLASS(CULTUREINFOMARSHALER, StubHelpers, CultureInfoMarshaler)
DEFINE_METHOD(CULTUREINFOMARSHALER, GET_CURRENT_CULTURE, GetCurrentCulture, NoSig)
DEFINE_METHOD(CULTUREINFOMARSHALER, SET_CURRENT_CULTURE, SetCurrentCulture, NoSig)
DEFINE_METHOD(CULTUREINFOMARSHALER, CREATE_CULTURE_INFO, CreateCultureInfo, NoSig)

DEFINE_CLASS(COLORMARSHALER, StubHelpers, ColorMarshaler)
DEFINE_METHOD(COLORMARSHALER, CONVERT_TO_NATIVE, ConvertToNative, SM_Obj_RetInt)
DEFINE_METHOD(COLORMARSHALER, CONVERT_TO_MANAGED, ConvertToManaged, SM_Int_RetObj)
DEFINE_METHOD(COLORMARSHALER, CONVERT_TO_NATIVE, ConvertToNative, SM_Obj_RetInt)
DEFINE_METHOD(COLORMARSHALER, CONVERT_TO_MANAGED, ConvertToManaged, SM_Int_RetObj)
DEFINE_METHOD(COLORMARSHALER, CONVERT_TO_NATIVE_UCO, ConvertToNative, SM_PtrObj_PtrInt_PtrException_RetVoid)
DEFINE_METHOD(COLORMARSHALER, CONVERT_TO_MANAGED_UCO, ConvertToManaged, SM_Int_PtrObj_PtrException_RetVoid)
DEFINE_FIELD(COLORMARSHALER, COLOR_TYPE, s_colorType)
#endif // FEATURE_COMINTEROP
END_ILLINK_FEATURE_SWITCH()
Expand Down Expand Up @@ -1223,7 +1230,7 @@ DEFINE_CLASS_U(Reflection, LoaderAllocator, LoaderAllocator
DEFINE_FIELD_U(m_slots, LoaderAllocatorObject, m_pSlots)
DEFINE_FIELD_U(m_slotsUsed, LoaderAllocatorObject, m_slotsUsed)
DEFINE_CLASS(LOADERALLOCATOR, Reflection, LoaderAllocator)
DEFINE_METHOD(LOADERALLOCATOR, CTOR, .ctor, IM_RetVoid)
DEFINE_METHOD(LOADERALLOCATOR, CREATE, Create, NoSig)

DEFINE_CLASS_U(Reflection, LoaderAllocatorScout, LoaderAllocatorScoutObject)
DEFINE_FIELD_U(m_nativeLoaderAllocator, LoaderAllocatorScoutObject, m_nativeLoaderAllocator)
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/corhost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,8 @@ HRESULT CorHost2::CreateAppDomainWithManager(
// Initialize default event sources
{
GCX_COOP();
MethodDescCallSite initEventSources(METHOD__EVENT_SOURCE__INITIALIZE_DEFAULT_EVENT_SOURCES);
initEventSources.Call(NULL);
UnmanagedCallersOnlyCaller initEventSources(METHOD__EVENT_SOURCE__INITIALIZE_DEFAULT_EVENT_SOURCES);
initEventSources.InvokeThrowing();
}
#endif // FEATURE_PERFTRACING

Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/vm/dispatchinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1555,8 +1555,8 @@ void DispatchInfo::InvokeMemberWorker(DispatchMemberInfo* pDispMemberInfo,
{
// If the method is culture aware, then set the specified culture on the thread.
GetCultureInfoForLCID(lcid, &pObjs->CultureInfo);
pObjs->OldCultureInfo = Thread::GetCulture(FALSE);
Thread::SetCulture(&pObjs->CultureInfo, FALSE);
pObjs->OldCultureInfo = GetCurrentCulture(FALSE);
SetCurrentCulture(&pObjs->CultureInfo, FALSE);
}

// If the method has custom marshalers then we will need to call
Expand Down Expand Up @@ -2284,7 +2284,7 @@ HRESULT DispatchInfo::InvokeMember(SimpleComCallWrapper *pSimpleWrap, DISPID id,

// If the culture was changed then restore it to the old culture.
if (Objs.OldCultureInfo != NULL)
Thread::SetCulture(&Objs.OldCultureInfo, FALSE);
SetCurrentCulture(&Objs.OldCultureInfo, FALSE);
}
GCPROTECT_END();
GCPROTECT_END();
Expand Down
21 changes: 19 additions & 2 deletions src/coreclr/vm/dllimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6168,7 +6168,7 @@ EXTERN_C void STDCALL GenericPInvokeCalliStubWorker(TransitionBlock * pTransitio
pFrame->Pop(CURRENT_THREAD);
}

EXTERN_C void LookupMethodByName(const char* fullQualifiedTypeName, const char* methodName, MethodDesc** ppMD)
EXTERN_C void LookupUnmanagedCallersOnlyMethodByName(const char* fullQualifiedTypeName, const char* methodName, MethodDesc** ppMD)
{
CONTRACTL
{
Expand All @@ -6185,7 +6185,24 @@ EXTERN_C void LookupMethodByName(const char* fullQualifiedTypeName, const char*
TypeHandle type = TypeName::GetTypeFromAsmQualifiedName(fullQualifiedTypeNameUtf8.GetUnicode(), /*bThrowIfNotFound*/ TRUE);
_ASSERTE(!type.IsTypeDesc());

*ppMD = MemberLoader::FindMethodByName(type.GetMethodTable(), methodName);
// Iterate the type looking for a method with the given name that has the
// UnmanagedCallersOnly attribute.
MethodTable* pMT = type.GetMethodTable();
MethodTable::MethodIterator it(pMT);
it.MoveToEnd();
for (; it.IsValid(); it.Prev())
{
MethodDesc* pMD = it.GetMethodDesc();
if (strcmp(pMD->GetNameOnNonArrayClass(), methodName) == 0
&& pMD->HasUnmanagedCallersOnlyAttribute())
{
*ppMD = pMD;
return;
}
}

// Fallback if no UCO match found.
*ppMD = MemberLoader::FindMethodByName(pMT, methodName);
}

namespace
Expand Down
Loading
Loading