Skip to content

Commit c02f8cd

Browse files
committed
[mono][sgen] Allocated gchandle for this object when invoking finalizers
We were assuming the object is kept alive from the stack/regs, which is not reliable on wasm.
1 parent 9aaf602 commit c02f8cd

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/mono/mono/sgen/sgen-gc.c

+15-1
Original file line numberDiff line numberDiff line change
@@ -2848,6 +2848,9 @@ sgen_gc_invoke_finalizers (void)
28482848

28492849
g_assert (!pending_unqueued_finalizer);
28502850

2851+
gboolean gchandle_allocated = FALSE;
2852+
guint32 gchandle = 0;
2853+
28512854
/* FIXME: batch to reduce lock contention */
28522855
while (sgen_have_pending_finalizers ()) {
28532856
GCObject *obj;
@@ -2878,8 +2881,16 @@ sgen_gc_invoke_finalizers (void)
28782881
if (!obj)
28792882
break;
28802883

2884+
// We explicitly pin the object via a gchandle so we don't rely on the ref being
2885+
// present on stack/regs which is not scannable on WASM.
2886+
if (!gchandle_allocated) {
2887+
gchandle = sgen_gchandle_new (obj, TRUE);
2888+
gchandle_allocated = TRUE;
2889+
} else {
2890+
sgen_gchandle_set_target (gchandle, obj);
2891+
}
2892+
28812893
count++;
2882-
/* the object is on the stack so it is pinned */
28832894
/*g_print ("Calling finalizer for object: %p (%s)\n", obj, sgen_client_object_safe_name (obj));*/
28842895
sgen_client_run_finalize (obj);
28852896
}
@@ -2889,6 +2900,9 @@ sgen_gc_invoke_finalizers (void)
28892900
pending_unqueued_finalizer = FALSE;
28902901
}
28912902

2903+
if (gchandle_allocated)
2904+
sgen_gchandle_free (gchandle);
2905+
28922906
return count;
28932907
}
28942908

0 commit comments

Comments
 (0)