Files
ww/test/wcc/762_struct_abi_size.c
Hojun-Cho 65b7e15e09 test: replace duplicate row 5 in 762 with mid-record-pad shape
Row 5 in 098b58d was byte-identical to row 2 ({i32,i32,i64} fields
{{4,4},{4,4},{8,8}}): same struct, same field table, only label
differed. The row's own comment acknowledged "off from 8→8 (already
aligned)" — i.e., it did not exercise mid-record alignment at all
despite the label "i32_i32_i64_mid_align" and the commit-body
listing "mid-record align step explicit".

Replace with {i16, i64}: after f1 off=2, f2 must align off 2→8
before placing the i64. A missing `off = align(off, fa)` on either
walker (check.c:760 / check.ww:958 / structabisize) would land f2
at off=2 and size to 10 — clearly red. This is the shape the row
was meant to be.

Six distinct shapes after this fix; gate polarity unchanged (GREEN).
2026-05-28 17:12:20 +09:00

291 lines
9.3 KiB
C

/*
* 762_struct_abi_size — defensive lock for project #78. cstage once
* folded size(struct{i32,i32,i32}) to 12 while wwstage folded it to
* 16; the two checkers walked different layout formulas. After
* d4e500f (#169) introduced structabisize and the cgen ABI sites
* converged onto a single (off + maxalign - 1) & ~(maxalign - 1)
* formula on both sides, every layout-edge shape should agree —
* BOTH at the size(T) fold (cstage check.c:760 / wwstage check.ww
* astsize N_TSTRUCT) and at the register-RECV/RETURN ABI walker
* (cgen.c struct_arg_size / structabisize). This probe pins both.
*
* The fold and the ABI walker share the formula but live in two
* different sources: check.c:760 vs check.ww:958 (the language
* builtin) and cmd/w6c/cgen.c struct_arg_size vs
* selfhost/cmd/wcc/cgenutil.ww structabisize (the cgen ABI). Either
* can drift independently — locking only the runtime exit code via
* size(T) misses an ABI-walker regression that does not change the
* literal fold; locking only the .s byte-id misses a fold regression
* the cgen would happily honour. Both gates run per row.
*
* Expected size is COMPUTED from a (sz, aln) field table per row,
* not hardcoded — a stage-internal formula bump (rule 13) lands the
* change in compute_expected() and every row tracks. The probe must
* red the moment any source-of-truth drifts from the documented
* Hare-style layout: align(off, fa); off += fs; size = align(off,
* maxalign).
*
* Shape coverage targets the maxalign edges where the bug class
* lived:
* 1. {3xi32} — #78 canonical, maxalign 4, natural 12,
* ABI 12 (no round-up)
* 2. {i32,i32,i64} — maxalign 8, narrow lead, ABI 16
* 3. {i64,i32,i32} — maxalign 8 with sub-8 tail, ABI 16
* (the #169 round-up case)
* 4. {3xi16} — maxalign 2, natural 6, ABI 6
* 5. {i16,i64} — maxalign 8 with mid-record alignment
* padding (f2 must skip off 2→8)
* 6. {3xi64} — maxalign 8, all 8-wide, natural==ABI=24
*
* GATE POLARITY: this file must stay GREEN. A red here means either
* d4e500f (#169 structabisize) reverted, or one of the two layout
* walkers drifted from the formula.
*/
#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 field { int sz; int aln; };
struct row {
const char *label;
const char *src; /* must `return size(T): i32` */
int nfields;
struct field fields[8];
};
/* Hare / cstage check.c:701-760 / wwstage check.ww astsize N_TSTRUCT
* formula. Per CLAUDE.md rule 13 the expected size for each row is
* derived here, not literalled into the row table. */
static int
compute_expected(const struct field *f, int n)
{
int off = 0, maxalign = 1;
for (int i = 0; i < n; i++) {
int a = f[i].aln;
if (a > maxalign) maxalign = a;
off = (off + a - 1) & ~(a - 1);
off += f[i].sz;
}
return (off + maxalign - 1) & ~(maxalign - 1);
}
static const struct row rows[] = {
/* 1. {3xi32} — #78 canonical. cstage check.c:760 returned 12,
* wwstage formerly walked a slot-padded ladder yielding 16. The
* fold now uses astsize's (off + maxal - 1) & ~(maxal - 1) on
* both sides; expect 12. */
{ "i32_x3_maxalign4",
"type t = struct { a: i32, b: i32, c: i32 };\n"
"export fn main() i32 = { return size(t): i32; };\n",
3, {{4,4},{4,4},{4,4}} },
/* 2. {i32,i32,i64} — maxalign 8, off lands at 8 before the i64,
* total 16. */
{ "i32_i32_i64_lead_narrow",
"type t = struct { a: i32, b: i32, c: i64 };\n"
"export fn main() i32 = { return size(t): i32; };\n",
3, {{4,4},{4,4},{8,8}} },
/* 3. {i64,i32,i32} — natural extent 16, maxalign 8: the post-
* field-3 round-up is a no-op here, but the #169 commit added
* exactly this round-up so the structabisize walker matches
* cstage on the maxalign-8 sub-8-tail shape (cf. {i64,i32}). */
{ "i64_i32_i32_sub8_tail",
"type t = struct { a: i64, b: i32, c: i32 };\n"
"export fn main() i32 = { return size(t): i32; };\n",
3, {{8,8},{4,4},{4,4}} },
/* 4. {3xi16} — maxalign 2, natural 6. Guards against a future
* "round to 8" shortcut creeping back into either walker. */
{ "i16_x3_maxalign2",
"type t = struct { a: i16, b: i16, c: i16 };\n"
"export fn main() i32 = { return size(t): i32; };\n",
3, {{2,2},{2,2},{2,2}} },
/* 5. {i16,i64} — exercises a non-trivial mid-record align step:
* after f1 off=2, f2 must align off 2→8 before placing the i64.
* A missing `off = align(off, fa)` would land f2 at off=2 and
* size to 10 (or 16 only via the final maxalign round); either
* way it would diverge from the formula. {i32,i32,i64} (rows 2)
* already aligns at 8 by the natural off, so the align-step is
* a no-op there. */
{ "i16_i64_mid_pad",
"type t = struct { a: i16, b: i64 };\n"
"export fn main() i32 = { return size(t): i32; };\n",
2, {{2,2},{8,8}} },
/* 6. {3xi64} — natural==ABI, the maxalign-rounded formula must
* not double-round. */
{ "i64_x3_natural_24",
"type t = struct { a: i64, b: i64, c: i64 };\n"
"export fn main() i32 = { return size(t): i32; };\n",
3, {{8,8},{8,8},{8,8}} },
};
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/wcabi_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/wcabi_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
/* timeout 180 per repo convention (cf. 760, 761); test/run does
* not bound individual binaries. */
snprintf(cmd, sizeof cmd, "cd %s && timeout 180 %s build %s 2>/dev/null",
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;
}
/* asm_byte_identical — diff w6c vs w6c_ww text output. The cgen ABI
* walker (structabisize since #169 / d4e500f) feeds the struct-
* return + struct-arg push widths; a divergence on these shapes
* surfaces as a .s difference even when the size(T) fold still
* matches (the two walkers are independent SSoTs). */
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/wcabi_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/wcabi_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/wcabi_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "timeout 180 %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, "timeout 180 %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;
}
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
int rc = 0;
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);
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_abi_size: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
int want = compute_expected(rows[i].fields,
rows[i].nfields);
int got = run_driver(drivers[d].path, &rows[i], i);
total++;
if (got != want) {
fprintf(stderr,
"struct_abi_size[%s][%s]: size=%d want=%d\n",
drivers[d].name, rows[i].label,
got, 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_abi_size: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("struct_abi_size: %d/%d ok\n", total, total);
return 0;
}