Files
ww/test/wcc/756_alias_chain_unwrap.c
Hojun-Cho a95a7a316b test/wcc: carrier ownership repair and driver-contract adaptation
Every surviving carrier now owns its artifacts: checked mkdir/mkdtemp/
fopen acquisition, one all-exit cleanup funnel per carrier, ENOENT-
tolerant checked unlinks, exact-path deletion (rm -rf only for an
owned pid-keyed dir or a .sepwork beneath one), and cleanup failure
fails a passing carrier without overwriting its diagnostic. In the
same pass the carriers adapt to the driver contract this branch lands:
--sep and WW_PKGCACHE are gone, -S and the /tmp/ww_run_<pid> scratch
contract are asserted, and rows whose runtime or reject coverage moved
to test/wcc/data fixtures or test/lang @test owners are trimmed to the
byte/artifact/diagnostic observations only they can make.

Repair and adaptation ride together because most files interleave both
in the same hunks; splitting would manufacture intermediate carrier
states that never existed and cannot run against either driver.
2026-08-07 23:21:04 +09:00

245 lines
7.4 KiB
C

/*
* 756_alias_chain_unwrap — sentinel for #22 alias-chain SRET/N_DOT
* wedge. Pre-fix both stages single-peeled `if (t->kind == TY_NAMED)
* t = t->under` in cg_sret_retsize / N_LET sizing / cgreturn / N_DOT
* field-access (cstage) and structlookup-on-N_TNAME (wwstage); a
* `type b = a;` over an alias-of-struct stacked two TY_NAMED layers
* and the single peel bottomed out at the inner alias — still
* TY_NAMED, not TY_STRUCT. Outcome: SRET-shaped returns silently
* routed through the scalar-AX ABI (caller's receive slot
* corrupted), let-init slots under-sized, and `s.field` reads
* collapsed to base+0 instead of base+field_offset.
*
* Polarity (rule 10, mutual-symmetric): cstage's hot-path peels
* graduated to a transitive `while (t->kind == TY_NAMED) t = t->under`
* walk; wwstage's structlookup-misses fall through to aliaslookup +
* recurse, mirroring slotsize's existing N_TNAME arm (cgenutil.ww
* line 1955). The strings.tokenize wrapper shape (`type tokenizer =
* bytes::tokenizer;`) is the original surfacing site (#22).
*
* row | shape | gate
* ---------------------+----------------------------------------+--------
* r1_i32_struct | direct TY_STRUCT, no alias | works pre-fix
* r2_slice_single | `type a = struct{[]u8,[]u8,i64}` | wedge pre-fix (RC=12)
* | aliased once → `type b = a;` |
* r3_slice_direct | direct struct, slice fields | works pre-fix
* r4_i32_double | `type b = a; type a = struct{i32...}` | wedge pre-fix (RC=14)
* | aliased twice → `type c = b;` |
*
* Runtime ownership moved to the matching r756_* fixtures. This carrier
* retains the cstage/wwstage assembly byte-identity discriminator.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.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; };
static const struct row rows[] = {
/* r1: direct i32-fields struct, no alias. Baseline — single
* TY_NAMED layer (the type itself) suffices for the existing
* single-peel pattern. */
{ "r1_i32_struct",
"package main;\n"
"type r1 = struct {\n"
"\ta: i32, b: i32, c: i32, d: i32, e: i32, f: i32, g: i32,\n"
"\th: i32, i: i32, j: i32, k: i32, l: i32, m: i32, p: i32,\n"
"};\n"
"fn make_r1() r1 = {\n"
"\tlet b: r1;\n"
"\tb.p = 99i32;\n"
"\treturn b;\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet s: r1 = make_r1();\n"
"\tif (s.p != 99i32) { return 11; };\n"
"\treturn 0;\n"
"};\n",
},
/* r2: slice-fields struct aliased once. Pre-fix wedge:
* `type r2_alias = r2_struct;` stacks two TY_NAMED layers, the
* single peel lands on the inner alias and the SRET gate falls
* through to scalar-AX. RC=12 pre-fix on both stages. */
{ "r2_slice_single",
"package main;\n"
"type r2_struct = struct { in: []u8, delim: []u8, p: i64 };\n"
"type r2_alias = r2_struct;\n"
"fn make_r2() r2_alias = {\n"
"\tlet b: r2_struct;\n"
"\tb.p = 99i64;\n"
"\treturn b;\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet s: r2_alias = make_r2();\n"
"\tif (s.p != 99i64) { return 12; };\n"
"\treturn 0;\n"
"};\n",
},
/* r3: slice-fields struct, no alias — baseline that proves the
* slice-payload sret discipline itself is unaffected. */
{ "r3_slice_direct",
"package main;\n"
"type r3 = struct { in: []u8, delim: []u8, p: i64 };\n"
"fn make_r3() r3 = {\n"
"\tlet b: r3;\n"
"\tb.p = 99i64;\n"
"\treturn b;\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet s: r3 = make_r3();\n"
"\tif (s.p != 99i64) { return 13; };\n"
"\treturn 0;\n"
"};\n",
},
/* r4: i32-fields struct double-aliased. Same wedge as r2 but
* chain length 3 — extra TY_NAMED layer between the alias and
* the struct. Confirms the discriminator is the chain length,
* not the field shape. RC=14 pre-fix on both stages. */
{ "r4_i32_double",
"package main;\n"
"type r4_struct = struct {\n"
"\ta: i32, b: i32, c: i32, d: i32, e: i32, f: i32, g: i32,\n"
"\th: i32, i: i32, j: i32, k: i32, l: i32, m: i32, p: i32,\n"
"};\n"
"type r4_base = r4_struct;\n"
"type r4_alias = r4_base;\n"
"fn make_r4() r4_alias = {\n"
"\tlet b: r4_alias;\n"
"\tb.p = 99i32;\n"
"\treturn b;\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet s: r4_alias = make_r4();\n"
"\tif (s.p != 99i32) { return 14; };\n"
"\treturn 0;\n"
"};\n",
},
};
static int
emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap)
{
char src[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/acu_s_%d_%d.ww", getpid(), i);
snprintf(out_s, cap, "/tmp/acu_s_%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);
int cleanfail = 0;
if (unlink(src) != 0 && errno != ENOENT) {
perror(src);
cleanfail = 1;
}
/* a failing compile can still leave a partial .s behind */
if (rc != 0 && unlink(out_s) != 0 && errno != ENOENT) {
perror(out_s);
cleanfail = 1;
}
if (cleanfail && rc == 0)
rc = -1;
return rc;
}
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 cw6c[640], ww6c[640];
snprintf(cw6c, sizeof cw6c, "%s/w6c", bin);
snprintf(ww6c, sizeof ww6c, "%s/w6c_ww", bin);
int have_ww = access(ww6c, X_OK) == 0;
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
int cleanfail = 0;
for (int i = 0; i < n; i++) {
if (!have_ww) continue;
/* byte-id cstage vs wwstage */
total++;
char cs_path[160], ws_path[160];
/* legs split: on a first-leg failure ws_path is never
* filled in, so it must never reach unlink. */
if (emit_s(cw6c, &rows[i], i, cs_path, sizeof cs_path) != 0) {
fprintf(stderr,
"alias_chain[byte-id][%s]: emit failed\n",
rows[i].label);
fail++;
continue;
}
if (emit_s(ww6c, &rows[i], i, ws_path, sizeof ws_path) != 0) {
fprintf(stderr,
"alias_chain[byte-id][%s]: emit failed\n",
rows[i].label);
fail++;
if (unlink(cs_path) != 0 && errno != ENOENT) {
perror(cs_path);
cleanfail = 1;
}
continue;
}
char cmpcmd[512];
snprintf(cmpcmd, sizeof cmpcmd, "cmp -s %s %s",
cs_path, ws_path);
if (runwait(cmpcmd) != 0) {
fprintf(stderr,
"alias_chain[byte-id][%s]: cstage vs wwstage asm differs\n",
rows[i].label);
fail++;
}
if (unlink(cs_path) != 0 && errno != ENOENT) {
perror(cs_path);
cleanfail = 1;
}
if (unlink(ws_path) != 0 && errno != ENOENT) {
perror(ws_path);
cleanfail = 1;
}
}
/* a cleanup failure must not pass silently, but must not mask a
* real assertion failure's own report either. */
if (cleanfail && fail == 0) {
fprintf(stderr, "alias_chain: cleanup failed\n");
return 1;
}
if (fail) {
fprintf(stderr,
"alias_chain: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("alias_chain: %d/%d ok\n", total, total);
return 0;
}