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
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); 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); } buf_printf(b, "switch (_t%d.cls_id) {", tv); /* Only an SP_TAG_OBJ value carries a real cls_id; a boxed scalar's cls_id is 0, which would otherwise alias the first user class (class id 0) and mis-dispatch (e.g. a poly int's `to_s` calling that class's to_s -- infinite recursion). Map non-object tags to a value matching no case so they fall to the default (sp_poly_to_s / 0). */ buf_printf(b, "switch (_t%d.tag == SP_TAG_OBJ ? _t%d.cls_id : 0x7fffffff) {", tv, tv); for (int k = 0; k < c->nclasses; k++) { int defcls = -1; int mi = comp_method_in_chain(c, k, name, &defcls); else buf_printf(b, "if (_t%d.tag == SP_TAG_INT) { _t%d = (_t%d.v.i >> %s) & 1; } else ", tv, tr, tv, idxref); } buf_printf(b, "switch (_t%d.cls_id) {", tv); /* Only an SP_TAG_OBJ value carries a real cls_id; a boxed scalar's cls_id is 0, which would otherwise alias the first user class (class id 0) and mis-dispatch (e.g. a poly int's `to_s` calling that class's to_s -- infinite recursion). Map non-object tags to a value matching no case so they fall to the default (sp_poly_to_s / 0). */ buf_printf(b, "switch (_t%d.tag == SP_TAG_OBJ ? _t%d.cls_id : 0x7fffffff) {", tv, tv); for (int k = 0; k < c->nclasses; k++) { int defcls = -1; int mi = comp_method_in_chain(c, k, name, &defcls);