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

204 lines
6.1 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.
*
* Reports the harness skip status on systems without a supported libc.so.6.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.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;
}
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 77;
}
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[] = "/tmp/wwdyn_XXXXXX";
char src[128], outbin[128], scratch[160], rmcmd[192];
int rowfail = 0;
if (mkdtemp(tmpdir) == NULL) {
fprintf(stderr, "dyn row %d: mkdtemp failed\n", i);
fail++;
continue;
}
snprintf(src, sizeof src, "%s/wwdyn_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wwdyn_%d_%d", tmpdir, getpid(), i);
snprintf(scratch, sizeof scratch, "%s.sepwork", outbin);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", scratch);
FILE *f = fopen(src, "wb");
if (f == NULL) {
fprintf(stderr, "dyn row %d: source create failed\n", i);
rowfail = 1;
goto row_done;
}
wwtest_fputs(rows[i].src, f);
int werr = ferror(f);
if (fclose(f) != 0) werr = 1;
if (werr) {
fprintf(stderr, "dyn row %d: source write failed\n", i);
rowfail = 1;
goto row_done;
}
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);
rowfail = 1;
goto row_done;
}
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);
rowfail = 1;
}
row_done:
if (runwait(rmcmd) != 0) {
fprintf(stderr, "dyn row %d: scratch cleanup failed\n", i);
rowfail = 1;
}
if (unlink(outbin) != 0 && errno != ENOENT) {
fprintf(stderr, "dyn row %d: output cleanup failed\n", i);
rowfail = 1;
}
if (unlink(src) != 0 && errno != ENOENT) {
fprintf(stderr, "dyn row %d: source cleanup failed\n", i);
rowfail = 1;
}
if (rmdir(tmpdir) != 0) {
fprintf(stderr, "dyn row %d: workspace cleanup failed\n", i);
rowfail = 1;
}
if (rowfail) fail++;
}
if (fail) {
fprintf(stderr, "%d/%d dyn tests failed\n", fail, n);
return 1;
}
printf("dyn: %d/%d ok\n", n, n);
return 0;
}