Files
ww/test/wcc/689_globptr_field_read_run.c
Hojun-Cho 11afd82e16 test: pin i64-sink chained global-ptr read is full-width, guards #18
#18 verified there is no spurious load-narrow on a chained global-ptr field
read into an i64 sink -- the only MOVSXD is the legitimate :i32 return cast,
byte-identical in both stages. Lock it: a chain_i64_sink row reads q:i64 =
0x1_0000_0001 and asserts the high word == 1. A truncating load-narrow would
drop the high word to 0, so the row pins the spine-narrow family (runtime +
byte-id, both stages).
2026-06-24 00:28:25 +09:00

294 lines
10 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 <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)
{
char tmpdir[64], srcf[128], outbin[160], rmcmd[192], cmd[2400];
snprintf(tmpdir, sizeof tmpdir, "/tmp/gpfr_%d_%d_d", getpid(), idx);
mkdir(tmpdir, 0755);
snprintf(srcf, sizeof srcf, "%s/t.ww", tmpdir);
snprintf(outbin, sizeof outbin, "%s/t", tmpdir);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(srcf, "wb");
if (!f) { runwait(rmcmd); return -1; }
fputs(src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
driver, outbin, srcf);
if (runwait(cmd) != 0) { runwait(rmcmd); return -1; }
int got = runwait(outbin);
runwait(rmcmd);
return got;
}
/* emit .s for `tool` (w6c / w6c_ww) into asmf; 0 ok, -1 on compile fail. */
static int
emit_asm(const char *bin, const char *tool, const char *src, int idx,
const char *asmf)
{
char srcf[80], cmd[2400];
snprintf(srcf, sizeof srcf, "/tmp/gpfr_asm_%d_%d.ww", getpid(), idx);
FILE *f = fopen(srcf, "wb");
if (!f) return -1;
fputs(src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s/%s -o %s %s 2>/dev/null",
bin, tool, asmf, srcf);
int rc = runwait(cmd);
unlink(srcf);
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);
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);
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 csf[80], wwf[80];
snprintf(csf, sizeof csf, "/tmp/gpfr_cs_%d_%d.s", getpid(), i);
snprintf(wwf, sizeof wwf, "/tmp/gpfr_ww_%d_%d.s", getpid(), i);
total++;
int ec = emit_asm(bin, "w6c", r->src, i, csf);
int ew = emit_asm(bin, "w6c_ww", r->src, i, wwf);
if (ec != 0 || ew != 0 || !files_identical(csf, wwf)) {
fprintf(stderr, "globptr_field_read[%s]: cs/ww .s NOT "
"byte-identical (ec=%d ew=%d)\n", r->name, ec, ew);
fail++;
}
unlink(csf); unlink(wwf);
}
}
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;
}