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

@@ -3083,6 +3083,34 @@
30833083 "sp_Tuple_" + tuple_elem_types_str(t).split(",").join("_")
30843084 end
30853085
3086+ # Whether a tuple element type must be traced by the GC scan function.
3087+ # Scalars (int/float/bool/symbol) are pure values; pointer-to-GC-object
3088+ # element types must be marked, otherwise the GC frees the inner object
3089+ # while the tuple keeps a dangling pointer.
3090+ def tuple_field_needs_mark(et)
3091+ if et == "poly"
3092+ return 1
3093+ end
3094+ if et == "int" || et == "float" || et == "bool" || et == "symbol" || et == "void" || et == "nil"
3095+ return 0
3096+ end
3097+ type_is_pointer(et)
3098+ end
3099+
3100+ # Returns the scan function name for the tuple, or "NULL" if no field
3101+ # requires marking.
3102+ def tuple_scan_name(t)
3103+ parts = tuple_elem_types_str(t).split(",")
3104+ fi = 0
3105+ while fi < parts.length
3106+ if tuple_field_needs_mark(parts[fi]) == 1
3107+ return tuple_c_name(t) + "_scan"
3108+ end
3109+ fi = fi + 1
3110+ end
3111+ "NULL"
3112+ end
3113+
30863114 def register_tuple_type(t)
30873115 if is_tuple_type(t) == 1
30883116 k = 0
@@ -8076,6 +8104,33 @@
80768104 @deferred_tuple << " } "
80778105 @deferred_tuple << name
80788106 @deferred_tuple << ";\n"
8107+ # GC scan function — only emit when at least one field is a GC ref.
8108+ # Without this the tuple's children are collected while the tuple
8109+ # itself is still alive, leaving dangling pointers in the fields.
8110+ needs_scan = 0
8111+ fi = 0
8112+ while fi < parts.length
8113+ if tuple_field_needs_mark(parts[fi]) == 1
8114+ needs_scan = 1
8115+ end
8116+ fi = fi + 1
8117+ end
8118+ if needs_scan == 1
8119+ body = ""
8120+ fi = 0
8121+ while fi < parts.length
8122+ if tuple_field_needs_mark(parts[fi]) == 1
8123+ field = "_t->_" + fi.to_s
8124+ if parts[fi] == "poly"
8125+ body = body + " sp_mark_rbval(" + field + ");"
8126+ else
8127+ body = body + " sp_gc_mark((void *)" + field + ");"
8128+ end
8129+ end
8130+ fi = fi + 1
8131+ end
8132+ @deferred_tuple << "static void " + name + "_scan(void *_p) { " + name + " *_t = (" + name + " *)_p;" + body + " }\n"
8133+ end
80798134 k = k + 1
80808135 end
80818136 end
@@ -14302,7 +14357,7 @@
1430214357 tname = tuple_c_name(tt)
1430314358 sep = compile_arg0(nid)
1430414359 tmp = new_temp
14305- emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")
14360+ emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")
1430614361 emit(" { const char *_p = strstr(" + rc + ", " + sep + ");")
1430714362 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 + ")); }")
1430814363 emit(" else { " + tmp + "->_0 = " + rc + "; " + tmp + "->_1 = \"\"; " + tmp + "->_2 = \"\"; } }")
@@ -14315,7 +14370,7 @@
1431514370 tname = tuple_c_name(tt)
1431614371 sep = compile_arg0(nid)
1431714372 tmp = new_temp
14318- emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")
14373+ emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")
1431914374 emit(" { size_t _sl = strlen(" + rc + "), _pl = strlen(" + sep + "); const char *_last = NULL;")
1432014375 emit(" for (const char *_p = " + rc + "; (_p = strstr(_p, " + sep + ")); _p += _pl) _last = _p;")
1432114376 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); }")
@@ -14798,7 +14853,7 @@
1479814853 name = tuple_c_name(tt)
1479914854 arg = compile_arg0(nid)
1480014855 tmp = new_temp
14801- emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);")
14856+ emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");")
1480214857 emit(" " + tmp + "->_0 = " + rc + " / " + arg + ";")
1480314858 emit(" " + tmp + "->_1 = sp_imod(" + rc + ", " + arg + ");")
1480414859 return tmp
@@ -14898,7 +14953,7 @@
1489814953 tname = tuple_c_name(tt)
1489914954 arg = compile_arg0(nid)
1490014955 tmp = new_temp
14901- emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")
14956+ emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")
1490214957 emit(" " + tmp + "->_0 = (mrb_int)floor(" + rc + " / " + arg + ");")
1490314958 emit(" " + tmp + "->_1 = " + rc + " - " + tmp + "->_0 * " + arg + ";")
1490414959 return tmp
@@ -14970,7 +15025,7 @@
1497015025 tt = "tuple:" + parts.join(",")
1497115026 register_tuple_type(tt)
1497215027 tname = tuple_c_name(tt)
14973- emit(" " + tname + " *" + pair_tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")
15028+ emit(" " + tname + " *" + pair_tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")
1497415029 emit(" " + pair_tmp + "->_0 = sp_" + pfx_recv + "_get(" + rc + ", " + itmp + ");")
1497515030 k = 0
1497615031 while k < arg_rcs.length
@@ -15189,7 +15244,7 @@
1518915244 end
1519015245 emit(" if (" + bexpr + ") sp_" + pfx + "_push(" + tmp_t + ", lv_" + bp1 + "); else sp_" + pfx + "_push(" + tmp_f + ", lv_" + bp1 + ");")
1519115246 emit(" }")
15192- emit(" " + name + " *" + tmp_res + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);")
15247+ emit(" " + name + " *" + tmp_res + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");")
1519315248 emit(" " + tmp_res + "->_0 = " + tmp_t + ";")
1519415249 emit(" " + tmp_res + "->_1 = " + tmp_f + ";")
1519515250 return tmp_res
@@ -15217,7 +15272,7 @@
1521715272 emit(" if (_v > " + tmp_max + ") " + tmp_max + " = _v;")
1521815273 end
1521915274 emit(" }")
15220- emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);")
15275+ emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");")
1522115276 emit(" " + tmp + "->_0 = " + tmp_min + ";")
1522215277 emit(" " + tmp + "->_1 = " + tmp_max + ";")
1522315278 return tmp
@@ -15784,7 +15839,7 @@
1578415839 itmp = new_temp
1578515840 emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")
1578615841 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")
15787- emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")
15842+ emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")
1578815843 emit(" _tp->_0 = " + rc + "->order[" + itmp + "];")
1578915844 emit(" _tp->_1 = sp_StrIntHash_get(" + rc + ", " + rc + "->order[" + itmp + "]);")
1579015845 emit(" sp_PtrArray_push(" + tmp + ", _tp);")
@@ -15890,7 +15945,7 @@
1589015945 itmp = new_temp
1589115946 emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")
1589215947 emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + rc + "->len; " + itmp + "++) {")
15893- emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")
15948+ emit(" " + tname + " *_tp = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")
1589415949 emit(" _tp->_0 = " + rc + "->order[" + itmp + "];")
1589515950 emit(" _tp->_1 = sp_StrStrHash_get(" + rc + ", " + rc + "->order[" + itmp + "]);")
1589615951 emit(" sp_PtrArray_push(" + tmp + ", _tp);")
@@ -17207,20 +17262,8 @@
1720717262 if is_tuple_type(arr_type) == 1
1720817263 name = tuple_c_name(arr_type)
1720917264 tmp = new_temp
17210- has_ptr = 0
1721117265 parts = tuple_elem_types_str(arr_type).split(",")
17212- fi = 0
17213- while fi < parts.length
17214- if type_is_pointer(parts[fi]) == 1
17215- has_ptr = 1
17216- end
17217- fi = fi + 1
17218- end
17219- scan_fn = "NULL"
17220- if has_ptr == 1
17221- scan_fn = name + "_gc_scan"
17222- end
17223- emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + scan_fn + ");")
17266+ emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(arr_type) + ");")
1722417267 k = 0
1722517268 while k < elems.length && k < parts.length
1722617269 emit(" " + tmp + "->_" + k.to_s + " = " + compile_expr(elems[k]) + ";")