Commit 5e7b054b

5e7b054b681eMerged

Ryan Seys <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

codegen: make unsupported() noreturn to fix SP_COLLECT_ERRORS crashes

unsupported() aborts on the first codegen gap unless SP_COLLECT_ERRORS is set, in which case it returned so one run could surface every gap. But the code after each unsupported() call assumes it does not return -- it then dereferenced a now-NULL name/op or indexed an empty argv, so collect mode crashed rather than collecting.

Make unsupported() never return: it still exits in normal mode, and in collect mode longjmps to a per-unit recovery point armed by the output driver. Each top-level unit (method, constructor, BEGIN/END block, the top-level body) emits under a fresh setjmp; a gap prints, abandons that unit's discarded output, and the driver proceeds to the next. This is safe because emit_method re-sets all per-method emission globals at entry, so an abandoned unit cannot corrupt the next; the noreturn attribute lets the compiler treat every post-unsupported path as unreachable.

Normal-mode output is unchanged (the recovery wrapper is a no-op outside collect mode; bootstrap fixpoint holds). Collect mode now surfaces one gap per unit across the whole program without crashing.

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

Details

Commit
5e7b054b681e
Parent
ef0dd7102c46

src/codegen.cmodifiedView file

@@ -2059,6 +2059,32 @@
20592059 return 0;
20602060 }
20612061
2062+/* Emit one top-level output unit (a method, constructor, BEGIN/END block, or the
2063+ top-level body). Outside SP_COLLECT_ERRORS this is just the bare call. In
2064+ collect mode each unit runs under a setjmp: an `unsupported` gap longjmps back
2065+ here (instead of exiting), so one run surfaces every unsupported construct --
2066+ the gap is already printed, this unit's malformed output is discarded, and the
2067+ driver proceeds to the next unit. `unsupported` re-sets all per-method globals
2068+ on the next emit_method, so an abandoned unit cannot corrupt the next.
2069+ On recovery the buffer is rolled back to its length before this unit, so the
2070+ abandoned unit's partial output is dropped. `body` must be the heap pointer
2071+ (not an automatic) so a longjmp doesn't leave it indeterminate (C99 7.13.2.1);
2072+ _saved_len is set before setjmp and so stays determinate across the jump. */
2073+#define EMIT_COLLECT_UNIT(emit_call) \
2074+ do { \
2075+ if (!collect_mode()) { emit_call; } \
2076+ else { \
2077+ size_t _saved_len = body->len; \
2078+ if (setjmp(g_unsup_recover) == 0) { \
2079+ g_unsup_armed = 1; emit_call; g_unsup_armed = 0; \
2080+ } else { \
2081+ g_unsup_armed = 0; \
2082+ body->len = _saved_len; \
2083+ if (body->p) body->p[_saved_len] = '\0'; \
2084+ } \
2085+ } \
2086+ } while (0)
2087+
20622088 char *codegen_program(const NodeTable *nt) {
20632089 Compiler *c = comp_new(nt);
20642090 analyze_program(c);
@@ -2608,12 +2634,12 @@
26082634 proc literals they contain accumulate static functions into g_procs /
26092635 g_proc_protos; we splice those in ahead of these bodies, since a proc
26102636 function must be declared before the body that references it. */
2611- Buf body; memset(&body, 0, sizeof body);
2637+ Buf *body = (Buf *)calloc(1, sizeof *body); /* heap: must survive a collect-mode longjmp (see EMIT_COLLECT_UNIT) */
26122638 for (int i = 0; i < c->nclasses; i++)
26132639 if (!is_builtin_reopen(c->classes[i].name))
2614- emit_class_new(c, &c->classes[i], &body);
2640+ EMIT_COLLECT_UNIT(emit_class_new(c, &c->classes[i], body));
26152641 for (int s = 1; s < c->nscopes; s++) {
2616- if (c->scopes[s].yields || !c->scopes[s].reachable || scope_is_shadowed(c, s) || c->scopes[s].is_transplanted_source) continue; emit_method(c, &c->scopes[s], &body);
2642+ if (c->scopes[s].yields || !c->scopes[s].reachable || scope_is_shadowed(c, s) || c->scopes[s].is_transplanted_source) continue; EMIT_COLLECT_UNIT(emit_method(c, &c->scopes[s], body));
26172643 }
26182644
26192645 /* Emit END block static functions for atexit registration */
@@ -2630,38 +2656,38 @@
26302656 if (!sty || strcmp(sty, "PostExecutionNode")) continue;
26312657 int stmts = nt_ref(c->nt, tbody[k], "statements");
26322658 end_count++;
2633- buf_printf(&body, "static void sp_end_fn_%d(void) { SP_GC_SAVE();\n", end_count);
2634- emit_stmts(c, stmts, &body, 1);
2635- buf_puts(&body, "}\n");
2659+ buf_printf(body, "static void sp_end_fn_%d(void) { SP_GC_SAVE();\n", end_count);
2660+ EMIT_COLLECT_UNIT(emit_stmts(c, stmts, body, 1));
2661+ buf_puts(body, "}\n");
26362662 }
26372663 }
26382664 }
26392665
2640- buf_puts(&body, "int main(int argc,char**argv){\n");
2641- buf_puts(&body, " SP_GC_SAVE();\n");
2642- buf_puts(&body, " sp_re_init();\n");
2643- buf_puts(&body, " { sp_argv.len = argc - 1; sp_argv.data = (const char**)malloc(sizeof(const char*) * (size_t)(argc > 1 ? argc - 1 : 1)); for (int _ai = 0; _ai < argc - 1; _ai++) sp_argv.data[_ai] = sp_str_dup_external(argv[_ai + 1]); }\n");
2644- buf_puts(&body, " sp_program_name = argc > 0 ? argv[0] : \"\";\n");
2666+ buf_puts(body, "int main(int argc,char**argv){\n");
2667+ buf_puts(body, " SP_GC_SAVE();\n");
2668+ buf_puts(body, " sp_re_init();\n");
2669+ buf_puts(body, " { sp_argv.len = argc - 1; sp_argv.data = (const char**)malloc(sizeof(const char*) * (size_t)(argc > 1 ? argc - 1 : 1)); for (int _ai = 0; _ai < argc - 1; _ai++) sp_argv.data[_ai] = sp_str_dup_external(argv[_ai + 1]); }\n");
2670+ buf_puts(body, " sp_program_name = argc > 0 ? argv[0] : \"\";\n");
26452671 /* Enable the backtrace substrate (Exception#backtrace, Kernel#caller) in
26462672 debug builds only: --debug compiles at -O0 with non-inlined methods, so
26472673 the captured frames demangle to Class#method. Optimized/release builds
26482674 leave sp_bt_enabled = 0 (frames inline away), keeping the empty-array
26492675 behavior. */
26502676 if (getenv("SPINEL_DEBUG")) {
2651- buf_puts(&body, " sp_bt_enabled = 1;\n");
2652- buf_puts(&body, " sp_bt_srcfile = ");
2653- emit_str_literal(&body, c->nt->source_file ? c->nt->source_file : "source.rb");
2654- buf_puts(&body, ";\n");
2677+ buf_puts(body, " sp_bt_enabled = 1;\n");
2678+ buf_puts(body, " sp_bt_srcfile = ");
2679+ emit_str_literal(body, c->nt->source_file ? c->nt->source_file : "source.rb");
2680+ buf_puts(body, ";\n");
26552681 }
26562682 /* Ruby auto-seeds its PRNG at startup, so rand/shuffle/sample vary per
26572683 run. Seed once here; an explicit srand(seed) in user code runs later
26582684 and overrides this for reproducible sequences. */
2659- buf_puts(&body, " srand((unsigned)time(NULL));\n");
2685+ buf_puts(body, " srand((unsigned)time(NULL));\n");
26602686 /* Register END blocks (atexit runs LIFO, so they execute in reverse registration order) */
26612687 for (int e = 1; e <= end_count; e++)
2662- buf_printf(&body, " atexit(sp_end_fn_%d);\n", e);
2663- emit_scope_decls(c, &c->scopes[0], &body);
2664- buf_puts(&body, "\n");
2688+ buf_printf(body, " atexit(sp_end_fn_%d);\n", e);
2689+ emit_scope_decls(c, &c->scopes[0], body);
2690+ buf_puts(body, "\n");
26652691 /* Hoist BEGIN blocks to run first */
26662692 {
26672693 int top_body = c->scopes[0].body;
@@ -2674,20 +2700,21 @@
26742700 const char *sty = nt_type(c->nt, tbody[k]);
26752701 if (!sty || strcmp(sty, "PreExecutionNode")) continue;
26762702 int stmts = nt_ref(c->nt, tbody[k], "statements");
2677- emit_stmts(c, stmts, &body, 1);
2703+ EMIT_COLLECT_UNIT(emit_stmts(c, stmts, body, 1));
26782704 }
26792705 }
26802706 }
2681- emit_stmts(c, c->scopes[0].body, &body, 1);
2707+ EMIT_COLLECT_UNIT(emit_stmts(c, c->scopes[0].body, body, 1));
26822708 if (g_needs_at_exit)
2683- buf_puts(&body, " { mrb_int _ax_args[16] = {0}; for (mrb_int _ax = sp_at_exit_count - 1; _ax >= 0; _ax--) sp_proc_call(sp_at_exit_hooks[_ax], 0, _ax_args); }\n");
2684- buf_puts(&body, " return 0;\n}\n");
2709+ buf_puts(body, " { mrb_int _ax_args[16] = {0}; for (mrb_int _ax = sp_at_exit_count - 1; _ax >= 0; _ax--) sp_proc_call(sp_at_exit_hooks[_ax], 0, _ax_args); }\n");
2710+ buf_puts(body, " return 0;\n}\n");
26852711
26862712 emit_regex_section(&b);
26872713 if (g_proc_protos.len) { buf_puts(&b, g_proc_protos.p); buf_puts(&b, "\n"); }
26882714 if (g_procs.len) { buf_puts(&b, g_procs.p); buf_puts(&b, "\n"); }
2689- buf_puts(&b, body.p ? body.p : "");
2690- free(body.p);
2715+ buf_puts(&b, body->p ? body->p : "");
2716+ free(body->p);
2717+ free(body);
26912718 free(g_procs.p); free(g_proc_protos.p);
26922719 memset(&g_procs, 0, sizeof g_procs);
26932720 memset(&g_proc_protos, 0, sizeof g_proc_protos);