Every surviving carrier now owns its artifacts: checked mkdir/mkdtemp/ fopen acquisition, one all-exit cleanup funnel per carrier, ENOENT- tolerant checked unlinks, exact-path deletion (rm -rf only for an owned pid-keyed dir or a .sepwork beneath one), and cleanup failure fails a passing carrier without overwriting its diagnostic. In the same pass the carriers adapt to the driver contract this branch lands: --sep and WW_PKGCACHE are gone, -S and the /tmp/ww_run_<pid> scratch contract are asserted, and rows whose runtime or reject coverage moved to test/wcc/data fixtures or test/lang @test owners are trimmed to the byte/artifact/diagnostic observations only they can make. Repair and adaptation ride together because most files interleave both in the same hunks; splitting would manufacture intermediate carrier states that never existed and cannot run against either driver.
335 lines
10 KiB
C
335 lines
10 KiB
C
/*
|
|
* 682_arr_enum_elem — cstage and wwstage agree, byte-for-byte and at
|
|
* runtime, on `[N]enum` element access (task #8).
|
|
*
|
|
* The bug: wwstage's elemsizeofc (selfhost/cmd/wcc/cgenutil.ww) was the
|
|
* odd-one-out among the elem*c helpers — its siblings elemissignedc /
|
|
* elemisfloatc read the checker-stamped tinfo (t.type_.sub), but
|
|
* elemsizeofc derived the element width purely structurally
|
|
* (elemsizeof -> primsize -> slotsize). A named-narrow element
|
|
* (`[N]tk`, tk = enum i32) is not a builtin prim, so primsize returned
|
|
* 0 and elemsizeofc fell through to a raw 8-byte slot instead of the
|
|
* enum's i32 backing (4). That mis-sized BOTH manifestations through
|
|
* the one choke-point:
|
|
* (a) cgindex READ — `a[i]` strode by 8 (MOVQ) where cstage strode by
|
|
* 4 (MOVSXD), reading the wrong/out-of-bounds element for i>=1.
|
|
* (b) array-init STORE — a local `[N]enum` literal init stored at
|
|
* stride 8 into a stride-4 frame slot, overrunning the slot and
|
|
* smashing the frame -> wwstage-built binary SEGFAULTED.
|
|
* cstage is runtime-correct (cgen.c N_INDEX idx_eff(bt)->sub->size,
|
|
* N_LET array-init lu->sub->size, cgen.c:6387). The fix aligns wwstage
|
|
* UP: elemsizeofc now reads the element size off the stamped tinfo
|
|
* (peeling TY_NAMED), recovering 4 for `enum i32` and bringing all four
|
|
* elem*c helpers onto the same tinfo SSoT.
|
|
*
|
|
* No in-tree `[N]enum` / aliased-narrow element existed before kwtab,
|
|
* which is why this was byte-id-gate-blind until now.
|
|
*
|
|
* row | shape | want
|
|
* -----------------+------------------------------------+--------------
|
|
* global_idx0 | global [4]tk, return g[0]. idx0 is | 7 + byte-id
|
|
* | stride-independent (offset 0). |
|
|
* global_read | global [4]tk, return g[2]. Pre-fix | 99 + byte-id
|
|
* | stride-8 read offset 16 = past the |
|
|
* | 16-byte array -> garbage. |
|
|
* local_read | LOCAL [4]tk init, return a[1]. | 21 + byte-id
|
|
* | Exercises the array-init STORE + |
|
|
* | the stride-4 READ together. |
|
|
* local_init_sum | LOCAL [4]tk, return a[0]+a[3]. | 10 + byte-id
|
|
* | First+last element after init. |
|
|
* local_signed | LOCAL [2]tk with a negative enum | 251 (-5)
|
|
* | value, return a[0]:i32. Pins the | + byte-id
|
|
* | MOVSXD sign-extend at stride 4 |
|
|
* | (pre-fix MOVQ read 8 bytes). |
|
|
* frame_smash | LOCAL [6]tk init + read a[5]. | 5 + byte-id
|
|
* | Pre-fix stride-8 store writes 48 |
|
|
* | bytes into a 24-byte slot -> |
|
|
* | frame smash / SEGFAULT under |
|
|
* | wwstage. Correct stride-4 store |
|
|
* | (24 bytes) round-trips a[5]=5. |
|
|
* width1_u8 | LOCAL [3]tu (enum u8), read a[1]. | 200 + byte-id
|
|
* | Pins the narrow-scalar class at |
|
|
* | width 1: stride-1 store + MOVZBQ |
|
|
* | zero-extend load (UNSIGNED enum). |
|
|
* | Pre-fix stride-8 store/read over- |
|
|
* | runs the 3-byte slot -> garbage. |
|
|
* width2_i16 | LOCAL [3]ts (enum i16) w/ negative | 251 (-5)
|
|
* | value, read a[1]:i32. Pins width 2:| + byte-id
|
|
* | stride-2 store + MOVSWQ sign- |
|
|
* | extend (pre-fix stride-8 garbage). |
|
|
*
|
|
* width1/width2 widen the coverage from the single enum-i32 (width 4)
|
|
* case to the whole {1,2,4} narrow-scalar class the fix closes, and add
|
|
* the unsigned (MOVZBQ) load path alongside the signed (MOVSXD/MOVSWQ).
|
|
*
|
|
* Exit-code rows confirm both stages run correctly. The asm-byte-id
|
|
* rows pin the symmetric stride-4 MOVSXD emit (cstage == wwstage);
|
|
* pre-fix wwstage emitted stride-8 MOVQ, so the diff was non-empty.
|
|
*/
|
|
#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 *label; const char *src; int want; };
|
|
|
|
static const struct row rows[] = {
|
|
{ "global_idx0",
|
|
"type tk = enum i32 { A = 7i32, B = 21i32, C = 99i32, D = 3i32 };\n"
|
|
"let g: [4]tk = [tk.A, tk.B, tk.C, tk.D];\n"
|
|
"fn main() i32 = {\n"
|
|
"\treturn g[0]: i32;\n"
|
|
"};\n",
|
|
7 },
|
|
|
|
{ "global_read",
|
|
"type tk = enum i32 { A = 7i32, B = 21i32, C = 99i32, D = 3i32 };\n"
|
|
"let g: [4]tk = [tk.A, tk.B, tk.C, tk.D];\n"
|
|
"fn main() i32 = {\n"
|
|
"\treturn g[2]: i32;\n"
|
|
"};\n",
|
|
99 },
|
|
|
|
{ "local_read",
|
|
"type tk = enum i32 { A = 7i32, B = 21i32, C = 99i32, D = 3i32 };\n"
|
|
"fn main() i32 = {\n"
|
|
"\tlet a: [4]tk = [tk.A, tk.B, tk.C, tk.D];\n"
|
|
"\treturn a[1]: i32;\n"
|
|
"};\n",
|
|
21 },
|
|
|
|
{ "local_init_sum",
|
|
"type tk = enum i32 { A = 7i32, B = 21i32, C = 99i32, D = 3i32 };\n"
|
|
"fn main() i32 = {\n"
|
|
"\tlet a: [4]tk = [tk.A, tk.B, tk.C, tk.D];\n"
|
|
"\treturn (a[0]: i32) + (a[3]: i32);\n"
|
|
"};\n",
|
|
10 },
|
|
|
|
{ "local_signed",
|
|
"type tk = enum i32 { A = 7i32, G = -5i32 };\n"
|
|
"fn main() i32 = {\n"
|
|
"\tlet a: [2]tk = [tk.G, tk.A];\n"
|
|
"\treturn a[0]: i32;\n"
|
|
"};\n",
|
|
251 },
|
|
|
|
{ "frame_smash",
|
|
"type tk = enum i32 { A = 7i32, B = 21i32, C = 99i32,\n"
|
|
"\tD = 3i32, E = 11i32, F = 5i32 };\n"
|
|
"fn main() i32 = {\n"
|
|
"\tlet a: [6]tk = [tk.A, tk.B, tk.C, tk.D, tk.E, tk.F];\n"
|
|
"\treturn a[5]: i32;\n"
|
|
"};\n",
|
|
5 },
|
|
|
|
{ "width1_u8",
|
|
"type tu = enum u8 { A = 7u8, B = 200u8, C = 3u8 };\n"
|
|
"fn main() i32 = {\n"
|
|
"\tlet a: [3]tu = [tu.A, tu.B, tu.C];\n"
|
|
"\treturn a[1]: i32;\n"
|
|
"};\n",
|
|
200 },
|
|
|
|
{ "width2_i16",
|
|
"type ts = enum i16 { A = 7i16, G = -5i16, C = 99i16 };\n"
|
|
"fn main() i32 = {\n"
|
|
"\tlet a: [3]ts = [ts.A, ts.G, ts.C];\n"
|
|
"\treturn a[1]: i32;\n"
|
|
"};\n",
|
|
251 },
|
|
};
|
|
|
|
static int
|
|
run_driver(const char *driver, const struct row *r, int i)
|
|
{
|
|
char tmpdir[64], src[128], outbin[128], scratch[160], cmd[1024];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/aee_%d_d_%d", getpid(), i);
|
|
snprintf(src, sizeof src, "%s/aee_%d_%d.ww", tmpdir, getpid(), i);
|
|
snprintf(outbin, sizeof outbin, "%s/aee_%d_%d", tmpdir, getpid(), i);
|
|
snprintf(scratch, sizeof scratch, "%s.sepwork", outbin);
|
|
|
|
if (mkdir(tmpdir, 0755) != 0) {
|
|
perror(tmpdir);
|
|
return -1;
|
|
}
|
|
int result = -1;
|
|
FILE *f = fopen(src, "wb");
|
|
if (!f) { perror(src); goto cleanup; }
|
|
wwtest_fputs(r->src, f);
|
|
if (fclose(f) != 0) { perror(src); goto cleanup; }
|
|
|
|
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
|
|
driver, outbin, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: build via %s failed\n",
|
|
r->label, driver);
|
|
goto cleanup;
|
|
}
|
|
|
|
result = runwait(outbin);
|
|
|
|
cleanup:
|
|
/* `ww build -o` retains this exact caller-owned scratch tree. */
|
|
snprintf(cmd, sizeof cmd, "rm -rf %s", scratch);
|
|
int cleanfail = runwait(cmd) != 0;
|
|
if (unlink(outbin) != 0 && errno != ENOENT) {
|
|
perror(outbin); cleanfail = 1;
|
|
}
|
|
if (unlink(src) != 0 && errno != ENOENT) {
|
|
perror(src); cleanfail = 1;
|
|
}
|
|
if (rmdir(tmpdir) != 0) {
|
|
perror(tmpdir); cleanfail = 1;
|
|
}
|
|
if (cleanfail && result == r->want) result = -1;
|
|
return result;
|
|
}
|
|
|
|
/* asm_byte_identical — generate .s via cstage's w6c and wwstage's
|
|
* w6c_ww and diff. This is the regression-pinning row for #8: pre-fix
|
|
* wwstage emitted stride-8 MOVQ where cstage emitted stride-4 MOVSXD,
|
|
* so the diff was non-empty. */
|
|
static int
|
|
asm_byte_identical(const char *bin, const struct row *r, int i)
|
|
{
|
|
char tmpdir[64], src[128], cs[128], ws[128], cmd[1024];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/aee_asm_%d_%d", getpid(), i);
|
|
snprintf(src, sizeof src, "%s/input.ww", tmpdir);
|
|
snprintf(cs, sizeof cs, "%s/c.s", tmpdir);
|
|
snprintf(ws, sizeof ws, "%s/w.s", tmpdir);
|
|
if (mkdir(tmpdir, 0755) != 0) {
|
|
perror(tmpdir);
|
|
return -1;
|
|
}
|
|
int rc = -1;
|
|
|
|
FILE *f = fopen(src, "wb");
|
|
if (!f) { perror(src); goto cleanup; }
|
|
wwtest_fputs(r->src, f);
|
|
if (fclose(f) != 0) { perror(src); goto cleanup; }
|
|
|
|
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
|
|
goto cleanup;
|
|
}
|
|
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
|
|
bin, ws, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
|
|
goto cleanup;
|
|
}
|
|
|
|
FILE *fc = fopen(cs, "rb");
|
|
FILE *fw = fopen(ws, "rb");
|
|
rc = 0;
|
|
if (!fc || !fw) {
|
|
rc = -1;
|
|
} else {
|
|
for (;;) {
|
|
int a = fgetc(fc);
|
|
int b = fgetc(fw);
|
|
if (a != b) { rc = -1; break; }
|
|
if (a == EOF) break;
|
|
}
|
|
}
|
|
if (fc) fclose(fc);
|
|
if (fw) fclose(fw);
|
|
if (rc != 0)
|
|
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
|
|
r->label);
|
|
|
|
cleanup:
|
|
if (unlink(src) != 0 && errno != ENOENT) {
|
|
perror(src); rc = -1;
|
|
}
|
|
if (unlink(cs) != 0 && errno != ENOENT) {
|
|
perror(cs); rc = -1;
|
|
}
|
|
if (unlink(ws) != 0 && errno != ENOENT) {
|
|
perror(ws); rc = -1;
|
|
}
|
|
if (rmdir(tmpdir) != 0) {
|
|
perror(tmpdir); rc = -1;
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
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;
|
|
}
|
|
|
|
char cdrv[1024];
|
|
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
|
char wdrv[1024];
|
|
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
|
|
|
struct { const char *name; const char *path; int gated_on_existence; }
|
|
drivers[] = {
|
|
{ "cstage", cdrv, 0 },
|
|
{ "wwstage", wdrv, 1 },
|
|
{ NULL, NULL, 0 },
|
|
};
|
|
|
|
int n = (int)(sizeof rows / sizeof rows[0]);
|
|
int total = 0, fail = 0;
|
|
|
|
for (int d = 0; drivers[d].name; d++) {
|
|
if (drivers[d].gated_on_existence
|
|
&& access(drivers[d].path, X_OK) != 0) {
|
|
fprintf(stderr, "arr_enum_elem: skip %s (no %s)\n",
|
|
drivers[d].name, drivers[d].path);
|
|
continue;
|
|
}
|
|
for (int i = 0; i < n; i++) {
|
|
int got = run_driver(drivers[d].path, &rows[i], i);
|
|
total++;
|
|
if (got != rows[i].want) {
|
|
fprintf(stderr,
|
|
"arr_enum_elem[%s][%s]: exit=%d want=%d\n",
|
|
drivers[d].name, rows[i].label,
|
|
got, rows[i].want);
|
|
fail++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (access(wdrv, X_OK) == 0) {
|
|
for (int i = 0; i < n; i++) {
|
|
total++;
|
|
if (asm_byte_identical(bin, &rows[i], i) != 0)
|
|
fail++;
|
|
}
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr,
|
|
"arr_enum_elem: %d/%d fixtures failed\n", fail, total);
|
|
return 1;
|
|
}
|
|
printf("arr_enum_elem: %d/%d ok\n", total, total);
|
|
return 0;
|
|
}
|