Files
ww/test/wcc/915_arr_module_index_run.c
Hojun-Cho a95a7a316b test/wcc: carrier ownership repair and driver-contract adaptation
Every surviving carrier now owns its artifacts: checked mkdir/mkdtemp/
fopen acquisition, one all-exit cleanup funnel per carrier, ENOENT-
tolerant checked unlinks, exact-path deletion (rm -rf only for an
owned pid-keyed dir or a .sepwork beneath one), and cleanup failure
fails a passing carrier without overwriting its diagnostic. In the
same pass the carriers adapt to the driver contract this branch lands:
--sep and WW_PKGCACHE are gone, -S and the /tmp/ww_run_<pid> scratch
contract are asserted, and rows whose runtime or reject coverage moved
to test/wcc/data fixtures or test/lang @test owners are trimmed to the
byte/artifact/diagnostic observations only they can make.

Repair and adaptation ride together because most files interleave both
in the same hunks; splitting would manufacture intermediate carrier
states that never existed and cannot run against either driver.
2026-08-07 23:21:04 +09:00

328 lines
11 KiB
C

/*
* 915_arr_module_index_run — runtime + byte-id net for #128b: a
* cross-package indexed read on an imported global `let arr: [N]T`
* (the `mod.arr[i]` shape) must compute the array's ADDRESS via LEAQ
* and stride by the element type's size. Pre-fix both stages
* mis-compiled this shape, but differently:
* - cstage: emitted `MOVQ mod.arr(SB), AX` (loaded the first 8 bytes
* of the array as if it were a pointer-var value), then ADDed the
* index, then MOVZBQ at that junk address — SEGFAULT-class memory
* corruption on any non-trivial index. cstage also defaulted esz=1
* (the `bt = n->lhs->type` was NULL for SK_USE module idents).
* - wwstage: had the same MOVQ-not-LEAQ bug; in earlier session
* state also defaulted esz=8 + MOVQ-load (wrong stride + wrong
* load-width). By the time #128b ratified, wwstage was emitting
* IMUL$2 + MOVZWQ correctly (via stamped n.type_ path) but the
* base load remained MOVQ-not-LEAQ.
*
* Fix (per the design's option B — use-site at cgindex/cgassign, NOT
* a provider-shape change at cgdot):
* - cstage: extend `cg_dotbase_addr` (the #135 helper) to recognise
* module-imported `let X: [N]T` and emit `LEAQ X(SB)` for the
* base. Add a `let_var_type(name)` lookup (LetVar gains a `type`
* field) so cgindex's N_INDEX case can fall back to the imported
* let's type when `n->lhs->type` is NULL (SK_USE-bound ident).
* - wwstage: extend `dotbaseaddr` parallel — when the inner ident
* isn't a local (localfindnode → nil) but `letvartnode(base.str)`
* resolves to N_TARRAY, emit `LEAQ symname(SB)`.
*
* Rule-10 alignment: both stages share the same cgindex N_INDEX
* fallback shape; the helper is the use-site fix per #135's pattern,
* preserving cgdot's current MOVQ semantics so whole-array-assign
* shapes (defensive case; not exercised in committed code per
* design-pass audit) aren't disturbed.
*
* Each row carries a `ww build` + run asserting the exit code (the
* cstage-segfault repro now exits cleanly). Single-file rows also carry
* a w6c vs w6c_ww `.s` cmp (rule-10 byte-id); the cross-package module
* rows gate on runtime exit here and are byte-id-covered cross-package
* by 994. The probe table is a test-local exported `[8]u16` (package
* wcmodarr — see the #93 retarget note below; it replaced strconv's
* private `let left_shift_table`, which separate compilation correctly
* hides).
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.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; /* main.ww content */
const char *modname; /* exported-probe-table package, or NULL */
const char *modsrc; /* that package's content, or NULL */
int want_exit;
};
/* #93 (915 encapsulation retarget, ken-t): the module rows used to read
* strconv's PRIVATE `let left_shift_table` — which separate compilation
* CORRECTLY hides (the combined path only "worked" because the private
* symbol leaked into one unit). Retargeted to a test-LOCAL package that
* EXPORTS its own probe table: the same cross-package `mod.arr[i]` cgen
* shape (LEAQ base + element-size stride, #128b), encapsulation-faithful
* and sep-linkable. (The deeper checker tighten — loud-reject a cross-pkg
* private ref — is tracked as #45; NOT attempted here.) Table prefix is
* the same u16 sequence strconv used, so the expected exits are unchanged
* (idx0→0, idx2→0x0801&0xFF=1, idx4→0x1006&0xFF=6). */
#define PROBE_MOD \
"package wcmodarr;\n" \
"export let probe_table: [8]u16 = [0u16, 0x0800u16, 0x0801u16, " \
"0x0803u16, 0x1006u16, 0x1009u16, 0x100Du16, 0x1812u16];\n"
static const struct row rows[] = {
/* Indexed read on an imported (exported) [N]u16 at idx=0 — load-
* width fix: MOVZWQ reads the full u16 = 0x0000 → exit 0. */
{ "mod_u16_idx0",
"package main;\n"
"import wcmodarr;\n"
"export fn main() i32 = {\n"
" let v: u32 = (wcmodarr.probe_table[0]: u32);\n"
" return v: i32;\n"
"};\n", "wcmodarr", PROBE_MOD, 0 },
/* Nontrivial idx — pre-fix segfault repro. table[2] = 0x0801;
* 0x0801 mod 256 = 1. */
{ "mod_u16_idx2",
"package main;\n"
"import wcmodarr;\n"
"export fn main() i32 = {\n"
" let v: u32 = (wcmodarr.probe_table[2]: u32);\n"
" return v: i32;\n"
"};\n", "wcmodarr", PROBE_MOD, 1 },
/* Further idx — pins per-element stride. table[4] = 0x1006;
* 0x1006 mod 256 = 6. */
{ "mod_u16_idx4",
"package main;\n"
"import wcmodarr;\n"
"export fn main() i32 = {\n"
" let v: u32 = (wcmodarr.probe_table[4]: u32);\n"
" return v: i32;\n"
"};\n", "wcmodarr", PROBE_MOD, 6 },
/* Local-array control: confirms the existing IDENT-base path is
* unchanged by the #128b cgindex edit. */
{ "local_arr_control",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [4]u16 = [10u16, 20u16, 30u16, 40u16];\n"
" let v: u32 = (a[2]: u32);\n"
" return v: i32;\n"
"};\n", NULL, NULL, 30 },
/* Same-package (NOT module-qualified) access — pins the in-package
* IDENT path stays unchanged; uses a local table. */
{ "local_u16_idx_nonzero",
"package main;\n"
"export fn main() i32 = {\n"
" let t: [8]u16 = [0u16, 0x0800u16, 0x0801u16, 0x0803u16, "
"0x1006u16, 0x1009u16, 0x100Du16, 0x1812u16];\n"
" let v: u32 = (t[2]: u32);\n"
" return v: i32;\n"
"};\n", NULL, NULL, 1 },
{ NULL, NULL, NULL, NULL, 0 }
};
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;
}
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, "modarridx: 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 tmpdir[] = "/tmp/wwmoda_XXXXXX";
char src[160], moddir[224] = {0}, modf[320] = {0};
char stem[200] = {0}, scratch[232] = {0};
char cs_s[200] = {0}, ws_s[200] = {0}, cmd[2048];
int rowfail = 0, moddir_owned = 0;
if (mkdtemp(tmpdir) == NULL) {
fprintf(stderr, "row[%s]: workspace acquisition failed\n",
rows[i].label);
fail++;
continue;
}
snprintf(src, sizeof src, "%s/main915.ww", tmpdir);
FILE *f = fopen(src, "wb");
if (f == NULL) {
fprintf(stderr, "row[%s]: source create failed\n",
rows[i].label);
rowfail = 1;
goto row_done;
}
fputs(rows[i].src, f);
int werr = ferror(f);
if (fclose(f) != 0) werr = 1;
if (werr) {
fprintf(stderr, "row[%s]: source write failed\n",
rows[i].label);
rowfail = 1;
goto row_done;
}
/* Exported-probe-table package lives at <tmpdir>/<mod>/<mod>.ww
* so the sep dep-scan (rooted at the entry's dir) resolves the
* `import <mod>;`. */
if (rows[i].modname != NULL) {
snprintf(moddir, sizeof moddir, "%s/%s", tmpdir,
rows[i].modname);
if (mkdir(moddir, 0755) != 0) {
fprintf(stderr, "row[%s]: module directory create failed\n",
rows[i].label);
rowfail = 1;
goto row_done;
}
moddir_owned = 1;
snprintf(modf, sizeof modf, "%s/%s.ww", moddir,
rows[i].modname);
FILE *mf = fopen(modf, "wb");
if (mf == NULL) {
fprintf(stderr, "row[%s]: module source create failed\n",
rows[i].label);
rowfail = 1;
goto row_done;
}
fputs(rows[i].modsrc, mf);
werr = ferror(mf);
if (fclose(mf) != 0) werr = 1;
if (werr) {
fprintf(stderr, "row[%s]: module source write failed\n",
rows[i].label);
rowfail = 1;
goto row_done;
}
}
/* Single-file runtime behavior moved to r915_arr_* wwfixtures.
* Keep auxiliary-package rows here because the fixture grammar
* cannot materialize their directory/import topology. */
if (rows[i].modname != NULL) {
/* #93 sep layout: build via `-o <stem>` (the exported
* probe table links across packages under separate compilation);
* all outputs stay under tmpdir. */
snprintf(stem, sizeof stem, "%s/main915", tmpdir);
snprintf(scratch, sizeof scratch, "%s.sepwork", stem);
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww build -o %s %s "
">/dev/null 2>&1", tmpdir, bin, stem, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: cstage build failed\n",
rows[i].label);
rowfail = 1;
goto row_done;
}
int got = runwait(stem);
if (got != rows[i].want_exit) {
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
rows[i].label, got, rows[i].want_exit);
rowfail = 1;
}
}
/* Byte-id leg — bare w6c vs w6c_ww (the compiler binaries, NOT
* the driver, so layout-invariant). Only single-file rows can be
* fed standalone; the module rows' runtime exit IS their gate
* (+ the 994 bootstrap corpus for the cross-stage byte-id). */
if (rows[i].modname == NULL) {
snprintf(cs_s, sizeof cs_s, "%s/cs.s", tmpdir);
snprintf(ws_s, sizeof ws_s, "%s/ww.s", tmpdir);
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);
rowfail = 1;
} else {
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);
rowfail = 1;
} else 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);
rowfail = 1;
}
}
}
row_done:
{
int cleanfail = 0;
if (cs_s[0] && unlink(cs_s) != 0 && errno != ENOENT)
cleanfail = 1;
if (ws_s[0] && unlink(ws_s) != 0 && errno != ENOENT)
cleanfail = 1;
if (scratch[0]) {
snprintf(cmd, sizeof cmd, "rm -rf %s", scratch);
if (runwait(cmd) != 0) cleanfail = 1;
}
if (stem[0] && unlink(stem) != 0 && errno != ENOENT)
cleanfail = 1;
if (modf[0] && unlink(modf) != 0 && errno != ENOENT)
cleanfail = 1;
if (moddir_owned && rmdir(moddir) != 0) cleanfail = 1;
if (unlink(src) != 0 && errno != ENOENT) cleanfail = 1;
if (rmdir(tmpdir) != 0) cleanfail = 1;
if (cleanfail) {
fprintf(stderr, "row[%s]: workspace cleanup failed\n",
rows[i].label);
rowfail = 1;
}
}
if (rowfail) fail++;
}
if (fail) {
fprintf(stderr, "%d/%d mod-arr-idx tests failed\n", fail, n);
return 1;
}
printf("modarridx: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}