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.
280 lines
8.6 KiB
C
280 lines
8.6 KiB
C
/*
|
|
* 951_defdim_struct_run — byte-id net for #141: a struct
|
|
* field whose array dimension is a `def` (`[MAX]u8`, MAX a same-module
|
|
* def), not an integer literal. BOTH stages were wrong, oppositely:
|
|
*
|
|
* cstage LOUD-rejected ("array length must be an integer literal").
|
|
* resolve_type's N_TARRAY arm folded only N_INTLIT dims; a def-ref
|
|
* fell through to the err. Root was a phase-ordering trap: def names
|
|
* weren't bound in scope when resolve_typedecl walked the struct
|
|
* body, so eval_def_const couldn't see MAX. Fix binds def-name stubs
|
|
* before type-body resolution, then folds the dim via eval_def_const.
|
|
*
|
|
* wwstage SILENTLY mis-laid-out the struct. astsize sized the def-dim
|
|
* field to 0 (only N_INTLIT dims were read), so the N_TSTRUCT
|
|
* `off += astsize(field)` loop gave the NEXT field the array's offset
|
|
* — the trailing scalar overlapped the array, reading/writing the
|
|
* wrong bytes (ken pdefM4: end != written). Fix routes every N_TARRAY
|
|
* length reader through a shared arrayelen() that folds the def.
|
|
*
|
|
* The TEETH row is the trailing scalar after the def-dim array: pre-fix
|
|
* ww read a corrupted `end`; post-fix it round-trips and cs==ww byte-id.
|
|
* Closes #13's def-dim half (the slice-repeat clause stays open).
|
|
*
|
|
* Runtime values live in r951_defdim_* fixtures. This carrier compares the
|
|
* `ww build`-produced
|
|
* .combined.ww, not the raw src: w6c does no import resolution, so the
|
|
* cross-module row (C1, `import os`) only folds once concatenated, and a
|
|
* same-module row combines to itself. The last row is the ACTUAL
|
|
* path::buffer shape — a cross-module `os.PATH_MAX` def dimension (the
|
|
* N_DOT fold arm). u8 array elements + i32 trailing scalars only (the
|
|
* i32 return is MOVL/MOVSXD byte-id; see 949_dotbase_arr_run).
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.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; };
|
|
|
|
static const struct row rows[] = {
|
|
/* TEETH: def-dim array FOLLOWED by a scalar; write both, read the
|
|
* scalar back. Pre-fix ww laid `end` at the array's offset (array
|
|
* sized 0) → end read garbage. Post-fix end at offset 4 → 42. */
|
|
{ "m4_end",
|
|
"package main;\n"
|
|
"def M: i32 = 4;\n"
|
|
"type t = struct { b: [M]u8, end: i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: t;\n"
|
|
" s.b[3] = 9u8;\n"
|
|
" s.end = 42;\n"
|
|
" return s.end;\n"
|
|
"};\n" },
|
|
/* the array element itself must survive too — read the last byte
|
|
* (pre-fix the overlapping `end` write would clobber it). */
|
|
{ "m4_belem",
|
|
"package main;\n"
|
|
"def M: i32 = 4;\n"
|
|
"type t = struct { b: [M]u8, end: i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: t;\n"
|
|
" s.b[3] = 200u8;\n"
|
|
" s.end = 7;\n"
|
|
" return s.b[3]: i32;\n"
|
|
"};\n" },
|
|
/* the ~4KB path::buffer shape — def MAX=4095, read the trailing
|
|
* scalar after a 4095-byte array. */
|
|
{ "m4095_end",
|
|
"package main;\n"
|
|
"def MAX: i32 = 4095;\n"
|
|
"type t = struct { b: [MAX]u8, end: i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: t;\n"
|
|
" s.b[4094] = 5u8;\n"
|
|
" s.end = 99;\n"
|
|
" return s.end;\n"
|
|
"};\n" },
|
|
/* two scalars after the def-dim array — pins the cumulative offset
|
|
* (a+c read their written values: 11+22=33). */
|
|
{ "twoafter",
|
|
"package main;\n"
|
|
"def M: i32 = 4;\n"
|
|
"type t = struct { b: [M]u8, a: i32, c: i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: t;\n"
|
|
" s.a = 11;\n"
|
|
" s.c = 22;\n"
|
|
" return s.a + s.c;\n"
|
|
"};\n" },
|
|
/* C1 (rob-mandatory): the ACTUAL path::buffer shape — a CROSS-
|
|
* MODULE def dimension. `def MAX = os.PATH_MAX - 1` folds an N_DOT
|
|
* (os.PATH_MAX) const-ref through eval_def_const's cross-module arm,
|
|
* which the same-module rows don't exercise. os.PATH_MAX is 4096, so
|
|
* MAX is 4095. Byte-id runs on the ww-build combined.ww (w6c alone
|
|
* does no import resolution, so it can't compile the raw `import os`
|
|
* source). */
|
|
{ "xmod_pathmax",
|
|
"package main;\n"
|
|
"import os;\n"
|
|
"def MAX: i32 = os.PATH_MAX - 1;\n"
|
|
"type t = struct { b: [MAX]u8, end: i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: t;\n"
|
|
" s.b[4094] = 7u8;\n"
|
|
" s.end = 77;\n"
|
|
" return s.end;\n"
|
|
"};\n" },
|
|
{ NULL, NULL }
|
|
};
|
|
|
|
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 wdrv[1100];
|
|
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
|
if (access(wdrv, X_OK) != 0) {
|
|
fprintf(stderr, "defdim_struct: ww_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/wwdds_%d_%d.ww", getpid(), i);
|
|
FILE *f = fopen(src, "wb");
|
|
if (f == NULL) { perror(src); fail++; continue; }
|
|
fputs(rows[i].src, f);
|
|
fclose(f);
|
|
|
|
char tmpdir[64];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwdds_%d_d_%d",
|
|
getpid(), i);
|
|
if (mkdir(tmpdir, 0755) != 0) {
|
|
/* not owned — must not rm a path we failed to create */
|
|
perror(tmpdir);
|
|
fail++;
|
|
if (unlink(src) != 0 && errno != ENOENT)
|
|
perror(src);
|
|
continue;
|
|
}
|
|
|
|
char base[64];
|
|
const char *b = strrchr(src, '/');
|
|
b = b ? b + 1 : src;
|
|
snprintf(base, sizeof base, "%s", b);
|
|
char *dot = strrchr(base, '.');
|
|
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
|
|
|
/* #94 sep layout: each driver's build emits the runnable
|
|
* binary at <stem> + per-package asm under <stem>.sepwork/.
|
|
* Distinct stems so the wwstage sepwork doesn't clobber cstage's;
|
|
* pin all outputs stay under tmpdir. A
|
|
* cross-module row's `import os` resolves per-package, a same-
|
|
* module row is just __root — both compare via the sorted glob. */
|
|
char cmd[2048], stem_c[160], stem_w[160];
|
|
snprintf(stem_c, sizeof stem_c, "%s/%s_c", tmpdir, base);
|
|
snprintf(stem_w, sizeof stem_w, "%s/%s_w", tmpdir, base);
|
|
|
|
char cs_s[64], ws_s[64];
|
|
snprintf(cs_s, sizeof cs_s, "/tmp/wwdds_%d_%d_cs.s",
|
|
getpid(), i);
|
|
snprintf(ws_s, sizeof ws_s, "/tmp/wwdds_%d_%d_ww.s",
|
|
getpid(), i);
|
|
|
|
snprintf(cmd, sizeof cmd,
|
|
"cd %s && %s/ww build -S -o %s %s",
|
|
tmpdir, bin, stem_c, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: cstage build failed\n",
|
|
rows[i].label);
|
|
fail++;
|
|
goto rowdone;
|
|
}
|
|
|
|
snprintf(cmd, sizeof cmd,
|
|
"cd %s && %s/ww_ww build -S -o %s %s "
|
|
">/dev/null 2>&1", tmpdir, bin, stem_w, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: ww_ww build failed\n",
|
|
rows[i].label);
|
|
fail++;
|
|
} else {
|
|
/* a failed cat (e.g. an empty sepwork glob) must fail
|
|
* the row — two absent trees would compare equal-empty. */
|
|
snprintf(cmd, sizeof cmd,
|
|
"cat %s.sepwork/*.s > %s 2>/dev/null", stem_c, cs_s);
|
|
int catc = runwait(cmd);
|
|
snprintf(cmd, sizeof cmd,
|
|
"cat %s.sepwork/*.s > %s 2>/dev/null", stem_w, ws_s);
|
|
int catw = runwait(cmd);
|
|
if (catc != 0 || catw != 0) {
|
|
fprintf(stderr,
|
|
"row[%s]: sepwork .s concat failed "
|
|
"(cstage=%d wwstage=%d)\n",
|
|
rows[i].label, catc, catw);
|
|
fail++;
|
|
} 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);
|
|
fail++;
|
|
}
|
|
}
|
|
|
|
rowdone:
|
|
/* a failed cleanup must fail the carrier, not leak silently;
|
|
* ENOENT is fine — an early leg may exit before creating the
|
|
* concat files. */
|
|
snprintf(cmd, sizeof cmd, "rm -rf %s", tmpdir);
|
|
int cleanfail = runwait(cmd) != 0;
|
|
if (unlink(src) != 0 && errno != ENOENT) {
|
|
perror(src);
|
|
cleanfail = 1;
|
|
}
|
|
if (unlink(cs_s) != 0 && errno != ENOENT) {
|
|
perror(cs_s);
|
|
cleanfail = 1;
|
|
}
|
|
if (unlink(ws_s) != 0 && errno != ENOENT) {
|
|
perror(ws_s);
|
|
cleanfail = 1;
|
|
}
|
|
if (cleanfail) {
|
|
fprintf(stderr, "row[%s]: cleanup failed\n",
|
|
rows[i].label);
|
|
fail++;
|
|
}
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr, "%d/%d defdim-struct tests failed\n",
|
|
fail, n);
|
|
return 1;
|
|
}
|
|
printf("defdim_struct: %d/%d ok (cs==ww byte-id)\n",
|
|
n, n);
|
|
return 0;
|
|
}
|