Commit ad239b34

ad239b34b987Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

codegen: capture self into an escaping block's proc cap struct

A block that escapes inlining -- stored in an ivar or otherwise forwarded, so it lowers to a real _proc_N(void *_cap, mrb_int, mrb_int *) rather than being inlined -- and that references self (an instance-method call, an ivar, super, or self itself) emitted self in the proc body with no parameter or capture for it: _cap carried only the celled free locals, so (sp_Foo *)self was an undeclared identifier (#1436).

Detect a self-referencing body (proc_body_uses_self) and, for a pointer (heap-object) instance self, capture it through the cap struct: add a void *__self field (marked by the cap scan so the receiver survives as long as the proc), populate it from the enclosing self at the proc's creation site, and read it back as a typed self local at the top of the proc body. The detection over-approximates (it recurses into nested blocks, whose self is forwarded from this proc); a trailing (void)self; keeps an unused readback harmless. A class-method or by-value self is a different shape and is left as-is.

Verified through a side effect rather than the block's return value: a stored scalar-returning block read back via @blk.call has a separate, pre-existing return-ABI mismatch (the proc returns the int directly while the poly call site reads _sp_proc_poly_ret), independent of self and of this fix.

Fixes #1436

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

Details

Commit
ad239b34b987
Parent
1eb79fd83e74

src/codegen.cmodifiedView file

@@ -814,6 +814,33 @@
814814 free(caps.v);
815815 }
816816
817+/* Does a proc body reference `self` -- explicitly, via an ivar, via `super`, or
818+ via a receiverless call that dispatches on an instance method of class_id?
819+ Such a block, when it escapes inlining into a real _proc_N(void*, ...), emits
820+ `self` with no parameter or capture for it (#1436). Recurses into nested
821+ blocks too: a nested block's self is forwarded from this proc's, so this proc
822+ must capture it. Over-approximation is harmless -- the readback is followed by
823+ `(void)self;`. */
824+static int proc_body_uses_self(Compiler *c, int id, int class_id) {
825+ if (id < 0) return 0;
826+ const char *ty = nt_type(c->nt, id);
827+ if (!ty) return 0;
828+ if (!strcmp(ty, "SelfNode")) return 1;
829+ if (!strncmp(ty, "InstanceVariable", 16)) return 1;
830+ if (!strcmp(ty, "SuperNode") || !strcmp(ty, "ForwardingSuperNode")) return 1;
831+ if (!strcmp(ty, "CallNode") && nt_ref(c->nt, id, "receiver") < 0) {
832+ const char *nm = nt_str(c->nt, id, "name");
833+ if (nm && comp_method_in_chain(c, class_id, nm, NULL) >= 0) return 1;
834+ }
835+ int nr = nt_num_refs(c->nt, id);
836+ for (int i = 0; i < nr; i++)
837+ if (proc_body_uses_self(c, nt_ref_at(c->nt, id, i), class_id)) return 1;
838+ int na = nt_num_arrs(c->nt, id);
839+ for (int i = 0; i < na; i++) { int n = 0; const int *ids = nt_arr_at(c->nt, id, i, &n);
840+ for (int k = 0; k < n; k++) if (proc_body_uses_self(c, ids[k], class_id)) return 1; }
841+ return 0;
842+}
843+
817844 /* Lower a `proc {}` / `lambda {}` / `Proc.new {}` / `->(){}` literal: emit a
818845 standalone `static mrb_int _proc_N(void *cap, mrb_int argc, mrb_int *args)`
819846 (sp_proc_call's ABI) into g_procs, and emit the boxing `sp_proc_new_meta(...)`
@@ -917,6 +944,13 @@
917944
918945 int pid = ++g_proc_counter;
919946 int ncap = caps.n;
947+ /* A block that references self and escapes into a real proc must capture self
948+ through _cap (#1436). Only for a pointer (heap-object) instance self -- a
949+ class-method self or a by-value self is a different shape, left as-is. */
950+ int cap_self = bs && bs->class_id >= 0 && !bs->is_cmethod &&
951+ !c->classes[bs->class_id].is_value_type &&
952+ proc_body_uses_self(c, body, bs->class_id);
953+ const char *self_cls = cap_self ? c->classes[bs->class_id].name : NULL;
920954
921955 /* parameter metadata for Proc#parameters: kinds (:req for lambdas, :opt for
922956 procs) + names, as interned symbol ids (pre-interned in analyze). */
@@ -936,14 +970,16 @@
936970 the cap struct itself first (sp_Proc_scan does not), then each cell --
937971 matching the sp_hashproc convention; marking only the cells would leave
938972 the cap struct unreachable and free it out from under the proc. */
939- if (ncap > 0) {
973+ if (ncap > 0 || cap_self) {
940974 buf_printf(&g_procs, "typedef struct {");
941975 for (int i = 0; i < ncap; i++) buf_printf(&g_procs, " mrb_int *%s;", caps.v[i]);
976+ if (cap_self) buf_puts(&g_procs, " void *__self;");
942977 buf_printf(&g_procs, " } _proc_cap_%d;\n", pid);
943978 buf_printf(&g_procs, "static void _proc_cap_scan_%d(void *p) {\n", pid);
944979 buf_printf(&g_procs, " sp_gc_mark(p);\n");
945980 buf_printf(&g_procs, " _proc_cap_%d *_c = (_proc_cap_%d *)p;\n", pid, pid);
946981 for (int i = 0; i < ncap; i++) buf_printf(&g_procs, " if (_c->%s) sp_gc_mark((void *)_c->%s);\n", caps.v[i], caps.v[i]);
982+ if (cap_self) buf_puts(&g_procs, " if (_c->__self) sp_gc_mark(_c->__self);\n");
947983 buf_puts(&g_procs, "}\n");
948984 }
949985
@@ -964,9 +1000,15 @@
9641000 Buf *pb = &g_procs;
9651001 buf_printf(pb, "static mrb_int _proc_%d(void *_cap, mrb_int argc, mrb_int *args) {\n", pid);
9661002 buf_puts(pb, " SP_GC_SAVE();\n");
967- if (ncap == 0) buf_puts(pb, " (void)_cap;\n");
1003+ if (ncap == 0 && !cap_self) buf_puts(pb, " (void)_cap;\n");
9681004 buf_puts(pb, " (void)args;\n");
9691005 buf_puts(pb, " (void)argc;\n");
1006+ /* Captured instance self, read back from _cap (#1436). (void) guards the
1007+ over-approximating use-of-self detection. */
1008+ if (cap_self) {
1009+ buf_printf(pb, " sp_%s *self = (sp_%s *)((_proc_cap_%d *)_cap)->__self;\n", self_cls, self_cls, pid);
1010+ buf_puts(pb, " (void)self;\n");
1011+ }
9701012 /* Lambda: enforce strict arity (required params only -- no optionals/rest yet). */
9711013 if (is_lambda) buf_printf(pb, " sp_proc_lambda_arity_check(argc, %d, 0, FALSE);\n", arity);
9721014 for (int k = 0; k < arity; k++) {
@@ -1034,7 +1076,7 @@
10341076 g_cap_struct = sv_cap_struct; g_cap_names = sv_cap_names; g_ensure_depth = sv_ensure_depth;
10351077 g_result_poly = sv_rp;
10361078
1037- if (ncap == 0) {
1079+ if (ncap == 0 && !cap_self) {
10381080 buf_printf(b, "sp_proc_new_meta((void *)_proc_%d, NULL, NULL, %d, %s, %d, %s)",
10391081 pid, arity, is_lambda ? "TRUE" : "FALSE", arity, meta_args);
10401082 }
@@ -1050,6 +1092,8 @@
10501092 emit_indent(g_pre, g_indent);
10511093 buf_printf(g_pre, "SP_GC_ROOT(_capv_%d);\n", pid);
10521094 for (int i = 0; i < ncap; i++) { emit_indent(g_pre, g_indent); buf_printf(g_pre, "_capv_%d->%s = _cell_%s;\n", pid, caps.v[i], caps.v[i]); }
1095+ /* Capture the enclosing instance self by pointer (#1436). */
1096+ if (cap_self) { emit_indent(g_pre, g_indent); buf_printf(g_pre, "_capv_%d->__self = (void *)%s;\n", pid, sv_self ? sv_self : "self"); }
10531097 }
10541098 buf_printf(b, "sp_proc_new_meta((void *)_proc_%d, _capv_%d, _proc_cap_scan_%d, %d, %s, %d, %s)",
10551099 pid, pid, pid, arity, is_lambda ? "TRUE" : "FALSE", arity, meta_args);