omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / gen / abstract / gabs.c
| 1 | /* |
| 2 | * gabs.c -- C helpers of the GEN-3 abstraction extractor (tools/gen/abstract/extract, verify; aien-spark/omega#478). |
| 3 | * Deterministic, no clock, no randomness. Only the corpus files and the repo's own tools' outputs are read. |
| 4 | * |
| 5 | * gabs split <solution.omegese> <out prefix> |
| 6 | * splits a program into its functions: writes <prefix>.<k>.omegese (k = 0, 1, ... in program order) and |
| 7 | * prints one line per function "fn <k> <name> <calls> <nlines>", <calls> = the earlier functions its body |
| 8 | * names, comma-separated, or "-". Refuses blank lines, tabs, CR and text outside a function. Block depth |
| 9 | * follows the Omegese statement words (tools/omegese/omegese.c): `if`, `while` and `for` open a block, |
| 10 | * `end` closes one; a line starting with any other Omegese keyword that is not a known statement word |
| 11 | * (else break ret trap put free) is refused, so a new block keyword cannot silently misplace a boundary. |
| 12 | * gabs canon <in.json> <out.json> |
| 13 | * the omegese op list with every function name replaced by fn<k>_ (k in program order) in the "name" |
| 14 | * and "func" keys (RECORD_SCHEMA.md section 6) |
| 15 | * gabs inputs <count> [--max <arg>:<max>] <type>... |
| 16 | * <count> deterministic input lines for parameters of these IR types (bool u8 .. i64): boundary values |
| 17 | * first, then a fixed 64-bit LCG mixed over each type's range (small values weighted in) |
| 18 | * gabs run <ola_runner> <scratch dir> <ir file> <entry> <ret> <inputs> <expected|-> [<param type>...] |
| 19 | * runs the IR in the harness's sandboxed runner (tests/one_layer/acceptance/ola_runner, called, not |
| 20 | * copied) on <inputs> and compares with <expected> (decimal per line, "TRAP" or "TRAP <code>"), the |
| 21 | * way ola.c compares (values normalised at the return type's width). Prints one line: |
| 22 | * "ACCEPT n=<lines>" (exit 0) or "REJECT <reason> [line=<k> got=<x> want=<y>]" (exit 1). |
| 23 | * With expected "-" it prints the observed result per line instead ("<value>" or "TRAP <code>"). |
| 24 | * Exit 2: usage or I/O error. |
| 25 | * |
| 26 | * Copied, because ola.c is a program, not a library (its main is static and it scores whole cases): the |
| 27 | * value grammar (strict decimal, parse_val) and the width normalisation (at_width), about 30 lines. |
| 28 | */ |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <inttypes.h> |
| 32 | #include <poll.h> |
| 33 | #include <signal.h> |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <sys/resource.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/syscall.h> |
| 40 | #include <sys/wait.h> |
| 41 | #include <unistd.h> |
| 42 | |
| 43 | static void die(const char *m, const char *a) |
| 44 | { |
| 45 | fprintf(stderr, "gabs: %s%s%s\n", m, a ? " " : "", a ? a : ""); |
| 46 | exit(2); |
| 47 | } |
| 48 | |
| 49 | /* fopen(path, "wb") with an explicit creation mode 0644 instead of 0666 & umask */ |
| 50 | static FILE *wcreate(const char *path) |
| 51 | { |
| 52 | int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); |
| 53 | if (fd < 0) return NULL; |
| 54 | FILE *f = fdopen(fd, "wb"); |
| 55 | if (!f) close(fd); |
| 56 | return f; |
| 57 | } |
| 58 | |
| 59 | static char *slurp(const char *p, size_t *n) |
| 60 | { |
| 61 | FILE *f = fopen(p, "rb"); |
| 62 | if (!f) return NULL; |
| 63 | size_t cap = 1 << 16, len = 0, r; |
| 64 | char *b = malloc(cap + 1); |
| 65 | while (b && (r = fread(b + len, 1, cap - len, f)) > 0) { |
| 66 | len += r; |
| 67 | if (len == cap) { |
| 68 | if (cap >= (16u << 20)) { free(b); b = NULL; break; } |
| 69 | char *nb = realloc(b, (cap *= 2) + 1); |
| 70 | if (!nb) { free(b); b = NULL; break; } |
| 71 | b = nb; |
| 72 | } |
| 73 | } |
| 74 | fclose(f); |
| 75 | if (b) { b[len] = 0; *n = len; } |
| 76 | return b; |
| 77 | } |
| 78 | |
| 79 | /* ---- split ---------------------------------------------------------------------------------------------- */ |
| 80 | #define MAXF 32 |
| 81 | static int split(const char *in, const char *prefix) |
| 82 | { |
| 83 | size_t n; |
| 84 | char *s = slurp(in, &n); |
| 85 | if (!s) die("cannot read", in); |
| 86 | if (!n || s[n - 1] != '\n') die("program does not end with a newline:", in); |
| 87 | char *names[MAXF]; |
| 88 | int start[MAXF], nf = 0, line = 0, open = 0; |
| 89 | char *lines[4096]; |
| 90 | int nl = 0; |
| 91 | for (char *p = s; *p;) { |
| 92 | char *e = strchr(p, '\n'); |
| 93 | *e = 0; |
| 94 | if (nl == 4096) die("too many lines", in); |
| 95 | for (char *q = p; *q; q++) if ((unsigned char)*q < 0x20 || (unsigned char)*q > 0x7e) die("byte outside 0x20..0x7E in", in); |
| 96 | if (!*p) die("blank line in", in); |
| 97 | lines[nl++] = p; |
| 98 | p = e + 1; |
| 99 | } |
| 100 | for (line = 0; line < nl; line++) { |
| 101 | char *l = lines[line]; |
| 102 | if (!strncmp(l, "fn ", 3)) { |
| 103 | if (open) die("fn inside a function in", in); |
| 104 | if (nf == MAXF) die("too many functions in", in); |
| 105 | char nm[64]; |
| 106 | if (sscanf(l + 3, "%63s", nm) != 1) die("fn without a name in", in); |
| 107 | names[nf] = strdup(nm); |
| 108 | start[nf++] = line; |
| 109 | open = 1; |
| 110 | } else if (!open) die("text outside a function in", in); |
| 111 | /* a function ends at the `end` that closes it: track block depth */ |
| 112 | if (!strncmp(l, "fn ", 3)) { open = 1; continue; } |
| 113 | char w0[16] = ""; |
| 114 | if (sscanf(l, "%15s", w0) != 1) die("line without a word in", in); |
| 115 | if (!strcmp(w0, "if") || !strcmp(w0, "while") || !strcmp(w0, "for")) open++; |
| 116 | else if (!strcmp(w0, "end")) { |
| 117 | if (strcmp(l, "end")) die("text after end in", in); |
| 118 | open--; |
| 119 | } else if (!strcmp(w0, "else") || !strcmp(w0, "break") || !strcmp(w0, "ret") || !strcmp(w0, "trap") || |
| 120 | !strcmp(w0, "put") || !strcmp(w0, "free")) { |
| 121 | } else { |
| 122 | static const char *const other_kw[] = {"to", "void", "true", "false", "bound", "="}; |
| 123 | for (size_t i = 0; i < sizeof other_kw / sizeof other_kw[0]; i++) |
| 124 | if (!strcmp(w0, other_kw[i])) die("unknown statement opener (not a split block word) in", in); |
| 125 | } |
| 126 | } |
| 127 | if (open) die("unclosed function in", in); |
| 128 | for (int k = 0; k < nf; k++) { |
| 129 | int a = start[k], b = k + 1 < nf ? start[k + 1] : nl; |
| 130 | char path[4096]; |
| 131 | snprintf(path, sizeof path, "%s.%d.omegese", prefix, k); |
| 132 | FILE *f = wcreate(path); |
| 133 | if (!f) die("cannot write", path); |
| 134 | char calls[1024] = ""; |
| 135 | for (int i = a; i < b; i++) { |
| 136 | fprintf(f, "%s\n", lines[i]); |
| 137 | if (i == a) continue; |
| 138 | /* words of the line (brackets are separate words) naming an earlier function */ |
| 139 | char tmp[1100]; |
| 140 | snprintf(tmp, sizeof tmp, "%s", lines[i]); |
| 141 | for (char *q = tmp; *q; q++) if (*q == '(' || *q == ')') *q = ' '; |
| 142 | for (char *w = strtok(tmp, " "); w; w = strtok(NULL, " ")) |
| 143 | for (int j = 0; j < k; j++) |
| 144 | if (!strcmp(w, names[j])) { |
| 145 | char pat[80]; |
| 146 | snprintf(pat, sizeof pat, ",%s,", w); |
| 147 | char have[1100]; |
| 148 | snprintf(have, sizeof have, ",%s,", calls); |
| 149 | if (!strstr(have, pat)) { |
| 150 | if (calls[0]) strncat(calls, ",", sizeof calls - strlen(calls) - 1); |
| 151 | strncat(calls, w, sizeof calls - strlen(calls) - 1); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | if (fclose(f)) die("cannot write", path); |
| 156 | printf("fn %d %s %s %d\n", k, names[k], calls[0] ? calls : "-", b - a); |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* ---- canon ---------------------------------------------------------------------------------------------- */ |
| 162 | static int canon(const char *in, const char *out) |
| 163 | { |
| 164 | size_t n; |
| 165 | char *s = slurp(in, &n); |
| 166 | if (!s) die("cannot read", in); |
| 167 | char *names[MAXF]; |
| 168 | int nf = 0; |
| 169 | const char *K1 = "\"name\": \""; |
| 170 | for (char *p = strstr(s, K1); p; p = strstr(p + 1, K1)) { |
| 171 | char *a = p + strlen(K1), *e = strchr(a, '"'); |
| 172 | if (!e || e - a > 63 || nf == MAXF) die("bad function name in", in); |
| 173 | names[nf] = strndup(a, (size_t)(e - a)); |
| 174 | nf++; |
| 175 | } |
| 176 | if (!nf) die("no function in", in); |
| 177 | FILE *f = wcreate(out); |
| 178 | if (!f) die("cannot write", out); |
| 179 | for (char *p = s; *p;) { |
| 180 | const char *keys[2] = {"\"name\": \"", "\"func\": \""}; |
| 181 | int hit = -1; |
| 182 | for (int k = 0; k < 2; k++) if (!strncmp(p, keys[k], strlen(keys[k]))) hit = k; |
| 183 | if (hit < 0) { fputc(*p++, f); continue; } |
| 184 | char *a = p + strlen(keys[hit]), *e = strchr(a, '"'); |
| 185 | if (!e) die("unterminated name in", in); |
| 186 | int idx = -1; |
| 187 | for (int k = 0; k < nf; k++) if ((size_t)(e - a) == strlen(names[k]) && !strncmp(a, names[k], (size_t)(e - a))) idx = k; |
| 188 | if (idx < 0) die("call to an unknown function in", in); |
| 189 | fprintf(f, "%sfn%d_\"", keys[hit], idx); |
| 190 | p = e + 1; |
| 191 | } |
| 192 | if (fclose(f)) die("cannot write", out); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* ---- types and values (parse_val / at_width copied from tests/one_layer/acceptance/ola.c) -------------- */ |
| 197 | enum { K_BOOL, K_U8, K_U16, K_U32, K_U64, K_I8, K_I16, K_I32, K_I64 }; |
| 198 | static const char *const KN[] = {"bool", "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64"}; |
| 199 | static int kind_of(const char *s) { for (int i = 0; i < 9; i++) if (!strcmp(s, KN[i])) return i; return -1; } |
| 200 | static unsigned kwidth(int k) { static const unsigned w[] = {8, 8, 16, 32, 64, 8, 16, 32, 64}; return w[k]; } |
| 201 | static int ksigned(int k) { return k >= K_I8 && k <= K_I64; } |
| 202 | |
| 203 | static int parse_val(const char *s, int k, uint64_t *out) |
| 204 | { |
| 205 | int neg = 0; |
| 206 | if (*s == '-') { neg = 1; s++; } |
| 207 | if (!*s || (s[0] == '0' && s[1])) return -1; |
| 208 | uint64_t v = 0; |
| 209 | for (; *s; s++) { |
| 210 | if (*s < '0' || *s > '9') return -1; |
| 211 | if (v > (UINT64_MAX - (uint64_t)(*s - '0')) / 10) return -1; |
| 212 | v = v * 10 + (uint64_t)(*s - '0'); |
| 213 | } |
| 214 | unsigned w = kwidth(k); |
| 215 | if (k == K_BOOL) { if (neg || v > 1) return -1; *out = v; return 0; } |
| 216 | if (!ksigned(k)) { |
| 217 | if (neg && v) return -1; |
| 218 | if (w < 64 && v >> w) return -1; |
| 219 | *out = v; |
| 220 | return 0; |
| 221 | } |
| 222 | uint64_t lim = (uint64_t)1 << (w - 1); |
| 223 | if (neg ? v > lim : v >= lim) return -1; |
| 224 | *out = neg ? (uint64_t)(-(int64_t)(v - 1) - 1) : v; |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static uint64_t at_width(uint64_t r, int k) |
| 229 | { |
| 230 | unsigned w = kwidth(k); |
| 231 | if (w == 64) return r; |
| 232 | uint64_t m = ((uint64_t)1 << w) - 1; |
| 233 | r &= m; |
| 234 | if (ksigned(k) && (r >> (w - 1))) r |= ~m; |
| 235 | return r; |
| 236 | } |
| 237 | |
| 238 | static void put_val(FILE *f, uint64_t v, int k) |
| 239 | { |
| 240 | if (ksigned(k)) fprintf(f, "%" PRId64, (int64_t)v); |
| 241 | else fprintf(f, "%" PRIu64, v); |
| 242 | } |
| 243 | |
| 244 | /* ---- inputs --------------------------------------------------------------------------------------------- */ |
| 245 | static uint64_t kmin(int k) { return ksigned(k) ? (~(uint64_t)0 << (kwidth(k) - 1)) : 0; } |
| 246 | static uint64_t kmax(int k) { if (k == K_BOOL) return 1; unsigned w = kwidth(k) - (unsigned)ksigned(k); return w == 64 ? UINT64_MAX : ((uint64_t)1 << w) - 1; } |
| 247 | |
| 248 | static uint64_t pick(int k, uint64_t r, int line) |
| 249 | { |
| 250 | static const int64_t small[] = {0, 1, 2, 3, 5, 7, 9, 10, 11, 99, 100, 255, 256, 1000}; |
| 251 | uint64_t lo = kmin(k), hi = kmax(k); |
| 252 | if (k == K_BOOL) return r & 1; |
| 253 | if (line < 4) { /* boundaries */ |
| 254 | uint64_t b[4] = {lo, hi, 0, 1}; |
| 255 | if (ksigned(k)) b[3] = (uint64_t)-1; |
| 256 | return b[line]; |
| 257 | } |
| 258 | switch (r % 4) { |
| 259 | case 0: { int64_t v = small[(r >> 8) % 14]; if (ksigned(k) && (r >> 20) & 1) v = -v; return at_width((uint64_t)v, k); } |
| 260 | case 1: { int64_t v = (int64_t)((r >> 8) % 512); if (ksigned(k) && (r >> 20) & 1) v = -v; return at_width((uint64_t)v, k); } /* dense 0..511: every 8- and 9-bit edge */ |
| 261 | case 2: return at_width(r >> 11, k); |
| 262 | default: { uint64_t v = (r >> 13) % 100000; if (ksigned(k) && (r >> 40) & 1) v = (uint64_t)-(int64_t)v; return at_width(v, k); } |
| 263 | } |
| 264 | (void)hi; |
| 265 | } |
| 266 | |
| 267 | static int inputs(int argc, char **argv) |
| 268 | { |
| 269 | /* --max <arg>:<max> bounds argument <arg> to 0..<max> (value mod max+1), for tasks that loop that many times */ |
| 270 | int marg = -1; |
| 271 | uint64_t mmax = 0; |
| 272 | if (argc >= 3 && !strcmp(argv[1], "--max")) { |
| 273 | char *e; |
| 274 | long a = strtol(argv[2], &e, 10); |
| 275 | if (*e != ':' || a < 0 || a > 5) die("--max wants <arg 0..5>:<max>", argv[2]); |
| 276 | unsigned long long m = strtoull(e + 1, &e, 10); |
| 277 | if (*e || m < 1 || m > 1000000) die("--max wants <arg 0..5>:<max 1..1000000>", argv[2]); |
| 278 | marg = (int)a, mmax = m; |
| 279 | argv[2] = argv[0], argc -= 2, argv += 2; |
| 280 | } |
| 281 | int count = atoi(argv[0]), np = argc - 1, ks[6]; |
| 282 | if (count < 1 || count > 4096 || np < 1 || np > 6) die("usage: gabs inputs <count 1..4096> [--max <arg>:<max>] <type 1..6>...", NULL); |
| 283 | if (marg >= np) die("--max names an argument past the last type", NULL); |
| 284 | for (int i = 0; i < np; i++) if ((ks[i] = kind_of(argv[1 + i])) < 0) die("unknown type", argv[1 + i]); |
| 285 | if (marg >= 0 && (ks[marg] == K_BOOL || ksigned(ks[marg]))) die("--max applies to an unsigned argument", NULL); |
| 286 | uint64_t x = 0x9e3779b97f4a7c15ull; |
| 287 | for (int l = 0; l < count; l++) { |
| 288 | for (int i = 0; i < np; i++) { |
| 289 | x = x * 6364136223846793005ull + 1442695040888963407ull; |
| 290 | uint64_t v = pick(ks[i], x ^ (x >> 29), l < 4 ? (l + i) % 4 : l); |
| 291 | if (i == marg) v %= mmax + 1; |
| 292 | if (i) putchar(' '); |
| 293 | put_val(stdout, v, ks[i]); |
| 294 | } |
| 295 | putchar('\n'); |
| 296 | } |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | /* ---- run ------------------------------------------------------------------------------------------------ */ |
| 301 | static int memfd(const char *name, const void *p, size_t n) |
| 302 | { |
| 303 | int fd = (int)syscall(SYS_memfd_create, name, 0); |
| 304 | if (fd < 0) die("memfd_create failed", NULL); |
| 305 | if (n && write(fd, p, n) != (ssize_t)n) die("memfd write failed", NULL); |
| 306 | if (lseek(fd, 0, SEEK_SET) != 0) die("memfd seek failed", NULL); |
| 307 | return fd; |
| 308 | } |
| 309 | |
| 310 | static char *read_fd(int fd, size_t *n) |
| 311 | { |
| 312 | struct stat st; |
| 313 | if (fstat(fd, &st)) die("fstat failed", NULL); |
| 314 | size_t len = (size_t)st.st_size; |
| 315 | if (len > (1u << 20)) len = 1u << 20; |
| 316 | char *b = malloc(len + 1); |
| 317 | if (!b || lseek(fd, 0, SEEK_SET) != 0) die("read memfd failed", NULL); |
| 318 | size_t got = 0; |
| 319 | while (got < len) { ssize_t r = read(fd, b + got, len - got); if (r <= 0) break; got += (size_t)r; } |
| 320 | b[got] = 0; |
| 321 | *n = got; |
| 322 | return b; |
| 323 | } |
| 324 | |
| 325 | static int run(int argc, char **argv) |
| 326 | { |
| 327 | if (argc < 7) die("usage: gabs run <ola_runner> <scratch> <ir> <entry> <ret> <inputs> <expected|-> [<type>...]", NULL); |
| 328 | const char *runner = argv[0], *scratch = argv[1], *ir = argv[2], *entry = argv[3], *rets = argv[4], *inp = argv[5], *exp = argv[6]; |
| 329 | int ret = kind_of(rets), np = argc - 7, ks[6]; |
| 330 | if (ret < 0) die("unknown return type", rets); |
| 331 | if (np > 6) die("more than 6 parameters", NULL); |
| 332 | for (int i = 0; i < np; i++) if ((ks[i] = kind_of(argv[7 + i])) < 0) die("unknown type", argv[7 + i]); |
| 333 | size_t in_n, ir_n; |
| 334 | char *in = slurp(inp, &in_n); |
| 335 | char *irb = slurp(ir, &ir_n); |
| 336 | if (!in) die("cannot read", inp); |
| 337 | if (!irb) { printf("REJECT no_ir\n"); return 1; } |
| 338 | /* inputs: u32 LE count, then n * np u64 LE (ola_runner load_inputs) */ |
| 339 | size_t cap = 4 + 4096u * 6u * 8u, bl = 4; |
| 340 | uint8_t *bin = calloc(1, cap); |
| 341 | uint32_t nlines = 0; |
| 342 | for (char *p = in; *p;) { |
| 343 | char *e = strchr(p, '\n'); |
| 344 | if (!e) die("inputs do not end with a newline:", inp); |
| 345 | *e = 0; |
| 346 | int k = 0; |
| 347 | for (char *t = strtok(p, " "); t; t = strtok(NULL, " ")) { |
| 348 | uint64_t v; |
| 349 | if (k == np || parse_val(t, ks[k], &v)) die("bad input value in", inp); |
| 350 | for (int b = 0; b < 8; b++) bin[bl++] = (uint8_t)(v >> (8 * b)); |
| 351 | k++; |
| 352 | } |
| 353 | if (k != np) die("input arity differs from the parameter list in", inp); |
| 354 | if (++nlines > 4096) die("more than 4096 input lines in", inp); |
| 355 | p = e + 1; |
| 356 | } |
| 357 | if (!nlines) die("no input lines in", inp); |
| 358 | for (int b = 0; b < 4; b++) bin[b] = (uint8_t)(nlines >> (8 * b)); |
| 359 | char *av[16]; |
| 360 | int ac = 0; |
| 361 | av[ac++] = (char *)runner; av[ac++] = "oscir"; av[ac++] = "gabs"; av[ac++] = (char *)scratch; |
| 362 | av[ac++] = (char *)entry; av[ac++] = (char *)rets; |
| 363 | for (int i = 0; i < np; i++) av[ac++] = argv[7 + i]; |
| 364 | av[ac] = NULL; |
| 365 | int fin = memfd("gabs-in", bin, bl), fout = memfd("gabs-out", NULL, 0), ferr = memfd("gabs-err", NULL, 0), fcand = memfd("gabs-cand", irb, ir_n); |
| 366 | pid_t pid = fork(); |
| 367 | if (pid < 0) die("fork failed", NULL); |
| 368 | if (pid == 0) { |
| 369 | if (dup2(fin, 0) < 0 || dup2(fout, 1) < 0 || dup2(ferr, 2) < 0 || dup2(fcand, 3) < 0) _exit(127); |
| 370 | syscall(SYS_close_range, 4, ~0u, 0); |
| 371 | struct rlimit rl; |
| 372 | rl.rlim_cur = 10; rl.rlim_max = 11; setrlimit(RLIMIT_CPU, &rl); |
| 373 | rl.rlim_cur = rl.rlim_max = (rlim_t)1024 << 20; setrlimit(RLIMIT_AS, &rl); |
| 374 | rl.rlim_cur = rl.rlim_max = 0; setrlimit(RLIMIT_CORE, &rl); |
| 375 | char *envp[] = {NULL}; |
| 376 | execve(runner, av, envp); |
| 377 | _exit(127); |
| 378 | } |
| 379 | int st = 0; |
| 380 | while (waitpid(pid, &st, 0) < 0) if (errno != EINTR) die("waitpid failed", NULL); |
| 381 | size_t on, en; |
| 382 | char *out = read_fd(fout, &on), *err = read_fd(ferr, &en); |
| 383 | if (WIFEXITED(st) && WEXITSTATUS(st) == 127) die("cannot exec", runner); |
| 384 | if (WIFEXITED(st) && WEXITSTATUS(st) == 96) die("the sandbox could not be built:", err); |
| 385 | if (!WIFEXITED(st) || WEXITSTATUS(st) != 0) { |
| 386 | if (WIFSIGNALED(st)) printf("REJECT crash signal=%d\n", WTERMSIG(st)); |
| 387 | else printf("REJECT runner_exit code=%d\n", WEXITSTATUS(st)); |
| 388 | return 1; |
| 389 | } |
| 390 | /* transcript: OLA1 gabs / REFUSE r d | R v / T c lines / END gabs n */ |
| 391 | char *lines[4100]; |
| 392 | int nl = 0; |
| 393 | for (char *p = out; *p && nl < 4100;) { char *e = strchr(p, '\n'); if (!e) break; *e = 0; lines[nl++] = p; p = e + 1; } |
| 394 | if (nl < 2 || strcmp(lines[0], "OLA1 gabs")) { printf("REJECT bad_transcript\n"); return 1; } |
| 395 | if (!strncmp(lines[1], "REFUSE ", 7)) { printf("REJECT %s\n", lines[1] + 7); return 1; } |
| 396 | char endl[64]; |
| 397 | snprintf(endl, sizeof endl, "END gabs %u", nlines); |
| 398 | if ((uint32_t)nl != nlines + 2 || strcmp(lines[nl - 1], endl)) { printf("REJECT bad_transcript\n"); return 1; } |
| 399 | if (!strcmp(exp, "-")) { |
| 400 | for (uint32_t i = 0; i < nlines; i++) { |
| 401 | const char *l = lines[1 + i]; |
| 402 | if (l[0] == 'T') printf("TRAP %s\n", l + 2); |
| 403 | else { uint64_t v = strtoull(l + 2, NULL, 10); put_val(stdout, at_width(v, ret), ret); putchar('\n'); } |
| 404 | } |
| 405 | return 0; |
| 406 | } |
| 407 | size_t xn; |
| 408 | char *xs = slurp(exp, &xn); |
| 409 | if (!xs) die("cannot read", exp); |
| 410 | uint32_t i = 0; |
| 411 | for (char *p = xs; *p; i++) { |
| 412 | char *e = strchr(p, '\n'); |
| 413 | if (!e) die("expected does not end with a newline:", exp); |
| 414 | *e = 0; |
| 415 | if (i >= nlines) { printf("REJECT expected_line_count\n"); return 1; } |
| 416 | const char *l = lines[1 + i]; |
| 417 | int got_trap = l[0] == 'T'; |
| 418 | long got_code = got_trap ? strtol(l + 2, NULL, 10) : 0; |
| 419 | uint64_t got = got_trap ? 0 : at_width(strtoull(l + 2, NULL, 10), ret); |
| 420 | char gs[32]; |
| 421 | if (got_trap) snprintf(gs, sizeof gs, "TRAP_%ld", got_code); |
| 422 | else if (ksigned(ret)) snprintf(gs, sizeof gs, "%" PRId64, (int64_t)got); |
| 423 | else snprintf(gs, sizeof gs, "%" PRIu64, got); |
| 424 | if (!strncmp(p, "TRAP", 4) && (p[4] == 0 || p[4] == ' ')) { |
| 425 | if (!got_trap) { printf("REJECT missing_trap line=%u got=%s want=%s\n", i + 1, gs, p); return 1; } |
| 426 | if (p[4] == ' ' && atol(p + 5) != got_code) { printf("REJECT wrong_trap line=%u got=%s want=%s\n", i + 1, gs, p); return 1; } |
| 427 | } else { |
| 428 | uint64_t want; |
| 429 | if (parse_val(p, ret, &want)) die("bad expected value in", exp); |
| 430 | if (got_trap) { printf("REJECT unexpected_trap line=%u got=%s want=%s\n", i + 1, gs, p); return 1; } |
| 431 | if (got != want) { printf("REJECT wrong_result line=%u got=%s want=%s\n", i + 1, gs, p); return 1; } |
| 432 | } |
| 433 | p = e + 1; |
| 434 | } |
| 435 | if (i != nlines) { printf("REJECT expected_line_count\n"); return 1; } |
| 436 | printf("ACCEPT n=%u\n", nlines); |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | int main(int argc, char **argv) |
| 441 | { |
| 442 | if (argc >= 4 && !strcmp(argv[1], "split")) return split(argv[2], argv[3]); |
| 443 | if (argc == 4 && !strcmp(argv[1], "canon")) return canon(argv[2], argv[3]); |
| 444 | if (argc >= 4 && !strcmp(argv[1], "inputs")) return inputs(argc - 2, argv + 2); |
| 445 | if (argc >= 2 && !strcmp(argv[1], "run")) return run(argc - 2, argv + 2); |
| 446 | fprintf(stderr, "usage: gabs split <prog> <prefix> | canon <in.json> <out.json> | inputs <count> [--max <arg>:<max>] <type>... | run ...\n"); |
| 447 | return 2; |
| 448 | } |