Skip to content

Commit dd88d29

Browse files
Fix init race in mono_class_try_get_[shortname]_class. (#112296)
Backport of #112282 to release/9.0 We observed several crash reports in dotnet Android coming from google play store, like this one: #109921 (comment) and it happens at the following callstack: mono_class_setup_vtable_full at /__w/1/s/src/mono/mono/metadata/class-setup-vtable.c:900 init_io_stream_slots at /__w/1/s/src/mono/mono/metadata/icall.c:3378 ves_icall_System_IO_Stream_HasOverriddenBeginEndRead at /__w/1/s/src/mono/mono/metadata/icall.c:3437 (inlined by) ves_icall_System_IO_Stream_HasOverriddenBeginEndRead_raw at /__w/1/s/src/mono/mono/metadata/../metadata/icall-def.h:228 Looking a little deeper at that that code path expects call to mono_class_try_get_stream_class to succeed since it calls mono_class_setup_vtable on returned class pointer. There are other places where code expectes mono_class_try_get_[shortname]_class to always succeed or it will assert. Other locations handles the case where it returns NULL meaning that the class has been linked out. After looking into the macro implementation generating the mono_class_try_get_[shortname]_class functions it appears that it has a race where one thread could return NULL even if the class successfully gets loaded by another racing thread. Initially that might have been intentionally and callers would need to redo the call, but then there is no way for callers to distinct between hitting the race and class not available. Adding to the fact that code would also expect that some critical classes never fail loading, like what init_io_stream_slots does, this race ended up in race conditions. In the past, this particular race in init_io_stream_slots was hidden due to multiple calls to mono_class_try_get_stream_class minimzing the risk of a race to cause a crash. That implementation was however changed by e74da7c ending up in just one call to mono_class_try_get_stream_class in the critical paths. Since code relies on mono_class_try_get_[shortname]_class to succeed for some critical classes, code asserts if it returns NULL or crashes. The fix will use acquire/release semantics on the static variable guarding the cached type and make sure memory order is preserved on both read and write. Previous implementation adopted a similar approach but it took a copy of the static tmp_class into a local variable meaning that memory order would not be guaranteed, even if it applied memory barriers and volatile keywords on the static variables. Fix also adds an assert into init_io_stream_slots since it expects failures in calls to mono_class_try_get_stream_class as fatal.
1 parent 71a9df7 commit dd88d29

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

src/mono/mono/metadata/class-internals.h

+9-14
Original file line numberDiff line numberDiff line change
@@ -952,25 +952,20 @@ mono_class_get_##shortname##_class (void) \
952952

953953
// GENERATE_TRY_GET_CLASS_WITH_CACHE attempts mono_class_load_from_name approximately
954954
// only once. i.e. if it fails, it will return null and not retry.
955-
// In a race it might try a few times, but not indefinitely.
956-
//
957-
// FIXME This maybe has excessive volatile/barriers.
958-
//
959955
#define GENERATE_TRY_GET_CLASS_WITH_CACHE(shortname,name_space,name) \
960956
MonoClass* \
961957
mono_class_try_get_##shortname##_class (void) \
962958
{ \
963-
static volatile MonoClass *tmp_class; \
964-
static volatile gboolean inited; \
965-
MonoClass *klass = (MonoClass *)tmp_class; \
966-
mono_memory_barrier (); \
967-
if (!inited) { \
968-
klass = mono_class_try_load_from_name (mono_class_generate_get_corlib_impl (), name_space, name); \
969-
tmp_class = klass; \
970-
mono_memory_barrier (); \
971-
inited = TRUE; \
959+
static MonoClass *cached_class; \
960+
static gboolean cached_class_inited; \
961+
gboolean tmp_inited; \
962+
mono_atomic_load_acquire(tmp_inited, gboolean, &cached_class_inited); \
963+
if (G_LIKELY(tmp_inited)) { \
964+
return (MonoClass*)cached_class; \
972965
} \
973-
return klass; \
966+
cached_class = mono_class_try_load_from_name (mono_class_generate_get_corlib_impl (), name_space, name); \
967+
mono_atomic_store_release(&cached_class_inited, TRUE); \
968+
return (MonoClass*)cached_class; \
974969
}
975970

976971
GENERATE_TRY_GET_CLASS_WITH_CACHE_DECL (safehandle)

src/mono/mono/metadata/icall.c

+2
Original file line numberDiff line numberDiff line change
@@ -3391,6 +3391,8 @@ static void
33913391
init_io_stream_slots (void)
33923392
{
33933393
MonoClass* klass = mono_class_try_get_stream_class ();
3394+
g_assert(klass);
3395+
33943396
mono_class_setup_vtable (klass);
33953397
MonoMethod **klass_methods = m_class_get_methods (klass);
33963398
if (!klass_methods) {

0 commit comments

Comments
 (0)