Files
ww/test/wcc/932_str_elem_cap_run.c
Hojun-Cho 97707155ae cgen: str N_INDEX read -> 3-word {ptr,len,cap} -- Phase 2 F2 (both stages)
str element value read at N_INDEX dropped the cap word (2-word ptr,len load); str is 24B {ptr,len,cap} since 1140a59. A new named helper cgslicehdr (both stages) loads the full 3-word header and is called by the N_INDEX str-element sites, kind-gated type_isstr/elemisstr -- never size==24, since str and slice collide at 24B. The base-targeting word loads last (clobber-safe). cstage==wwstage byte-identical. The #9 typeassert leaf is split out to F2b (it needs a wwstage spill twin first).

test/wcc/932: table-driven runtime .cap-survives probe over both N_INDEX base forms and both drivers; verified fail-before/pass-after. NNN<950 mirrors the 928 precedent -- the fixtures are self-contained (/tmp, no imports), so rule-14's selfhost-sibling race does not apply.

main.combined.ww regenerated via the canonical make path (md5-stable) and committed alongside source, per the 1140a59 precedent.
2026-05-24 12:27:15 +09:00

174 lines
5.1 KiB
C

/*
* 932_str_elem_cap_run — runtime coverage for the F2 fold: a str-element
* VALUE read via N_INDEX must load the full 24B {ptr,len,cap} header, not
* just {ptr,len}. str is 24B since Phase 2 (#1); pre-F2 the N_INDEX
* str-element arms dropped the cap word.
*
* The existing byte-id gates (990-997) can't catch a no-op fold here: a
* shared 2-word miscompile passes byte-id silently. So this pins the
* *runtime* contract — build each fixture through both the cstage `ww`
* and the wwstage `ww_ww` driver and confirm the program's assertions
* hold (exit 0).
*
* Each row POISONS the element so cap != len (a `.cap =` pseudo-field
* write, no malloc / no import — keeps the fixture self-contained so
* ww_ww writes intermediates only next to the /tmp source, not lib/).
* A 2-word read leaves cap = len (Site A) or a stale slice-cap (Site B),
* so the read-back .cap mismatches the poisoned value and the row fails.
*
* Both N_INDEX base forms are covered:
* - Site A: N_IDENT base — `xs[0]` on a local `[N]str`.
* - Site B: fallback base — `b.items[0]` where the base `b.items` is
* an N_DOT (slice field), not a plain ident.
*
* The cap word is observed through `let e: str = <index>` (a 3-word
* copy into the slot) then `e.cap` (an N_IDENT pseudo-field read off the
* slot). A direct `<index>.cap` would NOT observe it — the non-ident
* .cap N_DOT path only handles .ptr/.len today (a separate gap, out of
* F2's scope).
*/
#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; };
static const struct row rows[] = {
/* Site A — N_IDENT base. Poison cap=8 (len=2), read back via a
* local [2]str. Broken 2-word read leaves cap = len = 2. */
{ "sitea_ident_base",
"export fn main() i32 = {\n"
" let p: str = \"hi\";\n"
" p.cap = 8i32;\n"
" let xs: [2]str;\n"
" xs[0] = p;\n"
" let e: str = xs[0];\n"
" if (e.cap: i32 != 8) { return 1; };\n"
" if (e.len: i32 != 2) { return 2; };\n"
" return 0;\n"
"};\n",
0 },
/* Site B — fallback base. Element behind an N_DOT slice field, so
* the N_INDEX base isn't a plain ident. Poison cap=9 (len=5).
* Broken read leaves cap = the slice header's own cap, not the
* element's. */
{ "siteb_fallback_base",
"type box = struct { items: []str };\n"
"export fn main() i32 = {\n"
" let q: str = \"world\";\n"
" q.cap = 9i32;\n"
" let arr: [2]str;\n"
" arr[0] = q;\n"
" let b: box;\n"
" b.items = arr[0:2];\n"
" let e: str = b.items[0];\n"
" if (e.cap: i32 != 9) { return 1; };\n"
" if (e.len: i32 != 5) { return 2; };\n"
" return 0;\n"
"};\n",
0 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char src[96], tmpdir[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/strelemcap_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/strelemcap_%d_d_%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 && %s build %s",
tmpdir, driver, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[160];
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); rmdir(tmpdir);
return got;
}
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 cdrv[640];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char wdrv[640];
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *path; int gated_on_existence; }
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_on_existence
&& access(drivers[d].path, X_OK) != 0) {
fprintf(stderr,
"str_elem_cap_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
int got = run_driver(drivers[d].path, &rows[i], i);
total++;
if (got != rows[i].want) {
fprintf(stderr,
"str_elem_cap_run[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "str_elem_cap_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("str_elem_cap_run: %d/%d ok\n", total, total);
return 0;
}