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.
283 lines
10 KiB
C
283 lines
10 KiB
C
/*
|
|
* 962_opaque_assign_cast_run — runtime + byte-id proof of #108 sub-fold
|
|
* (c): opaque as a type-erasure sink. Two assignability rules + the
|
|
* reinterpret casts sort's implementation relies on.
|
|
*
|
|
* rule 1 `*T -> *opaque` IMPLICIT (no cast). Any pointer is the
|
|
* universal void-pointer. harec type_is_assignable, pointer
|
|
* arm: ref/harec/src/types.c:1053 (`case STORAGE_OPAQUE:
|
|
* break;` — the referent need not match).
|
|
* rule 2 `[]T -> []opaque` IMPLICIT (no cast). Any slice is the
|
|
* type-erased slice; the {ptr,len,cap} header is normal,
|
|
* the byte stride is supplied at runtime (itemsz). harec
|
|
* slice arm: types.c:1094 (`if (to_secondary->storage ==
|
|
* STORAGE_OPAQUE) return true;`).
|
|
* casts `[]opaque -> *u8` (slice -> byte ptr; cgexpr leaves the
|
|
* ptr in AX, so the cast naturally takes .ptr) and
|
|
* `*opaque -> *u8` / `*opaque -> *i32` (ptr->ptr reinterpret,
|
|
* a no-op). drew described the Hare idiom as `*[*]u8`; ww has
|
|
* no unbounded-array `[*]`, so the ww-faithful reinterpret
|
|
* target is `*u8` + uintptr stride arithmetic.
|
|
*
|
|
* Rule-10 placement (per-rule, empirical):
|
|
* rules 1 & 2 are CSTAGE-ONLY. cstage type_assignable (cmd/wcc/
|
|
* type.c) gained the opaque sink; the wwstage check.ww isassignable
|
|
* is a resolve-only AST approximation that returns "can't tell, stay
|
|
* quiet" (confident=false) for a ptr/slice whose element it cannot
|
|
* match, so it already ACCEPTS every form here (let-init AND call-
|
|
* arg). Verified empirically: w6c_ww compiles each row's source with
|
|
* exit 0, byte-identically to w6c (the cs==ww gate below). cstage
|
|
* rejected these before the type.c change. No ww twin is needed
|
|
* (same align-down precedent as 960/961's cstage-only arms).
|
|
* The casts are validation-free in BOTH stages (N_CAST never checks
|
|
* legality) and the reinterpret cgen needed no change — proven by
|
|
* the cs==ww byte-id gate.
|
|
*
|
|
* Array->[]opaque (harec's array->slice decay, types.c:1080-1099) is
|
|
* deliberately EXCLUDED: ww has no implicit array->slice conversion for
|
|
* any element type (`let s: []i32 = a` is rejected too — a slice is
|
|
* built only via an explicit `a[0:n]`), so there is no array->slice-
|
|
* header cgen. Accepting array->[]opaque alone would assign a fat
|
|
* array local into a 24-byte slot with no decay: a silent miscompile
|
|
* (rule 7). sort's caller passes a slice, so slice->[]opaque suffices.
|
|
*
|
|
* opaque is unused by the bootstrap, so the new rules fire only on
|
|
* opaque-typed operands — INERT on the selfhost corpus, 990-997 stay
|
|
* byte-identical. But that same inertness means the 990-997 gates
|
|
* never exercise opaque cs==ww; this test carries its own w6c-vs-w6c_ww
|
|
* byte-id gate (dimension (b)) to cover the rule-10 symmetry directly.
|
|
*
|
|
* Each row carries BOTH dimensions, like 953_f64crossmod_run:
|
|
* (a) cstage `ww build` + run, asserting the exit code — pins that
|
|
* the converged asm is runtime-correct (the type erasure round-
|
|
* trips: a value written/read through the opaque path reads back
|
|
* intact).
|
|
* (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge.
|
|
*
|
|
* NOTE — call results are bound to locals before any comparison, never
|
|
* compared inline (`if (f(x) != k)`). That inline-call-result-in-
|
|
* comparison shape is mis-compiled by a PRE-EXISTING cgen bug (#116
|
|
* family: reproduced with zero opaque — a fn-call result compared
|
|
* inline when its pointer arg was produced by a prior call doing
|
|
* uintptr arithmetic). Binding first is the same dodge 960 uses; it is
|
|
* NOT a workaround for the opaque feature, which is exercised in full.
|
|
*/
|
|
#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;
|
|
}
|
|
|
|
static int
|
|
slurp_eq(const char *a, const char *b)
|
|
{
|
|
FILE *fa = fopen(a, "rb");
|
|
FILE *fb = fopen(b, "rb");
|
|
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
|
|
int rc = 0;
|
|
for (;;) {
|
|
int ca = fgetc(fa);
|
|
int cb = fgetc(fb);
|
|
if (ca != cb) { rc = -1; break; }
|
|
if (ca == EOF) break;
|
|
}
|
|
fclose(fa); fclose(fb);
|
|
return rc;
|
|
}
|
|
|
|
struct row { const char *label; const char *src; int want_exit; };
|
|
|
|
static const struct row rows[] = {
|
|
/* rule 1: `*T -> *opaque` IMPLICIT (no cast) at a let-init AND a
|
|
* call-arg. Round-trip a real *i32 through *opaque and back, deref. */
|
|
{ "rule1_implicit_ptr",
|
|
"package main;\n"
|
|
"fn readi32(p: *opaque) i32 = { let pi: *i32 = p: *i32; return *pi; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let n: i32 = 42;\n"
|
|
" let po: *opaque = &n;\n" /* let-init *i32 -> *opaque */
|
|
" let v: i32 = readi32(po);\n"
|
|
" let v2: i32 = readi32(&n);\n" /* call-arg *i32 -> *opaque */
|
|
" if (v != 42) { return 1; };\n"
|
|
" if (v2 != 42) { return 2; };\n"
|
|
" return v;\n"
|
|
"};\n", 42 },
|
|
/* rule 2: `[]T -> []opaque` IMPLICIT (no cast) at a let-init AND a
|
|
* call-arg. Read .len, round-trip .ptr (a *opaque) back to *i32. */
|
|
{ "rule2_implicit_slice",
|
|
"package main;\n"
|
|
"fn slen(o: []opaque) i32 = { return o.len: i32; };\n"
|
|
"fn first(o: []opaque) i32 = { let p: *opaque = o.ptr; let pi: *i32 = p: *i32; return *pi; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let a: [4]i32 = [11, 22, 33, 44];\n"
|
|
" let s: []i32 = a[0:4];\n"
|
|
" let o: []opaque = s;\n" /* let-init []i32 -> []opaque */
|
|
" let n: i32 = slen(o);\n"
|
|
" let n2: i32 = slen(s);\n" /* call-arg []i32 -> []opaque */
|
|
" if (n != 4) { return 1; };\n"
|
|
" if (n2 != 4) { return 2; };\n"
|
|
" let f: i32 = first(s);\n"
|
|
" if (f != 11) { return 3; };\n"
|
|
" return n;\n"
|
|
"};\n", 4 },
|
|
/* sort's actual usage end-to-end: a `fn(items: []opaque, itemsz:
|
|
* size)` called with a []i32; inside, reinterpret the slice as a
|
|
* byte base (`items: *u8`), uintptr-arith two element addresses,
|
|
* byte-swap them by itemsz. Then read back through the *opaque
|
|
* element path and through the original []i32 view — the type
|
|
* erasure round-trips iff both agree. Also a *opaque arg straight
|
|
* from a *i32 (rule 1). exit 0 == every assertion held. */
|
|
{ "sort_pattern",
|
|
"package main;\n"
|
|
"fn elemptr(items: []opaque, i: size, itemsz: size) *opaque = {\n"
|
|
" let base: *u8 = items: *u8;\n" /* []opaque -> *u8 (takes .ptr) */
|
|
" let off: uintptr = (i * itemsz): uintptr;\n"
|
|
" return ((base: uintptr) + off): *opaque;\n"
|
|
"};\n"
|
|
"fn swap(items: []opaque, x: size, y: size, itemsz: size) void = {\n"
|
|
" let pa: *u8 = elemptr(items, x, itemsz): *u8;\n"
|
|
" let pb: *u8 = elemptr(items, y, itemsz): *u8;\n"
|
|
" let k: size = 0;\n"
|
|
" for (k < itemsz) {\n"
|
|
" let qa: *u8 = ((pa: uintptr) + k: uintptr): *u8;\n"
|
|
" let qb: *u8 = ((pb: uintptr) + k: uintptr): *u8;\n"
|
|
" let t: u8 = *qa;\n"
|
|
" *qa = *qb;\n"
|
|
" *qb = t;\n"
|
|
" k = k + 1;\n"
|
|
" };\n"
|
|
"};\n"
|
|
"fn readi32(p: *opaque) i32 = { let pi: *i32 = p: *i32; return *pi; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let a: [4]i32 = [10, 20, 30, 40];\n"
|
|
" let s: []i32 = a[0:4];\n"
|
|
" swap(s, 0: size, 3: size, size(i32));\n" /* []i32 -> []opaque call-arg */
|
|
" if (a[0] != 40) { return 1; };\n" /* erased swap round-trips */
|
|
" if (a[3] != 10) { return 2; };\n"
|
|
" let p0: *opaque = elemptr(s, 0: size, size(i32));\n"
|
|
" let v0: i32 = readi32(p0);\n"
|
|
" if (v0 != 40) { return 3; };\n"
|
|
" let nn: i32 = 77;\n"
|
|
" let pn: *opaque = &nn;\n" /* *i32 -> *opaque let-init */
|
|
" let vn: i32 = readi32(pn);\n"
|
|
" if (vn != 77) { return 4; };\n"
|
|
" let o: []opaque = s;\n"
|
|
" let ol: i32 = o.len: i32;\n"
|
|
" if (ol != 4) { return 5; };\n"
|
|
" return 0;\n"
|
|
"};\n", 0 },
|
|
{ NULL, NULL, 0 }
|
|
};
|
|
|
|
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 w6c[1100], w6c_ww[1100];
|
|
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
|
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
|
if (access(w6c_ww, X_OK) != 0) {
|
|
fprintf(stderr, "opaque_assign_cast: w6c_ww missing — cannot "
|
|
"run the cs==ww byte-id gate (the rule-10 proof)\n");
|
|
return 1;
|
|
}
|
|
|
|
int n = 0, fail = 0;
|
|
for (int i = 0; rows[i].src; i++, n++) {
|
|
/* src reused across (a) build+run AND (b) the byte-id phase;
|
|
* keep src, outbin, cs.s, ws.s ALL under the one tmpdir and
|
|
* defer a single rm -rf to the end so .sepwork never leaks. */
|
|
char tmpdir[64];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwopqc_%d_d_%d",
|
|
getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
|
|
char rmcmd[160];
|
|
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
|
|
|
|
char src[128], outbin[128];
|
|
snprintf(src, sizeof src, "%s/wwopqc_%d_%d.ww",
|
|
tmpdir, getpid(), i);
|
|
snprintf(outbin, sizeof outbin, "%s/wwopqc_%d_%d",
|
|
tmpdir, getpid(), i);
|
|
FILE *f = fopen(src, "wb");
|
|
if (f == NULL) { runwait(rmcmd); fail++; continue; }
|
|
fputs(rows[i].src, f);
|
|
fclose(f);
|
|
|
|
/* (a) cstage build + run. */
|
|
char cmd[2048];
|
|
snprintf(cmd, sizeof cmd, "%s/ww build -o %s %s",
|
|
bin, outbin, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: cstage build failed\n",
|
|
rows[i].label);
|
|
fail++;
|
|
runwait(rmcmd);
|
|
continue;
|
|
}
|
|
|
|
int got = runwait(outbin);
|
|
if (got != rows[i].want_exit) {
|
|
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
|
|
rows[i].label, got, rows[i].want_exit);
|
|
fail++;
|
|
}
|
|
|
|
/* (b) cs==ww byte-id gate: emit .s from both stages, cmp. */
|
|
char cs_s[160], ws_s[160];
|
|
snprintf(cs_s, sizeof cs_s, "%s/wwopqc_%d_%d_cs.s",
|
|
tmpdir, getpid(), i);
|
|
snprintf(ws_s, sizeof ws_s, "%s/wwopqc_%d_%d_ww.s",
|
|
tmpdir, getpid(), i);
|
|
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
|
w6c, cs_s, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
|
|
fail++; runwait(rmcmd); continue;
|
|
}
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
|
w6c_ww, ws_s, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c_ww failed\n",
|
|
rows[i].label);
|
|
fail++; runwait(rmcmd); continue;
|
|
}
|
|
if (slurp_eq(cs_s, ws_s) != 0) {
|
|
fprintf(stderr,
|
|
"row[%s]: cstage/wwstage .s DIFFER (rule-10 "
|
|
"byte-id violation)\n", rows[i].label);
|
|
fail++;
|
|
}
|
|
runwait(rmcmd);
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr, "%d/%d opaque assign/cast tests failed\n",
|
|
fail, n);
|
|
return 1;
|
|
}
|
|
printf("opaque_assign_cast: %d/%d ok (cstage run + cs==ww byte-id)\n",
|
|
n, n);
|
|
return 0;
|
|
}
|