Skip to content

Fix execution of static constructors #3168

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 3 commits into from
May 8, 2025
Merged
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
20 changes: 16 additions & 4 deletions src/CLR/Core/TypeSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2468,7 +2468,7 @@ HRESULT CLR_RT_Assembly::CreateInstance(const CLR_RECORD_ASSEMBLY *header, CLR_R
}
#endif
CLR_Debug::Printf("\r\n\r\n");

CLR_Debug::Printf(
" AssemblyRef = %6d bytes (%5d elements)\r\n",
offsets.assemblyRef,
Expand Down Expand Up @@ -3843,8 +3843,7 @@ HRESULT CLR_RT_AppDomain::GetAssemblies(CLR_RT_HeapBlock &ref)

if (pass == 0)
{
NANOCLR_CHECK_HRESULT(
CLR_RT_HeapBlock_Array::CreateInstance(ref, count, g_CLR_RT_WellKnownTypes.Assembly));
NANOCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(ref, count, g_CLR_RT_WellKnownTypes.Assembly));

pArray = (CLR_RT_HeapBlock *)ref.DereferenceArray()->GetFirstElement();
}
Expand Down Expand Up @@ -4787,7 +4786,20 @@ bool CLR_RT_Assembly::FindNextStaticConstructor(CLR_RT_MethodDef_Index &index)

index.Set(assemblyIndex, i);

if (md->flags & CLR_RECORD_METHODDEF::MD_StaticConstructor)
// turn the index into a MethodDef_Instance
CLR_RT_MethodDef_Instance methodDefInst;
methodDefInst.InitializeFromIndex(index);

CLR_RT_TypeDef_Instance typeDefInst;
typeDefInst.InitializeFromMethod(methodDefInst);

// check if this is a static constructor
// but skip it if:
// - references generic parameters
// - it lives on a generic type definition
if ((md->flags & CLR_RECORD_METHODDEF::MD_StaticConstructor) &&
((md->flags & CLR_RECORD_METHODDEF::MD_ContainsGenericParameter) == 0) &&
typeDefInst.target->genericParamCount == 0)
{
return true;
}
Expand Down