From 817bdd28d83864b2e5073adbddf98e4d3c24e2e6 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Tue, 18 Jul 2023 15:12:36 -0400 Subject: [PATCH 1/4] Add regression test --- .../AddStaticField.cs | 7 +++++++ .../AddStaticField_v1.cs | 9 +++++++++ .../System.Runtime.Loader/tests/ApplyUpdateTest.cs | 3 +++ 3 files changed, 19 insertions(+) diff --git a/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.AddStaticField/AddStaticField.cs b/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.AddStaticField/AddStaticField.cs index 6ac1360c3581bf..895d99d4344ae7 100644 --- a/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.AddStaticField/AddStaticField.cs +++ b/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.AddStaticField/AddStaticField.cs @@ -19,4 +19,11 @@ public void TestMethod () { } } + public class AddStaticField2 + { + public static int Test() + { + return -1; + } + } } diff --git a/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.AddStaticField/AddStaticField_v1.cs b/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.AddStaticField/AddStaticField_v1.cs index f9282469a4fce7..e07f6a7ec93eec 100644 --- a/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.AddStaticField/AddStaticField_v1.cs +++ b/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.AddStaticField/AddStaticField_v1.cs @@ -22,4 +22,13 @@ public void TestMethod () { } } + public class AddStaticField2 + { + private static int A {get; set;} + public static int Test() + { + A = 11; + return A + A; + } + } } diff --git a/src/libraries/System.Runtime.Loader/tests/ApplyUpdateTest.cs b/src/libraries/System.Runtime.Loader/tests/ApplyUpdateTest.cs index 3593c5588eee70..799381f87ed0f5 100644 --- a/src/libraries/System.Runtime.Loader/tests/ApplyUpdateTest.cs +++ b/src/libraries/System.Runtime.Loader/tests/ApplyUpdateTest.cs @@ -302,6 +302,9 @@ public static void TestAddStaticField() string result = x.GetField; Assert.Equal("4567", result); + + int aa = System.Reflection.Metadata.ApplyUpdate.Test.AddStaticField2.Test(); + Assert.Equal(22, aa); }); } From b306f26a123314f1d4878e36aa37776effea8732 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Tue, 18 Jul 2023 15:15:45 -0400 Subject: [PATCH 2/4] [hot_reload] Check for added fields earlier in compute_class_bitmap and in mono_class_create_runtime_vtable Added fields don't contribute to the class bitmap, and they also might not have their type resolved yet - move the "is from update" check before we need to access the field's type --- src/mono/mono/metadata/object.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/mono/mono/metadata/object.c b/src/mono/mono/metadata/object.c index b0289cebf414ae..b46f14114f5c95 100644 --- a/src/mono/mono/metadata/object.c +++ b/src/mono/mono/metadata/object.c @@ -834,6 +834,11 @@ compute_class_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset, int while ((field = mono_class_get_fields_internal (p, &iter))) { MonoType *type; + /* metadadta-update: added fields aren't stored in the object, don't + * contribute to the GC descriptor. */ + if (m_field_is_from_update (field)) + continue; + if (static_fields) { if (!(field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))) continue; @@ -847,11 +852,6 @@ compute_class_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset, int if (m_type_is_byref (field->type)) break; - /* metadadta-update: added fields aren't stored in the object, don't - * contribute to the GC descriptor. */ - if (m_field_is_from_update (field)) - continue; - int field_offset = m_field_get_offset (field); if (static_fields && (field->offset == -1 || field->offset == -2)) @@ -2211,13 +2211,13 @@ mono_class_create_runtime_vtable (MonoClass *klass, MonoError *error) iter = NULL; while ((field = mono_class_get_fields_internal (klass, &iter))) { + if (m_field_is_from_update (field)) + continue; if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) continue; if (mono_field_is_deleted (field)) continue; /* metadata-update: added fields are stored external to the object, and don't contribute to the bitmap */ - if (m_field_is_from_update (field)) - continue; if (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL)) { gint32 special_static = m_class_has_no_special_static_fields (klass) ? SPECIAL_STATIC_NONE : field_is_special_static (klass, field); if (special_static != SPECIAL_STATIC_NONE) { From 3bdc9fa1c720796f738124d12ad520ba2c6c031b Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Tue, 18 Jul 2023 15:46:05 -0400 Subject: [PATCH 3/4] fix comment typo --- src/mono/mono/metadata/object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/object.c b/src/mono/mono/metadata/object.c index b46f14114f5c95..78b3be432bfe5a 100644 --- a/src/mono/mono/metadata/object.c +++ b/src/mono/mono/metadata/object.c @@ -834,7 +834,7 @@ compute_class_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset, int while ((field = mono_class_get_fields_internal (p, &iter))) { MonoType *type; - /* metadadta-update: added fields aren't stored in the object, don't + /* metadata-update: added fields aren't stored in the object, don't * contribute to the GC descriptor. */ if (m_field_is_from_update (field)) continue; From d26234b64a8dd1b8383dcaec5993b71054b69403 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Tue, 18 Jul 2023 15:47:07 -0400 Subject: [PATCH 4/4] one more comment fix --- src/mono/mono/metadata/object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/object.c b/src/mono/mono/metadata/object.c index 78b3be432bfe5a..98d3e436068be5 100644 --- a/src/mono/mono/metadata/object.c +++ b/src/mono/mono/metadata/object.c @@ -2211,13 +2211,13 @@ mono_class_create_runtime_vtable (MonoClass *klass, MonoError *error) iter = NULL; while ((field = mono_class_get_fields_internal (klass, &iter))) { + /* metadata-update: added fields are stored external to the object, and don't contribute to the bitmap */ if (m_field_is_from_update (field)) continue; if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) continue; if (mono_field_is_deleted (field)) continue; - /* metadata-update: added fields are stored external to the object, and don't contribute to the bitmap */ if (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL)) { gint32 special_static = m_class_has_no_special_static_fields (klass) ? SPECIAL_STATIC_NONE : field_is_special_static (klass, field); if (special_static != SPECIAL_STATIC_NONE) {