Skip to content

Commit

Permalink
Run class cctor in RuntimeHelpers.GetUninitializedObject(type). (#44898)
Browse files Browse the repository at this point in the history
* Run class cctor in RuntimeHelpers.GetUninitializedObject(type).

Fixes #44852.

* Reenable test.

* Don't run the cctor for beforefieldinit classes.
  • Loading branch information
vargaz authored Nov 19, 2020
1 parent 63923b5 commit 3440021
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ public static void GetUninitalizedObject_DoesNotRunBeforeFieldInitCctors()
Assert.Null(AppDomain.CurrentDomain.GetData("ClassWithBeforeFieldInitCctor_CctorRan"));
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/44852", TestRuntimes.Mono)]
[Fact]
public static void GetUninitalizedObject_RunsNormalStaticCtors()
{
Expand Down
9 changes: 9 additions & 0 deletions src/mono/mono/metadata/icall.c
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ MonoObjectHandle
ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetUninitializedObjectInternal (MonoType *handle, MonoError *error)
{
MonoClass *klass;
MonoVTable *vtable;

g_assert (handle);

Expand Down Expand Up @@ -1401,6 +1402,14 @@ ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetUninitializedObjectI
return NULL_HANDLE;
}

if (!mono_class_is_before_field_init (klass)) {
vtable = mono_class_vtable_checked (mono_domain_get (), klass, error);
return_val_if_nok (error, NULL_HANDLE);

mono_runtime_class_init_full (vtable, error);
return_val_if_nok (error, NULL_HANDLE);
}

if (m_class_is_nullable (klass))
return mono_object_new_handle (mono_domain_get (), m_class_get_nullable_elem_class (klass), error);
else
Expand Down

0 comments on commit 3440021

Please sign in to comment.