Skip to content

Commit 6bbf352

Browse files
radekdoulikjkotasAaronRobinsonMSFT
authored
Fix unboxing stubs on wasm (#120153)
* Fix unboxing stubs on wasm * Apply suggestions from code review Co-authored-by: Jan Kotas <jkotas@microsoft.com> Co-authored-by: Aaron Robinson <arobins@microsoft.com> * Feedback Remove GenerateShuffleArray and s_pShuffleThunkCache for FEATURE_PORTABLE_ENTRYPOINTS * Feedback Handle all pStubs for FEATURE_PORTABLE_ENTRYPOINTS * Do not double free * Add thunk for the IL stub * Update src/coreclr/vm/wasm/helpers.cpp Co-authored-by: Jan Kotas <jkotas@microsoft.com> * Update src/coreclr/vm/prestub.cpp Co-authored-by: Jan Kotas <jkotas@microsoft.com> * Finish the feedback * Feedback * Feedback * Feedback * Feedback --------- Co-authored-by: Jan Kotas <jkotas@microsoft.com> Co-authored-by: Aaron Robinson <arobins@microsoft.com>
1 parent 252034f commit 6bbf352

File tree

8 files changed

+62
-26
lines changed

8 files changed

+62
-26
lines changed

src/coreclr/clrdefinitions.cmake

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ if(CLR_CMAKE_TARGET_WIN32 AND CLR_CMAKE_TARGET_ARCH_AMD64)
6060
add_compile_definitions(OUT_OF_PROCESS_SETTHREADCONTEXT)
6161
endif(CLR_CMAKE_TARGET_WIN32 AND CLR_CMAKE_TARGET_ARCH_AMD64)
6262

63-
if(NOT CLR_CMAKE_TARGET_ARCH_I386)
64-
add_definitions(-DFEATURE_PORTABLE_SHUFFLE_THUNKS)
65-
endif()
66-
67-
if(CLR_CMAKE_TARGET_UNIX OR NOT CLR_CMAKE_TARGET_ARCH_I386)
68-
add_definitions(-DFEATURE_INSTANTIATINGSTUB_AS_IL)
69-
endif()
70-
7163
add_compile_definitions(FEATURE_CODE_VERSIONING)
7264
add_definitions(-DFEATURE_COLLECTIBLE_TYPES)
7365

src/coreclr/inc/switches.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@
165165
#define CHAIN_LOOKUP
166166
#endif // FEATURE_VIRTUAL_STUB_DISPATCH
167167

168+
#if !defined(FEATURE_PORTABLE_ENTRYPOINTS) && !defined(TARGET_X86)
169+
#define FEATURE_PORTABLE_SHUFFLE_THUNKS
170+
#endif
171+
172+
#if defined(TARGET_UNIX) || !defined(TARGET_X86)
173+
#define FEATURE_INSTANTIATINGSTUB_AS_IL
174+
#endif
175+
168176
// If this is uncommented, leaves a file "StubLog_<pid>.log" with statistics on the behavior
169177
// of stub-based interface dispatch.
170178
//#define STUB_LOGGING

src/coreclr/vm/comdelegate.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ BOOL GenerateShuffleArrayPortable(MethodDesc* pMethodSrc, MethodDesc *pMethodDst
635635
}
636636
#endif // FEATURE_PORTABLE_SHUFFLE_THUNKS
637637

638+
#ifndef FEATURE_PORTABLE_ENTRYPOINTS
638639
BOOL GenerateShuffleArray(MethodDesc* pInvoke, MethodDesc *pTargetMeth, SArray<ShuffleEntry> * pShuffleEntryArray)
639640
{
640641
STANDARD_VM_CONTRACT;
@@ -773,8 +774,8 @@ BOOL GenerateShuffleArray(MethodDesc* pInvoke, MethodDesc *pTargetMeth, SArray<S
773774
}
774775
return TRUE;
775776
}
776-
777777
static ShuffleThunkCache* s_pShuffleThunkCache = NULL;
778+
#endif // !FEATURE_PORTABLE_ENTRYPOINTS
778779

779780
// One time init.
780781
void COMDelegate::Init()
@@ -786,8 +787,9 @@ void COMDelegate::Init()
786787
MODE_ANY;
787788
}
788789
CONTRACTL_END;
789-
790+
#ifndef FEATURE_PORTABLE_ENTRYPOINTS
790791
s_pShuffleThunkCache = new ShuffleThunkCache(SystemDomain::GetGlobalLoaderAllocator()->GetStubHeap());
792+
#endif
791793
}
792794

793795
#ifdef FEATURE_COMINTEROP
@@ -913,6 +915,7 @@ static PCODE SetupShuffleThunk(MethodTable * pDelMT, MethodDesc *pTargetMeth)
913915

914916
MethodDesc *pMD = pClass->GetInvokeMethod();
915917

918+
#ifndef FEATURE_PORTABLE_ENTRYPOINTS
916919
// We haven't already setup a shuffle thunk, go do it now (which will cache the result automatically).
917920
StackSArray<ShuffleEntry> rShuffleEntryArray;
918921
if (GenerateShuffleArray(pMD, pTargetMeth, &rShuffleEntryArray))
@@ -928,6 +931,7 @@ static PCODE SetupShuffleThunk(MethodTable * pDelMT, MethodDesc *pTargetMeth)
928931
pShuffleThunk = pShuffleThunkCache->Canonicalize((const BYTE *)&rShuffleEntryArray[0], "DelegateShuffleThunk");
929932
}
930933
else
934+
#endif // !FEATURE_PORTABLE_ENTRYPOINTS
931935
{
932936
#if defined(TARGET_RISCV64) || defined(TARGET_LOONGARCH64)
933937
pShuffleThunk = CreateILDelegateShuffleThunk(pMD, isInstRetBuff);

src/coreclr/vm/comdelegate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ struct ShuffleEntry
170170

171171
#include <poppack.h>
172172

173+
#ifndef FEATURE_PORTABLE_ENTRYPOINTS
173174
class ShuffleThunkCache : public StubCacheBase
174175
{
175176
public:
@@ -204,5 +205,6 @@ class ShuffleThunkCache : public StubCacheBase
204205
return sizeof(ShuffleEntry) * (UINT)(1 + (pse - (ShuffleEntry*)pRawStub));
205206
}
206207
};
208+
#endif // !FEATURE_PORTABLE_ENTRYPOINTS
207209

208210
#endif // _COMDELEGATE_H_

src/coreclr/vm/loaderallocator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,7 @@ void AssemblyLoaderAllocator::Init()
17671767
m_dependentHandleToNativeObjectSetCrst.Init(CrstLeafLock, CRST_UNSAFE_ANYMODE);
17681768

17691769
LoaderAllocator::Init(NULL /*pExecutableHeapMemory*/);
1770+
#ifndef FEATURE_PORTABLE_ENTRYPOINTS
17701771
if (IsCollectible())
17711772
{
17721773
// TODO: the ShuffleThunkCache should really be using the m_pStubHeap, however the unloadability support
@@ -1775,6 +1776,7 @@ void AssemblyLoaderAllocator::Init()
17751776
// https://github.com/dotnet/runtime/issues/55697 tracks this issue.
17761777
m_pShuffleThunkCache = new ShuffleThunkCache(SystemDomain::GetGlobalLoaderAllocator()->GetExecutableHeap());
17771778
}
1779+
#endif // !FEATURE_PORTABLE_ENTRYPOINTS
17781780
}
17791781

17801782

@@ -1786,8 +1788,10 @@ AssemblyLoaderAllocator::~AssemblyLoaderAllocator()
17861788
m_binderToRelease = NULL;
17871789
}
17881790

1791+
#ifndef FEATURE_PORTABLE_ENTRYPOINTS
17891792
delete m_pShuffleThunkCache;
17901793
m_pShuffleThunkCache = NULL;
1794+
#endif // !FEATURE_PORTABLE_ENTRYPOINTS
17911795
}
17921796

17931797
void AssemblyLoaderAllocator::RegisterBinder(CustomAssemblyBinder* binderToRelease)

src/coreclr/vm/prestub.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ void MethodDesc::CreateDerivedTargetSigWithExtraParams(MetaSig& msig, SigBuilder
14591459

14601460
#ifdef FEATURE_INSTANTIATINGSTUB_AS_IL
14611461

1462-
Stub * CreateUnboxingILStubForSharedGenericValueTypeMethods(MethodDesc* pTargetMD)
1462+
Stub * CreateUnboxingILStubForValueTypeMethods(MethodDesc* pTargetMD)
14631463
{
14641464

14651465
CONTRACT(Stub*)
@@ -1510,13 +1510,16 @@ Stub * CreateUnboxingILStubForSharedGenericValueTypeMethods(MethodDesc* pTargetM
15101510
}
15111511
#endif
15121512

1513-
// Push the hidden context param
1514-
// The context is going to be captured from the thisptr
1515-
pCode->EmitLoadThis();
1516-
pCode->EmitLDFLDA(tokRawData);
1517-
pCode->EmitLDC(Object::GetOffsetOfFirstField());
1518-
pCode->EmitSUB();
1519-
pCode->EmitLDIND_I();
1513+
if (pTargetMD->RequiresInstMethodTableArg())
1514+
{
1515+
// Push the hidden context param
1516+
// The context is going to be captured from the thisptr
1517+
pCode->EmitLoadThis();
1518+
pCode->EmitLDFLDA(tokRawData);
1519+
pCode->EmitLDC(Object::GetOffsetOfFirstField());
1520+
pCode->EmitSUB();
1521+
pCode->EmitLDIND_I();
1522+
}
15201523

15211524
#ifndef TARGET_X86
15221525
if (msig.HasAsyncContinuation())
@@ -1727,13 +1730,16 @@ Stub * MakeUnboxingStubWorker(MethodDesc *pMD)
17271730
else
17281731
#endif
17291732
{
1733+
#ifdef FEATURE_PORTABLE_ENTRYPOINTS
1734+
pstub = CreateUnboxingILStubForValueTypeMethods(pUnboxedMD);
1735+
#else // !FEATURE_PORTABLE_ENTRYPOINTS
17301736
#ifdef FEATURE_INSTANTIATINGSTUB_AS_IL
17311737
#ifndef FEATURE_PORTABLE_SHUFFLE_THUNKS
17321738
if (pUnboxedMD->RequiresInstMethodTableArg())
17331739
#endif // !FEATURE_PORTABLE_SHUFFLE_THUNKS
17341740
{
17351741
_ASSERTE(pUnboxedMD->RequiresInstMethodTableArg());
1736-
pstub = CreateUnboxingILStubForSharedGenericValueTypeMethods(pUnboxedMD);
1742+
pstub = CreateUnboxingILStubForValueTypeMethods(pUnboxedMD);
17371743
}
17381744
#ifndef FEATURE_PORTABLE_SHUFFLE_THUNKS
17391745
else
@@ -1746,6 +1752,7 @@ Stub * MakeUnboxingStubWorker(MethodDesc *pMD)
17461752
pstub = sl.Link(pMD->GetLoaderAllocator()->GetStubHeap(), NEWSTUB_FL_NONE, "UnboxingStub");
17471753
}
17481754
#endif // !FEATURE_PORTABLE_SHUFFLE_THUNKS
1755+
#endif // FEATURE_PORTABLE_ENTRYPOINTS
17491756
}
17501757
RETURN pstub;
17511758
}
@@ -2377,6 +2384,15 @@ PCODE MethodDesc::DoPrestub(MethodTable *pDispatchingMT, CallerGCMode callerGCMo
23772384
}
23782385
else
23792386
{
2387+
#ifdef FEATURE_PORTABLE_ENTRYPOINTS
2388+
pCode = pStub->GetEntryPoint();
2389+
pStub->DecRef();
2390+
2391+
void* ilStubInterpData = PortableEntryPoint::GetInterpreterData(pCode);
2392+
_ASSERTE(ilStubInterpData != NULL);
2393+
SetInterpreterCode((InterpByteCodeStart*)ilStubInterpData);
2394+
SetCodeEntryPoint(pCode);
2395+
#else
23802396
if (!GetOrCreatePrecode()->SetTargetInterlocked(pStub->GetEntryPoint()))
23812397
{
23822398
if (pStub->HasExternalEntryPoint())
@@ -2396,12 +2412,12 @@ PCODE MethodDesc::DoPrestub(MethodTable *pDispatchingMT, CallerGCMode callerGCMo
23962412
// need to free the Stub allocation now.
23972413
pStub->DecRef();
23982414
}
2415+
#endif // !FEATURE_PORTABLE_ENTRYPOINTS
23992416
}
24002417

24012418
_ASSERTE(!IsPointingToPrestub());
24022419
_ASSERTE(HasStableEntryPoint());
24032420

2404-
24052421
pCode = DoBackpatch(pMT, pDispatchingMT, FALSE);
24062422

24072423
Return:

src/coreclr/vm/wasm/cgencpu.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ class StubLinkerCPU : public StubLinker
7474
{
7575
public:
7676
static void Init() { /* no-op on wasm */ }
77-
inline void EmitShuffleThunk(struct ShuffleEntry *pShuffleEntryArray) {
78-
_ASSERTE("The EmitShuffleThunk is not implemented on wasm");
79-
}
80-
inline VOID EmitComputedInstantiatingMethodStub(MethodDesc* pSharedMD, struct ShuffleEntry *pShuffleEntryArray, void* extraArg) {
81-
_ASSERTE("The EmitComputedInstantiatingMethodStub is not implemented on wasm");
82-
}
8377
};
8478

8579
//**********************************************************************

src/coreclr/vm/wasm/helpers.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,12 @@ namespace
553553
*(int32_t*)pRet = (*fptr)(ARG_IND(0), ARG(1));
554554
}
555555

556+
void CallFunc_I32_I32IND_I32_I32IND_I32_RetI32(PCODE pcode, int8_t *pArgs, int8_t *pRet)
557+
{
558+
int32_t (*fptr)(int32_t, int32_t, int32_t, int32_t, int32_t) = (int32_t (*)(int32_t, int32_t, int32_t, int32_t, int32_t))pcode;
559+
*(int32_t*)pRet = (*fptr)(ARG(0), ARG_IND(1), ARG(2), ARG_IND(3), ARG(4));
560+
}
561+
556562
void CallFunc_I32IND_I32_I32_I32_I32_I32_RetI32(PCODE pcode, int8_t *pArgs, int8_t *pRet)
557563
{
558564
int32_t (*fptr)(int32_t, int32_t, int32_t, int32_t, int32_t, int32_t) = (int32_t (*)(int32_t, int32_t, int32_t, int32_t, int32_t, int32_t))pcode;
@@ -690,6 +696,16 @@ namespace
690696
return (void*)&CallFunc_I32IND_I32_RetI32;
691697
}
692698
break;
699+
case 5:
700+
if (args[0] == ConvertType::ToI32 &&
701+
args[1] == ConvertType::ToI32Indirect &&
702+
args[2] == ConvertType::ToI32 &&
703+
args[3] == ConvertType::ToI32Indirect &&
704+
args[4] == ConvertType::ToI32)
705+
{
706+
return (void*)&CallFunc_I32_I32IND_I32_I32IND_I32_RetI32;
707+
}
708+
break;
693709
case 6:
694710
if (args[0] == ConvertType::ToI32Indirect &&
695711
args[1] == ConvertType::ToI32 &&

0 commit comments

Comments
 (0)