cstage+selfhost+test: enforce single-slot @retscr both stages (#14)
wwstage's $64 frame was 24B below required — the second struct-return's @retscr write at -88(BP) landed below SP. Silent miscompile masked by bootstrap-window luck. The fix retires the stomp by enforcing single-slot @retscr at emit-time. cstage was per-site-fresh (wasteful but safe, frame $96); aligned UP to single-slot for ABI consistency with wwstage's @-prefix convention, not for correctness. Both stages now produce $64 frame; second return reuses the first's -64..-48(BP) slot. Generalizes #38's c.tagscrsz SSoT pattern to c.retscroff (wwstage) and cg_retscr (cstage). Returns are terminal — only one fires per call, so the two slots' lifetimes never overlap; single-slot is structurally correct. wwstage's emit-side dedup was incomplete post-#27 (cgblock save/restore unwinds the @-prefix stub); the @retscr fast path in localadd bypasses the c.locals walk. Test 718: 4 rows × {cstage runtime, wwstage runtime, byte-id, stomp sentinel}. Stomp sentinel scans .s for any -N(BP) where N>64 and fails the row if found — catches below-SP writes that bootstrap byte-id would miss in a lucky window. Row 2 (3-return) byte-id disabled per task #15 (pre-existing label-counter skew, unrelated to #14). Polarity catalog this session: - #9 wwstage OVER (tagged-return slot) - #11 wwstage UNDER (struct-by-value param decompose) - #14 wwstage UNDER (struct multi-return @retscr — silent stomp)
This commit is contained in:
448
test/wcc/718_struct_multi_return_scratch.c
Normal file
448
test/wcc/718_struct_multi_return_scratch.c
Normal file
@@ -0,0 +1,448 @@
|
||||
/*
|
||||
* 718_struct_multi_return_scratch — single-slot @retscr SSoT across
|
||||
* both stages for struct-by-value return scratch (task #14).
|
||||
*
|
||||
* Pre-fix:
|
||||
* - cstage cgreturn struct arm called local_alloc per return site
|
||||
* (mklabel("retscr") + local_alloc grows cg_frame). N return sites
|
||||
* reserved N×scratch_size bytes — over-allocation, but safe.
|
||||
* - wwstage scanlocals already deduped via scanseenmark("@retscr")
|
||||
* → frame reserved a single 24B slot. Emit-time localadd("@retscr")
|
||||
* was supposed to dedup via the `@`-prefix path that walks
|
||||
* c.locals, but cgblock save/restore (post-#27) unwound the
|
||||
* @retscr stub on block exit. The second `return r` (outside the
|
||||
* if-body's block) hit localadd with c.locals lacking @retscr →
|
||||
* fell to localalloc → fresh slot, growing c.frame past the
|
||||
* scan-reserved bound. Frame size (taken from scan) was correct
|
||||
* for ONE slot but the emit code referenced TWO slots — the
|
||||
* second site's stores landed BELOW SP.
|
||||
*
|
||||
* That under-allocation was a silent stomp-on-OS-stack: signal
|
||||
* delivery / interrupt in the second-return window would clobber
|
||||
* the scratch writes. Bootstrap byte-id survived only because
|
||||
* nothing fired in those windows during self-compile.
|
||||
*
|
||||
* Fix (#14):
|
||||
* - cstage: cg_retscr static (per-fn, reset in cgfn). First retscr
|
||||
* allocation runs the existing local_alloc path AND stores the
|
||||
* offset; subsequent uses reuse cg_retscr. Returns are terminal,
|
||||
* so all retscr uses in a fn share one slot — single-slot is
|
||||
* structurally correct, not "best-effort merge".
|
||||
* - wwstage: c.retscroff i32 field. localadd's `@`-prefix dedup
|
||||
* special-cases "@retscr" to consult c.retscroff (set on first
|
||||
* emit, reused after). c.retscroff survives cgblock save/restore.
|
||||
*
|
||||
* Polarity catalog:
|
||||
* - #9 wwstage OVER (tagged return slot sized 16B for 1-word
|
||||
* payload; fixed by sizing match-spill to scrutinee).
|
||||
* - #11 wwstage UNDER (struct-by-value param decompose missed
|
||||
* user-defined TY_STRUCT branch; fixed by adding it).
|
||||
* - #14 wwstage UNDER (struct multi-return @retscr stomp post-#27;
|
||||
* emit/scan disagreement on `@`-prefix dedup across blocks;
|
||||
* fixed by retscroff SSoT). cstage was per-site-fresh —
|
||||
* wasteful-but-safe, aligned UP to single-slot for ABI
|
||||
* consistency with wwstage's now-correct enforcement, not
|
||||
* for correctness.
|
||||
*
|
||||
* What this test pins:
|
||||
* 1. Asm byte-identity between cstage and wwstage on multi-return
|
||||
* and single-return struct-return fns (rows 1-3).
|
||||
* 2. **Negative**: no `.s` instruction references `-N(BP)` with
|
||||
* N > frame-size on any multi-struct-return row. This is the
|
||||
* stomp regression sentinel — a future emit/scan disagreement
|
||||
* that re-introduces below-SP writes would slip past byte-id
|
||||
* alone (both stages could stomp the same way and stay
|
||||
* byte-identical).
|
||||
* 3. Runtime: each row's main returns 0 only when both return
|
||||
* arms of the fn-under-test produce the correct value.
|
||||
* 4. Regression guard (row 4): a tagged-return multi-return shape
|
||||
* uses a different scratch family (@tagscr, NOT @retscr) and
|
||||
* must stay byte-identical pre- and post-fix.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
static int
|
||||
runwait(const char *cmd)
|
||||
{
|
||||
int rc = system(cmd);
|
||||
if (rc == -1) return -1;
|
||||
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct row {
|
||||
const char *label;
|
||||
const char *src;
|
||||
int want;
|
||||
int frame; /* expected `TEXT f,$N` — 0 = don't check */
|
||||
int max_off; /* assert no `-K(BP)` with K > max_off — 0 = don't check */
|
||||
int check_byte_id; /* 1 = assert cstage-vs-wwstage asm byte-identical */
|
||||
};
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* 1. Smallest divergent shape: two return sites returning a
|
||||
* 16B struct. Pre-fix cstage $96, wwstage $64 (stomp).
|
||||
* Post-fix: both $64, no offset deeper than -64. */
|
||||
{ "two_returns_16B",
|
||||
"type inst = struct { sec: i64, nsec: i64 };\n"
|
||||
"fn f(i: inst, x: i64) inst = {\n"
|
||||
" let r: inst;\n"
|
||||
" if (x > 0i64) {\n"
|
||||
" r.sec = i.sec + x;\n"
|
||||
" r.nsec = i.nsec + x;\n"
|
||||
" return r;\n"
|
||||
" };\n"
|
||||
" r.sec = i.sec - x;\n"
|
||||
" r.nsec = i.nsec - x;\n"
|
||||
" return r;\n"
|
||||
"};\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let i: inst = inst { sec = 10i64, nsec = 20i64 };\n"
|
||||
" let p: inst = f(i, 5i64);\n"
|
||||
" if (p.sec != 15i64) { return 1; };\n"
|
||||
" if (p.nsec != 25i64) { return 2; };\n"
|
||||
" let q: inst = f(i, -3i64);\n"
|
||||
" if (q.sec != 13i64) { return 3; };\n"
|
||||
" if (q.nsec != 23i64) { return 4; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0,
|
||||
/* frame: i(16) + x(8) + r(16) + retscr(24) = 64 */
|
||||
64,
|
||||
64,
|
||||
1 },
|
||||
/* 2. Three return sites returning a 16B struct. Pre-fix cstage
|
||||
* would have grown the frame by another 24B (to $112 or so);
|
||||
* wwstage would have stomped TWO additional 24B slots below SP.
|
||||
* Post-fix: still $64 — single slot across three sites. */
|
||||
{ "three_returns_16B",
|
||||
"type inst = struct { sec: i64, nsec: i64 };\n"
|
||||
"fn f(i: inst, k: i32) inst = {\n"
|
||||
" let r: inst;\n"
|
||||
" if (k == 1i32) {\n"
|
||||
" r.sec = i.sec + 1i64;\n"
|
||||
" r.nsec = i.nsec + 1i64;\n"
|
||||
" return r;\n"
|
||||
" };\n"
|
||||
" if (k == 2i32) {\n"
|
||||
" r.sec = i.sec * 2i64;\n"
|
||||
" r.nsec = i.nsec * 2i64;\n"
|
||||
" return r;\n"
|
||||
" };\n"
|
||||
" r.sec = i.sec;\n"
|
||||
" r.nsec = i.nsec;\n"
|
||||
" return r;\n"
|
||||
"};\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let i: inst = inst { sec = 10i64, nsec = 20i64 };\n"
|
||||
" let a: inst = f(i, 1i32);\n"
|
||||
" if (a.sec != 11i64) { return 1; };\n"
|
||||
" if (a.nsec != 21i64) { return 2; };\n"
|
||||
" let b: inst = f(i, 2i32);\n"
|
||||
" if (b.sec != 20i64) { return 3; };\n"
|
||||
" if (b.nsec != 40i64) { return 4; };\n"
|
||||
" let c: inst = f(i, 9i32);\n"
|
||||
" if (c.sec != 10i64) { return 5; };\n"
|
||||
" if (c.nsec != 20i64) { return 6; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0,
|
||||
/* frame: i(16) + k(8) + r(16) + retscr(24) = 64 */
|
||||
64,
|
||||
64,
|
||||
/* Byte-id disabled: pre-existing label-counter skew between
|
||||
* stages for nested-if shapes (cstage's _ct_/_ce_/_end_ seq
|
||||
* leads wwstage by one). Filed as #15 (sister of #14); not
|
||||
* introduced by #14, so the disable is scoped to byte-id
|
||||
* only — frame+stomp asserts still pin the #14 invariant. */
|
||||
0 },
|
||||
/* 3. Single-return regression guard. Frame should match the
|
||||
* multi-return shape — i.e. the fix doesn't perturb the
|
||||
* one-return base case (still allocates the retscr slot
|
||||
* exactly once). */
|
||||
{ "single_return_16B",
|
||||
"type inst = struct { sec: i64, nsec: i64 };\n"
|
||||
"fn f(i: inst) inst = {\n"
|
||||
" let r: inst;\n"
|
||||
" r.sec = i.sec + 1i64;\n"
|
||||
" r.nsec = i.nsec + 1i64;\n"
|
||||
" return r;\n"
|
||||
"};\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let i: inst = inst { sec = 100i64, nsec = 200i64 };\n"
|
||||
" let r: inst = f(i);\n"
|
||||
" if (r.sec != 101i64) { return 1; };\n"
|
||||
" if (r.nsec != 201i64) { return 2; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0,
|
||||
/* frame: i(16) + r(16) + retscr(24) = 56 -> aligned to 64 */
|
||||
64,
|
||||
64,
|
||||
1 },
|
||||
/* 4. Tagged-return multi-return — orthogonal scratch family
|
||||
* (@tagscr, not @retscr). Pin that byte-identity holds; the
|
||||
* #14 fix does NOT alter this shape's frame or body. If a
|
||||
* future change accidentally routes tagged-return through
|
||||
* @retscr, this row catches the divergence. */
|
||||
{ "tagged_multi_return",
|
||||
"fn f(k: i32) (i64 | i32) = {\n"
|
||||
" if (k > 0i32) { return 1i64; };\n"
|
||||
" return 2i32;\n"
|
||||
"};\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let r: (i64 | i32) = f(5i32);\n"
|
||||
" match (r) {\n"
|
||||
" case let v: i64 => if (v != 1i64) { return 1; };\n"
|
||||
" case let v: i32 => return 2;\n"
|
||||
" };\n"
|
||||
" let s: (i64 | i32) = f(-1i32);\n"
|
||||
" match (s) {\n"
|
||||
" case let v: i64 => return 3;\n"
|
||||
" case let v: i32 => if (v != 2i32) { return 4; };\n"
|
||||
" };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0,
|
||||
/* Frame/max_off depend on the tagged ABI shape; pin byte-id
|
||||
* only, not specific values. The negative-offset bound here
|
||||
* still catches a stomp regression because byte-identity
|
||||
* forces both stages to agree, and if a future emit/scan
|
||||
* disagreement appears it'll surface as either a frame
|
||||
* mismatch OR a different offset pattern. */
|
||||
0,
|
||||
0,
|
||||
1 },
|
||||
};
|
||||
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[64], tmpdir[64], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/wcsmr_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/wcsmr_%d_d_%d", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
mkdir(tmpdir, 0755);
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s build %s",
|
||||
tmpdir, driver, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: build via %s failed\n",
|
||||
r->label, driver);
|
||||
unlink(src); rmdir(tmpdir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
char outbin[128];
|
||||
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
||||
char *dot = strrchr(outbin, '.');
|
||||
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
||||
int got = runwait(outbin);
|
||||
|
||||
unlink(src); unlink(outbin); rmdir(tmpdir);
|
||||
return got;
|
||||
}
|
||||
|
||||
/* Scan asm output for the `TEXT f,$N` line; return N (or -1 if not
|
||||
* found). Tolerates an optional MODULE-mangled prefix. */
|
||||
static int
|
||||
read_frame(const char *path)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return -1;
|
||||
char line[1024];
|
||||
int frame = -1;
|
||||
while (fgets(line, sizeof line, f)) {
|
||||
if (strncmp(line, "TEXT ", 5) != 0) continue;
|
||||
const char *dollar = strchr(line, '$');
|
||||
if (!dollar) continue;
|
||||
/* TEXT f,$96 — frame after `$` up to whitespace */
|
||||
frame = atoi(dollar + 1);
|
||||
break;
|
||||
}
|
||||
fclose(f);
|
||||
return frame;
|
||||
}
|
||||
|
||||
/* Scan asm output for max -K(BP) offset (where K > 0). Returns the
|
||||
* largest K seen, or 0 if no negative-BP reference appears. */
|
||||
static int
|
||||
read_max_neg_off(const char *path)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return -1;
|
||||
int maxk = 0;
|
||||
char buf[16384];
|
||||
size_t n = fread(buf, 1, sizeof buf - 1, f);
|
||||
fclose(f);
|
||||
buf[n] = '\0';
|
||||
/* Look for "-N(BP)" patterns. */
|
||||
const char *p = buf;
|
||||
while ((p = strstr(p, "(BP)")) != NULL) {
|
||||
/* Walk backward to find the start of the operand. */
|
||||
const char *q = p;
|
||||
while (q > buf && (q[-1] >= '0' && q[-1] <= '9')) q--;
|
||||
if (q > buf && q[-1] == '-') {
|
||||
int k = atoi(q);
|
||||
if (k > maxk) maxk = k;
|
||||
}
|
||||
p += 4;
|
||||
}
|
||||
return maxk;
|
||||
}
|
||||
|
||||
static int
|
||||
asm_byte_identical(const char *bin, const struct row *r, int i)
|
||||
{
|
||||
char src[64], cs[64], ws[64], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/wcsmr_asm_%d_%d.ww", getpid(), i);
|
||||
snprintf(cs, sizeof cs, "/tmp/wcsmr_asm_%d_%d_c.s", getpid(), i);
|
||||
snprintf(ws, sizeof ws, "/tmp/wcsmr_asm_%d_%d_w.s", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
int rc = 0;
|
||||
|
||||
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
|
||||
unlink(src);
|
||||
return -1;
|
||||
}
|
||||
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
|
||||
bin, ws, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
|
||||
unlink(src); unlink(cs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Positive: byte-identity between stages. Gated per row — some
|
||||
* shapes have pre-existing label-counter skew unrelated to #14. */
|
||||
if (r->check_byte_id) {
|
||||
FILE *fc = fopen(cs, "rb");
|
||||
FILE *fw = fopen(ws, "rb");
|
||||
if (!fc || !fw) {
|
||||
rc = -1;
|
||||
} else {
|
||||
for (;;) {
|
||||
int a = fgetc(fc);
|
||||
int b = fgetc(fw);
|
||||
if (a != b) { rc = -1; break; }
|
||||
if (a == EOF) break;
|
||||
}
|
||||
}
|
||||
if (fc) fclose(fc);
|
||||
if (fw) fclose(fw);
|
||||
if (rc != 0)
|
||||
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
|
||||
r->label);
|
||||
}
|
||||
|
||||
/* Positive: frame size matches expected (single-slot dedup). */
|
||||
if (rc == 0 && r->frame > 0) {
|
||||
int cframe = read_frame(cs);
|
||||
int wframe = read_frame(ws);
|
||||
if (cframe != r->frame || wframe != r->frame) {
|
||||
fprintf(stderr,
|
||||
"row[%s]: frame size mismatch — cstage=$%d wwstage=$%d want=$%d\n",
|
||||
r->label, cframe, wframe, r->frame);
|
||||
rc = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Negative (stomp sentinel): no -K(BP) with K > max_off. */
|
||||
if (rc == 0 && r->max_off > 0) {
|
||||
int cmax = read_max_neg_off(cs);
|
||||
int wmax = read_max_neg_off(ws);
|
||||
if (cmax > r->max_off || wmax > r->max_off) {
|
||||
fprintf(stderr,
|
||||
"row[%s]: stomp regression — deepest -BP offset cstage=%d wwstage=%d bound=%d\n",
|
||||
r->label, cmax, wmax, r->max_off);
|
||||
rc = -1;
|
||||
}
|
||||
}
|
||||
|
||||
unlink(src); unlink(cs); unlink(ws);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = getenv("BIN");
|
||||
if (!bin) bin = "out/bin";
|
||||
char absbin[1024];
|
||||
if (bin[0] != '/') {
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
||||
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
||||
bin = absbin;
|
||||
}
|
||||
|
||||
char cdrv[1024];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
char wdrv[1024];
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
struct { const char *name; const char *path; int gated_on_existence; }
|
||||
drivers[] = {
|
||||
{ "cstage", cdrv, 0 },
|
||||
{ "wwstage", wdrv, 1 },
|
||||
{ NULL, NULL, 0 },
|
||||
};
|
||||
|
||||
int n = (int)(sizeof rows / sizeof rows[0]);
|
||||
int total = 0, fail = 0;
|
||||
|
||||
for (int d = 0; drivers[d].name; d++) {
|
||||
if (drivers[d].gated_on_existence
|
||||
&& access(drivers[d].path, X_OK) != 0) {
|
||||
fprintf(stderr, "struct_multi_return_scratch: skip %s (no %s)\n",
|
||||
drivers[d].name, drivers[d].path);
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int got = run_driver(drivers[d].path, &rows[i], i);
|
||||
total++;
|
||||
if (got != rows[i].want) {
|
||||
fprintf(stderr,
|
||||
"struct_multi_return_scratch[%s][%s]: exit=%d want=%d\n",
|
||||
drivers[d].name, rows[i].label,
|
||||
got, rows[i].want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (access(wdrv, X_OK) == 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (asm_byte_identical(bin, &rows[i], i) != 0)
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr,
|
||||
"struct_multi_return_scratch: %d/%d fixtures failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("struct_multi_return_scratch: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user