Files
ww/test/wcc/689_structlit_slice_field.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

363 lines
11 KiB
C

/*
* 689_structlit_slice_field — cstage and wwstage agree, byte-for-byte
* and at runtime, that a struct-LITERAL initializer `cl{ items = b, n =
* .. }` with a SLICE field copies the FULL 24B { ptr, len, cap } header
* of that field (task #24).
*
* The bug: cg_structlit_fill / cgstructlitfill had a TY_STR arm that
* stored all three header words (ptr@+0, len@+8, cap@+16), but NO
* TY_SLICE arm — a slice field fell through to the generic scalar tail
* which stored only the ptr word, so the stored .len and .cap read 0.
* str fields (same 24B shape) worked; slice fields silently dropped
* two words. Both stages emitted IDENTICAL wrong asm (the 990-997
* byte-id gate was green on both-wrong), so RUNTIME readback is the
* correctness net. Same is_str/is_slice discrimination gap as #10
* part-b, here in the struct-literal field-init path.
*
* The fix (BOTH stages, converged byte-identical): the str arm's guard
* widens to `TY_STR || TY_SLICE` (cstage) / `isstrtype || isslicetype`
* (wwstage) — a slice rides the existing, already-correct 3-word store.
* The TAGGED arm stays ordered BEFORE it, so a nullable/tagged slice
* (TY_TAGGED) still routes to the widener, not the 3-word store.
*
* Mutation-sanity: the .len and .cap rows fail if the store drops
* either word (the pre-fix 1-word slice store), and the scalar-field
* rows (y.n, y.head) pin that the field beside/before the slice is
* untouched. The str_* rows are a regression pin on the untouched str
* arm.
*
* row | shape | want
* ---------------+------------------------------------------+------
* slice_len | cl{items=b,n=9}; b.len=5; y.items.len | 5
* slice_cap | cl{items=b,n=9}; b.cap=8; y.items.cap | 8
* slice_n | cl{items=b,n=9}; y.n (scalar beside slice)| 9
* slice_ptr | hb[0]=4; cl{items=b,..}; y.items[0] | 4
* off_len | struct{head:i64,s:[]u8}; s @foff 8; | 7
* | s.len=7; y.s.len (pins disp+foff+8 hdr) |
* off_cap | same; s.cap=8; y.s.cap (disp+foff+16) | 8
* off_head | same; head=1; y.head (scalar before slice)| 1
* two_b_len | struct{a:[]u8,b:[]u8}; b @foff 24; | 6
* | b.len=6; y.b.len (2nd slice, non-0 foff) |
* two_b_cap | same; b.cap=8; y.b.cap | 8
* str_name_len | struct{s:[]u8,name:str}; name="hello"; | 5
* | y.name.len (str arm regression pin) |
* str_s_len | same; s.len=3; y.s.len (slice beside str) | 3
*
* The asm-byte-id rows pin the symmetric fix (cstage == wwstage); a
* non-empty diff means one stage missed the new slice arm.
*/
#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; int want; };
static const struct row rows[] = {
{ "slice_len",
"package main;\n"
"type cl = struct { items: []u8, n: i64 };\n"
"export fn main() i32 = {\n"
"\tlet hb: [8]u8;\n"
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n"
"\tlet y: cl = cl { items = b, n = 9i64 };\n"
"\treturn y.items.len: i32;\n"
"};\n",
5 },
{ "slice_cap",
"package main;\n"
"type cl = struct { items: []u8, n: i64 };\n"
"export fn main() i32 = {\n"
"\tlet hb: [8]u8;\n"
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n"
"\tlet y: cl = cl { items = b, n = 9i64 };\n"
"\treturn y.items.cap: i32;\n"
"};\n",
8 },
{ "slice_n",
"package main;\n"
"type cl = struct { items: []u8, n: i64 };\n"
"export fn main() i32 = {\n"
"\tlet hb: [8]u8;\n"
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n"
"\tlet y: cl = cl { items = b, n = 9i64 };\n"
"\treturn y.n: i32;\n"
"};\n",
9 },
{ "slice_ptr",
"package main;\n"
"type cl = struct { items: []u8, n: i64 };\n"
"export fn main() i32 = {\n"
"\tlet hb: [8]u8; hb[0] = 4u8;\n"
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n"
"\tlet y: cl = cl { items = b, n = 9i64 };\n"
"\treturn y.items[0]: i32;\n"
"};\n",
4 },
/* slice field at a NON-ZERO field offset (head: i64 @0, s @8).
* Pins disp+foff+8/+16 in the stored header. */
{ "off_len",
"package main;\n"
"type hs = struct { head: i64, s: []u8 };\n"
"export fn main() i32 = {\n"
"\tlet hb: [8]u8;\n"
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 7; b.cap = 8;\n"
"\tlet y: hs = hs { head = 1i64, s = b };\n"
"\treturn y.s.len: i32;\n"
"};\n",
7 },
{ "off_cap",
"package main;\n"
"type hs = struct { head: i64, s: []u8 };\n"
"export fn main() i32 = {\n"
"\tlet hb: [8]u8;\n"
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 7; b.cap = 8;\n"
"\tlet y: hs = hs { head = 1i64, s = b };\n"
"\treturn y.s.cap: i32;\n"
"};\n",
8 },
{ "off_head",
"package main;\n"
"type hs = struct { head: i64, s: []u8 };\n"
"export fn main() i32 = {\n"
"\tlet hb: [8]u8;\n"
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 7; b.cap = 8;\n"
"\tlet y: hs = hs { head = 1i64, s = b };\n"
"\treturn y.head: i32;\n"
"};\n",
1 },
/* two slice fields: a @0, b @24. Pins the 2nd slice's header at a
* non-zero field offset (the 24B stride). */
{ "two_b_len",
"package main;\n"
"type tt = struct { a: []u8, b: []u8 };\n"
"export fn main() i32 = {\n"
"\tlet hb: [8]u8;\n"
"\tlet p: []u8; p.ptr = &hb[0]; p.len = 3; p.cap = 8;\n"
"\tlet q: []u8; q.ptr = &hb[0]; q.len = 6; q.cap = 8;\n"
"\tlet y: tt = tt { a = p, b = q };\n"
"\treturn y.b.len: i32;\n"
"};\n",
6 },
{ "two_b_cap",
"package main;\n"
"type tt = struct { a: []u8, b: []u8 };\n"
"export fn main() i32 = {\n"
"\tlet hb: [8]u8;\n"
"\tlet p: []u8; p.ptr = &hb[0]; p.len = 3; p.cap = 8;\n"
"\tlet q: []u8; q.ptr = &hb[0]; q.len = 6; q.cap = 8;\n"
"\tlet y: tt = tt { a = p, b = q };\n"
"\treturn y.b.cap: i32;\n"
"};\n",
8 },
/* str field beside a slice field — the untouched str arm must still
* store its full header (regression pin). */
{ "str_name_len",
"package main;\n"
"type sn = struct { s: []u8, name: str };\n"
"export fn main() i32 = {\n"
"\tlet hb: [8]u8;\n"
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 3; b.cap = 8;\n"
"\tlet y: sn = sn { s = b, name = \"hello\" };\n"
"\treturn y.name.len: i32;\n"
"};\n",
5 },
{ "str_s_len",
"package main;\n"
"type sn = struct { s: []u8, name: str };\n"
"export fn main() i32 = {\n"
"\tlet hb: [8]u8;\n"
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 3; b.cap = 8;\n"
"\tlet y: sn = sn { s = b, name = \"hello\" };\n"
"\treturn y.s.len: i32;\n"
"};\n",
3 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], scratch[160], rmcmd[192], cmd[1024];
int result = -1, cleanfail = 0;
snprintf(tmpdir, sizeof tmpdir, "/tmp/slf_%d_d_%d", getpid(), i);
if (mkdir(tmpdir, 0755) != 0) return -1;
snprintf(src, sizeof src, "%s/slf_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/slf_%d_%d", tmpdir, getpid(), i);
snprintf(scratch, sizeof scratch, "%s.sepwork", outbin);
FILE *f = fopen(src, "wb");
if (!f) goto cleanup;
int writefail = fputs(r->src, f) == EOF;
if (fclose(f) != 0) writefail = 1;
if (writefail) goto cleanup;
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);
goto cleanup;
}
result = runwait(outbin);
cleanup:
snprintf(rmcmd, sizeof rmcmd, "rm -rf -- %s", scratch);
if (runwait(rmcmd) != 0) cleanfail = 1;
if (unlink(src) != 0 && errno != ENOENT) cleanfail = 1;
if (unlink(outbin) != 0 && errno != ENOENT) cleanfail = 1;
if (rmdir(tmpdir) != 0) cleanfail = 1;
if (cleanfail) {
fprintf(stderr, "row[%s]: temporary cleanup failed\n", r->label);
if (result == r->want) return -1;
}
return result;
}
/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match.
* Pre-fix BOTH stages dropped len+cap identically (byte-id green on
* both-wrong); the symmetric fix keeps them byte-identical and correct. */
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char tmpdir[64], src[96], cs[96], ws[96], cmd[1024];
int rc = -1, cleanfail = 0;
snprintf(tmpdir, sizeof tmpdir, "/tmp/slf_asm_%d_%d", getpid(), i);
if (mkdir(tmpdir, 0755) != 0) return -1;
snprintf(src, sizeof src, "%s/t.ww", tmpdir);
snprintf(cs, sizeof cs, "%s/c.s", tmpdir);
snprintf(ws, sizeof ws, "%s/w.s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) goto cleanup;
int writefail = fputs(r->src, f) == EOF;
if (fclose(f) != 0) writefail = 1;
if (writefail) goto cleanup;
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);
goto cleanup;
}
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);
goto cleanup;
}
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
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);
cleanup:
if (unlink(src) != 0 && errno != ENOENT) cleanfail = 1;
if (unlink(cs) != 0 && errno != ENOENT) cleanfail = 1;
if (unlink(ws) != 0 && errno != ENOENT) cleanfail = 1;
if (rmdir(tmpdir) != 0) cleanfail = 1;
if (cleanfail) {
fprintf(stderr, "row[%s]: temporary cleanup failed\n", r->label);
return -1;
}
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[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char wdrv[1024];
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *path; int gated_on_existence; }
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_on_existence
&& access(drivers[d].path, X_OK) != 0) {
fprintf(stderr, "structlit_slice_field: 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,
"structlit_slice_field[%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,
"structlit_slice_field: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("structlit_slice_field: %d/%d ok\n", total, total);
return 0;
}