Files
ww/test/wcc/743_variadic_pack.c
Hojun-Cho 7b9488706b parse: enforce strict-package — reject package-less files (#24a)
Flip the soft-default to a hard "missing package clause" error symmetrically in
both stages (cmd/wcc/parse.c + lib/ww/syntax/parse.ww): the first real decl of a
primary section with empty pathmod/resetmod and no seen clause is now rejected.
Closes the documented soft-default divergence (the 63-wrapper carve-out).

The gate flip can't be split from the migration it breaks, so this is one atomic
commit: ~80 test/wcc wrappers gain `package main;` via a shared wwtestpkg.h
helper, 6 data fixtures plus 17 asm-grep assertions update for the bare->main.<leaf>
root-helper mangle shift, and rt/ declares `package rt;` with @symbol pinning the
bare rt_ensure/rt_malloc linker names.

Root mangling narrows: the executable entry `main` stays bare (existing
carve-out), but root helper symbols become main.X. The #84 cluster is rewritten
to assert main.run distinct from aa.run/test.run; its cgen fix and bare machinery
are retained — still load-bearing for package-less module-reset deps. New
table-driven test 782_strict_package.c (6 rows, both stages).

Retiring //ww:module-reset is deferred to #24b: it is load-bearing (clears the
.wwi pathmod so the body's package clause asserts), not a vestige; fusing its
removal here would be a silent mismatch.

All byte-id gates green; full make test reports "all 335 tests passed".
2026-06-29 03:55:26 +09:00

190 lines
4.9 KiB
C

/*
* 743_variadic_pack — sentinel for STATUS-3 #16. cstage cgen's
* variadic gather (`f(args: str...)` called as `f("a", "b", "c")`)
* emitted only the ptr eightbyte of each str element; the .len
* eightbyte was never stored, so the callee's `args[i].len` read
* stack residue. Tagged-union variadics escaped because they took
* the cg_widen_tagged_store branch; primitive-type variadics
* (str..., slice..., rune... bigger than 8B) did not.
*
* Surfaced by worker-strings pre-flight on the Hare-faithful
* `concat(strs: str...)` shape; no in-tree caller exercised it
* because the c1 strings subset shipped non-variadic. Bootstrap
* byte-id masked it (selfhost only calls fmt.println, which uses
* tagged-union variadics, hitting the widen-store branch).
*
* Rule 10: both stages must emit the same {ptr-store, len-store}
* pair sequence. Pre-fix cstage emitted 3 ptr-stores only; wwstage
* already emitted both halves via cgenexpr.ww's velemstr branch.
*
* Rows pin asm-presence in both stages and cmp -s byte-id.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "wwtestpkg.h"
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return -1;
}
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;
}
/* Count occurrences of `needle` in `hay` between [start, end). */
static int
count_substr(const char *hay, const char *start, const char *end,
const char *needle)
{
int n = 0;
size_t nlen = strlen(needle);
const char *p = start;
while (p + nlen <= end) {
if (memcmp(p, needle, nlen) == 0) { n++; p += nlen; }
else { p++; }
}
return n;
}
static int
check_caller_stores(const char *spath)
{
char buf[1 << 16];
if (slurp(spath, buf, sizeof buf) < 0) return -1;
const char *body = strstr(buf, "TEXT main,");
if (!body) {
fprintf(stderr, "variadic_pack: no TEXT main label\n");
return -1;
}
const char *end = strstr(body, "\nTEXT ");
if (!end) end = buf + strlen(buf);
/* Three str elements => 3 ptr-stores (MOVQ AX, off(BP)) AND
* 3 len-stores (MOVQ BX, off(BP)). The len-store is the bug
* pin: pre-fix it was missing entirely. */
int ptr_stores = count_substr(buf, body, end, "MOVQ\tAX, -");
int len_stores = count_substr(buf, body, end, "MOVQ\tBX, -");
if (len_stores < 3) {
fprintf(stderr,
"variadic_pack: only %d len-stores (MOVQ BX, -K(BP)) "
"in main; expected >= 3\n", len_stores);
return -1;
}
if (ptr_stores < 3) {
fprintf(stderr,
"variadic_pack: only %d ptr-stores in main; "
"expected >= 3\n", ptr_stores);
return -1;
}
return 0;
}
static const char *src_text =
"fn sumlen(parts: str...) i32 = {\n"
" let z: i32 = 0;\n"
" let i: i32 = 0;\n"
" for (i < parts.len) {\n"
" z += parts[i].len;\n"
" i += 1;\n"
" };\n"
" return z;\n"
"};\n"
"fn main() i32 = {\n"
" return sumlen(\"a\", \"bb\", \"ccc\");\n"
"};\n";
static int
emit_s(const char *w6c, char *out_s, size_t cap, int tag)
{
char src[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/vp_asm_%d_%d.ww", getpid(), tag);
snprintf(out_s, cap, "/tmp/vp_asm_%d_%d.s", getpid(), tag);
FILE *f = fopen(src, "wb");
if (!f) return -1;
wwtest_fputs(src_text, 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;
}
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 total = 0, fail = 0;
char cs_path[128], ws_path[128];
if (emit_s(w6c, cs_path, sizeof cs_path, 0) != 0) {
fprintf(stderr, "variadic_pack[cstage]: w6c failed\n");
return 1;
}
total++;
if (check_caller_stores(cs_path) != 0) fail++;
if (have_ww) {
if (emit_s(w6c_ww, ws_path, sizeof ws_path, 1) != 0) {
fprintf(stderr,
"variadic_pack[wwstage]: w6c_ww failed\n");
unlink(cs_path);
return 1;
}
total++;
if (check_caller_stores(ws_path) != 0) fail++;
/* Byte-id between stages on the variadic-pack shape. */
total++;
char cmd[512];
snprintf(cmd, sizeof cmd, "cmp -s %s %s", cs_path, ws_path);
if (runwait(cmd) != 0) {
fprintf(stderr,
"variadic_pack: cstage vs wwstage asm differs\n");
fail++;
}
unlink(ws_path);
}
unlink(cs_path);
if (fail) {
fprintf(stderr,
"variadic_pack: %d/%d checks failed\n", fail, total);
return 1;
}
printf("variadic_pack: %d/%d ok\n", total, total);
return 0;
}