Skip to content

Commit 6649e7f

Browse files
authored
Guard members of MonoType union & fix related bugs (#111645)
* Guard members of MonoType union behind type check helpers * Add _unchecked to call sites that are obviously guarded correctly * Fix type confusion in bulk_type_log_single_type * Fix genericinst fallthrough treating a MonoGenericClass ptr as a MonoClass ptr * Fix type confusion in amd64 mini * Fix type confusion in aot-runtime-wasm * Prune a dead goto to make unchecked safe * Fix two cases where we were partially initializing a stack-allocated MonoType instead of fully initializing it * See #112395 for detailed list of bugs fixed
1 parent 7715391 commit 6649e7f

32 files changed

+709
-562
lines changed

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

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,11 @@ mono_debugger_agent_init_internal (void)
779779

780780
mono_de_init (&cbs);
781781
transport_init ();
782-
782+
783783
start_debugger_thread_func = start_debugger_thread;
784784
suspend_vm_func = suspend_vm;
785785
suspend_current_func = suspend_current;
786-
786+
787787

788788
/* Need to know whenever a thread has acquired the loader mutex */
789789
mono_loader_lock_track_ownership (TRUE);
@@ -1627,7 +1627,7 @@ mono_init_debugger_agent_common (MonoProfilerHandle *prof)
16271627
mono_profiler_set_thread_started_callback (*prof, thread_startup);
16281628
mono_profiler_set_thread_stopped_callback (*prof, thread_end);
16291629
mono_profiler_set_jit_done_callback (*prof, jit_done);
1630-
1630+
16311631
mono_native_tls_alloc (&debugger_tls_id, NULL);
16321632

16331633
/* Needed by the hash_table_new_type () call below */
@@ -2634,7 +2634,7 @@ resume_vm (void)
26342634
mono_loader_unlock ();
26352635
#else
26362636
resumed_from_wasi = TRUE;
2637-
#endif
2637+
#endif
26382638
}
26392639

26402640
/*
@@ -3122,7 +3122,7 @@ compute_frame_info (MonoInternalThread *thread, DebuggerTlsData *tls, gboolean f
31223122
#else
31233123
tls->frame_count = 0;
31243124
return;
3125-
#endif
3125+
#endif
31263126
}
31273127

31283128
new_frame_count = g_slist_length (user_data.frames);
@@ -4629,7 +4629,7 @@ ensure_runtime_is_suspended (void)
46294629
{
46304630
#ifdef HOST_WASI
46314631
return ERR_NONE;
4632-
#endif
4632+
#endif
46334633
if (suspend_count == 0)
46344634
return ERR_NOT_SUSPENDED;
46354635

@@ -5291,7 +5291,7 @@ buffer_add_value_full (Buffer *buf, MonoType *t, void *addr, MonoDomain *domain,
52915291
continue;
52925292
if (CHECK_ICORDBG (TRUE))
52935293
buffer_add_int (buf, mono_class_get_field_token (f));
5294-
if (mono_vtype_get_field_addr (addr, f) == addr && mono_class_from_mono_type_internal (t) == mono_class_from_mono_type_internal (f->type) && !boxed_vtype) //to avoid infinite recursion
5294+
if (mono_vtype_get_field_addr (addr, f) == addr && mono_class_from_mono_type_internal (t) == mono_class_from_mono_type_internal (f->type) && !boxed_vtype) //to avoid infinite recursion
52955295
{
52965296
gssize val = *(gssize*)addr;
52975297
if (CHECK_ICORDBG (TRUE))
@@ -5351,7 +5351,7 @@ obj_is_of_type (MonoObject *obj, MonoType *t)
53515351
static ErrorCode
53525352
decode_value (MonoType *t, MonoDomain *domain, gpointer void_addr, gpointer void_buf, guint8 **endbuf, guint8 *limit, gboolean check_field_datatype, guint8 **extra_space, gboolean from_by_ref_value_type);
53535353

5354-
static int
5354+
static int
53555355
decode_value_compute_size (MonoType *t, int type, MonoDomain *domain, guint8 *buf, guint8 **endbuf, guint8 *limit, gboolean from_by_ref_value_type);
53565356

53575357
static ErrorCode
@@ -5419,7 +5419,7 @@ decode_vtype (MonoType *t, MonoDomain *domain, gpointer void_addr, gpointer void
54195419
return ERR_NONE;
54205420
}
54215421

5422-
static ErrorCode
5422+
static ErrorCode
54235423
decode_fixed_size_array_internal (MonoType *t, int type, MonoDomain *domain, guint8 *addr, guint8 *buf, guint8 **endbuf, guint8 *limit, gboolean check_field_datatype)
54245424
{
54255425
ErrorCode err = ERR_NONE;
@@ -5475,7 +5475,7 @@ decode_fixed_size_array_internal (MonoType *t, int type, MonoDomain *domain, gui
54755475
}
54765476

54775477

5478-
static int
5478+
static int
54795479
decode_fixed_size_array_compute_size_internal (MonoType *t, int type, MonoDomain *domain, guint8 *buf, guint8 **endbuf, guint8 *limit)
54805480
{
54815481
int ret = 0;
@@ -5510,7 +5510,7 @@ decode_fixed_size_array_compute_size_internal (MonoType *t, int type, MonoDomain
55105510
ret += sizeof(guint32);
55115511
break;
55125512
case MONO_TYPE_I8:
5513-
case MONO_TYPE_U8:
5513+
case MONO_TYPE_U8:
55145514
case MONO_TYPE_R8:
55155515
decode_long (buf, &buf, limit);
55165516
ret += sizeof(guint64);
@@ -5561,7 +5561,7 @@ decode_vtype_compute_size (MonoType *t, MonoDomain *domain, gpointer void_buf, g
55615561
continue;
55625562
if (mono_field_is_deleted (f))
55635563
continue;
5564-
5564+
55655565
gboolean cur_field_in_extra_space = from_by_ref_value_type;
55665566
gboolean members_in_extra_space = cur_field_in_extra_space || m_type_is_byref (f->type) || m_class_is_byreflike (mono_class_from_mono_type_internal (f->type));
55675567
int field_size = decode_value_compute_size (f->type, 0, domain, buf, &buf, limit, members_in_extra_space);
@@ -5587,7 +5587,7 @@ decode_vtype_compute_size (MonoType *t, MonoDomain *domain, gpointer void_buf, g
55875587
return ret;
55885588
}
55895589

5590-
static int
5590+
static int
55915591
decode_value_compute_size (MonoType *t, int type, MonoDomain *domain, guint8 *buf, guint8 **endbuf, guint8 *limit, gboolean from_by_ref_value_type)
55925592
{
55935593
if (type == 0)
@@ -5632,12 +5632,12 @@ decode_value_compute_size (MonoType *t, int type, MonoDomain *domain, guint8 *bu
56325632
break;
56335633
case MONO_TYPE_I4:
56345634
case MONO_TYPE_U4:
5635-
case MONO_TYPE_R4:
5635+
case MONO_TYPE_R4:
56365636
decode_int (buf, &buf, limit);
56375637
ret += sizeof(guint32);
56385638
break;
56395639
case MONO_TYPE_I8:
5640-
case MONO_TYPE_U8:
5640+
case MONO_TYPE_U8:
56415641
case MONO_TYPE_R8:
56425642
decode_long (buf, &buf, limit);
56435643
ret += sizeof(guint64);
@@ -5693,8 +5693,8 @@ decode_value_compute_size (MonoType *t, int type, MonoDomain *domain, guint8 *bu
56935693
goto end;
56945694
}
56955695
} else if ((t->type == MONO_TYPE_GENERICINST) &&
5696-
mono_metadata_generic_class_is_valuetype (t->data.generic_class) &&
5697-
m_class_is_enumtype (t->data.generic_class->container_class)){
5696+
mono_metadata_generic_class_is_valuetype (m_type_data_get_generic_class_unchecked (t)) &&
5697+
m_class_is_enumtype (m_type_data_get_generic_class_unchecked (t)->container_class)){
56985698
ret += decode_vtype_compute_size (t, domain, buf, &buf, limit, from_by_ref_value_type);
56995699
} else {
57005700
NOT_IMPLEMENTED;
@@ -5916,8 +5916,8 @@ decode_value_internal (MonoType *t, int type, MonoDomain *domain, guint8 *addr,
59165916
return ERR_INVALID_ARGUMENT;
59175917
}
59185918
} else if ((t->type == MONO_TYPE_GENERICINST) &&
5919-
mono_metadata_generic_class_is_valuetype (t->data.generic_class) &&
5920-
m_class_is_enumtype (t->data.generic_class->container_class)){
5919+
mono_metadata_generic_class_is_valuetype (m_type_data_get_generic_class_unchecked (t)) &&
5920+
m_class_is_enumtype (m_type_data_get_generic_class_unchecked (t)->container_class)){
59215921
err = decode_vtype (t, domain, addr, buf, &buf, limit, check_field_datatype, extra_space, from_by_ref_value_type);
59225922
if (err != ERR_NONE)
59235923
return err;
@@ -5944,7 +5944,7 @@ decode_value (MonoType *t, MonoDomain *domain, gpointer void_addr, gpointer void
59445944
int type = decode_byte (buf, &buf, limit);
59455945

59465946
if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type_internal (t))) {
5947-
MonoType *targ = t->data.generic_class->context.class_inst->type_argv [0];
5947+
MonoType *targ = m_type_data_get_generic_class_unchecked (t)->context.class_inst->type_argv [0];
59485948
guint8 *nullable_buf;
59495949

59505950
/*
@@ -6359,7 +6359,7 @@ mono_do_invoke_method (DebuggerTlsData *tls, Buffer *buf, InvokeData *invoke, gu
63596359
MonoDomain *domain;
63606360
guint8 *this_buf;
63616361
guint8 *extra_space = NULL;
6362-
int extra_space_size = 0;
6362+
int extra_space_size = 0;
63636363
#ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
63646364
MonoLMFExt ext;
63656365
#endif
@@ -7099,7 +7099,7 @@ find_assembly_by_name (char* assembly_name)
70997099
//resolve the assembly
71007100
MonoImageOpenStatus status;
71017101
MonoAssemblyName* aname = mono_assembly_name_new (lookup_name);
7102-
g_free (lookup_name);
7102+
g_free (lookup_name);
71037103
if (!aname) {
71047104
PRINT_DEBUG_MSG (1, "Could not resolve assembly %s\n", assembly_name);
71057105
return NULL;
@@ -7518,7 +7518,7 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
75187518
guint8* memory = (guint8*)GINT_TO_POINTER (decode_long (p, &p, end));
75197519
int size = decode_int (p, &p, end);
75207520
if (!valid_memory_address(memory, size))
7521-
return ERR_INVALID_ARGUMENT;
7521+
return ERR_INVALID_ARGUMENT;
75227522
PRINT_DEBUG_MSG (1, "MDBGPROT_CMD_VM_READ_MEMORY - [%p] - size - %d\n", memory, size);
75237523
buffer_add_byte_array (buf, memory, size);
75247524
break;
@@ -7595,7 +7595,7 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
75957595
break;
75967596
case MONO_TYPE_I8:
75977597
buffer_add_typeid (buf, mono_get_root_domain (), mono_get_int64_class ());
7598-
break;
7598+
break;
75997599
case MONO_TYPE_U8:
76007600
buffer_add_typeid (buf, mono_get_root_domain (), mono_get_uint64_class ());
76017601
break;
@@ -7642,7 +7642,7 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
76427642
symfile_size = ppdb_compressed_size;
76437643
pdb_bytes = ppdb_data;
76447644
}
7645-
}
7645+
}
76467646
}
76477647
m_dbgprot_buffer_init (buf, assembly_size + symfile_size + 1024);
76487648
m_dbgprot_buffer_add_byte_array (buf, (uint8_t *) assembly_bytes, assembly_size);
@@ -7805,7 +7805,7 @@ event_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
78057805

78067806
if (req->event_kind == EVENT_KIND_BREAKPOINT) {
78077807
g_assert (method);
7808-
7808+
78097809
req->info = mono_de_set_breakpoint (method, location, req, error);
78107810
if (!is_ok (error)) {
78117811
g_free (req);
@@ -8064,7 +8064,7 @@ domain_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
80648064
klass = decode_typeid (p, &p, end, NULL, &err);
80658065
int rank = decode_int (p, &p, end);
80668066
uintptr_t *lengths = g_newa (uintptr_t, rank);
8067-
intptr_t *lower_bounds = g_newa (intptr_t, rank);
8067+
intptr_t *lower_bounds = g_newa (intptr_t, rank);
80688068
for (int i = 0 ; i < rank; i++)
80698069
{
80708070
lengths [i] = decode_int (p, &p, end);
@@ -8628,10 +8628,10 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
86288628
{
86298629
if (type->type == MONO_TYPE_FNPTR)
86308630
{
8631-
buffer_add_int (buf, 1 + type->data.method->param_count);
8632-
buffer_add_typeid (buf, domain, mono_class_from_mono_type_internal (type->data.method->ret));
8633-
for (int j = 0; j < type->data.method->param_count; ++j) {
8634-
buffer_add_typeid (buf, domain, mono_class_from_mono_type_internal (type->data.method->params[j]));
8631+
buffer_add_int (buf, 1 + m_type_data_get_method_unchecked (type)->param_count);
8632+
buffer_add_typeid (buf, domain, mono_class_from_mono_type_internal (m_type_data_get_method_unchecked (type)->ret));
8633+
for (int j = 0; j < m_type_data_get_method_unchecked (type)->param_count; ++j) {
8634+
buffer_add_typeid (buf, domain, mono_class_from_mono_type_internal (m_type_data_get_method_unchecked (type)->params[j]));
86358635
}
86368636
} else {
86378637
buffer_add_int (buf, 0);
@@ -8830,7 +8830,7 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
88308830
}
88318831
case MDBGPROT_CMD_TYPE_SET_VALUES_BY_FIELD_TOKEN: {
88328832
len = 0;
8833-
gpointer iter = NULL;
8833+
gpointer iter = NULL;
88348834
len = decode_int (p, &p, end);
88358835
int field_token = decode_int (p, &p, end);
88368836
while ((f = mono_class_get_fields_internal (klass, &iter))) {
@@ -9122,18 +9122,18 @@ type_commands_internal (int command, MonoClass *klass, MonoDomain *domain, guint
91229122
g_free (type_argv);
91239123
break;
91249124
}
9125-
case MDBGPROT_CMD_TYPE_ELEMENT_TYPE:
9125+
case MDBGPROT_CMD_TYPE_ELEMENT_TYPE:
91269126
{
91279127
buffer_add_int (buf, m_class_get_byval_arg (klass)->type);
91289128
buffer_add_byte (buf, GINT_TO_UINT8 (MONO_TYPE_ISSTRUCT (m_class_get_byval_arg (klass))));
91299129
break;
91309130
}
9131-
case MDBGPROT_CMD_TYPE_RANK:
9131+
case MDBGPROT_CMD_TYPE_RANK:
91329132
{
91339133
buffer_add_byte (buf, m_class_get_rank (klass));
91349134
break;
91359135
}
9136-
case MDBGPROT_CMD_TYPE_GET_FIELD_RVA:
9136+
case MDBGPROT_CMD_TYPE_GET_FIELD_RVA:
91379137
{
91389138
gpointer iter = NULL;
91399139
int field_token = decode_int (p, &p, end);
@@ -9683,9 +9683,9 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
96839683
MonoGenericContext *context;
96849684
GString *res;
96859685
res = g_string_new ("");
9686-
mono_type_get_desc (res, m_class_get_byval_arg (type->data.generic_class->container_class), TRUE);
9686+
mono_type_get_desc (res, m_class_get_byval_arg (m_type_data_get_generic_class_unchecked (type)->container_class), TRUE);
96879687
buffer_add_string (buf, (g_string_free (res, FALSE)));
9688-
context = &type->data.generic_class->context;
9688+
context = &m_type_data_get_generic_class_unchecked (type)->context;
96899689
if (context->class_inst)
96909690
buffer_add_int (buf, context->class_inst->type_argc);
96919691
else
@@ -9892,7 +9892,7 @@ thread_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
98929892
if (suspend_count)
98939893
wait_for_suspend ();
98949894
}
9895-
#endif
9895+
#endif
98969896
/*
98979897
if (suspend_count)
98989898
wait_for_suspend ();
@@ -10548,7 +10548,7 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
1054810548
break;
1054910549
}
1055010550
}
10551-
//Above for loop might end if 'k' is null , ensure 'k' is not
10551+
//Above for loop might end if 'k' is null , ensure 'k' is not
1055210552
//null before passing it to mono_class_get_fields_internal to avoid crash
1055310553
while (k && (f = mono_class_get_fields_internal (k, &iter))) {
1055410554
if (mono_class_get_field_token (f) == field_token) {
@@ -10699,7 +10699,7 @@ object_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
1069910699
break;
1070010700
case MDBGPROT_CMD_OBJECT_IS_DELEGATE: {
1070110701
MonoType *type = m_class_get_byval_arg (obj_type);
10702-
if (m_class_is_delegate (obj_type) || (type->type == MONO_TYPE_GENERICINST && m_class_is_delegate (type->data.generic_class->container_class)))
10702+
if (m_class_is_delegate (obj_type) || (type->type == MONO_TYPE_GENERICINST && m_class_is_delegate (m_type_data_get_generic_class_unchecked (type)->container_class)))
1070310703
buffer_add_byte (buf, TRUE);
1070410704
else
1070510705
buffer_add_byte (buf, FALSE);
@@ -11156,7 +11156,7 @@ gboolean mono_debugger_agent_receive_and_process_command (void)
1115611156
Buffer buf;
1115711157
ErrorCode err;
1115811158
gboolean no_reply;
11159-
11159+
1116011160
gboolean log_each_step = g_hasenv ("MONO_DEBUGGER_LOG_AFTER_COMMAND");
1116111161

1116211162
while (TRUE) {

src/mono/mono/component/marshal-ilgen.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ static MonoComponentMarshalILgen component_func_table = {
5454
&ilgen_init_internal,
5555
&emit_marshal_ilgen,
5656
&ilgen_install_callbacks_mono,
57-
};
57+
};
5858

5959

6060
MonoComponentMarshalILgen*
61-
mono_component_marshal_ilgen_init (void)
61+
mono_component_marshal_ilgen_init (void)
6262
{
6363
return &component_func_table;
6464
}
@@ -842,7 +842,7 @@ emit_marshal_ptr_ilgen (EmitMarshalContext *m, int argnum, MonoType *t,
842842
case MARSHAL_ACTION_CONV_IN:
843843
/* MS seems to allow this in some cases, ie. bxc #158 */
844844
/*
845-
if (MONO_TYPE_ISSTRUCT (t->data.type) && !mono_class_from_mono_type_internal (t->data.type)->blittable) {
845+
if (MONO_TYPE_ISSTRUCT (m_type_data_get_type (t)) && !mono_class_from_mono_type_internal (m_type_data_get_type (t))->blittable) {
846846
char *msg = g_strdup_printf ("Can not marshal 'parameter #%d': Pointers can not reference marshaled structures. Use byref instead.", argnum + 1);
847847
cb_to_mono->methodBuilder.emit_exception_marshal_directive (m->mb, msg);
848848
}
@@ -1965,7 +1965,7 @@ emit_marshal_safehandle_ilgen (EmitMarshalContext *m, int argnum, MonoType *t,
19651965
* leak the handle. We should move the allocation of the SafeHandle to the
19661966
* input marshalling code to prevent that.
19671967
*/
1968-
ctor = mono_class_get_method_from_name_checked (t->data.klass, ".ctor", 0, 0, local_error);
1968+
ctor = mono_class_get_method_from_name_checked (m_type_data_get_klass (t), ".ctor", 0, 0, local_error);
19691969
if (ctor == NULL || !is_ok (local_error)){
19701970
cb_to_mono->methodBuilder.emit_exception (mb, "MissingMethodException", "parameterless constructor required");
19711971
mono_error_cleanup (local_error);
@@ -2003,13 +2003,15 @@ emit_marshal_safehandle_ilgen (EmitMarshalContext *m, int argnum, MonoType *t,
20032003
MonoMethod *ctor = NULL;
20042004
int intptr_handle_slot;
20052005

2006-
if (mono_class_is_abstract (t->data.klass)) {
2006+
MonoClass *klass_of_t = m_type_data_get_klass (t);
2007+
2008+
if (mono_class_is_abstract (klass_of_t)) {
20072009
cb_to_mono->methodBuilder.emit_byte (mb, CEE_POP);
20082010
cb_to_mono->methodBuilder.emit_exception_marshal_directive (mb, g_strdup ("Returned SafeHandles should not be abstract"));
20092011
break;
20102012
}
20112013

2012-
ctor = mono_class_get_method_from_name_checked (t->data.klass, ".ctor", 0, 0, error);
2014+
ctor = mono_class_get_method_from_name_checked (klass_of_t, ".ctor", 0, 0, error);
20132015
if (ctor == NULL || !is_ok (error)){
20142016
mono_error_cleanup (error);
20152017
cb_to_mono->methodBuilder.emit_byte (mb, CEE_POP);
@@ -2628,16 +2630,16 @@ emit_marshal_ilgen (EmitMarshalContext *m, int argnum, MonoType *t,
26282630

26292631
switch (t->type) {
26302632
case MONO_TYPE_VALUETYPE:
2631-
if (t->data.klass == cb_to_mono->class_try_get_handleref_class ())
2633+
if (m_type_data_get_klass_unchecked (t) == cb_to_mono->class_try_get_handleref_class ())
26322634
return emit_marshal_handleref_ilgen (m, argnum, t, spec, conv_arg, conv_arg_type, action);
26332635

26342636
return emit_marshal_vtype_ilgen (m, argnum, t, spec, conv_arg, conv_arg_type, action);
26352637
case MONO_TYPE_STRING:
26362638
return emit_marshal_string_ilgen (m, argnum, t, spec, conv_arg, conv_arg_type, action);
26372639
case MONO_TYPE_CLASS:
26382640
case MONO_TYPE_OBJECT:
2639-
if (cb_to_mono->try_get_safehandle_class () != NULL && t->data.klass &&
2640-
cb_to_mono->is_subclass_of_internal (t->data.klass, cb_to_mono->try_get_safehandle_class (), FALSE))
2641+
if (cb_to_mono->try_get_safehandle_class () != NULL && m_type_data_get_klass_unchecked (t) &&
2642+
cb_to_mono->is_subclass_of_internal (m_type_data_get_klass_unchecked (t), cb_to_mono->try_get_safehandle_class (), FALSE))
26412643
return emit_marshal_safehandle_ilgen (m, argnum, t, spec, conv_arg, conv_arg_type, action);
26422644

26432645
return emit_marshal_object_ilgen (m, argnum, t, spec, conv_arg, conv_arg_type, action);

0 commit comments

Comments
 (0)