Commit e3360c87

e3360c876d88Merged

Ryan Seys <[email protected]> · 5 months ago · 6 files · +2 added · ~4 modified

Description

codegen: type polymorphic proc parameters through a side-channel

Details

Commit
e3360c876d88
Parent
c01a389239c1

src/codegen.cmodifiedView file

@@ -1074,8 +1074,21 @@
10741074 LocalVar *lv = scope_local(bs, p);
10751075 TyKind pt = lv ? lv->type : TY_INT;
10761076 buf_puts(pb, " "); emit_ctype(c, pt, pb); buf_printf(pb, " lv_%s = ", p);
1077- /* a heap-pointer param is laundered back from the mrb_int slot */
1078- if (proc_slot_is_ptr(pt)) { buf_puts(pb, "("); emit_ctype(c, pt, pb); buf_printf(pb, ")(uintptr_t)args[%d];\n", k); }
1077+ /* a heap-pointer param is laundered back from the mrb_int slot; a TY_POLY
1078+ (sp_RbVal) param doesn't fit the slot, so it rides the _sp_proc_poly_args
1079+ side-channel the call site published before the call. */
1080+ if (pt == TY_POLY) {
1081+ /* the side-channel array holds 16 slots (the proc-call ABI cap) */
1082+ if (k < 16) {
1083+ if (!g_needs_proc_poly_argslot) {
1084+ g_needs_proc_poly_argslot = 1;
1085+ buf_puts(&g_proc_protos, "static sp_RbVal _sp_proc_poly_args[16];\n");
1086+ }
1087+ buf_printf(pb, "_sp_proc_poly_args[%d];\n", k);
1088+ }
1089+ else buf_puts(pb, "0;\n");
1090+ }
1091+ else if (proc_slot_is_ptr(pt)) { buf_puts(pb, "("); emit_ctype(c, pt, pb); buf_printf(pb, ")(uintptr_t)args[%d];\n", k); }
10791092 else buf_printf(pb, "args[%d];\n", k);
10801093 }
10811094 for (int i = 0; i < locals.n; i++) {
@@ -2673,6 +2686,7 @@
26732686 memset(&g_procs, 0, sizeof g_procs);
26742687 memset(&g_proc_protos, 0, sizeof g_proc_protos);
26752688 g_needs_proc_poly_retslot = 0;
2689+ g_needs_proc_poly_argslot = 0;
26762690
26772691 comp_free(c);
26782692 return b.p;