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
#define SP_TAG_NIL 4#define SP_TAG_OBJ 5#define SP_TAG_SYM 6typedef struct { int tag; union { mrb_int i; const char *s; mrb_float f; mrb_bool b; void *p; int cls_id; } v; } sp_RbVal;static sp_RbVal sp_box_int(mrb_int v) { sp_RbVal r; r.tag = SP_TAG_INT; r.v.i = v; return r; }static sp_RbVal sp_box_str(const char *v) { sp_RbVal r; r.tag = SP_TAG_STR; r.v.s = v; return r; }static sp_RbVal sp_box_float(mrb_float v) { sp_RbVal r; r.tag = SP_TAG_FLT; r.v.f = v; return r; }static sp_RbVal sp_box_bool(mrb_bool v) { sp_RbVal r; r.tag = SP_TAG_BOOL; r.v.b = v; return r; }static sp_RbVal sp_box_nil(void) { sp_RbVal r; r.tag = SP_TAG_NIL; r.v.i = 0; return r; }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; }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; }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;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; }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; }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; }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; }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; }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; }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; }static void sp_poly_puts(sp_RbVal v) { switch (v.tag) { case SP_TAG_INT: printf("%lld\n", (long long)v.v.i); break;spinel_codegen.rbmodifiedView file
Loading diff…
test/bm_poly_self_deref.rbaddedView file
Loading diff…