Files
ww/test/wcc/930_free_noop_run.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

258 lines
7.6 KiB
C

/*
* 930_free_noop_run — the free() builtin compiles to a documented
* NO-OP (#27).
*
* ww has no free by design (rt/alloc.s:30 — the bump allocator cannot
* reclaim a mid-chunk pointer; process exit does; drop-amalloc). The
* pre-#27 lowering emitted CALL ffi_resolve("free") — an undefined
* reference at w6l unless an @symbol decl happened to be in scope —
* and wwstage had no free arm at all (generic CALL free, same link
* failure). Post-#27 both stages evaluate the operand for side
* effects (Hare's free(expr) evaluates expr — regex fold-2b calls
* free() at 4+ sites; finish() ports verbatim) and emit nothing else.
*
* Rows pin: free of a plain local pointer with deref-after-free (the
* no-op's documented leak semantics — the pointee stays valid), free
* of a struct field, free of a CALL operand twice (the side effect
* must run per call — a global counter observes both evaluations),
* free in a 1M-iteration loop (the no-op must not accumulate stack
* damage — a leaked push per free would segfault),
* and the Hare-port shape alloc-then-free round-trip (import rt;
* *i64 — i64 sidesteps the pre-existing unrelated cs≠ww alloc(value)
* size divergence on narrow pointee types, filed separately). The
* 2-arg `os.free(p, n)` public API is NOT intercepted (the builtin
* gate requires exactly one arg) and keeps resolving via its
* @symbol("rt_free") decl — covered by the lisp example / stdlib
* suites, not re-pinned here.
*
* Per row: w6c vs w6c_ww .s byte-id (rule 10), a negative grep that
* no `free` symbol survives anywhere in the .s (byte-id alone would
* pass if BOTH stages still emitted CALL free), and runtime via both
* drivers.
*/
#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[] = {
/* free(ident): compiles, links, and the pointee stays valid —
* the no-op's documented semantics (a harmless leak, never a
* dangling pointer). */
{ "free_local_ptr_deref_after",
"export fn main() i32 = {\n"
" let x: i32 = 5;\n"
" let p: *i32 = &x;\n"
" free(p);\n"
" if (*p != 5) { return 1; };\n"
" return 0;\n"
"};\n",
0 },
/* free(field): the N_DOT operand shape (regex finish() frees
* re.insts / re.charsets through a pointer field). */
{ "free_struct_field",
"type holder = struct { p: *i32, n: i32 };\n"
"export fn main() i32 = {\n"
" let x: i32 = 3;\n"
" let h: holder = holder { p = &x, n = 4 };\n"
" free(h.p);\n"
" if (h.n != 4) { return 2; };\n"
" return 0;\n"
"};\n",
0 },
/* free(f(...)): Hare evaluates free's operand, so the call must
* run — twice freed, twice bumped. Pins the
* evaluate-for-side-effects half of the lowering (emitting
* nothing at all would leave g at 0). */
{ "free_call_operand_effects",
"let g: i32 = 0;\n"
"fn bump(p: *i32) *i32 = {\n"
" g = g + 2;\n"
" return p;\n"
"};\n"
"export fn main() i32 = {\n"
" let x: i32 = 1;\n"
" free(bump(&x));\n"
" free(bump(&x));\n"
" if (g != 4) { return 3; };\n"
" return 0;\n"
"};\n",
0 },
/* free() in a hot loop: the no-op must not accumulate stack
* damage — a leaked 8B push per free would blow the 8MiB stack
* long before 1M iterations (segfault, not a wrong exit code). */
{ "free_loop_no_stack_damage",
"let g: i32 = 0;\n"
"fn bump(p: *i32) *i32 = {\n"
" g = g + 1;\n"
" return p;\n"
"};\n"
"export fn main() i32 = {\n"
" let x: i32 = 1;\n"
" let i: i32 = 0;\n"
" for (i < 1000000) {\n"
" free(bump(&x));\n"
" i += 1;\n"
" };\n"
" if (g != 1000000) { return 5; };\n"
" return 0;\n"
"};\n",
0 },
/* The verbatim Hare-port shape: alloc then free, deref after.
* Pre-#27 this was THE w6l undefined-reference repro. */
{ "free_alloc_roundtrip",
"import rt;\n"
"export fn main() i32 = {\n"
" let p: *i64 = alloc(11i64)!;\n"
" free(p);\n"
" if (*p != 11i64) { return 4; };\n"
" return 0;\n"
"};\n",
0 },
};
static const char *g_bin;
static int
compile_s(const char *tool, const char *src, const char *outpath)
{
char cmd[1024];
snprintf(cmd, sizeof cmd, "%s/%s %s > %s 2>&1",
g_bin, tool, src, outpath);
return runwait(cmd);
}
static int
file_eq(const char *a, const char *b)
{
char cmd[1024];
snprintf(cmd, sizeof cmd, "cmp -s %s %s", a, b);
return runwait(cmd) == 0;
}
static int
has_free_sym(const char *s_path)
{
char cmd[1024];
snprintf(cmd, sizeof cmd, "grep -q 'free' %s", s_path);
return runwait(cmd) == 0;
}
static int
run_driver(const char *driver, const char *src, const char *tmpdir,
const char *label)
{
char cmd[1024], outbin[256];
snprintf(outbin, sizeof outbin, "%s/%s_out", tmpdir, driver);
snprintf(cmd, sizeof cmd, "%s/%s build -o %s %s >/dev/null 2>&1",
g_bin, driver, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
label, driver);
return -1;
}
return runwait(outbin);
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
static 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;
}
g_bin = bin;
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
const struct row *r = &rows[i];
/* #8: source, .sepwork scratch, both driver binaries and the
* byte-id .s dumps all live under one tmpdir; rm -rf on every
* exit path. Symbol mangling is package/import-derived, not
* entry-path derived, so the in-tmpdir source keeps the cs==ww
* .s identical. */
char tmpdir[64], src[128], cs_s[128], ww_s[128], rmcmd[160];
snprintf(tmpdir, sizeof tmpdir, "/tmp/freenoop_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/freenoop_%d_%d.ww",
tmpdir, getpid(), i);
snprintf(cs_s, sizeof cs_s, "%s/cs.s", tmpdir);
snprintf(ww_s, sizeof ww_s, "%s/ww.s", tmpdir);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return 1; }
fputs("package main;\n\n", f);
fputs(r->src, f);
fclose(f);
total++;
int cs_rc = compile_s("w6c", src, cs_s);
int ww_rc = compile_s("w6c_ww", src, ww_s);
if (cs_rc != 0 || ww_rc != 0) {
fprintf(stderr, "FAIL row[%s]: compile rc cs=%d "
"ww=%d\n", r->label, cs_rc, ww_rc);
fail++;
runwait(rmcmd);
continue;
}
if (!file_eq(cs_s, ww_s)) {
fprintf(stderr, "FAIL row[%s]: cs != ww .s\n",
r->label);
fail++;
}
if (has_free_sym(cs_s)) {
fprintf(stderr, "FAIL row[%s]: 'free' survives in "
"the .s — lowering is not a no-op\n", r->label);
fail++;
}
int got_cs = run_driver("ww", src, tmpdir, r->label);
if (got_cs != r->want) {
fprintf(stderr, "FAIL row[%s] cstage: want %d "
"got %d\n", r->label, r->want, got_cs);
fail++;
}
char wwdrv[600];
snprintf(wwdrv, sizeof wwdrv, "%s/ww_ww", g_bin);
if (access(wwdrv, X_OK) == 0) {
int got_ww = run_driver("ww_ww", src, tmpdir, r->label);
if (got_ww != r->want) {
fprintf(stderr, "FAIL row[%s] wwstage: "
"want %d got %d\n",
r->label, r->want, got_ww);
fail++;
}
}
runwait(rmcmd);
}
if (fail) {
fprintf(stderr, "free_noop_run: %d/%d rows failed\n",
fail, total);
return 1;
}
printf("free_noop_run: %d rows ok\n", total);
return 0;
}