test: port the c6 sep soak to ww; retire 989_c6soak_run

This commit is contained in:
2026-08-08 14:50:07 +09:00
parent c8131d773e
commit e1bb49fb54
3 changed files with 162 additions and 331 deletions

View File

@@ -421,7 +421,8 @@ MISC_WW_TARGETS = $(MISC_WW_TESTS:%=wwtest/%)
# test/sep: they run under test-compiler.
TOOL_WW_TESTS = test/tool/rejects_test.ww test/tool/driver_test.ww \
test/tool/wwdump_test.ww test/tool/ffi_test.ww \
test/tool/attest_test.ww test/tool/wwirune_test.ww
test/tool/attest_test.ww test/tool/wwirune_test.ww \
test/tool/c6soak_test.ww
TOOL_WW_TARGETS = $(TOOL_WW_TESTS:%=wwtest/%)
BOOTSTRAP_WRAPPER_SOURCES = test/wcc/950_selfcheck.c \
test/wcc/991_w6a_ww.c \

160
test/tool/c6soak_test.ww Normal file
View File

@@ -0,0 +1,160 @@
package c6soak_test;
// M3-tail commit-6 separate-compilation soak gate (#46 c6,
// rob-c6-spec.md Tier-A/B + section-3 collision). Port of the retired
// native carrier test/wcc/989_c6soak_run.c; every assertion
// preserved at the carrier's own scale (real library chains, both
// driver stages, full per-file artifact compare).
//
// utf8 — a root importing encoding.utf8 (DOTTED path #57) + strings
// counts the runes of "héllo" via a decode/next match loop:
// build+run exit 5 on both stages. The C fixture embedded the e-acute
// as raw UTF-8 bytes; the generated source uses the \u00e9 escape,
// the same string bytes after lexing (escape decode owned by
// test/lang).
//
// collide — strings.contains AND bytes.contains, the SAME leaf
// exported by two packages (rob-c6-spec section 3, the #48/#49 class
// on real code): both true, exit 7 on both stages.
//
// cs==ww (rule 10) — EVERY per-package .s and .wwi in the cstage
// sepwork must be byte-identical to the wwstage same-named file, and
// at least ONE file must be compared (an empty sepwork would pass the
// loop vacuously).
//
// section-3 non-vacuity (collide only) — the cstage strings.s defines
// `TEXT strings.contains` and bytes.s `TEXT bytes.contains`: two
// genuinely DISTINCT path-qualified symbols, disambiguation real, not
// luck.
//
// The heavy Tier-C capstone (tool self-compile equivalence) stays
// folded into the 990-997 bootstrap oracle (994_w6c_ww), not here
// (rob-c6-spec section 2 / amendment-B).
import os;
import os.exec;
import strings;
import testenv;
import time;
fn fail(label: str, why: str) void = {
let m: str = strings.concat("c6soak FAIL: ", label, " -- ", why,
"\n");
os.write(2, m.ptr, m.len: u64);
assert(false);
};
fn tmo() time.duration = {
return (240i64 * (time.second: i64)): time.duration;
};
fn runcode(dir: str, name: str, argv: []str) i32 = {
let co: testenv.commandout;
testenv.runcommand(dir, dir, name, argv, tmo(), &co);
if (co.termination != exec.termination.EXIT) { return -1; };
return co.code;
};
// every .s/.wwi in csdir byte-identical in wwdir; at least one seen
fn cmpsepwork(label: str, csdir: str, wwdir: str) void = {
let names: []str = testenv.listdir(csdir);
let seen: i32 = 0;
let i: i32 = 0;
for (i < names.len) {
if (strings.hassuffix(names[i], ".s")
|| strings.hassuffix(names[i], ".wwi")) {
seen += 1;
if (!testenv.same(
testenv.readfile(strings.concat(csdir, "/", names[i])),
testenv.readfile(strings.concat(wwdir, "/", names[i])))) {
fail(label, strings.concat("cs!=ww for ", names[i],
" (rule 10)"));
};
};
i += 1;
};
if (seen == 0) {
fail(label, "no .s/.wwi in the cs sepwork (vacuous compare)");
};
};
fn soak(label: str, src: str, expect: i32, collide: bool) void = {
let td: str = testenv.fresh();
let rootww: str = strings.concat(td, "/", label, ".ww");
testenv.writefile(rootww, src);
// the real include set the sep producer needs for the lib +
// frontend package graph
let libinc: str = strings.concat(testenv.repo(), "/lib");
let wwinc: str = strings.concat(testenv.repo(), "/lib/ww");
let wccinc: str = strings.concat(testenv.repo(), "/selfhost/cmd/wcc");
let drvs: []str = ["ww", "ww_ww"];
let tags: []str = ["cs", "ww"];
let s: i32 = 0;
for (s < 2) {
// `.bin` infix keeps the stage-`ww` output distinct from the
// <label>.ww SOURCE (else the build clobbers its own input)
let prog: str = strings.concat(td, "/", label, ".", tags[s],
".bin");
let av: []str = [testenv.driver(drvs[s]), "build",
"-I", libinc, "-I", wwinc, "-I", wccinc, "-o", prog, rootww];
if (runcode(td, strings.concat("build_", tags[s]), av) != 0) {
fail(label, strings.concat(drvs[s], " build failed"));
};
let rav: []str = [prog];
if (runcode(td, strings.concat("run_", tags[s]), rav) != expect) {
fail(label, strings.concat(drvs[s],
" run exit != expected"));
};
s += 1;
};
let csdir: str = strings.concat(td, "/", label, ".cs.bin.sepwork");
let wwdir: str = strings.concat(td, "/", label, ".ww.bin.sepwork");
cmpsepwork(label, csdir, wwdir);
if (collide) {
if (!testenv.has(testenv.readfile(strings.concat(csdir,
"/strings.s")), "TEXT strings.contains")) {
fail(label, strings.concat("strings.s lacks `TEXT ",
"strings.contains` (section-3 disambiguation)"));
};
if (!testenv.has(testenv.readfile(strings.concat(csdir,
"/bytes.s")), "TEXT bytes.contains")) {
fail(label, strings.concat("bytes.s lacks `TEXT ",
"bytes.contains` (section-3 disambiguation)"));
};
};
testenv.clean(td);
};
@test fn utf8() void = {
soak("utf8", strings.concat(
"package main;\n",
"import encoding.utf8;\n",
"import strings;\n",
"fn main() i32 = {\n",
" let d = utf8.decode(strings.toutf8(\"h\\u00e9llo\"));\n",
" let n: i32 = 0;\n",
" for (true) {\n",
" match (utf8.next(&d)) {\n",
" case rune => n += 1;\n",
" case => break;\n",
" };\n",
" };\n",
" return n;\n",
"};\n"), 5, false);
};
@test fn collide() void = {
soak("collide", strings.concat(
"package main;\n",
"import strings;\n",
"import bytes;\n",
"fn main() i32 = {\n",
" let s = strings.contains(\"hello\", \"ell\");\n",
" let b = bytes.contains(strings.toutf8(\"hello\"), ",
"strings.toutf8(\"ell\"));\n",
" if (s && b) { return 7; };\n",
" return 1;\n",
"};\n"), 7, true);
};

View File

@@ -1,330 +0,0 @@
/*
* 989_c6soak_run — M3-tail commit-6 separate-compilation soak gate
* (#46 c6, rob-c6-spec.md Tier-A/B + §3 collision).
*
* The standing real-code regression gate for separate compilation. The
* oracle is observable output plus byte-identical per-package compiler
* artifacts across the C and WW driver stages.
*
* Targets (real library chains):
* - utf8 : encoding.utf8 (DOTTED-path #57) + strings → decode + count
* runes ("héllo" = 5). Exercises the multi-component import
* resolution by the full dotted path.
* - collide: strings.contains AND bytes.contains — the SAME leaf `contains`
* exported by two packages (rob-c6-spec §3, the #48/#49 class
* on real code). The driver path-qualifies them to DISTINCT
* symbols;
* the program computes both correct results; the §3 non-vacuity
* check below greps the per-pkg .s to prove the two labels are
* genuinely distinct (disambiguation is real, not luck).
*
* Asserts per target (direct per-stage builds use fresh output stems under
* an invocation-owned root; retained scratch is inspected, then removed by
* final carrier cleanup):
* 1. RUN-EXIT: both driver stages build and run to the expected result.
* 2. cs==ww (rule 10): the cstage `ww` and wwstage `ww_ww` sep-drivers emit
* byte-identical per-package .s and .wwi (the .wwi enters the compare
* set — rob-c6-spec). Both also link to a binary that runs to `expect`.
* 3. §3 NON-VACUITY (collide only): the per-package strings.s defines
* `TEXT strings.contains` and bytes.s defines `TEXT bytes.contains` —
* two DISTINCT symbols. The w6l dup-gate stays silent (no collision);
* both resolve; the program returns 7.
*
* The heavy Tier-C capstone (real tool w6c/wwdump self-compile output
* equivalence) is FOLDED into the 990-997 bootstrap oracle (994_w6c_ww),
* not bolted on here (rob-c6-spec §2 / amendment-B).
*
* All intermediates are `-o`-redirected to /tmp for isolation. Models
* 989_sepbuild_run.c / 989_sepdotpath_run.c conventions; 989 prefix per
* the sep-gate precedent.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.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
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;
}
/* 0 if `needle` occurs in the file at `path`, 1 if absent, -1 on read err. */
static int
file_contains(const char *path, const char *needle)
{
char *b = NULL;
size_t n = 0;
if (slurp(path, &b, &n) < 0) return -1;
int found = (strstr(b, needle) != NULL);
free(b);
return found ? 0 : 1;
}
static int
write_file(const char *path, const char *body)
{
FILE *f = fopen(path, "wb");
if (!f) return -1;
fputs(body, f);
fclose(f);
return 0;
}
/* cs==ww over a sep-build's per-package output: every .s/.wwi the cstage
* driver produced in `csdir` must be byte-identical to the wwstage
* driver's same-named file in `wwdir`. Returns the count of mismatches. */
static int
cmp_sepwork(const char *csdir, const char *wwdir, const char *label)
{
DIR *d = opendir(csdir);
if (!d) {
fprintf(stderr, "c6soak FAIL: %s — no cs sepwork %s\n", label, 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_wwi = (nl > 4 && strcmp(nm + nl - 4, ".wwi") == 0);
if (!is_s && !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, "c6soak FAIL: %s — cs!=ww for %s (rule 10)\n",
label, nm);
bad++;
}
}
closedir(d);
/* An existing-but-empty sepwork would pass the cs==ww loop vacuously
* (no per-pkg .s/.wwi to compare); the utf8 root has no other cs==ww
* anchor, so demand at least one compared file. */
if (seen == 0) {
fprintf(stderr, "c6soak FAIL: %s — no .s/.wwi in %s\n", label, csdir);
bad++;
}
return bad;
}
struct root {
const char *label;
const char *src;
int expect;
int collide; /* run the §3 distinct-label non-vacuity check */
};
static struct root roots[] = {
{ "utf8",
"package main;\n"
"import encoding.utf8;\n"
"import strings;\n"
"fn main() i32 = {\n"
" let d = utf8.decode(strings.toutf8(\"h\303\251llo\"));\n"
" let n: i32 = 0;\n"
" for (true) {\n"
" match (utf8.next(&d)) {\n"
" case rune => n += 1;\n"
" case => break;\n"
" };\n"
" };\n"
" return n;\n"
"};\n", 5, 0 },
{ "collide",
"package main;\n"
"import strings;\n"
"import bytes;\n"
"fn main() i32 = {\n"
" let s = strings.contains(\"hello\", \"ell\");\n"
" let b = bytes.contains(strings.toutf8(\"hello\"), strings.toutf8(\"ell\"));\n"
" if (s && b) { return 7; };\n"
" return 1;\n"
"};\n", 7, 1 },
{ NULL, NULL, 0, 0 },
};
int
main(void)
{
const char *bin = absbin();
if (!bin) return 1;
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
/* The real include set the sep producer needs to resolve the lib +
* frontend package graph (verified live). */
char inc[4096];
snprintf(inc, sizeof inc, "-I %s/lib -I %s/lib/ww -I %s/selfhost/cmd/wcc",
cwd, cwd, cwd);
char td[] = "/tmp/wwc6soak_XXXXXX";
char cmd[8192];
int fail = 0;
int source_created[2] = { 0, 0 };
int stage_started[2][2] = { { 0, 0 }, { 0, 0 } };
if (mkdtemp(td) == NULL) {
perror("wwc6soak: mkdtemp");
return 1;
}
for (int i = 0; roots[i].label; i++) {
struct root *r = &roots[i];
char rootww[1024];
snprintf(rootww, sizeof rootww, "%s/%s.ww", td, r->label);
if (write_file(rootww, r->src)) { fail++; continue; }
source_created[i] = 1;
/* Both stages produce every per-package
* .s/.wwi artifact this gate inspects. */
struct { const char *drv, *tag; char prog[1024]; int rc; }
stg[] = { { "ww", "cs", {0}, -1 }, { "ww_ww", "ww", {0}, -1 } };
int built = 1;
for (int s = 0; s < 2; s++) {
/* `.bin` infix keeps the stage-`ww` output distinct from the
* `<label>.ww` SOURCE (else the build clobbers its own input). */
snprintf(stg[s].prog, sizeof stg[s].prog, "%s/%s.%s.bin",
td, r->label, stg[s].tag);
snprintf(cmd, sizeof cmd,
"timeout 240 %s/%s build "
"%s -o %s %s >/dev/null 2>&1",
bin, stg[s].drv, inc,
stg[s].prog, rootww);
stage_started[i][s] = 1;
if (runwait(cmd) != 0) {
fprintf(stderr, "c6soak FAIL: %s %s build\n",
r->label, stg[s].drv);
fail++;
built = 0;
continue;
}
stg[s].rc = runwait(stg[s].prog);
}
if (!built) continue;
/* 1. Runtime behavior agrees across both stages. */
if (stg[0].rc != r->expect || stg[1].rc != r->expect) {
fprintf(stderr, "c6soak FAIL: %s exits cs=%d ww=%d "
"(expected %d)\n", r->label, stg[0].rc, stg[1].rc,
r->expect);
fail++;
}
/* 2. cs==ww (rule 10): per-package .s/.wwi byte-identical. */
char csdir[1024], wwdir[1024];
snprintf(csdir, sizeof csdir, "%s/%s.cs.bin.sepwork", td, r->label);
snprintf(wwdir, sizeof wwdir, "%s/%s.ww.bin.sepwork", td, r->label);
fail += cmp_sepwork(csdir, wwdir, r->label);
/* 3. §3 non-vacuity: distinct same-leaf labels in distinct .s. */
if (r->collide) {
char ss[2048], bs[2048];
snprintf(ss, sizeof ss, "%s/strings.s", csdir);
snprintf(bs, sizeof bs, "%s/bytes.s", csdir);
if (file_contains(ss, "TEXT strings.contains") != 0) {
fprintf(stderr, "c6soak FAIL: %s — strings.s lacks "
"`TEXT strings.contains` (§3 disambiguation)\n",
r->label);
fail++;
}
if (file_contains(bs, "TEXT bytes.contains") != 0) {
fprintf(stderr, "c6soak FAIL: %s — bytes.s lacks "
"`TEXT bytes.contains` (§3 disambiguation)\n",
r->label);
fail++;
}
}
}
{
int cleanfail = 0;
char path[1024];
const char *labels[] = { "utf8", "collide" };
const char *tags[] = { "cs", "ww" };
for (int i = 0; i < 2; i++) {
for (int s = 0; s < 2; s++) {
if (!stage_started[i][s]) continue;
snprintf(path, sizeof path, "%s/%s.%s.bin.sepwork",
td, labels[i], tags[s]);
snprintf(cmd, sizeof cmd, "rm -rf %s", path);
if (runwait(cmd) != 0) cleanfail = 1;
snprintf(path, sizeof path, "%s/%s.%s.bin",
td, labels[i], tags[s]);
if (unlink(path) != 0 && errno != ENOENT) cleanfail = 1;
}
if (source_created[i]) {
snprintf(path, sizeof path, "%s/%s.ww", td, labels[i]);
if (unlink(path) != 0 && errno != ENOENT) cleanfail = 1;
}
}
if (rmdir(td) != 0) cleanfail = 1;
if (cleanfail) {
fprintf(stderr, "c6soak FAIL: temporary cleanup failed\n");
fail++;
}
}
if (fail) {
fprintf(stderr, "c6soak: %d check(s) failed\n", fail);
return 1;
}
printf("c6soak: separate-compilation behavior on real chains "
"(encoding.utf8 dotted + strings/bytes same-leaf `contains`) — "
"cs==ww per-pkg .s/.wwi + §3 distinct labels\n");
return 0;
}