Skip to content

Commit

Permalink
[mono][sgen] Fix detection of weakref objects (#77170)
Browse files Browse the repository at this point in the history
Comparison with klass mono_defaults.generic_weakreference_class was broken because that class is a generic class while the objects have as a class generic instantiations of that so the comparison would fail. Simplify the code by adding a new gc bit where we can use name comparison.
  • Loading branch information
BrzVlad committed Oct 19, 2022
1 parent b8653dd commit 31d4e3a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
2 changes: 0 additions & 2 deletions src/mono/mono/metadata/class-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,6 @@ typedef struct {
MonoClass *generic_ienumerator_class;
MonoClass *alc_class;
MonoClass *appcontext_class;
MonoClass *weakreference_class;
MonoClass *generic_weakreference_class;
} MonoDefaults;

/* If you need a MonoType, use one of the mono_get_*_type () functions in class-inlines.h */
Expand Down
5 changes: 0 additions & 5 deletions src/mono/mono/metadata/domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,6 @@ mono_init_internal (const char *root_domain_name)
mono_defaults.alc_class = mono_class_get_assembly_load_context_class ();
mono_defaults.appcontext_class = mono_class_try_load_from_name (mono_defaults.corlib, "System", "AppContext");

mono_defaults.weakreference_class = mono_class_try_load_from_name (
mono_defaults.corlib, "System", "WeakReference");
mono_defaults.generic_weakreference_class = mono_class_try_load_from_name (
mono_defaults.corlib, "System", "WeakReference`1");

// in the past we got a filename as the root_domain_name so try to get the basename
domain->friendly_name = g_path_get_basename (root_domain_name);

Expand Down
9 changes: 7 additions & 2 deletions src/mono/mono/metadata/sgen-mono.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,12 @@ mono_gc_get_vtable_bits (MonoClass *klass)
if (fin_callbacks.is_class_finalization_aware (klass))
res |= SGEN_GC_BIT_FINALIZER_AWARE;
}

if (m_class_get_image (klass) == mono_defaults.corlib &&
strcmp (m_class_get_name_space (klass), "System") == 0 &&
strncmp (m_class_get_name (klass), "WeakReference", 13) == 0)
res |= SGEN_GC_BIT_WEAKREF;

return res;
}

Expand All @@ -457,8 +463,7 @@ is_finalization_aware (MonoObject *obj)
gboolean
sgen_client_object_finalize_eagerly (GCObject *obj)
{
if (obj->vtable->klass == mono_defaults.weakreference_class ||
obj->vtable->klass == mono_defaults.generic_weakreference_class) {
if (obj->vtable->gc_bits & SGEN_GC_BIT_WEAKREF) {
MonoWeakReference *wr = (MonoWeakReference*)obj;
MonoGCHandle gc_handle = (MonoGCHandle)(wr->handleAndKind & ~(gsize)1);
mono_gchandle_free_internal (gc_handle);
Expand Down
1 change: 1 addition & 0 deletions src/mono/mono/sgen/sgen-gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ enum {
SGEN_GC_BIT_BRIDGE_OBJECT = 1,
SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT = 2,
SGEN_GC_BIT_FINALIZER_AWARE = 4,
SGEN_GC_BIT_WEAKREF = 8,
};

void sgen_gc_init (void)
Expand Down

0 comments on commit 31d4e3a

Please sign in to comment.