w6c+wwstage: read array field of a global struct (#249 BUG B)
Reading an array-typed field of a module-global struct value (`G.arr[i]`) silently miscompiled: the N_INDEX fallback's cg_dotbase_addr (cstage) / dotbaseaddr (wwstage) helper — the #135 sibling that computes &(s.field) for a `[N]T` field — had no module-global-struct base arm. cstage emitted `LEAQ (BP)` (localfind returns 0 for a global, so it read the stack frame → 0); wwstage's localfindnode returned nil and the fallback keyed on the FIELD name, so it returned false and the caller's cgexpr(N_DOT) loaded the field VALUE as a pointer → SEGFAULT. The .data was already correct (emit_struct_lit_bytes #129 A.3); only the READ base address was wrong. Both stages now emit `LEAQ name(SB) (+ ADDQ field_off)` for a global value-struct base, mirroring the scalar global-field read (cgen.c:7532); const globals resolve via def_isstructdef. Symmetric both stages (rule 10), byte-identical .s. Unblocks base64's `const std_encoding.encmap[i]` reads (#22). Test 949_structlit_arrfield_run: global `let`/`def` struct array-field read, cstage run + cs==ww byte-id.
This commit is contained in:
6
Makefile
6
Makefile
@@ -402,6 +402,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_deref_narrow_run \
|
||||
$(BIN)/test_idx_compound_run \
|
||||
$(BIN)/test_dotbase_arr_run \
|
||||
$(BIN)/test_structlit_arrfield_run \
|
||||
$(BIN)/test_continue_run \
|
||||
$(BIN)/test_callret_unsigned_arith_run \
|
||||
$(BIN)/test_sar_shr_run \
|
||||
@@ -1592,6 +1593,11 @@ $(BIN)/test_dotbase_arr_run: test/wcc/949_dotbase_arr_run.c $(BIN)/ww \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_structlit_arrfield_run: test/wcc/949_structlit_arrfield_run.c \
|
||||
$(BIN)/ww $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_continue_run: test/wcc/911_continue_run.c $(BIN)/ww \
|
||||
$(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
|
||||
@@ -1754,10 +1754,20 @@ cg_dotbase_addr(Cg *c, Node *base, int dst_reg, Local *locals)
|
||||
if (ft == NULL || ft->kind != TY_ARRAY) return 0;
|
||||
int inner_off = localfind(locals, inner->str);
|
||||
int foff = (int)f->offset;
|
||||
/* #249 (sibling of #135): a module-GLOBAL struct value base. localfind
|
||||
* returns 0 for a global, so the BP-rel form below would emit `LEAQ
|
||||
* (BP)` (read the stack frame, not the global). Resolve the same way
|
||||
* the scalar N_DOT global-field read does (cgen.c:7532) — LEAQ
|
||||
* name(SB) + field offset. const globals are def_isstructdef. */
|
||||
if (viaptr) {
|
||||
ins2(c, A_MOVQ, amem(D_BP, inner_off), areg(dst_reg));
|
||||
if (foff != 0)
|
||||
ins2(c, A_ADDQ, aimm(foff), areg(dst_reg));
|
||||
} else if (inner_off == 0 && (let_islet(inner->str)
|
||||
|| def_isstructdef(inner->str))) {
|
||||
ins2(c, A_LEAQ, masym(c, inner->str), areg(dst_reg));
|
||||
if (foff != 0)
|
||||
ins2(c, A_ADDQ, aimm(foff), areg(dst_reg));
|
||||
} else {
|
||||
ins2(c, A_LEAQ, amem(D_BP, inner_off + foff),
|
||||
areg(dst_reg));
|
||||
|
||||
@@ -20025,6 +20025,7 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
// 8 bytes as if it were a pointer-var — wrong shape (cstage
|
||||
// sister fix in cg_dotbase_addr).
|
||||
let lc: *local = localfindnode(c, inner.str);
|
||||
let isglobal: bool = false;
|
||||
if (lc == nil) {
|
||||
let gt: *node = letvartnode(c, base.str);
|
||||
if (gt != nil && gt.kind == nkind.N_TARRAY) {
|
||||
@@ -20035,7 +20036,12 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
emitline("\n");
|
||||
return true;
|
||||
};
|
||||
return false;
|
||||
// #249 (sibling of #135): inner is a module-GLOBAL struct value
|
||||
// (let/def), not a local — lc is nil but inner.type_ is a valid
|
||||
// struct. Resolve the field below and emit a global base (LEAQ
|
||||
// name(SB)). A non-struct inner (e.g. an SK_USE module qualifier,
|
||||
// type ty_err) falls through the struct gate to `return false`.
|
||||
isglobal = true;
|
||||
};
|
||||
let bu: *tinfo = inner.type_: *tinfo;
|
||||
for (bu != nil && bu.kind == tykind.TY_NAMED) { bu = bu.under; };
|
||||
@@ -20072,7 +20078,8 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
for (ft != nil && ft.kind == tykind.TY_NAMED) { ft = ft.under; };
|
||||
if (ft == nil) { return false; };
|
||||
if (ft.kind != tykind.TY_ARRAY) { return false; };
|
||||
let innoff: i64 = lc.off: i64;
|
||||
let innoff: i64 = 0;
|
||||
if (lc != nil) { innoff = lc.off: i64; };
|
||||
if (viaptr) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(innoff);
|
||||
@@ -20086,6 +20093,21 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
emitline(dstreg);
|
||||
emitline("\n");
|
||||
};
|
||||
} else if (isglobal) {
|
||||
// #249: LEAQ name(SB) + field offset. Mirror cstage
|
||||
// cg_dotbase_addr's global value-struct arm.
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, inner.str);
|
||||
emitline("(SB), ");
|
||||
emitline(dstreg);
|
||||
emitline("\n");
|
||||
if (foff != 0) {
|
||||
emitline("\tADDQ\t$");
|
||||
emitint(foff);
|
||||
emitline(", ");
|
||||
emitline(dstreg);
|
||||
emitline("\n");
|
||||
};
|
||||
} else {
|
||||
emitline("\tLEAQ\t");
|
||||
emitoff(innoff + foff);
|
||||
|
||||
@@ -918,6 +918,7 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
// 8 bytes as if it were a pointer-var — wrong shape (cstage
|
||||
// sister fix in cg_dotbase_addr).
|
||||
let lc: *local = localfindnode(c, inner.str);
|
||||
let isglobal: bool = false;
|
||||
if (lc == nil) {
|
||||
let gt: *node = letvartnode(c, base.str);
|
||||
if (gt != nil && gt.kind == nkind.N_TARRAY) {
|
||||
@@ -928,7 +929,12 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
emitline("\n");
|
||||
return true;
|
||||
};
|
||||
return false;
|
||||
// #249 (sibling of #135): inner is a module-GLOBAL struct value
|
||||
// (let/def), not a local — lc is nil but inner.type_ is a valid
|
||||
// struct. Resolve the field below and emit a global base (LEAQ
|
||||
// name(SB)). A non-struct inner (e.g. an SK_USE module qualifier,
|
||||
// type ty_err) falls through the struct gate to `return false`.
|
||||
isglobal = true;
|
||||
};
|
||||
let bu: *tinfo = inner.type_: *tinfo;
|
||||
for (bu != nil && bu.kind == tykind.TY_NAMED) { bu = bu.under; };
|
||||
@@ -965,7 +971,8 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
for (ft != nil && ft.kind == tykind.TY_NAMED) { ft = ft.under; };
|
||||
if (ft == nil) { return false; };
|
||||
if (ft.kind != tykind.TY_ARRAY) { return false; };
|
||||
let innoff: i64 = lc.off: i64;
|
||||
let innoff: i64 = 0;
|
||||
if (lc != nil) { innoff = lc.off: i64; };
|
||||
if (viaptr) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(innoff);
|
||||
@@ -979,6 +986,21 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
emitline(dstreg);
|
||||
emitline("\n");
|
||||
};
|
||||
} else if (isglobal) {
|
||||
// #249: LEAQ name(SB) + field offset. Mirror cstage
|
||||
// cg_dotbase_addr's global value-struct arm.
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, inner.str);
|
||||
emitline("(SB), ");
|
||||
emitline(dstreg);
|
||||
emitline("\n");
|
||||
if (foff != 0) {
|
||||
emitline("\tADDQ\t$");
|
||||
emitint(foff);
|
||||
emitline(", ");
|
||||
emitline(dstreg);
|
||||
emitline("\n");
|
||||
};
|
||||
} else {
|
||||
emitline("\tLEAQ\t");
|
||||
emitoff(innoff + foff);
|
||||
|
||||
@@ -20025,6 +20025,7 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
// 8 bytes as if it were a pointer-var — wrong shape (cstage
|
||||
// sister fix in cg_dotbase_addr).
|
||||
let lc: *local = localfindnode(c, inner.str);
|
||||
let isglobal: bool = false;
|
||||
if (lc == nil) {
|
||||
let gt: *node = letvartnode(c, base.str);
|
||||
if (gt != nil && gt.kind == nkind.N_TARRAY) {
|
||||
@@ -20035,7 +20036,12 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
emitline("\n");
|
||||
return true;
|
||||
};
|
||||
return false;
|
||||
// #249 (sibling of #135): inner is a module-GLOBAL struct value
|
||||
// (let/def), not a local — lc is nil but inner.type_ is a valid
|
||||
// struct. Resolve the field below and emit a global base (LEAQ
|
||||
// name(SB)). A non-struct inner (e.g. an SK_USE module qualifier,
|
||||
// type ty_err) falls through the struct gate to `return false`.
|
||||
isglobal = true;
|
||||
};
|
||||
let bu: *tinfo = inner.type_: *tinfo;
|
||||
for (bu != nil && bu.kind == tykind.TY_NAMED) { bu = bu.under; };
|
||||
@@ -20072,7 +20078,8 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
for (ft != nil && ft.kind == tykind.TY_NAMED) { ft = ft.under; };
|
||||
if (ft == nil) { return false; };
|
||||
if (ft.kind != tykind.TY_ARRAY) { return false; };
|
||||
let innoff: i64 = lc.off: i64;
|
||||
let innoff: i64 = 0;
|
||||
if (lc != nil) { innoff = lc.off: i64; };
|
||||
if (viaptr) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(innoff);
|
||||
@@ -20086,6 +20093,21 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
|
||||
emitline(dstreg);
|
||||
emitline("\n");
|
||||
};
|
||||
} else if (isglobal) {
|
||||
// #249: LEAQ name(SB) + field offset. Mirror cstage
|
||||
// cg_dotbase_addr's global value-struct arm.
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, inner.str);
|
||||
emitline("(SB), ");
|
||||
emitline(dstreg);
|
||||
emitline("\n");
|
||||
if (foff != 0) {
|
||||
emitline("\tADDQ\t$");
|
||||
emitint(foff);
|
||||
emitline(", ");
|
||||
emitline(dstreg);
|
||||
emitline("\n");
|
||||
};
|
||||
} else {
|
||||
emitline("\tLEAQ\t");
|
||||
emitoff(innoff + foff);
|
||||
|
||||
185
test/wcc/949_structlit_arrfield_run.c
Normal file
185
test/wcc/949_structlit_arrfield_run.c
Normal file
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
* 949_structlit_arrfield_run — runtime + byte-id net for #249: struct
|
||||
* array-field init/read silent miscompiles. Two distinct roots, both
|
||||
* gate-blind (cs==ww broken identically pre-fix):
|
||||
*
|
||||
* BUG B — reading an array field of a module-GLOBAL struct value
|
||||
* (`G.arr[i]`). The N_INDEX fallback's cg_dotbase_addr / dotbaseaddr
|
||||
* helper (the #135 sibling) had no module-global-struct base arm:
|
||||
* cstage emitted `LEAQ (BP)` (read the stack → 0), wwstage fell to
|
||||
* cgexpr(N_DOT) which loaded the field VALUE as a pointer → SEGFAULT.
|
||||
* The .data was already correct (emit_struct_lit_bytes #129 A.3); only
|
||||
* the READ base address was wrong. Fix: a global value-struct base
|
||||
* emits `LEAQ name(SB) (+ ADDQ field_off)`, mirroring the scalar
|
||||
* global-field read (cgen.c:7532). const globals are def_isstructdef.
|
||||
*
|
||||
* BUG A — initializing an array field from a struct literal
|
||||
* (`e{ arr = [..] }`) as a local. cg_structlit_fill / cgstructlitfill
|
||||
* had no TY_ARRAY field arm; the array field fell to the generic
|
||||
* scalar tail (cgexpr the N_ARRLIT → AX, store one sized word) which
|
||||
* silently DROPPED every element. Fix: a TY_ARRAY field arm element-
|
||||
* wise stores the N_ARRLIT at base+field_off+i*esz, reusing the proven
|
||||
* N_LET array-init shape. The GLOBAL literal-init path is unaffected
|
||||
* (it goes through emit_struct_lit_bytes, already correct).
|
||||
*
|
||||
* cstage `ww build` + run for exit code; w6c vs w6c_ww `.s` cmp for the
|
||||
* rule-10 byte-id gate. u8 element rows only (a `[N]u8` read cast to i32
|
||||
* is byte-id; wider widths hit the i32-return MOVL/MOVSXD cs/ww
|
||||
* divergence — see 949_dotbase_arr_run's deferral note).
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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;
|
||||
}
|
||||
|
||||
struct row { const char *label; const char *src; int want_exit; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* BUG B: read [4]u8 field of a module-global `let` struct at idx 0
|
||||
* (field_off 0). Pre-fix cstage→0, wwstage→SEGFAULT. encmap[0]='A'
|
||||
* (65). */
|
||||
{ "global_let_read",
|
||||
"package main;\n"
|
||||
"type e = struct { encmap: [4]u8 };\n"
|
||||
"let g: e = e { encmap = [65u8, 66u8, 67u8, 68u8] };\n"
|
||||
"export fn main() i32 = { return g.encmap[0]: i32; };\n", 65 },
|
||||
/* BUG B: read [4]u8 field of a module-global `def` (const) struct
|
||||
* at idx 2, with a non-zero field offset (a [3]u8 pad ahead of it) —
|
||||
* exercises def_isstructdef + the ADDQ $field_off arm. The base64
|
||||
* `const std_encoding` shape. pad ahead, encmap[2]='C' (67). */
|
||||
{ "global_def_read_off",
|
||||
"package main;\n"
|
||||
"type e = struct { pad: [3]u8, encmap: [4]u8 };\n"
|
||||
"def G: e = e { pad = [9u8, 9u8, 9u8],"
|
||||
" encmap = [65u8, 66u8, 67u8, 68u8] };\n"
|
||||
"export fn main() i32 = { return G.encmap[2]: i32; };\n", 67 },
|
||||
{ NULL, NULL, 0 }
|
||||
};
|
||||
|
||||
static int
|
||||
slurp_eq(const char *a, const char *b)
|
||||
{
|
||||
FILE *fa = fopen(a, "rb");
|
||||
FILE *fb = fopen(b, "rb");
|
||||
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
|
||||
int rc = 0;
|
||||
for (;;) {
|
||||
int ca = fgetc(fa);
|
||||
int cb = fgetc(fb);
|
||||
if (ca != cb) { rc = -1; break; }
|
||||
if (ca == EOF) break;
|
||||
}
|
||||
fclose(fa); fclose(fb);
|
||||
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 w6c[1100], w6c_ww[1100];
|
||||
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
||||
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
||||
if (access(w6c_ww, X_OK) != 0) {
|
||||
fprintf(stderr, "structlit_arrfield: w6c_ww missing — cannot run "
|
||||
"the cs==ww byte-id gate (the whole point of this test)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int n = 0, fail = 0;
|
||||
for (int i = 0; rows[i].src; i++, n++) {
|
||||
char src[64];
|
||||
snprintf(src, sizeof src, "/tmp/wwsaf_%d_%d.ww", getpid(), i);
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (f == NULL) { fail++; continue; }
|
||||
fputs(rows[i].src, f);
|
||||
fclose(f);
|
||||
|
||||
char tmpdir[64];
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwsaf_%d_d_%d",
|
||||
getpid(), i);
|
||||
mkdir(tmpdir, 0755);
|
||||
|
||||
char cmd[2048];
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
|
||||
tmpdir, bin, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: cstage build failed\n",
|
||||
rows[i].label);
|
||||
fail++;
|
||||
unlink(src); rmdir(tmpdir);
|
||||
continue;
|
||||
}
|
||||
|
||||
char outbin[128];
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
||||
char *dot = strrchr(outbin, '.');
|
||||
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
||||
|
||||
int got = runwait(outbin);
|
||||
if (got != rows[i].want_exit) {
|
||||
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
|
||||
rows[i].label, got, rows[i].want_exit);
|
||||
fail++;
|
||||
}
|
||||
unlink(outbin); rmdir(tmpdir);
|
||||
|
||||
char cs_s[64], ws_s[64];
|
||||
snprintf(cs_s, sizeof cs_s, "/tmp/wwsaf_%d_%d_cs.s",
|
||||
getpid(), i);
|
||||
snprintf(ws_s, sizeof ws_s, "/tmp/wwsaf_%d_%d_ww.s",
|
||||
getpid(), i);
|
||||
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
||||
w6c, cs_s, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
|
||||
fail++; unlink(src); continue;
|
||||
}
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
||||
w6c_ww, ws_s, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c_ww failed\n",
|
||||
rows[i].label);
|
||||
fail++; unlink(src); unlink(cs_s); continue;
|
||||
}
|
||||
if (slurp_eq(cs_s, ws_s) != 0) {
|
||||
fprintf(stderr,
|
||||
"row[%s]: cstage/wwstage .s DIFFER (rule-10 "
|
||||
"byte-id violation)\n", rows[i].label);
|
||||
fail++;
|
||||
}
|
||||
unlink(src); unlink(cs_s); unlink(ws_s);
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "%d/%d structlit-arrfield tests failed\n",
|
||||
fail, n);
|
||||
return 1;
|
||||
}
|
||||
printf("structlit_arrfield: %d/%d ok (cstage run + cs==ww byte-id)\n",
|
||||
n, n);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user