Commit 508a32b6
508a32b696a8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 7 files · ~7 modified
Description
Fix the GC-rooting holes behind the object-graph benchmark crashes
Five distinct rooting/tracing gaps, each letting the collector sweep a live object (bm_binary_trees, bm_linked_list, bm_splay, bm_rbtree, bm_io_wordcount, and bm_ao_render's segfault):
-
Poly params and inlined-method poly locals were rooted with SP_GC_ROOT, which treats the slot as a raw object pointer; an sp_RbVal's first word is its tag, so the real boxed pointer was never marked. Use SP_GC_ROOT_RBVAL.
-
Generated class scan functions only traced string/array/hash/object ivars. A poly ivar (tree children:
@left = node_or_nil) was never marked, so entire subtrees reachable only through poly slots were swept. Trace the same type set needs_root covers, marking poly slots via sp_mark_rbval. -
Call-argument evaluation: in Node.new(make_tree(d), make_tree(d)) the first subtree sat in an unrooted C temporary while the second allocated. emit_args_filled now pre-evaluates heap-typed positional args into rooted temps (left to right) whenever a later argument may allocate, substituting the temp via a node-override in emit_expr. emit_dispatch's existing arg temps get the same rooting.
-
A hoisted method receiver (Scene.new.render(...)) was unrooted across the whole call; root the temp.
-
File handles: TY_IO (plus FIBER/RANDOM/MATCHDATA) joined needs_root, and the File.open-with-block emitter roots the handle for the block's duration — its GC finalizer fcloses, which silently truncated each_line iteration mid-loop.
All six benchmarks now run to completion; ao_render's remaining diff is the unrelated NUL-byte-in-string limitation. make test stays green (845 pass).
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 508a32b696a8
- Parent
- 672bdd12abb9
src/codegen.cmodifiedView file
continue; } if (lv->is_param) { if (needs_root(lv->type)) buf_printf(b, " SP_GC_ROOT(lv_%s);\n", lv->name); /* A poly param is an sp_RbVal by value: root through the tagged RBVAL form so the collector reads the boxed pointer, not the struct's first word (the tag). */ if (lv->type == TY_POLY) buf_printf(b, " SP_GC_ROOT_RBVAL(lv_%s);\n", lv->name); else if (needs_root(lv->type)) buf_printf(b, " SP_GC_ROOT(lv_%s);\n", lv->name); } else { declare_local(c, b, lv, vol);/* A class needs a GC scan iff any ivar holds a heap reference. */int class_needs_scan(ClassInfo *ci) { for (int i = 0; i < ci->nivars; i++) { TyKind t = ci->ivar_types[i]; if (t == TY_STRING || ty_is_array(t) || ty_is_hash(t) || ty_is_object(t)) return 1; if (needs_root(ci->ivar_types[i])) return 1; } return 0;}/* Emit the GC scan function (marks heap ivars) for a class that needs one. *//* Emit the GC scan function (marks heap ivars) for a class that needs one. Covers the same type set as needs_root: a heap reference reachable only through an unscanned ivar would be swept out from under the object (poly ivars holding tree children were the canonical case). */void emit_class_scan(Compiler *c, ClassInfo *ci, Buf *b) { (void)c; if (!class_needs_scan(ci)) return; TyKind t = ci->ivar_types[i]; const char *iv = ci->ivars[i] + 1; if (t == TY_STRING) buf_printf(b, " sp_mark_string(o->iv_%s);\n", iv); else if (ty_is_array(t) || ty_is_hash(t) || ty_is_object(t)) buf_printf(b, " if (o->iv_%s) sp_gc_mark(o->iv_%s);\n", iv, iv); else if (t == TY_POLY) buf_printf(b, " sp_mark_rbval(o->iv_%s);\n", iv); else if (needs_root(t)) buf_printf(b, " if (o->iv_%s) sp_gc_mark((void *)o->iv_%s);\n", iv, iv); } buf_puts(b, "}\n");} emit_indent(b, din); emit_ctype(c, lv->type, b); buf_printf(b, " lv_%s = %s;\n", rn, lv->type == TY_RANGE ? "(sp_Range){0}" : default_value(lv->type)); if (needs_root(lv->type)) { emit_indent(b, din); buf_printf(b, "SP_GC_ROOT(lv_%s);\n", rn); } if (lv->type == TY_POLY) { emit_indent(b, din); buf_printf(b, "SP_GC_ROOT_RBVAL(lv_%s);\n", rn); } else if (needs_root(lv->type)) { emit_indent(b, din); buf_printf(b, "SP_GC_ROOT(lv_%s);\n", rn); } } const char *ty = nt_type(c->nt, id);src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_expr.cmodifiedView file
Loading diff…
src/codegen_fold.cmodifiedView file
Loading diff…
src/codegen_internal.hmodifiedView file
Loading diff…
src/codegen_stmt.cmodifiedView file
Loading diff…
src/codegen_util.cmodifiedView file
Loading diff…