Skip to content

Commit

Permalink
make mono_class_get_property_token work for added props
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdageek committed Nov 18, 2022
1 parent fbcd1b2 commit 2934993
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/libraries/System.Runtime.Loader/tests/ApplyUpdateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,16 @@ public static void TestAddInstanceField()
Assert.Equal("abcdTest", x2.GetStringProp);
var addedPropToken = propInfo.MetadataToken;
Assert.True (addedPropToken > 0);
// we don't know exactly what token Roslyn will assign to the added property, but
// since the AddInstanceField.dll assembly is relatively small, assume that the
// total number of properties in the updated generation is less than 64 and the
// token is in that range. If more code is added, revise this test.
Assert.True ((addedPropToken & 0x00ffffff) < 64);
});
}
Expand Down
10 changes: 10 additions & 0 deletions src/mono/mono/component/hot_reload-stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ hot_reload_stub_added_field_ldflda (MonoObject *instance, MonoType *field_type,
static MonoProperty *
hot_reload_stub_added_properties_iter (MonoClass *klass, gpointer *iter);

static uint32_t
hot_reload_stub_get_property_idx (MonoProperty *prop);

static MonoComponentHotReload fn_table = {
{ MONO_COMPONENT_ITF_VERSION, &hot_reload_stub_available },
&hot_reload_stub_set_fastpath_data,
Expand Down Expand Up @@ -155,6 +158,7 @@ static MonoComponentHotReload fn_table = {
&hot_reload_stub_get_method_params,
&hot_reload_stub_added_field_ldflda,
&hot_reload_stub_added_properties_iter,
&hot_reload_stub_get_property_idx,
};

static bool
Expand Down Expand Up @@ -373,6 +377,12 @@ hot_reload_stub_added_properties_iter (MonoClass *klass, gpointer *iter)
return NULL;
}

static uint32_t
hot_reload_stub_get_property_idx (MonoProperty *prop)
{
return 0;
}


MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentHotReload *
Expand Down
13 changes: 13 additions & 0 deletions src/mono/mono/component/hot_reload.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ hot_reload_added_field_ldflda (MonoObject *instance, MonoType *field_type, uint3
static MonoProperty *
hot_reload_added_properties_iter (MonoClass *klass, gpointer *iter);

static uint32_t
hot_reload_get_property_idx (MonoProperty *prop);


static MonoClassMetadataUpdateField *
metadata_update_field_setup_basic_info (MonoImage *image_base, BaselineInfo *base_info, uint32_t generation, DeltaInfo *delta_info, MonoClass *parent_klass, uint32_t fielddef_token, uint32_t field_flags);

Expand Down Expand Up @@ -201,6 +205,7 @@ static MonoComponentHotReload fn_table = {
&hot_reload_get_method_params,
&hot_reload_added_field_ldflda,
&hot_reload_added_properties_iter,
&hot_reload_get_property_idx,
};

MonoComponentHotReload *
Expand Down Expand Up @@ -3449,3 +3454,11 @@ hot_reload_added_properties_iter (MonoClass *klass, gpointer *iter)
*iter = GUINT_TO_POINTER (idx);
return &prop->prop;
}

uint32_t
hot_reload_get_property_idx (MonoProperty *prop)
{
g_assert (m_property_is_from_update (prop));
MonoClassMetadataUpdateProperty *prop_info = (MonoClassMetadataUpdateProperty *)prop;
return mono_metadata_token_index (prop_info->token);
}
1 change: 1 addition & 0 deletions src/mono/mono/component/hot_reload.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ typedef struct _MonoComponentHotReload {
uint32_t (*get_method_params) (MonoImage *base_image, uint32_t methoddef_token, uint32_t *out_param_count_opt);
gpointer (*added_field_ldflda) (MonoObject *instance, MonoType *field_type, uint32_t fielddef_token, MonoError *error);
MonoProperty* (*added_properties_iter) (MonoClass *klass, gpointer *iter);
uint32_t (*get_property_idx) (MonoProperty *prop);
} MonoComponentHotReload;

MONO_COMPONENT_EXPORT_ENTRYPOINT
Expand Down
10 changes: 9 additions & 1 deletion src/mono/mono/metadata/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -2637,7 +2637,7 @@ mono_class_get_property_default_value (MonoProperty *property, MonoTypeEnum *def
}
return NULL;
}
/* TODO: metadata-update: if property is from update, get the token directly. */
/* metadata-update: Roslyn doesn't emit HasDefault on added properties. */
g_assert (!m_property_is_from_update (property));
cindex = mono_metadata_get_constant_index (klass_image, mono_class_get_property_token (property), 0);
if (!cindex)
Expand Down Expand Up @@ -2699,6 +2699,14 @@ guint32
mono_class_get_property_token (MonoProperty *prop)
{
MonoClass *klass = prop->parent;

if (G_UNLIKELY (m_class_get_image (klass)->has_updates)) {
if (G_UNLIKELY (m_property_is_from_update (prop))) {
uint32_t idx = mono_metadata_update_get_property_idx (prop);
return mono_metadata_make_token (MONO_TABLE_PROPERTY, idx);
}
}

while (klass) {
MonoProperty* p;
int i = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/metadata/custom-attrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ find_field_index (MonoClass *klass, MonoClassField *field) {
static guint32
find_property_index (MonoClass *klass, MonoProperty *property)
{
if (G_UNLIKELY (m_property_is_from_update (property)))
return mono_metadata_update_get_property_idx (property);
MonoClassPropertyInfo *info = mono_class_get_property_info (klass);

for (guint32 i = 0; i < info->count; ++i) {
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono/metadata/icall.c
Original file line number Diff line number Diff line change
Expand Up @@ -6519,6 +6519,9 @@ ves_icall_property_info_get_default_value (MonoReflectionPropertyHandle property
return NULL_HANDLE;
}

/* metadata-update: looks like Roslyn doesn't set the HasDefault attribute for updates */
g_assert (!m_property_is_from_update (prop));

def_value = mono_class_get_property_default_value (prop, &def_type);

mono_type_from_blob_type (&blob_type, def_type, type);
Expand Down
6 changes: 6 additions & 0 deletions src/mono/mono/metadata/metadata-update.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,9 @@ mono_metadata_update_added_properties_iter (MonoClass *klass, gpointer *iter)
{
return mono_component_hot_reload()->added_properties_iter (klass, iter);
}

uint32_t
mono_metadata_update_get_property_idx (MonoProperty *prop)
{
return mono_component_hot_reload()->get_property_idx (prop);
}
3 changes: 3 additions & 0 deletions src/mono/mono/metadata/metadata-update.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ mono_metadata_update_added_field_ldflda (MonoObject *instance, MonoType *field_t
MonoProperty *
mono_metadata_update_added_properties_iter (MonoClass *klass, gpointer *iter);

uint32_t
mono_metadata_update_get_property_idx (MonoProperty *prop);

#endif /*__MONO_METADATA_UPDATE_H__*/
1 change: 0 additions & 1 deletion src/mono/mono/metadata/reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2503,7 +2503,6 @@ mono_reflection_get_token_checked (MonoObjectHandle obj, MonoError *error)
MonoReflectionPropertyHandle p = MONO_HANDLE_CAST (MonoReflectionProperty, obj);

MonoProperty *prop = MONO_HANDLE_GETVAL (p, property);
g_assert (!m_property_is_from_update (prop)); // TODO: metadata-update: get the token directly
token = mono_class_get_property_token (prop);
} else if (strcmp (klass_name, "RuntimeEventInfo") == 0) {
MonoReflectionMonoEventHandle p = MONO_HANDLE_CAST (MonoReflectionMonoEvent, obj);
Expand Down

0 comments on commit 2934993

Please sign in to comment.