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
emit_raw(" sp_PtrArray *" + tmp_iv + " = sp_PtrArray_new();") emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_iv + ", NULL); }") emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";") elsif ivt == "poly_array" && @nd_type[expr_id_iv] == "ArrayNode" # Non-empty array literal `[a, b, ...]` going into a # poly_array slot. compile_array_literal infers a typed # storage (ptr_array of one class for homogeneous obj # literals, etc.) — emit a fresh PolyArray with each # element boxed instead. @needs_gc = 1 @needs_rb_value = 1 tmp_iv = new_temp emit_raw(" sp_PolyArray *" + tmp_iv + " = sp_PolyArray_new();") elems_iv = parse_id_list(@nd_elements[expr_id_iv]) ek = 0 while ek < elems_iv.length eid = elems_iv[ek] et = infer_type(eid) ev = compile_expr(eid) ebox = et == "poly" ? ev : box_value_to_poly(et, ev) emit_raw(" sp_PolyArray_push(" + tmp_iv + ", " + ebox + ");") ek = ek + 1 end emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";") else # Issue #130: same poly-slot boxing as the general # InstanceVariableWriteNode emit path (compile_expr branch). emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";") return end # Non-empty array literal `[a, b, ...]` going into a poly_array # slot. compile_array_literal infers a typed array (ptr_array of # one class for homogeneous obj literals, etc.) and emits the # corresponding storage — but the slot was widened to poly_array, # so a typed-array-pointer assignment would fail the C compile. # Emit a fresh PolyArray with each element boxed to sp_RbVal. if ivt == "poly_array" && @nd_type[expr_id] == "ArrayNode" @needs_gc = 1 @needs_rb_value = 1 tmp_arr = new_temp emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();") elems_pa = parse_id_list(@nd_elements[expr_id]) ek = 0 while ek < elems_pa.length eid = elems_pa[ek] et = infer_type(eid) ev = compile_expr(eid) ebox = et == "poly" ? ev : box_value_to_poly(et, ev) emit(" sp_PolyArray_push(" + tmp_arr + ", " + ebox + ");") ek = ek + 1 end emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";") return end # Issue #130: poly slot — every concrete-typed RHS must be boxed # to sp_RbVal. Without the box, C compiler sees `sp_RbVal = mrb_int` # (or const char *, etc.) and either rejects the assignment ortest/poly_array_slot_literal.rbaddedView file
Loading diff…