Commit 4058359a

4058359a8053Merged

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

Description

Merge pull request #1473 from ryanseys/rs-collect-errors-recover

codegen: make unsupported() noreturn to fix SP_COLLECT_ERRORS crashes

Details

Commit
4058359a8053
Parent
86828e88be37
Merge parent
5e7b054b681e

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);
@@ -2612,12 +2638,12 @@
26122638 proc literals they contain accumulate static functions into g_procs /
26132639 g_proc_protos; we splice those in ahead of these bodies, since a proc
26142640 function must be declared before the body that references it. */
2615- Buf body; memset(&body, 0, sizeof body);
2641+ Buf *body = (Buf *)calloc(1, sizeof *body); /* heap: must survive a collect-mode longjmp (see EMIT_COLLECT_UNIT) */
26162642 for (int i = 0; i < c->nclasses; i++)
26172643 if (!is_builtin_reopen(c->classes[i].name))
2618- emit_class_new(c, &c->classes[i], &body);
2644+ EMIT_COLLECT_UNIT(emit_class_new(c, &c->classes[i], body));
26192645 for (int s = 1; s < c->nscopes; s++) {
2620- 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);
2646+ 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));
26212647 }
26222648
26232649 /* Emit END block static functions for atexit registration */
@@ -2634,38 +2660,38 @@
26342660 if (!sty || strcmp(sty, "PostExecutionNode")) continue;
26352661 int stmts = nt_ref(c->nt, tbody[k], "statements");
26362662 end_count++;
2637- buf_printf(&body, "static void sp_end_fn_%d(void) { SP_GC_SAVE();\n", end_count);
2638- emit_stmts(c, stmts, &body, 1);
2639- buf_puts(&body, "}\n");
2663+ buf_printf(body, "static void sp_end_fn_%d(void) { SP_GC_SAVE();\n", end_count);
2664+ EMIT_COLLECT_UNIT(emit_stmts(c, stmts, body, 1));
2665+ buf_puts(body, "}\n");
26402666 }
26412667 }
26422668 }
26432669
2644- buf_puts(&body, "int main(int argc,char**argv){\n");
2645- buf_puts(&body, " SP_GC_SAVE();\n");
2646- buf_puts(&body, " sp_re_init();\n");
2647- 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");
2648- buf_puts(&body, " sp_program_name = argc > 0 ? argv[0] : \"\";\n");
2670+ buf_puts(body, "int main(int argc,char**argv){\n");
2671+ buf_puts(body, " SP_GC_SAVE();\n");
2672+ buf_puts(body, " sp_re_init();\n");
2673+ 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");
2674+ buf_puts(body, " sp_program_name = argc > 0 ? argv[0] : \"\";\n");
26492675 /* Enable the backtrace substrate (Exception#backtrace, Kernel#caller) in
26502676 debug builds only: --debug compiles at -O0 with non-inlined methods, so
26512677 the captured frames demangle to Class#method. Optimized/release builds
26522678 leave sp_bt_enabled = 0 (frames inline away), keeping the empty-array
26532679 behavior. */
26542680 if (getenv("SPINEL_DEBUG")) {
2655- buf_puts(&body, " sp_bt_enabled = 1;\n");
2656- buf_puts(&body, " sp_bt_srcfile = ");
2657- emit_str_literal(&body, c->nt->source_file ? c->nt->source_file : "source.rb");
2658- buf_puts(&body, ";\n");
2681+ buf_puts(body, " sp_bt_enabled = 1;\n");
2682+ buf_puts(body, " sp_bt_srcfile = ");
2683+ emit_str_literal(body, c->nt->source_file ? c->nt->source_file : "source.rb");
2684+ buf_puts(body, ";\n");
26592685 }
26602686 /* Ruby auto-seeds its PRNG at startup, so rand/shuffle/sample vary per
26612687 run. Seed once here; an explicit srand(seed) in user code runs later
26622688 and overrides this for reproducible sequences. */
2663- buf_puts(&body, " srand((unsigned)time(NULL));\n");
2689+ buf_puts(body, " srand((unsigned)time(NULL));\n");
26642690 /* Register END blocks (atexit runs LIFO, so they execute in reverse registration order) */
26652691 for (int e = 1; e <= end_count; e++)
2666- buf_printf(&body, " atexit(sp_end_fn_%d);\n", e);
2667- emit_scope_decls(c, &c->scopes[0], &body);
2668- buf_puts(&body, "\n");
2692+ buf_printf(body, " atexit(sp_end_fn_%d);\n", e);
2693+ emit_scope_decls(c, &c->scopes[0], body);
2694+ buf_puts(body, "\n");
26692695 /* Hoist BEGIN blocks to run first */
26702696 {
26712697 int top_body = c->scopes[0].body;
@@ -2678,20 +2704,21 @@
26782704 const char *sty = nt_type(c->nt, tbody[k]);
26792705 if (!sty || strcmp(sty, "PreExecutionNode")) continue;
26802706 int stmts = nt_ref(c->nt, tbody[k], "statements");
2681- emit_stmts(c, stmts, &body, 1);
2707+ EMIT_COLLECT_UNIT(emit_stmts(c, stmts, body, 1));
26822708 }
26832709 }
26842710 }
2685- emit_stmts(c, c->scopes[0].body, &body, 1);
2711+ EMIT_COLLECT_UNIT(emit_stmts(c, c->scopes[0].body, body, 1));
26862712 if (g_needs_at_exit)
2687- 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");
2688- buf_puts(&body, " return 0;\n}\n");
2713+ 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");
2714+ buf_puts(body, " return 0;\n}\n");
26892715
26902716 emit_regex_section(&b);
26912717 if (g_proc_protos.len) { buf_puts(&b, g_proc_protos.p); buf_puts(&b, "\n"); }
26922718 if (g_procs.len) { buf_puts(&b, g_procs.p); buf_puts(&b, "\n"); }
2693- buf_puts(&b, body.p ? body.p : "");
2694- free(body.p);
2719+ buf_puts(&b, body->p ? body->p : "");
2720+ free(body->p);
2721+ free(body);
26952722 free(g_procs.p); free(g_proc_protos.p);
26962723 memset(&g_procs, 0, sizeof g_procs);
26972724 memset(&g_proc_protos, 0, sizeof g_proc_protos);