Skip to content

Commit

Permalink
[mono][sgen] Allocated gchandle for this object when invoking finalizers
Browse files Browse the repository at this point in the history
We were assuming the object is kept alive from the stack/regs, which is not reliable on wasm.
  • Loading branch information
BrzVlad committed Apr 24, 2024
1 parent 9aaf602 commit c02f8cd
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/mono/mono/sgen/sgen-gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2848,6 +2848,9 @@ sgen_gc_invoke_finalizers (void)

g_assert (!pending_unqueued_finalizer);

gboolean gchandle_allocated = FALSE;
guint32 gchandle = 0;

/* FIXME: batch to reduce lock contention */
while (sgen_have_pending_finalizers ()) {
GCObject *obj;
Expand Down Expand Up @@ -2878,8 +2881,16 @@ sgen_gc_invoke_finalizers (void)
if (!obj)
break;

// We explicitly pin the object via a gchandle so we don't rely on the ref being
// present on stack/regs which is not scannable on WASM.
if (!gchandle_allocated) {
gchandle = sgen_gchandle_new (obj, TRUE);
gchandle_allocated = TRUE;
} else {
sgen_gchandle_set_target (gchandle, obj);
}

count++;
/* the object is on the stack so it is pinned */
/*g_print ("Calling finalizer for object: %p (%s)\n", obj, sgen_client_object_safe_name (obj));*/
sgen_client_run_finalize (obj);
}
Expand All @@ -2889,6 +2900,9 @@ sgen_gc_invoke_finalizers (void)
pending_unqueued_finalizer = FALSE;
}

if (gchandle_allocated)
sgen_gchandle_free (gchandle);

return count;
}

Expand Down

0 comments on commit c02f8cd

Please sign in to comment.