Skip to content
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

Detect class inited status more correctly in prestub/jitinterface #104253

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 6 additions & 9 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,10 +1210,9 @@ CorInfoHelpFunc CEEInfo::getSharedStaticsHelper(FieldDesc * pField, MethodTable
{
STANDARD_VM_CONTRACT;

pFieldMT->AttemptToPreinit();
bool GCStatic = (pField->GetFieldType() == ELEMENT_TYPE_CLASS ||
pField->GetFieldType() == ELEMENT_TYPE_VALUETYPE);
bool noCtor = pFieldMT->IsClassInited();
bool noCtor = pFieldMT->IsClassInitedOrPreinited();
bool threadStatic = pField->IsThreadStatic();
bool isInexactMT = pFieldMT->IsSharedByGenericInstantiations();
bool isCollectible = pFieldMT->Collectible();
Expand Down Expand Up @@ -1451,7 +1450,7 @@ void CEEInfo::getFieldInfo (CORINFO_RESOLVED_TOKEN * pResolvedToken,
}

// We are not going through a helper. The constructor has to be triggered explicitly.
if (!pFieldMT->IsClassInited())
if (!pFieldMT->IsClassInitedOrPreinited())
fieldFlags |= CORINFO_FLG_FIELD_INITCLASS;
}
else
Expand Down Expand Up @@ -1536,10 +1535,9 @@ void CEEInfo::getFieldInfo (CORINFO_RESOLVED_TOKEN * pResolvedToken,
// Allocate space for the local class if necessary, but don't trigger
// class construction.
pFieldMT->EnsureStaticDataAllocated();
pFieldMT->AttemptToPreinit();

// We are not going through a helper. The constructor has to be triggered explicitly.
if (!pFieldMT->IsClassInited())
if (!pFieldMT->IsClassInitedOrPreinited())
fieldFlags |= CORINFO_FLG_FIELD_INITCLASS;

GCX_COOP();
Expand Down Expand Up @@ -3884,8 +3882,7 @@ CorInfoInitClassResult CEEInfo::initClass(

MethodTable *pTypeToInitMT = typeToInitTH.AsMethodTable();

pTypeToInitMT->AttemptToPreinit();
if (pTypeToInitMT->IsClassInited())
if (pTypeToInitMT->IsClassInitedOrPreinited())
{
// If the type is initialized there really is nothing to do.
result = CORINFO_INITCLASS_INITIALIZED;
Expand Down Expand Up @@ -11725,7 +11722,7 @@ bool CEEInfo::getStaticFieldContent(CORINFO_FIELD_HANDLE fieldHnd, uint8_t* buff
// class construction.
pEnclosingMT->EnsureStaticDataAllocated();

if (!field->IsThreadStatic() && pEnclosingMT->IsClassInited() && IsFdInitOnly(field->GetAttributes()))
if (!field->IsThreadStatic() && pEnclosingMT->IsClassInitedOrPreinited() && IsFdInitOnly(field->GetAttributes()))
{
if (field->IsObjRef())
{
Expand Down Expand Up @@ -11913,7 +11910,7 @@ CORINFO_CLASS_HANDLE CEEJitInfo::getStaticFieldCurrentClass(CORINFO_FIELD_HANDLE
VALIDATEOBJECTREF(fieldObj);

// Check for initialization before looking at the value
isClassInitialized = !!pEnclosingMT->IsClassInited();
isClassInitialized = !!pEnclosingMT->IsClassInitedOrPreinited();

if (fieldObj != NULL)
{
Expand Down
18 changes: 18 additions & 0 deletions src/coreclr/vm/methodtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3795,6 +3795,24 @@ void MethodTable::EnsureStaticDataAllocated()
pAuxiliaryData->SetIsStaticDataAllocated();
}

BOOL MethodTable::IsClassInitedOrPreinited()
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
INJECT_FAULT(COMPlusThrowOM());
}
CONTRACTL_END;

if (IsClassInited())
return TRUE;

AttemptToPreinit();
Copy link
Member

Choose a reason for hiding this comment

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

AttemptToPreinit is a bit complex method. We may end up calling it here repeatedly number of times.

Would it better to set the initialized flag during SetIsStaticDataAllocated so that we do not need to be repeating the preinit check and all calls to IsClassInited() will take care of the pre-initialization automatically?

    inline void SetIsStaticDataAllocated()
    {
        LIMITED_METHOD_CONTRACT;
        DWORD dwFlags = enum_flag_IsStaticDataAllocated;
        if (IsPreinited())
            dwFlags |= enum_flag_Initialized;
        InterlockedOr((LONG*)&m_dwFlags, (LONG)dwFlags);
    }

Copy link
Member Author

Choose a reason for hiding this comment

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

Mmm... seems like a decent idea, I'll look into that once I understand the arm64 failure seen in the tests.

Copy link
Member

Choose a reason for hiding this comment

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

The linux arm64 failure is happening on multiple PRs. Opened #104263.


return IsClassInited();
}

void MethodTable::AttemptToPreinit()
{
CONTRACTL
Expand Down
9 changes: 9 additions & 0 deletions src/coreclr/vm/methodtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ struct MethodTableAuxiliaryData

inline BOOL IsClassInited() const
{
LIMITED_METHOD_DAC_CONTRACT;
return VolatileLoad(&m_dwFlags) & enum_flag_Initialized;
}

Expand Down Expand Up @@ -1049,11 +1050,19 @@ class MethodTable
}
}

private:
void AttemptToPreinit();
public:
// Is the MethodTable current initialized, and/or can the runtime initialize the MethodTable
// without running any user code. (This function may allocate memory, and may throw OutOfMemory)
BOOL IsClassInitedOrPreinited();
#endif

// Is the MethodTable current known to be initialized
// If you want to know if it is initialized and allocation/throwing is permitted, call IsClassInitedOrPreinited instead
BOOL IsClassInited()
{
LIMITED_METHOD_DAC_CONTRACT;
return GetAuxiliaryDataForWrite()->IsClassInited();
}

Expand Down
5 changes: 2 additions & 3 deletions src/coreclr/vm/prestub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3545,9 +3545,8 @@ static PCODE getHelperForStaticBase(Module * pModule, CORCOMPILE_FIXUP_BLOB_KIND
{
STANDARD_VM_CONTRACT;

pMT->AttemptToPreinit();
bool GCStatic = (kind == ENCODE_STATIC_BASE_GC_HELPER || kind == ENCODE_THREAD_STATIC_BASE_GC_HELPER);
bool noCtor = pMT->IsClassInited();
bool noCtor = pMT->IsClassInitedOrPreinited();
bool threadStatic = (kind == ENCODE_THREAD_STATIC_BASE_NONGC_HELPER || kind == ENCODE_THREAD_STATIC_BASE_GC_HELPER);

CorInfoHelpFunc helper;
Expand Down Expand Up @@ -3916,7 +3915,7 @@ PCODE DynamicHelperFixup(TransitionBlock * pTransitionBlock, TADDR * pCell, DWOR
else
{
// Delay the creation of the helper until the type is initialized
if (pMT->IsClassInited())
if (pMT->IsClassInitedOrPreinited())
pHelper = getHelperForInitializedStatic(pModule, (CORCOMPILE_FIXUP_BLOB_KIND)kind, pMT, pFD);
}
}
Expand Down
Loading