Commit 65067034
650670341b24Merged
Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): emit GC scan function for tuples holding GC pointers
Tuples were allocated with sp_gc_alloc(..., NULL, NULL), so the GC kept the tuple itself alive but never traced its fields. When a tuple's only reference to an inner sp_IntArray / sp_String / etc. was the field itself, the next collection swept the inner object and any later allocation could reuse the memory, leaving a dangling pointer in the tuple — e.g. arr.partition { ... }[0].length returned the length of some unrelated array after enough GC pressure.
For each registered tuple type that has at least one pointer field, emit a static _scan(void *) that calls sp_gc_mark on each ptr field and sp_mark_rbval on poly fields. Wire every tuple sp_gc_alloc site (String#partition/rpartition, Integer/Float#divmod, Array#zip heterogeneous, Array#partition with block, Array#minmax, Hash#to_a, tuple literals) to pass the per-type scan via the new tuple_scan_name helper. Tuples with only scalar fields (int/float/bool/symbol) keep scan=NULL.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 650670341b24
- Parent
- 0c3c5d918fc5
spinel_codegen.rbmodifiedView file
"sp_Tuple_" + tuple_elem_types_str(t).split(",").join("_") end # Whether a tuple element type must be traced by the GC scan function. # Scalars (int/float/bool/symbol) are pure values; pointer-to-GC-object # element types must be marked, otherwise the GC frees the inner object # while the tuple keeps a dangling pointer. def tuple_field_needs_mark(et) if et == "poly" return 1 end if et == "int" || et == "float" || et == "bool" || et == "symbol" || et == "void" || et == "nil" return 0 end type_is_pointer(et) end # Returns the scan function name for the tuple, or "NULL" if no field # requires marking. def tuple_scan_name(t) parts = tuple_elem_types_str(t).split(",") fi = 0 while fi < parts.length if tuple_field_needs_mark(parts[fi]) == 1 return tuple_c_name(t) + "_scan" end fi = fi + 1 end "NULL" end def register_tuple_type(t) if is_tuple_type(t) == 1 k = 0 @deferred_tuple << " } " @deferred_tuple << name @deferred_tuple << ";\n" # GC scan function — only emit when at least one field is a GC ref. # Without this the tuple's children are collected while the tuple # itself is still alive, leaving dangling pointers in the fields. needs_scan = 0 fi = 0 while fi < parts.length if tuple_field_needs_mark(parts[fi]) == 1 needs_scan = 1 end fi = fi + 1 end if needs_scan == 1 body = "" fi = 0 while fi < parts.length if tuple_field_needs_mark(parts[fi]) == 1 field = "_t->_" + fi.to_s if parts[fi] == "poly" body = body + " sp_mark_rbval(" + field + ");" else body = body + " sp_gc_mark((void *)" + field + ");" end end fi = fi + 1 end @deferred_tuple << "static void " + name + "_scan(void *_p) { " + name + " *_t = (" + name + " *)_p;" + body + " }\n" end k = k + 1 end end tname = tuple_c_name(tt) sep = compile_arg0(nid) tmp = new_temp emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);") emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");") emit(" { const char *_p = strstr(" + rc + ", " + sep + ");") emit(" if (_p) { " + tmp + "->_0 = sp_str_substr(" + rc + ", 0, _p - " + rc + "); " + tmp + "->_1 = " + sep + "; " + tmp + "->_2 = sp_str_substr(" + rc + ", _p - " + rc + " + strlen(" + sep + "), strlen(_p) - strlen(" + sep + ")); }") emit(" else { " + tmp + "->_0 = " + rc + "; " + tmp + "->_1 = \"\"; " + tmp + "->_2 = \"\"; } }") tname = tuple_c_name(tt) sep = compile_arg0(nid) tmp = new_temp emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);") emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");") emit(" { size_t _sl = strlen(" + rc + "), _pl = strlen(" + sep + "); const char *_last = NULL;") emit(" for (const char *_p = " + rc + "; (_p = strstr(_p, " + sep + ")); _p += _pl) _last = _p;") emit(" if (_last) { " + tmp + "->_0 = sp_str_substr(" + rc + ", 0, _last - " + rc + "); " + tmp + "->_1 = " + sep + "; " + tmp + "->_2 = sp_str_substr(" + rc + ", _last - " + rc + " + _pl, _sl - (_last - " + rc + ") - _pl); }") name = tuple_c_name(tt) arg = compile_arg0(nid) tmp = new_temp emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);") emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");") emit(" " + tmp + "->_0 = " + rc + " / " + arg + ";") emit(" " + tmp + "->_1 = sp_imod(" + rc + ", " + arg + ");") return tmp tname = tuple_c_name(tt) arg = compile_arg0(nid) tmp = new_temp emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);") emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");") emit(" " + tmp + "->_0 = (mrb_int)floor(" + rc + " / " + arg + ");") emit(" " + tmp + "->_1 = " + rc + " - " + tmp + "->_0 * " + arg + ";") return tmp tt = "tuple:" + parts.join(",") register_tuple_type(tt) tname = tuple_c_name(tt) emit(" " + tname + " *" + pair_tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);") emit(" " + tname + " *" + pair_tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");") emit(" " + pair_tmp + "->_0 = sp_" + pfx_recv + "_get(" + rc + ", " + itmp + ");") k = 0 while k < arg_rcs.length end emit(" if (" + bexpr + ") sp_" + pfx + "_push(" + tmp_t + ", lv_" + bp1 + "); else sp_" + pfx + "_push(" + tmp_f + ", lv_" + bp1 + ");") emit(" }") emit(" " + name + " *" + tmp_res + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);") emit(" " + name + " *" + tmp_res + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");") emit(" " + tmp_res + "->_0 = " + tmp_t + ";") emit(" " + tmp_res + "->_1 = " + tmp_f + ";") return tmp_res emit(" if (_v > " + tmp_max + ") " + tmp_max + " = _v;") end emit(" }") emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);") emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");") emit(" " + tmp + "->_0 = " + tmp_min + ";") emit(" " + tmp + "->_1 = " + tmp_max + ";") return tmp itmp = new_temp emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();") emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {") emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);") emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");") emit(" _tp->_0 = " + rc + "->order[" + itmp + "];") emit(" _tp->_1 = sp_StrIntHash_get(" + rc + ", " + rc + "->order[" + itmp + "]);") emit(" sp_PtrArray_push(" + tmp + ", _tp);") itmp = new_temp emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();") emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {") emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);") emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");") emit(" _tp->_0 = " + rc + "->order[" + itmp + "];") emit(" _tp->_1 = sp_StrStrHash_get(" + rc + ", " + rc + "->order[" + itmp + "]);") emit(" sp_PtrArray_push(" + tmp + ", _tp);") if is_tuple_type(arr_type) == 1 name = tuple_c_name(arr_type) tmp = new_temp has_ptr = 0 parts = tuple_elem_types_str(arr_type).split(",") fi = 0 while fi < parts.length if type_is_pointer(parts[fi]) == 1 has_ptr = 1 end fi = fi + 1 end scan_fn = "NULL" if has_ptr == 1 scan_fn = name + "_gc_scan" end emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + scan_fn + ");") emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(arr_type) + ");") k = 0 while k < elems.length && k < parts.length emit(" " + tmp + "->_" + k.to_s + " = " + compile_expr(elems[k]) + ";")test/array_partition_gc.rbaddedView file
Loading diff…