Skip to content

Commit 5881981

Browse files
authored
[2019-08] [mini] Add missing membars when initializing rgctx entries (#16909)
[2019-08] [mini] Add missing membars when initializing rgctx entries Whenever we are publishing changes to the rgctx arrays, we are racing with rgctx_lazy_fetch_trampoline, so we need to make sure any pointers we set there must have their contents initialized. Backport of #16904. /cc @marek-safar @BrzVlad
1 parent 6290b6c commit 5881981

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

mono/mini/mini-generic-sharing.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,8 +2902,12 @@ fill_runtime_generic_context (MonoVTable *class_vtable, MonoRuntimeGenericContex
29022902
}
29032903
break;
29042904
}
2905-
if (!rgctx [offset + 0])
2906-
rgctx [offset + 0] = alloc_rgctx_array (domain, i + 1, is_mrgctx);
2905+
if (!rgctx [offset + 0]) {
2906+
gpointer *array = alloc_rgctx_array (domain, i + 1, is_mrgctx);
2907+
/* Make sure that this array is zeroed if other threads access it */
2908+
mono_memory_write_barrier ();
2909+
rgctx [offset + 0] = array;
2910+
}
29072911
rgctx = (void **)rgctx [offset + 0];
29082912
first_slot += size - 1;
29092913
size = mono_class_rgctx_get_array_size (i + 1, is_mrgctx);
@@ -2930,10 +2934,13 @@ fill_runtime_generic_context (MonoVTable *class_vtable, MonoRuntimeGenericContex
29302934

29312935
/* Check whether the slot hasn't been instantiated in the
29322936
meantime. */
2933-
if (rgctx [rgctx_index])
2937+
if (rgctx [rgctx_index]) {
29342938
info = (MonoRuntimeGenericContext*)rgctx [rgctx_index];
2935-
else
2939+
} else {
2940+
/* Make sure other threads see the contents of info */
2941+
mono_memory_write_barrier ();
29362942
rgctx [rgctx_index] = info;
2943+
}
29372944

29382945
mono_domain_unlock (domain);
29392946

@@ -2964,6 +2971,8 @@ mono_class_fill_runtime_generic_context (MonoVTable *class_vtable, guint32 slot,
29642971
rgctx = class_vtable->runtime_generic_context;
29652972
if (!rgctx) {
29662973
rgctx = alloc_rgctx_array (domain, 0, FALSE);
2974+
/* Make sure that this array is zeroed if other threads access it */
2975+
mono_memory_write_barrier ();
29672976
class_vtable->runtime_generic_context = rgctx;
29682977
UnlockedIncrement (&rgctx_num_allocated); /* interlocked by domain lock */
29692978
}

0 commit comments

Comments
 (0)