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 / tests / one_layer / acceptance / ola.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * ola.c -- trusted half of the one-layer stage-1 hostile acceptance harness (omega#443). |
| 19 | * Contract: tests/one_layer/acceptance/HARNESS_INTERFACE.md (v1, sha256 in RULES). |
| 20 | * |
| 21 | * ola untar <corpus.tar> <SEALED.sha256> <destdir> cases|labels |
| 22 | * check the tarball hash against the sealed file, validate every member (regular files and |
| 23 | * directories only, no absolute paths, no "..", only the contract layout), then extract |
| 24 | * either everything except corpus/LABELS ("cases") or only corpus/LABELS ("labels"). |
| 25 | * Prints "corpus_sha256=<hex>". |
| 26 | * ola pass <corpus_dir> <verdicts.txt> <receipts_dir> <scratch_root> <ola_runner> <meta> |
| 27 | * one verdict line + one receipt per case (sorted by case id); never reads LABELS. |
| 28 | * ola probes <ola_runner> <scratch_root> <probes.log> <time_ms> <mem_mb> name=path... |
| 29 | * every isolation probe sandboxed (must be blocked) and as a negative control without the |
| 30 | * defence (must escape); "PROBES PASS" only if every probe is blocked AND its control escaped. |
| 31 | * Where the scorer can observe the outcome itself (target files unchanged, outside file absent, |
| 32 | * listener saw no connection, scratch file written) that observation overrides the runner. |
| 33 | * ola tally <verdicts.txt> <LABELS> <replay_identical 0|1> <probes.log> <clean 0|1> |
| 34 | * prints the score.txt lines; probes count only if probes.log holds every probe, each PASS. |
| 35 | * ola cmp <a> <b> byte-identical files (exit 0) or not (exit 1) |
| 36 | * |
| 37 | * v2 (HARNESS_INTERFACE_V2.md, omega#445; v1 commands above keep their exact meaning): |
| 38 | * ola untar <tar> <sealed> <destdir> cases2|labels2 corpus.tar, also admits candidate.oscir |
| 39 | * ola untar <tar> <sealed> <destdir> tasks|public tasks.tar: everything, or only requirement + prompt |
| 40 | * ola pass2 <corpus_dir> ... as pass, plus kind oscir (decoded in the sandbox) |
| 41 | * ola xpass <tasks_dir> <set_dir|REFERENCES> <verdicts> <receipts> <scratch> <runner> <meta> |
| 42 | * experiment: every task scored with the set's candidate (or REJECT no_candidate), or with its own |
| 43 | * hidden/reference.osc; a set dir holding anything but known task dirs with <= 1 candidate is refused. |
| 44 | * ola tally2 <verdicts> <LABELS> <replay> <probes.log> <clean> <corpus_dir> v2 corpus gate |
| 45 | * ola xscore <sets_dir> <replay> <probes.log> <clean> experiment score.txt |
| 46 | * |
| 47 | * Hosted C (glibc, Linux syscalls) is a temporary dependency: see evidence/ONE-LAYER-1/INVENTORY.txt. |
| 48 | */ |
| 49 | #include <dirent.h> |
| 50 | #include <errno.h> |
| 51 | #include <fcntl.h> |
| 52 | #include <inttypes.h> |
| 53 | #include <limits.h> |
| 54 | #include <netinet/in.h> |
| 55 | #include <arpa/inet.h> |
| 56 | #include <poll.h> |
| 57 | #include <signal.h> |
| 58 | #include <stdarg.h> |
| 59 | #include <stdio.h> |
| 60 | #include <stdlib.h> |
| 61 | #include <string.h> |
| 62 | #include <sys/mman.h> |
| 63 | #include <sys/random.h> |
| 64 | #include <sys/resource.h> |
| 65 | #include <sys/socket.h> |
| 66 | #include <sys/stat.h> |
| 67 | #include <sys/syscall.h> |
| 68 | #include <sys/wait.h> |
| 69 | #include <unistd.h> |
| 70 | |
| 71 | #include "sha256.h" |
| 72 | |
| 73 | #define MAX_TAR (64u << 20) |
| 74 | #define MAX_FILE (2u << 20) |
| 75 | #define MAX_CAND (1u << 20) |
| 76 | #define MAX_LINES 4096 |
| 77 | #define OLA_EXIT_UNAVAIL 96 |
| 78 | #define OLA_EXIT_SANDBOX 97 |
| 79 | #define OLA_EXIT_MEMORY 98 |
| 80 | |
| 81 | static void die(const char *fmt, ...) |
| 82 | { |
| 83 | va_list ap; |
| 84 | va_start(ap, fmt); |
| 85 | fputs("ola: ", stderr); |
| 86 | vfprintf(stderr, fmt, ap); |
| 87 | fputc('\n', stderr); |
| 88 | va_end(ap); |
| 89 | exit(2); |
| 90 | } |
| 91 | |
| 92 | static void hex32(const uint8_t d[32], char out[65]) |
| 93 | { |
| 94 | static const char h[] = "0123456789abcdef"; |
| 95 | for (int i = 0; i < 32; i++) { out[2 * i] = h[d[i] >> 4]; out[2 * i + 1] = h[d[i] & 15]; } |
| 96 | out[64] = 0; |
| 97 | } |
| 98 | |
| 99 | static void sha_hex(const void *p, size_t n, char out[65]) |
| 100 | { |
| 101 | uint8_t d[32]; |
| 102 | sha256_hash((const uint8_t *)p, n, d); |
| 103 | hex32(d, out); |
| 104 | } |
| 105 | |
| 106 | /* Read a whole regular file (no symlink following), at most max bytes. NULL on any failure. */ |
| 107 | static uint8_t *slurp_at(int dirfd, const char *path, size_t max, size_t *len) |
| 108 | { |
| 109 | int fd = openat(dirfd, path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); |
| 110 | if (fd < 0) return NULL; |
| 111 | struct stat st; |
| 112 | if (fstat(fd, &st) || !S_ISREG(st.st_mode) || (uint64_t)st.st_size > max) { close(fd); errno = EFBIG; return NULL; } |
| 113 | size_t n = (size_t)st.st_size, got = 0; |
| 114 | uint8_t *b = malloc(n + 1); |
| 115 | if (!b) { close(fd); return NULL; } |
| 116 | while (got < n) { |
| 117 | ssize_t r = read(fd, b + got, n - got); |
| 118 | if (r <= 0) { if (r < 0 && errno == EINTR) continue; free(b); close(fd); return NULL; } |
| 119 | got += (size_t)r; |
| 120 | } |
| 121 | close(fd); |
| 122 | b[n] = 0; |
| 123 | *len = n; |
| 124 | return b; |
| 125 | } |
| 126 | static uint8_t *slurp(const char *path, size_t max, size_t *len) { return slurp_at(AT_FDCWD, path, max, len); } |
| 127 | |
| 128 | /* ======================================================================================== untar */ |
| 129 | |
| 130 | static uint64_t octal(const char *p, size_t n, int *bad) |
| 131 | { |
| 132 | uint64_t v = 0; |
| 133 | size_t i = 0; |
| 134 | while (i < n && p[i] == ' ') i++; |
| 135 | int digits = 0; |
| 136 | for (; i < n && p[i] >= '0' && p[i] <= '7'; i++, digits++) v = v * 8 + (uint64_t)(p[i] - '0'); |
| 137 | for (; i < n; i++) if (p[i] != ' ' && p[i] != 0) *bad = 1; |
| 138 | if (!digits) *bad = 1; |
| 139 | return v; |
| 140 | } |
| 141 | |
| 142 | /* The contract layout (HARNESS_INTERFACE.md section 1). 1 directory, 2 file, 0 refused. */ |
| 143 | static int case_id_ok(const char *s, size_t n) |
| 144 | { |
| 145 | if (n < 1 || n > 40) return 0; |
| 146 | for (size_t i = 0; i < n; i++) |
| 147 | if (!((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9') || s[i] == '_' || s[i] == '-')) return 0; |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | /* Which contract layout untar checks: 0 = v1 corpus, 1 = v2 corpus (adds candidate.oscir), |
| 152 | * 2 = v2 experiment task corpus (HARNESS_INTERFACE_V2.md section 3). */ |
| 153 | static int g_lay; |
| 154 | |
| 155 | static int layout_tasks(const char *p) |
| 156 | { |
| 157 | if (!strcmp(p, "tasks") || !strcmp(p, "tasks/cases")) return 1; |
| 158 | if (strncmp(p, "tasks/cases/", 12)) return 0; |
| 159 | const char *id = p + 12, *sl = strchr(id, '/'); |
| 160 | if (!sl) return case_id_ok(id, strlen(id)) ? 1 : 0; |
| 161 | if (!case_id_ok(id, (size_t)(sl - id))) return 0; |
| 162 | const char *rest = sl + 1; |
| 163 | if (!strcmp(rest, "hidden")) return 1; |
| 164 | if (!strcmp(rest, "requirement") || !strcmp(rest, "prompt") || !strcmp(rest, "hidden/inputs") || |
| 165 | !strcmp(rest, "hidden/expected") || !strcmp(rest, "hidden/reference.osc")) |
| 166 | return 2; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int layout(const char *p) |
| 171 | { |
| 172 | if (g_lay == 2) return layout_tasks(p); |
| 173 | if (g_lay == 1 && !strncmp(p, "corpus/cases/", 13)) { /* OLA:lay-v2 */ |
| 174 | const char *sl = strchr(p + 13, '/'); |
| 175 | if (sl && !strcmp(sl + 1, "candidate.oscir")) return case_id_ok(p + 13, (size_t)(sl - (p + 13))) ? 2 : 0; |
| 176 | } |
| 177 | if (!strcmp(p, "corpus") || !strcmp(p, "corpus/cases")) return 1; |
| 178 | if (!strcmp(p, "corpus/LABELS")) return 2; |
| 179 | if (strncmp(p, "corpus/cases/", 13)) return 0; |
| 180 | const char *id = p + 13, *sl = strchr(id, '/'); |
| 181 | if (!sl) return case_id_ok(id, strlen(id)) ? 1 : 0; |
| 182 | if (!case_id_ok(id, (size_t)(sl - id))) return 0; |
| 183 | const char *rest = sl + 1; |
| 184 | if (!strcmp(rest, "hidden")) return 1; |
| 185 | if (!strcmp(rest, "requirement") || !strcmp(rest, "candidate.osc") || !strcmp(rest, "candidate.opir") || |
| 186 | !strcmp(rest, "hidden/inputs") || !strcmp(rest, "hidden/expected")) |
| 187 | return 2; |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* Only mtime/atime/ctime records are accepted in a pax extended header (GNU tar --format=posix). */ |
| 192 | static int pax_ok(const uint8_t *p, size_t n) |
| 193 | { |
| 194 | size_t i = 0; |
| 195 | while (i < n) { |
| 196 | size_t len = 0, j = i; |
| 197 | while (j < n && p[j] >= '0' && p[j] <= '9') len = len * 10 + (size_t)(p[j++] - '0'); |
| 198 | if (j >= n || p[j] != ' ' || len == 0 || i + len > n || p[i + len - 1] != '\n') return 0; |
| 199 | const char *k = (const char *)p + j + 1; |
| 200 | size_t kl = i + len - (j + 1); |
| 201 | if (!((kl > 6 && !memcmp(k, "mtime=", 6)) || (kl > 6 && !memcmp(k, "atime=", 6)) || (kl > 6 && !memcmp(k, "ctime=", 6)))) |
| 202 | return 0; |
| 203 | i += len; |
| 204 | } |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | static int mkdirs_at(int root, const char *path) |
| 209 | { |
| 210 | char buf[512]; |
| 211 | snprintf(buf, sizeof buf, "%s", path); |
| 212 | for (char *s = buf + 1; ; s++) { |
| 213 | if (*s == '/' || *s == 0) { |
| 214 | char c = *s; |
| 215 | *s = 0; |
| 216 | if (mkdirat(root, buf, 0700) && errno != EEXIST) return -1; |
| 217 | struct stat st; |
| 218 | if (fstatat(root, buf, &st, AT_SYMLINK_NOFOLLOW) || !S_ISDIR(st.st_mode)) return -1; |
| 219 | *s = c; |
| 220 | if (!c) break; |
| 221 | } |
| 222 | } |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int cmd_untar(int argc, char **argv) |
| 227 | { |
| 228 | if (argc != 6) die("usage: ola untar <tar> <sealed> <destdir> cases|labels|cases2|labels2|tasks|public"); |
| 229 | /* v1: cases, labels (corpus.tar). v2: cases2, labels2 (corpus.tar, + candidate.oscir); tasks = the whole |
| 230 | * task corpus, public = only requirement + prompt of every task (tasks.tar, HARNESS_INTERFACE_V2.md s.3) */ |
| 231 | const char *md = argv[5]; |
| 232 | int want_labels = !strcmp(md, "labels") || !strcmp(md, "labels2"); |
| 233 | int task_mode = !strcmp(md, "tasks") || !strcmp(md, "public"), public_mode = !strcmp(md, "public"); |
| 234 | g_lay = task_mode ? 2 : (!strcmp(md, "cases2") || !strcmp(md, "labels2")) ? 1 : 0; |
| 235 | const char *tname = task_mode ? "tasks.tar" : "corpus.tar"; |
| 236 | if (!want_labels && !task_mode && strcmp(md, "cases") && strcmp(md, "cases2")) die("untar: unknown mode %s", md); |
| 237 | size_t tn = 0, sn = 0; |
| 238 | uint8_t *tar = slurp(argv[2], MAX_TAR, &tn); |
| 239 | if (!tar) die("untar: cannot read %s (%s)", argv[2], strerror(errno)); |
| 240 | uint8_t *sealed = slurp(argv[3], 4096, &sn); |
| 241 | if (!sealed) die("untar: SEALED file %s is missing: the corpus is not sealed yet; refusing to score", argv[3]); |
| 242 | char h[65]; |
| 243 | sha_hex(tar, tn, h); |
| 244 | char tail[32]; |
| 245 | snprintf(tail, sizeof tail, " %s\n", tname); |
| 246 | if (sn != 64 + strlen(tail) || memcmp(sealed, h, 64) || memcmp(sealed + 64, tail, strlen(tail))) /* OLA:seal-check */ |
| 247 | die("untar: corpus sha256 %s does not match the sealed hash file; refusing to score", h); |
| 248 | int root = open(argv[4], O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 249 | if (root < 0) die("untar: destdir %s", argv[4]); |
| 250 | /* pass 0 validates everything, pass 1 extracts: a refused corpus leaves nothing behind */ |
| 251 | for (int pass = 0; pass < 2; pass++) { |
| 252 | size_t off = 0, members = 0; |
| 253 | int ended = 0, pax_pending = 0; |
| 254 | char seen[1024][96]; |
| 255 | size_t nseen = 0; |
| 256 | while (off + 512 <= tn) { |
| 257 | const uint8_t *hd = tar + off; |
| 258 | int zero = 1; |
| 259 | for (int i = 0; i < 512; i++) if (hd[i]) { zero = 0; break; } |
| 260 | if (zero) { ended = 1; break; } |
| 261 | int bad = 0; |
| 262 | uint64_t chk = octal((const char *)hd + 148, 8, &bad), sum = 0; |
| 263 | for (int i = 0; i < 512; i++) sum += (i >= 148 && i < 156) ? ' ' : hd[i]; |
| 264 | if (bad || chk != sum) die("untar: refused: bad header checksum at offset %zu", off); |
| 265 | if (memcmp(hd + 257, "ustar", 5)) die("untar: refused: not a POSIX ustar member at offset %zu", off); |
| 266 | uint64_t size = octal((const char *)hd + 124, 12, &bad); |
| 267 | if (bad || size > MAX_FILE) die("untar: refused: bad or oversized member size at offset %zu", off); |
| 268 | char type = (char)hd[156]; |
| 269 | const uint8_t *data = hd + 512; |
| 270 | size_t span = 512 + (size_t)((size + 511) / 512) * 512; |
| 271 | if (off + span > tn) die("untar: refused: truncated member at offset %zu", off); |
| 272 | if (type == 'x') { |
| 273 | if (!pax_ok(data, (size_t)size)) die("untar: refused: pax header with a key other than mtime/atime/ctime"); |
| 274 | pax_pending = 1; |
| 275 | off += span; |
| 276 | continue; |
| 277 | } |
| 278 | pax_pending = 0; |
| 279 | char name[160 + 2 + 101]; |
| 280 | char pre[156], nm[101]; |
| 281 | memcpy(pre, hd + 345, 155); pre[155] = 0; |
| 282 | memcpy(nm, hd, 100); nm[100] = 0; |
| 283 | if (pre[0]) snprintf(name, sizeof name, "%s/%s", pre, nm); |
| 284 | else snprintf(name, sizeof name, "%s", nm); |
| 285 | size_t L = strlen(name); |
| 286 | if (type == '5' && L && name[L - 1] == '/') name[--L] = 0; |
| 287 | if (!L || name[0] == '/' || strstr(name, "//") || !strcmp(name, "..") || !strncmp(name, "../", 3) || |
| 288 | strstr(name, "/../") || (L >= 3 && !strcmp(name + L - 3, "/..")) || !strncmp(name, "./", 2) || |
| 289 | strstr(name, "/./")) |
| 290 | die("untar: refused: unsafe member name '%s'", name); |
| 291 | int kind = (type == '0' || type == 0) ? 2 : type == '5' ? 1 : 0; |
| 292 | if (!kind) die("untar: refused: member '%s' has type '%c' (only regular files and directories)", name, type ? type : '0'); |
| 293 | if (kind == 1 && size) die("untar: refused: directory '%s' with data", name); |
| 294 | int lay = layout(name); |
| 295 | if (lay != kind) die("untar: refused: member '%s' is not in the contract layout", name); /* OLA:layout */ |
| 296 | for (size_t i = 0; i < nseen; i++) if (!strcmp(seen[i], name)) die("untar: refused: duplicate member '%s'", name); |
| 297 | if (nseen >= 1024 || L >= sizeof seen[0]) die("untar: refused: too many members or long name"); |
| 298 | snprintf(seen[nseen++], sizeof seen[0], "%s", name); |
| 299 | members++; |
| 300 | if (pass == 1) { |
| 301 | int is_labels = !strcmp(name, "corpus/LABELS"); |
| 302 | int is_hidden = !strncmp(name, "tasks/cases/", 12) && strstr(name + 12, "/hidden") != NULL; |
| 303 | const char *base = strrchr(name, '/') ? strrchr(name, '/') + 1 : name; |
| 304 | int take_dir = kind == 1 && !want_labels && !(public_mode && is_hidden); |
| 305 | int take_file = kind == 2 && (task_mode ? (!public_mode || (!is_hidden && (!strcmp(base, "requirement") || !strcmp(base, "prompt")))) /* OLA:public */ |
| 306 | : is_labels == want_labels); |
| 307 | if (take_dir) { |
| 308 | if (mkdirs_at(root, name)) die("untar: mkdir %s", name); |
| 309 | } else if (take_file) { |
| 310 | char *sl = strrchr(name, '/'); |
| 311 | *sl = 0; |
| 312 | if (mkdirs_at(root, name)) die("untar: mkdir %s", name); |
| 313 | *sl = '/'; |
| 314 | int fd = openat(root, name, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600); |
| 315 | if (fd < 0) die("untar: create %s: %s", name, strerror(errno)); |
| 316 | if (size && write(fd, data, (size_t)size) != (ssize_t)size) die("untar: write %s", name); |
| 317 | if (close(fd)) die("untar: close %s", name); |
| 318 | } |
| 319 | } |
| 320 | off += span; |
| 321 | } |
| 322 | if (!ended || pax_pending) die("untar: refused: no end-of-archive marker"); |
| 323 | if (!members) die("untar: refused: empty corpus"); |
| 324 | } |
| 325 | printf("corpus_sha256=%s\n", h); |
| 326 | free(tar); |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* ===================================================================== sandboxed execution */ |
| 331 | |
| 332 | typedef struct { |
| 333 | int timed_out; |
| 334 | int exited, code, signaled, sig; |
| 335 | char *out; size_t outn; |
| 336 | char *err; size_t errn; |
| 337 | } Run; |
| 338 | |
| 339 | typedef struct { |
| 340 | unsigned time_ms, mem_mb; |
| 341 | int limits; /* 0 = negative control: no rlimits (wall limit 3x) */ |
| 342 | } Limits; |
| 343 | |
| 344 | static int memfd_with(const char *name, const void *p, size_t n) |
| 345 | { |
| 346 | int fd = (int)syscall(SYS_memfd_create, name, 0); |
| 347 | if (fd < 0) die("memfd_create: %s", strerror(errno)); |
| 348 | if (n && write(fd, p, n) != (ssize_t)n) die("memfd write"); |
| 349 | if (lseek(fd, 0, SEEK_SET) != 0) die("memfd seek"); |
| 350 | return fd; |
| 351 | } |
| 352 | |
| 353 | static char *read_fd(int fd, size_t max, size_t *n) |
| 354 | { |
| 355 | struct stat st; |
| 356 | if (fstat(fd, &st)) die("fstat memfd"); |
| 357 | size_t len = (size_t)st.st_size; |
| 358 | if (len > max) len = max; |
| 359 | char *b = malloc(len + 1); |
| 360 | if (!b) die("out of memory"); |
| 361 | if (lseek(fd, 0, SEEK_SET) != 0) die("seek memfd"); |
| 362 | size_t got = 0; |
| 363 | while (got < len) { |
| 364 | ssize_t r = read(fd, b + got, len - got); |
| 365 | if (r <= 0) break; |
| 366 | got += (size_t)r; |
| 367 | } |
| 368 | b[got] = 0; |
| 369 | *n = got; |
| 370 | return b; |
| 371 | } |
| 372 | |
| 373 | static void run_free(Run *r) { free(r->out); free(r->err); } |
| 374 | |
| 375 | static void spawn(const char *runner, char *const argv[], int cand_fd, const void *in, size_t inn, const Limits *lim, Run *r) |
| 376 | { |
| 377 | memset(r, 0, sizeof *r); |
| 378 | int fin = memfd_with("ola-in", in, inn), fout = memfd_with("ola-out", NULL, 0), ferr = memfd_with("ola-err", NULL, 0); |
| 379 | pid_t pid = fork(); |
| 380 | if (pid < 0) die("fork: %s", strerror(errno)); |
| 381 | if (pid == 0) { |
| 382 | if (dup2(fin, 0) < 0 || dup2(fout, 1) < 0 || dup2(ferr, 2) < 0) _exit(127); |
| 383 | if (cand_fd >= 0) { if (dup2(cand_fd, 3) < 0) _exit(127); } |
| 384 | else { int nul = open("/dev/null", O_RDONLY); if (nul < 0 || dup2(nul, 3) < 0) _exit(127); } |
| 385 | syscall(SYS_close_range, 4, ~0u, 0); |
| 386 | setpgid(0, 0); |
| 387 | struct rlimit rl; |
| 388 | unsigned cpu = (lim->time_ms + 999) / 1000 + 1; |
| 389 | if (lim->limits) { |
| 390 | rl.rlim_cur = cpu; rl.rlim_max = cpu + 1; setrlimit(RLIMIT_CPU, &rl); /* OLA:rlimit-cpu */ |
| 391 | rl.rlim_cur = rl.rlim_max = (rlim_t)lim->mem_mb << 20; setrlimit(RLIMIT_AS, &rl); /* OLA:rlimit-as */ |
| 392 | rl.rlim_cur = rl.rlim_max = 8u << 20; setrlimit(RLIMIT_FSIZE, &rl); |
| 393 | rl.rlim_cur = rl.rlim_max = 0; setrlimit(RLIMIT_CORE, &rl); |
| 394 | rl.rlim_cur = rl.rlim_max = 32; setrlimit(RLIMIT_NOFILE, &rl); |
| 395 | } |
| 396 | char *envp[] = {NULL}; |
| 397 | execve(runner, argv, envp); |
| 398 | _exit(127); |
| 399 | } |
| 400 | int pfd = (int)syscall(SYS_pidfd_open, pid, 0); |
| 401 | if (pfd < 0) die("pidfd_open: %s", strerror(errno)); |
| 402 | struct pollfd p = {.fd = pfd, .events = POLLIN}; |
| 403 | int wall = (int)(lim->limits ? lim->time_ms : lim->time_ms * 3 + 5000); /* OLA:wall-limit */ |
| 404 | int pr; |
| 405 | do pr = poll(&p, 1, wall); while (pr < 0 && errno == EINTR); |
| 406 | if (pr == 0) { |
| 407 | r->timed_out = 1; |
| 408 | kill(-pid, SIGKILL); |
| 409 | kill(pid, SIGKILL); |
| 410 | } |
| 411 | int st = 0; |
| 412 | while (waitpid(pid, &st, 0) < 0) if (errno != EINTR) die("waitpid: %s", strerror(errno)); |
| 413 | kill(-pid, SIGKILL); /* nothing in its process group outlives it */ |
| 414 | close(pfd); |
| 415 | if (WIFEXITED(st)) { r->exited = 1; r->code = WEXITSTATUS(st); } |
| 416 | if (WIFSIGNALED(st)) { r->signaled = 1; r->sig = WTERMSIG(st); } |
| 417 | r->out = read_fd(fout, 1u << 20, &r->outn); |
| 418 | r->err = read_fd(ferr, 4096, &r->errn); |
| 419 | close(fin); close(fout); close(ferr); |
| 420 | if (r->exited && r->code == OLA_EXIT_UNAVAIL) |
| 421 | die("the sandbox could not be built (%s); refusing to score", r->err); /* fail closed */ |
| 422 | if (r->exited && r->code == 127) die("cannot exec %s", runner); |
| 423 | } |
| 424 | |
| 425 | static void new_nonce(char out[33]) |
| 426 | { |
| 427 | uint8_t b[16]; |
| 428 | if (getrandom(b, sizeof b, 0) != sizeof b) die("getrandom"); |
| 429 | static const char h[] = "0123456789abcdef"; |
| 430 | for (int i = 0; i < 16; i++) { out[2 * i] = h[b[i] >> 4]; out[2 * i + 1] = h[b[i] & 15]; } |
| 431 | out[32] = 0; |
| 432 | } |
| 433 | |
| 434 | static int rm_tree_at(int dirfd, const char *name) |
| 435 | { |
| 436 | int fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 437 | if (fd < 0) return unlinkat(dirfd, name, 0); |
| 438 | DIR *d = fdopendir(fd); |
| 439 | if (!d) { close(fd); return -1; } |
| 440 | struct dirent *e; |
| 441 | while ((e = readdir(d))) { |
| 442 | if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue; |
| 443 | if (e->d_type == DT_DIR) rm_tree_at(fd, e->d_name); |
| 444 | else unlinkat(fd, e->d_name, 0); |
| 445 | } |
| 446 | closedir(d); |
| 447 | return unlinkat(dirfd, name, AT_REMOVEDIR); |
| 448 | } |
| 449 | |
| 450 | /* Fresh empty scratch directory <root>/<name>. */ |
| 451 | static void fresh_scratch(const char *root, const char *name, char *path, size_t n) |
| 452 | { |
| 453 | snprintf(path, n, "%s/%s", root, name); |
| 454 | int rfd = open(root, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 455 | if (rfd < 0) die("scratch root %s", root); |
| 456 | rm_tree_at(rfd, name); |
| 457 | if (mkdirat(rfd, name, 0700)) die("mkdir scratch %s", path); |
| 458 | close(rfd); |
| 459 | } |
| 460 | |
| 461 | /* ================================================================================ case model */ |
| 462 | |
| 463 | typedef enum { K_BOOL, K_U8, K_U16, K_U32, K_U64, K_I8, K_I16, K_I32, K_I64, K_NONE } Kind; |
| 464 | static const char *const kname[] = {"bool", "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64"}; |
| 465 | |
| 466 | static Kind parse_kind(const char *s) |
| 467 | { |
| 468 | for (int i = 0; i < 9; i++) if (!strcmp(s, kname[i])) return (Kind)i; |
| 469 | return K_NONE; |
| 470 | } |
| 471 | /* compare width: bool is one byte under AAPCS64 */ |
| 472 | static unsigned kwidth(Kind k) { static const unsigned w[] = {8, 8, 16, 32, 64, 8, 16, 32, 64}; return w[k]; } |
| 473 | static int ksigned(Kind k) { return k >= K_I8 && k <= K_I64; } |
| 474 | |
| 475 | /* Strict decimal: [-]digits, no '+', no leading zeros except "0", range of kind k. */ |
| 476 | static int parse_val(const char *s, Kind k, uint64_t *out) |
| 477 | { |
| 478 | int neg = 0; |
| 479 | if (*s == '-') { neg = 1; s++; } |
| 480 | if (!*s || (s[0] == '0' && s[1])) return -1; |
| 481 | uint64_t v = 0; |
| 482 | for (; *s; s++) { |
| 483 | if (*s < '0' || *s > '9') return -1; |
| 484 | if (v > (UINT64_MAX - (uint64_t)(*s - '0')) / 10) return -1; |
| 485 | v = v * 10 + (uint64_t)(*s - '0'); |
| 486 | } |
| 487 | unsigned w = kwidth(k); |
| 488 | if (k == K_BOOL) { if (neg || v > 1) return -1; *out = v; return 0; } |
| 489 | if (!ksigned(k)) { |
| 490 | if (neg && v) return -1; |
| 491 | if (w < 64 && v >> w) return -1; |
| 492 | *out = v; |
| 493 | return 0; |
| 494 | } |
| 495 | uint64_t lim = (uint64_t)1 << (w - 1); /* |min| */ |
| 496 | if (neg ? v > lim : v >= lim) return -1; |
| 497 | *out = neg ? (uint64_t)(-(int64_t)(v - 1) - 1) : v; /* canonical: sign-extended to 64 bits */ |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | /* Normalize a raw 64-bit return at the declared width (contract section 2). */ |
| 502 | static uint64_t at_width(uint64_t r, Kind k) |
| 503 | { |
| 504 | unsigned w = kwidth(k); |
| 505 | if (w == 64) return r; |
| 506 | uint64_t m = ((uint64_t)1 << w) - 1; |
| 507 | r &= m; |
| 508 | if (ksigned(k) && (r >> (w - 1))) r |= ~m; /* OLA:sign-extend */ |
| 509 | return r; |
| 510 | } |
| 511 | |
| 512 | typedef struct { |
| 513 | char id[48]; |
| 514 | int osc; /* 1 osc (and oscir, task: the osc signature keys), 0 omegaprog */ |
| 515 | int ir; /* v2: kind oscir (candidate.oscir) */ |
| 516 | int task; /* v2: kind task (experiment task corpus; the candidate decides osc/oscir) */ |
| 517 | char entry[64]; |
| 518 | Kind params[6]; unsigned np; |
| 519 | Kind ret; |
| 520 | unsigned width; |
| 521 | char pid[65]; /* "-" if absent */ |
| 522 | uint32_t n; /* lines */ |
| 523 | uint64_t *in; /* n * np canonical */ |
| 524 | char sha_cand[65], sha_req[65], sha_in[65], sha_exp[65]; |
| 525 | const char *cand_name; |
| 526 | size_t cand_len; |
| 527 | } Case; |
| 528 | |
| 529 | /* Next logical line (comments and blank lines skipped, trailing spaces trimmed). */ |
| 530 | static char *next_line(char **cur) |
| 531 | { |
| 532 | while (**cur) { |
| 533 | char *s = *cur, *e = strchr(s, '\n'); |
| 534 | if (e) { *e = 0; *cur = e + 1; } else *cur = s + strlen(s); |
| 535 | char *h = strchr(s, '#'); |
| 536 | if (h) *h = 0; |
| 537 | size_t L = strlen(s); |
| 538 | while (L && (s[L - 1] == ' ' || s[L - 1] == '\t' || s[L - 1] == '\r')) s[--L] = 0; |
| 539 | while (*s == ' ' || *s == '\t') s++; |
| 540 | if (*s) return s; |
| 541 | } |
| 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | static int ascii_ok(const uint8_t *b, size_t n) |
| 546 | { |
| 547 | for (size_t i = 0; i < n; i++) if (b[i] == 0 || b[i] > 126) return 0; |
| 548 | return 1; |
| 549 | } |
| 550 | |
| 551 | static int name_ok(const char *s) |
| 552 | { |
| 553 | size_t n = strlen(s); |
| 554 | if (!n || n > 63 || !((s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z') || s[0] == '_')) return 0; |
| 555 | for (size_t i = 1; i < n; i++) |
| 556 | if (!((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '0' && s[i] <= '9') || s[i] == '_')) return 0; |
| 557 | return 1; |
| 558 | } |
| 559 | |
| 560 | /* Parse requirement text; "" on success, else the malformation. */ |
| 561 | /* Kinds a pass admits (bitmask): v1 osc + omegaprog; v2 corpus adds oscir; the experiment admits task only. */ |
| 562 | enum { ADMIT_V1 = 1, ADMIT_OSCIR = 2, ADMIT_TASK = 4 }; |
| 563 | static unsigned g_admit = ADMIT_V1; |
| 564 | |
| 565 | static const char *parse_requirement(char *txt, Case *c) |
| 566 | { |
| 567 | int have_kind = 0, have_entry = 0, have_params = 0, have_ret = 0, have_width = 0, have_pid = 0; |
| 568 | char *cur = txt, *ln; |
| 569 | snprintf(c->pid, sizeof c->pid, "-"); |
| 570 | while ((ln = next_line(&cur))) { |
| 571 | char *tok[10] = {0}; int nt = 0; |
| 572 | for (char *t = strtok(ln, " \t"); t; t = strtok(NULL, " \t")) { if (nt == 10) return "requirement_too_many_tokens"; tok[nt++] = t; } |
| 573 | if (!strcmp(tok[0], "kind")) { |
| 574 | if (have_kind++ || nt != 2) return "requirement_kind"; |
| 575 | if (!strcmp(tok[1], "osc") && (g_admit & ADMIT_V1)) c->osc = 1; |
| 576 | else if (!strcmp(tok[1], "oscir") && (g_admit & ADMIT_OSCIR)) { c->osc = 1; c->ir = 1; } |
| 577 | else if (!strcmp(tok[1], "task") && (g_admit & ADMIT_TASK)) { c->osc = 1; c->task = 1; } |
| 578 | else if (!strcmp(tok[1], "omegaprog") && (g_admit & ADMIT_V1)) c->osc = 0; |
| 579 | else return "requirement_kind"; |
| 580 | } else if (!strcmp(tok[0], "entry")) { |
| 581 | if (have_entry++ || nt != 2 || !name_ok(tok[1])) return "requirement_entry"; |
| 582 | snprintf(c->entry, sizeof c->entry, "%s", tok[1]); |
| 583 | } else if (!strcmp(tok[0], "params")) { |
| 584 | if (have_params++ || nt > 7) return "requirement_params"; |
| 585 | c->np = (unsigned)(nt - 1); |
| 586 | for (int i = 1; i < nt; i++) if ((c->params[i - 1] = parse_kind(tok[i])) == K_NONE) return "requirement_params"; |
| 587 | } else if (!strcmp(tok[0], "ret")) { |
| 588 | if (have_ret++ || nt != 2 || (c->ret = parse_kind(tok[1])) == K_NONE) return "requirement_ret"; |
| 589 | } else if (!strcmp(tok[0], "width")) { |
| 590 | if (have_width++ || nt != 2) return "requirement_width"; |
| 591 | if (!strcmp(tok[1], "8")) c->width = 8; else if (!strcmp(tok[1], "16")) c->width = 16; |
| 592 | else if (!strcmp(tok[1], "32")) c->width = 32; else if (!strcmp(tok[1], "64")) c->width = 64; |
| 593 | else return "requirement_width"; |
| 594 | } else if (!strcmp(tok[0], "program_id")) { |
| 595 | if (have_pid++ || nt != 2 || strlen(tok[1]) != 64) return "requirement_program_id"; |
| 596 | for (int i = 0; i < 64; i++) if (!((tok[1][i] >= '0' && tok[1][i] <= '9') || (tok[1][i] >= 'a' && tok[1][i] <= 'f'))) return "requirement_program_id"; |
| 597 | snprintf(c->pid, sizeof c->pid, "%s", tok[1]); |
| 598 | } else return "requirement_unknown_key"; |
| 599 | } |
| 600 | if (!have_kind) return "requirement_kind"; |
| 601 | if (c->osc) { |
| 602 | if (!have_entry || !have_params || !have_ret || have_width || have_pid) return "requirement_keys_for_osc"; |
| 603 | } else { |
| 604 | if (!have_width || have_entry || have_params || have_ret) return "requirement_keys_for_omegaprog"; |
| 605 | c->np = 1; |
| 606 | c->ret = c->width == 8 ? K_U8 : c->width == 16 ? K_U16 : c->width == 32 ? K_U32 : K_U64; |
| 607 | c->params[0] = c->ret; |
| 608 | } |
| 609 | return ""; |
| 610 | } |
| 611 | |
| 612 | static const char *parse_inputs(char *txt, Case *c) |
| 613 | { |
| 614 | char *cur = txt, *ln; |
| 615 | c->in = calloc((size_t)MAX_LINES * 6 + 1, sizeof *c->in); |
| 616 | if (!c->in) die("out of memory"); |
| 617 | c->n = 0; |
| 618 | while ((ln = next_line(&cur))) { |
| 619 | if (c->n == MAX_LINES) return "inputs_too_many_lines"; |
| 620 | unsigned k = 0; |
| 621 | for (char *t = strtok(ln, " \t"); t; t = strtok(NULL, " \t")) { |
| 622 | if (k == c->np) return "inputs_arity"; |
| 623 | if (parse_val(t, c->params[k], &c->in[(size_t)c->n * c->np + k])) return "inputs_value"; |
| 624 | k++; |
| 625 | } |
| 626 | if (k != c->np) return "inputs_arity"; |
| 627 | c->n++; |
| 628 | } |
| 629 | if (c->n < 1) return "inputs_empty"; |
| 630 | return ""; |
| 631 | } |
| 632 | |
| 633 | typedef struct { int trap; int code; uint64_t v; } Want; /* trap: 0 value, 1 any trap, 2 that code */ |
| 634 | |
| 635 | static const char *parse_expected(char *txt, const Case *c, Want *w) |
| 636 | { |
| 637 | char *cur = txt, *ln; |
| 638 | uint32_t n = 0; |
| 639 | while ((ln = next_line(&cur))) { |
| 640 | if (n == c->n) return "expected_line_count"; |
| 641 | Want *x = &w[n]; |
| 642 | memset(x, 0, sizeof *x); |
| 643 | if (!strncmp(ln, "TRAP", 4) && (ln[4] == 0 || ln[4] == ' ' || ln[4] == '\t')) { |
| 644 | char *a = ln + 4; |
| 645 | while (*a == ' ' || *a == '\t') a++; |
| 646 | if (!*a) x->trap = 1; |
| 647 | else { |
| 648 | uint64_t code; |
| 649 | if (parse_val(a, K_U8, &code) || code == 0) return "expected_trap_code"; |
| 650 | x->trap = 2; x->code = (int)code; |
| 651 | } |
| 652 | } else if (parse_val(ln, c->ret, &x->v)) return "expected_value"; |
| 653 | n++; |
| 654 | } |
| 655 | if (n != c->n) return "expected_line_count"; |
| 656 | return ""; |
| 657 | } |
| 658 | |
| 659 | /* ========================================================================= verdict + receipt */ |
| 660 | |
| 661 | typedef struct { |
| 662 | char commit[64], harness[65], rules[65], corpus[65]; |
| 663 | unsigned time_ms, mem_mb; |
| 664 | } Meta; |
| 665 | |
| 666 | static void read_meta(const char *path, Meta *m) |
| 667 | { |
| 668 | size_t n; |
| 669 | char *t = (char *)slurp(path, 4096, &n); |
| 670 | if (!t) die("meta %s", path); |
| 671 | memset(m, 0, sizeof *m); |
| 672 | char *cur = t, *ln; |
| 673 | while ((ln = next_line(&cur))) { |
| 674 | char *eq = strchr(ln, '='); |
| 675 | if (!eq) die("meta line without '='"); |
| 676 | *eq++ = 0; |
| 677 | if (!strcmp(ln, "commit")) snprintf(m->commit, sizeof m->commit, "%s", eq); |
| 678 | else if (!strcmp(ln, "harness_sha256")) snprintf(m->harness, sizeof m->harness, "%s", eq); |
| 679 | else if (!strcmp(ln, "rules_sha256")) snprintf(m->rules, sizeof m->rules, "%s", eq); |
| 680 | else if (!strcmp(ln, "corpus_sha256")) snprintf(m->corpus, sizeof m->corpus, "%s", eq); |
| 681 | else if (!strcmp(ln, "time_ms")) m->time_ms = (unsigned)atoi(eq); |
| 682 | else if (!strcmp(ln, "mem_mb")) m->mem_mb = (unsigned)atoi(eq); |
| 683 | else die("meta: unknown key %s", ln); |
| 684 | } |
| 685 | if (!m->commit[0] || strlen(m->harness) != 64 || strlen(m->rules) != 64 || strlen(m->corpus) != 64 || !m->time_ms || !m->mem_mb) |
| 686 | die("meta: incomplete"); |
| 687 | free(t); |
| 688 | } |
| 689 | |
| 690 | /* Write receipt bytes under receipts/<sha256>; never rewrite. Returns the hash in h. */ |
| 691 | static void put_receipt(int rdir, const char *body, size_t n, char h[65]) |
| 692 | { |
| 693 | sha_hex(body, n, h); |
| 694 | char tmp[128]; |
| 695 | snprintf(tmp, sizeof tmp, ".tmp.%s.%d", h, (int)getpid()); |
| 696 | unlinkat(rdir, tmp, 0); |
| 697 | int fd = openat(rdir, tmp, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0444); |
| 698 | if (fd < 0 || write(fd, body, n) != (ssize_t)n || fsync(fd) || close(fd)) die("receipt write"); |
| 699 | if (linkat(rdir, tmp, rdir, h, 0)) { |
| 700 | if (errno != EEXIST) die("receipt link: %s", strerror(errno)); |
| 701 | size_t on; |
| 702 | int ofd = openat(rdir, h, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); |
| 703 | uint8_t *old = ofd >= 0 ? slurp_at(rdir, h, 1u << 16, &on) : NULL; |
| 704 | if (ofd >= 0) close(ofd); |
| 705 | if (!old || on != n || memcmp(old, body, n)) die("receipt %s exists with different bytes: evidence tampered", h); /* OLA:receipt-immutable */ |
| 706 | free(old); |
| 707 | } |
| 708 | unlinkat(rdir, tmp, 0); |
| 709 | } |
| 710 | |
| 711 | /* The transcript must open with "OLA1 <nonce of THIS execution>": a replayed or forged one fails. */ |
| 712 | static int header_ok(const char *out, const char *nonce) |
| 713 | { |
| 714 | char hdr[64]; |
| 715 | snprintf(hdr, sizeof hdr, "OLA1 %s\n", nonce); |
| 716 | return !strncmp(out, hdr, strlen(hdr)); /* OLA:nonce-check */ |
| 717 | } |
| 718 | |
| 719 | static const char *judge(const Case *c, const Want *w, const Run *r, const char *nonce, char *det, size_t dn) |
| 720 | { |
| 721 | snprintf(det, dn, "-"); |
| 722 | if (r->timed_out || (r->signaled && (r->sig == SIGXCPU || r->sig == SIGKILL))) return "timeout"; |
| 723 | if ((r->exited && r->code == OLA_EXIT_SANDBOX) || (r->signaled && r->sig == SIGSYS)) { |
| 724 | const char *s = strstr(r->err, "syscall="); |
| 725 | int nr = s ? atoi(s + 8) : -1; |
| 726 | snprintf(det, dn, "syscall=%d", nr); |
| 727 | return "sandbox_violation"; |
| 728 | } |
| 729 | if (r->exited && r->code == OLA_EXIT_MEMORY) { snprintf(det, dn, "rlimit_as"); return "memory"; } |
| 730 | if (r->signaled) { snprintf(det, dn, "signal=%d", r->sig); return "crash"; } |
| 731 | if (!r->exited || r->code) { snprintf(det, dn, "exit=%d", r->code); return "crash"; } |
| 732 | /* strict transcript */ |
| 733 | char *t = strdup(r->out), *save = NULL; |
| 734 | if (!t) die("out of memory"); |
| 735 | const char *reason = "ok"; |
| 736 | char *ln = strtok_r(t, "\n", &save); |
| 737 | if (!ln || !header_ok(r->out, nonce)) { |
| 738 | snprintf(det, dn, "stale_or_forged_transcript"); |
| 739 | free(t); |
| 740 | return "sandbox_violation"; |
| 741 | } |
| 742 | uint32_t i = 0; |
| 743 | int ended = 0; |
| 744 | while ((ln = strtok_r(NULL, "\n", &save))) { |
| 745 | if (ended) { reason = "crash"; snprintf(det, dn, "trailing_transcript"); break; } |
| 746 | if (!strncmp(ln, "REFUSE ", 7) && i == 0) { |
| 747 | char rs[40] = "", dd[100] = ""; |
| 748 | if (sscanf(ln + 7, "%39s %99s", rs, dd) != 2) { reason = "crash"; snprintf(det, dn, "malformed_transcript"); break; } |
| 749 | static const char *const ok[] = {"compile_refused", "decode_refused", "realize_refused", "signature_mismatch", "oversize"}; |
| 750 | reason = NULL; |
| 751 | for (int k = 0; k < 5; k++) if (!strcmp(rs, ok[k])) reason = ok[k]; |
| 752 | if (!reason) { reason = "crash"; snprintf(det, dn, "malformed_transcript"); break; } |
| 753 | snprintf(det, dn, "%s", dd); |
| 754 | char end[64]; |
| 755 | snprintf(end, sizeof end, "END %s 0", nonce); |
| 756 | ln = strtok_r(NULL, "\n", &save); |
| 757 | if (!ln || strcmp(ln, end) || strtok_r(NULL, "\n", &save)) { reason = "crash"; snprintf(det, dn, "malformed_transcript"); } |
| 758 | free(t); |
| 759 | return reason; |
| 760 | } |
| 761 | if (!strncmp(ln, "END ", 4)) { |
| 762 | char end[64]; |
| 763 | snprintf(end, sizeof end, "END %s %" PRIu32, nonce, c->n); |
| 764 | if (strcmp(ln, end) || i != c->n) { reason = "crash"; snprintf(det, dn, "malformed_transcript"); break; } |
| 765 | ended = 1; |
| 766 | continue; |
| 767 | } |
| 768 | if (i >= c->n) { reason = "crash"; snprintf(det, dn, "malformed_transcript"); break; } |
| 769 | const Want *x = &w[i]; |
| 770 | if (ln[0] == 'T' && ln[1] == ' ') { |
| 771 | int code = atoi(ln + 2); |
| 772 | if (!x->trap) { reason = "unexpected_trap"; snprintf(det, dn, "line=%u,trap=%d", i + 1, code); break; } |
| 773 | if (x->trap == 2 && code != x->code) { reason = "wrong_trap"; snprintf(det, dn, "line=%u,want=%d,got=%d", i + 1, x->code, code); break; } /* OLA:trap-code */ |
| 774 | } else if (ln[0] == 'R' && ln[1] == ' ') { |
| 775 | char *e; |
| 776 | errno = 0; |
| 777 | uint64_t v = strtoull(ln + 2, &e, 10); |
| 778 | if (*e || errno) { reason = "crash"; snprintf(det, dn, "malformed_transcript"); break; } |
| 779 | if (x->trap) { reason = "missing_trap"; snprintf(det, dn, "line=%u", i + 1); break; } |
| 780 | if (at_width(v, c->ret) != x->v) { /* OLA:value-compare */ |
| 781 | reason = "wrong_result"; |
| 782 | snprintf(det, dn, "line=%u", i + 1); |
| 783 | break; |
| 784 | } |
| 785 | } else { reason = "crash"; snprintf(det, dn, "malformed_transcript"); break; } |
| 786 | i++; |
| 787 | } |
| 788 | if (!strcmp(reason, "ok") && !ended) { reason = "crash"; snprintf(det, dn, "truncated_transcript"); } |
| 789 | free(t); |
| 790 | return reason; |
| 791 | } |
| 792 | |
| 793 | static int cmpstr(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); } |
| 794 | |
| 795 | static void hash_file_at(int dfd, const char *p, char out[65], size_t *len) |
| 796 | { |
| 797 | size_t n = 0; |
| 798 | uint8_t *b = slurp_at(dfd, p, MAX_FILE, &n); |
| 799 | if (!b) { snprintf(out, 65, "-"); if (len) *len = 0; return; } |
| 800 | sha_hex(b, n, out); |
| 801 | if (len) *len = n; |
| 802 | free(b); |
| 803 | } |
| 804 | |
| 805 | /* Which pass: v1 `pass` (osc, omegaprog), v2 `pass2` (+ oscir), v2 `xpass` (experiment task corpus). */ |
| 806 | enum { PM_V1, PM_V2, PM_X }; |
| 807 | |
| 808 | /* xpass: refuse the whole run unless every entry of the candidate set is a directory named after a |
| 809 | * task of the corpus holding at most one regular candidate.osc or candidate.oscir (V2 section 3.3). */ |
| 810 | static void check_set_dir(const char *set, char **ids, size_t nid) |
| 811 | { |
| 812 | DIR *d = opendir(set); |
| 813 | if (!d) die("xpass: candidate set %s is not a directory", set); |
| 814 | struct dirent *e; |
| 815 | while ((e = readdir(d))) { |
| 816 | if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue; |
| 817 | size_t i; |
| 818 | for (i = 0; i < nid; i++) if (!strcmp(ids[i], e->d_name)) break; |
| 819 | if (i == nid) die("xpass: refused: '%s' in the candidate set is not a task of the corpus", e->d_name); /* OLA:set-ids */ |
| 820 | char p[4096]; |
| 821 | struct stat st; |
| 822 | snprintf(p, sizeof p, "%s/%s", set, e->d_name); |
| 823 | if (lstat(p, &st) || !S_ISDIR(st.st_mode)) die("xpass: refused: '%s' in the candidate set is not a directory", e->d_name); |
| 824 | DIR *t = opendir(p); |
| 825 | if (!t) die("xpass: cannot open %s", p); |
| 826 | int n = 0; |
| 827 | struct dirent *f; |
| 828 | while ((f = readdir(t))) { |
| 829 | if (!strcmp(f->d_name, ".") || !strcmp(f->d_name, "..")) continue; |
| 830 | char q[8192]; |
| 831 | snprintf(q, sizeof q, "%s/%s", p, f->d_name); |
| 832 | if ((strcmp(f->d_name, "candidate.osc") && strcmp(f->d_name, "candidate.oscir")) || lstat(q, &st) || !S_ISREG(st.st_mode)) |
| 833 | die("xpass: refused: '%s/%s' is not a candidate file", e->d_name, f->d_name); /* OLA:set-files */ |
| 834 | if (++n > 1) die("xpass: refused: task %s has more than one candidate file", e->d_name); /* OLA:set-one */ |
| 835 | } |
| 836 | closedir(t); |
| 837 | } |
| 838 | closedir(d); |
| 839 | } |
| 840 | |
| 841 | /* v2 task prompt: bytes '\n' and 0x20..0x7e only, 1..4096 bytes (V2 section 3). "" if fine. */ |
| 842 | static const char *prompt_bad(int cfd) |
| 843 | { |
| 844 | size_t n = 0; |
| 845 | uint8_t *p = slurp_at(cfd, "prompt", MAX_FILE, &n); |
| 846 | if (!p) return "missing_prompt"; |
| 847 | const char *r = ""; |
| 848 | if (n < 1 || n > 4096) r = "prompt_size"; |
| 849 | else for (size_t i = 0; i < n; i++) if (p[i] != '\n' && (p[i] < 0x20 || p[i] > 0x7e)) { r = "prompt_bytes"; break; } /* OLA:prompt */ |
| 850 | free(p); |
| 851 | return r; |
| 852 | } |
| 853 | |
| 854 | static int cmd_pass(int argc, char **argv) |
| 855 | { |
| 856 | /* ola pass <corpus_dir> <verdicts> <receipts_dir> <scratch_root> <ola_runner> <meta> (v1) |
| 857 | * ola pass2 <corpus_dir> <verdicts> <receipts_dir> <scratch_root> <ola_runner> <meta> (v2 corpus) |
| 858 | * ola xpass <tasks_dir> <set_dir|REFERENCES> <verdicts> <receipts_dir> <scratch_root> <ola_runner> <meta> */ |
| 859 | int pm = !strcmp(argv[1], "xpass") ? PM_X : !strcmp(argv[1], "pass2") ? PM_V2 : PM_V1; |
| 860 | const char *set = NULL; |
| 861 | if (pm == PM_X) { |
| 862 | if (argc != 9) die("usage: ola xpass <tasks_dir> <set_dir|REFERENCES> <verdicts> <receipts_dir> <scratch_root> <ola_runner> <meta>"); |
| 863 | set = argv[3]; |
| 864 | static char *pv[8]; |
| 865 | pv[0] = argv[0]; pv[1] = argv[1]; pv[2] = argv[2]; |
| 866 | for (int i = 3; i < 8; i++) pv[i] = argv[i + 1]; /* drop the set: the rest lines up with pass */ |
| 867 | argv = pv; argc = 8; |
| 868 | } else if (argc != 8) die("usage: ola %s <corpus_dir> <verdicts> <receipts_dir> <scratch_root> <ola_runner> <meta>", argv[1]); |
| 869 | int refs = set && !strcmp(set, "REFERENCES"); |
| 870 | g_admit = pm == PM_X ? ADMIT_TASK : pm == PM_V2 ? (ADMIT_V1 | ADMIT_OSCIR) : ADMIT_V1; |
| 871 | const char *corpus = argv[2], *runner = argv[6]; |
| 872 | Meta m; |
| 873 | read_meta(argv[7], &m); |
| 874 | char cases_path[4096]; |
| 875 | snprintf(cases_path, sizeof cases_path, "%s/cases", corpus); |
| 876 | struct stat lst; |
| 877 | char lpath[4096]; |
| 878 | snprintf(lpath, sizeof lpath, "%s/LABELS", corpus); |
| 879 | if (!lstat(lpath, &lst)) die("LABELS is present before verdicts are written; refusing"); /* OLA:labels-late */ |
| 880 | DIR *d = opendir(cases_path); |
| 881 | if (!d) die("no cases directory"); |
| 882 | char *ids[1024]; |
| 883 | size_t nid = 0; |
| 884 | struct dirent *e; |
| 885 | while ((e = readdir(d))) { |
| 886 | if (e->d_name[0] == '.') continue; |
| 887 | if (nid == 1024) die("too many cases"); |
| 888 | ids[nid++] = strdup(e->d_name); |
| 889 | } |
| 890 | closedir(d); |
| 891 | qsort(ids, nid, sizeof ids[0], cmpstr); |
| 892 | if (set && !refs) check_set_dir(set, ids, nid); /* before anything is written */ |
| 893 | int vfd = open(argv[3], O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0644); |
| 894 | if (vfd < 0) die("verdicts %s exists or cannot be created: never overwritten", argv[3]); |
| 895 | FILE *vf = fdopen(vfd, "w"); |
| 896 | int rdir = open(argv[4], O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 897 | if (rdir < 0) die("receipts dir %s", argv[4]); |
| 898 | int casesfd = open(cases_path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 899 | if (casesfd < 0) die("cases dir"); |
| 900 | int setfd = set && !refs ? open(set, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC) : -1; |
| 901 | if (set && !refs && setfd < 0) die("xpass: candidate set %s", set); |
| 902 | for (size_t ci = 0; ci < nid; ci++) { |
| 903 | Case c; |
| 904 | memset(&c, 0, sizeof c); |
| 905 | snprintf(c.id, sizeof c.id, "%s", ids[ci]); |
| 906 | int cfd = openat(casesfd, c.id, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 907 | if (cfd < 0) die("case dir %s", c.id); |
| 908 | const char *verdict = "ERROR", *reason = "case_malformed"; |
| 909 | char det[160] = "-"; |
| 910 | size_t rn = 0, inn = 0, en = 0; |
| 911 | uint8_t *req = slurp_at(cfd, "requirement", MAX_FILE, &rn); |
| 912 | uint8_t *inp = slurp_at(cfd, "hidden/inputs", MAX_FILE, &inn); |
| 913 | uint8_t *exp = NULL; |
| 914 | hash_file_at(cfd, "requirement", c.sha_req, NULL); |
| 915 | hash_file_at(cfd, "hidden/inputs", c.sha_in, NULL); |
| 916 | hash_file_at(cfd, "hidden/expected", c.sha_exp, NULL); |
| 917 | /* where the candidate lives: the case dir (v1, v2), the set's task dir, or the case's reference */ |
| 918 | int dfd = cfd, own_dfd = 0; |
| 919 | const char *n_osc = "candidate.osc"; |
| 920 | if (refs) n_osc = "hidden/reference.osc"; |
| 921 | else if (set) { |
| 922 | dfd = openat(setfd, c.id, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 923 | own_dfd = dfd >= 0; |
| 924 | } |
| 925 | struct stat so, sp, si; |
| 926 | int has_osc = dfd >= 0 && !fstatat(dfd, n_osc, &so, AT_SYMLINK_NOFOLLOW); |
| 927 | int has_op = pm != PM_X && !fstatat(cfd, "candidate.opir", &sp, AT_SYMLINK_NOFOLLOW); |
| 928 | int has_ir = pm != PM_V1 && !refs && dfd >= 0 && !fstatat(dfd, "candidate.oscir", &si, AT_SYMLINK_NOFOLLOW); |
| 929 | snprintf(c.sha_cand, sizeof c.sha_cand, "-"); |
| 930 | const char *bad = ""; |
| 931 | Want *want = NULL; |
| 932 | if (!req) bad = "missing_requirement"; |
| 933 | else if (!inp) bad = "missing_inputs"; |
| 934 | else if (!ascii_ok(req, rn) || !ascii_ok(inp, inn)) bad = "not_ascii"; |
| 935 | const char *kinds = "-"; |
| 936 | if (!*bad) bad = parse_requirement((char *)req, &c); |
| 937 | if (!*bad && pm == PM_X) { |
| 938 | bad = prompt_bad(cfd); |
| 939 | if (!*bad && refs && !has_osc) bad = "missing_reference"; |
| 940 | if (!*bad && has_osc + has_ir == 1) c.ir = has_ir; /* the candidate decides osc or oscir */ |
| 941 | } |
| 942 | if (!*bad) kinds = c.ir ? "oscir" : c.osc ? "osc" : "omegaprog"; |
| 943 | int none = pm == PM_X && has_osc + has_ir == 0; |
| 944 | if (none) kinds = "-"; |
| 945 | if (!*bad && !none && has_osc + has_op + has_ir != 1) bad = "candidate_count"; |
| 946 | if (!*bad && !none && (c.ir ? !has_ir : c.osc != has_osc)) bad = "candidate_kind"; |
| 947 | if (!*bad && !none) { |
| 948 | c.cand_name = c.ir ? "candidate.oscir" : c.osc ? n_osc : "candidate.opir"; |
| 949 | const struct stat *cs = c.ir ? &si : c.osc ? &so : &sp; |
| 950 | if (!S_ISREG(cs->st_mode)) bad = "candidate_not_regular"; |
| 951 | else { c.cand_len = (size_t)cs->st_size; hash_file_at(c.osc ? dfd : cfd, c.cand_name, c.sha_cand, NULL); } |
| 952 | } |
| 953 | if (!*bad) bad = parse_inputs((char *)inp, &c); |
| 954 | if (!*bad && pm == PM_X && c.n < 8) bad = "inputs_fewer_than_8"; /* OLA:inputs-8 */ |
| 955 | if (*bad) snprintf(det, sizeof det, "%s", bad); |
| 956 | else if (none) { verdict = "REJECT"; reason = "no_candidate"; } /* OLA:no-candidate */ |
| 957 | else if (c.cand_len > MAX_CAND) { verdict = "REJECT"; reason = "oversize"; snprintf(det, sizeof det, "bytes=%zu", c.cand_len); } /* OLA:oversize */ |
| 958 | else { |
| 959 | /* the candidate runs BEFORE hidden/expected is even read */ |
| 960 | size_t inb = 4 + (size_t)c.n * c.np * 8; |
| 961 | uint8_t *ib = malloc(inb); |
| 962 | if (!ib) die("out of memory"); |
| 963 | ib[0] = (uint8_t)c.n; ib[1] = (uint8_t)(c.n >> 8); ib[2] = (uint8_t)(c.n >> 16); ib[3] = (uint8_t)(c.n >> 24); |
| 964 | for (size_t k = 0; k < (size_t)c.n * c.np; k++) |
| 965 | for (int b = 0; b < 8; b++) ib[4 + 8 * k + b] = (uint8_t)(c.in[k] >> (8 * b)); |
| 966 | int cand = openat(c.osc ? dfd : cfd, c.cand_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); |
| 967 | if (cand < 0) die("open candidate"); |
| 968 | char nonce[33], scratch[4096]; |
| 969 | new_nonce(nonce); |
| 970 | fresh_scratch(argv[5], "case", scratch, sizeof scratch); |
| 971 | char *av[16]; |
| 972 | int ac = 0; |
| 973 | char wbuf[8]; |
| 974 | av[ac++] = (char *)"ola_runner"; |
| 975 | av[ac++] = (char *)(c.ir ? "oscir" : c.osc ? "osc" : "omegaprog"); |
| 976 | av[ac++] = nonce; |
| 977 | av[ac++] = scratch; |
| 978 | if (c.osc) { |
| 979 | av[ac++] = c.entry; |
| 980 | av[ac++] = (char *)kname[c.ret]; |
| 981 | for (unsigned k = 0; k < c.np; k++) av[ac++] = (char *)kname[c.params[k]]; |
| 982 | } else { |
| 983 | snprintf(wbuf, sizeof wbuf, "%u", c.width); |
| 984 | av[ac++] = wbuf; |
| 985 | av[ac++] = c.pid; |
| 986 | } |
| 987 | av[ac] = NULL; |
| 988 | Limits lim = {m.time_ms, m.mem_mb, 1}; |
| 989 | Run r; |
| 990 | spawn(runner, av, cand, ib, inb, &lim, &r); |
| 991 | close(cand); |
| 992 | free(ib); |
| 993 | exp = slurp_at(cfd, "hidden/expected", MAX_FILE, &en); |
| 994 | want = calloc(MAX_LINES + 1, sizeof *want); |
| 995 | if (!want) die("out of memory"); |
| 996 | const char *eb = !exp ? "missing_expected" : !ascii_ok(exp, en) ? "not_ascii" : parse_expected((char *)exp, &c, want); |
| 997 | if (*eb) snprintf(det, sizeof det, "%s", eb); |
| 998 | else { |
| 999 | reason = judge(&c, want, &r, nonce, det, sizeof det); |
| 1000 | verdict = !strcmp(reason, "ok") ? "ACCEPT" : "REJECT"; |
| 1001 | } |
| 1002 | run_free(&r); |
| 1003 | } |
| 1004 | for (char *p = det; *p; p++) if ((unsigned char)*p <= ' ' || *p == '=') *p = '_'; |
| 1005 | char body[2048]; |
| 1006 | int bn = snprintf(body, sizeof body, |
| 1007 | "schema=omega.one_layer.verdict.v1\ncommit=%s\nharness_sha256=%s\nrules_sha256=%s\ncorpus_sha256=%s\n" |
| 1008 | "case=%s\nkind=%s\ncandidate_sha256=%s\nrequirement_sha256=%s\ninputs_sha256=%s\nexpected_sha256=%s\n" |
| 1009 | "verdict=%s\nreason=%s\ndetail=%s\n", |
| 1010 | m.commit, m.harness, m.rules, m.corpus, c.id, kinds, |
| 1011 | c.sha_cand, c.sha_req, c.sha_in, c.sha_exp, verdict, reason, det); |
| 1012 | if (bn <= 0 || (size_t)bn >= sizeof body) die("receipt too long"); |
| 1013 | char rh[65]; |
| 1014 | put_receipt(rdir, body, (size_t)bn, rh); |
| 1015 | fprintf(vf, "%s %s %s %s\n", c.id, verdict, reason, rh); |
| 1016 | fprintf(stderr, " %-40s %s %s %s\n", c.id, verdict, reason, det); |
| 1017 | free(req); free(inp); free(exp); free(want); free(c.in); |
| 1018 | if (own_dfd) close(dfd); |
| 1019 | close(cfd); |
| 1020 | free(ids[ci]); |
| 1021 | } |
| 1022 | if (setfd >= 0) close(setfd); |
| 1023 | if (fflush(vf) || fsync(fileno(vf)) || fclose(vf)) die("verdicts write"); |
| 1024 | close(rdir); |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
| 1028 | /* ===================================================================================== probes */ |
| 1029 | |
| 1030 | typedef struct { const char *name; const char *target_key; } Probe; |
| 1031 | |
| 1032 | static const Probe k_probes[] = { |
| 1033 | {"read_hidden_inputs", "hidden_inputs"}, {"read_expected", "expected"}, {"read_labels", "labels"}, |
| 1034 | {"read_corpus_tar", "tar"}, {"read_key", "key"}, {"write_evidence", "evidence"}, {"write_scorer", "scorer"}, |
| 1035 | {"change_rules", "rules"}, {"network", NULL}, {"outlive_time", NULL}, {"outlive_fork", NULL}, |
| 1036 | {"exceed_memory", NULL}, {"write_outside_scratch", "outside"}, {"replay_verdict", NULL}, |
| 1037 | }; |
| 1038 | |
| 1039 | /* Outcome of one probe execution: "ESCAPED:<what>" or "BLOCKED:<how>". */ |
| 1040 | static void classify(const Run *r, const char *nonce_or_null, char *out, size_t n) |
| 1041 | { |
| 1042 | if (r->timed_out || (r->signaled && (r->sig == SIGXCPU || r->sig == SIGKILL))) { snprintf(out, n, "BLOCKED:time_limit"); return; } |
| 1043 | if ((r->exited && r->code == OLA_EXIT_SANDBOX) || (r->signaled && r->sig == SIGSYS)) { |
| 1044 | const char *s = strstr(r->err, "syscall="); |
| 1045 | snprintf(out, n, "BLOCKED:seccomp_syscall=%d", s ? atoi(s + 8) : -1); |
| 1046 | return; |
| 1047 | } |
| 1048 | if (nonce_or_null) { /* replay: the transcript check is the defence */ |
| 1049 | if (!header_ok(r->out, nonce_or_null)) { snprintf(out, n, "BLOCKED:stale_or_forged_transcript"); return; } |
| 1050 | snprintf(out, n, "ESCAPED:transcript_accepted"); |
| 1051 | return; |
| 1052 | } |
| 1053 | if (r->signaled) { snprintf(out, n, "BLOCKED:signal=%d", r->sig); return; } |
| 1054 | if (!strncmp(r->out, "ESCAPED ", 8)) { |
| 1055 | snprintf(out, n, "ESCAPED:%.*s", (int)strcspn(r->out + 8, "\n"), r->out + 8); |
| 1056 | return; |
| 1057 | } |
| 1058 | if (!strncmp(r->out, "BLOCKED ", 8)) { |
| 1059 | snprintf(out, n, "BLOCKED:%.*s", (int)strcspn(r->out + 8, "\n"), r->out + 8); |
| 1060 | for (char *p = out; *p; p++) if (*p == ' ') *p = '_'; |
| 1061 | return; |
| 1062 | } |
| 1063 | snprintf(out, n, "UNKNOWN:exit=%d", r->exited ? r->code : -1); |
| 1064 | } |
| 1065 | |
| 1066 | /* ---- independent checks: what the parent observes itself, never taken from the runner's report ---- */ |
| 1067 | |
| 1068 | #define MAX_TARGETS 16 |
| 1069 | typedef struct { const char *key, *path; char sha[65]; size_t len; } Target; |
| 1070 | |
| 1071 | static void target_hash(const char *path, char out[65], size_t *len) |
| 1072 | { |
| 1073 | uint8_t *b = slurp(path, MAX_TAR, len); |
| 1074 | if (!b) die("probe target %s cannot be read by the scorer: %s", path, strerror(errno)); |
| 1075 | sha_hex(b, *len, out); |
| 1076 | free(b); |
| 1077 | } |
| 1078 | |
| 1079 | /* Name of the first file target whose bytes differ from the snapshot, or NULL. */ |
| 1080 | static const char *targets_changed(const Target *t, size_t nt) |
| 1081 | { |
| 1082 | for (size_t i = 0; i < nt; i++) { |
| 1083 | char h[65]; |
| 1084 | size_t len; |
| 1085 | target_hash(t[i].path, h, &len); |
| 1086 | if (len != t[i].len || strcmp(h, t[i].sha)) return t[i].key; /* OLA:targets-unchanged */ |
| 1087 | } |
| 1088 | return NULL; |
| 1089 | } |
| 1090 | |
| 1091 | static int path_exists(const char *p) { struct stat st; return !lstat(p, &st); } |
| 1092 | |
| 1093 | /* Connections the local listener has seen since the last call (listener is non-blocking). */ |
| 1094 | static int drain_accepts(int ls) |
| 1095 | { |
| 1096 | int n = 0, c; |
| 1097 | while ((c = accept4(ls, NULL, NULL, SOCK_CLOEXEC)) >= 0) { close(c); n++; } |
| 1098 | return n; |
| 1099 | } |
| 1100 | |
| 1101 | static int scratch_has_ok(const char *scratch) |
| 1102 | { |
| 1103 | char p[4200]; |
| 1104 | size_t n; |
| 1105 | snprintf(p, sizeof p, "%s/scratch_ok", scratch); |
| 1106 | uint8_t *b = slurp(p, 64, &n); |
| 1107 | int ok = b && n == 3 && !memcmp(b, "ok\n", 3); |
| 1108 | free(b); |
| 1109 | return ok; |
| 1110 | } |
| 1111 | |
| 1112 | static int cmd_probes(int argc, char **argv) |
| 1113 | { |
| 1114 | if (argc < 7) die("usage: ola probes <ola_runner> <scratch_root> <probes.log> <time_ms> <mem_mb> key=path..."); |
| 1115 | const char *runner = argv[2], *sroot = argv[3]; |
| 1116 | unsigned time_ms = (unsigned)atoi(argv[5]), mem_mb = (unsigned)atoi(argv[6]); |
| 1117 | const char *only = getenv("OLA_PROBE_ONLY"); /* selftest: run a subset (ola.sh score refuses it) */ |
| 1118 | if (only && !*only) only = NULL; |
| 1119 | FILE *log = fopen(argv[4], "wx"); |
| 1120 | if (!log) die("probes log %s exists or cannot be created", argv[4]); |
| 1121 | /* every file a probe aims at (except the one write_outside_scratch tries to create) is hashed now |
| 1122 | * and re-hashed after every probe execution: a runner that changes one fails, whatever it reports */ |
| 1123 | Target tg[MAX_TARGETS]; |
| 1124 | size_t nt = 0; |
| 1125 | for (int i = 7; i < argc; i++) { |
| 1126 | char *eq = strchr(argv[i], '='); |
| 1127 | if (!eq || eq == argv[i]) die("probe target %s is not key=path", argv[i]); |
| 1128 | if (!strncmp(argv[i], "outside=", 8)) continue; |
| 1129 | if (nt == MAX_TARGETS) die("too many probe targets"); |
| 1130 | tg[nt].key = strndup(argv[i], (size_t)(eq - argv[i])); |
| 1131 | tg[nt].path = eq + 1; |
| 1132 | if (!tg[nt].key) die("out of memory"); |
| 1133 | target_hash(tg[nt].path, tg[nt].sha, &tg[nt].len); |
| 1134 | nt++; |
| 1135 | } |
| 1136 | /* a local listener so the network probe's control has something real to reach; the parent counts |
| 1137 | * the connections it accepts instead of believing the runner */ |
| 1138 | int ls = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); |
| 1139 | struct sockaddr_in a; |
| 1140 | memset(&a, 0, sizeof a); |
| 1141 | a.sin_family = AF_INET; |
| 1142 | a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 1143 | socklen_t al = sizeof a; |
| 1144 | if (ls < 0 || bind(ls, (struct sockaddr *)&a, sizeof a) || listen(ls, 8) || getsockname(ls, (struct sockaddr *)&a, &al)) |
| 1145 | die("probe listener: %s", strerror(errno)); |
| 1146 | char port[16], tbuf[16], mbuf[16]; |
| 1147 | snprintf(port, sizeof port, "%u", (unsigned)ntohs(a.sin_port)); |
| 1148 | snprintf(tbuf, sizeof tbuf, "%.3f", time_ms / 1000.0); |
| 1149 | snprintf(mbuf, sizeof mbuf, "%u", mem_mb); |
| 1150 | int all = 1, nrun = 0; |
| 1151 | /* positive control inside the sandbox: scratch must stay writable, else "blocked" means nothing */ |
| 1152 | for (size_t pi = 0; pi < sizeof k_probes / sizeof k_probes[0] + 1; pi++) { |
| 1153 | int is_pos = pi == sizeof k_probes / sizeof k_probes[0]; |
| 1154 | const char *name = is_pos ? "scratch_ok" : k_probes[pi].name; |
| 1155 | if (only && !strstr(only, name)) continue; |
| 1156 | const char *target = ""; |
| 1157 | if (!is_pos && k_probes[pi].target_key) { |
| 1158 | size_t kl = strlen(k_probes[pi].target_key); |
| 1159 | for (int i = 7; i < argc; i++) |
| 1160 | if (!strncmp(argv[i], k_probes[pi].target_key, kl) && argv[i][kl] == '=') target = argv[i] + kl + 1; |
| 1161 | if (!*target) die("probe %s: no target %s=", name, k_probes[pi].target_key); |
| 1162 | } |
| 1163 | int is_outside = !strcmp(name, "write_outside_scratch"), is_net = !strcmp(name, "network"); |
| 1164 | if (is_net) target = port; |
| 1165 | if (!strcmp(name, "outlive_time")) target = tbuf; |
| 1166 | if (!strcmp(name, "exceed_memory")) target = mbuf; |
| 1167 | char res[2][160]; |
| 1168 | for (int control = 0; control < 2; control++) { |
| 1169 | char nonce[33], stale[33], scratch[4096]; |
| 1170 | new_nonce(nonce); |
| 1171 | new_nonce(stale); /* the "recorded earlier" nonce a replayed transcript carries */ |
| 1172 | fresh_scratch(sroot, "probe", scratch, sizeof scratch); |
| 1173 | if (!strcmp(name, "replay_verdict")) target = stale; |
| 1174 | if (is_outside && (unlink(target), path_exists(target))) die("probe %s: %s cannot be cleared", name, target); |
| 1175 | if (is_net) drain_accepts(ls); |
| 1176 | char *av[] = {(char *)"ola_runner", (char *)"probe", nonce, scratch, (char *)(control ? "1" : "0"), |
| 1177 | (char *)name, (char *)target, NULL}; |
| 1178 | Limits lim = {time_ms, mem_mb, !control}; |
| 1179 | Run r; |
| 1180 | spawn(runner, av, -1, "\0\0\0\0", 4, &lim, &r); |
| 1181 | /* replay control: the defence removed = the transcript is checked against the nonce it carries */ |
| 1182 | const char *chk = !strcmp(name, "replay_verdict") ? (control ? stale : nonce) : NULL; |
| 1183 | classify(&r, chk, res[control], sizeof res[control]); |
| 1184 | run_free(&r); |
| 1185 | /* independent checks override the runner's own report (OLA:probe-independent) */ |
| 1186 | const char *changed = targets_changed(tg, nt); |
| 1187 | if (changed) snprintf(res[control], sizeof res[control], "TAMPERED:%s_changed", changed); |
| 1188 | else if (is_outside) { |
| 1189 | int made = path_exists(target); |
| 1190 | if (!control && made) snprintf(res[0], sizeof res[0], "ESCAPED:file_created_seen_by_scorer"); |
| 1191 | if (control && !made) snprintf(res[1], sizeof res[1], "BLOCKED:no_file_seen_by_scorer"); |
| 1192 | unlink(target); /* the control really created it */ |
| 1193 | } else if (is_net) { |
| 1194 | int seen = drain_accepts(ls); |
| 1195 | if (!control && seen) snprintf(res[0], sizeof res[0], "ESCAPED:connection_seen_by_listener"); |
| 1196 | if (control && !seen) snprintf(res[1], sizeof res[1], "BLOCKED:no_connection_seen_by_listener"); |
| 1197 | } else if (is_pos && !scratch_has_ok(scratch)) |
| 1198 | snprintf(res[control], sizeof res[control], "BLOCKED:scratch_file_missing"); |
| 1199 | } |
| 1200 | int pass = is_pos ? !strncmp(res[0], "ESCAPED:", 8) && !strncmp(res[1], "ESCAPED:", 8) |
| 1201 | : !strncmp(res[0], "BLOCKED:", 8) && !strncmp(res[1], "ESCAPED:", 8); /* OLA:probe-verdict */ |
| 1202 | fprintf(log, "probe=%s expect=%s sandboxed=%s control=%s result=%s\n", name, is_pos ? "ALLOWED" : "BLOCKED", |
| 1203 | res[0], res[1], pass ? "PASS" : "FAIL"); |
| 1204 | fprintf(stderr, " probe %-22s sandboxed=%-40s control=%-28s %s\n", name, res[0], res[1], pass ? "PASS" : "FAIL"); |
| 1205 | all &= pass; |
| 1206 | nrun++; |
| 1207 | } |
| 1208 | close(ls); |
| 1209 | fprintf(log, "PROBES %s count=%d\n", all && nrun ? "PASS" : "FAIL", nrun); |
| 1210 | if (fclose(log)) die("probes log write"); |
| 1211 | return all && nrun ? 0 : 1; |
| 1212 | } |
| 1213 | |
| 1214 | /* ====================================================================================== tally */ |
| 1215 | |
| 1216 | /* probes.log is complete: every probe (and scratch_ok) exactly once with result=PASS, no other |
| 1217 | * line, and the closing "PROBES PASS count=<all>". A subset run (OLA_PROBE_ONLY) never passes. */ |
| 1218 | static int probes_complete(const char *path) |
| 1219 | { |
| 1220 | size_t n; |
| 1221 | char *b = (char *)slurp(path, MAX_FILE, &n); |
| 1222 | if (!b) return 0; |
| 1223 | const size_t np = sizeof k_probes / sizeof k_probes[0] + 1; |
| 1224 | int seen[sizeof k_probes / sizeof k_probes[0] + 1] = {0}; |
| 1225 | size_t lines = 0; |
| 1226 | int ok = 1, closed = 0; |
| 1227 | char *cur = b, *line, want_end[48]; |
| 1228 | snprintf(want_end, sizeof want_end, "PROBES PASS count=%zu", np); |
| 1229 | while ((line = next_line(&cur))) { |
| 1230 | if (closed) { ok = 0; break; } |
| 1231 | if (!strncmp(line, "PROBES ", 7)) { closed = 1; ok &= !strcmp(line, want_end); continue; } /* OLA:probe-count */ |
| 1232 | size_t ll = strlen(line); |
| 1233 | if (strncmp(line, "probe=", 6) || ll < 12 || strcmp(line + ll - 12, " result=PASS")) { ok = 0; break; } |
| 1234 | size_t k; |
| 1235 | for (k = 0; k < np; k++) { |
| 1236 | const char *nm = k + 1 == np ? "scratch_ok" : k_probes[k].name; |
| 1237 | size_t nl = strlen(nm); |
| 1238 | if (!strncmp(line + 6, nm, nl) && line[6 + nl] == ' ') break; |
| 1239 | } |
| 1240 | if (k == np || seen[k]++) { ok = 0; break; } |
| 1241 | lines++; |
| 1242 | } |
| 1243 | free(b); |
| 1244 | return ok && closed && lines == np; /* OLA:probe-all */ |
| 1245 | } |
| 1246 | |
| 1247 | static int cmd_tally(int argc, char **argv) |
| 1248 | { |
| 1249 | /* v2 `tally2` adds <corpus_dir>: counts come from LABELS, oscir cases from the requirements */ |
| 1250 | int v2 = !strcmp(argv[1], "tally2"); |
| 1251 | if (argc != (v2 ? 8 : 7)) die(v2 ? "usage: ola tally2 <verdicts> <LABELS> <replay_identical> <probes.log> <clean> <corpus_dir>" |
| 1252 | : "usage: ola tally <verdicts> <LABELS> <replay_identical> <probes.log> <clean>"); |
| 1253 | size_t vn, ln; |
| 1254 | char *v = (char *)slurp(argv[2], MAX_FILE, &vn), *l = (char *)slurp(argv[3], MAX_FILE, &ln); |
| 1255 | if (!v || !l) die("tally: cannot read verdicts or LABELS"); |
| 1256 | char *ids[1024], *lab[1024]; |
| 1257 | size_t nl = 0; |
| 1258 | char *cur = l, *line; |
| 1259 | while ((line = next_line(&cur))) { |
| 1260 | char id[64], cls[16], ac[64]; |
| 1261 | if (sscanf(line, "%63s %15s %63s", id, cls, ac) != 3 || (strcmp(cls, "DEFECT") && strcmp(cls, "CONTROL"))) die("tally: bad LABELS line"); |
| 1262 | if (nl == 1024) die("tally: too many labels"); |
| 1263 | for (size_t i = 0; i < nl; i++) if (!strcmp(ids[i], id)) die("tally: duplicate label %s", id); |
| 1264 | ids[nl] = strdup(id); lab[nl] = strdup(cls); nl++; |
| 1265 | } |
| 1266 | unsigned defects = 0, controls = 0, drej = 0, cacc = 0, errors = 0, seen = 0, unlabelled = 0; |
| 1267 | for (size_t i = 0; i < nl; i++) { if (!strcmp(lab[i], "DEFECT")) defects++; else controls++; } |
| 1268 | cur = v; |
| 1269 | while ((line = next_line(&cur))) { |
| 1270 | char id[64], verdict[16], reason[40], rh[80]; |
| 1271 | if (sscanf(line, "%63s %15s %39s %79s", id, verdict, reason, rh) != 4) die("tally: bad verdict line"); |
| 1272 | if (!strcmp(verdict, "ERROR")) errors++; |
| 1273 | size_t i; |
| 1274 | for (i = 0; i < nl; i++) if (!strcmp(ids[i], id)) break; |
| 1275 | if (i == nl) { unlabelled++; continue; } |
| 1276 | seen++; |
| 1277 | if (!strcmp(lab[i], "DEFECT") && !strcmp(verdict, "REJECT")) drej++; /* ERROR never counts as a rejection */ |
| 1278 | if (!strcmp(lab[i], "CONTROL") && !strcmp(verdict, "ACCEPT")) cacc++; |
| 1279 | } |
| 1280 | int replay = !strcmp(argv[4], "1"), probes = probes_complete(argv[5]), clean = !strcmp(argv[6], "1"); |
| 1281 | if (v2) { |
| 1282 | unsigned ird = 0, irc = 0; |
| 1283 | g_admit = ADMIT_V1 | ADMIT_OSCIR; |
| 1284 | for (size_t i = 0; i < nl; i++) { |
| 1285 | char p[4096]; |
| 1286 | size_t rn; |
| 1287 | snprintf(p, sizeof p, "%s/cases/%s/requirement", argv[7], ids[i]); |
| 1288 | char *rq = case_id_ok(ids[i], strlen(ids[i])) ? (char *)slurp(p, MAX_FILE, &rn) : NULL; |
| 1289 | Case c; |
| 1290 | memset(&c, 0, sizeof c); |
| 1291 | if (rq && ascii_ok((uint8_t *)rq, rn) && !*parse_requirement(rq, &c) && c.ir) { if (!strcmp(lab[i], "DEFECT")) ird++; else irc++; } |
| 1292 | free(rq); |
| 1293 | } |
| 1294 | int gate2 = drej == defects && cacc == controls && errors == 0 && seen == nl && !unlabelled && |
| 1295 | ird >= 10 && irc >= 5 && replay && probes && clean; /* OLA:v2-gate */ |
| 1296 | printf("defects_rejected=%u/%u controls_accepted=%u/%u errors=%u\n", drej, defects, cacc, controls, errors); |
| 1297 | printf("labelled_defects=%u labelled_controls=%u verdicts_matched=%u unlabelled_verdicts=%u\n", defects, controls, seen, unlabelled); |
| 1298 | printf("replay_identical=%s probes=%s clean_tree=%s\n", replay ? "yes" : "no", probes ? "PASS" : "FAIL", clean ? "yes" : "no"); |
| 1299 | printf("oscir_defects=%u oscir_controls=%u\n", ird, irc); |
| 1300 | printf("GATE %s\n", gate2 ? "PASS" : "FAIL"); |
| 1301 | return 0; |
| 1302 | } |
| 1303 | int gate = defects == 30 && controls == 10 && drej == 30 && cacc == 10 && errors == 0 && seen == nl && !unlabelled && |
| 1304 | replay && probes && clean; /* OLA:gate */ |
| 1305 | printf("defects_rejected=%u/30 controls_accepted=%u/10 errors=%u\n", drej, cacc, errors); |
| 1306 | printf("labelled_defects=%u labelled_controls=%u verdicts_matched=%u unlabelled_verdicts=%u\n", defects, controls, seen, unlabelled); |
| 1307 | printf("replay_identical=%s probes=%s clean_tree=%s\n", replay ? "yes" : "no", probes ? "PASS" : "FAIL", clean ? "yes" : "no"); |
| 1308 | printf("GATE %s\n", gate ? "PASS" : "FAIL"); |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | /* ola xscore <sets_dir> <replay_identical 0|1> <probes.log> <clean 0|1>: experiment score.txt |
| 1313 | * (HARNESS_INTERFACE_V2.md section 3.3). sets_dir/<set>/verdicts.txt; set `references` is the |
| 1314 | * authors' reference solutions. No LABELS, no gate: RUN VALID|INVALID only. */ |
| 1315 | static int cmd_xscore(int argc, char **argv) |
| 1316 | { |
| 1317 | if (argc != 6) die("usage: ola xscore <sets_dir> <replay_identical> <probes.log> <clean>"); |
| 1318 | DIR *d = opendir(argv[2]); |
| 1319 | if (!d) die("xscore: no sets dir"); |
| 1320 | char *sets[1024]; |
| 1321 | size_t ns = 0; |
| 1322 | struct dirent *e; |
| 1323 | while ((e = readdir(d))) { |
| 1324 | if (e->d_name[0] == '.') continue; |
| 1325 | if (ns == 1024) die("xscore: too many sets"); |
| 1326 | sets[ns++] = strdup(e->d_name); |
| 1327 | } |
| 1328 | closedir(d); |
| 1329 | qsort(sets, ns, sizeof sets[0], cmpstr); |
| 1330 | unsigned ref_n = 0, ref_acc = 0, errors = 0, have_ref = 0, mismatch = 0; |
| 1331 | char out[1024][160]; |
| 1332 | size_t no = 0; |
| 1333 | unsigned ns_n[1024]; |
| 1334 | for (size_t s = 0; s < ns; s++) { |
| 1335 | char p[4096]; |
| 1336 | size_t vn; |
| 1337 | snprintf(p, sizeof p, "%s/%s/verdicts.txt", argv[2], sets[s]); |
| 1338 | char *v = (char *)slurp(p, MAX_FILE, &vn), *cur, *line; |
| 1339 | if (!v) die("xscore: cannot read %s", p); |
| 1340 | unsigned n = 0, a = 0, r = 0, er = 0, k = 0; |
| 1341 | cur = v; |
| 1342 | while ((line = next_line(&cur))) { |
| 1343 | char id[64], verdict[16], reason[40], rh[80]; |
| 1344 | if (sscanf(line, "%63s %15s %39s %79s", id, verdict, reason, rh) != 4) die("xscore: bad verdict line"); |
| 1345 | n++; |
| 1346 | if (!strcmp(verdict, "ACCEPT")) a++; |
| 1347 | else if (!strcmp(verdict, "REJECT")) { r++; if (!strcmp(reason, "no_candidate")) k++; } |
| 1348 | else er++; |
| 1349 | } |
| 1350 | free(v); |
| 1351 | errors += er; |
| 1352 | if (!strcmp(sets[s], "references")) { have_ref = 1; ref_n = n; ref_acc = a; continue; } |
| 1353 | ns_n[no] = n; |
| 1354 | snprintf(out[no++], sizeof out[0], "set=%s accepted=%u/%u rejected=%u errors=%u no_candidate=%u", sets[s], a, n, r, er, k); |
| 1355 | } |
| 1356 | for (size_t i = 0; i < no; i++) if (ns_n[i] != ref_n) mismatch++; |
| 1357 | int replay = !strcmp(argv[3], "1"), probes = probes_complete(argv[4]), clean = !strcmp(argv[5], "1"); |
| 1358 | int valid = have_ref && ref_n > 0 && ref_acc == ref_n && errors == 0 && !mismatch && replay && probes && clean; /* OLA:run-valid */ |
| 1359 | for (size_t i = 0; i < no; i++) printf("%s\n", out[i]); |
| 1360 | printf("references_accepted=%u/%u\n", ref_acc, ref_n); |
| 1361 | printf("replay_identical=%s probes=%s clean_tree=%s\n", replay ? "yes" : "no", probes ? "PASS" : "FAIL", clean ? "yes" : "no"); |
| 1362 | printf("RUN %s\n", valid ? "VALID" : "INVALID"); |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
| 1366 | static int cmd_cmp(int argc, char **argv) |
| 1367 | { |
| 1368 | if (argc != 4) die("usage: ola cmp <a> <b>"); |
| 1369 | size_t an, bn; |
| 1370 | uint8_t *a = slurp(argv[2], MAX_TAR, &an), *b = slurp(argv[3], MAX_TAR, &bn); |
| 1371 | return a && b && an == bn && !memcmp(a, b, an) ? 0 : 1; |
| 1372 | } |
| 1373 | |
| 1374 | int main(int argc, char **argv) |
| 1375 | { |
| 1376 | signal(SIGPIPE, SIG_IGN); |
| 1377 | if (argc < 2) die("usage: ola untar|pass|pass2|xpass|probes|tally|tally2|xscore|cmp ..."); |
| 1378 | if (!strcmp(argv[1], "untar")) return cmd_untar(argc, argv); |
| 1379 | if (!strcmp(argv[1], "pass") || !strcmp(argv[1], "pass2") || !strcmp(argv[1], "xpass")) return cmd_pass(argc, argv); |
| 1380 | if (!strcmp(argv[1], "probes")) return cmd_probes(argc, argv); |
| 1381 | if (!strcmp(argv[1], "tally") || !strcmp(argv[1], "tally2")) return cmd_tally(argc, argv); |
| 1382 | if (!strcmp(argv[1], "xscore")) return cmd_xscore(argc, argv); |
| 1383 | if (!strcmp(argv[1], "cmp")) return cmd_cmp(argc, argv); |
| 1384 | die("unknown command %s", argv[1]); |
| 1385 | return 2; |
| 1386 | } |