Files
ww/test/wcc/994_w6c_ww.c
Hojun-Cho 4ae29d6949 test/wcc: c6 dual-path sep==combined soak gate (#46)
Make the sep-compile == combined-compile output equivalence a standing
regression test — the last criterion before M4 can delete the combined.ww
embedding model.

989_c6soak_run (phase-1): Tier-A/B + same-leaf collision soak. Two real
roots (utf8: encoding.utf8 dotted-path + strings; collide: strings.contains
+ bytes.contains) built both ways with isolated cold WW_PKGCACHE; asserts
combined-run-exit == sep-run-exit on both drivers, cs==ww per-pkg .s/.wwi,
and two distinct collision labels.

994_w6c_ww (phase-2): Tier-C folded into the existing w6c byte-id oracle.
A sep-built w6c emits byte-identical .s to a combined-built w6c on all five
bootstrap targets (w6c/wwdump/w6a/w6l/ww .combined.ww); .wwi is in the
compare set. The tool binaries themselves differ by a benign constant
link-layout delta — the oracle is tool OUTPUT, not tool-binary identity.

Test-only; the five wwstage binary pins are unchanged.
2026-06-17 00:17:25 +09:00

467 lines
14 KiB
C

/*
* 994_w6c_ww — phase-10 marker for the ww-side w6c port.
*
* w6c_ww is a thin packaging of selfhost/cmd/wcc/cgen.ww: it slurps a
* .ww file, runs lex+parse+cgen, and writes Plan 9 amd64 asm to the
* file given by -o. The same cgen is reached through `wwdump_ww -c`,
* so w6c_ww must produce byte-identical output to wwdump_ww -c on
* every program — this test pins that.
*
* (We do not diff against C-side `w6c` here because the C compiler has
* features the ww cgen has not yet ported — float compare, fn-address
* @symbol, indirect call. Test 990 probe 5 covers the C-vs-ww diff on
* the subset that the ww cgen handles today.)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <dirent.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return 1;
}
static const char *
absbin(void)
{
const char *b = getenv("BIN");
if (!b) b = "out/bin";
if (b[0] == '/') return b;
static char buf[2048];
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return NULL;
snprintf(buf, sizeof buf, "%s/%s", cwd, b);
return buf;
}
static int
slurp(const char *path, char **outbuf, size_t *outlen)
{
FILE *f = fopen(path, "rb");
if (!f) return -1;
fseek(f, 0, SEEK_END);
long n = ftell(f);
fseek(f, 0, SEEK_SET);
if (n < 0) { fclose(f); return -1; }
char *b = malloc((size_t)n + 1);
if (!b) { fclose(f); return -1; }
if (fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); fclose(f); return -1; }
b[n] = '\0';
fclose(f);
*outbuf = b;
*outlen = (size_t)n;
return 0;
}
static int
diff_one(const char *bin, const char *label, const char *src)
{
char ws[64], cs[64], cmd[2048];
snprintf(ws, sizeof ws, "/tmp/wwc6_%d_w.s", getpid());
snprintf(cs, sizeof cs, "/tmp/wwc6_%d_c.s", getpid());
snprintf(cmd, sizeof cmd, "timeout 180 %s/wwdump_ww -c %s > %s 2>/dev/null",
bin, src, ws);
if (runwait(cmd) != 0) {
fprintf(stderr, "w6c_ww FAIL: wwdump_ww -c errored on %s\n", label);
unlink(ws);
return -1;
}
snprintf(cmd, sizeof cmd, "timeout 180 %s/w6c_ww -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "w6c_ww FAIL: w6c_ww errored on %s\n", label);
unlink(ws); unlink(cs);
return -1;
}
char *bw = NULL, *bc = NULL;
size_t nw = 0, nc = 0;
int rc = 0;
if (slurp(ws, &bw, &nw) < 0 || slurp(cs, &bc, &nc) < 0) {
fprintf(stderr, "w6c_ww FAIL: cannot read .s for %s\n", label);
rc = -1;
} else if (nw != nc || memcmp(bw, bc, nw) != 0) {
fprintf(stderr, "w6c_ww FAIL: %s — wwdump_ww %zu vs w6c_ww %zu bytes\n",
label, nw, nc);
rc = -1;
}
free(bw); free(bc);
unlink(ws); unlink(cs);
return rc;
}
static int
write_file(const char *path, const char *content)
{
FILE *f = fopen(path, "wb");
if (!f) return -1;
fputs(content, f);
fclose(f);
return 0;
}
static int
files_eq(const char *a, const char *b)
{
char *ba = NULL, *bb = NULL;
size_t na = 0, nb = 0;
if (slurp(a, &ba, &na) < 0 || slurp(b, &bb, &nb) < 0) {
free(ba); free(bb);
return -1;
}
int eq = (na == nb && memcmp(ba, bb, na) == 0);
free(ba); free(bb);
return eq ? 0 : 1;
}
/* Tier-C oracle helper (rob-c6-spec §2 / amendment-B): a sep-built w6c at
* `sepw6c` must EMIT byte-identical asm to the combined-built `w6c_ww` on
* `target`. (We assert OUTPUT equivalence, never `cmp` the tool binaries —
* sep vs combined differ by a constant link-layout delta.) Returns 0 on
* byte-identical .s, non-zero otherwise. */
static int
emit_eq(const char *sepw6c, const char *w6c_ww, const char *target,
const char *label)
{
char a[64], b[64], cmd[4096];
snprintf(a, sizeof a, "/tmp/wwc6tc_%d_sep.s", getpid());
snprintf(b, sizeof b, "/tmp/wwc6tc_%d_comb.s", getpid());
snprintf(cmd, sizeof cmd, "timeout 240 %s -o %s %s 2>/dev/null",
sepw6c, a, target);
if (runwait(cmd) != 0) {
fprintf(stderr, "Tier-C FAIL: sep-w6c emit errored on %s\n", label);
unlink(a);
return 1;
}
snprintf(cmd, sizeof cmd, "timeout 240 %s -o %s %s 2>/dev/null",
w6c_ww, b, target);
if (runwait(cmd) != 0) {
fprintf(stderr, "Tier-C FAIL: combined-w6c_ww emit errored on %s\n",
label);
unlink(a); unlink(b);
return 1;
}
int rc = files_eq(a, b);
if (rc != 0)
fprintf(stderr, "Tier-C FAIL: %s — sep-out != combined-out "
"(M4 output-equivalence broken)\n", label);
unlink(a); unlink(b);
return rc == 0 ? 0 : 1;
}
/* cs==ww over a sep-build's per-package artifacts: every .s/.o/.wwi the
* cstage driver produced in `csdir` must be byte-identical to the wwstage
* driver's same-named file in `wwdir` (rule 10; the .wwi interface files
* enter the compare set per rob-c6-spec §2). Returns the mismatch count
* (≥1 also on an unreadable / empty cs sepwork). */
static int
cmp_sepwork(const char *csdir, const char *wwdir)
{
DIR *d = opendir(csdir);
if (!d) {
fprintf(stderr, "Tier-C FAIL: no cs sepwork %s\n", csdir);
return 1;
}
int bad = 0, seen = 0;
struct dirent *ent;
while ((ent = readdir(d)) != NULL) {
const char *nm = ent->d_name;
size_t nl = strlen(nm);
int is_s = (nl > 2 && strcmp(nm + nl - 2, ".s") == 0);
int is_o = (nl > 2 && strcmp(nm + nl - 2, ".o") == 0);
int is_wwi = (nl > 4 && strcmp(nm + nl - 4, ".wwi") == 0);
if (!is_s && !is_o && !is_wwi) continue;
seen++;
char a[2048], b[2048];
snprintf(a, sizeof a, "%s/%s", csdir, nm);
snprintf(b, sizeof b, "%s/%s", wwdir, nm);
if (files_eq(a, b) != 0) {
fprintf(stderr, "Tier-C FAIL: cs!=ww for %s (rule 10)\n", nm);
bad++;
}
}
closedir(d);
if (seen == 0) {
fprintf(stderr, "Tier-C FAIL: no .s/.o/.wwi in %s\n", csdir);
bad++;
}
return bad;
}
int
main(void)
{
const char *bin = absbin();
if (!bin) return 1;
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
/* In-source corpus: programs the ww-side cgen handles today.
* Mirrors probe 5 in 990_selfhost without the parts that drift
* against C w6c. */
struct { const char *label; const char *src; } progs[] = {
{ "ret42",
"fn main() i32 = { return 42; };" },
{ "add",
"fn add(a: i32, b: i32) i32 = { return a + b; };\n"
"fn main() i32 = { return add(7, 35); };" },
{ "sum_for",
"fn sum(n: i32) i32 = {\n"
" let s: i32 = 0;\n"
" let i: i32 = 0;\n"
" for (i < n) { s += i; i += 1; };\n"
" return s;\n"
"};\n"
"fn main() i32 = { return sum(10); };" },
{ "if_else",
"fn check(x: i32) i32 = {\n"
" if (x > 0) { return 1; };\n"
" if (x < 0) { return 100; };\n"
" return 0;\n"
"};\n"
"fn main() i32 = { return check(7); };" },
{ "recursion",
"fn fact(n: i32) i32 = {\n"
" if (n <= 1) { return 1; };\n"
" return n * fact(n - 1);\n"
"};\n"
"fn main() i32 = { return fact(5); };" },
{ "enum",
"type mode = enum u8 { R = 1, W = 2, RW = R | W };\n"
"fn main() i32 = {\n"
" let m: mode = mode.RW;\n"
" return m as i32;\n"
"};" },
{ "switch",
"fn classify(x: i32) i32 = {\n"
" switch (x) {\n"
" case 1, 2, 3: return 10;\n"
" case 7: return 70;\n"
" case: return 0;\n"
" };\n"
" return -1;\n"
"};\n"
"fn main() i32 = { return classify(2); };" },
{ "append",
"import os;\n"
"fn main() i32 = {\n"
" let s: []u8;\n"
" s.ptr = nil; s.len = 0; s.cap = 0;\n"
" append(s, 65u8, 66u8, 67u8);\n"
" return s.len: i32;\n"
"};" },
{ "append_spread",
"import os;\n"
"fn main() i32 = {\n"
" let src: []i64;\n"
" src.ptr = nil; src.len = 0; src.cap = 0;\n"
" append(src, 10i64, 20i64);\n"
" let dst: []i64;\n"
" dst.ptr = nil; dst.len = 0; dst.cap = 0;\n"
" append(dst, src...);\n"
" return dst.len: i32;\n"
"};" },
{ "forrange",
"import os;\n"
"fn main() i32 = {\n"
" let s: []u8;\n"
" s.ptr = nil; s.len = 0; s.cap = 0;\n"
" append(s, 10u8, 20u8, 30u8);\n"
" let total: i32 = 0;\n"
" for (let b .. s) { total += b: i32; };\n"
" return total;\n"
"};" },
{ "forrange_tuple",
"fn main() i32 = {\n"
" let buf: [4]i64;\n"
" buf[0] = 1i64; buf[1] = 10i64; buf[2] = 2i64; buf[3] = 20i64;\n"
" let s: [](i64, i64);\n"
" s.ptr = buf.ptr: *(i64, i64);\n"
" s.len = 2; s.cap = 2;\n"
" let total: i64 = 0i64;\n"
" for (let (k, v) .. s) { total += k + v; };\n"
" return total: i32;\n"
"};" },
/* #45: alloc-slice `!` form, []T element ≠ u8. */
{ "alloc_slice_rune_unw",
"package main;\n"
"import rt;\n"
"fn main() i32 = {\n"
" let s: []rune = alloc([], 8)!;\n"
" return s.cap;\n"
"};" },
/* #45: alloc-slice `?` form propagates nomem via the
* enclosing fn's tagged return. */
{ "alloc_slice_str_prop",
"package main;\n"
"import rt;\n"
"fn doit() (i32 | nomem) = {\n"
" let s: []str = alloc([], 4)?;\n"
" return s.cap: i32;\n"
"};\n"
"fn main() i32 = { return doit()!; };" },
{ NULL, NULL },
};
/* Source-tree corpus: the same .combined.ww files the bootstrap
* fixed point chews on. Confirms w6c_ww handles realistic inputs,
* not just hand-tailored ones. */
const char *combined_rel[] = {
"selfhost/cmd/wwdump/main.combined.ww",
"selfhost/cmd/w6a/main.combined.ww",
"selfhost/cmd/w6l/main.combined.ww",
"selfhost/cmd/ww/main.combined.ww",
NULL,
};
int fail = 0, n = 0;
for (int i = 0; progs[i].label; i++) {
char src[64];
snprintf(src, sizeof src, "/tmp/wwc6_%d_%d.ww", getpid(), i);
if (write_file(src, progs[i].src) != 0) { fail++; n++; continue; }
if (diff_one(bin, progs[i].label, src) != 0) fail++;
unlink(src);
n++;
}
for (int i = 0; combined_rel[i]; i++) {
char p[2048];
snprintf(p, sizeof p, "%s/%s", cwd, combined_rel[i]);
if (diff_one(bin, combined_rel[i], p) != 0) fail++;
n++;
}
/* #50: the check pass is wired into both wwdump_ww -c and
* w6c_ww in front of cgen. Pre-#50, a hard type error parsed
* cleanly and reached cgen, which would emit asm with a zero
* exit (silent miscompile). Pin the rejection by giving each
* driver a trivially-broken file and demanding non-zero exit. */
struct { const char *label; const char *src; } bad[] = {
{ "let_str_to_i32",
"package main;\n"
"fn main() i32 = {\n"
" let x: i32 = \"hello\";\n"
" return x;\n"
"};\n" },
{ NULL, NULL },
};
for (int i = 0; bad[i].label; i++) {
char src[64], wcmd[2048], dcmd[2048], s[64];
snprintf(src, sizeof src, "/tmp/wwc6_bad_%d_%d.ww", getpid(), i);
snprintf(s, sizeof s, "/tmp/wwc6_bad_%d_%d.s", getpid(), i);
if (write_file(src, bad[i].src) != 0) { fail++; n++; continue; }
snprintf(wcmd, sizeof wcmd,
"timeout 180 %s/w6c_ww -o %s %s >/dev/null 2>&1", bin, s, src);
snprintf(dcmd, sizeof dcmd,
"timeout 180 %s/wwdump_ww -c %s >%s 2>/dev/null", bin, src, s);
if (runwait(wcmd) == 0) {
fprintf(stderr,
"w6c_ww FAIL: %s slipped past check (silent miscompile)\n",
bad[i].label);
fail++;
}
if (runwait(dcmd) == 0) {
fprintf(stderr,
"wwdump_ww -c FAIL: %s slipped past check (silent miscompile)\n",
bad[i].label);
fail++;
}
unlink(src); unlink(s);
n++;
}
/* ---- Tier-C capstone (#46 c6, rob-c6-spec §2 / amendment-B) ----------
* Fold the dual-path soak into this bootstrap oracle: a w6c built by
* SEPARATE compilation must EMIT byte-identical .s to the combined-built
* w6c_ww on every real bootstrap input, and both driver stages must
* sep-produce byte-identical per-package .s/.o/.wwi (cs==ww on the real
* tool's sep artifacts). This is the load-bearing M4 proof — the
* combined-out==sep-out leg the bootstrap fixed point alone cannot reach.
* We assert OBSERVABLE OUTPUT, never `cmp` the tool binaries (sep vs
* combined differ by a constant link-layout delta). COLD (pid-keyed
* scratch, wiped); heavy (≈ one bootstrap leg) → this gate is phase-2. */
{
char inc[4096], cmd[16384];
snprintf(inc, sizeof inc,
"-I %s/lib -I %s/lib/ww -I %s/selfhost/cmd/wcc", cwd, cwd, cwd);
char w6c_ww[2048], root[2048];
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
snprintf(root, sizeof root, "%s/selfhost/cmd/w6c/main.ww", cwd);
struct { const char *drv, *tag; char prog[2048]; int ok; }
stg[] = { { "ww", "cs", {0}, 0 }, { "ww_ww", "ww", {0}, 0 } };
for (int s = 0; s < 2; s++) {
snprintf(stg[s].prog, sizeof stg[s].prog,
"/tmp/wwc6tc_%d_%s", getpid(), stg[s].tag);
/* Isolated per-(pid,stage) WW_PKGCACHE → COLD + race-free
* (rob-c6-spec §4): the default out/.pkgcache is shared with
* the other phase-2 gates' `ww build`s and would both perturb
* this gate and corrupt under concurrent writes. */
snprintf(cmd, sizeof cmd,
"WW_PKGCACHE='%s.cache' timeout 600 %s/%s build --sep "
"%s -o %s %s >/dev/null 2>&1",
stg[s].prog, bin, stg[s].drv, inc, stg[s].prog, root);
if (runwait(cmd) != 0) {
fprintf(stderr, "Tier-C FAIL: %s build --sep w6c\n",
stg[s].drv);
fail++;
} else stg[s].ok = 1;
n++;
}
/* OUTPUT equivalence on every real bootstrap input: each built
* sep-w6c vs the combined w6c_ww (the M4 combined-out==sep-out bar;
* cs==ww follows since both sep stages match the same reference). */
const char *targ[] = {
"selfhost/cmd/w6c/main.combined.ww",
"selfhost/cmd/wwdump/main.combined.ww",
"selfhost/cmd/w6a/main.combined.ww",
"selfhost/cmd/w6l/main.combined.ww",
"selfhost/cmd/ww/main.combined.ww",
NULL,
};
for (int s = 0; s < 2; s++) {
if (!stg[s].ok) continue;
for (int i = 0; targ[i]; i++) {
char t[2048], label[256];
snprintf(t, sizeof t, "%s/%s", cwd, targ[i]);
snprintf(label, sizeof label, "sep-%s emit %s",
stg[s].tag, targ[i]);
if (emit_eq(stg[s].prog, w6c_ww, t, label) != 0) fail++;
n++;
}
}
/* cs==ww on the real tool's sep .s/.o/.wwi (rule 10). */
if (stg[0].ok && stg[1].ok) {
char csdir[2176], wwdir[2176];
snprintf(csdir, sizeof csdir, "%s.sepwork", stg[0].prog);
snprintf(wwdir, sizeof wwdir, "%s.sepwork", stg[1].prog);
fail += cmp_sepwork(csdir, wwdir);
n++;
}
for (int s = 0; s < 2; s++) {
snprintf(cmd, sizeof cmd, "rm -rf %s %s.sepwork %s.cache",
stg[s].prog, stg[s].prog, stg[s].prog);
runwait(cmd);
}
}
if (fail) {
fprintf(stderr, "w6c_ww: %d/%d diff(s) failed\n", fail, n);
return 1;
}
printf("w6c_ww: byte-identical to wwdump_ww -c on %d corpus inputs "
"(in-source + selfhost combined.ww) + Tier-C: sep-built w6c emits "
"== combined w6c_ww on all bootstrap inputs, cs==ww sep .s/.o/.wwi\n",
n);
return 0;
}