Files
ww/test/wcc/992_w6l_ww.c

163 lines
4.7 KiB
C

/*
* Explicit bootstrap gate: the C-built `w6l` and the ww-built `w6l_ww`
* must produce byte-identical static ELF binaries on the same link
* inputs. Any divergence is a port bug in selfhost/cmd/w6l/. The
* corpus mixes a single-.o link (no archive resolution) with a
* .o + libwwrt.a link exercising the SysV `ar` two-pass loader.
*/
#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;
}
/* #93 sep layout: the flip stops emitting a monolithic <tool>/main.o;
* the linkable unit is now the root .a + reverse-topo .a set the
* sep driver assembles (a raw `w6l <root>.a *.a libwwrt.a` from the test
* side fails — `undefined reference` — because it can't reproduce the
* driver's topo order). Drive each real directory package through the
* full sep build twice; both compile directly and differ only in the
* linker: once with the default C w6l, once with WW_W6L=w6l_ww. Diff
* the two real tool binaries. Exercises the full archive two-pass loader
* + reverse-topo .a resolution — a stronger link than the old single
* main.o. */
static int
build_and_diff(const char *bin, const char *cwd, const char *tool, int id)
{
char cstem[256], wstem[256], cmd[4096];
snprintf(cstem, sizeof cstem, "/tmp/wwl_%d_%d_c", getpid(), id);
snprintf(wstem, sizeof wstem, "/tmp/wwl_%d_%d_w", getpid(), id);
char *bc = NULL, *bw = NULL;
size_t nc = 0, nw = 0;
int rc = 0;
snprintf(cmd, sizeof cmd,
"timeout 300 %s/ww build -o %s "
"%s/selfhost/cmd/%s >/dev/null 2>&1",
bin, cstem, cwd, tool);
if (runwait(cmd) != 0) {
fprintf(stderr, "w6l_ww FAIL: C-link sep-build of %s\n", tool);
rc = -1;
goto cleanup;
}
snprintf(cmd, sizeof cmd,
"WW_W6L=%s/w6l_ww timeout 300 %s/ww build "
"-o %s %s/selfhost/cmd/%s >/dev/null 2>&1",
bin, bin, wstem, cwd, tool);
if (runwait(cmd) != 0) {
fprintf(stderr, "w6l_ww FAIL: ww-link sep-build of %s\n", tool);
rc = -1;
goto cleanup;
}
if (slurp(cstem, &bc, &nc) < 0 || slurp(wstem, &bw, &nw) < 0) {
fprintf(stderr, "w6l_ww FAIL: cannot read output for %s\n", tool);
rc = -1;
} else if (nc == 0) {
fprintf(stderr, "w6l_ww FAIL: empty output for %s\n", tool);
rc = -1;
} else if (nc != nw || memcmp(bc, bw, nc) != 0) {
fprintf(stderr, "w6l_ww FAIL: %s — C %zu vs ww %zu bytes\n",
tool, nc, nw);
rc = -1;
}
free(bc); free(bw);
cleanup:
/* `ww build` retains these exact caller-owned scratch trees; a
* never-created pid+id-keyed path is harmless to remove, so one
* funnel covers every exit. */
int cleanfail = 0;
if (unlink(cstem) != 0 && errno != ENOENT) {
perror(cstem);
cleanfail = 1;
}
if (unlink(wstem) != 0 && errno != ENOENT) {
perror(wstem);
cleanfail = 1;
}
snprintf(cmd, sizeof cmd, "rm -rf %s.sepwork %s.sepwork",
cstem, wstem);
if (runwait(cmd) != 0)
cleanfail = 1;
if (cleanfail)
fprintf(stderr, "w6l_ww: cleanup failed for %s\n", tool);
if (cleanfail && rc == 0)
rc = -1;
return rc;
}
int
main(void)
{
const char *bin = absbin();
if (!bin) return 1;
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
/* Real bootstrap-style links: each selfhost tool's full sep build
* (root .a + reverse-topo dep .a set + libwwrt.a). Both builds compile
* directly and differ only in the selected linker. (wwdump is excluded —
* it imports the compiler-internal cmd packages syntax/check/cgen, which don't resolve
* under the lib-path sep dep scan; w6a/w6l are self-contained
* lib-only tools.) */
const char *tools[] = { "w6a", "w6l", NULL };
int fail = 0;
int n = 0;
for (int i = 0; tools[i]; i++) {
if (build_and_diff(bin, cwd, tools[i], i) != 0) fail++;
n++;
}
if (fail) {
fprintf(stderr, "w6l_ww: %d/%d diff(s) failed\n", fail, n);
return 1;
}
printf("w6l_ww: byte-identical to C w6l on %d selfhost tool links "
"(sep root .a + reverse-topo .a set + libwwrt.a)\n", n);
return 0;
}