Skip to content

Commit 35e4aad

Browse files
Ensure LoaderAllocator can't be collected while we clean handles on collectible LoaderAllocators (#99998)
1 parent ce1477b commit 35e4aad

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/coreclr/vm/loaderallocator.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -964,13 +964,11 @@ void LoaderAllocator::SetHandleValue(LOADERHANDLE handle, OBJECTREF value)
964964
{
965965
NOTHROW;
966966
GC_NOTRIGGER;
967-
MODE_ANY;
967+
MODE_COOPERATIVE;
968968
PRECONDITION(handle != NULL);
969969
}
970970
CONTRACTL_END;
971971

972-
GCX_COOP();
973-
974972
GCPROTECT_BEGIN(value);
975973

976974
// If the slot value does have the low bit set, then it is a simple pointer to the value

src/coreclr/vm/threadstatics.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,22 @@ void ThreadLocalBlock::FreeTLM(SIZE_T i, BOOL isThreadShuttingdown)
5252
ThreadLocalModule::CollectibleDynamicEntry *entry = (ThreadLocalModule::CollectibleDynamicEntry*)pThreadLocalModule->m_pDynamicClassTable[k].m_pDynamicEntry;
5353
PTR_LoaderAllocator pLoaderAllocator = entry->m_pLoaderAllocator;
5454

55-
if (entry->m_hGCStatics != 0)
56-
{
57-
pLoaderAllocator->FreeHandle(entry->m_hGCStatics);
58-
}
59-
if (entry->m_hNonGCStatics != 0)
55+
// LoaderAllocator may be collected when the thread is shutting down.
56+
// We enter coop mode to ensure that we get a valid value of the exposed object and
57+
// can safely clean up handles if it is not yet collected.
58+
GCX_COOP();
59+
60+
LOADERALLOCATORREF loaderAllocator = pLoaderAllocator->GetExposedObject();
61+
if (loaderAllocator != NULL)
6062
{
61-
pLoaderAllocator->FreeHandle(entry->m_hNonGCStatics);
63+
if (entry->m_hGCStatics != 0)
64+
{
65+
pLoaderAllocator->FreeHandle(entry->m_hGCStatics);
66+
}
67+
if (entry->m_hNonGCStatics != 0)
68+
{
69+
pLoaderAllocator->FreeHandle(entry->m_hNonGCStatics);
70+
}
6271
}
6372
}
6473
delete pThreadLocalModule->m_pDynamicClassTable[k].m_pDynamicEntry;

0 commit comments

Comments
 (0)