Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mono]: Potential deadlock during EventPipe rundown using interpreter. #58996

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/mono/mono/mini/interp/interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7435,28 +7435,44 @@ interp_invalidate_transformed (void)
}

typedef struct {
InterpJitInfoFunc func;
gpointer user_data;
} InterpJitInfoFuncUserData;
MonoJitInfo **jit_info_array;
gint size;
gint next;
} InterpCopyJitInfoFuncUserData;

static void
interp_call_jit_info_func (gpointer imethod, gpointer user_data)
interp_copy_jit_info_func (gpointer imethod, gpointer user_data)
{
InterpJitInfoFuncUserData *data = (InterpJitInfoFuncUserData *)user_data;
data->func (((InterpMethod *)imethod)->jinfo, data->user_data);
InterpCopyJitInfoFuncUserData *data = (InterpCopyJitInfoFuncUserData*)user_data;
if (data->next < data->size)
data->jit_info_array [data->next++] = ((InterpMethod *)imethod)->jinfo;
}

static void
interp_jit_info_foreach (InterpJitInfoFunc func, gpointer user_data)
{
InterpJitInfoFuncUserData data = {func, user_data};
InterpCopyJitInfoFuncUserData copy_jit_info_data;

// FIXME: Enumerate all memory managers
MonoJitMemoryManager *jit_mm = get_default_jit_mm ();

jit_mm_lock (jit_mm);
mono_internal_hash_table_apply (&jit_mm->interp_code_hash, interp_call_jit_info_func, &data);
jit_mm_unlock (jit_mm);
// Can't keep memory manager lock while iterating and calling callback since it might take other locks
// causing poential deadlock situations. Instead, create copy of interpreter imethod jinfo pointers into
// plain array and use pointers from array when when running callbacks.
copy_jit_info_data.size = mono_atomic_load_i32 (&(jit_mm->interp_code_hash.num_entries));
copy_jit_info_data.next = 0;
copy_jit_info_data.jit_info_array = (MonoJitInfo**) g_new (MonoJitInfo*, copy_jit_info_data.size);
if (copy_jit_info_data.jit_info_array) {
jit_mm_lock (jit_mm);
mono_internal_hash_table_apply (&jit_mm->interp_code_hash, interp_copy_jit_info_func, &copy_jit_info_data);
jit_mm_unlock (jit_mm);
}

if (copy_jit_info_data.jit_info_array) {
for (size_t i = 0; i < copy_jit_info_data.next; ++i)
func (copy_jit_info_data.jit_info_array [i], user_data);
g_free (copy_jit_info_data.jit_info_array);
}
}

static void
Expand Down