Commit e495a314

e495a314e72fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

codegen: guard poly method dispatch on SP_TAG_OBJ before switching on cls_id

The poly method-dispatch switch keyed on the boxed value's cls_id, but a boxed scalar (int/float/...) carries cls_id 0, which aliases the first user class (class id 0). A poly value holding an int then mis-dispatched to that class's method -- e.g. @x.to_s on a poly-widened int whose owner class happens to be class 0 called the class's own to_s, recursing until the stack overflowed (SIGSEGV at startup).

Switch on tag == SP_TAG_OBJ ? cls_id : 0x7fffffff so a non-object tag maps to a value matching no case and falls to the default arm (sp_poly_to_s / 0). Builtin collections stay SP_TAG_OBJ and are unaffected. Surfaced under promote (ivars widen to poly), but the collision was a latent correctness bug independent of overflow mode.

Fixes promote-mode attr (SIGSEGV).

Default test 992/0/0 and optcarrot checksum 59662 unchanged.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
e495a314e72f
Parent
54b4c2adcfa0

src/codegen_call.cmodifiedView file

@@ -8293,7 +8293,12 @@
82938293 buf_printf(b, "if (_t%d.tag == SP_TAG_STR) _t%d = %ssp_str_length(_t%d.v.s) == 0%s; else ", tv, tr, ebopen, tv, ebclose);
82948294 buf_printf(b, "if (_t%d.tag == SP_TAG_SYM) _t%d = %sstrlen(sp_sym_to_s((sp_sym)_t%d.v.i)) == 0%s; else ", tv, tr, ebopen, tv, ebclose);
82958295 }
8296- buf_printf(b, "switch (_t%d.cls_id) {", tv);
8296+ /* Only an SP_TAG_OBJ value carries a real cls_id; a boxed scalar's cls_id
8297+ is 0, which would otherwise alias the first user class (class id 0) and
8298+ mis-dispatch (e.g. a poly int's `to_s` calling that class's to_s --
8299+ infinite recursion). Map non-object tags to a value matching no case so
8300+ they fall to the default (sp_poly_to_s / 0). */
8301+ buf_printf(b, "switch (_t%d.tag == SP_TAG_OBJ ? _t%d.cls_id : 0x7fffffff) {", tv, tv);
82978302 for (int k = 0; k < c->nclasses; k++) {
82988303 int defcls = -1;
82998304 int mi = comp_method_in_chain(c, k, name, &defcls);
@@ -8446,7 +8451,12 @@
84468451 else
84478452 buf_printf(b, "if (_t%d.tag == SP_TAG_INT) { _t%d = (_t%d.v.i >> %s) & 1; } else ", tv, tr, tv, idxref);
84488453 }
8449- buf_printf(b, "switch (_t%d.cls_id) {", tv);
8454+ /* Only an SP_TAG_OBJ value carries a real cls_id; a boxed scalar's cls_id
8455+ is 0, which would otherwise alias the first user class (class id 0) and
8456+ mis-dispatch (e.g. a poly int's `to_s` calling that class's to_s --
8457+ infinite recursion). Map non-object tags to a value matching no case so
8458+ they fall to the default (sp_poly_to_s / 0). */
8459+ buf_printf(b, "switch (_t%d.tag == SP_TAG_OBJ ? _t%d.cls_id : 0x7fffffff) {", tv, tv);
84508460 for (int k = 0; k < c->nclasses; k++) {
84518461 int defcls = -1;
84528462 int mi = comp_method_in_chain(c, k, name, &defcls);