Commit 134c93b5

134c93b56dd4Merged

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

Description

fix(runtime): move cls_id out of sp_RbVal union to avoid pointer corruption

sp_RbVal had cls_id and void *p sharing storage in the same union. sp_box_obj wrote both fields:

r.v.p = p; r.v.cls_id = cls_id;

so on 64-bit platforms the second store clobbered the low 32 bits of the pointer. Any poly-dispatched method that dereferenced self crashed with SIGSEGV. The bug was masked because existing tests only poly-dispatched methods that ignored their receiver.

This program crashes on master but should print 42 and other:

class C attr_accessor :n def initialize(n); @n = n; end def name; @n.to_s; end end class D def name; "other"; end end def show(o); o.name; end puts show(C.new(42)) puts show(D.new)

The fix lifts cls_id out of the union so it has its own storage:

typedef struct {
  int tag;
  int cls_id;
  union { mrb_int i; const char *s; mrb_float f; mrb_bool b; void *p; } v;
} sp_RbVal;

All sp_box_* helpers now write r.cls_id separately, and the poly dispatch path reads recv.cls_id (not recv.v.cls_id).

Adds test/bm_poly_self_deref.rb as a small reproducer.

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

Details

Commit
134c93b56dd4
Parent
adb76fd253fd

lib/sp_runtime.hmodifiedView file

@@ -528,14 +528,14 @@
528528 #define SP_TAG_NIL 4
529529 #define SP_TAG_OBJ 5
530530 #define SP_TAG_SYM 6
531-typedef struct { int tag; union { mrb_int i; const char *s; mrb_float f; mrb_bool b; void *p; int cls_id; } v; } sp_RbVal;
532-static sp_RbVal sp_box_int(mrb_int v) { sp_RbVal r; r.tag = SP_TAG_INT; r.v.i = v; return r; }
533-static sp_RbVal sp_box_str(const char *v) { sp_RbVal r; r.tag = SP_TAG_STR; r.v.s = v; return r; }
534-static sp_RbVal sp_box_float(mrb_float v) { sp_RbVal r; r.tag = SP_TAG_FLT; r.v.f = v; return r; }
535-static sp_RbVal sp_box_bool(mrb_bool v) { sp_RbVal r; r.tag = SP_TAG_BOOL; r.v.b = v; return r; }
536-static sp_RbVal sp_box_nil(void) { sp_RbVal r; r.tag = SP_TAG_NIL; r.v.i = 0; return r; }
537-static sp_RbVal sp_box_obj(void *p, int cls_id) { sp_RbVal r; r.tag = SP_TAG_OBJ; r.v.p = p; r.v.cls_id = cls_id; return r; }
538-static sp_RbVal sp_box_sym(sp_sym v) { sp_RbVal r; r.tag = SP_TAG_SYM; r.v.i = (mrb_int)v; return r; }
531+typedef struct { int tag; int cls_id; union { mrb_int i; const char *s; mrb_float f; mrb_bool b; void *p; } v; } sp_RbVal;
532+static sp_RbVal sp_box_int(mrb_int v) { sp_RbVal r; r.tag = SP_TAG_INT; r.cls_id = 0; r.v.i = v; return r; }
533+static sp_RbVal sp_box_str(const char *v) { sp_RbVal r; r.tag = SP_TAG_STR; r.cls_id = 0; r.v.s = v; return r; }
534+static sp_RbVal sp_box_float(mrb_float v) { sp_RbVal r; r.tag = SP_TAG_FLT; r.cls_id = 0; r.v.f = v; return r; }
535+static sp_RbVal sp_box_bool(mrb_bool v) { sp_RbVal r; r.tag = SP_TAG_BOOL; r.cls_id = 0; r.v.b = v; return r; }
536+static sp_RbVal sp_box_nil(void) { sp_RbVal r; r.tag = SP_TAG_NIL; r.cls_id = 0; r.v.i = 0; return r; }
537+static sp_RbVal sp_box_obj(void *p, int cls_id) { sp_RbVal r; r.tag = SP_TAG_OBJ; r.cls_id = cls_id; r.v.p = p; return r; }
538+static sp_RbVal sp_box_sym(sp_sym v) { sp_RbVal r; r.tag = SP_TAG_SYM; r.cls_id = 0; r.v.i = (mrb_int)v; return r; }
539539 static void sp_poly_puts(sp_RbVal v) {
540540 switch (v.tag) {
541541 case SP_TAG_INT: printf("%lld\n", (long long)v.v.i); break;