Files
ww/test/wcc/989_idxarg_run.c
Hojun-Cho cbbfd1a4a6 wcc/ww: nodeisslice/nodeisstr gain the N_INDEX arm (stamp-keyed)
A slice/str ELEMENT of an indexed expression passed as a call-arg
pushed one word instead of the 24B/16B header — the predicates had no
N_INDEX arm, so element-typed args fell to the scalar path (review
findings #45/#46). Read the element-type stamp; dual-stage rows in
989_idxarg_run pin cs==ww (red 2/8 on pre-fix binaries).
2026-06-12 00:04:22 +09:00

205 lines
6.9 KiB
C

/*
* 989_idxarg_run — F7-c2 (#45/#46): an INDEXED slice/str element passed as
* a call argument must push its full multi-word header, both stages.
*
* THE BUG (cat-A silent miscompile, gate-blind): pushargsrev sizes a
* call arg via nodeisslice/nodeisstr (selfhost/cmd/wcc/cgenutil.ww). A
* str arg takes 2 slots (ptr+len), a slice 3 (ptr+len+cap); a scalar
* takes 1. Pre-fix:
* - nodeisslice had NO N_INDEX arm (#45) → `take(rows[i])` where the
* element is a slice fell to the scalar default: one PUSHQ AX, the
* callee read .len/.cap from stack residue (garbage len).
* - nodeisstr's N_INDEX arm was a base-kind whitelist that only knew
* N_IDENT and N_DOT bases (#46) → a chained index `take(m[1][1])`
* (the outer index's base is itself an N_INDEX) fell through to
* `return false` → 1-word push, garbage .len.
* cstage is type-keyed (cmd/w6c/cgen.c node_isslice/node_isstr =
* type_isXXX(n->type)), so it pushed the full header and ran correct —
* the divergence is the cat-A signature. 990-997 stay green because the
* bootstrap corpus never feeds an indexed slice/str element as a call
* arg, so this value miscompile is invisible to byte-id; only a runtime
* row catches it. THE FIX: both N_INDEX arms read the checker-stamped
* element type n.type_ (exprtype N_INDEX, check.ww), aligning wwstage UP.
*
* Rows (every row builds+runs on cstage `ww` and, when present, wwstage
* `ww_ww`; rule-10 — both stages must agree AND hit want_exit):
* row | shape | want
* -------------------+------------------------------------+------
* slice_elem_arg | take(rows[1]) rows:[2][]int | 2 [#45 bug]
* call_str_elem | take(getarr()[1]) getarr()->[]str | 5 [#46 bug:
* | the index's base is an N_CALL — non-ident/non-dot,
* | the shape the old base-kind whitelist missed. The
* | slice-base READ already loads the header, so this
* | isolates the PUSH-side fix (c2) cleanly.]
* ident_str_elem | take(arr[1]) arr:[3]str | 5 (#46 control:
* | the N_IDENT-base case the old whitelist DID handle —
* | must still work through the stamp read)
* slice_local_arg | take(xs) xs:[]int | 3 (control: the
* | N_IDENT-local arm, unchanged since c1)
*
* The chained-index `take(m[1][1])` shape (an N_INDEX base) is covered by
* the c3 sibling test (989_chainidx_run.c): its push side is fixed here,
* but it ALSO needs the cgindex read-side header load (#22/c3), so it is
* pinned where both halves are present.
*/
#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[] = {
/* (1) #45 — slice element `rows[1]` (a []int) as a call arg. Pre-fix
* nodeisslice had no N_INDEX arm → 1-word push, callee read garbage
* len. len(rows[1]) == len(b) == 2. */
{ "slice_elem_arg",
"package main;\n"
"fn take(xs: []int) int = { return len(xs): int; };\n"
"export fn main() int = {\n"
" let a: []int = [10, 20, 30, 40];\n"
" let b: []int = [1, 2];\n"
" let rows: [2][]int = [a, b];\n"
" return take(rows[1]);\n"
"};\n",
2 },
/* (2) #46 — index whose base is an N_CALL (`getarr()[1]`): a non-ident/
* non-dot base the old base-kind whitelist missed. The slice-base READ
* already loads the (ptr,len) header, so this isolates the PUSH-side
* fix (nodeisstr N_INDEX arm). len("ddddd") == 5. */
{ "call_str_elem",
"package main;\n"
"fn getarr() []str = {\n"
" let a: []str = [\"x\", \"ddddd\", \"zz\"];\n"
" return a;\n"
"};\n"
"fn take(s: str) int = { return len(s): int; };\n"
"export fn main() int = { return take(getarr()[1]); };\n",
5 },
/* (3) #46 control — `arr[1]` with an N_IDENT base (the case the old
* whitelist DID handle): the stamp read must not regress it. */
{ "ident_str_elem",
"package main;\n"
"fn take(s: str) int = { return len(s): int; };\n"
"export fn main() int = {\n"
" let arr: [3]str = [\"x\", \"ddddd\", \"zz\"];\n"
" return take(arr[1]);\n"
"};\n",
5 },
/* (4) control — a plain str/slice local arg (the c1 N_IDENT-local
* arm): pins that c2 leaves the non-indexed path alone. */
{ "slice_local_arg",
"package main;\n"
"fn take(xs: []int) int = { return len(xs): int; };\n"
"export fn main() int = {\n"
" let xs: []int = [7, 8, 9];\n"
" return take(xs);\n"
"};\n",
3 },
};
/* run_build — build+run `src` via `driver`; returns the binary's exit
* code, or -1 on a build failure. */
static int
run_build(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/idxarg_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/idxarg_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -2;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
int brc = runwait(cmd);
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = -1;
if (brc == 0) got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return brc == 0 ? got : -1;
}
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);
struct { const char *name; const char *drv; int gated; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) {
fprintf(stderr, "idxarg_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
for (int i = 0; i < n; i++) {
total++;
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != rows[i].want_exit) {
fprintf(stderr, "idxarg_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, rows[i].want_exit);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "idxarg_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("idxarg_run: %d/%d ok\n", total, total);
return 0;
}