@amritk/nish-x86_64-linux 0.42.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
@@ -216,6 +216,8 @@ void *nish_cross_keep(void *v, uint64_t esize, uint64_t mark) {
216
216
  if (!x) {
217
217
  v = nish_cross_copy(v, esize);
218
218
  } else {
219
+ /* `x->off` needs no realigning under NISH_ARENA_RANGE: only this
220
+ code bumps a transfer block, by `nish_cross_size`'s 8-byte multiples. */
219
221
  uint64_t n = nish_cross_size(v, esize);
220
222
  if (!x->block || x->block->cap - x->off < n) {
221
223
  uint64_t want = x->block && x->block->cap < 32768 ? x->block->cap * 2 : x->block ? 65536 : 4096;
@@ -243,7 +245,14 @@ void *nish_cross_in(void *p, uint64_t esize) {
243
245
  void *v = (void *)((uintptr_t)p & ~(uintptr_t)1);
244
246
  nish_xchunk *c = (nish_xchunk *)v - 1;
245
247
  uint64_t n = c->cap;
248
+ #ifdef NISH_ARENA_RANGE
249
+ /* What `nish_alloc_struct` would have left: it starts at `off` rounded up
250
+ to 8 (runtime.c gives the reason). */
251
+ uint64_t at = (nish_arena.off + 7) & ~(uint64_t)7;
252
+ if (n >= 65536 && (at > nish_arena.cap || n > nish_arena.cap - at)) {
253
+ #else
246
254
  if (n >= 65536 && n > nish_arena.cap - nish_arena.off) {
255
+ #endif
247
256
  nish_cross_link(c);
248
257
  return v;
249
258
  }
@@ -250,10 +250,13 @@ int32_t nish_net_address(nish_array *out, const nish_str *host, int32_t port) {
250
250
  keeps every handle below 2^31; their ids are not reused, so a closed
251
251
  listener's handles answer -9 for good.
252
252
 
253
- Every call that takes a handle and is not one of the slot paths here
254
- (`sendFd`, `netShutdown`, `connectResult`, `peerCredentials`, ...) passes it
255
- to the kernel as a descriptor, which no process holds at 2^20 or above, so
256
- it answers -9 (EBADF) with nothing to check.
253
+ `netShutdown` queues an io_uring SHUTDOWN behind the slot's sends, so the
254
+ peer reads every byte written before it and then the FIN (NURING-39);
255
+ `netMss` answers -1, its "not known", for a slot. Every other call that
256
+ takes a handle and is not one of the slot paths here (`sendFd`,
257
+ `connectResult`, `peerCredentials`, ...) passes it to the kernel as a
258
+ descriptor, which no process holds at 2^20 or above, so it answers -9
259
+ (EBADF) with nothing to check.
257
260
 
258
261
  Readiness keeps the descriptor contract: a read with nothing queued, a write
259
262
  to a full send area and an accept with nothing accepted answer -11, and the
@@ -291,6 +294,7 @@ int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
291
294
  int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
292
295
  int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
293
296
  int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
297
+ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how);
294
298
  int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot);
295
299
  int32_t nish_uring_full(const struct nish_uring_set *s);
296
300
  int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on);
@@ -305,6 +309,9 @@ void nish_uring_destroy(struct nish_uring_set *s);
305
309
  /* What a held slot's queue may still take from the kernel before the cancel
306
310
  lands; past it the connection ends with -105 (ENOBUFS). */
307
311
  #define NR_RECV_MAX (16 * 1024 * 1024)
312
+ /* A slot's first receive queue, doubled as it fills: small enough for
313
+ glibc's thread cache, and freed once drained (NURING-38). */
314
+ #define NR_RECV_FIRST 1024
308
315
  /* The most slots per ring: a table is the kernel's memory, sized up front. */
309
316
  #define NR_SLOTS_MAX 16384
310
317
  #define NR_EVENTS 256
@@ -320,11 +327,12 @@ struct nr_vent {
320
327
 
321
328
  /* One slot's connection. `gen` counts the slot's closes; `lingering` is a
322
329
  close the program made while bytes were still queued, which ends when they
323
- are sent, the peer goes or a send fails. */
330
+ are sent, the peer goes or a send fails. `wshut` is a shutdown of the
331
+ write side, after which a write answers -32 as send's EPIPE would. */
324
332
  struct nr_conn {
325
333
  struct nr_vent v;
326
334
  uint32_t gen;
327
- uint8_t open, lingering, hup, wblocked, rheld;
335
+ uint8_t open, lingering, hup, wblocked, rheld, wshut;
328
336
  int32_t hup_res, werr;
329
337
  uint8_t *rbuf;
330
338
  uint32_t rlen, roff, rcap;
@@ -471,13 +479,10 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
471
479
  }
472
480
  struct nr_conn *c = &S->conns[e->ring][e->slot];
473
481
  if (e->kind == 1) {
482
+ /* nr_close freed the queue of a slot reused here (NURING-38). */
474
483
  uint32_t gen = c->gen;
475
- uint8_t *buf = c->rbuf;
476
- uint32_t cap = c->rcap;
477
484
  memset(c, 0, sizeof *c);
478
485
  c->gen = gen;
479
- c->rbuf = buf;
480
- c->rcap = cap;
481
486
  c->open = 1;
482
487
  c->v.loop = -1;
483
488
  uint32_t i = (S->fhead + S->flen) % (S->fcap ? S->fcap : 1);
@@ -507,7 +512,6 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
507
512
  so a read never runs past the failure it reports. */
508
513
  if (c->lingering || c->hup || e->res <= 0) return;
509
514
  uint32_t n = (uint32_t)e->res;
510
- if (c->roff == c->rlen) c->roff = c->rlen = 0;
511
515
  if ((uint64_t)(c->rlen - c->roff) + n > NR_RECV_MAX) {
512
516
  c->hup = 1;
513
517
  c->hup_res = -105;
@@ -521,7 +525,7 @@ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
521
525
  c->roff = 0;
522
526
  }
523
527
  if (c->rlen + n > c->rcap) {
524
- uint32_t nc = c->rcap ? c->rcap : 4096;
528
+ uint32_t nc = c->rcap ? c->rcap : NR_RECV_FIRST;
525
529
  while (nc < c->rlen + n) nc *= 2;
526
530
  uint8_t *p = realloc(c->rbuf, nc);
527
531
  if (!p) {
@@ -712,6 +716,13 @@ static int32_t nr_read(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_
712
716
  c->roff += n;
713
717
  /* A held receive goes again once the program has read half the queue. */
714
718
  if (c->rheld && c->rlen - c->roff < NR_AREA / 2 && nish_uring_hold(S->u, ring, slot, 0) == 0) c->rheld = 0;
719
+ /* A drained queue is freed and emptied, so an idle slot holds none
720
+ (NURING-38) and a receive never finds roff at rlen past 0. */
721
+ if (c->roff == c->rlen) {
722
+ free(c->rbuf);
723
+ c->rbuf = NULL;
724
+ c->rcap = c->roff = c->rlen = 0;
725
+ }
715
726
  return (int32_t)n;
716
727
  }
717
728
  if (c->hup) return c->hup_res < 0 ? c->hup_res : 0;
@@ -730,6 +741,7 @@ static int32_t nr_fail_code(const struct nr_conn *c) {
730
741
  queue. -11 when it has none, or the connection's errno once it has failed. */
731
742
  static int32_t nr_send(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot, const char *p, int64_t len) {
732
743
  if (nr_failed(c)) return nr_fail_code(c);
744
+ if (c->wshut) return -32;
733
745
  int64_t q = nish_uring_queued(S->u, ring, slot);
734
746
  if (q < 0) return -9;
735
747
  if (q >= NR_AREA) {
@@ -748,6 +760,22 @@ static int32_t nr_send(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_
748
760
  return len > 0 && n == 0 ? -11 : n;
749
761
  }
750
762
 
763
+ /* Half-closes a slot (NURING-39). The SHUTDOWN goes behind the queued sends,
764
+ so the peer reads them and then the FIN. A shut read side reads as the
765
+ peer's end: what is queued, then 0, with later bytes dropped. */
766
+ static int32_t nr_shutdown(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot, int32_t how) {
767
+ if (nr_failed(c)) return -107;
768
+ int32_t r = nish_uring_shutdown(S->u, ring, slot, how);
769
+ if (r < 0) return r;
770
+ if (how != 0) c->wshut = 1;
771
+ if (how != 1 && !c->hup) {
772
+ c->hup = 1;
773
+ c->hup_res = 0;
774
+ nr_touch(&c->v);
775
+ }
776
+ return 0;
777
+ }
778
+
751
779
  static int32_t nr_close(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot) {
752
780
  nr_unregister(&c->v);
753
781
  c->gen++;
@@ -1290,6 +1318,11 @@ int32_t nish_net_write2(int32_t fd, const nish_array *a, int64_t aoff, int64_t a
1290
1318
  are `SHUT_RD`, `SHUT_WR` and `SHUT_RDWR` on both platforms. */
1291
1319
  int32_t nish_net_shutdown(int32_t fd, int32_t how) {
1292
1320
  if ((uint32_t)how > 2) return -22;
1321
+ #if defined(__linux__)
1322
+ if (fd >= NR_BASE) { struct nr_set *S; int32_t ring, slot;
1323
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1324
+ return c ? nr_shutdown(S, c, ring, slot, how) : -9; }
1325
+ #endif
1293
1326
  return shutdown(fd, how) != 0 ? nish_net_fail() : 0;
1294
1327
  }
1295
1328
 
@@ -1363,8 +1396,13 @@ int32_t nish_net_set_option(int32_t fd, int32_t option, int32_t value) {
1363
1396
  options below. Linux's `TCP_INFO` answers the current `tcpi_snd_mss`; the
1364
1397
  kernel copies at most the struct it has and says how much, so an answer
1365
1398
  too short to reach the field is not read. `TCP_MAXSEG` is the fallback,
1366
- and Darwin's only reading. */
1399
+ and Darwin's only reading. A ring slot is -1 too (NURING-39): its socket
1400
+ is a fixed file with no descriptor to ask, and io_uring's getsockopt
1401
+ command reads only SOL_SOCKET options, not TCP's. */
1367
1402
  int32_t nish_net_mss(int32_t fd) {
1403
+ #if defined(__linux__)
1404
+ if (fd >= NR_BASE) return -1;
1405
+ #endif
1368
1406
  struct sockaddr_storage peer;
1369
1407
  socklen_t len = sizeof peer;
1370
1408
  if (getpeername(fd, (struct sockaddr *)&peer, &len) != 0) return -1;
@@ -92,6 +92,10 @@ extern NISH_TLS uint64_t nish_region_chunk;
92
92
  #ifdef NISH_ARENA_DEBUG
93
93
  /* runtime.c's: retire a debug arena chunk, or free one C code malloc'd. */
94
94
  void nish_debug_drop(void *c);
95
+ #elif defined NISH_ARENA_RANGE && !defined NISH_ASAN
96
+ /* runtime.c's: put a compact-range chunk on its class's free list, or free
97
+ one malloc made. */
98
+ void nish_range_drop(void *c);
95
99
  #endif
96
100
 
97
101
  #define NISH_REGION_SLOT_BITS 24
@@ -386,6 +390,8 @@ void nish_region_adopt(uint64_t r) {
386
390
  nish_chunk_link *next = head->next;
387
391
  #ifdef NISH_ARENA_DEBUG
388
392
  if (!(head->cap & NISH_CHUNK_CARVED)) nish_debug_drop(head);
393
+ #elif defined NISH_ARENA_RANGE && !defined NISH_ASAN
394
+ if (!(head->cap & NISH_CHUNK_CARVED)) nish_range_drop(head);
389
395
  #else
390
396
  if (!(head->cap & NISH_CHUNK_CARVED)) free(head);
391
397
  #endif
@@ -397,6 +403,13 @@ void nish_region_adopt(uint64_t r) {
397
403
  nish_chunk_link *last = head;
398
404
  while (last->next) last = last->next;
399
405
  struct nish_arena caller = nish_arena;
406
+ #ifdef NISH_ARENA_RANGE
407
+ /* A compact class's 4-byte bump may leave `off` 4 past an 8-byte
408
+ boundary (runtime.c's `nish_alloc_struct`): the carved chunk's header,
409
+ and every block bumped from it, start at the next one. */
410
+ caller.off = (caller.off + 7) & ~(uint64_t)7;
411
+ if (caller.off > caller.cap) caller.off = caller.cap;
412
+ #endif
400
413
  uint64_t left = caller.buf ? caller.cap - caller.off : 0;
401
414
  last->next = caller.chunks;
402
415
  nish_arena = s->arena;
@@ -70,11 +70,22 @@ extern NISH_TEXT_TLS struct nish_arena nish_arena;
70
70
  void *nish_arena_grow(uint64_t size);
71
71
  static inline void *nish_text_alloc(uint64_t size) {
72
72
  size = (size + 7) & ~(uint64_t)7;
73
+ #ifdef NISH_ARENA_RANGE
74
+ /* runtime.c's `nish_alloc_struct` gives the reason: `off` may be 4 bytes
75
+ past an 8-byte boundary once a compact class has been bumped. */
76
+ uint64_t at = (nish_arena.off + 7) & ~(uint64_t)7;
77
+ if (at + size <= nish_arena.cap) {
78
+ void *p = nish_arena.buf + at;
79
+ nish_arena.off = at + size;
80
+ return p;
81
+ }
82
+ #else
73
83
  if (nish_arena.off + size <= nish_arena.cap) {
74
84
  void *p = nish_arena.buf + nish_arena.off;
75
85
  nish_arena.off += size;
76
86
  return p;
77
87
  }
88
+ #endif
78
89
  return nish_arena_grow(size);
79
90
  }
80
91
  #else
@@ -204,6 +204,7 @@ void nish_uring_destroy(struct nish_uring_set *s);
204
204
  #define UR_OP_CLOSE 19
205
205
  #define UR_OP_SEND 26
206
206
  #define UR_OP_RECV 27
207
+ #define UR_OP_SHUTDOWN 34
207
208
  #define UR_RECV_MULTISHOT (1u << 1)
208
209
  #define UR_CANCEL_ALL (1u << 0)
209
210
  #define UR_CANCEL_FD (1u << 1)
@@ -332,12 +333,14 @@ enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5 };
332
333
  /* One send's bytes, owned by the set until the kernel has taken all of them.
333
334
  * `next` queues it behind the slot's earlier sends; `prev_all`/`next_all`
334
335
  * keep every record on one list so `nish_uring_destroy` can free those the
335
- * kernel still held. */
336
+ * kernel still held. `how` is -1 for bytes, or a shutdown's 0 to 2: that
337
+ * record carries none and goes to the kernel as a SHUTDOWN once the sends
338
+ * queued before it are done, so the peer reads them before the FIN. */
336
339
  struct ur_send {
337
340
  struct ur_send *next, *prev_all, *next_all;
338
341
  uint32_t slot, gen;
339
342
  uint32_t off, len;
340
- int in_flight;
343
+ int in_flight, how;
341
344
  uint8_t data[];
342
345
  };
343
346
 
@@ -439,7 +442,7 @@ static int32_t ur_ops_ok(int fd) {
439
442
  memset(&pr, 0, sizeof pr);
440
443
  int e = ur_register(fd, UR_REGISTER_PROBE, &pr, 64);
441
444
  if (e < 0) return e == -EINVAL ? -EOPNOTSUPP : e;
442
- static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_ASYNC_CANCEL, UR_OP_CLOSE, UR_OP_SEND, UR_OP_RECV};
445
+ static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_ASYNC_CANCEL, UR_OP_CLOSE, UR_OP_SEND, UR_OP_RECV, UR_OP_SHUTDOWN};
443
446
  for (size_t i = 0; i < sizeof ops; i++) {
444
447
  uint8_t op = ops[i];
445
448
  if (op > pr.last_op || op >= pr.ops_len || !(pr.ops[op].flags & UR_OP_SUPPORTED)) return -EOPNOTSUPP;
@@ -625,10 +628,15 @@ static void ur_free_ring(struct ur_ring *r) {
625
628
  }
626
629
 
627
630
  /* Restrict a ring made with R_DISABLED to what the layer submits (WP63 §6.2):
628
- * the five opcodes and NOP, the three flags the layer sets, and no register
629
- * opcode at all once it is enabled. */
631
+ * the six opcodes and NOP, the three flags the layer sets, and no register
632
+ * opcode at all once it is enabled. The flags entry allows bits and requires
633
+ * none, so it is the layer, which always sets FIXED_FILE, that confines
634
+ * SHUTDOWN to the ring's own table: an SQE written into the ring by other
635
+ * means could name an ordinary descriptor, as it already could for SEND
636
+ * (NURING-39). */
630
637
  static int ur_restrict(int fd) {
631
- static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_RECV, UR_OP_SEND, UR_OP_CLOSE, UR_OP_ASYNC_CANCEL, UR_OP_NOP};
638
+ static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_RECV, UR_OP_SEND, UR_OP_CLOSE,
639
+ UR_OP_ASYNC_CANCEL, UR_OP_NOP, UR_OP_SHUTDOWN};
632
640
  struct ur_restriction res[sizeof ops + 1];
633
641
  memset(res, 0, sizeof res);
634
642
  for (size_t i = 0; i < sizeof ops; i++) {
@@ -961,7 +969,8 @@ static void ur_top_up(struct nish_uring_set *s) {
961
969
  if (rings > s->stats.max_accepting_rings) s->stats.max_accepting_rings = rings;
962
970
  }
963
971
 
964
- /* Submit the slot's first queued send, from where the kernel left off. */
972
+ /* Submit the slot's first queued send, from where the kernel left off, or
973
+ * its shutdown. */
965
974
  static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set *s) {
966
975
  struct ur_send *q = r->sq_first[slot];
967
976
  if (!q || q->in_flight) return;
@@ -970,12 +979,17 @@ static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set
970
979
  s->stalled_sends++;
971
980
  return;
972
981
  }
973
- e->opcode = UR_OP_SEND;
974
982
  e->fd = (int32_t)slot;
975
983
  e->flags = UR_SQE_FIXED_FILE;
976
- e->addr = (uint64_t)(uintptr_t)(q->data + q->off);
977
- e->len = q->len - q->off;
978
- e->op_flags = MSG_NOSIGNAL;
984
+ if (q->how >= 0) {
985
+ e->opcode = UR_OP_SHUTDOWN;
986
+ e->len = (uint32_t)q->how;
987
+ } else {
988
+ e->opcode = UR_OP_SEND;
989
+ e->addr = (uint64_t)(uintptr_t)(q->data + q->off);
990
+ e->len = q->len - q->off;
991
+ e->op_flags = MSG_NOSIGNAL;
992
+ }
979
993
  e->user_data = (uint64_t)(uintptr_t)q | UR_SEND;
980
994
  q->in_flight = 1;
981
995
  }
@@ -1033,6 +1047,11 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
1033
1047
  ur_unlink_all(s, q);
1034
1048
  return 0;
1035
1049
  }
1050
+ if (q->how >= 0) {
1051
+ /* A shutdown's answer is not a send's failure: -ENOTCONN from a peer
1052
+ * already gone says nothing a recv will not, so it is not reported. */
1053
+ res = 0;
1054
+ }
1036
1055
  if (res > 0) r->qbytes[slot] -= (uint32_t)res < r->qbytes[slot] ? (uint32_t)res : r->qbytes[slot];
1037
1056
  if (res > 0 && q->off + (uint32_t)res < q->len) {
1038
1057
  q->off += (uint32_t)res;
@@ -1388,6 +1407,23 @@ static struct ur_ring *ur_open_slot(struct nish_uring_set *s, int32_t ring, int3
1388
1407
  return (r->st[slot] & ST_OPEN) ? r : NULL;
1389
1408
  }
1390
1409
 
1410
+ /* Put `q` on the set's list of records and behind the slot's queue, and
1411
+ * submit it if it is now the head. */
1412
+ static void ur_enqueue(struct nish_uring_set *s, struct ur_ring *r, uint32_t slot, struct ur_send *q) {
1413
+ q->next = NULL;
1414
+ q->prev_all = NULL;
1415
+ q->next_all = s->all;
1416
+ if (s->all) s->all->prev_all = q;
1417
+ s->all = q;
1418
+ if (r->sq_last[slot]) {
1419
+ r->sq_last[slot]->next = q;
1420
+ } else {
1421
+ r->sq_first[slot] = q;
1422
+ }
1423
+ r->sq_last[slot] = q;
1424
+ ur_send_head(r, slot, s);
1425
+ }
1426
+
1391
1427
  /* Queue `n` bytes for (ring, slot). They are copied, so the caller's buffer
1392
1428
  * is free on return; a failure comes back later as a SEND_FAILED event. */
1393
1429
  int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n) {
@@ -1398,34 +1434,49 @@ int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, co
1398
1434
  struct ur_send *q = malloc(sizeof *q + (size_t)n);
1399
1435
  if (!q) return -ENOMEM;
1400
1436
  memcpy(q->data, p, (size_t)n);
1401
- q->next = NULL;
1402
- q->prev_all = NULL;
1403
- q->next_all = s->all;
1404
- if (s->all) s->all->prev_all = q;
1405
- s->all = q;
1406
1437
  q->slot = (uint32_t)slot;
1407
1438
  q->gen = ST_GEN(r->st[slot]);
1408
1439
  q->off = 0;
1409
1440
  q->len = (uint32_t)n;
1410
1441
  q->in_flight = 0;
1442
+ q->how = -1;
1411
1443
  r->qbytes[slot] += (uint32_t)n;
1412
- if (r->sq_last[slot]) {
1413
- r->sq_last[slot]->next = q;
1414
- } else {
1415
- r->sq_first[slot] = q;
1416
- }
1417
- r->sq_last[slot] = q;
1418
- ur_send_head(r, (uint32_t)slot, s);
1444
+ ur_enqueue(s, r, (uint32_t)slot, q);
1419
1445
  return n;
1420
1446
  }
1421
1447
 
1448
+ /* Queue a shutdown of (ring, slot)'s `how` side (0 read, 1 write, 2 both)
1449
+ * behind the bytes already queued for it, so they reach the peer first. Its
1450
+ * answer is not reported: a shutdown that fails, as on a peer already gone,
1451
+ * leaves a connection whose next read or write says so. */
1452
+ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how) {
1453
+ struct ur_ring *r = ur_open_slot(s, ring, slot);
1454
+ if (!r) return -EBADF;
1455
+ if ((uint32_t)how > 2) return -EINVAL;
1456
+ struct ur_send *q = malloc(sizeof *q);
1457
+ if (!q) return -ENOMEM;
1458
+ q->slot = (uint32_t)slot;
1459
+ q->gen = ST_GEN(r->st[slot]);
1460
+ q->off = q->len = 0;
1461
+ q->in_flight = 0;
1462
+ q->how = how;
1463
+ ur_enqueue(s, r, (uint32_t)slot, q);
1464
+ return 0;
1465
+ }
1466
+
1422
1467
  /* Cancel the slot's recv, close the slot, and move its generation on, so
1423
1468
  * every completion still on its way for it is stale. The freed slot goes to
1424
- * the accepting job. -EAGAIN when the SQ has no room for both entries, with
1425
- * nothing changed. */
1469
+ * the accepting job. -EAGAIN, with nothing changed, when the SQ has no room
1470
+ * for both entries or while the slot's shutdown is in flight: a SHUTDOWN runs
1471
+ * on io-wq, which looks the slot's file up only when it runs, and the cancel
1472
+ * below cannot match a request that has no file yet, so a slot freed before
1473
+ * it ran could take a new connection that the shutdown then reaches
1474
+ * (NURING-39). */
1426
1475
  int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
1427
1476
  struct ur_ring *r = ur_open_slot(s, ring, slot);
1428
1477
  if (!r) return -EBADF;
1478
+ const struct ur_send *h = r->sq_first[slot];
1479
+ if (h && h->in_flight && h->how >= 0) return -EAGAIN;
1429
1480
  uint32_t head = __atomic_load_n(r->sq_head, __ATOMIC_ACQUIRE);
1430
1481
  if (r->sq_local_tail - head + 2 > r->sq_entries) {
1431
1482
  (void)ur_flush(r);
@@ -1566,6 +1617,13 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
1566
1617
  (void)slot;
1567
1618
  return -38;
1568
1619
  }
1620
+ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how) {
1621
+ (void)s;
1622
+ (void)ring;
1623
+ (void)slot;
1624
+ (void)how;
1625
+ return -38;
1626
+ }
1569
1627
  int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot) {
1570
1628
  (void)s;
1571
1629
  (void)ring;
@@ -78,6 +78,9 @@ extern unsigned char __heap_base;
78
78
  * past 2^32 truncated to the `uintptr_t` `memory.grow` takes: a request of
79
79
  * 2^48 bytes grew nothing, claimed 2^48 bytes of capacity, and the next block
80
80
  * landed at `buf + off` reduced mod 2^32, on top of live data. */
81
+ /* No compact-class range here (`NISH_ARENA_RANGE`, runtime.c): a wasm
82
+ module's memory is one 32-bit space already, so this allocator and its
83
+ bump are unchanged and build.sh never passes the define to this file. */
81
84
  void *nish_arena_grow(uint64_t size) {
82
85
  if (!nish_arena.buf) {
83
86
  uintptr_t base = ((uintptr_t)&__heap_base + 7) & ~(uintptr_t)7;