/* * 930_struct_tuple_field_slot — project #237: a tuple-typed struct field * must contribute its real slot width to the enclosing struct's slotsize. * * wwstage's checker `fieldslotsize` (check.ww) summed struct field SLOT * sizes to stamp a struct's tinfo.slotsize, but had no TY_TUPLE arm — a * tuple field fell to the 8B default. So `S = struct { f: ([]u8,[]u8) }` * stamped slotsize=8 while size=48 (the natural sum, correct). A `let s:S` * slot is allocated off ti.slotsize (cgenutil.ww slotsize), so wwstage * reserved an 8-byte frame slot for a 48-byte struct: a SILENT stack- * corrupting miscompile. cstage has no size/slotsize split (it sizes the * field at f->type->size=48 throughout), so the two stages DIVERGED on the * emitted frame ($16 wwstage vs $64 cstage) — invisible to a cstage-only * check, caught only by cs==ww byte-id (rule 10). The fix adds the TY_TUPLE * arm (return the tuple's own slotsize), aligning fieldslotsize with the * cgen-side cgenutil.ww fieldsize that already returns 48. * * GATE: pure cs==ww .s byte-id. No runtime — the divergence is a frame/slot * SIZE, fully visible in the emitted assembly. The fixture takes &s.f so the * struct slot is materialised; no over-cap call is involved (this isolates * #237 from the #234 store-routing). A FAIL means the stages disagree on the * struct's frame size (the #237 regression). */ #include #include #include #include static int runwait(const char *cmd) { int rc = system(cmd); if (rc == -1) return -1; if (WIFEXITED(rc)) return WEXITSTATUS(rc); return -1; } static int slurp_eq(const char *a, const char *b) { FILE *fa = fopen(a, "rb"); FILE *fb = fopen(b, "rb"); if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; } int rc = 0; for (;;) { int ca = fgetc(fa); int cb = fgetc(fb); if (ca != cb) { rc = -1; break; } if (ca == EOF) break; } fclose(fa); fclose(fb); return rc; } struct row { const char *label; const char *src; }; static const struct row rows[] = { /* one tuple field: slotsize must be 48, not the 8B default. */ { "single_tuple_field", "package main;\n" "type S = struct { f: ([]u8, []u8) };\n" "export fn main() i32 = {\n" " let s: S;\n" " let p: *int = (&s.f): *int;\n" " p[1] = 1;\n" " return 0;\n" "};\n" }, /* tuple field preceded by a scalar: foff!=0, slot still full-width. */ { "hdr_then_tuple_field", "package main;\n" "type S = struct { hdr: int, f: ([]u8, []u8) };\n" "export fn main() i32 = {\n" " let s: S;\n" " s.hdr = 9;\n" " let p: *int = (&s.f): *int;\n" " p[1] = 1;\n" " return 0;\n" "};\n" }, { NULL, NULL }, }; int main(void) { const char *bin = getenv("BIN"); if (!bin) bin = "out/bin"; char absbin[1024]; if (bin[0] != '/') { char cwd[256]; if (getcwd(cwd, sizeof cwd) == NULL) return 1; snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); bin = absbin; } char w6c[1100], w6c_ww[1100]; snprintf(w6c, sizeof w6c, "%s/w6c", bin); snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); if (access(w6c_ww, X_OK) != 0) { fprintf(stderr, "struct_tuple_field_slot: w6c_ww missing — " "cannot run the cs==ww byte-id gate\n"); return 1; } int n = 0, fail = 0; for (int i = 0; rows[i].src; i++, n++) { char src[64], cs_s[64], ws_s[64], cmd[2048]; snprintf(src, sizeof src, "/tmp/stfs_%d_%d.ww", getpid(), i); snprintf(cs_s, sizeof cs_s, "/tmp/stfs_%d_%d_cs.s", getpid(), i); snprintf(ws_s, sizeof ws_s, "/tmp/stfs_%d_%d_ww.s", getpid(), i); FILE *f = fopen(src, "wb"); if (f == NULL) { fail++; continue; } fputs(rows[i].src, f); fclose(f); snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label); fail++; unlink(src); continue; } snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c_ww, ws_s, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c_ww failed\n", rows[i].label); fail++; unlink(src); unlink(cs_s); continue; } if (slurp_eq(cs_s, ws_s) != 0) { fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER " "(#237 struct tuple-field slot-size regression)\n", rows[i].label); fail++; } unlink(src); unlink(cs_s); unlink(ws_s); } if (fail) { fprintf(stderr, "%d/%d struct-tuple-field-slot tests failed\n", fail, n); return 1; } printf("struct_tuple_field_slot: %d/%d ok (cs==ww byte-id)\n", n, n); return 0; }