196 lines
5.8 KiB
C
196 lines
5.8 KiB
C
/*
|
|
* Covers the claims the exit status alone can't show: dyn.c reading
|
|
* ET_DYN and extracting exports, PLT/GOT generation with JUMP_SLOT
|
|
* relocations, and 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.
|
|
*/
|
|
#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;
|
|
}
|
|
|
|
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 -L %s -l c %s",
|
|
bin, outbin, libdir, src);
|
|
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;
|
|
}
|