Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/tests/test_pipe.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / tests / test_pipe.c` 1296 lines ยท 47612 bytes Raw Blame History 1 /* test_pipe.c -- host tests for core/pipe.c (aienos-mv#428): the fixture corpus in tests/fixtures/pipe/ (format in its 2 * README.md), then producer / consumer transfers through the scheduler (core/pipe_drive.h), revocation mid-stream, 3 * epoch staleness and the versioned op table, then pthreads (concurrent read/write, close while parked, shared-pool 4 * create/close). Every case ends with both capability tables and the pipe pool empty. */ 5 #define _GNU_SOURCE 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include "ck_test.h" 18 #include "pipe.h" 19 #include "pipe_drive.h" 20 21 #ifndef PIPE_FIXTURE_DIR 22 #define PIPE_FIXTURE_DIR "native/kernel/tests/fixtures/pipe" 23 #endif 24 25 static struct ck_pipe_pool pool; 26 static struct ck_cap_table ta, tb; 27 28 static int expect(int ok, const char *file, int line, const char *what) 29 { 30 ck_t_run++; 31 if (!ok) { 32 ck_t_fail++; 33 printf(" FAIL %s:%d: %s\n", file, line, what); 34 } 35 return ok; 36 } 37 38 /* ---- fixture runner ---- */ 39 #define TOK_MAX 24 40 #define BYTES_MAX 256 41 struct tok { 42 char s[64]; 43 int quoted; 44 uint8_t b[BYTES_MAX]; 45 size_t n; 46 }; 47 struct name { 48 char s[16]; 49 struct ck_cap_table *t; 50 struct ck_handle h; 51 }; 52 static struct name names[32]; 53 static unsigned nnames; 54 static const char *cur_file; 55 static int cur_line; 56 57 /* the wait hook's scripted peer: first wait runs the action, every later wait gives up (a stalled peer) */ 58 static struct tok *act; 59 static int nact, wait_calls; 60 static unsigned act_err; 61 62 static int hexval(int c) 63 { 64 return c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : c >= 'A' && c <= 'F' ? c - 'A' + 10 : -1; 65 } 66 67 static int tokenize(const char *line, struct tok *t, int max) 68 { 69 int n = 0; 70 const char *p = line; 71 while (*p) { 72 while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') 73 p++; 74 if (!*p || *p == '#') 75 break; 76 if (n == max) 77 return -1; 78 struct tok *k = &t[n++]; 79 memset(k, 0, sizeof *k); 80 if (*p == '"') { 81 k->quoted = 1; 82 p++; 83 while (*p && *p != '"') { 84 int c = (unsigned char)*p++; 85 if (c == '\\') { 86 if (*p == 'x' && hexval(p[1]) >= 0 && hexval(p[2]) >= 0) { 87 c = hexval(p[1]) * 16 + hexval(p[2]); 88 p += 3; 89 } else if (*p == '"' || *p == '\\') { 90 c = (unsigned char)*p++; 91 } else { 92 return -1; 93 } 94 } 95 if (k->n == BYTES_MAX) 96 return -1; 97 k->b[k->n++] = (uint8_t)c; 98 } 99 if (*p != '"') 100 return -1; 101 p++; 102 strcpy(k->s, "\"\""); 103 } else { 104 size_t l = 0; 105 while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r' && *p != '"') { 106 if (l + 1 >= sizeof k->s) 107 return -1; 108 k->s[l++] = *p++; 109 } 110 } 111 } 112 return n; 113 } 114 115 static struct ck_cap_table *table_of(const char *s) 116 { 117 return !strcmp(s, "A") ? &ta : !strcmp(s, "B") ? &tb : 0; 118 } 119 120 static struct name *find(const char *s) 121 { 122 for (unsigned i = 0; i < nnames; i++) 123 if (!strcmp(names[i].s, s)) 124 return &names[i]; 125 return 0; 126 } 127 128 static void bind(const char *s, struct ck_cap_table *t, struct ck_handle h) 129 { 130 struct name *n = find(s); 131 if (!n && nnames < 32) 132 n = &names[nnames++]; 133 if (!n) 134 return; 135 snprintf(n->s, sizeof n->s, "%s", s); 136 n->t = t; 137 n->h = h; 138 } 139 140 static int parse_err(const char *s) 141 { 142 for (unsigned i = 0; i < OSH_E_COUNT; i++) 143 if (!strcmp(s, osh_err_label(i))) 144 return (int)i; 145 return -1; 146 } 147 148 static int parse_rights(const char *s, unsigned *r) 149 { 150 *r = 0; 151 for (; *s; s++) { 152 switch (*s) { 153 case 'r': *r |= CK_R_READ; break; 154 case 'w': *r |= CK_R_WRITE; break; 155 case 'm': *r |= CK_R_MAP; break; 156 case 'g': *r |= CK_R_GRANT; break; 157 case 'd': *r |= CK_R_DERIVE; break; 158 case 'v': *r |= CK_R_REVOKE; break; 159 default: return -1; 160 } 161 } 162 return 0; 163 } 164 165 static uint8_t big[70000], rbig[70000]; 166 167 /* Runs one data or control command (also used for the wait hook's action). *n and the read bytes are returned. */ 168 static int run_cmd(struct tok *c, int nc, unsigned *err, size_t *n, const uint8_t **data, int *handled_bind) 169 { 170 *n = 0; 171 *data = 0; 172 *handled_bind = 0; 173 struct name *h = nc > 1 ? find(c[1].s) : 0; 174 if (!strcmp(c[0].s, "write") && nc == 3 && h && c[2].quoted) { 175 *err = ck_stream_write(&pool, h->t, h->h, c[2].b, c[2].n, 0, n); 176 } else if (!strcmp(c[0].s, "fill") && nc == 4 && h) { 177 size_t cnt = strtoul(c[2].s, 0, 10); 178 if (cnt > sizeof big) 179 return -1; 180 memset(big, (int)strtoul(c[3].s, 0, 16), cnt); 181 *err = ck_stream_write(&pool, h->t, h->h, big, cnt, 0, n); 182 } else if (!strcmp(c[0].s, "read") && nc == 3 && h) { 183 size_t max = strtoul(c[2].s, 0, 10); 184 if (max > sizeof rbig) 185 return -1; 186 *err = ck_stream_read(&pool, h->t, h->h, rbig, max, 0, n); 187 *data = rbig; 188 } else if (!strcmp(c[0].s, "close") && nc == 2 && h) { 189 *err = ck_stream_close(&pool, h->t, h->h); 190 } else if (!strcmp(c[0].s, "revoke") && nc == 2 && h) { 191 *err = ck_pipe_revoke(&pool, h->t, h->h); 192 } else if (!strcmp(c[0].s, "cancel") && nc == 2 && h) { 193 *err = ck_pipe_cancel(&pool, h->t, h->h); 194 } else { 195 return -1; 196 } 197 return 0; 198 } 199 200 static unsigned vec_wait(void *ctx, unsigned pipe, unsigned end) 201 { 202 (void)ctx; 203 (void)pipe; 204 (void)end; 205 if (wait_calls++ > 0 || !act) 206 return OSH_E_INTERRUPTED; 207 if (nact == 1 && !strcmp(act[0].s, "none")) 208 return OSH_E_OK; 209 size_t n; 210 const uint8_t *d; 211 int hb; 212 if (run_cmd(act, nact, &act_err, &n, &d, &hb) != 0) 213 act_err = 0xff; 214 return OSH_E_OK; 215 } 216 static void vec_wake(void *ctx, unsigned pipe, unsigned end) 217 { 218 (void)ctx; 219 (void)pipe; 220 (void)end; 221 } 222 static const struct ck_pipe_hooks vec_hooks = { 0, vec_wait, vec_wake, 0 }; 223 224 static uint64_t stat_of(const char *k, int *ok) 225 { 226 const struct ck_pipe_stats *s = &pool.total; 227 *ok = 1; 228 #define S(f) if (!strcmp(k, #f)) return s->f; 229 S(bytes_written) S(bytes_read) S(writes) S(reads) S(max_resident) S(would_block) S(waits) S(wakeups) 230 S(cancellations) S(revocations) S(broken_pipe) S(eof) S(refused) 231 #undef S 232 if (!strcmp(k, "pipes")) 233 return pool.live; 234 *ok = 0; 235 return 0; 236 } 237 238 static int all_empty(void) 239 { 240 return pd_table_empty(&ta) && pd_table_empty(&tb) && ck_pipe_pool_empty(&pool); 241 } 242 243 static void run_line(struct tok *t, int nt) 244 { 245 char what[160]; 246 if (!strcmp(t[0].s, "case") || !strcmp(t[0].s, "kind")) 247 return; 248 if (!strcmp(t[0].s, "check")) { 249 for (int i = 1; i < nt; i++) { 250 if (!strcmp(t[i].s, "empty")) { 251 expect(all_empty(), cur_file, cur_line, "check empty: tables and pool hold nothing"); 252 continue; 253 } 254 char key[64]; 255 snprintf(key, sizeof key, "%s", t[i].s); 256 char *eq = strchr(key, '='); 257 int ok = 0; 258 if (eq) { 259 *eq = 0; 260 uint64_t want = strtoull(eq + 1, 0, 10), got = stat_of(key, &ok); 261 snprintf(what, sizeof what, "check %s: want %llu got %llu", key, (unsigned long long)want, 262 (unsigned long long)got); 263 ok = ok && got == want; 264 } else { 265 snprintf(what, sizeof what, "check: bad item %s", t[i].s); 266 } 267 expect(ok, cur_file, cur_line, what); 268 } 269 return; 270 } 271 int arrow = -1; 272 for (int i = 0; i < nt; i++) 273 if (!t[i].quoted && !strcmp(t[i].s, "=>")) 274 arrow = i; 275 if (!expect(arrow > 0 && arrow + 1 < nt, cur_file, cur_line, "line has ' => '")) 276 return; 277 int want = parse_err(t[arrow + 1].s); 278 if (!expect(want >= 0, cur_file, cur_line, "known error name")) 279 return; 280 long want_n = -1; 281 struct tok *want_data = 0; 282 for (int i = arrow + 2; i < nt; i++) { 283 if (!strncmp(t[i].s, "n=", 2)) 284 want_n = strtol(t[i].s + 2, 0, 10); 285 else if (!strcmp(t[i].s, "data=") && i + 1 < nt && t[i + 1].quoted) 286 want_data = &t[++i]; 287 else 288 expect(0, cur_file, cur_line, "unknown expectation item"); 289 } 290 struct tok *c = t; 291 int nc = arrow; 292 unsigned err = 0xff; 293 size_t n = 0; 294 const uint8_t *data = 0; 295 struct name *h = nc > 1 ? find(c[1].s) : 0; 296 act = 0; 297 nact = 0; 298 wait_calls = 0; 299 act_err = 0; 300 if (!strcmp(c[0].s, "create") && nc == 4 && table_of(c[3].s)) { 301 struct ck_handle r, w; 302 err = ck_pipe_create(&pool, table_of(c[3].s), &r, &w); 303 if (err == OSH_E_OK) { 304 bind(c[1].s, table_of(c[3].s), r); 305 bind(c[2].s, table_of(c[3].s), w); 306 } 307 } else if (!strcmp(c[0].s, "dup") && nc == 5 && h && table_of(c[3].s)) { 308 unsigned rights; 309 struct ck_handle o; 310 if (!expect(parse_rights(c[4].s, &rights) == 0, cur_file, cur_line, "rights letters")) 311 return; 312 err = ck_stream_dup(&pool, h->t, h->h, table_of(c[3].s), rights, &o); 313 if (err == OSH_E_OK) 314 bind(c[2].s, table_of(c[3].s), o); 315 } else if (!strcmp(c[0].s, "release") && nc == 2 && table_of(c[1].s)) { 316 n = ck_pipe_release_table(&pool, table_of(c[1].s)); 317 err = OSH_E_OK; 318 } else if ((!strcmp(c[0].s, "bread") || !strcmp(c[0].s, "bwrite")) && nc >= 4 && h) { 319 act = &c[3]; 320 nact = nc - 3; 321 if (c[0].s[1] == 'r') { 322 size_t max = strtoul(c[2].s, 0, 10); 323 if (!expect(max <= sizeof rbig, cur_file, cur_line, "read size")) 324 return; 325 err = ck_stream_read(&pool, h->t, h->h, rbig, max, CK_PIPE_F_BLOCK, &n); 326 data = rbig; 327 } else { 328 err = ck_stream_write(&pool, h->t, h->h, c[2].b, c[2].n, CK_PIPE_F_BLOCK, &n); 329 } 330 expect(act_err == OSH_E_OK, cur_file, cur_line, "the wait hook's action succeeded"); 331 } else if (!strcmp(c[0].s, "frame") && nc >= 3 && table_of(c[1].s)) { 332 uint64_t cells[16]; 333 size_t ncell = 0; 334 for (int i = 3; i < nc && ncell < 16; i++) { 335 if (c[i].s[0] == '$') { 336 struct name *x = find(c[i].s + 1); 337 if (!expect(x != 0, cur_file, cur_line, "frame names a known handle")) 338 return; 339 cells[ncell++] = ck_handle_raw(x->h); 340 } else { 341 cells[ncell++] = strtoull(c[i].s, 0, 16); 342 } 343 } 344 struct ck_pipe_ret ret; 345 struct ck_cap_table *me = table_of(c[1].s); 346 uint8_t slice[BYTES_MAX]; 347 size_t sl = 0; 348 if (c[2].quoted) { 349 memcpy(slice, c[2].b, c[2].n); 350 sl = c[2].n; 351 } 352 err = ck_pipe_dispatch(&pool, me, me == &ta ? &tb : &ta, cells, ncell, sl ? slice : 0, sl, 353 (uintptr_t)slice, (uintptr_t)slice + sizeof slice, &ret); 354 expect(ret.error == err, cur_file, cur_line, "frame result carries the error"); 355 n = (size_t)ret.value; 356 if (want_n < 0) 357 n = 0; 358 } else { 359 int hb; 360 if (!expect(run_cmd(c, nc, &err, &n, &data, &hb) == 0, cur_file, cur_line, "known command")) 361 return; 362 } 363 snprintf(what, sizeof what, "%s: want %s got %s", c[0].s, osh_err_label((unsigned)want), osh_err_label(err)); 364 expect(err == (unsigned)want, cur_file, cur_line, what); 365 if (want_n >= 0) { 366 snprintf(what, sizeof what, "%s: want n=%ld got n=%zu", c[0].s, want_n, n); 367 expect(n == (size_t)want_n, cur_file, cur_line, what); 368 } 369 if (want_data) 370 expect(data && n == want_data->n && !memcmp(data, want_data->b, n), cur_file, cur_line, "data byte for byte"); 371 } 372 373 static int run_file(const char *dir, const char *fname) 374 { 375 char path[512], line[1024]; 376 snprintf(path, sizeof path, "%s/%s", dir, fname); 377 FILE *f = fopen(path, "r"); 378 if (!expect(f != 0, fname, 0, "fixture opens")) 379 return 0; 380 ck_cap_init(&ta, 0x41, CK_CAP_SLOTS); 381 ck_cap_init(&tb, 0x42, CK_CAP_SLOTS); 382 ck_pipe_pool_init(&pool, &vec_hooks); 383 nnames = 0; 384 cur_file = fname; 385 cur_line = 0; 386 int has_case = 0, has_kind = 0, fail_before = ck_t_fail; 387 struct tok t[TOK_MAX]; 388 while (fgets(line, sizeof line, f)) { 389 cur_line++; 390 int nt = tokenize(line, t, TOK_MAX); 391 if (!expect(nt >= 0, fname, cur_line, "line tokenizes")) 392 continue; 393 if (nt == 0) 394 continue; 395 has_case |= !strcmp(t[0].s, "case") && nt == 2; 396 has_kind |= !strcmp(t[0].s, "kind") && nt == 2 && (!strcmp(t[1].s, "positive") || !strcmp(t[1].s, "negative")); 397 run_line(t, nt); 398 } 399 fclose(f); 400 expect(has_case && has_kind, fname, cur_line, "fixture names its case and kind"); 401 expect(all_empty(), fname, cur_line, "end of case: both tables and the pool are empty"); 402 printf(" pipe fixture %s: %s\n", fname, ck_t_fail == fail_before ? "ok" : "FAILED"); 403 return 1; 404 } 405 406 static int cmp_str(const void *a, const void *b) 407 { 408 return strcmp(*(char *const *)a, *(char *const *)b); 409 } 410 411 static void run_fixtures(const char *dir) 412 { 413 DIR *d = opendir(dir); 414 if (!expect(d != 0, dir, 0, "fixture directory opens")) 415 return; 416 char *list[128]; 417 int n = 0; 418 struct dirent *e; 419 while ((e = readdir(d)) && n < 128) { 420 size_t l = strlen(e->d_name); 421 if (l > 4 && !strcmp(e->d_name + l - 4, ".vec")) 422 list[n++] = strdup(e->d_name); 423 } 424 closedir(d); 425 qsort(list, (size_t)n, sizeof list[0], cmp_str); 426 int ran = 0; 427 for (int i = 0; i < n; i++) { 428 ran += run_file(dir, list[i]); 429 free(list[i]); 430 } 431 CHECK(ran >= 20); 432 } 433 434 /* ---- transfers through the scheduler ---- */ 435 static struct pd_world world; 436 static uint8_t sink[200000]; 437 438 static size_t expected_nuls(size_t n) 439 { 440 size_t z = 0; 441 for (size_t i = 0; i < n; i++) 442 z += pd_byte(i) == 0; 443 return z; 444 } 445 446 static void fresh_tables(void) 447 { 448 ck_cap_init(&ta, 0x41, CK_CAP_SLOTS); 449 ck_cap_init(&tb, 0x42, CK_CAP_SLOTS); 450 } 451 452 static void check_full_transfer(const char *label, unsigned flags, size_t cw, size_t cr, unsigned pe, unsigned ce) 453 { 454 fresh_tables(); 455 memset(sink, 0xaa, sizeof sink); 456 pd_run(&world, &pool, &ta, &tb, sink, sizeof sink, cw, cr, flags, pe, ce, 0); 457 const struct ck_pipe_stats *s = &pool.total; 458 printf(" transfer %s: sent=%zu recvd=%zu nul=%zu would_block=%llu waits=%llu wakeups=%llu max_resident=%llu " 459 "prod_steps=%llu cons_steps=%llu ticks=%llu\n", 460 label, world.sent, world.recvd, pd_nul_count(&world), (unsigned long long)s->would_block, 461 (unsigned long long)s->waits, (unsigned long long)s->wakeups, (unsigned long long)s->max_resident, 462 (unsigned long long)world.prod_steps, (unsigned long long)world.cons_steps, 463 (unsigned long long)world.ticks); 464 CHECK(world.setup_err == OSH_E_OK); 465 CHECK(world.prod_err == OSH_E_OK && world.cons_err == OSH_E_OK); 466 CHECK(world.tasks[0].done && world.tasks[1].done); 467 CHECK(world.sent == sizeof sink && world.recvd == sizeof sink); 468 CHECK(pd_mismatches(&world) == 0); /* byte for byte */ 469 CHECK(pd_nul_count(&world) == expected_nuls(sizeof sink)); /* NUL bytes included */ 470 CHECK(pd_nul_count(&world) >= sizeof sink / 5); 471 CHECK(world.eof_seen && s->eof == 1); /* last writer closed -> EOF */ 472 CHECK(s->bytes_written == sizeof sink && s->bytes_read == sizeof sink); 473 CHECK(s->max_resident <= CK_PIPE_CAPACITY); /* bounded memory */ 474 CHECK(s->cancellations == 0 && s->broken_pipe == 0 && s->revocations == 0); 475 CHECK(world.prod_steps > 1 && world.cons_steps > 1); /* real interleaving */ 476 CHECK(all_empty()); 477 } 478 479 static void test_transfers(void) 480 { 481 /* slow consumer, non-blocking: the producer sees WOULD_BLOCK and the buffer reaches capacity */ 482 check_full_transfer("slow-consumer-nonblocking", 0, 8192, 1000, 1, 4); 483 CHECK(pool.total.would_block > 0); 484 CHECK(pool.total.max_resident == CK_PIPE_CAPACITY); 485 CHECK(pool.total.waits == 0); 486 /* slow consumer, blocking: the producer parks and the consumer's reads wake it */ 487 check_full_transfer("slow-consumer-blocking", CK_PIPE_F_BLOCK, 8192, 1000, 1, 4); 488 CHECK(pool.total.waits > 0 && pool.total.wakeups > 0); 489 CHECK(pool.total.max_resident == CK_PIPE_CAPACITY); 490 /* slow producer, blocking: the consumer parks on an empty pipe and the producer's writes wake it */ 491 check_full_transfer("slow-producer-blocking", CK_PIPE_F_BLOCK, 1000, 8192, 4, 1); 492 CHECK(pool.total.waits > 0 && pool.total.wakeups > 0); 493 CHECK(pool.total.max_resident < CK_PIPE_CAPACITY); 494 } 495 496 static void test_revoke_midstream(unsigned flags) 497 { 498 fresh_tables(); 499 memset(sink, 0xaa, sizeof sink); 500 pd_run(&world, &pool, &ta, &tb, sink, sizeof sink, 8192, 1000, flags, 1, 4, 50000); 501 const struct ck_pipe_stats *s = &pool.total; 502 printf(" revoke mid-stream (%s): recvd=%zu sent=%zu reads_after_revoke=%zu prod_err=%s cons_err=%s\n", 503 flags ? "blocking" : "nonblocking", world.recvd, world.sent, world.reads_after_revoke, 504 osh_err_label(world.prod_err), osh_err_label(world.cons_err)); 505 CHECK(world.setup_err == OSH_E_OK); 506 CHECK(world.revoked); 507 CHECK(world.reads_after_revoke == 0); /* no data emitted after revocation */ 508 CHECK(world.recvd >= 50000 && world.recvd < 50000 + 1000); 509 CHECK(s->bytes_read == world.recvd); 510 CHECK(world.cons_err == OSH_E_REVOKED && world.prod_err == OSH_E_REVOKED); 511 CHECK(world.sent < sizeof sink); 512 CHECK(pd_mismatches(&world) == 0); 513 CHECK(s->revocations == 1 && s->eof == 0); 514 CHECK(world.tasks[0].done && world.tasks[1].done); 515 CHECK(all_empty()); 516 } 517 518 static void test_epoch_and_forgery(void) 519 { 520 fresh_tables(); 521 ck_pipe_pool_init(&pool, 0); 522 struct ck_handle r, w, f, g; 523 size_t n = 0; 524 uint8_t b[4] = { 1, 2, 3, 4 }; 525 CHECK(ck_pipe_create(&pool, &ta, &r, &w) == OSH_E_OK); 526 uint32_t epoch = pool.pipes[0].epoch; 527 CHECK(epoch == 1); 528 /* a capability naming another incarnation of the same slot is STALE (section 4.4), and close still releases it */ 529 CHECK(ck_cap_insert(&ta, ck_pipe_resource(0, CK_PIPE_END_WRITE, epoch + 1), CK_R_WRITE, &f) == CK_CAP_OK); 530 CHECK(ck_stream_write(&pool, &ta, f, b, 4, 0, &n) == OSH_E_STALE && n == 0); 531 CHECK(ck_stream_close(&pool, &ta, f) == OSH_E_STALE); 532 CHECK(ck_cap_lookup(&ta, f, 0, 0) == CK_CAP_INVALID); 533 CHECK(pool.pipes[0].writer_refs == 1); /* the live pipe's count is untouched */ 534 /* a capability on something that is not a stream is DENIED and not closed here */ 535 CHECK(ck_cap_insert(&ta, CK_IPC_OBJECT_RESOURCE, CK_R_WRITE, &g) == CK_CAP_OK); 536 CHECK(ck_stream_write(&pool, &ta, g, b, 4, 0, &n) == OSH_E_DENIED); 537 CHECK(ck_stream_close(&pool, &ta, g) == OSH_E_DENIED); 538 CHECK(ck_cap_remove(&ta, g) == CK_CAP_OK); 539 /* no wait hook: a blocking read on an empty pipe returns WOULD_BLOCK */ 540 CHECK(ck_stream_read(&pool, &ta, r, b, 4, CK_PIPE_F_BLOCK, &n) == OSH_E_WOULD_BLOCK); 541 CHECK(ck_stream_read(&pool, &ta, r, b, 4, 0x80, &n) == OSH_E_INVALID_ARG); 542 CHECK(ck_stream_close(&pool, &ta, r) == OSH_E_OK && ck_stream_close(&pool, &ta, w) == OSH_E_OK); 543 CHECK(all_empty()); 544 /* a slot whose epoch reached the maximum is retired; with every slot retired create is LIMIT */ 545 for (unsigned i = 0; i < CK_PIPE_POOL_PIPES; i++) 546 pool.pipes[i].epoch = CK_PIPE_EPOCH_MAX - 1; 547 CHECK(ck_pipe_create(&pool, &ta, &r, &w) == OSH_E_OK && pool.pipes[0].epoch == CK_PIPE_EPOCH_MAX); 548 CHECK(ck_pipe_release_table(&pool, &ta) == 2); 549 for (unsigned i = 1; i < CK_PIPE_POOL_PIPES; i++) 550 pool.pipes[i].epoch = CK_PIPE_EPOCH_MAX; 551 CHECK(ck_pipe_create(&pool, &ta, &r, &w) == OSH_E_LIMIT); 552 CHECK(all_empty()); 553 } 554 555 /* The slice is the whole window, so a length that fits the buffer passes the range check. A length of 0 does not 556 * consult the window. */ 557 static unsigned dispatch_owned(struct ck_cap_table *caller, struct ck_cap_table *peer, const uint64_t *frame, 558 size_t ncells, uint8_t *slice, size_t slice_len, struct ck_pipe_ret *ret) 559 { 560 uintptr_t lo = (uintptr_t)slice; 561 uintptr_t hi = lo + slice_len; 562 return ck_pipe_dispatch(&pool, caller, peer, frame, ncells, slice_len ? slice : 0, slice_len, lo, hi, ret); 563 } 564 565 static void test_dispatch(void) 566 { 567 fresh_tables(); 568 ck_pipe_pool_init(&pool, 0); 569 struct ck_pipe_ret ret; 570 uint64_t create[4] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_CREATE, 0 }; 571 CHECK(dispatch_owned(&ta, &tb, create, 4, 0, 0, &ret) == OSH_E_OK && ret.error == OSH_E_OK); 572 uint64_t rd = ret.value, wr = ret.value2; 573 CHECK(rd != 0 && wr != 0 && rd != wr); 574 uint8_t msg[5] = { 'p', 0, 'i', 0, 'p' }, got[8]; 575 uint64_t wf[6] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_WRITE, 0, wr, 0 }; 576 CHECK(dispatch_owned(&ta, &tb, wf, 6, msg, 5, &ret) == OSH_E_OK && ret.value == 5); 577 uint64_t dup[7] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_DUP, 0, rd, CK_R_READ, 1 }; 578 CHECK(dispatch_owned(&ta, &tb, dup, 7, 0, 0, &ret) == OSH_E_OK); 579 uint64_t rb = ret.value; 580 uint64_t rf[6] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_READ, 0, rb, 0 }; 581 /* the dup lives in the peer table: from the caller's table the same raw handle is not ours */ 582 CHECK(dispatch_owned(&tb, &ta, rf, 6, got, 8, &ret) == OSH_E_OK && ret.value == 5 && !memcmp(got, msg, 5)); 583 /* the read end in the peer table cannot write */ 584 uint64_t wb[6] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_WRITE, 0, rb, 0 }; 585 CHECK(dispatch_owned(&tb, &ta, wb, 6, msg, 5, &ret) == OSH_E_DENIED && ret.value == 0); 586 uint64_t cl[5] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_CLOSE, 0, 0 }; 587 cl[4] = rd; 588 CHECK(dispatch_owned(&ta, &tb, cl, 5, 0, 0, &ret) == OSH_E_OK); 589 cl[4] = wr; 590 CHECK(dispatch_owned(&ta, &tb, cl, 5, 0, 0, &ret) == OSH_E_OK); 591 cl[4] = rb; 592 CHECK(dispatch_owned(&tb, &ta, cl, 5, 0, 0, &ret) == OSH_E_OK); 593 CHECK(all_empty()); 594 CHECK(dispatch_owned(&ta, &tb, 0, 0, 0, 0, &ret) == OSH_E_INVALID_ARG); 595 } 596 597 /* A close that fails must not add to the release count. The forged cap is removed (STALE) and still not counted. 598 * A pipe-tagged index outside the pool is DENIED, left in the table, and not counted. */ 599 static void test_release_count(void) 600 { 601 fresh_tables(); 602 ck_pipe_pool_init(&pool, 0); 603 struct ck_handle r, w, forged, outside; 604 CHECK(ck_pipe_create(&pool, &ta, &r, &w) == OSH_E_OK); 605 uint32_t epoch = pool.pipes[0].epoch; 606 CHECK(ck_cap_insert(&ta, ck_pipe_resource(0, CK_PIPE_END_READ, epoch + 1), CK_R_READ, &forged) == CK_CAP_OK); 607 CHECK(ck_cap_insert(&ta, ck_pipe_resource(7, CK_PIPE_END_WRITE, 1), CK_R_WRITE, &outside) == CK_CAP_OK); 608 CHECK(ck_pipe_release_table(&pool, &ta) == 2); 609 CHECK(ck_cap_lookup(&ta, forged, 0, 0) == CK_CAP_INVALID); 610 CHECK(ck_cap_lookup(&ta, outside, 0, 0) == CK_CAP_OK); 611 CHECK(ck_cap_remove(&ta, outside) == CK_CAP_OK); 612 CHECK(all_empty()); 613 } 614 615 /* Blocking write must commit the bytes it snapshotted, not a buffer the peer rewrote while the writer was parked. */ 616 static uint8_t toctou_src[4] = { 1, 2, 3, 4 }; 617 static struct ck_handle toctou_src_rd; 618 619 static unsigned toctou_wait(void *ctx, unsigned pipe, unsigned end) 620 { 621 (void)ctx; 622 (void)pipe; 623 (void)end; 624 memset(toctou_src, 0xA5, sizeof toctou_src); 625 uint8_t dump[4]; 626 size_t n = 0; 627 unsigned e = ck_stream_read(&pool, &ta, toctou_src_rd, dump, 4, 0, &n); 628 if (e != OSH_E_OK || n != 4) 629 return OSH_E_IO; 630 return OSH_E_OK; 631 } 632 633 static void toctou_wake(void *ctx, unsigned pipe, unsigned end) 634 { 635 (void)ctx; 636 (void)pipe; 637 (void)end; 638 } 639 640 static void test_toctou_snapshot(void) 641 { 642 fresh_tables(); 643 static const struct ck_pipe_hooks hooks = { 0, toctou_wait, toctou_wake, 0 }; 644 ck_pipe_pool_init(&pool, &hooks); 645 struct ck_handle r, w; 646 size_t n = 0; 647 CHECK(ck_pipe_create(&pool, &ta, &r, &w) == OSH_E_OK); 648 toctou_src_rd = r; 649 memset(big, 0x11, CK_PIPE_CAPACITY); 650 CHECK(ck_stream_write(&pool, &ta, w, big, CK_PIPE_CAPACITY, 0, &n) == OSH_E_OK && n == CK_PIPE_CAPACITY); 651 toctou_src[0] = 1; 652 toctou_src[1] = 2; 653 toctou_src[2] = 3; 654 toctou_src[3] = 4; 655 CHECK(ck_stream_write(&pool, &ta, w, toctou_src, 4, CK_PIPE_F_BLOCK, &n) == OSH_E_OK && n == 4); 656 CHECK(toctou_src[0] == 0xA5 && toctou_src[3] == 0xA5); 657 int bad = 0; 658 for (size_t got = 0; got < CK_PIPE_CAPACITY;) { 659 uint8_t b[64]; 660 size_t want = CK_PIPE_CAPACITY - got; 661 if (want > sizeof b) 662 want = sizeof b; 663 size_t rn = 0; 664 if (ck_stream_read(&pool, &ta, r, b, want, 0, &rn) != OSH_E_OK || rn != want) { 665 bad = 1; 666 break; 667 } 668 for (size_t i = 0; i < rn; i++) { 669 uint8_t expect = (got + i) < CK_PIPE_CAPACITY - 4 ? 0x11 : (uint8_t)(got + i - (CK_PIPE_CAPACITY - 4) + 1); 670 if (b[i] != expect) 671 bad = 1; 672 } 673 got += rn; 674 } 675 CHECK(!bad); 676 CHECK(ck_stream_close(&pool, &ta, r) == OSH_E_OK && ck_stream_close(&pool, &ta, w) == OSH_E_OK); 677 CHECK(all_empty()); 678 } 679 680 /* Host threads. The wait hook is a counting semaphore: a wake that lands before wait runs is still observed. */ 681 struct host_sync { 682 pthread_mutex_t mu; 683 pthread_cond_t cv; 684 unsigned pending[CK_PIPE_POOL_PIPES][2]; 685 int in_window, peer_done, use_window, timed_out; 686 }; 687 688 static struct host_sync host; 689 690 static void on_alarm(int sig) 691 { 692 (void)sig; 693 const char msg[] = "test_pipe: timeout (lock or wakeup)\n"; 694 ssize_t wr = write(STDERR_FILENO, msg, sizeof msg - 1); 695 (void)wr; 696 _Exit(1); 697 } 698 699 static void host_lock(void) 700 { 701 pthread_mutex_lock(&host.mu); 702 } 703 704 static void host_unlock(void) 705 { 706 pthread_mutex_unlock(&host.mu); 707 } 708 709 static int wait_pred(int *pred) 710 { 711 struct timespec ts; 712 clock_gettime(CLOCK_REALTIME, &ts); 713 ts.tv_sec += 3; 714 while (!*pred) { 715 if (pthread_cond_timedwait(&host.cv, &host.mu, &ts) == ETIMEDOUT) 716 return -1; 717 } 718 return 0; 719 } 720 721 static void host_before(void *ctx) 722 { 723 struct host_sync *s = ctx; 724 if (!s->use_window) 725 return; 726 host_lock(); 727 s->in_window = 1; 728 pthread_cond_broadcast(&s->cv); 729 int bad = wait_pred(&s->peer_done); 730 if (bad) 731 s->timed_out = 1; 732 host_unlock(); 733 } 734 735 static unsigned host_wait(void *ctx, unsigned pipe, unsigned end) 736 { 737 struct host_sync *s = ctx; 738 host_lock(); 739 if (!s->use_window) { 740 s->in_window = 1; 741 pthread_cond_broadcast(&s->cv); 742 } 743 struct timespec ts; 744 clock_gettime(CLOCK_REALTIME, &ts); 745 ts.tv_sec += 3; 746 while (s->pending[pipe][end] == 0) { 747 if (pthread_cond_timedwait(&s->cv, &s->mu, &ts) == ETIMEDOUT) { 748 s->timed_out = 1; 749 host_unlock(); 750 return OSH_E_INTERRUPTED; 751 } 752 } 753 s->pending[pipe][end]--; 754 host_unlock(); 755 return OSH_E_OK; 756 } 757 758 static void host_wake(void *ctx, unsigned pipe, unsigned end) 759 { 760 struct host_sync *s = ctx; 761 host_lock(); 762 s->pending[pipe][end]++; 763 pthread_cond_broadcast(&s->cv); 764 host_unlock(); 765 } 766 767 static void host_reset(int window) 768 { 769 host_lock(); 770 memset(host.pending, 0, sizeof host.pending); 771 host.in_window = host.peer_done = host.timed_out = 0; 772 host.use_window = window; 773 host_unlock(); 774 } 775 776 static int wait_window(void) 777 { 778 host_lock(); 779 int bad = wait_pred(&host.in_window); 780 host_unlock(); 781 return bad; 782 } 783 784 static void mark_peer(void) 785 { 786 host_lock(); 787 host.peer_done = 1; 788 pthread_cond_broadcast(&host.cv); 789 host_unlock(); 790 } 791 792 struct xfer { 793 struct ck_cap_table *table; 794 struct ck_handle h; 795 size_t total, chunk, moved, bad; 796 unsigned err; 797 int reader; 798 }; 799 800 static void *xfer_thread(void *arg) 801 { 802 struct xfer *x = arg; 803 size_t off = 0; 804 while (off < x->total) { 805 size_t want = x->total - off; 806 if (want > x->chunk) 807 want = x->chunk; 808 uint8_t tmp[1024]; 809 size_t n = 0; 810 unsigned e; 811 if (x->reader) { 812 e = ck_stream_read(&pool, x->table, x->h, tmp, want, CK_PIPE_F_BLOCK, &n); 813 if (e != OSH_E_OK || n == 0) { 814 x->err = e == OSH_E_OK ? 0xffu : e; 815 return 0; 816 } 817 for (size_t i = 0; i < n; i++) 818 x->bad += tmp[i] != pd_byte(off + i); 819 } else { 820 for (size_t i = 0; i < want; i++) 821 tmp[i] = pd_byte(off + i); 822 e = ck_stream_write(&pool, x->table, x->h, tmp, want, CK_PIPE_F_BLOCK, &n); 823 if (e != OSH_E_OK || n == 0) { 824 x->err = e == OSH_E_OK ? 0xffu : e; 825 return 0; 826 } 827 } 828 off += n; 829 } 830 x->moved = off; 831 if (x->reader) { 832 size_t n = 0; 833 uint8_t b; 834 unsigned e = ck_stream_read(&pool, x->table, x->h, &b, 1, CK_PIPE_F_BLOCK, &n); 835 if (e != OSH_E_OK || n != 0) 836 x->err = e == OSH_E_OK ? 0xffu : e; 837 ck_stream_close(&pool, x->table, x->h); 838 } else { 839 unsigned e = ck_stream_close(&pool, x->table, x->h); 840 if (e != OSH_E_OK) 841 x->err = e; 842 } 843 return 0; 844 } 845 846 static void *nb_thread(void *arg) 847 { 848 struct xfer *x = arg; 849 size_t off = 0; 850 while (off < x->total) { 851 size_t want = x->total - off; 852 if (want > x->chunk) 853 want = x->chunk; 854 uint8_t tmp[256]; 855 size_t n = 0; 856 unsigned e; 857 if (x->reader) { 858 e = ck_stream_read(&pool, x->table, x->h, tmp, want, 0, &n); 859 if (e == OSH_E_WOULD_BLOCK) { 860 sched_yield(); 861 continue; 862 } 863 if (e != OSH_E_OK || n == 0) { 864 x->err = e == OSH_E_OK ? 0xffu : e; 865 return 0; 866 } 867 for (size_t i = 0; i < n; i++) 868 x->bad += tmp[i] != pd_byte(off + i); 869 } else { 870 for (size_t i = 0; i < want; i++) 871 tmp[i] = pd_byte(off + i); 872 e = ck_stream_write(&pool, x->table, x->h, tmp, want, 0, &n); 873 if (e == OSH_E_WOULD_BLOCK) { 874 sched_yield(); 875 continue; 876 } 877 if (e != OSH_E_OK || n == 0) { 878 x->err = e == OSH_E_OK ? 0xffu : e; 879 return 0; 880 } 881 } 882 off += n; 883 } 884 x->moved = off; 885 if (!x->reader) { 886 unsigned e = ck_stream_close(&pool, x->table, x->h); 887 if (e != OSH_E_OK) 888 x->err = e; 889 } else { 890 for (;;) { 891 uint8_t b; 892 size_t n = 0; 893 unsigned e = ck_stream_read(&pool, x->table, x->h, &b, 1, 0, &n); 894 if (e == OSH_E_WOULD_BLOCK) { 895 sched_yield(); 896 continue; 897 } 898 if (e != OSH_E_OK || n != 0) 899 x->err = e == OSH_E_OK ? 0xffu : e; 900 break; 901 } 902 ck_stream_close(&pool, x->table, x->h); 903 } 904 return 0; 905 } 906 907 struct pair_job { 908 struct ck_cap_table *table; 909 unsigned err; 910 int n; 911 }; 912 913 static void *pair_thread(void *arg) 914 { 915 struct pair_job *j = arg; 916 for (int i = 0; i < j->n; i++) { 917 struct ck_handle r, w; 918 unsigned e = ck_pipe_create(&pool, j->table, &r, &w); 919 if (e != OSH_E_OK) { 920 j->err = e; 921 return 0; 922 } 923 uint8_t b[8], out[8]; 924 for (int k = 0; k < 8; k++) 925 b[k] = (uint8_t)(i + k); 926 size_t n = 0; 927 e = ck_stream_write(&pool, j->table, w, b, 8, 0, &n); 928 if (e != OSH_E_OK || n != 8) { 929 j->err = e == OSH_E_OK ? 0xffu : e; 930 return 0; 931 } 932 e = ck_stream_read(&pool, j->table, r, out, 8, 0, &n); 933 if (e != OSH_E_OK || n != 8 || memcmp(out, b, 8) != 0) { 934 j->err = e == OSH_E_OK ? 0xffu : e; 935 return 0; 936 } 937 if (ck_stream_close(&pool, j->table, r) != OSH_E_OK || ck_stream_close(&pool, j->table, w) != OSH_E_OK) { 938 j->err = OSH_E_IO; 939 return 0; 940 } 941 } 942 return 0; 943 } 944 945 struct one_call { 946 struct ck_cap_table *table; 947 struct ck_handle h; 948 int is_write; 949 unsigned err; 950 size_t n; 951 uint8_t buf[4]; 952 }; 953 954 static void *one_call_thread(void *arg) 955 { 956 struct one_call *c = arg; 957 if (c->is_write) 958 c->err = ck_stream_write(&pool, c->table, c->h, c->buf, 4, CK_PIPE_F_BLOCK, &c->n); 959 else 960 c->err = ck_stream_read(&pool, c->table, c->h, c->buf, 4, CK_PIPE_F_BLOCK, &c->n); 961 return 0; 962 } 963 964 static void join_one(pthread_t th, struct one_call *c, unsigned want, size_t want_n) 965 { 966 pthread_join(th, 0); 967 CHECK(c->err == want); 968 CHECK(c->n == want_n); 969 CHECK(!host.timed_out); 970 } 971 972 static void test_close_park(int window, int break_writer) 973 { 974 fresh_tables(); 975 host_reset(window); 976 struct ck_pipe_hooks hooks = { &host, host_wait, host_wake, host_before }; 977 ck_pipe_pool_init(&pool, &hooks); 978 struct ck_handle r, w; 979 CHECK(ck_pipe_create(&pool, &ta, &r, &w) == OSH_E_OK); 980 struct one_call call; 981 memset(&call, 0, sizeof call); 982 pthread_t th; 983 if (break_writer) { 984 size_t n = 0; 985 memset(big, 0x22, CK_PIPE_CAPACITY); 986 CHECK(ck_stream_write(&pool, &ta, w, big, CK_PIPE_CAPACITY, 0, &n) == OSH_E_OK && n == CK_PIPE_CAPACITY); 987 call.table = &ta; 988 call.h = w; 989 call.is_write = 1; 990 call.buf[0] = call.buf[1] = call.buf[2] = call.buf[3] = 9; 991 CHECK(pthread_create(&th, 0, one_call_thread, &call) == 0); 992 CHECK(wait_window() == 0); 993 CHECK(ck_stream_close(&pool, &ta, r) == OSH_E_OK); 994 if (window) 995 mark_peer(); 996 join_one(th, &call, OSH_E_BROKEN_PIPE, 0); 997 CHECK(pool.total.bytes_written == CK_PIPE_CAPACITY); 998 CHECK(ck_stream_close(&pool, &ta, w) == OSH_E_OK); 999 } else { 1000 call.table = &ta; 1001 call.h = r; 1002 CHECK(pthread_create(&th, 0, one_call_thread, &call) == 0); 1003 CHECK(wait_window() == 0); 1004 CHECK(ck_stream_close(&pool, &ta, w) == OSH_E_OK); 1005 if (window) 1006 mark_peer(); 1007 join_one(th, &call, OSH_E_OK, 0); 1008 CHECK(pool.total.eof == 1); 1009 CHECK(ck_stream_close(&pool, &ta, r) == OSH_E_OK); 1010 } 1011 CHECK(all_empty()); 1012 } 1013 1014 static void test_concurrent(void) 1015 { 1016 signal(SIGALRM, on_alarm); 1017 alarm(60); 1018 static const struct ck_pipe_hooks hooks = { &host, host_wait, host_wake, host_before }; 1019 const size_t total = 128u * 1024u; 1020 for (int iter = 0; iter < 4; iter++) { 1021 fresh_tables(); 1022 host_reset(0); 1023 ck_pipe_pool_init(&pool, &hooks); 1024 struct ck_handle r0, w, r; 1025 CHECK(ck_pipe_create(&pool, &ta, &r0, &w) == OSH_E_OK); 1026 CHECK(ck_stream_dup(&pool, &ta, r0, &tb, CK_R_READ, &r) == OSH_E_OK); 1027 CHECK(ck_stream_close(&pool, &ta, r0) == OSH_E_OK); 1028 struct xfer wr = { .table = &ta, .h = w, .total = total, .chunk = 1000 }; 1029 struct xfer rd = { .table = &tb, .h = r, .total = total, .chunk = 700, .reader = 1 }; 1030 pthread_t tw, tr; 1031 CHECK(pthread_create(&tw, 0, xfer_thread, &wr) == 0); 1032 CHECK(pthread_create(&tr, 0, xfer_thread, &rd) == 0); 1033 pthread_join(tw, 0); 1034 pthread_join(tr, 0); 1035 CHECK(wr.err == 0 && rd.err == 0); 1036 CHECK(wr.moved == total && rd.moved == total && rd.bad == 0); 1037 CHECK(!host.timed_out); 1038 CHECK(all_empty()); 1039 } 1040 fresh_tables(); 1041 ck_pipe_pool_init(&pool, 0); 1042 struct ck_handle r0, w, r; 1043 CHECK(ck_pipe_create(&pool, &ta, &r0, &w) == OSH_E_OK); 1044 CHECK(ck_stream_dup(&pool, &ta, r0, &tb, CK_R_READ, &r) == OSH_E_OK); 1045 CHECK(ck_stream_close(&pool, &ta, r0) == OSH_E_OK); 1046 struct xfer wr = { .table = &ta, .h = w, .total = total, .chunk = 256 }; 1047 struct xfer rd = { .table = &tb, .h = r, .total = total, .chunk = 128, .reader = 1 }; 1048 pthread_t tw, tr; 1049 CHECK(pthread_create(&tw, 0, nb_thread, &wr) == 0); 1050 CHECK(pthread_create(&tr, 0, nb_thread, &rd) == 0); 1051 pthread_join(tw, 0); 1052 pthread_join(tr, 0); 1053 CHECK(wr.err == 0 && rd.err == 0); 1054 CHECK(wr.moved == total && rd.moved == total && rd.bad == 0); 1055 CHECK(all_empty()); 1056 1057 test_close_park(1, 0); 1058 test_close_park(1, 1); 1059 test_close_park(0, 0); 1060 test_close_park(0, 1); 1061 1062 fresh_tables(); 1063 ck_pipe_pool_init(&pool, 0); 1064 struct pair_job ja = { .table = &ta, .n = 80 }; 1065 struct pair_job jb = { .table = &tb, .n = 80 }; 1066 CHECK(pthread_create(&tw, 0, pair_thread, &ja) == 0); 1067 CHECK(pthread_create(&tr, 0, pair_thread, &jb) == 0); 1068 pthread_join(tw, 0); 1069 pthread_join(tr, 0); 1070 CHECK(ja.err == 0 && jb.err == 0); 1071 CHECK(all_empty()); 1072 alarm(0); 1073 } 1074 1075 /* Above the user-VA top named in pipe.h: ARM64 TTBR1 is 0xFFFF000000000000, and this is the x86 canonical high half. */ 1076 #define SLICE_KERNEL_HALF ((uintptr_t)0xFFFF800000000000ULL) 1077 1078 static sigjmp_buf slice_jmp; 1079 1080 static void slice_fault(int sig) 1081 { 1082 (void)sig; 1083 siglongjmp(slice_jmp, 1); 1084 } 1085 1086 static unsigned frame_rw(int is_write, uint64_t raw, uint8_t *slice, size_t len, uintptr_t lo, uintptr_t hi, 1087 struct ck_pipe_ret *ret) 1088 { 1089 uint64_t fr[6] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, is_write ? CK_PIPE_OP_WRITE : CK_PIPE_OP_READ, 0, raw, 1090 0 }; 1091 return ck_pipe_dispatch(&pool, &ta, &tb, fr, 6, slice, len, lo, hi, ret); 1092 } 1093 1094 /* A rejected slice must not move a byte. A fault means the check did not run before the copy; the ticket lock 1095 * would still be held, so stop rather than deadlock the next op. */ 1096 static void slice_reject(int is_write, uint64_t raw, void *slice, size_t len, uintptr_t lo, uintptr_t hi, 1097 uint32_t plen, uint64_t bw, uint64_t br) 1098 { 1099 struct ck_pipe_ret ret; 1100 if (sigsetjmp(slice_jmp, 1) != 0) { 1101 CHECK(0 && "slice faulted; the range check did not reject it"); 1102 ck_t_verdict("test_pipe"); 1103 fflush(stdout); 1104 _Exit(1); 1105 } 1106 unsigned err = frame_rw(is_write, raw, slice, len, lo, hi, &ret); 1107 CHECK(err == OSH_E_INVALID_ARG && ret.error == OSH_E_INVALID_ARG && ret.value == 0); 1108 CHECK(pool.pipes[0].len == plen && pool.total.bytes_written == bw && pool.total.bytes_read == br); 1109 } 1110 1111 static void test_slice_window(void) 1112 { 1113 fresh_tables(); 1114 ck_pipe_pool_init(&pool, 0); 1115 struct sigaction sa, old_segv, old_bus; 1116 memset(&sa, 0, sizeof sa); 1117 sa.sa_handler = slice_fault; 1118 sigemptyset(&sa.sa_mask); 1119 CHECK(sigaction(SIGSEGV, &sa, &old_segv) == 0); 1120 CHECK(sigaction(SIGBUS, &sa, &old_bus) == 0); 1121 1122 struct ck_pipe_ret ret; 1123 uint64_t create[4] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_CREATE, 0 }; 1124 CHECK(ck_pipe_dispatch(&pool, &ta, &tb, create, 4, 0, 0, 0, 0, &ret) == OSH_E_OK); 1125 uint64_t rd = ret.value, wr = ret.value2; 1126 CHECK(pool.pipes[0].flags & CK_PIPE_LIVE); 1127 1128 /* The declared window is mem[16, 32). The allocation is larger, so a slice past hi is still mapped. */ 1129 uint8_t mem[64]; 1130 memset(mem, 0x5A, sizeof mem); 1131 uintptr_t lo = (uintptr_t)(mem + 16); 1132 uintptr_t hi = lo + 16; 1133 memcpy(mem + 16, "abcd", 4); 1134 CHECK(frame_rw(1, wr, mem + 16, 4, lo, hi, &ret) == OSH_E_OK && ret.value == 4); 1135 uint32_t plen = pool.pipes[0].len; 1136 uint64_t bw = pool.total.bytes_written, br = pool.total.bytes_read; 1137 CHECK(plen == 4 && bw == 4 && br == 0); 1138 1139 /* len 0 passes, including a pointer that must not be dereferenced. */ 1140 CHECK(frame_rw(0, rd, 0, 0, lo, hi, &ret) == OSH_E_OK && ret.value == 0); 1141 CHECK(frame_rw(0, rd, (uint8_t *)(UINTPTR_MAX - 3), 0, lo, hi, &ret) == OSH_E_OK && ret.value == 0); 1142 CHECK(frame_rw(1, wr, (uint8_t *)SLICE_KERNEL_HALF, 0, lo, hi, &ret) == OSH_E_OK && ret.value == 0); 1143 CHECK(pool.pipes[0].len == plen && pool.total.bytes_written == bw && pool.total.bytes_read == br); 1144 1145 /* Outside the window, still inside the allocation. */ 1146 slice_reject(1, wr, mem, 4, lo, hi, plen, bw, br); 1147 slice_reject(1, wr, mem + 40, 4, lo, hi, plen, bw, br); 1148 /* One byte before lo, and the exclusive end. */ 1149 slice_reject(1, wr, (void *)(lo - 1), 1, lo, hi, plen, bw, br); 1150 slice_reject(1, wr, (void *)hi, 1, lo, hi, plen, bw, br); 1151 /* Starts on the last byte of the window and runs past hi. */ 1152 slice_reject(1, wr, (void *)(hi - 1), 2, lo, hi, plen, bw, br); 1153 /* base == lo and len == hi - lo + 1. */ 1154 slice_reject(1, wr, (void *)lo, 17, lo, hi, plen, bw, br); 1155 /* slice + len wraps the address space. */ 1156 slice_reject(1, wr, (void *)(UINTPTR_MAX - 3), 8, lo, hi, plen, bw, br); 1157 /* Kernel half. */ 1158 slice_reject(1, wr, (void *)SLICE_KERNEL_HALF, 4, lo, hi, plen, bw, br); 1159 1160 /* Boundaries that are inside: lo, the last byte, and a length that lands exactly on hi. */ 1161 uint8_t got[4]; 1162 CHECK(frame_rw(0, rd, got, 4, (uintptr_t)got, (uintptr_t)got + sizeof got, &ret) == OSH_E_OK && ret.value == 4); 1163 CHECK(!memcmp(got, "abcd", 4)); 1164 mem[31] = 'Z'; 1165 CHECK(frame_rw(1, wr, (uint8_t *)(hi - 1), 1, lo, hi, &ret) == OSH_E_OK && ret.value == 1); 1166 CHECK(frame_rw(1, wr, (uint8_t *)lo, 16, lo, hi, &ret) == OSH_E_OK && ret.value == 16); 1167 CHECK(pool.pipes[0].len == 1 + 16); 1168 1169 uint64_t cl[5] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_CLOSE, 0, rd }; 1170 CHECK(ck_pipe_dispatch(&pool, &ta, &tb, cl, 5, 0, 0, 0, 0, &ret) == OSH_E_OK); 1171 cl[4] = wr; 1172 CHECK(ck_pipe_dispatch(&pool, &ta, &tb, cl, 5, 0, 0, 0, 0, &ret) == OSH_E_OK); 1173 CHECK(all_empty()); 1174 CHECK(sigaction(SIGSEGV, &old_segv, 0) == 0); 1175 CHECK(sigaction(SIGBUS, &old_bus, 0) == 0); 1176 } 1177 1178 /* #494: ck_pipe_dispatch touches the slice only outside the pool lock. The slice sits on a page that faults on the 1179 * first access; the handler records whether the ticket lock was held at that moment, then opens the page so the 1180 * access retries. Before the bounce buffer, the ring copy faulted inside the lock. */ 1181 static volatile sig_atomic_t bounce_faults, bounce_held; 1182 static uint8_t *bounce_page; 1183 static size_t bounce_pagesz; 1184 1185 static void bounce_fault(int sig) 1186 { 1187 (void)sig; 1188 bounce_faults++; 1189 if (ck_spin_busy(&pool.lock)) 1190 bounce_held = 1; 1191 if (mprotect(bounce_page, bounce_pagesz, PROT_READ | PROT_WRITE) != 0) 1192 _Exit(2); 1193 } 1194 1195 static void bounce_arm(int prot) 1196 { 1197 bounce_faults = 0; 1198 bounce_held = 0; 1199 CHECK(mprotect(bounce_page, bounce_pagesz, prot) == 0); 1200 } 1201 1202 static void test_slice_bounce(void) 1203 { 1204 fresh_tables(); 1205 ck_pipe_pool_init(&pool, 0); 1206 long ps = sysconf(_SC_PAGESIZE); 1207 CHECK(ps >= (long)CK_PIPE_BOUNCE + 8); 1208 bounce_pagesz = (size_t)ps; 1209 void *pg = mmap(0, bounce_pagesz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 1210 CHECK(pg != MAP_FAILED); 1211 if (pg == MAP_FAILED) 1212 return; 1213 bounce_page = pg; 1214 struct sigaction sa, old_segv, old_bus; 1215 memset(&sa, 0, sizeof sa); 1216 sa.sa_handler = bounce_fault; 1217 sigemptyset(&sa.sa_mask); 1218 CHECK(sigaction(SIGSEGV, &sa, &old_segv) == 0); 1219 CHECK(sigaction(SIGBUS, &sa, &old_bus) == 0); 1220 1221 struct ck_pipe_ret ret; 1222 uint64_t create[4] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_CREATE, 0 }; 1223 CHECK(ck_pipe_dispatch(&pool, &ta, &tb, create, 4, 0, 0, 0, 0, &ret) == OSH_E_OK); 1224 uint64_t rd = ret.value, wr = ret.value2; 1225 uintptr_t lo = (uintptr_t)bounce_page, hi = lo + bounce_pagesz; 1226 1227 /* WRITE: the slice is read before the lock is taken. */ 1228 memcpy(bounce_page, "bounce", 6); 1229 bounce_arm(PROT_NONE); 1230 CHECK(frame_rw(1, wr, bounce_page, 6, lo, hi, &ret) == OSH_E_OK && ret.value == 6); 1231 CHECK(bounce_faults == 1 && bounce_held == 0); 1232 CHECK(pool.pipes[0].len == 6); 1233 1234 /* READ: the slice is written after the lock is dropped. */ 1235 memset(bounce_page, 0, 16); 1236 bounce_arm(PROT_READ); 1237 CHECK(frame_rw(0, rd, bounce_page, 16, lo, hi, &ret) == OSH_E_OK && ret.value == 6); 1238 CHECK(bounce_faults == 1 && bounce_held == 0); 1239 CHECK(!memcmp(bounce_page, "bounce", 6) && pool.pipes[0].len == 0); 1240 1241 /* A slice longer than the bounce buffer moves CK_PIPE_BOUNCE bytes; the caller loops for the rest. */ 1242 bounce_arm(PROT_READ | PROT_WRITE); 1243 for (size_t i = 0; i < CK_PIPE_BOUNCE + 7; i++) 1244 bounce_page[i] = (uint8_t)(i * 7u + 1u); 1245 CHECK(frame_rw(1, wr, bounce_page, CK_PIPE_BOUNCE + 7, lo, hi, &ret) == OSH_E_OK && ret.value == CK_PIPE_BOUNCE); 1246 CHECK(frame_rw(1, wr, bounce_page + CK_PIPE_BOUNCE, 7, lo, hi, &ret) == OSH_E_OK && ret.value == 7); 1247 CHECK(pool.pipes[0].len == CK_PIPE_BOUNCE + 7); 1248 uint8_t back[CK_PIPE_BOUNCE + 7]; 1249 CHECK(frame_rw(0, rd, back, sizeof back, (uintptr_t)back, (uintptr_t)back + sizeof back, &ret) == OSH_E_OK && 1250 ret.value == CK_PIPE_BOUNCE); 1251 CHECK(frame_rw(0, rd, back + CK_PIPE_BOUNCE, 7, (uintptr_t)back, (uintptr_t)back + sizeof back, &ret) == 1252 OSH_E_OK && 1253 ret.value == 7); 1254 CHECK(!memcmp(back, bounce_page, sizeof back) && pool.pipes[0].len == 0); 1255 1256 /* A window that ends above the user-VA top is refused before the slice is touched, for both directions. */ 1257 bounce_arm(PROT_NONE); 1258 CHECK(frame_rw(1, wr, bounce_page, 4, lo, CK_PIPE_USER_TOP + 1, &ret) == OSH_E_INVALID_ARG && ret.value == 0); 1259 CHECK(frame_rw(0, rd, bounce_page, 4, lo, CK_PIPE_USER_TOP + 1, &ret) == OSH_E_INVALID_ARG && ret.value == 0); 1260 CHECK(frame_rw(1, wr, bounce_page, 4, lo, UINTPTR_MAX, &ret) == OSH_E_INVALID_ARG && ret.value == 0); 1261 CHECK(bounce_faults == 0 && pool.pipes[0].len == 0); 1262 /* The top itself is a legal exclusive end. */ 1263 bounce_arm(PROT_READ | PROT_WRITE); 1264 CHECK(frame_rw(1, wr, bounce_page, 4, lo, CK_PIPE_USER_TOP, &ret) == OSH_E_OK && ret.value == 4); 1265 1266 uint64_t cl[5] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, CK_PIPE_OP_CLOSE, 0, rd }; 1267 CHECK(ck_pipe_dispatch(&pool, &ta, &tb, cl, 5, 0, 0, 0, 0, &ret) == OSH_E_OK); 1268 cl[4] = wr; 1269 CHECK(ck_pipe_dispatch(&pool, &ta, &tb, cl, 5, 0, 0, 0, 0, &ret) == OSH_E_OK); 1270 CHECK(all_empty()); 1271 CHECK(sigaction(SIGSEGV, &old_segv, 0) == 0); 1272 CHECK(sigaction(SIGBUS, &old_bus, 0) == 0); 1273 CHECK(munmap(bounce_page, bounce_pagesz) == 0); 1274 } 1275 1276 int main(int argc, char **argv) 1277 { 1278 _Static_assert(CK_PIPE_CAPACITY >= 65536u, "section 8.5 minimum"); 1279 CHECK(osh_err_label(OSH_E_WOULD_BLOCK)[0] == 'W' && !strcmp(osh_err_label(99), "IO")); 1280 run_fixtures(argc > 1 ? argv[1] : PIPE_FIXTURE_DIR); 1281 test_transfers(); 1282 test_revoke_midstream(0); 1283 test_revoke_midstream(CK_PIPE_F_BLOCK); 1284 test_epoch_and_forgery(); 1285 test_dispatch(); 1286 test_slice_window(); 1287 test_slice_bounce(); 1288 test_release_count(); 1289 test_toctou_snapshot(); 1290 CHECK(pthread_mutex_init(&host.mu, 0) == 0); 1291 CHECK(pthread_cond_init(&host.cv, 0) == 0); 1292 test_concurrent(); 1293 pthread_cond_destroy(&host.cv); 1294 pthread_mutex_destroy(&host.mu); 1295 return ck_t_verdict("test_pipe"); 1296 }