Commit 94c29ab5

94c29ab5b72fMerged

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

Description

fix(codegen): poly_array slot consumes ArrayNode literal via PolyArray

@arr = [a, b, ...] going into a poly_array slot used to compile the rhs via compile_array_literal, which infers a typed storage (ptr_array of one class for homogeneous obj literals, etc.) and emits the matching sp_PtrArray *. The slot is sp_PolyArray * (widened by writer-scan via heterogeneous @arr[i] = v writes), so the resulting C contains

sp_PtrArray *_t1 = sp_PtrArray_new();
sp_PtrArray_push(_t1, sp_<X>_new());
...
self->iv_arr = _t1;          /* iv_arr is sp_PolyArray * */

— a pointer-type mismatch the C compiler rejects with strict flags ("assignment to 'sp_PolyArray *' from incompatible pointer type 'sp_PtrArray *'"). Under the default -Wno-all the cast is silently coerced and the runtime is corrupted: subsequent sp_PolyArray_set writes 16-byte sp_RbVal entries into 8-byte sp_PtrArray slots, smashing adjacent memory.

Detect the IVW + ArrayNode RHS shape with poly_array slot in both the compile_stmt and emit_constructor paths. Emit a fresh sp_PolyArray populated with box_value_to_poly-wrapped elements instead — same shape compile_array_literal would emit for a heterogeneous literal, just driven by the slot type rather than the inferred element type.

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

Details

Commit
94c29ab5b72f
Parent
84d241a676e0

spinel_codegen.rbmodifiedView file

@@ -15057,6 +15057,27 @@
1505715057 emit_raw(" sp_PtrArray *" + tmp_iv + " = sp_PtrArray_new();")
1505815058 emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_iv + ", NULL); }")
1505915059 emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";")
15060+ elsif ivt == "poly_array" && @nd_type[expr_id_iv] == "ArrayNode"
15061+ # Non-empty array literal `[a, b, ...]` going into a
15062+ # poly_array slot. compile_array_literal infers a typed
15063+ # storage (ptr_array of one class for homogeneous obj
15064+ # literals, etc.) — emit a fresh PolyArray with each
15065+ # element boxed instead.
15066+ @needs_gc = 1
15067+ @needs_rb_value = 1
15068+ tmp_iv = new_temp
15069+ emit_raw(" sp_PolyArray *" + tmp_iv + " = sp_PolyArray_new();")
15070+ elems_iv = parse_id_list(@nd_elements[expr_id_iv])
15071+ ek = 0
15072+ while ek < elems_iv.length
15073+ eid = elems_iv[ek]
15074+ et = infer_type(eid)
15075+ ev = compile_expr(eid)
15076+ ebox = et == "poly" ? ev : box_value_to_poly(et, ev)
15077+ emit_raw(" sp_PolyArray_push(" + tmp_iv + ", " + ebox + ");")
15078+ ek = ek + 1
15079+ end
15080+ emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";")
1506015081 else
1506115082 # Issue #130: same poly-slot boxing as the general
1506215083 # InstanceVariableWriteNode emit path (compile_expr branch).
@@ -24731,6 +24752,30 @@
2473124752 emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")
2473224753 return
2473324754 end
24755+ # Non-empty array literal `[a, b, ...]` going into a poly_array
24756+ # slot. compile_array_literal infers a typed array (ptr_array of
24757+ # one class for homogeneous obj literals, etc.) and emits the
24758+ # corresponding storage — but the slot was widened to poly_array,
24759+ # so a typed-array-pointer assignment would fail the C compile.
24760+ # Emit a fresh PolyArray with each element boxed to sp_RbVal.
24761+ if ivt == "poly_array" && @nd_type[expr_id] == "ArrayNode"
24762+ @needs_gc = 1
24763+ @needs_rb_value = 1
24764+ tmp_arr = new_temp
24765+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
24766+ elems_pa = parse_id_list(@nd_elements[expr_id])
24767+ ek = 0
24768+ while ek < elems_pa.length
24769+ eid = elems_pa[ek]
24770+ et = infer_type(eid)
24771+ ev = compile_expr(eid)
24772+ ebox = et == "poly" ? ev : box_value_to_poly(et, ev)
24773+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + ebox + ");")
24774+ ek = ek + 1
24775+ end
24776+ emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")
24777+ return
24778+ end
2473424779 # Issue #130: poly slot — every concrete-typed RHS must be boxed
2473524780 # to sp_RbVal. Without the box, C compiler sees `sp_RbVal = mrb_int`
2473624781 # (or const char *, etc.) and either rejects the assignment or