wcc/cgen: #58 indexed tagged-field read+assign cursor arm (both-stage)
Reading or writing a tagged field of an indexed array element (xs[i].field) was broken on BOTH stages, byte-identically and silently (#263 gate-blind): the arr[i].field branches had arms for array/str/slice/float but no TY_TAGGED arm, so the tagged field fell to the single-word scalar path. READ loaded only the tag word (stale payload -> `xs[i].min as T` read garbage); ASSIGN stored the raw unboxed scalar into the tag slot, corrupting the box. Insert a TY_TAGGED cursor arm before each scalar fallback, both sites both stages (cgen.c read + assign; cgenexpr.ww cgdot N_INDEX-lhs read + cgassign indexed-field). READ mirrors cg_tagged_memread (payload -> DX/CX/R8, tag -> AX last). ASSIGN synthesizes the tag for the concrete variant (taggedvariantindext) and stores tag+payload via the str/slice 3-word store spine -- not the source-remap widener (concrete rhs has no source tag to remap). >32B / multi-word / float payloads are loud-stopped at all four arms (emission not yet wired; see #114). That shape is reachable today via a narrow-variant ctor, so it louds rather than silently miscompiling. Both stages get the same arm -> byte-id preserved (990-997 green; the runtime is the net for this #263 class). Pin 944_idx_tagged_field_run (read/assign runtime rows + >32B expect-loud rows).
This commit is contained in:
368
test/wcc/944_idx_tagged_field_run.c
Normal file
368
test/wcc/944_idx_tagged_field_run.c
Normal file
@@ -0,0 +1,368 @@
|
||||
/*
|
||||
* 944_idx_tagged_field_run — #58 INDEXED-element TAGGED-field read+assign.
|
||||
*
|
||||
* `xs[i].f` where f is a TAGGED union field of a struct array element was
|
||||
* broken on BOTH stages, byte-IDENTICAL and SILENT (#263 gate-blind; the
|
||||
* byte-id gate cannot see it, runtime is the only net). The `arr[i].field`
|
||||
* spine had arms for array / str/slice / float but NO TY_TAGGED arm, so a
|
||||
* tagged field fell to the single-word scalar fallback:
|
||||
* READ: loaded only the tag word; the payload cursor (DX/CX/R8) was
|
||||
* never loaded -> `xs[i].f as T` read stale DX (#38a residual).
|
||||
* ASSIGN: DOUBLY broken -- stored the raw unboxed scalar into the TAG
|
||||
* slot, corrupting the box (never boxed, never wrote payload).
|
||||
* The fix inserts a TY_TAGGED arm before each scalar fallback, both
|
||||
* stages (align-BOTH, one commit). READ: AX=&xs[i] -> cursor (AX=tag,
|
||||
* DX/CX/R8=payload), tag LAST. ASSIGN: box the concrete scalar variant
|
||||
* (tag-index + payload), store over the indexed dest spine.
|
||||
*
|
||||
* row | shape | want
|
||||
* -------------+------------------------------------------------+-----
|
||||
* const_read | xs[0].m as size ((void|size), tag1) | 0
|
||||
* var_read | xs[i].m as size variable index | 0
|
||||
* tag2_read | xs[k].m as size ((void|bool|size), TAG2) | 0
|
||||
* assign_back | xs[0].m = 8:size; readback via *row ptr | 0
|
||||
*
|
||||
* Pre-fix (base cc896bd): const_read/var_read/tag2_read read stale DX
|
||||
* (payload dropped); assign_back corrupts the box (scalar-into-tag) so
|
||||
* the ptr-readback control sees the un-overwritten constructed payload
|
||||
* (99, not 8) -- all four FAIL both stages, cs==ww byte-id (the teeth).
|
||||
* MULTIPLE tag positions (tag1-of-2 + tag2-of-3) are mandatory: an
|
||||
* aligned-tag-only pin false-greens the assign scalar-into-tag-slot
|
||||
* corruption. Post-fix: all four 0/0, cs/ww still byte-identical.
|
||||
*
|
||||
* The >32B / multi-word / float-payload arms LOUD-STOP (their real emission
|
||||
* is not yet wired — #114). These shapes are NOT unconstructible: a wide-box
|
||||
* union is built via a NARROW variant, so each tripwire is REACHABLE and is
|
||||
* pinned expect-loud by the cfrows[] rows below. #114 wires the real emission
|
||||
* and graduates those rows to runtime-value pins.
|
||||
*/
|
||||
#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;
|
||||
}
|
||||
|
||||
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), cb = fgetc(fb);
|
||||
if (ca != cb) { rc = -1; break; }
|
||||
if (ca == EOF) break;
|
||||
}
|
||||
fclose(fa); fclose(fb);
|
||||
return rc;
|
||||
}
|
||||
|
||||
struct row { const char *label; const char *src; int want; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
{ "const_read",
|
||||
"package main;\n"
|
||||
"type opt = (void | size);\n"
|
||||
"type row = struct { id: int, m: opt };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: [2]row = [\n"
|
||||
" row { id = 1, m = 7: size },\n"
|
||||
" row { id = 2, m = 8: size },\n"
|
||||
" ];\n"
|
||||
" let v: size = xs[0].m as size;\n"
|
||||
" if (v != 7) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
{ "var_read",
|
||||
"package main;\n"
|
||||
"type opt = (void | size);\n"
|
||||
"type row = struct { id: int, m: opt };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: [2]row = [\n"
|
||||
" row { id = 1, m = 7: size },\n"
|
||||
" row { id = 2, m = 8: size },\n"
|
||||
" ];\n"
|
||||
" let i: size = 1;\n"
|
||||
" let v: size = xs[i].m as size;\n"
|
||||
" if (v != 8) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
{ "tag2_read",
|
||||
"package main;\n"
|
||||
"type opt3 = (void | bool | size);\n"
|
||||
"type row = struct { id: int, m: opt3 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: [2]row = [\n"
|
||||
" row { id = 1, m = 5: size },\n"
|
||||
" row { id = 2, m = 9: size },\n"
|
||||
" ];\n"
|
||||
" let i: size = 1;\n"
|
||||
" let v: size = xs[i].m as size;\n"
|
||||
" if (v != 9) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
{ "assign_back",
|
||||
"package main;\n"
|
||||
"type opt = (void | size);\n"
|
||||
"type row = struct { id: int, m: opt };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: [2]row = [\n"
|
||||
" row { id = 1, m = 99: size },\n"
|
||||
" row { id = 2, m = 2: size },\n"
|
||||
" ];\n"
|
||||
" xs[0].m = 8: size;\n"
|
||||
" let p: *row = &xs[0];\n"
|
||||
" let v: size = p.m as size;\n"
|
||||
" if (v != 8) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
};
|
||||
|
||||
/*
|
||||
* #58 LOUD-STOP TRIPWIRES — expect-compile-fail rows.
|
||||
*
|
||||
* The >32B / multi-word / float-payload arms of the indexed tagged-field
|
||||
* read+store are NOT silently miscompiled: they LOUD-STOP (rule 7). Earlier
|
||||
* triage believed these shapes were "unconstructible at HEAD" — they are NOT.
|
||||
* A wide-box union (largest variant >32B) is constructible via a NARROW/scalar
|
||||
* variant: `(void | size | [5]size)` initialised `m = 7: size` builds and runs
|
||||
* fine; only reading/assigning that field via the indexed spine reaches the
|
||||
* tripwire. So each tripwire is REACHABLE and must be pinned as expect-loud:
|
||||
* a build that SUCCEEDS here would mean the loud-stop regressed into a silent
|
||||
* miscompile. Each row must (a) fail the build and (b) emit a "#58" tripwire on
|
||||
* stderr, attributing the CFAIL to this fold (not the #54/#23 construction
|
||||
* hole, which only fires on STRUCT-LITERAL payloads). When #114 wires real
|
||||
* >32B box+memcpy emission, it replaces the tripwires AND graduates these rows
|
||||
* to runtime-value pins.
|
||||
*/
|
||||
struct cfrow { const char *label; const char *src; };
|
||||
|
||||
static const struct cfrow cfrows[] = {
|
||||
{ "big_read", /* >32B box read tripwire */
|
||||
"package main;\n"
|
||||
"type big = (void | size | [5]size);\n"
|
||||
"type row = struct { id: int, m: big };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: [2]row = [\n"
|
||||
" row { id = 1, m = 7: size },\n"
|
||||
" row { id = 2, m = 8: size },\n"
|
||||
" ];\n"
|
||||
" let v: size = xs[0].m as size;\n"
|
||||
" if (v != 7) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n" },
|
||||
{ "big_assign", /* >32B box store tripwire */
|
||||
"package main;\n"
|
||||
"type big = (void | size | [5]size);\n"
|
||||
"type row = struct { id: int, m: big };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: [2]row = [\n"
|
||||
" row { id = 1, m = 7: size },\n"
|
||||
" row { id = 2, m = 8: size },\n"
|
||||
" ];\n"
|
||||
" xs[0].m = 9: size;\n"
|
||||
" return 0;\n"
|
||||
"};\n" },
|
||||
{ "midword_assign", /* multi-word (>16B, <=32B box) store tripwire */
|
||||
"package main;\n"
|
||||
"type mid = (void | size | [3]size);\n"
|
||||
"type row = struct { id: int, m: mid };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: [2]row = [\n"
|
||||
" row { id = 1, m = 7: size },\n"
|
||||
" row { id = 2, m = 8: size },\n"
|
||||
" ];\n"
|
||||
" xs[0].m = 9: size;\n"
|
||||
" return 0;\n"
|
||||
"};\n" },
|
||||
{ "float_assign", /* float-payload store tripwire */
|
||||
"package main;\n"
|
||||
"type fo = (void | f64);\n"
|
||||
"type row = struct { id: int, m: fo };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: [2]row = [\n"
|
||||
" row { id = 1, m = 1.0: f64 },\n"
|
||||
" row { id = 2, m = 2.0: f64 },\n"
|
||||
" ];\n"
|
||||
" xs[0].m = 3.0: f64;\n"
|
||||
" return 0;\n"
|
||||
"};\n" },
|
||||
};
|
||||
|
||||
static int
|
||||
file_has(const char *path, const char *needle)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return 0;
|
||||
char buf[8192];
|
||||
size_t n = fread(buf, 1, sizeof buf - 1, f);
|
||||
fclose(f);
|
||||
buf[n] = '\0';
|
||||
return strstr(buf, needle) != NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
run_cfail(const char *driver, const struct cfrow *r, int i)
|
||||
{
|
||||
char src[96], errf[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/itf_cf_%d_%d.ww", getpid(), i);
|
||||
snprintf(errf, sizeof errf, "/tmp/itf_cf_%d_e_%d", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"timeout 20 %s build %s >/dev/null 2>%s", driver, src, errf);
|
||||
int brc = runwait(cmd);
|
||||
int ok = (brc != 0) && file_has(errf, "#58");
|
||||
if (!ok)
|
||||
fprintf(stderr,
|
||||
"row[%s]: %s build rc=%d, want CFAIL with \"#58\" tripwire\n",
|
||||
r->label, driver, brc);
|
||||
unlink(src); unlink(errf);
|
||||
return ok ? 0 : 1;
|
||||
}
|
||||
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[96], tmpdir[96], errf[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/itf_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/itf_%d_d_%d", getpid(), i);
|
||||
snprintf(errf, sizeof errf, "/tmp/itf_%d_e_%d", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
mkdir(tmpdir, 0755);
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"cd %s && timeout 20 %s build %s >/dev/null 2>%s",
|
||||
tmpdir, driver, src, errf);
|
||||
int brc = runwait(cmd);
|
||||
if (brc != 0) {
|
||||
fprintf(stderr, "row[%s]: build via %s failed\n",
|
||||
r->label, driver);
|
||||
unlink(src); unlink(errf); rmdir(tmpdir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
char outbin[256];
|
||||
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
||||
char *dot = strrchr(outbin, '.');
|
||||
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
||||
int got = runwait(outbin);
|
||||
|
||||
unlink(src); unlink(outbin); unlink(errf); rmdir(tmpdir);
|
||||
if (got != r->want) {
|
||||
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
|
||||
r->label, driver, got, r->want);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
asm_byte_identical(const char *bin, const struct row *r, int i)
|
||||
{
|
||||
char src[96], cs[96], ws[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/itf_asm_%d_%d.ww", getpid(), i);
|
||||
snprintf(cs, sizeof cs, "/tmp/itf_asm_%d_%d_c.s", getpid(), i);
|
||||
snprintf(ws, sizeof ws, "/tmp/itf_asm_%d_%d_w.s", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
|
||||
unlink(src);
|
||||
return -1;
|
||||
}
|
||||
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
|
||||
bin, ws, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
|
||||
unlink(src); unlink(cs);
|
||||
return -1;
|
||||
}
|
||||
int rc = slurp_eq(cs, ws);
|
||||
if (rc != 0)
|
||||
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
|
||||
r->label);
|
||||
unlink(src); unlink(cs); unlink(ws);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = getenv("BIN");
|
||||
if (!bin) bin = "out/bin";
|
||||
char absbin[2080];
|
||||
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[2120], wdrv[2120];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
int n = (int)(sizeof rows / sizeof rows[0]);
|
||||
int cn = (int)(sizeof cfrows / sizeof cfrows[0]);
|
||||
int total = 0, fail = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (run_driver(cdrv, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
/* cstage loud-stop tripwires (cgen.c) — expect CFAIL with "#58". */
|
||||
for (int i = 0; i < cn; i++) {
|
||||
total++;
|
||||
if (run_cfail(cdrv, &cfrows[i], i) != 0) fail++;
|
||||
}
|
||||
if (access(wdrv, X_OK) == 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (run_driver(wdrv, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
/* wwstage loud-stop tripwires (cgenexpr.ww). */
|
||||
for (int i = 0; i < cn; i++) {
|
||||
total++;
|
||||
if (run_cfail(wdrv, &cfrows[i], cn + i) != 0) fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "idx_tagged_field: %d/%d checks failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("idx_tagged_field: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user