Skip to content

Commit 59d8abc

Browse files
wip calculate object id before creating MMTkFinalizeJob
1 parent a39d213 commit 59d8abc

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

gc/mmtk/mmtk.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ rb_mmtk_vm_live_bytes(void)
280280
}
281281

282282
static void set_object_has_finalizer(VALUE obj, bool has_finalizer);
283+
static void set_object_seen_obj_id(VALUE obj, bool seen);
284+
bool rb_gc_impl_object_id_seen_p(VALUE obj);
283285
bool rb_gc_impl_has_finalizer(VALUE obj);
284286
static void
285287
make_final_job(struct objspace *objspace, VALUE obj, VALUE table)
@@ -290,10 +292,21 @@ make_final_job(struct objspace *objspace, VALUE obj, VALUE table)
290292

291293
set_object_has_finalizer(obj, false);
292294

295+
// Calculate object_id before we potentially lose the metadata
296+
VALUE object_id;
297+
st_data_t val;
298+
if (rb_gc_impl_object_id_seen_p(obj) &&
299+
st_lookup(objspace->obj_to_id_tbl, (st_data_t)obj, &val)) {
300+
object_id = (VALUE)val;
301+
} else {
302+
// Get a new ID if needed
303+
object_id = rb_gc_impl_object_id(objspace, obj);
304+
}
305+
293306
struct MMTk_final_job *job = xmalloc(sizeof(struct MMTk_final_job));
294307
job->next = objspace->finalizer_jobs;
295308
job->kind = MMTK_FINAL_JOB_FINALIZE;
296-
job->as.finalize.object_id = rb_obj_id((VALUE)obj);
309+
job->as.finalize.object_id = object_id;
297310
job->as.finalize.finalizer_array = table;
298311

299312
objspace->finalizer_jobs = job;
@@ -1225,7 +1238,17 @@ rb_gc_impl_object_id(void *objspace_ptr, VALUE obj)
12251238
id = (VALUE)val;
12261239
}
12271240
else {
1228-
rb_bug("rb_gc_impl_object_id: object id seen already, but not found in table");
1241+
/*
1242+
* The object's 'seen' flag is set, but the ID isn't in the table.
1243+
* This can happen during GC when entries are removed from the table
1244+
* but the metadata wasn't properly updated. Rather than crashing,
1245+
* we'll assign a new ID.
1246+
*/
1247+
id = ULL2NUM(objspace->next_object_id);
1248+
objspace->next_object_id += OBJ_ID_INCREMENT;
1249+
1250+
st_insert(objspace->obj_to_id_tbl, (st_data_t)obj, (st_data_t)id);
1251+
st_insert(objspace->id_to_obj_tbl, (st_data_t)id, (st_data_t)obj);
12291252
}
12301253
}
12311254
else {

0 commit comments

Comments
 (0)