Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b465094

Browse files
authored
Custom Marshalers in custom-ALC-loaded assemblies results in types loaded from crossing ALCs (#21606)
* Create repro for dotnet/coreclr#19654 * Update ICustomMarshaler.csproj * Update ICustomMarshaler.csproj * Clean up repro per feedback. * Add test case for different assemblies with the same CustomMarshaler name. * Move EEMarshalingData cache from AppDomain to LoaderAllocator. This fixes the custom-marshaler conflict when using unloadable assembly contexts. * Internalize the LoaderHeap* parameter. * Add the pointer to the requesting assembly to the hashtable key. * Fix linux-musl build break. * Move Crst out of FEATURE_COMINTEROP block. * Make sure to copy over the assembly pointer to the key that's actually stored in the hash table. * Add comment for m_invokingAssembly. * Move all usages of EEMarshallingData to hang off the correct loader allocator instead of always the global one. * Change to m_InteropDataCrst since this EEMarshallingData can be used in preemptive GC mode. * Always init m_InteropDataCrst (since it's used by EEMarshallingData as well as COM). * PR Feedback. * Remove extraneous inlines.
1 parent e62ee27 commit b465094

27 files changed

+463
-146
lines changed

src/vm/appdomain.cpp

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,6 @@ BaseDomain::BaseDomain()
711711
m_handleStore = NULL;
712712
#endif
713713

714-
m_pMarshalingData = NULL;
715-
716714
#ifdef FEATURE_COMINTEROP
717715
m_pMngStdInterfacesInfo = NULL;
718716
m_pWinRtBinder = NULL;
@@ -785,9 +783,6 @@ void BaseDomain::Init()
785783
m_crstAssemblyList.Init(CrstAssemblyList, CrstFlags(
786784
CRST_GC_NOTRIGGER_WHEN_TAKEN | CRST_DEBUGGER_THREAD | CRST_TAKEN_DURING_SHUTDOWN));
787785

788-
// Initialize the EE marshaling data to NULL.
789-
m_pMarshalingData = NULL;
790-
791786
#ifdef FEATURE_COMINTEROP
792787
// Allocate the managed standard interfaces information.
793788
m_pMngStdInterfacesInfo = new MngStdInterfacesInfo();
@@ -1805,51 +1800,6 @@ OBJECTREF AppDomain::GetMissingObject()
18051800

18061801
#ifndef DACCESS_COMPILE
18071802

1808-
EEMarshalingData *BaseDomain::GetMarshalingData()
1809-
{
1810-
CONTRACT (EEMarshalingData*)
1811-
{
1812-
THROWS;
1813-
GC_TRIGGERS;
1814-
MODE_ANY;
1815-
INJECT_FAULT(COMPlusThrowOM());
1816-
POSTCONDITION(CheckPointer(m_pMarshalingData));
1817-
}
1818-
CONTRACT_END;
1819-
1820-
if (!m_pMarshalingData)
1821-
{
1822-
// Take the lock
1823-
CrstHolder holder(&m_InteropDataCrst);
1824-
1825-
if (!m_pMarshalingData)
1826-
{
1827-
LoaderHeap* pHeap = GetLoaderAllocator()->GetLowFrequencyHeap();
1828-
m_pMarshalingData = new (pHeap) EEMarshalingData(this, pHeap, &m_DomainCrst);
1829-
}
1830-
}
1831-
1832-
RETURN m_pMarshalingData;
1833-
}
1834-
1835-
void BaseDomain::DeleteMarshalingData()
1836-
{
1837-
CONTRACTL
1838-
{
1839-
NOTHROW;
1840-
GC_TRIGGERS;
1841-
MODE_ANY;
1842-
}
1843-
CONTRACTL_END;
1844-
1845-
// We are in shutdown - no need to take any lock
1846-
if (m_pMarshalingData)
1847-
{
1848-
delete m_pMarshalingData;
1849-
m_pMarshalingData = NULL;
1850-
}
1851-
}
1852-
18531803
#ifndef CROSSGEN_COMPILE
18541804

18551805
STRINGREF *BaseDomain::IsStringInterned(STRINGREF *pString)

src/vm/appdomain.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,14 +1121,6 @@ class BaseDomain
11211121
return m_pWinRtBinder;
11221122
}
11231123
#endif // FEATURE_COMINTEROP
1124-
1125-
//****************************************************************************************
1126-
// This method returns marshaling data that the EE uses that is stored on a per app domain
1127-
// basis.
1128-
EEMarshalingData *GetMarshalingData();
1129-
1130-
// Deletes marshaling data at shutdown (which contains cached factories that needs to be released)
1131-
void DeleteMarshalingData();
11321124

11331125
#ifdef _DEBUG
11341126
BOOL OwnDomainLocalBlockLock()
@@ -1329,8 +1321,6 @@ class BaseDomain
13291321
// The large heap handle table critical section.
13301322
CrstExplicitInit m_LargeHeapHandleTableCrst;
13311323

1332-
EEMarshalingData *m_pMarshalingData;
1333-
13341324
#ifdef FEATURE_COMINTEROP
13351325
// Information regarding the managed standard interfaces.
13361326
MngStdInterfacesInfo *m_pMngStdInterfacesInfo;

src/vm/ceemain.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,11 +1189,6 @@ void InnerCoEEShutDownCOM()
11891189
// Release all of the RCWs in all contexts in all caches.
11901190
ReleaseRCWsInCaches(NULL);
11911191

1192-
// Release all marshaling data in all AppDomains
1193-
AppDomainIterator i(TRUE);
1194-
while (i.Next())
1195-
i.GetDomain()->DeleteMarshalingData();
1196-
11971192
#ifdef FEATURE_APPX
11981193
// Cleanup cached factory pointer in SynchronizationContextNative
11991194
SynchronizationContextNative::Cleanup();

src/vm/cominterfacemarshaler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ void COMInterfaceMarshaler::MarshalToNonRCWType(OBJECTREF *poref)
745745
LPCWSTR pwszRawUri = hsRawUri.GetRawBuffer(&cchRawUri);
746746
gc.refRawURI = StringObject::NewString(pwszRawUri, cchRawUri);
747747

748-
UriMarshalingInfo *pUriMarshalingInfo = GetAppDomain()->GetMarshalingData()->GetUriMarshalingInfo();
748+
UriMarshalingInfo *pUriMarshalingInfo = GetAppDomain()->GetLoaderAllocator()->GetMarshalingData()->GetUriMarshalingInfo();
749749
MethodDesc* pSystemUriCtorMD = pUriMarshalingInfo->GetSystemUriCtorMD();
750750

751751
MethodTable *pMTSystemUri = pUriMarshalingInfo->GetSystemUriType().AsMethodTable();
@@ -787,7 +787,7 @@ void COMInterfaceMarshaler::MarshalToNonRCWType(OBJECTREF *poref)
787787
case WinMDAdapter::RedirectedTypeIndex_System_ComponentModel_PropertyChangedEventArgs:
788788
{
789789
MethodDesc *pMD;
790-
EventArgsMarshalingInfo *pInfo = GetAppDomain()->GetMarshalingData()->GetEventArgsMarshalingInfo();
790+
EventArgsMarshalingInfo *pInfo = GetAppDomain()->GetLoaderAllocator()->GetMarshalingData()->GetEventArgsMarshalingInfo();
791791

792792
if (index == WinMDAdapter::RedirectedTypeIndex_System_Collections_Specialized_NotifyCollectionChangedEventArgs)
793793
pMD = pInfo->GetWinRTNCCEventArgsToSystemNCCEventArgsMD();

src/vm/custommarshalerinfo.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// Implementation of the custom marshaler info class.
2424
//==========================================================================
2525

26-
CustomMarshalerInfo::CustomMarshalerInfo(BaseDomain *pDomain, TypeHandle hndCustomMarshalerType, TypeHandle hndManagedType, LPCUTF8 strCookie, DWORD cCookieStrBytes)
26+
CustomMarshalerInfo::CustomMarshalerInfo(LoaderAllocator *pLoaderAllocator, TypeHandle hndCustomMarshalerType, TypeHandle hndManagedType, LPCUTF8 strCookie, DWORD cCookieStrBytes)
2727
: m_NativeSize(0)
2828
, m_hndManagedType(hndManagedType)
2929
, m_hndCustomMarshaler(NULL)
@@ -38,7 +38,7 @@ CustomMarshalerInfo::CustomMarshalerInfo(BaseDomain *pDomain, TypeHandle hndCust
3838
THROWS;
3939
GC_TRIGGERS;
4040
MODE_COOPERATIVE;
41-
PRECONDITION(CheckPointer(pDomain));
41+
PRECONDITION(CheckPointer(pLoaderAllocator));
4242
}
4343
CONTRACTL_END;
4444

@@ -112,7 +112,7 @@ CustomMarshalerInfo::CustomMarshalerInfo(BaseDomain *pDomain, TypeHandle hndCust
112112
IDS_EE_NOCUSTOMMARSHALER,
113113
GetFullyQualifiedNameForClassW(hndCustomMarshalerType.GetMethodTable()));
114114
}
115-
m_hndCustomMarshaler = pDomain->CreateHandle(CustomMarshalerObj);
115+
m_hndCustomMarshaler = pLoaderAllocator->GetDomain()->CreateHandle(CustomMarshalerObj);
116116

117117
// Retrieve the size of the native data.
118118
if (m_bDataIsByValue)
@@ -367,6 +367,7 @@ EEHashEntry_t * EECMHelperHashtableHelper::AllocateEntry(EECMHelperHashtableKey
367367
cbEntry += S_SIZE_T(pKey->GetMarshalerTypeNameByteCount());
368368
cbEntry += S_SIZE_T(pKey->GetCookieStringByteCount());
369369
cbEntry += S_SIZE_T(pKey->GetMarshalerInstantiation().GetNumArgs()) * S_SIZE_T(sizeof(LPVOID));
370+
cbEntry += S_SIZE_T(sizeof(LPVOID)); // For EECMHelperHashtableKey::m_invokingAssembly
370371

371372
if (cbEntry.IsOverflow())
372373
return NULL;
@@ -387,6 +388,7 @@ EEHashEntry_t * EECMHelperHashtableHelper::AllocateEntry(EECMHelperHashtableKey
387388
memcpy((void*)pEntryKey->m_strCookie, pKey->GetCookieString(), pKey->GetCookieStringByteCount());
388389
memcpy((void*)pEntryKey->m_Instantiation.GetRawArgs(), pKey->GetMarshalerInstantiation().GetRawArgs(),
389390
pEntryKey->m_Instantiation.GetNumArgs() * sizeof(LPVOID));
391+
pEntryKey->m_invokingAssembly = pKey->GetInvokingAssembly();
390392
}
391393
else
392394
{
@@ -401,6 +403,7 @@ EEHashEntry_t * EECMHelperHashtableHelper::AllocateEntry(EECMHelperHashtableKey
401403
pEntryKey->m_cCookieStrBytes = pKey->GetCookieStringByteCount();
402404
pEntryKey->m_strCookie = pKey->GetCookieString();
403405
pEntryKey->m_Instantiation = Instantiation(pKey->GetMarshalerInstantiation());
406+
pEntryKey->m_invokingAssembly = pKey->GetInvokingAssembly();
404407
}
405408

406409
return pEntry;
@@ -458,6 +461,9 @@ BOOL EECMHelperHashtableHelper::CompareKeys(EEHashEntry_t *pEntry, EECMHelperHas
458461
return FALSE;
459462
}
460463

464+
if (pEntryKey->GetInvokingAssembly() != pKey->GetInvokingAssembly())
465+
return FALSE;
466+
461467
return TRUE;
462468
}
463469

@@ -628,7 +634,7 @@ CustomMarshalerInfo *SharedCustomMarshalerHelper::GetCustomMarshalerInfo()
628634
CONTRACTL_END;
629635

630636
// Retrieve the marshalling data for the current app domain.
631-
EEMarshalingData *pMarshalingData = GetThread()->GetDomain()->GetMarshalingData();
637+
EEMarshalingData *pMarshalingData = GetThread()->GetDomain()->GetLoaderAllocator()->GetMarshalingData();
632638

633639
// Retrieve the custom marshaling information for the current shared custom
634640
// marshaling helper.

src/vm/custommarshalerinfo.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CustomMarshalerInfo
3636
{
3737
public:
3838
// Constructor and destructor.
39-
CustomMarshalerInfo(BaseDomain* pDomain, TypeHandle hndCustomMarshalerType, TypeHandle hndManagedType, LPCUTF8 strCookie, DWORD cCookieStrBytes);
39+
CustomMarshalerInfo(LoaderAllocator* pLoaderAllocator, TypeHandle hndCustomMarshalerType, TypeHandle hndManagedType, LPCUTF8 strCookie, DWORD cCookieStrBytes);
4040
~CustomMarshalerInfo();
4141

4242
// CustomMarshalerInfo's are always allocated on the loader heap so we need to redefine
@@ -114,51 +114,59 @@ class CustomMarshalerInfo
114114

115115
typedef SList<CustomMarshalerInfo, true> CMINFOLIST;
116116

117+
class Assembly;
117118

118119
class EECMHelperHashtableKey
119120
{
120121
public:
121-
EECMHelperHashtableKey(DWORD cMarshalerTypeNameBytes, LPCSTR strMarshalerTypeName, DWORD cCookieStrBytes, LPCSTR strCookie, Instantiation instantiation)
122+
EECMHelperHashtableKey(DWORD cMarshalerTypeNameBytes, LPCSTR strMarshalerTypeName, DWORD cCookieStrBytes, LPCSTR strCookie, Instantiation instantiation, Assembly* invokingAssembly)
122123
: m_cMarshalerTypeNameBytes(cMarshalerTypeNameBytes)
123124
, m_strMarshalerTypeName(strMarshalerTypeName)
124125
, m_cCookieStrBytes(cCookieStrBytes)
125126
, m_strCookie(strCookie)
126127
, m_Instantiation(instantiation)
128+
, m_invokingAssembly(invokingAssembly)
127129
{
128130
LIMITED_METHOD_CONTRACT;
129131
}
130132

131-
inline DWORD GetMarshalerTypeNameByteCount() const
133+
DWORD GetMarshalerTypeNameByteCount() const
132134
{
133135
LIMITED_METHOD_CONTRACT;
134136
return m_cMarshalerTypeNameBytes;
135137
}
136-
inline LPCSTR GetMarshalerTypeName() const
138+
LPCSTR GetMarshalerTypeName() const
137139
{
138140
LIMITED_METHOD_CONTRACT;
139141
return m_strMarshalerTypeName;
140142
}
141-
inline LPCSTR GetCookieString() const
143+
LPCSTR GetCookieString() const
142144
{
143145
LIMITED_METHOD_CONTRACT;
144146
return m_strCookie;
145147
}
146-
inline ULONG GetCookieStringByteCount() const
148+
ULONG GetCookieStringByteCount() const
147149
{
148150
LIMITED_METHOD_CONTRACT;
149151
return m_cCookieStrBytes;
150152
}
151-
inline Instantiation GetMarshalerInstantiation() const
153+
Instantiation GetMarshalerInstantiation() const
152154
{
153155
LIMITED_METHOD_CONTRACT;
154156
return m_Instantiation;
155157
}
158+
Assembly* GetInvokingAssembly() const
159+
{
160+
LIMITED_METHOD_CONTRACT;
161+
return m_invokingAssembly;
162+
}
156163

157164
DWORD m_cMarshalerTypeNameBytes;
158165
LPCSTR m_strMarshalerTypeName;
159166
DWORD m_cCookieStrBytes;
160167
LPCSTR m_strCookie;
161168
Instantiation m_Instantiation;
169+
Assembly* m_invokingAssembly;
162170
};
163171

164172

src/vm/dispparammarshaler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void DispParamOleColorMarshaler::MarshalNativeToManaged(VARIANT *pSrcVar, OBJECT
102102

103103
// Box the System.Drawing.Color value class and give back the boxed object.
104104
TypeHandle hndColorType =
105-
GetThread()->GetDomain()->GetMarshalingData()->GetOleColorMarshalingInfo()->GetColorType();
105+
GetThread()->GetDomain()->GetLoaderAllocator()->GetMarshalingData()->GetOleColorMarshalingInfo()->GetColorType();
106106

107107
*pDestObj = hndColorType.GetMethodTable()->Box(&MngColor);
108108
}

0 commit comments

Comments
 (0)