Skip to content

Commit 4fc45ee

Browse files
thaystgUnityAlex
authored andcommitted
[debugger] Removing some asserts (mono#19758)
* Removing some asserts and returning err_invalid_argument with an error message when it's possible. * if we don't find method get_ObjectIdForDebugger we try to find the property Task to continue async debug. Cherry-picked by Alex Thibodeau with some moderate changes
1 parent 127b402 commit 4fc45ee

File tree

1 file changed

+67
-18
lines changed

1 file changed

+67
-18
lines changed

mono/mini/debugger-agent.c

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5442,7 +5442,16 @@ get_object_id_for_debugger_method (MonoClass* async_builder_class)
54425442
MonoError error;
54435443
GPtrArray *array = mono_class_get_methods_by_name (async_builder_class, "get_ObjectIdForDebugger", 0x24, FALSE, FALSE, &error);
54445444
mono_error_assert_ok (&error);
5445-
g_assert (array->len == 1);
5445+
if (array->len != 1) {
5446+
g_ptr_array_free (array, TRUE);
5447+
//if we don't find method get_ObjectIdForDebugger we try to find the property Task to continue async debug.
5448+
MonoProperty *prop = mono_class_get_property_from_name (async_builder_class, "Task");
5449+
if (!prop) {
5450+
DEBUG_PRINTF (1, "Impossible to debug async methods.\n");
5451+
return NULL;
5452+
}
5453+
return prop->get;
5454+
}
54465455
MonoMethod *method = (MonoMethod *)g_ptr_array_index (array, 0);
54475456
g_ptr_array_free (array, TRUE);
54485457
return method;
@@ -5460,7 +5469,9 @@ get_class_to_get_builder_field(StackFrame *frame)
54605469
MonoGenericContext context;
54615470
MonoType *inflated_type;
54625471

5463-
g_assert (this_obj);
5472+
if (!this_obj)
5473+
return NULL;
5474+
54645475
context = mono_get_generic_context_from_stack_frame (frame->ji, this_obj->vtable);
54655476
inflated_type = mono_class_inflate_generic_type_checked (&original_class->byval_arg, &context, &error);
54665477
mono_error_assert_ok (&error); /* FIXME don't swallow the error */
@@ -5484,7 +5495,8 @@ get_async_method_builder (StackFrame *frame)
54845495

54855496
klass = get_class_to_get_builder_field(frame);
54865497
builder_field = mono_class_get_field_from_name_full (klass, "<>t__builder", NULL);
5487-
g_assert (builder_field);
5498+
if (!builder_field)
5499+
return NULL;
54885500

54895501
this_addr = get_this_addr (frame);
54905502
if (!this_addr)
@@ -5524,7 +5536,8 @@ get_this_async_id (StackFrame *frame)
55245536
return 0;
55255537

55265538
builder_field = mono_class_get_field_from_name (get_class_to_get_builder_field(frame), "<>t__builder");
5527-
g_assert (builder_field);
5539+
if (!builder_field)
5540+
return 0;
55285541

55295542
tls = (DebuggerTlsData *)mono_native_tls_get_value (debugger_tls_id);
55305543
if (tls) {
@@ -5533,6 +5546,11 @@ get_this_async_id (StackFrame *frame)
55335546
}
55345547

55355548
method = get_object_id_for_debugger_method (mono_class_from_mono_type (mono_field_get_type (builder_field)));
5549+
if (!method) {
5550+
if (tls)
5551+
tls->disable_breakpoints = old_disable_breakpoints;
5552+
return 0;
5553+
}
55365554
obj = mono_runtime_try_invoke (method, builder, NULL, &ex, &error);
55375555
mono_error_assert_ok (&error);
55385556

@@ -5548,9 +5566,11 @@ static gboolean
55485566
set_set_notification_for_wait_completion_flag (StackFrame *frame)
55495567
{
55505568
MonoClassField *builder_field = mono_class_get_field_from_name (get_class_to_get_builder_field(frame), "<>t__builder");
5551-
g_assert (builder_field);
5569+
if (!builder_field)
5570+
return FALSE;
55525571
gpointer builder = get_async_method_builder (frame);
5553-
g_assert (builder);
5572+
if (!builder)
5573+
return FALSE;
55545574

55555575
void* args [1];
55565576
gboolean arg = TRUE;
@@ -6042,7 +6062,6 @@ process_single_step_inner (DebuggerTlsData *tls, gboolean from_signal)
60426062
return;
60436063
}
60446064

6045-
60466065
/*
60476066
* The ip points to the instruction causing the single step event, which is before
60486067
* the offset recorded in the seq point map, so find the next seq point after ip.
@@ -6814,7 +6833,10 @@ ss_create (MonoInternalThread *thread, StepSize size, StepDepth depth, StepFilte
68146833
* We are stopped at a throw site. Stepping should go to the catch site.
68156834
*/
68166835
frame = tls->catch_frame;
6817-
g_assert (frame.type == FRAME_TYPE_MANAGED || frame.type == FRAME_TYPE_INTERP);
6836+
if (frame.type != FRAME_TYPE_MANAGED && frame.type != FRAME_TYPE_INTERP) {
6837+
DEBUG_PRINTF (1, "Current frame is not managed nor interpreter.\n");
6838+
return ERR_INVALID_ARGUMENT;
6839+
}
68186840

68196841
/*
68206842
* Find the seq point corresponding to the landing site ip, which is the first seq
@@ -6824,7 +6846,11 @@ ss_create (MonoInternalThread *thread, StepSize size, StepDepth depth, StepFilte
68246846
sp = (found_sp)? &local_sp : NULL;
68256847
if (!sp)
68266848
no_seq_points_found (frame.method, frame.native_offset);
6827-
g_assert (sp);
6849+
6850+
if (!sp) {
6851+
DEBUG_PRINTF (1, "Could not find next sequence point.\n");
6852+
return ERR_INVALID_ARGUMENT;
6853+
}
68286854

68296855
method = frame.method;
68306856

@@ -6870,7 +6896,10 @@ ss_create (MonoInternalThread *thread, StepSize size, StepDepth depth, StepFilte
68706896
sp = (found_sp)? &local_sp : NULL;
68716897
if (!sp)
68726898
no_seq_points_found (frame->method, frame->native_offset);
6873-
g_assert (sp);
6899+
if (!sp) {
6900+
DEBUG_PRINTF (1, "Could not find next sequence point.\n");
6901+
return ERR_INVALID_ARGUMENT;
6902+
}
68746903
method = frame->method;
68756904
}
68766905
}
@@ -10838,7 +10867,11 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
1083810867
if (mono_class_get_context (klass)) {
1083910868
MonoError error;
1084010869
result = mono_class_inflate_generic_method_full_checked (result, klass, mono_class_get_context (klass), &error);
10841-
g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
10870+
if (!mono_error_ok (&error)) {
10871+
buffer_add_string (buf, mono_error_get_message (&error));
10872+
mono_error_cleanup (&error);
10873+
return ERR_INVALID_ARGUMENT;
10874+
}
1084210875
}
1084310876
}
1084410877
}
@@ -10981,7 +11014,12 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
1098111014
char *s;
1098211015

1098311016
s = mono_string_to_utf8_checked ((MonoString *)val, &error);
10984-
mono_error_assert_ok (&error);
11017+
if (!mono_error_ok (&error)) {
11018+
buffer_add_string (buf, mono_error_get_message (&error));
11019+
mono_error_cleanup (&error);
11020+
g_free (s);
11021+
return ERR_INVALID_ARGUMENT;
11022+
}
1098511023
buffer_add_byte (buf, TOKEN_TYPE_STRING);
1098611024
buffer_add_string (buf, s);
1098711025
g_free (s);
@@ -11049,7 +11087,11 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
1104911087
tmp_context.method_inst = ginst;
1105011088

1105111089
inflated = mono_class_inflate_generic_method_checked (method, &tmp_context, &error);
11052-
g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
11090+
if (!mono_error_ok (&error)) {
11091+
buffer_add_string (buf, mono_error_get_message (&error));
11092+
mono_error_cleanup (&error);
11093+
return ERR_INVALID_ARGUMENT;
11094+
}
1105311095
if (!mono_verifier_is_method_valid_generic_instantiation (inflated))
1105411096
return ERR_INVALID_ARGUMENT;
1105511097
buffer_add_methodid (buf, domain, inflated);
@@ -11518,7 +11560,10 @@ frame_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
1151811560
set_interp_var (&frame->actual_method->klass->this_arg, addr, val_buf);
1151911561
} else {
1152011562
var = jit->this_var;
11521-
g_assert (var);
11563+
if (!var) {
11564+
buffer_add_string (buf, "Invalid this object");
11565+
return ERR_INVALID_ARGUMENT;
11566+
}
1152211567

1152311568
set_var (&frame->actual_method->klass->this_arg, var, &frame->ctx, frame->domain, val_buf, frame->reg_locations, &tls->restore_state.ctx);
1152411569
}
@@ -11561,9 +11606,11 @@ array_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
1156111606
index = decode_int (p, &p, end);
1156211607
len = decode_int (p, &p, end);
1156311608

11564-
g_assert (index >= 0 && len >= 0);
11609+
if (index < 0 || len < 0)
11610+
return ERR_INVALID_ARGUMENT;
1156511611
// Reordered to avoid integer overflow
11566-
g_assert (!(index > arr->max_length - len));
11612+
if (index > arr->max_length - len)
11613+
return ERR_INVALID_ARGUMENT;
1156711614

1156811615
esize = mono_array_element_size (mono_object_get_class (&arr->obj));
1156911616
for (i = index; i < index + len; ++i) {
@@ -11575,9 +11622,11 @@ array_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
1157511622
index = decode_int (p, &p, end);
1157611623
len = decode_int (p, &p, end);
1157711624

11578-
g_assert (index >= 0 && len >= 0);
11625+
if (index < 0 || len < 0)
11626+
return ERR_INVALID_ARGUMENT;
1157911627
// Reordered to avoid integer overflow
11580-
g_assert (!(index > arr->max_length - len));
11628+
if (index > arr->max_length - len)
11629+
return ERR_INVALID_ARGUMENT;
1158111630

1158211631
esize = mono_array_element_size (mono_object_get_class (&arr->obj));
1158311632
for (i = index; i < index + len; ++i) {

0 commit comments

Comments
 (0)