/* * 953_arrlit_slice_run — runtime + byte-id + reject net for #25/#31: a * one-step array-LITERAL initialiser for a SLICE local (`let xs:[]T=[..]`). * * #31 (silent miscompile, cs!=ww): the #258 array→slice borrow wrapped the * un-addressable N_ARRLIT directly as the N_SLICE base; cgen never spilled * the literal to a stack slot, so .ptr pointed at garbage (`let xs:[]i32= * [10,20,30]; xs[1]` returned 1; []u8/[]str segfaulted). #25 (over-strict * reject): a slice target fell through to the exact-element type_eq borrow * gate, so bare-int-width ([]u8=[1,2,3]) and str elements rejected. * * Fix (re-stamp + per-borrow scratch, both stages symmetric, NO new codegen * shape): the checker re-stamps the slice arrlit as [count]T (per-element * coercion + range-check, #25); cgen materialises the borrow base into a * FRESH per-borrow @slicescr stack slot (distinct slot per borrow — a * borrow's backing outlives the lowering, so it can't share a cached slot; * two live borrows would otherwise alias one backing), filled via the shared * array-init element store (#31). Supported ONLY at a `let` init — in * call-arg / return / assign position there is no addressable backing, so * both stages LOUD-REJECT ("bind it to a `let` first"), aligning cstage DOWN * to wwstage per rule-10; full non-let support is deferred (#33). * * Accept rows (cstage `ww build` + run for exit code, then w6c vs w6c_ww .s * cmp for the rule-10 byte-id gate): * - i32_sum let xs:[]i32=[10,20,30]; xs[0]+xs[1]+xs[2] → 60 * - i32_idx xs[1] (the #31 pin: was 1, want 20) → 20 * - u8_coerce let zs:[]u8=[1,2,3]; bare-int→u8 width (#25) → 6 * - u8_typed let xs:[]u8=[10u8,20u8,30u8]; xs[2] → 30 * - i64_stride let xs:[]i64=[7i64,42i64]; xs[1] (8B stride) → 42 * - str_read let ys:[]str=["ab","c"]; ys[0].len*10+ys[1].len → 21 * - len_read let xs:[]i32=[10,20,30]; xs.len → 3 * - multi_live let xs=[1,2,3]; let ys=[4,5]; xs[0]+ys[0] → 5 * (SOUNDNESS PIN: fresh-per-borrow; a shared slot → 4+4=8) * - borrow_mut xs[1]=99 through the borrow, read back → 99 * * Reject rows (cs==ww symmetric loud-reject, both w6c and w6c_ww non-zero): * - reject_oob let q:[]u8=[256,1] → out-of-range element (#25) * - reject_call sum([1,2,3]) → non-let borrow (#31/#33) * - reject_ret return [1,2,3] → non-let borrow * - reject_assign s = [1,2,3] → non-let borrow */ #include #include #include #include #include #include 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"), *fb = fopen(b, "rb"); if (fa == NULL || fb == NULL) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; } int ca, cb, eq = 0; do { ca = fgetc(fa); cb = fgetc(fb); if (ca != cb) { eq = -1; break; } } while (ca != EOF); fclose(fa); fclose(fb); return eq; } struct row { const char *label; const char *src; int want_exit; int reject; }; static const struct row rows[] = { { "i32_sum", "package main;\n" "export fn main() i32 = {\n" " let xs: []i32 = [10, 20, 30];\n" " return xs[0] + xs[1] + xs[2];\n" "};\n", 60, 0 }, { "i32_idx", "package main;\n" "export fn main() i32 = {\n" " let xs: []i32 = [10, 20, 30];\n" " return xs[1];\n" "};\n", 20, 0 }, { "u8_coerce", "package main;\n" "export fn main() i32 = {\n" " let zs: []u8 = [1, 2, 3];\n" " return zs[0]: i32 + zs[1]: i32 + zs[2]: i32;\n" "};\n", 6, 0 }, { "u8_typed", "package main;\n" "export fn main() i32 = {\n" " let xs: []u8 = [10u8, 20u8, 30u8];\n" " return xs[2]: i32;\n" "};\n", 30, 0 }, { "i64_stride", "package main;\n" "export fn main() i32 = {\n" " let xs: []i64 = [7i64, 42i64];\n" " return xs[1]: i32;\n" "};\n", 42, 0 }, { "str_read", "package main;\n" "export fn main() i32 = {\n" " let ys: []str = [\"ab\", \"c\"];\n" " return ys[0].len: i32 * 10 + ys[1].len: i32;\n" "};\n", 21, 0 }, { "len_read", "package main;\n" "export fn main() i32 = {\n" " let xs: []i32 = [10, 20, 30];\n" " return xs.len: i32;\n" "};\n", 3, 0 }, { "multi_live", "package main;\n" "export fn main() i32 = {\n" " let xs: []i32 = [1, 2, 3];\n" " let ys: []i32 = [4, 5];\n" " return xs[0] + ys[0];\n" "};\n", 5, 0 }, { "borrow_mut", "package main;\n" "export fn main() i32 = {\n" " let xs: []i32 = [1, 2, 3];\n" " xs[1] = 99;\n" " return xs[1];\n" "};\n", 99, 0 }, /* Reject rows: out-of-range element + the three non-let contexts. */ { "reject_oob", "package main;\n" "export fn main() i32 = {\n" " let q: []u8 = [256, 1];\n" " return q[0]: i32;\n" "};\n", 0, 1 }, { "reject_call", "package main;\n" "fn sum(s: []i32) i32 = { return s[0]; };\n" "export fn main() i32 = {\n" " return sum([1, 2, 3]);\n" "};\n", 0, 1 }, { "reject_ret", "package main;\n" "fn mk() []i32 = { return [1, 2, 3]; };\n" "export fn main() i32 = {\n" " return mk()[0];\n" "};\n", 0, 1 }, { "reject_assign", "package main;\n" "export fn main() i32 = {\n" " let s: []i32 = [0, 0];\n" " s = [1, 2, 3];\n" " return s[0];\n" "};\n", 0, 1 }, { NULL, NULL, 0, 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, "arrlit_slice: w6c_ww missing — cannot run the " "cs==ww byte-id gate (the whole point of this test)\n"); return 1; } int n = 0, fail = 0; for (int i = 0; rows[i].src; i++, n++) { char src[64]; snprintf(src, sizeof src, "/tmp/wwas_%d_%d.ww", getpid(), i); FILE *f = fopen(src, "wb"); if (f == NULL) { fail++; continue; } fputs(rows[i].src, f); fclose(f); char cs_s[64], ws_s[64]; snprintf(cs_s, sizeof cs_s, "/tmp/wwas_%d_%d_cs.s", getpid(), i); snprintf(ws_s, sizeof ws_s, "/tmp/wwas_%d_%d_ww.s", getpid(), i); char cmd[2048]; if (rows[i].reject) { /* Both stages must refuse (non-zero). */ snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src); if (runwait(cmd) == 0) { fprintf(stderr, "row[%s]: w6c ACCEPTED a row that " "must reject\n", rows[i].label); fail++; } 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 ACCEPTED a row " "that must reject\n", rows[i].label); fail++; } unlink(src); unlink(cs_s); unlink(ws_s); continue; } /* Positive row: cstage build + run for exit code. */ char tmpdir[64]; snprintf(tmpdir, sizeof tmpdir, "/tmp/wwas_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s", tmpdir, bin, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: cstage build failed\n", rows[i].label); fail++; unlink(src); rmdir(tmpdir); continue; } char outbin[128]; const char *base = strrchr(src, '/'); base = base ? base + 1 : src; snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); char *dot = strrchr(outbin, '.'); if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; 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++; } unlink(outbin); rmdir(tmpdir); /* rule-10 byte-id: w6c vs w6c_ww .s. */ 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++; unlink(src); 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++; unlink(src); unlink(cs_s); 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++; } unlink(src); unlink(cs_s); unlink(ws_s); } if (fail) { fprintf(stderr, "%d/%d arrlit-slice tests failed\n", fail, n); return 1; } printf("arrlit_slice: %d/%d ok (cstage run + cs==ww byte-id + " "reject)\n", n, n); return 0; }