Files
ww/test/wcc/810_dyn.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

168 lines
5.2 KiB
C

/*
* 810_dyn — end-to-end test of w6l's dynamic linker.
*
* Drives `ww build -L /usr/lib -l c` over a small ww program that
* binds libc symbols via @symbol, then runs the produced binary and
* checks the exit status. Verifies:
*
* - the .so loader (dyn.c) reads ET_DYN and extracts exports
* - PLT/GOT generation and the JUMP_SLOT relocation
* - PT_INTERP/PT_DYNAMIC emission
* - symbol versioning (.gnu.version + .gnu.version_r) — clock_gettime
* has both GLIBC_2.2.5 (compat) and GLIBC_2.17 (default) on glibc;
* the loader requires the right .gnu.version_r entry to bind
* the default vDSO-aware impl.
*
* Skipped (passing trivially) on systems without /usr/lib/libc.so.6.
*/
#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 *src;
int want_exit;
};
static const struct row rows[] = {
/* 1. _exit via dynamically-linked libc. Exit code is the plumbing
* of choice — proves the PLT entry calls into libc.so.6. */
{ "@symbol(\"_exit\") fn libc_exit(c: i32) void;\n"
"export fn main() i32 = { libc_exit(42); return 0; };", 42 },
/* 2. Multiple dyn syms in one binary: write + _exit. */
{ "@symbol(\"write\") fn libc_write(fd: i32, b: *u8, n: u64) i64;\n"
"@symbol(\"_exit\") fn libc_exit(c: i32) void;\n"
"export fn main() i32 = {\n"
" libc_write(1, \"x\".ptr, 1u64);\n"
" libc_exit(7);\n"
" return 0;\n"
"};", 7 },
/* 3. Symbol versioning: clock_gettime. Without DT_VERSYM/VERNEED
* pointing at a Vernaux entry for GLIBC_2.17, the loader either
* picks the wrong impl or fails outright on modern glibc. */
{ "@symbol(\"clock_gettime\") fn clock_gettime(c: i32, ts: *u8) i32;\n"
"@symbol(\"_exit\") fn libc_exit(c: i32) void;\n"
"export fn main() i32 = {\n"
" let ts: [16]u8;\n"
" let r: i32 = clock_gettime(1, ts.ptr);\n" /* CLOCK_MONOTONIC */
" libc_exit(r);\n"
" return 0;\n"
"};", 0 },
/* 4. Take the address of an FFI binding and call it indirectly.
* Codegen must apply @symbol resolution at the LEAQ site (so the
* stub address is `write`, not the ww-side ident `libc_write`),
* and the local-variable call must be CALL *AX, not CALL fp(SB). */
{ "@symbol(\"write\") fn libc_write(fd: i32, b: *u8, n: u64) i64;\n"
"@symbol(\"_exit\") fn libc_exit(c: i32) void;\n"
"export fn main() i32 = {\n"
" let fp: fn(fd: i32, b: *u8, n: u64) i64 = libc_write;\n"
" fp(1, \"X\".ptr, 1u64);\n"
" libc_exit(5);\n"
" return 0;\n"
"};", 5 },
/* 5. Writable global on the dyn-link path. .data now lives in the
* shared R+W segment with .got.plt and .dynamic; read and write
* exercise the new layout's data_va. */
{ "@symbol(\"_exit\") fn libc_exit(c: i32) void;\n"
"let counter: i32 = 0;\n"
"export fn main() i32 = {\n"
" counter = 11;\n"
" libc_exit(counter);\n"
" return 0;\n"
"};", 11 },
/* 6. Zero-init slot under dyn-link: the BSS trailing-zero scan
* should still produce a valid p_filesz<p_memsz layout when the
* R+W segment is the shared gotplt+dynamic+.data one. */
{ "@symbol(\"_exit\") fn libc_exit(c: i32) void;\n"
"let z: i64;\n"
"export fn main() i32 = {\n"
" z = 9i64;\n"
" libc_exit(z: i32);\n"
" return 0;\n"
"};", 9 },
{ NULL, 0 }
};
int
main(void)
{
if (access("/usr/lib/libc.so.6", 0) != 0
&& access("/lib/x86_64-linux-gnu/libc.so.6", 0) != 0
&& access("/lib64/libc.so.6", 0) != 0) {
puts("dyn: no libc.so.6 on this system — skipping");
return 0;
}
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;
}
/* Probe each common libc dir to find the right -L. */
const char *libdir = NULL;
if (access("/usr/lib/libc.so.6", 0) == 0) libdir = "/usr/lib";
else if (access("/lib/x86_64-linux-gnu/libc.so.6", 0) == 0) libdir = "/lib/x86_64-linux-gnu";
else if (access("/lib64/libc.so.6", 0) == 0) libdir = "/lib64";
int n = 0, fail = 0;
for (int i = 0; rows[i].src; i++, n++) {
char tmpdir[80], src[128], outbin[128], rmcmd[160];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwdyn_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/wwdyn_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wwdyn_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
fputs(rows[i].src, f);
fclose(f);
char cmd[1024];
snprintf(cmd, sizeof cmd,
"%s/ww build -o %s %s -L %s -l c",
bin, outbin, src, libdir);
if (runwait(cmd) != 0) {
fprintf(stderr, "dyn row %d: build failed\n", i);
fail++;
runwait(rmcmd);
continue;
}
int got = runwait(outbin);
if (got != rows[i].want_exit) {
fprintf(stderr, "dyn row %d: exit %d, want %d\n",
i, got, rows[i].want_exit);
fail++;
}
runwait(rmcmd);
}
if (fail) {
fprintf(stderr, "%d/%d dyn tests failed\n", fail, n);
return 1;
}
printf("dyn: %d/%d ok\n", n, n);
return 0;
}