Commit 024156fd

024156fdcf70Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 14 files · +6 added · ~8 modified

Description

Give an ivar-bearing Exception subclass its own struct, carried across raise

A user exception subclass with an instance variable was typedef'd straight to the base sp_Exception struct, so emitted o->iv_<name> / self->iv_<name> accesses referenced a nonexistent member and the generated C failed to compile (#1415). It was also mis-detected as a value type (returned by value with a cls_id the base struct lacks), compounding the breakage.

Fixes, end to end:

  • Value-type detection now excludes any class with a superclass node, not just a user superclass -- a builtin parent (StandardError) left parent == -1 and slipped through. An exception subclass is never a standalone leaf value type.

  • An exception subclass that carries ivars gets a dedicated struct whose leading members mirror sp_Exception (cls_name / parent_cls_name / msg, a common initial sequence) so every (sp_Exception *) cast in the raise/message machinery stays valid, with the ivar fields after. Its constructor allocates that struct and its GC scan marks msg plus heap ivars. An ivar-less subclass still aliases the base struct as before.

  • The raised object is carried alongside (cls, msg) through the longjmp via a parallel sp_exc_obj stack (NULL for a string/builtin raise), so the subclass keeps its ivars across raise -> rescue. The carried object is GC-marked while in flight.

  • A rescue <UserExc> => e arm that names exactly one ivar-bearing exception subclass types e as that object so e.<ivar> reads resolve; the binding restores the carried object. A name reused across arms with divergent classes stays TY_EXCEPTION (no slot collapse). e.message and friends still route through the base exception helpers.

  • raise Cls, arg on such a subclass now constructs Cls.new(arg) (so the ivar is set and the message comes from initialize/super) and seeds Cls#initialize's param type from arg, keeping the constructor reachable.

Fixes #1415

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

Details

Commit
024156fdcf70
Parent
a135ad2f5989

lib/sp_runtime.hmodifiedView file

@@ -4442,6 +4442,13 @@
44424442 static volatile int sp_exc_top = 0;
44434443 static const char *sp_exc_cls[SP_EXC_STACK_MAX];
44444444 static volatile const char *sp_last_exc_cls = sp_str_empty;
4445+/* The raised exception OBJECT, carried alongside (cls,msg) so a user
4446+ exception subclass keeps its ivars across raise -> rescue (#1415).
4447+ NULL for a bare string/builtin raise, which reconstructs on catch.
4448+ sp_pending_exc_obj is set by sp_raise_exc just before the longjmp and
4449+ consumed into the per-frame slot by sp_raise_cls. */
4450+static void *sp_exc_obj[SP_EXC_STACK_MAX];
4451+static void *sp_pending_exc_obj = NULL;
44454452 /* ---- Native backtrace formatting (spinel --debug) ---------------------- */
44464453 /* True for sp_<name> symbols that are runtime helpers, not user Ruby methods.
44474454 A denylist of the lowercase runtime prefixes; user methods are sp_<rubyname>
@@ -4579,7 +4586,7 @@
45794586 #if SP_BT_AVAILABLE
45804587 if (sp_bt_enabled) sp_bt_n = backtrace(sp_bt_buf, 256);
45814588 #endif
4582- if (sp_exc_top > 0) { sp_exc_msg[sp_exc_top-1] = msg; sp_exc_cls[sp_exc_top-1] = cls; sp_last_exc_cls = cls; longjmp(sp_exc_stack[sp_exc_top-1], 1); } fprintf(stderr, "unhandled exception: %s\n", msg); exit(1); }
4589+ if (sp_exc_top > 0) { sp_exc_msg[sp_exc_top-1] = msg; sp_exc_cls[sp_exc_top-1] = cls; sp_exc_obj[sp_exc_top-1] = sp_pending_exc_obj; sp_pending_exc_obj = NULL; sp_last_exc_cls = cls; longjmp(sp_exc_stack[sp_exc_top-1], 1); } fprintf(stderr, "unhandled exception: %s\n", msg); exit(1); }
45834590 static void sp_raise(const char *msg) { sp_raise_cls("RuntimeError", msg); }
45844591
45854592 /* Issue #781: bridge between the regex compile-error path (which lives
@@ -4625,7 +4632,13 @@
46254632 longjmp(sp_re_startup_jmp, 1);
46264633 }
46274634 extern void sp_re_set_error_handler(void (*fn)(const char *msg));
4628-static void sp_mark_in_flight_exceptions(void) { for (int i = 0; i < sp_exc_top; i++) sp_mark_string(sp_exc_msg[i]); }
4635+static void sp_mark_in_flight_exceptions(void) {
4636+ for (int i = 0; i < sp_exc_top; i++) {
4637+ sp_mark_string(sp_exc_msg[i]);
4638+ if (sp_exc_obj[i]) sp_gc_mark(sp_exc_obj[i]); /* carried subclass object + its ivars */
4639+ }
4640+ if (sp_pending_exc_obj) sp_gc_mark(sp_pending_exc_obj);
4641+}
46294642
46304643 /* sp_Exception: first-class exception object. cls_name is a pointer
46314644 to the per-class const string literal emitted by codegen
@@ -4656,6 +4669,19 @@
46564669 if (!msg || !msg[0]) e->msg = cls_name ? cls_name : "RuntimeError";
46574670 return e;
46584671 }
4672+/* Allocate a zeroed exception-subclass struct of `sz` bytes with the base
4673+ {cls_name, parent_cls_name, msg} prefix set, for the degenerate catch path
4674+ where a user subclass with ivars was raised without a carried object
4675+ (#1415). Its ivar fields stay zero (nil/0). msg is the only heap field, so
4676+ the base scan suffices. */
4677+static void *sp_exc_new_sub_sized(size_t sz, const char *cls_name, const char *msg) {
4678+ sp_Exception *e = (sp_Exception *)sp_gc_alloc(sz, NULL, sp_exc_gc_scan);
4679+ memset(e, 0, sz);
4680+ e->cls_name = cls_name ? cls_name : "RuntimeError";
4681+ e->msg = (msg && msg[0]) ? msg : e->cls_name;
4682+ if (sp_user_exc_parent_fn) e->parent_cls_name = sp_user_exc_parent_fn(e->cls_name);
4683+ return e;
4684+}
46594685 /* Accept `volatile` pointers: LV slots holding sp_Exception * are
46604686 declared volatile when they live across setjmp, so callers may
46614687 pass volatile-qualified pointers in. The pointee itself isn't
@@ -4672,6 +4698,9 @@
46724698 static void sp_raise_exc(volatile sp_Exception *ve) {
46734699 sp_Exception *e = (sp_Exception *)ve;
46744700 if (!e) sp_raise("(nil exception)");
4701+ /* Carry the object so a user subclass keeps its ivars across the
4702+ longjmp; sp_raise_cls moves it into the current frame's slot. */
4703+ sp_pending_exc_obj = (void *)e;
46754704 sp_raise_cls(e->cls_name, e->msg);
46764705 }
46774706 /* Create an exception for a `rescue => e` binding: like sp_exc_new but