Files
ww/test/wcc/996_dyn_ww.c
Hojun-Cho 83f5956df2 test: banner purge + WHY-only comment sweep (rule 8)
Every section banner dies (103 -> 0) across test/lang, the observer
suites, the C carriers, and the five comment-heavy corpus fixtures;
banner provenance (#N cites, carrier numbers, repair-cluster labels)
folded into headers or adjacent WHY comments. Narration deleted; row
provenance, ref cites, divergence pins, and layout contracts kept
(fwd-ref decl-order guards and bootstrap-gate corpus rationale
restored where the sweep over-cut). Comment-only proven: all 3742
wwbuild workdir .s byte-identical before/after; test-commit and
test-byteid (161 lang + 1399 data, 0 pinned-divergent) green.
2026-08-08 21:40:23 +09:00

144 lines
3.7 KiB
C

/*
* Phase-8 marker for ET_DYN linking on the ww side: a green run means
* the ww-side linker emits PT_INTERP/PT_DYNAMIC binaries byte-for-byte
* identical to the C linker on the same inputs — i.e. the dynamic
* linking story is fully ported.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.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;
}
static const char *
absbin(void)
{
const char *b = getenv("BIN");
if (!b) b = "out/bin";
if (b[0] == '/') return b;
static char buf[2048];
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return NULL;
snprintf(buf, sizeof buf, "%s/%s", cwd, b);
return buf;
}
static int
slurp(const char *path, char **outbuf, size_t *outlen)
{
FILE *f = fopen(path, "rb");
if (!f) return -1;
fseek(f, 0, SEEK_END);
long n = ftell(f);
fseek(f, 0, SEEK_SET);
if (n < 0) { fclose(f); return -1; }
char *b = malloc((size_t)n + 1);
if (!b) { fclose(f); return -1; }
if (fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); fclose(f); return -1; }
b[n] = '\0';
fclose(f);
*outbuf = b;
*outlen = (size_t)n;
return 0;
}
int
main(void)
{
const char *bin = absbin();
if (!bin) return 1;
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
/* #93 sep layout: build the example via `ww build` into a
* private /tmp stem (NOT next to the tracked example source), then
* link <stem>.sepwork/__root.o — the self-contained single-package
* object the flip emits in place of the old next-to-source .o. */
char cmd[4096], mbstem[256], mbobj[320], co[64], wo[64];
snprintf(mbstem, sizeof mbstem, "/tmp/wwld_%d_mb", getpid());
snprintf(mbobj, sizeof mbobj, "%s.sepwork/__root.o", mbstem);
snprintf(co, sizeof co, "/tmp/wwld_%d_c", getpid());
snprintf(wo, sizeof wo, "/tmp/wwld_%d_w", getpid());
int rc = 0;
snprintf(cmd, sizeof cmd,
"cd %s/examples/mandelbrot && %s/ww build "
"-o %s mandelbrot.ww -L /usr/lib -l c >/dev/null 2>&1",
cwd, bin, mbstem);
if (runwait(cmd) != 0) {
fprintf(stderr, "w6l_ww-dyn FAIL: cannot build mandelbrot.o via C driver\n");
rc = 1;
goto cleanup;
}
snprintf(cmd, sizeof cmd,
"%s/w6l -o %s %s -L /usr/lib "
"-l c %s/out/lib/libwwrt.a 2>/dev/null",
bin, co, mbobj, cwd);
if (runwait(cmd) != 0) {
fprintf(stderr, "w6l_ww-dyn FAIL: C w6l errored\n");
rc = 1;
goto cleanup;
}
snprintf(cmd, sizeof cmd,
"%s/w6l_ww -o %s %s -L /usr/lib "
"-l c %s/out/lib/libwwrt.a 2>/dev/null",
bin, wo, mbobj, cwd);
if (runwait(cmd) != 0) {
fprintf(stderr, "w6l_ww-dyn FAIL: ww w6l errored\n");
rc = 1;
goto cleanup;
}
char *bc = NULL, *bw = NULL;
size_t nc = 0, nw = 0;
if (slurp(co, &bc, &nc) < 0 || slurp(wo, &bw, &nw) < 0) {
fprintf(stderr, "w6l_ww-dyn FAIL: cannot read outputs\n");
rc = 1;
} else if (nc == 0) {
fprintf(stderr, "w6l_ww-dyn FAIL: empty outputs\n");
rc = 1;
} else if (nc != nw || memcmp(bc, bw, nc) != 0) {
fprintf(stderr, "w6l_ww-dyn FAIL: C %zu vs ww %zu bytes\n",
nc, nw);
rc = 1;
} else {
printf("w6l_ww-dyn: byte-identical to C w6l on mandelbrot "
"(PT_INTERP + PT_DYNAMIC + .rela.plt + .gnu.version_r, "
"%zu bytes)\n", nc);
}
free(bc); free(bw);
cleanup:
/* #93: `ww build` retains this exact caller-owned scratch tree. */
snprintf(cmd, sizeof cmd, "rm -rf %s.sepwork", mbstem);
int cleanfail = runwait(cmd) != 0;
if (unlink(co) != 0 && errno != ENOENT) {
perror(co);
cleanfail = 1;
}
if (unlink(wo) != 0 && errno != ENOENT) {
perror(wo);
cleanfail = 1;
}
if (unlink(mbstem) != 0 && errno != ENOENT) {
perror(mbstem);
cleanfail = 1;
}
if (cleanfail && rc == 0)
rc = 1;
return rc;
}