Files
ww/test/wcc/989_libprecond_abort.c
Hojun-Cho ce3a25a0b4 test: contain sepwork scratch per-driver tmpdir, fix /tmp+in-repo leak (#8)
The wcc test drivers ran `ww build <bare-/tmp src>` with no -o, so the
compiler's <stem>.sepwork scratch landed beside the source and was never
cleaned: unbounded /tmp growth (2195 stale dirs observed) that fills tmpfs
and fabricates phantom test failures + silent harness aborts, and for
in-repo fixture builds leaked .sepwork into the tracked tree.

Each leaking build now writes its source + output inside a per-invocation
tmpdir, passes -o <tmpdir>/<stem> so the .sepwork lands inside it, and
rm -rf's the tmpdir on every exit path -- including fopen-fail and the
expected-fail reject builds (scratch is mkdir'd before the build can fail).
`ww run` and explicit-`-o`/byte-id helpers are left as-is; the 990/993
byte-id comparison logic is byte-for-byte unchanged.

Two items filed separately (this commit holds the no-Makefile / no-main.c
rail):
- #13: a stale <src>.s byte-id readback (749) silently no-ops since
  separate-compile emits .s to <ostem>.sepwork/__root.s; documented inline.
- #14: build-system Makefile recipes build selfhost/cmd/*/main.ww with no
  -o and leak main.sepwork in-tree (bounded, gitignored; own commit).

One concern -- sepwork leak hygiene -- across 228 drivers; uniform
transform applied per-file and two-round reviewed. make test: all 402
passed, zero net-new /tmp scratch, zero test-driven in-repo .sepwork.
2026-06-22 23:29:39 +09:00

197 lines
6.5 KiB
C

/*
* 989_libprecond_abort (F-Q) — three lib functions had lost their Hare
* loud-abort PRECONDITIONS, so an out-of-domain argument silently returned
* garbage instead of aborting. This pins the restored asserts:
*
* lib/math/random/random.ww u32n assert(n != 0) ref/hare/.../random.ha:26
* lib/math/random/random.ww u64n assert(n != 0) ref/hare/.../random.ha:42
* lib/encoding/base64/... decodedsize assert(sz % 4 == 0) base64.ha:597
*
* Pre-fix every abort row BUILT rc=0 and RAN rc=0 (the precondition was
* absent — silent garbage). The fix is a lib source change, source-bundled
* into each build, so it takes effect identically on both stages with no
* codegen change (byte-id NEUTRAL).
*
* row | call | result (cs == ww)
* --------------------------+-----------------------+-------------------
* u32n_zero_abort | u32n(&g, 0) | run aborts (rc!=0)
* u32n_valid_ok | u32n(&g, 10) in [0,10)| run rc == 0
* u32n_one_ok | u32n(&g, 1) in [0,1) | run rc == 0
* u64n_zero_abort | u64n(&g, 0) | run aborts (rc!=0)
* u64n_valid_ok | u64n(&g, 10) in [0,10)| run rc == 0
* u64n_one_ok | u64n(&g, 1) in [0,1) | run rc == 0
* decodedsize_unalign_abort | decodedsize(5) | run aborts (rc!=0)
* decodedsize_align_ok | decodedsize(8) == 6 | run rc == 0
*
* The abort rows BUILD clean and abort at RUNTIME (unlike a reject row,
* which fails the build) — this harness builds then runs each fixture and
* checks the run exit, not the build exit. Rows run on cstage `ww` and,
* when present, wwstage `ww_ww`; both must agree. Sibling idiom:
* 989_taggedcompoundderef_reject.c (driver loop, /tmp fixture scaffolding).
*/
#include <stdio.h>
#include <stdlib.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 row {
const char *label;
const char *src;
int want_abort; /* 1 = run must abort (rc!=0); 0 = run rc==want_exit */
int want_exit;
};
#define RNG_HDR \
"package main;\n" \
"import random;\n" \
"export fn main() int = {\n" \
"\tlet g: random.random = random.init(1u64);\n"
#define B64_HDR \
"package main;\n" \
"import base64;\n" \
"export fn main() int = {\n"
static const struct row rows[] = {
{ "u32n_zero_abort",
RNG_HDR "\tlet x: u32 = random.u32n(&g, 0u32);\n\treturn 0;\n};\n",
1, 0 },
{ "u32n_valid_ok",
RNG_HDR "\tlet x: u32 = random.u32n(&g, 10u32);\n"
"\tif (x >= 10u32) { return 1; };\n\treturn 0;\n};\n",
0, 0 },
{ "u32n_one_ok", /* boundary: n=1 is the smallest VALID arg */
RNG_HDR "\tlet x: u32 = random.u32n(&g, 1u32);\n"
"\tif (x >= 1u32) { return 1; };\n\treturn 0;\n};\n",
0, 0 },
{ "u64n_zero_abort",
RNG_HDR "\tlet x: u64 = random.u64n(&g, 0u64);\n\treturn 0;\n};\n",
1, 0 },
{ "u64n_valid_ok",
RNG_HDR "\tlet x: u64 = random.u64n(&g, 10u64);\n"
"\tif (x >= 10u64) { return 1; };\n\treturn 0;\n};\n",
0, 0 },
{ "u64n_one_ok", /* boundary: n=1 is the smallest VALID arg */
RNG_HDR "\tlet x: u64 = random.u64n(&g, 1u64);\n"
"\tif (x >= 1u64) { return 1; };\n\treturn 0;\n};\n",
0, 0 },
{ "decodedsize_unalign_abort",
B64_HDR "\tlet c: i32 = base64.decodedsize(5i32);\n\treturn 0;\n};\n",
1, 0 },
{ "decodedsize_align_ok",
B64_HDR "\tlet c: i32 = base64.decodedsize(8i32);\n"
"\tif (c != 6i32) { return 1; };\n\treturn 0;\n};\n",
0, 0 },
};
/* Build `r->src` with `driver`, then run it. Returns the RUN exit code, or
* -1 if the build failed (the harness treats a build miss as a hard error:
* every row here must build clean — only the RUN may abort). `incs` is the
* colon-free `-I a -I b ...` include flag string already composed by main. */
static int
build_and_run(const char *driver, const char *incs, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[2048];
snprintf(tmpdir, sizeof tmpdir, "/tmp/lpc_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/lpc_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/lpc_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -2; }
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s %s 2>/dev/null",
driver, outbin, incs, src);
int brc = runwait(cmd);
int got = (brc == 0) ? runwait(outbin) : -1;
runwait(rmcmd);
return brc == 0 ? got : -1;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
char absbin[1024];
if (bin[0] != '/') {
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
/* random lives at lib/math/random, base64 at lib/encoding/base64;
* the leaf-only imports resolve via these -I dirs (base64's own
* bytes/io/... deps resolve via the driver's default lib srcdir).
* Absolute so include resolution does not depend on the build cwd. */
char incs[1024];
snprintf(incs, sizeof incs, "-I %s/lib -I %s/lib/math -I %s/lib/encoding",
cwd, cwd, cwd);
char cdrv[1024], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *drv; int gated; }
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 && access(drivers[d].drv, X_OK) != 0) {
fprintf(stderr, "libprecond_abort: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
for (int i = 0; i < n; i++) {
total++;
int got = build_and_run(drivers[d].drv, incs, &rows[i], i);
if (rows[i].want_abort) {
if (got == 0 || got < 0) {
fprintf(stderr, "libprecond_abort[%s][%s]: run rc=%d, "
"expected a loud abort (rc!=0)%s\n",
drivers[d].name, rows[i].label, got,
got < 0 ? " [build failed]" : "");
fail++;
}
} else {
if (got != rows[i].want_exit) {
fprintf(stderr, "libprecond_abort[%s][%s]: run rc=%d "
"want=%d\n", drivers[d].name, rows[i].label, got,
rows[i].want_exit);
fail++;
}
}
}
}
if (fail) {
fprintf(stderr, "libprecond_abort: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("libprecond_abort: %d/%d ok\n", total, total);
return 0;
}