Skip to content

Commit

Permalink
Fix throwing exception when calling RunClassConstructor on a generic …
Browse files Browse the repository at this point in the history
…type with a static constructor

#99183 seems to have done away with assuming that a generic type's static constructor was
always "initialized". As a result, if you call RunClassConstructor on it, the runtime would throw an exception.

Fixes #103891
  • Loading branch information
steveisok authored and github-actions committed Jul 26, 2024
1 parent eedf30e commit bf60557
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/coreclr/vm/methodtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3768,6 +3768,10 @@ void MethodTable::CheckRunClassInitThrowing()
if (IsSharedByGenericInstantiations())
return;

// For back-compat reasons, act like it has already been initialized
if (ContainsGenericVariables())
return;

EnsureStaticDataAllocated();
DoRunClassInitThrowing();
}
Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/vm/reflectioninvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,8 @@ extern "C" void QCALLTYPE ReflectionInvocation_RunClassConstructor(QCall::TypeHa
return;

MethodTable *pMT = typeHnd.AsMethodTable();
if (pMT->IsClassInited())
// The ContainsGenericVariables check is to preserve back-compat where we assume the generic type is already initialized
if (pMT->IsClassInited() || pMT->ContainsGenericVariables())
return;

BEGIN_QCALL;
Expand Down

0 comments on commit bf60557

Please sign in to comment.