Skip to content

[release/8.0-staging] Don't call EnsureInstanceActive under StaticBoxInitLock #108736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
7 changes: 4 additions & 3 deletions src/coreclr/vm/methodtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4200,6 +4200,9 @@ void MethodTable::AllocateRegularStaticBox(FieldDesc* pField, Object** boxedStat
MethodTable* pFieldMT = pField->GetFieldTypeHandleThrowing().GetMethodTable();
bool hasFixedAddr = HasFixedAddressVTStatics();

// Activate any dependent modules if necessary
pFieldMT->EnsureInstanceActive();

// Taking a lock since we might come here from multiple threads/places
CrstHolder crst(GetAppDomain()->GetStaticBoxInitLock());
Copy link
Member Author

@EgorBo EgorBo Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: we can also just remove the lock completely here (to improve perf a bit) and assign the field via InterlockedCompareExchange pattern here, but that might lead to small memory leaks as GC won't collect a redundant allocation on NonGC heap in case of a race.


Expand Down Expand Up @@ -4227,9 +4230,7 @@ OBJECTREF MethodTable::AllocateStaticBox(MethodTable* pFieldMT, BOOL fPinned, OB
CONTRACTL_END

_ASSERTE(pFieldMT->IsValueType());

// Activate any dependent modules if necessary
pFieldMT->EnsureInstanceActive();
_ASSERTE(pFieldMT->CheckInstanceActivated());

OBJECTREF obj = NULL;
if (canBeFrozen)
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/vm/threadstatics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ void ThreadLocalBlock::AllocateThreadStaticBoxes(MethodTable * pMT)
// We save this pinning handle in a list attached to the ThreadLocalBlock. When
// the thread dies, we release all the pinning handles in the list.

// Activate any dependent modules if necessary
pFieldMT->EnsureInstanceActive();

OBJECTHANDLE handle;
OBJECTREF obj = MethodTable::AllocateStaticBox(pFieldMT, pMT->HasFixedAddressVTStatics(), &handle);

Expand Down
Loading