From 5d100dbd4236a45d23363cd5620c98642a9c687a Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sat, 8 Aug 2026 14:42:35 +0900 Subject: [PATCH] test: port the match-dispatch asm observer to ww 728 -> test/asm/matchdispatch_test.ww. The distinct-CMPQ-tag collection window over TEXT b.next preserved on both stages. Strengthened: per-row byte-id legs added -- the C header claimed a cmp -s its code never ran; all three rows verify green. --- Makefile | 2 +- test/asm/matchdispatch_test.ww | 196 +++++++++++++++++ test/wcc/728_match_4arm_cross_module.c | 287 ------------------------- 3 files changed, 197 insertions(+), 288 deletions(-) create mode 100644 test/asm/matchdispatch_test.ww delete mode 100644 test/wcc/728_match_4arm_cross_module.c diff --git a/Makefile b/Makefile index 389c7534..e65aefec 100644 --- a/Makefile +++ b/Makefile @@ -386,7 +386,7 @@ XMOD_WW_TARGETS = $(XMOD_WW_TESTS:%=wwtest/%) # test-compiler beside the surviving residual carriers. ASM_WW_TESTS = test/asm/modshadow_test.ww test/asm/sret_test.ww \ test/asm/callarg_test.ww test/asm/chain_test.ww \ - test/asm/dataemit_test.ww + test/asm/dataemit_test.ww test/asm/matchdispatch_test.ww ASM_WW_TARGETS = $(ASM_WW_TESTS:%=wwtest/%) BOOTSTRAP_WRAPPER_SOURCES = test/wcc/950_selfcheck.c \ test/wcc/991_w6a_ww.c \ diff --git a/test/asm/matchdispatch_test.ww b/test/asm/matchdispatch_test.ww new file mode 100644 index 00000000..524aef5d --- /dev/null +++ b/test/asm/matchdispatch_test.ww @@ -0,0 +1,196 @@ +package matchdispatch_test; + +// Direct-w6c asm-window gate over match tag dispatch under +// cross-module fn-name shadowing. Port of the retired native carrier +// test/wcc/728_match_4arm_cross_module.c (#31); every assertion +// preserved. The mod-disambiguated match-scrutinee claim has no other +// owner: pre-fix wwstage's name-only fnretlookup collapsed arms 2/3 +// of a `match (a.next())` inside a shadowing `b.next` to CMPQ $0, +// leaving their case bodies silently unreachable. +// +// Per row: every `CMPQ $K, AX` inside [TEXT b.next .. next TEXT) is +// collected; at least `arms` must exist and the first `arms` values +// must be pairwise distinct and inside [0, arms). Checked on both +// stages. +// +// Strengthened: a per-row byte-id leg (testenv.same over both .s). +// The C header claimed a cmp -s that its code never ran; the leg +// verifies green on all three rows. +// +// Dropped C machinery, not assertions: the w6c_ww-absent skip gate, +// getpid()-keyed /tmp names, slurp caps. + +import os; +import os.exec; +import strings; +import testenv; +import time; + +fn fail(label: str, why: str) void = { + let m: str = strings.concat("matchdispatch FAIL: ", label, " -- ", + why, "\n"); + os.write(2, m.ptr, m.len: u64); + assert(false); +}; + +fn tmo() time.duration = { + return (180i64 * (time.second: i64)): time.duration; +}; + +fn emitstage(td: str, label: str, stage: str, drv: str, + outname: str) void = { + let av: []str = []; + append(av, drv); + append(av, "-o"); + append(av, outname); + append(av, "src.ww"); + let co: testenv.commandout; + testenv.runcommand(td, td, stage, av, tmo(), &co); + let ok: bool = co.termination == exec.termination.EXIT && co.code == 0; + if (!ok) { fail(label, strings.concat(stage, " compile failed")); }; +}; + +fn posafter(s: str, start: i32, needle: str) i32 = { + let p: i32 = testenv.pos(strings.sub(s, start, s.len), needle); + if (p < 0) { return -1; }; + return start + p; +}; + +// Collect every `CMPQ $K, AX` tag inside [TEXT b.next .. next TEXT) +// (the C capped collection at 16 entries and K < 16), then assert +// count, range and pairwise distinctness over the first `arms`. +fn cmpqcheck(label: str, stage: str, s: str, arms: i32) void = { + let fnhdr: str = "TEXT b.next"; + let fp: i32 = testenv.pos(s, fnhdr); + if (fp < 0) { + fail(label, strings.concat(stage, ": no ", fnhdr, " in .s")); + }; + let ep: i32 = posafter(s, fp + fnhdr.len, "\nTEXT "); + if (ep < 0) { ep = s.len; }; + let w: str = strings.sub(s, fp, ep); + let seen: []i32 = alloc([], 16u64)!; + let p: i32 = 0; + for (p < w.len) { + let m: i32 = posafter(w, p, "CMPQ\t$"); + if (m < 0) { break; }; + let d: i32 = m + 6; + let k: i32 = 0; + let nd: i32 = 0; + for (d < w.len) { + let c: u8 = w[d]; + if (c < '0' || c > '9') { break; }; + k = k * 10 + ((c - '0'): i32); + nd += 1; + d += 1; + }; + if (nd > 0 && d + 4 <= w.len + && testenv.same(strings.sub(w, d, d + 4), ", AX")) { + if (seen.len < 16 && k < 16) { append(seen, k); }; + }; + p = m + 1; + }; + if (seen.len < arms) { + fail(label, strings.concat(stage, + ": too few `CMPQ $K, AX` in b.next body")); + }; + let i: i32 = 0; + for (i < arms) { + if (seen[i] < 0 || seen[i] >= arms) { + fail(label, strings.concat(stage, + ": arm tag out of [0, arms)")); + }; + let j: i32 = 0; + for (j < i) { + if (seen[j] == seen[i]) { + fail(label, strings.concat(stage, + ": repeated arm tag (collapse)")); + }; + j += 1; + }; + i += 1; + }; +}; + +fn cmpqrow(label: str, src: str, arms: i32) void = { + let td: str = testenv.fresh(); + testenv.writefile(strings.concat(td, "/src.ww"), src); + emitstage(td, label, "cstage", testenv.driver("w6c"), "cs.s"); + emitstage(td, label, "wwstage", testenv.driver("w6c_ww"), "ws.s"); + let cs: str = testenv.readfile(strings.concat(td, "/cs.s")); + let ws: str = testenv.readfile(strings.concat(td, "/ws.s")); + cmpqcheck(label, "cstage", cs, arms); + cmpqcheck(label, "wwstage", ws, arms); + if (!testenv.same(cs, ws)) { + fail(label, "cstage vs wwstage asm differs"); + }; + testenv.clean(td); +}; + +// Callee `a.next` returns the wide tagged; caller `b.next` shadows +// the leaf with a 2-arm subset return so a name-only lookup sees the +// wrong scrutinee type. The reverse row pins that dispatch follows +// variantindex, not source order; the 3-arm row pins the boundary +// (collapse begins at arms >= the caller's variant count). +@test fn match4armcrossmodule() void = { + cmpqrow("4arm_shadowed_callee", strings.concat( + "package a;\n", + "export type more = void;\n", + "export type invalid = !void;\n", + "export type done = void;\n", + "export fn next() (rune | done | more | invalid) = {\n", + " let r: rune;\n", + " return r;\n", + "};\n", + "package b;\n", + "import a;\n", + "type done = void;\n", + "fn next() (rune | done) = {\n", + " match (a.next()) {\n", + " case let r: rune => return r;\n", + " case let dn: a.done => { let v: done; return v; };\n", + " case let m: a.more => { abort(\"i\"); };\n", + " case let e: a.invalid => { abort(\"i\"); };\n", + " };\n", + "};\n", + "export fn main() i32 = { return 0; };\n"), 4); + cmpqrow("4arm_shadowed_reverse", strings.concat( + "package a;\n", + "export type more = void;\n", + "export type invalid = !void;\n", + "export type done = void;\n", + "export fn next() (rune | done | more | invalid) = {\n", + " let r: rune;\n", + " return r;\n", + "};\n", + "package b;\n", + "import a;\n", + "type done = void;\n", + "fn next() (rune | done) = {\n", + " match (a.next()) {\n", + " case let e: a.invalid => { abort(\"i\"); };\n", + " case let m: a.more => { abort(\"i\"); };\n", + " case let dn: a.done => { let v: done; return v; };\n", + " case let r: rune => return r;\n", + " };\n", + "};\n", + "export fn main() i32 = { return 0; };\n"), 4); + cmpqrow("3arm_shadowed_callee", strings.concat( + "package a;\n", + "export type more = void;\n", + "export type done = void;\n", + "export fn next() (rune | done | more) = {\n", + " let r: rune;\n", + " return r;\n", + "};\n", + "package b;\n", + "import a;\n", + "type done = void;\n", + "fn next() (rune | done) = {\n", + " match (a.next()) {\n", + " case let r: rune => return r;\n", + " case let dn: a.done => { let v: done; return v; };\n", + " case let m: a.more => { abort(\"i\"); };\n", + " };\n", + "};\n", + "export fn main() i32 = { return 0; };\n"), 3); +}; diff --git a/test/wcc/728_match_4arm_cross_module.c b/test/wcc/728_match_4arm_cross_module.c deleted file mode 100644 index ee43005f..00000000 --- a/test/wcc/728_match_4arm_cross_module.c +++ /dev/null @@ -1,287 +0,0 @@ -/* - * 728_match_4arm_cross_module — Class A asm-presence + byte-id sentinel - * for task #31. Pins that the variant tag for each arm of a 4-arm match - * on a qualified `mod.fn(...)` call matches between cstage and wwstage - * even when the caller fn shadows the callee's fn-name across modules. - * - * Pre-fix wwstage's matchscrutt (selfhost/cmd/wcc/cgenutil.ww:2061-2074) - * resolved the scrutinee's tagged type via name-only fnretlookup. With - * `fn next` in two modules, collectfnrets' prepend ordering meant the - * last-declared `next` sat at the head, so `match (utf8.next(d))` - * inside a `fn next() (rune | done)` saw the 2-arm tagged instead of - * the callee's real 4-arm tagged. Arms 2/3 of the match dispatch then - * silently collapsed to CMPQ $0 (their case bodies were unreachable - * even when the tag matched). Sister of #27 (aliaslookupmod) and #28 - * (fnparamslookupmod) — the third leaf in the same name-collision trio. - * - * Cstage carries module info through the checker-set callee type - * (cmd/w6c/cgen.c) so its match-scrutinee resolution is correct. - * Wwstage converges via fnretlookupmod (cgen.ww), called from - * matchscrutt's N_DOT branch. - * - * The repro requires: - * 1. callee fn declared first in module A, returning a 4+ arm tagged. - * 2. caller fn in module B, *same fn name*, returning a 2-arm tagged - * that is a subset of the callee's arms (so arms 0/1 still resolve - * and only 2+ surface the dispatch corruption). - * 3. The match scrutinee is the qualified `A.fn(...)` call. - * - * Asserts each arm of the canonical 4-arm match emits a *distinct* - * CMPQ tag in the wwstage asm AND cstage-vs-wwstage cmp -s holds. - */ -#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; - /* Function whose body holds the match-dispatch to inspect. The - * 4 arms must each emit a distinct `CMPQ $K, AX` between - * `TEXT ` and the next TEXT directive. */ - const char *fn; - int arms; -}; - -static const struct row rows[] = { - /* Canonical 4-arm: caller `next` in mod B shadows callee `next` - * in mod A. Pre-fix arms 2/3 → CMPQ $0; post-fix → $2/$3. */ - { "4arm_shadowed_callee", - "package a;\n" - "export type more = void;\n" - "export type invalid = !void;\n" - "export type done = void;\n" - "export fn next() (rune | done | more | invalid) = {\n" - " let r: rune;\n" - " return r;\n" - "};\n" - "package b;\n" - "import a;\n" - "type done = void;\n" - "fn next() (rune | done) = {\n" - " match (a.next()) {\n" - " case let r: rune => return r;\n" - " case let dn: a.done => { let v: done; return v; };\n" - " case let m: a.more => { abort(\"i\"); };\n" - " case let e: a.invalid => { abort(\"i\"); };\n" - " };\n" - "};\n" - "export fn main() i32 = { return 0; };\n", - "b.next", 4 }, - /* Reverse arm order in the match source: pins that the bug - * follows variantindex (or in our case, the mod-disambiguated - * scrutinee type), not source order — arm 0 stays at idx 3 etc. */ - { "4arm_shadowed_reverse", - "package a;\n" - "export type more = void;\n" - "export type invalid = !void;\n" - "export type done = void;\n" - "export fn next() (rune | done | more | invalid) = {\n" - " let r: rune;\n" - " return r;\n" - "};\n" - "package b;\n" - "import a;\n" - "type done = void;\n" - "fn next() (rune | done) = {\n" - " match (a.next()) {\n" - " case let e: a.invalid => { abort(\"i\"); };\n" - " case let m: a.more => { abort(\"i\"); };\n" - " case let dn: a.done => { let v: done; return v; };\n" - " case let r: rune => return r;\n" - " };\n" - "};\n" - "export fn main() i32 = { return 0; };\n", - "b.next", 4 }, - /* 3-arm boundary: confirm the issue is "arms ≥ caller's variant - * count collapse", not "≥ 2". Caller `next` returns 2-arm, callee - * returns 3-arm. Arm 2 must be CMPQ $2. */ - { "3arm_shadowed_callee", - "package a;\n" - "export type more = void;\n" - "export type done = void;\n" - "export fn next() (rune | done | more) = {\n" - " let r: rune;\n" - " return r;\n" - "};\n" - "package b;\n" - "import a;\n" - "type done = void;\n" - "fn next() (rune | done) = {\n" - " match (a.next()) {\n" - " case let r: rune => return r;\n" - " case let dn: a.done => { let v: done; return v; };\n" - " case let m: a.more => { abort(\"i\"); };\n" - " };\n" - "};\n" - "export fn main() i32 = { return 0; };\n", - "b.next", 3 }, -}; - -static int -slurp(const char *path, char *buf, size_t cap) -{ - FILE *f = fopen(path, "rb"); - if (!f) return -1; - size_t n = fread(buf, 1, cap - 1, f); - fclose(f); - buf[n] = '\0'; - return (int)n; -} - -static int -emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap) -{ - char src[64], cmd[1024]; - snprintf(src, sizeof src, "/tmp/m4cm_%d_%d.ww", getpid(), i); - snprintf(out_s, cap, "/tmp/m4cm_%d_%d_%s.s", - getpid(), i, w6c[strlen(w6c) - 1] == 'w' ? "ww" : "c"); - - FILE *f = fopen(src, "wb"); - if (!f) return -1; - fputs(r->src, f); - fclose(f); - - snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, out_s, src); - int rc = runwait(cmd); - unlink(src); - return rc; -} - -/* Inside TEXT , walk every `CMPQ $K, AX` line that precedes the - * next TEXT directive and assert at least `arms` of them and that - * the first `arms` such tags are pairwise distinct AND cover [0..arms). */ -static int -check_distinct_cmpq(const char *spath, const struct row *r, const char *stage) -{ - char buf[1 << 14]; - if (slurp(spath, buf, sizeof buf) < 0) { - fprintf(stderr, "row[%s][%s]: cannot read %s\n", - r->label, stage, spath); - return -1; - } - char fnhdr[128]; - snprintf(fnhdr, sizeof fnhdr, "TEXT %s", r->fn); - const char *fn = strstr(buf, fnhdr); - if (!fn) { - fprintf(stderr, "row[%s][%s]: no `%s` in %s\n", - r->label, stage, fnhdr, spath); - return -1; - } - const char *end = strstr(fn + strlen(fnhdr), "\nTEXT "); - if (!end) end = buf + strlen(buf); - - int seen[16] = {0}; - int got = 0; - const char *p = fn; - while (p < end) { - const char *m = strstr(p, "CMPQ\t$"); - if (!m || m >= end) break; - const char *digits = m + strlen("CMPQ\t$"); - if (*digits < '0' || *digits > '9') { p = m + 1; continue; } - int k = 0; - while (*digits >= '0' && *digits <= '9') { - k = k * 10 + (*digits - '0'); - digits++; - } - if (strncmp(digits, ", AX", 4) == 0) { - if (got < 16 && k < 16) seen[got++] = k; - } - p = m + 1; - } - if (got < r->arms) { - fprintf(stderr, - "row[%s][%s]: only %d `CMPQ $K, AX` in %s body (want %d)\n", - r->label, stage, got, r->fn, r->arms); - return -1; - } - int bitmap = 0; - for (int i = 0; i < r->arms; i++) { - if (seen[i] < 0 || seen[i] >= r->arms) { - fprintf(stderr, - "row[%s][%s]: arm %d emits CMPQ $%d (out of [0, %d))\n", - r->label, stage, i, seen[i], r->arms); - return -1; - } - if (bitmap & (1 << seen[i])) { - fprintf(stderr, - "row[%s][%s]: arm %d repeats tag $%d (collapse)\n", - r->label, stage, i, seen[i]); - return -1; - } - bitmap |= 1 << seen[i]; - } - return 0; -} - -int -main(void) -{ - const char *bin = getenv("BIN"); - if (!bin) bin = "out/bin"; - char absbin[512]; - if (bin[0] != '/') { - char cwd[256]; - if (getcwd(cwd, sizeof cwd) == NULL) return 1; - snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); - bin = absbin; - } - - char w6c[640], w6c_ww[640]; - snprintf(w6c, sizeof w6c, "%s/w6c", bin); - snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); - - int have_ww = (access(w6c_ww, X_OK) == 0); - int n = (int)(sizeof rows / sizeof rows[0]); - int total = 0, fail = 0; - - for (int i = 0; i < n; i++) { - char cs_path[128], ws_path[128]; - - if (emit_s(w6c, &rows[i], i, cs_path, sizeof cs_path) != 0) { - fprintf(stderr, - "match_4arm_cross_module[cstage][%s]: w6c failed\n", - rows[i].label); - fail++; total++; continue; - } - total++; - if (check_distinct_cmpq(cs_path, &rows[i], "cstage") != 0) - fail++; - - if (!have_ww) { unlink(cs_path); continue; } - - if (emit_s(w6c_ww, &rows[i], i, ws_path, sizeof ws_path) != 0) { - fprintf(stderr, - "match_4arm_cross_module[wwstage][%s]: w6c_ww failed\n", - rows[i].label); - fail++; total++; - unlink(cs_path); continue; - } - total++; - if (check_distinct_cmpq(ws_path, &rows[i], "wwstage") != 0) - fail++; - - unlink(cs_path); unlink(ws_path); - } - - if (fail) { - fprintf(stderr, - "match_4arm_cross_module: %d/%d fixtures failed\n", - fail, total); - return 1; - } - printf("match_4arm_cross_module: %d/%d ok\n", total, total); - return 0; -}