Files
ww/test/wcc/813_arrlit_infer_elem_run.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

358 lines
11 KiB
C

/*
* 813_arrlit_infer_elem_run — #103 + #108 (+ #104 + the m8_slice
* acceptance gap): an INFERRED let defaults its untyped-int to `int`
* (8B machine word) on BOTH stages, byte-identically and at runtime.
*
* THE BUG (cstage was the truncating side — #263 polarity):
* cstage type_default(TY_UNTYPED_INT) returned ty_i32 (4B). For a
* scalar `let x = <v>` and an inferred array `let a = [<v>, ...]`
* (no annotation) the literal defaulted to i32 — silently TRUNCATING
* any value > 2^31 (5000000000 → 705032704) and striding arrays at 4.
* wwstage kept the element as raw untyped_int (size 0), which sized
* INCONSISTENTLY across cgen consumers: the array STORE strode the 8
* sentinel (so the value width was right) but letslotsize under-
* allocated the frame (SEGV) and cgindex strode the READ at 1 — so
* `let a = [..]` SEGV'd on wwstage and the scalar ran correct only by
* the 8B-store accident. The two stages were each wrong differently.
*
* THE FIX (one root, both stages, FUSE):
* - cstage type.c type_default(TY_UNTYPED_INT) i32 -> int; the empty
* arrlit fallback check.c ty_i32 -> ty_int (symmetric, count-0
* neutral). int = machine word = 8B (Go-style; MEMORY
* project_int_machine_word_derived_limits) — Hare lowers a flexible
* iconst to `int`, never a fixed i32 (ref/harec/src/types.c:835).
* - wwstage check.ww exprtype N_ARRLIT: default the inferred
* element's UNTYPED flavor to its concrete type (untyped_int->int,
* _float->f64, _str->str, _rune->rune, _bool->bool), the empty-elt
* fallback "i32"->"int", and stamp the synthesized N_TARRAY's
* .type_ — so slotsize / elemsizeofc / letslotsize all read its
* real [N]int size (32 for [4]int), routing through the type table
* (rule-13) instead of the nil/0/sentinel fallbacks. SSoT — no
* letslotsize special-case.
*
* POLARITY TEETH: the wide rows use 5000000000 ( > 2^31). A small-value
* row would pass both stages by luck (gate-blind); only a > 2^31 value
* distinguishes the truncating i32 default from the correct int.
*
* COVERAGE — the two direct repros plus the FIVE corpus movers ken's v2
* re-census named (each converges 0/0 byte-id + run-correct, NONE
* both-wrong): m2_while (#108 scalar, alias-bool loop), m8_range1/2
* (#104 for-range elem over alias / 2-level-alias array), m8_slice1/2
* (#103 SEGV + slice-init acceptance over alias / 2-level-alias slice).
* Plus the annotated controls [4]i32 (stride-4) and [4]int (stride-8),
* which the fix must leave byte-identical — it touches ONLY the inferred
* untyped default, never an explicit element type.
*
* row | shape | want
* -----------------+---------------------------------------------+-----
* arr_wide | let a=[5e9,2,3,4]; a[0]==5e9 && a[3]==4 | 0
* scalar_wide | let x=5e9; x==5e9 (#108 teeth) | 0
* arr_small | let a=[10,20,30,40]; a[2] | 30
* m2_while | #108 scalar via alias-bool loop counter | 0
* m8_range1 | #104 for-range elem over alias [4]int | 0
* m8_range2 | #104 for-range elem over 2-level-alias | 0
* m8_slice1 | #103 inferred a + slice s:sl=a[1:3] | 0
* m8_slice2 | #103 + 2-level-alias slice + re-slice | 0
* ctrl_i32 | let a:[4]i32=[1,2,3,4]; a[3] (stride-4) | 4
* ctrl_int | let a:[4]int=[1,2,3,4]; a[3] (stride-8) | 4
*
* EACH ROW CARRIES THREE DIMENSIONS (684/802 model):
* (a) cstage `ww build` + run, asserting the exit — pins the converged
* asm is runtime-correct (real values, no truncation/SEGV).
* (b) wwstage `ww_ww build` + run (gated on w6c_ww) — pins the ww SEGV
* is gone and the value round-trips.
* (c) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge (rule-10).
*
* GATE POLARITY: must stay GREEN. A wrong exit means the inferred
* default regressed to a truncating/under-strided type; a byte-id FAIL
* means the stages diverged.
*/
#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; };
static const struct row rows[] = {
/* #103 array, > 2^31 element — truncation + frame teeth. */
{ "arr_wide",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet a = [5000000000, 2, 3, 4];\n"
"\tif (a[0] != 5000000000) { return 1; };\n"
"\tif (a[3] != 4) { return 2; };\n"
"\treturn 0;\n"
"};\n",
0 },
/* #108 scalar, > 2^31 — the cstage general truncation teeth. */
{ "scalar_wide",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x = 5000000000;\n"
"\tif (x != 5000000000) { return 1; };\n"
"\treturn 0;\n"
"};\n",
0 },
/* basic inferred array — frame + stride sanity (mutation: a wrong
* stride/slot reads a neighbouring word, not 30). */
{ "arr_small",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet a = [10, 20, 30, 40];\n"
"\treturn a[2]: i32;\n"
"};\n",
30 },
/* corpus mover m2_while (#108 scalar via an alias-bool loop). */
{ "m2_while",
"package main;\n"
"type myb = bool;\n"
"export fn main() i32 = {\n"
"\tlet b: myb = true;\n"
"\tlet i = 0;\n"
"\tfor (b) {\n"
"\t\ti = i + 1;\n"
"\t\tif (i >= 3) { b = false; };\n"
"\t};\n"
"\tif (i != 3) { return 1; };\n"
"\treturn 0;\n"
"};\n",
0 },
/* corpus mover m8_range1 (#104 for-range elem over alias array). */
{ "m8_range1",
"package main;\n"
"type arr = [4]int;\n"
"export fn main() i32 = {\n"
"\tlet a: arr = [1, 2, 3, 4];\n"
"\tlet sum = 0;\n"
"\tfor (let x .. a) {\n"
"\t\tsum = sum + x;\n"
"\t};\n"
"\tif (sum != 10) { return 1; };\n"
"\treturn 0;\n"
"};\n",
0 },
/* corpus mover m8_range2 (#104 for-range elem over 2-level alias). */
{ "m8_range2",
"package main;\n"
"type arr = [4]int;\n"
"type arr2 = arr;\n"
"export fn main() i32 = {\n"
"\tlet a: arr2 = [1, 2, 3, 4];\n"
"\tlet sum = 0;\n"
"\tfor (let x .. a) {\n"
"\t\tsum = sum + x;\n"
"\t};\n"
"\tif (sum != 10) { return 1; };\n"
"\treturn 0;\n"
"};\n",
0 },
/* corpus mover m8_slice1 (#103 inferred array + alias-slice init). */
{ "m8_slice1",
"package main;\n"
"type sl = []int;\n"
"export fn main() i32 = {\n"
"\tlet a = [10, 20, 30, 40];\n"
"\tlet s: sl = a[1:3];\n"
"\tif (s.len != 2) { return 1; };\n"
"\tif (s[0] != 20) { return 2; };\n"
"\tif (s[1] != 30) { return 3; };\n"
"\treturn 0;\n"
"};\n",
0 },
/* corpus mover m8_slice2 (#103 + 2-level-alias slice + re-slice). */
{ "m8_slice2",
"package main;\n"
"type sl = []int;\n"
"type sl2 = sl;\n"
"export fn main() i32 = {\n"
"\tlet a = [10, 20, 30, 40];\n"
"\tlet s: sl2 = a[1:3];\n"
"\tif (s.len != 2) { return 1; };\n"
"\tif (s[0] != 20) { return 2; };\n"
"\tlet t = s[0:1];\n"
"\tif (t[0] != 20) { return 3; };\n"
"\treturn 0;\n"
"};\n",
0 },
/* CONTROL: annotated [4]i32 — must stay i32/stride-4, byte-id,
* UNTOUCHED by the inferred-default fix. */
{ "ctrl_i32",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet a: [4]i32 = [1, 2, 3, 4];\n"
"\treturn a[3];\n"
"};\n",
4 },
/* CONTROL: annotated [4]int — int/stride-8, byte-id. */
{ "ctrl_int",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet a: [4]int = [1, 2, 3, 4];\n"
"\treturn a[3]: i32;\n"
"};\n",
4 },
};
static int
run_driver(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/aie_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/aie_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/aie_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -1; }
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
driver, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
runwait(rmcmd);
return -1;
}
int got = runwait(outbin);
runwait(rmcmd);
return got;
}
/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[64], cs[72], ws[72], cmd[1024];
snprintf(src, sizeof src, "/tmp/aie_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/aie_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/aie_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
unlink(src);
return -1;
}
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
bin, ws, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
unlink(src); unlink(cs);
return -1;
}
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
int rc = 0;
if (!fc || !fw) {
rc = -1;
} else {
for (;;) {
int a = fgetc(fc);
int b = fgetc(fw);
if (a != b) { rc = -1; break; }
if (a == EOF) break;
}
}
if (fc) fclose(fc);
if (fw) fclose(fw);
if (rc != 0)
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
r->label);
unlink(src); unlink(cs); unlink(ws);
return rc;
}
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[1100], wdrv[1100];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *path; 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].path, X_OK) != 0) {
fprintf(stderr, "arrlit_infer_elem: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
int got = run_driver(drivers[d].path, &rows[i], i);
total++;
if (got != rows[i].want) {
fprintf(stderr,
"arrlit_infer_elem[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0)
fail++;
}
}
if (fail) {
fprintf(stderr,
"arrlit_infer_elem: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("arrlit_infer_elem: %d/%d ok\n", total, total);
return 0;
}