Files
ww/test/wcc/686_slice_str_global_zero.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

248 lines
7.0 KiB
C

/*
* 686_slice_str_global_zero — a bare module-level `str` global and a
* bare `[]u8` slice global emit their 24-byte zero header EXACTLY ONCE,
* and cstage / wwstage agree byte-for-byte (task #10 part b).
*
* The bug (wwstage only): post-#1, size(str) == size(slice) == 24. The
* emitletdataw str arm and slice arm were sequential `if`s gated on
* SIZE alone, so both matched a 24-byte bare global and BOTH fired the
* no-rhs zero fallback — two `DATAW main.g` rows. cstage discriminates
* on type kind (let_isstr / let_isslice) and emits one. Runtime reads
* the null header {0,0,0} correctly either way, so the divergence is
* byte-id-visible only — the asm_byte_identical gate is the real
* discriminator; the exit-code rows assert correctness is preserved.
*
* The fix: emitletdataw's str and slice arms now branch on the declared
* type kind (letdeclkind, TY_NAMED-peeled, mirroring let_isstr/
* let_isslice), mutually exclusive, so a 24B global hits one arm.
*
* row | shape | want
* -----------------+----------------------------------------+------
* slice_len | global let g:[]u8; g.len | 0
* slice_cap | global let g:[]u8; g.cap | 0
* slice_ptr_null | global let g:[]u8; g.ptr==nil ? 7 : 0 | 7
* str_len | global let s:str; s.len | 0
* str_ptr_null | global let s:str; s.ptr==nil ? 9 : 0 | 9
*/
#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"
"let g: []u8;\n"
"export fn main() i32 = { return g.len: i32; };\n",
0 },
{ "slice_cap",
"package main;\n"
"let g: []u8;\n"
"export fn main() i32 = { return g.cap: i32; };\n",
0 },
{ "slice_ptr_null",
"package main;\n"
"let g: []u8;\n"
"export fn main() i32 = {\n"
"\tif (g.ptr == nil) { return 7; };\n"
"\treturn 0;\n"
"};\n",
7 },
{ "str_len",
"package main;\n"
"let s: str;\n"
"export fn main() i32 = { return s.len: i32; };\n",
0 },
{ "str_ptr_null",
"package main;\n"
"let s: str;\n"
"export fn main() i32 = {\n"
"\tif (s.ptr == nil) { return 9; };\n"
"\treturn 0;\n"
"};\n",
9 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char src[128], tmpdir[64], outbin[128], scratch[160], rmcmd[192], cmd[1024];
int result = -1, cleanfail = 0;
snprintf(tmpdir, sizeof tmpdir, "/tmp/ssgz_%d_d_%d", getpid(), i);
if (mkdir(tmpdir, 0755) != 0) return -1;
snprintf(src, sizeof src, "%s/ssgz_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/ssgz_%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.
* This is the #10-part-b discriminator: the old size-keyed double-emit
* produced a second DATAW row in the wwstage .s. */
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/ssgz_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, "slice_str_global_zero: 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,
"slice_str_global_zero[%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,
"slice_str_global_zero: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("slice_str_global_zero: %d/%d ok\n", total, total);
return 0;
}