Commit 842574c8
842574c8fd43Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: root GC-returning subexpressions as transient temps, close #4
Method-call receivers (and other GC-returning subexpressions) were emitted inline like sp_str_split(sp_str_strip(lv_line), " "); The result of the inner call is held only as a C-stack argument, not on sp_gc_roots[], so when the outer call triggers sp_gc_collect (e.g. sp_str_split's internal sp_StrArray_new crossing the heap threshold), the inner allocation is swept and the outer reads freed memory. On glibc this is usually masked because the freed bytes aren't immediately reused. On Darwin's malloc the reuse is aggressive and the bug surfaces as corrupted hash keys in io_wordcount.
The same structural UAF applies to every GC-allocated type, not just strings: chained calls producing IntArrays, StrArrays, class instances, etc. all share the pattern. The fix is therefore at codegen level, uniform across types.
Introduce compile_expr_gc_rooted(nid). If the expression is a
CallNode whose result type needs GC protection, bind the result to a
fresh temp and SP_GC_ROOT it before use. Route the receiver of every
call dispatch (the 58 rc = compile_expr(recv) sites) through this
helper instead of plain compile_expr.
lv_parts = sp_str_split(sp_str_strip(lv_line), " "); becomes const char *_t1 = sp_str_strip(lv_line); SP_GC_ROOT(_t1); lv_parts = sp_str_split(_t1, " ");
Locals, ivars, and literals are skipped: they're already rooted or immortal. Non-GC types are skipped: no rooting needed.
Bootstrap still reproduces (gen2.c == gen3.c). 74 tests / 55 benchmarks pass on Linux with no measurable compile-time regression. macOS verification pending on the PR thread; the bug the OP diagnosed should be fixed by construction for all GC types, not just strings.
Diagnosis and macOS symptom reported by Stoyan Zhekov (@zh) in #4. Credit for the root-cause analysis is entirely his.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 842574c8fd43
- Parent
- fba2456a451b
spinel_codegen.rbmodifiedView file
result end # True if `t` is a GC-allocated pointer that could be swept by # sp_gc_collect if held only as a C-stack temp when the collector # runs mid-expression. def type_needs_transient_root(t) if t == "" return 0 end if is_nullable_type(t) == 1 t = base_type(t) end if t == "string" || t == "mutable_str" return 1 end if t == "int_array" || t == "str_array" || t == "float_array" || t == "sym_array" return 1 end if t == "str_int_hash" || t == "str_str_hash" || t == "sym_int_hash" || t == "sym_str_hash" return 1 end if t == "str_poly_hash" || t == "sym_poly_hash" return 1 end if is_ptr_array_type(t) == 1 return 1 end if is_obj_type(t) == 1 return 1 end if is_tuple_type(t) == 1 return 1 end if t == "stringio" || t == "fiber" || t == "bigint" || t == "lambda" || t == "poly_array" return 1 end 0 end # Compile `nid`, and if it's a call expression whose result is a # GC-allocated pointer, bind that result to a rooted temp variable # so a subsequent mid-expression sp_gc_collect cannot sweep it. # For non-call expressions (locals, ivars, literals) rooting is # either already in place or unnecessary. def compile_expr_gc_rooted(nid) val = compile_expr(nid) if nid < 0 return val end if @nd_type[nid] != "CallNode" return val end t = infer_type(nid) if type_needs_transient_root(t) == 0 return val end @needs_gc = 1 tmp = new_temp emit(" " + c_type(t) + " " + tmp + " = " + val + ";") emit(" SP_GC_ROOT(" + tmp + ");") tmp end def compile_arg0(nid) args_id = @nd_arguments[nid] if args_id >= 0 if mname == "resume" && recv >= 0 rt2 = base_type(infer_type(recv)) if rt2 == "fiber" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) args_id = @nd_arguments[nid] if args_id >= 0 arg_ids = get_args(args_id) if mname == "alive?" && recv >= 0 rt2 = base_type(infer_type(recv)) if rt2 == "fiber" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) return "sp_Fiber_alive((sp_Fiber *)(" + rc + "))" end end if mname == "transfer" && recv >= 0 rt2 = base_type(infer_type(recv)) if rt2 == "fiber" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) args_id = @nd_arguments[nid] if args_id >= 0 arg_ids = get_args(args_id) end recv_type = infer_type(recv) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) # Root receiver if it may be collected during argument evaluation if expr_may_gc(recv) == 1 && type_is_pointer(recv_type) == 1 args_id = @nd_arguments[nid] end def compile_lambda_call_expr(nid, mname, recv) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) # Determine return type unboxing ret_type = "" if @nd_type[recv] == "LocalVariableReadNode" owner = find_method_owner(ci, mname) if owner != "" ca = compile_call_args(nid) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) if owner == cname if ca != "" return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ", " + ca + ")" cmp_owner = find_method_owner(ci, "<=>") if cmp_owner != "" ca = compile_call_args(nid) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) cmp_call = "sp_" + cmp_owner + "__cmp(" + rc + ", " + ca + ")" if mname == "<" return "(" + cmp_call + " < 0)" return "sp_poly_add(" + compile_expr(recv) + ", " + box_expr_to_poly(@nd_arguments[nid] >= 0 ? get_args(@nd_arguments[nid])[0] : -1) + ")" end if lt == "int_array" || lt == "str_array" || lt == "float_array" || lt == "sym_array" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) arg = compile_arg0(nid) pfx = array_c_prefix(lt) tmp = new_temp end if mname == "=~" # str =~ /pattern/ → sp_re_match(pat, str) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) re_args_id = @nd_arguments[nid] if re_args_id >= 0 argl = get_args(re_args_id) if args_id >= 0 aargs = get_args(args_id) if aargs.length >= 2 rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) lo = compile_expr(aargs[0]) hi = compile_expr(aargs[1]) lt = infer_type(recv) lt = infer_type(recv) if lt == "mutable_str" @needs_mutable_str = 1 rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) val = compile_arg0(nid) return "(sp_String_append(" + rc + ", " + val + "), " + rc + ")" end return "" end tmp = new_temp rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) emit(" mrb_int " + tmp + " = (mrb_int)strlen(" + rc + ");") @hoisted_strlen_recv = rc return tmp # (The pred_nid is the while predicate; body scan happens below via caller # passing @while_body_nid. Here we only check if type is hoistable.) tmp = new_temp rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) emit(" mrb_int " + tmp + " = " + length_c_expr(rt, rc) + ";") @hoisted_strlen_recv = rc tmp if args_id >= 0 aargs = get_args(args_id) if aargs.length >= 2 rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) val = compile_expr(aargs[1]) if rt == "sym_int_hash" emit(" sp_SymIntHash_set(" + rc + ", " + compile_expr(aargs[0]) + ", " + val + ");") if mname == "delete" if recv >= 0 rt = infer_type(recv) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) if rt == "sym_int_hash" emit(" sp_SymIntHash_delete(" + rc + ", " + compile_arg0(nid) + ");") return 1 rt = infer_type(recv) if rt == "mutable_str" @needs_mutable_str = 1 rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) arg_id = @nd_arguments[nid] if arg_id >= 0 argl = parse_id_list(@nd_args[arg_id]) end if rt == "string" @needs_string_helpers = 1 rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) val = compile_arg0(nid) # If receiver is a local variable, reassign if @nd_type[recv] == "LocalVariableReadNode" if recv >= 0 rt = infer_type(recv) if rt == "int_array" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) emit(" sp_IntArray_push(" + rc + ", " + compile_arg0(nid) + ");") return 1 end if rt == "str_array" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) emit(" sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + ");") return 1 end if rt == "float_array" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) emit(" sp_FloatArray_push(" + rc + ", " + compile_arg0(nid) + ");") return 1 end if is_ptr_array_type(rt) == 1 rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) emit(" sp_PtrArray_push(" + rc + ", " + compile_arg0(nid) + ");") return 1 end if recv >= 0 rt = infer_type(recv) if rt == "str_int_hash" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) emit(" sp_StrIntHash_update(" + rc + ", " + compile_arg0(nid) + ");") return 1 end if rt == "str_str_hash" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) emit(" sp_StrStrHash_update(" + rc + ", " + compile_arg0(nid) + ");") return 1 end if recv >= 0 rt = infer_type(recv) if rt == "int_array" || rt == "str_array" || rt == "float_array" || rt == "sym_array" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) arg = compile_arg0(nid) pfx = array_c_prefix(rt) tmp = new_temp end if rt == "mutable_str" @needs_mutable_str = 1 rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) args_id = @nd_arguments[nid] if args_id >= 0 aargs = get_args(args_id) if recv >= 0 rt = infer_type(recv) if rt == "mutable_str" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) val = compile_arg0(nid) emit(" " + rc + "->len = 0; " + rc + "->data[0] = 0; sp_String_append(" + rc + ", " + val + ");") return 1 rt = infer_type(recv) if rt == "mutable_str" @needs_mutable_str = 1 rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) val = compile_arg0(nid) emit(" sp_String_prepend(" + rc + ", " + val + ");") return 1 if recv >= 0 rt = infer_type(recv) if rt == "mutable_str" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) emit(" " + rc + "->len = 0; " + rc + "->data[0] = 0;") return 1 end if mname == "push" if recv >= 0 rt = infer_type(recv) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) if rt == "int_array" av = compile_arg0(nid) # If pushing a lambda value, cast to mrb_int if mname == "reverse!" if recv >= 0 rt = infer_type(recv) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) if rt == "int_array" emit(" sp_IntArray_reverse_bang(" + rc + ");") return 1 if mname == "sort!" if recv >= 0 rt = infer_type(recv) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) if rt == "sym_array" emit(" sp_sym_array_sort(" + rc + ");") return 1 if @nd_block[nid] >= 0 && recv >= 0 rt = infer_type(recv) if rt == "string" || rt == "mutable_str" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) bp = get_block_param(nid, 0) if bp == "" bp = "_c" if @nd_block[nid] >= 0 && recv >= 0 rt = infer_type(recv) if rt == "string" || rt == "mutable_str" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) bp = get_block_param(nid, 0) if bp == "" bp = "_b" if @nd_block[nid] >= 0 && recv >= 0 rt = infer_type(recv) if rt == "string" || rt == "mutable_str" rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) bp = get_block_param(nid, 0) if bp == "" bp = "_l" old = @in_loop @in_loop = 1 rt = infer_type(recv) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) arg = compile_arg0(nid) bp1 = get_block_param(nid, 0) bp2 = get_block_param(nid, 1) if @nd_block[nid] >= 0 && recv >= 0 old = @in_loop @in_loop = 1 rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) args_id = @nd_arguments[nid] limit_val = "0" step_val = "1" old = @in_loop @in_loop = 1 rt = infer_type(recv) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) n = compile_arg0(nid) bp1 = get_block_param(nid, 0) if bp1 == "" ridx = find_regexp_index(argl[0]) if ridx >= 0 @needs_str_array = 1 rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) bp = get_block_param(nid, 0) if bp == "" bp = "_m" bname = mname[0, mname.length - 1] rt = infer_type(recv) if is_obj_type(rt) == 1 rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) arrow2 = "->" if is_value_type_obj(rt) == 1 arrow2 = "." def compile_bracket_assign(nid) recv = @nd_receiver[nid] rt = infer_type(recv) rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) args_id = @nd_arguments[nid] arg_ids = [] if args_id >= 0 old = @in_loop @in_loop = 1 rt = infer_type(@nd_receiver[nid]) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) n = compile_arg0(nid) bp1 = get_block_param(nid, 0) if bp1 == "" old = @in_loop @in_loop = 1 rt = infer_type(@nd_receiver[nid]) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) n = compile_arg0(nid) bp1 = get_block_param(nid, 0) if bp1 == "" old = @in_loop @in_loop = 1 rt = infer_type(@nd_receiver[nid]) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) obj_arg = compile_arg0(nid) bp1 = get_block_param(nid, 0) bp2 = get_block_param(nid, 1) old = @in_loop @in_loop = 1 rt = infer_type(@nd_receiver[nid]) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) bp1 = get_block_param(nid, 0) bp2 = get_block_param(nid, 1) if bp1 == "" old = @in_loop @in_loop = 1 rt = infer_type(@nd_receiver[nid]) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) bp1 = get_block_param(nid, 0) bp2 = get_block_param(nid, 1) has_bp = 1 def compile_times_block(nid) old = @in_loop @in_loop = 1 rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) bp1 = get_block_param(nid, 0) tmp = new_temp emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < " + rc + "; " + tmp + "++) {") def compile_upto_block(nid) old = @in_loop @in_loop = 1 rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) lim = compile_arg0(nid) bp1 = get_block_param(nid, 0) tmp = new_temp def compile_downto_block(nid) old = @in_loop @in_loop = 1 rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) lim = compile_arg0(nid) bp1 = get_block_param(nid, 0) tmp = new_temp def compile_tap_expr(nid) # Execute block with receiver bound to block param, return receiver rt = infer_type(@nd_receiver[nid]) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) bp = get_block_param(nid, 0) if bp == "" bp = "_x" def compile_then_expr(nid) # Execute block with receiver bound to block param, return block result rt = infer_type(@nd_receiver[nid]) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) bp = get_block_param(nid, 0) if bp == "" bp = "_x" def compile_flat_map_expr(nid) # flat_map: for each element, block returns an array; concat all results rt = infer_type(@nd_receiver[nid]) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) bp1 = get_block_param(nid, 0) if bp1 == "" bp1 = "_x" def compile_select_expr(nid) rt = infer_type(@nd_receiver[nid]) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) bp1 = get_block_param(nid, 0) if bp1 == "" bp1 = "_x" end def compile_reduce_block(nid) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) init_val = compile_arg0(nid) bp1 = get_block_param(nid, 0) bp2 = get_block_param(nid, 1) end def compile_reject_expr(nid) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) bp1 = get_block_param(nid, 0) if bp1 == "" bp1 = "_x" end def compile_reject_block(nid) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) bp1 = get_block_param(nid, 0) if bp1 == "" bp1 = "_x" end recv = @nd_receiver[nid] rc = compile_expr(recv) rc = compile_expr_gc_rooted(recv) bodies = @cls_meth_bodies[cci].split(";") bid = -1 return end rt = infer_type(@nd_receiver[nid]) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) bp1 = get_block_param(nid, 0) if bp1 == "" bp1 = "_x" old = @in_loop @in_loop = 1 rt = infer_type(@nd_receiver[nid]) rc = compile_expr(@nd_receiver[nid]) rc = compile_expr_gc_rooted(@nd_receiver[nid]) bp1 = get_block_param(nid, 0) if bp1 == "" bp1 = "_x"