Skip to content

Commit

Permalink
[mono] Don't access MonoClassField:parent directly (dotnet#63081)
Browse files Browse the repository at this point in the history
Rename it to parent_ and add m_field_get_parent / m_field_set_parent accessors.

(The intention is to borrow the bottom bit of the pointer for an EnC metadata
update flag)
  • Loading branch information
lambdageek authored Jan 3, 2022
1 parent 6c50d9f commit d34c0d6
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 100 deletions.
22 changes: 11 additions & 11 deletions src/mono/mono/component/debugger-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -2932,7 +2932,7 @@ static gint32 isFixedSizeArray (MonoClassField *f)
MonoCustomAttrEntry *attr;
int aindex;
gint32 ret = 1;
cinfo = mono_custom_attrs_from_field_checked (f->parent, f, error);
cinfo = mono_custom_attrs_from_field_checked (m_field_get_parent (f), f, error);
goto_if_nok (error, leave);
attr = NULL;
if (cinfo) {
Expand Down Expand Up @@ -7734,12 +7734,12 @@ field_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
MonoClassField *f = decode_fieldid (p, &p, end, &domain, &err);

buffer_add_string (buf, f->name);
buffer_add_typeid (buf, domain, f->parent);
buffer_add_typeid (buf, domain, m_field_get_parent (f));
buffer_add_typeid (buf, domain, mono_class_from_mono_type_internal (f->type));
buffer_add_int (buf, f->type->attrs);
if (CHECK_PROTOCOL_VERSION (2, 59)) {
buffer_add_int (buf, f->type->type);
buffer_add_int (buf, m_class_get_type_token (f->parent));
buffer_add_int (buf, m_class_get_type_token (m_field_get_parent (f)));
buffer_add_int (buf, m_class_get_type_token (mono_class_from_mono_type_internal (f->type)));
}
break;
Expand Down Expand Up @@ -7796,15 +7796,15 @@ static int get_static_field_value(MonoClassField* f, MonoClass* klass, MonoDomai
/* Check that the field belongs to the object */
found = FALSE;
for (k = klass; k; k = m_class_get_parent(k)) {
if (k == f->parent) {
if (k == m_field_get_parent (f)) {
found = TRUE;
break;
}
}
if (!found)
return -1;

vtable = mono_class_vtable_checked(f->parent, error);
vtable = mono_class_vtable_checked(m_field_get_parent (f), error);
if (!is_ok(error))
return -1;

Expand Down Expand Up @@ -8127,7 +8127,7 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
/* Check that the field belongs to the object */
found = FALSE;
for (k = klass; k; k = m_class_get_parent (k)) {
if (k == f->parent) {
if (k == m_field_get_parent (f)) {
found = TRUE;
break;
}
Expand All @@ -8137,7 +8137,7 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint

// FIXME: Check for literal/const

vtable = mono_class_vtable_checked (f->parent, error);
vtable = mono_class_vtable_checked (m_field_get_parent (f), error);
goto_if_nok (error, invalid_fieldid);

val = (guint8 *)g_malloc (mono_class_instance_size (mono_class_from_mono_type_internal (f->type)));
Expand Down Expand Up @@ -9634,7 +9634,7 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
/* Check that the field belongs to the object */
found = FALSE;
for (k = obj_type; k; k = m_class_get_parent (k)) {
if (k == f->parent) {
if (k == m_field_get_parent (f)) {
found = TRUE;
break;
}
Expand All @@ -9650,7 +9650,7 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
goto invalid_fieldid;

g_assert (f->type->attrs & FIELD_ATTRIBUTE_STATIC);
vtable = mono_class_vtable_checked (f->parent, error);
vtable = mono_class_vtable_checked (m_field_get_parent (f), error);
if (!is_ok (error)) {
mono_error_cleanup (error);
goto invalid_object;
Expand Down Expand Up @@ -9681,7 +9681,7 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
/* Check that the field belongs to the object */
found = FALSE;
for (k = obj_type; k; k = m_class_get_parent (k)) {
if (k == f->parent) {
if (k == m_field_get_parent (f)) {
found = TRUE;
break;
}
Expand All @@ -9697,7 +9697,7 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
goto invalid_fieldid;

g_assert (f->type->attrs & FIELD_ATTRIBUTE_STATIC);
vtable = mono_class_vtable_checked (f->parent, error);
vtable = mono_class_vtable_checked (m_field_get_parent (f), error);
if (!is_ok (error)) {
mono_error_cleanup (error);
goto invalid_fieldid;
Expand Down
10 changes: 8 additions & 2 deletions src/mono/mono/metadata/class-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ mono_class_setup_basic_field_info (MonoClass *klass)
int first_field_idx = mono_class_has_static_metadata (klass) ? mono_class_get_first_field_idx (klass) : 0;
for (i = 0; i < top; i++) {
field = &fields [i];
field->parent = klass;
m_field_set_parent (field, klass);

if (gtd) {
field->name = mono_field_get_name (&gtd->fields [i]);
Expand Down Expand Up @@ -2399,7 +2399,7 @@ mono_class_layout_fields (MonoClass *klass, int base_instance_size, int packing_
guint32 field_idx = first_field_idx + (field - p->fields);
if (MONO_TYPE_IS_REFERENCE (field->type) && mono_assembly_is_weak_field (p->image, field_idx + 1)) {
has_weak_fields = TRUE;
mono_trace_message (MONO_TRACE_TYPE, "Field %s:%s at offset %x is weak.", field->parent->name, field->name, field->offset);
mono_trace_message (MONO_TRACE_TYPE, "Field %s:%s at offset %x is weak.", m_field_get_parent (field)->name, field->name, field->offset);
}
}
}
Expand Down Expand Up @@ -4080,3 +4080,9 @@ mono_classes_init (void)
mono_counters_register ("MonoClass size",
MONO_COUNTER_METADATA | MONO_COUNTER_INT, &classes_size);
}

void
m_field_set_parent (MonoClassField *field, MonoClass *klass)
{
field->parent_ = klass;
}
14 changes: 12 additions & 2 deletions src/mono/mono/metadata/class-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ struct _MonoClassField {
const char *name;

/* Type where the field was defined */
MonoClass *parent;
/* Do not access directly, use m_field_get_parent */
MonoClass *parent_;

/*
* Offset where this field is stored; if it is an instance
Expand Down Expand Up @@ -1535,10 +1536,19 @@ mono_method_has_unmanaged_callers_only_attribute (MonoMethod *method);
} \
} \

static inline MonoClass *
m_field_get_parent (MonoClassField *field)
{
return field->parent_;
}

void
m_field_set_parent (MonoClassField *field, MonoClass *klass);

static inline gboolean
m_field_get_offset (MonoClassField *field)
{
g_assert (m_class_is_fields_inited (field->parent));
g_assert (m_class_is_fields_inited (m_field_get_parent (field)));
return field->offset;
}

Expand Down
32 changes: 16 additions & 16 deletions src/mono/mono/metadata/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -2501,7 +2501,7 @@ mono_class_get_field_from_name_full (MonoClass *klass, const char *name, MonoTyp
guint32
mono_class_get_field_token (MonoClassField *field)
{
MonoClass *klass = field->parent;
MonoClass *klass = m_field_get_parent (field);
int i;

mono_class_setup_fields (klass);
Expand Down Expand Up @@ -2531,8 +2531,8 @@ mono_class_get_field_token (MonoClassField *field)
static int
mono_field_get_index (MonoClassField *field)
{
int index = field - m_class_get_fields (field->parent);
g_assert (index >= 0 && index < mono_class_get_field_count (field->parent));
int index = field - m_class_get_fields (m_field_get_parent (field));
g_assert (index >= 0 && index < mono_class_get_field_count (m_field_get_parent (field)));

return index;
}
Expand All @@ -2548,7 +2548,7 @@ mono_class_get_field_default_value (MonoClassField *field, MonoTypeEnum *def_typ
guint32 cindex;
guint32 constant_cols [MONO_CONSTANT_SIZE];
int field_index;
MonoClass *klass = field->parent;
MonoClass *klass = m_field_get_parent (field);
MonoFieldDefaultValue *def_values;

g_assert (field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT);
Expand All @@ -2563,7 +2563,7 @@ mono_class_get_field_default_value (MonoClassField *field, MonoTypeEnum *def_typ
field_index = mono_field_get_index (field);

if (!def_values [field_index].data) {
MonoImage *field_parent_image = m_class_get_image (field->parent);
MonoImage *field_parent_image = m_class_get_image (m_field_get_parent (field));
cindex = mono_metadata_get_constant_index (field_parent_image, mono_class_get_field_token (field), 0);
if (!cindex)
return NULL;
Expand Down Expand Up @@ -5402,7 +5402,7 @@ mono_field_get_type_checked (MonoClassField *field, MonoError *error)
MonoClass*
mono_field_get_parent (MonoClassField *field)
{
return field->parent;
return m_field_get_parent (field);
}

/**
Expand Down Expand Up @@ -5431,7 +5431,7 @@ mono_field_get_flags (MonoClassField *field)
guint32
mono_field_get_offset (MonoClassField *field)
{
mono_class_setup_fields(field->parent);
mono_class_setup_fields(m_field_get_parent (field));
return field->offset;
}

Expand All @@ -5440,7 +5440,7 @@ mono_field_get_rva (MonoClassField *field, int swizzle)
{
guint32 rva;
int field_index;
MonoClass *klass = field->parent;
MonoClass *klass = m_field_get_parent (field);
MonoFieldDefaultValue *def_values;

g_assert (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA);
Expand All @@ -5459,11 +5459,11 @@ mono_field_get_rva (MonoClassField *field, int swizzle)

if (!image_is_dynamic (m_class_get_image (klass))) {
int first_field_idx = mono_class_get_first_field_idx (klass);
mono_metadata_field_info (m_class_get_image (field->parent), first_field_idx + field_index, NULL, &rva, NULL);
mono_metadata_field_info (m_class_get_image (m_field_get_parent (field)), first_field_idx + field_index, NULL, &rva, NULL);
if (!rva)
g_warning ("field %s in %s should have RVA data, but hasn't", mono_field_get_name (field), m_class_get_name (field->parent));
g_warning ("field %s in %s should have RVA data, but hasn't", mono_field_get_name (field), m_class_get_name (m_field_get_parent (field)));

rvaData = mono_image_rva_map (m_class_get_image (field->parent), rva);
rvaData = mono_image_rva_map (m_class_get_image (m_field_get_parent (field)), rva);
} else {
rvaData = mono_field_get_data (field);
}
Expand Down Expand Up @@ -6170,11 +6170,11 @@ gboolean
mono_method_can_access_field (MonoMethod *method, MonoClassField *field)
{
/* FIXME: check all overlapping fields */
int can = can_access_member (method->klass, field->parent, NULL, mono_field_get_type_internal (field)->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
int can = can_access_member (method->klass, m_field_get_parent (field), NULL, mono_field_get_type_internal (field)->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
if (!can) {
MonoClass *nested = m_class_get_nested_in (method->klass);
while (nested) {
can = can_access_member (nested, field->parent, NULL, mono_field_get_type_internal (field)->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
can = can_access_member (nested, m_field_get_parent (field), NULL, mono_field_get_type_internal (field)->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
if (can)
return TRUE;
nested = m_class_get_nested_in (nested);
Expand Down Expand Up @@ -6282,7 +6282,7 @@ gboolean
mono_method_can_access_field_full (MonoMethod *method, MonoClassField *field, MonoClass *context_klass)
{
MonoClass *access_class = method->klass;
MonoClass *member_class = field->parent;
MonoClass *member_class = m_field_get_parent (field);
/* FIXME: check all overlapping fields */
int can = can_access_member (access_class, member_class, context_klass, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
if (!can) {
Expand Down Expand Up @@ -6413,7 +6413,7 @@ mono_generic_class_is_generic_type_definition (MonoGenericClass *gklass)
void
mono_field_resolve_type (MonoClassField *field, MonoError *error)
{
MonoClass *klass = field->parent;
MonoClass *klass = m_field_get_parent (field);
MonoImage *image = m_class_get_image (klass);
MonoClass *gtd = mono_class_is_ginst (klass) ? mono_class_get_generic_type_definition (klass) : NULL;
MonoType *ftype;
Expand Down Expand Up @@ -6475,7 +6475,7 @@ mono_field_resolve_type (MonoClassField *field, MonoError *error)
static guint32
mono_field_resolve_flags (MonoClassField *field)
{
MonoClass *klass = field->parent;
MonoClass *klass = m_field_get_parent (field);
MonoImage *image = m_class_get_image (klass);
MonoClass *gtd = mono_class_is_ginst (klass) ? mono_class_get_generic_type_definition (klass) : NULL;
int field_idx = field - m_class_get_fields (klass);
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/custom-attrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,7 @@ mono_reflection_get_custom_attrs_info_checked (MonoObjectHandle obj, MonoError *
} else if (strcmp ("RuntimeFieldInfo", klass_name) == 0) {
MonoReflectionFieldHandle rfield = MONO_HANDLE_CAST (MonoReflectionField, obj);
MonoClassField *field = MONO_HANDLE_GETVAL (rfield, field);
cinfo = mono_custom_attrs_from_field_checked (field->parent, field, error);
cinfo = mono_custom_attrs_from_field_checked (m_field_get_parent (field), field, error);
goto_if_nok (error, leave);
} else if ((strcmp ("RuntimeMethodInfo", klass_name) == 0) || (strcmp ("RuntimeConstructorInfo", klass_name) == 0)) {
MonoReflectionMethodHandle rmethod = MONO_HANDLE_CAST (MonoReflectionMethod, obj);
Expand Down
4 changes: 2 additions & 2 deletions src/mono/mono/metadata/debug-helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -868,10 +868,10 @@ char *
mono_field_full_name (MonoClassField *field)
{
char *res;
const char *nspace = field->parent->name_space;
const char *nspace = m_field_get_parent (field)->name_space;

res = g_strdup_printf ("%s%s%s:%s", nspace, *nspace ? "." : "",
field->parent->name, mono_field_get_name (field));
m_field_get_parent (field)->name, mono_field_get_name (field));

return res;
}
Expand Down
Loading

0 comments on commit d34c0d6

Please sign in to comment.