selfhost+test: gate wwstage w6c cgen on parser errors (#17)

Wwstage w6c_ww silently exited 0 on parse errors (stderr noise
only). The driver ran cgen on the broken AST then only checked
l.errs; ps.errs was never read, so callers downstream
(`ww build`, make rules) saw no signal and proceeded with junk
asm. Cstage cmd/w6c/main.c gates on `l.errs || p.errs` before
cgen; mirror that, hoisting the check above cgfile so the broken
AST never reaches codegen.

New test 742_parse_error pins the contract on both binaries:
writes a known-bad fragment to a pid-scoped /tmp file, runs cstage
w6c unconditionally and wwstage w6c_ww if available, asserts both
exit non-zero. Pre-fix wwstage exited 0 with junk asm; post-fix
exits 1 with parse: messages preserved on stderr.

116/116 ok. ww2 == ww3 == ww4 byte-id holds (no behavior change
for valid input).
This commit is contained in:
2026-05-18 20:55:37 +09:00
parent d78956e5df
commit c40edaa7f9
4 changed files with 109 additions and 4 deletions

View File

@@ -274,6 +274,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_chained_index \
$(BIN)/test_chained_write \
$(BIN)/test_dotbase_chained \
$(BIN)/test_parse_error \
$(BIN)/test_fnparams_bare_leaf_shadow \
$(BIN)/test_fnret_bare_leaf_shadow \
$(BIN)/test_param_shadow_mod \
@@ -659,6 +660,10 @@ $(BIN)/test_dotbase_chained: test/wcc/741_dotbase_chained.c \
$(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_parse_error: test/wcc/742_parse_error.c \
$(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_fnparams_bare_leaf_shadow: test/wcc/732_fnparams_bare_leaf_shadow.c \
$(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
$(CC) $(CFLAGS) -o $@ $<

View File

@@ -20949,12 +20949,14 @@ export fn main(argc: i32, argv: **u8) i32 = {
let ps: parser;
parserinit(&ps, ar, &l);
let f: *node = parsefile(&ps);
// Gate cgen on parse-stage errors. Mirrors cmd/w6c/main.c's
// `if (l.errs || p.errs) return 1;` — broken AST otherwise reaches
// cgen and emits junk asm with a zero exit (silent miscompile).
if (l.errs > 0 || ps.errs > 0) { return 1; };
let cg: cgen;
cgeninit(&cg, ar);
cgfile(&cg, f);
if (l.errs > 0) { return 1; };
return 0;
};

View File

@@ -142,11 +142,13 @@ export fn main(argc: i32, argv: **u8) i32 = {
let ps: parser;
parserinit(&ps, ar, &l);
let f: *node = parsefile(&ps);
// Gate cgen on parse-stage errors. Mirrors cmd/w6c/main.c's
// `if (l.errs || p.errs) return 1;` — broken AST otherwise reaches
// cgen and emits junk asm with a zero exit (silent miscompile).
if (l.errs > 0 || ps.errs > 0) { return 1; };
let cg: cgen;
cgeninit(&cg, ar);
cgfile(&cg, f);
if (l.errs > 0) { return 1; };
return 0;
};

View File

@@ -0,0 +1,96 @@
/*
* 742_parse_error — both stages of w6c must exit non-zero on a parse
* error. Pins rule 10 symmetry on the failure path.
*
* Predecessor symptom: wwstage `w6c_ww` ran cgen on the broken AST and
* exited 0 (stderr noise only). Drivers downstream of w6c (`ww build`,
* make rules) saw no signal and proceeded.
*
* cstage `w6c` checks `l.errs || p.errs` before cgen and returns 1.
* This test pins the same exit-code contract on both binaries against a
* known-bad fixture.
*/
#include <stdio.h>
#include <stdlib.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 int
write_file(const char *path, const char *content)
{
FILE *f = fopen(path, "wb");
if (!f) return -1;
fputs(content, f);
fclose(f);
return 0;
}
static int
check_fail(const char *tool, const char *src)
{
char cmd[4096];
snprintf(cmd, sizeof cmd, "%s %s >/dev/null 2>/dev/null", tool, src);
int rc = runwait(cmd);
if (rc == 0) {
fprintf(stderr, "742 FAIL: %s exited 0 on parse-error fixture\n",
tool);
return -1;
}
return 0;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[2048];
if (bin[0] != '/') {
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char src[64], outs[64];
snprintf(src, sizeof src, "/tmp/parse_err_%d.ww", getpid());
snprintf(outs, sizeof outs, "/tmp/parse_err_%d.s", getpid());
if (write_file(src, "fn fn fn ;\n") != 0) {
fprintf(stderr, "742: cannot write fixture\n");
return 1;
}
char w6c[2240], w6c_ww[2240], w6c_ww_bin[2240];
snprintf(w6c, sizeof w6c, "%s/w6c -o %s", bin, outs);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww -o %s", bin, outs);
snprintf(w6c_ww_bin, sizeof w6c_ww_bin, "%s/w6c_ww", bin);
int total = 0, fail = 0;
total++;
if (check_fail(w6c, src) != 0) fail++;
if (access(w6c_ww_bin, X_OK) == 0) {
total++;
if (check_fail(w6c_ww, src) != 0) fail++;
}
unlink(src);
unlink(outs);
if (fail) {
fprintf(stderr, "742_parse_error: %d/%d failed\n", fail, total);
return 1;
}
printf("742_parse_error: %d/%d ok\n", total, total);
return 0;
}