aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / specs / osh-platform / tools / osh_sv_ref.c
| 1 | /* osh_sv_ref.c: reference model for the OSH stream and task vectors (OSHSV1). |
| 2 | * |
| 3 | * Contract: ../STREAM_TASK.md (OSH Platform ABI v1 DRAFT, revision 2, not frozen), written against |
| 4 | * ../OSH_PLATFORM_ABI.md at aien-protocols-mv commit fb5fca98e667. Corpus format: ../vectors/README.md, section |
| 5 | * "Stream and task vectors". This program plays one adapter that has every feature and both capability domains, and |
| 6 | * runs every vectors/s*.vec and vectors/t*.vec file against it. It is a check that the corpus is self-consistent and |
| 7 | * has teeth (tools/mutate-stream-ref.sh), not evidence about Omega or Aienux, and it qualifies nothing physical. |
| 8 | * |
| 9 | * Model choices the spec does not pin (each UNVERIFIED, listed in the handoff and in STREAM_TASK.md ST11): |
| 10 | * - limits: 8 pipes of 65536 bytes, 16 handle slots per principal table, 16 tasks; |
| 11 | * - handle cell form generation << 32 | (slot + 1), opaque to the corpus; |
| 12 | * - a task runs one step per scheduler round, and checks a pending interrupt before each step; |
| 13 | * - the default capability generation is 7 in domain 1 and 0x100000007 in domain 2. |
| 14 | * |
| 15 | * Usage: osh_sv_ref --corpus DIR [--pin corpus=SHA] [--pin toolchain=TEXT] |
| 16 | * Build: cc -std=c99 -O2 -Wall -Wextra -Werror -pedantic osh_sv_ref.c. -DSV_MUTANT=K disables check K (enum mut). |
| 17 | * C99 and libc only. */ |
| 18 | #define _POSIX_C_SOURCE 200809L |
| 19 | #include <dirent.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #ifndef SV_MUTANT |
| 26 | #define SV_MUTANT 0 |
| 27 | #endif |
| 28 | /* Every check the vectors must catch. tools/mutate-stream-ref.sh builds one model per entry with that check off. */ |
| 29 | enum mut { |
| 30 | MUT_NONE = 0, |
| 31 | MUT_WRITE_RIGHT = 1, |
| 32 | MUT_EOF_ANY, |
| 33 | MUT_NO_BROKEN_PIPE, |
| 34 | MUT_ZERO_WRITE_NO_READER, |
| 35 | MUT_UNBOUNDED, |
| 36 | MUT_NO_LATCH, |
| 37 | MUT_NO_GENERATION, |
| 38 | MUT_NO_EPOCH, |
| 39 | MUT_FRAME_VERSION, |
| 40 | MUT_FRAME_RESERVED, |
| 41 | MUT_GEN_TRUNCATE, |
| 42 | MUT_GEN_32BIT_COMPARE, |
| 43 | MUT_DOMAIN_BRIDGE, |
| 44 | MUT_BIND_ONCE, |
| 45 | MUT_PARTIAL_NO_CANCEL, |
| 46 | MUT_CRASH_REPLAY, |
| 47 | MUT_TASK_KEEPS_ENDS, |
| 48 | MUT_DUP_NO_SUBSET, |
| 49 | MUT_LIMIT_LEAK, |
| 50 | MUT_STAGE_FAIL_ABORTS, |
| 51 | MUT_WAIT_NO_RELEASE, |
| 52 | MUT_TERMINATE_IGNORABLE, |
| 53 | MUT_LAST |
| 54 | }; |
| 55 | #define MUT(k) (SV_MUTANT == (k)) |
| 56 | |
| 57 | enum err { |
| 58 | E_OK = 0, E_DENIED, E_REVOKED, E_STALE, E_NOT_FOUND, E_UNAVAILABLE, E_INVALID_ARG, E_LIMIT, E_INTERRUPTED, |
| 59 | E_BROKEN_PIPE, E_IO, E_OUTCOME_UNKNOWN, E_CAP_DOMAIN_MISMATCH, E_CAP_GEN_NARROW, E_NOT_SUPPORTED, E_WOULD_BLOCK, |
| 60 | E_PARTIAL_LAUNCH, E_COUNT |
| 61 | }; |
| 62 | static const char *const k_err[E_COUNT] = {"OK", "DENIED", "REVOKED", "STALE", "NOT_FOUND", "UNAVAILABLE", |
| 63 | "INVALID_ARG", "LIMIT", "INTERRUPTED", "BROKEN_PIPE", "IO", |
| 64 | "OUTCOME_UNKNOWN", "CAP_DOMAIN_MISMATCH", "CAP_GEN_NARROW", |
| 65 | "NOT_SUPPORTED", "WOULD_BLOCK", "PARTIAL_LAUNCH"}; |
| 66 | enum outc { O_NOT_STARTED = 0, O_COMPLETED, O_FAILED_NO_EFFECT, O_CANCELLED, O_UNKNOWN }; |
| 67 | static const char *const k_out[] = {"NOT_STARTED", "COMPLETED", "FAILED_NO_EFFECT", "CANCELLED", "OUTCOME_UNKNOWN"}; |
| 68 | |
| 69 | /* ---- limits (section ST9 minimums; the model uses exactly them) ---- */ |
| 70 | #define CAP_BYTES 65536u |
| 71 | #define NPIPES 8 |
| 72 | #define NSLOTS 16 |
| 73 | #define NTASKS 16 |
| 74 | #define NTABLES (2 + NTASKS) /* A, B, then one table per task */ |
| 75 | #define NNAMES 96 |
| 76 | #define MAXSTAGES 8 |
| 77 | |
| 78 | /* ---- rights (ST4.11) ---- */ |
| 79 | #define R_READ 0x01u |
| 80 | #define R_WRITE 0x02u |
| 81 | #define R_MAP 0x04u |
| 82 | #define R_GRANT 0x08u |
| 83 | #define R_DERIVE 0x10u |
| 84 | #define R_REVOKE 0x20u |
| 85 | #define R_CTL (R_GRANT | R_DERIVE | R_REVOKE) |
| 86 | |
| 87 | #define FRAME_MAGIC 0x4F534846u |
| 88 | |
| 89 | struct pipe { |
| 90 | uint8_t buf[CAP_BYTES * 2]; /* twice the capacity so MUT_UNBOUNDED has room to misbehave */ |
| 91 | uint32_t head, len, inc; |
| 92 | unsigned readers, writers; |
| 93 | int live, revoked, latch[2]; |
| 94 | }; |
| 95 | struct slot { |
| 96 | int live; |
| 97 | uint32_t gen, inc; |
| 98 | uint64_t epoch; |
| 99 | unsigned pipe, end, rights; |
| 100 | }; |
| 101 | struct table { |
| 102 | struct slot s[NSLOTS]; |
| 103 | uint32_t gens[NSLOTS]; |
| 104 | }; |
| 105 | struct href { /* a named handle as the vector sees it (opaque cell) */ |
| 106 | char name[24]; |
| 107 | unsigned table, slot; |
| 108 | uint32_t gen; |
| 109 | uint64_t epoch; |
| 110 | }; |
| 111 | |
| 112 | enum prog { P_EXIT, P_EMIT, P_CAT, P_DRAIN, P_FLOOD, P_HANG, P_STUBBORN }; |
| 113 | enum tstate { T_FREE = 0, T_RUNNING, T_ENDED }; |
| 114 | struct task { |
| 115 | int state, intr, lost, intent_open; |
| 116 | uint32_t gen; |
| 117 | uint64_t epoch; |
| 118 | enum prog prog; |
| 119 | int arg, status, outcome, cancelled; |
| 120 | uint8_t data[256]; |
| 121 | size_t dlen, dpos; |
| 122 | uint8_t carry[4096]; |
| 123 | size_t clen; |
| 124 | int in_slot, out_slot; /* slot in the task's table, -1 none */ |
| 125 | int src_virtual; /* stdin is the pipeline source */ |
| 126 | int sink_virtual; /* stdout is the pipeline sink */ |
| 127 | int pipeline; /* index into pipelines or -1 */ |
| 128 | }; |
| 129 | struct tref { |
| 130 | char name[24]; |
| 131 | unsigned idx; |
| 132 | uint32_t gen; |
| 133 | uint64_t epoch; |
| 134 | }; |
| 135 | struct cap { |
| 136 | char name[24]; |
| 137 | unsigned index; |
| 138 | uint64_t gen; |
| 139 | int revoked; |
| 140 | }; |
| 141 | struct binding { |
| 142 | char name[24]; |
| 143 | int none; |
| 144 | unsigned domain, index; |
| 145 | uint64_t gen; |
| 146 | }; |
| 147 | struct pline { |
| 148 | char name[24]; |
| 149 | int live, n; |
| 150 | int task[MAXSTAGES]; /* task index, or -1 */ |
| 151 | int outcome[MAXSTAGES], status[MAXSTAGES]; |
| 152 | uint8_t src[256]; |
| 153 | size_t slen, spos; |
| 154 | uint8_t sink[4096]; |
| 155 | size_t sinklen; |
| 156 | uint64_t epoch; |
| 157 | }; |
| 158 | |
| 159 | static struct { |
| 160 | struct pipe pipes[NPIPES]; |
| 161 | struct table tables[NTABLES]; |
| 162 | struct task tasks[NTASKS]; |
| 163 | struct href h[NNAMES]; |
| 164 | unsigned nh; |
| 165 | struct tref t[NNAMES]; |
| 166 | unsigned nt; |
| 167 | struct cap caps[16]; |
| 168 | unsigned ncaps; |
| 169 | struct binding b[16]; |
| 170 | unsigned nb; |
| 171 | struct pline p[4]; |
| 172 | unsigned np; |
| 173 | uint64_t epoch; |
| 174 | unsigned domain; /* authority domain of this vector, 0 = any */ |
| 175 | unsigned open_intents, crashed_unknown; |
| 176 | } M; |
| 177 | |
| 178 | static int g_fail; |
| 179 | static const char *g_file; |
| 180 | static unsigned g_line; |
| 181 | |
| 182 | static void bad(const char *msg) |
| 183 | { |
| 184 | fprintf(stderr, "VECTOR_ERROR %s:%u %s\n", g_file, g_line, msg); |
| 185 | g_fail = 1; |
| 186 | } |
| 187 | |
| 188 | static void model_reset(void) |
| 189 | { |
| 190 | memset(&M, 0, sizeof M); |
| 191 | M.epoch = 1; |
| 192 | for (unsigned t = 0; t < NTABLES; t++) |
| 193 | for (unsigned i = 0; i < NSLOTS; i++) |
| 194 | M.tables[t].gens[i] = 0; |
| 195 | } |
| 196 | |
| 197 | /* ---------------- handles and pipes ---------------- */ |
| 198 | |
| 199 | static struct href *hname(const char *n, int create) |
| 200 | { |
| 201 | for (unsigned i = 0; i < M.nh; i++) |
| 202 | if (!strcmp(M.h[i].name, n)) |
| 203 | return &M.h[i]; |
| 204 | if (!create || M.nh == NNAMES || strlen(n) >= sizeof M.h[0].name) |
| 205 | return 0; |
| 206 | struct href *r = &M.h[M.nh++]; |
| 207 | memset(r, 0, sizeof *r); |
| 208 | strcpy(r->name, n); |
| 209 | return r; |
| 210 | } |
| 211 | |
| 212 | static int slot_alloc(unsigned t, unsigned pipe, unsigned end, unsigned rights) |
| 213 | { |
| 214 | struct table *tb = &M.tables[t]; |
| 215 | for (unsigned i = 0; i < NSLOTS; i++) |
| 216 | if (!tb->s[i].live) { |
| 217 | tb->gens[i]++; |
| 218 | tb->s[i].live = 1; |
| 219 | tb->s[i].gen = tb->gens[i]; |
| 220 | tb->s[i].inc = M.pipes[pipe].inc; |
| 221 | tb->s[i].epoch = M.epoch; |
| 222 | tb->s[i].pipe = pipe; |
| 223 | tb->s[i].end = end; |
| 224 | tb->s[i].rights = rights; |
| 225 | if (end == 0) |
| 226 | M.pipes[pipe].readers++; |
| 227 | else |
| 228 | M.pipes[pipe].writers++; |
| 229 | return (int)i; |
| 230 | } |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | static void pipe_free_if_done(unsigned p) |
| 235 | { |
| 236 | struct pipe *q = &M.pipes[p]; |
| 237 | if (q->live && q->readers == 0 && q->writers == 0) { |
| 238 | memset(q->buf, 0, sizeof q->buf); |
| 239 | q->live = 0; |
| 240 | q->revoked = 0; |
| 241 | q->head = q->len = 0; |
| 242 | q->latch[0] = q->latch[1] = 0; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static void slot_release(unsigned t, unsigned i) |
| 247 | { |
| 248 | struct slot *s = &M.tables[t].s[i]; |
| 249 | if (!s->live) |
| 250 | return; |
| 251 | struct pipe *q = &M.pipes[s->pipe]; |
| 252 | if (s->inc == q->inc && q->live) { |
| 253 | if (s->end == 0 && q->readers) |
| 254 | q->readers--; |
| 255 | else if (s->end == 1 && q->writers) |
| 256 | q->writers--; |
| 257 | if (s->end == 0 && q->readers == 0) { /* bytes nobody can read any more are discarded (ST4.5) */ |
| 258 | memset(q->buf, 0, sizeof q->buf); |
| 259 | q->head = q->len = 0; |
| 260 | } |
| 261 | pipe_free_if_done(s->pipe); |
| 262 | } |
| 263 | s->live = 0; |
| 264 | } |
| 265 | |
| 266 | /* ST3.2 check order. need: rights; want_end: 0 read, 1 write, -1 either; close_ok: a revoked stream may be closed. */ |
| 267 | static int resolve(unsigned t, const struct href *h, unsigned need, int want_end, int close_ok, unsigned *pipe, |
| 268 | unsigned *end) |
| 269 | { |
| 270 | if (!h || h->gen == 0 || h->slot >= NSLOTS) |
| 271 | return E_INVALID_ARG; |
| 272 | if (h->table != t) |
| 273 | return E_STALE; /* a handle names an entry of its own table only */ |
| 274 | const struct slot *s = &M.tables[t].s[h->slot]; |
| 275 | if (!s->live || (s->gen != h->gen && !MUT(MUT_NO_GENERATION))) |
| 276 | return E_STALE; |
| 277 | if (h->epoch != M.epoch && !MUT(MUT_NO_EPOCH)) |
| 278 | return E_STALE; |
| 279 | if ((s->rights & need) != need) |
| 280 | return E_DENIED; |
| 281 | if (want_end >= 0 && s->end != (unsigned)want_end) |
| 282 | return E_DENIED; |
| 283 | const struct pipe *q = &M.pipes[s->pipe]; |
| 284 | if (!q->live || q->inc != s->inc) |
| 285 | return E_STALE; |
| 286 | if (q->revoked && !close_ok) |
| 287 | return E_REVOKED; |
| 288 | *pipe = s->pipe; |
| 289 | *end = s->end; |
| 290 | return E_OK; |
| 291 | } |
| 292 | |
| 293 | static int op_create(unsigned t, struct href *r, struct href *w) |
| 294 | { |
| 295 | unsigned idx = NPIPES; |
| 296 | for (unsigned i = 0; i < NPIPES; i++) |
| 297 | if (!M.pipes[i].live) { |
| 298 | idx = i; |
| 299 | break; |
| 300 | } |
| 301 | if (idx == NPIPES) |
| 302 | return E_LIMIT; |
| 303 | struct pipe *q = &M.pipes[idx]; |
| 304 | q->inc++; |
| 305 | q->live = 1; |
| 306 | q->revoked = 0; |
| 307 | q->head = q->len = 0; |
| 308 | q->readers = q->writers = 0; |
| 309 | q->latch[0] = q->latch[1] = 0; |
| 310 | int rs = slot_alloc(t, idx, 0, R_READ | R_CTL); |
| 311 | if (rs < 0) { |
| 312 | pipe_free_if_done(idx); |
| 313 | return E_LIMIT; |
| 314 | } |
| 315 | int ws = slot_alloc(t, idx, 1, R_WRITE | R_CTL); |
| 316 | if (ws < 0) { |
| 317 | if (!MUT(MUT_LIMIT_LEAK)) |
| 318 | slot_release(t, (unsigned)rs); |
| 319 | return E_LIMIT; |
| 320 | } |
| 321 | r->table = w->table = t; |
| 322 | r->slot = (unsigned)rs; |
| 323 | w->slot = (unsigned)ws; |
| 324 | r->gen = M.tables[t].s[rs].gen; |
| 325 | w->gen = M.tables[t].s[ws].gen; |
| 326 | r->epoch = w->epoch = M.epoch; |
| 327 | return E_OK; |
| 328 | } |
| 329 | |
| 330 | static int latched(struct pipe *q, unsigned end) |
| 331 | { |
| 332 | if (q->latch[end]) { |
| 333 | q->latch[end] = 0; |
| 334 | return 1; |
| 335 | } |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static int op_read(unsigned t, const struct href *h, uint8_t *out, size_t max, size_t *n) |
| 340 | { |
| 341 | unsigned p, end; |
| 342 | *n = 0; |
| 343 | int e = resolve(t, h, R_READ, 0, 0, &p, &end); |
| 344 | if (e) |
| 345 | return e; |
| 346 | struct pipe *q = &M.pipes[p]; |
| 347 | if (latched(q, 0)) |
| 348 | return E_INTERRUPTED; |
| 349 | if (max == 0) |
| 350 | return E_OK; |
| 351 | if (q->len == 0) |
| 352 | return (q->writers == 0 || MUT(MUT_EOF_ANY)) ? E_OK : E_WOULD_BLOCK; |
| 353 | size_t take = max < q->len ? max : q->len; |
| 354 | for (size_t i = 0; i < take; i++) { |
| 355 | out[i] = q->buf[q->head]; |
| 356 | q->buf[q->head] = 0; |
| 357 | q->head = (q->head + 1) % (uint32_t)sizeof q->buf; |
| 358 | } |
| 359 | q->len -= (uint32_t)take; |
| 360 | *n = take; |
| 361 | return E_OK; |
| 362 | } |
| 363 | |
| 364 | static int op_write(unsigned t, const struct href *h, const uint8_t *in, size_t len, size_t *n) |
| 365 | { |
| 366 | unsigned p, end; |
| 367 | *n = 0; |
| 368 | int e = resolve(t, h, MUT(MUT_WRITE_RIGHT) ? 0 : R_WRITE, MUT(MUT_WRITE_RIGHT) ? -1 : 1, 0, &p, &end); |
| 369 | if (e) |
| 370 | return e; |
| 371 | struct pipe *q = &M.pipes[p]; |
| 372 | if (latched(q, 1)) |
| 373 | return E_INTERRUPTED; |
| 374 | if (len == 0 && MUT(MUT_ZERO_WRITE_NO_READER)) |
| 375 | return E_OK; |
| 376 | if (q->readers == 0 && !MUT(MUT_NO_BROKEN_PIPE)) |
| 377 | return E_BROKEN_PIPE; |
| 378 | if (len == 0) |
| 379 | return E_OK; |
| 380 | size_t cap = MUT(MUT_UNBOUNDED) ? sizeof q->buf : CAP_BYTES; |
| 381 | size_t room = cap - q->len; |
| 382 | if (room == 0) |
| 383 | return E_WOULD_BLOCK; |
| 384 | size_t put = len < room ? len : room; |
| 385 | for (size_t i = 0; i < put; i++) |
| 386 | q->buf[(q->head + q->len + i) % (uint32_t)sizeof q->buf] = in[i]; |
| 387 | q->len += (uint32_t)put; |
| 388 | *n = put; |
| 389 | return E_OK; |
| 390 | } |
| 391 | |
| 392 | static int op_close(unsigned t, const struct href *h) |
| 393 | { |
| 394 | if (!h || h->gen == 0 || h->slot >= NSLOTS) |
| 395 | return E_INVALID_ARG; |
| 396 | if (h->table != t) |
| 397 | return E_STALE; |
| 398 | struct slot *s = &M.tables[t].s[h->slot]; |
| 399 | if (!s->live || (s->gen != h->gen && !MUT(MUT_NO_GENERATION)) || (h->epoch != M.epoch && !MUT(MUT_NO_EPOCH))) |
| 400 | return E_STALE; |
| 401 | const struct pipe *q = &M.pipes[s->pipe]; |
| 402 | int stale = !q->live || q->inc != s->inc; |
| 403 | slot_release(t, h->slot); |
| 404 | return stale ? E_STALE : E_OK; |
| 405 | } |
| 406 | |
| 407 | static int op_dup(unsigned t, const struct href *h, unsigned to, unsigned rights, struct href *out) |
| 408 | { |
| 409 | unsigned p, end; |
| 410 | unsigned need = to == t ? R_DERIVE : R_GRANT; |
| 411 | int e = resolve(t, h, need, -1, 0, &p, &end); |
| 412 | if (e) |
| 413 | return e; |
| 414 | unsigned dr = end == 0 ? R_READ : R_WRITE; |
| 415 | if (!(rights & dr) || (rights & ~(dr | R_CTL))) |
| 416 | return E_INVALID_ARG; |
| 417 | if ((rights & ~M.tables[t].s[h->slot].rights) && !MUT(MUT_DUP_NO_SUBSET)) |
| 418 | return E_DENIED; |
| 419 | int ns = slot_alloc(to, p, end, rights); |
| 420 | if (ns < 0) |
| 421 | return E_LIMIT; |
| 422 | out->table = to; |
| 423 | out->slot = (unsigned)ns; |
| 424 | out->gen = M.tables[to].s[ns].gen; |
| 425 | out->epoch = M.epoch; |
| 426 | return E_OK; |
| 427 | } |
| 428 | |
| 429 | static int op_revoke(unsigned t, const struct href *h) |
| 430 | { |
| 431 | unsigned p, end; |
| 432 | int e = resolve(t, h, R_REVOKE, -1, 0, &p, &end); |
| 433 | if (e) |
| 434 | return e; |
| 435 | struct pipe *q = &M.pipes[p]; |
| 436 | q->revoked = 1; |
| 437 | q->latch[0] = q->latch[1] = 0; |
| 438 | { |
| 439 | memset(q->buf, 0, sizeof q->buf); |
| 440 | q->head = q->len = 0; |
| 441 | } |
| 442 | return E_OK; |
| 443 | } |
| 444 | |
| 445 | /* An interrupt aimed at the end h names (ST4.7). With nobody parked it is latched for the next call. */ |
| 446 | static int op_cancel(unsigned t, const struct href *h) |
| 447 | { |
| 448 | unsigned p, end; |
| 449 | if (!h || h->table != t || h->slot >= NSLOTS || !M.tables[t].s[h->slot].live) |
| 450 | return resolve(t, h, 0, -1, 0, &p, &end); |
| 451 | unsigned need = M.tables[t].s[h->slot].end == 0 ? R_READ : R_WRITE; |
| 452 | int e = resolve(t, h, need, -1, 0, &p, &end); |
| 453 | if (e) |
| 454 | return e; |
| 455 | if (!MUT(MUT_NO_LATCH)) |
| 456 | M.pipes[p].latch[end] = 1; |
| 457 | return E_OK; |
| 458 | } |
| 459 | |
| 460 | /* ---------------- capabilities and bindings (ST5) ---------------- */ |
| 461 | |
| 462 | static struct cap *cname(const char *n) |
| 463 | { |
| 464 | for (unsigned i = 0; i < M.ncaps; i++) |
| 465 | if (!strcmp(M.caps[i].name, n)) |
| 466 | return &M.caps[i]; |
| 467 | return 0; |
| 468 | } |
| 469 | static struct binding *bname(const char *n) |
| 470 | { |
| 471 | for (unsigned i = 0; i < M.nb; i++) |
| 472 | if (!strcmp(M.b[i].name, n)) |
| 473 | return &M.b[i]; |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /* steps 0 to 2: the shape of the binding (checked before any effect of a launch) */ |
| 478 | static int bind_shape(const struct binding *b) |
| 479 | { |
| 480 | if (!b || b->none) |
| 481 | return E_DENIED; |
| 482 | if (b->domain != 1 && b->domain != 2) |
| 483 | return E_CAP_DOMAIN_MISMATCH; |
| 484 | if (b->domain == 1 && b->gen > 0xFFFFFFFFull && !MUT(MUT_GEN_TRUNCATE)) |
| 485 | return E_CAP_GEN_NARROW; |
| 486 | return E_OK; |
| 487 | } |
| 488 | |
| 489 | /* steps 3 to 6: against the authority, at the moment of the effect */ |
| 490 | static int bind_effect(const struct binding *b) |
| 491 | { |
| 492 | int e = bind_shape(b); |
| 493 | if (e) |
| 494 | return e; |
| 495 | if (b->domain != M.domain && !MUT(MUT_DOMAIN_BRIDGE)) |
| 496 | return E_CAP_DOMAIN_MISMATCH; |
| 497 | uint64_t g = b->gen; |
| 498 | if (b->domain == 1) |
| 499 | g &= 0xFFFFFFFFull; /* only reachable under MUT_GEN_TRUNCATE: shape refused anything wider */ |
| 500 | for (unsigned i = 0; i < M.ncaps; i++) { |
| 501 | const struct cap *c = &M.caps[i]; |
| 502 | if (c->index != b->index) |
| 503 | continue; |
| 504 | int same = MUT(MUT_GEN_32BIT_COMPARE) ? (uint32_t)c->gen == (uint32_t)g : c->gen == g; |
| 505 | if (!same) |
| 506 | return E_STALE; |
| 507 | return c->revoked ? E_REVOKED : E_OK; |
| 508 | } |
| 509 | return E_STALE; |
| 510 | } |
| 511 | |
| 512 | /* ---------------- tasks ---------------- */ |
| 513 | |
| 514 | static void task_end(unsigned ti, int status, int outcome) |
| 515 | { |
| 516 | struct task *k = &M.tasks[ti]; |
| 517 | k->state = T_ENDED; |
| 518 | k->status = status; |
| 519 | k->outcome = outcome; |
| 520 | if (!MUT(MUT_TASK_KEEPS_ENDS)) |
| 521 | for (unsigned i = 0; i < NSLOTS; i++) |
| 522 | slot_release(2 + ti, i); |
| 523 | } |
| 524 | |
| 525 | static struct href g_tmp; /* a handle into a task's own table */ |
| 526 | static const struct href *task_h(unsigned ti, int slot) |
| 527 | { |
| 528 | const struct slot *s = &M.tables[2 + ti].s[slot]; |
| 529 | g_tmp.table = 2 + ti; |
| 530 | g_tmp.slot = (unsigned)slot; |
| 531 | g_tmp.gen = s->gen; |
| 532 | g_tmp.epoch = s->epoch; |
| 533 | return &g_tmp; |
| 534 | } |
| 535 | |
| 536 | /* Task output: to its stdout pipe, to the pipeline sink, or discarded. */ |
| 537 | static int task_out(unsigned ti, const uint8_t *b, size_t len, size_t *n) |
| 538 | { |
| 539 | struct task *k = &M.tasks[ti]; |
| 540 | if (k->sink_virtual) { |
| 541 | struct pline *pl = &M.p[k->pipeline]; |
| 542 | size_t room = sizeof pl->sink - pl->sinklen, put = len < room ? len : room; |
| 543 | memcpy(pl->sink + pl->sinklen, b, put); |
| 544 | pl->sinklen += put; |
| 545 | *n = len; /* the sink never applies backpressure; overflow is a vector error caught by out= */ |
| 546 | return E_OK; |
| 547 | } |
| 548 | if (k->out_slot < 0) { |
| 549 | *n = len; |
| 550 | return E_OK; |
| 551 | } |
| 552 | return op_write(2 + ti, task_h(ti, k->out_slot), b, len, n); |
| 553 | } |
| 554 | static int task_in(unsigned ti, uint8_t *b, size_t max, size_t *n) |
| 555 | { |
| 556 | struct task *k = &M.tasks[ti]; |
| 557 | *n = 0; |
| 558 | if (k->src_virtual) { |
| 559 | struct pline *pl = &M.p[k->pipeline]; |
| 560 | size_t take = pl->slen - pl->spos < max ? pl->slen - pl->spos : max; |
| 561 | memcpy(b, pl->src + pl->spos, take); |
| 562 | pl->spos += take; |
| 563 | *n = take; |
| 564 | return E_OK; |
| 565 | } |
| 566 | if (k->in_slot < 0) |
| 567 | return E_OK; |
| 568 | return op_read(2 + ti, task_h(ti, k->in_slot), b, max, n); |
| 569 | } |
| 570 | |
| 571 | /* One step of a running task. Returns 1 if it made progress. */ |
| 572 | static int task_step(unsigned ti) |
| 573 | { |
| 574 | struct task *k = &M.tasks[ti]; |
| 575 | if (k->state != T_RUNNING) |
| 576 | return 0; |
| 577 | if (k->intr && k->prog != P_STUBBORN) { |
| 578 | k->intr = 0; |
| 579 | task_end(ti, 130, k->cancelled ? O_CANCELLED : O_COMPLETED); |
| 580 | return 1; |
| 581 | } |
| 582 | k->intr = 0; |
| 583 | size_t n = 0; |
| 584 | int e; |
| 585 | switch (k->prog) { |
| 586 | case P_EXIT: |
| 587 | task_end(ti, k->arg, O_COMPLETED); |
| 588 | return 1; |
| 589 | case P_EMIT: |
| 590 | case P_FLOOD: |
| 591 | if (k->prog == P_FLOOD && k->dpos == k->dlen) |
| 592 | k->dpos = 0; |
| 593 | if (k->dpos == k->dlen) { |
| 594 | task_end(ti, k->arg, O_COMPLETED); |
| 595 | return 1; |
| 596 | } |
| 597 | e = task_out(ti, k->data + k->dpos, k->dlen - k->dpos, &n); |
| 598 | if (e == E_BROKEN_PIPE) { |
| 599 | task_end(ti, 141, O_COMPLETED); |
| 600 | return 1; |
| 601 | } |
| 602 | if (e == E_INTERRUPTED) { |
| 603 | task_end(ti, 130, k->cancelled ? O_CANCELLED : O_COMPLETED); |
| 604 | return 1; |
| 605 | } |
| 606 | if (e != E_OK) |
| 607 | return 0; /* WOULD_BLOCK: parked */ |
| 608 | k->dpos += n; |
| 609 | return n > 0; |
| 610 | case P_CAT: |
| 611 | case P_DRAIN: |
| 612 | if (k->clen) { |
| 613 | e = task_out(ti, k->carry, k->clen, &n); |
| 614 | if (e == E_BROKEN_PIPE) { |
| 615 | task_end(ti, 141, O_COMPLETED); |
| 616 | return 1; |
| 617 | } |
| 618 | if (e != E_OK) |
| 619 | return 0; |
| 620 | memmove(k->carry, k->carry + n, k->clen - n); |
| 621 | k->clen -= n; |
| 622 | return n > 0; |
| 623 | } |
| 624 | e = task_in(ti, k->carry, sizeof k->carry, &n); |
| 625 | if (e == E_WOULD_BLOCK) |
| 626 | return 0; |
| 627 | if (e == E_INTERRUPTED) { |
| 628 | task_end(ti, 130, k->cancelled ? O_CANCELLED : O_COMPLETED); |
| 629 | return 1; |
| 630 | } |
| 631 | if (e != E_OK || n == 0) { |
| 632 | task_end(ti, e == E_OK ? 0 : 1, O_COMPLETED); |
| 633 | return 1; |
| 634 | } |
| 635 | k->clen = k->prog == P_CAT ? n : 0; |
| 636 | return 1; |
| 637 | case P_HANG: |
| 638 | case P_STUBBORN: |
| 639 | return 0; |
| 640 | } |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static void sched_run(void) |
| 645 | { |
| 646 | for (int round = 0; round < 100000; round++) { |
| 647 | int progress = 0; |
| 648 | for (unsigned i = 0; i < NTASKS; i++) |
| 649 | progress |= task_step(i); |
| 650 | if (!progress) |
| 651 | return; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | static void intent_close(unsigned ti) |
| 656 | { |
| 657 | if (M.tasks[ti].intent_open) { |
| 658 | M.tasks[ti].intent_open = 0; |
| 659 | M.open_intents--; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | static void task_release(unsigned ti) |
| 664 | { |
| 665 | intent_close(ti); |
| 666 | for (unsigned i = 0; i < NSLOTS; i++) |
| 667 | slot_release(2 + ti, i); |
| 668 | M.tasks[ti].state = T_FREE; |
| 669 | } |
| 670 | |
| 671 | static int task_cancel(unsigned ti, int terminate) |
| 672 | { |
| 673 | struct task *k = &M.tasks[ti]; |
| 674 | if (k->state != T_RUNNING) |
| 675 | return E_OK; |
| 676 | k->cancelled = 1; |
| 677 | if (terminate && !MUT(MUT_TERMINATE_IGNORABLE)) { |
| 678 | task_end(ti, 137, O_CANCELLED); |
| 679 | return E_OK; |
| 680 | } |
| 681 | k->intr = 1; |
| 682 | return E_OK; |
| 683 | } |
| 684 | |
| 685 | /* ST6.5 cleanup of a set of tasks: interrupt, grace (the scheduler runs until nothing moves), terminate. */ |
| 686 | static void cleanup_tasks(const int *list, int n) |
| 687 | { |
| 688 | for (int i = 0; i < n; i++) |
| 689 | if (list[i] >= 0) |
| 690 | task_cancel((unsigned)list[i], 0); |
| 691 | sched_run(); |
| 692 | for (int i = 0; i < n; i++) |
| 693 | if (list[i] >= 0) |
| 694 | task_cancel((unsigned)list[i], 1); |
| 695 | } |
| 696 | |
| 697 | static int parse_prog(char **tok, int ntok, struct task *k, int *consumed) |
| 698 | { |
| 699 | const char *p = tok[0]; |
| 700 | *consumed = 1; |
| 701 | k->arg = 0; |
| 702 | k->dlen = k->dpos = 0; |
| 703 | if (!strcmp(p, "exit") || !strcmp(p, "emit")) { |
| 704 | if (!strcmp(p, "exit")) { |
| 705 | if (ntok < 2) |
| 706 | return -1; |
| 707 | k->prog = P_EXIT; |
| 708 | k->arg = atoi(tok[1]); |
| 709 | *consumed = 2; |
| 710 | return 0; |
| 711 | } |
| 712 | return -1; /* emit is handled by the caller (needs the byte decoder) */ |
| 713 | } |
| 714 | if (!strcmp(p, "cat")) k->prog = P_CAT; |
| 715 | else if (!strcmp(p, "drain")) k->prog = P_DRAIN; |
| 716 | else if (!strcmp(p, "flood")) { |
| 717 | k->prog = P_FLOOD; |
| 718 | memcpy(k->data, "y\n", 2); |
| 719 | k->dlen = 2; |
| 720 | } else if (!strcmp(p, "hang")) k->prog = P_HANG; |
| 721 | else if (!strcmp(p, "stubborn")) k->prog = P_STUBBORN; |
| 722 | else return -1; |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | /* ---------------- corpus parsing ---------------- */ |
| 727 | |
| 728 | static int unq(const char *q, uint8_t *out, size_t cap, size_t *n) |
| 729 | { |
| 730 | size_t len = strlen(q), o = 0; |
| 731 | if (len < 2 || q[0] != '"' || q[len - 1] != '"') |
| 732 | return -1; |
| 733 | for (size_t i = 1; i + 1 < len; i++) { |
| 734 | unsigned c = (unsigned char)q[i]; |
| 735 | if (c == '\\') { |
| 736 | if (i + 2 >= len) return -1; |
| 737 | if (q[i + 1] == 'x') { |
| 738 | if (i + 4 >= len) return -1; |
| 739 | char hx[3] = {q[i + 2], q[i + 3], 0}; |
| 740 | c = (unsigned)strtoul(hx, 0, 16); |
| 741 | i += 3; |
| 742 | } else { |
| 743 | c = (unsigned char)q[i + 1]; |
| 744 | i++; |
| 745 | } |
| 746 | } |
| 747 | if (o == cap) return -1; |
| 748 | out[o++] = (uint8_t)c; |
| 749 | } |
| 750 | *n = o; |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | static void q_out(char *dst, size_t cap, const uint8_t *b, size_t n) |
| 755 | { |
| 756 | size_t o = 0; |
| 757 | dst[o++] = '"'; |
| 758 | for (size_t i = 0; i < n && o + 6 < cap; i++) { |
| 759 | if (b[i] == '"' || b[i] == '\\') { |
| 760 | dst[o++] = '\\'; |
| 761 | dst[o++] = (char)b[i]; |
| 762 | } else if (b[i] < 0x20 || b[i] > 0x7E) { |
| 763 | o += (size_t)snprintf(dst + o, cap - o, "\\x%02x", b[i]); |
| 764 | } else { |
| 765 | dst[o++] = (char)b[i]; |
| 766 | } |
| 767 | } |
| 768 | dst[o++] = '"'; |
| 769 | dst[o] = 0; |
| 770 | } |
| 771 | |
| 772 | static int split(char *s, char **tok, int max) |
| 773 | { |
| 774 | int n = 0; |
| 775 | while (*s) { |
| 776 | while (*s == ' ' || *s == '\t') s++; |
| 777 | if (!*s) break; |
| 778 | if (n == max) return -1; |
| 779 | tok[n++] = s; |
| 780 | int inq = 0; |
| 781 | while (*s && (inq || (*s != ' ' && *s != '\t'))) { |
| 782 | if (*s == '\\' && inq && s[1]) { s += 2; continue; } |
| 783 | if (*s == '"') inq = !inq; |
| 784 | s++; |
| 785 | } |
| 786 | if (*s) *s++ = 0; |
| 787 | } |
| 788 | return n; |
| 789 | } |
| 790 | |
| 791 | static unsigned tbl(const char *s) |
| 792 | { |
| 793 | if (!strcmp(s, "A")) return 0; |
| 794 | if (!strcmp(s, "B")) return 1; |
| 795 | return 99; |
| 796 | } |
| 797 | |
| 798 | static unsigned rights_of(const char *s) |
| 799 | { |
| 800 | unsigned r = 0; |
| 801 | for (; *s; s++) |
| 802 | switch (*s) { |
| 803 | case 'r': r |= R_READ; break; |
| 804 | case 'w': r |= R_WRITE; break; |
| 805 | case 'm': r |= R_MAP; break; |
| 806 | case 'g': r |= R_GRANT; break; |
| 807 | case 'd': r |= R_DERIVE; break; |
| 808 | case 'v': r |= R_REVOKE; break; |
| 809 | default: return 0x100; |
| 810 | } |
| 811 | return r; |
| 812 | } |
| 813 | |
| 814 | static struct tref *tname(const char *n, int create) |
| 815 | { |
| 816 | for (unsigned i = 0; i < M.nt; i++) |
| 817 | if (!strcmp(M.t[i].name, n)) |
| 818 | return &M.t[i]; |
| 819 | if (!create || M.nt == NNAMES || strlen(n) >= sizeof M.t[0].name) |
| 820 | return 0; |
| 821 | struct tref *r = &M.t[M.nt++]; |
| 822 | memset(r, 0, sizeof *r); |
| 823 | strcpy(r->name, n); |
| 824 | return r; |
| 825 | } |
| 826 | |
| 827 | static int task_lookup(const struct tref *r, unsigned *ti) |
| 828 | { |
| 829 | if (!r) return E_INVALID_ARG; |
| 830 | const struct task *k = &M.tasks[r->idx]; |
| 831 | if (k->state == T_FREE || k->gen != r->gen || (r->epoch != M.epoch && !MUT(MUT_NO_EPOCH))) |
| 832 | return E_STALE; |
| 833 | *ti = r->idx; |
| 834 | return E_OK; |
| 835 | } |
| 836 | |
| 837 | static int task_alloc(unsigned *ti) |
| 838 | { |
| 839 | for (unsigned i = 0; i < NTASKS; i++) |
| 840 | if (M.tasks[i].state == T_FREE) { |
| 841 | uint32_t g = M.tasks[i].gen + 1; |
| 842 | memset(&M.tasks[i], 0, sizeof M.tasks[i]); |
| 843 | M.tasks[i].gen = g; |
| 844 | M.tasks[i].epoch = M.epoch; |
| 845 | M.tasks[i].in_slot = M.tasks[i].out_slot = -1; |
| 846 | M.tasks[i].pipeline = -1; |
| 847 | for (unsigned s = 0; s < NSLOTS; s++) |
| 848 | M.tables[2 + i].s[s].live = 0; |
| 849 | *ti = i; |
| 850 | return E_OK; |
| 851 | } |
| 852 | return E_LIMIT; |
| 853 | } |
| 854 | |
| 855 | /* give the task its own duplicate of a stream end (ST6.1) */ |
| 856 | static int task_take(unsigned ti, unsigned pipe, unsigned end) |
| 857 | { |
| 858 | return slot_alloc(2 + ti, pipe, end, end == 0 ? R_READ : R_WRITE); |
| 859 | } |
| 860 | |
| 861 | static void list_out(char *dst, size_t cap, const int *v, int n, int outcomes) |
| 862 | { |
| 863 | size_t o = 0; |
| 864 | dst[0] = 0; |
| 865 | for (int i = 0; i < n; i++) { |
| 866 | if (outcomes) |
| 867 | o += (size_t)snprintf(dst + o, cap - o, "%s%s", i ? "," : "", k_out[v[i]]); |
| 868 | else if (v[i] < 0) |
| 869 | o += (size_t)snprintf(dst + o, cap - o, "%s-", i ? "," : ""); |
| 870 | else |
| 871 | o += (size_t)snprintf(dst + o, cap - o, "%s%d", i ? "," : "", v[i]); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | static struct pline *pname(const char *n, int create) |
| 876 | { |
| 877 | for (unsigned i = 0; i < M.np; i++) |
| 878 | if (!strcmp(M.p[i].name, n)) |
| 879 | return &M.p[i]; |
| 880 | if (!create || M.np == 4 || strlen(n) >= sizeof M.p[0].name) |
| 881 | return 0; |
| 882 | struct pline *r = &M.p[M.np++]; |
| 883 | memset(r, 0, sizeof *r); |
| 884 | strcpy(r->name, n); |
| 885 | return r; |
| 886 | } |
| 887 | |
| 888 | static void pl_result(char *res, size_t cap, struct pline *pl, int err, int status) |
| 889 | { |
| 890 | char st[128], oc[256]; |
| 891 | list_out(st, sizeof st, pl->status, pl->n, 0); |
| 892 | list_out(oc, sizeof oc, pl->outcome, pl->n, 1); |
| 893 | snprintf(res, cap, "%s status=%d outcomes=%s statuses=%s", k_err[err], status, oc, st); |
| 894 | } |
| 895 | |
| 896 | /* launch P bind=B [in=Q] [inject=K:ERR] [revoke=K:CAP] : stage ! stage ... (ST7) */ |
| 897 | static void cmd_launch(char **tok, int n, char *res, size_t cap) |
| 898 | { |
| 899 | struct pline *pl = pname(tok[1], 1); |
| 900 | if (!pl) { bad("too many pipelines"); return; } |
| 901 | pl->live = 1; |
| 902 | pl->epoch = M.epoch; |
| 903 | pl->slen = pl->spos = pl->sinklen = 0; |
| 904 | const struct binding *b = 0; |
| 905 | int inject_at = 0, inject_err = 0, revoke_at = 0; |
| 906 | struct cap *rc = 0; |
| 907 | int i = 2; |
| 908 | for (; i < n && strcmp(tok[i], ":"); i++) { |
| 909 | if (!strncmp(tok[i], "bind=", 5)) { |
| 910 | b = bname(tok[i] + 5); |
| 911 | if (!b) { bad("unknown binding"); return; } |
| 912 | } else if (!strncmp(tok[i], "in=", 3)) { |
| 913 | if (unq(tok[i] + 3, pl->src, sizeof pl->src, &pl->slen)) { bad("bad in="); return; } |
| 914 | } else if (!strncmp(tok[i], "inject=", 7)) { |
| 915 | inject_at = atoi(tok[i] + 7); |
| 916 | const char *c = strchr(tok[i], ':'); |
| 917 | for (int e = 0; c && e < E_COUNT; e++) |
| 918 | if (!strcmp(c + 1, k_err[e])) inject_err = e; |
| 919 | if (!c || !inject_err) { bad("bad inject="); return; } |
| 920 | } else if (!strncmp(tok[i], "revoke=", 7)) { |
| 921 | revoke_at = atoi(tok[i] + 7); |
| 922 | const char *c = strchr(tok[i], ':'); |
| 923 | rc = c ? cname(c + 1) : 0; |
| 924 | if (!rc) { bad("bad revoke="); return; } |
| 925 | } else { bad("unknown launch option"); return; } |
| 926 | } |
| 927 | if (i >= n) { bad("launch needs ':'"); return; } |
| 928 | /* stages */ |
| 929 | struct task proto[MAXSTAGES + 2]; |
| 930 | int kind[MAXSTAGES + 2]; /* 0 program, 1 nosuch, 2 foreign */ |
| 931 | int ns = 0; |
| 932 | i++; |
| 933 | while (i < n) { |
| 934 | if (ns == MAXSTAGES + 2) { bad("too many stages"); return; } |
| 935 | memset(&proto[ns], 0, sizeof proto[ns]); |
| 936 | kind[ns] = 0; |
| 937 | int used = 1; |
| 938 | if (!strcmp(tok[i], "nosuch")) kind[ns] = 1; |
| 939 | else if (!strcmp(tok[i], "foreign")) kind[ns] = 2; |
| 940 | else if (!strcmp(tok[i], "emit")) { |
| 941 | if (i + 1 >= n || unq(tok[i + 1], proto[ns].data, sizeof proto[ns].data, &proto[ns].dlen)) { bad("emit"); return; } |
| 942 | proto[ns].prog = P_EMIT; |
| 943 | used = 2; |
| 944 | if (i + 2 < n && strcmp(tok[i + 2], "!")) { proto[ns].arg = atoi(tok[i + 2]); used = 3; } |
| 945 | } else if (parse_prog(tok + i, n - i, &proto[ns], &used)) { bad("unknown program"); return; } |
| 946 | i += used; |
| 947 | ns++; |
| 948 | if (i < n) { |
| 949 | if (strcmp(tok[i], "!")) { bad("stages are separated by '!'"); return; } |
| 950 | i++; |
| 951 | } |
| 952 | } |
| 953 | pl->n = ns > MAXSTAGES ? MAXSTAGES : ns; |
| 954 | for (int s = 0; s < MAXSTAGES; s++) { |
| 955 | pl->task[s] = -1; |
| 956 | pl->outcome[s] = O_FAILED_NO_EFFECT; |
| 957 | pl->status[s] = -1; |
| 958 | } |
| 959 | /* steps 1 to 3: no effect yet; a refusal starts nothing */ |
| 960 | int pre = E_OK, pst = 1; |
| 961 | if (ns > MAXSTAGES) pre = E_LIMIT, pst = 1; |
| 962 | for (int s = 0; !pre && s < ns; s++) |
| 963 | if (kind[s] == 2) pre = E_UNAVAILABLE, pst = 126; |
| 964 | if (!pre && (pre = bind_shape(b)) != E_OK) pst = 126; |
| 965 | /* step 4: all pipes */ |
| 966 | int pp[MAXSTAGES][2]; |
| 967 | struct href ph[MAXSTAGES][2]; |
| 968 | for (int s = 0; s < MAXSTAGES; s++) pp[s][0] = pp[s][1] = -1; |
| 969 | for (int s = 0; !pre && s + 1 < ns; s++) { |
| 970 | memset(ph[s], 0, sizeof ph[s]); |
| 971 | if (op_create(0, &ph[s][0], &ph[s][1]) != E_OK) { |
| 972 | pre = E_LIMIT, pst = 1; |
| 973 | for (int u = 0; u <= s; u++) { |
| 974 | if (pp[u][0] >= 0) op_close(0, &ph[u][0]); |
| 975 | if (pp[u][1] >= 0) op_close(0, &ph[u][1]); |
| 976 | } |
| 977 | break; |
| 978 | } |
| 979 | pp[s][0] = 1; |
| 980 | pp[s][1] = 1; |
| 981 | } |
| 982 | if (pre) { |
| 983 | for (int s = 0; s < pl->n; s++) pl->status[s] = pst; |
| 984 | pl_result(res, cap, pl, pre, pst); |
| 985 | pl->live = 0; |
| 986 | return; |
| 987 | } |
| 988 | /* step 5 and 6: start left to right */ |
| 989 | int started = 0, fail = 0, fail_err = 0; |
| 990 | int bind_once = MUT(MUT_BIND_ONCE) ? bind_effect(b) : 0; |
| 991 | for (int s = 0; s < ns; s++) { |
| 992 | if (revoke_at == s + 1) rc->revoked = 1; |
| 993 | int st_e = E_OK, st_status = 0; |
| 994 | if (kind[s] == 1) st_e = E_NOT_FOUND, st_status = 127; |
| 995 | else { |
| 996 | int be = MUT(MUT_BIND_ONCE) ? bind_once : bind_effect(b); |
| 997 | if (be) st_e = be, st_status = 126; |
| 998 | } |
| 999 | if (st_e && MUT(MUT_STAGE_FAIL_ABORTS)) { fail = s + 1; fail_err = st_e; break; } |
| 1000 | if (!st_e && inject_at == s + 1) { fail = s + 1; fail_err = inject_err; break; } |
| 1001 | if (st_e) { |
| 1002 | pl->outcome[s] = O_FAILED_NO_EFFECT; |
| 1003 | pl->status[s] = st_status; |
| 1004 | } else { |
| 1005 | unsigned ti; |
| 1006 | if (task_alloc(&ti)) { fail = s + 1; fail_err = E_LIMIT; break; } |
| 1007 | struct task *k = &M.tasks[ti]; |
| 1008 | uint32_t g = k->gen; |
| 1009 | *k = proto[s]; |
| 1010 | k->gen = g; |
| 1011 | k->epoch = M.epoch; |
| 1012 | k->state = T_RUNNING; |
| 1013 | k->pipeline = (int)(pl - M.p); |
| 1014 | k->in_slot = k->out_slot = -1; |
| 1015 | if (s == 0) k->src_virtual = 1; |
| 1016 | else k->in_slot = task_take(ti, M.tables[0].s[ph[s - 1][0].slot].pipe, 0); |
| 1017 | if (s == ns - 1) k->sink_virtual = 1; |
| 1018 | else k->out_slot = task_take(ti, M.tables[0].s[ph[s][1].slot].pipe, 1); |
| 1019 | k->intent_open = 1; |
| 1020 | M.open_intents++; |
| 1021 | pl->task[s] = (int)ti; |
| 1022 | pl->outcome[s] = O_COMPLETED; |
| 1023 | started++; |
| 1024 | } |
| 1025 | /* the adapter closes its own copies of this stage's ends right after starting (or failing) it */ |
| 1026 | if (s > 0 && pp[s - 1][0] >= 0) { op_close(0, &ph[s - 1][0]); pp[s - 1][0] = -1; } |
| 1027 | if (s + 1 < ns && pp[s][1] >= 0) { op_close(0, &ph[s][1]); pp[s][1] = -1; } |
| 1028 | } |
| 1029 | if (fail) { |
| 1030 | for (int u = 0; u + 1 < ns; u++) |
| 1031 | for (int e = 0; e < 2; e++) |
| 1032 | if (pp[u][e] >= 0) { op_close(0, &ph[u][e]); pp[u][e] = -1; } |
| 1033 | pl->outcome[fail - 1] = O_FAILED_NO_EFFECT; |
| 1034 | pl->status[fail - 1] = 1; |
| 1035 | for (int s = fail; s < ns; s++) { pl->outcome[s] = O_NOT_STARTED; pl->status[s] = -1; } |
| 1036 | if (started == 0) { |
| 1037 | pl_result(res, cap, pl, fail_err, 1); |
| 1038 | pl->live = 0; |
| 1039 | return; |
| 1040 | } |
| 1041 | if (!MUT(MUT_PARTIAL_NO_CANCEL)) { |
| 1042 | cleanup_tasks(pl->task, fail - 1); |
| 1043 | for (int s = 0; s < fail - 1; s++) { |
| 1044 | if (pl->task[s] < 0) continue; |
| 1045 | unsigned ti = (unsigned)pl->task[s]; |
| 1046 | pl->outcome[s] = O_CANCELLED; |
| 1047 | pl->status[s] = M.tasks[ti].status; |
| 1048 | task_release(ti); |
| 1049 | pl->task[s] = -1; |
| 1050 | } |
| 1051 | } |
| 1052 | pl_result(res, cap, pl, E_PARTIAL_LAUNCH, 1); |
| 1053 | if (!MUT(MUT_PARTIAL_NO_CANCEL)) pl->live = 0; |
| 1054 | return; |
| 1055 | } |
| 1056 | snprintf(res, cap, "OK"); |
| 1057 | } |
| 1058 | |
| 1059 | static void cmd_waitp(struct pline *pl, char *res, size_t cap) |
| 1060 | { |
| 1061 | if (!pl || !pl->live || (pl->epoch != M.epoch && !MUT(MUT_NO_EPOCH))) { snprintf(res, cap, "STALE"); return; } |
| 1062 | for (int s = 0; s < pl->n; s++) |
| 1063 | if (pl->task[s] >= 0 && M.tasks[pl->task[s]].state == T_RUNNING) { snprintf(res, cap, "WOULD_BLOCK"); return; } |
| 1064 | for (int s = 0; s < pl->n; s++) { |
| 1065 | if (pl->task[s] < 0) continue; |
| 1066 | unsigned ti = (unsigned)pl->task[s]; |
| 1067 | pl->outcome[s] = M.tasks[ti].outcome; |
| 1068 | pl->status[s] = M.tasks[ti].status; |
| 1069 | task_release(ti); |
| 1070 | pl->task[s] = -1; |
| 1071 | } |
| 1072 | char base[512], out[1024]; |
| 1073 | int last = pl->status[pl->n - 1]; |
| 1074 | pl_result(base, sizeof base, pl, E_OK, last < 0 ? 1 : last); |
| 1075 | q_out(out, sizeof out, pl->sink, pl->sinklen); |
| 1076 | snprintf(res, cap, "%s out=%s", base, out); |
| 1077 | pl->live = 0; |
| 1078 | } |
| 1079 | |
| 1080 | /* ---------------- one vector file ---------------- */ |
| 1081 | |
| 1082 | static void exec_line(char **tok, int n, char *res, size_t cap) |
| 1083 | { |
| 1084 | static uint8_t bufs[2][CAP_BYTES * 2]; /* one per nesting level: a peer action runs inside bread / bwrite */ |
| 1085 | static int depth; |
| 1086 | if (depth > 1) { bad("a peer action cannot block"); return; } |
| 1087 | uint8_t *buf = bufs[depth]; |
| 1088 | const size_t bufcap = sizeof bufs[0]; |
| 1089 | const char *c = tok[0]; |
| 1090 | size_t got = 0; |
| 1091 | int e; |
| 1092 | res[0] = 0; |
| 1093 | #define NEED(k) do { if (n < (k)) { bad("missing argument"); return; } } while (0) |
| 1094 | if (!strcmp(c, "create")) { |
| 1095 | NEED(4); |
| 1096 | unsigned t = tbl(tok[3]); |
| 1097 | if (t > 1) { bad("table"); return; } |
| 1098 | struct href *r = hname(tok[1], 1), *w = hname(tok[2], 1); |
| 1099 | if (!r || !w) { bad("names"); return; } |
| 1100 | struct href nr = *r, nw = *w; |
| 1101 | e = op_create(t, &nr, &nw); |
| 1102 | if (!e) { *r = nr; *w = nw; } |
| 1103 | snprintf(res, cap, "%s", k_err[e]); |
| 1104 | } else if (!strcmp(c, "write") || !strcmp(c, "fill") || !strcmp(c, "bwrite")) { |
| 1105 | NEED(3); |
| 1106 | struct href *h = hname(tok[1], 0); |
| 1107 | size_t len = 0; |
| 1108 | if (!strcmp(c, "fill")) { |
| 1109 | NEED(4); |
| 1110 | len = !strcmp(tok[2], "cap") ? CAP_BYTES : strtoul(tok[2], 0, 10); |
| 1111 | if (len > bufcap) { bad("fill"); return; } |
| 1112 | memset(buf, (int)strtoul(tok[3], 0, 16), len); |
| 1113 | } else if (unq(tok[2], buf, bufcap, &len)) { bad("bytes"); return; } |
| 1114 | unsigned t = h ? h->table : 0; |
| 1115 | e = op_write(t, h, buf, len, &got); |
| 1116 | if (!strcmp(c, "bwrite")) { |
| 1117 | NEED(4); |
| 1118 | if (e == E_WOULD_BLOCK) { |
| 1119 | if (strcmp(tok[3], "none")) { |
| 1120 | char pr[256]; |
| 1121 | depth++; |
| 1122 | exec_line(tok + 3, n - 3, pr, sizeof pr); |
| 1123 | depth--; |
| 1124 | if (strncmp(pr, "OK", 2)) { bad("peer action did not return OK"); return; } |
| 1125 | } |
| 1126 | e = op_write(t, h, buf, len, &got); |
| 1127 | if (e == E_WOULD_BLOCK) e = E_INTERRUPTED; /* stalled peer: the deadline stand-in */ |
| 1128 | } |
| 1129 | } |
| 1130 | if (e == E_OK) snprintf(res, cap, "OK n=%zu", got); |
| 1131 | else snprintf(res, cap, "%s", k_err[e]); |
| 1132 | } else if (!strcmp(c, "read") || !strcmp(c, "bread") || !strcmp(c, "skim")) { |
| 1133 | NEED(3); |
| 1134 | struct href *h = hname(tok[1], 0); |
| 1135 | size_t max = strtoul(tok[2], 0, 10); |
| 1136 | if (max > bufcap) { bad("max"); return; } |
| 1137 | unsigned t = h ? h->table : 0; |
| 1138 | e = op_read(t, h, buf, max, &got); |
| 1139 | if (!strcmp(c, "bread")) { |
| 1140 | NEED(4); |
| 1141 | if (e == E_WOULD_BLOCK) { |
| 1142 | if (strcmp(tok[3], "none")) { |
| 1143 | char pr[256]; |
| 1144 | depth++; |
| 1145 | exec_line(tok + 3, n - 3, pr, sizeof pr); |
| 1146 | depth--; |
| 1147 | if (strncmp(pr, "OK", 2)) { bad("peer action did not return OK"); return; } |
| 1148 | } |
| 1149 | e = op_read(t, h, buf, max, &got); |
| 1150 | if (e == E_WOULD_BLOCK) e = E_INTERRUPTED; |
| 1151 | } |
| 1152 | } |
| 1153 | if (e == E_OK && got && !strcmp(c, "skim")) snprintf(res, cap, "OK n=%zu", got); |
| 1154 | else if (e == E_OK && got) { |
| 1155 | static char q[CAP_BYTES * 8]; |
| 1156 | q_out(q, sizeof q, buf, got); |
| 1157 | snprintf(res, cap, "OK n=%zu data=%s", got, q); |
| 1158 | } else if (e == E_OK) snprintf(res, cap, "OK n=0"); |
| 1159 | else snprintf(res, cap, "%s", k_err[e]); |
| 1160 | } else if (!strcmp(c, "close") || !strcmp(c, "revoke") || !strcmp(c, "cancel")) { |
| 1161 | NEED(2); |
| 1162 | struct href *h = hname(tok[1], 0); |
| 1163 | unsigned t = h ? h->table : 0; |
| 1164 | e = !strcmp(c, "close") ? op_close(t, h) : !strcmp(c, "revoke") ? op_revoke(t, h) : op_cancel(t, h); |
| 1165 | snprintf(res, cap, "%s", k_err[e]); |
| 1166 | } else if (!strcmp(c, "dup")) { |
| 1167 | NEED(5); |
| 1168 | struct href *h = hname(tok[1], 0), *o = hname(tok[2], 1); |
| 1169 | unsigned to = tbl(tok[3]), r = rights_of(tok[4]); |
| 1170 | if (!o || to > 1 || r > 0xFF) { bad("dup"); return; } |
| 1171 | struct href no = *o; |
| 1172 | e = op_dup(h ? h->table : 0, h, to, r, &no); |
| 1173 | if (!e) *o = no; |
| 1174 | snprintf(res, cap, "%s", k_err[e]); |
| 1175 | } else if (!strcmp(c, "release") || !strcmp(c, "sweep")) { |
| 1176 | /* section 12 cleanup of one table; `sweep` is the same without the adapter-specific count */ |
| 1177 | NEED(2); |
| 1178 | unsigned t = tbl(tok[1]), k = 0; |
| 1179 | if (t > 1) { bad("table"); return; } |
| 1180 | for (unsigned i = 0; i < NSLOTS; i++) |
| 1181 | if (M.tables[t].s[i].live) { slot_release(t, i); k++; } |
| 1182 | if (!strcmp(c, "sweep")) snprintf(res, cap, "OK"); |
| 1183 | else snprintf(res, cap, "OK n=%u", k); |
| 1184 | } else if (!strcmp(c, "saturate")) { |
| 1185 | /* saturate H T last=D: duplicate H (its data right only) into T until LIMIT; D names the last duplicate */ |
| 1186 | NEED(4); |
| 1187 | struct href *h = hname(tok[1], 0), *d = strncmp(tok[3], "last=", 5) ? 0 : hname(tok[3] + 5, 1); |
| 1188 | unsigned to = tbl(tok[2]); |
| 1189 | if (!h || !d || to > 1 || !M.tables[h->table].s[h->slot].live) { bad("saturate"); return; } |
| 1190 | unsigned dr = M.tables[h->table].s[h->slot].end == 0 ? R_READ : R_WRITE; |
| 1191 | struct href nd; |
| 1192 | int made = 0; |
| 1193 | for (;;) { |
| 1194 | nd = *d; /* keeps the name */ |
| 1195 | e = op_dup(h->table, h, to, dr, &nd); |
| 1196 | if (e) break; |
| 1197 | *d = nd; |
| 1198 | made = 1; |
| 1199 | } |
| 1200 | if (!made) { bad("saturate made no duplicate"); return; } |
| 1201 | snprintf(res, cap, "%s", k_err[e]); |
| 1202 | } else if (!strcmp(c, "exhaust")) { |
| 1203 | NEED(3); |
| 1204 | unsigned t = tbl(tok[1]); |
| 1205 | unsigned min = (unsigned)strtoul(tok[2] + (strncmp(tok[2], "min=", 4) ? 0 : 4), 0, 10); |
| 1206 | struct href rr[NPIPES + 1], ww[NPIPES + 1]; |
| 1207 | unsigned k = 0, before = 0; |
| 1208 | for (unsigned i = 0; i < NSLOTS; i++) before += (unsigned)M.tables[t].s[i].live; |
| 1209 | int last = E_OK; |
| 1210 | while (k <= NPIPES) { |
| 1211 | memset(&rr[k], 0, sizeof rr[k]); |
| 1212 | memset(&ww[k], 0, sizeof ww[k]); |
| 1213 | last = op_create(t, &rr[k], &ww[k]); |
| 1214 | if (last) break; |
| 1215 | k++; |
| 1216 | } |
| 1217 | unsigned live_slots = 0; |
| 1218 | for (unsigned i = 0; i < NSLOTS; i++) live_slots += (unsigned)M.tables[t].s[i].live; |
| 1219 | int leak = live_slots != before + 2 * k; /* the refused create left nothing allocated (ST9) */ |
| 1220 | for (unsigned i = 0; i < k; i++) { op_close(t, &rr[i]); op_close(t, &ww[i]); } |
| 1221 | if (k < min) snprintf(res, cap, "TOO_FEW n=%u", k); |
| 1222 | else if (leak) snprintf(res, cap, "LEAK"); |
| 1223 | else snprintf(res, cap, "%s", k_err[last]); |
| 1224 | } else if (!strcmp(c, "frame")) { |
| 1225 | NEED(3); |
| 1226 | unsigned t = tbl(tok[1]); |
| 1227 | uint64_t f[16]; |
| 1228 | size_t nc = 0, slen = 0; |
| 1229 | int has = strcmp(tok[2], "-") != 0; |
| 1230 | if (has && unq(tok[2], buf, bufcap, &slen)) { bad("slice"); return; } |
| 1231 | for (int i = 3; i < n; i++) { |
| 1232 | if (nc == 16) { bad("frame too long"); return; } |
| 1233 | if (tok[i][0] == '$') { |
| 1234 | struct href *h = hname(tok[i] + 1, 0); |
| 1235 | f[nc++] = h ? ((uint64_t)h->gen << 32 | (h->slot + 1)) : 0; |
| 1236 | } else f[nc++] = strtoull(tok[i], 0, 16); |
| 1237 | } |
| 1238 | static const unsigned nargs[6] = {0, 0, 2, 2, 1, 3}; |
| 1239 | int ef; |
| 1240 | if (nc < 4 || f[0] != FRAME_MAGIC) ef = E_INVALID_ARG; |
| 1241 | else if (f[1] != 1 && !MUT(MUT_FRAME_VERSION)) ef = E_NOT_SUPPORTED; |
| 1242 | else if (f[3] != 0 && !MUT(MUT_FRAME_RESERVED)) ef = E_INVALID_ARG; |
| 1243 | else if (f[2] == 0 || f[2] > 5) ef = E_NOT_SUPPORTED; /* 6 to 15 are assigned but not in this model */ |
| 1244 | else { |
| 1245 | unsigned op = (unsigned)f[2]; |
| 1246 | struct href fh; |
| 1247 | memset(&fh, 0, sizeof fh); |
| 1248 | if (op != 1 && nc > 4) { |
| 1249 | fh.table = t; |
| 1250 | fh.gen = (uint32_t)(f[4] >> 32); |
| 1251 | fh.slot = (unsigned)(f[4] & 0xFFFFFFFFu) - 1u; |
| 1252 | fh.epoch = M.epoch; |
| 1253 | for (unsigned i = 0; i < M.nh; i++) /* the cell carries no epoch; the model's names do */ |
| 1254 | if (M.h[i].table == t && M.h[i].slot == fh.slot && M.h[i].gen == fh.gen) fh.epoch = M.h[i].epoch; |
| 1255 | } |
| 1256 | if (nc != 4 + nargs[op] || (op != 1 && fh.gen == 0)) ef = E_INVALID_ARG; |
| 1257 | else if (op == 1) { |
| 1258 | struct href a, b; |
| 1259 | memset(&a, 0, sizeof a); |
| 1260 | memset(&b, 0, sizeof b); |
| 1261 | ef = op_create(t, &a, &b); |
| 1262 | /* the new ends are unnamed: a vector releases them with `release` */ |
| 1263 | } else if (op == 2 || op == 3) { |
| 1264 | if (f[5] & ~1ull) ef = E_INVALID_ARG; |
| 1265 | else ef = op == 2 ? op_read(t, &fh, buf, slen, &got) : op_write(t, &fh, buf, slen, &got); |
| 1266 | } else if (op == 4) ef = op_close(t, &fh); |
| 1267 | else { |
| 1268 | if ((f[5] & ~0x3Full) || f[6] > 1) ef = E_INVALID_ARG; |
| 1269 | else { |
| 1270 | struct href o; |
| 1271 | memset(&o, 0, sizeof o); |
| 1272 | ef = op_dup(t, &fh, f[6] ? 1 - t : t, (unsigned)f[5], &o); |
| 1273 | if (!ef) { /* register the anonymous handle so `release` can clean it */ } |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | if (ef == E_OK && (f[2] == 2 || f[2] == 3)) snprintf(res, cap, "OK n=%zu", got); |
| 1278 | else snprintf(res, cap, "%s", k_err[ef]); |
| 1279 | } else if (!strcmp(c, "cap")) { |
| 1280 | NEED(2); |
| 1281 | if (M.ncaps == 16 || M.domain == 0) { bad("cap needs a domain header"); return; } |
| 1282 | struct cap *k = &M.caps[M.ncaps]; |
| 1283 | memset(k, 0, sizeof *k); |
| 1284 | snprintf(k->name, sizeof k->name, "%s", tok[1]); |
| 1285 | k->index = 3 + M.ncaps; |
| 1286 | k->gen = M.domain == 1 ? 7 : 0x100000007ull; |
| 1287 | for (int i = 2; i < n; i++) |
| 1288 | if (!strncmp(tok[i], "gen=", 4)) k->gen = strtoull(tok[i] + 4, 0, 0); |
| 1289 | if (M.domain == 1 && k->gen > 0xFFFFFFFFull) { bad("domain-1 authority cannot mint a 64-bit generation"); return; } |
| 1290 | M.ncaps++; |
| 1291 | snprintf(res, cap, "OK"); |
| 1292 | } else if (!strcmp(c, "revokecap")) { |
| 1293 | NEED(2); |
| 1294 | struct cap *k = cname(tok[1]); |
| 1295 | if (!k) { bad("cap"); return; } |
| 1296 | k->revoked = 1; |
| 1297 | snprintf(res, cap, "OK"); |
| 1298 | } else if (!strcmp(c, "bind")) { |
| 1299 | NEED(3); |
| 1300 | if (M.nb == 16) { bad("bindings"); return; } |
| 1301 | struct binding *b = &M.b[M.nb]; |
| 1302 | memset(b, 0, sizeof *b); |
| 1303 | snprintf(b->name, sizeof b->name, "%s", tok[1]); |
| 1304 | if (!strcmp(tok[2], "none")) b->none = 1; |
| 1305 | else { |
| 1306 | if (strncmp(tok[2], "cap=", 4) || !cname(tok[2] + 4)) { bad("bind cap="); return; } |
| 1307 | const struct cap *k = cname(tok[2] + 4); |
| 1308 | b->domain = M.domain; |
| 1309 | b->index = k->index; |
| 1310 | b->gen = k->gen; |
| 1311 | for (int i = 3; i < n; i++) { |
| 1312 | if (!strncmp(tok[i], "gen=", 4)) b->gen = strtoull(tok[i] + 4, 0, 0); |
| 1313 | else if (!strncmp(tok[i], "domain=", 7)) b->domain = (unsigned)strtoul(tok[i] + 7, 0, 0); |
| 1314 | else if (!strncmp(tok[i], "index=", 6)) b->index = (unsigned)strtoul(tok[i] + 6, 0, 0); |
| 1315 | else { bad("bind option"); return; } |
| 1316 | } |
| 1317 | } |
| 1318 | M.nb++; |
| 1319 | snprintf(res, cap, "OK"); |
| 1320 | } else if (!strcmp(c, "spawn")) { |
| 1321 | /* spawn T <program> [in=H|-] [out=H|-] bind=B */ |
| 1322 | NEED(4); |
| 1323 | struct tref *r = tname(tok[1], 1); |
| 1324 | struct task proto; |
| 1325 | memset(&proto, 0, sizeof proto); |
| 1326 | int i = 2, used = 1; |
| 1327 | if (!strcmp(tok[2], "emit")) { |
| 1328 | if (unq(tok[3], proto.data, sizeof proto.data, &proto.dlen)) { bad("emit"); return; } |
| 1329 | proto.prog = P_EMIT; |
| 1330 | used = 2; |
| 1331 | } else if (parse_prog(tok + 2, n - 2, &proto, &used)) { bad("program"); return; } |
| 1332 | i += used; |
| 1333 | struct href *in = 0, *out = 0; |
| 1334 | const struct binding *b = 0; |
| 1335 | for (; i < n; i++) { |
| 1336 | if (!strncmp(tok[i], "in=", 3)) in = strcmp(tok[i] + 3, "-") ? hname(tok[i] + 3, 0) : 0; |
| 1337 | else if (!strncmp(tok[i], "out=", 4)) out = strcmp(tok[i] + 4, "-") ? hname(tok[i] + 4, 0) : 0; |
| 1338 | else if (!strncmp(tok[i], "bind=", 5)) b = bname(tok[i] + 5); |
| 1339 | else { bad("spawn option"); return; } |
| 1340 | } |
| 1341 | if (!r) { bad("task name"); return; } |
| 1342 | unsigned ip = 0, ie = 0, op = 0, oe = 0; |
| 1343 | e = E_OK; |
| 1344 | if (in) { |
| 1345 | e = resolve(in->table, in, 0, -1, 0, &ip, &ie); /* the end decides, ST6.1 */ |
| 1346 | if (!e && ie != 0) e = E_INVALID_ARG; |
| 1347 | } |
| 1348 | if (!e && out) { |
| 1349 | e = resolve(out->table, out, 0, -1, 0, &op, &oe); |
| 1350 | if (!e && oe != 1) e = E_INVALID_ARG; |
| 1351 | } |
| 1352 | if (!e) e = bind_effect(b); |
| 1353 | unsigned ti = 0; |
| 1354 | if (!e) e = task_alloc(&ti); |
| 1355 | if (!e) { |
| 1356 | struct task *k = &M.tasks[ti]; |
| 1357 | uint32_t g = k->gen; |
| 1358 | *k = proto; |
| 1359 | k->gen = g; |
| 1360 | k->epoch = M.epoch; |
| 1361 | k->state = T_RUNNING; |
| 1362 | k->pipeline = -1; |
| 1363 | k->in_slot = in ? task_take(ti, ip, 0) : -1; |
| 1364 | k->out_slot = out ? task_take(ti, op, 1) : -1; |
| 1365 | k->intent_open = 1; |
| 1366 | M.open_intents++; |
| 1367 | r->idx = ti; |
| 1368 | r->gen = g; |
| 1369 | r->epoch = M.epoch; |
| 1370 | } |
| 1371 | snprintf(res, cap, "%s", k_err[e]); |
| 1372 | } else if (!strcmp(c, "run")) { |
| 1373 | sched_run(); |
| 1374 | snprintf(res, cap, "OK"); |
| 1375 | } else if (!strcmp(c, "wait") || !strcmp(c, "state") || !strcmp(c, "tcancel") || !strcmp(c, "lose")) { |
| 1376 | NEED(2); |
| 1377 | unsigned ti; |
| 1378 | e = task_lookup(tname(tok[1], 0), &ti); |
| 1379 | if (e) { snprintf(res, cap, "%s", k_err[e]); return; } |
| 1380 | struct task *k = &M.tasks[ti]; |
| 1381 | if (!strcmp(c, "state")) snprintf(res, cap, "OK state=%s", k->state == T_RUNNING ? "RUNNING" : "ENDED"); |
| 1382 | else if (!strcmp(c, "tcancel")) { |
| 1383 | NEED(3); |
| 1384 | snprintf(res, cap, "%s", k_err[task_cancel(ti, !strcmp(tok[2], "terminate"))]); |
| 1385 | } else if (!strcmp(c, "lose")) { |
| 1386 | if (k->state != T_ENDED) { bad("lose needs an ended task"); return; } |
| 1387 | k->lost = 1; |
| 1388 | snprintf(res, cap, "OK"); |
| 1389 | } else if (k->state == T_RUNNING) snprintf(res, cap, "WOULD_BLOCK"); |
| 1390 | else if (k->lost) { |
| 1391 | snprintf(res, cap, "OUTCOME_UNKNOWN status=1 outcome=OUTCOME_UNKNOWN"); |
| 1392 | task_release(ti); |
| 1393 | } else { |
| 1394 | snprintf(res, cap, "OK status=%d outcome=%s", k->status, k_out[k->outcome]); |
| 1395 | if (!MUT(MUT_WAIT_NO_RELEASE)) task_release(ti); |
| 1396 | } |
| 1397 | } else if (!strcmp(c, "launch")) { |
| 1398 | NEED(4); |
| 1399 | cmd_launch(tok, n, res, cap); |
| 1400 | } else if (!strcmp(c, "waitp")) { |
| 1401 | NEED(2); |
| 1402 | cmd_waitp(pname(tok[1], 0), res, cap); |
| 1403 | } else if (!strcmp(c, "cancelp")) { |
| 1404 | NEED(3); |
| 1405 | struct pline *pl = pname(tok[1], 0); |
| 1406 | if (!pl || !pl->live) { snprintf(res, cap, "STALE"); return; } |
| 1407 | for (int s = 0; s < pl->n; s++) |
| 1408 | if (pl->task[s] >= 0) task_cancel((unsigned)pl->task[s], !strcmp(tok[2], "terminate")); |
| 1409 | snprintf(res, cap, "OK"); |
| 1410 | } else if (!strcmp(c, "reset")) { |
| 1411 | /* ABI section 12 cleanup, then a new session_epoch */ |
| 1412 | int list[NTASKS], k = 0; |
| 1413 | for (unsigned i = 0; i < NTASKS; i++) |
| 1414 | list[i] = M.tasks[i].state == T_RUNNING ? (k++, (int)i) : -1; |
| 1415 | cleanup_tasks(list, NTASKS); |
| 1416 | for (unsigned i = 0; i < NTASKS; i++) |
| 1417 | if (M.tasks[i].state != T_FREE) task_release(i); |
| 1418 | for (unsigned t = 0; t < 2; t++) |
| 1419 | for (unsigned i = 0; i < NSLOTS; i++) slot_release(t, i); |
| 1420 | for (unsigned i = 0; i < M.np; i++) M.p[i].live = 0; |
| 1421 | M.epoch++; |
| 1422 | snprintf(res, cap, "OK cancelled=%d", k); |
| 1423 | } else if (!strcmp(c, "crash")) { |
| 1424 | /* the adapter process is gone: nothing is cleaned, nothing records an outcome; its tables restart empty */ |
| 1425 | M.crashed_unknown = M.open_intents; |
| 1426 | for (unsigned i = 0; i < NTASKS; i++) M.tasks[i].state = T_FREE, M.tasks[i].intent_open = 0; |
| 1427 | for (unsigned t = 0; t < NTABLES; t++) memset(&M.tables[t], 0, sizeof M.tables[t]); |
| 1428 | for (unsigned i = 0; i < NPIPES; i++) { |
| 1429 | uint32_t inc = M.pipes[i].inc; |
| 1430 | memset(&M.pipes[i], 0, sizeof M.pipes[i]); |
| 1431 | M.pipes[i].inc = inc; |
| 1432 | } |
| 1433 | for (unsigned i = 0; i < M.np; i++) M.p[i].live = 0; |
| 1434 | M.open_intents = 0; |
| 1435 | M.epoch++; |
| 1436 | snprintf(res, cap, "OK"); |
| 1437 | } else if (!strcmp(c, "recover")) { |
| 1438 | snprintf(res, cap, "OK unknown=%u replayed=%u", MUT(MUT_CRASH_REPLAY) ? 0 : M.crashed_unknown, |
| 1439 | MUT(MUT_CRASH_REPLAY) ? M.crashed_unknown : 0); |
| 1440 | M.crashed_unknown = 0; |
| 1441 | } else if (!strcmp(c, "limits")) { |
| 1442 | snprintf(res, cap, "OK max_pipe_bytes>=65536 max_streams>=16 max_tasks>=8"); |
| 1443 | if (!(CAP_BYTES >= 65536 && NSLOTS >= 16 && NTASKS >= 8)) snprintf(res, cap, "LIMIT"); |
| 1444 | } else { |
| 1445 | bad("unknown command"); |
| 1446 | } |
| 1447 | #undef NEED |
| 1448 | } |
| 1449 | |
| 1450 | static int check_items(char **tok, int n) |
| 1451 | { |
| 1452 | for (int i = 1; i < n; i++) { |
| 1453 | if (!strcmp(tok[i], "empty")) { |
| 1454 | for (unsigned t = 0; t < NTABLES; t++) |
| 1455 | for (unsigned s = 0; s < NSLOTS; s++) |
| 1456 | if (M.tables[t].s[s].live) return 0; |
| 1457 | for (unsigned p = 0; p < NPIPES; p++) |
| 1458 | if (M.pipes[p].live) return 0; |
| 1459 | for (unsigned k = 0; k < NTASKS; k++) |
| 1460 | if (M.tasks[k].state != T_FREE) return 0; |
| 1461 | if (M.open_intents || M.crashed_unknown) return 0; |
| 1462 | } else if (!strncmp(tok[i], "pipes=", 6)) { |
| 1463 | unsigned live = 0; |
| 1464 | for (unsigned p = 0; p < NPIPES; p++) live += (unsigned)M.pipes[p].live; |
| 1465 | if (live != strtoul(tok[i] + 6, 0, 10)) return 0; |
| 1466 | } else { |
| 1467 | bad("unknown check item"); |
| 1468 | return 0; |
| 1469 | } |
| 1470 | } |
| 1471 | return 1; |
| 1472 | } |
| 1473 | |
| 1474 | static int run_file(const char *path, const char *base, int *nfail) |
| 1475 | { |
| 1476 | FILE *f = fopen(path, "r"); |
| 1477 | if (!f) { fprintf(stderr, "cannot open %s\n", path); return 1; } |
| 1478 | model_reset(); |
| 1479 | g_file = base; |
| 1480 | g_line = 0; |
| 1481 | g_fail = 0; |
| 1482 | static char line[4096]; |
| 1483 | char res[CAP_BYTES * 8 + 64]; |
| 1484 | int have_case = 0, have_kind = 0, have_exec = 0, have_needs = 0, have_domain = 0, last_check_empty = 0, mism = 0; |
| 1485 | while (fgets(line, sizeof line, f)) { |
| 1486 | g_line++; |
| 1487 | size_t L = strlen(line); |
| 1488 | while (L && (line[L - 1] == '\n' || line[L - 1] == '\r')) line[--L] = 0; |
| 1489 | char *s = line; |
| 1490 | while (*s == ' ') s++; |
| 1491 | if (!*s || *s == '#') continue; |
| 1492 | char *arrow = strstr(s, " => "); |
| 1493 | char *want = 0; |
| 1494 | if (arrow) { *arrow = 0; want = arrow + 4; } |
| 1495 | char *tok[64]; |
| 1496 | int n = split(s, tok, 64); |
| 1497 | if (n <= 0) { bad("cannot split"); continue; } |
| 1498 | if (!strcmp(tok[0], "case")) { have_case = 1; continue; } |
| 1499 | if (!strcmp(tok[0], "kind")) { have_kind = n == 2 && (!strcmp(tok[1], "positive") || !strcmp(tok[1], "negative")); continue; } |
| 1500 | if (!strcmp(tok[0], "exec")) { have_exec = n == 2 && (!strcmp(tok[1], "host-logic") || !strcmp(tok[1], "native-qemu")); continue; } |
| 1501 | if (!strcmp(tok[0], "needs")) { have_needs = n >= 2; continue; } |
| 1502 | if (!strcmp(tok[0], "domain")) { |
| 1503 | have_domain = n == 2; |
| 1504 | M.domain = !strcmp(tok[1], "any") ? 0 : (unsigned)atoi(tok[1]); |
| 1505 | continue; |
| 1506 | } |
| 1507 | if (!have_case || !have_kind || !have_exec || !have_needs || !have_domain) { bad("header incomplete before first step"); break; } |
| 1508 | if (!strcmp(tok[0], "check")) { |
| 1509 | last_check_empty = 0; |
| 1510 | for (int i = 1; i < n; i++) last_check_empty |= !strcmp(tok[i], "empty"); |
| 1511 | if (!check_items(tok, n)) { |
| 1512 | printf("MISMATCH %s:%u check failed\n", base, g_line); |
| 1513 | mism = 1; |
| 1514 | } |
| 1515 | continue; |
| 1516 | } |
| 1517 | last_check_empty = 0; |
| 1518 | if (!want) { bad("step without ' => '"); continue; } |
| 1519 | exec_line(tok, n, res, sizeof res); |
| 1520 | if (strcmp(res, want)) { |
| 1521 | printf("MISMATCH %s:%u got \"%.200s\" want \"%.200s\"\n", base, g_line, res, want); |
| 1522 | mism = 1; |
| 1523 | } |
| 1524 | } |
| 1525 | fclose(f); |
| 1526 | if (!last_check_empty) bad("a vector must end with 'check empty'"); |
| 1527 | if (g_fail || mism) { (*nfail)++; return 1; } |
| 1528 | printf("PASS %s\n", base); |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | static int cmpstr(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); } |
| 1533 | |
| 1534 | int main(int argc, char **argv) |
| 1535 | { |
| 1536 | const char *dir = 0, *corpus_pin = "unpinned", *tc_pin = "unpinned"; |
| 1537 | for (int i = 1; i < argc; i++) { |
| 1538 | if (!strcmp(argv[i], "--corpus") && i + 1 < argc) dir = argv[++i]; |
| 1539 | else if (!strcmp(argv[i], "--pin") && i + 1 < argc) { |
| 1540 | const char *p = argv[++i]; |
| 1541 | if (!strncmp(p, "corpus=", 7)) corpus_pin = p + 7; |
| 1542 | else if (!strncmp(p, "toolchain=", 10)) tc_pin = p + 10; |
| 1543 | } else { fprintf(stderr, "usage: osh_sv_ref --corpus DIR [--pin corpus=SHA] [--pin toolchain=TEXT]\n"); return 2; } |
| 1544 | } |
| 1545 | if (!dir) { fprintf(stderr, "usage: osh_sv_ref --corpus DIR\n"); return 2; } |
| 1546 | DIR *d = opendir(dir); |
| 1547 | if (!d) { fprintf(stderr, "cannot open %s\n", dir); return 2; } |
| 1548 | char *names[256]; |
| 1549 | int nn = 0; |
| 1550 | struct dirent *de; |
| 1551 | while ((de = readdir(d)) && nn < 256) { |
| 1552 | size_t L = strlen(de->d_name); |
| 1553 | if (L > 4 && (de->d_name[0] == 's' || de->d_name[0] == 't') && !strcmp(de->d_name + L - 4, ".vec")) |
| 1554 | names[nn++] = strdup(de->d_name); |
| 1555 | } |
| 1556 | closedir(d); |
| 1557 | qsort(names, (size_t)nn, sizeof names[0], cmpstr); |
| 1558 | printf("OSHSV1_RUN impl=reference-model mutant=%d corpus=%s toolchain=%s target=host\n", SV_MUTANT, corpus_pin, tc_pin); |
| 1559 | int nfail = 0; |
| 1560 | for (int i = 0; i < nn; i++) { |
| 1561 | char path[4096]; |
| 1562 | snprintf(path, sizeof path, "%s/%s", dir, names[i]); |
| 1563 | run_file(path, names[i], &nfail); |
| 1564 | free(names[i]); |
| 1565 | } |
| 1566 | printf("OSHSV1_CONFORMANCE impl=reference-model vectors=%d pass=%d fail=%d not_run=0\n", nn, nn - nfail, nfail); |
| 1567 | return nn > 0 && nfail == 0 ? 0 : 1; |
| 1568 | } |