/* * 949_ptrarr_index_run — runtime + byte-id net for #61: indexing * through a pointer-to-array (`p[i]`, p: *[N]T) must stride by * size(T), the pointee array's ELEMENT, never by the whole-array * byte size. * * The family (one root class): * A. wwstage value-route scaling — elemsizeofc's #270-2 nested-array * block (selfhost/cmd/wcc/cgenutil.ww) fed a `*[N]T` pointee into * the "outer stride = whole sub-array" rule that is only correct * for [N][M]T / [][M]T. Every read/write/compound through `p[i]` * scaled by N*size(T) (OOB for any i>=1), and the same wrong * element belief reached the store-width chooser: a var-idx write * emitted an N*8-byte aggregate copy sourced at the 8B rhs slot — * OOB read of the frame neighborhood + OOB write at base+N*8*i, * caller-frame smash. This is exactly lib/hash/siphash round()'s * `v[0]=v0 .. v[3]=v3` corruption. cstage was runtime-correct * (idx_eff, cgen.c:1163, peels TY_PTR→TY_ARRAY); wwstage aligned * UP via the idxeffti/idxelemtn choke-point. * B. `&p[i]` addr-of route — BOTH stages identically wrong * (byte-id-BLIND): the TK_AMP &base[i] arm read bu->sub->size * without the ptr peel, so &p[3]-&a[0] returned 3*N*size(T). * Both stages converged on the idx_eff'd element size; only the * RUNTIME rows here can pin this class — the 990-997 byte-id * gates can never see a both-stages-identical miscompile. * * Each row carries (a) a cstage `ww build` + run asserting the exit * code and (b) a w6c vs w6c_ww `.s` cmp (rule-10 byte-id). Together * they pin BOTH stages: byte-id + cstage-runtime-correct implies * wwstage-runtime-correct. The matrix covers the elem widths the * scaling class is sensitive to ({1,2,4,8}B), const + var indices, * param / local / cast bases, read / write / compound routes, * neighbor-corruption guards, the &p[i] pointer-difference (B), and * the live consumer's mix-in-place shape (siphash round). * * C. `(*p)[i]` explicit deref + index — BOTH stages SEGV'd * identically (byte-id-blind): cgun's TK_STAR materialized an * 8-byte SCALAR load of a[0]'s value and the index used that * VALUE as its base — a wild deref. Fixed in both stages at the * deref choke-point: an ARRAY pointee takes the #185 *fn skip * (an array value IS its address, #270-1a), so `*p` leaves AX = * p's value and every index/addr-of/store route through * `cgexpr(base)` materializes the array address for free. The * deref_* rows pin read / write / compound at 8B and narrow * widths (the narrow rows also pin the wwstage N_UN-base * stamped-tinfo esz arm against the 8B default). */ #include #include #include #include #include #include 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[] = { /* A: read, const idx, param base, 8B elem (ken p2). Pre-fix * wwstage strode 32 → read past the array. */ { "rd_u64_param_const", "package main;\n" "fn rd(p: *[4]u64) u64 = {\n" " return p[1];\n" "};\n" "export fn main() i32 = {\n" " let a: [4]u64 = [100u64, 101u64, 102u64, 103u64];\n" " return rd(&a): i32;\n" "};\n", 101 }, /* A: read, var idx, 1B elem. */ { "rd_u8_param_var", "package main;\n" "fn rd(p: *[4]u8, i: i32) u8 = {\n" " return p[i];\n" "};\n" "export fn main() i32 = {\n" " let a: [4]u8 = [10u8, 20u8, 30u8, 40u8];\n" " return rd(&a, 2): i32;\n" "};\n", 30 }, /* A: read, 2B elem. */ { "rd_u16_param", "package main;\n" "fn rd(p: *[4]u16) u16 = {\n" " return p[3];\n" "};\n" "export fn main() i32 = {\n" " let a: [4]u16 = [11u16, 22u16, 33u16, 44u16];\n" " return rd(&a): i32;\n" "};\n", 44 }, /* A: read, 4B elem. */ { "rd_u32_param", "package main;\n" "fn rd(p: *[4]u32) u32 = {\n" " return p[2];\n" "};\n" "export fn main() i32 = {\n" " let a: [4]u32 = [11u32, 22u32, 33u32, 44u32];\n" " return rd(&a): i32;\n" "};\n", 33 }, /* A: signed-narrow elem behind the ptr — pins the MOVSXD * sign-extend the idxeffti'd elemissignedc picks (an undrilled * read of the ptr tinfo classified the ARRAY: unsigned). */ { "rd_i32_signed", "package main;\n" "fn rd(p: *[4]i32) i32 = {\n" " return p[1];\n" "};\n" "export fn main() i32 = {\n" " let a: [4]i32 = [7, -5, 9, 1];\n" " return rd(&a) + 10;\n" "};\n", 5 }, /* A: write, const idx (ken p3) — scale-only half. */ { "wr_u64_param_const", "package main;\n" "fn wr(p: *[4]u64) void = {\n" " p[1] = 7u64;\n" "};\n" "export fn main() i32 = {\n" " let a: [4]u64 = [1u64, 2u64, 3u64, 4u64];\n" " wr(&a);\n" " return (a[0] + a[1] + a[2]): i32;\n" "};\n", 11 }, /* A: write, VAR idx + param rhs (ken p10) — the corruption * proof: pre-fix wwstage emitted a 32B aggregate copy sourced * at &x (reading i, p, saved BP) to base+32*i → frame smash / * SIGSEGV. Neighbor guards assert no byte outside a[1] moved. */ { "wr_u64_varidx_paramrhs", "package main;\n" "fn wr(p: *[4]u64, i: i32, x: u64) void = {\n" " p[i] = x;\n" "};\n" "export fn main() i32 = {\n" " let a: [4]u64 = [10u64, 11u64, 12u64, 13u64];\n" " let i: i32 = 1;\n" " wr(&a, i, 77u64);\n" " if (a[0] != 10u64) { return 1; };\n" " if (a[1] != 77u64) { return 2; };\n" " if (a[2] != 12u64) { return 3; };\n" " if (a[3] != 13u64) { return 4; };\n" " return 0;\n" "};\n", 0 }, /* A: write, 1B elem, neighbor guards at the tightest width. */ { "wr_u8_neighbors", "package main;\n" "fn wr(p: *[4]u8) void = {\n" " p[1] = 9u8;\n" "};\n" "export fn main() i32 = {\n" " let a: [4]u8 = [1u8, 2u8, 3u8, 4u8];\n" " wr(&a);\n" " return (a[0] + a[1] + a[2]): i32;\n" "};\n", 13 }, /* A: compound, const idx, 8B elem (ken p9b). */ { "compound_u64", "package main;\n" "fn add5(p: *[4]u64) void = {\n" " p[1] += 5u64;\n" "};\n" "export fn main() i32 = {\n" " let a: [4]u64 = [100u64, 101u64, 102u64, 103u64];\n" " add5(&a);\n" " return a[1]: i32;\n" "};\n", 106 }, /* A: compound, var idx, 4B elem. */ { "compound_u32_varidx", "package main;\n" "fn addat(p: *[4]u32, i: i32) void = {\n" " p[i] += 7u32;\n" "};\n" "export fn main() i32 = {\n" " let a: [4]u32 = [10u32, 20u32, 30u32, 40u32];\n" " addat(&a, 2);\n" " return a[2]: i32;\n" "};\n", 37 }, /* A: local-ptr base, no call boundary (ken p4). */ { "rd_localptr", "package main;\n" "export fn main() i32 = {\n" " let a: [4]u64 = [100u64, 101u64, 102u64, 103u64];\n" " let p: *[4]u64 = &a;\n" " return p[2]: i32;\n" "};\n", 102 }, /* A: cast base (ken p6). */ { "rd_castbase", "package main;\n" "export fn main() i32 = {\n" " let big: [4]u64 = [5u64, 6u64, 7u64, 8u64];\n" " let p: *[4]u64 = (&big): *[4]u64;\n" " return p[1]: i32;\n" "};\n", 6 }, /* A: nested *[2][3]u32 (ken p16) — the elemsizeofc N_TPTR * carve-out must coexist with the #270-2 outer-stride rule for * the pointee's OWN nesting: inner stride 4, outer 12. Read, * write, neighbor guards; param + local ptr bases. */ { "nested_2d", "package main;\n" "fn rd(p: *[2][3]u32, i: i32, j: i32) u32 = { return p[i][j]; };\n" "export fn main() i32 = {\n" " let a: [2][3]u32;\n" " a[0][0] = 0: u32; a[0][1] = 1: u32; a[0][2] = 2: u32;\n" " a[1][0] = 10: u32; a[1][1] = 11: u32; a[1][2] = 12: u32;\n" " if (rd(&a, 1, 2) != 12: u32) { return 1; };\n" " if (rd(&a, 0, 1) != 1: u32) { return 2; };\n" " let p: *[2][3]u32 = &a;\n" " p[1][0] = 99: u32;\n" " if (a[1][0] != 99: u32) { return 3; };\n" " if (a[1][1] != 11: u32) { return 4; };\n" " if (a[0][2] != 2: u32) { return 5; };\n" " return 0;\n" "};\n", 0 }, /* A: *[3]str — 3-word (ptr,len,cap) header elements; pins the * read-side str-header gate on the idx_eff'd element. Pre-fix * BOTH stages were runtime-wrong here, differently: cstage's * u->sub gate missed the ptr base and dropped len/cap (ken * p17). */ { "rd_str_elem", "package main;\n" "fn lenof(p: *[3]str, i: i32) i32 = { return p[i].len; };\n" "export fn main() i32 = {\n" " let a: [3]str;\n" " a[0] = \"x\"; a[1] = \"yy\"; a[2] = \"zzz\";\n" " if (lenof(&a, 2) != 3) { return 1; };\n" " if (lenof(&a, 0) != 1) { return 2; };\n" " let p: *[3]str = &a;\n" " if (p[1].len != 2) { return 3; };\n" " return 0;\n" "};\n", 0 }, /* B: &p[i] pointer difference (ken p8b) — both stages emitted * the whole-array stride (96) byte-IDENTICALLY pre-fix; only * this runtime row can see the class. 3 * size(u64) = 24. */ { "amp_diff_u64", "package main;\n" "export fn main() i32 = {\n" " let a: [4]u64 = [1u64, 2u64, 3u64, 4u64];\n" " let p: *[4]u64 = &a;\n" " let d: u64 = (&p[3]): u64 - (&a[0]): u64;\n" " return d: i32;\n" "};\n", 24 }, /* B: &p[i] difference at a narrow width. 3 * size(u16) = 6. */ { "amp_diff_u16", "package main;\n" "export fn main() i32 = {\n" " let a: [4]u16 = [1u16, 2u16, 3u16, 4u16];\n" " let p: *[4]u16 = &a;\n" " let d: u64 = (&p[3]): u64 - (&a[0]): u64;\n" " return d: i32;\n" "};\n", 6 }, /* A: the live consumer's shape — siphash round() mutates all * four lanes through the param ptr, each read feeding a later * write. Pre-fix wwstage smashed the caller frame here. */ { "mix_inplace_round", "package main;\n" "fn mix(v: *[4]u64) void = {\n" " v[0] += v[1];\n" " v[2] += v[3];\n" " v[1] += v[0];\n" " v[3] += v[2];\n" "};\n" "export fn main() i32 = {\n" " let v: [4]u64 = [1u64, 2u64, 3u64, 4u64];\n" " mix(&v);\n" " if (v[0] != 3u64) { return 1; };\n" " if (v[1] != 5u64) { return 2; };\n" " if (v[2] != 7u64) { return 3; };\n" " if (v[3] != 11u64) { return 4; };\n" " return 0;\n" "};\n", 0 }, /* C: (*p)[i] read, 8B elem (ken p7 — SEGV'd both stages). */ { "deref_rd_u64", "package main;\n" "export fn main() i32 = {\n" " let a: [4]u64 = [100u64, 101u64, 102u64, 103u64];\n" " let p: *[4]u64 = &a;\n" " return (*p)[1]: i32;\n" "};\n", 101 }, /* C: (*p)[i] read, 4B elem — pins the N_UN-base esz arm (the 8B * default would mis-stride once the base materializes). */ { "deref_rd_u32", "package main;\n" "export fn main() i32 = {\n" " let a: [4]u32 = [11u32, 22u32, 33u32, 44u32];\n" " let p: *[4]u32 = &a;\n" " return (*p)[2]: i32;\n" "};\n", 33 }, /* C: (*p)[i] read through a param base. */ { "deref_rd_param", "package main;\n" "fn rd(p: *[4]u64) u64 = {\n" " return (*p)[1];\n" "};\n" "export fn main() i32 = {\n" " let a: [4]u64 = [100u64, 101u64, 102u64, 103u64];\n" " return rd(&a): i32;\n" "};\n", 101 }, /* C: (*p)[i] write, 8B elem. */ { "deref_wr_u64", "package main;\n" "export fn main() i32 = {\n" " let a: [4]u64 = [1u64, 2u64, 3u64, 4u64];\n" " let p: *[4]u64 = &a;\n" " (*p)[1] = 7u64;\n" " return (a[0] + a[1] + a[2]): i32;\n" "};\n", 11 }, /* C: (*p)[i] write, 1B elem, neighbor guards. */ { "deref_wr_u8", "package main;\n" "export fn main() i32 = {\n" " let a: [4]u8 = [1u8, 2u8, 3u8, 4u8];\n" " let p: *[4]u8 = &a;\n" " (*p)[1] = 9u8;\n" " return (a[0] + a[1] + a[2]): i32;\n" "};\n", 13 }, /* C: (*p)[i] compound. */ { "deref_compound_u64", "package main;\n" "export fn main() i32 = {\n" " let a: [4]u64 = [100u64, 101u64, 102u64, 103u64];\n" " let p: *[4]u64 = &a;\n" " (*p)[1] += 5u64;\n" " return a[1]: i32;\n" "};\n", 106 }, { 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, "ptrarr_index: w6c_ww missing — cannot run " "the cs==ww byte-id gate\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/wwpai_%d_%d.ww", getpid(), i); FILE *f = fopen(src, "wb"); if (f == NULL) { fail++; continue; } fputs(rows[i].src, f); fclose(f); /* (a) cstage build + run. */ char tmpdir[64]; snprintf(tmpdir, sizeof tmpdir, "/tmp/wwpai_%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); /* (b) cs==ww byte-id gate. */ char cs_s[64], ws_s[64]; snprintf(cs_s, sizeof cs_s, "/tmp/wwpai_%d_%d_cs.s", getpid(), i); snprintf(ws_s, sizeof ws_s, "/tmp/wwpai_%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 ptrarr-index tests failed\n", fail, n); return 1; } printf("ptrarr_index: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n); return 0; }