Files
ww/test/wcc/911_attest_record.c
Hojun-Cho 16c83e70d3 ww test: fork-isolated record-and-continue harness (lib/test, both stages)
lib/test/run.ww: fork+wait4 runner; each @test runs in its own child,
abort/SEGV/FPE decoded from wait-status, failures recorded and the run
continues; exit = fail count. Tests are hermetic: module globals do not
persist test-to-test (fresh fork image; sanctioned divergence from
harec's shared-process __test_main, no setjmp/signal layer needed).
-T synth (both stages) emits a module-global (str,*fn() void) table +
return run(table) instead of straight-line calls. Driver twins bundle
lib/test under test mode and gain ww test -c/-o (go test -c) so the
byte-id gates diff the same artifact the real path builds. Gates
989/910/997 rewired onto it; new 911 pins record-and-continue across
all three fault classes; 949 +3 rows. (#17-team commit-2)
2026-06-11 00:08:39 +09:00

174 lines
5.1 KiB
C

/*
* 911_attest_record — #17 record-and-continue harness (lib/test + the
* table-synth -T entry). Sibling of 910_at_test (which pins the synth
* itself + the #6 non-T drop); this one pins the RUNTIME behaviour the
* fork+wait4 runner adds:
*
* 1. RECORD-AND-CONTINUE — `ww test` the mixed fixture (one pass, then
* assert-fail/SIGSEGV/SIGFPE, then a final pass). The runner must
* proceed past every failure, emit the right per-test `name ... ok/
* FAIL` line (table-driven below), print the right `P passed, F
* failed` summary, and exit with the failure count (nonzero).
* 2. ALL-PASS — `ww test` the all-pass fixture exits 0 (no false
* failure from the new runner).
* 3. BYTE-ID (rule 10) — `ww test -c` the mixed fixture into a
* lib/test-inclusive combined, then `w6c -T` and `w6c_ww -T` must
* emit byte-identical asm (the table + fork loop, both stages).
*
* The fork changes failure OUTPUT, not pass behaviour: a passing fixture
* still exits 0, so the 35 converted lib tests stay green (904/967/...).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.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 *out, size_t outsz)
{
FILE *f = fopen(path, "rb");
if (!f) return -1;
size_t n = fread(out, 1, outsz - 1, f);
out[n] = '\0';
fclose(f);
return 0;
}
/* Each expected per-test verdict line, in source/collection order. */
static const char *expect_lines[] = {
"rec_pass_a ... ok",
"rec_assert ... FAIL",
"rec_segv ... FAIL",
"rec_div0 ... FAIL",
"rec_pass_b ... ok",
"2 passed, 3 failed",
};
static int
record_continue(const char *bin)
{
int pid = getpid();
char out[256], cmd[4096], buf[8192];
snprintf(out, sizeof out, "/tmp/at911_%d.out", pid);
/* `ww test` builds the fixture (auto-bundling lib/test) and runs it;
* the exit code is the runner's failure count. */
snprintf(cmd, sizeof cmd,
"%s/ww test test/wcc/data/attest_record.ww > %s 2>/dev/null",
bin, out);
int rc = runwait(cmd);
if (rc != 3) {
fprintf(stderr, "911 FAIL: record-and-continue exit %d "
"(expected 3 failures)\n", rc);
return 1;
}
if (slurp(out, buf, sizeof buf) != 0) {
fprintf(stderr, "911 FAIL: cannot read runner output\n");
return 1;
}
for (size_t i = 0; i < sizeof expect_lines / sizeof expect_lines[0]; i++) {
if (strstr(buf, expect_lines[i]) == NULL) {
fprintf(stderr, "911 FAIL: missing runner line '%s'\n"
"--- got ---\n%s\n", expect_lines[i], buf);
unlink(out);
return 1;
}
}
unlink(out);
return 0;
}
static int
all_pass(const char *bin)
{
char cmd[4096];
snprintf(cmd, sizeof cmd,
"%s/ww test test/wcc/data/attest_pass.ww > /dev/null 2>&1", bin);
int rc = runwait(cmd);
if (rc != 0) {
fprintf(stderr, "911 FAIL: all-pass fixture exit %d "
"(expected 0)\n", rc);
return 1;
}
return 0;
}
static int
byteid(const char *bin)
{
int pid = getpid();
char stem[256], comb[256], cs[256], ws[256], cmd[4096];
snprintf(stem, sizeof stem, "/tmp/at911_bid_%d", pid);
snprintf(comb, sizeof comb, "%s.combined.ww", stem);
snprintf(cs, sizeof cs, "/tmp/at911_bid_c_%d.s", pid);
snprintf(ws, sizeof ws, "/tmp/at911_bid_w_%d.s", pid);
/* `ww test -c -o <stem>` (compile-only) writes a lib/test-inclusive
* combined beside -o without running the harness. */
snprintf(cmd, sizeof cmd,
"%s/ww test -c -o %s test/wcc/data/attest_record.ww > /dev/null 2>&1",
bin, stem);
runwait(cmd);
if (access(comb, 0) != 0) {
fprintf(stderr, "911 FAIL: ww test -c produced no %s\n", comb);
return 1;
}
snprintf(cmd, sizeof cmd, "%s/w6c -T %s -o %s 2>/dev/null", bin, comb, cs);
if (runwait(cmd) != 0) { fprintf(stderr, "911 FAIL: w6c -T (byteid)\n"); return 1; }
snprintf(cmd, sizeof cmd, "%s/w6c_ww -T %s -o %s 2>/dev/null", bin, comb, ws);
if (runwait(cmd) != 0) { fprintf(stderr, "911 FAIL: w6c_ww -T (byteid)\n"); return 1; }
char bc[262144], bw[262144];
int rc = 0;
if (slurp(cs, bc, sizeof bc) < 0 || slurp(ws, bw, sizeof bw) < 0) {
fprintf(stderr, "911 FAIL: slurp byteid asm\n");
rc = 1;
} else if (strcmp(bc, bw) != 0) {
fprintf(stderr, "911 FAIL: -T asm differs between stages\n");
rc = 1;
}
unlink(comb); unlink(cs); unlink(ws);
snprintf(cmd, sizeof cmd, "%s.s", stem); unlink(cmd);
snprintf(cmd, sizeof cmd, "%s.o", stem); unlink(cmd);
unlink(stem);
return rc;
}
int
main(void)
{
const char *bin = absbin();
if (!bin) { fprintf(stderr, "911 FAIL: getcwd\n"); return 1; }
if (record_continue(bin) != 0) return 1;
if (all_pass(bin) != 0) return 1;
if (byteid(bin) != 0) return 1;
printf("@test record-and-continue: 3 fault classes recorded + run "
"proceeds + counts + all-pass exit 0 + cs/ww byte-id (#17)\n");
return 0;
}