Files
ww/test/wcc/680_arr_elem_field.c
Hojun-Cho ce3a25a0b4 test: contain sepwork scratch per-driver tmpdir, fix /tmp+in-repo leak (#8)
The wcc test drivers ran `ww build <bare-/tmp src>` with no -o, so the
compiler's <stem>.sepwork scratch landed beside the source and was never
cleaned: unbounded /tmp growth (2195 stale dirs observed) that fills tmpfs
and fabricates phantom test failures + silent harness aborts, and for
in-repo fixture builds leaked .sepwork into the tracked tree.

Each leaking build now writes its source + output inside a per-invocation
tmpdir, passes -o <tmpdir>/<stem> so the .sepwork lands inside it, and
rm -rf's the tmpdir on every exit path -- including fopen-fail and the
expected-fail reject builds (scratch is mkdir'd before the build can fail).
`ww run` and explicit-`-o`/byte-id helpers are left as-is; the 990/993
byte-id comparison logic is byte-for-byte unchanged.

Two items filed separately (this commit holds the no-Makefile / no-main.c
rail):
- #13: a stale <src>.s byte-id readback (749) silently no-ops since
  separate-compile emits .s to <ostem>.sepwork/__root.s; documented inline.
- #14: build-system Makefile recipes build selfhost/cmd/*/main.ww with no
  -o and leak main.sepwork in-tree (bounded, gitignored; own commit).

One concern -- sepwork leak hygiene -- across 228 drivers; uniform
transform applied per-file and two-round reviewed. make test: all 402
passed, zero net-new /tmp scratch, zero test-driven in-repo .sepwork.
2026-06-22 23:29:39 +09:00

228 lines
7.1 KiB
C

/*
* 680_arr_elem_field — `arr[i].field` lowers to a single (idx*esz +
* base) → field-load sequence, not a fall-through that returns the
* element value (or address) as the dot's result. Filed from task #8
* (originally framed as a wwstage bug; investigation flipped — cstage's
* N_DOT had no N_INDEX-lhs branch and fell through, leaving the
* caller's (AX, BX) shape junk for str/scalar leaf consumers).
*
* The cgenutil dotchainresolve workaround spills via `let nd = stk[i];
* nd.str` to keep cstage from seeing the direct shape; this test pins
* the direct shape so the spill can be retired in a follow-up.
*
* Coverage — both `[N]*Struct` and `[N]Struct` shapes, str / i32 / u8
* fields. Cstage and wwstage each on every fixture; wwstage gated on
* access(X_OK).
*
* `&arr[i].field` (TK_AMP through chained N_DOT N_INDEX) is task #9
* territory and intentionally out of scope here. Write-side
* `arr[i].field = v` is covered elsewhere — the wwstage write gap
* surfaces separately.
*/
#include <stdio.h>
#include <stdlib.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[] = {
/* [N]*Struct, str field at non-zero offset. The original
* task-#8 repro — stk[0].name where stk: [16]*nd.
* Returns len("hello") = 5. */
{ "ptr_arr_str_field",
"type nd = struct {\n"
" a: i32, b: i32,\n"
" name: str,\n"
"};\n"
"fn main() i32 = {\n"
" let v: nd; v.a = 1; v.b = 2; v.name = \"hello\";\n"
" let stk: [16]*nd; stk[0] = &v;\n"
" let s: str = stk[0].name;\n"
" return s.len: i32;\n"
"};\n",
5 },
/* [N]*Struct, scalar i32 field. Pins the non-str leaf load
* path through fldloadop (MOVSXD here for i32). Returns 42. */
{ "ptr_arr_i32_field",
"type nd = struct { name: str, x: i32 };\n"
"fn main() i32 = {\n"
" let v: nd; v.name = \"hi\"; v.x = 42;\n"
" let stk: [16]*nd; stk[0] = &v;\n"
" return stk[0].x;\n"
"};\n",
42 },
/* [N]*Struct, u8 field — pins the sub-word zero-extend (MOVZBQ)
* branch. Returns 7. */
{ "ptr_arr_u8_field",
"type nd = struct { tag: u8, pad: u8, x: i32 };\n"
"fn main() i32 = {\n"
" let v: nd; v.tag = 7u8; v.pad = 0u8; v.x = 99;\n"
" let stk: [8]*nd; stk[0] = &v;\n"
" return stk[0].tag: i32;\n"
"};\n",
7 },
/* [N]Struct (value array), str field. Element address goes
* straight into AX; field load reads at foff(AX). Returns
* len("world") = 5. */
{ "val_arr_str_field",
"type nd = struct { name: str, x: i32 };\n"
"fn setit(p: *nd, s: str, vv: i32) void = { p.name = s; p.x = vv; };\n"
"fn main() i32 = {\n"
" let arr: [4]nd;\n"
" setit(&arr[1], \"world\", 100);\n"
" let s: str = arr[1].name;\n"
" return s.len: i32;\n"
"};\n",
5 },
/* [N]Struct, scalar i32 field. Pins the value-array path for a
* non-str leaf — same address compute, MOVL load. Returns 100. */
{ "val_arr_i32_field",
"type nd = struct { name: str, x: i32 };\n"
"fn setit(p: *nd, s: str, vv: i32) void = { p.name = s; p.x = vv; };\n"
"fn main() i32 = {\n"
" let arr: [4]nd;\n"
" setit(&arr[2], \"q\", 100);\n"
" return arr[2].x;\n"
"};\n",
100 },
/* [N]Struct, u8 leaf — pins the value-array sub-word path
* (no MOVQ-to-deref, direct address arithmetic into AX) then
* fldloadop's MOVZBQ. Returns 9. */
{ "val_arr_u8_field",
"type nd = struct { tag: u8, pad: u8, x: i32 };\n"
"fn setit(p: *nd, t: u8, vv: i32) void = { p.tag = t; p.x = vv; };\n"
"fn main() i32 = {\n"
" let arr: [4]nd;\n"
" setit(&arr[2], 9u8, 50);\n"
" return arr[2].tag: i32;\n"
"};\n",
9 },
/* [N]*Struct, i8 leaf with a negative value — pins that
* fldloadop sign-extends (MOVBQSX) through the new branch
* rather than the old MOVZBQ. Compare against -3 in i32 to
* avoid the WEXITSTATUS truncation collision (sign-ext: -3,
* zero-ext: 253 — both clash mod 256 if returned directly). */
{ "ptr_arr_i8_signed",
"type nd = struct { tag: i8, pad: u8, x: i32 };\n"
"fn main() i32 = {\n"
" let v: nd; v.tag = -3i8; v.pad = 0u8; v.x = 0;\n"
" let stk: [4]*nd; stk[0] = &v;\n"
" let t: i32 = stk[0].tag: i32;\n"
" if (t == -3) { return 42; };\n"
" return 0;\n"
"};\n",
42 },
/* Idx is a non-zero variable expression — pins the IMULQ path
* and confirms the address compute doesn't accidentally fold
* to a constant displacement. Returns len("third") = 5. */
{ "ptr_arr_dyn_idx",
"type nd = struct { name: str, x: i32 };\n"
"fn main() i32 = {\n"
" let a: nd; a.name = \"first\"; a.x = 1;\n"
" let b: nd; b.name = \"two\"; b.x = 2;\n"
" let c: nd; c.name = \"third\"; c.x = 3;\n"
" let stk: [4]*nd;\n"
" stk[0] = &a; stk[1] = &b; stk[2] = &c;\n"
" let i: i32 = 2;\n"
" let s: str = stk[i].name;\n"
" return s.len: i32;\n"
"};\n",
5 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wae_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/wae_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wae_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -1; }
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s",
driver, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
runwait(rmcmd);
return -1;
}
int got = runwait(outbin);
runwait(rmcmd);
return got;
}
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];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char wdrv[1024];
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, "arr_elem_field: 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,
"arr_elem_field[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr,
"arr_elem_field: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("arr_elem_field: %d/%d ok\n", total, total);
return 0;
}