Skip to content

Commit

Permalink
Enable PInvoke stub compilation for R2R (#35229)
Browse files Browse the repository at this point in the history
R2R doesn't generate PInvoke stub for arm because of crossbitness issue.
This patch enables PInvoke stub compilation for R2R,
and updates native size, field offset, and so on based on target machine's bitness.
  • Loading branch information
clamp03 authored Jul 1, 2020
1 parent a2ef44f commit d69cfa0
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 123 deletions.
8 changes: 1 addition & 7 deletions src/coreclr/src/vm/compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ mdToken CEECompileInfo::TryEncodeMethodAsToken(
{
_ASSERTE(pResolvedToken != NULL);

Module * pReferencingModule = (Module *)pResolvedToken->tokenScope;
Module * pReferencingModule = GetModule(pResolvedToken->tokenScope);

if (!pReferencingModule->IsInCurrentVersionBubble())
return mdTokenNil;
Expand Down Expand Up @@ -5937,12 +5937,6 @@ void CEEPreloader::GenerateMethodStubs(
if (IsReadyToRunCompilation() && (!GetAppDomain()->ToCompilationDomain()->GetTargetModule()->IsSystem() || !pMD->IsNDirect()))
return;

#if defined(TARGET_ARM) && defined(TARGET_UNIX)
// Cross-bitness compilation of il stubs does not work. Disable here.
if (IsReadyToRunCompilation())
return;
#endif // defined(TARGET_ARM) && defined(TARGET_UNIX)

DWORD dwNGenStubFlags = NDIRECTSTUB_FL_NGENEDSTUB;

if (fNgenProfilerImage)
Expand Down
36 changes: 21 additions & 15 deletions src/coreclr/src/vm/dllimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2080,7 +2080,7 @@ void NDirectStubLinker::DoNDirect(ILCodeStream *pcsEmit, DWORD dwStubFlags, Meth
// for managed-to-unmanaged CALLI that requires marshaling, the target is passed
// as the secret argument to the stub by GenericPInvokeCalliHelper (asmhelpers.asm)
EmitLoadStubContext(pcsEmit, dwStubFlags);
#ifdef HOST_64BIT
#ifdef TARGET_64BIT
// the secret arg has been shifted to left and ORed with 1 (see code:GenericPInvokeCalliHelper)
pcsEmit->EmitLDC(1);
pcsEmit->EmitSHR_UN();
Expand All @@ -2093,7 +2093,14 @@ void NDirectStubLinker::DoNDirect(ILCodeStream *pcsEmit, DWORD dwStubFlags, Meth
{
EmitLoadStubContext(pcsEmit, dwStubFlags);
// pcsEmit->EmitCALL(METHOD__STUBHELPERS__GET_NDIRECT_TARGET, 1, 1);
pcsEmit->EmitLDC(offsetof(NDirectMethodDesc, ndirect.m_pWriteableData));

#ifdef _DEBUG // There are five more pointer values for _DEBUG
_ASSERTE(offsetof(NDirectMethodDesc, ndirect.m_pWriteableData) == sizeof(void*) * 8 + 8);
pcsEmit->EmitLDC(TARGET_POINTER_SIZE * 8 + 8); // offsetof(NDirectMethodDesc, ndirect.m_pWriteableData)
#else // _DEBUG
_ASSERTE(offsetof(NDirectMethodDesc, ndirect.m_pWriteableData) == sizeof(void*) * 3 + 8);
pcsEmit->EmitLDC(TARGET_POINTER_SIZE * 3 + 8); // offsetof(NDirectMethodDesc, ndirect.m_pWriteableData)
#endif // _DEBUG
pcsEmit->EmitADD();

if (decltype(NDirectMethodDesc::ndirect.m_pWriteableData)::isRelative)
Expand Down Expand Up @@ -3118,7 +3125,7 @@ BOOL NDirect::MarshalingRequired(MethodDesc *pMD, PCCOR_SIGNATURE pSig /*= NULL*
return TRUE;
}
}
if (i > 0) dwStackSize += sizeof(SLOT);
if (i > 0) dwStackSize += TARGET_POINTER_SIZE;
break;
}

Expand Down Expand Up @@ -3373,7 +3380,7 @@ static inline UINT GetStackOffsetFromStackSize(UINT stackSize, bool fThisCall)
if (fThisCall)
{
// -1 means that the argument is not on the stack
return (stackSize >= sizeof(SLOT) ? (stackSize - sizeof(SLOT)) : (UINT)-1);
return (stackSize >= TARGET_POINTER_SIZE ? (stackSize - TARGET_POINTER_SIZE) : (UINT)-1);
}
#endif // TARGET_X86
return stackSize;
Expand Down Expand Up @@ -3471,8 +3478,7 @@ static void CreateNDirectStubWorker(StubState* pss,
//
// Marshal the return value.
//

UINT nativeStackSize = (SF_IsCOMStub(dwStubFlags) ? sizeof(SLOT) : 0);
UINT nativeStackSize = (SF_IsCOMStub(dwStubFlags) ? TARGET_POINTER_SIZE : 0);
bool fStubNeedsCOM = SF_IsCOMStub(dwStubFlags);

// Normally we would like this to be false so that we use the correct signature
Expand Down Expand Up @@ -3589,7 +3595,7 @@ static void CreateNDirectStubWorker(StubState* pss,
fStubNeedsCOM |= info.MarshalerRequiresCOM();

// make sure that the first parameter is enregisterable
if (info.GetNativeArgSize() > sizeof(SLOT))
if (info.GetNativeArgSize() > TARGET_POINTER_SIZE)
COMPlusThrow(kMarshalDirectiveException, IDS_EE_NDIRECT_BADNATL_THISCALL);

argidx++;
Expand Down Expand Up @@ -3640,7 +3646,7 @@ static void CreateNDirectStubWorker(StubState* pss,
MethodTable *pRetMT = msig.GetRetTypeHandleThrowing().AsMethodTable();
if (IsUnmanagedValueTypeReturnedByRef(pRetMT->GetNativeSize()))
{
nativeStackSize += sizeof(LPVOID);
nativeStackSize += TARGET_POINTER_SIZE;
}
}
}
Expand All @@ -3653,7 +3659,7 @@ static void CreateNDirectStubWorker(StubState* pss,
if (argidx == iLCIDArg)
{
pss->MarshalLCID(argidx);
nativeStackSize += sizeof(LPVOID);
nativeStackSize += TARGET_POINTER_SIZE;

if (SF_IsReverseStub(dwStubFlags))
argOffset++;
Expand All @@ -3671,7 +3677,7 @@ static void CreateNDirectStubWorker(StubState* pss,
if (fThisCall && argidx == 1)
{
// make sure that the first parameter is enregisterable
if (info.GetNativeArgSize() > sizeof(SLOT))
if (info.GetNativeArgSize() > TARGET_POINTER_SIZE)
COMPlusThrow(kMarshalDirectiveException, IDS_EE_NDIRECT_BADNATL_THISCALL);
}

Expand All @@ -3684,7 +3690,7 @@ static void CreateNDirectStubWorker(StubState* pss,
if (argidx == iLCIDArg)
{
pss->MarshalLCID(argidx);
nativeStackSize += sizeof(LPVOID);
nativeStackSize += TARGET_POINTER_SIZE;

if (SF_IsReverseStub(dwStubFlags))
argOffset++;
Expand Down Expand Up @@ -3727,7 +3733,7 @@ static void CreateNDirectStubWorker(StubState* pss,
if (SF_IsHRESULTSwapping(dwStubFlags))
{
if (msig.GetReturnType() != ELEMENT_TYPE_VOID)
nativeStackSize += sizeof(LPVOID);
nativeStackSize += TARGET_POINTER_SIZE;
}

if (pMD->IsDynamicMethod())
Expand All @@ -3741,8 +3747,8 @@ static void CreateNDirectStubWorker(StubState* pss,
#ifdef TARGET_X86
if (fThisCall)
{
_ASSERTE(nativeStackSize >= sizeof(SLOT));
nativeStackSize -= sizeof(SLOT);
_ASSERTE(nativeStackSize >= TARGET_POINTER_SIZE);
nativeStackSize -= TARGET_POINTER_SIZE;
}
#else // TARGET_X86
//
Expand Down Expand Up @@ -3859,7 +3865,7 @@ static void CreateStructStub(ILStubState* pss,
if (pMD->IsDynamicMethod())
{
DynamicMethodDesc* pDMD = pMD->AsDynamicMethodDesc();
pDMD->SetNativeStackArgSize(4 * sizeof(SLOT)); // The native stack arg size is constant since the signature for struct stubs is constant.
pDMD->SetNativeStackArgSize(4 * TARGET_POINTER_SIZE); // The native stack arg size is constant since the signature for struct stubs is constant.
}

// FinishEmit needs to know the native stack arg size so we call it after the number
Expand Down
15 changes: 10 additions & 5 deletions src/coreclr/src/vm/ilmarshalers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3666,7 +3666,8 @@ void ILAsAnyMarshalerBase::EmitCreateMngdMarshaler(ILCodeStream* pslILEmit)
m_dwMngdMarshalerLocalNum = pslILEmit->NewLocal(marshalerType);
DWORD dwTmpLocalNum = pslILEmit->NewLocal(ELEMENT_TYPE_I);

pslILEmit->EmitLDC(sizeof(MngdNativeArrayMarshaler));
_ASSERTE(sizeof(MngdNativeArrayMarshaler) == sizeof(void*) * 3 + 16);
pslILEmit->EmitLDC(TARGET_POINTER_SIZE * 3 + 16); // sizeof(MngdNativeArrayMarshaler)
pslILEmit->EmitLOCALLOC();
pslILEmit->EmitSTLOC(dwTmpLocalNum);

Expand Down Expand Up @@ -3763,7 +3764,8 @@ void ILNativeArrayMarshaler::EmitCreateMngdMarshaler(ILCodeStream* pslILEmit)

m_dwMngdMarshalerLocalNum = pslILEmit->NewLocal(ELEMENT_TYPE_I);

pslILEmit->EmitLDC(sizeof(MngdNativeArrayMarshaler));
_ASSERTE(sizeof(MngdNativeArrayMarshaler) == sizeof(void*) * 3 + 16);
pslILEmit->EmitLDC(TARGET_POINTER_SIZE * 3 + 16); // sizeof(MngdNativeArrayMarshaler)
pslILEmit->EmitLOCALLOC();
pslILEmit->EmitSTLOC(m_dwMngdMarshalerLocalNum);

Expand Down Expand Up @@ -4379,7 +4381,8 @@ void ILFixedArrayMarshaler::EmitCreateMngdMarshaler(ILCodeStream* pslILEmit)

m_dwMngdMarshalerLocalNum = pslILEmit->NewLocal(ELEMENT_TYPE_I);

pslILEmit->EmitLDC(sizeof(MngdFixedArrayMarshaler));
_ASSERTE(sizeof(MngdFixedArrayMarshaler) == sizeof(void*) * 4 + 16);
pslILEmit->EmitLDC(TARGET_POINTER_SIZE * 4 + 16); // sizeof(MngdFixedArrayMarshaler)
pslILEmit->EmitLOCALLOC();
pslILEmit->EmitSTLOC(m_dwMngdMarshalerLocalNum);

Expand Down Expand Up @@ -4606,7 +4609,8 @@ void ILSafeArrayMarshaler::EmitCreateMngdMarshaler(ILCodeStream* pslILEmit)

m_dwMngdMarshalerLocalNum = pslILEmit->NewLocal(ELEMENT_TYPE_I);

pslILEmit->EmitLDC(sizeof(MngdSafeArrayMarshaler));
_ASSERTE(sizeof(MngdSafeArrayMarshaler) == sizeof(void*) * 2 + 8);
pslILEmit->EmitLDC(TARGET_POINTER_SIZE * 2 + 8); // sizeof(MngdSafeArrayMarshaler)
pslILEmit->EmitLOCALLOC();
pslILEmit->EmitSTLOC(m_dwMngdMarshalerLocalNum);

Expand Down Expand Up @@ -4902,7 +4906,8 @@ void ILReferenceCustomMarshaler::EmitCreateMngdMarshaler(ILCodeStream* pslILEmit

m_dwMngdMarshalerLocalNum = pslILEmit->NewLocal(ELEMENT_TYPE_I);

pslILEmit->EmitLDC(sizeof(MngdRefCustomMarshaler));
_ASSERTE(sizeof(MngdRefCustomMarshaler) == sizeof(void*));
pslILEmit->EmitLDC(TARGET_POINTER_SIZE); // sizeof(MngdRefCustomMarshaler)
pslILEmit->EmitLOCALLOC();
pslILEmit->EmitSTLOC(m_dwMngdMarshalerLocalNum);

Expand Down
Loading

0 comments on commit d69cfa0

Please sign in to comment.