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

@@ -199,7 +199,11 @@
199199 continue;
200200 }
201201 if (lv->is_param) {
202- if (needs_root(lv->type)) buf_printf(b, " SP_GC_ROOT(lv_%s);\n", lv->name);
202+ /* A poly param is an sp_RbVal by value: root through the tagged
203+ RBVAL form so the collector reads the boxed pointer, not the
204+ struct's first word (the tag). */
205+ if (lv->type == TY_POLY) buf_printf(b, " SP_GC_ROOT_RBVAL(lv_%s);\n", lv->name);
206+ else if (needs_root(lv->type)) buf_printf(b, " SP_GC_ROOT(lv_%s);\n", lv->name);
203207 }
204208 else {
205209 declare_local(c, b, lv, vol);
@@ -1007,13 +1011,15 @@
10071011 /* A class needs a GC scan iff any ivar holds a heap reference. */
10081012 int class_needs_scan(ClassInfo *ci) {
10091013 for (int i = 0; i < ci->nivars; i++) {
1010- TyKind t = ci->ivar_types[i];
1011- if (t == TY_STRING || ty_is_array(t) || ty_is_hash(t) || ty_is_object(t)) return 1;
1014+ if (needs_root(ci->ivar_types[i])) return 1;
10121015 }
10131016 return 0;
10141017 }
10151018
1016-/* Emit the GC scan function (marks heap ivars) for a class that needs one. */
1019+/* Emit the GC scan function (marks heap ivars) for a class that needs one.
1020+ Covers the same type set as needs_root: a heap reference reachable only
1021+ through an unscanned ivar would be swept out from under the object
1022+ (poly ivars holding tree children were the canonical case). */
10171023 void emit_class_scan(Compiler *c, ClassInfo *ci, Buf *b) {
10181024 (void)c;
10191025 if (!class_needs_scan(ci)) return;
@@ -1023,8 +1029,9 @@
10231029 TyKind t = ci->ivar_types[i];
10241030 const char *iv = ci->ivars[i] + 1;
10251031 if (t == TY_STRING) buf_printf(b, " sp_mark_string(o->iv_%s);\n", iv);
1026- else if (ty_is_array(t) || ty_is_hash(t) || ty_is_object(t))
1027- buf_printf(b, " if (o->iv_%s) sp_gc_mark(o->iv_%s);\n", iv, iv);
1032+ else if (t == TY_POLY) buf_printf(b, " sp_mark_rbval(o->iv_%s);\n", iv);
1033+ else if (needs_root(t))
1034+ buf_printf(b, " if (o->iv_%s) sp_gc_mark((void *)o->iv_%s);\n", iv, iv);
10281035 }
10291036 buf_puts(b, "}\n");
10301037 }
@@ -1145,7 +1152,8 @@
11451152 emit_indent(b, din);
11461153 emit_ctype(c, lv->type, b);
11471154 buf_printf(b, " lv_%s = %s;\n", rn, lv->type == TY_RANGE ? "(sp_Range){0}" : default_value(lv->type));
1148- if (needs_root(lv->type)) { emit_indent(b, din); buf_printf(b, "SP_GC_ROOT(lv_%s);\n", rn); }
1155+ if (lv->type == TY_POLY) { emit_indent(b, din); buf_printf(b, "SP_GC_ROOT_RBVAL(lv_%s);\n", rn); }
1156+ else if (needs_root(lv->type)) { emit_indent(b, din); buf_printf(b, "SP_GC_ROOT(lv_%s);\n", rn); }
11491157 }
11501158
11511159 const char *ty = nt_type(c->nt, id);