From 949a0a0c36e7704e68aeebd576d4a9b64bf42211 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 24 Aug 2021 15:27:13 -0400 Subject: [PATCH] [interp] Use existing InterpMethod if allocation and lookup race (#57985) If two threads both want to get an InterpMethod for the same MonoMethod, and they both see null from the first hash table lookup, make sure that whichever one comes into the jit_mm lock second re-uses the previously inserted InterpMethod, instead of its own version. Without this change, racing threads will overwrite MonoJitInfo:seq_points (in mono_interp_transform_method) which sometimes leads to deallocating the same sequence points multiple times. Fixes https://github.com/dotnet/runtime/issues/57812 Co-authored-by: Aleksey Kliger --- src/mono/mono/mini/interp/interp.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/mono/mono/mini/interp/interp.c b/src/mono/mono/mini/interp/interp.c index 6db2f62..623b7af 100644 --- a/src/mono/mono/mini/interp/interp.c +++ b/src/mono/mono/mini/interp/interp.c @@ -495,8 +495,12 @@ mono_interp_get_imethod (MonoMethod *method, MonoError *error) imethod->param_types [i] = mini_get_underlying_type (sig->params [i]); jit_mm_lock (jit_mm); - if (!mono_internal_hash_table_lookup (&jit_mm->interp_code_hash, method)) + InterpMethod *old_imethod; + if (!((old_imethod = mono_internal_hash_table_lookup (&jit_mm->interp_code_hash, method)))) mono_internal_hash_table_insert (&jit_mm->interp_code_hash, method, imethod); + else { + imethod = old_imethod; /* leak the newly allocated InterpMethod to the mempool */ + } jit_mm_unlock (jit_mm); imethod->prof_flags = mono_profiler_get_call_instrumentation_flags (imethod->method); -- 2.7.4