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

178 lines
5.3 KiB
C

/*
* 689_arrglob_nodup_run (#12) — a no-init module-level array global emits
* its zero-fill DATAW row EXACTLY ONCE, and cstage / wwstage agree
* byte-for-byte.
*
* THE BUG (wwstage only): emitletdataw's str-payload zero-fallback arm is
* SIZE-keyed (`sz == size(str)` == 24), not type-keyed. A no-init array
* global whose bytes sum to 24 (e.g. `let g: [3]u64;`) matched that arm AND
* the dedicated TY_ARRAY arm below → two identical `DATAW main.g` rows.
* cstage discriminates on the declared type kind (let_isstr) and emits one.
* Runtime reads the null bytes correctly either way (the linker keeps one
* symbol), so the divergence is byte-id-visible only — the asm-byte-id check
* is the real discriminator; the row-count + run rows pin absolute correctness.
*
* THE FIX: exclude arrays from the str-size arm (`&& !isarr8`), mirroring the
* isarr8 guard already on the sz==8 scalar arm. The emitarraydata path owns
* array globals.
*
* check | shape | want
* -------------------+-----------------------------+--------------
* run | g[0]+g[1]+g[2], = 1+2+3 | 6
* dataw_rows (ww) | count of `DATAW main.g` rows | 1
* asm_byte_identical | w6c vs w6c_ww .s | identical
* Pre-fix: ww emitted 2 `DATAW main.g` rows; cs vs ww .s differed.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.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;
}
static const char *SRC =
"package main;\n"
"let g: [3]u64;\n"
"export fn main() i32 = {\n"
" g[0] = 1; g[1] = 2; g[2] = 3;\n"
" return (g[0] + g[1] + g[2]): i32;\n"
"};\n";
static int
run_driver(const char *driver)
{
char tmpdir[64], src[128], outbin[128], scratch[160], rmcmd[192], cmd[1024];
int result = -1, cleanfail = 0;
snprintf(tmpdir, sizeof tmpdir, "/tmp/agnd_%d_d", getpid());
if (mkdir(tmpdir, 0755) != 0) return -1;
snprintf(src, sizeof src, "%s/agnd_%d.ww", tmpdir, getpid());
snprintf(outbin, sizeof outbin, "%s/agnd_%d", tmpdir, getpid());
snprintf(scratch, sizeof scratch, "%s.sepwork", outbin);
FILE *f = fopen(src, "wb");
if (!f) goto cleanup;
int writefail = fputs(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) 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, "arrglob_nodup: temporary cleanup failed\n");
if (result == 6) return -1;
}
return result;
}
/* emit .s via `tool`, return the count of `DATAW main.g(SB)` rows, or -1. */
static int
dataw_rows(const char *bin, const char *tool)
{
char tmpdir[80], src[96], asmf[96], cmd[1024];
int n = -1, cleanfail = 0;
snprintf(tmpdir, sizeof tmpdir, "/tmp/agnd_asm_%d_%s", getpid(), tool);
if (mkdir(tmpdir, 0755) != 0) return -1;
snprintf(src, sizeof src, "%s/t.ww", tmpdir);
snprintf(asmf, sizeof asmf, "%s/t.s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) goto cleanup;
int writefail = fputs(SRC, f) == EOF;
if (fclose(f) != 0) writefail = 1;
if (writefail) goto cleanup;
snprintf(cmd, sizeof cmd, "%s/%s -o %s %s 2>/dev/null", bin, tool, asmf, src);
if (runwait(cmd) != 0) goto cleanup;
FILE *a = fopen(asmf, "rb");
n = 0;
if (a) {
char line[512];
while (fgets(line, sizeof line, a))
if (strncmp(line, "DATAW main.g(SB)", 16) == 0) n++;
fclose(a);
} else n = -1;
cleanup:
if (unlink(src) != 0 && errno != ENOENT) cleanfail = 1;
if (unlink(asmf) != 0 && errno != ENOENT) cleanfail = 1;
if (rmdir(tmpdir) != 0) cleanfail = 1;
if (cleanfail) {
fprintf(stderr, "arrglob_nodup[%s]: temporary cleanup failed\n", tool);
if (n == 1) return -1;
}
return n;
}
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], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int have_ww = (access(wdrv, X_OK) == 0);
int fail = 0, total = 0;
/* run correctness, both drivers */
total++;
if (run_driver(cdrv) != 6) {
fprintf(stderr, "arrglob_nodup[cstage]: run != 6\n"); fail++;
}
if (have_ww) {
total++;
if (run_driver(wdrv) != 6) {
fprintf(stderr, "arrglob_nodup[wwstage]: run != 6\n"); fail++;
}
}
/* exactly one DATAW row per stage */
total++;
if (dataw_rows(bin, "w6c") != 1) {
fprintf(stderr, "arrglob_nodup[w6c]: DATAW main.g rows != 1\n"); fail++;
}
if (access(wdrv, X_OK) == 0) {
total++;
int r = dataw_rows(bin, "w6c_ww");
if (r != 1) {
fprintf(stderr, "arrglob_nodup[w6c_ww]: DATAW main.g rows = %d "
"(want 1 — double-emit #12)\n", r);
fail++;
}
}
if (fail) {
fprintf(stderr, "arrglob_nodup_run: %d/%d checks failed\n", fail, total);
return 1;
}
printf("arrglob_nodup_run: %d/%d ok\n", total, total);
return 0;
}