Skip to content

Commit d34c0d6

Browse files
authored
[mono] Don't access MonoClassField:parent directly (#63081)
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)
1 parent 6c50d9f commit d34c0d6

20 files changed

+118
-100
lines changed

src/mono/mono/component/debugger-agent.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,7 +2932,7 @@ static gint32 isFixedSizeArray (MonoClassField *f)
29322932
MonoCustomAttrEntry *attr;
29332933
int aindex;
29342934
gint32 ret = 1;
2935-
cinfo = mono_custom_attrs_from_field_checked (f->parent, f, error);
2935+
cinfo = mono_custom_attrs_from_field_checked (m_field_get_parent (f), f, error);
29362936
goto_if_nok (error, leave);
29372937
attr = NULL;
29382938
if (cinfo) {
@@ -7734,12 +7734,12 @@ field_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
77347734
MonoClassField *f = decode_fieldid (p, &p, end, &domain, &err);
77357735

77367736
buffer_add_string (buf, f->name);
7737-
buffer_add_typeid (buf, domain, f->parent);
7737+
buffer_add_typeid (buf, domain, m_field_get_parent (f));
77387738
buffer_add_typeid (buf, domain, mono_class_from_mono_type_internal (f->type));
77397739
buffer_add_int (buf, f->type->attrs);
77407740
if (CHECK_PROTOCOL_VERSION (2, 59)) {
77417741
buffer_add_int (buf, f->type->type);
7742-
buffer_add_int (buf, m_class_get_type_token (f->parent));
7742+
buffer_add_int (buf, m_class_get_type_token (m_field_get_parent (f)));
77437743
buffer_add_int (buf, m_class_get_type_token (mono_class_from_mono_type_internal (f->type)));
77447744
}
77457745
break;
@@ -7796,15 +7796,15 @@ static int get_static_field_value(MonoClassField* f, MonoClass* klass, MonoDomai
77967796
/* Check that the field belongs to the object */
77977797
found = FALSE;
77987798
for (k = klass; k; k = m_class_get_parent(k)) {
7799-
if (k == f->parent) {
7799+
if (k == m_field_get_parent (f)) {
78007800
found = TRUE;
78017801
break;
78027802
}
78037803
}
78047804
if (!found)
78057805
return -1;
78067806

7807-
vtable = mono_class_vtable_checked(f->parent, error);
7807+
vtable = mono_class_vtable_checked(m_field_get_parent (f), error);
78087808
if (!is_ok(error))
78097809
return -1;
78107810

@@ -8127,7 +8127,7 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
81278127
/* Check that the field belongs to the object */
81288128
found = FALSE;
81298129
for (k = klass; k; k = m_class_get_parent (k)) {
8130-
if (k == f->parent) {
8130+
if (k == m_field_get_parent (f)) {
81318131
found = TRUE;
81328132
break;
81338133
}
@@ -8137,7 +8137,7 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
81378137

81388138
// FIXME: Check for literal/const
81398139

8140-
vtable = mono_class_vtable_checked (f->parent, error);
8140+
vtable = mono_class_vtable_checked (m_field_get_parent (f), error);
81418141
goto_if_nok (error, invalid_fieldid);
81428142

81438143
val = (guint8 *)g_malloc (mono_class_instance_size (mono_class_from_mono_type_internal (f->type)));
@@ -9634,7 +9634,7 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
96349634
/* Check that the field belongs to the object */
96359635
found = FALSE;
96369636
for (k = obj_type; k; k = m_class_get_parent (k)) {
9637-
if (k == f->parent) {
9637+
if (k == m_field_get_parent (f)) {
96389638
found = TRUE;
96399639
break;
96409640
}
@@ -9650,7 +9650,7 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
96509650
goto invalid_fieldid;
96519651

96529652
g_assert (f->type->attrs & FIELD_ATTRIBUTE_STATIC);
9653-
vtable = mono_class_vtable_checked (f->parent, error);
9653+
vtable = mono_class_vtable_checked (m_field_get_parent (f), error);
96549654
if (!is_ok (error)) {
96559655
mono_error_cleanup (error);
96569656
goto invalid_object;
@@ -9681,7 +9681,7 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
96819681
/* Check that the field belongs to the object */
96829682
found = FALSE;
96839683
for (k = obj_type; k; k = m_class_get_parent (k)) {
9684-
if (k == f->parent) {
9684+
if (k == m_field_get_parent (f)) {
96859685
found = TRUE;
96869686
break;
96879687
}
@@ -9697,7 +9697,7 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
96979697
goto invalid_fieldid;
96989698

96999699
g_assert (f->type->attrs & FIELD_ATTRIBUTE_STATIC);
9700-
vtable = mono_class_vtable_checked (f->parent, error);
9700+
vtable = mono_class_vtable_checked (m_field_get_parent (f), error);
97019701
if (!is_ok (error)) {
97029702
mono_error_cleanup (error);
97039703
goto invalid_fieldid;

src/mono/mono/metadata/class-init.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ mono_class_setup_basic_field_info (MonoClass *klass)
204204
int first_field_idx = mono_class_has_static_metadata (klass) ? mono_class_get_first_field_idx (klass) : 0;
205205
for (i = 0; i < top; i++) {
206206
field = &fields [i];
207-
field->parent = klass;
207+
m_field_set_parent (field, klass);
208208

209209
if (gtd) {
210210
field->name = mono_field_get_name (&gtd->fields [i]);
@@ -2399,7 +2399,7 @@ mono_class_layout_fields (MonoClass *klass, int base_instance_size, int packing_
23992399
guint32 field_idx = first_field_idx + (field - p->fields);
24002400
if (MONO_TYPE_IS_REFERENCE (field->type) && mono_assembly_is_weak_field (p->image, field_idx + 1)) {
24012401
has_weak_fields = TRUE;
2402-
mono_trace_message (MONO_TRACE_TYPE, "Field %s:%s at offset %x is weak.", field->parent->name, field->name, field->offset);
2402+
mono_trace_message (MONO_TRACE_TYPE, "Field %s:%s at offset %x is weak.", m_field_get_parent (field)->name, field->name, field->offset);
24032403
}
24042404
}
24052405
}
@@ -4080,3 +4080,9 @@ mono_classes_init (void)
40804080
mono_counters_register ("MonoClass size",
40814081
MONO_COUNTER_METADATA | MONO_COUNTER_INT, &classes_size);
40824082
}
4083+
4084+
void
4085+
m_field_set_parent (MonoClassField *field, MonoClass *klass)
4086+
{
4087+
field->parent_ = klass;
4088+
}

src/mono/mono/metadata/class-internals.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ struct _MonoClassField {
146146
const char *name;
147147

148148
/* Type where the field was defined */
149-
MonoClass *parent;
149+
/* Do not access directly, use m_field_get_parent */
150+
MonoClass *parent_;
150151

151152
/*
152153
* Offset where this field is stored; if it is an instance
@@ -1535,10 +1536,19 @@ mono_method_has_unmanaged_callers_only_attribute (MonoMethod *method);
15351536
} \
15361537
} \
15371538

1539+
static inline MonoClass *
1540+
m_field_get_parent (MonoClassField *field)
1541+
{
1542+
return field->parent_;
1543+
}
1544+
1545+
void
1546+
m_field_set_parent (MonoClassField *field, MonoClass *klass);
1547+
15381548
static inline gboolean
15391549
m_field_get_offset (MonoClassField *field)
15401550
{
1541-
g_assert (m_class_is_fields_inited (field->parent));
1551+
g_assert (m_class_is_fields_inited (m_field_get_parent (field)));
15421552
return field->offset;
15431553
}
15441554

src/mono/mono/metadata/class.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,7 +2501,7 @@ mono_class_get_field_from_name_full (MonoClass *klass, const char *name, MonoTyp
25012501
guint32
25022502
mono_class_get_field_token (MonoClassField *field)
25032503
{
2504-
MonoClass *klass = field->parent;
2504+
MonoClass *klass = m_field_get_parent (field);
25052505
int i;
25062506

25072507
mono_class_setup_fields (klass);
@@ -2531,8 +2531,8 @@ mono_class_get_field_token (MonoClassField *field)
25312531
static int
25322532
mono_field_get_index (MonoClassField *field)
25332533
{
2534-
int index = field - m_class_get_fields (field->parent);
2535-
g_assert (index >= 0 && index < mono_class_get_field_count (field->parent));
2534+
int index = field - m_class_get_fields (m_field_get_parent (field));
2535+
g_assert (index >= 0 && index < mono_class_get_field_count (m_field_get_parent (field)));
25362536

25372537
return index;
25382538
}
@@ -2548,7 +2548,7 @@ mono_class_get_field_default_value (MonoClassField *field, MonoTypeEnum *def_typ
25482548
guint32 cindex;
25492549
guint32 constant_cols [MONO_CONSTANT_SIZE];
25502550
int field_index;
2551-
MonoClass *klass = field->parent;
2551+
MonoClass *klass = m_field_get_parent (field);
25522552
MonoFieldDefaultValue *def_values;
25532553

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

25652565
if (!def_values [field_index].data) {
2566-
MonoImage *field_parent_image = m_class_get_image (field->parent);
2566+
MonoImage *field_parent_image = m_class_get_image (m_field_get_parent (field));
25672567
cindex = mono_metadata_get_constant_index (field_parent_image, mono_class_get_field_token (field), 0);
25682568
if (!cindex)
25692569
return NULL;
@@ -5402,7 +5402,7 @@ mono_field_get_type_checked (MonoClassField *field, MonoError *error)
54025402
MonoClass*
54035403
mono_field_get_parent (MonoClassField *field)
54045404
{
5405-
return field->parent;
5405+
return m_field_get_parent (field);
54065406
}
54075407

54085408
/**
@@ -5431,7 +5431,7 @@ mono_field_get_flags (MonoClassField *field)
54315431
guint32
54325432
mono_field_get_offset (MonoClassField *field)
54335433
{
5434-
mono_class_setup_fields(field->parent);
5434+
mono_class_setup_fields(m_field_get_parent (field));
54355435
return field->offset;
54365436
}
54375437

@@ -5440,7 +5440,7 @@ mono_field_get_rva (MonoClassField *field, int swizzle)
54405440
{
54415441
guint32 rva;
54425442
int field_index;
5443-
MonoClass *klass = field->parent;
5443+
MonoClass *klass = m_field_get_parent (field);
54445444
MonoFieldDefaultValue *def_values;
54455445

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

54605460
if (!image_is_dynamic (m_class_get_image (klass))) {
54615461
int first_field_idx = mono_class_get_first_field_idx (klass);
5462-
mono_metadata_field_info (m_class_get_image (field->parent), first_field_idx + field_index, NULL, &rva, NULL);
5462+
mono_metadata_field_info (m_class_get_image (m_field_get_parent (field)), first_field_idx + field_index, NULL, &rva, NULL);
54635463
if (!rva)
5464-
g_warning ("field %s in %s should have RVA data, but hasn't", mono_field_get_name (field), m_class_get_name (field->parent));
5464+
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)));
54655465

5466-
rvaData = mono_image_rva_map (m_class_get_image (field->parent), rva);
5466+
rvaData = mono_image_rva_map (m_class_get_image (m_field_get_parent (field)), rva);
54675467
} else {
54685468
rvaData = mono_field_get_data (field);
54695469
}
@@ -6170,11 +6170,11 @@ gboolean
61706170
mono_method_can_access_field (MonoMethod *method, MonoClassField *field)
61716171
{
61726172
/* FIXME: check all overlapping fields */
6173-
int can = can_access_member (method->klass, field->parent, NULL, mono_field_get_type_internal (field)->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
6173+
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);
61746174
if (!can) {
61756175
MonoClass *nested = m_class_get_nested_in (method->klass);
61766176
while (nested) {
6177-
can = can_access_member (nested, field->parent, NULL, mono_field_get_type_internal (field)->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
6177+
can = can_access_member (nested, m_field_get_parent (field), NULL, mono_field_get_type_internal (field)->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
61786178
if (can)
61796179
return TRUE;
61806180
nested = m_class_get_nested_in (nested);
@@ -6282,7 +6282,7 @@ gboolean
62826282
mono_method_can_access_field_full (MonoMethod *method, MonoClassField *field, MonoClass *context_klass)
62836283
{
62846284
MonoClass *access_class = method->klass;
6285-
MonoClass *member_class = field->parent;
6285+
MonoClass *member_class = m_field_get_parent (field);
62866286
/* FIXME: check all overlapping fields */
62876287
int can = can_access_member (access_class, member_class, context_klass, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
62886288
if (!can) {
@@ -6413,7 +6413,7 @@ mono_generic_class_is_generic_type_definition (MonoGenericClass *gklass)
64136413
void
64146414
mono_field_resolve_type (MonoClassField *field, MonoError *error)
64156415
{
6416-
MonoClass *klass = field->parent;
6416+
MonoClass *klass = m_field_get_parent (field);
64176417
MonoImage *image = m_class_get_image (klass);
64186418
MonoClass *gtd = mono_class_is_ginst (klass) ? mono_class_get_generic_type_definition (klass) : NULL;
64196419
MonoType *ftype;
@@ -6475,7 +6475,7 @@ mono_field_resolve_type (MonoClassField *field, MonoError *error)
64756475
static guint32
64766476
mono_field_resolve_flags (MonoClassField *field)
64776477
{
6478-
MonoClass *klass = field->parent;
6478+
MonoClass *klass = m_field_get_parent (field);
64796479
MonoImage *image = m_class_get_image (klass);
64806480
MonoClass *gtd = mono_class_is_ginst (klass) ? mono_class_get_generic_type_definition (klass) : NULL;
64816481
int field_idx = field - m_class_get_fields (klass);

src/mono/mono/metadata/custom-attrs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2132,7 +2132,7 @@ mono_reflection_get_custom_attrs_info_checked (MonoObjectHandle obj, MonoError *
21322132
} else if (strcmp ("RuntimeFieldInfo", klass_name) == 0) {
21332133
MonoReflectionFieldHandle rfield = MONO_HANDLE_CAST (MonoReflectionField, obj);
21342134
MonoClassField *field = MONO_HANDLE_GETVAL (rfield, field);
2135-
cinfo = mono_custom_attrs_from_field_checked (field->parent, field, error);
2135+
cinfo = mono_custom_attrs_from_field_checked (m_field_get_parent (field), field, error);
21362136
goto_if_nok (error, leave);
21372137
} else if ((strcmp ("RuntimeMethodInfo", klass_name) == 0) || (strcmp ("RuntimeConstructorInfo", klass_name) == 0)) {
21382138
MonoReflectionMethodHandle rmethod = MONO_HANDLE_CAST (MonoReflectionMethod, obj);

src/mono/mono/metadata/debug-helpers.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,10 +868,10 @@ char *
868868
mono_field_full_name (MonoClassField *field)
869869
{
870870
char *res;
871-
const char *nspace = field->parent->name_space;
871+
const char *nspace = m_field_get_parent (field)->name_space;
872872

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

876876
return res;
877877
}

0 commit comments

Comments
 (0)