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

325 lines
12 KiB
C

/*
* 689_globptr_field_read_run (#15) — reading a field through a module-
* GLOBAL `*struct` pointer base (`gp.f`, single-dot) loads correctly AND
* cstage / wwstage agree byte-for-byte.
*
* THE BUG (both stages, two different wrong ways — no byte-id oracle until
* one side was fixed, but they DID diverge): a global `*struct` ptr base
* carries via_ptr=1 but has NO local slot (localfind/localfindnode = 0),
* so the pointer-to-struct field-READ arm never loaded the pointer VALUE
* from the global's data slot before applying the field offset:
* cstage : `MOVQ (BP),BX; MOVQ 8(BX),AX` — derefed the saved BP → SEGV.
* wwstage: `MOVQ f(SB),AX` — collapsed gp.f to a bare
* undefined symbol `f`.
* Read twin of the #6 STORE fix (689_globptr_field_store_run).
*
* THE FIX (both stages converge): load the pointer value via SB first —
* `MOVQ gp(SB),BX; MOVQ off(BX),AX`
* cstage: base load is now `off==0 && let_islet → MOVQ mafn(gp)(SB),BX`.
* wwstage: a dedicated global-ptr arm (lc==nil + isletvar + N_TPTR) emits
* `MOVQ name(SB),BX` and shares the field tail (cgptrfieldload).
*
* shape | want
* -------------------------------+------
* gp.f (scalar, offset 8) | 170
* gp.f+gp.g (scalar, offset 0/8) | 14
* let x=gp.sf; x.len (str field) | 3
* gp.d : i32 (f64 field) | 7
* match gp.t (tagged field, 16B) | 5
* gp.len (*[]u8 pseudo-field) | 3
* lp.f (LOCAL ptr control) | 42
* bump param p.f (param control) | 10
* gp.sf.len (CHAINED str leaf) | 3 (#16)
* gp.x.q (CHAINED struct leaf)| 7 (#16)
* gp.x.q i64 sink, (v>>32) | 1 (#18 spine-narrow PIN)
* Each row also asserts w6c .s == w6c_ww .s byte-identical.
* Pre-fix: cstage SEGV(139) on every global-ptr row; wwstage ran wrong;
* cs vs ww .s differed.
*
* #16 (chained-through-global-ptr-root, the last two rows): after #15
* both stages RAN correct but byte-DIVERGED on the chained-N_DOT spine —
* cstage offset-folds (LEAQ name(SB),CX; MOVQ (CX),CX; MOVQ <folded>(CX))
* while wwstage's chained spine bailed (dotchainresolve missed a global
* `*struct` root) onto the naive inner-load-and-shuffle catch-all. Fix
* aligns wwstage UP: dotchainresolve resolves a global `*struct` root as
* isglobal && ptrroot and the spine emits the same offset-fold (rule 10).
*/
#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[] = {
{ "scalar_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;"
" s.f = 170; return gp.f: i32; };\n", 170 },
{ "scalar_off0_off8",
"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;"
" s.f = 5; s.g = 9; return (gp.f + gp.g): i32; };\n", 14 },
{ "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=\"abc\"}; gp = &s;"
" let x: str = gp.sf; return x.len: i32; };\n", 3 },
{ "float_field",
"package main;\n"
"type S = struct { a: i64, d: f64 };\n"
"let gp: *S = nil;\n"
"export fn main() i32 = { let s: S = S{a=0,d=0.0}; gp = &s;"
" s.d = 7.0; return (gp.d): i32; };\n", 7 },
{ "tagged_field",
"package main;\n"
"type S = struct { a: i64, t: (i64 | i32) };\n"
"let gp: *S = nil;\n"
"export fn main() i32 = { let s: S = S{a=0,t=5i32}; gp = &s;"
" let v: i32 = 0; match (gp.t) { case let x: i32 => v = x;"
" case let y: i64 => v = y: i32; }; return v; };\n", 5 },
{ "tagged_slice_field",
/* #17: a 32B slice-payload tagged field (8B tag + 24B slice =
* full 4-reg cursor) read via global *struct ptr. cgloadtaggedfield
* fills AX/DX/CX/R8; wwstage loaded R8@+24 before CX@+16 while
* cstage's direct *struct-ptr arm loads them in offset order, so the
* .s byte-diverged (both ran correct). The 16B tagged_field row above
* is too small to fire the R8/CX split. */
"package main;\n"
"type S = struct { a: i64, t: (i64 | []u8) };\n"
"let gp: *S = nil;\n"
"export fn main() i32 = { let s: S = S{a=0,t=3i64}; gp = &s;"
" let v: i32 = 0; match (gp.t) { case let x: i64 => v = x: i32;"
" case let y: []u8 => v = len(y): i32; }; return v; };\n", 3 },
{ "tagged_slice_field_local",
/* #17 twin: same 32B slice-payload tagged read, but via a LOCAL
* *struct ptr. cgptrfieldload is shared by the local (MOVQ off(BP),BX)
* and global (MOVQ name(SB),BX) direct *struct-ptr arms, so the same
* cxlast=false fix closes both — this row locks the local path against
* a future regression of the R8/CX order. */
"package main;\n"
"type S = struct { a: i64, t: (i64 | []u8) };\n"
"export fn main() i32 = { let s: S = S{a=0,t=3i64}; let p: *S = &s;"
" let v: i32 = 0; match (p.t) { case let x: i64 => v = x: i32;"
" case let y: []u8 => v = len(y): i32; }; return v; };\n", 3 },
{ "pslice_len",
"package main;\n"
"let gp: *[]u8 = nil;\n"
"export fn main() i32 = { let b: []u8 = [1u8,2u8,3u8]; gp = &b;"
" return (gp.len): i32; };\n", 3 },
{ "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;"
" s.f = 42; return lp.f: i32; };\n", 42 },
{ "param_ctrl",
"package main;\n"
"type S = struct { f: i64, g: i64 };\n"
"fn rd(p: *S) i32 = { return (p.f + p.g): i32; };\n"
"export fn main() i32 = { let s: S = S{f=4,g=6}; return rd(&s); };\n", 10 },
/* #16: CHAINED read through a global-`*struct` root. Pre-fix BOTH stages
* ran correct after #15 but byte-DIVERGED — cstage offset-folds (LEAQ
* name(SB),CX; MOVQ (CX),CX; MOVQ <folded>(CX)) while wwstage's chained
* spine bailed (dotchainresolve missed a global `*struct` root) onto the
* naive inner-load-and-shuffle catch-all. */
{ "chain_str_len",
"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=\"abc\"}; gp = &s;"
" return gp.sf.len: i32; };\n", 3 },
{ "chain_struct_field",
"package main;\n"
"type Inner = struct { p: i64, q: i64 };\n"
"type S = struct { a: i64, x: Inner };\n"
"let gp: *S = nil;\n"
"export fn main() i32 = { let s: S = S{a=0,x=Inner{p=0,q=0}}; gp = &s;"
" s.x.q = 7; return gp.x.q: i32; };\n", 7 },
/* #18 PIN: a CHAINED read into an i64 SINK must NOT carry a spurious load/
* spine narrow. q holds 0x1_0000_0001; we observe the HIGH word via
* (v>>32):i32 — a truncating MOVSXD on the chained load would sign-extend
* the low word (1) and the high word would vanish (1 -> 0). The legitimate
* MOVSXD must stay on the `:i32` RETURN cast only (after a full 64-bit
* SARQ over the complete i64). Verified no-bug at HEAD; this row guards the
* spine-narrow family (cf. fld-load-op / int-cast-no-truncate fixes). */
{ "chain_i64_sink",
"package main;\n"
"type T = struct { q: i64 };\n"
"type S = struct { x: T };\n"
"let gp: *S = nil;\n"
"export fn main() i32 = { let s: S = S{x=T{q=0}}; gp = &s;"
" s.x.q = 4294967297; let v: i64 = gp.x.q; return (v >> 32): i32; };\n", 1 },
};
#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/gpfr_%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_read[%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_read[%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_read[%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/gpfr_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_read[%s]: temporary setup failed\n",
r->name);
if (!same) {
fprintf(stderr, "globptr_field_read[%s]: cs/ww .s NOT "
"byte-identical (ec=%d ew=%d)\n", r->name, ec, ew);
fail++;
}
if (cleanfail) {
fprintf(stderr, "globptr_field_read[%s]: temporary cleanup failed\n",
r->name);
if (same) fail++;
}
}
}
if (fail) {
fprintf(stderr, "globptr_field_read_run: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("globptr_field_read_run: %d/%d ok\n", total, total);
return 0;
}