Files
ww/test/wcc/810_dyn.c
Hojun-Cho 2c33228b7e ww: rename toolchain to w-prefix + hare-style build/run/test driver
Plan 9-style w-prefix on the per-arch tools, disambiguating from the
real Plan 9 6c/6a/6l in ref/plan9front/:

    cmd/wwc/      → cmd/wcc/        libwwc.a → libwcc.a
    cmd/6{c,a,l}  → cmd/w6{c,a,l}   binary names too
    test/wwc/     → test/wcc/       6 test files w/ w6 prefix
    selfhost/cmd  mirror in lockstep
    bootstrap/amd64/{w6c,w6a,w6l}   snapshot binaries (gitignored)
    WW_6{C,A,L}   → WW_W6{C,A,L}    env-var overrides

Plan 9 source-tree refs ("Plan 9 6c shape", ref/plan9front/, etc.)
preserved. Hare-style driver, both C and ww sides:

    ww test [path]   discover *_test.ww in a directory module, run
                     each; single-file mode for `ww test foo.ww`
    Module-by-name   `ww build foo` resolves to foo.ww or foo/foo.ww
                     via search path (cwd : -I dirs : $WW_LIB)
    Default-to-cwd   `ww build` / `ww test` build the cwd module
    Run pass-through `ww run path arg1 arg2` reaches the program

lib/os: getcwd (79) and getdents64 (217) syscalls power `.` resolution
and directory enumeration on the ww side.

Makefile: wwstage tool deps now include lib/os/os.ww (+ lib/strconv
for wwdump_ww) so lib/* edits force their rebuild instead of leaving
stale binaries — surfaced when test 995 first failed against a stale
w6c_ww built before the lib/os additions.

Test 993 byte-identical parity gate (C-side ww vs ww-side ww_ww on a
build corpus) stays green; all 19 tests pass.
2026-05-11 13:49:27 +09:00

153 lines
4.7 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 <string.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 },
{ 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 src[80], exe[80], tmpdir[80];
snprintf(src, sizeof src, "/tmp/wwdyn_%d_%d.ww", getpid(), i);
snprintf(exe, sizeof exe, "/tmp/wwdyn_%d_%d", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwdyn_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
FILE *f = fopen(src, "wb");
fputs(rows[i].src, f);
fclose(f);
char cmd[1024];
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww build %s -L %s -l c",
tmpdir, bin, src, libdir);
if (runwait(cmd) != 0) {
fprintf(stderr, "dyn row %d: build failed\n", i);
fail++;
unlink(src); rmdir(tmpdir);
continue;
}
char outbin[160];
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
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++;
}
unlink(src); unlink(outbin); rmdir(tmpdir);
}
if (fail) {
fprintf(stderr, "%d/%d dyn tests failed\n", fail, n);
return 1;
}
printf("dyn: %d/%d ok\n", n, n);
return 0;
}