test/wcc: carrier ownership repair and driver-contract adaptation
Every surviving carrier now owns its artifacts: checked mkdir/mkdtemp/ fopen acquisition, one all-exit cleanup funnel per carrier, ENOENT- tolerant checked unlinks, exact-path deletion (rm -rf only for an owned pid-keyed dir or a .sepwork beneath one), and cleanup failure fails a passing carrier without overwriting its diagnostic. In the same pass the carriers adapt to the driver contract this branch lands: --sep and WW_PKGCACHE are gone, -S and the /tmp/ww_run_<pid> scratch contract are asserted, and rows whose runtime or reject coverage moved to test/wcc/data fixtures or test/lang @test owners are trimmed to the byte/artifact/diagnostic observations only they can make. Repair and adaptation ride together because most files interleave both in the same hunks; splitting would manufacture intermediate carrier states that never existed and cannot run against either driver.
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
static int
|
||||
@@ -101,56 +102,152 @@ static const struct row rows[] = {
|
||||
0 },
|
||||
};
|
||||
|
||||
/* write_file — create `dir/rel` (mkdir -p its parents) with `content`. */
|
||||
/* write_file — create `dir/rel` in an already-owned package tree. */
|
||||
static int
|
||||
write_file(const char *dir, const char *rel, const char *content)
|
||||
write_file(const char *dir, const char *rel, const char *content, int *acquired)
|
||||
{
|
||||
char path[512], cmd[1024];
|
||||
char path[512];
|
||||
*acquired = 0;
|
||||
snprintf(path, sizeof path, "%s/%s", dir, rel);
|
||||
char parent[512];
|
||||
snprintf(parent, sizeof parent, "%s", path);
|
||||
char *slash = strrchr(parent, '/');
|
||||
if (slash) {
|
||||
*slash = '\0';
|
||||
snprintf(cmd, sizeof cmd, "mkdir -p '%s'", parent);
|
||||
if (runwait(cmd) != 0) return -1;
|
||||
}
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(content, f);
|
||||
fclose(f);
|
||||
return 0;
|
||||
*acquired = 1;
|
||||
int rc = fputs(content, f) == EOF ? -1 : 0;
|
||||
if (fclose(f) != 0) rc = -1;
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* layout — lay out the row's package tree + root under `dir`. */
|
||||
struct layout_owned {
|
||||
int root, a, amath, b, bmath, one, two;
|
||||
int files[6], mainfile;
|
||||
};
|
||||
|
||||
/* Remove exactly the files and semantic directories acquired for one row. */
|
||||
static int
|
||||
layout(const char *dir, const struct row *r)
|
||||
cleanup_layout(const char *dir, const struct row *r, int byteid,
|
||||
const struct layout_owned *owned, const int started[2])
|
||||
{
|
||||
char cmd[1024];
|
||||
snprintf(cmd, sizeof cmd, "rm -rf '%s' && mkdir -p '%s'", dir, dir);
|
||||
if (runwait(cmd) != 0) return -1;
|
||||
char path[512], cmd[1024];
|
||||
int fail = 0;
|
||||
if (byteid) {
|
||||
const char *tags[] = { "cstage", "wwstage" };
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (!started[i]) continue;
|
||||
snprintf(path, sizeof path, "%s/%s.sepwork", dir, tags[i]);
|
||||
snprintf(cmd, sizeof cmd, "rm -rf '%s'", path);
|
||||
if (runwait(cmd) != 0) fail = 1;
|
||||
snprintf(path, sizeof path, "%s/%s", dir, tags[i]);
|
||||
if (unlink(path) != 0 && access(path, F_OK) == 0) fail = 1;
|
||||
snprintf(path, sizeof path, "%s/%s.s", dir, tags[i]);
|
||||
if (unlink(path) != 0 && access(path, F_OK) == 0) fail = 1;
|
||||
}
|
||||
} else {
|
||||
if (started[0]) {
|
||||
snprintf(path, sizeof path, "%s/main.sepwork", dir);
|
||||
snprintf(cmd, sizeof cmd, "rm -rf '%s'", path);
|
||||
if (runwait(cmd) != 0) fail = 1;
|
||||
snprintf(path, sizeof path, "%s/main", dir);
|
||||
if (unlink(path) != 0 && access(path, F_OK) == 0) fail = 1;
|
||||
}
|
||||
}
|
||||
if (owned->mainfile) {
|
||||
snprintf(path, sizeof path, "%s/main.ww", dir);
|
||||
if (unlink(path) != 0 && access(path, F_OK) == 0) fail = 1;
|
||||
}
|
||||
for (int k = 0; r->files[k].path; k++) {
|
||||
if (!owned->files[k]) continue;
|
||||
snprintf(path, sizeof path, "%s/%s", dir, r->files[k].path);
|
||||
if (unlink(path) != 0 && access(path, F_OK) == 0) fail = 1;
|
||||
}
|
||||
if (owned->amath) {
|
||||
snprintf(path, sizeof path, "%s/a/math", dir);
|
||||
if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1;
|
||||
}
|
||||
if (owned->a) {
|
||||
snprintf(path, sizeof path, "%s/a", dir);
|
||||
if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1;
|
||||
}
|
||||
if (owned->bmath) {
|
||||
snprintf(path, sizeof path, "%s/b/math", dir);
|
||||
if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1;
|
||||
}
|
||||
if (owned->b) {
|
||||
snprintf(path, sizeof path, "%s/b", dir);
|
||||
if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1;
|
||||
}
|
||||
if (owned->one) {
|
||||
snprintf(path, sizeof path, "%s/one", dir);
|
||||
if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1;
|
||||
}
|
||||
if (owned->two) {
|
||||
snprintf(path, sizeof path, "%s/two", dir);
|
||||
if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1;
|
||||
}
|
||||
if (owned->root && rmdir(dir) != 0) fail = 1;
|
||||
return fail ? -1 : 0;
|
||||
}
|
||||
|
||||
/* layout — acquire and lay out the row's package tree + root under `dir`. */
|
||||
static int
|
||||
layout(const char *dir, const struct row *r, struct layout_owned *owned)
|
||||
{
|
||||
char path[512];
|
||||
memset(owned, 0, sizeof *owned);
|
||||
if (mkdir(dir, 0755) != 0) return -1;
|
||||
owned->root = 1;
|
||||
snprintf(path, sizeof path, "%s/a", dir);
|
||||
if (mkdir(path, 0755) != 0) goto fail;
|
||||
owned->a = 1;
|
||||
snprintf(path, sizeof path, "%s/a/math", dir);
|
||||
if (mkdir(path, 0755) != 0) goto fail;
|
||||
owned->amath = 1;
|
||||
snprintf(path, sizeof path, "%s/b", dir);
|
||||
if (mkdir(path, 0755) != 0) goto fail;
|
||||
owned->b = 1;
|
||||
snprintf(path, sizeof path, "%s/b/math", dir);
|
||||
if (mkdir(path, 0755) != 0) goto fail;
|
||||
owned->bmath = 1;
|
||||
snprintf(path, sizeof path, "%s/one", dir);
|
||||
if (mkdir(path, 0755) != 0) goto fail;
|
||||
owned->one = 1;
|
||||
snprintf(path, sizeof path, "%s/two", dir);
|
||||
if (mkdir(path, 0755) != 0) goto fail;
|
||||
owned->two = 1;
|
||||
for (int k = 0; r->files[k].path; k++)
|
||||
if (write_file(dir, r->files[k].path, r->files[k].content) != 0)
|
||||
return -1;
|
||||
return write_file(dir, "main.ww", r->root);
|
||||
if (write_file(dir, r->files[k].path, r->files[k].content,
|
||||
&owned->files[k]) != 0)
|
||||
goto fail;
|
||||
if (write_file(dir, "main.ww", r->root, &owned->mainfile) != 0) goto fail;
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
{
|
||||
int started[2] = { 0, 0 };
|
||||
if (cleanup_layout(dir, r, 0, owned, started) != 0)
|
||||
fprintf(stderr, "m1usehint_run: temporary cleanup failed during setup\n");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* run_build — build+run the row's root via `driver`; return the binary's
|
||||
* exit code, or -1 on a build failure. */
|
||||
static int
|
||||
run_build(const char *driver, const struct row *r, int i)
|
||||
run_build(const char *driver, const struct row *r, int i, int *cleanup_failed)
|
||||
{
|
||||
char dir[64], cmd[2048];
|
||||
struct layout_owned owned;
|
||||
int started[2] = { 0, 0 };
|
||||
*cleanup_failed = 0;
|
||||
snprintf(dir, sizeof dir, "/tmp/m1usehint_%d_%d", getpid(), i);
|
||||
if (layout(dir, r) != 0) return -2;
|
||||
if (layout(dir, r, &owned) != 0) return -2;
|
||||
|
||||
/* #93 sep layout: `--sep -o main` emits per-unit asm under
|
||||
/* #93 sep layout: `-o main` emits per-unit asm under
|
||||
* main.sepwork/ (each dir-pkg in its own <pkgpath>.s); binary stays
|
||||
* <dir>/main. WW_PKGCACHE pinned under <dir>; the whole tree is
|
||||
* rm -rf'd below, so the scratch dies with it. */
|
||||
* <dir>/main. The whole tree is removed below. */
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"cd '%s' && WW_PKGCACHE='%s/pkgc' %s build --sep -o main "
|
||||
"main.ww 2>/dev/null", dir, dir, driver);
|
||||
"cd '%s' && %s build -o main "
|
||||
"main.ww 2>/dev/null", dir, driver);
|
||||
started[0] = 1;
|
||||
int brc = runwait(cmd);
|
||||
|
||||
int got = -1;
|
||||
@@ -159,40 +256,46 @@ run_build(const char *driver, const struct row *r, int i)
|
||||
snprintf(outbin, sizeof outbin, "%s/main", dir);
|
||||
got = runwait(outbin);
|
||||
}
|
||||
snprintf(cmd, sizeof cmd, "rm -rf '%s'", dir);
|
||||
runwait(cmd);
|
||||
if (cleanup_layout(dir, r, 0, &owned, started) != 0) {
|
||||
*cleanup_failed = 1;
|
||||
}
|
||||
return brc == 0 ? got : -1;
|
||||
}
|
||||
|
||||
/* byteid — build the leaf_collision unit with both drivers and cmp the
|
||||
* emitted combined .s (rule-10). Returns 0 on byte-identical, nonzero else. */
|
||||
* concatenated per-package .s (rule 10). Returns 0 on byte-identical. */
|
||||
static int
|
||||
byteid(const char *cdrv, const char *wdrv, const struct row *r)
|
||||
byteid(const char *cdrv, const char *wdrv, const struct row *r,
|
||||
int *cleanup_failed)
|
||||
{
|
||||
char dir[64], cmd[2048];
|
||||
char cstem[80], wstem[80], cs[96], ws[96];
|
||||
char cstem[128], wstem[128], cs[160], ws[160];
|
||||
struct layout_owned owned;
|
||||
int started[2] = { 0, 0 };
|
||||
*cleanup_failed = 0;
|
||||
snprintf(dir, sizeof dir, "/tmp/m1usehint_bid_%d", getpid());
|
||||
if (layout(dir, r) != 0) return -1;
|
||||
snprintf(cstem, sizeof cstem, "/tmp/m1usehint_c_%d", getpid());
|
||||
snprintf(wstem, sizeof wstem, "/tmp/m1usehint_w_%d", getpid());
|
||||
if (layout(dir, r, &owned) != 0) return -1;
|
||||
snprintf(cstem, sizeof cstem, "%s/cstage", dir);
|
||||
snprintf(wstem, sizeof wstem, "%s/wwstage", dir);
|
||||
snprintf(cs, sizeof cs, "%s.s", cstem);
|
||||
snprintf(ws, sizeof ws, "%s.s", wstem);
|
||||
|
||||
/* #93 sep layout: each stage emits per-unit asm under <stem>.sepwork/
|
||||
* (a.math.s, b.math.s, one.s, two.s, __root.s). The rule-10 byte-id
|
||||
* gate concats each tree's per-unit *.s (sorted glob order is
|
||||
* deterministic) into a flat <stem>.s and cmps the two. WW_PKGCACHE
|
||||
* is pinned per-stem so the shared out/.pkgcache stays untouched. */
|
||||
* deterministic) into a flat <stem>.s and cmps the two. */
|
||||
int rc = -1;
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"cd '%s' && WW_PKGCACHE='%s.pkgc' %s build --sep -o '%s' "
|
||||
"cd '%s' && %s build -o '%s' "
|
||||
"main.ww 2>/dev/null && cat '%s.sepwork/'*.s > '%s'",
|
||||
dir, cstem, cdrv, cstem, cstem, cs);
|
||||
dir, cdrv, cstem, cstem, cs);
|
||||
started[0] = 1;
|
||||
int cb = runwait(cmd);
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"cd '%s' && WW_PKGCACHE='%s.pkgc' %s build --sep -o '%s' "
|
||||
"cd '%s' && %s build -o '%s' "
|
||||
"main.ww 2>/dev/null && cat '%s.sepwork/'*.s > '%s'",
|
||||
dir, wstem, wdrv, wstem, wstem, ws);
|
||||
dir, wdrv, wstem, wstem, ws);
|
||||
started[1] = 1;
|
||||
int wb = runwait(cmd);
|
||||
if (cb == 0 && wb == 0) {
|
||||
snprintf(cmd, sizeof cmd, "cmp -s '%s' '%s'", cs, ws);
|
||||
@@ -201,8 +304,9 @@ byteid(const char *cdrv, const char *wdrv, const struct row *r)
|
||||
fprintf(stderr, "m1usehint_run: byte-id build failed "
|
||||
"(cstage=%d wwstage=%d)\n", cb, wb);
|
||||
}
|
||||
snprintf(cmd, sizeof cmd, "rm -rf '%s' '%s'* '%s'*", dir, cstem, wstem);
|
||||
runwait(cmd);
|
||||
if (cleanup_layout(dir, r, 1, &owned, started) != 0) {
|
||||
*cleanup_failed = 1;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
@@ -241,24 +345,37 @@ main(void)
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
int got = run_build(drivers[d].drv, &rows[i], i);
|
||||
int cleanfail = 0;
|
||||
int got = run_build(drivers[d].drv, &rows[i], i,
|
||||
&cleanfail);
|
||||
if (got != rows[i].want_exit) {
|
||||
fprintf(stderr, "m1usehint_run[%s][%s]: exit=%d "
|
||||
"want=%d\n", drivers[d].name, rows[i].label,
|
||||
got, rows[i].want_exit);
|
||||
fail++;
|
||||
}
|
||||
if (cleanfail) {
|
||||
fprintf(stderr, "m1usehint_run[%s][%s]: temporary cleanup "
|
||||
"failed\n", drivers[d].name, rows[i].label);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* rule-10 byte-id gate on the leaf_collision unit. */
|
||||
if (access(wdrv, X_OK) == 0) {
|
||||
total++;
|
||||
if (byteid(cdrv, wdrv, &rows[0]) != 0) {
|
||||
int cleanfail = 0;
|
||||
if (byteid(cdrv, wdrv, &rows[0], &cleanfail) != 0) {
|
||||
fprintf(stderr, "m1usehint_run: cstage.s != wwstage.s "
|
||||
"(byte-id break)\n");
|
||||
fail++;
|
||||
}
|
||||
if (cleanfail) {
|
||||
fprintf(stderr, "m1usehint_run: byte-id temporary cleanup "
|
||||
"failed\n");
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
|
||||
Reference in New Issue
Block a user