Commit b126003a

b126003a2465Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): poly_array slot consumes sized-empty default via PolyArray

@arr = [nil] * N going into a poly_array slot lowers the rhs via the default * codegen, which produces an sp_IntArray. The slot is sp_PolyArray * (widened by writer-scan via heterogeneous @arr[i] = v writes elsewhere in the class), so the resulting C contains a pointer-type mismatch:

sp_IntArray _t1 = sp_IntArray_new(); ... self->iv_arr = _t1; / iv_arr is sp_PolyArray * */

The default -Wno-all build silently coerces; subsequent sp_PolyArray_set(@arr, ...) calls then write 16-byte sp_RbVal entries into 8-byte IntArray slots, corrupting adjacent memory and skewing @arr.length.

The same guard already exists for ptr_array slots — extend it to poly_array in both compile_stmt's general InstanceVariableWriteNode path and emit_constructor's IVW path (parallel to the recent ArrayNode-literal fix). Build a sized PolyArray pre-filled with sp_box_nil() so the slot holds the matching storage. compile_expr_for_expected_type already had the poly_array branch; this brings the IVW paths in line.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
b126003a2465
Parent
84d241a676e0

spinel_codegen.rbmodifiedView file

@@ -15046,16 +15046,23 @@
1504615046 if iv_ctor != ""
1504715047 @needs_gc = 1
1504815048 emit_raw(" " + self_arrow + ivar + " = " + iv_ctor + ";")
15049- elsif is_ptr_array_type(ivt) == 1 && is_sized_empty_array_default(expr_id_iv) == 1
15049+ elsif (ivt == "poly_array" || is_ptr_array_type(ivt) == 1) && is_sized_empty_array_default(expr_id_iv) == 1
1505015050 # `@arr = [nil] * N` with @arr already widened to
15051- # ptr_array via writer-scan. The default `*` codegen
15052- # produces an sp_IntArray that mismatches the slot
15053- # type; emit a sized PtrArray of NULLs inline.
15051+ # poly_array (or ptr_array) via writer-scan. The
15052+ # default `*` codegen produces an sp_IntArray that
15053+ # mismatches the slot type; emit a sized PolyArray
15054+ # of sp_box_nil() (or PtrArray of NULLs) inline.
1505415055 @needs_gc = 1
1505515056 cnt_e_iv = compile_arg0(expr_id_iv)
1505615057 tmp_iv = new_temp
15057- emit_raw(" sp_PtrArray *" + tmp_iv + " = sp_PtrArray_new();")
15058- emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_iv + ", NULL); }")
15058+ if ivt == "poly_array"
15059+ @needs_rb_value = 1
15060+ emit_raw(" sp_PolyArray *" + tmp_iv + " = sp_PolyArray_new();")
15061+ emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp_iv + ", sp_box_nil()); }")
15062+ else
15063+ emit_raw(" sp_PtrArray *" + tmp_iv + " = sp_PtrArray_new();")
15064+ emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_iv + ", NULL); }")
15065+ end
1505915066 emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";")
1506015067 else
1506115068 # Issue #130: same poly-slot boxing as the general
@@ -24717,17 +24724,24 @@
2471724724 return
2471824725 end
2471924726 end
24720- # `@arr = [nil] * N` where @arr is a ptr_array slot. The
24721- # default `*` codegen produces an sp_IntArray, which mismatches
24722- # the slot type and corrupts later sp_PtrArray_set calls. Emit
24723- # a sized PtrArray pre-filled with NULLs inline so the slot
24727+ # `@arr = [nil] * N` where @arr is a poly_array (or ptr_array)
24728+ # slot. The default `*` codegen produces an sp_IntArray, which
24729+ # mismatches the slot type and corrupts later
24730+ # `sp_PolyArray_set(@arr, ...)` calls. Emit a sized PolyArray
24731+ # of sp_box_nil() (or PtrArray of NULLs) inline so the slot
2472424732 # holds the matching storage.
24725- if is_ptr_array_type(ivt) == 1 && is_sized_empty_array_default(expr_id) == 1
24733+ if (ivt == "poly_array" || is_ptr_array_type(ivt) == 1) && is_sized_empty_array_default(expr_id) == 1
2472624734 @needs_gc = 1
2472724735 cnt_e = compile_arg0(expr_id)
2472824736 tmp_arr = new_temp
24729- emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
24730- emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_arr + ", NULL); }")
24737+ if ivt == "poly_array"
24738+ @needs_rb_value = 1
24739+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
24740+ emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp_arr + ", sp_box_nil()); }")
24741+ else
24742+ emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
24743+ emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_arr + ", NULL); }")
24744+ end
2473124745 emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")
2473224746 return
2473324747 end