Files
ww/test/wcc/989_nullableglobal_reject.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

267 lines
9.2 KiB
C

/*
* 989_nullableglobal_reject — #45 silent->loud bridge (ww-core task #15
* prereq). A module-level nullable `(*T | void)` GLOBAL has no storage
* path in either stage: letemitsize / let_emit_size returned 0 for the
* nullable TY_TAGGED, so let_collect skipped registration and emit_lets
* skipped DATA. The three READ paths then miscompiled SILENTLY:
* - match (g) : cgmatch's #87 arm is let_islet-gated; with no
* registration it fell to localfind -> read 0(BP) =
* saved BP (garbage tag).
* - g is *T : cgtypetest's nullable arm emitted MOVQ name(SB),AX
* for a symbol with no DATA -> w6l undefined-reference.
* - g as *T : cgtypeassert, same undefined-reference.
* cstage's #87 match arm was ALSO `!is_nullable`-gated, so BOTH stages
* were wrong (a #263-class both-wrong gap, not an align-up).
*
* THE BRIDGE (this commit): die LOUD at the size/storage layer
* (let_emit_size:1278 / letemitsize cgen.ww:1054) the instant a nullable
* global is declared, so all three read paths hit one diagnostic instead
* of a silent miscompile. The full storage + read-class arc (real DATA,
* nil/void/address-of init, let-registration, the three SB-resolution
* read arms) is deferred to task #15 — it is the CSP handle-singleton
* prereq (`let c: *Chan | void`), and a silent gap there is exactly what
* "stable before CSP" forbids. Byte-id-neutral: the corpus declares zero
* nullable globals (verified by grep), so the loud path is unreached in
* self-compile and the emitted asm is zero-move.
*
* The diagnostic core text is identical on both stages
* ("nullable-global storage unimplemented (task #15)"); cstage's fatal()
* adds the harness-wide "ww: " prefix (err.c) that err.ww does not — the
* same per-stage prefix asymmetry every existing both-stage reject
* carries (e.g. the #37 / #48 fatals). The matrix asserts rc!=0 AND the
* shared core substring on each driver.
*
* Reject-matrix idiom (sibling 989_catA_f2_reject.c): a REJECT row must
* FAIL to build with the diagnostic on every driver; an ACCEPT control
* must build + run to its expected exit. Rows run on cstage `ww` and,
* when present, wwstage `ww_ww`; both must agree.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
static const char *DIAG = "nullable-global storage unimplemented (task #15)";
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 expect_build; /* 1 = build+run to want_exit; 0 = must REJECT */
int want_exit;
};
#define NP "type np = (*i64 | void);\nlet gv: i64 = 7;\n"
static const struct row rows[] = {
/* match on a nullable global -> loud reject (was: read 0(BP)). */
{ "match_nullable_global",
"package main;\n" NP "let g: np = &gv;\n"
"fn ck() int = { match (g) { case let p: *i64 => return (*p): int; "
"case void => return 9; }; };\n"
"export fn main() int = { return ck(); };\n",
0, 0 },
/* `g is *T` on a nullable global -> loud reject (was: undefined ref). */
{ "is_nullable_global",
"package main;\n" NP "let g: np = &gv;\n"
"fn ck() int = { if (g is *i64) { return 0; }; return 12; };\n"
"export fn main() int = { return ck(); };\n",
0, 0 },
/* `g as *T` on a nullable global -> loud reject (was: undefined ref). */
{ "as_nullable_global",
"package main;\n" NP "let g: np = &gv;\n"
"fn ck() int = { let p: *i64 = g as *i64; return (*p): int; };\n"
"export fn main() int = { return ck(); };\n",
0, 0 },
/* A void-initialised nullable global rejects too — storage is
* unimplemented regardless of the initialiser (the full arc handles
* nil/void as 8 zero bytes; that is task #15). */
{ "void_init_nullable_global",
"package main;\n" NP "let g: np = void;\n"
"fn ck() int = { match (g) { case let p: *i64 => return (*p): int; "
"case void => return 9; }; };\n"
"export fn main() int = { return ck(); };\n",
0, 0 },
/* A nil-initialised nullable global rejects too — init-invariant,
* same as void (the storage gate keys on the nullable TY_TAGGED, not
* the initialiser; nil is the silent-match case the reconcile flagged
* alongside void/&gv). */
{ "nil_init_nullable_global",
"package main;\n" NP "let g: np = nil;\n"
"fn ck() int = { match (g) { case let p: *i64 => return (*p): int; "
"case void => return 9; }; };\n"
"export fn main() int = { return ck(); };\n",
0, 0 },
/* The INLINE nullable form (no alias) rejects identically — exercises
* the storage gate's direct N_TTAGGED arm rather than the
* alias-resolution hop the other rows take, proving form-invariance. */
{ "inline_nullable_global",
"package main;\nlet gv: i64 = 7;\nlet g: (*i64 | void) = &gv;\n"
"fn ck() int = { match (g) { case let p: *i64 => return (*p): int; "
"case void => return 9; }; };\n"
"export fn main() int = { return ck(); };\n",
0, 0 },
/* CONTROL — a NON-nullable tagged global still builds + runs (the
* #87 storage path is untouched). is i64 on g=5 -> 0. */
{ "nonnull_tagged_global_ok",
"package main;\n"
"let g: (i64 | bool) = 5;\n"
"fn ck() int = { if (g is i64) { return 0; }; return 12; };\n"
"export fn main() int = { return ck(); };\n",
1, 0 },
/* CONTROL — a PLAIN `*T` global (not a nullable tagged) still builds +
* runs; the reject is keyed on the nullable TY_TAGGED, not on any
* pointer. nil-init (an address-of init is a separate #48-reloc gap,
* out of scope here). */
{ "plain_ptr_global_ok",
"package main;\n"
"let p: *i64 = nil;\n"
"export fn main() int = { if (p == nil) { return 5; }; return 9; };\n",
1, 5 },
};
static int
run_build(const char *driver, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/nbg_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/nbg_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/nbg_%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 2>/dev/null",
driver, outbin, src);
int brc = runwait(cmd);
int got = -1;
if (brc == 0) got = runwait(outbin);
runwait(rmcmd);
return brc == 0 ? got : -1;
}
/* A reject row must (a) fail to build and (b) emit the shared diagnostic
* core text. Returns 0 on the expected reject, -1 otherwise. */
static int
build_should_reject(const char *driver, const char *src, int i)
{
char tmpdir[64], s[128], outbin[128], errf[128], rmcmd[160], cmd[1280];
snprintf(tmpdir, sizeof tmpdir, "/tmp/nbn_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(s, sizeof s, "%s/nbn_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/nbn_%d_%d", tmpdir, getpid(), i);
snprintf(errf, sizeof errf, "%s/nbn_%d_%d.err", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(s, "wb");
if (!f) { runwait(rmcmd); return -1; }
fputs(src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>%s",
driver, outbin, s, errf);
int rc = runwait(cmd);
int have_diag = 0;
FILE *e = fopen(errf, "rb");
if (e) {
char buf[4096];
size_t n = fread(buf, 1, sizeof buf - 1, e);
buf[n] = '\0';
fclose(e);
have_diag = (strstr(buf, DIAG) != NULL);
}
runwait(rmcmd);
/* build must NOT succeed AND the shared diagnostic must appear. */
return (rc != 0 && have_diag) ? 0 : -1;
}
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], 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, "nullableglobal_reject: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
for (int i = 0; i < n; i++) {
total++;
if (rows[i].expect_build) {
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != rows[i].want_exit) {
fprintf(stderr, "nullableglobal_reject[%s][%s]: "
"exit=%d want=%d\n", drivers[d].name,
rows[i].label, got, rows[i].want_exit);
fail++;
}
} else {
if (build_should_reject(drivers[d].drv, rows[i].src,
100 + i) != 0) {
fprintf(stderr, "nullableglobal_reject[%s][%s]: "
"expected a loud reject with \"%s\"\n",
drivers[d].name, rows[i].label, DIAG);
fail++;
}
}
}
}
if (fail) {
fprintf(stderr, "nullableglobal_reject: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("nullableglobal_reject: %d/%d ok\n", total, total);
return 0;
}