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

259 lines
8.2 KiB
C

/*
* 689_globptr_field_store_run (#6) — a module-GLOBAL `*struct` pointer base
* (`gp.f = v` / `gp.f += v` / `gp.x.y = v` / `gp.a[i].f = v`) stores
* correctly AND cstage / wwstage agree byte-for-byte.
*
* THE BUG (cstage only): a global `*struct` ptr base carries via_ptr=1 but
* has no local slot — localfind returns 0, so the enumerated via_ptr field-
* store arms emitted `MOVQ (BP),BX` (deref the saved BP = garbage → SEGV).
* The plain-scalar-`=` STORE half was already correct (the #47/#8 fix, LEAQ
* gp(SB)), so load/store were asymmetric. wwstage routes every case EXCEPT
* plain-scalar-`=` through its F6 cgplaceaddr resolver and was correct.
*
* THE FIX (align cstage UP): decline the global-`*struct`-ptr base at the
* enumerated arm's dispatch for compound + non-scalar (and a chained
* `gp.x.y` root), so cstage falls to its OWN F6 assign-resolver — byte-
* identical to wwstage. Plain-scalar-`=` stays in the enumerated arm (its
* #47 byte-id is undisturbed); a local ptr and a param at offset 0
* (localfind != 0) also stay local.
*
* shape | want
* ------------------------------+------
* gp.f += 5 (compound off 8) | 5
* gp.f += 7 (compound off 0) | 7
* gp.in = Inner{7,9} (struct) | 16
* gp.sf = "hi" (str field) | 2
* gp.x.y=11; gp.x.z+=7 (chain) | 18
* gp.a[1].f=5; gp.a[1].g+=8 | 13
* lp.f += 5 (LOCAL ptr ctrl) | 5
* bump(&s): p.f/p.g (param ctrl)| 10
* Each row also asserts w6c .s == w6c_ww .s byte-identical.
* Pre-fix: cstage SEGV(139) on every global-ptr row; cs vs ww .s differed.
*/
#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 *name;
const char *src;
int want;
};
static const struct row ROWS[] = {
{ "compound_off8",
"package main;\n"
"type S = struct { a: i64, f: i64 };\n"
"let gp: *S = nil;\n"
"export fn main() i32 = { let s: S = S{a=0,f=0}; gp = &s;"
" gp.f += 5; return s.f: i32; };\n", 5 },
{ "compound_off0",
"package main;\n"
"type S = struct { f: i64, g: i64 };\n"
"let gp: *S = nil;\n"
"export fn main() i32 = { let s: S = S{f=0,g=0}; gp = &s;"
" gp.f += 7; return s.f: i32; };\n", 7 },
{ "struct_field",
"package main;\n"
"type I = struct { x: i64, y: i64 };\n"
"type S = struct { a: i64, in: I };\n"
"let gp: *S = nil;\n"
"export fn main() i32 = { let s: S = S{a=0,in=I{x=0,y=0}}; gp = &s;"
" gp.in = I{x=7,y=9}; return (s.in.x+s.in.y): i32; };\n", 16 },
{ "str_field",
"package main;\n"
"type S = struct { a: i64, sf: str };\n"
"let gp: *S = nil;\n"
"export fn main() i32 = { let s: S = S{a=0,sf=\"\"}; gp = &s;"
" gp.sf = \"hi\"; return (s.sf.len): i32; };\n", 2 },
{ "chained",
"package main;\n"
"type I = struct { y: i64, z: i64 };\n"
"type S = struct { a: i64, x: I };\n"
"let gp: *S = nil;\n"
"export fn main() i32 = { let s: S = S{a=0,x=I{y=0,z=0}}; gp = &s;"
" gp.x.y = 11; gp.x.z += 7; return (s.x.y+s.x.z): i32; };\n", 18 },
{ "indexed",
"package main;\n"
"type E = struct { f: i64, g: i64 };\n"
"type S = struct { n: i64, a: [3]E };\n"
"let gp: *S = nil;\n"
"export fn main() i32 = { let s: S; gp = &s;"
" gp.a[1].f = 5; gp.a[1].g += 8; return (s.a[1].f+s.a[1].g): i32; };\n", 13 },
{ "local_ptr_ctrl",
"package main;\n"
"type S = struct { a: i64, f: i64 };\n"
"export fn main() i32 = { let s: S = S{a=0,f=0}; let lp: *S = &s;"
" lp.f += 5; return s.f: i32; };\n", 5 },
{ "param_off0_ctrl",
"package main;\n"
"type S = struct { f: i64, g: i64 };\n"
"fn bump(p: *S) void = { p.f += 3; p.g += 4; };\n"
"export fn main() i32 = { let s: S = S{f=1,g=2}; bump(&s);"
" return (s.f+s.g): i32; };\n", 10 },
};
#define NROWS ((int)(sizeof ROWS / sizeof ROWS[0]))
/* build+run one row through `driver`; return exit code or -1. */
static int
run_driver(const char *driver, const char *src, int idx, int want)
{
char tmpdir[64], srcf[128], outbin[160], scratch[192], rmcmd[224], cmd[2400];
int result = -1, cleanfail = 0;
snprintf(tmpdir, sizeof tmpdir, "/tmp/gpfs_%d_%d_d", getpid(), idx);
if (mkdir(tmpdir, 0755) != 0) return -1;
snprintf(srcf, sizeof srcf, "%s/t.ww", tmpdir);
snprintf(outbin, sizeof outbin, "%s/t", tmpdir);
snprintf(scratch, sizeof scratch, "%s.sepwork", outbin);
FILE *f = fopen(srcf, "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, srcf);
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(srcf) != 0 && errno != ENOENT) cleanfail = 1;
if (unlink(outbin) != 0 && errno != ENOENT) cleanfail = 1;
if (rmdir(tmpdir) != 0) cleanfail = 1;
if (cleanfail) {
fprintf(stderr, "globptr_field_store[%d]: temporary cleanup failed\n", idx);
if (result == want) return -1;
}
return result;
}
/* Emit .s for `tool`; the caller owns srcf and asmf. */
static int
emit_asm(const char *bin, const char *tool, const char *srcf,
const char *asmf)
{
char cmd[2400];
snprintf(cmd, sizeof cmd, "%s/%s -o %s %s 2>/dev/null",
bin, tool, asmf, srcf);
int rc = runwait(cmd);
return rc == 0 ? 0 : -1;
}
static int
files_identical(const char *a, const char *b)
{
FILE *fa = fopen(a, "rb"), *fb = fopen(b, "rb");
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return 0; }
int ca, cb, same = 1;
do {
ca = fgetc(fa); cb = fgetc(fb);
if (ca != cb) { same = 0; break; }
} while (ca != EOF);
fclose(fa); fclose(fb);
return same;
}
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;
for (int i = 0; i < NROWS; i++) {
const struct row *r = &ROWS[i];
total++;
int gc = run_driver(cdrv, r->src, i, r->want);
if (gc != r->want) {
fprintf(stderr, "globptr_field_store[%s,cstage]: run=%d want=%d\n",
r->name, gc, r->want);
fail++;
}
if (have_ww) {
total++;
int gw = run_driver(wdrv, r->src, i, r->want);
if (gw != r->want) {
fprintf(stderr, "globptr_field_store[%s,wwstage]: run=%d want=%d\n",
r->name, gw, r->want);
fail++;
}
}
/* byte-id: w6c .s == w6c_ww .s */
if (have_ww) {
char td[80], srcf[96], csf[96], wwf[96];
int ec = -1, ew = -1, same = 0, setupfail = 0, cleanfail = 0;
snprintf(td, sizeof td, "/tmp/gpfs_asm_%d_%d", getpid(), i);
snprintf(srcf, sizeof srcf, "%s/t.ww", td);
snprintf(csf, sizeof csf, "%s/c.s", td);
snprintf(wwf, sizeof wwf, "%s/w.s", td);
total++;
if (mkdir(td, 0755) == 0) {
FILE *f = fopen(srcf, "wb");
if (f) {
int writefail = fputs(r->src, f) == EOF;
if (fclose(f) != 0) writefail = 1;
if (!writefail) {
ec = emit_asm(bin, "w6c", srcf, csf);
ew = emit_asm(bin, "w6c_ww", srcf, wwf);
same = ec == 0 && ew == 0
&& files_identical(csf, wwf);
} else setupfail = 1;
} else setupfail = 1;
if (unlink(srcf) != 0 && errno != ENOENT) cleanfail = 1;
if (unlink(csf) != 0 && errno != ENOENT) cleanfail = 1;
if (unlink(wwf) != 0 && errno != ENOENT) cleanfail = 1;
if (rmdir(td) != 0) cleanfail = 1;
} else setupfail = 1;
if (setupfail)
fprintf(stderr, "globptr_field_store[%s]: temporary setup failed\n",
r->name);
if (!same) {
fprintf(stderr, "globptr_field_store[%s]: cs/ww .s NOT "
"byte-identical (ec=%d ew=%d)\n", r->name, ec, ew);
fail++;
}
if (cleanfail) {
fprintf(stderr, "globptr_field_store[%s]: temporary cleanup failed\n",
r->name);
if (same) fail++;
}
}
}
if (fail) {
fprintf(stderr, "globptr_field_store_run: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("globptr_field_store_run: %d/%d ok\n", total, total);
return 0;
}